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 | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4145 | } |
| 4146 | ctxt->input->cur = in; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4147 | if (*in == 0xD) { |
| 4148 | in++; |
| 4149 | if (*in == 0xA) { |
| 4150 | ctxt->input->cur = in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4151 | in++; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 4152 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4153 | continue; /* while */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4154 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 4155 | in--; |
| 4156 | } |
| 4157 | if (*in == '<') { |
| 4158 | return; |
| 4159 | } |
| 4160 | if (*in == '&') { |
| 4161 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4162 | } |
| 4163 | SHRINK; |
| 4164 | GROW; |
| 4165 | in = ctxt->input->cur; |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 4166 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4167 | nbchar = 0; |
| 4168 | } |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 4169 | ctxt->input->line = line; |
| 4170 | ctxt->input->col = col; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4171 | xmlParseCharDataComplex(ctxt, cdata); |
| 4172 | } |
| 4173 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4174 | /** |
| 4175 | * xmlParseCharDataComplex: |
| 4176 | * @ctxt: an XML parser context |
| 4177 | * @cdata: int indicating whether we are within a CDATA section |
| 4178 | * |
| 4179 | * parse a CharData section.this is the fallback function |
| 4180 | * of xmlParseCharData() when the parsing requires handling |
| 4181 | * of non-ASCII characters. |
| 4182 | */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 4183 | void |
| 4184 | xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4185 | xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5]; |
| 4186 | int nbchar = 0; |
| 4187 | int cur, l; |
| 4188 | int count = 0; |
| 4189 | |
| 4190 | SHRINK; |
| 4191 | GROW; |
| 4192 | cur = CUR_CHAR(l); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4193 | while ((cur != '<') && /* checked */ |
| 4194 | (cur != '&') && |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4195 | (IS_CHAR(cur))) /* test also done in xmlCurrentChar() */ { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4196 | if ((cur == ']') && (NXT(1) == ']') && |
| 4197 | (NXT(2) == '>')) { |
| 4198 | if (cdata) break; |
| 4199 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4200 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4201 | } |
| 4202 | } |
| 4203 | COPY_BUF(l,buf,nbchar,cur); |
| 4204 | if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4205 | buf[nbchar] = 0; |
| 4206 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4207 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4208 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4209 | */ |
| 4210 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4211 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4212 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4213 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 4214 | buf, nbchar); |
| 4215 | } else { |
| 4216 | if (ctxt->sax->characters != NULL) |
| 4217 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4218 | if ((ctxt->sax->characters != |
| 4219 | ctxt->sax->ignorableWhitespace) && |
| 4220 | (*ctxt->space == -1)) |
| 4221 | *ctxt->space = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4222 | } |
| 4223 | } |
| 4224 | nbchar = 0; |
| 4225 | } |
| 4226 | count++; |
| 4227 | if (count > 50) { |
| 4228 | GROW; |
| 4229 | count = 0; |
| 4230 | } |
| 4231 | NEXTL(l); |
| 4232 | cur = CUR_CHAR(l); |
| 4233 | } |
| 4234 | if (nbchar != 0) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 4235 | buf[nbchar] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4236 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4237 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4238 | */ |
| 4239 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 4240 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4241 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 4242 | ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar); |
| 4243 | } else { |
| 4244 | if (ctxt->sax->characters != NULL) |
| 4245 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 4246 | if ((ctxt->sax->characters != ctxt->sax->ignorableWhitespace) && |
| 4247 | (*ctxt->space == -1)) |
| 4248 | *ctxt->space = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4249 | } |
| 4250 | } |
| 4251 | } |
Daniel Veillard | 3b1478b | 2006-04-24 08:50:10 +0000 | [diff] [blame] | 4252 | if ((cur != 0) && (!IS_CHAR(cur))) { |
| 4253 | /* Generate the error and skip the offending character */ |
| 4254 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4255 | "PCDATA invalid Char value %d\n", |
| 4256 | cur); |
| 4257 | NEXTL(l); |
| 4258 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4259 | } |
| 4260 | |
| 4261 | /** |
| 4262 | * xmlParseExternalID: |
| 4263 | * @ctxt: an XML parser context |
| 4264 | * @publicID: a xmlChar** receiving PubidLiteral |
| 4265 | * @strict: indicate whether we should restrict parsing to only |
| 4266 | * production [75], see NOTE below |
| 4267 | * |
| 4268 | * Parse an External ID or a Public ID |
| 4269 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4270 | * NOTE: Productions [75] and [83] interact badly since [75] can generate |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4271 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 4272 | * |
| 4273 | * [75] ExternalID ::= 'SYSTEM' S SystemLiteral |
| 4274 | * | 'PUBLIC' S PubidLiteral S SystemLiteral |
| 4275 | * |
| 4276 | * [83] PublicID ::= 'PUBLIC' S PubidLiteral |
| 4277 | * |
| 4278 | * Returns the function returns SystemLiteral and in the second |
| 4279 | * case publicID receives PubidLiteral, is strict is off |
| 4280 | * it is possible to return NULL and have publicID set. |
| 4281 | */ |
| 4282 | |
| 4283 | xmlChar * |
| 4284 | xmlParseExternalID(xmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) { |
| 4285 | xmlChar *URI = NULL; |
| 4286 | |
| 4287 | SHRINK; |
Daniel Veillard | 146c912 | 2001-03-22 15:22:27 +0000 | [diff] [blame] | 4288 | |
| 4289 | *publicID = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4290 | if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4291 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4292 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4293 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4294 | "Space required after 'SYSTEM'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4295 | } |
| 4296 | SKIP_BLANKS; |
| 4297 | URI = xmlParseSystemLiteral(ctxt); |
| 4298 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4299 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4300 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4301 | } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4302 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4303 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4304 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4305 | "Space required after 'PUBLIC'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4306 | } |
| 4307 | SKIP_BLANKS; |
| 4308 | *publicID = xmlParsePubidLiteral(ctxt); |
| 4309 | if (*publicID == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4310 | xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4311 | } |
| 4312 | if (strict) { |
| 4313 | /* |
| 4314 | * We don't handle [83] so "S SystemLiteral" is required. |
| 4315 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4316 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4317 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4318 | "Space required after the Public Identifier\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4319 | } |
| 4320 | } else { |
| 4321 | /* |
| 4322 | * We handle [83] so we return immediately, if |
| 4323 | * "S SystemLiteral" is not detected. From a purely parsing |
| 4324 | * point of view that's a nice mess. |
| 4325 | */ |
| 4326 | const xmlChar *ptr; |
| 4327 | GROW; |
| 4328 | |
| 4329 | ptr = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4330 | if (!IS_BLANK_CH(*ptr)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4331 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4332 | while (IS_BLANK_CH(*ptr)) ptr++; /* TODO: dangerous, fix ! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4333 | if ((*ptr != '\'') && (*ptr != '"')) return(NULL); |
| 4334 | } |
| 4335 | SKIP_BLANKS; |
| 4336 | URI = xmlParseSystemLiteral(ctxt); |
| 4337 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4338 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4339 | } |
| 4340 | } |
| 4341 | return(URI); |
| 4342 | } |
| 4343 | |
| 4344 | /** |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4345 | * xmlParseCommentComplex: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4346 | * @ctxt: an XML parser context |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4347 | * @buf: the already parsed part of the buffer |
| 4348 | * @len: number of bytes filles in the buffer |
| 4349 | * @size: allocated size of the buffer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4350 | * |
| 4351 | * Skip an XML (SGML) comment <!-- .... --> |
| 4352 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 4353 | * must not occur within comments. " |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4354 | * 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] | 4355 | * |
| 4356 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 4357 | */ |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4358 | static void |
| 4359 | xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf, int len, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4360 | int q, ql; |
| 4361 | int r, rl; |
| 4362 | int cur, l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4363 | int count = 0; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4364 | int inputid; |
| 4365 | |
| 4366 | inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4367 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4368 | if (buf == NULL) { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4369 | len = 0; |
| 4370 | size = XML_PARSER_BUFFER_SIZE; |
| 4371 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 4372 | if (buf == NULL) { |
| 4373 | xmlErrMemory(ctxt, NULL); |
| 4374 | return; |
| 4375 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4376 | } |
William M. Brack | bf9a73d | 2007-02-09 00:07:07 +0000 | [diff] [blame] | 4377 | GROW; /* Assure there's enough input data */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4378 | q = CUR_CHAR(ql); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4379 | if (q == 0) |
| 4380 | goto not_terminated; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4381 | if (!IS_CHAR(q)) { |
| 4382 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4383 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4384 | q); |
| 4385 | xmlFree (buf); |
| 4386 | return; |
| 4387 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4388 | NEXTL(ql); |
| 4389 | r = CUR_CHAR(rl); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4390 | if (r == 0) |
| 4391 | goto not_terminated; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4392 | if (!IS_CHAR(r)) { |
| 4393 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4394 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4395 | q); |
| 4396 | xmlFree (buf); |
| 4397 | return; |
| 4398 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4399 | NEXTL(rl); |
| 4400 | cur = CUR_CHAR(l); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4401 | if (cur == 0) |
| 4402 | goto not_terminated; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4403 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4404 | ((cur != '>') || |
| 4405 | (r != '-') || (q != '-'))) { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 4406 | if ((r == '-') && (q == '-')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4407 | xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4408 | } |
| 4409 | if (len + 5 >= size) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4410 | xmlChar *new_buf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4411 | size *= 2; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4412 | new_buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 4413 | if (new_buf == NULL) { |
| 4414 | xmlFree (buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4415 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4416 | return; |
| 4417 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 4418 | buf = new_buf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4419 | } |
| 4420 | COPY_BUF(ql,buf,len,q); |
| 4421 | q = r; |
| 4422 | ql = rl; |
| 4423 | r = cur; |
| 4424 | rl = l; |
| 4425 | |
| 4426 | count++; |
| 4427 | if (count > 50) { |
| 4428 | GROW; |
| 4429 | count = 0; |
| 4430 | } |
| 4431 | NEXTL(l); |
| 4432 | cur = CUR_CHAR(l); |
| 4433 | if (cur == 0) { |
| 4434 | SHRINK; |
| 4435 | GROW; |
| 4436 | cur = CUR_CHAR(l); |
| 4437 | } |
| 4438 | } |
| 4439 | buf[len] = 0; |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4440 | if (cur == 0) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 4441 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4442 | "Comment not terminated \n<!--%.50s\n", buf); |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4443 | } else if (!IS_CHAR(cur)) { |
| 4444 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 4445 | "xmlParseComment: invalid xmlChar value %d\n", |
| 4446 | cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4447 | } else { |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4448 | if (inputid != ctxt->input->id) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4449 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4450 | "Comment doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4451 | } |
| 4452 | NEXT; |
| 4453 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 4454 | (!ctxt->disableSAX)) |
| 4455 | ctxt->sax->comment(ctxt->userData, buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4456 | } |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4457 | xmlFree(buf); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4458 | return; |
| 4459 | not_terminated: |
| 4460 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4461 | "Comment not terminated\n", NULL); |
| 4462 | xmlFree(buf); |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4463 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4464 | } |
Daniel Veillard | da62934 | 2007-08-01 07:49:06 +0000 | [diff] [blame] | 4465 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4466 | /** |
| 4467 | * xmlParseComment: |
| 4468 | * @ctxt: an XML parser context |
| 4469 | * |
| 4470 | * Skip an XML (SGML) comment <!-- .... --> |
| 4471 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 4472 | * must not occur within comments. " |
| 4473 | * |
| 4474 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 4475 | */ |
| 4476 | void |
| 4477 | xmlParseComment(xmlParserCtxtPtr ctxt) { |
| 4478 | xmlChar *buf = NULL; |
| 4479 | int size = XML_PARSER_BUFFER_SIZE; |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 4480 | int len = 0; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4481 | xmlParserInputState state; |
| 4482 | const xmlChar *in; |
| 4483 | int nbchar = 0, ccol; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4484 | int inputid; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4485 | |
| 4486 | /* |
| 4487 | * Check that there is a comment right here. |
| 4488 | */ |
| 4489 | if ((RAW != '<') || (NXT(1) != '!') || |
| 4490 | (NXT(2) != '-') || (NXT(3) != '-')) return; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4491 | state = ctxt->instate; |
| 4492 | ctxt->instate = XML_PARSER_COMMENT; |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4493 | inputid = ctxt->input->id; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4494 | SKIP(4); |
| 4495 | SHRINK; |
| 4496 | GROW; |
| 4497 | |
| 4498 | /* |
| 4499 | * Accelerated common case where input don't need to be |
| 4500 | * modified before passing it to the handler. |
| 4501 | */ |
| 4502 | in = ctxt->input->cur; |
| 4503 | do { |
| 4504 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4505 | do { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4506 | ctxt->input->line++; ctxt->input->col = 1; |
| 4507 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4508 | } while (*in == 0xA); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4509 | } |
| 4510 | get_more: |
| 4511 | ccol = ctxt->input->col; |
| 4512 | while (((*in > '-') && (*in <= 0x7F)) || |
| 4513 | ((*in >= 0x20) && (*in < '-')) || |
| 4514 | (*in == 0x09)) { |
| 4515 | in++; |
| 4516 | ccol++; |
| 4517 | } |
| 4518 | ctxt->input->col = ccol; |
| 4519 | if (*in == 0xA) { |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4520 | do { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4521 | ctxt->input->line++; ctxt->input->col = 1; |
| 4522 | in++; |
Daniel Veillard | b20c63a | 2006-01-04 17:08:46 +0000 | [diff] [blame] | 4523 | } while (*in == 0xA); |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4524 | goto get_more; |
| 4525 | } |
| 4526 | nbchar = in - ctxt->input->cur; |
| 4527 | /* |
| 4528 | * save current set of data |
| 4529 | */ |
| 4530 | if (nbchar > 0) { |
| 4531 | if ((ctxt->sax != NULL) && |
| 4532 | (ctxt->sax->comment != NULL)) { |
| 4533 | if (buf == NULL) { |
| 4534 | if ((*in == '-') && (in[1] == '-')) |
| 4535 | size = nbchar + 1; |
| 4536 | else |
| 4537 | size = XML_PARSER_BUFFER_SIZE + nbchar; |
| 4538 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 4539 | if (buf == NULL) { |
| 4540 | xmlErrMemory(ctxt, NULL); |
| 4541 | ctxt->instate = state; |
| 4542 | return; |
| 4543 | } |
| 4544 | len = 0; |
| 4545 | } else if (len + nbchar + 1 >= size) { |
| 4546 | xmlChar *new_buf; |
| 4547 | size += len + nbchar + XML_PARSER_BUFFER_SIZE; |
| 4548 | new_buf = (xmlChar *) xmlRealloc(buf, |
| 4549 | size * sizeof(xmlChar)); |
| 4550 | if (new_buf == NULL) { |
| 4551 | xmlFree (buf); |
| 4552 | xmlErrMemory(ctxt, NULL); |
| 4553 | ctxt->instate = state; |
| 4554 | return; |
| 4555 | } |
| 4556 | buf = new_buf; |
| 4557 | } |
| 4558 | memcpy(&buf[len], ctxt->input->cur, nbchar); |
| 4559 | len += nbchar; |
| 4560 | buf[len] = 0; |
| 4561 | } |
| 4562 | } |
| 4563 | ctxt->input->cur = in; |
Daniel Veillard | 9b528c7 | 2006-02-05 03:06:15 +0000 | [diff] [blame] | 4564 | if (*in == 0xA) { |
| 4565 | in++; |
| 4566 | ctxt->input->line++; ctxt->input->col = 1; |
| 4567 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4568 | if (*in == 0xD) { |
| 4569 | in++; |
| 4570 | if (*in == 0xA) { |
| 4571 | ctxt->input->cur = in; |
| 4572 | in++; |
| 4573 | ctxt->input->line++; ctxt->input->col = 1; |
| 4574 | continue; /* while */ |
| 4575 | } |
| 4576 | in--; |
| 4577 | } |
| 4578 | SHRINK; |
| 4579 | GROW; |
| 4580 | in = ctxt->input->cur; |
| 4581 | if (*in == '-') { |
| 4582 | if (in[1] == '-') { |
| 4583 | if (in[2] == '>') { |
Daniel Veillard | 051d52c | 2008-07-29 16:44:59 +0000 | [diff] [blame] | 4584 | if (ctxt->input->id != inputid) { |
| 4585 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4586 | "comment doesn't start and stop in the same entity\n"); |
| 4587 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4588 | SKIP(3); |
| 4589 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 4590 | (!ctxt->disableSAX)) { |
| 4591 | if (buf != NULL) |
| 4592 | ctxt->sax->comment(ctxt->userData, buf); |
| 4593 | else |
| 4594 | ctxt->sax->comment(ctxt->userData, BAD_CAST ""); |
| 4595 | } |
| 4596 | if (buf != NULL) |
| 4597 | xmlFree(buf); |
| 4598 | ctxt->instate = state; |
| 4599 | return; |
| 4600 | } |
| 4601 | if (buf != NULL) |
| 4602 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4603 | "Comment not terminated \n<!--%.50s\n", |
| 4604 | buf); |
| 4605 | else |
| 4606 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 4607 | "Comment not terminated \n", NULL); |
| 4608 | in++; |
| 4609 | ctxt->input->col++; |
| 4610 | } |
| 4611 | in++; |
| 4612 | ctxt->input->col++; |
| 4613 | goto get_more; |
| 4614 | } |
| 4615 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
| 4616 | xmlParseCommentComplex(ctxt, buf, len, size); |
| 4617 | ctxt->instate = state; |
| 4618 | return; |
| 4619 | } |
| 4620 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4621 | |
| 4622 | /** |
| 4623 | * xmlParsePITarget: |
| 4624 | * @ctxt: an XML parser context |
| 4625 | * |
| 4626 | * parse the name of a PI |
| 4627 | * |
| 4628 | * [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l')) |
| 4629 | * |
| 4630 | * Returns the PITarget name or NULL |
| 4631 | */ |
| 4632 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4633 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4634 | xmlParsePITarget(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4635 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4636 | |
| 4637 | name = xmlParseName(ctxt); |
| 4638 | if ((name != NULL) && |
| 4639 | ((name[0] == 'x') || (name[0] == 'X')) && |
| 4640 | ((name[1] == 'm') || (name[1] == 'M')) && |
| 4641 | ((name[2] == 'l') || (name[2] == 'L'))) { |
| 4642 | int i; |
| 4643 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 4644 | (name[2] == 'l') && (name[3] == 0)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4645 | xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4646 | "XML declaration allowed only at the start of the document\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4647 | return(name); |
| 4648 | } else if (name[3] == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4649 | xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4650 | return(name); |
| 4651 | } |
| 4652 | for (i = 0;;i++) { |
| 4653 | if (xmlW3CPIs[i] == NULL) break; |
| 4654 | if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i])) |
| 4655 | return(name); |
| 4656 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 4657 | xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
| 4658 | "xmlParsePITarget: invalid name prefix 'xml'\n", |
| 4659 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4660 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 4661 | if ((name != NULL) && (xmlStrchr(name, ':') != NULL)) { |
| 4662 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
| 4663 | "colon are forbidden from PI names '%s'\n", name, NULL, NULL); |
| 4664 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4665 | return(name); |
| 4666 | } |
| 4667 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4668 | #ifdef LIBXML_CATALOG_ENABLED |
| 4669 | /** |
| 4670 | * xmlParseCatalogPI: |
| 4671 | * @ctxt: an XML parser context |
| 4672 | * @catalog: the PI value string |
| 4673 | * |
| 4674 | * parse an XML Catalog Processing Instruction. |
| 4675 | * |
| 4676 | * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?> |
| 4677 | * |
| 4678 | * Occurs only if allowed by the user and if happening in the Misc |
| 4679 | * part of the document before any doctype informations |
| 4680 | * This will add the given catalog to the parsing context in order |
| 4681 | * to be used if there is a resolution need further down in the document |
| 4682 | */ |
| 4683 | |
| 4684 | static void |
| 4685 | xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) { |
| 4686 | xmlChar *URL = NULL; |
| 4687 | const xmlChar *tmp, *base; |
| 4688 | xmlChar marker; |
| 4689 | |
| 4690 | tmp = catalog; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4691 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4692 | if (xmlStrncmp(tmp, BAD_CAST"catalog", 7)) |
| 4693 | goto error; |
| 4694 | tmp += 7; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4695 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4696 | if (*tmp != '=') { |
| 4697 | return; |
| 4698 | } |
| 4699 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4700 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4701 | marker = *tmp; |
| 4702 | if ((marker != '\'') && (marker != '"')) |
| 4703 | goto error; |
| 4704 | tmp++; |
| 4705 | base = tmp; |
| 4706 | while ((*tmp != 0) && (*tmp != marker)) tmp++; |
| 4707 | if (*tmp == 0) |
| 4708 | goto error; |
| 4709 | URL = xmlStrndup(base, tmp - base); |
| 4710 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4711 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4712 | if (*tmp != 0) |
| 4713 | goto error; |
| 4714 | |
| 4715 | if (URL != NULL) { |
| 4716 | ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL); |
| 4717 | xmlFree(URL); |
| 4718 | } |
| 4719 | return; |
| 4720 | |
| 4721 | error: |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 4722 | xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI, |
| 4723 | "Catalog PI syntax error: %s\n", |
| 4724 | catalog, NULL); |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4725 | if (URL != NULL) |
| 4726 | xmlFree(URL); |
| 4727 | } |
| 4728 | #endif |
| 4729 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4730 | /** |
| 4731 | * xmlParsePI: |
| 4732 | * @ctxt: an XML parser context |
| 4733 | * |
| 4734 | * parse an XML Processing Instruction. |
| 4735 | * |
| 4736 | * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>' |
| 4737 | * |
| 4738 | * The processing is transfered to SAX once parsed. |
| 4739 | */ |
| 4740 | |
| 4741 | void |
| 4742 | xmlParsePI(xmlParserCtxtPtr ctxt) { |
| 4743 | xmlChar *buf = NULL; |
| 4744 | int len = 0; |
| 4745 | int size = XML_PARSER_BUFFER_SIZE; |
| 4746 | int cur, l; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4747 | const xmlChar *target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4748 | xmlParserInputState state; |
| 4749 | int count = 0; |
| 4750 | |
| 4751 | if ((RAW == '<') && (NXT(1) == '?')) { |
| 4752 | xmlParserInputPtr input = ctxt->input; |
| 4753 | state = ctxt->instate; |
| 4754 | ctxt->instate = XML_PARSER_PI; |
| 4755 | /* |
| 4756 | * this is a Processing Instruction. |
| 4757 | */ |
| 4758 | SKIP(2); |
| 4759 | SHRINK; |
| 4760 | |
| 4761 | /* |
| 4762 | * Parse the target name and check for special support like |
| 4763 | * namespace. |
| 4764 | */ |
| 4765 | target = xmlParsePITarget(ctxt); |
| 4766 | if (target != NULL) { |
| 4767 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 4768 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4769 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4770 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4771 | } |
| 4772 | SKIP(2); |
| 4773 | |
| 4774 | /* |
| 4775 | * SAX: PI detected. |
| 4776 | */ |
| 4777 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 4778 | (ctxt->sax->processingInstruction != NULL)) |
| 4779 | ctxt->sax->processingInstruction(ctxt->userData, |
| 4780 | target, NULL); |
| 4781 | ctxt->instate = state; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4782 | return; |
| 4783 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4784 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4785 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4786 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4787 | ctxt->instate = state; |
| 4788 | return; |
| 4789 | } |
| 4790 | cur = CUR; |
| 4791 | if (!IS_BLANK(cur)) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 4792 | xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4793 | "ParsePI: PI %s space expected\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4794 | } |
| 4795 | SKIP_BLANKS; |
| 4796 | cur = CUR_CHAR(l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 4797 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4798 | ((cur != '?') || (NXT(1) != '>'))) { |
| 4799 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4800 | xmlChar *tmp; |
| 4801 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4802 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4803 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 4804 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4805 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4806 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4807 | ctxt->instate = state; |
| 4808 | return; |
| 4809 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 4810 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4811 | } |
| 4812 | count++; |
| 4813 | if (count > 50) { |
| 4814 | GROW; |
| 4815 | count = 0; |
| 4816 | } |
| 4817 | COPY_BUF(l,buf,len,cur); |
| 4818 | NEXTL(l); |
| 4819 | cur = CUR_CHAR(l); |
| 4820 | if (cur == 0) { |
| 4821 | SHRINK; |
| 4822 | GROW; |
| 4823 | cur = CUR_CHAR(l); |
| 4824 | } |
| 4825 | } |
| 4826 | buf[len] = 0; |
| 4827 | if (cur != '?') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 4828 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 4829 | "ParsePI: PI %s never end ...\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4830 | } else { |
| 4831 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4832 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4833 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4834 | } |
| 4835 | SKIP(2); |
| 4836 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 4837 | #ifdef LIBXML_CATALOG_ENABLED |
| 4838 | if (((state == XML_PARSER_MISC) || |
| 4839 | (state == XML_PARSER_START)) && |
| 4840 | (xmlStrEqual(target, XML_CATALOG_PI))) { |
| 4841 | xmlCatalogAllow allow = xmlCatalogGetDefaults(); |
| 4842 | if ((allow == XML_CATA_ALLOW_DOCUMENT) || |
| 4843 | (allow == XML_CATA_ALLOW_ALL)) |
| 4844 | xmlParseCatalogPI(ctxt, buf); |
| 4845 | } |
| 4846 | #endif |
| 4847 | |
| 4848 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4849 | /* |
| 4850 | * SAX: PI detected. |
| 4851 | */ |
| 4852 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 4853 | (ctxt->sax->processingInstruction != NULL)) |
| 4854 | ctxt->sax->processingInstruction(ctxt->userData, |
| 4855 | target, buf); |
| 4856 | } |
| 4857 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4858 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4859 | xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4860 | } |
| 4861 | ctxt->instate = state; |
| 4862 | } |
| 4863 | } |
| 4864 | |
| 4865 | /** |
| 4866 | * xmlParseNotationDecl: |
| 4867 | * @ctxt: an XML parser context |
| 4868 | * |
| 4869 | * parse a notation declaration |
| 4870 | * |
| 4871 | * [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>' |
| 4872 | * |
| 4873 | * Hence there is actually 3 choices: |
| 4874 | * 'PUBLIC' S PubidLiteral |
| 4875 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 4876 | * and 'SYSTEM' S SystemLiteral |
| 4877 | * |
| 4878 | * See the NOTE on xmlParseExternalID(). |
| 4879 | */ |
| 4880 | |
| 4881 | void |
| 4882 | xmlParseNotationDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4883 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4884 | xmlChar *Pubid; |
| 4885 | xmlChar *Systemid; |
| 4886 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4887 | if (CMP10(CUR_PTR, '<', '!', 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4888 | xmlParserInputPtr input = ctxt->input; |
| 4889 | SHRINK; |
| 4890 | SKIP(10); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4891 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4892 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4893 | "Space required after '<!NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4894 | return; |
| 4895 | } |
| 4896 | SKIP_BLANKS; |
| 4897 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4898 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4899 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4900 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4901 | return; |
| 4902 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4903 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4904 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4905 | "Space required after the NOTATION name'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4906 | return; |
| 4907 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 4908 | if (xmlStrchr(name, ':') != NULL) { |
| 4909 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
| 4910 | "colon are forbidden from notation names '%s'\n", |
| 4911 | name, NULL, NULL); |
| 4912 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4913 | SKIP_BLANKS; |
| 4914 | |
| 4915 | /* |
| 4916 | * Parse the IDs. |
| 4917 | */ |
| 4918 | Systemid = xmlParseExternalID(ctxt, &Pubid, 0); |
| 4919 | SKIP_BLANKS; |
| 4920 | |
| 4921 | if (RAW == '>') { |
| 4922 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4923 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4924 | "Notation declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4925 | } |
| 4926 | NEXT; |
| 4927 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 4928 | (ctxt->sax->notationDecl != NULL)) |
| 4929 | ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid); |
| 4930 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4931 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4932 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4933 | if (Systemid != NULL) xmlFree(Systemid); |
| 4934 | if (Pubid != NULL) xmlFree(Pubid); |
| 4935 | } |
| 4936 | } |
| 4937 | |
| 4938 | /** |
| 4939 | * xmlParseEntityDecl: |
| 4940 | * @ctxt: an XML parser context |
| 4941 | * |
| 4942 | * parse <!ENTITY declarations |
| 4943 | * |
| 4944 | * [70] EntityDecl ::= GEDecl | PEDecl |
| 4945 | * |
| 4946 | * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>' |
| 4947 | * |
| 4948 | * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>' |
| 4949 | * |
| 4950 | * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?) |
| 4951 | * |
| 4952 | * [74] PEDef ::= EntityValue | ExternalID |
| 4953 | * |
| 4954 | * [76] NDataDecl ::= S 'NDATA' S Name |
| 4955 | * |
| 4956 | * [ VC: Notation Declared ] |
| 4957 | * The Name must match the declared name of a notation. |
| 4958 | */ |
| 4959 | |
| 4960 | void |
| 4961 | xmlParseEntityDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4962 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4963 | xmlChar *value = NULL; |
| 4964 | xmlChar *URI = NULL, *literal = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4965 | const xmlChar *ndata = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4966 | int isParameter = 0; |
| 4967 | xmlChar *orig = NULL; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 4968 | int skipped; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4969 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4970 | /* GROW; done in the caller */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4971 | if (CMP8(CUR_PTR, '<', '!', 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4972 | xmlParserInputPtr input = ctxt->input; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4973 | SHRINK; |
| 4974 | SKIP(8); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 4975 | skipped = SKIP_BLANKS; |
| 4976 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4977 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4978 | "Space required after '<!ENTITY'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4979 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4980 | |
| 4981 | if (RAW == '%') { |
| 4982 | NEXT; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 4983 | skipped = SKIP_BLANKS; |
| 4984 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4985 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4986 | "Space required after '%'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4987 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4988 | isParameter = 1; |
| 4989 | } |
| 4990 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4991 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4992 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4993 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 4994 | "xmlParseEntityDecl: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4995 | return; |
| 4996 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 4997 | if (xmlStrchr(name, ':') != NULL) { |
| 4998 | xmlNsErr(ctxt, XML_NS_ERR_COLON, |
| 4999 | "colon are forbidden from entities names '%s'\n", |
| 5000 | name, NULL, NULL); |
| 5001 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5002 | skipped = SKIP_BLANKS; |
| 5003 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5004 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5005 | "Space required after the entity name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5006 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5007 | |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 5008 | ctxt->instate = XML_PARSER_ENTITY_DECL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5009 | /* |
| 5010 | * handle the various case of definitions... |
| 5011 | */ |
| 5012 | if (isParameter) { |
| 5013 | if ((RAW == '"') || (RAW == '\'')) { |
| 5014 | value = xmlParseEntityValue(ctxt, &orig); |
| 5015 | if (value) { |
| 5016 | if ((ctxt->sax != NULL) && |
| 5017 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5018 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5019 | XML_INTERNAL_PARAMETER_ENTITY, |
| 5020 | NULL, NULL, value); |
| 5021 | } |
| 5022 | } else { |
| 5023 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 5024 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5025 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5026 | } |
| 5027 | if (URI) { |
| 5028 | xmlURIPtr uri; |
| 5029 | |
| 5030 | uri = xmlParseURI((const char *) URI); |
| 5031 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5032 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 5033 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5034 | /* |
| 5035 | * This really ought to be a well formedness error |
| 5036 | * but the XML Core WG decided otherwise c.f. issue |
| 5037 | * E26 of the XML erratas. |
| 5038 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5039 | } else { |
| 5040 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5041 | /* |
| 5042 | * Okay this is foolish to block those but not |
| 5043 | * invalid URIs. |
| 5044 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5045 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5046 | } else { |
| 5047 | if ((ctxt->sax != NULL) && |
| 5048 | (!ctxt->disableSAX) && |
| 5049 | (ctxt->sax->entityDecl != NULL)) |
| 5050 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5051 | XML_EXTERNAL_PARAMETER_ENTITY, |
| 5052 | literal, URI, NULL); |
| 5053 | } |
| 5054 | xmlFreeURI(uri); |
| 5055 | } |
| 5056 | } |
| 5057 | } |
| 5058 | } else { |
| 5059 | if ((RAW == '"') || (RAW == '\'')) { |
| 5060 | value = xmlParseEntityValue(ctxt, &orig); |
| 5061 | if ((ctxt->sax != NULL) && |
| 5062 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5063 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5064 | XML_INTERNAL_GENERAL_ENTITY, |
| 5065 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5066 | /* |
| 5067 | * For expat compatibility in SAX mode. |
| 5068 | */ |
| 5069 | if ((ctxt->myDoc == NULL) || |
| 5070 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 5071 | if (ctxt->myDoc == NULL) { |
| 5072 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5073 | if (ctxt->myDoc == NULL) { |
| 5074 | xmlErrMemory(ctxt, "New Doc failed"); |
| 5075 | return; |
| 5076 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 5077 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5078 | } |
| 5079 | if (ctxt->myDoc->intSubset == NULL) |
| 5080 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 5081 | BAD_CAST "fake", NULL, NULL); |
| 5082 | |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5083 | xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY, |
| 5084 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5085 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5086 | } else { |
| 5087 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 5088 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5089 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5090 | } |
| 5091 | if (URI) { |
| 5092 | xmlURIPtr uri; |
| 5093 | |
| 5094 | uri = xmlParseURI((const char *)URI); |
| 5095 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5096 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 5097 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5098 | /* |
| 5099 | * This really ought to be a well formedness error |
| 5100 | * but the XML Core WG decided otherwise c.f. issue |
| 5101 | * E26 of the XML erratas. |
| 5102 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5103 | } else { |
| 5104 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 5105 | /* |
| 5106 | * Okay this is foolish to block those but not |
| 5107 | * invalid URIs. |
| 5108 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5109 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5110 | } |
| 5111 | xmlFreeURI(uri); |
| 5112 | } |
| 5113 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5114 | if ((RAW != '>') && (!IS_BLANK_CH(CUR))) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5115 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5116 | "Space required before 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5117 | } |
| 5118 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5119 | if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5120 | SKIP(5); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5121 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5122 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5123 | "Space required after 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5124 | } |
| 5125 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5126 | ndata = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5127 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 5128 | (ctxt->sax->unparsedEntityDecl != NULL)) |
| 5129 | ctxt->sax->unparsedEntityDecl(ctxt->userData, name, |
| 5130 | literal, URI, ndata); |
| 5131 | } else { |
| 5132 | if ((ctxt->sax != NULL) && |
| 5133 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 5134 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 5135 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 5136 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5137 | /* |
| 5138 | * For expat compatibility in SAX mode. |
| 5139 | * assuming the entity repalcement was asked for |
| 5140 | */ |
| 5141 | if ((ctxt->replaceEntities != 0) && |
| 5142 | ((ctxt->myDoc == NULL) || |
| 5143 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) { |
| 5144 | if (ctxt->myDoc == NULL) { |
| 5145 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5146 | if (ctxt->myDoc == NULL) { |
| 5147 | xmlErrMemory(ctxt, "New Doc failed"); |
| 5148 | return; |
| 5149 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 5150 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5151 | } |
| 5152 | |
| 5153 | if (ctxt->myDoc->intSubset == NULL) |
| 5154 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 5155 | BAD_CAST "fake", NULL, NULL); |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5156 | xmlSAX2EntityDecl(ctxt, name, |
| 5157 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 5158 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5159 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5160 | } |
| 5161 | } |
| 5162 | } |
| 5163 | SKIP_BLANKS; |
| 5164 | if (RAW != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5165 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5166 | "xmlParseEntityDecl: entity %s not terminated\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5167 | } else { |
| 5168 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5169 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5170 | "Entity declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5171 | } |
| 5172 | NEXT; |
| 5173 | } |
| 5174 | if (orig != NULL) { |
| 5175 | /* |
| 5176 | * Ugly mechanism to save the raw entity value. |
| 5177 | */ |
| 5178 | xmlEntityPtr cur = NULL; |
| 5179 | |
| 5180 | if (isParameter) { |
| 5181 | if ((ctxt->sax != NULL) && |
| 5182 | (ctxt->sax->getParameterEntity != NULL)) |
| 5183 | cur = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 5184 | } else { |
| 5185 | if ((ctxt->sax != NULL) && |
| 5186 | (ctxt->sax->getEntity != NULL)) |
| 5187 | cur = ctxt->sax->getEntity(ctxt->userData, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5188 | if ((cur == NULL) && (ctxt->userData==ctxt)) { |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5189 | cur = xmlSAX2GetEntity(ctxt, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5190 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5191 | } |
| 5192 | if (cur != NULL) { |
| 5193 | if (cur->orig != NULL) |
| 5194 | xmlFree(orig); |
| 5195 | else |
| 5196 | cur->orig = orig; |
| 5197 | } else |
| 5198 | xmlFree(orig); |
| 5199 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5200 | if (value != NULL) xmlFree(value); |
| 5201 | if (URI != NULL) xmlFree(URI); |
| 5202 | if (literal != NULL) xmlFree(literal); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5203 | } |
| 5204 | } |
| 5205 | |
| 5206 | /** |
| 5207 | * xmlParseDefaultDecl: |
| 5208 | * @ctxt: an XML parser context |
| 5209 | * @value: Receive a possible fixed default value for the attribute |
| 5210 | * |
| 5211 | * Parse an attribute default declaration |
| 5212 | * |
| 5213 | * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue) |
| 5214 | * |
| 5215 | * [ VC: Required Attribute ] |
| 5216 | * if the default declaration is the keyword #REQUIRED, then the |
| 5217 | * attribute must be specified for all elements of the type in the |
| 5218 | * attribute-list declaration. |
| 5219 | * |
| 5220 | * [ VC: Attribute Default Legal ] |
| 5221 | * The declared default value must meet the lexical constraints of |
| 5222 | * the declared attribute type c.f. xmlValidateAttributeDecl() |
| 5223 | * |
| 5224 | * [ VC: Fixed Attribute Default ] |
| 5225 | * if an attribute has a default value declared with the #FIXED |
| 5226 | * keyword, instances of that attribute must match the default value. |
| 5227 | * |
| 5228 | * [ WFC: No < in Attribute Values ] |
| 5229 | * handled in xmlParseAttValue() |
| 5230 | * |
| 5231 | * returns: XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED |
| 5232 | * or XML_ATTRIBUTE_FIXED. |
| 5233 | */ |
| 5234 | |
| 5235 | int |
| 5236 | xmlParseDefaultDecl(xmlParserCtxtPtr ctxt, xmlChar **value) { |
| 5237 | int val; |
| 5238 | xmlChar *ret; |
| 5239 | |
| 5240 | *value = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5241 | if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5242 | SKIP(9); |
| 5243 | return(XML_ATTRIBUTE_REQUIRED); |
| 5244 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5245 | if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5246 | SKIP(8); |
| 5247 | return(XML_ATTRIBUTE_IMPLIED); |
| 5248 | } |
| 5249 | val = XML_ATTRIBUTE_NONE; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5250 | if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5251 | SKIP(6); |
| 5252 | val = XML_ATTRIBUTE_FIXED; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5253 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5254 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5255 | "Space required after '#FIXED'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5256 | } |
| 5257 | SKIP_BLANKS; |
| 5258 | } |
| 5259 | ret = xmlParseAttValue(ctxt); |
| 5260 | ctxt->instate = XML_PARSER_DTD; |
| 5261 | if (ret == NULL) { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 5262 | xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5263 | "Attribute default value declaration error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5264 | } else |
| 5265 | *value = ret; |
| 5266 | return(val); |
| 5267 | } |
| 5268 | |
| 5269 | /** |
| 5270 | * xmlParseNotationType: |
| 5271 | * @ctxt: an XML parser context |
| 5272 | * |
| 5273 | * parse an Notation attribute type. |
| 5274 | * |
| 5275 | * Note: the leading 'NOTATION' S part has already being parsed... |
| 5276 | * |
| 5277 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 5278 | * |
| 5279 | * [ VC: Notation Attributes ] |
| 5280 | * Values of this type must match one of the notation names included |
| 5281 | * in the declaration; all notation names in the declaration must be declared. |
| 5282 | * |
| 5283 | * Returns: the notation attribute tree built while parsing |
| 5284 | */ |
| 5285 | |
| 5286 | xmlEnumerationPtr |
| 5287 | xmlParseNotationType(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5288 | const xmlChar *name; |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5289 | xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5290 | |
| 5291 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5292 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5293 | return(NULL); |
| 5294 | } |
| 5295 | SHRINK; |
| 5296 | do { |
| 5297 | NEXT; |
| 5298 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5299 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5300 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5301 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5302 | "Name expected in NOTATION declaration\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5303 | return(ret); |
| 5304 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5305 | tmp = ret; |
| 5306 | while (tmp != NULL) { |
| 5307 | if (xmlStrEqual(name, tmp->name)) { |
| 5308 | xmlValidityError(ctxt, XML_DTD_DUP_TOKEN, |
| 5309 | "standalone: attribute notation value token %s duplicated\n", |
| 5310 | name, NULL); |
| 5311 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5312 | xmlFree((xmlChar *) name); |
| 5313 | break; |
| 5314 | } |
| 5315 | tmp = tmp->next; |
| 5316 | } |
| 5317 | if (tmp == NULL) { |
| 5318 | cur = xmlCreateEnumeration(name); |
| 5319 | if (cur == NULL) return(ret); |
| 5320 | if (last == NULL) ret = last = cur; |
| 5321 | else { |
| 5322 | last->next = cur; |
| 5323 | last = cur; |
| 5324 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5325 | } |
| 5326 | SKIP_BLANKS; |
| 5327 | } while (RAW == '|'); |
| 5328 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5329 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5330 | if ((last != NULL) && (last != ret)) |
| 5331 | xmlFreeEnumeration(last); |
| 5332 | return(ret); |
| 5333 | } |
| 5334 | NEXT; |
| 5335 | return(ret); |
| 5336 | } |
| 5337 | |
| 5338 | /** |
| 5339 | * xmlParseEnumerationType: |
| 5340 | * @ctxt: an XML parser context |
| 5341 | * |
| 5342 | * parse an Enumeration attribute type. |
| 5343 | * |
| 5344 | * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')' |
| 5345 | * |
| 5346 | * [ VC: Enumeration ] |
| 5347 | * Values of this type must match one of the Nmtoken tokens in |
| 5348 | * the declaration |
| 5349 | * |
| 5350 | * Returns: the enumeration attribute tree built while parsing |
| 5351 | */ |
| 5352 | |
| 5353 | xmlEnumerationPtr |
| 5354 | xmlParseEnumerationType(xmlParserCtxtPtr ctxt) { |
| 5355 | xmlChar *name; |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5356 | xmlEnumerationPtr ret = NULL, last = NULL, cur, tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5357 | |
| 5358 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5359 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5360 | return(NULL); |
| 5361 | } |
| 5362 | SHRINK; |
| 5363 | do { |
| 5364 | NEXT; |
| 5365 | SKIP_BLANKS; |
| 5366 | name = xmlParseNmtoken(ctxt); |
| 5367 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5368 | xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5369 | return(ret); |
| 5370 | } |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5371 | tmp = ret; |
| 5372 | while (tmp != NULL) { |
| 5373 | if (xmlStrEqual(name, tmp->name)) { |
| 5374 | xmlValidityError(ctxt, XML_DTD_DUP_TOKEN, |
| 5375 | "standalone: attribute enumeration value token %s duplicated\n", |
| 5376 | name, NULL); |
| 5377 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5378 | xmlFree(name); |
| 5379 | break; |
| 5380 | } |
| 5381 | tmp = tmp->next; |
| 5382 | } |
| 5383 | if (tmp == NULL) { |
| 5384 | cur = xmlCreateEnumeration(name); |
| 5385 | if (!xmlDictOwns(ctxt->dict, name)) |
| 5386 | xmlFree(name); |
| 5387 | if (cur == NULL) return(ret); |
| 5388 | if (last == NULL) ret = last = cur; |
| 5389 | else { |
| 5390 | last->next = cur; |
| 5391 | last = cur; |
| 5392 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5393 | } |
| 5394 | SKIP_BLANKS; |
| 5395 | } while (RAW == '|'); |
| 5396 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5397 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5398 | return(ret); |
| 5399 | } |
| 5400 | NEXT; |
| 5401 | return(ret); |
| 5402 | } |
| 5403 | |
| 5404 | /** |
| 5405 | * xmlParseEnumeratedType: |
| 5406 | * @ctxt: an XML parser context |
| 5407 | * @tree: the enumeration tree built while parsing |
| 5408 | * |
| 5409 | * parse an Enumerated attribute type. |
| 5410 | * |
| 5411 | * [57] EnumeratedType ::= NotationType | Enumeration |
| 5412 | * |
| 5413 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 5414 | * |
| 5415 | * |
| 5416 | * Returns: XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION |
| 5417 | */ |
| 5418 | |
| 5419 | int |
| 5420 | xmlParseEnumeratedType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5421 | if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5422 | SKIP(8); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5423 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5424 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5425 | "Space required after 'NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5426 | return(0); |
| 5427 | } |
| 5428 | SKIP_BLANKS; |
| 5429 | *tree = xmlParseNotationType(ctxt); |
| 5430 | if (*tree == NULL) return(0); |
| 5431 | return(XML_ATTRIBUTE_NOTATION); |
| 5432 | } |
| 5433 | *tree = xmlParseEnumerationType(ctxt); |
| 5434 | if (*tree == NULL) return(0); |
| 5435 | return(XML_ATTRIBUTE_ENUMERATION); |
| 5436 | } |
| 5437 | |
| 5438 | /** |
| 5439 | * xmlParseAttributeType: |
| 5440 | * @ctxt: an XML parser context |
| 5441 | * @tree: the enumeration tree built while parsing |
| 5442 | * |
| 5443 | * parse the Attribute list def for an element |
| 5444 | * |
| 5445 | * [54] AttType ::= StringType | TokenizedType | EnumeratedType |
| 5446 | * |
| 5447 | * [55] StringType ::= 'CDATA' |
| 5448 | * |
| 5449 | * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' | |
| 5450 | * 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS' |
| 5451 | * |
| 5452 | * Validity constraints for attribute values syntax are checked in |
| 5453 | * xmlValidateAttributeValue() |
| 5454 | * |
| 5455 | * [ VC: ID ] |
| 5456 | * Values of type ID must match the Name production. A name must not |
| 5457 | * appear more than once in an XML document as a value of this type; |
| 5458 | * i.e., ID values must uniquely identify the elements which bear them. |
| 5459 | * |
| 5460 | * [ VC: One ID per Element Type ] |
| 5461 | * No element type may have more than one ID attribute specified. |
| 5462 | * |
| 5463 | * [ VC: ID Attribute Default ] |
| 5464 | * An ID attribute must have a declared default of #IMPLIED or #REQUIRED. |
| 5465 | * |
| 5466 | * [ VC: IDREF ] |
| 5467 | * Values of type IDREF must match the Name production, and values |
| 5468 | * of type IDREFS must match Names; each IDREF Name must match the value |
| 5469 | * of an ID attribute on some element in the XML document; i.e. IDREF |
| 5470 | * values must match the value of some ID attribute. |
| 5471 | * |
| 5472 | * [ VC: Entity Name ] |
| 5473 | * Values of type ENTITY must match the Name production, values |
| 5474 | * of type ENTITIES must match Names; each Entity Name must match the |
| 5475 | * name of an unparsed entity declared in the DTD. |
| 5476 | * |
| 5477 | * [ VC: Name Token ] |
| 5478 | * Values of type NMTOKEN must match the Nmtoken production; values |
| 5479 | * of type NMTOKENS must match Nmtokens. |
| 5480 | * |
| 5481 | * Returns the attribute type |
| 5482 | */ |
| 5483 | int |
| 5484 | xmlParseAttributeType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
| 5485 | SHRINK; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5486 | if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5487 | SKIP(5); |
| 5488 | return(XML_ATTRIBUTE_CDATA); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5489 | } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5490 | SKIP(6); |
| 5491 | return(XML_ATTRIBUTE_IDREFS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5492 | } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5493 | SKIP(5); |
| 5494 | return(XML_ATTRIBUTE_IDREF); |
| 5495 | } else if ((RAW == 'I') && (NXT(1) == 'D')) { |
| 5496 | SKIP(2); |
| 5497 | return(XML_ATTRIBUTE_ID); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5498 | } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5499 | SKIP(6); |
| 5500 | return(XML_ATTRIBUTE_ENTITY); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5501 | } 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] | 5502 | SKIP(8); |
| 5503 | return(XML_ATTRIBUTE_ENTITIES); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5504 | } 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] | 5505 | SKIP(8); |
| 5506 | return(XML_ATTRIBUTE_NMTOKENS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5507 | } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5508 | SKIP(7); |
| 5509 | return(XML_ATTRIBUTE_NMTOKEN); |
| 5510 | } |
| 5511 | return(xmlParseEnumeratedType(ctxt, tree)); |
| 5512 | } |
| 5513 | |
| 5514 | /** |
| 5515 | * xmlParseAttributeListDecl: |
| 5516 | * @ctxt: an XML parser context |
| 5517 | * |
| 5518 | * : parse the Attribute list def for an element |
| 5519 | * |
| 5520 | * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>' |
| 5521 | * |
| 5522 | * [53] AttDef ::= S Name S AttType S DefaultDecl |
| 5523 | * |
| 5524 | */ |
| 5525 | void |
| 5526 | xmlParseAttributeListDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5527 | const xmlChar *elemName; |
| 5528 | const xmlChar *attrName; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5529 | xmlEnumerationPtr tree; |
| 5530 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5531 | if (CMP9(CUR_PTR, '<', '!', 'A', 'T', 'T', 'L', 'I', 'S', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5532 | xmlParserInputPtr input = ctxt->input; |
| 5533 | |
| 5534 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5535 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5536 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5537 | "Space required after '<!ATTLIST'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5538 | } |
| 5539 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5540 | elemName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5541 | if (elemName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5542 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5543 | "ATTLIST: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5544 | return; |
| 5545 | } |
| 5546 | SKIP_BLANKS; |
| 5547 | GROW; |
| 5548 | while (RAW != '>') { |
| 5549 | const xmlChar *check = CUR_PTR; |
| 5550 | int type; |
| 5551 | int def; |
| 5552 | xmlChar *defaultValue = NULL; |
| 5553 | |
| 5554 | GROW; |
| 5555 | tree = NULL; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5556 | attrName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5557 | if (attrName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5558 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5559 | "ATTLIST: no name for Attribute\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5560 | break; |
| 5561 | } |
| 5562 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5563 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5564 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5565 | "Space required after the attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5566 | break; |
| 5567 | } |
| 5568 | SKIP_BLANKS; |
| 5569 | |
| 5570 | type = xmlParseAttributeType(ctxt, &tree); |
| 5571 | if (type <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5572 | break; |
| 5573 | } |
| 5574 | |
| 5575 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5576 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5577 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5578 | "Space required after the attribute type\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5579 | if (tree != NULL) |
| 5580 | xmlFreeEnumeration(tree); |
| 5581 | break; |
| 5582 | } |
| 5583 | SKIP_BLANKS; |
| 5584 | |
| 5585 | def = xmlParseDefaultDecl(ctxt, &defaultValue); |
| 5586 | if (def <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5587 | if (defaultValue != NULL) |
| 5588 | xmlFree(defaultValue); |
| 5589 | if (tree != NULL) |
| 5590 | xmlFreeEnumeration(tree); |
| 5591 | break; |
| 5592 | } |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 5593 | if ((type != XML_ATTRIBUTE_CDATA) && (defaultValue != NULL)) |
| 5594 | xmlAttrNormalizeSpace(defaultValue, defaultValue); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5595 | |
| 5596 | GROW; |
| 5597 | if (RAW != '>') { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5598 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5599 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5600 | "Space required after the attribute default value\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5601 | if (defaultValue != NULL) |
| 5602 | xmlFree(defaultValue); |
| 5603 | if (tree != NULL) |
| 5604 | xmlFreeEnumeration(tree); |
| 5605 | break; |
| 5606 | } |
| 5607 | SKIP_BLANKS; |
| 5608 | } |
| 5609 | if (check == CUR_PTR) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5610 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5611 | "in xmlParseAttributeListDecl\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5612 | if (defaultValue != NULL) |
| 5613 | xmlFree(defaultValue); |
| 5614 | if (tree != NULL) |
| 5615 | xmlFreeEnumeration(tree); |
| 5616 | break; |
| 5617 | } |
| 5618 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 5619 | (ctxt->sax->attributeDecl != NULL)) |
| 5620 | ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName, |
| 5621 | type, def, defaultValue, tree); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 5622 | else if (tree != NULL) |
| 5623 | xmlFreeEnumeration(tree); |
| 5624 | |
| 5625 | if ((ctxt->sax2) && (defaultValue != NULL) && |
| 5626 | (def != XML_ATTRIBUTE_IMPLIED) && |
| 5627 | (def != XML_ATTRIBUTE_REQUIRED)) { |
| 5628 | xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue); |
| 5629 | } |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 5630 | if (ctxt->sax2) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 5631 | xmlAddSpecialAttr(ctxt, elemName, attrName, type); |
| 5632 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5633 | if (defaultValue != NULL) |
| 5634 | xmlFree(defaultValue); |
| 5635 | GROW; |
| 5636 | } |
| 5637 | if (RAW == '>') { |
| 5638 | if (input != ctxt->input) { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 5639 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5640 | "Attribute list declaration doesn't start and stop in the same entity\n", |
| 5641 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5642 | } |
| 5643 | NEXT; |
| 5644 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5645 | } |
| 5646 | } |
| 5647 | |
| 5648 | /** |
| 5649 | * xmlParseElementMixedContentDecl: |
| 5650 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5651 | * @inputchk: the input used for the current entity, needed for boundary checks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5652 | * |
| 5653 | * parse the declaration for a Mixed Element content |
| 5654 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
| 5655 | * |
| 5656 | * [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' | |
| 5657 | * '(' S? '#PCDATA' S? ')' |
| 5658 | * |
| 5659 | * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49]) |
| 5660 | * |
| 5661 | * [ VC: No Duplicate Types ] |
| 5662 | * The same name must not appear more than once in a single |
| 5663 | * mixed-content declaration. |
| 5664 | * |
| 5665 | * returns: the list of the xmlElementContentPtr describing the element choices |
| 5666 | */ |
| 5667 | xmlElementContentPtr |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5668 | xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5669 | xmlElementContentPtr ret = NULL, cur = NULL, n; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5670 | const xmlChar *elem = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5671 | |
| 5672 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5673 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5674 | SKIP(7); |
| 5675 | SKIP_BLANKS; |
| 5676 | SHRINK; |
| 5677 | if (RAW == ')') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5678 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5679 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5680 | "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] | 5681 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5682 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5683 | NEXT; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5684 | ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5685 | if (ret == NULL) |
| 5686 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5687 | if (RAW == '*') { |
| 5688 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 5689 | NEXT; |
| 5690 | } |
| 5691 | return(ret); |
| 5692 | } |
| 5693 | if ((RAW == '(') || (RAW == '|')) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5694 | ret = cur = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5695 | if (ret == NULL) return(NULL); |
| 5696 | } |
| 5697 | while (RAW == '|') { |
| 5698 | NEXT; |
| 5699 | if (elem == NULL) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5700 | ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5701 | if (ret == NULL) return(NULL); |
| 5702 | ret->c1 = cur; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5703 | if (cur != NULL) |
| 5704 | cur->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5705 | cur = ret; |
| 5706 | } else { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5707 | n = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5708 | if (n == NULL) return(NULL); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5709 | n->c1 = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5710 | if (n->c1 != NULL) |
| 5711 | n->c1->parent = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5712 | cur->c2 = n; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5713 | if (n != NULL) |
| 5714 | n->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5715 | cur = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5716 | } |
| 5717 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5718 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5719 | if (elem == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5720 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5721 | "xmlParseElementMixedContentDecl : Name expected\n"); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5722 | xmlFreeDocElementContent(ctxt->myDoc, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5723 | return(NULL); |
| 5724 | } |
| 5725 | SKIP_BLANKS; |
| 5726 | GROW; |
| 5727 | } |
| 5728 | if ((RAW == ')') && (NXT(1) == '*')) { |
| 5729 | if (elem != NULL) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5730 | cur->c2 = xmlNewDocElementContent(ctxt->myDoc, elem, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5731 | XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5732 | if (cur->c2 != NULL) |
| 5733 | cur->c2->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5734 | } |
| 5735 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5736 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5737 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5738 | "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] | 5739 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5740 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5741 | SKIP(2); |
| 5742 | } else { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5743 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5744 | xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5745 | return(NULL); |
| 5746 | } |
| 5747 | |
| 5748 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5749 | xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5750 | } |
| 5751 | return(ret); |
| 5752 | } |
| 5753 | |
| 5754 | /** |
| 5755 | * xmlParseElementChildrenContentDecl: |
| 5756 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5757 | * @inputchk: the input used for the current entity, needed for boundary checks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5758 | * |
| 5759 | * parse the declaration for a Mixed Element content |
| 5760 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
| 5761 | * |
| 5762 | * |
| 5763 | * [47] children ::= (choice | seq) ('?' | '*' | '+')? |
| 5764 | * |
| 5765 | * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')? |
| 5766 | * |
| 5767 | * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')' |
| 5768 | * |
| 5769 | * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')' |
| 5770 | * |
| 5771 | * [ VC: Proper Group/PE Nesting ] applies to [49] and [50] |
| 5772 | * TODO Parameter-entity replacement text must be properly nested |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5773 | * with parenthesized groups. That is to say, if either of the |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5774 | * opening or closing parentheses in a choice, seq, or Mixed |
| 5775 | * construct is contained in the replacement text for a parameter |
| 5776 | * entity, both must be contained in the same replacement text. For |
| 5777 | * interoperability, if a parameter-entity reference appears in a |
| 5778 | * choice, seq, or Mixed construct, its replacement text should not |
| 5779 | * be empty, and neither the first nor last non-blank character of |
| 5780 | * the replacement text should be a connector (| or ,). |
| 5781 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 5782 | * Returns the tree of xmlElementContentPtr describing the element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5783 | * hierarchy. |
| 5784 | */ |
| 5785 | xmlElementContentPtr |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5786 | xmlParseElementChildrenContentDecl (xmlParserCtxtPtr ctxt, int inputchk) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5787 | xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5788 | const xmlChar *elem; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5789 | xmlChar type = 0; |
| 5790 | |
| 5791 | SKIP_BLANKS; |
| 5792 | GROW; |
| 5793 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5794 | int inputid = ctxt->input->id; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5795 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5796 | /* Recurse on first child */ |
| 5797 | NEXT; |
| 5798 | SKIP_BLANKS; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5799 | cur = ret = xmlParseElementChildrenContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5800 | SKIP_BLANKS; |
| 5801 | GROW; |
| 5802 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5803 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5804 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5805 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5806 | return(NULL); |
| 5807 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5808 | cur = ret = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5809 | if (cur == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5810 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5811 | return(NULL); |
| 5812 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5813 | GROW; |
| 5814 | if (RAW == '?') { |
| 5815 | cur->ocur = XML_ELEMENT_CONTENT_OPT; |
| 5816 | NEXT; |
| 5817 | } else if (RAW == '*') { |
| 5818 | cur->ocur = XML_ELEMENT_CONTENT_MULT; |
| 5819 | NEXT; |
| 5820 | } else if (RAW == '+') { |
| 5821 | cur->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 5822 | NEXT; |
| 5823 | } else { |
| 5824 | cur->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 5825 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5826 | GROW; |
| 5827 | } |
| 5828 | SKIP_BLANKS; |
| 5829 | SHRINK; |
| 5830 | while (RAW != ')') { |
| 5831 | /* |
| 5832 | * Each loop we parse one separator and one element. |
| 5833 | */ |
| 5834 | if (RAW == ',') { |
| 5835 | if (type == 0) type = CUR; |
| 5836 | |
| 5837 | /* |
| 5838 | * Detect "Name | Name , Name" error |
| 5839 | */ |
| 5840 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5841 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5842 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5843 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 5844 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5845 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5846 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5847 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5848 | return(NULL); |
| 5849 | } |
| 5850 | NEXT; |
| 5851 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5852 | op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_SEQ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5853 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 5854 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5855 | xmlFreeDocElementContent(ctxt->myDoc, last); |
| 5856 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5857 | return(NULL); |
| 5858 | } |
| 5859 | if (last == NULL) { |
| 5860 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5861 | if (ret != NULL) |
| 5862 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5863 | ret = cur = op; |
| 5864 | } else { |
| 5865 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5866 | if (op != NULL) |
| 5867 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5868 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5869 | if (last != NULL) |
| 5870 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5871 | cur =op; |
| 5872 | last = NULL; |
| 5873 | } |
| 5874 | } else if (RAW == '|') { |
| 5875 | if (type == 0) type = CUR; |
| 5876 | |
| 5877 | /* |
| 5878 | * Detect "Name , Name | Name" error |
| 5879 | */ |
| 5880 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5881 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5882 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5883 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 5884 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5885 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5886 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5887 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5888 | return(NULL); |
| 5889 | } |
| 5890 | NEXT; |
| 5891 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5892 | op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5893 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 5894 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5895 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5896 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5897 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5898 | return(NULL); |
| 5899 | } |
| 5900 | if (last == NULL) { |
| 5901 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5902 | if (ret != NULL) |
| 5903 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5904 | ret = cur = op; |
| 5905 | } else { |
| 5906 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5907 | if (op != NULL) |
| 5908 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5909 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5910 | if (last != NULL) |
| 5911 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5912 | cur =op; |
| 5913 | last = NULL; |
| 5914 | } |
| 5915 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5916 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL); |
Daniel Veillard | c707d0b | 2008-01-24 14:48:54 +0000 | [diff] [blame] | 5917 | if ((last != NULL) && (last != ret)) |
| 5918 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5919 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5920 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5921 | return(NULL); |
| 5922 | } |
| 5923 | GROW; |
| 5924 | SKIP_BLANKS; |
| 5925 | GROW; |
| 5926 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5927 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5928 | /* Recurse on second child */ |
| 5929 | NEXT; |
| 5930 | SKIP_BLANKS; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5931 | last = xmlParseElementChildrenContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5932 | SKIP_BLANKS; |
| 5933 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5934 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5935 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5936 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5937 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5938 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5939 | return(NULL); |
| 5940 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5941 | last = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 5942 | if (last == NULL) { |
| 5943 | if (ret != NULL) |
| 5944 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
| 5945 | return(NULL); |
| 5946 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5947 | if (RAW == '?') { |
| 5948 | last->ocur = XML_ELEMENT_CONTENT_OPT; |
| 5949 | NEXT; |
| 5950 | } else if (RAW == '*') { |
| 5951 | last->ocur = XML_ELEMENT_CONTENT_MULT; |
| 5952 | NEXT; |
| 5953 | } else if (RAW == '+') { |
| 5954 | last->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 5955 | NEXT; |
| 5956 | } else { |
| 5957 | last->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 5958 | } |
| 5959 | } |
| 5960 | SKIP_BLANKS; |
| 5961 | GROW; |
| 5962 | } |
| 5963 | if ((cur != NULL) && (last != NULL)) { |
| 5964 | cur->c2 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5965 | if (last != NULL) |
| 5966 | last->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5967 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5968 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5969 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5970 | "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] | 5971 | NULL, NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5972 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5973 | NEXT; |
| 5974 | if (RAW == '?') { |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 5975 | if (ret != NULL) { |
| 5976 | if ((ret->ocur == XML_ELEMENT_CONTENT_PLUS) || |
| 5977 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 5978 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 5979 | else |
| 5980 | ret->ocur = XML_ELEMENT_CONTENT_OPT; |
| 5981 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5982 | NEXT; |
| 5983 | } else if (RAW == '*') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 5984 | if (ret != NULL) { |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 5985 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 5986 | cur = ret; |
| 5987 | /* |
| 5988 | * Some normalization: |
| 5989 | * (a | b* | c?)* == (a | b | c)* |
| 5990 | */ |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 5991 | while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 5992 | if ((cur->c1 != NULL) && |
| 5993 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 5994 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 5995 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 5996 | if ((cur->c2 != NULL) && |
| 5997 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 5998 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 5999 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6000 | cur = cur->c2; |
| 6001 | } |
| 6002 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6003 | NEXT; |
| 6004 | } else if (RAW == '+') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6005 | if (ret != NULL) { |
| 6006 | int found = 0; |
| 6007 | |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 6008 | if ((ret->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6009 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 6010 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
William M. Brack | eb8509c | 2004-05-14 03:48:02 +0000 | [diff] [blame] | 6011 | else |
| 6012 | ret->ocur = XML_ELEMENT_CONTENT_PLUS; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6013 | /* |
| 6014 | * Some normalization: |
| 6015 | * (a | b*)+ == (a | b)* |
| 6016 | * (a | b?)+ == (a | b)* |
| 6017 | */ |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 6018 | while ((cur != NULL) && (cur->type == XML_ELEMENT_CONTENT_OR)) { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 6019 | if ((cur->c1 != NULL) && |
| 6020 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6021 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 6022 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6023 | found = 1; |
| 6024 | } |
| 6025 | if ((cur->c2 != NULL) && |
| 6026 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 6027 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 6028 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 6029 | found = 1; |
| 6030 | } |
| 6031 | cur = cur->c2; |
| 6032 | } |
| 6033 | if (found) |
| 6034 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 6035 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6036 | NEXT; |
| 6037 | } |
| 6038 | return(ret); |
| 6039 | } |
| 6040 | |
| 6041 | /** |
| 6042 | * xmlParseElementContentDecl: |
| 6043 | * @ctxt: an XML parser context |
| 6044 | * @name: the name of the element being defined. |
| 6045 | * @result: the Element Content pointer will be stored here if any |
| 6046 | * |
| 6047 | * parse the declaration for an Element content either Mixed or Children, |
| 6048 | * the cases EMPTY and ANY are handled directly in xmlParseElementDecl |
| 6049 | * |
| 6050 | * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children |
| 6051 | * |
| 6052 | * returns: the type of element content XML_ELEMENT_TYPE_xxx |
| 6053 | */ |
| 6054 | |
| 6055 | int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6056 | xmlParseElementContentDecl(xmlParserCtxtPtr ctxt, const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6057 | xmlElementContentPtr *result) { |
| 6058 | |
| 6059 | xmlElementContentPtr tree = NULL; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6060 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6061 | int res; |
| 6062 | |
| 6063 | *result = NULL; |
| 6064 | |
| 6065 | if (RAW != '(') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6066 | xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6067 | "xmlParseElementContentDecl : %s '(' expected\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6068 | return(-1); |
| 6069 | } |
| 6070 | NEXT; |
| 6071 | GROW; |
| 6072 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6073 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6074 | tree = xmlParseElementMixedContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6075 | res = XML_ELEMENT_TYPE_MIXED; |
| 6076 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6077 | tree = xmlParseElementChildrenContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6078 | res = XML_ELEMENT_TYPE_ELEMENT; |
| 6079 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6080 | SKIP_BLANKS; |
| 6081 | *result = tree; |
| 6082 | return(res); |
| 6083 | } |
| 6084 | |
| 6085 | /** |
| 6086 | * xmlParseElementDecl: |
| 6087 | * @ctxt: an XML parser context |
| 6088 | * |
| 6089 | * parse an Element declaration. |
| 6090 | * |
| 6091 | * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>' |
| 6092 | * |
| 6093 | * [ VC: Unique Element Type Declaration ] |
| 6094 | * No element type may be declared more than once |
| 6095 | * |
| 6096 | * Returns the type of the element, or -1 in case of error |
| 6097 | */ |
| 6098 | int |
| 6099 | xmlParseElementDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6100 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6101 | int ret = -1; |
| 6102 | xmlElementContentPtr content = NULL; |
| 6103 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 6104 | /* GROW; done in the caller */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6105 | if (CMP9(CUR_PTR, '<', '!', 'E', 'L', 'E', 'M', 'E', 'N', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6106 | xmlParserInputPtr input = ctxt->input; |
| 6107 | |
| 6108 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6109 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6110 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6111 | "Space required after 'ELEMENT'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6112 | } |
| 6113 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6114 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6115 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6116 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6117 | "xmlParseElementDecl: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6118 | return(-1); |
| 6119 | } |
| 6120 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6121 | xmlPopInput(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6122 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6123 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6124 | "Space required after the element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6125 | } |
| 6126 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6127 | if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6128 | SKIP(5); |
| 6129 | /* |
| 6130 | * Element must always be empty. |
| 6131 | */ |
| 6132 | ret = XML_ELEMENT_TYPE_EMPTY; |
| 6133 | } else if ((RAW == 'A') && (NXT(1) == 'N') && |
| 6134 | (NXT(2) == 'Y')) { |
| 6135 | SKIP(3); |
| 6136 | /* |
| 6137 | * Element is a generic container. |
| 6138 | */ |
| 6139 | ret = XML_ELEMENT_TYPE_ANY; |
| 6140 | } else if (RAW == '(') { |
| 6141 | ret = xmlParseElementContentDecl(ctxt, name, &content); |
| 6142 | } else { |
| 6143 | /* |
| 6144 | * [ WFC: PEs in Internal Subset ] error handling. |
| 6145 | */ |
| 6146 | if ((RAW == '%') && (ctxt->external == 0) && |
| 6147 | (ctxt->inputNr == 1)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6148 | xmlFatalErrMsg(ctxt, XML_ERR_PEREF_IN_INT_SUBSET, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6149 | "PEReference: forbidden within markup decl in internal subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6150 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6151 | xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6152 | "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n"); |
| 6153 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6154 | return(-1); |
| 6155 | } |
| 6156 | |
| 6157 | SKIP_BLANKS; |
| 6158 | /* |
| 6159 | * Pop-up of finished entities. |
| 6160 | */ |
| 6161 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6162 | xmlPopInput(ctxt); |
| 6163 | SKIP_BLANKS; |
| 6164 | |
| 6165 | if (RAW != '>') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6166 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6167 | if (content != NULL) { |
| 6168 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6169 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6170 | } else { |
| 6171 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6172 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6173 | "Element declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6174 | } |
| 6175 | |
| 6176 | NEXT; |
| 6177 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6178 | (ctxt->sax->elementDecl != NULL)) { |
| 6179 | if (content != NULL) |
| 6180 | content->parent = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6181 | ctxt->sax->elementDecl(ctxt->userData, name, ret, |
| 6182 | content); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 6183 | if ((content != NULL) && (content->parent == NULL)) { |
| 6184 | /* |
| 6185 | * this is a trick: if xmlAddElementDecl is called, |
| 6186 | * instead of copying the full tree it is plugged directly |
| 6187 | * if called from the parser. Avoid duplicating the |
| 6188 | * interfaces or change the API/ABI |
| 6189 | */ |
| 6190 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6191 | } |
| 6192 | } else if (content != NULL) { |
| 6193 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 6194 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6195 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6196 | } |
| 6197 | return(ret); |
| 6198 | } |
| 6199 | |
| 6200 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6201 | * xmlParseConditionalSections |
| 6202 | * @ctxt: an XML parser context |
| 6203 | * |
| 6204 | * [61] conditionalSect ::= includeSect | ignoreSect |
| 6205 | * [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>' |
| 6206 | * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>' |
| 6207 | * [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)* |
| 6208 | * [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*) |
| 6209 | */ |
| 6210 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6211 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6212 | xmlParseConditionalSections(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6213 | int id = ctxt->input->id; |
| 6214 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6215 | SKIP(3); |
| 6216 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6217 | if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6218 | SKIP(7); |
| 6219 | SKIP_BLANKS; |
| 6220 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6221 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6222 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6223 | if (ctxt->input->id != id) { |
| 6224 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6225 | "All markup of the conditional section is not in the same entity\n", |
| 6226 | NULL, NULL); |
| 6227 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6228 | NEXT; |
| 6229 | } |
| 6230 | if (xmlParserDebugEntities) { |
| 6231 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6232 | xmlGenericError(xmlGenericErrorContext, |
| 6233 | "%s(%d): ", ctxt->input->filename, |
| 6234 | ctxt->input->line); |
| 6235 | xmlGenericError(xmlGenericErrorContext, |
| 6236 | "Entering INCLUDE Conditional Section\n"); |
| 6237 | } |
| 6238 | |
| 6239 | while ((RAW != 0) && ((RAW != ']') || (NXT(1) != ']') || |
| 6240 | (NXT(2) != '>'))) { |
| 6241 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 6242 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6243 | |
| 6244 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6245 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6246 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6247 | NEXT; |
| 6248 | } else if (RAW == '%') { |
| 6249 | xmlParsePEReference(ctxt); |
| 6250 | } else |
| 6251 | xmlParseMarkupDecl(ctxt); |
| 6252 | |
| 6253 | /* |
| 6254 | * Pop-up of finished entities. |
| 6255 | */ |
| 6256 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6257 | xmlPopInput(ctxt); |
| 6258 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 6259 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6260 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6261 | break; |
| 6262 | } |
| 6263 | } |
| 6264 | if (xmlParserDebugEntities) { |
| 6265 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6266 | xmlGenericError(xmlGenericErrorContext, |
| 6267 | "%s(%d): ", ctxt->input->filename, |
| 6268 | ctxt->input->line); |
| 6269 | xmlGenericError(xmlGenericErrorContext, |
| 6270 | "Leaving INCLUDE Conditional Section\n"); |
| 6271 | } |
| 6272 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6273 | } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6274 | int state; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 6275 | xmlParserInputState instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6276 | int depth = 0; |
| 6277 | |
| 6278 | SKIP(6); |
| 6279 | SKIP_BLANKS; |
| 6280 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6281 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6282 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6283 | if (ctxt->input->id != id) { |
| 6284 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6285 | "All markup of the conditional section is not in the same entity\n", |
| 6286 | NULL, NULL); |
| 6287 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6288 | NEXT; |
| 6289 | } |
| 6290 | if (xmlParserDebugEntities) { |
| 6291 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6292 | xmlGenericError(xmlGenericErrorContext, |
| 6293 | "%s(%d): ", ctxt->input->filename, |
| 6294 | ctxt->input->line); |
| 6295 | xmlGenericError(xmlGenericErrorContext, |
| 6296 | "Entering IGNORE Conditional Section\n"); |
| 6297 | } |
| 6298 | |
| 6299 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6300 | * Parse up to the end of the conditional section |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6301 | * But disable SAX event generating DTD building in the meantime |
| 6302 | */ |
| 6303 | state = ctxt->disableSAX; |
| 6304 | instate = ctxt->instate; |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 6305 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6306 | ctxt->instate = XML_PARSER_IGNORE; |
| 6307 | |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 6308 | while ((depth >= 0) && (RAW != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6309 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6310 | depth++; |
| 6311 | SKIP(3); |
| 6312 | continue; |
| 6313 | } |
| 6314 | if ((RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) { |
| 6315 | if (--depth >= 0) SKIP(3); |
| 6316 | continue; |
| 6317 | } |
| 6318 | NEXT; |
| 6319 | continue; |
| 6320 | } |
| 6321 | |
| 6322 | ctxt->disableSAX = state; |
| 6323 | ctxt->instate = instate; |
| 6324 | |
| 6325 | if (xmlParserDebugEntities) { |
| 6326 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 6327 | xmlGenericError(xmlGenericErrorContext, |
| 6328 | "%s(%d): ", ctxt->input->filename, |
| 6329 | ctxt->input->line); |
| 6330 | xmlGenericError(xmlGenericErrorContext, |
| 6331 | "Leaving IGNORE Conditional Section\n"); |
| 6332 | } |
| 6333 | |
| 6334 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6335 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6336 | } |
| 6337 | |
| 6338 | if (RAW == 0) |
| 6339 | SHRINK; |
| 6340 | |
| 6341 | if (RAW == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6342 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6343 | } else { |
Daniel Veillard | 49d4405 | 2008-08-27 19:57:06 +0000 | [diff] [blame] | 6344 | if (ctxt->input->id != id) { |
| 6345 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 6346 | "All markup of the conditional section is not in the same entity\n", |
| 6347 | NULL, NULL); |
| 6348 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6349 | SKIP(3); |
| 6350 | } |
| 6351 | } |
| 6352 | |
| 6353 | /** |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6354 | * xmlParseMarkupDecl: |
| 6355 | * @ctxt: an XML parser context |
| 6356 | * |
| 6357 | * parse Markup declarations |
| 6358 | * |
| 6359 | * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl | |
| 6360 | * NotationDecl | PI | Comment |
| 6361 | * |
| 6362 | * [ VC: Proper Declaration/PE Nesting ] |
| 6363 | * Parameter-entity replacement text must be properly nested with |
| 6364 | * markup declarations. That is to say, if either the first character |
| 6365 | * or the last character of a markup declaration (markupdecl above) is |
| 6366 | * contained in the replacement text for a parameter-entity reference, |
| 6367 | * both must be contained in the same replacement text. |
| 6368 | * |
| 6369 | * [ WFC: PEs in Internal Subset ] |
| 6370 | * In the internal DTD subset, parameter-entity references can occur |
| 6371 | * only where markup declarations can occur, not within markup declarations. |
| 6372 | * (This does not apply to references that occur in external parameter |
| 6373 | * entities or to the external subset.) |
| 6374 | */ |
| 6375 | void |
| 6376 | xmlParseMarkupDecl(xmlParserCtxtPtr ctxt) { |
| 6377 | GROW; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 6378 | if (CUR == '<') { |
| 6379 | if (NXT(1) == '!') { |
| 6380 | switch (NXT(2)) { |
| 6381 | case 'E': |
| 6382 | if (NXT(3) == 'L') |
| 6383 | xmlParseElementDecl(ctxt); |
| 6384 | else if (NXT(3) == 'N') |
| 6385 | xmlParseEntityDecl(ctxt); |
| 6386 | break; |
| 6387 | case 'A': |
| 6388 | xmlParseAttributeListDecl(ctxt); |
| 6389 | break; |
| 6390 | case 'N': |
| 6391 | xmlParseNotationDecl(ctxt); |
| 6392 | break; |
| 6393 | case '-': |
| 6394 | xmlParseComment(ctxt); |
| 6395 | break; |
| 6396 | default: |
| 6397 | /* there is an error but it will be detected later */ |
| 6398 | break; |
| 6399 | } |
| 6400 | } else if (NXT(1) == '?') { |
| 6401 | xmlParsePI(ctxt); |
| 6402 | } |
| 6403 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6404 | /* |
| 6405 | * This is only for internal subset. On external entities, |
| 6406 | * the replacement is done before parsing stage |
| 6407 | */ |
| 6408 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 6409 | xmlParsePEReference(ctxt); |
| 6410 | |
| 6411 | /* |
| 6412 | * Conditional sections are allowed from entities included |
| 6413 | * by PE References in the internal subset. |
| 6414 | */ |
| 6415 | if ((ctxt->external == 0) && (ctxt->inputNr > 1)) { |
| 6416 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6417 | xmlParseConditionalSections(ctxt); |
| 6418 | } |
| 6419 | } |
| 6420 | |
| 6421 | ctxt->instate = XML_PARSER_DTD; |
| 6422 | } |
| 6423 | |
| 6424 | /** |
| 6425 | * xmlParseTextDecl: |
| 6426 | * @ctxt: an XML parser context |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 6427 | * |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6428 | * parse an XML declaration header for external entities |
| 6429 | * |
| 6430 | * [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>' |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6431 | */ |
| 6432 | |
| 6433 | void |
| 6434 | xmlParseTextDecl(xmlParserCtxtPtr ctxt) { |
| 6435 | xmlChar *version; |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 6436 | const xmlChar *encoding; |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6437 | |
| 6438 | /* |
| 6439 | * We know that '<?xml' is here. |
| 6440 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6441 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6442 | SKIP(5); |
| 6443 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6444 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6445 | return; |
| 6446 | } |
| 6447 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6448 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6449 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6450 | "Space needed after '<?xml'\n"); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6451 | } |
| 6452 | SKIP_BLANKS; |
| 6453 | |
| 6454 | /* |
| 6455 | * We may have the VersionInfo here. |
| 6456 | */ |
| 6457 | version = xmlParseVersionInfo(ctxt); |
| 6458 | if (version == NULL) |
| 6459 | version = xmlCharStrdup(XML_DEFAULT_VERSION); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 6460 | else { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6461 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6462 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6463 | "Space needed here\n"); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 6464 | } |
| 6465 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6466 | ctxt->input->version = version; |
| 6467 | |
| 6468 | /* |
| 6469 | * We must have the encoding declaration |
| 6470 | */ |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 6471 | encoding = xmlParseEncodingDecl(ctxt); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6472 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 6473 | /* |
| 6474 | * The XML REC instructs us to stop parsing right here |
| 6475 | */ |
| 6476 | return; |
| 6477 | } |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 6478 | if ((encoding == NULL) && (ctxt->errNo == XML_ERR_OK)) { |
| 6479 | xmlFatalErrMsg(ctxt, XML_ERR_MISSING_ENCODING, |
| 6480 | "Missing encoding in text declaration\n"); |
| 6481 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6482 | |
| 6483 | SKIP_BLANKS; |
| 6484 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 6485 | SKIP(2); |
| 6486 | } else if (RAW == '>') { |
| 6487 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6488 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6489 | NEXT; |
| 6490 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6491 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 6492 | MOVETO_ENDTAG(CUR_PTR); |
| 6493 | NEXT; |
| 6494 | } |
| 6495 | } |
| 6496 | |
| 6497 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6498 | * xmlParseExternalSubset: |
| 6499 | * @ctxt: an XML parser context |
| 6500 | * @ExternalID: the external identifier |
| 6501 | * @SystemID: the system identifier (or URL) |
| 6502 | * |
| 6503 | * parse Markup declarations from an external subset |
| 6504 | * |
| 6505 | * [30] extSubset ::= textDecl? extSubsetDecl |
| 6506 | * |
| 6507 | * [31] extSubsetDecl ::= (markupdecl | conditionalSect | PEReference | S) * |
| 6508 | */ |
| 6509 | void |
| 6510 | xmlParseExternalSubset(xmlParserCtxtPtr ctxt, const xmlChar *ExternalID, |
| 6511 | const xmlChar *SystemID) { |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 6512 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6513 | GROW; |
Daniel Veillard | 6ccc56d | 2008-04-03 12:59:06 +0000 | [diff] [blame] | 6514 | |
| 6515 | if ((ctxt->encoding == (const xmlChar *)XML_CHAR_ENCODING_NONE) && |
| 6516 | (ctxt->input->end - ctxt->input->cur >= 4)) { |
| 6517 | xmlChar start[4]; |
| 6518 | xmlCharEncoding enc; |
| 6519 | |
| 6520 | start[0] = RAW; |
| 6521 | start[1] = NXT(1); |
| 6522 | start[2] = NXT(2); |
| 6523 | start[3] = NXT(3); |
| 6524 | enc = xmlDetectCharEncoding(start, 4); |
| 6525 | if (enc != XML_CHAR_ENCODING_NONE) |
| 6526 | xmlSwitchEncoding(ctxt, enc); |
| 6527 | } |
| 6528 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6529 | if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6530 | xmlParseTextDecl(ctxt); |
| 6531 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 6532 | /* |
| 6533 | * The XML REC instructs us to stop parsing right here |
| 6534 | */ |
| 6535 | ctxt->instate = XML_PARSER_EOF; |
| 6536 | return; |
| 6537 | } |
| 6538 | } |
| 6539 | if (ctxt->myDoc == NULL) { |
| 6540 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 6541 | if (ctxt->myDoc == NULL) { |
| 6542 | xmlErrMemory(ctxt, "New Doc failed"); |
| 6543 | return; |
| 6544 | } |
| 6545 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6546 | } |
| 6547 | if ((ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset == NULL)) |
| 6548 | xmlCreateIntSubset(ctxt->myDoc, NULL, ExternalID, SystemID); |
| 6549 | |
| 6550 | ctxt->instate = XML_PARSER_DTD; |
| 6551 | ctxt->external = 1; |
| 6552 | while (((RAW == '<') && (NXT(1) == '?')) || |
| 6553 | ((RAW == '<') && (NXT(1) == '!')) || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6554 | (RAW == '%') || IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6555 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 6556 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6557 | |
| 6558 | GROW; |
| 6559 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 6560 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6561 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6562 | NEXT; |
| 6563 | } else if (RAW == '%') { |
| 6564 | xmlParsePEReference(ctxt); |
| 6565 | } else |
| 6566 | xmlParseMarkupDecl(ctxt); |
| 6567 | |
| 6568 | /* |
| 6569 | * Pop-up of finished entities. |
| 6570 | */ |
| 6571 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6572 | xmlPopInput(ctxt); |
| 6573 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 6574 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6575 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6576 | break; |
| 6577 | } |
| 6578 | } |
| 6579 | |
| 6580 | if (RAW != 0) { |
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 | } |
| 6583 | |
| 6584 | } |
| 6585 | |
| 6586 | /** |
| 6587 | * xmlParseReference: |
| 6588 | * @ctxt: an XML parser context |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6589 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6590 | * parse and handle entity references in content, depending on the SAX |
| 6591 | * interface, this may end-up in a call to character() if this is a |
| 6592 | * CharRef, a predefined entity, if there is no reference() callback. |
| 6593 | * or if the parser was asked to switch to that mode. |
| 6594 | * |
| 6595 | * [67] Reference ::= EntityRef | CharRef |
| 6596 | */ |
| 6597 | void |
| 6598 | xmlParseReference(xmlParserCtxtPtr ctxt) { |
| 6599 | xmlEntityPtr ent; |
| 6600 | xmlChar *val; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6601 | int was_checked; |
| 6602 | xmlNodePtr list = NULL; |
| 6603 | xmlParserErrors ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6604 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6605 | |
| 6606 | if (RAW != '&') |
| 6607 | return; |
| 6608 | |
| 6609 | /* |
| 6610 | * Simple case of a CharRef |
| 6611 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6612 | if (NXT(1) == '#') { |
| 6613 | int i = 0; |
| 6614 | xmlChar out[10]; |
| 6615 | int hex = NXT(2); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6616 | int value = xmlParseCharRef(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6617 | |
Daniel Veillard | dc17160 | 2008-03-26 17:41:38 +0000 | [diff] [blame] | 6618 | if (value == 0) |
| 6619 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6620 | if (ctxt->charset != XML_CHAR_ENCODING_UTF8) { |
| 6621 | /* |
| 6622 | * So we are using non-UTF-8 buffers |
| 6623 | * Check that the char fit on 8bits, if not |
| 6624 | * generate a CharRef. |
| 6625 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6626 | if (value <= 0xFF) { |
| 6627 | out[0] = value; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6628 | out[1] = 0; |
| 6629 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 6630 | (!ctxt->disableSAX)) |
| 6631 | ctxt->sax->characters(ctxt->userData, out, 1); |
| 6632 | } else { |
| 6633 | if ((hex == 'x') || (hex == 'X')) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 6634 | snprintf((char *)out, sizeof(out), "#x%X", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6635 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 6636 | snprintf((char *)out, sizeof(out), "#%d", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6637 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 6638 | (!ctxt->disableSAX)) |
| 6639 | ctxt->sax->reference(ctxt->userData, out); |
| 6640 | } |
| 6641 | } else { |
| 6642 | /* |
| 6643 | * Just encode the value in UTF-8 |
| 6644 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6645 | COPY_BUF(0 ,out, i, value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6646 | out[i] = 0; |
| 6647 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 6648 | (!ctxt->disableSAX)) |
| 6649 | ctxt->sax->characters(ctxt->userData, out, i); |
| 6650 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6651 | return; |
| 6652 | } |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 6653 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6654 | /* |
| 6655 | * We are seeing an entity reference |
| 6656 | */ |
| 6657 | ent = xmlParseEntityRef(ctxt); |
| 6658 | if (ent == NULL) return; |
| 6659 | if (!ctxt->wellFormed) |
| 6660 | return; |
| 6661 | was_checked = ent->checked; |
| 6662 | |
| 6663 | /* special case of predefined entities */ |
| 6664 | if ((ent->name == NULL) || |
| 6665 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 6666 | val = ent->content; |
| 6667 | if (val == NULL) return; |
| 6668 | /* |
| 6669 | * inline the entity. |
| 6670 | */ |
| 6671 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 6672 | (!ctxt->disableSAX)) |
| 6673 | ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val)); |
| 6674 | return; |
| 6675 | } |
| 6676 | |
| 6677 | /* |
| 6678 | * The first reference to the entity trigger a parsing phase |
| 6679 | * where the ent->children is filled with the result from |
| 6680 | * the parsing. |
| 6681 | */ |
| 6682 | if (ent->checked == 0) { |
| 6683 | unsigned long oldnbent = ctxt->nbentities; |
| 6684 | |
| 6685 | /* |
| 6686 | * This is a bit hackish but this seems the best |
| 6687 | * way to make sure both SAX and DOM entity support |
| 6688 | * behaves okay. |
| 6689 | */ |
| 6690 | void *user_data; |
| 6691 | if (ctxt->userData == ctxt) |
| 6692 | user_data = NULL; |
| 6693 | else |
| 6694 | user_data = ctxt->userData; |
| 6695 | |
| 6696 | /* |
| 6697 | * Check that this entity is well formed |
| 6698 | * 4.3.2: An internal general parsed entity is well-formed |
| 6699 | * if its replacement text matches the production labeled |
| 6700 | * content. |
| 6701 | */ |
| 6702 | if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) { |
| 6703 | ctxt->depth++; |
| 6704 | ret = xmlParseBalancedChunkMemoryInternal(ctxt, ent->content, |
| 6705 | user_data, &list); |
| 6706 | ctxt->depth--; |
| 6707 | |
| 6708 | } else if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) { |
| 6709 | ctxt->depth++; |
| 6710 | ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, ctxt->sax, |
| 6711 | user_data, ctxt->depth, ent->URI, |
| 6712 | ent->ExternalID, &list); |
| 6713 | ctxt->depth--; |
| 6714 | } else { |
| 6715 | ret = XML_ERR_ENTITY_PE_INTERNAL; |
| 6716 | xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 6717 | "invalid entity type found\n", NULL); |
| 6718 | } |
| 6719 | |
| 6720 | /* |
| 6721 | * Store the number of entities needing parsing for this entity |
| 6722 | * content and do checkings |
| 6723 | */ |
| 6724 | ent->checked = ctxt->nbentities - oldnbent; |
| 6725 | if (ret == XML_ERR_ENTITY_LOOP) { |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 6726 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6727 | xmlFreeNodeList(list); |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 6728 | return; |
| 6729 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6730 | if (xmlParserEntityCheck(ctxt, 0, ent)) { |
| 6731 | xmlFreeNodeList(list); |
| 6732 | return; |
| 6733 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6734 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6735 | if ((ret == XML_ERR_OK) && (list != NULL)) { |
| 6736 | if (((ent->etype == XML_INTERNAL_GENERAL_ENTITY) || |
| 6737 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY))&& |
| 6738 | (ent->children == NULL)) { |
| 6739 | ent->children = list; |
| 6740 | if (ctxt->replaceEntities) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6741 | /* |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6742 | * Prune it directly in the generated document |
| 6743 | * except for single text nodes. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6744 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6745 | if (((list->type == XML_TEXT_NODE) && |
| 6746 | (list->next == NULL)) || |
| 6747 | (ctxt->parseMode == XML_PARSE_READER)) { |
| 6748 | list->parent = (xmlNodePtr) ent; |
| 6749 | list = NULL; |
| 6750 | ent->owner = 1; |
| 6751 | } else { |
| 6752 | ent->owner = 0; |
| 6753 | while (list != NULL) { |
| 6754 | list->parent = (xmlNodePtr) ctxt->node; |
| 6755 | list->doc = ctxt->myDoc; |
| 6756 | if (list->next == NULL) |
| 6757 | ent->last = list; |
| 6758 | list = list->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6759 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6760 | list = ent->children; |
| 6761 | #ifdef LIBXML_LEGACY_ENABLED |
| 6762 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 6763 | xmlAddEntityReference(ent, list, NULL); |
| 6764 | #endif /* LIBXML_LEGACY_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6765 | } |
| 6766 | } else { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6767 | ent->owner = 1; |
| 6768 | while (list != NULL) { |
| 6769 | list->parent = (xmlNodePtr) ent; |
| 6770 | if (list->next == NULL) |
| 6771 | ent->last = list; |
| 6772 | list = list->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6773 | } |
| 6774 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6775 | } else { |
| 6776 | xmlFreeNodeList(list); |
| 6777 | list = NULL; |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 6778 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6779 | } else if ((ret != XML_ERR_OK) && |
| 6780 | (ret != XML_WAR_UNDECLARED_ENTITY)) { |
| 6781 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 6782 | "Entity '%s' failed to parse\n", ent->name); |
| 6783 | } else if (list != NULL) { |
| 6784 | xmlFreeNodeList(list); |
| 6785 | list = NULL; |
| 6786 | } |
| 6787 | if (ent->checked == 0) |
| 6788 | ent->checked = 1; |
| 6789 | } else if (ent->checked != 1) { |
| 6790 | ctxt->nbentities += ent->checked; |
| 6791 | } |
Daniel Veillard | a37a6ad | 2006-10-10 20:05:45 +0000 | [diff] [blame] | 6792 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6793 | /* |
| 6794 | * Now that the entity content has been gathered |
| 6795 | * provide it to the application, this can take different forms based |
| 6796 | * on the parsing modes. |
| 6797 | */ |
| 6798 | if (ent->children == NULL) { |
| 6799 | /* |
| 6800 | * Probably running in SAX mode and the callbacks don't |
| 6801 | * build the entity content. So unless we already went |
| 6802 | * though parsing for first checking go though the entity |
| 6803 | * content to generate callbacks associated to the entity |
| 6804 | */ |
| 6805 | if (was_checked != 0) { |
| 6806 | void *user_data; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6807 | /* |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6808 | * This is a bit hackish but this seems the best |
| 6809 | * way to make sure both SAX and DOM entity support |
| 6810 | * behaves okay. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6811 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6812 | if (ctxt->userData == ctxt) |
| 6813 | user_data = NULL; |
| 6814 | else |
| 6815 | user_data = ctxt->userData; |
| 6816 | |
| 6817 | if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) { |
| 6818 | ctxt->depth++; |
| 6819 | ret = xmlParseBalancedChunkMemoryInternal(ctxt, |
| 6820 | ent->content, user_data, NULL); |
| 6821 | ctxt->depth--; |
| 6822 | } else if (ent->etype == |
| 6823 | XML_EXTERNAL_GENERAL_PARSED_ENTITY) { |
| 6824 | ctxt->depth++; |
| 6825 | ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, |
| 6826 | ctxt->sax, user_data, ctxt->depth, |
| 6827 | ent->URI, ent->ExternalID, NULL); |
| 6828 | ctxt->depth--; |
| 6829 | } else { |
| 6830 | ret = XML_ERR_ENTITY_PE_INTERNAL; |
| 6831 | xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 6832 | "invalid entity type found\n", NULL); |
| 6833 | } |
| 6834 | if (ret == XML_ERR_ENTITY_LOOP) { |
| 6835 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
| 6836 | return; |
| 6837 | } |
| 6838 | } |
| 6839 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 6840 | (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) { |
| 6841 | /* |
| 6842 | * Entity reference callback comes second, it's somewhat |
| 6843 | * superfluous but a compatibility to historical behaviour |
| 6844 | */ |
| 6845 | ctxt->sax->reference(ctxt->userData, ent->name); |
| 6846 | } |
| 6847 | return; |
| 6848 | } |
| 6849 | |
| 6850 | /* |
| 6851 | * If we didn't get any children for the entity being built |
| 6852 | */ |
| 6853 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 6854 | (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) { |
| 6855 | /* |
| 6856 | * Create a node. |
| 6857 | */ |
| 6858 | ctxt->sax->reference(ctxt->userData, ent->name); |
| 6859 | return; |
| 6860 | } |
| 6861 | |
| 6862 | if ((ctxt->replaceEntities) || (ent->children == NULL)) { |
| 6863 | /* |
| 6864 | * There is a problem on the handling of _private for entities |
| 6865 | * (bug 155816): Should we copy the content of the field from |
| 6866 | * the entity (possibly overwriting some value set by the user |
| 6867 | * when a copy is created), should we leave it alone, or should |
| 6868 | * we try to take care of different situations? The problem |
| 6869 | * is exacerbated by the usage of this field by the xmlReader. |
| 6870 | * To fix this bug, we look at _private on the created node |
| 6871 | * and, if it's NULL, we copy in whatever was in the entity. |
| 6872 | * If it's not NULL we leave it alone. This is somewhat of a |
| 6873 | * hack - maybe we should have further tests to determine |
| 6874 | * what to do. |
| 6875 | */ |
| 6876 | if ((ctxt->node != NULL) && (ent->children != NULL)) { |
| 6877 | /* |
| 6878 | * Seems we are generating the DOM content, do |
| 6879 | * a simple tree copy for all references except the first |
| 6880 | * In the first occurrence list contains the replacement. |
| 6881 | * progressive == 2 means we are operating on the Reader |
| 6882 | * and since nodes are discarded we must copy all the time. |
| 6883 | */ |
| 6884 | if (((list == NULL) && (ent->owner == 0)) || |
| 6885 | (ctxt->parseMode == XML_PARSE_READER)) { |
| 6886 | xmlNodePtr nw = NULL, cur, firstChild = NULL; |
| 6887 | |
| 6888 | /* |
| 6889 | * when operating on a reader, the entities definitions |
| 6890 | * are always owning the entities subtree. |
| 6891 | if (ctxt->parseMode == XML_PARSE_READER) |
| 6892 | ent->owner = 1; |
| 6893 | */ |
| 6894 | |
| 6895 | cur = ent->children; |
| 6896 | while (cur != NULL) { |
| 6897 | nw = xmlDocCopyNode(cur, ctxt->myDoc, 1); |
| 6898 | if (nw != NULL) { |
| 6899 | if (nw->_private == NULL) |
| 6900 | nw->_private = cur->_private; |
| 6901 | if (firstChild == NULL){ |
| 6902 | firstChild = nw; |
| 6903 | } |
| 6904 | nw = xmlAddChild(ctxt->node, nw); |
| 6905 | } |
| 6906 | if (cur == ent->last) { |
| 6907 | /* |
| 6908 | * needed to detect some strange empty |
| 6909 | * node cases in the reader tests |
| 6910 | */ |
| 6911 | if ((ctxt->parseMode == XML_PARSE_READER) && |
| 6912 | (nw != NULL) && |
| 6913 | (nw->type == XML_ELEMENT_NODE) && |
| 6914 | (nw->children == NULL)) |
| 6915 | nw->extra = 1; |
| 6916 | |
| 6917 | break; |
| 6918 | } |
| 6919 | cur = cur->next; |
| 6920 | } |
| 6921 | #ifdef LIBXML_LEGACY_ENABLED |
| 6922 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 6923 | xmlAddEntityReference(ent, firstChild, nw); |
| 6924 | #endif /* LIBXML_LEGACY_ENABLED */ |
| 6925 | } else if (list == NULL) { |
| 6926 | xmlNodePtr nw = NULL, cur, next, last, |
| 6927 | firstChild = NULL; |
| 6928 | /* |
| 6929 | * Copy the entity child list and make it the new |
| 6930 | * entity child list. The goal is to make sure any |
| 6931 | * ID or REF referenced will be the one from the |
| 6932 | * document content and not the entity copy. |
| 6933 | */ |
| 6934 | cur = ent->children; |
| 6935 | ent->children = NULL; |
| 6936 | last = ent->last; |
| 6937 | ent->last = NULL; |
| 6938 | while (cur != NULL) { |
| 6939 | next = cur->next; |
| 6940 | cur->next = NULL; |
| 6941 | cur->parent = NULL; |
| 6942 | nw = xmlDocCopyNode(cur, ctxt->myDoc, 1); |
| 6943 | if (nw != NULL) { |
| 6944 | if (nw->_private == NULL) |
| 6945 | nw->_private = cur->_private; |
| 6946 | if (firstChild == NULL){ |
| 6947 | firstChild = cur; |
| 6948 | } |
| 6949 | xmlAddChild((xmlNodePtr) ent, nw); |
| 6950 | xmlAddChild(ctxt->node, cur); |
| 6951 | } |
| 6952 | if (cur == last) |
| 6953 | break; |
| 6954 | cur = next; |
| 6955 | } |
Daniel Veillard | cba6839 | 2008-08-29 12:43:40 +0000 | [diff] [blame] | 6956 | if (ent->owner == 0) |
| 6957 | ent->owner = 1; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 6958 | #ifdef LIBXML_LEGACY_ENABLED |
| 6959 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 6960 | xmlAddEntityReference(ent, firstChild, nw); |
| 6961 | #endif /* LIBXML_LEGACY_ENABLED */ |
| 6962 | } else { |
| 6963 | const xmlChar *nbktext; |
| 6964 | |
| 6965 | /* |
| 6966 | * the name change is to avoid coalescing of the |
| 6967 | * node with a possible previous text one which |
| 6968 | * would make ent->children a dangling pointer |
| 6969 | */ |
| 6970 | nbktext = xmlDictLookup(ctxt->dict, BAD_CAST "nbktext", |
| 6971 | -1); |
| 6972 | if (ent->children->type == XML_TEXT_NODE) |
| 6973 | ent->children->name = nbktext; |
| 6974 | if ((ent->last != ent->children) && |
| 6975 | (ent->last->type == XML_TEXT_NODE)) |
| 6976 | ent->last->name = nbktext; |
| 6977 | xmlAddChildList(ctxt->node, ent->children); |
| 6978 | } |
| 6979 | |
| 6980 | /* |
| 6981 | * This is to avoid a nasty side effect, see |
| 6982 | * characters() in SAX.c |
| 6983 | */ |
| 6984 | ctxt->nodemem = 0; |
| 6985 | ctxt->nodelen = 0; |
| 6986 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6987 | } |
| 6988 | } |
| 6989 | } |
| 6990 | |
| 6991 | /** |
| 6992 | * xmlParseEntityRef: |
| 6993 | * @ctxt: an XML parser context |
| 6994 | * |
| 6995 | * parse ENTITY references declarations |
| 6996 | * |
| 6997 | * [68] EntityRef ::= '&' Name ';' |
| 6998 | * |
| 6999 | * [ WFC: Entity Declared ] |
| 7000 | * In a document without any DTD, a document with only an internal DTD |
| 7001 | * subset which contains no parameter entity references, or a document |
| 7002 | * with "standalone='yes'", the Name given in the entity reference |
| 7003 | * must match that in an entity declaration, except that well-formed |
| 7004 | * documents need not declare any of the following entities: amp, lt, |
| 7005 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 7006 | * any reference to it. Similarly, the declaration of a general entity |
| 7007 | * must precede any reference to it which appears in a default value in an |
| 7008 | * attribute-list declaration. Note that if entities are declared in the |
| 7009 | * external subset or in external parameter entities, a non-validating |
| 7010 | * processor is not obligated to read and process their declarations; |
| 7011 | * for such documents, the rule that an entity must be declared is a |
| 7012 | * well-formedness constraint only if standalone='yes'. |
| 7013 | * |
| 7014 | * [ WFC: Parsed Entity ] |
| 7015 | * An entity reference must not contain the name of an unparsed entity |
| 7016 | * |
| 7017 | * Returns the xmlEntityPtr if found, or NULL otherwise. |
| 7018 | */ |
| 7019 | xmlEntityPtr |
| 7020 | xmlParseEntityRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7021 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7022 | xmlEntityPtr ent = NULL; |
| 7023 | |
| 7024 | GROW; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7025 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7026 | if (RAW != '&') |
| 7027 | return(NULL); |
| 7028 | NEXT; |
| 7029 | name = xmlParseName(ctxt); |
| 7030 | if (name == NULL) { |
| 7031 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7032 | "xmlParseEntityRef: no name\n"); |
| 7033 | return(NULL); |
| 7034 | } |
| 7035 | if (RAW != ';') { |
| 7036 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 7037 | return(NULL); |
| 7038 | } |
| 7039 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7040 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7041 | /* |
| 7042 | * Predefined entites override any extra definition |
| 7043 | */ |
| 7044 | ent = xmlGetPredefinedEntity(name); |
| 7045 | if (ent != NULL) |
| 7046 | return(ent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7047 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7048 | /* |
| 7049 | * Increate the number of entity references parsed |
| 7050 | */ |
| 7051 | ctxt->nbentities++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7052 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7053 | /* |
| 7054 | * Ask first SAX for entity resolution, otherwise try the |
| 7055 | * entities which may have stored in the parser context. |
| 7056 | */ |
| 7057 | if (ctxt->sax != NULL) { |
| 7058 | if (ctxt->sax->getEntity != NULL) |
| 7059 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
| 7060 | if ((ctxt->wellFormed == 1 ) && (ent == NULL) && |
| 7061 | (ctxt->userData==ctxt)) { |
| 7062 | ent = xmlSAX2GetEntity(ctxt, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7063 | } |
| 7064 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7065 | /* |
| 7066 | * [ WFC: Entity Declared ] |
| 7067 | * In a document without any DTD, a document with only an |
| 7068 | * internal DTD subset which contains no parameter entity |
| 7069 | * references, or a document with "standalone='yes'", the |
| 7070 | * Name given in the entity reference must match that in an |
| 7071 | * entity declaration, except that well-formed documents |
| 7072 | * need not declare any of the following entities: amp, lt, |
| 7073 | * gt, apos, quot. |
| 7074 | * The declaration of a parameter entity must precede any |
| 7075 | * reference to it. |
| 7076 | * Similarly, the declaration of a general entity must |
| 7077 | * precede any reference to it which appears in a default |
| 7078 | * value in an attribute-list declaration. Note that if |
| 7079 | * entities are declared in the external subset or in |
| 7080 | * external parameter entities, a non-validating processor |
| 7081 | * is not obligated to read and process their declarations; |
| 7082 | * for such documents, the rule that an entity must be |
| 7083 | * declared is a well-formedness constraint only if |
| 7084 | * standalone='yes'. |
| 7085 | */ |
| 7086 | if (ent == NULL) { |
| 7087 | if ((ctxt->standalone == 1) || |
| 7088 | ((ctxt->hasExternalSubset == 0) && |
| 7089 | (ctxt->hasPErefs == 0))) { |
| 7090 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7091 | "Entity '%s' not defined\n", name); |
| 7092 | } else { |
| 7093 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7094 | "Entity '%s' not defined\n", name); |
| 7095 | if ((ctxt->inSubset == 0) && |
| 7096 | (ctxt->sax != NULL) && |
| 7097 | (ctxt->sax->reference != NULL)) { |
| 7098 | ctxt->sax->reference(ctxt->userData, name); |
| 7099 | } |
| 7100 | } |
| 7101 | ctxt->valid = 0; |
| 7102 | } |
| 7103 | |
| 7104 | /* |
| 7105 | * [ WFC: Parsed Entity ] |
| 7106 | * An entity reference must not contain the name of an |
| 7107 | * unparsed entity |
| 7108 | */ |
| 7109 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 7110 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
| 7111 | "Entity reference to unparsed entity %s\n", name); |
| 7112 | } |
| 7113 | |
| 7114 | /* |
| 7115 | * [ WFC: No External Entity References ] |
| 7116 | * Attribute values cannot contain direct or indirect |
| 7117 | * entity references to external entities. |
| 7118 | */ |
| 7119 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7120 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
| 7121 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
| 7122 | "Attribute references external entity '%s'\n", name); |
| 7123 | } |
| 7124 | /* |
| 7125 | * [ WFC: No < in Attribute Values ] |
| 7126 | * The replacement text of any entity referred to directly or |
| 7127 | * indirectly in an attribute value (other than "<") must |
| 7128 | * not contain a <. |
| 7129 | */ |
| 7130 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7131 | (ent != NULL) && (ent->content != NULL) && |
| 7132 | (xmlStrchr(ent->content, '<'))) { |
| 7133 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
| 7134 | "'<' in entity '%s' is not allowed in attributes values\n", name); |
| 7135 | } |
| 7136 | |
| 7137 | /* |
| 7138 | * Internal check, no parameter entities here ... |
| 7139 | */ |
| 7140 | else { |
| 7141 | switch (ent->etype) { |
| 7142 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 7143 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 7144 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 7145 | "Attempt to reference the parameter entity '%s'\n", |
| 7146 | name); |
| 7147 | break; |
| 7148 | default: |
| 7149 | break; |
| 7150 | } |
| 7151 | } |
| 7152 | |
| 7153 | /* |
| 7154 | * [ WFC: No Recursion ] |
| 7155 | * A parsed entity must not contain a recursive reference |
| 7156 | * to itself, either directly or indirectly. |
| 7157 | * Done somewhere else |
| 7158 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7159 | return(ent); |
| 7160 | } |
| 7161 | |
| 7162 | /** |
| 7163 | * xmlParseStringEntityRef: |
| 7164 | * @ctxt: an XML parser context |
| 7165 | * @str: a pointer to an index in the string |
| 7166 | * |
| 7167 | * parse ENTITY references declarations, but this version parses it from |
| 7168 | * a string value. |
| 7169 | * |
| 7170 | * [68] EntityRef ::= '&' Name ';' |
| 7171 | * |
| 7172 | * [ WFC: Entity Declared ] |
| 7173 | * In a document without any DTD, a document with only an internal DTD |
| 7174 | * subset which contains no parameter entity references, or a document |
| 7175 | * with "standalone='yes'", the Name given in the entity reference |
| 7176 | * must match that in an entity declaration, except that well-formed |
| 7177 | * documents need not declare any of the following entities: amp, lt, |
| 7178 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 7179 | * any reference to it. Similarly, the declaration of a general entity |
| 7180 | * must precede any reference to it which appears in a default value in an |
| 7181 | * attribute-list declaration. Note that if entities are declared in the |
| 7182 | * external subset or in external parameter entities, a non-validating |
| 7183 | * processor is not obligated to read and process their declarations; |
| 7184 | * for such documents, the rule that an entity must be declared is a |
| 7185 | * well-formedness constraint only if standalone='yes'. |
| 7186 | * |
| 7187 | * [ WFC: Parsed Entity ] |
| 7188 | * An entity reference must not contain the name of an unparsed entity |
| 7189 | * |
| 7190 | * Returns the xmlEntityPtr if found, or NULL otherwise. The str pointer |
| 7191 | * is updated to the current location in the string. |
| 7192 | */ |
| 7193 | xmlEntityPtr |
| 7194 | xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) { |
| 7195 | xmlChar *name; |
| 7196 | const xmlChar *ptr; |
| 7197 | xmlChar cur; |
| 7198 | xmlEntityPtr ent = NULL; |
| 7199 | |
| 7200 | if ((str == NULL) || (*str == NULL)) |
| 7201 | return(NULL); |
| 7202 | ptr = *str; |
| 7203 | cur = *ptr; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7204 | if (cur != '&') |
| 7205 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7206 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7207 | ptr++; |
| 7208 | cur = *ptr; |
| 7209 | name = xmlParseStringName(ctxt, &ptr); |
| 7210 | if (name == NULL) { |
| 7211 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7212 | "xmlParseStringEntityRef: no name\n"); |
| 7213 | *str = ptr; |
| 7214 | return(NULL); |
| 7215 | } |
| 7216 | if (*ptr != ';') { |
| 7217 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 7218 | *str = ptr; |
| 7219 | return(NULL); |
| 7220 | } |
| 7221 | ptr++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7222 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7223 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7224 | /* |
| 7225 | * Predefined entites override any extra definition |
| 7226 | */ |
| 7227 | ent = xmlGetPredefinedEntity(name); |
| 7228 | if (ent != NULL) |
| 7229 | return(ent); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7230 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7231 | /* |
| 7232 | * Increate the number of entity references parsed |
| 7233 | */ |
| 7234 | ctxt->nbentities++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7235 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7236 | /* |
| 7237 | * Ask first SAX for entity resolution, otherwise try the |
| 7238 | * entities which may have stored in the parser context. |
| 7239 | */ |
| 7240 | if (ctxt->sax != NULL) { |
| 7241 | if (ctxt->sax->getEntity != NULL) |
| 7242 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
| 7243 | if ((ent == NULL) && (ctxt->userData==ctxt)) { |
| 7244 | ent = xmlSAX2GetEntity(ctxt, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7245 | } |
| 7246 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7247 | |
| 7248 | /* |
| 7249 | * [ WFC: Entity Declared ] |
| 7250 | * In a document without any DTD, a document with only an |
| 7251 | * internal DTD subset which contains no parameter entity |
| 7252 | * references, or a document with "standalone='yes'", the |
| 7253 | * Name given in the entity reference must match that in an |
| 7254 | * entity declaration, except that well-formed documents |
| 7255 | * need not declare any of the following entities: amp, lt, |
| 7256 | * gt, apos, quot. |
| 7257 | * The declaration of a parameter entity must precede any |
| 7258 | * reference to it. |
| 7259 | * Similarly, the declaration of a general entity must |
| 7260 | * precede any reference to it which appears in a default |
| 7261 | * value in an attribute-list declaration. Note that if |
| 7262 | * entities are declared in the external subset or in |
| 7263 | * external parameter entities, a non-validating processor |
| 7264 | * is not obligated to read and process their declarations; |
| 7265 | * for such documents, the rule that an entity must be |
| 7266 | * declared is a well-formedness constraint only if |
| 7267 | * standalone='yes'. |
| 7268 | */ |
| 7269 | if (ent == NULL) { |
| 7270 | if ((ctxt->standalone == 1) || |
| 7271 | ((ctxt->hasExternalSubset == 0) && |
| 7272 | (ctxt->hasPErefs == 0))) { |
| 7273 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7274 | "Entity '%s' not defined\n", name); |
| 7275 | } else { |
| 7276 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7277 | "Entity '%s' not defined\n", |
| 7278 | name); |
| 7279 | } |
| 7280 | /* TODO ? check regressions ctxt->valid = 0; */ |
| 7281 | } |
| 7282 | |
| 7283 | /* |
| 7284 | * [ WFC: Parsed Entity ] |
| 7285 | * An entity reference must not contain the name of an |
| 7286 | * unparsed entity |
| 7287 | */ |
| 7288 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
| 7289 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
| 7290 | "Entity reference to unparsed entity %s\n", name); |
| 7291 | } |
| 7292 | |
| 7293 | /* |
| 7294 | * [ WFC: No External Entity References ] |
| 7295 | * Attribute values cannot contain direct or indirect |
| 7296 | * entity references to external entities. |
| 7297 | */ |
| 7298 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7299 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
| 7300 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
| 7301 | "Attribute references external entity '%s'\n", name); |
| 7302 | } |
| 7303 | /* |
| 7304 | * [ WFC: No < in Attribute Values ] |
| 7305 | * The replacement text of any entity referred to directly or |
| 7306 | * indirectly in an attribute value (other than "<") must |
| 7307 | * not contain a <. |
| 7308 | */ |
| 7309 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 7310 | (ent != NULL) && (ent->content != NULL) && |
| 7311 | (xmlStrchr(ent->content, '<'))) { |
| 7312 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
| 7313 | "'<' in entity '%s' is not allowed in attributes values\n", |
| 7314 | name); |
| 7315 | } |
| 7316 | |
| 7317 | /* |
| 7318 | * Internal check, no parameter entities here ... |
| 7319 | */ |
| 7320 | else { |
| 7321 | switch (ent->etype) { |
| 7322 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 7323 | case XML_EXTERNAL_PARAMETER_ENTITY: |
| 7324 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 7325 | "Attempt to reference the parameter entity '%s'\n", |
| 7326 | name); |
| 7327 | break; |
| 7328 | default: |
| 7329 | break; |
| 7330 | } |
| 7331 | } |
| 7332 | |
| 7333 | /* |
| 7334 | * [ WFC: No Recursion ] |
| 7335 | * A parsed entity must not contain a recursive reference |
| 7336 | * to itself, either directly or indirectly. |
| 7337 | * Done somewhere else |
| 7338 | */ |
| 7339 | |
| 7340 | xmlFree(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7341 | *str = ptr; |
| 7342 | return(ent); |
| 7343 | } |
| 7344 | |
| 7345 | /** |
| 7346 | * xmlParsePEReference: |
| 7347 | * @ctxt: an XML parser context |
| 7348 | * |
| 7349 | * parse PEReference declarations |
| 7350 | * The entity content is handled directly by pushing it's content as |
| 7351 | * a new input stream. |
| 7352 | * |
| 7353 | * [69] PEReference ::= '%' Name ';' |
| 7354 | * |
| 7355 | * [ WFC: No Recursion ] |
| 7356 | * A parsed entity must not contain a recursive |
| 7357 | * reference to itself, either directly or indirectly. |
| 7358 | * |
| 7359 | * [ WFC: Entity Declared ] |
| 7360 | * In a document without any DTD, a document with only an internal DTD |
| 7361 | * subset which contains no parameter entity references, or a document |
| 7362 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 7363 | * entity must precede any reference to it... |
| 7364 | * |
| 7365 | * [ VC: Entity Declared ] |
| 7366 | * In a document with an external subset or external parameter entities |
| 7367 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 7368 | * must precede any reference to it... |
| 7369 | * |
| 7370 | * [ WFC: In DTD ] |
| 7371 | * Parameter-entity references may only appear in the DTD. |
| 7372 | * NOTE: misleading but this is handled. |
| 7373 | */ |
| 7374 | void |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 7375 | xmlParsePEReference(xmlParserCtxtPtr ctxt) |
| 7376 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7377 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7378 | xmlEntityPtr entity = NULL; |
| 7379 | xmlParserInputPtr input; |
| 7380 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7381 | if (RAW != '%') |
| 7382 | return; |
| 7383 | NEXT; |
| 7384 | name = xmlParseName(ctxt); |
| 7385 | if (name == NULL) { |
| 7386 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7387 | "xmlParsePEReference: no name\n"); |
| 7388 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7389 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7390 | if (RAW != ';') { |
| 7391 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 7392 | return; |
| 7393 | } |
| 7394 | |
| 7395 | NEXT; |
| 7396 | |
| 7397 | /* |
| 7398 | * Increate the number of entity references parsed |
| 7399 | */ |
| 7400 | ctxt->nbentities++; |
| 7401 | |
| 7402 | /* |
| 7403 | * Request the entity from SAX |
| 7404 | */ |
| 7405 | if ((ctxt->sax != NULL) && |
| 7406 | (ctxt->sax->getParameterEntity != NULL)) |
| 7407 | entity = ctxt->sax->getParameterEntity(ctxt->userData, |
| 7408 | name); |
| 7409 | if (entity == NULL) { |
| 7410 | /* |
| 7411 | * [ WFC: Entity Declared ] |
| 7412 | * In a document without any DTD, a document with only an |
| 7413 | * internal DTD subset which contains no parameter entity |
| 7414 | * references, or a document with "standalone='yes'", ... |
| 7415 | * ... The declaration of a parameter entity must precede |
| 7416 | * any reference to it... |
| 7417 | */ |
| 7418 | if ((ctxt->standalone == 1) || |
| 7419 | ((ctxt->hasExternalSubset == 0) && |
| 7420 | (ctxt->hasPErefs == 0))) { |
| 7421 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7422 | "PEReference: %%%s; not found\n", |
| 7423 | name); |
| 7424 | } else { |
| 7425 | /* |
| 7426 | * [ VC: Entity Declared ] |
| 7427 | * In a document with an external subset or external |
| 7428 | * parameter entities with "standalone='no'", ... |
| 7429 | * ... The declaration of a parameter entity must |
| 7430 | * precede any reference to it... |
| 7431 | */ |
| 7432 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7433 | "PEReference: %%%s; not found\n", |
| 7434 | name, NULL); |
| 7435 | ctxt->valid = 0; |
| 7436 | } |
| 7437 | } else { |
| 7438 | /* |
| 7439 | * Internal checking in case the entity quest barfed |
| 7440 | */ |
| 7441 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 7442 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 7443 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7444 | "Internal: %%%s; is not a parameter entity\n", |
| 7445 | name, NULL); |
| 7446 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 7447 | input = xmlNewBlanksWrapperInputStream(ctxt, entity); |
| 7448 | if (xmlPushInput(ctxt, input) < 0) |
| 7449 | return; |
| 7450 | } else { |
| 7451 | /* |
| 7452 | * TODO !!! |
| 7453 | * handle the extra spaces added before and after |
| 7454 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
| 7455 | */ |
| 7456 | input = xmlNewEntityInputStream(ctxt, entity); |
| 7457 | if (xmlPushInput(ctxt, input) < 0) |
| 7458 | return; |
| 7459 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
| 7460 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && |
| 7461 | (IS_BLANK_CH(NXT(5)))) { |
| 7462 | xmlParseTextDecl(ctxt); |
| 7463 | if (ctxt->errNo == |
| 7464 | XML_ERR_UNSUPPORTED_ENCODING) { |
| 7465 | /* |
| 7466 | * The XML REC instructs us to stop parsing |
| 7467 | * right here |
| 7468 | */ |
| 7469 | ctxt->instate = XML_PARSER_EOF; |
| 7470 | return; |
| 7471 | } |
| 7472 | } |
| 7473 | } |
| 7474 | } |
| 7475 | ctxt->hasPErefs = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7476 | } |
| 7477 | |
| 7478 | /** |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 7479 | * xmlLoadEntityContent: |
| 7480 | * @ctxt: an XML parser context |
| 7481 | * @entity: an unloaded system entity |
| 7482 | * |
| 7483 | * Load the original content of the given system entity from the |
| 7484 | * ExternalID/SystemID given. This is to be used for Included in Literal |
| 7485 | * http://www.w3.org/TR/REC-xml/#inliteral processing of entities references |
| 7486 | * |
| 7487 | * Returns 0 in case of success and -1 in case of failure |
| 7488 | */ |
| 7489 | static int |
| 7490 | xmlLoadEntityContent(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 7491 | xmlParserInputPtr input; |
| 7492 | xmlBufferPtr buf; |
| 7493 | int l, c; |
| 7494 | int count = 0; |
| 7495 | |
| 7496 | if ((ctxt == NULL) || (entity == NULL) || |
| 7497 | ((entity->etype != XML_EXTERNAL_PARAMETER_ENTITY) && |
| 7498 | (entity->etype != XML_EXTERNAL_GENERAL_PARSED_ENTITY)) || |
| 7499 | (entity->content != NULL)) { |
| 7500 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7501 | "xmlLoadEntityContent parameter error"); |
| 7502 | return(-1); |
| 7503 | } |
| 7504 | |
| 7505 | if (xmlParserDebugEntities) |
| 7506 | xmlGenericError(xmlGenericErrorContext, |
| 7507 | "Reading %s entity content input\n", entity->name); |
| 7508 | |
| 7509 | buf = xmlBufferCreate(); |
| 7510 | if (buf == NULL) { |
| 7511 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7512 | "xmlLoadEntityContent parameter error"); |
| 7513 | return(-1); |
| 7514 | } |
| 7515 | |
| 7516 | input = xmlNewEntityInputStream(ctxt, entity); |
| 7517 | if (input == NULL) { |
| 7518 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7519 | "xmlLoadEntityContent input error"); |
| 7520 | xmlBufferFree(buf); |
| 7521 | return(-1); |
| 7522 | } |
| 7523 | |
| 7524 | /* |
| 7525 | * Push the entity as the current input, read char by char |
| 7526 | * saving to the buffer until the end of the entity or an error |
| 7527 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 7528 | if (xmlPushInput(ctxt, input) < 0) { |
| 7529 | xmlBufferFree(buf); |
| 7530 | return(-1); |
| 7531 | } |
| 7532 | |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 7533 | GROW; |
| 7534 | c = CUR_CHAR(l); |
| 7535 | while ((ctxt->input == input) && (ctxt->input->cur < ctxt->input->end) && |
| 7536 | (IS_CHAR(c))) { |
| 7537 | xmlBufferAdd(buf, ctxt->input->cur, l); |
| 7538 | if (count++ > 100) { |
| 7539 | count = 0; |
| 7540 | GROW; |
| 7541 | } |
| 7542 | NEXTL(l); |
| 7543 | c = CUR_CHAR(l); |
| 7544 | } |
| 7545 | |
| 7546 | if ((ctxt->input == input) && (ctxt->input->cur >= ctxt->input->end)) { |
| 7547 | xmlPopInput(ctxt); |
| 7548 | } else if (!IS_CHAR(c)) { |
| 7549 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 7550 | "xmlLoadEntityContent: invalid char value %d\n", |
| 7551 | c); |
| 7552 | xmlBufferFree(buf); |
| 7553 | return(-1); |
| 7554 | } |
| 7555 | entity->content = buf->content; |
| 7556 | buf->content = NULL; |
| 7557 | xmlBufferFree(buf); |
| 7558 | |
| 7559 | return(0); |
| 7560 | } |
| 7561 | |
| 7562 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7563 | * xmlParseStringPEReference: |
| 7564 | * @ctxt: an XML parser context |
| 7565 | * @str: a pointer to an index in the string |
| 7566 | * |
| 7567 | * parse PEReference declarations |
| 7568 | * |
| 7569 | * [69] PEReference ::= '%' Name ';' |
| 7570 | * |
| 7571 | * [ WFC: No Recursion ] |
| 7572 | * A parsed entity must not contain a recursive |
Daniel Veillard | 8bf64ae | 2008-03-24 20:45:21 +0000 | [diff] [blame] | 7573 | * reference to itself, either directly or indirectly. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7574 | * |
| 7575 | * [ WFC: Entity Declared ] |
| 7576 | * In a document without any DTD, a document with only an internal DTD |
| 7577 | * subset which contains no parameter entity references, or a document |
| 7578 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 7579 | * entity must precede any reference to it... |
| 7580 | * |
| 7581 | * [ VC: Entity Declared ] |
| 7582 | * In a document with an external subset or external parameter entities |
| 7583 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 7584 | * must precede any reference to it... |
| 7585 | * |
| 7586 | * [ WFC: In DTD ] |
| 7587 | * Parameter-entity references may only appear in the DTD. |
| 7588 | * NOTE: misleading but this is handled. |
| 7589 | * |
| 7590 | * Returns the string of the entity content. |
| 7591 | * str is updated to the current value of the index |
| 7592 | */ |
| 7593 | xmlEntityPtr |
| 7594 | xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 7595 | const xmlChar *ptr; |
| 7596 | xmlChar cur; |
| 7597 | xmlChar *name; |
| 7598 | xmlEntityPtr entity = NULL; |
| 7599 | |
| 7600 | if ((str == NULL) || (*str == NULL)) return(NULL); |
| 7601 | ptr = *str; |
| 7602 | cur = *ptr; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7603 | if (cur != '%') |
| 7604 | return(NULL); |
| 7605 | ptr++; |
| 7606 | cur = *ptr; |
| 7607 | name = xmlParseStringName(ctxt, &ptr); |
| 7608 | if (name == NULL) { |
| 7609 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7610 | "xmlParseStringPEReference: no name\n"); |
| 7611 | *str = ptr; |
| 7612 | return(NULL); |
| 7613 | } |
| 7614 | cur = *ptr; |
| 7615 | if (cur != ';') { |
| 7616 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 7617 | xmlFree(name); |
| 7618 | *str = ptr; |
| 7619 | return(NULL); |
| 7620 | } |
| 7621 | ptr++; |
| 7622 | |
| 7623 | /* |
| 7624 | * Increate the number of entity references parsed |
| 7625 | */ |
| 7626 | ctxt->nbentities++; |
| 7627 | |
| 7628 | /* |
| 7629 | * Request the entity from SAX |
| 7630 | */ |
| 7631 | if ((ctxt->sax != NULL) && |
| 7632 | (ctxt->sax->getParameterEntity != NULL)) |
| 7633 | entity = ctxt->sax->getParameterEntity(ctxt->userData, |
| 7634 | name); |
| 7635 | if (entity == NULL) { |
| 7636 | /* |
| 7637 | * [ WFC: Entity Declared ] |
| 7638 | * In a document without any DTD, a document with only an |
| 7639 | * internal DTD subset which contains no parameter entity |
| 7640 | * references, or a document with "standalone='yes'", ... |
| 7641 | * ... The declaration of a parameter entity must precede |
| 7642 | * any reference to it... |
| 7643 | */ |
| 7644 | if ((ctxt->standalone == 1) || |
| 7645 | ((ctxt->hasExternalSubset == 0) && (ctxt->hasPErefs == 0))) { |
| 7646 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 7647 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7648 | } else { |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7649 | /* |
| 7650 | * [ VC: Entity Declared ] |
| 7651 | * In a document with an external subset or external |
| 7652 | * parameter entities with "standalone='no'", ... |
| 7653 | * ... The declaration of a parameter entity must |
| 7654 | * precede any reference to it... |
| 7655 | */ |
| 7656 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7657 | "PEReference: %%%s; not found\n", |
| 7658 | name, NULL); |
| 7659 | ctxt->valid = 0; |
| 7660 | } |
| 7661 | } else { |
| 7662 | /* |
| 7663 | * Internal checking in case the entity quest barfed |
| 7664 | */ |
| 7665 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 7666 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 7667 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 7668 | "%%%s; is not a parameter entity\n", |
| 7669 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7670 | } |
| 7671 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 7672 | ctxt->hasPErefs = 1; |
| 7673 | xmlFree(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7674 | *str = ptr; |
| 7675 | return(entity); |
| 7676 | } |
| 7677 | |
| 7678 | /** |
| 7679 | * xmlParseDocTypeDecl: |
| 7680 | * @ctxt: an XML parser context |
| 7681 | * |
| 7682 | * parse a DOCTYPE declaration |
| 7683 | * |
| 7684 | * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? |
| 7685 | * ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 7686 | * |
| 7687 | * [ VC: Root Element Type ] |
| 7688 | * The Name in the document type declaration must match the element |
| 7689 | * type of the root element. |
| 7690 | */ |
| 7691 | |
| 7692 | void |
| 7693 | xmlParseDocTypeDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7694 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7695 | xmlChar *ExternalID = NULL; |
| 7696 | xmlChar *URI = NULL; |
| 7697 | |
| 7698 | /* |
| 7699 | * We know that '<!DOCTYPE' has been detected. |
| 7700 | */ |
| 7701 | SKIP(9); |
| 7702 | |
| 7703 | SKIP_BLANKS; |
| 7704 | |
| 7705 | /* |
| 7706 | * Parse the DOCTYPE name. |
| 7707 | */ |
| 7708 | name = xmlParseName(ctxt); |
| 7709 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7710 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7711 | "xmlParseDocTypeDecl : no DOCTYPE name !\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7712 | } |
| 7713 | ctxt->intSubName = name; |
| 7714 | |
| 7715 | SKIP_BLANKS; |
| 7716 | |
| 7717 | /* |
| 7718 | * Check for SystemID and ExternalID |
| 7719 | */ |
| 7720 | URI = xmlParseExternalID(ctxt, &ExternalID, 1); |
| 7721 | |
| 7722 | if ((URI != NULL) || (ExternalID != NULL)) { |
| 7723 | ctxt->hasExternalSubset = 1; |
| 7724 | } |
| 7725 | ctxt->extSubURI = URI; |
| 7726 | ctxt->extSubSystem = ExternalID; |
| 7727 | |
| 7728 | SKIP_BLANKS; |
| 7729 | |
| 7730 | /* |
| 7731 | * Create and update the internal subset. |
| 7732 | */ |
| 7733 | if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) && |
| 7734 | (!ctxt->disableSAX)) |
| 7735 | ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI); |
| 7736 | |
| 7737 | /* |
| 7738 | * Is there any internal subset declarations ? |
| 7739 | * they are handled separately in xmlParseInternalSubset() |
| 7740 | */ |
| 7741 | if (RAW == '[') |
| 7742 | return; |
| 7743 | |
| 7744 | /* |
| 7745 | * We should be at the end of the DOCTYPE declaration. |
| 7746 | */ |
| 7747 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7748 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7749 | } |
| 7750 | NEXT; |
| 7751 | } |
| 7752 | |
| 7753 | /** |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 7754 | * xmlParseInternalSubset: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7755 | * @ctxt: an XML parser context |
| 7756 | * |
| 7757 | * parse the internal subset declaration |
| 7758 | * |
| 7759 | * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 7760 | */ |
| 7761 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 7762 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7763 | xmlParseInternalSubset(xmlParserCtxtPtr ctxt) { |
| 7764 | /* |
| 7765 | * Is there any DTD definition ? |
| 7766 | */ |
| 7767 | if (RAW == '[') { |
| 7768 | ctxt->instate = XML_PARSER_DTD; |
| 7769 | NEXT; |
| 7770 | /* |
| 7771 | * Parse the succession of Markup declarations and |
| 7772 | * PEReferences. |
| 7773 | * Subsequence (markupdecl | PEReference | S)* |
| 7774 | */ |
| 7775 | while (RAW != ']') { |
| 7776 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 7777 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7778 | |
| 7779 | SKIP_BLANKS; |
| 7780 | xmlParseMarkupDecl(ctxt); |
| 7781 | xmlParsePEReference(ctxt); |
| 7782 | |
| 7783 | /* |
| 7784 | * Pop-up of finished entities. |
| 7785 | */ |
| 7786 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 7787 | xmlPopInput(ctxt); |
| 7788 | |
| 7789 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7790 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7791 | "xmlParseInternalSubset: error detected in Markup declaration\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7792 | break; |
| 7793 | } |
| 7794 | } |
| 7795 | if (RAW == ']') { |
| 7796 | NEXT; |
| 7797 | SKIP_BLANKS; |
| 7798 | } |
| 7799 | } |
| 7800 | |
| 7801 | /* |
| 7802 | * We should be at the end of the DOCTYPE declaration. |
| 7803 | */ |
| 7804 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7805 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7806 | } |
| 7807 | NEXT; |
| 7808 | } |
| 7809 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 7810 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7811 | /** |
| 7812 | * xmlParseAttribute: |
| 7813 | * @ctxt: an XML parser context |
| 7814 | * @value: a xmlChar ** used to store the value of the attribute |
| 7815 | * |
| 7816 | * parse an attribute |
| 7817 | * |
| 7818 | * [41] Attribute ::= Name Eq AttValue |
| 7819 | * |
| 7820 | * [ WFC: No External Entity References ] |
| 7821 | * Attribute values cannot contain direct or indirect entity references |
| 7822 | * to external entities. |
| 7823 | * |
| 7824 | * [ WFC: No < in Attribute Values ] |
| 7825 | * The replacement text of any entity referred to directly or indirectly in |
| 7826 | * an attribute value (other than "<") must not contain a <. |
| 7827 | * |
| 7828 | * [ VC: Attribute Value Type ] |
| 7829 | * The attribute must have been declared; the value must be of the type |
| 7830 | * declared for it. |
| 7831 | * |
| 7832 | * [25] Eq ::= S? '=' S? |
| 7833 | * |
| 7834 | * With namespace: |
| 7835 | * |
| 7836 | * [NS 11] Attribute ::= QName Eq AttValue |
| 7837 | * |
| 7838 | * Also the case QName == xmlns:??? is handled independently as a namespace |
| 7839 | * definition. |
| 7840 | * |
| 7841 | * Returns the attribute name, and the value in *value. |
| 7842 | */ |
| 7843 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7844 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7845 | xmlParseAttribute(xmlParserCtxtPtr ctxt, xmlChar **value) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7846 | const xmlChar *name; |
| 7847 | xmlChar *val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7848 | |
| 7849 | *value = NULL; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 7850 | GROW; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7851 | name = xmlParseName(ctxt); |
| 7852 | if (name == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 7853 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7854 | "error parsing attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7855 | return(NULL); |
| 7856 | } |
| 7857 | |
| 7858 | /* |
| 7859 | * read the value |
| 7860 | */ |
| 7861 | SKIP_BLANKS; |
| 7862 | if (RAW == '=') { |
| 7863 | NEXT; |
| 7864 | SKIP_BLANKS; |
| 7865 | val = xmlParseAttValue(ctxt); |
| 7866 | ctxt->instate = XML_PARSER_CONTENT; |
| 7867 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 7868 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7869 | "Specification mandate value for attribute %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7870 | return(NULL); |
| 7871 | } |
| 7872 | |
| 7873 | /* |
| 7874 | * Check that xml:lang conforms to the specification |
| 7875 | * No more registered as an error, just generate a warning now |
| 7876 | * since this was deprecated in XML second edition |
| 7877 | */ |
| 7878 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) { |
| 7879 | if (!xmlCheckLanguageID(val)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 7880 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 7881 | "Malformed value for xml:lang : %s\n", |
| 7882 | val, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7883 | } |
| 7884 | } |
| 7885 | |
| 7886 | /* |
| 7887 | * Check that xml:space conforms to the specification |
| 7888 | */ |
| 7889 | if (xmlStrEqual(name, BAD_CAST "xml:space")) { |
| 7890 | if (xmlStrEqual(val, BAD_CAST "default")) |
| 7891 | *(ctxt->space) = 0; |
| 7892 | else if (xmlStrEqual(val, BAD_CAST "preserve")) |
| 7893 | *(ctxt->space) = 1; |
| 7894 | else { |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 7895 | xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE, |
Daniel Veillard | 642104e | 2003-03-26 16:32:05 +0000 | [diff] [blame] | 7896 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 7897 | val, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7898 | } |
| 7899 | } |
| 7900 | |
| 7901 | *value = val; |
| 7902 | return(name); |
| 7903 | } |
| 7904 | |
| 7905 | /** |
| 7906 | * xmlParseStartTag: |
| 7907 | * @ctxt: an XML parser context |
| 7908 | * |
| 7909 | * parse a start of tag either for rule element or |
| 7910 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 7911 | * |
| 7912 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 7913 | * |
| 7914 | * [ WFC: Unique Att Spec ] |
| 7915 | * No attribute name may appear more than once in the same start-tag or |
| 7916 | * empty-element tag. |
| 7917 | * |
| 7918 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 7919 | * |
| 7920 | * [ WFC: Unique Att Spec ] |
| 7921 | * No attribute name may appear more than once in the same start-tag or |
| 7922 | * empty-element tag. |
| 7923 | * |
| 7924 | * With namespace: |
| 7925 | * |
| 7926 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 7927 | * |
| 7928 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 7929 | * |
| 7930 | * Returns the element name parsed |
| 7931 | */ |
| 7932 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7933 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7934 | xmlParseStartTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7935 | const xmlChar *name; |
| 7936 | const xmlChar *attname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7937 | xmlChar *attvalue; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 7938 | const xmlChar **atts = ctxt->atts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7939 | int nbatts = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 7940 | int maxatts = ctxt->maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7941 | int i; |
| 7942 | |
| 7943 | if (RAW != '<') return(NULL); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 7944 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7945 | |
| 7946 | name = xmlParseName(ctxt); |
| 7947 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7948 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7949 | "xmlParseStartTag: invalid element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7950 | return(NULL); |
| 7951 | } |
| 7952 | |
| 7953 | /* |
| 7954 | * Now parse the attributes, it ends up with the ending |
| 7955 | * |
| 7956 | * (S Attribute)* S? |
| 7957 | */ |
| 7958 | SKIP_BLANKS; |
| 7959 | GROW; |
| 7960 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 7961 | while ((RAW != '>') && |
| 7962 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 7963 | (IS_BYTE_CHAR(RAW))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7964 | const xmlChar *q = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 7965 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7966 | |
| 7967 | attname = xmlParseAttribute(ctxt, &attvalue); |
| 7968 | if ((attname != NULL) && (attvalue != NULL)) { |
| 7969 | /* |
| 7970 | * [ WFC: Unique Att Spec ] |
| 7971 | * No attribute name may appear more than once in the same |
| 7972 | * start-tag or empty-element tag. |
| 7973 | */ |
| 7974 | for (i = 0; i < nbatts;i += 2) { |
| 7975 | if (xmlStrEqual(atts[i], attname)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7976 | xmlErrAttributeDup(ctxt, NULL, attname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7977 | xmlFree(attvalue); |
| 7978 | goto failed; |
| 7979 | } |
| 7980 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7981 | /* |
| 7982 | * Add the pair to atts |
| 7983 | */ |
| 7984 | if (atts == NULL) { |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 7985 | maxatts = 22; /* allow for 10 attrs by default */ |
| 7986 | atts = (const xmlChar **) |
| 7987 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7988 | if (atts == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7989 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 7990 | if (attvalue != NULL) |
| 7991 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 7992 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7993 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 7994 | ctxt->atts = atts; |
| 7995 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7996 | } else if (nbatts + 4 > maxatts) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 7997 | const xmlChar **n; |
| 7998 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7999 | maxatts *= 2; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8000 | n = (const xmlChar **) xmlRealloc((void *) atts, |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8001 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8002 | if (n == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8003 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8004 | if (attvalue != NULL) |
| 8005 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8006 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8007 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8008 | atts = n; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8009 | ctxt->atts = atts; |
| 8010 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8011 | } |
| 8012 | atts[nbatts++] = attname; |
| 8013 | atts[nbatts++] = attvalue; |
| 8014 | atts[nbatts] = NULL; |
| 8015 | atts[nbatts + 1] = NULL; |
| 8016 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8017 | if (attvalue != NULL) |
| 8018 | xmlFree(attvalue); |
| 8019 | } |
| 8020 | |
| 8021 | failed: |
| 8022 | |
Daniel Veillard | 3772de3 | 2002-12-17 10:31:45 +0000 | [diff] [blame] | 8023 | GROW |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8024 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 8025 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8026 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8027 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 8028 | "attributes construct error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8029 | } |
| 8030 | SKIP_BLANKS; |
Daniel Veillard | 02111c1 | 2003-02-24 19:14:52 +0000 | [diff] [blame] | 8031 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 8032 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8033 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8034 | "xmlParseStartTag: problem parsing attributes\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8035 | break; |
| 8036 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8037 | SHRINK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8038 | GROW; |
| 8039 | } |
| 8040 | |
| 8041 | /* |
| 8042 | * SAX: Start of Element ! |
| 8043 | */ |
| 8044 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) && |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 8045 | (!ctxt->disableSAX)) { |
| 8046 | if (nbatts > 0) |
| 8047 | ctxt->sax->startElement(ctxt->userData, name, atts); |
| 8048 | else |
| 8049 | ctxt->sax->startElement(ctxt->userData, name, NULL); |
| 8050 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8051 | |
| 8052 | if (atts != NULL) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8053 | /* Free only the content strings */ |
| 8054 | for (i = 1;i < nbatts;i+=2) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8055 | if (atts[i] != NULL) |
| 8056 | xmlFree((xmlChar *) atts[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8057 | } |
| 8058 | return(name); |
| 8059 | } |
| 8060 | |
| 8061 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8062 | * xmlParseEndTag1: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8063 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8064 | * @line: line of the start tag |
| 8065 | * @nsNr: number of namespaces on the start tag |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8066 | * |
| 8067 | * parse an end of tag |
| 8068 | * |
| 8069 | * [42] ETag ::= '</' Name S? '>' |
| 8070 | * |
| 8071 | * With namespace |
| 8072 | * |
| 8073 | * [NS 9] ETag ::= '</' QName S? '>' |
| 8074 | */ |
| 8075 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8076 | static void |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8077 | xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8078 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8079 | |
| 8080 | GROW; |
| 8081 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8082 | xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8083 | "xmlParseEndTag: '</' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8084 | return; |
| 8085 | } |
| 8086 | SKIP(2); |
| 8087 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8088 | name = xmlParseNameAndCompare(ctxt,ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8089 | |
| 8090 | /* |
| 8091 | * We should definitely be at the ending "S? '>'" part |
| 8092 | */ |
| 8093 | GROW; |
| 8094 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 8095 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8096 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8097 | } else |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8098 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8099 | |
| 8100 | /* |
| 8101 | * [ WFC: Element Type Match ] |
| 8102 | * The Name in an element's end-tag must match the element type in the |
| 8103 | * start-tag. |
| 8104 | * |
| 8105 | */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8106 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8107 | if (name == NULL) name = BAD_CAST "unparseable"; |
| 8108 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8109 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8110 | ctxt->name, line, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8111 | } |
| 8112 | |
| 8113 | /* |
| 8114 | * SAX: End of Tag |
| 8115 | */ |
| 8116 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 8117 | (!ctxt->disableSAX)) |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 8118 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8119 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8120 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8121 | spacePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8122 | return; |
| 8123 | } |
| 8124 | |
| 8125 | /** |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8126 | * xmlParseEndTag: |
| 8127 | * @ctxt: an XML parser context |
| 8128 | * |
| 8129 | * parse an end of tag |
| 8130 | * |
| 8131 | * [42] ETag ::= '</' Name S? '>' |
| 8132 | * |
| 8133 | * With namespace |
| 8134 | * |
| 8135 | * [NS 9] ETag ::= '</' QName S? '>' |
| 8136 | */ |
| 8137 | |
| 8138 | void |
| 8139 | xmlParseEndTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8140 | xmlParseEndTag1(ctxt, 0); |
| 8141 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8142 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8143 | |
| 8144 | /************************************************************************ |
| 8145 | * * |
| 8146 | * SAX 2 specific operations * |
| 8147 | * * |
| 8148 | ************************************************************************/ |
| 8149 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8150 | /* |
| 8151 | * xmlGetNamespace: |
| 8152 | * @ctxt: an XML parser context |
| 8153 | * @prefix: the prefix to lookup |
| 8154 | * |
| 8155 | * Lookup the namespace name for the @prefix (which ca be NULL) |
| 8156 | * The prefix must come from the @ctxt->dict dictionnary |
| 8157 | * |
| 8158 | * Returns the namespace name or NULL if not bound |
| 8159 | */ |
| 8160 | static const xmlChar * |
| 8161 | xmlGetNamespace(xmlParserCtxtPtr ctxt, const xmlChar *prefix) { |
| 8162 | int i; |
| 8163 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8164 | if (prefix == ctxt->str_xml) return(ctxt->str_xml_ns); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8165 | for (i = ctxt->nsNr - 2;i >= 0;i-=2) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8166 | if (ctxt->nsTab[i] == prefix) { |
| 8167 | if ((prefix == NULL) && (*ctxt->nsTab[i + 1] == 0)) |
| 8168 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8169 | return(ctxt->nsTab[i + 1]); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8170 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8171 | return(NULL); |
| 8172 | } |
| 8173 | |
| 8174 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8175 | * xmlParseQName: |
| 8176 | * @ctxt: an XML parser context |
| 8177 | * @prefix: pointer to store the prefix part |
| 8178 | * |
| 8179 | * parse an XML Namespace QName |
| 8180 | * |
| 8181 | * [6] QName ::= (Prefix ':')? LocalPart |
| 8182 | * [7] Prefix ::= NCName |
| 8183 | * [8] LocalPart ::= NCName |
| 8184 | * |
| 8185 | * Returns the Name parsed or NULL |
| 8186 | */ |
| 8187 | |
| 8188 | static const xmlChar * |
| 8189 | xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) { |
| 8190 | const xmlChar *l, *p; |
| 8191 | |
| 8192 | GROW; |
| 8193 | |
| 8194 | l = xmlParseNCName(ctxt); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8195 | if (l == NULL) { |
| 8196 | if (CUR == ':') { |
| 8197 | l = xmlParseName(ctxt); |
| 8198 | if (l != NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8199 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 8200 | "Failed to parse QName '%s'\n", l, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8201 | *prefix = NULL; |
| 8202 | return(l); |
| 8203 | } |
| 8204 | } |
| 8205 | return(NULL); |
| 8206 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8207 | if (CUR == ':') { |
| 8208 | NEXT; |
| 8209 | p = l; |
| 8210 | l = xmlParseNCName(ctxt); |
| 8211 | if (l == NULL) { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8212 | xmlChar *tmp; |
| 8213 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8214 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 8215 | "Failed to parse QName '%s:'\n", p, NULL, NULL); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8216 | l = xmlParseNmtoken(ctxt); |
| 8217 | if (l == NULL) |
| 8218 | tmp = xmlBuildQName(BAD_CAST "", p, NULL, 0); |
| 8219 | else { |
| 8220 | tmp = xmlBuildQName(l, p, NULL, 0); |
| 8221 | xmlFree((char *)l); |
| 8222 | } |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8223 | p = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8224 | if (tmp != NULL) xmlFree(tmp); |
| 8225 | *prefix = NULL; |
| 8226 | return(p); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8227 | } |
| 8228 | if (CUR == ':') { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8229 | xmlChar *tmp; |
| 8230 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8231 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 8232 | "Failed to parse QName '%s:%s:'\n", p, l, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8233 | NEXT; |
| 8234 | tmp = (xmlChar *) xmlParseName(ctxt); |
| 8235 | if (tmp != NULL) { |
| 8236 | tmp = xmlBuildQName(tmp, l, NULL, 0); |
| 8237 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8238 | if (tmp != NULL) xmlFree(tmp); |
| 8239 | *prefix = p; |
| 8240 | return(l); |
| 8241 | } |
| 8242 | tmp = xmlBuildQName(BAD_CAST "", l, NULL, 0); |
| 8243 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 8244 | if (tmp != NULL) xmlFree(tmp); |
| 8245 | *prefix = p; |
| 8246 | return(l); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8247 | } |
| 8248 | *prefix = p; |
| 8249 | } else |
| 8250 | *prefix = NULL; |
| 8251 | return(l); |
| 8252 | } |
| 8253 | |
| 8254 | /** |
| 8255 | * xmlParseQNameAndCompare: |
| 8256 | * @ctxt: an XML parser context |
| 8257 | * @name: the localname |
| 8258 | * @prefix: the prefix, if any. |
| 8259 | * |
| 8260 | * parse an XML name and compares for match |
| 8261 | * (specialized for endtag parsing) |
| 8262 | * |
| 8263 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 8264 | * and the name for mismatch |
| 8265 | */ |
| 8266 | |
| 8267 | static const xmlChar * |
| 8268 | xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name, |
| 8269 | xmlChar const *prefix) { |
| 8270 | const xmlChar *cmp = name; |
| 8271 | const xmlChar *in; |
| 8272 | const xmlChar *ret; |
| 8273 | const xmlChar *prefix2; |
| 8274 | |
| 8275 | if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name)); |
| 8276 | |
| 8277 | GROW; |
| 8278 | in = ctxt->input->cur; |
| 8279 | |
| 8280 | cmp = prefix; |
| 8281 | while (*in != 0 && *in == *cmp) { |
| 8282 | ++in; |
| 8283 | ++cmp; |
| 8284 | } |
| 8285 | if ((*cmp == 0) && (*in == ':')) { |
| 8286 | in++; |
| 8287 | cmp = name; |
| 8288 | while (*in != 0 && *in == *cmp) { |
| 8289 | ++in; |
| 8290 | ++cmp; |
| 8291 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8292 | if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8293 | /* success */ |
| 8294 | ctxt->input->cur = in; |
| 8295 | return((const xmlChar*) 1); |
| 8296 | } |
| 8297 | } |
| 8298 | /* |
| 8299 | * all strings coms from the dictionary, equality can be done directly |
| 8300 | */ |
| 8301 | ret = xmlParseQName (ctxt, &prefix2); |
| 8302 | if ((ret == name) && (prefix == prefix2)) |
| 8303 | return((const xmlChar*) 1); |
| 8304 | return ret; |
| 8305 | } |
| 8306 | |
| 8307 | /** |
| 8308 | * xmlParseAttValueInternal: |
| 8309 | * @ctxt: an XML parser context |
| 8310 | * @len: attribute len result |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8311 | * @alloc: whether the attribute was reallocated as a new string |
| 8312 | * @normalize: if 1 then further non-CDATA normalization must be done |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8313 | * |
| 8314 | * parse a value for an attribute. |
| 8315 | * NOTE: if no normalization is needed, the routine will return pointers |
| 8316 | * directly from the data buffer. |
| 8317 | * |
| 8318 | * 3.3.3 Attribute-Value Normalization: |
| 8319 | * Before the value of an attribute is passed to the application or |
| 8320 | * checked for validity, the XML processor must normalize it as follows: |
| 8321 | * - a character reference is processed by appending the referenced |
| 8322 | * character to the attribute value |
| 8323 | * - an entity reference is processed by recursively processing the |
| 8324 | * replacement text of the entity |
| 8325 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 8326 | * appending #x20 to the normalized value, except that only a single |
| 8327 | * #x20 is appended for a "#xD#xA" sequence that is part of an external |
| 8328 | * parsed entity or the literal entity value of an internal parsed entity |
| 8329 | * - other characters are processed by appending them to the normalized value |
| 8330 | * If the declared value is not CDATA, then the XML processor must further |
| 8331 | * process the normalized attribute value by discarding any leading and |
| 8332 | * trailing space (#x20) characters, and by replacing sequences of space |
| 8333 | * (#x20) characters by a single space (#x20) character. |
| 8334 | * All attributes for which no declaration has been read should be treated |
| 8335 | * by a non-validating parser as if declared CDATA. |
| 8336 | * |
| 8337 | * Returns the AttValue parsed or NULL. The value has to be freed by the |
| 8338 | * caller if it was copied, this can be detected by val[*len] == 0. |
| 8339 | */ |
| 8340 | |
| 8341 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8342 | xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc, |
| 8343 | int normalize) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8344 | { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8345 | xmlChar limit = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8346 | const xmlChar *in = NULL, *start, *end, *last; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8347 | xmlChar *ret = NULL; |
| 8348 | |
| 8349 | GROW; |
| 8350 | in = (xmlChar *) CUR_PTR; |
| 8351 | if (*in != '"' && *in != '\'') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8352 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8353 | return (NULL); |
| 8354 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8355 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8356 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8357 | /* |
| 8358 | * try to handle in this routine the most common case where no |
| 8359 | * allocation of a new string is required and where content is |
| 8360 | * pure ASCII. |
| 8361 | */ |
| 8362 | limit = *in++; |
| 8363 | end = ctxt->input->end; |
| 8364 | start = in; |
| 8365 | if (in >= end) { |
| 8366 | const xmlChar *oldbase = ctxt->input->base; |
| 8367 | GROW; |
| 8368 | if (oldbase != ctxt->input->base) { |
| 8369 | long delta = ctxt->input->base - oldbase; |
| 8370 | start = start + delta; |
| 8371 | in = in + delta; |
| 8372 | } |
| 8373 | end = ctxt->input->end; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8374 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8375 | if (normalize) { |
| 8376 | /* |
| 8377 | * Skip any leading spaces |
| 8378 | */ |
| 8379 | while ((in < end) && (*in != limit) && |
| 8380 | ((*in == 0x20) || (*in == 0x9) || |
| 8381 | (*in == 0xA) || (*in == 0xD))) { |
| 8382 | in++; |
| 8383 | start = in; |
| 8384 | if (in >= end) { |
| 8385 | const xmlChar *oldbase = ctxt->input->base; |
| 8386 | GROW; |
| 8387 | if (oldbase != ctxt->input->base) { |
| 8388 | long delta = ctxt->input->base - oldbase; |
| 8389 | start = start + delta; |
| 8390 | in = in + delta; |
| 8391 | } |
| 8392 | end = ctxt->input->end; |
| 8393 | } |
| 8394 | } |
| 8395 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 8396 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 8397 | if ((*in++ == 0x20) && (*in == 0x20)) break; |
| 8398 | if (in >= end) { |
| 8399 | const xmlChar *oldbase = ctxt->input->base; |
| 8400 | GROW; |
| 8401 | if (oldbase != ctxt->input->base) { |
| 8402 | long delta = ctxt->input->base - oldbase; |
| 8403 | start = start + delta; |
| 8404 | in = in + delta; |
| 8405 | } |
| 8406 | end = ctxt->input->end; |
| 8407 | } |
| 8408 | } |
| 8409 | last = in; |
| 8410 | /* |
| 8411 | * skip the trailing blanks |
| 8412 | */ |
Daniel Veillard | c6e20e4 | 2003-09-11 16:30:26 +0000 | [diff] [blame] | 8413 | while ((last[-1] == 0x20) && (last > start)) last--; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8414 | while ((in < end) && (*in != limit) && |
| 8415 | ((*in == 0x20) || (*in == 0x9) || |
| 8416 | (*in == 0xA) || (*in == 0xD))) { |
| 8417 | in++; |
| 8418 | if (in >= end) { |
| 8419 | const xmlChar *oldbase = ctxt->input->base; |
| 8420 | GROW; |
| 8421 | if (oldbase != ctxt->input->base) { |
| 8422 | long delta = ctxt->input->base - oldbase; |
| 8423 | start = start + delta; |
| 8424 | in = in + delta; |
| 8425 | last = last + delta; |
| 8426 | } |
| 8427 | end = ctxt->input->end; |
| 8428 | } |
| 8429 | } |
| 8430 | if (*in != limit) goto need_complex; |
| 8431 | } else { |
| 8432 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 8433 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 8434 | in++; |
| 8435 | if (in >= end) { |
| 8436 | const xmlChar *oldbase = ctxt->input->base; |
| 8437 | GROW; |
| 8438 | if (oldbase != ctxt->input->base) { |
| 8439 | long delta = ctxt->input->base - oldbase; |
| 8440 | start = start + delta; |
| 8441 | in = in + delta; |
| 8442 | } |
| 8443 | end = ctxt->input->end; |
| 8444 | } |
| 8445 | } |
| 8446 | last = in; |
| 8447 | if (*in != limit) goto need_complex; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8448 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8449 | in++; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8450 | if (len != NULL) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8451 | *len = last - start; |
| 8452 | ret = (xmlChar *) start; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8453 | } else { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8454 | if (alloc) *alloc = 1; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8455 | ret = xmlStrndup(start, last - start); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8456 | } |
| 8457 | CUR_PTR = in; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8458 | if (alloc) *alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8459 | return ret; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8460 | need_complex: |
| 8461 | if (alloc) *alloc = 1; |
| 8462 | return xmlParseAttValueComplex(ctxt, len, normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8463 | } |
| 8464 | |
| 8465 | /** |
| 8466 | * xmlParseAttribute2: |
| 8467 | * @ctxt: an XML parser context |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8468 | * @pref: the element prefix |
| 8469 | * @elem: the element name |
| 8470 | * @prefix: a xmlChar ** used to store the value of the attribute prefix |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8471 | * @value: a xmlChar ** used to store the value of the attribute |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8472 | * @len: an int * to save the length of the attribute |
| 8473 | * @alloc: an int * to indicate if the attribute was allocated |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8474 | * |
| 8475 | * parse an attribute in the new SAX2 framework. |
| 8476 | * |
| 8477 | * Returns the attribute name, and the value in *value, . |
| 8478 | */ |
| 8479 | |
| 8480 | static const xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8481 | xmlParseAttribute2(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8482 | const xmlChar * pref, const xmlChar * elem, |
| 8483 | const xmlChar ** prefix, xmlChar ** value, |
| 8484 | int *len, int *alloc) |
| 8485 | { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8486 | const xmlChar *name; |
Daniel Veillard | d892557 | 2005-06-08 22:34:55 +0000 | [diff] [blame] | 8487 | xmlChar *val, *internal_val = NULL; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8488 | int normalize = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8489 | |
| 8490 | *value = NULL; |
| 8491 | GROW; |
| 8492 | name = xmlParseQName(ctxt, prefix); |
| 8493 | if (name == NULL) { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8494 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 8495 | "error parsing attribute name\n"); |
| 8496 | return (NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8497 | } |
| 8498 | |
| 8499 | /* |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8500 | * get the type if needed |
| 8501 | */ |
| 8502 | if (ctxt->attsSpecial != NULL) { |
| 8503 | int type; |
| 8504 | |
| 8505 | type = (int) (long) xmlHashQLookup2(ctxt->attsSpecial, |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8506 | pref, elem, *prefix, name); |
| 8507 | if (type != 0) |
| 8508 | normalize = 1; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8509 | } |
| 8510 | |
| 8511 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8512 | * read the value |
| 8513 | */ |
| 8514 | SKIP_BLANKS; |
| 8515 | if (RAW == '=') { |
| 8516 | NEXT; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8517 | SKIP_BLANKS; |
| 8518 | val = xmlParseAttValueInternal(ctxt, len, alloc, normalize); |
| 8519 | if (normalize) { |
| 8520 | /* |
| 8521 | * Sometimes a second normalisation pass for spaces is needed |
| 8522 | * but that only happens if charrefs or entities refernces |
| 8523 | * have been used in the attribute value, i.e. the attribute |
| 8524 | * value have been extracted in an allocated string already. |
| 8525 | */ |
| 8526 | if (*alloc) { |
| 8527 | const xmlChar *val2; |
| 8528 | |
| 8529 | val2 = xmlAttrNormalizeSpace2(ctxt, val, len); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8530 | if ((val2 != NULL) && (val2 != val)) { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8531 | xmlFree(val); |
Daniel Veillard | 6a31b83 | 2008-03-26 14:06:44 +0000 | [diff] [blame] | 8532 | val = (xmlChar *) val2; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8533 | } |
| 8534 | } |
| 8535 | } |
| 8536 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8537 | } else { |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8538 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
| 8539 | "Specification mandate value for attribute %s\n", |
| 8540 | name); |
| 8541 | return (NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8542 | } |
| 8543 | |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8544 | if (*prefix == ctxt->str_xml) { |
| 8545 | /* |
| 8546 | * Check that xml:lang conforms to the specification |
| 8547 | * No more registered as an error, just generate a warning now |
| 8548 | * since this was deprecated in XML second edition |
| 8549 | */ |
| 8550 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "lang"))) { |
| 8551 | internal_val = xmlStrndup(val, *len); |
| 8552 | if (!xmlCheckLanguageID(internal_val)) { |
| 8553 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 8554 | "Malformed value for xml:lang : %s\n", |
| 8555 | internal_val, NULL); |
| 8556 | } |
| 8557 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8558 | |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8559 | /* |
| 8560 | * Check that xml:space conforms to the specification |
| 8561 | */ |
| 8562 | if (xmlStrEqual(name, BAD_CAST "space")) { |
| 8563 | internal_val = xmlStrndup(val, *len); |
| 8564 | if (xmlStrEqual(internal_val, BAD_CAST "default")) |
| 8565 | *(ctxt->space) = 0; |
| 8566 | else if (xmlStrEqual(internal_val, BAD_CAST "preserve")) |
| 8567 | *(ctxt->space) = 1; |
| 8568 | else { |
| 8569 | xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE, |
| 8570 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
| 8571 | internal_val, NULL); |
| 8572 | } |
| 8573 | } |
| 8574 | if (internal_val) { |
| 8575 | xmlFree(internal_val); |
| 8576 | } |
| 8577 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8578 | |
| 8579 | *value = val; |
Daniel Veillard | 97c9ce2 | 2008-03-25 16:52:41 +0000 | [diff] [blame] | 8580 | return (name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8581 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8582 | /** |
| 8583 | * xmlParseStartTag2: |
| 8584 | * @ctxt: an XML parser context |
| 8585 | * |
| 8586 | * parse a start of tag either for rule element or |
| 8587 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 8588 | * This routine is called when running SAX2 parsing |
| 8589 | * |
| 8590 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 8591 | * |
| 8592 | * [ WFC: Unique Att Spec ] |
| 8593 | * No attribute name may appear more than once in the same start-tag or |
| 8594 | * empty-element tag. |
| 8595 | * |
| 8596 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 8597 | * |
| 8598 | * [ WFC: Unique Att Spec ] |
| 8599 | * No attribute name may appear more than once in the same start-tag or |
| 8600 | * empty-element tag. |
| 8601 | * |
| 8602 | * With namespace: |
| 8603 | * |
| 8604 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 8605 | * |
| 8606 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 8607 | * |
| 8608 | * Returns the element name parsed |
| 8609 | */ |
| 8610 | |
| 8611 | static const xmlChar * |
| 8612 | xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 8613 | const xmlChar **URI, int *tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8614 | const xmlChar *localname; |
| 8615 | const xmlChar *prefix; |
| 8616 | const xmlChar *attname; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8617 | const xmlChar *aprefix; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8618 | const xmlChar *nsname; |
| 8619 | xmlChar *attvalue; |
| 8620 | const xmlChar **atts = ctxt->atts; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8621 | int maxatts = ctxt->maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8622 | int nratts, nbatts, nbdef; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 8623 | int i, j, nbNs, attval, oldline, oldcol; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8624 | const xmlChar *base; |
| 8625 | unsigned long cur; |
Daniel Veillard | 365cf67 | 2005-06-09 08:18:24 +0000 | [diff] [blame] | 8626 | int nsNr = ctxt->nsNr; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8627 | |
| 8628 | if (RAW != '<') return(NULL); |
| 8629 | NEXT1; |
| 8630 | |
| 8631 | /* |
| 8632 | * NOTE: it is crucial with the SAX2 API to never call SHRINK beyond that |
| 8633 | * point since the attribute values may be stored as pointers to |
| 8634 | * the buffer and calling SHRINK would destroy them ! |
| 8635 | * The Shrinking is only possible once the full set of attribute |
| 8636 | * callbacks have been done. |
| 8637 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8638 | reparse: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8639 | SHRINK; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8640 | base = ctxt->input->base; |
| 8641 | cur = ctxt->input->cur - ctxt->input->base; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 8642 | oldline = ctxt->input->line; |
| 8643 | oldcol = ctxt->input->col; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8644 | nbatts = 0; |
| 8645 | nratts = 0; |
| 8646 | nbdef = 0; |
| 8647 | nbNs = 0; |
| 8648 | attval = 0; |
Daniel Veillard | 365cf67 | 2005-06-09 08:18:24 +0000 | [diff] [blame] | 8649 | /* Forget any namespaces added during an earlier parse of this element. */ |
| 8650 | ctxt->nsNr = nsNr; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8651 | |
| 8652 | localname = xmlParseQName(ctxt, &prefix); |
| 8653 | if (localname == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8654 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 8655 | "StartTag: invalid element name\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8656 | return(NULL); |
| 8657 | } |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 8658 | *tlen = ctxt->input->cur - ctxt->input->base - cur; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8659 | |
| 8660 | /* |
| 8661 | * Now parse the attributes, it ends up with the ending |
| 8662 | * |
| 8663 | * (S Attribute)* S? |
| 8664 | */ |
| 8665 | SKIP_BLANKS; |
| 8666 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8667 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8668 | |
| 8669 | while ((RAW != '>') && |
| 8670 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 8671 | (IS_BYTE_CHAR(RAW))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8672 | const xmlChar *q = CUR_PTR; |
| 8673 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8674 | int len = -1, alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8675 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8676 | attname = xmlParseAttribute2(ctxt, prefix, localname, |
| 8677 | &aprefix, &attvalue, &len, &alloc); |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 8678 | if (ctxt->input->base != base) { |
| 8679 | if ((attvalue != NULL) && (alloc != 0)) |
| 8680 | xmlFree(attvalue); |
| 8681 | attvalue = NULL; |
| 8682 | goto base_changed; |
| 8683 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8684 | if ((attname != NULL) && (attvalue != NULL)) { |
| 8685 | if (len < 0) len = xmlStrlen(attvalue); |
| 8686 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8687 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 8688 | xmlURIPtr uri; |
| 8689 | |
| 8690 | if (*URL != 0) { |
| 8691 | uri = xmlParseURI((const char *) URL); |
| 8692 | if (uri == NULL) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8693 | xmlNsErr(ctxt, XML_WAR_NS_URI, |
| 8694 | "xmlns: '%s' is not a valid URI\n", |
| 8695 | URL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8696 | } else { |
Daniel Veillard | da3fee4 | 2008-09-01 13:08:57 +0000 | [diff] [blame^] | 8697 | if (uri->scheme == NULL) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8698 | xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 8699 | "xmlns: URI %s is not absolute\n", |
| 8700 | URL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8701 | } |
| 8702 | xmlFreeURI(uri); |
| 8703 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8704 | if (URL == ctxt->str_xml_ns) { |
| 8705 | if (attname != ctxt->str_xml) { |
| 8706 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 8707 | "xml namespace URI cannot be the default namespace\n", |
| 8708 | NULL, NULL, NULL); |
| 8709 | } |
| 8710 | goto skip_default_ns; |
| 8711 | } |
| 8712 | if ((len == 29) && |
| 8713 | (xmlStrEqual(URL, |
| 8714 | BAD_CAST "http://www.w3.org/2000/xmlns/"))) { |
| 8715 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 8716 | "reuse of the xmlns namespace name is forbidden\n", |
| 8717 | NULL, NULL, NULL); |
| 8718 | goto skip_default_ns; |
| 8719 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8720 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8721 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8722 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8723 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8724 | for (j = 1;j <= nbNs;j++) |
| 8725 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 8726 | break; |
| 8727 | if (j <= nbNs) |
| 8728 | xmlErrAttributeDup(ctxt, NULL, attname); |
| 8729 | else |
| 8730 | if (nsPush(ctxt, NULL, URL) > 0) nbNs++; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8731 | skip_default_ns: |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8732 | if (alloc != 0) xmlFree(attvalue); |
| 8733 | SKIP_BLANKS; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8734 | continue; |
| 8735 | } |
| 8736 | if (aprefix == ctxt->str_xmlns) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8737 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 8738 | xmlURIPtr uri; |
| 8739 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8740 | if (attname == ctxt->str_xml) { |
| 8741 | if (URL != ctxt->str_xml_ns) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8742 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 8743 | "xml namespace prefix mapped to wrong URI\n", |
| 8744 | NULL, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8745 | } |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8746 | /* |
| 8747 | * Do not keep a namespace definition node |
| 8748 | */ |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8749 | goto skip_ns; |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 8750 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8751 | if (URL == ctxt->str_xml_ns) { |
| 8752 | if (attname != ctxt->str_xml) { |
| 8753 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 8754 | "xml namespace URI mapped to wrong prefix\n", |
| 8755 | NULL, NULL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8756 | } |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8757 | goto skip_ns; |
| 8758 | } |
| 8759 | if (attname == ctxt->str_xmlns) { |
| 8760 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 8761 | "redefinition of the xmlns prefix is forbidden\n", |
| 8762 | NULL, NULL, NULL); |
| 8763 | goto skip_ns; |
| 8764 | } |
| 8765 | if ((len == 29) && |
| 8766 | (xmlStrEqual(URL, |
| 8767 | BAD_CAST "http://www.w3.org/2000/xmlns/"))) { |
| 8768 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 8769 | "reuse of the xmlns namespace name is forbidden\n", |
| 8770 | NULL, NULL, NULL); |
| 8771 | goto skip_ns; |
| 8772 | } |
| 8773 | if ((URL == NULL) || (URL[0] == 0)) { |
| 8774 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 8775 | "xmlns:%s: Empty XML namespace is not allowed\n", |
| 8776 | attname, NULL, NULL); |
| 8777 | goto skip_ns; |
| 8778 | } else { |
| 8779 | uri = xmlParseURI((const char *) URL); |
| 8780 | if (uri == NULL) { |
| 8781 | xmlNsErr(ctxt, XML_WAR_NS_URI, |
| 8782 | "xmlns:%s: '%s' is not a valid URI\n", |
| 8783 | attname, URL, NULL); |
| 8784 | } else { |
| 8785 | if ((ctxt->pedantic) && (uri->scheme == NULL)) { |
| 8786 | xmlNsWarn(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 8787 | "xmlns:%s: URI %s is not absolute\n", |
| 8788 | attname, URL, NULL); |
| 8789 | } |
| 8790 | xmlFreeURI(uri); |
| 8791 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8792 | } |
| 8793 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8794 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8795 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8796 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8797 | for (j = 1;j <= nbNs;j++) |
| 8798 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 8799 | break; |
| 8800 | if (j <= nbNs) |
| 8801 | xmlErrAttributeDup(ctxt, aprefix, attname); |
| 8802 | else |
| 8803 | if (nsPush(ctxt, attname, URL) > 0) nbNs++; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8804 | skip_ns: |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8805 | if (alloc != 0) xmlFree(attvalue); |
| 8806 | SKIP_BLANKS; |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 8807 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8808 | continue; |
| 8809 | } |
| 8810 | |
| 8811 | /* |
| 8812 | * Add the pair to atts |
| 8813 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8814 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 8815 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8816 | if (attvalue[len] == 0) |
| 8817 | xmlFree(attvalue); |
| 8818 | goto failed; |
| 8819 | } |
| 8820 | maxatts = ctxt->maxatts; |
| 8821 | atts = ctxt->atts; |
| 8822 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8823 | ctxt->attallocs[nratts++] = alloc; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8824 | atts[nbatts++] = attname; |
| 8825 | atts[nbatts++] = aprefix; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8826 | atts[nbatts++] = NULL; /* the URI will be fetched later */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8827 | atts[nbatts++] = attvalue; |
| 8828 | attvalue += len; |
| 8829 | atts[nbatts++] = attvalue; |
| 8830 | /* |
| 8831 | * tag if some deallocation is needed |
| 8832 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8833 | if (alloc != 0) attval = 1; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8834 | } else { |
| 8835 | if ((attvalue != NULL) && (attvalue[len] == 0)) |
| 8836 | xmlFree(attvalue); |
| 8837 | } |
| 8838 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 8839 | failed: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8840 | |
| 8841 | GROW |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8842 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8843 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 8844 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8845 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8846 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 8847 | "attributes construct error\n"); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 8848 | break; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8849 | } |
| 8850 | SKIP_BLANKS; |
| 8851 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 8852 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8853 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8854 | "xmlParseStartTag: problem parsing attributes\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8855 | break; |
| 8856 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8857 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8858 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8859 | } |
| 8860 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8861 | /* |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8862 | * The attributes defaulting |
| 8863 | */ |
| 8864 | if (ctxt->attsDefault != NULL) { |
| 8865 | xmlDefAttrsPtr defaults; |
| 8866 | |
| 8867 | defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix); |
| 8868 | if (defaults != NULL) { |
| 8869 | for (i = 0;i < defaults->nbAttrs;i++) { |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8870 | attname = defaults->values[5 * i]; |
| 8871 | aprefix = defaults->values[5 * i + 1]; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8872 | |
| 8873 | /* |
| 8874 | * special work for namespaces defaulted defs |
| 8875 | */ |
| 8876 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
| 8877 | /* |
| 8878 | * check that it's not a defined namespace |
| 8879 | */ |
| 8880 | for (j = 1;j <= nbNs;j++) |
| 8881 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 8882 | break; |
| 8883 | if (j <= nbNs) continue; |
| 8884 | |
| 8885 | nsname = xmlGetNamespace(ctxt, NULL); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8886 | if (nsname != defaults->values[5 * i + 2]) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8887 | if (nsPush(ctxt, NULL, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8888 | defaults->values[5 * i + 2]) > 0) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8889 | nbNs++; |
| 8890 | } |
| 8891 | } else if (aprefix == ctxt->str_xmlns) { |
| 8892 | /* |
| 8893 | * check that it's not a defined namespace |
| 8894 | */ |
| 8895 | for (j = 1;j <= nbNs;j++) |
| 8896 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 8897 | break; |
| 8898 | if (j <= nbNs) continue; |
| 8899 | |
| 8900 | nsname = xmlGetNamespace(ctxt, attname); |
| 8901 | if (nsname != defaults->values[2]) { |
| 8902 | if (nsPush(ctxt, attname, |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8903 | defaults->values[5 * i + 2]) > 0) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8904 | nbNs++; |
| 8905 | } |
| 8906 | } else { |
| 8907 | /* |
| 8908 | * check that it's not a defined attribute |
| 8909 | */ |
| 8910 | for (j = 0;j < nbatts;j+=5) { |
| 8911 | if ((attname == atts[j]) && (aprefix == atts[j+1])) |
| 8912 | break; |
| 8913 | } |
| 8914 | if (j < nbatts) continue; |
| 8915 | |
| 8916 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 8917 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 8918 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8919 | } |
| 8920 | maxatts = ctxt->maxatts; |
| 8921 | atts = ctxt->atts; |
| 8922 | } |
| 8923 | atts[nbatts++] = attname; |
| 8924 | atts[nbatts++] = aprefix; |
| 8925 | if (aprefix == NULL) |
| 8926 | atts[nbatts++] = NULL; |
| 8927 | else |
| 8928 | atts[nbatts++] = xmlGetNamespace(ctxt, aprefix); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 8929 | atts[nbatts++] = defaults->values[5 * i + 2]; |
| 8930 | atts[nbatts++] = defaults->values[5 * i + 3]; |
| 8931 | if ((ctxt->standalone == 1) && |
| 8932 | (defaults->values[5 * i + 4] != NULL)) { |
| 8933 | xmlValidityError(ctxt, XML_DTD_STANDALONE_DEFAULTED, |
| 8934 | "standalone: attribute %s on %s defaulted from external subset\n", |
| 8935 | attname, localname); |
| 8936 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8937 | nbdef++; |
| 8938 | } |
| 8939 | } |
| 8940 | } |
| 8941 | } |
| 8942 | |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 8943 | /* |
| 8944 | * The attributes checkings |
| 8945 | */ |
| 8946 | for (i = 0; i < nbatts;i += 5) { |
Kasimier T. Buchcik | 455472f | 2005-04-29 10:04:43 +0000 | [diff] [blame] | 8947 | /* |
| 8948 | * The default namespace does not apply to attribute names. |
| 8949 | */ |
| 8950 | if (atts[i + 1] != NULL) { |
| 8951 | nsname = xmlGetNamespace(ctxt, atts[i + 1]); |
| 8952 | if (nsname == NULL) { |
| 8953 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 8954 | "Namespace prefix %s for %s on %s is not defined\n", |
| 8955 | atts[i + 1], atts[i], localname); |
| 8956 | } |
| 8957 | atts[i + 2] = nsname; |
| 8958 | } else |
| 8959 | nsname = NULL; |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 8960 | /* |
| 8961 | * [ WFC: Unique Att Spec ] |
| 8962 | * No attribute name may appear more than once in the same |
| 8963 | * start-tag or empty-element tag. |
| 8964 | * As extended by the Namespace in XML REC. |
| 8965 | */ |
| 8966 | for (j = 0; j < i;j += 5) { |
| 8967 | if (atts[i] == atts[j]) { |
| 8968 | if (atts[i+1] == atts[j+1]) { |
| 8969 | xmlErrAttributeDup(ctxt, atts[i+1], atts[i]); |
| 8970 | break; |
| 8971 | } |
| 8972 | if ((nsname != NULL) && (atts[j + 2] == nsname)) { |
| 8973 | xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED, |
| 8974 | "Namespaced Attribute %s in '%s' redefined\n", |
| 8975 | atts[i], nsname, NULL); |
| 8976 | break; |
| 8977 | } |
| 8978 | } |
| 8979 | } |
| 8980 | } |
| 8981 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8982 | nsname = xmlGetNamespace(ctxt, prefix); |
| 8983 | if ((prefix != NULL) && (nsname == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8984 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 8985 | "Namespace prefix %s on %s is not defined\n", |
| 8986 | prefix, localname, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8987 | } |
| 8988 | *pref = prefix; |
| 8989 | *URI = nsname; |
| 8990 | |
| 8991 | /* |
| 8992 | * SAX: Start of Element ! |
| 8993 | */ |
| 8994 | if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) && |
| 8995 | (!ctxt->disableSAX)) { |
| 8996 | if (nbNs > 0) |
| 8997 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 8998 | nsname, nbNs, &ctxt->nsTab[ctxt->nsNr - 2 * nbNs], |
| 8999 | nbatts / 5, nbdef, atts); |
| 9000 | else |
| 9001 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 9002 | nsname, 0, NULL, nbatts / 5, nbdef, atts); |
| 9003 | } |
| 9004 | |
| 9005 | /* |
| 9006 | * Free up attribute allocated strings if needed |
| 9007 | */ |
| 9008 | if (attval != 0) { |
| 9009 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 9010 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 9011 | xmlFree((xmlChar *) atts[i]); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9012 | } |
| 9013 | |
| 9014 | return(localname); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9015 | |
| 9016 | base_changed: |
| 9017 | /* |
| 9018 | * the attribute strings are valid iif the base didn't changed |
| 9019 | */ |
| 9020 | if (attval != 0) { |
| 9021 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 9022 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 9023 | xmlFree((xmlChar *) atts[i]); |
| 9024 | } |
| 9025 | ctxt->input->cur = ctxt->input->base + cur; |
Daniel Veillard | dcec672 | 2006-10-15 20:32:53 +0000 | [diff] [blame] | 9026 | ctxt->input->line = oldline; |
| 9027 | ctxt->input->col = oldcol; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9028 | if (ctxt->wellFormed == 1) { |
| 9029 | goto reparse; |
| 9030 | } |
| 9031 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9032 | } |
| 9033 | |
| 9034 | /** |
| 9035 | * xmlParseEndTag2: |
| 9036 | * @ctxt: an XML parser context |
| 9037 | * @line: line of the start tag |
| 9038 | * @nsNr: number of namespaces on the start tag |
| 9039 | * |
| 9040 | * parse an end of tag |
| 9041 | * |
| 9042 | * [42] ETag ::= '</' Name S? '>' |
| 9043 | * |
| 9044 | * With namespace |
| 9045 | * |
| 9046 | * [NS 9] ETag ::= '</' QName S? '>' |
| 9047 | */ |
| 9048 | |
| 9049 | static void |
| 9050 | xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlChar *prefix, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9051 | const xmlChar *URI, int line, int nsNr, int tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9052 | const xmlChar *name; |
| 9053 | |
| 9054 | GROW; |
| 9055 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9056 | xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9057 | return; |
| 9058 | } |
| 9059 | SKIP(2); |
| 9060 | |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 9061 | if ((tlen > 0) && (xmlStrncmp(ctxt->input->cur, ctxt->name, tlen) == 0)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9062 | if (ctxt->input->cur[tlen] == '>') { |
| 9063 | ctxt->input->cur += tlen + 1; |
| 9064 | goto done; |
| 9065 | } |
| 9066 | ctxt->input->cur += tlen; |
| 9067 | name = (xmlChar*)1; |
| 9068 | } else { |
| 9069 | if (prefix == NULL) |
| 9070 | name = xmlParseNameAndCompare(ctxt, ctxt->name); |
| 9071 | else |
| 9072 | name = xmlParseQNameAndCompare(ctxt, ctxt->name, prefix); |
| 9073 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9074 | |
| 9075 | /* |
| 9076 | * We should definitely be at the ending "S? '>'" part |
| 9077 | */ |
| 9078 | GROW; |
| 9079 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 9080 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9081 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9082 | } else |
| 9083 | NEXT1; |
| 9084 | |
| 9085 | /* |
| 9086 | * [ WFC: Element Type Match ] |
| 9087 | * The Name in an element's end-tag must match the element type in the |
| 9088 | * start-tag. |
| 9089 | * |
| 9090 | */ |
| 9091 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9092 | if (name == NULL) name = BAD_CAST "unparseable"; |
| 9093 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9094 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9095 | ctxt->name, line, name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9096 | } |
| 9097 | |
| 9098 | /* |
| 9099 | * SAX: End of Tag |
| 9100 | */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9101 | done: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9102 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 9103 | (!ctxt->disableSAX)) |
| 9104 | ctxt->sax->endElementNs(ctxt->userData, ctxt->name, prefix, URI); |
| 9105 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9106 | spacePop(ctxt); |
| 9107 | if (nsNr != 0) |
| 9108 | nsPop(ctxt, nsNr); |
| 9109 | return; |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 9110 | } |
| 9111 | |
| 9112 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9113 | * xmlParseCDSect: |
| 9114 | * @ctxt: an XML parser context |
| 9115 | * |
| 9116 | * Parse escaped pure raw content. |
| 9117 | * |
| 9118 | * [18] CDSect ::= CDStart CData CDEnd |
| 9119 | * |
| 9120 | * [19] CDStart ::= '<![CDATA[' |
| 9121 | * |
| 9122 | * [20] Data ::= (Char* - (Char* ']]>' Char*)) |
| 9123 | * |
| 9124 | * [21] CDEnd ::= ']]>' |
| 9125 | */ |
| 9126 | void |
| 9127 | xmlParseCDSect(xmlParserCtxtPtr ctxt) { |
| 9128 | xmlChar *buf = NULL; |
| 9129 | int len = 0; |
| 9130 | int size = XML_PARSER_BUFFER_SIZE; |
| 9131 | int r, rl; |
| 9132 | int s, sl; |
| 9133 | int cur, l; |
| 9134 | int count = 0; |
| 9135 | |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 9136 | /* Check 2.6.0 was NXT(0) not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9137 | if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9138 | SKIP(9); |
| 9139 | } else |
| 9140 | return; |
| 9141 | |
| 9142 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
| 9143 | r = CUR_CHAR(rl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9144 | if (!IS_CHAR(r)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9145 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9146 | ctxt->instate = XML_PARSER_CONTENT; |
| 9147 | return; |
| 9148 | } |
| 9149 | NEXTL(rl); |
| 9150 | s = CUR_CHAR(sl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9151 | if (!IS_CHAR(s)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9152 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9153 | ctxt->instate = XML_PARSER_CONTENT; |
| 9154 | return; |
| 9155 | } |
| 9156 | NEXTL(sl); |
| 9157 | cur = CUR_CHAR(l); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 9158 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9159 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9160 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9161 | return; |
| 9162 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 9163 | while (IS_CHAR(cur) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9164 | ((r != ']') || (s != ']') || (cur != '>'))) { |
| 9165 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9166 | xmlChar *tmp; |
| 9167 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9168 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9169 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 9170 | if (tmp == NULL) { |
| 9171 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9172 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9173 | return; |
| 9174 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9175 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9176 | } |
| 9177 | COPY_BUF(rl,buf,len,r); |
| 9178 | r = s; |
| 9179 | rl = sl; |
| 9180 | s = cur; |
| 9181 | sl = l; |
| 9182 | count++; |
| 9183 | if (count > 50) { |
| 9184 | GROW; |
| 9185 | count = 0; |
| 9186 | } |
| 9187 | NEXTL(l); |
| 9188 | cur = CUR_CHAR(l); |
| 9189 | } |
| 9190 | buf[len] = 0; |
| 9191 | ctxt->instate = XML_PARSER_CONTENT; |
| 9192 | if (cur != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 9193 | xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9194 | "CData section not finished\n%.50s\n", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9195 | xmlFree(buf); |
| 9196 | return; |
| 9197 | } |
| 9198 | NEXTL(l); |
| 9199 | |
| 9200 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9201 | * OK the buffer is to be consumed as cdata. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9202 | */ |
| 9203 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 9204 | if (ctxt->sax->cdataBlock != NULL) |
| 9205 | ctxt->sax->cdataBlock(ctxt->userData, buf, len); |
Daniel Veillard | 7583a59 | 2001-07-08 13:15:55 +0000 | [diff] [blame] | 9206 | else if (ctxt->sax->characters != NULL) |
| 9207 | ctxt->sax->characters(ctxt->userData, buf, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9208 | } |
| 9209 | xmlFree(buf); |
| 9210 | } |
| 9211 | |
| 9212 | /** |
| 9213 | * xmlParseContent: |
| 9214 | * @ctxt: an XML parser context |
| 9215 | * |
| 9216 | * Parse a content: |
| 9217 | * |
| 9218 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 9219 | */ |
| 9220 | |
| 9221 | void |
| 9222 | xmlParseContent(xmlParserCtxtPtr ctxt) { |
| 9223 | GROW; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9224 | while ((RAW != 0) && |
Daniel Veillard | 4a9fe38 | 2006-09-19 12:44:35 +0000 | [diff] [blame] | 9225 | ((RAW != '<') || (NXT(1) != '/')) && |
| 9226 | (ctxt->instate != XML_PARSER_EOF)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9227 | const xmlChar *test = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 9228 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9229 | const xmlChar *cur = ctxt->input->cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9230 | |
| 9231 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9232 | * First case : a Processing Instruction. |
| 9233 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9234 | if ((*cur == '<') && (cur[1] == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9235 | xmlParsePI(ctxt); |
| 9236 | } |
| 9237 | |
| 9238 | /* |
| 9239 | * Second case : a CDSection |
| 9240 | */ |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 9241 | /* 2.6.0 test was *cur not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9242 | else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9243 | xmlParseCDSect(ctxt); |
| 9244 | } |
| 9245 | |
| 9246 | /* |
| 9247 | * Third case : a comment |
| 9248 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9249 | else if ((*cur == '<') && (NXT(1) == '!') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9250 | (NXT(2) == '-') && (NXT(3) == '-')) { |
| 9251 | xmlParseComment(ctxt); |
| 9252 | ctxt->instate = XML_PARSER_CONTENT; |
| 9253 | } |
| 9254 | |
| 9255 | /* |
| 9256 | * Fourth case : a sub-element. |
| 9257 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9258 | else if (*cur == '<') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9259 | xmlParseElement(ctxt); |
| 9260 | } |
| 9261 | |
| 9262 | /* |
| 9263 | * Fifth case : a reference. If if has not been resolved, |
| 9264 | * parsing returns it's Name, create the node |
| 9265 | */ |
| 9266 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9267 | else if (*cur == '&') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9268 | xmlParseReference(ctxt); |
| 9269 | } |
| 9270 | |
| 9271 | /* |
| 9272 | * Last case, text. Note that References are handled directly. |
| 9273 | */ |
| 9274 | else { |
| 9275 | xmlParseCharData(ctxt, 0); |
| 9276 | } |
| 9277 | |
| 9278 | GROW; |
| 9279 | /* |
| 9280 | * Pop-up of finished entities. |
| 9281 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 9282 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9283 | xmlPopInput(ctxt); |
| 9284 | SHRINK; |
| 9285 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 9286 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9287 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 9288 | "detected an error in element content\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9289 | ctxt->instate = XML_PARSER_EOF; |
| 9290 | break; |
| 9291 | } |
| 9292 | } |
| 9293 | } |
| 9294 | |
| 9295 | /** |
| 9296 | * xmlParseElement: |
| 9297 | * @ctxt: an XML parser context |
| 9298 | * |
| 9299 | * parse an XML element, this is highly recursive |
| 9300 | * |
| 9301 | * [39] element ::= EmptyElemTag | STag content ETag |
| 9302 | * |
| 9303 | * [ WFC: Element Type Match ] |
| 9304 | * The Name in an element's end-tag must match the element type in the |
| 9305 | * start-tag. |
| 9306 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9307 | */ |
| 9308 | |
| 9309 | void |
| 9310 | xmlParseElement(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 9311 | const xmlChar *name; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9312 | const xmlChar *prefix; |
| 9313 | const xmlChar *URI; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9314 | xmlParserNodeInfo node_info; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9315 | int line, tlen; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9316 | xmlNodePtr ret; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9317 | int nsNr = ctxt->nsNr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9318 | |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 9319 | if (((unsigned int) ctxt->nameNr > xmlParserMaxDepth) && |
| 9320 | ((ctxt->options & XML_PARSE_HUGE) == 0)) { |
| 9321 | xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR, |
| 9322 | "Excessive depth in document: %d use XML_PARSE_HUGE option\n", |
| 9323 | xmlParserMaxDepth); |
Daniel Veillard | 4a9fe38 | 2006-09-19 12:44:35 +0000 | [diff] [blame] | 9324 | ctxt->instate = XML_PARSER_EOF; |
| 9325 | return; |
| 9326 | } |
| 9327 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9328 | /* Capture start position */ |
| 9329 | if (ctxt->record_info) { |
| 9330 | node_info.begin_pos = ctxt->input->consumed + |
| 9331 | (CUR_PTR - ctxt->input->base); |
| 9332 | node_info.begin_line = ctxt->input->line; |
| 9333 | } |
| 9334 | |
| 9335 | if (ctxt->spaceNr == 0) |
| 9336 | spacePush(ctxt, -1); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 9337 | else if (*ctxt->space == -2) |
| 9338 | spacePush(ctxt, -1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9339 | else |
| 9340 | spacePush(ctxt, *ctxt->space); |
| 9341 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 9342 | line = ctxt->input->line; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9343 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9344 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9345 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9346 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9347 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9348 | else |
| 9349 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9350 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9351 | if (name == NULL) { |
| 9352 | spacePop(ctxt); |
| 9353 | return; |
| 9354 | } |
| 9355 | namePush(ctxt, name); |
| 9356 | ret = ctxt->node; |
| 9357 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 9358 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9359 | /* |
| 9360 | * [ VC: Root Element Type ] |
| 9361 | * The Name in the document type declaration must match the element |
| 9362 | * type of the root element. |
| 9363 | */ |
| 9364 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 9365 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 9366 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 9367 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9368 | |
| 9369 | /* |
| 9370 | * Check for an Empty Element. |
| 9371 | */ |
| 9372 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 9373 | SKIP(2); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9374 | if (ctxt->sax2) { |
| 9375 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 9376 | (!ctxt->disableSAX)) |
| 9377 | ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9378 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9379 | } else { |
| 9380 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 9381 | (!ctxt->disableSAX)) |
| 9382 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9383 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9384 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9385 | namePop(ctxt); |
| 9386 | spacePop(ctxt); |
| 9387 | if (nsNr != ctxt->nsNr) |
| 9388 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9389 | if ( ret != NULL && ctxt->record_info ) { |
| 9390 | node_info.end_pos = ctxt->input->consumed + |
| 9391 | (CUR_PTR - ctxt->input->base); |
| 9392 | node_info.end_line = ctxt->input->line; |
| 9393 | node_info.node = ret; |
| 9394 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 9395 | } |
| 9396 | return; |
| 9397 | } |
| 9398 | if (RAW == '>') { |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 9399 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9400 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9401 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED, |
| 9402 | "Couldn't find end of Start Tag %s line %d\n", |
| 9403 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9404 | |
| 9405 | /* |
| 9406 | * end of parsing of this node. |
| 9407 | */ |
| 9408 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9409 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9410 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9411 | if (nsNr != ctxt->nsNr) |
| 9412 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9413 | |
| 9414 | /* |
| 9415 | * Capture end position and add node |
| 9416 | */ |
| 9417 | if ( ret != NULL && ctxt->record_info ) { |
| 9418 | node_info.end_pos = ctxt->input->consumed + |
| 9419 | (CUR_PTR - ctxt->input->base); |
| 9420 | node_info.end_line = ctxt->input->line; |
| 9421 | node_info.node = ret; |
| 9422 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 9423 | } |
| 9424 | return; |
| 9425 | } |
| 9426 | |
| 9427 | /* |
| 9428 | * Parse the content of the element: |
| 9429 | */ |
| 9430 | xmlParseContent(ctxt); |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 9431 | if (!IS_BYTE_CHAR(RAW)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9432 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED, |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 9433 | "Premature end of data in tag %s line %d\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9434 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9435 | |
| 9436 | /* |
| 9437 | * end of parsing of this node. |
| 9438 | */ |
| 9439 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9440 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9441 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9442 | if (nsNr != ctxt->nsNr) |
| 9443 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9444 | return; |
| 9445 | } |
| 9446 | |
| 9447 | /* |
| 9448 | * parse the end of tag: '</' should be here. |
| 9449 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9450 | if (ctxt->sax2) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9451 | xmlParseEndTag2(ctxt, prefix, URI, line, ctxt->nsNr - nsNr, tlen); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9452 | namePop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9453 | } |
| 9454 | #ifdef LIBXML_SAX1_ENABLED |
| 9455 | else |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9456 | xmlParseEndTag1(ctxt, line); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9457 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9458 | |
| 9459 | /* |
| 9460 | * Capture end position and add node |
| 9461 | */ |
| 9462 | if ( ret != NULL && ctxt->record_info ) { |
| 9463 | node_info.end_pos = ctxt->input->consumed + |
| 9464 | (CUR_PTR - ctxt->input->base); |
| 9465 | node_info.end_line = ctxt->input->line; |
| 9466 | node_info.node = ret; |
| 9467 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 9468 | } |
| 9469 | } |
| 9470 | |
| 9471 | /** |
| 9472 | * xmlParseVersionNum: |
| 9473 | * @ctxt: an XML parser context |
| 9474 | * |
| 9475 | * parse the XML version value. |
| 9476 | * |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9477 | * [26] VersionNum ::= '1.' [0-9]+ |
| 9478 | * |
| 9479 | * In practice allow [0-9].[0-9]+ at that level |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9480 | * |
| 9481 | * Returns the string giving the XML version number, or NULL |
| 9482 | */ |
| 9483 | xmlChar * |
| 9484 | xmlParseVersionNum(xmlParserCtxtPtr ctxt) { |
| 9485 | xmlChar *buf = NULL; |
| 9486 | int len = 0; |
| 9487 | int size = 10; |
| 9488 | xmlChar cur; |
| 9489 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 9490 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9491 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9492 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9493 | return(NULL); |
| 9494 | } |
| 9495 | cur = CUR; |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9496 | if (!((cur >= '0') && (cur <= '9'))) { |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 9497 | xmlFree(buf); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9498 | return(NULL); |
| 9499 | } |
| 9500 | buf[len++] = cur; |
| 9501 | NEXT; |
| 9502 | cur=CUR; |
| 9503 | if (cur != '.') { |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 9504 | xmlFree(buf); |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9505 | return(NULL); |
| 9506 | } |
| 9507 | buf[len++] = cur; |
| 9508 | NEXT; |
| 9509 | cur=CUR; |
| 9510 | while ((cur >= '0') && (cur <= '9')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9511 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9512 | xmlChar *tmp; |
| 9513 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9514 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9515 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 9516 | if (tmp == NULL) { |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 9517 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9518 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9519 | return(NULL); |
| 9520 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9521 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9522 | } |
| 9523 | buf[len++] = cur; |
| 9524 | NEXT; |
| 9525 | cur=CUR; |
| 9526 | } |
| 9527 | buf[len] = 0; |
| 9528 | return(buf); |
| 9529 | } |
| 9530 | |
| 9531 | /** |
| 9532 | * xmlParseVersionInfo: |
| 9533 | * @ctxt: an XML parser context |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 9534 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9535 | * parse the XML version. |
| 9536 | * |
| 9537 | * [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ") |
Daniel Veillard | 68b6e02 | 2008-03-31 09:26:00 +0000 | [diff] [blame] | 9538 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9539 | * [25] Eq ::= S? '=' S? |
| 9540 | * |
| 9541 | * Returns the version string, e.g. "1.0" |
| 9542 | */ |
| 9543 | |
| 9544 | xmlChar * |
| 9545 | xmlParseVersionInfo(xmlParserCtxtPtr ctxt) { |
| 9546 | xmlChar *version = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9547 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9548 | if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9549 | SKIP(7); |
| 9550 | SKIP_BLANKS; |
| 9551 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9552 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9553 | return(NULL); |
| 9554 | } |
| 9555 | NEXT; |
| 9556 | SKIP_BLANKS; |
| 9557 | if (RAW == '"') { |
| 9558 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9559 | version = xmlParseVersionNum(ctxt); |
| 9560 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9561 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9562 | } else |
| 9563 | NEXT; |
| 9564 | } else if (RAW == '\''){ |
| 9565 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9566 | version = xmlParseVersionNum(ctxt); |
| 9567 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9568 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9569 | } else |
| 9570 | NEXT; |
| 9571 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9572 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9573 | } |
| 9574 | } |
| 9575 | return(version); |
| 9576 | } |
| 9577 | |
| 9578 | /** |
| 9579 | * xmlParseEncName: |
| 9580 | * @ctxt: an XML parser context |
| 9581 | * |
| 9582 | * parse the XML encoding name |
| 9583 | * |
| 9584 | * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')* |
| 9585 | * |
| 9586 | * Returns the encoding name value or NULL |
| 9587 | */ |
| 9588 | xmlChar * |
| 9589 | xmlParseEncName(xmlParserCtxtPtr ctxt) { |
| 9590 | xmlChar *buf = NULL; |
| 9591 | int len = 0; |
| 9592 | int size = 10; |
| 9593 | xmlChar cur; |
| 9594 | |
| 9595 | cur = CUR; |
| 9596 | if (((cur >= 'a') && (cur <= 'z')) || |
| 9597 | ((cur >= 'A') && (cur <= 'Z'))) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 9598 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9599 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9600 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9601 | return(NULL); |
| 9602 | } |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9603 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9604 | buf[len++] = cur; |
| 9605 | NEXT; |
| 9606 | cur = CUR; |
| 9607 | while (((cur >= 'a') && (cur <= 'z')) || |
| 9608 | ((cur >= 'A') && (cur <= 'Z')) || |
| 9609 | ((cur >= '0') && (cur <= '9')) || |
| 9610 | (cur == '.') || (cur == '_') || |
| 9611 | (cur == '-')) { |
| 9612 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9613 | xmlChar *tmp; |
| 9614 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9615 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9616 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 9617 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9618 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9619 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9620 | return(NULL); |
| 9621 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 9622 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9623 | } |
| 9624 | buf[len++] = cur; |
| 9625 | NEXT; |
| 9626 | cur = CUR; |
| 9627 | if (cur == 0) { |
| 9628 | SHRINK; |
| 9629 | GROW; |
| 9630 | cur = CUR; |
| 9631 | } |
| 9632 | } |
| 9633 | buf[len] = 0; |
| 9634 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9635 | xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9636 | } |
| 9637 | return(buf); |
| 9638 | } |
| 9639 | |
| 9640 | /** |
| 9641 | * xmlParseEncodingDecl: |
| 9642 | * @ctxt: an XML parser context |
| 9643 | * |
| 9644 | * parse the XML encoding declaration |
| 9645 | * |
| 9646 | * [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'") |
| 9647 | * |
| 9648 | * this setups the conversion filters. |
| 9649 | * |
| 9650 | * Returns the encoding value or NULL |
| 9651 | */ |
| 9652 | |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 9653 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9654 | xmlParseEncodingDecl(xmlParserCtxtPtr ctxt) { |
| 9655 | xmlChar *encoding = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9656 | |
| 9657 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9658 | if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9659 | SKIP(8); |
| 9660 | SKIP_BLANKS; |
| 9661 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9662 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9663 | return(NULL); |
| 9664 | } |
| 9665 | NEXT; |
| 9666 | SKIP_BLANKS; |
| 9667 | if (RAW == '"') { |
| 9668 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9669 | encoding = xmlParseEncName(ctxt); |
| 9670 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9671 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9672 | } else |
| 9673 | NEXT; |
| 9674 | } else if (RAW == '\''){ |
| 9675 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9676 | encoding = xmlParseEncName(ctxt); |
| 9677 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9678 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9679 | } else |
| 9680 | NEXT; |
Daniel Veillard | 82ac6b0 | 2002-02-17 23:18:55 +0000 | [diff] [blame] | 9681 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9682 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9683 | } |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 9684 | /* |
| 9685 | * UTF-16 encoding stwich has already taken place at this stage, |
| 9686 | * more over the little-endian/big-endian selection is already done |
| 9687 | */ |
| 9688 | if ((encoding != NULL) && |
| 9689 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-16")) || |
| 9690 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF16")))) { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 9691 | /* |
| 9692 | * If no encoding was passed to the parser, that we are |
| 9693 | * using UTF-16 and no decoder is present i.e. the |
| 9694 | * document is apparently UTF-8 compatible, then raise an |
| 9695 | * encoding mismatch fatal error |
| 9696 | */ |
| 9697 | if ((ctxt->encoding == NULL) && |
| 9698 | (ctxt->input->buf != NULL) && |
| 9699 | (ctxt->input->buf->encoder == NULL)) { |
| 9700 | xmlFatalErrMsg(ctxt, XML_ERR_INVALID_ENCODING, |
| 9701 | "Document labelled UTF-16 but has UTF-8 content\n"); |
| 9702 | } |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 9703 | if (ctxt->encoding != NULL) |
| 9704 | xmlFree((xmlChar *) ctxt->encoding); |
| 9705 | ctxt->encoding = encoding; |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 9706 | } |
| 9707 | /* |
| 9708 | * UTF-8 encoding is handled natively |
| 9709 | */ |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 9710 | else if ((encoding != NULL) && |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 9711 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-8")) || |
| 9712 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF8")))) { |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 9713 | if (ctxt->encoding != NULL) |
| 9714 | xmlFree((xmlChar *) ctxt->encoding); |
| 9715 | ctxt->encoding = encoding; |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 9716 | } |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 9717 | else if (encoding != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9718 | xmlCharEncodingHandlerPtr handler; |
| 9719 | |
| 9720 | if (ctxt->input->encoding != NULL) |
| 9721 | xmlFree((xmlChar *) ctxt->input->encoding); |
| 9722 | ctxt->input->encoding = encoding; |
| 9723 | |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 9724 | handler = xmlFindCharEncodingHandler((const char *) encoding); |
| 9725 | if (handler != NULL) { |
| 9726 | xmlSwitchToEncoding(ctxt, handler); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9727 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 9728 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 9729 | "Unsupported encoding %s\n", encoding); |
| 9730 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9731 | } |
| 9732 | } |
| 9733 | } |
| 9734 | return(encoding); |
| 9735 | } |
| 9736 | |
| 9737 | /** |
| 9738 | * xmlParseSDDecl: |
| 9739 | * @ctxt: an XML parser context |
| 9740 | * |
| 9741 | * parse the XML standalone declaration |
| 9742 | * |
| 9743 | * [32] SDDecl ::= S 'standalone' Eq |
| 9744 | * (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"')) |
| 9745 | * |
| 9746 | * [ VC: Standalone Document Declaration ] |
| 9747 | * TODO The standalone document declaration must have the value "no" |
| 9748 | * if any external markup declarations contain declarations of: |
| 9749 | * - attributes with default values, if elements to which these |
| 9750 | * attributes apply appear in the document without specifications |
| 9751 | * of values for these attributes, or |
| 9752 | * - entities (other than amp, lt, gt, apos, quot), if references |
| 9753 | * to those entities appear in the document, or |
| 9754 | * - attributes with values subject to normalization, where the |
| 9755 | * attribute appears in the document with a value which will change |
| 9756 | * as a result of normalization, or |
| 9757 | * - element types with element content, if white space occurs directly |
| 9758 | * within any instance of those types. |
| 9759 | * |
Daniel Veillard | 602f2bd | 2006-12-04 09:26:04 +0000 | [diff] [blame] | 9760 | * Returns: |
| 9761 | * 1 if standalone="yes" |
| 9762 | * 0 if standalone="no" |
| 9763 | * -2 if standalone attribute is missing or invalid |
| 9764 | * (A standalone value of -2 means that the XML declaration was found, |
| 9765 | * but no value was specified for the standalone attribute). |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9766 | */ |
| 9767 | |
| 9768 | int |
| 9769 | xmlParseSDDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 602f2bd | 2006-12-04 09:26:04 +0000 | [diff] [blame] | 9770 | int standalone = -2; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9771 | |
| 9772 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9773 | 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] | 9774 | SKIP(10); |
| 9775 | SKIP_BLANKS; |
| 9776 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9777 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9778 | return(standalone); |
| 9779 | } |
| 9780 | NEXT; |
| 9781 | SKIP_BLANKS; |
| 9782 | if (RAW == '\''){ |
| 9783 | NEXT; |
| 9784 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 9785 | standalone = 0; |
| 9786 | SKIP(2); |
| 9787 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 9788 | (NXT(2) == 's')) { |
| 9789 | standalone = 1; |
| 9790 | SKIP(3); |
| 9791 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9792 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9793 | } |
| 9794 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9795 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9796 | } else |
| 9797 | NEXT; |
| 9798 | } else if (RAW == '"'){ |
| 9799 | NEXT; |
| 9800 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 9801 | standalone = 0; |
| 9802 | SKIP(2); |
| 9803 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 9804 | (NXT(2) == 's')) { |
| 9805 | standalone = 1; |
| 9806 | SKIP(3); |
| 9807 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9808 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9809 | } |
| 9810 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9811 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9812 | } else |
| 9813 | NEXT; |
| 9814 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9815 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9816 | } |
| 9817 | } |
| 9818 | return(standalone); |
| 9819 | } |
| 9820 | |
| 9821 | /** |
| 9822 | * xmlParseXMLDecl: |
| 9823 | * @ctxt: an XML parser context |
| 9824 | * |
| 9825 | * parse an XML declaration header |
| 9826 | * |
| 9827 | * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' |
| 9828 | */ |
| 9829 | |
| 9830 | void |
| 9831 | xmlParseXMLDecl(xmlParserCtxtPtr ctxt) { |
| 9832 | xmlChar *version; |
| 9833 | |
| 9834 | /* |
Daniel Veillard | ae487ba | 2005-11-17 07:25:52 +0000 | [diff] [blame] | 9835 | * This value for standalone indicates that the document has an |
| 9836 | * XML declaration but it does not have a standalone attribute. |
| 9837 | * It will be overwritten later if a standalone attribute is found. |
| 9838 | */ |
| 9839 | ctxt->input->standalone = -2; |
| 9840 | |
| 9841 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9842 | * We know that '<?xml' is here. |
| 9843 | */ |
| 9844 | SKIP(5); |
| 9845 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9846 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9847 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 9848 | "Blank needed after '<?xml'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9849 | } |
| 9850 | SKIP_BLANKS; |
| 9851 | |
| 9852 | /* |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 9853 | * We must have the VersionInfo here. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9854 | */ |
| 9855 | version = xmlParseVersionInfo(ctxt); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 9856 | if (version == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9857 | xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 9858 | } else { |
| 9859 | if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) { |
| 9860 | /* |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9861 | * Changed here for XML-1.0 5th edition |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 9862 | */ |
Daniel Veillard | 34e3f64 | 2008-07-29 09:02:27 +0000 | [diff] [blame] | 9863 | if (ctxt->options & XML_PARSE_OLD10) { |
| 9864 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION, |
| 9865 | "Unsupported version '%s'\n", |
| 9866 | version); |
| 9867 | } else { |
| 9868 | if ((version[0] == '1') && ((version[1] == '.'))) { |
| 9869 | xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION, |
| 9870 | "Unsupported version '%s'\n", |
| 9871 | version, NULL); |
| 9872 | } else { |
| 9873 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNKNOWN_VERSION, |
| 9874 | "Unsupported version '%s'\n", |
| 9875 | version); |
| 9876 | } |
| 9877 | } |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 9878 | } |
| 9879 | if (ctxt->version != NULL) |
Daniel Veillard | d3b0882 | 2001-12-05 12:03:33 +0000 | [diff] [blame] | 9880 | xmlFree((void *) ctxt->version); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 9881 | ctxt->version = version; |
Daniel Veillard | a050d23 | 2001-09-05 15:51:05 +0000 | [diff] [blame] | 9882 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9883 | |
| 9884 | /* |
| 9885 | * We may have the encoding declaration |
| 9886 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9887 | if (!IS_BLANK_CH(RAW)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9888 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 9889 | SKIP(2); |
| 9890 | return; |
| 9891 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9892 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9893 | } |
| 9894 | xmlParseEncodingDecl(ctxt); |
| 9895 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 9896 | /* |
| 9897 | * The XML REC instructs us to stop parsing right here |
| 9898 | */ |
| 9899 | return; |
| 9900 | } |
| 9901 | |
| 9902 | /* |
| 9903 | * We may have the standalone status. |
| 9904 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9905 | if ((ctxt->input->encoding != NULL) && (!IS_BLANK_CH(RAW))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9906 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 9907 | SKIP(2); |
| 9908 | return; |
| 9909 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 9910 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9911 | } |
| 9912 | SKIP_BLANKS; |
| 9913 | ctxt->input->standalone = xmlParseSDDecl(ctxt); |
| 9914 | |
| 9915 | SKIP_BLANKS; |
| 9916 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 9917 | SKIP(2); |
| 9918 | } else if (RAW == '>') { |
| 9919 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9920 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9921 | NEXT; |
| 9922 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9923 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9924 | MOVETO_ENDTAG(CUR_PTR); |
| 9925 | NEXT; |
| 9926 | } |
| 9927 | } |
| 9928 | |
| 9929 | /** |
| 9930 | * xmlParseMisc: |
| 9931 | * @ctxt: an XML parser context |
| 9932 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 9933 | * parse an XML Misc* optional field. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9934 | * |
| 9935 | * [27] Misc ::= Comment | PI | S |
| 9936 | */ |
| 9937 | |
| 9938 | void |
| 9939 | xmlParseMisc(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 9940 | while (((RAW == '<') && (NXT(1) == '?')) || |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 9941 | (CMP4(CUR_PTR, '<', '!', '-', '-')) || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9942 | IS_BLANK_CH(CUR)) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 9943 | if ((RAW == '<') && (NXT(1) == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9944 | xmlParsePI(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9945 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9946 | NEXT; |
| 9947 | } else |
| 9948 | xmlParseComment(ctxt); |
| 9949 | } |
| 9950 | } |
| 9951 | |
| 9952 | /** |
| 9953 | * xmlParseDocument: |
| 9954 | * @ctxt: an XML parser context |
| 9955 | * |
| 9956 | * parse an XML document (and build a tree if using the standard SAX |
| 9957 | * interface). |
| 9958 | * |
| 9959 | * [1] document ::= prolog element Misc* |
| 9960 | * |
| 9961 | * [22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)? |
| 9962 | * |
| 9963 | * Returns 0, -1 in case of error. the parser context is augmented |
| 9964 | * as a result of the parsing. |
| 9965 | */ |
| 9966 | |
| 9967 | int |
| 9968 | xmlParseDocument(xmlParserCtxtPtr ctxt) { |
| 9969 | xmlChar start[4]; |
| 9970 | xmlCharEncoding enc; |
| 9971 | |
| 9972 | xmlInitParser(); |
| 9973 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 9974 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
| 9975 | return(-1); |
| 9976 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9977 | GROW; |
| 9978 | |
| 9979 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9980 | * SAX: detecting the level. |
| 9981 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9982 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 9983 | |
| 9984 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9985 | * SAX: beginning of the document processing. |
| 9986 | */ |
| 9987 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 9988 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 9989 | |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 9990 | if ((ctxt->encoding == (const xmlChar *)XML_CHAR_ENCODING_NONE) && |
| 9991 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 9992 | /* |
| 9993 | * Get the 4 first bytes and decode the charset |
| 9994 | * if enc != XML_CHAR_ENCODING_NONE |
| 9995 | * plug some encoding conversion routines. |
| 9996 | */ |
| 9997 | start[0] = RAW; |
| 9998 | start[1] = NXT(1); |
| 9999 | start[2] = NXT(2); |
| 10000 | start[3] = NXT(3); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10001 | enc = xmlDetectCharEncoding(&start[0], 4); |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 10002 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10003 | xmlSwitchEncoding(ctxt, enc); |
| 10004 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10005 | } |
| 10006 | |
| 10007 | |
| 10008 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10009 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10010 | } |
| 10011 | |
| 10012 | /* |
| 10013 | * Check for the XMLDecl in the Prolog. |
| 10014 | */ |
| 10015 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10016 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10017 | |
| 10018 | /* |
| 10019 | * Note that we will switch encoding on the fly. |
| 10020 | */ |
| 10021 | xmlParseXMLDecl(ctxt); |
| 10022 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10023 | /* |
| 10024 | * The XML REC instructs us to stop parsing right here |
| 10025 | */ |
| 10026 | return(-1); |
| 10027 | } |
| 10028 | ctxt->standalone = ctxt->input->standalone; |
| 10029 | SKIP_BLANKS; |
| 10030 | } else { |
| 10031 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 10032 | } |
| 10033 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 10034 | ctxt->sax->startDocument(ctxt->userData); |
| 10035 | |
| 10036 | /* |
| 10037 | * The Misc part of the Prolog |
| 10038 | */ |
| 10039 | GROW; |
| 10040 | xmlParseMisc(ctxt); |
| 10041 | |
| 10042 | /* |
| 10043 | * Then possibly doc type declaration(s) and more Misc |
| 10044 | * (doctypedecl Misc*)? |
| 10045 | */ |
| 10046 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10047 | if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10048 | |
| 10049 | ctxt->inSubset = 1; |
| 10050 | xmlParseDocTypeDecl(ctxt); |
| 10051 | if (RAW == '[') { |
| 10052 | ctxt->instate = XML_PARSER_DTD; |
| 10053 | xmlParseInternalSubset(ctxt); |
| 10054 | } |
| 10055 | |
| 10056 | /* |
| 10057 | * Create and update the external subset. |
| 10058 | */ |
| 10059 | ctxt->inSubset = 2; |
| 10060 | if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) && |
| 10061 | (!ctxt->disableSAX)) |
| 10062 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 10063 | ctxt->extSubSystem, ctxt->extSubURI); |
| 10064 | ctxt->inSubset = 0; |
| 10065 | |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 10066 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10067 | |
| 10068 | ctxt->instate = XML_PARSER_PROLOG; |
| 10069 | xmlParseMisc(ctxt); |
| 10070 | } |
| 10071 | |
| 10072 | /* |
| 10073 | * Time to start parsing the tree itself |
| 10074 | */ |
| 10075 | GROW; |
| 10076 | if (RAW != '<') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 10077 | xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY, |
| 10078 | "Start tag expected, '<' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10079 | } else { |
| 10080 | ctxt->instate = XML_PARSER_CONTENT; |
| 10081 | xmlParseElement(ctxt); |
| 10082 | ctxt->instate = XML_PARSER_EPILOG; |
| 10083 | |
| 10084 | |
| 10085 | /* |
| 10086 | * The Misc part at the end |
| 10087 | */ |
| 10088 | xmlParseMisc(ctxt); |
| 10089 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10090 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10091 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10092 | } |
| 10093 | ctxt->instate = XML_PARSER_EOF; |
| 10094 | } |
| 10095 | |
| 10096 | /* |
| 10097 | * SAX: end of the document processing. |
| 10098 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 10099 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10100 | ctxt->sax->endDocument(ctxt->userData); |
| 10101 | |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 10102 | /* |
| 10103 | * Remove locally kept entity definitions if the tree was not built |
| 10104 | */ |
| 10105 | if ((ctxt->myDoc != NULL) && |
| 10106 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 10107 | xmlFreeDoc(ctxt->myDoc); |
| 10108 | ctxt->myDoc = NULL; |
| 10109 | } |
| 10110 | |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 10111 | if ((ctxt->wellFormed) && (ctxt->myDoc != NULL)) { |
| 10112 | ctxt->myDoc->properties |= XML_DOC_WELLFORMED; |
| 10113 | if (ctxt->valid) |
| 10114 | ctxt->myDoc->properties |= XML_DOC_DTDVALID; |
| 10115 | if (ctxt->nsWellFormed) |
| 10116 | ctxt->myDoc->properties |= XML_DOC_NSVALID; |
| 10117 | if (ctxt->options & XML_PARSE_OLD10) |
| 10118 | ctxt->myDoc->properties |= XML_DOC_OLD10; |
| 10119 | } |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 10120 | if (! ctxt->wellFormed) { |
| 10121 | ctxt->valid = 0; |
| 10122 | return(-1); |
| 10123 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10124 | return(0); |
| 10125 | } |
| 10126 | |
| 10127 | /** |
| 10128 | * xmlParseExtParsedEnt: |
| 10129 | * @ctxt: an XML parser context |
| 10130 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10131 | * parse a general parsed entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10132 | * An external general parsed entity is well-formed if it matches the |
| 10133 | * production labeled extParsedEnt. |
| 10134 | * |
| 10135 | * [78] extParsedEnt ::= TextDecl? content |
| 10136 | * |
| 10137 | * Returns 0, -1 in case of error. the parser context is augmented |
| 10138 | * as a result of the parsing. |
| 10139 | */ |
| 10140 | |
| 10141 | int |
| 10142 | xmlParseExtParsedEnt(xmlParserCtxtPtr ctxt) { |
| 10143 | xmlChar start[4]; |
| 10144 | xmlCharEncoding enc; |
| 10145 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10146 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
| 10147 | return(-1); |
| 10148 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10149 | xmlDefaultSAXHandlerInit(); |
| 10150 | |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 10151 | xmlDetectSAX2(ctxt); |
| 10152 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10153 | GROW; |
| 10154 | |
| 10155 | /* |
| 10156 | * SAX: beginning of the document processing. |
| 10157 | */ |
| 10158 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10159 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 10160 | |
| 10161 | /* |
| 10162 | * Get the 4 first bytes and decode the charset |
| 10163 | * if enc != XML_CHAR_ENCODING_NONE |
| 10164 | * plug some encoding conversion routines. |
| 10165 | */ |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10166 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 10167 | start[0] = RAW; |
| 10168 | start[1] = NXT(1); |
| 10169 | start[2] = NXT(2); |
| 10170 | start[3] = NXT(3); |
| 10171 | enc = xmlDetectCharEncoding(start, 4); |
| 10172 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10173 | xmlSwitchEncoding(ctxt, enc); |
| 10174 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10175 | } |
| 10176 | |
| 10177 | |
| 10178 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10179 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10180 | } |
| 10181 | |
| 10182 | /* |
| 10183 | * Check for the XMLDecl in the Prolog. |
| 10184 | */ |
| 10185 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10186 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10187 | |
| 10188 | /* |
| 10189 | * Note that we will switch encoding on the fly. |
| 10190 | */ |
| 10191 | xmlParseXMLDecl(ctxt); |
| 10192 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10193 | /* |
| 10194 | * The XML REC instructs us to stop parsing right here |
| 10195 | */ |
| 10196 | return(-1); |
| 10197 | } |
| 10198 | SKIP_BLANKS; |
| 10199 | } else { |
| 10200 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 10201 | } |
| 10202 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 10203 | ctxt->sax->startDocument(ctxt->userData); |
| 10204 | |
| 10205 | /* |
| 10206 | * Doing validity checking on chunk doesn't make sense |
| 10207 | */ |
| 10208 | ctxt->instate = XML_PARSER_CONTENT; |
| 10209 | ctxt->validate = 0; |
| 10210 | ctxt->loadsubset = 0; |
| 10211 | ctxt->depth = 0; |
| 10212 | |
| 10213 | xmlParseContent(ctxt); |
| 10214 | |
| 10215 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10216 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10217 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10218 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10219 | } |
| 10220 | |
| 10221 | /* |
| 10222 | * SAX: end of the document processing. |
| 10223 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 10224 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10225 | ctxt->sax->endDocument(ctxt->userData); |
| 10226 | |
| 10227 | if (! ctxt->wellFormed) return(-1); |
| 10228 | return(0); |
| 10229 | } |
| 10230 | |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 10231 | #ifdef LIBXML_PUSH_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10232 | /************************************************************************ |
| 10233 | * * |
| 10234 | * Progressive parsing interfaces * |
| 10235 | * * |
| 10236 | ************************************************************************/ |
| 10237 | |
| 10238 | /** |
| 10239 | * xmlParseLookupSequence: |
| 10240 | * @ctxt: an XML parser context |
| 10241 | * @first: the first char to lookup |
| 10242 | * @next: the next char to lookup or zero |
| 10243 | * @third: the next char to lookup or zero |
| 10244 | * |
| 10245 | * Try to find if a sequence (first, next, third) or just (first next) or |
| 10246 | * (first) is available in the input stream. |
| 10247 | * This function has a side effect of (possibly) incrementing ctxt->checkIndex |
| 10248 | * to avoid rescanning sequences of bytes, it DOES change the state of the |
| 10249 | * parser, do not use liberally. |
| 10250 | * |
| 10251 | * Returns the index to the current parsing point if the full sequence |
| 10252 | * is available, -1 otherwise. |
| 10253 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 10254 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10255 | xmlParseLookupSequence(xmlParserCtxtPtr ctxt, xmlChar first, |
| 10256 | xmlChar next, xmlChar third) { |
| 10257 | int base, len; |
| 10258 | xmlParserInputPtr in; |
| 10259 | const xmlChar *buf; |
| 10260 | |
| 10261 | in = ctxt->input; |
| 10262 | if (in == NULL) return(-1); |
| 10263 | base = in->cur - in->base; |
| 10264 | if (base < 0) return(-1); |
| 10265 | if (ctxt->checkIndex > base) |
| 10266 | base = ctxt->checkIndex; |
| 10267 | if (in->buf == NULL) { |
| 10268 | buf = in->base; |
| 10269 | len = in->length; |
| 10270 | } else { |
| 10271 | buf = in->buf->buffer->content; |
| 10272 | len = in->buf->buffer->use; |
| 10273 | } |
| 10274 | /* take into account the sequence length */ |
| 10275 | if (third) len -= 2; |
| 10276 | else if (next) len --; |
| 10277 | for (;base < len;base++) { |
| 10278 | if (buf[base] == first) { |
| 10279 | if (third != 0) { |
| 10280 | if ((buf[base + 1] != next) || |
| 10281 | (buf[base + 2] != third)) continue; |
| 10282 | } else if (next != 0) { |
| 10283 | if (buf[base + 1] != next) continue; |
| 10284 | } |
| 10285 | ctxt->checkIndex = 0; |
| 10286 | #ifdef DEBUG_PUSH |
| 10287 | if (next == 0) |
| 10288 | xmlGenericError(xmlGenericErrorContext, |
| 10289 | "PP: lookup '%c' found at %d\n", |
| 10290 | first, base); |
| 10291 | else if (third == 0) |
| 10292 | xmlGenericError(xmlGenericErrorContext, |
| 10293 | "PP: lookup '%c%c' found at %d\n", |
| 10294 | first, next, base); |
| 10295 | else |
| 10296 | xmlGenericError(xmlGenericErrorContext, |
| 10297 | "PP: lookup '%c%c%c' found at %d\n", |
| 10298 | first, next, third, base); |
| 10299 | #endif |
| 10300 | return(base - (in->cur - in->base)); |
| 10301 | } |
| 10302 | } |
| 10303 | ctxt->checkIndex = base; |
| 10304 | #ifdef DEBUG_PUSH |
| 10305 | if (next == 0) |
| 10306 | xmlGenericError(xmlGenericErrorContext, |
| 10307 | "PP: lookup '%c' failed\n", first); |
| 10308 | else if (third == 0) |
| 10309 | xmlGenericError(xmlGenericErrorContext, |
| 10310 | "PP: lookup '%c%c' failed\n", first, next); |
| 10311 | else |
| 10312 | xmlGenericError(xmlGenericErrorContext, |
| 10313 | "PP: lookup '%c%c%c' failed\n", first, next, third); |
| 10314 | #endif |
| 10315 | return(-1); |
| 10316 | } |
| 10317 | |
| 10318 | /** |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10319 | * xmlParseGetLasts: |
| 10320 | * @ctxt: an XML parser context |
| 10321 | * @lastlt: pointer to store the last '<' from the input |
| 10322 | * @lastgt: pointer to store the last '>' from the input |
| 10323 | * |
| 10324 | * Lookup the last < and > in the current chunk |
| 10325 | */ |
| 10326 | static void |
| 10327 | xmlParseGetLasts(xmlParserCtxtPtr ctxt, const xmlChar **lastlt, |
| 10328 | const xmlChar **lastgt) { |
| 10329 | const xmlChar *tmp; |
| 10330 | |
| 10331 | if ((ctxt == NULL) || (lastlt == NULL) || (lastgt == NULL)) { |
| 10332 | xmlGenericError(xmlGenericErrorContext, |
| 10333 | "Internal error: xmlParseGetLasts\n"); |
| 10334 | return; |
| 10335 | } |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 10336 | if ((ctxt->progressive != 0) && (ctxt->inputNr == 1)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10337 | tmp = ctxt->input->end; |
| 10338 | tmp--; |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 10339 | while ((tmp >= ctxt->input->base) && (*tmp != '<')) tmp--; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10340 | if (tmp < ctxt->input->base) { |
| 10341 | *lastlt = NULL; |
| 10342 | *lastgt = NULL; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10343 | } else { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 10344 | *lastlt = tmp; |
| 10345 | tmp++; |
| 10346 | while ((tmp < ctxt->input->end) && (*tmp != '>')) { |
| 10347 | if (*tmp == '\'') { |
| 10348 | tmp++; |
| 10349 | while ((tmp < ctxt->input->end) && (*tmp != '\'')) tmp++; |
| 10350 | if (tmp < ctxt->input->end) tmp++; |
| 10351 | } else if (*tmp == '"') { |
| 10352 | tmp++; |
| 10353 | while ((tmp < ctxt->input->end) && (*tmp != '"')) tmp++; |
| 10354 | if (tmp < ctxt->input->end) tmp++; |
| 10355 | } else |
| 10356 | tmp++; |
| 10357 | } |
| 10358 | if (tmp < ctxt->input->end) |
| 10359 | *lastgt = tmp; |
| 10360 | else { |
| 10361 | tmp = *lastlt; |
| 10362 | tmp--; |
| 10363 | while ((tmp >= ctxt->input->base) && (*tmp != '>')) tmp--; |
| 10364 | if (tmp >= ctxt->input->base) |
| 10365 | *lastgt = tmp; |
| 10366 | else |
| 10367 | *lastgt = NULL; |
| 10368 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10369 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10370 | } else { |
| 10371 | *lastlt = NULL; |
| 10372 | *lastgt = NULL; |
| 10373 | } |
| 10374 | } |
| 10375 | /** |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10376 | * xmlCheckCdataPush: |
| 10377 | * @cur: pointer to the bock of characters |
| 10378 | * @len: length of the block in bytes |
| 10379 | * |
| 10380 | * Check that the block of characters is okay as SCdata content [20] |
| 10381 | * |
| 10382 | * Returns the number of bytes to pass if okay, a negative index where an |
| 10383 | * UTF-8 error occured otherwise |
| 10384 | */ |
| 10385 | static int |
| 10386 | xmlCheckCdataPush(const xmlChar *utf, int len) { |
| 10387 | int ix; |
| 10388 | unsigned char c; |
| 10389 | int codepoint; |
| 10390 | |
| 10391 | if ((utf == NULL) || (len <= 0)) |
| 10392 | return(0); |
| 10393 | |
| 10394 | for (ix = 0; ix < len;) { /* string is 0-terminated */ |
| 10395 | c = utf[ix]; |
| 10396 | if ((c & 0x80) == 0x00) { /* 1-byte code, starts with 10 */ |
| 10397 | if (c >= 0x20) |
| 10398 | ix++; |
| 10399 | else if ((c == 0xA) || (c == 0xD) || (c == 0x9)) |
| 10400 | ix++; |
| 10401 | else |
| 10402 | return(-ix); |
| 10403 | } else if ((c & 0xe0) == 0xc0) {/* 2-byte code, starts with 110 */ |
| 10404 | if (ix + 2 > len) return(ix); |
| 10405 | if ((utf[ix+1] & 0xc0 ) != 0x80) |
| 10406 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 10407 | codepoint = (utf[ix] & 0x1f) << 6; |
| 10408 | codepoint |= utf[ix+1] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10409 | if (!xmlIsCharQ(codepoint)) |
| 10410 | return(-ix); |
| 10411 | ix += 2; |
| 10412 | } else if ((c & 0xf0) == 0xe0) {/* 3-byte code, starts with 1110 */ |
| 10413 | if (ix + 3 > len) return(ix); |
| 10414 | if (((utf[ix+1] & 0xc0) != 0x80) || |
| 10415 | ((utf[ix+2] & 0xc0) != 0x80)) |
| 10416 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 10417 | codepoint = (utf[ix] & 0xf) << 12; |
| 10418 | codepoint |= (utf[ix+1] & 0x3f) << 6; |
| 10419 | codepoint |= utf[ix+2] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10420 | if (!xmlIsCharQ(codepoint)) |
| 10421 | return(-ix); |
| 10422 | ix += 3; |
| 10423 | } else if ((c & 0xf8) == 0xf0) {/* 4-byte code, starts with 11110 */ |
| 10424 | if (ix + 4 > len) return(ix); |
| 10425 | if (((utf[ix+1] & 0xc0) != 0x80) || |
| 10426 | ((utf[ix+2] & 0xc0) != 0x80) || |
| 10427 | ((utf[ix+3] & 0xc0) != 0x80)) |
| 10428 | return(-ix); |
Daniel Veillard | eca59a2 | 2005-09-09 10:56:28 +0000 | [diff] [blame] | 10429 | codepoint = (utf[ix] & 0x7) << 18; |
| 10430 | codepoint |= (utf[ix+1] & 0x3f) << 12; |
| 10431 | codepoint |= (utf[ix+2] & 0x3f) << 6; |
| 10432 | codepoint |= utf[ix+3] & 0x3f; |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10433 | if (!xmlIsCharQ(codepoint)) |
| 10434 | return(-ix); |
| 10435 | ix += 4; |
| 10436 | } else /* unknown encoding */ |
| 10437 | return(-ix); |
| 10438 | } |
| 10439 | return(ix); |
| 10440 | } |
| 10441 | |
| 10442 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10443 | * xmlParseTryOrFinish: |
| 10444 | * @ctxt: an XML parser context |
| 10445 | * @terminate: last chunk indicator |
| 10446 | * |
| 10447 | * Try to progress on parsing |
| 10448 | * |
| 10449 | * Returns zero if no parsing was possible |
| 10450 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 10451 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10452 | xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) { |
| 10453 | int ret = 0; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 10454 | int avail, tlen; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10455 | xmlChar cur, next; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10456 | const xmlChar *lastlt, *lastgt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10457 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10458 | if (ctxt->input == NULL) |
| 10459 | return(0); |
| 10460 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10461 | #ifdef DEBUG_PUSH |
| 10462 | switch (ctxt->instate) { |
| 10463 | case XML_PARSER_EOF: |
| 10464 | xmlGenericError(xmlGenericErrorContext, |
| 10465 | "PP: try EOF\n"); break; |
| 10466 | case XML_PARSER_START: |
| 10467 | xmlGenericError(xmlGenericErrorContext, |
| 10468 | "PP: try START\n"); break; |
| 10469 | case XML_PARSER_MISC: |
| 10470 | xmlGenericError(xmlGenericErrorContext, |
| 10471 | "PP: try MISC\n");break; |
| 10472 | case XML_PARSER_COMMENT: |
| 10473 | xmlGenericError(xmlGenericErrorContext, |
| 10474 | "PP: try COMMENT\n");break; |
| 10475 | case XML_PARSER_PROLOG: |
| 10476 | xmlGenericError(xmlGenericErrorContext, |
| 10477 | "PP: try PROLOG\n");break; |
| 10478 | case XML_PARSER_START_TAG: |
| 10479 | xmlGenericError(xmlGenericErrorContext, |
| 10480 | "PP: try START_TAG\n");break; |
| 10481 | case XML_PARSER_CONTENT: |
| 10482 | xmlGenericError(xmlGenericErrorContext, |
| 10483 | "PP: try CONTENT\n");break; |
| 10484 | case XML_PARSER_CDATA_SECTION: |
| 10485 | xmlGenericError(xmlGenericErrorContext, |
| 10486 | "PP: try CDATA_SECTION\n");break; |
| 10487 | case XML_PARSER_END_TAG: |
| 10488 | xmlGenericError(xmlGenericErrorContext, |
| 10489 | "PP: try END_TAG\n");break; |
| 10490 | case XML_PARSER_ENTITY_DECL: |
| 10491 | xmlGenericError(xmlGenericErrorContext, |
| 10492 | "PP: try ENTITY_DECL\n");break; |
| 10493 | case XML_PARSER_ENTITY_VALUE: |
| 10494 | xmlGenericError(xmlGenericErrorContext, |
| 10495 | "PP: try ENTITY_VALUE\n");break; |
| 10496 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 10497 | xmlGenericError(xmlGenericErrorContext, |
| 10498 | "PP: try ATTRIBUTE_VALUE\n");break; |
| 10499 | case XML_PARSER_DTD: |
| 10500 | xmlGenericError(xmlGenericErrorContext, |
| 10501 | "PP: try DTD\n");break; |
| 10502 | case XML_PARSER_EPILOG: |
| 10503 | xmlGenericError(xmlGenericErrorContext, |
| 10504 | "PP: try EPILOG\n");break; |
| 10505 | case XML_PARSER_PI: |
| 10506 | xmlGenericError(xmlGenericErrorContext, |
| 10507 | "PP: try PI\n");break; |
| 10508 | case XML_PARSER_IGNORE: |
| 10509 | xmlGenericError(xmlGenericErrorContext, |
| 10510 | "PP: try IGNORE\n");break; |
| 10511 | } |
| 10512 | #endif |
| 10513 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 10514 | if ((ctxt->input != NULL) && |
| 10515 | (ctxt->input->cur - ctxt->input->base > 4096)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10516 | xmlSHRINK(ctxt); |
| 10517 | ctxt->checkIndex = 0; |
| 10518 | } |
| 10519 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Aleksey Sanin | e48a318 | 2002-05-09 18:20:01 +0000 | [diff] [blame] | 10520 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10521 | while (1) { |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 10522 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 10523 | return(0); |
| 10524 | |
| 10525 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10526 | /* |
| 10527 | * Pop-up of finished entities. |
| 10528 | */ |
| 10529 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 10530 | xmlPopInput(ctxt); |
| 10531 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 10532 | if (ctxt->input == NULL) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10533 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10534 | avail = ctxt->input->length - |
| 10535 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 10536 | else { |
| 10537 | /* |
| 10538 | * If we are operating on converted input, try to flush |
| 10539 | * remainng chars to avoid them stalling in the non-converted |
| 10540 | * buffer. |
| 10541 | */ |
| 10542 | if ((ctxt->input->buf->raw != NULL) && |
| 10543 | (ctxt->input->buf->raw->use > 0)) { |
| 10544 | int base = ctxt->input->base - |
| 10545 | ctxt->input->buf->buffer->content; |
| 10546 | int current = ctxt->input->cur - ctxt->input->base; |
| 10547 | |
| 10548 | xmlParserInputBufferPush(ctxt->input->buf, 0, ""); |
| 10549 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 10550 | ctxt->input->cur = ctxt->input->base + current; |
| 10551 | ctxt->input->end = |
| 10552 | &ctxt->input->buf->buffer->content[ |
| 10553 | ctxt->input->buf->buffer->use]; |
| 10554 | } |
| 10555 | avail = ctxt->input->buf->buffer->use - |
| 10556 | (ctxt->input->cur - ctxt->input->base); |
| 10557 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10558 | if (avail < 1) |
| 10559 | goto done; |
| 10560 | switch (ctxt->instate) { |
| 10561 | case XML_PARSER_EOF: |
| 10562 | /* |
| 10563 | * Document parsing is done ! |
| 10564 | */ |
| 10565 | goto done; |
| 10566 | case XML_PARSER_START: |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 10567 | if (ctxt->charset == XML_CHAR_ENCODING_NONE) { |
| 10568 | xmlChar start[4]; |
| 10569 | xmlCharEncoding enc; |
| 10570 | |
| 10571 | /* |
| 10572 | * Very first chars read from the document flow. |
| 10573 | */ |
| 10574 | if (avail < 4) |
| 10575 | goto done; |
| 10576 | |
| 10577 | /* |
| 10578 | * Get the 4 first bytes and decode the charset |
| 10579 | * if enc != XML_CHAR_ENCODING_NONE |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 10580 | * plug some encoding conversion routines, |
| 10581 | * else xmlSwitchEncoding will set to (default) |
| 10582 | * UTF8. |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 10583 | */ |
| 10584 | start[0] = RAW; |
| 10585 | start[1] = NXT(1); |
| 10586 | start[2] = NXT(2); |
| 10587 | start[3] = NXT(3); |
| 10588 | enc = xmlDetectCharEncoding(start, 4); |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 10589 | xmlSwitchEncoding(ctxt, enc); |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 10590 | break; |
| 10591 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10592 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 10593 | if (avail < 2) |
| 10594 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10595 | cur = ctxt->input->cur[0]; |
| 10596 | next = ctxt->input->cur[1]; |
| 10597 | if (cur == 0) { |
| 10598 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10599 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 10600 | &xmlDefaultSAXLocator); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10601 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10602 | ctxt->instate = XML_PARSER_EOF; |
| 10603 | #ifdef DEBUG_PUSH |
| 10604 | xmlGenericError(xmlGenericErrorContext, |
| 10605 | "PP: entering EOF\n"); |
| 10606 | #endif |
| 10607 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 10608 | ctxt->sax->endDocument(ctxt->userData); |
| 10609 | goto done; |
| 10610 | } |
| 10611 | if ((cur == '<') && (next == '?')) { |
| 10612 | /* PI or XML decl */ |
| 10613 | if (avail < 5) return(ret); |
| 10614 | if ((!terminate) && |
| 10615 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 10616 | return(ret); |
| 10617 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10618 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 10619 | &xmlDefaultSAXLocator); |
| 10620 | if ((ctxt->input->cur[2] == 'x') && |
| 10621 | (ctxt->input->cur[3] == 'm') && |
| 10622 | (ctxt->input->cur[4] == 'l') && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 10623 | (IS_BLANK_CH(ctxt->input->cur[5]))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10624 | ret += 5; |
| 10625 | #ifdef DEBUG_PUSH |
| 10626 | xmlGenericError(xmlGenericErrorContext, |
| 10627 | "PP: Parsing XML Decl\n"); |
| 10628 | #endif |
| 10629 | xmlParseXMLDecl(ctxt); |
| 10630 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 10631 | /* |
| 10632 | * The XML REC instructs us to stop parsing right |
| 10633 | * here |
| 10634 | */ |
| 10635 | ctxt->instate = XML_PARSER_EOF; |
| 10636 | return(0); |
| 10637 | } |
| 10638 | ctxt->standalone = ctxt->input->standalone; |
| 10639 | if ((ctxt->encoding == NULL) && |
| 10640 | (ctxt->input->encoding != NULL)) |
| 10641 | ctxt->encoding = xmlStrdup(ctxt->input->encoding); |
| 10642 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 10643 | (!ctxt->disableSAX)) |
| 10644 | ctxt->sax->startDocument(ctxt->userData); |
| 10645 | ctxt->instate = XML_PARSER_MISC; |
| 10646 | #ifdef DEBUG_PUSH |
| 10647 | xmlGenericError(xmlGenericErrorContext, |
| 10648 | "PP: entering MISC\n"); |
| 10649 | #endif |
| 10650 | } else { |
| 10651 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 10652 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 10653 | (!ctxt->disableSAX)) |
| 10654 | ctxt->sax->startDocument(ctxt->userData); |
| 10655 | ctxt->instate = XML_PARSER_MISC; |
| 10656 | #ifdef DEBUG_PUSH |
| 10657 | xmlGenericError(xmlGenericErrorContext, |
| 10658 | "PP: entering MISC\n"); |
| 10659 | #endif |
| 10660 | } |
| 10661 | } else { |
| 10662 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 10663 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 10664 | &xmlDefaultSAXLocator); |
| 10665 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 10666 | if (ctxt->version == NULL) { |
| 10667 | xmlErrMemory(ctxt, NULL); |
| 10668 | break; |
| 10669 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10670 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 10671 | (!ctxt->disableSAX)) |
| 10672 | ctxt->sax->startDocument(ctxt->userData); |
| 10673 | ctxt->instate = XML_PARSER_MISC; |
| 10674 | #ifdef DEBUG_PUSH |
| 10675 | xmlGenericError(xmlGenericErrorContext, |
| 10676 | "PP: entering MISC\n"); |
| 10677 | #endif |
| 10678 | } |
| 10679 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10680 | case XML_PARSER_START_TAG: { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10681 | const xmlChar *name; |
| 10682 | const xmlChar *prefix; |
| 10683 | const xmlChar *URI; |
| 10684 | int nsNr = ctxt->nsNr; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10685 | |
| 10686 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 10687 | goto done; |
| 10688 | cur = ctxt->input->cur[0]; |
| 10689 | if (cur != '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10690 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10691 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10692 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 10693 | ctxt->sax->endDocument(ctxt->userData); |
| 10694 | goto done; |
| 10695 | } |
| 10696 | if (!terminate) { |
| 10697 | if (ctxt->progressive) { |
Daniel Veillard | b374400 | 2004-02-18 14:28:22 +0000 | [diff] [blame] | 10698 | /* > can be found unescaped in attribute values */ |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 10699 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10700 | goto done; |
| 10701 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 10702 | goto done; |
| 10703 | } |
| 10704 | } |
| 10705 | if (ctxt->spaceNr == 0) |
| 10706 | spacePush(ctxt, -1); |
Daniel Veillard | 1114d00 | 2006-10-12 16:24:35 +0000 | [diff] [blame] | 10707 | else if (*ctxt->space == -2) |
| 10708 | spacePush(ctxt, -1); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10709 | else |
| 10710 | spacePush(ctxt, *ctxt->space); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10711 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10712 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10713 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 10714 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10715 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10716 | else |
| 10717 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10718 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10719 | if (name == NULL) { |
| 10720 | spacePop(ctxt); |
| 10721 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10722 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 10723 | ctxt->sax->endDocument(ctxt->userData); |
| 10724 | goto done; |
| 10725 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10726 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10727 | /* |
| 10728 | * [ VC: Root Element Type ] |
| 10729 | * The Name in the document type declaration must match |
| 10730 | * the element type of the root element. |
| 10731 | */ |
| 10732 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 10733 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 10734 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10735 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10736 | |
| 10737 | /* |
| 10738 | * Check for an Empty Element. |
| 10739 | */ |
| 10740 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 10741 | SKIP(2); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10742 | |
| 10743 | if (ctxt->sax2) { |
| 10744 | if ((ctxt->sax != NULL) && |
| 10745 | (ctxt->sax->endElementNs != NULL) && |
| 10746 | (!ctxt->disableSAX)) |
| 10747 | ctxt->sax->endElementNs(ctxt->userData, name, |
| 10748 | prefix, URI); |
Daniel Veillard | 48df961 | 2005-01-04 21:50:05 +0000 | [diff] [blame] | 10749 | if (ctxt->nsNr - nsNr > 0) |
| 10750 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10751 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10752 | } else { |
| 10753 | if ((ctxt->sax != NULL) && |
| 10754 | (ctxt->sax->endElement != NULL) && |
| 10755 | (!ctxt->disableSAX)) |
| 10756 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10757 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10758 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10759 | spacePop(ctxt); |
| 10760 | if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10761 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10762 | } else { |
| 10763 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10764 | } |
| 10765 | break; |
| 10766 | } |
| 10767 | if (RAW == '>') { |
| 10768 | NEXT; |
| 10769 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 10770 | xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED, |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10771 | "Couldn't find end of Start Tag %s\n", |
| 10772 | name); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10773 | nodePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10774 | spacePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10775 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10776 | if (ctxt->sax2) |
| 10777 | nameNsPush(ctxt, name, prefix, URI, ctxt->nsNr - nsNr); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10778 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10779 | else |
| 10780 | namePush(ctxt, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10781 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10782 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10783 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10784 | break; |
| 10785 | } |
| 10786 | case XML_PARSER_CONTENT: { |
| 10787 | const xmlChar *test; |
| 10788 | unsigned int cons; |
| 10789 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 10790 | goto done; |
| 10791 | cur = ctxt->input->cur[0]; |
| 10792 | next = ctxt->input->cur[1]; |
| 10793 | |
| 10794 | test = CUR_PTR; |
| 10795 | cons = ctxt->input->consumed; |
| 10796 | if ((cur == '<') && (next == '/')) { |
| 10797 | ctxt->instate = XML_PARSER_END_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10798 | break; |
| 10799 | } else if ((cur == '<') && (next == '?')) { |
| 10800 | if ((!terminate) && |
| 10801 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 10802 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10803 | xmlParsePI(ctxt); |
| 10804 | } else if ((cur == '<') && (next != '!')) { |
| 10805 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10806 | break; |
| 10807 | } else if ((cur == '<') && (next == '!') && |
| 10808 | (ctxt->input->cur[2] == '-') && |
| 10809 | (ctxt->input->cur[3] == '-')) { |
Daniel Veillard | 6974feb | 2006-02-05 02:43:36 +0000 | [diff] [blame] | 10810 | int term; |
| 10811 | |
| 10812 | if (avail < 4) |
| 10813 | goto done; |
| 10814 | ctxt->input->cur += 4; |
| 10815 | term = xmlParseLookupSequence(ctxt, '-', '-', '>'); |
| 10816 | ctxt->input->cur -= 4; |
| 10817 | if ((!terminate) && (term < 0)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10818 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10819 | xmlParseComment(ctxt); |
| 10820 | ctxt->instate = XML_PARSER_CONTENT; |
| 10821 | } else if ((cur == '<') && (ctxt->input->cur[1] == '!') && |
| 10822 | (ctxt->input->cur[2] == '[') && |
| 10823 | (ctxt->input->cur[3] == 'C') && |
| 10824 | (ctxt->input->cur[4] == 'D') && |
| 10825 | (ctxt->input->cur[5] == 'A') && |
| 10826 | (ctxt->input->cur[6] == 'T') && |
| 10827 | (ctxt->input->cur[7] == 'A') && |
| 10828 | (ctxt->input->cur[8] == '[')) { |
| 10829 | SKIP(9); |
| 10830 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10831 | break; |
| 10832 | } else if ((cur == '<') && (next == '!') && |
| 10833 | (avail < 9)) { |
| 10834 | goto done; |
| 10835 | } else if (cur == '&') { |
| 10836 | if ((!terminate) && |
| 10837 | (xmlParseLookupSequence(ctxt, ';', 0, 0) < 0)) |
| 10838 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10839 | xmlParseReference(ctxt); |
| 10840 | } else { |
| 10841 | /* TODO Avoid the extra copy, handle directly !!! */ |
| 10842 | /* |
| 10843 | * Goal of the following test is: |
| 10844 | * - minimize calls to the SAX 'character' callback |
| 10845 | * when they are mergeable |
| 10846 | * - handle an problem for isBlank when we only parse |
| 10847 | * a sequence of blank chars and the next one is |
| 10848 | * not available to check against '<' presence. |
| 10849 | * - tries to homogenize the differences in SAX |
| 10850 | * callbacks between the push and pull versions |
| 10851 | * of the parser. |
| 10852 | */ |
| 10853 | if ((ctxt->inputNr == 1) && |
| 10854 | (avail < XML_PARSER_BIG_BUFFER_SIZE)) { |
| 10855 | if (!terminate) { |
| 10856 | if (ctxt->progressive) { |
| 10857 | if ((lastlt == NULL) || |
| 10858 | (ctxt->input->cur > lastlt)) |
| 10859 | goto done; |
| 10860 | } else if (xmlParseLookupSequence(ctxt, |
| 10861 | '<', 0, 0) < 0) { |
| 10862 | goto done; |
| 10863 | } |
| 10864 | } |
| 10865 | } |
| 10866 | ctxt->checkIndex = 0; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10867 | xmlParseCharData(ctxt, 0); |
| 10868 | } |
| 10869 | /* |
| 10870 | * Pop-up of finished entities. |
| 10871 | */ |
| 10872 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 10873 | xmlPopInput(ctxt); |
| 10874 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10875 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 10876 | "detected an error in element content\n"); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10877 | ctxt->instate = XML_PARSER_EOF; |
| 10878 | break; |
| 10879 | } |
| 10880 | break; |
| 10881 | } |
| 10882 | case XML_PARSER_END_TAG: |
| 10883 | if (avail < 2) |
| 10884 | goto done; |
| 10885 | if (!terminate) { |
| 10886 | if (ctxt->progressive) { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 10887 | /* > can be found unescaped in attribute values */ |
| 10888 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10889 | goto done; |
| 10890 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 10891 | goto done; |
| 10892 | } |
| 10893 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10894 | if (ctxt->sax2) { |
| 10895 | xmlParseEndTag2(ctxt, |
| 10896 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 3], |
| 10897 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 2], 0, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 10898 | (int) (long) ctxt->pushTab[ctxt->nameNr * 3 - 1], 0); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10899 | nameNsPop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10900 | } |
| 10901 | #ifdef LIBXML_SAX1_ENABLED |
| 10902 | else |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10903 | xmlParseEndTag1(ctxt, 0); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10904 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10905 | if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10906 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10907 | } else { |
| 10908 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10909 | } |
| 10910 | break; |
| 10911 | case XML_PARSER_CDATA_SECTION: { |
| 10912 | /* |
| 10913 | * The Push mode need to have the SAX callback for |
| 10914 | * cdataBlock merge back contiguous callbacks. |
| 10915 | */ |
| 10916 | int base; |
| 10917 | |
| 10918 | base = xmlParseLookupSequence(ctxt, ']', ']', '>'); |
| 10919 | if (base < 0) { |
| 10920 | if (avail >= XML_PARSER_BIG_BUFFER_SIZE + 2) { |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10921 | int tmp; |
| 10922 | |
| 10923 | tmp = xmlCheckCdataPush(ctxt->input->cur, |
| 10924 | XML_PARSER_BIG_BUFFER_SIZE); |
| 10925 | if (tmp < 0) { |
| 10926 | tmp = -tmp; |
| 10927 | ctxt->input->cur += tmp; |
| 10928 | goto encoding_error; |
| 10929 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10930 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 10931 | if (ctxt->sax->cdataBlock != NULL) |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 10932 | ctxt->sax->cdataBlock(ctxt->userData, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10933 | ctxt->input->cur, tmp); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 10934 | else if (ctxt->sax->characters != NULL) |
| 10935 | ctxt->sax->characters(ctxt->userData, |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10936 | ctxt->input->cur, tmp); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10937 | } |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10938 | SKIPL(tmp); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10939 | ctxt->checkIndex = 0; |
| 10940 | } |
| 10941 | goto done; |
| 10942 | } else { |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 10943 | int tmp; |
| 10944 | |
| 10945 | tmp = xmlCheckCdataPush(ctxt->input->cur, base); |
| 10946 | if ((tmp < 0) || (tmp != base)) { |
| 10947 | tmp = -tmp; |
| 10948 | ctxt->input->cur += tmp; |
| 10949 | goto encoding_error; |
| 10950 | } |
Daniel Veillard | d0d2f09 | 2008-03-07 16:50:21 +0000 | [diff] [blame] | 10951 | if ((ctxt->sax != NULL) && (base == 0) && |
| 10952 | (ctxt->sax->cdataBlock != NULL) && |
| 10953 | (!ctxt->disableSAX)) { |
| 10954 | /* |
| 10955 | * Special case to provide identical behaviour |
| 10956 | * between pull and push parsers on enpty CDATA |
| 10957 | * sections |
| 10958 | */ |
| 10959 | if ((ctxt->input->cur - ctxt->input->base >= 9) && |
| 10960 | (!strncmp((const char *)&ctxt->input->cur[-9], |
| 10961 | "<![CDATA[", 9))) |
| 10962 | ctxt->sax->cdataBlock(ctxt->userData, |
| 10963 | BAD_CAST "", 0); |
| 10964 | } else if ((ctxt->sax != NULL) && (base > 0) && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10965 | (!ctxt->disableSAX)) { |
| 10966 | if (ctxt->sax->cdataBlock != NULL) |
| 10967 | ctxt->sax->cdataBlock(ctxt->userData, |
| 10968 | ctxt->input->cur, base); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 10969 | else if (ctxt->sax->characters != NULL) |
| 10970 | ctxt->sax->characters(ctxt->userData, |
| 10971 | ctxt->input->cur, base); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10972 | } |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 10973 | SKIPL(base + 3); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10974 | ctxt->checkIndex = 0; |
| 10975 | ctxt->instate = XML_PARSER_CONTENT; |
| 10976 | #ifdef DEBUG_PUSH |
| 10977 | xmlGenericError(xmlGenericErrorContext, |
| 10978 | "PP: entering CONTENT\n"); |
| 10979 | #endif |
| 10980 | } |
| 10981 | break; |
| 10982 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10983 | case XML_PARSER_MISC: |
| 10984 | SKIP_BLANKS; |
| 10985 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10986 | avail = ctxt->input->length - |
| 10987 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10988 | else |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 10989 | avail = ctxt->input->buf->buffer->use - |
| 10990 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10991 | if (avail < 2) |
| 10992 | goto done; |
| 10993 | cur = ctxt->input->cur[0]; |
| 10994 | next = ctxt->input->cur[1]; |
| 10995 | if ((cur == '<') && (next == '?')) { |
| 10996 | if ((!terminate) && |
| 10997 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 10998 | goto done; |
| 10999 | #ifdef DEBUG_PUSH |
| 11000 | xmlGenericError(xmlGenericErrorContext, |
| 11001 | "PP: Parsing PI\n"); |
| 11002 | #endif |
| 11003 | xmlParsePI(ctxt); |
Daniel Veillard | 40e4b21 | 2007-06-12 14:46:40 +0000 | [diff] [blame] | 11004 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11005 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11006 | (ctxt->input->cur[2] == '-') && |
| 11007 | (ctxt->input->cur[3] == '-')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11008 | if ((!terminate) && |
| 11009 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 11010 | goto done; |
| 11011 | #ifdef DEBUG_PUSH |
| 11012 | xmlGenericError(xmlGenericErrorContext, |
| 11013 | "PP: Parsing Comment\n"); |
| 11014 | #endif |
| 11015 | xmlParseComment(ctxt); |
| 11016 | ctxt->instate = XML_PARSER_MISC; |
Daniel Veillard | dfac946 | 2007-06-12 14:44:32 +0000 | [diff] [blame] | 11017 | ctxt->checkIndex = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11018 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11019 | (ctxt->input->cur[2] == 'D') && |
| 11020 | (ctxt->input->cur[3] == 'O') && |
| 11021 | (ctxt->input->cur[4] == 'C') && |
| 11022 | (ctxt->input->cur[5] == 'T') && |
| 11023 | (ctxt->input->cur[6] == 'Y') && |
| 11024 | (ctxt->input->cur[7] == 'P') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11025 | (ctxt->input->cur[8] == 'E')) { |
| 11026 | if ((!terminate) && |
| 11027 | (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0)) |
| 11028 | goto done; |
| 11029 | #ifdef DEBUG_PUSH |
| 11030 | xmlGenericError(xmlGenericErrorContext, |
| 11031 | "PP: Parsing internal subset\n"); |
| 11032 | #endif |
| 11033 | ctxt->inSubset = 1; |
| 11034 | xmlParseDocTypeDecl(ctxt); |
| 11035 | if (RAW == '[') { |
| 11036 | ctxt->instate = XML_PARSER_DTD; |
| 11037 | #ifdef DEBUG_PUSH |
| 11038 | xmlGenericError(xmlGenericErrorContext, |
| 11039 | "PP: entering DTD\n"); |
| 11040 | #endif |
| 11041 | } else { |
| 11042 | /* |
| 11043 | * Create and update the external subset. |
| 11044 | */ |
| 11045 | ctxt->inSubset = 2; |
| 11046 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 11047 | (ctxt->sax->externalSubset != NULL)) |
| 11048 | ctxt->sax->externalSubset(ctxt->userData, |
| 11049 | ctxt->intSubName, ctxt->extSubSystem, |
| 11050 | ctxt->extSubURI); |
| 11051 | ctxt->inSubset = 0; |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 11052 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11053 | ctxt->instate = XML_PARSER_PROLOG; |
| 11054 | #ifdef DEBUG_PUSH |
| 11055 | xmlGenericError(xmlGenericErrorContext, |
| 11056 | "PP: entering PROLOG\n"); |
| 11057 | #endif |
| 11058 | } |
| 11059 | } else if ((cur == '<') && (next == '!') && |
| 11060 | (avail < 9)) { |
| 11061 | goto done; |
| 11062 | } else { |
| 11063 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11064 | ctxt->progressive = 1; |
| 11065 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11066 | #ifdef DEBUG_PUSH |
| 11067 | xmlGenericError(xmlGenericErrorContext, |
| 11068 | "PP: entering START_TAG\n"); |
| 11069 | #endif |
| 11070 | } |
| 11071 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11072 | case XML_PARSER_PROLOG: |
| 11073 | SKIP_BLANKS; |
| 11074 | if (ctxt->input->buf == NULL) |
| 11075 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 11076 | else |
| 11077 | avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base); |
| 11078 | if (avail < 2) |
| 11079 | goto done; |
| 11080 | cur = ctxt->input->cur[0]; |
| 11081 | next = ctxt->input->cur[1]; |
| 11082 | if ((cur == '<') && (next == '?')) { |
| 11083 | if ((!terminate) && |
| 11084 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 11085 | goto done; |
| 11086 | #ifdef DEBUG_PUSH |
| 11087 | xmlGenericError(xmlGenericErrorContext, |
| 11088 | "PP: Parsing PI\n"); |
| 11089 | #endif |
| 11090 | xmlParsePI(ctxt); |
| 11091 | } else if ((cur == '<') && (next == '!') && |
| 11092 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 11093 | if ((!terminate) && |
| 11094 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 11095 | goto done; |
| 11096 | #ifdef DEBUG_PUSH |
| 11097 | xmlGenericError(xmlGenericErrorContext, |
| 11098 | "PP: Parsing Comment\n"); |
| 11099 | #endif |
| 11100 | xmlParseComment(ctxt); |
| 11101 | ctxt->instate = XML_PARSER_PROLOG; |
| 11102 | } else if ((cur == '<') && (next == '!') && |
| 11103 | (avail < 4)) { |
| 11104 | goto done; |
| 11105 | } else { |
| 11106 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 11107 | if (ctxt->progressive == 0) |
| 11108 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11109 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11110 | #ifdef DEBUG_PUSH |
| 11111 | xmlGenericError(xmlGenericErrorContext, |
| 11112 | "PP: entering START_TAG\n"); |
| 11113 | #endif |
| 11114 | } |
| 11115 | break; |
| 11116 | case XML_PARSER_EPILOG: |
| 11117 | SKIP_BLANKS; |
| 11118 | if (ctxt->input->buf == NULL) |
| 11119 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 11120 | else |
| 11121 | avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base); |
| 11122 | if (avail < 2) |
| 11123 | goto done; |
| 11124 | cur = ctxt->input->cur[0]; |
| 11125 | next = ctxt->input->cur[1]; |
| 11126 | if ((cur == '<') && (next == '?')) { |
| 11127 | if ((!terminate) && |
| 11128 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 11129 | goto done; |
| 11130 | #ifdef DEBUG_PUSH |
| 11131 | xmlGenericError(xmlGenericErrorContext, |
| 11132 | "PP: Parsing PI\n"); |
| 11133 | #endif |
| 11134 | xmlParsePI(ctxt); |
| 11135 | ctxt->instate = XML_PARSER_EPILOG; |
| 11136 | } else if ((cur == '<') && (next == '!') && |
| 11137 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 11138 | if ((!terminate) && |
| 11139 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 11140 | goto done; |
| 11141 | #ifdef DEBUG_PUSH |
| 11142 | xmlGenericError(xmlGenericErrorContext, |
| 11143 | "PP: Parsing Comment\n"); |
| 11144 | #endif |
| 11145 | xmlParseComment(ctxt); |
| 11146 | ctxt->instate = XML_PARSER_EPILOG; |
| 11147 | } else if ((cur == '<') && (next == '!') && |
| 11148 | (avail < 4)) { |
| 11149 | goto done; |
| 11150 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11151 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11152 | ctxt->instate = XML_PARSER_EOF; |
| 11153 | #ifdef DEBUG_PUSH |
| 11154 | xmlGenericError(xmlGenericErrorContext, |
| 11155 | "PP: entering EOF\n"); |
| 11156 | #endif |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 11157 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11158 | ctxt->sax->endDocument(ctxt->userData); |
| 11159 | goto done; |
| 11160 | } |
| 11161 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11162 | case XML_PARSER_DTD: { |
| 11163 | /* |
| 11164 | * Sorry but progressive parsing of the internal subset |
| 11165 | * is not expected to be supported. We first check that |
| 11166 | * the full content of the internal subset is available and |
| 11167 | * the parsing is launched only at that point. |
| 11168 | * Internal subset ends up with "']' S? '>'" in an unescaped |
| 11169 | * section and not in a ']]>' sequence which are conditional |
| 11170 | * sections (whoever argued to keep that crap in XML deserve |
| 11171 | * a place in hell !). |
| 11172 | */ |
| 11173 | int base, i; |
| 11174 | xmlChar *buf; |
| 11175 | xmlChar quote = 0; |
| 11176 | |
| 11177 | base = ctxt->input->cur - ctxt->input->base; |
| 11178 | if (base < 0) return(0); |
| 11179 | if (ctxt->checkIndex > base) |
| 11180 | base = ctxt->checkIndex; |
| 11181 | buf = ctxt->input->buf->buffer->content; |
| 11182 | for (;(unsigned int) base < ctxt->input->buf->buffer->use; |
| 11183 | base++) { |
| 11184 | if (quote != 0) { |
| 11185 | if (buf[base] == quote) |
| 11186 | quote = 0; |
| 11187 | continue; |
| 11188 | } |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11189 | if ((quote == 0) && (buf[base] == '<')) { |
| 11190 | int found = 0; |
| 11191 | /* special handling of comments */ |
| 11192 | if (((unsigned int) base + 4 < |
| 11193 | ctxt->input->buf->buffer->use) && |
| 11194 | (buf[base + 1] == '!') && |
| 11195 | (buf[base + 2] == '-') && |
| 11196 | (buf[base + 3] == '-')) { |
| 11197 | for (;(unsigned int) base + 3 < |
| 11198 | ctxt->input->buf->buffer->use; base++) { |
| 11199 | if ((buf[base] == '-') && |
| 11200 | (buf[base + 1] == '-') && |
| 11201 | (buf[base + 2] == '>')) { |
| 11202 | found = 1; |
| 11203 | base += 2; |
| 11204 | break; |
| 11205 | } |
| 11206 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11207 | if (!found) { |
| 11208 | #if 0 |
| 11209 | fprintf(stderr, "unfinished comment\n"); |
| 11210 | #endif |
| 11211 | break; /* for */ |
| 11212 | } |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 11213 | continue; |
| 11214 | } |
| 11215 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11216 | if (buf[base] == '"') { |
| 11217 | quote = '"'; |
| 11218 | continue; |
| 11219 | } |
| 11220 | if (buf[base] == '\'') { |
| 11221 | quote = '\''; |
| 11222 | continue; |
| 11223 | } |
| 11224 | if (buf[base] == ']') { |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11225 | #if 0 |
| 11226 | fprintf(stderr, "%c%c%c%c: ", buf[base], |
| 11227 | buf[base + 1], buf[base + 2], buf[base + 3]); |
| 11228 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11229 | if ((unsigned int) base +1 >= |
| 11230 | ctxt->input->buf->buffer->use) |
| 11231 | break; |
| 11232 | if (buf[base + 1] == ']') { |
| 11233 | /* conditional crap, skip both ']' ! */ |
| 11234 | base++; |
| 11235 | continue; |
| 11236 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11237 | for (i = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11238 | (unsigned int) base + i < ctxt->input->buf->buffer->use; |
| 11239 | i++) { |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11240 | if (buf[base + i] == '>') { |
| 11241 | #if 0 |
| 11242 | fprintf(stderr, "found\n"); |
| 11243 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11244 | goto found_end_int_subset; |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11245 | } |
| 11246 | if (!IS_BLANK_CH(buf[base + i])) { |
| 11247 | #if 0 |
| 11248 | fprintf(stderr, "not found\n"); |
| 11249 | #endif |
| 11250 | goto not_end_of_int_subset; |
| 11251 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11252 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11253 | #if 0 |
| 11254 | fprintf(stderr, "end of stream\n"); |
| 11255 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11256 | break; |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11257 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11258 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 11259 | not_end_of_int_subset: |
| 11260 | continue; /* for */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11261 | } |
| 11262 | /* |
| 11263 | * We didn't found the end of the Internal subset |
| 11264 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11265 | #ifdef DEBUG_PUSH |
| 11266 | if (next == 0) |
| 11267 | xmlGenericError(xmlGenericErrorContext, |
| 11268 | "PP: lookup of int subset end filed\n"); |
| 11269 | #endif |
| 11270 | goto done; |
| 11271 | |
| 11272 | found_end_int_subset: |
| 11273 | xmlParseInternalSubset(ctxt); |
| 11274 | ctxt->inSubset = 2; |
| 11275 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 11276 | (ctxt->sax->externalSubset != NULL)) |
| 11277 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 11278 | ctxt->extSubSystem, ctxt->extSubURI); |
| 11279 | ctxt->inSubset = 0; |
Daniel Veillard | ac4118d | 2008-01-11 05:27:32 +0000 | [diff] [blame] | 11280 | xmlCleanSpecialAttr(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11281 | ctxt->instate = XML_PARSER_PROLOG; |
| 11282 | ctxt->checkIndex = 0; |
| 11283 | #ifdef DEBUG_PUSH |
| 11284 | xmlGenericError(xmlGenericErrorContext, |
| 11285 | "PP: entering PROLOG\n"); |
| 11286 | #endif |
| 11287 | break; |
| 11288 | } |
| 11289 | case XML_PARSER_COMMENT: |
| 11290 | xmlGenericError(xmlGenericErrorContext, |
| 11291 | "PP: internal error, state == COMMENT\n"); |
| 11292 | ctxt->instate = XML_PARSER_CONTENT; |
| 11293 | #ifdef DEBUG_PUSH |
| 11294 | xmlGenericError(xmlGenericErrorContext, |
| 11295 | "PP: entering CONTENT\n"); |
| 11296 | #endif |
| 11297 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 11298 | case XML_PARSER_IGNORE: |
| 11299 | xmlGenericError(xmlGenericErrorContext, |
| 11300 | "PP: internal error, state == IGNORE"); |
| 11301 | ctxt->instate = XML_PARSER_DTD; |
| 11302 | #ifdef DEBUG_PUSH |
| 11303 | xmlGenericError(xmlGenericErrorContext, |
| 11304 | "PP: entering DTD\n"); |
| 11305 | #endif |
| 11306 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11307 | case XML_PARSER_PI: |
| 11308 | xmlGenericError(xmlGenericErrorContext, |
| 11309 | "PP: internal error, state == PI\n"); |
| 11310 | ctxt->instate = XML_PARSER_CONTENT; |
| 11311 | #ifdef DEBUG_PUSH |
| 11312 | xmlGenericError(xmlGenericErrorContext, |
| 11313 | "PP: entering CONTENT\n"); |
| 11314 | #endif |
| 11315 | break; |
| 11316 | case XML_PARSER_ENTITY_DECL: |
| 11317 | xmlGenericError(xmlGenericErrorContext, |
| 11318 | "PP: internal error, state == ENTITY_DECL\n"); |
| 11319 | ctxt->instate = XML_PARSER_DTD; |
| 11320 | #ifdef DEBUG_PUSH |
| 11321 | xmlGenericError(xmlGenericErrorContext, |
| 11322 | "PP: entering DTD\n"); |
| 11323 | #endif |
| 11324 | break; |
| 11325 | case XML_PARSER_ENTITY_VALUE: |
| 11326 | xmlGenericError(xmlGenericErrorContext, |
| 11327 | "PP: internal error, state == ENTITY_VALUE\n"); |
| 11328 | ctxt->instate = XML_PARSER_CONTENT; |
| 11329 | #ifdef DEBUG_PUSH |
| 11330 | xmlGenericError(xmlGenericErrorContext, |
| 11331 | "PP: entering DTD\n"); |
| 11332 | #endif |
| 11333 | break; |
| 11334 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 11335 | xmlGenericError(xmlGenericErrorContext, |
| 11336 | "PP: internal error, state == ATTRIBUTE_VALUE\n"); |
| 11337 | ctxt->instate = XML_PARSER_START_TAG; |
| 11338 | #ifdef DEBUG_PUSH |
| 11339 | xmlGenericError(xmlGenericErrorContext, |
| 11340 | "PP: entering START_TAG\n"); |
| 11341 | #endif |
| 11342 | break; |
| 11343 | case XML_PARSER_SYSTEM_LITERAL: |
| 11344 | xmlGenericError(xmlGenericErrorContext, |
| 11345 | "PP: internal error, state == SYSTEM_LITERAL\n"); |
| 11346 | ctxt->instate = XML_PARSER_START_TAG; |
| 11347 | #ifdef DEBUG_PUSH |
| 11348 | xmlGenericError(xmlGenericErrorContext, |
| 11349 | "PP: entering START_TAG\n"); |
| 11350 | #endif |
| 11351 | break; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 11352 | case XML_PARSER_PUBLIC_LITERAL: |
| 11353 | xmlGenericError(xmlGenericErrorContext, |
| 11354 | "PP: internal error, state == PUBLIC_LITERAL\n"); |
| 11355 | ctxt->instate = XML_PARSER_START_TAG; |
| 11356 | #ifdef DEBUG_PUSH |
| 11357 | xmlGenericError(xmlGenericErrorContext, |
| 11358 | "PP: entering START_TAG\n"); |
| 11359 | #endif |
| 11360 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11361 | } |
| 11362 | } |
| 11363 | done: |
| 11364 | #ifdef DEBUG_PUSH |
| 11365 | xmlGenericError(xmlGenericErrorContext, "PP: done %d\n", ret); |
| 11366 | #endif |
| 11367 | return(ret); |
Daniel Veillard | 3fa5e7e | 2005-07-04 11:12:25 +0000 | [diff] [blame] | 11368 | encoding_error: |
| 11369 | { |
| 11370 | char buffer[150]; |
| 11371 | |
| 11372 | snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n", |
| 11373 | ctxt->input->cur[0], ctxt->input->cur[1], |
| 11374 | ctxt->input->cur[2], ctxt->input->cur[3]); |
| 11375 | __xmlErrEncoding(ctxt, XML_ERR_INVALID_CHAR, |
| 11376 | "Input is not proper UTF-8, indicate encoding !\n%s", |
| 11377 | BAD_CAST buffer, NULL); |
| 11378 | } |
| 11379 | return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11380 | } |
| 11381 | |
| 11382 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11383 | * xmlParseChunk: |
| 11384 | * @ctxt: an XML parser context |
| 11385 | * @chunk: an char array |
| 11386 | * @size: the size in byte of the chunk |
| 11387 | * @terminate: last chunk indicator |
| 11388 | * |
| 11389 | * Parse a Chunk of memory |
| 11390 | * |
| 11391 | * Returns zero if no error, the xmlParserErrors otherwise. |
| 11392 | */ |
| 11393 | int |
| 11394 | xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size, |
| 11395 | int terminate) { |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 11396 | int end_in_lf = 0; |
| 11397 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 11398 | if (ctxt == NULL) |
| 11399 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 11400 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11401 | return(ctxt->errNo); |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 11402 | if (ctxt->instate == XML_PARSER_START) |
| 11403 | xmlDetectSAX2(ctxt); |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 11404 | if ((size > 0) && (chunk != NULL) && (!terminate) && |
| 11405 | (chunk[size - 1] == '\r')) { |
| 11406 | end_in_lf = 1; |
| 11407 | size--; |
| 11408 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11409 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 11410 | (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) { |
| 11411 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 11412 | int cur = ctxt->input->cur - ctxt->input->base; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 11413 | int res; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11414 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 11415 | res =xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
| 11416 | if (res < 0) { |
| 11417 | ctxt->errNo = XML_PARSER_EOF; |
| 11418 | ctxt->disableSAX = 1; |
| 11419 | return (XML_PARSER_EOF); |
| 11420 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11421 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 11422 | ctxt->input->cur = ctxt->input->base + cur; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 11423 | ctxt->input->end = |
| 11424 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11425 | #ifdef DEBUG_PUSH |
| 11426 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 11427 | #endif |
| 11428 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11429 | } else if (ctxt->instate != XML_PARSER_EOF) { |
| 11430 | if ((ctxt->input != NULL) && ctxt->input->buf != NULL) { |
| 11431 | xmlParserInputBufferPtr in = ctxt->input->buf; |
| 11432 | if ((in->encoder != NULL) && (in->buffer != NULL) && |
| 11433 | (in->raw != NULL)) { |
| 11434 | int nbchars; |
| 11435 | |
| 11436 | nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw); |
| 11437 | if (nbchars < 0) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 11438 | /* TODO 2.6.0 */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11439 | xmlGenericError(xmlGenericErrorContext, |
| 11440 | "xmlParseChunk: encoder error\n"); |
| 11441 | return(XML_ERR_INVALID_ENCODING); |
| 11442 | } |
| 11443 | } |
| 11444 | } |
| 11445 | } |
| 11446 | xmlParseTryOrFinish(ctxt, terminate); |
Daniel Veillard | a617e24 | 2006-01-09 14:38:44 +0000 | [diff] [blame] | 11447 | if ((end_in_lf == 1) && (ctxt->input != NULL) && |
| 11448 | (ctxt->input->buf != NULL)) { |
| 11449 | xmlParserInputBufferPush(ctxt->input->buf, 1, "\r"); |
| 11450 | } |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 11451 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11452 | return(ctxt->errNo); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11453 | if (terminate) { |
| 11454 | /* |
| 11455 | * Check for termination |
| 11456 | */ |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 11457 | int avail = 0; |
| 11458 | |
| 11459 | if (ctxt->input != NULL) { |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 11460 | if (ctxt->input->buf == NULL) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 11461 | avail = ctxt->input->length - |
| 11462 | (ctxt->input->cur - ctxt->input->base); |
| 11463 | else |
| 11464 | avail = ctxt->input->buf->buffer->use - |
| 11465 | (ctxt->input->cur - ctxt->input->base); |
| 11466 | } |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 11467 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11468 | if ((ctxt->instate != XML_PARSER_EOF) && |
| 11469 | (ctxt->instate != XML_PARSER_EPILOG)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11470 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11471 | } |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 11472 | if ((ctxt->instate == XML_PARSER_EPILOG) && (avail > 0)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11473 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 11474 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11475 | if (ctxt->instate != XML_PARSER_EOF) { |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 11476 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11477 | ctxt->sax->endDocument(ctxt->userData); |
| 11478 | } |
| 11479 | ctxt->instate = XML_PARSER_EOF; |
| 11480 | } |
| 11481 | return((xmlParserErrors) ctxt->errNo); |
| 11482 | } |
| 11483 | |
| 11484 | /************************************************************************ |
| 11485 | * * |
| 11486 | * I/O front end functions to the parser * |
| 11487 | * * |
| 11488 | ************************************************************************/ |
| 11489 | |
| 11490 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11491 | * xmlCreatePushParserCtxt: |
| 11492 | * @sax: a SAX handler |
| 11493 | * @user_data: The user data returned on SAX callbacks |
| 11494 | * @chunk: a pointer to an array of chars |
| 11495 | * @size: number of chars in the array |
| 11496 | * @filename: an optional file name or URI |
| 11497 | * |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 11498 | * Create a parser context for using the XML parser in push mode. |
| 11499 | * If @buffer and @size are non-NULL, the data is used to detect |
| 11500 | * the encoding. The remaining characters will be parsed so they |
| 11501 | * don't need to be fed in again through xmlParseChunk. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11502 | * To allow content encoding detection, @size should be >= 4 |
| 11503 | * The value of @filename is used for fetching external entities |
| 11504 | * and error/warning reports. |
| 11505 | * |
| 11506 | * Returns the new parser context or NULL |
| 11507 | */ |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 11508 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11509 | xmlParserCtxtPtr |
| 11510 | xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
| 11511 | const char *chunk, int size, const char *filename) { |
| 11512 | xmlParserCtxtPtr ctxt; |
| 11513 | xmlParserInputPtr inputStream; |
| 11514 | xmlParserInputBufferPtr buf; |
| 11515 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 11516 | |
| 11517 | /* |
| 11518 | * plug some encoding conversion routines |
| 11519 | */ |
| 11520 | if ((chunk != NULL) && (size >= 4)) |
| 11521 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 11522 | |
| 11523 | buf = xmlAllocParserInputBuffer(enc); |
| 11524 | if (buf == NULL) return(NULL); |
| 11525 | |
| 11526 | ctxt = xmlNewParserCtxt(); |
| 11527 | if (ctxt == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 11528 | xmlErrMemory(NULL, "creating parser: out of memory\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11529 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11530 | return(NULL); |
| 11531 | } |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 11532 | ctxt->dictNames = 1; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11533 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * sizeof(xmlChar *)); |
| 11534 | if (ctxt->pushTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11535 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11536 | xmlFreeParserInputBuffer(buf); |
| 11537 | xmlFreeParserCtxt(ctxt); |
| 11538 | return(NULL); |
| 11539 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11540 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11541 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 11542 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11543 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11544 | xmlFree(ctxt->sax); |
| 11545 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 11546 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11547 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11548 | xmlFreeParserInputBuffer(buf); |
| 11549 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11550 | return(NULL); |
| 11551 | } |
Daniel Veillard | 5ea30d7 | 2004-11-08 11:54:28 +0000 | [diff] [blame] | 11552 | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
| 11553 | if (sax->initialized == XML_SAX2_MAGIC) |
| 11554 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 11555 | else |
| 11556 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11557 | if (user_data != NULL) |
| 11558 | ctxt->userData = user_data; |
| 11559 | } |
| 11560 | if (filename == NULL) { |
| 11561 | ctxt->directory = NULL; |
| 11562 | } else { |
| 11563 | ctxt->directory = xmlParserGetDirectory(filename); |
| 11564 | } |
| 11565 | |
| 11566 | inputStream = xmlNewInputStream(ctxt); |
| 11567 | if (inputStream == NULL) { |
| 11568 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 11569 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11570 | return(NULL); |
| 11571 | } |
| 11572 | |
| 11573 | if (filename == NULL) |
| 11574 | inputStream->filename = NULL; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 11575 | else { |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 11576 | inputStream->filename = (char *) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 11577 | xmlCanonicPath((const xmlChar *) filename); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 11578 | if (inputStream->filename == NULL) { |
| 11579 | xmlFreeParserCtxt(ctxt); |
| 11580 | xmlFreeParserInputBuffer(buf); |
| 11581 | return(NULL); |
| 11582 | } |
| 11583 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11584 | inputStream->buf = buf; |
| 11585 | inputStream->base = inputStream->buf->buffer->content; |
| 11586 | inputStream->cur = inputStream->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 11587 | inputStream->end = |
| 11588 | &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11589 | |
| 11590 | inputPush(ctxt, inputStream); |
| 11591 | |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 11592 | /* |
| 11593 | * If the caller didn't provide an initial 'chunk' for determining |
| 11594 | * the encoding, we set the context to XML_CHAR_ENCODING_NONE so |
| 11595 | * that it can be automatically determined later |
| 11596 | */ |
| 11597 | if ((size == 0) || (chunk == NULL)) { |
| 11598 | ctxt->charset = XML_CHAR_ENCODING_NONE; |
| 11599 | } else if ((ctxt->input != NULL) && (ctxt->input->buf != NULL)) { |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 11600 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 11601 | int cur = ctxt->input->cur - ctxt->input->base; |
| 11602 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11603 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 11604 | |
| 11605 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 11606 | ctxt->input->cur = ctxt->input->base + cur; |
| 11607 | ctxt->input->end = |
| 11608 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11609 | #ifdef DEBUG_PUSH |
| 11610 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 11611 | #endif |
| 11612 | } |
| 11613 | |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 11614 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 11615 | xmlSwitchEncoding(ctxt, enc); |
| 11616 | } |
| 11617 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11618 | return(ctxt); |
| 11619 | } |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 11620 | #endif /* LIBXML_PUSH_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11621 | |
| 11622 | /** |
Daniel Veillard | 39e5c89 | 2005-07-03 22:48:50 +0000 | [diff] [blame] | 11623 | * xmlStopParser: |
| 11624 | * @ctxt: an XML parser context |
| 11625 | * |
| 11626 | * Blocks further parser processing |
| 11627 | */ |
| 11628 | void |
| 11629 | xmlStopParser(xmlParserCtxtPtr ctxt) { |
| 11630 | if (ctxt == NULL) |
| 11631 | return; |
| 11632 | ctxt->instate = XML_PARSER_EOF; |
| 11633 | ctxt->disableSAX = 1; |
| 11634 | if (ctxt->input != NULL) { |
| 11635 | ctxt->input->cur = BAD_CAST""; |
| 11636 | ctxt->input->base = ctxt->input->cur; |
| 11637 | } |
| 11638 | } |
| 11639 | |
| 11640 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11641 | * xmlCreateIOParserCtxt: |
| 11642 | * @sax: a SAX handler |
| 11643 | * @user_data: The user data returned on SAX callbacks |
| 11644 | * @ioread: an I/O read function |
| 11645 | * @ioclose: an I/O close function |
| 11646 | * @ioctx: an I/O handler |
| 11647 | * @enc: the charset encoding if known |
| 11648 | * |
| 11649 | * Create a parser context for using the XML parser with an existing |
| 11650 | * I/O stream |
| 11651 | * |
| 11652 | * Returns the new parser context or NULL |
| 11653 | */ |
| 11654 | xmlParserCtxtPtr |
| 11655 | xmlCreateIOParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
| 11656 | xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
| 11657 | void *ioctx, xmlCharEncoding enc) { |
| 11658 | xmlParserCtxtPtr ctxt; |
| 11659 | xmlParserInputPtr inputStream; |
| 11660 | xmlParserInputBufferPtr buf; |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 11661 | |
| 11662 | if (ioread == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11663 | |
| 11664 | buf = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, enc); |
| 11665 | if (buf == NULL) return(NULL); |
| 11666 | |
| 11667 | ctxt = xmlNewParserCtxt(); |
| 11668 | if (ctxt == NULL) { |
Daniel Veillard | f0af8ec | 2005-07-08 17:27:33 +0000 | [diff] [blame] | 11669 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11670 | return(NULL); |
| 11671 | } |
| 11672 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11673 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 11674 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11675 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11676 | xmlFree(ctxt->sax); |
| 11677 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 11678 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11679 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | f0af8ec | 2005-07-08 17:27:33 +0000 | [diff] [blame] | 11680 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11681 | return(NULL); |
| 11682 | } |
Daniel Veillard | 5ea30d7 | 2004-11-08 11:54:28 +0000 | [diff] [blame] | 11683 | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
| 11684 | if (sax->initialized == XML_SAX2_MAGIC) |
| 11685 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 11686 | else |
| 11687 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11688 | if (user_data != NULL) |
| 11689 | ctxt->userData = user_data; |
| 11690 | } |
| 11691 | |
| 11692 | inputStream = xmlNewIOInputStream(ctxt, buf, enc); |
| 11693 | if (inputStream == NULL) { |
| 11694 | xmlFreeParserCtxt(ctxt); |
| 11695 | return(NULL); |
| 11696 | } |
| 11697 | inputPush(ctxt, inputStream); |
| 11698 | |
| 11699 | return(ctxt); |
| 11700 | } |
| 11701 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 11702 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11703 | /************************************************************************ |
| 11704 | * * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11705 | * Front ends when parsing a DTD * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11706 | * * |
| 11707 | ************************************************************************/ |
| 11708 | |
| 11709 | /** |
| 11710 | * xmlIOParseDTD: |
| 11711 | * @sax: the SAX handler block or NULL |
| 11712 | * @input: an Input Buffer |
| 11713 | * @enc: the charset encoding if known |
| 11714 | * |
| 11715 | * Load and parse a DTD |
| 11716 | * |
| 11717 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 11718 | * @input will be freed by the function in any case. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11719 | */ |
| 11720 | |
| 11721 | xmlDtdPtr |
| 11722 | xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input, |
| 11723 | xmlCharEncoding enc) { |
| 11724 | xmlDtdPtr ret = NULL; |
| 11725 | xmlParserCtxtPtr ctxt; |
| 11726 | xmlParserInputPtr pinput = NULL; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 11727 | xmlChar start[4]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11728 | |
| 11729 | if (input == NULL) |
| 11730 | return(NULL); |
| 11731 | |
| 11732 | ctxt = xmlNewParserCtxt(); |
| 11733 | if (ctxt == NULL) { |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 11734 | xmlFreeParserInputBuffer(input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11735 | return(NULL); |
| 11736 | } |
| 11737 | |
| 11738 | /* |
| 11739 | * Set-up the SAX context |
| 11740 | */ |
| 11741 | if (sax != NULL) { |
| 11742 | if (ctxt->sax != NULL) |
| 11743 | xmlFree(ctxt->sax); |
| 11744 | ctxt->sax = sax; |
Daniel Veillard | 500a1de | 2004-03-22 15:22:58 +0000 | [diff] [blame] | 11745 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11746 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11747 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11748 | |
| 11749 | /* |
| 11750 | * generate a parser input from the I/O handler |
| 11751 | */ |
| 11752 | |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 11753 | pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11754 | if (pinput == NULL) { |
| 11755 | if (sax != NULL) ctxt->sax = NULL; |
Daniel Veillard | 402b344 | 2006-10-13 10:28:21 +0000 | [diff] [blame] | 11756 | xmlFreeParserInputBuffer(input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11757 | xmlFreeParserCtxt(ctxt); |
| 11758 | return(NULL); |
| 11759 | } |
| 11760 | |
| 11761 | /* |
| 11762 | * plug some encoding conversion routines here. |
| 11763 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 11764 | if (xmlPushInput(ctxt, pinput) < 0) { |
| 11765 | if (sax != NULL) ctxt->sax = NULL; |
| 11766 | xmlFreeParserCtxt(ctxt); |
| 11767 | return(NULL); |
| 11768 | } |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 11769 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 11770 | xmlSwitchEncoding(ctxt, enc); |
| 11771 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11772 | |
| 11773 | pinput->filename = NULL; |
| 11774 | pinput->line = 1; |
| 11775 | pinput->col = 1; |
| 11776 | pinput->base = ctxt->input->cur; |
| 11777 | pinput->cur = ctxt->input->cur; |
| 11778 | pinput->free = NULL; |
| 11779 | |
| 11780 | /* |
| 11781 | * let's parse that entity knowing it's an external subset. |
| 11782 | */ |
| 11783 | ctxt->inSubset = 2; |
| 11784 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 11785 | if (ctxt->myDoc == NULL) { |
| 11786 | xmlErrMemory(ctxt, "New Doc failed"); |
| 11787 | return(NULL); |
| 11788 | } |
| 11789 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11790 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 11791 | BAD_CAST "none", BAD_CAST "none"); |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 11792 | |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 11793 | if ((enc == XML_CHAR_ENCODING_NONE) && |
| 11794 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 11795 | /* |
| 11796 | * Get the 4 first bytes and decode the charset |
| 11797 | * if enc != XML_CHAR_ENCODING_NONE |
| 11798 | * plug some encoding conversion routines. |
| 11799 | */ |
| 11800 | start[0] = RAW; |
| 11801 | start[1] = NXT(1); |
| 11802 | start[2] = NXT(2); |
| 11803 | start[3] = NXT(3); |
| 11804 | enc = xmlDetectCharEncoding(start, 4); |
| 11805 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 11806 | xmlSwitchEncoding(ctxt, enc); |
| 11807 | } |
| 11808 | } |
| 11809 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11810 | xmlParseExternalSubset(ctxt, BAD_CAST "none", BAD_CAST "none"); |
| 11811 | |
| 11812 | if (ctxt->myDoc != NULL) { |
| 11813 | if (ctxt->wellFormed) { |
| 11814 | ret = ctxt->myDoc->extSubset; |
| 11815 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | 329456a | 2003-04-26 21:21:00 +0000 | [diff] [blame] | 11816 | if (ret != NULL) { |
| 11817 | xmlNodePtr tmp; |
| 11818 | |
| 11819 | ret->doc = NULL; |
| 11820 | tmp = ret->children; |
| 11821 | while (tmp != NULL) { |
| 11822 | tmp->doc = NULL; |
| 11823 | tmp = tmp->next; |
| 11824 | } |
| 11825 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11826 | } else { |
| 11827 | ret = NULL; |
| 11828 | } |
| 11829 | xmlFreeDoc(ctxt->myDoc); |
| 11830 | ctxt->myDoc = NULL; |
| 11831 | } |
| 11832 | if (sax != NULL) ctxt->sax = NULL; |
| 11833 | xmlFreeParserCtxt(ctxt); |
| 11834 | |
| 11835 | return(ret); |
| 11836 | } |
| 11837 | |
| 11838 | /** |
| 11839 | * xmlSAXParseDTD: |
| 11840 | * @sax: the SAX handler block |
| 11841 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 11842 | * @SystemID: a NAME* containing the URL to the DTD |
| 11843 | * |
| 11844 | * Load and parse an external subset. |
| 11845 | * |
| 11846 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 11847 | */ |
| 11848 | |
| 11849 | xmlDtdPtr |
| 11850 | xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID, |
| 11851 | const xmlChar *SystemID) { |
| 11852 | xmlDtdPtr ret = NULL; |
| 11853 | xmlParserCtxtPtr ctxt; |
| 11854 | xmlParserInputPtr input = NULL; |
| 11855 | xmlCharEncoding enc; |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 11856 | xmlChar* systemIdCanonic; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11857 | |
| 11858 | if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL); |
| 11859 | |
| 11860 | ctxt = xmlNewParserCtxt(); |
| 11861 | if (ctxt == NULL) { |
| 11862 | return(NULL); |
| 11863 | } |
| 11864 | |
| 11865 | /* |
| 11866 | * Set-up the SAX context |
| 11867 | */ |
| 11868 | if (sax != NULL) { |
| 11869 | if (ctxt->sax != NULL) |
| 11870 | xmlFree(ctxt->sax); |
| 11871 | ctxt->sax = sax; |
Daniel Veillard | bf1e3d8 | 2003-08-14 23:57:26 +0000 | [diff] [blame] | 11872 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11873 | } |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 11874 | |
| 11875 | /* |
| 11876 | * Canonicalise the system ID |
| 11877 | */ |
| 11878 | systemIdCanonic = xmlCanonicPath(SystemID); |
Daniel Veillard | c93a19f | 2004-10-04 11:53:20 +0000 | [diff] [blame] | 11879 | if ((SystemID != NULL) && (systemIdCanonic == NULL)) { |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 11880 | xmlFreeParserCtxt(ctxt); |
| 11881 | return(NULL); |
| 11882 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11883 | |
| 11884 | /* |
| 11885 | * Ask the Entity resolver to load the damn thing |
| 11886 | */ |
| 11887 | |
| 11888 | if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL)) |
Daniel Veillard | a955795 | 2006-10-12 12:53:15 +0000 | [diff] [blame] | 11889 | input = ctxt->sax->resolveEntity(ctxt->userData, ExternalID, |
| 11890 | systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11891 | if (input == NULL) { |
| 11892 | if (sax != NULL) ctxt->sax = NULL; |
| 11893 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 11894 | if (systemIdCanonic != NULL) |
| 11895 | xmlFree(systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11896 | return(NULL); |
| 11897 | } |
| 11898 | |
| 11899 | /* |
| 11900 | * plug some encoding conversion routines here. |
| 11901 | */ |
Daniel Veillard | a8f09ce | 2008-08-27 13:02:01 +0000 | [diff] [blame] | 11902 | if (xmlPushInput(ctxt, input) < 0) { |
| 11903 | if (sax != NULL) ctxt->sax = NULL; |
| 11904 | xmlFreeParserCtxt(ctxt); |
| 11905 | if (systemIdCanonic != NULL) |
| 11906 | xmlFree(systemIdCanonic); |
| 11907 | return(NULL); |
| 11908 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 11909 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 11910 | enc = xmlDetectCharEncoding(ctxt->input->cur, 4); |
| 11911 | xmlSwitchEncoding(ctxt, enc); |
| 11912 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11913 | |
| 11914 | if (input->filename == NULL) |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 11915 | input->filename = (char *) systemIdCanonic; |
| 11916 | else |
| 11917 | xmlFree(systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11918 | input->line = 1; |
| 11919 | input->col = 1; |
| 11920 | input->base = ctxt->input->cur; |
| 11921 | input->cur = ctxt->input->cur; |
| 11922 | input->free = NULL; |
| 11923 | |
| 11924 | /* |
| 11925 | * let's parse that entity knowing it's an external subset. |
| 11926 | */ |
| 11927 | ctxt->inSubset = 2; |
| 11928 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 11929 | if (ctxt->myDoc == NULL) { |
| 11930 | xmlErrMemory(ctxt, "New Doc failed"); |
| 11931 | if (sax != NULL) ctxt->sax = NULL; |
| 11932 | xmlFreeParserCtxt(ctxt); |
| 11933 | return(NULL); |
| 11934 | } |
| 11935 | ctxt->myDoc->properties = XML_DOC_INTERNAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11936 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 11937 | ExternalID, SystemID); |
| 11938 | xmlParseExternalSubset(ctxt, ExternalID, SystemID); |
| 11939 | |
| 11940 | if (ctxt->myDoc != NULL) { |
| 11941 | if (ctxt->wellFormed) { |
| 11942 | ret = ctxt->myDoc->extSubset; |
| 11943 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | c557346 | 2003-04-25 16:43:49 +0000 | [diff] [blame] | 11944 | if (ret != NULL) { |
| 11945 | xmlNodePtr tmp; |
| 11946 | |
| 11947 | ret->doc = NULL; |
| 11948 | tmp = ret->children; |
| 11949 | while (tmp != NULL) { |
| 11950 | tmp->doc = NULL; |
| 11951 | tmp = tmp->next; |
| 11952 | } |
| 11953 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11954 | } else { |
| 11955 | ret = NULL; |
| 11956 | } |
| 11957 | xmlFreeDoc(ctxt->myDoc); |
| 11958 | ctxt->myDoc = NULL; |
| 11959 | } |
| 11960 | if (sax != NULL) ctxt->sax = NULL; |
| 11961 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 11962 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11963 | return(ret); |
| 11964 | } |
| 11965 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 11966 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11967 | /** |
| 11968 | * xmlParseDTD: |
| 11969 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 11970 | * @SystemID: a NAME* containing the URL to the DTD |
| 11971 | * |
| 11972 | * Load and parse an external subset. |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 11973 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11974 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 11975 | */ |
| 11976 | |
| 11977 | xmlDtdPtr |
| 11978 | xmlParseDTD(const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 11979 | return(xmlSAXParseDTD(NULL, ExternalID, SystemID)); |
| 11980 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 11981 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11982 | |
| 11983 | /************************************************************************ |
| 11984 | * * |
| 11985 | * Front ends when parsing an Entity * |
| 11986 | * * |
| 11987 | ************************************************************************/ |
| 11988 | |
| 11989 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11990 | * xmlParseCtxtExternalEntity: |
| 11991 | * @ctx: the existing parsing context |
| 11992 | * @URL: the URL for the entity to load |
| 11993 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 11994 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11995 | * |
| 11996 | * Parse an external general entity within an existing parsing context |
| 11997 | * An external general parsed entity is well-formed if it matches the |
| 11998 | * production labeled extParsedEnt. |
| 11999 | * |
| 12000 | * [78] extParsedEnt ::= TextDecl? content |
| 12001 | * |
| 12002 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 12003 | * the parser error code otherwise |
| 12004 | */ |
| 12005 | |
| 12006 | int |
| 12007 | xmlParseCtxtExternalEntity(xmlParserCtxtPtr ctx, const xmlChar *URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12008 | const xmlChar *ID, xmlNodePtr *lst) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12009 | xmlParserCtxtPtr ctxt; |
| 12010 | xmlDocPtr newDoc; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12011 | xmlNodePtr newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12012 | xmlSAXHandlerPtr oldsax = NULL; |
| 12013 | int ret = 0; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12014 | xmlChar start[4]; |
| 12015 | xmlCharEncoding enc; |
Daniel Veillard | 2937b3a | 2006-10-10 08:52:34 +0000 | [diff] [blame] | 12016 | xmlParserInputPtr inputStream; |
| 12017 | char *directory = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12018 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 12019 | if (ctx == NULL) return(-1); |
| 12020 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12021 | if (((ctx->depth > 40) && ((ctx->options & XML_PARSE_HUGE) == 0)) || |
| 12022 | (ctx->depth > 1024)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12023 | return(XML_ERR_ENTITY_LOOP); |
| 12024 | } |
| 12025 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12026 | if (lst != NULL) |
| 12027 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12028 | if ((URL == NULL) && (ID == NULL)) |
| 12029 | return(-1); |
| 12030 | if (ctx->myDoc == NULL) /* @@ relax but check for dereferences */ |
| 12031 | return(-1); |
| 12032 | |
Daniel Veillard | 2937b3a | 2006-10-10 08:52:34 +0000 | [diff] [blame] | 12033 | ctxt = xmlNewParserCtxt(); |
| 12034 | if (ctxt == NULL) { |
| 12035 | return(-1); |
| 12036 | } |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12037 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12038 | ctxt->userData = ctxt; |
Daniel Veillard | e2830f1 | 2003-01-08 17:47:49 +0000 | [diff] [blame] | 12039 | ctxt->_private = ctx->_private; |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12040 | |
Daniel Veillard | 2937b3a | 2006-10-10 08:52:34 +0000 | [diff] [blame] | 12041 | inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt); |
| 12042 | if (inputStream == NULL) { |
| 12043 | xmlFreeParserCtxt(ctxt); |
| 12044 | return(-1); |
| 12045 | } |
| 12046 | |
| 12047 | inputPush(ctxt, inputStream); |
| 12048 | |
| 12049 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 12050 | directory = xmlParserGetDirectory((char *)URL); |
| 12051 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 12052 | ctxt->directory = directory; |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12053 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12054 | oldsax = ctxt->sax; |
| 12055 | ctxt->sax = ctx->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12056 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12057 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 12058 | if (newDoc == NULL) { |
| 12059 | xmlFreeParserCtxt(ctxt); |
| 12060 | return(-1); |
| 12061 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12062 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12063 | if (ctx->myDoc->dict) { |
| 12064 | newDoc->dict = ctx->myDoc->dict; |
| 12065 | xmlDictReference(newDoc->dict); |
| 12066 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12067 | if (ctx->myDoc != NULL) { |
| 12068 | newDoc->intSubset = ctx->myDoc->intSubset; |
| 12069 | newDoc->extSubset = ctx->myDoc->extSubset; |
| 12070 | } |
| 12071 | if (ctx->myDoc->URL != NULL) { |
| 12072 | newDoc->URL = xmlStrdup(ctx->myDoc->URL); |
| 12073 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12074 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 12075 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12076 | ctxt->sax = oldsax; |
| 12077 | xmlFreeParserCtxt(ctxt); |
| 12078 | newDoc->intSubset = NULL; |
| 12079 | newDoc->extSubset = NULL; |
| 12080 | xmlFreeDoc(newDoc); |
| 12081 | return(-1); |
| 12082 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12083 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12084 | nodePush(ctxt, newDoc->children); |
| 12085 | if (ctx->myDoc == NULL) { |
| 12086 | ctxt->myDoc = newDoc; |
| 12087 | } else { |
| 12088 | ctxt->myDoc = ctx->myDoc; |
| 12089 | newDoc->children->doc = ctx->myDoc; |
| 12090 | } |
| 12091 | |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12092 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12093 | * Get the 4 first bytes and decode the charset |
| 12094 | * if enc != XML_CHAR_ENCODING_NONE |
| 12095 | * plug some encoding conversion routines. |
| 12096 | */ |
| 12097 | GROW |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12098 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 12099 | start[0] = RAW; |
| 12100 | start[1] = NXT(1); |
| 12101 | start[2] = NXT(2); |
| 12102 | start[3] = NXT(3); |
| 12103 | enc = xmlDetectCharEncoding(start, 4); |
| 12104 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12105 | xmlSwitchEncoding(ctxt, enc); |
| 12106 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12107 | } |
| 12108 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12109 | /* |
| 12110 | * Parse a possible text declaration first |
| 12111 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 12112 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12113 | xmlParseTextDecl(ctxt); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12114 | /* |
| 12115 | * An XML-1.0 document can't reference an entity not XML-1.0 |
| 12116 | */ |
| 12117 | if ((xmlStrEqual(ctx->version, BAD_CAST "1.0")) && |
| 12118 | (!xmlStrEqual(ctxt->input->version, BAD_CAST "1.0"))) { |
| 12119 | xmlFatalErrMsg(ctxt, XML_ERR_VERSION_MISMATCH, |
| 12120 | "Version mismatch between document and entity\n"); |
| 12121 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12122 | } |
| 12123 | |
| 12124 | /* |
| 12125 | * Doing validity checking on chunk doesn't make sense |
| 12126 | */ |
| 12127 | ctxt->instate = XML_PARSER_CONTENT; |
| 12128 | ctxt->validate = ctx->validate; |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 12129 | ctxt->valid = ctx->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12130 | ctxt->loadsubset = ctx->loadsubset; |
| 12131 | ctxt->depth = ctx->depth + 1; |
| 12132 | ctxt->replaceEntities = ctx->replaceEntities; |
| 12133 | if (ctxt->validate) { |
| 12134 | ctxt->vctxt.error = ctx->vctxt.error; |
| 12135 | ctxt->vctxt.warning = ctx->vctxt.warning; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12136 | } else { |
| 12137 | ctxt->vctxt.error = NULL; |
| 12138 | ctxt->vctxt.warning = NULL; |
| 12139 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 12140 | ctxt->vctxt.nodeTab = NULL; |
| 12141 | ctxt->vctxt.nodeNr = 0; |
| 12142 | ctxt->vctxt.nodeMax = 0; |
| 12143 | ctxt->vctxt.node = NULL; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 12144 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 12145 | ctxt->dict = ctx->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 12146 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 12147 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 12148 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 12149 | ctxt->dictNames = ctx->dictNames; |
| 12150 | ctxt->attsDefault = ctx->attsDefault; |
| 12151 | ctxt->attsSpecial = ctx->attsSpecial; |
William M. Brack | 503b610 | 2004-08-19 02:17:27 +0000 | [diff] [blame] | 12152 | ctxt->linenumbers = ctx->linenumbers; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12153 | |
| 12154 | xmlParseContent(ctxt); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12155 | |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 12156 | ctx->validate = ctxt->validate; |
| 12157 | ctx->valid = ctxt->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12158 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12159 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12160 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12161 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12162 | } |
| 12163 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12164 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12165 | } |
| 12166 | |
| 12167 | if (!ctxt->wellFormed) { |
| 12168 | if (ctxt->errNo == 0) |
| 12169 | ret = 1; |
| 12170 | else |
| 12171 | ret = ctxt->errNo; |
| 12172 | } else { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12173 | if (lst != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12174 | xmlNodePtr cur; |
| 12175 | |
| 12176 | /* |
| 12177 | * Return the newly created nodeset after unlinking it from |
| 12178 | * they pseudo parent. |
| 12179 | */ |
| 12180 | cur = newDoc->children->children; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12181 | *lst = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12182 | while (cur != NULL) { |
| 12183 | cur->parent = NULL; |
| 12184 | cur = cur->next; |
| 12185 | } |
| 12186 | newDoc->children->children = NULL; |
| 12187 | } |
| 12188 | ret = 0; |
| 12189 | } |
| 12190 | ctxt->sax = oldsax; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 12191 | ctxt->dict = NULL; |
| 12192 | ctxt->attsDefault = NULL; |
| 12193 | ctxt->attsSpecial = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12194 | xmlFreeParserCtxt(ctxt); |
| 12195 | newDoc->intSubset = NULL; |
| 12196 | newDoc->extSubset = NULL; |
| 12197 | xmlFreeDoc(newDoc); |
Daniel Veillard | 40ec29a | 2008-07-30 12:35:40 +0000 | [diff] [blame] | 12198 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12199 | return(ret); |
| 12200 | } |
| 12201 | |
| 12202 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12203 | * xmlParseExternalEntityPrivate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12204 | * @doc: the document the chunk pertains to |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12205 | * @oldctxt: the previous parser context if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12206 | * @sax: the SAX handler bloc (possibly NULL) |
| 12207 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 12208 | * @depth: Used for loop detection, use 0 |
| 12209 | * @URL: the URL for the entity to load |
| 12210 | * @ID: the System ID for the entity to load |
| 12211 | * @list: the return value for the set of parsed nodes |
| 12212 | * |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12213 | * Private version of xmlParseExternalEntity() |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12214 | * |
| 12215 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 12216 | * the parser error code otherwise |
| 12217 | */ |
| 12218 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12219 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12220 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 12221 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12222 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12223 | const xmlChar *ID, xmlNodePtr *list) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12224 | xmlParserCtxtPtr ctxt; |
| 12225 | xmlDocPtr newDoc; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12226 | xmlNodePtr newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12227 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12228 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12229 | xmlChar start[4]; |
| 12230 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12231 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12232 | if (((depth > 40) && |
| 12233 | ((oldctxt == NULL) || (oldctxt->options & XML_PARSE_HUGE) == 0)) || |
| 12234 | (depth > 1024)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12235 | return(XML_ERR_ENTITY_LOOP); |
| 12236 | } |
| 12237 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12238 | if (list != NULL) |
| 12239 | *list = NULL; |
| 12240 | if ((URL == NULL) && (ID == NULL)) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12241 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 12242 | if (doc == NULL) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12243 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12244 | |
| 12245 | |
| 12246 | ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL); |
William M. Brack | b670e2e | 2003-09-27 01:05:55 +0000 | [diff] [blame] | 12247 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12248 | ctxt->userData = ctxt; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12249 | if (oldctxt != NULL) { |
| 12250 | ctxt->_private = oldctxt->_private; |
| 12251 | ctxt->loadsubset = oldctxt->loadsubset; |
| 12252 | ctxt->validate = oldctxt->validate; |
| 12253 | ctxt->external = oldctxt->external; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 12254 | ctxt->record_info = oldctxt->record_info; |
| 12255 | ctxt->node_seq.maximum = oldctxt->node_seq.maximum; |
| 12256 | ctxt->node_seq.length = oldctxt->node_seq.length; |
| 12257 | ctxt->node_seq.buffer = oldctxt->node_seq.buffer; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12258 | } else { |
| 12259 | /* |
| 12260 | * Doing validity checking on chunk without context |
| 12261 | * doesn't make sense |
| 12262 | */ |
| 12263 | ctxt->_private = NULL; |
| 12264 | ctxt->validate = 0; |
| 12265 | ctxt->external = 2; |
| 12266 | ctxt->loadsubset = 0; |
| 12267 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12268 | if (sax != NULL) { |
| 12269 | oldsax = ctxt->sax; |
| 12270 | ctxt->sax = sax; |
| 12271 | if (user_data != NULL) |
| 12272 | ctxt->userData = user_data; |
| 12273 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12274 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12275 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 12276 | if (newDoc == NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 12277 | ctxt->node_seq.maximum = 0; |
| 12278 | ctxt->node_seq.length = 0; |
| 12279 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12280 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12281 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12282 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12283 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 12284 | newDoc->intSubset = doc->intSubset; |
| 12285 | newDoc->extSubset = doc->extSubset; |
| 12286 | newDoc->dict = doc->dict; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12287 | xmlDictReference(newDoc->dict); |
| 12288 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12289 | if (doc->URL != NULL) { |
| 12290 | newDoc->URL = xmlStrdup(doc->URL); |
| 12291 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12292 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 12293 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12294 | if (sax != NULL) |
| 12295 | ctxt->sax = oldsax; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 12296 | ctxt->node_seq.maximum = 0; |
| 12297 | ctxt->node_seq.length = 0; |
| 12298 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12299 | xmlFreeParserCtxt(ctxt); |
| 12300 | newDoc->intSubset = NULL; |
| 12301 | newDoc->extSubset = NULL; |
| 12302 | xmlFreeDoc(newDoc); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12303 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12304 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12305 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12306 | nodePush(ctxt, newDoc->children); |
Daniel Veillard | 30e7607 | 2006-03-09 14:13:55 +0000 | [diff] [blame] | 12307 | ctxt->myDoc = doc; |
| 12308 | newRoot->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12309 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12310 | /* |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12311 | * Get the 4 first bytes and decode the charset |
| 12312 | * if enc != XML_CHAR_ENCODING_NONE |
| 12313 | * plug some encoding conversion routines. |
| 12314 | */ |
| 12315 | GROW; |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 12316 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 12317 | start[0] = RAW; |
| 12318 | start[1] = NXT(1); |
| 12319 | start[2] = NXT(2); |
| 12320 | start[3] = NXT(3); |
| 12321 | enc = xmlDetectCharEncoding(start, 4); |
| 12322 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 12323 | xmlSwitchEncoding(ctxt, enc); |
| 12324 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 12325 | } |
| 12326 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12327 | /* |
| 12328 | * Parse a possible text declaration first |
| 12329 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 12330 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12331 | xmlParseTextDecl(ctxt); |
| 12332 | } |
| 12333 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12334 | ctxt->instate = XML_PARSER_CONTENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12335 | ctxt->depth = depth; |
| 12336 | |
| 12337 | xmlParseContent(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12338 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 12339 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12340 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 12341 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12342 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12343 | } |
| 12344 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12345 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12346 | } |
| 12347 | |
| 12348 | if (!ctxt->wellFormed) { |
| 12349 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12350 | ret = XML_ERR_INTERNAL_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12351 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12352 | ret = (xmlParserErrors)ctxt->errNo; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12353 | } else { |
| 12354 | if (list != NULL) { |
| 12355 | xmlNodePtr cur; |
| 12356 | |
| 12357 | /* |
| 12358 | * Return the newly created nodeset after unlinking it from |
| 12359 | * they pseudo parent. |
| 12360 | */ |
| 12361 | cur = newDoc->children->children; |
| 12362 | *list = cur; |
| 12363 | while (cur != NULL) { |
| 12364 | cur->parent = NULL; |
| 12365 | cur = cur->next; |
| 12366 | } |
| 12367 | newDoc->children->children = NULL; |
| 12368 | } |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12369 | ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12370 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12371 | |
| 12372 | /* |
| 12373 | * Record in the parent context the number of entities replacement |
| 12374 | * done when parsing that reference. |
| 12375 | */ |
| 12376 | oldctxt->nbentities += ctxt->nbentities; |
| 12377 | /* |
| 12378 | * Also record the size of the entity parsed |
| 12379 | */ |
| 12380 | if (ctxt->input != NULL) { |
| 12381 | oldctxt->sizeentities += ctxt->input->consumed; |
| 12382 | oldctxt->sizeentities += (ctxt->input->cur - ctxt->input->base); |
| 12383 | } |
| 12384 | /* |
| 12385 | * And record the last error if any |
| 12386 | */ |
| 12387 | if (ctxt->lastError.code != XML_ERR_OK) |
| 12388 | xmlCopyError(&ctxt->lastError, &oldctxt->lastError); |
| 12389 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12390 | if (sax != NULL) |
| 12391 | ctxt->sax = oldsax; |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 12392 | oldctxt->node_seq.maximum = ctxt->node_seq.maximum; |
| 12393 | oldctxt->node_seq.length = ctxt->node_seq.length; |
| 12394 | oldctxt->node_seq.buffer = ctxt->node_seq.buffer; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 12395 | ctxt->node_seq.maximum = 0; |
| 12396 | ctxt->node_seq.length = 0; |
| 12397 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12398 | xmlFreeParserCtxt(ctxt); |
| 12399 | newDoc->intSubset = NULL; |
| 12400 | newDoc->extSubset = NULL; |
| 12401 | xmlFreeDoc(newDoc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12402 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12403 | return(ret); |
| 12404 | } |
| 12405 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12406 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12407 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12408 | * xmlParseExternalEntity: |
| 12409 | * @doc: the document the chunk pertains to |
| 12410 | * @sax: the SAX handler bloc (possibly NULL) |
| 12411 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 12412 | * @depth: Used for loop detection, use 0 |
| 12413 | * @URL: the URL for the entity to load |
| 12414 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12415 | * @lst: the return value for the set of parsed nodes |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12416 | * |
| 12417 | * Parse an external general entity |
| 12418 | * An external general parsed entity is well-formed if it matches the |
| 12419 | * production labeled extParsedEnt. |
| 12420 | * |
| 12421 | * [78] extParsedEnt ::= TextDecl? content |
| 12422 | * |
| 12423 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 12424 | * the parser error code otherwise |
| 12425 | */ |
| 12426 | |
| 12427 | int |
| 12428 | xmlParseExternalEntity(xmlDocPtr doc, xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12429 | int depth, const xmlChar *URL, const xmlChar *ID, xmlNodePtr *lst) { |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 12430 | return(xmlParseExternalEntityPrivate(doc, NULL, sax, user_data, depth, URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12431 | ID, lst)); |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 12432 | } |
| 12433 | |
| 12434 | /** |
Daniel Veillard | e020c3a | 2001-03-21 18:06:15 +0000 | [diff] [blame] | 12435 | * xmlParseBalancedChunkMemory: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12436 | * @doc: the document the chunk pertains to |
| 12437 | * @sax: the SAX handler bloc (possibly NULL) |
| 12438 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 12439 | * @depth: Used for loop detection, use 0 |
| 12440 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12441 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12442 | * |
| 12443 | * Parse a well-balanced chunk of an XML document |
| 12444 | * called by the parser |
| 12445 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 12446 | * the content production in the XML grammar: |
| 12447 | * |
| 12448 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 12449 | * |
| 12450 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 12451 | * the parser error code otherwise |
| 12452 | */ |
| 12453 | |
| 12454 | int |
| 12455 | xmlParseBalancedChunkMemory(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12456 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst) { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 12457 | return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data, |
| 12458 | depth, string, lst, 0 ); |
| 12459 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12460 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 12461 | |
| 12462 | /** |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12463 | * xmlParseBalancedChunkMemoryInternal: |
| 12464 | * @oldctxt: the existing parsing context |
| 12465 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 12466 | * @user_data: the user data field for the parser context |
| 12467 | * @lst: the return value for the set of parsed nodes |
| 12468 | * |
| 12469 | * |
| 12470 | * Parse a well-balanced chunk of an XML document |
| 12471 | * called by the parser |
| 12472 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 12473 | * the content production in the XML grammar: |
| 12474 | * |
| 12475 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 12476 | * |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12477 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 12478 | * error code otherwise |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12479 | * |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12480 | * 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] | 12481 | * the parsed chunk is not well balanced. |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12482 | */ |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12483 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12484 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 12485 | const xmlChar *string, void *user_data, xmlNodePtr *lst) { |
| 12486 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12487 | xmlDocPtr newDoc = NULL; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12488 | xmlNodePtr newRoot; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12489 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12490 | xmlNodePtr content = NULL; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12491 | xmlNodePtr last = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12492 | int size; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12493 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12494 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12495 | if (((oldctxt->depth > 40) && ((oldctxt->options & XML_PARSE_HUGE) == 0)) || |
| 12496 | (oldctxt->depth > 1024)) { |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12497 | return(XML_ERR_ENTITY_LOOP); |
| 12498 | } |
| 12499 | |
| 12500 | |
| 12501 | if (lst != NULL) |
| 12502 | *lst = NULL; |
| 12503 | if (string == NULL) |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12504 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12505 | |
| 12506 | size = xmlStrlen(string); |
| 12507 | |
| 12508 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12509 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12510 | if (user_data != NULL) |
| 12511 | ctxt->userData = user_data; |
| 12512 | else |
| 12513 | ctxt->userData = ctxt; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 12514 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 12515 | ctxt->dict = oldctxt->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 12516 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 12517 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 12518 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12519 | |
| 12520 | oldsax = ctxt->sax; |
| 12521 | ctxt->sax = oldctxt->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12522 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12523 | ctxt->replaceEntities = oldctxt->replaceEntities; |
| 12524 | ctxt->options = oldctxt->options; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12525 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 12526 | ctxt->_private = oldctxt->_private; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12527 | if (oldctxt->myDoc == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12528 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 12529 | if (newDoc == NULL) { |
| 12530 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 12531 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12532 | xmlFreeParserCtxt(ctxt); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12533 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12534 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12535 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12536 | newDoc->dict = ctxt->dict; |
| 12537 | xmlDictReference(newDoc->dict); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12538 | ctxt->myDoc = newDoc; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12539 | } else { |
| 12540 | ctxt->myDoc = oldctxt->myDoc; |
| 12541 | content = ctxt->myDoc->children; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12542 | last = ctxt->myDoc->last; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12543 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12544 | newRoot = xmlNewDocNode(ctxt->myDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 12545 | if (newRoot == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12546 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 12547 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12548 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12549 | if (newDoc != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12550 | xmlFreeDoc(newDoc); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12551 | } |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12552 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12553 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12554 | ctxt->myDoc->children = NULL; |
| 12555 | ctxt->myDoc->last = NULL; |
| 12556 | xmlAddChild((xmlNodePtr) ctxt->myDoc, newRoot); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12557 | nodePush(ctxt, ctxt->myDoc->children); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12558 | ctxt->instate = XML_PARSER_CONTENT; |
| 12559 | ctxt->depth = oldctxt->depth + 1; |
| 12560 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12561 | ctxt->validate = 0; |
| 12562 | ctxt->loadsubset = oldctxt->loadsubset; |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 12563 | if ((oldctxt->validate) || (oldctxt->replaceEntities != 0)) { |
| 12564 | /* |
| 12565 | * ID/IDREF registration will be done in xmlValidateElement below |
| 12566 | */ |
| 12567 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 12568 | } |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 12569 | ctxt->dictNames = oldctxt->dictNames; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 12570 | ctxt->attsDefault = oldctxt->attsDefault; |
| 12571 | ctxt->attsSpecial = oldctxt->attsSpecial; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12572 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12573 | xmlParseContent(ctxt); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12574 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12575 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12576 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12577 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12578 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12579 | if (ctxt->node != ctxt->myDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12580 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12581 | } |
| 12582 | |
| 12583 | if (!ctxt->wellFormed) { |
| 12584 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 12585 | ret = XML_ERR_INTERNAL_ERROR; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12586 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12587 | ret = (xmlParserErrors)ctxt->errNo; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12588 | } else { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12589 | ret = XML_ERR_OK; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12590 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12591 | |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 12592 | if ((lst != NULL) && (ret == XML_ERR_OK)) { |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12593 | xmlNodePtr cur; |
| 12594 | |
| 12595 | /* |
| 12596 | * Return the newly created nodeset after unlinking it from |
| 12597 | * they pseudo parent. |
| 12598 | */ |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12599 | cur = ctxt->myDoc->children->children; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12600 | *lst = cur; |
| 12601 | while (cur != NULL) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12602 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 12603 | if ((oldctxt->validate) && (oldctxt->wellFormed) && |
| 12604 | (oldctxt->myDoc) && (oldctxt->myDoc->intSubset) && |
| 12605 | (cur->type == XML_ELEMENT_NODE)) { |
Daniel Veillard | 8d58904 | 2003-02-04 15:07:21 +0000 | [diff] [blame] | 12606 | oldctxt->valid &= xmlValidateElement(&oldctxt->vctxt, |
| 12607 | oldctxt->myDoc, cur); |
| 12608 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12609 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12610 | cur->parent = NULL; |
| 12611 | cur = cur->next; |
| 12612 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12613 | ctxt->myDoc->children->children = NULL; |
| 12614 | } |
| 12615 | if (ctxt->myDoc != NULL) { |
| 12616 | xmlFreeNode(ctxt->myDoc->children); |
| 12617 | ctxt->myDoc->children = content; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 12618 | ctxt->myDoc->last = last; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12619 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12620 | |
| 12621 | /* |
| 12622 | * Record in the parent context the number of entities replacement |
| 12623 | * done when parsing that reference. |
| 12624 | */ |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 12625 | oldctxt->nbentities += ctxt->nbentities; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12626 | /* |
| 12627 | * Also record the last error if any |
| 12628 | */ |
| 12629 | if (ctxt->lastError.code != XML_ERR_OK) |
| 12630 | xmlCopyError(&ctxt->lastError, &oldctxt->lastError); |
| 12631 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12632 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 12633 | ctxt->dict = NULL; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 12634 | ctxt->attsDefault = NULL; |
| 12635 | ctxt->attsSpecial = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12636 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12637 | if (newDoc != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 12638 | xmlFreeDoc(newDoc); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 12639 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12640 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12641 | return(ret); |
| 12642 | } |
| 12643 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12644 | /** |
| 12645 | * xmlParseInNodeContext: |
| 12646 | * @node: the context node |
| 12647 | * @data: the input string |
| 12648 | * @datalen: the input string length in bytes |
| 12649 | * @options: a combination of xmlParserOption |
| 12650 | * @lst: the return value for the set of parsed nodes |
| 12651 | * |
| 12652 | * Parse a well-balanced chunk of an XML document |
| 12653 | * within the context (DTD, namespaces, etc ...) of the given node. |
| 12654 | * |
| 12655 | * The allowed sequence for the data is a Well Balanced Chunk defined by |
| 12656 | * the content production in the XML grammar: |
| 12657 | * |
| 12658 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 12659 | * |
| 12660 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 12661 | * error code otherwise |
| 12662 | */ |
| 12663 | xmlParserErrors |
| 12664 | xmlParseInNodeContext(xmlNodePtr node, const char *data, int datalen, |
| 12665 | int options, xmlNodePtr *lst) { |
| 12666 | #ifdef SAX2 |
| 12667 | xmlParserCtxtPtr ctxt; |
| 12668 | xmlDocPtr doc = NULL; |
| 12669 | xmlNodePtr fake, cur; |
| 12670 | int nsnr = 0; |
| 12671 | |
| 12672 | xmlParserErrors ret = XML_ERR_OK; |
| 12673 | |
| 12674 | /* |
| 12675 | * check all input parameters, grab the document |
| 12676 | */ |
| 12677 | if ((lst == NULL) || (node == NULL) || (data == NULL) || (datalen < 0)) |
| 12678 | return(XML_ERR_INTERNAL_ERROR); |
| 12679 | switch (node->type) { |
| 12680 | case XML_ELEMENT_NODE: |
| 12681 | case XML_ATTRIBUTE_NODE: |
| 12682 | case XML_TEXT_NODE: |
| 12683 | case XML_CDATA_SECTION_NODE: |
| 12684 | case XML_ENTITY_REF_NODE: |
| 12685 | case XML_PI_NODE: |
| 12686 | case XML_COMMENT_NODE: |
| 12687 | case XML_DOCUMENT_NODE: |
| 12688 | case XML_HTML_DOCUMENT_NODE: |
| 12689 | break; |
| 12690 | default: |
| 12691 | return(XML_ERR_INTERNAL_ERROR); |
| 12692 | |
| 12693 | } |
| 12694 | while ((node != NULL) && (node->type != XML_ELEMENT_NODE) && |
| 12695 | (node->type != XML_DOCUMENT_NODE) && |
| 12696 | (node->type != XML_HTML_DOCUMENT_NODE)) |
| 12697 | node = node->parent; |
| 12698 | if (node == NULL) |
| 12699 | return(XML_ERR_INTERNAL_ERROR); |
| 12700 | if (node->type == XML_ELEMENT_NODE) |
| 12701 | doc = node->doc; |
| 12702 | else |
| 12703 | doc = (xmlDocPtr) node; |
| 12704 | if (doc == NULL) |
| 12705 | return(XML_ERR_INTERNAL_ERROR); |
| 12706 | |
| 12707 | /* |
| 12708 | * allocate a context and set-up everything not related to the |
| 12709 | * node position in the tree |
| 12710 | */ |
| 12711 | if (doc->type == XML_DOCUMENT_NODE) |
| 12712 | ctxt = xmlCreateMemoryParserCtxt((char *) data, datalen); |
| 12713 | #ifdef LIBXML_HTML_ENABLED |
| 12714 | else if (doc->type == XML_HTML_DOCUMENT_NODE) |
| 12715 | ctxt = htmlCreateMemoryParserCtxt((char *) data, datalen); |
| 12716 | #endif |
| 12717 | else |
| 12718 | return(XML_ERR_INTERNAL_ERROR); |
| 12719 | |
| 12720 | if (ctxt == NULL) |
| 12721 | return(XML_ERR_NO_MEMORY); |
| 12722 | fake = xmlNewComment(NULL); |
| 12723 | if (fake == NULL) { |
| 12724 | xmlFreeParserCtxt(ctxt); |
| 12725 | return(XML_ERR_NO_MEMORY); |
| 12726 | } |
| 12727 | xmlAddChild(node, fake); |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 12728 | |
| 12729 | /* |
| 12730 | * Use input doc's dict if present, else assure XML_PARSE_NODICT is set. |
| 12731 | * We need a dictionary for xmlDetectSAX2, so if there's no doc dict |
| 12732 | * we must wait until the last moment to free the original one. |
| 12733 | */ |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12734 | if (doc->dict != NULL) { |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 12735 | if (ctxt->dict != NULL) |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12736 | xmlDictFree(ctxt->dict); |
| 12737 | ctxt->dict = doc->dict; |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 12738 | } else |
| 12739 | options |= XML_PARSE_NODICT; |
| 12740 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 12741 | xmlCtxtUseOptionsInternal(ctxt, options, NULL); |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12742 | xmlDetectSAX2(ctxt); |
| 12743 | ctxt->myDoc = doc; |
| 12744 | |
| 12745 | if (node->type == XML_ELEMENT_NODE) { |
| 12746 | nodePush(ctxt, node); |
| 12747 | /* |
| 12748 | * initialize the SAX2 namespaces stack |
| 12749 | */ |
| 12750 | cur = node; |
| 12751 | while ((cur != NULL) && (cur->type == XML_ELEMENT_NODE)) { |
| 12752 | xmlNsPtr ns = cur->nsDef; |
| 12753 | const xmlChar *iprefix, *ihref; |
| 12754 | |
| 12755 | while (ns != NULL) { |
| 12756 | if (ctxt->dict) { |
| 12757 | iprefix = xmlDictLookup(ctxt->dict, ns->prefix, -1); |
| 12758 | ihref = xmlDictLookup(ctxt->dict, ns->href, -1); |
| 12759 | } else { |
| 12760 | iprefix = ns->prefix; |
| 12761 | ihref = ns->href; |
| 12762 | } |
| 12763 | |
| 12764 | if (xmlGetNamespace(ctxt, iprefix) == NULL) { |
| 12765 | nsPush(ctxt, iprefix, ihref); |
| 12766 | nsnr++; |
| 12767 | } |
| 12768 | ns = ns->next; |
| 12769 | } |
| 12770 | cur = cur->parent; |
| 12771 | } |
| 12772 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12773 | } |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12774 | |
| 12775 | if ((ctxt->validate) || (ctxt->replaceEntities != 0)) { |
| 12776 | /* |
| 12777 | * ID/IDREF registration will be done in xmlValidateElement below |
| 12778 | */ |
| 12779 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 12780 | } |
| 12781 | |
Daniel Veillard | 499cc92 | 2006-01-18 17:22:35 +0000 | [diff] [blame] | 12782 | #ifdef LIBXML_HTML_ENABLED |
| 12783 | if (doc->type == XML_HTML_DOCUMENT_NODE) |
| 12784 | __htmlParseContent(ctxt); |
| 12785 | else |
| 12786 | #endif |
| 12787 | xmlParseContent(ctxt); |
| 12788 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12789 | nsPop(ctxt, nsnr); |
| 12790 | if ((RAW == '<') && (NXT(1) == '/')) { |
| 12791 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 12792 | } else if (RAW != 0) { |
| 12793 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
| 12794 | } |
| 12795 | if ((ctxt->node != NULL) && (ctxt->node != node)) { |
| 12796 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 12797 | ctxt->wellFormed = 0; |
| 12798 | } |
| 12799 | |
| 12800 | if (!ctxt->wellFormed) { |
| 12801 | if (ctxt->errNo == 0) |
| 12802 | ret = XML_ERR_INTERNAL_ERROR; |
| 12803 | else |
| 12804 | ret = (xmlParserErrors)ctxt->errNo; |
| 12805 | } else { |
| 12806 | ret = XML_ERR_OK; |
| 12807 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12808 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12809 | /* |
| 12810 | * Return the newly created nodeset after unlinking it from |
| 12811 | * the pseudo sibling. |
| 12812 | */ |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12813 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12814 | cur = fake->next; |
| 12815 | fake->next = NULL; |
| 12816 | node->last = fake; |
| 12817 | |
| 12818 | if (cur != NULL) { |
| 12819 | cur->prev = NULL; |
| 12820 | } |
| 12821 | |
| 12822 | *lst = cur; |
| 12823 | |
| 12824 | while (cur != NULL) { |
| 12825 | cur->parent = NULL; |
| 12826 | cur = cur->next; |
| 12827 | } |
| 12828 | |
| 12829 | xmlUnlinkNode(fake); |
| 12830 | xmlFreeNode(fake); |
| 12831 | |
| 12832 | |
| 12833 | if (ret != XML_ERR_OK) { |
| 12834 | xmlFreeNodeList(*lst); |
| 12835 | *lst = NULL; |
| 12836 | } |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 12837 | |
William M. Brack | b7b54de | 2004-10-06 16:38:01 +0000 | [diff] [blame] | 12838 | if (doc->dict != NULL) |
| 12839 | ctxt->dict = NULL; |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12840 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12841 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 12842 | return(ret); |
| 12843 | #else /* !SAX2 */ |
| 12844 | return(XML_ERR_INTERNAL_ERROR); |
| 12845 | #endif |
| 12846 | } |
| 12847 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12848 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 12849 | /** |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 12850 | * xmlParseBalancedChunkMemoryRecover: |
| 12851 | * @doc: the document the chunk pertains to |
| 12852 | * @sax: the SAX handler bloc (possibly NULL) |
| 12853 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 12854 | * @depth: Used for loop detection, use 0 |
| 12855 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 12856 | * @lst: the return value for the set of parsed nodes |
| 12857 | * @recover: return nodes even if the data is broken (use 0) |
| 12858 | * |
| 12859 | * |
| 12860 | * Parse a well-balanced chunk of an XML document |
| 12861 | * called by the parser |
| 12862 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 12863 | * the content production in the XML grammar: |
| 12864 | * |
| 12865 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 12866 | * |
| 12867 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 12868 | * the parser error code otherwise |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 12869 | * |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 12870 | * 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] | 12871 | * the parsed chunk is not well balanced, assuming the parsing succeeded to |
| 12872 | * some extent. |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 12873 | */ |
| 12874 | int |
| 12875 | xmlParseBalancedChunkMemoryRecover(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 12876 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst, |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 12877 | int recover) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12878 | xmlParserCtxtPtr ctxt; |
| 12879 | xmlDocPtr newDoc; |
| 12880 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 12881 | xmlNodePtr content, newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12882 | int size; |
| 12883 | int ret = 0; |
| 12884 | |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12885 | if (depth > 40) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12886 | return(XML_ERR_ENTITY_LOOP); |
| 12887 | } |
| 12888 | |
| 12889 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 12890 | if (lst != NULL) |
| 12891 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12892 | if (string == NULL) |
| 12893 | return(-1); |
| 12894 | |
| 12895 | size = xmlStrlen(string); |
| 12896 | |
| 12897 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
| 12898 | if (ctxt == NULL) return(-1); |
| 12899 | ctxt->userData = ctxt; |
| 12900 | if (sax != NULL) { |
| 12901 | oldsax = ctxt->sax; |
| 12902 | ctxt->sax = sax; |
| 12903 | if (user_data != NULL) |
| 12904 | ctxt->userData = user_data; |
| 12905 | } |
| 12906 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 12907 | if (newDoc == NULL) { |
| 12908 | xmlFreeParserCtxt(ctxt); |
| 12909 | return(-1); |
| 12910 | } |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 12911 | newDoc->properties = XML_DOC_INTERNAL; |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 12912 | if ((doc != NULL) && (doc->dict != NULL)) { |
| 12913 | xmlDictFree(ctxt->dict); |
| 12914 | ctxt->dict = doc->dict; |
| 12915 | xmlDictReference(ctxt->dict); |
| 12916 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 12917 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 12918 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
| 12919 | ctxt->dictNames = 1; |
| 12920 | } else { |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 12921 | xmlCtxtUseOptionsInternal(ctxt, XML_PARSE_NODICT, NULL); |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 12922 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12923 | if (doc != NULL) { |
| 12924 | newDoc->intSubset = doc->intSubset; |
| 12925 | newDoc->extSubset = doc->extSubset; |
| 12926 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 12927 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 12928 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12929 | if (sax != NULL) |
| 12930 | ctxt->sax = oldsax; |
| 12931 | xmlFreeParserCtxt(ctxt); |
| 12932 | newDoc->intSubset = NULL; |
| 12933 | newDoc->extSubset = NULL; |
| 12934 | xmlFreeDoc(newDoc); |
| 12935 | return(-1); |
| 12936 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 12937 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
| 12938 | nodePush(ctxt, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12939 | if (doc == NULL) { |
| 12940 | ctxt->myDoc = newDoc; |
| 12941 | } else { |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 12942 | ctxt->myDoc = newDoc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12943 | newDoc->children->doc = doc; |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 12944 | /* Ensure that doc has XML spec namespace */ |
| 12945 | xmlSearchNsByHref(doc, (xmlNodePtr)doc, XML_XML_NAMESPACE); |
| 12946 | newDoc->oldNs = doc->oldNs; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12947 | } |
| 12948 | ctxt->instate = XML_PARSER_CONTENT; |
| 12949 | ctxt->depth = depth; |
| 12950 | |
| 12951 | /* |
| 12952 | * Doing validity checking on chunk doesn't make sense |
| 12953 | */ |
| 12954 | ctxt->validate = 0; |
| 12955 | ctxt->loadsubset = 0; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12956 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12957 | |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 12958 | if ( doc != NULL ){ |
| 12959 | content = doc->children; |
| 12960 | doc->children = NULL; |
| 12961 | xmlParseContent(ctxt); |
| 12962 | doc->children = content; |
| 12963 | } |
| 12964 | else { |
| 12965 | xmlParseContent(ctxt); |
| 12966 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12967 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12968 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12969 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12970 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12971 | } |
| 12972 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 12973 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12974 | } |
| 12975 | |
| 12976 | if (!ctxt->wellFormed) { |
| 12977 | if (ctxt->errNo == 0) |
| 12978 | ret = 1; |
| 12979 | else |
| 12980 | ret = ctxt->errNo; |
| 12981 | } else { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 12982 | ret = 0; |
| 12983 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 12984 | |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 12985 | if ((lst != NULL) && ((ret == 0) || (recover == 1))) { |
| 12986 | xmlNodePtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12987 | |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 12988 | /* |
| 12989 | * Return the newly created nodeset after unlinking it from |
| 12990 | * they pseudo parent. |
| 12991 | */ |
| 12992 | cur = newDoc->children->children; |
| 12993 | *lst = cur; |
| 12994 | while (cur != NULL) { |
| 12995 | xmlSetTreeDoc(cur, doc); |
| 12996 | cur->parent = NULL; |
| 12997 | cur = cur->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12998 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 12999 | newDoc->children->children = NULL; |
| 13000 | } |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13001 | |
| 13002 | if (sax != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13003 | ctxt->sax = oldsax; |
| 13004 | xmlFreeParserCtxt(ctxt); |
| 13005 | newDoc->intSubset = NULL; |
| 13006 | newDoc->extSubset = NULL; |
Rob Richards | a02f199 | 2006-09-16 14:04:26 +0000 | [diff] [blame] | 13007 | newDoc->oldNs = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13008 | xmlFreeDoc(newDoc); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13009 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13010 | return(ret); |
| 13011 | } |
| 13012 | |
| 13013 | /** |
| 13014 | * xmlSAXParseEntity: |
| 13015 | * @sax: the SAX handler block |
| 13016 | * @filename: the filename |
| 13017 | * |
| 13018 | * parse an XML external entity out of context and build a tree. |
| 13019 | * It use the given SAX function block to handle the parsing callback. |
| 13020 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 13021 | * |
| 13022 | * [78] extParsedEnt ::= TextDecl? content |
| 13023 | * |
| 13024 | * This correspond to a "Well Balanced" chunk |
| 13025 | * |
| 13026 | * Returns the resulting document tree |
| 13027 | */ |
| 13028 | |
| 13029 | xmlDocPtr |
| 13030 | xmlSAXParseEntity(xmlSAXHandlerPtr sax, const char *filename) { |
| 13031 | xmlDocPtr ret; |
| 13032 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13033 | |
| 13034 | ctxt = xmlCreateFileParserCtxt(filename); |
| 13035 | if (ctxt == NULL) { |
| 13036 | return(NULL); |
| 13037 | } |
| 13038 | if (sax != NULL) { |
| 13039 | if (ctxt->sax != NULL) |
| 13040 | xmlFree(ctxt->sax); |
| 13041 | ctxt->sax = sax; |
| 13042 | ctxt->userData = NULL; |
| 13043 | } |
| 13044 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13045 | xmlParseExtParsedEnt(ctxt); |
| 13046 | |
| 13047 | if (ctxt->wellFormed) |
| 13048 | ret = ctxt->myDoc; |
| 13049 | else { |
| 13050 | ret = NULL; |
| 13051 | xmlFreeDoc(ctxt->myDoc); |
| 13052 | ctxt->myDoc = NULL; |
| 13053 | } |
| 13054 | if (sax != NULL) |
| 13055 | ctxt->sax = NULL; |
| 13056 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13057 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13058 | return(ret); |
| 13059 | } |
| 13060 | |
| 13061 | /** |
| 13062 | * xmlParseEntity: |
| 13063 | * @filename: the filename |
| 13064 | * |
| 13065 | * parse an XML external entity out of context and build a tree. |
| 13066 | * |
| 13067 | * [78] extParsedEnt ::= TextDecl? content |
| 13068 | * |
| 13069 | * This correspond to a "Well Balanced" chunk |
| 13070 | * |
| 13071 | * Returns the resulting document tree |
| 13072 | */ |
| 13073 | |
| 13074 | xmlDocPtr |
| 13075 | xmlParseEntity(const char *filename) { |
| 13076 | return(xmlSAXParseEntity(NULL, filename)); |
| 13077 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13078 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13079 | |
| 13080 | /** |
| 13081 | * xmlCreateEntityParserCtxt: |
| 13082 | * @URL: the entity URL |
| 13083 | * @ID: the entity PUBLIC ID |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 13084 | * @base: a possible base for the target URI |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13085 | * |
| 13086 | * Create a parser context for an external entity |
| 13087 | * Automatic support for ZLIB/Compress compressed document is provided |
| 13088 | * by default if found at compile-time. |
| 13089 | * |
| 13090 | * Returns the new parser context or NULL |
| 13091 | */ |
| 13092 | xmlParserCtxtPtr |
| 13093 | xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID, |
| 13094 | const xmlChar *base) { |
| 13095 | xmlParserCtxtPtr ctxt; |
| 13096 | xmlParserInputPtr inputStream; |
| 13097 | char *directory = NULL; |
| 13098 | xmlChar *uri; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13099 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13100 | ctxt = xmlNewParserCtxt(); |
| 13101 | if (ctxt == NULL) { |
| 13102 | return(NULL); |
| 13103 | } |
| 13104 | |
| 13105 | uri = xmlBuildURI(URL, base); |
| 13106 | |
| 13107 | if (uri == NULL) { |
| 13108 | inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt); |
| 13109 | if (inputStream == NULL) { |
| 13110 | xmlFreeParserCtxt(ctxt); |
| 13111 | return(NULL); |
| 13112 | } |
| 13113 | |
| 13114 | inputPush(ctxt, inputStream); |
| 13115 | |
| 13116 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 13117 | directory = xmlParserGetDirectory((char *)URL); |
| 13118 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 13119 | ctxt->directory = directory; |
| 13120 | } else { |
| 13121 | inputStream = xmlLoadExternalEntity((char *)uri, (char *)ID, ctxt); |
| 13122 | if (inputStream == NULL) { |
| 13123 | xmlFree(uri); |
| 13124 | xmlFreeParserCtxt(ctxt); |
| 13125 | return(NULL); |
| 13126 | } |
| 13127 | |
| 13128 | inputPush(ctxt, inputStream); |
| 13129 | |
| 13130 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 13131 | directory = xmlParserGetDirectory((char *)uri); |
| 13132 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 13133 | ctxt->directory = directory; |
| 13134 | xmlFree(uri); |
| 13135 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13136 | return(ctxt); |
| 13137 | } |
| 13138 | |
| 13139 | /************************************************************************ |
| 13140 | * * |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13141 | * Front ends when parsing from a file * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13142 | * * |
| 13143 | ************************************************************************/ |
| 13144 | |
| 13145 | /** |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13146 | * xmlCreateURLParserCtxt: |
| 13147 | * @filename: the filename or URL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 13148 | * @options: a combination of xmlParserOption |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13149 | * |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13150 | * Create a parser context for a file or URL content. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13151 | * Automatic support for ZLIB/Compress compressed document is provided |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13152 | * by default if found at compile-time and for file accesses |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13153 | * |
| 13154 | * Returns the new parser context or NULL |
| 13155 | */ |
| 13156 | xmlParserCtxtPtr |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13157 | xmlCreateURLParserCtxt(const char *filename, int options) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13158 | { |
| 13159 | xmlParserCtxtPtr ctxt; |
| 13160 | xmlParserInputPtr inputStream; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13161 | char *directory = NULL; |
| 13162 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13163 | ctxt = xmlNewParserCtxt(); |
| 13164 | if (ctxt == NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13165 | xmlErrMemory(NULL, "cannot allocate parser context"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13166 | return(NULL); |
| 13167 | } |
| 13168 | |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 13169 | if (options) |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13170 | xmlCtxtUseOptionsInternal(ctxt, options, NULL); |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 13171 | ctxt->linenumbers = 1; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 13172 | |
Daniel Veillard | 4e9b1bc | 2003-06-09 10:30:33 +0000 | [diff] [blame] | 13173 | inputStream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13174 | if (inputStream == NULL) { |
| 13175 | xmlFreeParserCtxt(ctxt); |
| 13176 | return(NULL); |
| 13177 | } |
| 13178 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13179 | inputPush(ctxt, inputStream); |
| 13180 | if ((ctxt->directory == NULL) && (directory == NULL)) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 13181 | directory = xmlParserGetDirectory(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13182 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 13183 | ctxt->directory = directory; |
| 13184 | |
| 13185 | return(ctxt); |
| 13186 | } |
| 13187 | |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 13188 | /** |
| 13189 | * xmlCreateFileParserCtxt: |
| 13190 | * @filename: the filename |
| 13191 | * |
| 13192 | * Create a parser context for a file content. |
| 13193 | * Automatic support for ZLIB/Compress compressed document is provided |
| 13194 | * by default if found at compile-time. |
| 13195 | * |
| 13196 | * Returns the new parser context or NULL |
| 13197 | */ |
| 13198 | xmlParserCtxtPtr |
| 13199 | xmlCreateFileParserCtxt(const char *filename) |
| 13200 | { |
| 13201 | return(xmlCreateURLParserCtxt(filename, 0)); |
| 13202 | } |
| 13203 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13204 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13205 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13206 | * xmlSAXParseFileWithData: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13207 | * @sax: the SAX handler block |
| 13208 | * @filename: the filename |
| 13209 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 13210 | * documents |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13211 | * @data: the userdata |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13212 | * |
| 13213 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 13214 | * compressed document is provided by default if found at compile-time. |
| 13215 | * It use the given SAX function block to handle the parsing callback. |
| 13216 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 13217 | * |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 13218 | * User data (void *) is stored within the parser context in the |
| 13219 | * context's _private member, so it is available nearly everywhere in libxml |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13220 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13221 | * Returns the resulting document tree |
| 13222 | */ |
| 13223 | |
| 13224 | xmlDocPtr |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13225 | xmlSAXParseFileWithData(xmlSAXHandlerPtr sax, const char *filename, |
| 13226 | int recovery, void *data) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13227 | xmlDocPtr ret; |
| 13228 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13229 | |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 13230 | xmlInitParser(); |
| 13231 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13232 | ctxt = xmlCreateFileParserCtxt(filename); |
| 13233 | if (ctxt == NULL) { |
| 13234 | return(NULL); |
| 13235 | } |
| 13236 | if (sax != NULL) { |
| 13237 | if (ctxt->sax != NULL) |
| 13238 | xmlFree(ctxt->sax); |
| 13239 | ctxt->sax = sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13240 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13241 | xmlDetectSAX2(ctxt); |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13242 | if (data!=NULL) { |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 13243 | ctxt->_private = data; |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13244 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13245 | |
Daniel Veillard | 37d2d16 | 2008-03-14 10:54:00 +0000 | [diff] [blame] | 13246 | if (ctxt->directory == NULL) |
| 13247 | ctxt->directory = xmlParserGetDirectory(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13248 | |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 13249 | ctxt->recovery = recovery; |
| 13250 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13251 | xmlParseDocument(ctxt); |
| 13252 | |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 13253 | if ((ctxt->wellFormed) || recovery) { |
| 13254 | ret = ctxt->myDoc; |
Daniel Veillard | b65e12e | 2003-10-08 21:33:28 +0000 | [diff] [blame] | 13255 | if (ret != NULL) { |
| 13256 | if (ctxt->input->buf->compressed > 0) |
| 13257 | ret->compression = 9; |
| 13258 | else |
| 13259 | ret->compression = ctxt->input->buf->compressed; |
| 13260 | } |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 13261 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13262 | else { |
| 13263 | ret = NULL; |
| 13264 | xmlFreeDoc(ctxt->myDoc); |
| 13265 | ctxt->myDoc = NULL; |
| 13266 | } |
| 13267 | if (sax != NULL) |
| 13268 | ctxt->sax = NULL; |
| 13269 | xmlFreeParserCtxt(ctxt); |
| 13270 | |
| 13271 | return(ret); |
| 13272 | } |
| 13273 | |
| 13274 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 13275 | * xmlSAXParseFile: |
| 13276 | * @sax: the SAX handler block |
| 13277 | * @filename: the filename |
| 13278 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 13279 | * documents |
| 13280 | * |
| 13281 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 13282 | * compressed document is provided by default if found at compile-time. |
| 13283 | * It use the given SAX function block to handle the parsing callback. |
| 13284 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 13285 | * |
| 13286 | * Returns the resulting document tree |
| 13287 | */ |
| 13288 | |
| 13289 | xmlDocPtr |
| 13290 | xmlSAXParseFile(xmlSAXHandlerPtr sax, const char *filename, |
| 13291 | int recovery) { |
| 13292 | return(xmlSAXParseFileWithData(sax,filename,recovery,NULL)); |
| 13293 | } |
| 13294 | |
| 13295 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13296 | * xmlRecoverDoc: |
| 13297 | * @cur: a pointer to an array of xmlChar |
| 13298 | * |
| 13299 | * parse an XML in-memory document and build a tree. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13300 | * In the case the document is not Well Formed, a attempt to build a |
| 13301 | * tree is tried anyway |
| 13302 | * |
| 13303 | * Returns the resulting document tree or NULL in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13304 | */ |
| 13305 | |
| 13306 | xmlDocPtr |
| 13307 | xmlRecoverDoc(xmlChar *cur) { |
| 13308 | return(xmlSAXParseDoc(NULL, cur, 1)); |
| 13309 | } |
| 13310 | |
| 13311 | /** |
| 13312 | * xmlParseFile: |
| 13313 | * @filename: the filename |
| 13314 | * |
| 13315 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 13316 | * compressed document is provided by default if found at compile-time. |
| 13317 | * |
Daniel Veillard | 5d96fff | 2001-08-31 14:55:30 +0000 | [diff] [blame] | 13318 | * Returns the resulting document tree if the file was wellformed, |
| 13319 | * NULL otherwise. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13320 | */ |
| 13321 | |
| 13322 | xmlDocPtr |
| 13323 | xmlParseFile(const char *filename) { |
| 13324 | return(xmlSAXParseFile(NULL, filename, 0)); |
| 13325 | } |
| 13326 | |
| 13327 | /** |
| 13328 | * xmlRecoverFile: |
| 13329 | * @filename: the filename |
| 13330 | * |
| 13331 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 13332 | * compressed document is provided by default if found at compile-time. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13333 | * In the case the document is not Well Formed, it attempts to build |
| 13334 | * a tree anyway |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13335 | * |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13336 | * Returns the resulting document tree or NULL in case of failure |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13337 | */ |
| 13338 | |
| 13339 | xmlDocPtr |
| 13340 | xmlRecoverFile(const char *filename) { |
| 13341 | return(xmlSAXParseFile(NULL, filename, 1)); |
| 13342 | } |
| 13343 | |
| 13344 | |
| 13345 | /** |
| 13346 | * xmlSetupParserForBuffer: |
| 13347 | * @ctxt: an XML parser context |
| 13348 | * @buffer: a xmlChar * buffer |
| 13349 | * @filename: a file name |
| 13350 | * |
| 13351 | * Setup the parser context to parse a new buffer; Clears any prior |
| 13352 | * contents from the parser context. The buffer parameter must not be |
| 13353 | * NULL, but the filename parameter can be |
| 13354 | */ |
| 13355 | void |
| 13356 | xmlSetupParserForBuffer(xmlParserCtxtPtr ctxt, const xmlChar* buffer, |
| 13357 | const char* filename) |
| 13358 | { |
| 13359 | xmlParserInputPtr input; |
| 13360 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 13361 | if ((ctxt == NULL) || (buffer == NULL)) |
| 13362 | return; |
| 13363 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13364 | input = xmlNewInputStream(ctxt); |
| 13365 | if (input == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 13366 | xmlErrMemory(NULL, "parsing new buffer: out of memory\n"); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 13367 | xmlClearParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13368 | return; |
| 13369 | } |
| 13370 | |
| 13371 | xmlClearParserCtxt(ctxt); |
| 13372 | if (filename != NULL) |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 13373 | input->filename = (char *) xmlCanonicPath((const xmlChar *)filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13374 | input->base = buffer; |
| 13375 | input->cur = buffer; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 13376 | input->end = &buffer[xmlStrlen(buffer)]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13377 | inputPush(ctxt, input); |
| 13378 | } |
| 13379 | |
| 13380 | /** |
| 13381 | * xmlSAXUserParseFile: |
| 13382 | * @sax: a SAX handler |
| 13383 | * @user_data: The user data returned on SAX callbacks |
| 13384 | * @filename: a file name |
| 13385 | * |
| 13386 | * parse an XML file and call the given SAX handler routines. |
| 13387 | * Automatic support for ZLIB/Compress compressed document is provided |
| 13388 | * |
| 13389 | * Returns 0 in case of success or a error number otherwise |
| 13390 | */ |
| 13391 | int |
| 13392 | xmlSAXUserParseFile(xmlSAXHandlerPtr sax, void *user_data, |
| 13393 | const char *filename) { |
| 13394 | int ret = 0; |
| 13395 | xmlParserCtxtPtr ctxt; |
| 13396 | |
| 13397 | ctxt = xmlCreateFileParserCtxt(filename); |
| 13398 | if (ctxt == NULL) return -1; |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 13399 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13400 | xmlFree(ctxt->sax); |
| 13401 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13402 | xmlDetectSAX2(ctxt); |
| 13403 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13404 | if (user_data != NULL) |
| 13405 | ctxt->userData = user_data; |
| 13406 | |
| 13407 | xmlParseDocument(ctxt); |
| 13408 | |
| 13409 | if (ctxt->wellFormed) |
| 13410 | ret = 0; |
| 13411 | else { |
| 13412 | if (ctxt->errNo != 0) |
| 13413 | ret = ctxt->errNo; |
| 13414 | else |
| 13415 | ret = -1; |
| 13416 | } |
| 13417 | if (sax != NULL) |
| 13418 | ctxt->sax = NULL; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 13419 | if (ctxt->myDoc != NULL) { |
| 13420 | xmlFreeDoc(ctxt->myDoc); |
| 13421 | ctxt->myDoc = NULL; |
| 13422 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13423 | xmlFreeParserCtxt(ctxt); |
| 13424 | |
| 13425 | return ret; |
| 13426 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13427 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13428 | |
| 13429 | /************************************************************************ |
| 13430 | * * |
| 13431 | * Front ends when parsing from memory * |
| 13432 | * * |
| 13433 | ************************************************************************/ |
| 13434 | |
| 13435 | /** |
| 13436 | * xmlCreateMemoryParserCtxt: |
| 13437 | * @buffer: a pointer to a char array |
| 13438 | * @size: the size of the array |
| 13439 | * |
| 13440 | * Create a parser context for an XML in-memory document. |
| 13441 | * |
| 13442 | * Returns the new parser context or NULL |
| 13443 | */ |
| 13444 | xmlParserCtxtPtr |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 13445 | xmlCreateMemoryParserCtxt(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13446 | xmlParserCtxtPtr ctxt; |
| 13447 | xmlParserInputPtr input; |
| 13448 | xmlParserInputBufferPtr buf; |
| 13449 | |
| 13450 | if (buffer == NULL) |
| 13451 | return(NULL); |
| 13452 | if (size <= 0) |
| 13453 | return(NULL); |
| 13454 | |
| 13455 | ctxt = xmlNewParserCtxt(); |
| 13456 | if (ctxt == NULL) |
| 13457 | return(NULL); |
| 13458 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 13459 | /* TODO: xmlParserInputBufferCreateStatic, requires some serious changes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13460 | buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 13461 | if (buf == NULL) { |
| 13462 | xmlFreeParserCtxt(ctxt); |
| 13463 | return(NULL); |
| 13464 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13465 | |
| 13466 | input = xmlNewInputStream(ctxt); |
| 13467 | if (input == NULL) { |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 13468 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13469 | xmlFreeParserCtxt(ctxt); |
| 13470 | return(NULL); |
| 13471 | } |
| 13472 | |
| 13473 | input->filename = NULL; |
| 13474 | input->buf = buf; |
| 13475 | input->base = input->buf->buffer->content; |
| 13476 | input->cur = input->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 13477 | input->end = &input->buf->buffer->content[input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13478 | |
| 13479 | inputPush(ctxt, input); |
| 13480 | return(ctxt); |
| 13481 | } |
| 13482 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13483 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13484 | /** |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 13485 | * xmlSAXParseMemoryWithData: |
| 13486 | * @sax: the SAX handler block |
| 13487 | * @buffer: an pointer to a char array |
| 13488 | * @size: the size of the array |
| 13489 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 13490 | * documents |
| 13491 | * @data: the userdata |
| 13492 | * |
| 13493 | * parse an XML in-memory block and use the given SAX function block |
| 13494 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 13495 | * DOM tree building routines. |
| 13496 | * |
| 13497 | * User data (void *) is stored within the parser context in the |
| 13498 | * context's _private member, so it is available nearly everywhere in libxml |
| 13499 | * |
| 13500 | * Returns the resulting document tree |
| 13501 | */ |
| 13502 | |
| 13503 | xmlDocPtr |
| 13504 | xmlSAXParseMemoryWithData(xmlSAXHandlerPtr sax, const char *buffer, |
| 13505 | int size, int recovery, void *data) { |
| 13506 | xmlDocPtr ret; |
| 13507 | xmlParserCtxtPtr ctxt; |
| 13508 | |
| 13509 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 13510 | if (ctxt == NULL) return(NULL); |
| 13511 | if (sax != NULL) { |
| 13512 | if (ctxt->sax != NULL) |
| 13513 | xmlFree(ctxt->sax); |
| 13514 | ctxt->sax = sax; |
| 13515 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13516 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 13517 | if (data!=NULL) { |
| 13518 | ctxt->_private=data; |
| 13519 | } |
| 13520 | |
Daniel Veillard | adba5f1 | 2003-04-04 16:09:01 +0000 | [diff] [blame] | 13521 | ctxt->recovery = recovery; |
| 13522 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 13523 | xmlParseDocument(ctxt); |
| 13524 | |
| 13525 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 13526 | else { |
| 13527 | ret = NULL; |
| 13528 | xmlFreeDoc(ctxt->myDoc); |
| 13529 | ctxt->myDoc = NULL; |
| 13530 | } |
| 13531 | if (sax != NULL) |
| 13532 | ctxt->sax = NULL; |
| 13533 | xmlFreeParserCtxt(ctxt); |
| 13534 | |
| 13535 | return(ret); |
| 13536 | } |
| 13537 | |
| 13538 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13539 | * xmlSAXParseMemory: |
| 13540 | * @sax: the SAX handler block |
| 13541 | * @buffer: an pointer to a char array |
| 13542 | * @size: the size of the array |
| 13543 | * @recovery: work in recovery mode, i.e. tries to read not Well Formed |
| 13544 | * documents |
| 13545 | * |
| 13546 | * parse an XML in-memory block and use the given SAX function block |
| 13547 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 13548 | * DOM tree building routines. |
| 13549 | * |
| 13550 | * Returns the resulting document tree |
| 13551 | */ |
| 13552 | xmlDocPtr |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 13553 | xmlSAXParseMemory(xmlSAXHandlerPtr sax, const char *buffer, |
| 13554 | int size, int recovery) { |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 13555 | return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13556 | } |
| 13557 | |
| 13558 | /** |
| 13559 | * xmlParseMemory: |
| 13560 | * @buffer: an pointer to a char array |
| 13561 | * @size: the size of the array |
| 13562 | * |
| 13563 | * parse an XML in-memory block and build a tree. |
| 13564 | * |
| 13565 | * Returns the resulting document tree |
| 13566 | */ |
| 13567 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 13568 | xmlDocPtr xmlParseMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13569 | return(xmlSAXParseMemory(NULL, buffer, size, 0)); |
| 13570 | } |
| 13571 | |
| 13572 | /** |
| 13573 | * xmlRecoverMemory: |
| 13574 | * @buffer: an pointer to a char array |
| 13575 | * @size: the size of the array |
| 13576 | * |
| 13577 | * parse an XML in-memory block and build a tree. |
Daniel Veillard | 2135fc2 | 2008-04-04 16:10:51 +0000 | [diff] [blame] | 13578 | * In the case the document is not Well Formed, an attempt to |
| 13579 | * build a tree is tried anyway |
| 13580 | * |
| 13581 | * Returns the resulting document tree or NULL in case of error |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13582 | */ |
| 13583 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 13584 | xmlDocPtr xmlRecoverMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13585 | return(xmlSAXParseMemory(NULL, buffer, size, 1)); |
| 13586 | } |
| 13587 | |
| 13588 | /** |
| 13589 | * xmlSAXUserParseMemory: |
| 13590 | * @sax: a SAX handler |
| 13591 | * @user_data: The user data returned on SAX callbacks |
| 13592 | * @buffer: an in-memory XML document input |
| 13593 | * @size: the length of the XML document in bytes |
| 13594 | * |
| 13595 | * A better SAX parsing routine. |
| 13596 | * parse an XML in-memory buffer and call the given SAX handler routines. |
| 13597 | * |
| 13598 | * Returns 0 in case of success or a error number otherwise |
| 13599 | */ |
| 13600 | int xmlSAXUserParseMemory(xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 13601 | const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13602 | int ret = 0; |
| 13603 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13604 | |
| 13605 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 13606 | if (ctxt == NULL) return -1; |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 13607 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
| 13608 | xmlFree(ctxt->sax); |
Daniel Veillard | 9e92351 | 2002-08-14 08:48:52 +0000 | [diff] [blame] | 13609 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13610 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 13611 | |
Daniel Veillard | 30211a0 | 2001-04-26 09:33:18 +0000 | [diff] [blame] | 13612 | if (user_data != NULL) |
| 13613 | ctxt->userData = user_data; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13614 | |
| 13615 | xmlParseDocument(ctxt); |
| 13616 | |
| 13617 | if (ctxt->wellFormed) |
| 13618 | ret = 0; |
| 13619 | else { |
| 13620 | if (ctxt->errNo != 0) |
| 13621 | ret = ctxt->errNo; |
| 13622 | else |
| 13623 | ret = -1; |
| 13624 | } |
Daniel Veillard | 3dcd319 | 2007-08-14 13:46:54 +0000 | [diff] [blame] | 13625 | if (sax != NULL) |
| 13626 | ctxt->sax = NULL; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 13627 | if (ctxt->myDoc != NULL) { |
| 13628 | xmlFreeDoc(ctxt->myDoc); |
| 13629 | ctxt->myDoc = NULL; |
| 13630 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13631 | xmlFreeParserCtxt(ctxt); |
| 13632 | |
| 13633 | return ret; |
| 13634 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13635 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13636 | |
| 13637 | /** |
| 13638 | * xmlCreateDocParserCtxt: |
| 13639 | * @cur: a pointer to an array of xmlChar |
| 13640 | * |
| 13641 | * Creates a parser context for an XML in-memory document. |
| 13642 | * |
| 13643 | * Returns the new parser context or NULL |
| 13644 | */ |
| 13645 | xmlParserCtxtPtr |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13646 | xmlCreateDocParserCtxt(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13647 | int len; |
| 13648 | |
| 13649 | if (cur == NULL) |
| 13650 | return(NULL); |
| 13651 | len = xmlStrlen(cur); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13652 | return(xmlCreateMemoryParserCtxt((const char *)cur, len)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13653 | } |
| 13654 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13655 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13656 | /** |
| 13657 | * xmlSAXParseDoc: |
| 13658 | * @sax: the SAX handler block |
| 13659 | * @cur: a pointer to an array of xmlChar |
| 13660 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 13661 | * documents |
| 13662 | * |
| 13663 | * parse an XML in-memory document and build a tree. |
| 13664 | * It use the given SAX function block to handle the parsing callback. |
| 13665 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 13666 | * |
| 13667 | * Returns the resulting document tree |
| 13668 | */ |
| 13669 | |
| 13670 | xmlDocPtr |
Daniel Veillard | 7331e5c | 2005-03-31 14:59:00 +0000 | [diff] [blame] | 13671 | xmlSAXParseDoc(xmlSAXHandlerPtr sax, const xmlChar *cur, int recovery) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13672 | xmlDocPtr ret; |
| 13673 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 13674 | xmlSAXHandlerPtr oldsax = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13675 | |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 13676 | if (cur == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13677 | |
| 13678 | |
| 13679 | ctxt = xmlCreateDocParserCtxt(cur); |
| 13680 | if (ctxt == NULL) return(NULL); |
| 13681 | if (sax != NULL) { |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 13682 | oldsax = ctxt->sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13683 | ctxt->sax = sax; |
| 13684 | ctxt->userData = NULL; |
| 13685 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 13686 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13687 | |
| 13688 | xmlParseDocument(ctxt); |
| 13689 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 13690 | else { |
| 13691 | ret = NULL; |
| 13692 | xmlFreeDoc(ctxt->myDoc); |
| 13693 | ctxt->myDoc = NULL; |
| 13694 | } |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 13695 | if (sax != NULL) |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 13696 | ctxt->sax = oldsax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13697 | xmlFreeParserCtxt(ctxt); |
| 13698 | |
| 13699 | return(ret); |
| 13700 | } |
| 13701 | |
| 13702 | /** |
| 13703 | * xmlParseDoc: |
| 13704 | * @cur: a pointer to an array of xmlChar |
| 13705 | * |
| 13706 | * parse an XML in-memory document and build a tree. |
| 13707 | * |
| 13708 | * Returns the resulting document tree |
| 13709 | */ |
| 13710 | |
| 13711 | xmlDocPtr |
Daniel Veillard | 7331e5c | 2005-03-31 14:59:00 +0000 | [diff] [blame] | 13712 | xmlParseDoc(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13713 | return(xmlSAXParseDoc(NULL, cur, 0)); |
| 13714 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13715 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13716 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13717 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 13718 | /************************************************************************ |
| 13719 | * * |
| 13720 | * Specific function to keep track of entities references * |
| 13721 | * and used by the XSLT debugger * |
| 13722 | * * |
| 13723 | ************************************************************************/ |
| 13724 | |
| 13725 | static xmlEntityReferenceFunc xmlEntityRefFunc = NULL; |
| 13726 | |
| 13727 | /** |
| 13728 | * xmlAddEntityReference: |
| 13729 | * @ent : A valid entity |
| 13730 | * @firstNode : A valid first node for children of entity |
| 13731 | * @lastNode : A valid last node of children entity |
| 13732 | * |
| 13733 | * Notify of a reference to an entity of type XML_EXTERNAL_GENERAL_PARSED_ENTITY |
| 13734 | */ |
| 13735 | static void |
| 13736 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 13737 | xmlNodePtr lastNode) |
| 13738 | { |
| 13739 | if (xmlEntityRefFunc != NULL) { |
| 13740 | (*xmlEntityRefFunc) (ent, firstNode, lastNode); |
| 13741 | } |
| 13742 | } |
| 13743 | |
| 13744 | |
| 13745 | /** |
| 13746 | * xmlSetEntityReferenceFunc: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 13747 | * @func: A valid function |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 13748 | * |
| 13749 | * Set the function to call call back when a xml reference has been made |
| 13750 | */ |
| 13751 | void |
| 13752 | xmlSetEntityReferenceFunc(xmlEntityReferenceFunc func) |
| 13753 | { |
| 13754 | xmlEntityRefFunc = func; |
| 13755 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 13756 | #endif /* LIBXML_LEGACY_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13757 | |
| 13758 | /************************************************************************ |
| 13759 | * * |
| 13760 | * Miscellaneous * |
| 13761 | * * |
| 13762 | ************************************************************************/ |
| 13763 | |
| 13764 | #ifdef LIBXML_XPATH_ENABLED |
| 13765 | #include <libxml/xpath.h> |
| 13766 | #endif |
| 13767 | |
Daniel Veillard | ffa3c74 | 2005-07-21 13:24:09 +0000 | [diff] [blame] | 13768 | extern void XMLCDECL xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13769 | static int xmlParserInitialized = 0; |
| 13770 | |
| 13771 | /** |
| 13772 | * xmlInitParser: |
| 13773 | * |
| 13774 | * Initialization function for the XML parser. |
| 13775 | * This is not reentrant. Call once before processing in case of |
| 13776 | * use in multithreaded programs. |
| 13777 | */ |
| 13778 | |
| 13779 | void |
| 13780 | xmlInitParser(void) { |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 13781 | if (xmlParserInitialized != 0) |
| 13782 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13783 | |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 13784 | #ifdef LIBXML_THREAD_ENABLED |
| 13785 | __xmlGlobalInitMutexLock(); |
| 13786 | if (xmlParserInitialized == 0) { |
| 13787 | #endif |
| 13788 | if ((xmlGenericError == xmlGenericErrorDefaultFunc) || |
| 13789 | (xmlGenericError == NULL)) |
| 13790 | initGenericErrorDefaultFunc(NULL); |
| 13791 | xmlInitGlobals(); |
| 13792 | xmlInitThreads(); |
| 13793 | xmlInitMemory(); |
| 13794 | xmlInitCharEncodingHandlers(); |
| 13795 | xmlDefaultSAXHandlerInit(); |
| 13796 | xmlRegisterDefaultInputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 13797 | #ifdef LIBXML_OUTPUT_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 13798 | xmlRegisterDefaultOutputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 13799 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13800 | #ifdef LIBXML_HTML_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 13801 | htmlInitAutoClose(); |
| 13802 | htmlDefaultSAXHandlerInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13803 | #endif |
| 13804 | #ifdef LIBXML_XPATH_ENABLED |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 13805 | xmlXPathInit(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13806 | #endif |
Daniel Veillard | fde5b0b | 2007-02-12 17:31:53 +0000 | [diff] [blame] | 13807 | xmlParserInitialized = 1; |
| 13808 | #ifdef LIBXML_THREAD_ENABLED |
| 13809 | } |
| 13810 | __xmlGlobalInitMutexUnlock(); |
| 13811 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13812 | } |
| 13813 | |
| 13814 | /** |
| 13815 | * xmlCleanupParser: |
| 13816 | * |
Daniel Veillard | 05b37c6 | 2008-03-31 08:27:07 +0000 | [diff] [blame] | 13817 | * This function name is somewhat misleading. It does not clean up |
| 13818 | * parser state, it cleans up memory allocated by the library itself. |
| 13819 | * It is a cleanup function for the XML library. It tries to reclaim all |
| 13820 | * related global memory allocated for the library processing. |
| 13821 | * It doesn't deallocate any document related memory. One should |
| 13822 | * call xmlCleanupParser() only when the process has finished using |
| 13823 | * the library and all XML/HTML documents built with it. |
| 13824 | * See also xmlInitParser() which has the opposite function of preparing |
| 13825 | * the library for operations. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13826 | */ |
| 13827 | |
| 13828 | void |
| 13829 | xmlCleanupParser(void) { |
Daniel Veillard | 7fb801f | 2003-08-17 21:07:26 +0000 | [diff] [blame] | 13830 | if (!xmlParserInitialized) |
| 13831 | return; |
| 13832 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13833 | xmlCleanupCharEncodingHandlers(); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 13834 | #ifdef LIBXML_CATALOG_ENABLED |
| 13835 | xmlCatalogCleanup(); |
| 13836 | #endif |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 13837 | xmlDictCleanup(); |
Daniel Veillard | 04054be | 2003-10-15 10:48:54 +0000 | [diff] [blame] | 13838 | xmlCleanupInputCallbacks(); |
| 13839 | #ifdef LIBXML_OUTPUT_ENABLED |
| 13840 | xmlCleanupOutputCallbacks(); |
| 13841 | #endif |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 13842 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 13843 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 13844 | xmlRelaxNGCleanupTypes(); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 13845 | #endif |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 13846 | xmlCleanupGlobals(); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 13847 | xmlResetLastError(); |
Daniel Veillard | 74c0e59 | 2003-11-25 07:01:38 +0000 | [diff] [blame] | 13848 | xmlCleanupThreads(); /* must be last if called not from the main thread */ |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 13849 | xmlCleanupMemory(); |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 13850 | xmlParserInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13851 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13852 | |
| 13853 | /************************************************************************ |
| 13854 | * * |
| 13855 | * New set (2.6.0) of simpler and more flexible APIs * |
| 13856 | * * |
| 13857 | ************************************************************************/ |
| 13858 | |
| 13859 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 13860 | * DICT_FREE: |
| 13861 | * @str: a string |
| 13862 | * |
| 13863 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 13864 | * current scope |
| 13865 | */ |
| 13866 | #define DICT_FREE(str) \ |
| 13867 | if ((str) && ((!dict) || \ |
| 13868 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 13869 | xmlFree((char *)(str)); |
| 13870 | |
| 13871 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13872 | * xmlCtxtReset: |
| 13873 | * @ctxt: an XML parser context |
| 13874 | * |
| 13875 | * Reset a parser context |
| 13876 | */ |
| 13877 | void |
| 13878 | xmlCtxtReset(xmlParserCtxtPtr ctxt) |
| 13879 | { |
| 13880 | xmlParserInputPtr input; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 13881 | xmlDictPtr dict; |
| 13882 | |
| 13883 | if (ctxt == NULL) |
| 13884 | return; |
| 13885 | |
| 13886 | dict = ctxt->dict; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13887 | |
| 13888 | while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */ |
| 13889 | xmlFreeInputStream(input); |
| 13890 | } |
| 13891 | ctxt->inputNr = 0; |
| 13892 | ctxt->input = NULL; |
| 13893 | |
| 13894 | ctxt->spaceNr = 0; |
Daniel Veillard | 2e62086 | 2007-06-12 08:18:21 +0000 | [diff] [blame] | 13895 | if (ctxt->spaceTab != NULL) { |
| 13896 | ctxt->spaceTab[0] = -1; |
| 13897 | ctxt->space = &ctxt->spaceTab[0]; |
| 13898 | } else { |
| 13899 | ctxt->space = NULL; |
| 13900 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13901 | |
| 13902 | |
| 13903 | ctxt->nodeNr = 0; |
| 13904 | ctxt->node = NULL; |
| 13905 | |
| 13906 | ctxt->nameNr = 0; |
| 13907 | ctxt->name = NULL; |
| 13908 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 13909 | DICT_FREE(ctxt->version); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13910 | ctxt->version = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 13911 | DICT_FREE(ctxt->encoding); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13912 | ctxt->encoding = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 13913 | DICT_FREE(ctxt->directory); |
| 13914 | ctxt->directory = NULL; |
| 13915 | DICT_FREE(ctxt->extSubURI); |
| 13916 | ctxt->extSubURI = NULL; |
| 13917 | DICT_FREE(ctxt->extSubSystem); |
| 13918 | ctxt->extSubSystem = NULL; |
| 13919 | if (ctxt->myDoc != NULL) |
| 13920 | xmlFreeDoc(ctxt->myDoc); |
| 13921 | ctxt->myDoc = NULL; |
| 13922 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13923 | ctxt->standalone = -1; |
| 13924 | ctxt->hasExternalSubset = 0; |
| 13925 | ctxt->hasPErefs = 0; |
| 13926 | ctxt->html = 0; |
| 13927 | ctxt->external = 0; |
| 13928 | ctxt->instate = XML_PARSER_START; |
| 13929 | ctxt->token = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13930 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13931 | ctxt->wellFormed = 1; |
| 13932 | ctxt->nsWellFormed = 1; |
Daniel Veillard | ae28918 | 2004-01-21 16:00:43 +0000 | [diff] [blame] | 13933 | ctxt->disableSAX = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13934 | ctxt->valid = 1; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 13935 | #if 0 |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13936 | ctxt->vctxt.userData = ctxt; |
| 13937 | ctxt->vctxt.error = xmlParserValidityError; |
| 13938 | ctxt->vctxt.warning = xmlParserValidityWarning; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 13939 | #endif |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13940 | ctxt->record_info = 0; |
| 13941 | ctxt->nbChars = 0; |
| 13942 | ctxt->checkIndex = 0; |
| 13943 | ctxt->inSubset = 0; |
| 13944 | ctxt->errNo = XML_ERR_OK; |
| 13945 | ctxt->depth = 0; |
| 13946 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 13947 | ctxt->catalogs = NULL; |
Daniel Veillard | 4bf899b | 2008-08-20 17:04:30 +0000 | [diff] [blame] | 13948 | ctxt->nbentities = 0; |
Daniel Veillard | 0161e63 | 2008-08-28 15:36:32 +0000 | [diff] [blame] | 13949 | ctxt->sizeentities = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13950 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
| 13951 | |
| 13952 | if (ctxt->attsDefault != NULL) { |
| 13953 | xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree); |
| 13954 | ctxt->attsDefault = NULL; |
| 13955 | } |
| 13956 | if (ctxt->attsSpecial != NULL) { |
| 13957 | xmlHashFree(ctxt->attsSpecial, NULL); |
| 13958 | ctxt->attsSpecial = NULL; |
| 13959 | } |
| 13960 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 13961 | #ifdef LIBXML_CATALOG_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13962 | if (ctxt->catalogs != NULL) |
| 13963 | xmlCatalogFreeLocal(ctxt->catalogs); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 13964 | #endif |
Daniel Veillard | cc199e0 | 2003-10-24 21:11:48 +0000 | [diff] [blame] | 13965 | if (ctxt->lastError.code != XML_ERR_OK) |
| 13966 | xmlResetError(&ctxt->lastError); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13967 | } |
| 13968 | |
| 13969 | /** |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 13970 | * xmlCtxtResetPush: |
| 13971 | * @ctxt: an XML parser context |
| 13972 | * @chunk: a pointer to an array of chars |
| 13973 | * @size: number of chars in the array |
| 13974 | * @filename: an optional file name or URI |
| 13975 | * @encoding: the document encoding, or NULL |
| 13976 | * |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 13977 | * Reset a push parser context |
| 13978 | * |
| 13979 | * Returns 0 in case of success and 1 in case of error |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 13980 | */ |
| 13981 | int |
| 13982 | xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk, |
| 13983 | int size, const char *filename, const char *encoding) |
| 13984 | { |
| 13985 | xmlParserInputPtr inputStream; |
| 13986 | xmlParserInputBufferPtr buf; |
| 13987 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 13988 | |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 13989 | if (ctxt == NULL) |
| 13990 | return(1); |
| 13991 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 13992 | if ((encoding == NULL) && (chunk != NULL) && (size >= 4)) |
| 13993 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 13994 | |
| 13995 | buf = xmlAllocParserInputBuffer(enc); |
| 13996 | if (buf == NULL) |
| 13997 | return(1); |
| 13998 | |
| 13999 | if (ctxt == NULL) { |
| 14000 | xmlFreeParserInputBuffer(buf); |
| 14001 | return(1); |
| 14002 | } |
| 14003 | |
| 14004 | xmlCtxtReset(ctxt); |
| 14005 | |
| 14006 | if (ctxt->pushTab == NULL) { |
| 14007 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * |
| 14008 | sizeof(xmlChar *)); |
| 14009 | if (ctxt->pushTab == NULL) { |
| 14010 | xmlErrMemory(ctxt, NULL); |
| 14011 | xmlFreeParserInputBuffer(buf); |
| 14012 | return(1); |
| 14013 | } |
| 14014 | } |
| 14015 | |
| 14016 | if (filename == NULL) { |
| 14017 | ctxt->directory = NULL; |
| 14018 | } else { |
| 14019 | ctxt->directory = xmlParserGetDirectory(filename); |
| 14020 | } |
| 14021 | |
| 14022 | inputStream = xmlNewInputStream(ctxt); |
| 14023 | if (inputStream == NULL) { |
| 14024 | xmlFreeParserInputBuffer(buf); |
| 14025 | return(1); |
| 14026 | } |
| 14027 | |
| 14028 | if (filename == NULL) |
| 14029 | inputStream->filename = NULL; |
| 14030 | else |
| 14031 | inputStream->filename = (char *) |
| 14032 | xmlCanonicPath((const xmlChar *) filename); |
| 14033 | inputStream->buf = buf; |
| 14034 | inputStream->base = inputStream->buf->buffer->content; |
| 14035 | inputStream->cur = inputStream->buf->buffer->content; |
| 14036 | inputStream->end = |
| 14037 | &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; |
| 14038 | |
| 14039 | inputPush(ctxt, inputStream); |
| 14040 | |
| 14041 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 14042 | (ctxt->input->buf != NULL)) { |
| 14043 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 14044 | int cur = ctxt->input->cur - ctxt->input->base; |
| 14045 | |
| 14046 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
| 14047 | |
| 14048 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 14049 | ctxt->input->cur = ctxt->input->base + cur; |
| 14050 | ctxt->input->end = |
| 14051 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer-> |
| 14052 | use]; |
| 14053 | #ifdef DEBUG_PUSH |
| 14054 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 14055 | #endif |
| 14056 | } |
| 14057 | |
| 14058 | if (encoding != NULL) { |
| 14059 | xmlCharEncodingHandlerPtr hdlr; |
| 14060 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14061 | if (ctxt->encoding != NULL) |
| 14062 | xmlFree((xmlChar *) ctxt->encoding); |
| 14063 | ctxt->encoding = xmlStrdup((const xmlChar *) encoding); |
| 14064 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14065 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 14066 | if (hdlr != NULL) { |
| 14067 | xmlSwitchToEncoding(ctxt, hdlr); |
| 14068 | } else { |
| 14069 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 14070 | "Unsupported encoding %s\n", BAD_CAST encoding); |
| 14071 | } |
| 14072 | } else if (enc != XML_CHAR_ENCODING_NONE) { |
| 14073 | xmlSwitchEncoding(ctxt, enc); |
| 14074 | } |
| 14075 | |
| 14076 | return(0); |
| 14077 | } |
| 14078 | |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14079 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 14080 | /** |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14081 | * xmlCtxtUseOptionsInternal: |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14082 | * @ctxt: an XML parser context |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14083 | * @options: a combination of xmlParserOption |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14084 | * @encoding: the user provided encoding to use |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14085 | * |
| 14086 | * Applies the options to the parser context |
| 14087 | * |
| 14088 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 14089 | * in case of error. |
| 14090 | */ |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14091 | static int |
| 14092 | xmlCtxtUseOptionsInternal(xmlParserCtxtPtr ctxt, int options, const char *encoding) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14093 | { |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 14094 | if (ctxt == NULL) |
| 14095 | return(-1); |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14096 | if (encoding != NULL) { |
| 14097 | if (ctxt->encoding != NULL) |
| 14098 | xmlFree((xmlChar *) ctxt->encoding); |
| 14099 | ctxt->encoding = xmlStrdup((const xmlChar *) encoding); |
| 14100 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14101 | if (options & XML_PARSE_RECOVER) { |
| 14102 | ctxt->recovery = 1; |
| 14103 | options -= XML_PARSE_RECOVER; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14104 | ctxt->options |= XML_PARSE_RECOVER; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14105 | } else |
| 14106 | ctxt->recovery = 0; |
| 14107 | if (options & XML_PARSE_DTDLOAD) { |
| 14108 | ctxt->loadsubset = XML_DETECT_IDS; |
| 14109 | options -= XML_PARSE_DTDLOAD; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14110 | ctxt->options |= XML_PARSE_DTDLOAD; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14111 | } else |
| 14112 | ctxt->loadsubset = 0; |
| 14113 | if (options & XML_PARSE_DTDATTR) { |
| 14114 | ctxt->loadsubset |= XML_COMPLETE_ATTRS; |
| 14115 | options -= XML_PARSE_DTDATTR; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14116 | ctxt->options |= XML_PARSE_DTDATTR; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14117 | } |
| 14118 | if (options & XML_PARSE_NOENT) { |
| 14119 | ctxt->replaceEntities = 1; |
| 14120 | /* ctxt->loadsubset |= XML_DETECT_IDS; */ |
| 14121 | options -= XML_PARSE_NOENT; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14122 | ctxt->options |= XML_PARSE_NOENT; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14123 | } else |
| 14124 | ctxt->replaceEntities = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14125 | if (options & XML_PARSE_PEDANTIC) { |
| 14126 | ctxt->pedantic = 1; |
| 14127 | options -= XML_PARSE_PEDANTIC; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14128 | ctxt->options |= XML_PARSE_PEDANTIC; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14129 | } else |
| 14130 | ctxt->pedantic = 0; |
| 14131 | if (options & XML_PARSE_NOBLANKS) { |
| 14132 | ctxt->keepBlanks = 0; |
| 14133 | ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace; |
| 14134 | options -= XML_PARSE_NOBLANKS; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14135 | ctxt->options |= XML_PARSE_NOBLANKS; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14136 | } else |
| 14137 | ctxt->keepBlanks = 1; |
| 14138 | if (options & XML_PARSE_DTDVALID) { |
| 14139 | ctxt->validate = 1; |
| 14140 | if (options & XML_PARSE_NOWARNING) |
| 14141 | ctxt->vctxt.warning = NULL; |
| 14142 | if (options & XML_PARSE_NOERROR) |
| 14143 | ctxt->vctxt.error = NULL; |
| 14144 | options -= XML_PARSE_DTDVALID; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14145 | ctxt->options |= XML_PARSE_DTDVALID; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14146 | } else |
| 14147 | ctxt->validate = 0; |
Daniel Veillard | 971771e | 2005-07-09 17:32:57 +0000 | [diff] [blame] | 14148 | if (options & XML_PARSE_NOWARNING) { |
| 14149 | ctxt->sax->warning = NULL; |
| 14150 | options -= XML_PARSE_NOWARNING; |
| 14151 | } |
| 14152 | if (options & XML_PARSE_NOERROR) { |
| 14153 | ctxt->sax->error = NULL; |
| 14154 | ctxt->sax->fatalError = NULL; |
| 14155 | options -= XML_PARSE_NOERROR; |
| 14156 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14157 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14158 | if (options & XML_PARSE_SAX1) { |
| 14159 | ctxt->sax->startElement = xmlSAX2StartElement; |
| 14160 | ctxt->sax->endElement = xmlSAX2EndElement; |
| 14161 | ctxt->sax->startElementNs = NULL; |
| 14162 | ctxt->sax->endElementNs = NULL; |
| 14163 | ctxt->sax->initialized = 1; |
| 14164 | options -= XML_PARSE_SAX1; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14165 | ctxt->options |= XML_PARSE_SAX1; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14166 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 14167 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14168 | if (options & XML_PARSE_NODICT) { |
| 14169 | ctxt->dictNames = 0; |
| 14170 | options -= XML_PARSE_NODICT; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14171 | ctxt->options |= XML_PARSE_NODICT; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14172 | } else { |
| 14173 | ctxt->dictNames = 1; |
| 14174 | } |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 14175 | if (options & XML_PARSE_NOCDATA) { |
| 14176 | ctxt->sax->cdataBlock = NULL; |
| 14177 | options -= XML_PARSE_NOCDATA; |
Daniel Veillard | ae0765b | 2008-07-31 19:54:59 +0000 | [diff] [blame] | 14178 | ctxt->options |= XML_PARSE_NOCDATA; |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 14179 | } |
| 14180 | if (options & XML_PARSE_NSCLEAN) { |
| 14181 | ctxt->options |= XML_PARSE_NSCLEAN; |
| 14182 | options -= XML_PARSE_NSCLEAN; |
| 14183 | } |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14184 | if (options & XML_PARSE_NONET) { |
| 14185 | ctxt->options |= XML_PARSE_NONET; |
| 14186 | options -= XML_PARSE_NONET; |
| 14187 | } |
Daniel Veillard | 8874b94 | 2005-08-25 13:19:21 +0000 | [diff] [blame] | 14188 | if (options & XML_PARSE_COMPACT) { |
| 14189 | ctxt->options |= XML_PARSE_COMPACT; |
| 14190 | options -= XML_PARSE_COMPACT; |
| 14191 | } |
Daniel Veillard | 7e5c3f4 | 2008-07-29 16:12:31 +0000 | [diff] [blame] | 14192 | if (options & XML_PARSE_OLD10) { |
| 14193 | ctxt->options |= XML_PARSE_OLD10; |
| 14194 | options -= XML_PARSE_OLD10; |
| 14195 | } |
Daniel Veillard | 8915c15 | 2008-08-26 13:05:34 +0000 | [diff] [blame] | 14196 | if (options & XML_PARSE_NOBASEFIX) { |
| 14197 | ctxt->options |= XML_PARSE_NOBASEFIX; |
| 14198 | options -= XML_PARSE_NOBASEFIX; |
| 14199 | } |
| 14200 | if (options & XML_PARSE_HUGE) { |
| 14201 | ctxt->options |= XML_PARSE_HUGE; |
| 14202 | options -= XML_PARSE_HUGE; |
| 14203 | } |
Daniel Veillard | 7ec2997 | 2003-10-31 14:36:36 +0000 | [diff] [blame] | 14204 | ctxt->linenumbers = 1; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14205 | return (options); |
| 14206 | } |
| 14207 | |
| 14208 | /** |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14209 | * xmlCtxtUseOptions: |
| 14210 | * @ctxt: an XML parser context |
| 14211 | * @options: a combination of xmlParserOption |
| 14212 | * |
| 14213 | * Applies the options to the parser context |
| 14214 | * |
| 14215 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 14216 | * in case of error. |
| 14217 | */ |
| 14218 | int |
| 14219 | xmlCtxtUseOptions(xmlParserCtxtPtr ctxt, int options) |
| 14220 | { |
| 14221 | return(xmlCtxtUseOptionsInternal(ctxt, options, NULL)); |
| 14222 | } |
| 14223 | |
| 14224 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14225 | * xmlDoRead: |
| 14226 | * @ctxt: an XML parser context |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14227 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14228 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14229 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14230 | * @reuse: keep the context for reuse |
| 14231 | * |
| 14232 | * Common front-end for the xmlRead functions |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14233 | * |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14234 | * Returns the resulting document tree or NULL |
| 14235 | */ |
| 14236 | static xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14237 | xmlDoRead(xmlParserCtxtPtr ctxt, const char *URL, const char *encoding, |
| 14238 | int options, int reuse) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14239 | { |
| 14240 | xmlDocPtr ret; |
Daniel Veillard | 3733457 | 2008-07-31 08:20:02 +0000 | [diff] [blame] | 14241 | |
| 14242 | xmlCtxtUseOptionsInternal(ctxt, options, encoding); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14243 | if (encoding != NULL) { |
| 14244 | xmlCharEncodingHandlerPtr hdlr; |
| 14245 | |
| 14246 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 14247 | if (hdlr != NULL) |
| 14248 | xmlSwitchToEncoding(ctxt, hdlr); |
| 14249 | } |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14250 | if ((URL != NULL) && (ctxt->input != NULL) && |
| 14251 | (ctxt->input->filename == NULL)) |
| 14252 | ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14253 | xmlParseDocument(ctxt); |
| 14254 | if ((ctxt->wellFormed) || ctxt->recovery) |
| 14255 | ret = ctxt->myDoc; |
| 14256 | else { |
| 14257 | ret = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14258 | if (ctxt->myDoc != NULL) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14259 | xmlFreeDoc(ctxt->myDoc); |
| 14260 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14261 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14262 | ctxt->myDoc = NULL; |
| 14263 | if (!reuse) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14264 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 14265 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14266 | |
| 14267 | return (ret); |
| 14268 | } |
| 14269 | |
| 14270 | /** |
| 14271 | * xmlReadDoc: |
| 14272 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14273 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14274 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14275 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14276 | * |
| 14277 | * parse an XML in-memory document and build a tree. |
| 14278 | * |
| 14279 | * Returns the resulting document tree |
| 14280 | */ |
| 14281 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14282 | xmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14283 | { |
| 14284 | xmlParserCtxtPtr ctxt; |
| 14285 | |
| 14286 | if (cur == NULL) |
| 14287 | return (NULL); |
| 14288 | |
| 14289 | ctxt = xmlCreateDocParserCtxt(cur); |
| 14290 | if (ctxt == NULL) |
| 14291 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14292 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14293 | } |
| 14294 | |
| 14295 | /** |
| 14296 | * xmlReadFile: |
| 14297 | * @filename: a file or URL |
| 14298 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14299 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14300 | * |
| 14301 | * parse an XML file from the filesystem or the network. |
| 14302 | * |
| 14303 | * Returns the resulting document tree |
| 14304 | */ |
| 14305 | xmlDocPtr |
| 14306 | xmlReadFile(const char *filename, const char *encoding, int options) |
| 14307 | { |
| 14308 | xmlParserCtxtPtr ctxt; |
| 14309 | |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 14310 | ctxt = xmlCreateURLParserCtxt(filename, options); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14311 | if (ctxt == NULL) |
| 14312 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14313 | return (xmlDoRead(ctxt, NULL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14314 | } |
| 14315 | |
| 14316 | /** |
| 14317 | * xmlReadMemory: |
| 14318 | * @buffer: a pointer to a char array |
| 14319 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14320 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14321 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14322 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14323 | * |
| 14324 | * parse an XML in-memory document and build a tree. |
| 14325 | * |
| 14326 | * Returns the resulting document tree |
| 14327 | */ |
| 14328 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14329 | 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] | 14330 | { |
| 14331 | xmlParserCtxtPtr ctxt; |
| 14332 | |
| 14333 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 14334 | if (ctxt == NULL) |
| 14335 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14336 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14337 | } |
| 14338 | |
| 14339 | /** |
| 14340 | * xmlReadFd: |
| 14341 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14342 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14343 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14344 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14345 | * |
| 14346 | * parse an XML from a file descriptor and build a tree. |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 14347 | * NOTE that the file descriptor will not be closed when the |
| 14348 | * reader is closed or reset. |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14349 | * |
| 14350 | * Returns the resulting document tree |
| 14351 | */ |
| 14352 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14353 | xmlReadFd(int fd, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14354 | { |
| 14355 | xmlParserCtxtPtr ctxt; |
| 14356 | xmlParserInputBufferPtr input; |
| 14357 | xmlParserInputPtr stream; |
| 14358 | |
| 14359 | if (fd < 0) |
| 14360 | return (NULL); |
| 14361 | |
| 14362 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 14363 | if (input == NULL) |
| 14364 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 14365 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14366 | ctxt = xmlNewParserCtxt(); |
| 14367 | if (ctxt == NULL) { |
| 14368 | xmlFreeParserInputBuffer(input); |
| 14369 | return (NULL); |
| 14370 | } |
| 14371 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 14372 | if (stream == NULL) { |
| 14373 | xmlFreeParserInputBuffer(input); |
| 14374 | xmlFreeParserCtxt(ctxt); |
| 14375 | return (NULL); |
| 14376 | } |
| 14377 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14378 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14379 | } |
| 14380 | |
| 14381 | /** |
| 14382 | * xmlReadIO: |
| 14383 | * @ioread: an I/O read function |
| 14384 | * @ioclose: an I/O close function |
| 14385 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14386 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14387 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14388 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14389 | * |
| 14390 | * parse an XML document from I/O functions and source and build a tree. |
| 14391 | * |
| 14392 | * Returns the resulting document tree |
| 14393 | */ |
| 14394 | xmlDocPtr |
| 14395 | xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14396 | void *ioctx, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14397 | { |
| 14398 | xmlParserCtxtPtr ctxt; |
| 14399 | xmlParserInputBufferPtr input; |
| 14400 | xmlParserInputPtr stream; |
| 14401 | |
| 14402 | if (ioread == NULL) |
| 14403 | return (NULL); |
| 14404 | |
| 14405 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 14406 | XML_CHAR_ENCODING_NONE); |
| 14407 | if (input == NULL) |
| 14408 | return (NULL); |
| 14409 | ctxt = xmlNewParserCtxt(); |
| 14410 | if (ctxt == NULL) { |
| 14411 | xmlFreeParserInputBuffer(input); |
| 14412 | return (NULL); |
| 14413 | } |
| 14414 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 14415 | if (stream == NULL) { |
| 14416 | xmlFreeParserInputBuffer(input); |
| 14417 | xmlFreeParserCtxt(ctxt); |
| 14418 | return (NULL); |
| 14419 | } |
| 14420 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14421 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14422 | } |
| 14423 | |
| 14424 | /** |
| 14425 | * xmlCtxtReadDoc: |
| 14426 | * @ctxt: an XML parser context |
| 14427 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14428 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14429 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14430 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14431 | * |
| 14432 | * parse an XML in-memory document and build a tree. |
| 14433 | * This reuses the existing @ctxt parser context |
| 14434 | * |
| 14435 | * Returns the resulting document tree |
| 14436 | */ |
| 14437 | xmlDocPtr |
| 14438 | xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14439 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14440 | { |
| 14441 | xmlParserInputPtr stream; |
| 14442 | |
| 14443 | if (cur == NULL) |
| 14444 | return (NULL); |
| 14445 | if (ctxt == NULL) |
| 14446 | return (NULL); |
| 14447 | |
| 14448 | xmlCtxtReset(ctxt); |
| 14449 | |
| 14450 | stream = xmlNewStringInputStream(ctxt, cur); |
| 14451 | if (stream == NULL) { |
| 14452 | return (NULL); |
| 14453 | } |
| 14454 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14455 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14456 | } |
| 14457 | |
| 14458 | /** |
| 14459 | * xmlCtxtReadFile: |
| 14460 | * @ctxt: an XML parser context |
| 14461 | * @filename: a file or URL |
| 14462 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14463 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14464 | * |
| 14465 | * parse an XML file from the filesystem or the network. |
| 14466 | * This reuses the existing @ctxt parser context |
| 14467 | * |
| 14468 | * Returns the resulting document tree |
| 14469 | */ |
| 14470 | xmlDocPtr |
| 14471 | xmlCtxtReadFile(xmlParserCtxtPtr ctxt, const char *filename, |
| 14472 | const char *encoding, int options) |
| 14473 | { |
| 14474 | xmlParserInputPtr stream; |
| 14475 | |
| 14476 | if (filename == NULL) |
| 14477 | return (NULL); |
| 14478 | if (ctxt == NULL) |
| 14479 | return (NULL); |
| 14480 | |
| 14481 | xmlCtxtReset(ctxt); |
| 14482 | |
Daniel Veillard | 29614c7 | 2004-11-26 10:47:26 +0000 | [diff] [blame] | 14483 | stream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14484 | if (stream == NULL) { |
| 14485 | return (NULL); |
| 14486 | } |
| 14487 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14488 | return (xmlDoRead(ctxt, NULL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14489 | } |
| 14490 | |
| 14491 | /** |
| 14492 | * xmlCtxtReadMemory: |
| 14493 | * @ctxt: an XML parser context |
| 14494 | * @buffer: a pointer to a char array |
| 14495 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14496 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14497 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14498 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14499 | * |
| 14500 | * parse an XML in-memory document and build a tree. |
| 14501 | * This reuses the existing @ctxt parser context |
| 14502 | * |
| 14503 | * Returns the resulting document tree |
| 14504 | */ |
| 14505 | xmlDocPtr |
| 14506 | xmlCtxtReadMemory(xmlParserCtxtPtr ctxt, const char *buffer, int size, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14507 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14508 | { |
| 14509 | xmlParserInputBufferPtr input; |
| 14510 | xmlParserInputPtr stream; |
| 14511 | |
| 14512 | if (ctxt == NULL) |
| 14513 | return (NULL); |
| 14514 | if (buffer == NULL) |
| 14515 | return (NULL); |
| 14516 | |
| 14517 | xmlCtxtReset(ctxt); |
| 14518 | |
| 14519 | input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
| 14520 | if (input == NULL) { |
| 14521 | return(NULL); |
| 14522 | } |
| 14523 | |
| 14524 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 14525 | if (stream == NULL) { |
| 14526 | xmlFreeParserInputBuffer(input); |
| 14527 | return(NULL); |
| 14528 | } |
| 14529 | |
| 14530 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14531 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14532 | } |
| 14533 | |
| 14534 | /** |
| 14535 | * xmlCtxtReadFd: |
| 14536 | * @ctxt: an XML parser context |
| 14537 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14538 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14539 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14540 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14541 | * |
| 14542 | * parse an XML from a file descriptor and build a tree. |
| 14543 | * This reuses the existing @ctxt parser context |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 14544 | * NOTE that the file descriptor will not be closed when the |
| 14545 | * reader is closed or reset. |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14546 | * |
| 14547 | * Returns the resulting document tree |
| 14548 | */ |
| 14549 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14550 | xmlCtxtReadFd(xmlParserCtxtPtr ctxt, int fd, |
| 14551 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14552 | { |
| 14553 | xmlParserInputBufferPtr input; |
| 14554 | xmlParserInputPtr stream; |
| 14555 | |
| 14556 | if (fd < 0) |
| 14557 | return (NULL); |
| 14558 | if (ctxt == NULL) |
| 14559 | return (NULL); |
| 14560 | |
| 14561 | xmlCtxtReset(ctxt); |
| 14562 | |
| 14563 | |
| 14564 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 14565 | if (input == NULL) |
| 14566 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 14567 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14568 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 14569 | if (stream == NULL) { |
| 14570 | xmlFreeParserInputBuffer(input); |
| 14571 | return (NULL); |
| 14572 | } |
| 14573 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14574 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14575 | } |
| 14576 | |
| 14577 | /** |
| 14578 | * xmlCtxtReadIO: |
| 14579 | * @ctxt: an XML parser context |
| 14580 | * @ioread: an I/O read function |
| 14581 | * @ioclose: an I/O close function |
| 14582 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14583 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14584 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 14585 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14586 | * |
| 14587 | * parse an XML document from I/O functions and source and build a tree. |
| 14588 | * This reuses the existing @ctxt parser context |
| 14589 | * |
| 14590 | * Returns the resulting document tree |
| 14591 | */ |
| 14592 | xmlDocPtr |
| 14593 | xmlCtxtReadIO(xmlParserCtxtPtr ctxt, xmlInputReadCallback ioread, |
| 14594 | xmlInputCloseCallback ioclose, void *ioctx, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14595 | const char *URL, |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14596 | const char *encoding, int options) |
| 14597 | { |
| 14598 | xmlParserInputBufferPtr input; |
| 14599 | xmlParserInputPtr stream; |
| 14600 | |
| 14601 | if (ioread == NULL) |
| 14602 | return (NULL); |
| 14603 | if (ctxt == NULL) |
| 14604 | return (NULL); |
| 14605 | |
| 14606 | xmlCtxtReset(ctxt); |
| 14607 | |
| 14608 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 14609 | XML_CHAR_ENCODING_NONE); |
| 14610 | if (input == NULL) |
| 14611 | return (NULL); |
| 14612 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 14613 | if (stream == NULL) { |
| 14614 | xmlFreeParserInputBuffer(input); |
| 14615 | return (NULL); |
| 14616 | } |
| 14617 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 14618 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 14619 | } |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 14620 | |
| 14621 | #define bottom_parser |
| 14622 | #include "elfgcchack.h" |