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 | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 83 | /** |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 84 | * xmlParserMaxDepth: |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 85 | * |
| 86 | * arbitrary depth limit for the XML documents that we allow to |
| 87 | * process. This is not a limitation of the parser but a safety |
| 88 | * boundary feature. |
| 89 | */ |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 90 | unsigned int xmlParserMaxDepth = 1024; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 91 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 92 | #define SAX2 1 |
| 93 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 94 | #define XML_PARSER_BIG_BUFFER_SIZE 300 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 95 | #define XML_PARSER_BUFFER_SIZE 100 |
| 96 | |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 97 | #define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document" |
| 98 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 99 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 100 | * List of XML prefixed PI allowed by W3C specs |
| 101 | */ |
| 102 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 103 | static const char *xmlW3CPIs[] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 104 | "xml-stylesheet", |
| 105 | NULL |
| 106 | }; |
| 107 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 108 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 109 | /* DEPR void xmlParserHandleReference(xmlParserCtxtPtr ctxt); */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 110 | xmlEntityPtr xmlParseStringPEReference(xmlParserCtxtPtr ctxt, |
| 111 | const xmlChar **str); |
| 112 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 113 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 114 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 115 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 116 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 117 | const xmlChar *ID, xmlNodePtr *list); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 118 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 119 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 120 | static void |
| 121 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 122 | xmlNodePtr lastNode); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 123 | #endif /* LIBXML_LEGACY_ENABLED */ |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 124 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 125 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 126 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 127 | const xmlChar *string, void *user_data, xmlNodePtr *lst); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 128 | |
| 129 | /************************************************************************ |
| 130 | * * |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 131 | * Some factorized error routines * |
| 132 | * * |
| 133 | ************************************************************************/ |
| 134 | |
| 135 | /** |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 136 | * xmlErrAttributeDup: |
| 137 | * @ctxt: an XML parser context |
| 138 | * @prefix: the attribute prefix |
| 139 | * @localname: the attribute localname |
| 140 | * |
| 141 | * Handle a redefinition of attribute error |
| 142 | */ |
| 143 | static void |
| 144 | xmlErrAttributeDup(xmlParserCtxtPtr ctxt, const xmlChar * prefix, |
| 145 | const xmlChar * localname) |
| 146 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 147 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 148 | (ctxt->instate == XML_PARSER_EOF)) |
| 149 | return; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 150 | ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 151 | if (prefix == NULL) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 152 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 153 | ctxt->errNo, XML_ERR_FATAL, NULL, 0, |
| 154 | (const char *) localname, NULL, NULL, 0, 0, |
| 155 | "Attribute %s redefined\n", localname); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 156 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 157 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 158 | ctxt->errNo, XML_ERR_FATAL, NULL, 0, |
| 159 | (const char *) prefix, (const char *) localname, |
| 160 | NULL, 0, 0, "Attribute %s:%s redefined\n", prefix, |
| 161 | localname); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 162 | ctxt->wellFormed = 0; |
| 163 | if (ctxt->recovery == 0) |
| 164 | ctxt->disableSAX = 1; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * xmlFatalErr: |
| 169 | * @ctxt: an XML parser context |
| 170 | * @error: the error number |
| 171 | * @extra: extra information string |
| 172 | * |
| 173 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 174 | */ |
| 175 | static void |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 176 | xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info) |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 177 | { |
| 178 | const char *errmsg; |
| 179 | |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 180 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 181 | (ctxt->instate == XML_PARSER_EOF)) |
| 182 | return; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 183 | switch (error) { |
| 184 | case XML_ERR_INVALID_HEX_CHARREF: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 185 | errmsg = "CharRef: invalid hexadecimal value\n"; |
| 186 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 187 | case XML_ERR_INVALID_DEC_CHARREF: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 188 | errmsg = "CharRef: invalid decimal value\n"; |
| 189 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 190 | case XML_ERR_INVALID_CHARREF: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 191 | errmsg = "CharRef: invalid value\n"; |
| 192 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 193 | case XML_ERR_INTERNAL_ERROR: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 194 | errmsg = "internal error"; |
| 195 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 196 | case XML_ERR_PEREF_AT_EOF: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 197 | errmsg = "PEReference at end of document\n"; |
| 198 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 199 | case XML_ERR_PEREF_IN_PROLOG: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 200 | errmsg = "PEReference in prolog\n"; |
| 201 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 202 | case XML_ERR_PEREF_IN_EPILOG: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 203 | errmsg = "PEReference in epilog\n"; |
| 204 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 205 | case XML_ERR_PEREF_NO_NAME: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 206 | errmsg = "PEReference: no name\n"; |
| 207 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 208 | case XML_ERR_PEREF_SEMICOL_MISSING: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 209 | errmsg = "PEReference: expecting ';'\n"; |
| 210 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 211 | case XML_ERR_ENTITY_LOOP: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 212 | errmsg = "Detected an entity reference loop\n"; |
| 213 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 214 | case XML_ERR_ENTITY_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 215 | errmsg = "EntityValue: \" or ' expected\n"; |
| 216 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 217 | case XML_ERR_ENTITY_PE_INTERNAL: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 218 | errmsg = "PEReferences forbidden in internal subset\n"; |
| 219 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 220 | case XML_ERR_ENTITY_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 221 | errmsg = "EntityValue: \" or ' expected\n"; |
| 222 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 223 | case XML_ERR_ATTRIBUTE_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 224 | errmsg = "AttValue: \" or ' expected\n"; |
| 225 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 226 | case XML_ERR_LT_IN_ATTRIBUTE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 227 | errmsg = "Unescaped '<' not allowed in attributes values\n"; |
| 228 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 229 | case XML_ERR_LITERAL_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 230 | errmsg = "SystemLiteral \" or ' expected\n"; |
| 231 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 232 | case XML_ERR_LITERAL_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 233 | errmsg = "Unfinished System or Public ID \" or ' expected\n"; |
| 234 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 235 | case XML_ERR_MISPLACED_CDATA_END: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 236 | errmsg = "Sequence ']]>' not allowed in content\n"; |
| 237 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 238 | case XML_ERR_URI_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 239 | errmsg = "SYSTEM or PUBLIC, the URI is missing\n"; |
| 240 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 241 | case XML_ERR_PUBID_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 242 | errmsg = "PUBLIC, the Public Identifier is missing\n"; |
| 243 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 244 | case XML_ERR_HYPHEN_IN_COMMENT: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 245 | errmsg = "Comment must not contain '--' (double-hyphen)\n"; |
| 246 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 247 | case XML_ERR_PI_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 248 | errmsg = "xmlParsePI : no target name\n"; |
| 249 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 250 | case XML_ERR_RESERVED_XML_NAME: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 251 | errmsg = "Invalid PI name\n"; |
| 252 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 253 | case XML_ERR_NOTATION_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 254 | errmsg = "NOTATION: Name expected here\n"; |
| 255 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 256 | case XML_ERR_NOTATION_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 257 | errmsg = "'>' required to close NOTATION declaration\n"; |
| 258 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 259 | case XML_ERR_VALUE_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 260 | errmsg = "Entity value required\n"; |
| 261 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 262 | case XML_ERR_URI_FRAGMENT: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 263 | errmsg = "Fragment not allowed"; |
| 264 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 265 | case XML_ERR_ATTLIST_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 266 | errmsg = "'(' required to start ATTLIST enumeration\n"; |
| 267 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 268 | case XML_ERR_NMTOKEN_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 269 | errmsg = "NmToken expected in ATTLIST enumeration\n"; |
| 270 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 271 | case XML_ERR_ATTLIST_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 272 | errmsg = "')' required to finish ATTLIST enumeration\n"; |
| 273 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 274 | case XML_ERR_MIXED_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 275 | errmsg = "MixedContentDecl : '|' or ')*' expected\n"; |
| 276 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 277 | case XML_ERR_PCDATA_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 278 | errmsg = "MixedContentDecl : '#PCDATA' expected\n"; |
| 279 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 280 | case XML_ERR_ELEMCONTENT_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 281 | errmsg = "ContentDecl : Name or '(' expected\n"; |
| 282 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 283 | case XML_ERR_ELEMCONTENT_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 284 | errmsg = "ContentDecl : ',' '|' or ')' expected\n"; |
| 285 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 286 | case XML_ERR_PEREF_IN_INT_SUBSET: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 287 | errmsg = |
| 288 | "PEReference: forbidden within markup decl in internal subset\n"; |
| 289 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 290 | case XML_ERR_GT_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 291 | errmsg = "expected '>'\n"; |
| 292 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 293 | case XML_ERR_CONDSEC_INVALID: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 294 | errmsg = "XML conditional section '[' expected\n"; |
| 295 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 296 | case XML_ERR_EXT_SUBSET_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 297 | errmsg = "Content error in the external subset\n"; |
| 298 | break; |
| 299 | case XML_ERR_CONDSEC_INVALID_KEYWORD: |
| 300 | errmsg = |
| 301 | "conditional section INCLUDE or IGNORE keyword expected\n"; |
| 302 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 303 | case XML_ERR_CONDSEC_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 304 | errmsg = "XML conditional section not closed\n"; |
| 305 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 306 | case XML_ERR_XMLDECL_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 307 | errmsg = "Text declaration '<?xml' required\n"; |
| 308 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 309 | case XML_ERR_XMLDECL_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 310 | errmsg = "parsing XML declaration: '?>' expected\n"; |
| 311 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 312 | case XML_ERR_EXT_ENTITY_STANDALONE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 313 | errmsg = "external parsed entities cannot be standalone\n"; |
| 314 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 315 | case XML_ERR_ENTITYREF_SEMICOL_MISSING: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 316 | errmsg = "EntityRef: expecting ';'\n"; |
| 317 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 318 | case XML_ERR_DOCTYPE_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 319 | errmsg = "DOCTYPE improperly terminated\n"; |
| 320 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 321 | case XML_ERR_LTSLASH_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 322 | errmsg = "EndTag: '</' not found\n"; |
| 323 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 324 | case XML_ERR_EQUAL_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 325 | errmsg = "expected '='\n"; |
| 326 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 327 | case XML_ERR_STRING_NOT_CLOSED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 328 | errmsg = "String not closed expecting \" or '\n"; |
| 329 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 330 | case XML_ERR_STRING_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 331 | errmsg = "String not started expecting ' or \"\n"; |
| 332 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 333 | case XML_ERR_ENCODING_NAME: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 334 | errmsg = "Invalid XML encoding name\n"; |
| 335 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 336 | case XML_ERR_STANDALONE_VALUE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 337 | errmsg = "standalone accepts only 'yes' or 'no'\n"; |
| 338 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 339 | case XML_ERR_DOCUMENT_EMPTY: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 340 | errmsg = "Document is empty\n"; |
| 341 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 342 | case XML_ERR_DOCUMENT_END: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 343 | errmsg = "Extra content at the end of the document\n"; |
| 344 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 345 | case XML_ERR_NOT_WELL_BALANCED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 346 | errmsg = "chunk is not well balanced\n"; |
| 347 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 348 | case XML_ERR_EXTRA_CONTENT: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 349 | errmsg = "extra content at the end of well balanced chunk\n"; |
| 350 | break; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 351 | case XML_ERR_VERSION_MISSING: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 352 | errmsg = "Malformed declaration expecting version\n"; |
| 353 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 354 | #if 0 |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 355 | case: |
| 356 | errmsg = "\n"; |
| 357 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 358 | #endif |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 359 | default: |
| 360 | errmsg = "Unregistered error message\n"; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 361 | } |
| 362 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 363 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 364 | XML_ERR_FATAL, NULL, 0, info, NULL, NULL, 0, 0, errmsg, |
| 365 | info); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 366 | ctxt->wellFormed = 0; |
| 367 | if (ctxt->recovery == 0) |
| 368 | ctxt->disableSAX = 1; |
| 369 | } |
| 370 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 371 | /** |
| 372 | * xmlFatalErrMsg: |
| 373 | * @ctxt: an XML parser context |
| 374 | * @error: the error number |
| 375 | * @msg: the error message |
| 376 | * |
| 377 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 378 | */ |
| 379 | static void |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 380 | xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 381 | const char *msg) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 382 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 383 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 384 | (ctxt->instate == XML_PARSER_EOF)) |
| 385 | return; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 386 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 387 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 388 | XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, msg); |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 389 | ctxt->wellFormed = 0; |
| 390 | if (ctxt->recovery == 0) |
| 391 | ctxt->disableSAX = 1; |
| 392 | } |
| 393 | |
| 394 | /** |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 395 | * xmlWarningMsg: |
| 396 | * @ctxt: an XML parser context |
| 397 | * @error: the error number |
| 398 | * @msg: the error message |
| 399 | * @str1: extra data |
| 400 | * @str2: extra data |
| 401 | * |
| 402 | * Handle a warning. |
| 403 | */ |
| 404 | static void |
| 405 | xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 406 | const char *msg, const xmlChar *str1, const xmlChar *str2) |
| 407 | { |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 408 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 409 | |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 410 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 411 | (ctxt->instate == XML_PARSER_EOF)) |
| 412 | return; |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 413 | if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 414 | schannel = ctxt->sax->serror; |
| 415 | __xmlRaiseError(schannel, |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 416 | (ctxt->sax) ? ctxt->sax->warning : NULL, |
| 417 | ctxt->userData, |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 418 | ctxt, NULL, XML_FROM_PARSER, error, |
| 419 | XML_ERR_WARNING, NULL, 0, |
| 420 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 421 | msg, (const char *) str1, (const char *) str2); |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * xmlValidityError: |
| 426 | * @ctxt: an XML parser context |
| 427 | * @error: the error number |
| 428 | * @msg: the error message |
| 429 | * @str1: extra data |
| 430 | * |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 431 | * Handle a validity error. |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 432 | */ |
| 433 | static void |
| 434 | xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 435 | const char *msg, const xmlChar *str1) |
| 436 | { |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 437 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 438 | |
| 439 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 440 | (ctxt->instate == XML_PARSER_EOF)) |
| 441 | return; |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 442 | ctxt->errNo = error; |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 443 | if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 444 | schannel = ctxt->sax->serror; |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 445 | __xmlRaiseError(schannel, |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 446 | ctxt->vctxt.error, ctxt->vctxt.userData, |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 447 | ctxt, NULL, XML_FROM_DTD, error, |
| 448 | XML_ERR_ERROR, NULL, 0, (const char *) str1, |
| 449 | NULL, NULL, 0, 0, |
| 450 | msg, (const char *) str1); |
| 451 | ctxt->valid = 0; |
| 452 | } |
| 453 | |
| 454 | /** |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 455 | * xmlFatalErrMsgInt: |
| 456 | * @ctxt: an XML parser context |
| 457 | * @error: the error number |
| 458 | * @msg: the error message |
| 459 | * @val: an integer value |
| 460 | * |
| 461 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 462 | */ |
| 463 | static void |
| 464 | xmlFatalErrMsgInt(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 465 | const char *msg, int val) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 466 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 467 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 468 | (ctxt->instate == XML_PARSER_EOF)) |
| 469 | return; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 470 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 471 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 472 | ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 473 | NULL, 0, NULL, NULL, NULL, val, 0, msg, val); |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 474 | ctxt->wellFormed = 0; |
| 475 | if (ctxt->recovery == 0) |
| 476 | ctxt->disableSAX = 1; |
| 477 | } |
| 478 | |
| 479 | /** |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 480 | * xmlFatalErrMsgStrIntStr: |
| 481 | * @ctxt: an XML parser context |
| 482 | * @error: the error number |
| 483 | * @msg: the error message |
| 484 | * @str1: an string info |
| 485 | * @val: an integer value |
| 486 | * @str2: an string info |
| 487 | * |
| 488 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 489 | */ |
| 490 | static void |
| 491 | xmlFatalErrMsgStrIntStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 492 | const char *msg, const xmlChar *str1, int val, |
| 493 | const xmlChar *str2) |
| 494 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 495 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 496 | (ctxt->instate == XML_PARSER_EOF)) |
| 497 | return; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 498 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 499 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 500 | ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 501 | NULL, 0, (const char *) str1, (const char *) str2, |
| 502 | NULL, val, 0, msg, str1, val, str2); |
| 503 | ctxt->wellFormed = 0; |
| 504 | if (ctxt->recovery == 0) |
| 505 | ctxt->disableSAX = 1; |
| 506 | } |
| 507 | |
| 508 | /** |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 509 | * xmlFatalErrMsgStr: |
| 510 | * @ctxt: an XML parser context |
| 511 | * @error: the error number |
| 512 | * @msg: the error message |
| 513 | * @val: a string value |
| 514 | * |
| 515 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 516 | */ |
| 517 | static void |
| 518 | xmlFatalErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 519 | const char *msg, const xmlChar * val) |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 520 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 521 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 522 | (ctxt->instate == XML_PARSER_EOF)) |
| 523 | return; |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 524 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 525 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 526 | XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 527 | NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg, |
| 528 | val); |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 529 | ctxt->wellFormed = 0; |
| 530 | if (ctxt->recovery == 0) |
| 531 | ctxt->disableSAX = 1; |
| 532 | } |
| 533 | |
| 534 | /** |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 535 | * xmlErrMsgStr: |
| 536 | * @ctxt: an XML parser context |
| 537 | * @error: the error number |
| 538 | * @msg: the error message |
| 539 | * @val: a string value |
| 540 | * |
| 541 | * Handle a non fatal parser error |
| 542 | */ |
| 543 | static void |
| 544 | xmlErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 545 | const char *msg, const xmlChar * val) |
| 546 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 547 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 548 | (ctxt->instate == XML_PARSER_EOF)) |
| 549 | return; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 550 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 551 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 552 | XML_FROM_PARSER, error, XML_ERR_ERROR, |
| 553 | NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg, |
| 554 | val); |
| 555 | } |
| 556 | |
| 557 | /** |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 558 | * xmlNsErr: |
| 559 | * @ctxt: an XML parser context |
| 560 | * @error: the error number |
| 561 | * @msg: the message |
| 562 | * @info1: extra information string |
| 563 | * @info2: extra information string |
| 564 | * |
| 565 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 566 | */ |
| 567 | static void |
| 568 | xmlNsErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 569 | const char *msg, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 570 | const xmlChar * info1, const xmlChar * info2, |
| 571 | const xmlChar * info3) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 572 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 573 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 574 | (ctxt->instate == XML_PARSER_EOF)) |
| 575 | return; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 576 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 577 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 578 | XML_ERR_ERROR, NULL, 0, (const char *) info1, |
| 579 | (const char *) info2, (const char *) info3, 0, 0, msg, |
| 580 | info1, info2, info3); |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 581 | ctxt->nsWellFormed = 0; |
| 582 | } |
| 583 | |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 584 | /************************************************************************ |
| 585 | * * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 586 | * SAX2 defaulted attributes handling * |
| 587 | * * |
| 588 | ************************************************************************/ |
| 589 | |
| 590 | /** |
| 591 | * xmlDetectSAX2: |
| 592 | * @ctxt: an XML parser context |
| 593 | * |
| 594 | * Do the SAX2 detection and specific intialization |
| 595 | */ |
| 596 | static void |
| 597 | xmlDetectSAX2(xmlParserCtxtPtr ctxt) { |
| 598 | if (ctxt == NULL) return; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 599 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 600 | if ((ctxt->sax) && (ctxt->sax->initialized == XML_SAX2_MAGIC) && |
| 601 | ((ctxt->sax->startElementNs != NULL) || |
| 602 | (ctxt->sax->endElementNs != NULL))) ctxt->sax2 = 1; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 603 | #else |
| 604 | ctxt->sax2 = 1; |
| 605 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 606 | |
| 607 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 608 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 609 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
William M. Brack | 9f797ab | 2004-07-28 07:40:12 +0000 | [diff] [blame] | 610 | if ((ctxt->str_xml==NULL) || (ctxt->str_xmlns==NULL) || |
| 611 | (ctxt->str_xml_ns == NULL)) { |
| 612 | xmlErrMemory(ctxt, NULL); |
| 613 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 616 | typedef struct _xmlDefAttrs xmlDefAttrs; |
| 617 | typedef xmlDefAttrs *xmlDefAttrsPtr; |
| 618 | struct _xmlDefAttrs { |
| 619 | int nbAttrs; /* number of defaulted attributes on that element */ |
| 620 | int maxAttrs; /* the size of the array */ |
| 621 | const xmlChar *values[4]; /* array of localname/prefix/values */ |
| 622 | }; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 623 | |
| 624 | /** |
| 625 | * xmlAddDefAttrs: |
| 626 | * @ctxt: an XML parser context |
| 627 | * @fullname: the element fullname |
| 628 | * @fullattr: the attribute fullname |
| 629 | * @value: the attribute value |
| 630 | * |
| 631 | * Add a defaulted attribute for an element |
| 632 | */ |
| 633 | static void |
| 634 | xmlAddDefAttrs(xmlParserCtxtPtr ctxt, |
| 635 | const xmlChar *fullname, |
| 636 | const xmlChar *fullattr, |
| 637 | const xmlChar *value) { |
| 638 | xmlDefAttrsPtr defaults; |
| 639 | int len; |
| 640 | const xmlChar *name; |
| 641 | const xmlChar *prefix; |
| 642 | |
| 643 | if (ctxt->attsDefault == NULL) { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 644 | ctxt->attsDefault = xmlHashCreateDict(10, ctxt->dict); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 645 | if (ctxt->attsDefault == NULL) |
| 646 | goto mem_error; |
| 647 | } |
| 648 | |
| 649 | /* |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 650 | * split the element name into prefix:localname , the string found |
| 651 | * are within the DTD and then not associated to namespace names. |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 652 | */ |
| 653 | name = xmlSplitQName3(fullname, &len); |
| 654 | if (name == NULL) { |
| 655 | name = xmlDictLookup(ctxt->dict, fullname, -1); |
| 656 | prefix = NULL; |
| 657 | } else { |
| 658 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 659 | prefix = xmlDictLookup(ctxt->dict, fullname, len); |
| 660 | } |
| 661 | |
| 662 | /* |
| 663 | * make sure there is some storage |
| 664 | */ |
| 665 | defaults = xmlHashLookup2(ctxt->attsDefault, name, prefix); |
| 666 | if (defaults == NULL) { |
| 667 | defaults = (xmlDefAttrsPtr) xmlMalloc(sizeof(xmlDefAttrs) + |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 668 | (4 * 4) * sizeof(const xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 669 | if (defaults == NULL) |
| 670 | goto mem_error; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 671 | defaults->nbAttrs = 0; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 672 | defaults->maxAttrs = 4; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 673 | xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, defaults, NULL); |
| 674 | } else if (defaults->nbAttrs >= defaults->maxAttrs) { |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 675 | xmlDefAttrsPtr temp; |
| 676 | |
| 677 | temp = (xmlDefAttrsPtr) xmlRealloc(defaults, sizeof(xmlDefAttrs) + |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 678 | (2 * defaults->maxAttrs * 4) * sizeof(const xmlChar *)); |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 679 | if (temp == NULL) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 680 | goto mem_error; |
Daniel Veillard | 079f6a7 | 2004-09-23 13:15:03 +0000 | [diff] [blame] | 681 | defaults = temp; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 682 | defaults->maxAttrs *= 2; |
| 683 | xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, defaults, NULL); |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * plit the element name into prefix:localname , the string found |
| 688 | * are within the DTD and hen not associated to namespace names. |
| 689 | */ |
| 690 | name = xmlSplitQName3(fullattr, &len); |
| 691 | if (name == NULL) { |
| 692 | name = xmlDictLookup(ctxt->dict, fullattr, -1); |
| 693 | prefix = NULL; |
| 694 | } else { |
| 695 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 696 | prefix = xmlDictLookup(ctxt->dict, fullattr, len); |
| 697 | } |
| 698 | |
| 699 | defaults->values[4 * defaults->nbAttrs] = name; |
| 700 | defaults->values[4 * defaults->nbAttrs + 1] = prefix; |
| 701 | /* intern the string and precompute the end */ |
| 702 | len = xmlStrlen(value); |
| 703 | value = xmlDictLookup(ctxt->dict, value, len); |
| 704 | defaults->values[4 * defaults->nbAttrs + 2] = value; |
| 705 | defaults->values[4 * defaults->nbAttrs + 3] = value + len; |
| 706 | defaults->nbAttrs++; |
| 707 | |
| 708 | return; |
| 709 | |
| 710 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 711 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 712 | return; |
| 713 | } |
| 714 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 715 | /** |
| 716 | * xmlAddSpecialAttr: |
| 717 | * @ctxt: an XML parser context |
| 718 | * @fullname: the element fullname |
| 719 | * @fullattr: the attribute fullname |
| 720 | * @type: the attribute type |
| 721 | * |
| 722 | * Register that this attribute is not CDATA |
| 723 | */ |
| 724 | static void |
| 725 | xmlAddSpecialAttr(xmlParserCtxtPtr ctxt, |
| 726 | const xmlChar *fullname, |
| 727 | const xmlChar *fullattr, |
| 728 | int type) |
| 729 | { |
| 730 | if (ctxt->attsSpecial == NULL) { |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 731 | ctxt->attsSpecial = xmlHashCreateDict(10, ctxt->dict); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 732 | if (ctxt->attsSpecial == NULL) |
| 733 | goto mem_error; |
| 734 | } |
| 735 | |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 736 | xmlHashAddEntry2(ctxt->attsSpecial, fullname, fullattr, |
| 737 | (void *) (long) type); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 738 | return; |
| 739 | |
| 740 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 741 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 742 | return; |
| 743 | } |
| 744 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 745 | /** |
| 746 | * xmlCheckLanguageID: |
| 747 | * @lang: pointer to the string value |
| 748 | * |
| 749 | * Checks that the value conforms to the LanguageID production: |
| 750 | * |
| 751 | * NOTE: this is somewhat deprecated, those productions were removed from |
| 752 | * the XML Second edition. |
| 753 | * |
| 754 | * [33] LanguageID ::= Langcode ('-' Subcode)* |
| 755 | * [34] Langcode ::= ISO639Code | IanaCode | UserCode |
| 756 | * [35] ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z]) |
| 757 | * [36] IanaCode ::= ('i' | 'I') '-' ([a-z] | [A-Z])+ |
| 758 | * [37] UserCode ::= ('x' | 'X') '-' ([a-z] | [A-Z])+ |
| 759 | * [38] Subcode ::= ([a-z] | [A-Z])+ |
| 760 | * |
| 761 | * Returns 1 if correct 0 otherwise |
| 762 | **/ |
| 763 | int |
| 764 | xmlCheckLanguageID(const xmlChar * lang) |
| 765 | { |
| 766 | const xmlChar *cur = lang; |
| 767 | |
| 768 | if (cur == NULL) |
| 769 | return (0); |
| 770 | if (((cur[0] == 'i') && (cur[1] == '-')) || |
| 771 | ((cur[0] == 'I') && (cur[1] == '-'))) { |
| 772 | /* |
| 773 | * IANA code |
| 774 | */ |
| 775 | cur += 2; |
| 776 | while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */ |
| 777 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 778 | cur++; |
| 779 | } else if (((cur[0] == 'x') && (cur[1] == '-')) || |
| 780 | ((cur[0] == 'X') && (cur[1] == '-'))) { |
| 781 | /* |
| 782 | * User code |
| 783 | */ |
| 784 | cur += 2; |
| 785 | while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */ |
| 786 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 787 | cur++; |
| 788 | } else if (((cur[0] >= 'A') && (cur[0] <= 'Z')) || |
| 789 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) { |
| 790 | /* |
| 791 | * ISO639 |
| 792 | */ |
| 793 | cur++; |
| 794 | if (((cur[0] >= 'A') && (cur[0] <= 'Z')) || |
| 795 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 796 | cur++; |
| 797 | else |
| 798 | return (0); |
| 799 | } else |
| 800 | return (0); |
| 801 | while (cur[0] != 0) { /* non input consuming */ |
| 802 | if (cur[0] != '-') |
| 803 | return (0); |
| 804 | cur++; |
| 805 | if (((cur[0] >= 'A') && (cur[0] <= 'Z')) || |
| 806 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 807 | cur++; |
| 808 | else |
| 809 | return (0); |
| 810 | while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */ |
| 811 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 812 | cur++; |
| 813 | } |
| 814 | return (1); |
| 815 | } |
| 816 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 817 | /************************************************************************ |
| 818 | * * |
| 819 | * Parser stacks related functions and macros * |
| 820 | * * |
| 821 | ************************************************************************/ |
| 822 | |
| 823 | xmlEntityPtr xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, |
| 824 | const xmlChar ** str); |
| 825 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 826 | #ifdef SAX2 |
| 827 | /** |
| 828 | * nsPush: |
| 829 | * @ctxt: an XML parser context |
| 830 | * @prefix: the namespace prefix or NULL |
| 831 | * @URL: the namespace name |
| 832 | * |
| 833 | * Pushes a new parser namespace on top of the ns stack |
| 834 | * |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 835 | * Returns -1 in case of error, -2 if the namespace should be discarded |
| 836 | * and the index in the stack otherwise. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 837 | */ |
| 838 | static int |
| 839 | nsPush(xmlParserCtxtPtr ctxt, const xmlChar *prefix, const xmlChar *URL) |
| 840 | { |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 841 | if (ctxt->options & XML_PARSE_NSCLEAN) { |
| 842 | int i; |
| 843 | for (i = 0;i < ctxt->nsNr;i += 2) { |
| 844 | if (ctxt->nsTab[i] == prefix) { |
| 845 | /* in scope */ |
| 846 | if (ctxt->nsTab[i + 1] == URL) |
| 847 | return(-2); |
| 848 | /* out of scope keep it */ |
| 849 | break; |
| 850 | } |
| 851 | } |
| 852 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 853 | if ((ctxt->nsMax == 0) || (ctxt->nsTab == NULL)) { |
| 854 | ctxt->nsMax = 10; |
| 855 | ctxt->nsNr = 0; |
| 856 | ctxt->nsTab = (const xmlChar **) |
| 857 | xmlMalloc(ctxt->nsMax * sizeof(xmlChar *)); |
| 858 | if (ctxt->nsTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 859 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 860 | ctxt->nsMax = 0; |
| 861 | return (-1); |
| 862 | } |
| 863 | } else if (ctxt->nsNr >= ctxt->nsMax) { |
| 864 | ctxt->nsMax *= 2; |
| 865 | ctxt->nsTab = (const xmlChar **) |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 866 | xmlRealloc((char *) ctxt->nsTab, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 867 | ctxt->nsMax * sizeof(ctxt->nsTab[0])); |
| 868 | if (ctxt->nsTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 869 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 870 | ctxt->nsMax /= 2; |
| 871 | return (-1); |
| 872 | } |
| 873 | } |
| 874 | ctxt->nsTab[ctxt->nsNr++] = prefix; |
| 875 | ctxt->nsTab[ctxt->nsNr++] = URL; |
| 876 | return (ctxt->nsNr); |
| 877 | } |
| 878 | /** |
| 879 | * nsPop: |
| 880 | * @ctxt: an XML parser context |
| 881 | * @nr: the number to pop |
| 882 | * |
| 883 | * Pops the top @nr parser prefix/namespace from the ns stack |
| 884 | * |
| 885 | * Returns the number of namespaces removed |
| 886 | */ |
| 887 | static int |
| 888 | nsPop(xmlParserCtxtPtr ctxt, int nr) |
| 889 | { |
| 890 | int i; |
| 891 | |
| 892 | if (ctxt->nsTab == NULL) return(0); |
| 893 | if (ctxt->nsNr < nr) { |
| 894 | xmlGenericError(xmlGenericErrorContext, "Pbm popping %d NS\n", nr); |
| 895 | nr = ctxt->nsNr; |
| 896 | } |
| 897 | if (ctxt->nsNr <= 0) |
| 898 | return (0); |
| 899 | |
| 900 | for (i = 0;i < nr;i++) { |
| 901 | ctxt->nsNr--; |
| 902 | ctxt->nsTab[ctxt->nsNr] = NULL; |
| 903 | } |
| 904 | return(nr); |
| 905 | } |
| 906 | #endif |
| 907 | |
| 908 | static int |
| 909 | xmlCtxtGrowAttrs(xmlParserCtxtPtr ctxt, int nr) { |
| 910 | const xmlChar **atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 911 | int *attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 912 | int maxatts; |
| 913 | |
| 914 | if (ctxt->atts == NULL) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 915 | maxatts = 55; /* allow for 10 attrs by default */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 916 | atts = (const xmlChar **) |
| 917 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 918 | if (atts == NULL) goto mem_error; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 919 | ctxt->atts = atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 920 | attallocs = (int *) xmlMalloc((maxatts / 5) * sizeof(int)); |
| 921 | if (attallocs == NULL) goto mem_error; |
| 922 | ctxt->attallocs = attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 923 | ctxt->maxatts = maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 924 | } else if (nr + 5 > ctxt->maxatts) { |
| 925 | maxatts = (nr + 5) * 2; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 926 | atts = (const xmlChar **) xmlRealloc((void *) ctxt->atts, |
| 927 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 928 | if (atts == NULL) goto mem_error; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 929 | ctxt->atts = atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 930 | attallocs = (int *) xmlRealloc((void *) ctxt->attallocs, |
| 931 | (maxatts / 5) * sizeof(int)); |
| 932 | if (attallocs == NULL) goto mem_error; |
| 933 | ctxt->attallocs = attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 934 | ctxt->maxatts = maxatts; |
| 935 | } |
| 936 | return(ctxt->maxatts); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 937 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 938 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 939 | return(-1); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 942 | /** |
| 943 | * inputPush: |
| 944 | * @ctxt: an XML parser context |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 945 | * @value: the parser input |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 946 | * |
| 947 | * Pushes a new parser input on top of the input stack |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 948 | * |
| 949 | * Returns 0 in case of error, the index in the stack otherwise |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 950 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 951 | int |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 952 | inputPush(xmlParserCtxtPtr ctxt, xmlParserInputPtr value) |
| 953 | { |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 954 | if ((ctxt == NULL) || (value == NULL)) |
| 955 | return(0); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 956 | if (ctxt->inputNr >= ctxt->inputMax) { |
| 957 | ctxt->inputMax *= 2; |
| 958 | ctxt->inputTab = |
| 959 | (xmlParserInputPtr *) xmlRealloc(ctxt->inputTab, |
| 960 | ctxt->inputMax * |
| 961 | sizeof(ctxt->inputTab[0])); |
| 962 | if (ctxt->inputTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 963 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 964 | return (0); |
| 965 | } |
| 966 | } |
| 967 | ctxt->inputTab[ctxt->inputNr] = value; |
| 968 | ctxt->input = value; |
| 969 | return (ctxt->inputNr++); |
| 970 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 971 | /** |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 972 | * inputPop: |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 973 | * @ctxt: an XML parser context |
| 974 | * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 975 | * Pops the top parser input from the input stack |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 976 | * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 977 | * Returns the input just removed |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 978 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 979 | xmlParserInputPtr |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 980 | inputPop(xmlParserCtxtPtr ctxt) |
| 981 | { |
| 982 | xmlParserInputPtr ret; |
| 983 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 984 | if (ctxt == NULL) |
| 985 | return(NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 986 | if (ctxt->inputNr <= 0) |
| 987 | return (0); |
| 988 | ctxt->inputNr--; |
| 989 | if (ctxt->inputNr > 0) |
| 990 | ctxt->input = ctxt->inputTab[ctxt->inputNr - 1]; |
| 991 | else |
| 992 | ctxt->input = NULL; |
| 993 | ret = ctxt->inputTab[ctxt->inputNr]; |
| 994 | ctxt->inputTab[ctxt->inputNr] = 0; |
| 995 | return (ret); |
| 996 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 997 | /** |
| 998 | * nodePush: |
| 999 | * @ctxt: an XML parser context |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1000 | * @value: the element node |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1001 | * |
| 1002 | * Pushes a new element node on top of the node stack |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 1003 | * |
| 1004 | * Returns 0 in case of error, the index in the stack otherwise |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 1005 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1006 | int |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1007 | nodePush(xmlParserCtxtPtr ctxt, xmlNodePtr value) |
| 1008 | { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1009 | if (ctxt == NULL) return(0); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1010 | if (ctxt->nodeNr >= ctxt->nodeMax) { |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1011 | xmlNodePtr *tmp; |
| 1012 | |
| 1013 | tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab, |
| 1014 | ctxt->nodeMax * 2 * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1015 | sizeof(ctxt->nodeTab[0])); |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1016 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1017 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1018 | return (0); |
| 1019 | } |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 1020 | ctxt->nodeTab = tmp; |
| 1021 | ctxt->nodeMax *= 2; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1022 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 1023 | if (((unsigned int) ctxt->nodeNr) > xmlParserMaxDepth) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 1024 | xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 1025 | "Excessive depth in document: change xmlParserMaxDepth = %d\n", |
| 1026 | xmlParserMaxDepth); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 1027 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 1028 | return(0); |
| 1029 | } |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1030 | ctxt->nodeTab[ctxt->nodeNr] = value; |
| 1031 | ctxt->node = value; |
| 1032 | return (ctxt->nodeNr++); |
| 1033 | } |
| 1034 | /** |
| 1035 | * nodePop: |
| 1036 | * @ctxt: an XML parser context |
| 1037 | * |
| 1038 | * Pops the top element node from the node stack |
| 1039 | * |
| 1040 | * Returns the node just removed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1041 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1042 | xmlNodePtr |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1043 | nodePop(xmlParserCtxtPtr ctxt) |
| 1044 | { |
| 1045 | xmlNodePtr ret; |
| 1046 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1047 | if (ctxt == NULL) return(NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1048 | if (ctxt->nodeNr <= 0) |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1049 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1050 | ctxt->nodeNr--; |
| 1051 | if (ctxt->nodeNr > 0) |
| 1052 | ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1]; |
| 1053 | else |
| 1054 | ctxt->node = NULL; |
| 1055 | ret = ctxt->nodeTab[ctxt->nodeNr]; |
| 1056 | ctxt->nodeTab[ctxt->nodeNr] = 0; |
| 1057 | return (ret); |
| 1058 | } |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1059 | |
| 1060 | #ifdef LIBXML_PUSH_ENABLED |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1061 | /** |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1062 | * nameNsPush: |
| 1063 | * @ctxt: an XML parser context |
| 1064 | * @value: the element name |
| 1065 | * @prefix: the element prefix |
| 1066 | * @URI: the element namespace name |
| 1067 | * |
| 1068 | * Pushes a new element name/prefix/URL on top of the name stack |
| 1069 | * |
| 1070 | * Returns -1 in case of error, the index in the stack otherwise |
| 1071 | */ |
| 1072 | static int |
| 1073 | nameNsPush(xmlParserCtxtPtr ctxt, const xmlChar * value, |
| 1074 | const xmlChar *prefix, const xmlChar *URI, int nsNr) |
| 1075 | { |
| 1076 | if (ctxt->nameNr >= ctxt->nameMax) { |
| 1077 | const xmlChar * *tmp; |
| 1078 | void **tmp2; |
| 1079 | ctxt->nameMax *= 2; |
| 1080 | tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab, |
| 1081 | ctxt->nameMax * |
| 1082 | sizeof(ctxt->nameTab[0])); |
| 1083 | if (tmp == NULL) { |
| 1084 | ctxt->nameMax /= 2; |
| 1085 | goto mem_error; |
| 1086 | } |
| 1087 | ctxt->nameTab = tmp; |
| 1088 | tmp2 = (void **) xmlRealloc((void * *)ctxt->pushTab, |
| 1089 | ctxt->nameMax * 3 * |
| 1090 | sizeof(ctxt->pushTab[0])); |
| 1091 | if (tmp2 == NULL) { |
| 1092 | ctxt->nameMax /= 2; |
| 1093 | goto mem_error; |
| 1094 | } |
| 1095 | ctxt->pushTab = tmp2; |
| 1096 | } |
| 1097 | ctxt->nameTab[ctxt->nameNr] = value; |
| 1098 | ctxt->name = value; |
| 1099 | ctxt->pushTab[ctxt->nameNr * 3] = (void *) prefix; |
| 1100 | ctxt->pushTab[ctxt->nameNr * 3 + 1] = (void *) URI; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 1101 | ctxt->pushTab[ctxt->nameNr * 3 + 2] = (void *) (long) nsNr; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1102 | return (ctxt->nameNr++); |
| 1103 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1104 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1105 | return (-1); |
| 1106 | } |
| 1107 | /** |
| 1108 | * nameNsPop: |
| 1109 | * @ctxt: an XML parser context |
| 1110 | * |
| 1111 | * Pops the top element/prefix/URI name from the name stack |
| 1112 | * |
| 1113 | * Returns the name just removed |
| 1114 | */ |
| 1115 | static const xmlChar * |
| 1116 | nameNsPop(xmlParserCtxtPtr ctxt) |
| 1117 | { |
| 1118 | const xmlChar *ret; |
| 1119 | |
| 1120 | if (ctxt->nameNr <= 0) |
| 1121 | return (0); |
| 1122 | ctxt->nameNr--; |
| 1123 | if (ctxt->nameNr > 0) |
| 1124 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 1125 | else |
| 1126 | ctxt->name = NULL; |
| 1127 | ret = ctxt->nameTab[ctxt->nameNr]; |
| 1128 | ctxt->nameTab[ctxt->nameNr] = NULL; |
| 1129 | return (ret); |
| 1130 | } |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1131 | #endif /* LIBXML_PUSH_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1132 | |
| 1133 | /** |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1134 | * namePush: |
| 1135 | * @ctxt: an XML parser context |
| 1136 | * @value: the element name |
| 1137 | * |
| 1138 | * Pushes a new element name on top of the name stack |
| 1139 | * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1140 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1141 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1142 | int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1143 | namePush(xmlParserCtxtPtr ctxt, const xmlChar * value) |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1144 | { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1145 | if (ctxt == NULL) return (-1); |
| 1146 | |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1147 | if (ctxt->nameNr >= ctxt->nameMax) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1148 | const xmlChar * *tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1149 | ctxt->nameMax *= 2; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1150 | tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab, |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1151 | ctxt->nameMax * |
| 1152 | sizeof(ctxt->nameTab[0])); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1153 | if (tmp == NULL) { |
| 1154 | ctxt->nameMax /= 2; |
| 1155 | goto mem_error; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1156 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1157 | ctxt->nameTab = tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1158 | } |
| 1159 | ctxt->nameTab[ctxt->nameNr] = value; |
| 1160 | ctxt->name = value; |
| 1161 | return (ctxt->nameNr++); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1162 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1163 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1164 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1165 | } |
| 1166 | /** |
| 1167 | * namePop: |
| 1168 | * @ctxt: an XML parser context |
| 1169 | * |
| 1170 | * Pops the top element name from the name stack |
| 1171 | * |
| 1172 | * Returns the name just removed |
| 1173 | */ |
Daniel Veillard | 7a5e0dd | 2004-09-17 08:45:25 +0000 | [diff] [blame] | 1174 | const xmlChar * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1175 | namePop(xmlParserCtxtPtr ctxt) |
| 1176 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1177 | const xmlChar *ret; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1178 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 1179 | if ((ctxt == NULL) || (ctxt->nameNr <= 0)) |
| 1180 | return (NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1181 | ctxt->nameNr--; |
| 1182 | if (ctxt->nameNr > 0) |
| 1183 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 1184 | else |
| 1185 | ctxt->name = NULL; |
| 1186 | ret = ctxt->nameTab[ctxt->nameNr]; |
| 1187 | ctxt->nameTab[ctxt->nameNr] = 0; |
| 1188 | return (ret); |
| 1189 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1190 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1191 | static int spacePush(xmlParserCtxtPtr ctxt, int val) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1192 | if (ctxt->spaceNr >= ctxt->spaceMax) { |
| 1193 | ctxt->spaceMax *= 2; |
| 1194 | ctxt->spaceTab = (int *) xmlRealloc(ctxt->spaceTab, |
| 1195 | ctxt->spaceMax * sizeof(ctxt->spaceTab[0])); |
| 1196 | if (ctxt->spaceTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1197 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1198 | return(0); |
| 1199 | } |
| 1200 | } |
| 1201 | ctxt->spaceTab[ctxt->spaceNr] = val; |
| 1202 | ctxt->space = &ctxt->spaceTab[ctxt->spaceNr]; |
| 1203 | return(ctxt->spaceNr++); |
| 1204 | } |
| 1205 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1206 | static int spacePop(xmlParserCtxtPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1207 | int ret; |
| 1208 | if (ctxt->spaceNr <= 0) return(0); |
| 1209 | ctxt->spaceNr--; |
| 1210 | if (ctxt->spaceNr > 0) |
| 1211 | ctxt->space = &ctxt->spaceTab[ctxt->spaceNr - 1]; |
| 1212 | else |
| 1213 | ctxt->space = NULL; |
| 1214 | ret = ctxt->spaceTab[ctxt->spaceNr]; |
| 1215 | ctxt->spaceTab[ctxt->spaceNr] = -1; |
| 1216 | return(ret); |
| 1217 | } |
| 1218 | |
| 1219 | /* |
| 1220 | * Macros for accessing the content. Those should be used only by the parser, |
| 1221 | * and not exported. |
| 1222 | * |
| 1223 | * Dirty macros, i.e. one often need to make assumption on the context to |
| 1224 | * use them |
| 1225 | * |
| 1226 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 1227 | * To be used with extreme caution since operations consuming |
| 1228 | * characters may move the input buffer to a different location ! |
| 1229 | * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled |
| 1230 | * This should be used internally by the parser |
| 1231 | * only to compare to ASCII values otherwise it would break when |
| 1232 | * running with UTF-8 encoding. |
| 1233 | * RAW same as CUR but in the input buffer, bypass any token |
| 1234 | * extraction that may have been done |
| 1235 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 1236 | * to compare on ASCII based substring. |
| 1237 | * 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] | 1238 | * strings without newlines within the parser. |
| 1239 | * NEXT1(l) Skip 1 xmlChar, and must also be used only to skip 1 non-newline ASCII |
| 1240 | * defined char within the parser. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1241 | * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding |
| 1242 | * |
| 1243 | * NEXT Skip to the next character, this does the proper decoding |
| 1244 | * 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] | 1245 | * NEXTL(l) Skip the current unicode character of l xmlChars long. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1246 | * CUR_CHAR(l) returns the current unicode character (int), set l |
| 1247 | * to the number of xmlChars used for the encoding [0-5]. |
| 1248 | * CUR_SCHAR same but operate on a string instead of the context |
| 1249 | * COPY_BUF copy the current unicode char to the target buffer, increment |
| 1250 | * the index |
| 1251 | * GROW, SHRINK handling of input buffers |
| 1252 | */ |
| 1253 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1254 | #define RAW (*ctxt->input->cur) |
| 1255 | #define CUR (*ctxt->input->cur) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1256 | #define NXT(val) ctxt->input->cur[(val)] |
| 1257 | #define CUR_PTR ctxt->input->cur |
| 1258 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 1259 | #define CMP4( s, c1, c2, c3, c4 ) \ |
| 1260 | ( ((unsigned char *) s)[ 0 ] == c1 && ((unsigned char *) s)[ 1 ] == c2 && \ |
| 1261 | ((unsigned char *) s)[ 2 ] == c3 && ((unsigned char *) s)[ 3 ] == c4 ) |
| 1262 | #define CMP5( s, c1, c2, c3, c4, c5 ) \ |
| 1263 | ( CMP4( s, c1, c2, c3, c4 ) && ((unsigned char *) s)[ 4 ] == c5 ) |
| 1264 | #define CMP6( s, c1, c2, c3, c4, c5, c6 ) \ |
| 1265 | ( CMP5( s, c1, c2, c3, c4, c5 ) && ((unsigned char *) s)[ 5 ] == c6 ) |
| 1266 | #define CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) \ |
| 1267 | ( CMP6( s, c1, c2, c3, c4, c5, c6 ) && ((unsigned char *) s)[ 6 ] == c7 ) |
| 1268 | #define CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) \ |
| 1269 | ( CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) && ((unsigned char *) s)[ 7 ] == c8 ) |
| 1270 | #define CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) \ |
| 1271 | ( CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) && \ |
| 1272 | ((unsigned char *) s)[ 8 ] == c9 ) |
| 1273 | #define CMP10( s, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 ) \ |
| 1274 | ( CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) && \ |
| 1275 | ((unsigned char *) s)[ 9 ] == c10 ) |
| 1276 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1277 | #define SKIP(val) do { \ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1278 | ctxt->nbChars += (val),ctxt->input->cur += (val),ctxt->input->col+=(val); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1279 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1280 | if ((*ctxt->input->cur == 0) && \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1281 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \ |
| 1282 | xmlPopInput(ctxt); \ |
| 1283 | } while (0) |
| 1284 | |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 1285 | #define SKIPL(val) do { \ |
| 1286 | int skipl; \ |
| 1287 | for(skipl=0; skipl<val; skipl++) { \ |
| 1288 | if (*(ctxt->input->cur) == '\n') { \ |
| 1289 | ctxt->input->line++; ctxt->input->col = 1; \ |
| 1290 | } else ctxt->input->col++; \ |
| 1291 | ctxt->nbChars++; \ |
| 1292 | ctxt->input->cur++; \ |
| 1293 | } \ |
| 1294 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
| 1295 | if ((*ctxt->input->cur == 0) && \ |
| 1296 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \ |
| 1297 | xmlPopInput(ctxt); \ |
| 1298 | } while (0) |
| 1299 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1300 | #define SHRINK if ((ctxt->progressive == 0) && \ |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 1301 | (ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \ |
| 1302 | (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1303 | xmlSHRINK (ctxt); |
| 1304 | |
| 1305 | static void xmlSHRINK (xmlParserCtxtPtr ctxt) { |
| 1306 | xmlParserInputShrink(ctxt->input); |
| 1307 | if ((*ctxt->input->cur == 0) && |
| 1308 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 1309 | xmlPopInput(ctxt); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1310 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1311 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1312 | #define GROW if ((ctxt->progressive == 0) && \ |
| 1313 | (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1314 | xmlGROW (ctxt); |
| 1315 | |
| 1316 | static void xmlGROW (xmlParserCtxtPtr ctxt) { |
| 1317 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 1318 | if ((*ctxt->input->cur == 0) && |
| 1319 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 1320 | xmlPopInput(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1321 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1322 | |
| 1323 | #define SKIP_BLANKS xmlSkipBlankChars(ctxt) |
| 1324 | |
| 1325 | #define NEXT xmlNextChar(ctxt) |
| 1326 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 1327 | #define NEXT1 { \ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1328 | ctxt->input->col++; \ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 1329 | ctxt->input->cur++; \ |
| 1330 | ctxt->nbChars++; \ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1331 | if (*ctxt->input->cur == 0) \ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 1332 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); \ |
| 1333 | } |
| 1334 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1335 | #define NEXTL(l) do { \ |
| 1336 | if (*(ctxt->input->cur) == '\n') { \ |
| 1337 | ctxt->input->line++; ctxt->input->col = 1; \ |
| 1338 | } else ctxt->input->col++; \ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1339 | ctxt->input->cur += l; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1340 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1341 | } while (0) |
| 1342 | |
| 1343 | #define CUR_CHAR(l) xmlCurrentChar(ctxt, &l) |
| 1344 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l) |
| 1345 | |
| 1346 | #define COPY_BUF(l,b,i,v) \ |
| 1347 | if (l == 1) b[i++] = (xmlChar) v; \ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1348 | else i += xmlCopyCharMultiByte(&b[i],v) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1349 | |
| 1350 | /** |
| 1351 | * xmlSkipBlankChars: |
| 1352 | * @ctxt: the XML parser context |
| 1353 | * |
| 1354 | * skip all blanks character found at that point in the input streams. |
| 1355 | * It pops up finished entities in the process if allowable at that point. |
| 1356 | * |
| 1357 | * Returns the number of space chars skipped |
| 1358 | */ |
| 1359 | |
| 1360 | int |
| 1361 | xmlSkipBlankChars(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1362 | int res = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1363 | |
| 1364 | /* |
| 1365 | * It's Okay to use CUR/NEXT here since all the blanks are on |
| 1366 | * the ASCII range. |
| 1367 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1368 | if ((ctxt->inputNr == 1) && (ctxt->instate != XML_PARSER_DTD)) { |
| 1369 | const xmlChar *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1370 | /* |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1371 | * if we are in the document content, go really fast |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1372 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1373 | cur = ctxt->input->cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1374 | while (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1375 | if (*cur == '\n') { |
| 1376 | ctxt->input->line++; ctxt->input->col = 1; |
| 1377 | } |
| 1378 | cur++; |
| 1379 | res++; |
| 1380 | if (*cur == 0) { |
| 1381 | ctxt->input->cur = cur; |
| 1382 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 1383 | cur = ctxt->input->cur; |
| 1384 | } |
| 1385 | } |
| 1386 | ctxt->input->cur = cur; |
| 1387 | } else { |
| 1388 | int cur; |
| 1389 | do { |
| 1390 | cur = CUR; |
Daniel Veillard | 7da9270 | 2005-01-23 20:15:53 +0000 | [diff] [blame] | 1391 | while (IS_BLANK_CH(cur)) { /* CHECKED tstblanks.xml */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1392 | NEXT; |
| 1393 | cur = CUR; |
| 1394 | res++; |
| 1395 | } |
| 1396 | while ((cur == 0) && (ctxt->inputNr > 1) && |
| 1397 | (ctxt->instate != XML_PARSER_COMMENT)) { |
| 1398 | xmlPopInput(ctxt); |
| 1399 | cur = CUR; |
| 1400 | } |
| 1401 | /* |
| 1402 | * Need to handle support of entities branching here |
| 1403 | */ |
| 1404 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); |
| 1405 | } while (IS_BLANK(cur)); /* CHECKED tstblanks.xml */ |
| 1406 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1407 | return(res); |
| 1408 | } |
| 1409 | |
| 1410 | /************************************************************************ |
| 1411 | * * |
| 1412 | * Commodity functions to handle entities * |
| 1413 | * * |
| 1414 | ************************************************************************/ |
| 1415 | |
| 1416 | /** |
| 1417 | * xmlPopInput: |
| 1418 | * @ctxt: an XML parser context |
| 1419 | * |
| 1420 | * xmlPopInput: the current input pointed by ctxt->input came to an end |
| 1421 | * pop it and return the next char. |
| 1422 | * |
| 1423 | * Returns the current xmlChar in the parser context |
| 1424 | */ |
| 1425 | xmlChar |
| 1426 | xmlPopInput(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1427 | if ((ctxt == NULL) || (ctxt->inputNr <= 1)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1428 | if (xmlParserDebugEntities) |
| 1429 | xmlGenericError(xmlGenericErrorContext, |
| 1430 | "Popping input %d\n", ctxt->inputNr); |
| 1431 | xmlFreeInputStream(inputPop(ctxt)); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1432 | if ((*ctxt->input->cur == 0) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1433 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 1434 | return(xmlPopInput(ctxt)); |
| 1435 | return(CUR); |
| 1436 | } |
| 1437 | |
| 1438 | /** |
| 1439 | * xmlPushInput: |
| 1440 | * @ctxt: an XML parser context |
| 1441 | * @input: an XML parser input fragment (entity, XML fragment ...). |
| 1442 | * |
| 1443 | * xmlPushInput: switch to a new input stream which is stacked on top |
| 1444 | * of the previous one(s). |
| 1445 | */ |
| 1446 | void |
| 1447 | xmlPushInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr input) { |
| 1448 | if (input == NULL) return; |
| 1449 | |
| 1450 | if (xmlParserDebugEntities) { |
| 1451 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 1452 | xmlGenericError(xmlGenericErrorContext, |
| 1453 | "%s(%d): ", ctxt->input->filename, |
| 1454 | ctxt->input->line); |
| 1455 | xmlGenericError(xmlGenericErrorContext, |
| 1456 | "Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur); |
| 1457 | } |
| 1458 | inputPush(ctxt, input); |
| 1459 | GROW; |
| 1460 | } |
| 1461 | |
| 1462 | /** |
| 1463 | * xmlParseCharRef: |
| 1464 | * @ctxt: an XML parser context |
| 1465 | * |
| 1466 | * parse Reference declarations |
| 1467 | * |
| 1468 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 1469 | * '&#x' [0-9a-fA-F]+ ';' |
| 1470 | * |
| 1471 | * [ WFC: Legal Character ] |
| 1472 | * Characters referred to using character references must match the |
| 1473 | * production for Char. |
| 1474 | * |
| 1475 | * Returns the value parsed (as an int), 0 in case of error |
| 1476 | */ |
| 1477 | int |
| 1478 | xmlParseCharRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 1479 | unsigned int val = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1480 | int count = 0; |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 1481 | unsigned int outofrange = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1482 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1483 | /* |
| 1484 | * Using RAW/CUR/NEXT is okay since we are working on ASCII range here |
| 1485 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1486 | if ((RAW == '&') && (NXT(1) == '#') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1487 | (NXT(2) == 'x')) { |
| 1488 | SKIP(3); |
| 1489 | GROW; |
| 1490 | while (RAW != ';') { /* loop blocked by count */ |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 1491 | if (count++ > 20) { |
| 1492 | count = 0; |
| 1493 | GROW; |
| 1494 | } |
| 1495 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1496 | val = val * 16 + (CUR - '0'); |
| 1497 | else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20)) |
| 1498 | val = val * 16 + (CUR - 'a') + 10; |
| 1499 | else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20)) |
| 1500 | val = val * 16 + (CUR - 'A') + 10; |
| 1501 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1502 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1503 | val = 0; |
| 1504 | break; |
| 1505 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 1506 | if (val > 0x10FFFF) |
| 1507 | outofrange = val; |
| 1508 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1509 | NEXT; |
| 1510 | count++; |
| 1511 | } |
| 1512 | if (RAW == ';') { |
| 1513 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1514 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1515 | ctxt->nbChars ++; |
| 1516 | ctxt->input->cur++; |
| 1517 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1518 | } else if ((RAW == '&') && (NXT(1) == '#')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1519 | SKIP(2); |
| 1520 | GROW; |
| 1521 | while (RAW != ';') { /* loop blocked by count */ |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 1522 | if (count++ > 20) { |
| 1523 | count = 0; |
| 1524 | GROW; |
| 1525 | } |
| 1526 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1527 | val = val * 10 + (CUR - '0'); |
| 1528 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1529 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1530 | val = 0; |
| 1531 | break; |
| 1532 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 1533 | if (val > 0x10FFFF) |
| 1534 | outofrange = val; |
| 1535 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1536 | NEXT; |
| 1537 | count++; |
| 1538 | } |
| 1539 | if (RAW == ';') { |
| 1540 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1541 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1542 | ctxt->nbChars ++; |
| 1543 | ctxt->input->cur++; |
| 1544 | } |
| 1545 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1546 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | /* |
| 1550 | * [ WFC: Legal Character ] |
| 1551 | * Characters referred to using character references must match the |
| 1552 | * production for Char. |
| 1553 | */ |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 1554 | if ((IS_CHAR(val) && (outofrange == 0))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1555 | return(val); |
| 1556 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 1557 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 1558 | "xmlParseCharRef: invalid xmlChar value %d\n", |
| 1559 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1560 | } |
| 1561 | return(0); |
| 1562 | } |
| 1563 | |
| 1564 | /** |
| 1565 | * xmlParseStringCharRef: |
| 1566 | * @ctxt: an XML parser context |
| 1567 | * @str: a pointer to an index in the string |
| 1568 | * |
| 1569 | * parse Reference declarations, variant parsing from a string rather |
| 1570 | * than an an input flow. |
| 1571 | * |
| 1572 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 1573 | * '&#x' [0-9a-fA-F]+ ';' |
| 1574 | * |
| 1575 | * [ WFC: Legal Character ] |
| 1576 | * Characters referred to using character references must match the |
| 1577 | * production for Char. |
| 1578 | * |
| 1579 | * Returns the value parsed (as an int), 0 in case of error, str will be |
| 1580 | * updated to the current value of the index |
| 1581 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1582 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1583 | xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 1584 | const xmlChar *ptr; |
| 1585 | xmlChar cur; |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 1586 | unsigned int val = 0; |
| 1587 | unsigned int outofrange = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1588 | |
| 1589 | if ((str == NULL) || (*str == NULL)) return(0); |
| 1590 | ptr = *str; |
| 1591 | cur = *ptr; |
| 1592 | if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) { |
| 1593 | ptr += 3; |
| 1594 | cur = *ptr; |
| 1595 | while (cur != ';') { /* Non input consuming loop */ |
| 1596 | if ((cur >= '0') && (cur <= '9')) |
| 1597 | val = val * 16 + (cur - '0'); |
| 1598 | else if ((cur >= 'a') && (cur <= 'f')) |
| 1599 | val = val * 16 + (cur - 'a') + 10; |
| 1600 | else if ((cur >= 'A') && (cur <= 'F')) |
| 1601 | val = val * 16 + (cur - 'A') + 10; |
| 1602 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1603 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1604 | val = 0; |
| 1605 | break; |
| 1606 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 1607 | if (val > 0x10FFFF) |
| 1608 | outofrange = val; |
| 1609 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1610 | ptr++; |
| 1611 | cur = *ptr; |
| 1612 | } |
| 1613 | if (cur == ';') |
| 1614 | ptr++; |
| 1615 | } else if ((cur == '&') && (ptr[1] == '#')){ |
| 1616 | ptr += 2; |
| 1617 | cur = *ptr; |
| 1618 | while (cur != ';') { /* Non input consuming loops */ |
| 1619 | if ((cur >= '0') && (cur <= '9')) |
| 1620 | val = val * 10 + (cur - '0'); |
| 1621 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1622 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1623 | val = 0; |
| 1624 | break; |
| 1625 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 1626 | if (val > 0x10FFFF) |
| 1627 | outofrange = val; |
| 1628 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1629 | ptr++; |
| 1630 | cur = *ptr; |
| 1631 | } |
| 1632 | if (cur == ';') |
| 1633 | ptr++; |
| 1634 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1635 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1636 | return(0); |
| 1637 | } |
| 1638 | *str = ptr; |
| 1639 | |
| 1640 | /* |
| 1641 | * [ WFC: Legal Character ] |
| 1642 | * Characters referred to using character references must match the |
| 1643 | * production for Char. |
| 1644 | */ |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 1645 | if ((IS_CHAR(val) && (outofrange == 0))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1646 | return(val); |
| 1647 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 1648 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 1649 | "xmlParseStringCharRef: invalid xmlChar value %d\n", |
| 1650 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1651 | } |
| 1652 | return(0); |
| 1653 | } |
| 1654 | |
| 1655 | /** |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1656 | * xmlNewBlanksWrapperInputStream: |
| 1657 | * @ctxt: an XML parser context |
| 1658 | * @entity: an Entity pointer |
| 1659 | * |
| 1660 | * Create a new input stream for wrapping |
| 1661 | * blanks around a PEReference |
| 1662 | * |
| 1663 | * Returns the new input stream or NULL |
| 1664 | */ |
| 1665 | |
| 1666 | static void deallocblankswrapper (xmlChar *str) {xmlFree(str);} |
| 1667 | |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 1668 | static xmlParserInputPtr |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1669 | xmlNewBlanksWrapperInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 1670 | xmlParserInputPtr input; |
| 1671 | xmlChar *buffer; |
| 1672 | size_t length; |
| 1673 | if (entity == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1674 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1675 | "xmlNewBlanksWrapperInputStream entity\n"); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1676 | return(NULL); |
| 1677 | } |
| 1678 | if (xmlParserDebugEntities) |
| 1679 | xmlGenericError(xmlGenericErrorContext, |
| 1680 | "new blanks wrapper for entity: %s\n", entity->name); |
| 1681 | input = xmlNewInputStream(ctxt); |
| 1682 | if (input == NULL) { |
| 1683 | return(NULL); |
| 1684 | } |
| 1685 | length = xmlStrlen(entity->name) + 5; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 1686 | buffer = xmlMallocAtomic(length); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1687 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1688 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1689 | return(NULL); |
| 1690 | } |
| 1691 | buffer [0] = ' '; |
| 1692 | buffer [1] = '%'; |
| 1693 | buffer [length-3] = ';'; |
| 1694 | buffer [length-2] = ' '; |
| 1695 | buffer [length-1] = 0; |
| 1696 | memcpy(buffer + 2, entity->name, length - 5); |
| 1697 | input->free = deallocblankswrapper; |
| 1698 | input->base = buffer; |
| 1699 | input->cur = buffer; |
| 1700 | input->length = length; |
| 1701 | input->end = &buffer[length]; |
| 1702 | return(input); |
| 1703 | } |
| 1704 | |
| 1705 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1706 | * xmlParserHandlePEReference: |
| 1707 | * @ctxt: the parser context |
| 1708 | * |
| 1709 | * [69] PEReference ::= '%' Name ';' |
| 1710 | * |
| 1711 | * [ WFC: No Recursion ] |
| 1712 | * A parsed entity must not contain a recursive |
| 1713 | * reference to itself, either directly or indirectly. |
| 1714 | * |
| 1715 | * [ WFC: Entity Declared ] |
| 1716 | * In a document without any DTD, a document with only an internal DTD |
| 1717 | * subset which contains no parameter entity references, or a document |
| 1718 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 1719 | * entity must precede any reference to it... |
| 1720 | * |
| 1721 | * [ VC: Entity Declared ] |
| 1722 | * In a document with an external subset or external parameter entities |
| 1723 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 1724 | * must precede any reference to it... |
| 1725 | * |
| 1726 | * [ WFC: In DTD ] |
| 1727 | * Parameter-entity references may only appear in the DTD. |
| 1728 | * NOTE: misleading but this is handled. |
| 1729 | * |
| 1730 | * A PEReference may have been detected in the current input stream |
| 1731 | * the handling is done accordingly to |
| 1732 | * http://www.w3.org/TR/REC-xml#entproc |
| 1733 | * i.e. |
| 1734 | * - Included in literal in entity values |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1735 | * - Included as Parameter Entity reference within DTDs |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1736 | */ |
| 1737 | void |
| 1738 | xmlParserHandlePEReference(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1739 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1740 | xmlEntityPtr entity = NULL; |
| 1741 | xmlParserInputPtr input; |
| 1742 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1743 | if (RAW != '%') return; |
| 1744 | switch(ctxt->instate) { |
| 1745 | case XML_PARSER_CDATA_SECTION: |
| 1746 | return; |
| 1747 | case XML_PARSER_COMMENT: |
| 1748 | return; |
| 1749 | case XML_PARSER_START_TAG: |
| 1750 | return; |
| 1751 | case XML_PARSER_END_TAG: |
| 1752 | return; |
| 1753 | case XML_PARSER_EOF: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1754 | xmlFatalErr(ctxt, XML_ERR_PEREF_AT_EOF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1755 | return; |
| 1756 | case XML_PARSER_PROLOG: |
| 1757 | case XML_PARSER_START: |
| 1758 | case XML_PARSER_MISC: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1759 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_PROLOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1760 | return; |
| 1761 | case XML_PARSER_ENTITY_DECL: |
| 1762 | case XML_PARSER_CONTENT: |
| 1763 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 1764 | case XML_PARSER_PI: |
| 1765 | case XML_PARSER_SYSTEM_LITERAL: |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 1766 | case XML_PARSER_PUBLIC_LITERAL: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1767 | /* we just ignore it there */ |
| 1768 | return; |
| 1769 | case XML_PARSER_EPILOG: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1770 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_EPILOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1771 | return; |
| 1772 | case XML_PARSER_ENTITY_VALUE: |
| 1773 | /* |
| 1774 | * NOTE: in the case of entity values, we don't do the |
| 1775 | * substitution here since we need the literal |
| 1776 | * entity value to be able to save the internal |
| 1777 | * subset of the document. |
| 1778 | * This will be handled by xmlStringDecodeEntities |
| 1779 | */ |
| 1780 | return; |
| 1781 | case XML_PARSER_DTD: |
| 1782 | /* |
| 1783 | * [WFC: Well-Formedness Constraint: PEs in Internal Subset] |
| 1784 | * In the internal DTD subset, parameter-entity references |
| 1785 | * can occur only where markup declarations can occur, not |
| 1786 | * within markup declarations. |
| 1787 | * In that case this is handled in xmlParseMarkupDecl |
| 1788 | */ |
| 1789 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 1790 | return; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1791 | if (IS_BLANK_CH(NXT(1)) || NXT(1) == 0) |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1792 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1793 | break; |
| 1794 | case XML_PARSER_IGNORE: |
| 1795 | return; |
| 1796 | } |
| 1797 | |
| 1798 | NEXT; |
| 1799 | name = xmlParseName(ctxt); |
| 1800 | if (xmlParserDebugEntities) |
| 1801 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1802 | "PEReference: %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1803 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1804 | xmlFatalErr(ctxt, XML_ERR_PEREF_NO_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1805 | } else { |
| 1806 | if (RAW == ';') { |
| 1807 | NEXT; |
| 1808 | if ((ctxt->sax != NULL) && (ctxt->sax->getParameterEntity != NULL)) |
| 1809 | entity = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 1810 | if (entity == NULL) { |
| 1811 | |
| 1812 | /* |
| 1813 | * [ WFC: Entity Declared ] |
| 1814 | * In a document without any DTD, a document with only an |
| 1815 | * internal DTD subset which contains no parameter entity |
| 1816 | * references, or a document with "standalone='yes'", ... |
| 1817 | * ... The declaration of a parameter entity must precede |
| 1818 | * any reference to it... |
| 1819 | */ |
| 1820 | if ((ctxt->standalone == 1) || |
| 1821 | ((ctxt->hasExternalSubset == 0) && |
| 1822 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 1823 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1824 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1825 | } else { |
| 1826 | /* |
| 1827 | * [ VC: Entity Declared ] |
| 1828 | * In a document with an external subset or external |
| 1829 | * parameter entities with "standalone='no'", ... |
| 1830 | * ... The declaration of a parameter entity must precede |
| 1831 | * any reference to it... |
| 1832 | */ |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 1833 | if ((ctxt->validate) && (ctxt->vctxt.error != NULL)) { |
| 1834 | xmlValidityError(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 1835 | "PEReference: %%%s; not found\n", |
| 1836 | name); |
| 1837 | } else |
| 1838 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 1839 | "PEReference: %%%s; not found\n", |
| 1840 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1841 | ctxt->valid = 0; |
| 1842 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1843 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 1844 | input = xmlNewBlanksWrapperInputStream(ctxt, entity); |
| 1845 | xmlPushInput(ctxt, input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1846 | } else { |
| 1847 | if ((entity->etype == XML_INTERNAL_PARAMETER_ENTITY) || |
| 1848 | (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY)) { |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 1849 | xmlChar start[4]; |
| 1850 | xmlCharEncoding enc; |
| 1851 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1852 | /* |
| 1853 | * handle the extra spaces added before and after |
| 1854 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1855 | * this is done independently. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1856 | */ |
| 1857 | input = xmlNewEntityInputStream(ctxt, entity); |
| 1858 | xmlPushInput(ctxt, input); |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 1859 | |
| 1860 | /* |
| 1861 | * Get the 4 first bytes and decode the charset |
| 1862 | * if enc != XML_CHAR_ENCODING_NONE |
| 1863 | * plug some encoding conversion routines. |
William M. Brack | a0c48ad | 2004-04-16 15:58:29 +0000 | [diff] [blame] | 1864 | * Note that, since we may have some non-UTF8 |
| 1865 | * encoding (like UTF16, bug 135229), the 'length' |
| 1866 | * is not known, but we can calculate based upon |
| 1867 | * the amount of data in the buffer. |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 1868 | */ |
| 1869 | GROW |
William M. Brack | a0c48ad | 2004-04-16 15:58:29 +0000 | [diff] [blame] | 1870 | if ((ctxt->input->end - ctxt->input->cur)>=4) { |
Daniel Veillard | e059b89 | 2002-06-13 15:32:10 +0000 | [diff] [blame] | 1871 | start[0] = RAW; |
| 1872 | start[1] = NXT(1); |
| 1873 | start[2] = NXT(2); |
| 1874 | start[3] = NXT(3); |
| 1875 | enc = xmlDetectCharEncoding(start, 4); |
| 1876 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 1877 | xmlSwitchEncoding(ctxt, enc); |
| 1878 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1881 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 1882 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l' )) && |
| 1883 | (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1884 | xmlParseTextDecl(ctxt); |
| 1885 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1886 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 1887 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 1888 | "PEReference: %s is not a parameter entity\n", |
| 1889 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1890 | } |
| 1891 | } |
| 1892 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1893 | xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1894 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | /* |
| 1899 | * Macro used to grow the current buffer. |
| 1900 | */ |
| 1901 | #define growBuffer(buffer) { \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 1902 | xmlChar *tmp; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1903 | buffer##_size *= 2; \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 1904 | tmp = (xmlChar *) \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1905 | xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 1906 | if (tmp == NULL) goto mem_error; \ |
| 1907 | buffer = tmp; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | /** |
Daniel Veillard | 7a02cfe | 2003-09-25 12:18:34 +0000 | [diff] [blame] | 1911 | * xmlStringLenDecodeEntities: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1912 | * @ctxt: the parser context |
| 1913 | * @str: the input string |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1914 | * @len: the string length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1915 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 1916 | * @end: an end marker xmlChar, 0 if none |
| 1917 | * @end2: an end marker xmlChar, 0 if none |
| 1918 | * @end3: an end marker xmlChar, 0 if none |
| 1919 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1920 | * Takes a entity string content and process to do the adequate substitutions. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1921 | * |
| 1922 | * [67] Reference ::= EntityRef | CharRef |
| 1923 | * |
| 1924 | * [69] PEReference ::= '%' Name ';' |
| 1925 | * |
| 1926 | * Returns A newly allocated string with the substitution done. The caller |
| 1927 | * must deallocate it ! |
| 1928 | */ |
| 1929 | xmlChar * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1930 | xmlStringLenDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, |
| 1931 | int what, xmlChar end, xmlChar end2, xmlChar end3) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1932 | xmlChar *buffer = NULL; |
| 1933 | int buffer_size = 0; |
| 1934 | |
| 1935 | xmlChar *current = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1936 | const xmlChar *last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1937 | xmlEntityPtr ent; |
| 1938 | int c,l; |
| 1939 | int nbchars = 0; |
| 1940 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 1941 | if ((ctxt == NULL) || (str == NULL) || (len < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1942 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1943 | last = str + len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1944 | |
| 1945 | if (ctxt->depth > 40) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1946 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1947 | return(NULL); |
| 1948 | } |
| 1949 | |
| 1950 | /* |
| 1951 | * allocate a translation buffer. |
| 1952 | */ |
| 1953 | buffer_size = XML_PARSER_BIG_BUFFER_SIZE; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 1954 | buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar)); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1955 | if (buffer == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1956 | |
| 1957 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1958 | * 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] | 1959 | * we are operating on already parsed values. |
| 1960 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1961 | if (str < last) |
| 1962 | c = CUR_SCHAR(str, l); |
| 1963 | else |
| 1964 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1965 | while ((c != 0) && (c != end) && /* non input consuming loop */ |
| 1966 | (c != end2) && (c != end3)) { |
| 1967 | |
| 1968 | if (c == 0) break; |
| 1969 | if ((c == '&') && (str[1] == '#')) { |
| 1970 | int val = xmlParseStringCharRef(ctxt, &str); |
| 1971 | if (val != 0) { |
| 1972 | COPY_BUF(0,buffer,nbchars,val); |
| 1973 | } |
| 1974 | } else if ((c == '&') && (what & XML_SUBSTITUTE_REF)) { |
| 1975 | if (xmlParserDebugEntities) |
| 1976 | xmlGenericError(xmlGenericErrorContext, |
| 1977 | "String decoding Entity Reference: %.30s\n", |
| 1978 | str); |
| 1979 | ent = xmlParseStringEntityRef(ctxt, &str); |
| 1980 | if ((ent != NULL) && |
| 1981 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1982 | if (ent->content != NULL) { |
| 1983 | COPY_BUF(0,buffer,nbchars,ent->content[0]); |
| 1984 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 1985 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1986 | "predefined entity has no content\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1987 | } |
| 1988 | } else if ((ent != NULL) && (ent->content != NULL)) { |
| 1989 | xmlChar *rep; |
| 1990 | |
| 1991 | ctxt->depth++; |
| 1992 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 1993 | 0, 0, 0); |
| 1994 | ctxt->depth--; |
| 1995 | if (rep != NULL) { |
| 1996 | current = rep; |
| 1997 | while (*current != 0) { /* non input consuming loop */ |
| 1998 | buffer[nbchars++] = *current++; |
| 1999 | if (nbchars > |
| 2000 | buffer_size - XML_PARSER_BUFFER_SIZE) { |
| 2001 | growBuffer(buffer); |
| 2002 | } |
| 2003 | } |
| 2004 | xmlFree(rep); |
| 2005 | } |
| 2006 | } else if (ent != NULL) { |
| 2007 | int i = xmlStrlen(ent->name); |
| 2008 | const xmlChar *cur = ent->name; |
| 2009 | |
| 2010 | buffer[nbchars++] = '&'; |
| 2011 | if (nbchars > buffer_size - i - XML_PARSER_BUFFER_SIZE) { |
| 2012 | growBuffer(buffer); |
| 2013 | } |
| 2014 | for (;i > 0;i--) |
| 2015 | buffer[nbchars++] = *cur++; |
| 2016 | buffer[nbchars++] = ';'; |
| 2017 | } |
| 2018 | } else if (c == '%' && (what & XML_SUBSTITUTE_PEREF)) { |
| 2019 | if (xmlParserDebugEntities) |
| 2020 | xmlGenericError(xmlGenericErrorContext, |
| 2021 | "String decoding PE Reference: %.30s\n", str); |
| 2022 | ent = xmlParseStringPEReference(ctxt, &str); |
| 2023 | if (ent != NULL) { |
| 2024 | xmlChar *rep; |
| 2025 | |
| 2026 | ctxt->depth++; |
| 2027 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 2028 | 0, 0, 0); |
| 2029 | ctxt->depth--; |
| 2030 | if (rep != NULL) { |
| 2031 | current = rep; |
| 2032 | while (*current != 0) { /* non input consuming loop */ |
| 2033 | buffer[nbchars++] = *current++; |
| 2034 | if (nbchars > |
| 2035 | buffer_size - XML_PARSER_BUFFER_SIZE) { |
| 2036 | growBuffer(buffer); |
| 2037 | } |
| 2038 | } |
| 2039 | xmlFree(rep); |
| 2040 | } |
| 2041 | } |
| 2042 | } else { |
| 2043 | COPY_BUF(l,buffer,nbchars,c); |
| 2044 | str += l; |
| 2045 | if (nbchars > buffer_size - XML_PARSER_BUFFER_SIZE) { |
| 2046 | growBuffer(buffer); |
| 2047 | } |
| 2048 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2049 | if (str < last) |
| 2050 | c = CUR_SCHAR(str, l); |
| 2051 | else |
| 2052 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2053 | } |
| 2054 | buffer[nbchars++] = 0; |
| 2055 | return(buffer); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2056 | |
| 2057 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2058 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2059 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2062 | /** |
| 2063 | * xmlStringDecodeEntities: |
| 2064 | * @ctxt: the parser context |
| 2065 | * @str: the input string |
| 2066 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 2067 | * @end: an end marker xmlChar, 0 if none |
| 2068 | * @end2: an end marker xmlChar, 0 if none |
| 2069 | * @end3: an end marker xmlChar, 0 if none |
| 2070 | * |
| 2071 | * Takes a entity string content and process to do the adequate substitutions. |
| 2072 | * |
| 2073 | * [67] Reference ::= EntityRef | CharRef |
| 2074 | * |
| 2075 | * [69] PEReference ::= '%' Name ';' |
| 2076 | * |
| 2077 | * Returns A newly allocated string with the substitution done. The caller |
| 2078 | * must deallocate it ! |
| 2079 | */ |
| 2080 | xmlChar * |
| 2081 | xmlStringDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int what, |
| 2082 | xmlChar end, xmlChar end2, xmlChar end3) { |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 2083 | if ((ctxt == NULL) || (str == NULL)) return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2084 | return(xmlStringLenDecodeEntities(ctxt, str, xmlStrlen(str), what, |
| 2085 | end, end2, end3)); |
| 2086 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2087 | |
| 2088 | /************************************************************************ |
| 2089 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2090 | * Commodity functions, cleanup needed ? * |
| 2091 | * * |
| 2092 | ************************************************************************/ |
| 2093 | |
| 2094 | /** |
| 2095 | * areBlanks: |
| 2096 | * @ctxt: an XML parser context |
| 2097 | * @str: a xmlChar * |
| 2098 | * @len: the size of @str |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2099 | * @blank_chars: we know the chars are blanks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2100 | * |
| 2101 | * Is this a sequence of blank chars that one can ignore ? |
| 2102 | * |
| 2103 | * Returns 1 if ignorable 0 otherwise. |
| 2104 | */ |
| 2105 | |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2106 | static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, |
| 2107 | int blank_chars) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2108 | int i, ret; |
| 2109 | xmlNodePtr lastChild; |
| 2110 | |
Daniel Veillard | 05c13a2 | 2001-09-09 08:38:09 +0000 | [diff] [blame] | 2111 | /* |
| 2112 | * Don't spend time trying to differentiate them, the same callback is |
| 2113 | * used ! |
| 2114 | */ |
| 2115 | if (ctxt->sax->ignorableWhitespace == ctxt->sax->characters) |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 2116 | return(0); |
| 2117 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2118 | /* |
| 2119 | * Check for xml:space value. |
| 2120 | */ |
| 2121 | if (*(ctxt->space) == 1) |
| 2122 | return(0); |
| 2123 | |
| 2124 | /* |
| 2125 | * Check that the string is made of blanks |
| 2126 | */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2127 | if (blank_chars == 0) { |
| 2128 | for (i = 0;i < len;i++) |
| 2129 | if (!(IS_BLANK_CH(str[i]))) return(0); |
| 2130 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2131 | |
| 2132 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2133 | * Look if the element is mixed content in the DTD if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2134 | */ |
Daniel Veillard | 6dd398f | 2001-07-25 22:41:03 +0000 | [diff] [blame] | 2135 | if (ctxt->node == NULL) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2136 | if (ctxt->myDoc != NULL) { |
| 2137 | ret = xmlIsMixedElement(ctxt->myDoc, ctxt->node->name); |
| 2138 | if (ret == 0) return(1); |
| 2139 | if (ret == 1) return(0); |
| 2140 | } |
| 2141 | |
| 2142 | /* |
| 2143 | * Otherwise, heuristic :-\ |
| 2144 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2145 | if (RAW != '<') return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2146 | if ((ctxt->node->children == NULL) && |
| 2147 | (RAW == '<') && (NXT(1) == '/')) return(0); |
| 2148 | |
| 2149 | lastChild = xmlGetLastChild(ctxt->node); |
| 2150 | if (lastChild == NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 2151 | if ((ctxt->node->type != XML_ELEMENT_NODE) && |
| 2152 | (ctxt->node->content != NULL)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2153 | } else if (xmlNodeIsText(lastChild)) |
| 2154 | return(0); |
| 2155 | else if ((ctxt->node->children != NULL) && |
| 2156 | (xmlNodeIsText(ctxt->node->children))) |
| 2157 | return(0); |
| 2158 | return(1); |
| 2159 | } |
| 2160 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2161 | /************************************************************************ |
| 2162 | * * |
| 2163 | * Extra stuff for namespace support * |
| 2164 | * Relates to http://www.w3.org/TR/WD-xml-names * |
| 2165 | * * |
| 2166 | ************************************************************************/ |
| 2167 | |
| 2168 | /** |
| 2169 | * xmlSplitQName: |
| 2170 | * @ctxt: an XML parser context |
| 2171 | * @name: an XML parser context |
| 2172 | * @prefix: a xmlChar ** |
| 2173 | * |
| 2174 | * parse an UTF8 encoded XML qualified name string |
| 2175 | * |
| 2176 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 2177 | * |
| 2178 | * [NS 6] Prefix ::= NCName |
| 2179 | * |
| 2180 | * [NS 7] LocalPart ::= NCName |
| 2181 | * |
| 2182 | * Returns the local part, and prefix is updated |
| 2183 | * to get the Prefix if any. |
| 2184 | */ |
| 2185 | |
| 2186 | xmlChar * |
| 2187 | xmlSplitQName(xmlParserCtxtPtr ctxt, const xmlChar *name, xmlChar **prefix) { |
| 2188 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 2189 | xmlChar *buffer = NULL; |
| 2190 | int len = 0; |
| 2191 | int max = XML_MAX_NAMELEN; |
| 2192 | xmlChar *ret = NULL; |
| 2193 | const xmlChar *cur = name; |
| 2194 | int c; |
| 2195 | |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 2196 | if (prefix == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2197 | *prefix = NULL; |
| 2198 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2199 | if (cur == NULL) return(NULL); |
| 2200 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2201 | #ifndef XML_XML_NAMESPACE |
| 2202 | /* xml: prefix is not really a namespace */ |
| 2203 | if ((cur[0] == 'x') && (cur[1] == 'm') && |
| 2204 | (cur[2] == 'l') && (cur[3] == ':')) |
| 2205 | return(xmlStrdup(name)); |
| 2206 | #endif |
| 2207 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2208 | /* nasty but well=formed */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2209 | if (cur[0] == ':') |
| 2210 | return(xmlStrdup(name)); |
| 2211 | |
| 2212 | c = *cur++; |
| 2213 | while ((c != 0) && (c != ':') && (len < max)) { /* tested bigname.xml */ |
| 2214 | buf[len++] = c; |
| 2215 | c = *cur++; |
| 2216 | } |
| 2217 | if (len >= max) { |
| 2218 | /* |
| 2219 | * Okay someone managed to make a huge name, so he's ready to pay |
| 2220 | * for the processing speed. |
| 2221 | */ |
| 2222 | max = len * 2; |
| 2223 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2224 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2225 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2226 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2227 | return(NULL); |
| 2228 | } |
| 2229 | memcpy(buffer, buf, len); |
| 2230 | while ((c != 0) && (c != ':')) { /* tested bigname.xml */ |
| 2231 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2232 | xmlChar *tmp; |
| 2233 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2234 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2235 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2236 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2237 | if (tmp == NULL) { |
| 2238 | xmlFree(tmp); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2239 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2240 | return(NULL); |
| 2241 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2242 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2243 | } |
| 2244 | buffer[len++] = c; |
| 2245 | c = *cur++; |
| 2246 | } |
| 2247 | buffer[len] = 0; |
| 2248 | } |
| 2249 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2250 | /* nasty but well=formed |
| 2251 | if ((c == ':') && (*cur == 0)) { |
| 2252 | return(xmlStrdup(name)); |
| 2253 | } */ |
| 2254 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2255 | if (buffer == NULL) |
| 2256 | ret = xmlStrndup(buf, len); |
| 2257 | else { |
| 2258 | ret = buffer; |
| 2259 | buffer = NULL; |
| 2260 | max = XML_MAX_NAMELEN; |
| 2261 | } |
| 2262 | |
| 2263 | |
| 2264 | if (c == ':') { |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2265 | c = *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2266 | *prefix = ret; |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2267 | if (c == 0) { |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 2268 | return(xmlStrndup(BAD_CAST "", 0)); |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2269 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2270 | len = 0; |
| 2271 | |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2272 | /* |
| 2273 | * Check that the first character is proper to start |
| 2274 | * a new name |
| 2275 | */ |
| 2276 | if (!(((c >= 0x61) && (c <= 0x7A)) || |
| 2277 | ((c >= 0x41) && (c <= 0x5A)) || |
| 2278 | (c == '_') || (c == ':'))) { |
| 2279 | int l; |
| 2280 | int first = CUR_SCHAR(cur, l); |
| 2281 | |
| 2282 | if (!IS_LETTER(first) && (first != '_')) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2283 | xmlFatalErrMsgStr(ctxt, XML_NS_ERR_QNAME, |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2284 | "Name %s is not XML Namespace compliant\n", |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2285 | name); |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2286 | } |
| 2287 | } |
| 2288 | cur++; |
| 2289 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2290 | while ((c != 0) && (len < max)) { /* tested bigname2.xml */ |
| 2291 | buf[len++] = c; |
| 2292 | c = *cur++; |
| 2293 | } |
| 2294 | if (len >= max) { |
| 2295 | /* |
| 2296 | * Okay someone managed to make a huge name, so he's ready to pay |
| 2297 | * for the processing speed. |
| 2298 | */ |
| 2299 | max = len * 2; |
| 2300 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2301 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2302 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2303 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2304 | return(NULL); |
| 2305 | } |
| 2306 | memcpy(buffer, buf, len); |
| 2307 | while (c != 0) { /* tested bigname2.xml */ |
| 2308 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2309 | xmlChar *tmp; |
| 2310 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2311 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2312 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2313 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2314 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2315 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2316 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2317 | return(NULL); |
| 2318 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2319 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2320 | } |
| 2321 | buffer[len++] = c; |
| 2322 | c = *cur++; |
| 2323 | } |
| 2324 | buffer[len] = 0; |
| 2325 | } |
| 2326 | |
| 2327 | if (buffer == NULL) |
| 2328 | ret = xmlStrndup(buf, len); |
| 2329 | else { |
| 2330 | ret = buffer; |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | return(ret); |
| 2335 | } |
| 2336 | |
| 2337 | /************************************************************************ |
| 2338 | * * |
| 2339 | * The parser itself * |
| 2340 | * Relates to http://www.w3.org/TR/REC-xml * |
| 2341 | * * |
| 2342 | ************************************************************************/ |
| 2343 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2344 | static const xmlChar * xmlParseNameComplex(xmlParserCtxtPtr ctxt); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2345 | static xmlChar * xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2346 | int *len, int *alloc, int normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2347 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2348 | /** |
| 2349 | * xmlParseName: |
| 2350 | * @ctxt: an XML parser context |
| 2351 | * |
| 2352 | * parse an XML name. |
| 2353 | * |
| 2354 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 2355 | * CombiningChar | Extender |
| 2356 | * |
| 2357 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 2358 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 2359 | * [6] Names ::= Name (#x20 Name)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2360 | * |
| 2361 | * Returns the Name parsed or NULL |
| 2362 | */ |
| 2363 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2364 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2365 | xmlParseName(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2366 | const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2367 | const xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2368 | int count = 0; |
| 2369 | |
| 2370 | GROW; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2371 | |
| 2372 | /* |
| 2373 | * Accelerator for simple ASCII names |
| 2374 | */ |
| 2375 | in = ctxt->input->cur; |
| 2376 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 2377 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 2378 | (*in == '_') || (*in == ':')) { |
| 2379 | in++; |
| 2380 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 2381 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 2382 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 2383 | (*in == '_') || (*in == '-') || |
| 2384 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2385 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 2386 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2387 | count = in - ctxt->input->cur; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2388 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2389 | ctxt->input->cur = in; |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2390 | ctxt->nbChars += count; |
| 2391 | ctxt->input->col += count; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2392 | if (ret == NULL) |
| 2393 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2394 | return(ret); |
| 2395 | } |
| 2396 | } |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 2397 | return(xmlParseNameComplex(ctxt)); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2398 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2399 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2400 | /** |
| 2401 | * xmlParseNameAndCompare: |
| 2402 | * @ctxt: an XML parser context |
| 2403 | * |
| 2404 | * parse an XML name and compares for match |
| 2405 | * (specialized for endtag parsing) |
| 2406 | * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2407 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 2408 | * and the name for mismatch |
| 2409 | */ |
| 2410 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2411 | static const xmlChar * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2412 | xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2413 | register const xmlChar *cmp = other; |
| 2414 | register const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2415 | const xmlChar *ret; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2416 | |
| 2417 | GROW; |
| 2418 | |
| 2419 | in = ctxt->input->cur; |
| 2420 | while (*in != 0 && *in == *cmp) { |
| 2421 | ++in; |
| 2422 | ++cmp; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 2423 | ctxt->input->col++; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2424 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2425 | if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) { |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2426 | /* success */ |
| 2427 | ctxt->input->cur = in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2428 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2429 | } |
| 2430 | /* failure (or end of input buffer), check with full function */ |
| 2431 | ret = xmlParseName (ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2432 | /* strings coming from the dictionnary direct compare possible */ |
| 2433 | if (ret == other) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2434 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2435 | } |
| 2436 | return ret; |
| 2437 | } |
| 2438 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2439 | static const xmlChar * |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2440 | xmlParseNameComplex(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2441 | int len = 0, l; |
| 2442 | int c; |
| 2443 | int count = 0; |
| 2444 | |
| 2445 | /* |
| 2446 | * Handler for more complex cases |
| 2447 | */ |
| 2448 | GROW; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2449 | c = CUR_CHAR(l); |
| 2450 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 2451 | (!IS_LETTER(c) && (c != '_') && |
| 2452 | (c != ':'))) { |
| 2453 | return(NULL); |
| 2454 | } |
| 2455 | |
| 2456 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2457 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2458 | (c == '.') || (c == '-') || |
| 2459 | (c == '_') || (c == ':') || |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2460 | (IS_COMBINING(c)) || |
| 2461 | (IS_EXTENDER(c)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2462 | if (count++ > 100) { |
| 2463 | count = 0; |
| 2464 | GROW; |
| 2465 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2466 | len += l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2467 | NEXTL(l); |
| 2468 | c = CUR_CHAR(l); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2469 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2470 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | /** |
| 2474 | * xmlParseStringName: |
| 2475 | * @ctxt: an XML parser context |
| 2476 | * @str: a pointer to the string pointer (IN/OUT) |
| 2477 | * |
| 2478 | * parse an XML name. |
| 2479 | * |
| 2480 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 2481 | * CombiningChar | Extender |
| 2482 | * |
| 2483 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 2484 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 2485 | * [6] Names ::= Name (#x20 Name)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2486 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2487 | * Returns the Name parsed or NULL. The @str pointer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2488 | * is updated to the current location in the string. |
| 2489 | */ |
| 2490 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2491 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2492 | xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) { |
| 2493 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 2494 | const xmlChar *cur = *str; |
| 2495 | int len = 0, l; |
| 2496 | int c; |
| 2497 | |
| 2498 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2499 | if (!IS_LETTER(c) && (c != '_') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2500 | (c != ':')) { |
| 2501 | return(NULL); |
| 2502 | } |
| 2503 | |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2504 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigentname.xml */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2505 | (c == '.') || (c == '-') || |
| 2506 | (c == '_') || (c == ':') || |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2507 | (IS_COMBINING(c)) || |
| 2508 | (IS_EXTENDER(c))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2509 | COPY_BUF(l,buf,len,c); |
| 2510 | cur += l; |
| 2511 | c = CUR_SCHAR(cur, l); |
| 2512 | if (len >= XML_MAX_NAMELEN) { /* test bigentname.xml */ |
| 2513 | /* |
| 2514 | * Okay someone managed to make a huge name, so he's ready to pay |
| 2515 | * for the processing speed. |
| 2516 | */ |
| 2517 | xmlChar *buffer; |
| 2518 | int max = len * 2; |
| 2519 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2520 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2521 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2522 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2523 | return(NULL); |
| 2524 | } |
| 2525 | memcpy(buffer, buf, len); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2526 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 2527 | /* test bigentname.xml */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2528 | (c == '.') || (c == '-') || |
| 2529 | (c == '_') || (c == ':') || |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2530 | (IS_COMBINING(c)) || |
| 2531 | (IS_EXTENDER(c))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2532 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2533 | xmlChar *tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2534 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2535 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2536 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2537 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2538 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2539 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2540 | return(NULL); |
| 2541 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2542 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2543 | } |
| 2544 | COPY_BUF(l,buffer,len,c); |
| 2545 | cur += l; |
| 2546 | c = CUR_SCHAR(cur, l); |
| 2547 | } |
| 2548 | buffer[len] = 0; |
| 2549 | *str = cur; |
| 2550 | return(buffer); |
| 2551 | } |
| 2552 | } |
| 2553 | *str = cur; |
| 2554 | return(xmlStrndup(buf, len)); |
| 2555 | } |
| 2556 | |
| 2557 | /** |
| 2558 | * xmlParseNmtoken: |
| 2559 | * @ctxt: an XML parser context |
| 2560 | * |
| 2561 | * parse an XML Nmtoken. |
| 2562 | * |
| 2563 | * [7] Nmtoken ::= (NameChar)+ |
| 2564 | * |
Daniel Veillard | 807b4de | 2004-09-26 14:42:56 +0000 | [diff] [blame] | 2565 | * [8] Nmtokens ::= Nmtoken (#x20 Nmtoken)* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2566 | * |
| 2567 | * Returns the Nmtoken parsed or NULL |
| 2568 | */ |
| 2569 | |
| 2570 | xmlChar * |
| 2571 | xmlParseNmtoken(xmlParserCtxtPtr ctxt) { |
| 2572 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 2573 | int len = 0, l; |
| 2574 | int c; |
| 2575 | int count = 0; |
| 2576 | |
| 2577 | GROW; |
| 2578 | c = CUR_CHAR(l); |
| 2579 | |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2580 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigtoken.xml */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2581 | (c == '.') || (c == '-') || |
| 2582 | (c == '_') || (c == ':') || |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2583 | (IS_COMBINING(c)) || |
| 2584 | (IS_EXTENDER(c))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2585 | if (count++ > 100) { |
| 2586 | count = 0; |
| 2587 | GROW; |
| 2588 | } |
| 2589 | COPY_BUF(l,buf,len,c); |
| 2590 | NEXTL(l); |
| 2591 | c = CUR_CHAR(l); |
| 2592 | if (len >= XML_MAX_NAMELEN) { |
| 2593 | /* |
| 2594 | * Okay someone managed to make a huge token, so he's ready to pay |
| 2595 | * for the processing speed. |
| 2596 | */ |
| 2597 | xmlChar *buffer; |
| 2598 | int max = len * 2; |
| 2599 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2600 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2601 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2602 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2603 | return(NULL); |
| 2604 | } |
| 2605 | memcpy(buffer, buf, len); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2606 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigtoken.xml */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2607 | (c == '.') || (c == '-') || |
| 2608 | (c == '_') || (c == ':') || |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2609 | (IS_COMBINING(c)) || |
| 2610 | (IS_EXTENDER(c))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2611 | if (count++ > 100) { |
| 2612 | count = 0; |
| 2613 | GROW; |
| 2614 | } |
| 2615 | if (len + 10 > max) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2616 | xmlChar *tmp; |
| 2617 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2618 | max *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2619 | tmp = (xmlChar *) xmlRealloc(buffer, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2620 | max * sizeof(xmlChar)); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2621 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2622 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2623 | xmlFree(buffer); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2624 | return(NULL); |
| 2625 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2626 | buffer = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2627 | } |
| 2628 | COPY_BUF(l,buffer,len,c); |
| 2629 | NEXTL(l); |
| 2630 | c = CUR_CHAR(l); |
| 2631 | } |
| 2632 | buffer[len] = 0; |
| 2633 | return(buffer); |
| 2634 | } |
| 2635 | } |
| 2636 | if (len == 0) |
| 2637 | return(NULL); |
| 2638 | return(xmlStrndup(buf, len)); |
| 2639 | } |
| 2640 | |
| 2641 | /** |
| 2642 | * xmlParseEntityValue: |
| 2643 | * @ctxt: an XML parser context |
| 2644 | * @orig: if non-NULL store a copy of the original entity value |
| 2645 | * |
| 2646 | * parse a value for ENTITY declarations |
| 2647 | * |
| 2648 | * [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' | |
| 2649 | * "'" ([^%&'] | PEReference | Reference)* "'" |
| 2650 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2651 | * Returns the EntityValue parsed with reference substituted or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2652 | */ |
| 2653 | |
| 2654 | xmlChar * |
| 2655 | xmlParseEntityValue(xmlParserCtxtPtr ctxt, xmlChar **orig) { |
| 2656 | xmlChar *buf = NULL; |
| 2657 | int len = 0; |
| 2658 | int size = XML_PARSER_BUFFER_SIZE; |
| 2659 | int c, l; |
| 2660 | xmlChar stop; |
| 2661 | xmlChar *ret = NULL; |
| 2662 | const xmlChar *cur = NULL; |
| 2663 | xmlParserInputPtr input; |
| 2664 | |
| 2665 | if (RAW == '"') stop = '"'; |
| 2666 | else if (RAW == '\'') stop = '\''; |
| 2667 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2668 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2669 | return(NULL); |
| 2670 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2671 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2672 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2673 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2674 | return(NULL); |
| 2675 | } |
| 2676 | |
| 2677 | /* |
| 2678 | * The content of the entity definition is copied in a buffer. |
| 2679 | */ |
| 2680 | |
| 2681 | ctxt->instate = XML_PARSER_ENTITY_VALUE; |
| 2682 | input = ctxt->input; |
| 2683 | GROW; |
| 2684 | NEXT; |
| 2685 | c = CUR_CHAR(l); |
| 2686 | /* |
| 2687 | * NOTE: 4.4.5 Included in Literal |
| 2688 | * When a parameter entity reference appears in a literal entity |
| 2689 | * value, ... a single or double quote character in the replacement |
| 2690 | * text is always treated as a normal data character and will not |
| 2691 | * terminate the literal. |
| 2692 | * In practice it means we stop the loop only when back at parsing |
| 2693 | * the initial entity and the quote is found |
| 2694 | */ |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2695 | while ((IS_CHAR(c)) && ((c != stop) || /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2696 | (ctxt->input != input))) { |
| 2697 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2698 | xmlChar *tmp; |
| 2699 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2700 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2701 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 2702 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2703 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2704 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2705 | return(NULL); |
| 2706 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 2707 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2708 | } |
| 2709 | COPY_BUF(l,buf,len,c); |
| 2710 | NEXTL(l); |
| 2711 | /* |
| 2712 | * Pop-up of finished entities. |
| 2713 | */ |
| 2714 | while ((RAW == 0) && (ctxt->inputNr > 1)) /* non input consuming */ |
| 2715 | xmlPopInput(ctxt); |
| 2716 | |
| 2717 | GROW; |
| 2718 | c = CUR_CHAR(l); |
| 2719 | if (c == 0) { |
| 2720 | GROW; |
| 2721 | c = CUR_CHAR(l); |
| 2722 | } |
| 2723 | } |
| 2724 | buf[len] = 0; |
| 2725 | |
| 2726 | /* |
| 2727 | * Raise problem w.r.t. '&' and '%' being used in non-entities |
| 2728 | * reference constructs. Note Charref will be handled in |
| 2729 | * xmlStringDecodeEntities() |
| 2730 | */ |
| 2731 | cur = buf; |
| 2732 | while (*cur != 0) { /* non input consuming */ |
| 2733 | if ((*cur == '%') || ((*cur == '&') && (cur[1] != '#'))) { |
| 2734 | xmlChar *name; |
| 2735 | xmlChar tmp = *cur; |
| 2736 | |
| 2737 | cur++; |
| 2738 | name = xmlParseStringName(ctxt, &cur); |
| 2739 | if ((name == NULL) || (*cur != ';')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2740 | xmlFatalErrMsgInt(ctxt, XML_ERR_ENTITY_CHAR_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2741 | "EntityValue: '%c' forbidden except for entities references\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2742 | tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2743 | } |
Daniel Veillard | 5151c06 | 2001-10-23 13:10:19 +0000 | [diff] [blame] | 2744 | if ((tmp == '%') && (ctxt->inSubset == 1) && |
| 2745 | (ctxt->inputNr == 1)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2746 | xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2747 | } |
| 2748 | if (name != NULL) |
| 2749 | xmlFree(name); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 2750 | if (*cur == 0) |
| 2751 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2752 | } |
| 2753 | cur++; |
| 2754 | } |
| 2755 | |
| 2756 | /* |
| 2757 | * Then PEReference entities are substituted. |
| 2758 | */ |
| 2759 | if (c != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2760 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2761 | xmlFree(buf); |
| 2762 | } else { |
| 2763 | NEXT; |
| 2764 | /* |
| 2765 | * NOTE: 4.4.7 Bypassed |
| 2766 | * When a general entity reference appears in the EntityValue in |
| 2767 | * an entity declaration, it is bypassed and left as is. |
| 2768 | * so XML_SUBSTITUTE_REF is not set here. |
| 2769 | */ |
| 2770 | ret = xmlStringDecodeEntities(ctxt, buf, XML_SUBSTITUTE_PEREF, |
| 2771 | 0, 0, 0); |
| 2772 | if (orig != NULL) |
| 2773 | *orig = buf; |
| 2774 | else |
| 2775 | xmlFree(buf); |
| 2776 | } |
| 2777 | |
| 2778 | return(ret); |
| 2779 | } |
| 2780 | |
| 2781 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2782 | * xmlParseAttValueComplex: |
| 2783 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2784 | * @len: the resulting attribute len |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2785 | * @normalize: wether to apply the inner normalization |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2786 | * |
| 2787 | * parse a value for an attribute, this is the fallback function |
| 2788 | * of xmlParseAttValue() when the attribute parsing requires handling |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2789 | * of non-ASCII characters, or normalization compaction. |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2790 | * |
| 2791 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 2792 | */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2793 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2794 | xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) { |
Daniel Veillard | e72c756 | 2002-05-31 09:47:30 +0000 | [diff] [blame] | 2795 | xmlChar limit = 0; |
| 2796 | xmlChar *buf = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2797 | int len = 0; |
| 2798 | int buf_size = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2799 | int c, l, in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2800 | xmlChar *current = NULL; |
| 2801 | xmlEntityPtr ent; |
| 2802 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2803 | if (NXT(0) == '"') { |
| 2804 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 2805 | limit = '"'; |
| 2806 | NEXT; |
| 2807 | } else if (NXT(0) == '\'') { |
| 2808 | limit = '\''; |
| 2809 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 2810 | NEXT; |
| 2811 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2812 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2813 | return(NULL); |
| 2814 | } |
| 2815 | |
| 2816 | /* |
| 2817 | * allocate a translation buffer. |
| 2818 | */ |
| 2819 | buf_size = XML_PARSER_BUFFER_SIZE; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2820 | buf = (xmlChar *) xmlMallocAtomic(buf_size * sizeof(xmlChar)); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2821 | if (buf == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2822 | |
| 2823 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2824 | * 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] | 2825 | */ |
| 2826 | c = CUR_CHAR(l); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 2827 | while ((NXT(0) != limit) && /* checked */ |
| 2828 | (c != '<')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2829 | if (c == 0) break; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 2830 | if (c == '&') { |
Daniel Veillard | 62998c0 | 2003-09-15 12:56:36 +0000 | [diff] [blame] | 2831 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2832 | if (NXT(1) == '#') { |
| 2833 | int val = xmlParseCharRef(ctxt); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2834 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2835 | if (val == '&') { |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 2836 | if (ctxt->replaceEntities) { |
| 2837 | if (len > buf_size - 10) { |
| 2838 | growBuffer(buf); |
| 2839 | } |
| 2840 | buf[len++] = '&'; |
| 2841 | } else { |
| 2842 | /* |
| 2843 | * The reparsing will be done in xmlStringGetNodeList() |
| 2844 | * called by the attribute() function in SAX.c |
| 2845 | */ |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 2846 | if (len > buf_size - 10) { |
| 2847 | growBuffer(buf); |
| 2848 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2849 | buf[len++] = '&'; |
| 2850 | buf[len++] = '#'; |
| 2851 | buf[len++] = '3'; |
| 2852 | buf[len++] = '8'; |
| 2853 | buf[len++] = ';'; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2854 | } |
| 2855 | } else { |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 2856 | if (len > buf_size - 10) { |
| 2857 | growBuffer(buf); |
| 2858 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2859 | len += xmlCopyChar(0, &buf[len], val); |
| 2860 | } |
| 2861 | } else { |
| 2862 | ent = xmlParseEntityRef(ctxt); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2863 | if ((ent != NULL) && |
| 2864 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 2865 | if (len > buf_size - 10) { |
| 2866 | growBuffer(buf); |
| 2867 | } |
| 2868 | if ((ctxt->replaceEntities == 0) && |
| 2869 | (ent->content[0] == '&')) { |
| 2870 | buf[len++] = '&'; |
| 2871 | buf[len++] = '#'; |
| 2872 | buf[len++] = '3'; |
| 2873 | buf[len++] = '8'; |
| 2874 | buf[len++] = ';'; |
| 2875 | } else { |
| 2876 | buf[len++] = ent->content[0]; |
| 2877 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2878 | } else if ((ent != NULL) && |
| 2879 | (ctxt->replaceEntities != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2880 | xmlChar *rep; |
| 2881 | |
| 2882 | if (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) { |
| 2883 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2884 | XML_SUBSTITUTE_REF, |
| 2885 | 0, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2886 | if (rep != NULL) { |
| 2887 | current = rep; |
| 2888 | while (*current != 0) { /* non input consuming */ |
| 2889 | buf[len++] = *current++; |
| 2890 | if (len > buf_size - 10) { |
| 2891 | growBuffer(buf); |
| 2892 | } |
| 2893 | } |
| 2894 | xmlFree(rep); |
| 2895 | } |
| 2896 | } else { |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 2897 | if (len > buf_size - 10) { |
| 2898 | growBuffer(buf); |
| 2899 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2900 | if (ent->content != NULL) |
| 2901 | buf[len++] = ent->content[0]; |
| 2902 | } |
| 2903 | } else if (ent != NULL) { |
| 2904 | int i = xmlStrlen(ent->name); |
| 2905 | const xmlChar *cur = ent->name; |
| 2906 | |
| 2907 | /* |
| 2908 | * This may look absurd but is needed to detect |
| 2909 | * entities problems |
| 2910 | */ |
| 2911 | if ((ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
| 2912 | (ent->content != NULL)) { |
| 2913 | xmlChar *rep; |
| 2914 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
| 2915 | XML_SUBSTITUTE_REF, 0, 0, 0); |
| 2916 | if (rep != NULL) |
| 2917 | xmlFree(rep); |
| 2918 | } |
| 2919 | |
| 2920 | /* |
| 2921 | * Just output the reference |
| 2922 | */ |
| 2923 | buf[len++] = '&'; |
| 2924 | if (len > buf_size - i - 10) { |
| 2925 | growBuffer(buf); |
| 2926 | } |
| 2927 | for (;i > 0;i--) |
| 2928 | buf[len++] = *cur++; |
| 2929 | buf[len++] = ';'; |
| 2930 | } |
| 2931 | } |
| 2932 | } else { |
| 2933 | if ((c == 0x20) || (c == 0xD) || (c == 0xA) || (c == 0x9)) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2934 | if ((len != 0) || (!normalize)) { |
| 2935 | if ((!normalize) || (!in_space)) { |
| 2936 | COPY_BUF(l,buf,len,0x20); |
| 2937 | if (len > buf_size - 10) { |
| 2938 | growBuffer(buf); |
| 2939 | } |
| 2940 | } |
| 2941 | in_space = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2942 | } |
| 2943 | } else { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2944 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2945 | COPY_BUF(l,buf,len,c); |
| 2946 | if (len > buf_size - 10) { |
| 2947 | growBuffer(buf); |
| 2948 | } |
| 2949 | } |
| 2950 | NEXTL(l); |
| 2951 | } |
| 2952 | GROW; |
| 2953 | c = CUR_CHAR(l); |
| 2954 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2955 | if ((in_space) && (normalize)) { |
| 2956 | while (buf[len - 1] == 0x20) len--; |
| 2957 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2958 | buf[len] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2959 | if (RAW == '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2960 | xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2961 | } else if (RAW != limit) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2962 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
| 2963 | "AttValue: ' expected\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2964 | } else |
| 2965 | NEXT; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2966 | if (attlen != NULL) *attlen = len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2967 | return(buf); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2968 | |
| 2969 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2970 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2971 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2975 | * xmlParseAttValue: |
| 2976 | * @ctxt: an XML parser context |
| 2977 | * |
| 2978 | * parse a value for an attribute |
| 2979 | * Note: the parser won't do substitution of entities here, this |
| 2980 | * will be handled later in xmlStringGetNodeList |
| 2981 | * |
| 2982 | * [10] AttValue ::= '"' ([^<&"] | Reference)* '"' | |
| 2983 | * "'" ([^<&'] | Reference)* "'" |
| 2984 | * |
| 2985 | * 3.3.3 Attribute-Value Normalization: |
| 2986 | * Before the value of an attribute is passed to the application or |
| 2987 | * checked for validity, the XML processor must normalize it as follows: |
| 2988 | * - a character reference is processed by appending the referenced |
| 2989 | * character to the attribute value |
| 2990 | * - an entity reference is processed by recursively processing the |
| 2991 | * replacement text of the entity |
| 2992 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 2993 | * appending #x20 to the normalized value, except that only a single |
| 2994 | * #x20 is appended for a "#xD#xA" sequence that is part of an external |
| 2995 | * parsed entity or the literal entity value of an internal parsed entity |
| 2996 | * - other characters are processed by appending them to the normalized value |
| 2997 | * If the declared value is not CDATA, then the XML processor must further |
| 2998 | * process the normalized attribute value by discarding any leading and |
| 2999 | * trailing space (#x20) characters, and by replacing sequences of space |
| 3000 | * (#x20) characters by a single space (#x20) character. |
| 3001 | * All attributes for which no declaration has been read should be treated |
| 3002 | * by a non-validating parser as if declared CDATA. |
| 3003 | * |
| 3004 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 3005 | */ |
| 3006 | |
| 3007 | |
| 3008 | xmlChar * |
| 3009 | xmlParseAttValue(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2a4fb5a | 2004-11-08 14:02:18 +0000 | [diff] [blame] | 3010 | if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 3011 | return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0)); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
| 3014 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3015 | * xmlParseSystemLiteral: |
| 3016 | * @ctxt: an XML parser context |
| 3017 | * |
| 3018 | * parse an XML Literal |
| 3019 | * |
| 3020 | * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'") |
| 3021 | * |
| 3022 | * Returns the SystemLiteral parsed or NULL |
| 3023 | */ |
| 3024 | |
| 3025 | xmlChar * |
| 3026 | xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) { |
| 3027 | xmlChar *buf = NULL; |
| 3028 | int len = 0; |
| 3029 | int size = XML_PARSER_BUFFER_SIZE; |
| 3030 | int cur, l; |
| 3031 | xmlChar stop; |
| 3032 | int state = ctxt->instate; |
| 3033 | int count = 0; |
| 3034 | |
| 3035 | SHRINK; |
| 3036 | if (RAW == '"') { |
| 3037 | NEXT; |
| 3038 | stop = '"'; |
| 3039 | } else if (RAW == '\'') { |
| 3040 | NEXT; |
| 3041 | stop = '\''; |
| 3042 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3043 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3044 | return(NULL); |
| 3045 | } |
| 3046 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3047 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3048 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3049 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3050 | return(NULL); |
| 3051 | } |
| 3052 | ctxt->instate = XML_PARSER_SYSTEM_LITERAL; |
| 3053 | cur = CUR_CHAR(l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 3054 | while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3055 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3056 | xmlChar *tmp; |
| 3057 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3058 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3059 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3060 | if (tmp == NULL) { |
| 3061 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3062 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3063 | ctxt->instate = (xmlParserInputState) state; |
| 3064 | return(NULL); |
| 3065 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3066 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3067 | } |
| 3068 | count++; |
| 3069 | if (count > 50) { |
| 3070 | GROW; |
| 3071 | count = 0; |
| 3072 | } |
| 3073 | COPY_BUF(l,buf,len,cur); |
| 3074 | NEXTL(l); |
| 3075 | cur = CUR_CHAR(l); |
| 3076 | if (cur == 0) { |
| 3077 | GROW; |
| 3078 | SHRINK; |
| 3079 | cur = CUR_CHAR(l); |
| 3080 | } |
| 3081 | } |
| 3082 | buf[len] = 0; |
| 3083 | ctxt->instate = (xmlParserInputState) state; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 3084 | if (!IS_CHAR(cur)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3085 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3086 | } else { |
| 3087 | NEXT; |
| 3088 | } |
| 3089 | return(buf); |
| 3090 | } |
| 3091 | |
| 3092 | /** |
| 3093 | * xmlParsePubidLiteral: |
| 3094 | * @ctxt: an XML parser context |
| 3095 | * |
| 3096 | * parse an XML public literal |
| 3097 | * |
| 3098 | * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'" |
| 3099 | * |
| 3100 | * Returns the PubidLiteral parsed or NULL. |
| 3101 | */ |
| 3102 | |
| 3103 | xmlChar * |
| 3104 | xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) { |
| 3105 | xmlChar *buf = NULL; |
| 3106 | int len = 0; |
| 3107 | int size = XML_PARSER_BUFFER_SIZE; |
| 3108 | xmlChar cur; |
| 3109 | xmlChar stop; |
| 3110 | int count = 0; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 3111 | xmlParserInputState oldstate = ctxt->instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3112 | |
| 3113 | SHRINK; |
| 3114 | if (RAW == '"') { |
| 3115 | NEXT; |
| 3116 | stop = '"'; |
| 3117 | } else if (RAW == '\'') { |
| 3118 | NEXT; |
| 3119 | stop = '\''; |
| 3120 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3121 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3122 | return(NULL); |
| 3123 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3124 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3125 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3126 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3127 | return(NULL); |
| 3128 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 3129 | ctxt->instate = XML_PARSER_PUBLIC_LITERAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3130 | cur = CUR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3131 | while ((IS_PUBIDCHAR_CH(cur)) && (cur != stop)) { /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3132 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3133 | xmlChar *tmp; |
| 3134 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3135 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3136 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3137 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3138 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3139 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3140 | return(NULL); |
| 3141 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3142 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3143 | } |
| 3144 | buf[len++] = cur; |
| 3145 | count++; |
| 3146 | if (count > 50) { |
| 3147 | GROW; |
| 3148 | count = 0; |
| 3149 | } |
| 3150 | NEXT; |
| 3151 | cur = CUR; |
| 3152 | if (cur == 0) { |
| 3153 | GROW; |
| 3154 | SHRINK; |
| 3155 | cur = CUR; |
| 3156 | } |
| 3157 | } |
| 3158 | buf[len] = 0; |
| 3159 | if (cur != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3160 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3161 | } else { |
| 3162 | NEXT; |
| 3163 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 3164 | ctxt->instate = oldstate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3165 | return(buf); |
| 3166 | } |
| 3167 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3168 | void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3169 | /** |
| 3170 | * xmlParseCharData: |
| 3171 | * @ctxt: an XML parser context |
| 3172 | * @cdata: int indicating whether we are within a CDATA section |
| 3173 | * |
| 3174 | * parse a CharData section. |
| 3175 | * if we are within a CDATA section ']]>' marks an end of section. |
| 3176 | * |
| 3177 | * The right angle bracket (>) may be represented using the string ">", |
| 3178 | * and must, for compatibility, be escaped using ">" or a character |
| 3179 | * reference when it appears in the string "]]>" in content, when that |
| 3180 | * string is not marking the end of a CDATA section. |
| 3181 | * |
| 3182 | * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*) |
| 3183 | */ |
| 3184 | |
| 3185 | void |
| 3186 | xmlParseCharData(xmlParserCtxtPtr ctxt, int cdata) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3187 | const xmlChar *in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3188 | int nbchar = 0; |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 3189 | int line = ctxt->input->line; |
| 3190 | int col = ctxt->input->col; |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 3191 | int ccol; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3192 | |
| 3193 | SHRINK; |
| 3194 | GROW; |
| 3195 | /* |
| 3196 | * Accelerated common case where input don't need to be |
| 3197 | * modified before passing it to the handler. |
| 3198 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3199 | if (!cdata) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3200 | in = ctxt->input->cur; |
| 3201 | do { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3202 | get_more_space: |
| 3203 | while (*in == 0x20) in++; |
| 3204 | if (*in == 0xA) { |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 3205 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3206 | in++; |
| 3207 | while (*in == 0xA) { |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 3208 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3209 | in++; |
| 3210 | } |
| 3211 | goto get_more_space; |
| 3212 | } |
| 3213 | if (*in == '<') { |
| 3214 | nbchar = in - ctxt->input->cur; |
| 3215 | if (nbchar > 0) { |
| 3216 | const xmlChar *tmp = ctxt->input->cur; |
| 3217 | ctxt->input->cur = in; |
| 3218 | |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 3219 | if ((ctxt->sax != NULL) && |
| 3220 | (ctxt->sax->ignorableWhitespace != |
| 3221 | ctxt->sax->characters)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3222 | if (areBlanks(ctxt, tmp, nbchar, 1)) { |
Daniel Veillard | 32acf0c | 2005-03-31 14:12:37 +0000 | [diff] [blame] | 3223 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 3224 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 3225 | tmp, nbchar); |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3226 | } else if (ctxt->sax->characters != NULL) |
| 3227 | ctxt->sax->characters(ctxt->userData, |
| 3228 | tmp, nbchar); |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 3229 | } else if ((ctxt->sax != NULL) && |
| 3230 | (ctxt->sax->characters != NULL)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3231 | ctxt->sax->characters(ctxt->userData, |
| 3232 | tmp, nbchar); |
| 3233 | } |
| 3234 | } |
| 3235 | return; |
| 3236 | } |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 3237 | |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 3238 | get_more: |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 3239 | ccol = ctxt->input->col; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3240 | while (((*in > ']') && (*in <= 0x7F)) || |
| 3241 | ((*in > '&') && (*in < '<')) || |
| 3242 | ((*in > '<') && (*in < ']')) || |
| 3243 | ((*in >= 0x20) && (*in < '&')) || |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 3244 | (*in == 0x09)) { |
| 3245 | in++; |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 3246 | ccol++; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 3247 | } |
Daniel Veillard | 0714c5b | 2005-01-23 00:01:01 +0000 | [diff] [blame] | 3248 | ctxt->input->col = ccol; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3249 | if (*in == 0xA) { |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 3250 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 3251 | in++; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3252 | while (*in == 0xA) { |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 3253 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 3254 | in++; |
| 3255 | } |
| 3256 | goto get_more; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3257 | } |
| 3258 | if (*in == ']') { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 3259 | if ((in[1] == ']') && (in[2] == '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3260 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 3261 | ctxt->input->cur = in; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 3262 | return; |
| 3263 | } |
| 3264 | in++; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 3265 | ctxt->input->col++; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 3266 | goto get_more; |
| 3267 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3268 | nbchar = in - ctxt->input->cur; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 3269 | if (nbchar > 0) { |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 3270 | if ((ctxt->sax != NULL) && |
| 3271 | (ctxt->sax->ignorableWhitespace != |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 3272 | ctxt->sax->characters) && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3273 | (IS_BLANK_CH(*ctxt->input->cur))) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 3274 | const xmlChar *tmp = ctxt->input->cur; |
| 3275 | ctxt->input->cur = in; |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 3276 | |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3277 | if (areBlanks(ctxt, tmp, nbchar, 0)) { |
Daniel Veillard | 32acf0c | 2005-03-31 14:12:37 +0000 | [diff] [blame] | 3278 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 3279 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 3280 | tmp, nbchar); |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 3281 | } else if (ctxt->sax->characters != NULL) |
| 3282 | ctxt->sax->characters(ctxt->userData, |
| 3283 | tmp, nbchar); |
Daniel Veillard | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 3284 | line = ctxt->input->line; |
| 3285 | col = ctxt->input->col; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 3286 | } else if (ctxt->sax != NULL) { |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 3287 | if (ctxt->sax->characters != NULL) |
| 3288 | ctxt->sax->characters(ctxt->userData, |
| 3289 | ctxt->input->cur, nbchar); |
Daniel Veillard | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 3290 | line = ctxt->input->line; |
| 3291 | col = ctxt->input->col; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 3292 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3293 | } |
| 3294 | ctxt->input->cur = in; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3295 | if (*in == 0xD) { |
| 3296 | in++; |
| 3297 | if (*in == 0xA) { |
| 3298 | ctxt->input->cur = in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3299 | in++; |
Aleksey Sanin | 8fdc32a | 2005-01-05 15:37:55 +0000 | [diff] [blame] | 3300 | ctxt->input->line++; ctxt->input->col = 1; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3301 | continue; /* while */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3302 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3303 | in--; |
| 3304 | } |
| 3305 | if (*in == '<') { |
| 3306 | return; |
| 3307 | } |
| 3308 | if (*in == '&') { |
| 3309 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3310 | } |
| 3311 | SHRINK; |
| 3312 | GROW; |
| 3313 | in = ctxt->input->cur; |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 3314 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3315 | nbchar = 0; |
| 3316 | } |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 3317 | ctxt->input->line = line; |
| 3318 | ctxt->input->col = col; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3319 | xmlParseCharDataComplex(ctxt, cdata); |
| 3320 | } |
| 3321 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3322 | /** |
| 3323 | * xmlParseCharDataComplex: |
| 3324 | * @ctxt: an XML parser context |
| 3325 | * @cdata: int indicating whether we are within a CDATA section |
| 3326 | * |
| 3327 | * parse a CharData section.this is the fallback function |
| 3328 | * of xmlParseCharData() when the parsing requires handling |
| 3329 | * of non-ASCII characters. |
| 3330 | */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3331 | void |
| 3332 | xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3333 | xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5]; |
| 3334 | int nbchar = 0; |
| 3335 | int cur, l; |
| 3336 | int count = 0; |
| 3337 | |
| 3338 | SHRINK; |
| 3339 | GROW; |
| 3340 | cur = CUR_CHAR(l); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3341 | while ((cur != '<') && /* checked */ |
| 3342 | (cur != '&') && |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 3343 | (IS_CHAR(cur))) /* test also done in xmlCurrentChar() */ { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3344 | if ((cur == ']') && (NXT(1) == ']') && |
| 3345 | (NXT(2) == '>')) { |
| 3346 | if (cdata) break; |
| 3347 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3348 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3349 | } |
| 3350 | } |
| 3351 | COPY_BUF(l,buf,nbchar,cur); |
| 3352 | if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 3353 | buf[nbchar] = 0; |
| 3354 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3355 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3356 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3357 | */ |
| 3358 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3359 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3360 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 3361 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 3362 | buf, nbchar); |
| 3363 | } else { |
| 3364 | if (ctxt->sax->characters != NULL) |
| 3365 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
| 3366 | } |
| 3367 | } |
| 3368 | nbchar = 0; |
| 3369 | } |
| 3370 | count++; |
| 3371 | if (count > 50) { |
| 3372 | GROW; |
| 3373 | count = 0; |
| 3374 | } |
| 3375 | NEXTL(l); |
| 3376 | cur = CUR_CHAR(l); |
| 3377 | } |
| 3378 | if (nbchar != 0) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 3379 | buf[nbchar] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3380 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3381 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3382 | */ |
| 3383 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3384 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3385 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 3386 | ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar); |
| 3387 | } else { |
| 3388 | if (ctxt->sax->characters != NULL) |
| 3389 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
| 3390 | } |
| 3391 | } |
| 3392 | } |
| 3393 | } |
| 3394 | |
| 3395 | /** |
| 3396 | * xmlParseExternalID: |
| 3397 | * @ctxt: an XML parser context |
| 3398 | * @publicID: a xmlChar** receiving PubidLiteral |
| 3399 | * @strict: indicate whether we should restrict parsing to only |
| 3400 | * production [75], see NOTE below |
| 3401 | * |
| 3402 | * Parse an External ID or a Public ID |
| 3403 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3404 | * NOTE: Productions [75] and [83] interact badly since [75] can generate |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3405 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 3406 | * |
| 3407 | * [75] ExternalID ::= 'SYSTEM' S SystemLiteral |
| 3408 | * | 'PUBLIC' S PubidLiteral S SystemLiteral |
| 3409 | * |
| 3410 | * [83] PublicID ::= 'PUBLIC' S PubidLiteral |
| 3411 | * |
| 3412 | * Returns the function returns SystemLiteral and in the second |
| 3413 | * case publicID receives PubidLiteral, is strict is off |
| 3414 | * it is possible to return NULL and have publicID set. |
| 3415 | */ |
| 3416 | |
| 3417 | xmlChar * |
| 3418 | xmlParseExternalID(xmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) { |
| 3419 | xmlChar *URI = NULL; |
| 3420 | |
| 3421 | SHRINK; |
Daniel Veillard | 146c912 | 2001-03-22 15:22:27 +0000 | [diff] [blame] | 3422 | |
| 3423 | *publicID = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 3424 | if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3425 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3426 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3427 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 3428 | "Space required after 'SYSTEM'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3429 | } |
| 3430 | SKIP_BLANKS; |
| 3431 | URI = xmlParseSystemLiteral(ctxt); |
| 3432 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3433 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3434 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 3435 | } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3436 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3437 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3438 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3439 | "Space required after 'PUBLIC'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3440 | } |
| 3441 | SKIP_BLANKS; |
| 3442 | *publicID = xmlParsePubidLiteral(ctxt); |
| 3443 | if (*publicID == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3444 | xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3445 | } |
| 3446 | if (strict) { |
| 3447 | /* |
| 3448 | * We don't handle [83] so "S SystemLiteral" is required. |
| 3449 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3450 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3451 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3452 | "Space required after the Public Identifier\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3453 | } |
| 3454 | } else { |
| 3455 | /* |
| 3456 | * We handle [83] so we return immediately, if |
| 3457 | * "S SystemLiteral" is not detected. From a purely parsing |
| 3458 | * point of view that's a nice mess. |
| 3459 | */ |
| 3460 | const xmlChar *ptr; |
| 3461 | GROW; |
| 3462 | |
| 3463 | ptr = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3464 | if (!IS_BLANK_CH(*ptr)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3465 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3466 | while (IS_BLANK_CH(*ptr)) ptr++; /* TODO: dangerous, fix ! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3467 | if ((*ptr != '\'') && (*ptr != '"')) return(NULL); |
| 3468 | } |
| 3469 | SKIP_BLANKS; |
| 3470 | URI = xmlParseSystemLiteral(ctxt); |
| 3471 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3472 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3473 | } |
| 3474 | } |
| 3475 | return(URI); |
| 3476 | } |
| 3477 | |
| 3478 | /** |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 3479 | * xmlParseCommentComplex: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3480 | * @ctxt: an XML parser context |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 3481 | * @buf: the already parsed part of the buffer |
| 3482 | * @len: number of bytes filles in the buffer |
| 3483 | * @size: allocated size of the buffer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3484 | * |
| 3485 | * Skip an XML (SGML) comment <!-- .... --> |
| 3486 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 3487 | * must not occur within comments. " |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 3488 | * 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] | 3489 | * |
| 3490 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 3491 | */ |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 3492 | static void |
| 3493 | xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf, int len, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3494 | int q, ql; |
| 3495 | int r, rl; |
| 3496 | int cur, l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3497 | xmlParserInputPtr input = ctxt->input; |
| 3498 | int count = 0; |
| 3499 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3500 | if (buf == NULL) { |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 3501 | len = 0; |
| 3502 | size = XML_PARSER_BUFFER_SIZE; |
| 3503 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 3504 | if (buf == NULL) { |
| 3505 | xmlErrMemory(ctxt, NULL); |
| 3506 | return; |
| 3507 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3508 | } |
| 3509 | q = CUR_CHAR(ql); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3510 | if (q == 0) |
| 3511 | goto not_terminated; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3512 | NEXTL(ql); |
| 3513 | r = CUR_CHAR(rl); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3514 | if (r == 0) |
| 3515 | goto not_terminated; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3516 | NEXTL(rl); |
| 3517 | cur = CUR_CHAR(l); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3518 | if (cur == 0) |
| 3519 | goto not_terminated; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 3520 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3521 | ((cur != '>') || |
| 3522 | (r != '-') || (q != '-'))) { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 3523 | if ((r == '-') && (q == '-')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3524 | xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3525 | } |
| 3526 | if (len + 5 >= size) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 3527 | xmlChar *new_buf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3528 | size *= 2; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 3529 | new_buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3530 | if (new_buf == NULL) { |
| 3531 | xmlFree (buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3532 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3533 | return; |
| 3534 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 3535 | buf = new_buf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3536 | } |
| 3537 | COPY_BUF(ql,buf,len,q); |
| 3538 | q = r; |
| 3539 | ql = rl; |
| 3540 | r = cur; |
| 3541 | rl = l; |
| 3542 | |
| 3543 | count++; |
| 3544 | if (count > 50) { |
| 3545 | GROW; |
| 3546 | count = 0; |
| 3547 | } |
| 3548 | NEXTL(l); |
| 3549 | cur = CUR_CHAR(l); |
| 3550 | if (cur == 0) { |
| 3551 | SHRINK; |
| 3552 | GROW; |
| 3553 | cur = CUR_CHAR(l); |
| 3554 | } |
| 3555 | } |
| 3556 | buf[len] = 0; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 3557 | if (!IS_CHAR(cur)) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3558 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3559 | "Comment not terminated \n<!--%.50s\n", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3560 | xmlFree(buf); |
| 3561 | } else { |
| 3562 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3563 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 3564 | "Comment doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3565 | } |
| 3566 | NEXT; |
| 3567 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 3568 | (!ctxt->disableSAX)) |
| 3569 | ctxt->sax->comment(ctxt->userData, buf); |
| 3570 | xmlFree(buf); |
| 3571 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3572 | return; |
| 3573 | not_terminated: |
| 3574 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 3575 | "Comment not terminated\n", NULL); |
| 3576 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3577 | } |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 3578 | /** |
| 3579 | * xmlParseComment: |
| 3580 | * @ctxt: an XML parser context |
| 3581 | * |
| 3582 | * Skip an XML (SGML) comment <!-- .... --> |
| 3583 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 3584 | * must not occur within comments. " |
| 3585 | * |
| 3586 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 3587 | */ |
| 3588 | void |
| 3589 | xmlParseComment(xmlParserCtxtPtr ctxt) { |
| 3590 | xmlChar *buf = NULL; |
| 3591 | int size = XML_PARSER_BUFFER_SIZE; |
Daniel Veillard | 316a5c3 | 2005-01-23 22:56:39 +0000 | [diff] [blame] | 3592 | int len = 0; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 3593 | xmlParserInputState state; |
| 3594 | const xmlChar *in; |
| 3595 | int nbchar = 0, ccol; |
| 3596 | |
| 3597 | /* |
| 3598 | * Check that there is a comment right here. |
| 3599 | */ |
| 3600 | if ((RAW != '<') || (NXT(1) != '!') || |
| 3601 | (NXT(2) != '-') || (NXT(3) != '-')) return; |
| 3602 | |
| 3603 | state = ctxt->instate; |
| 3604 | ctxt->instate = XML_PARSER_COMMENT; |
| 3605 | SKIP(4); |
| 3606 | SHRINK; |
| 3607 | GROW; |
| 3608 | |
| 3609 | /* |
| 3610 | * Accelerated common case where input don't need to be |
| 3611 | * modified before passing it to the handler. |
| 3612 | */ |
| 3613 | in = ctxt->input->cur; |
| 3614 | do { |
| 3615 | if (*in == 0xA) { |
| 3616 | ctxt->input->line++; ctxt->input->col = 1; |
| 3617 | in++; |
| 3618 | while (*in == 0xA) { |
| 3619 | ctxt->input->line++; ctxt->input->col = 1; |
| 3620 | in++; |
| 3621 | } |
| 3622 | } |
| 3623 | get_more: |
| 3624 | ccol = ctxt->input->col; |
| 3625 | while (((*in > '-') && (*in <= 0x7F)) || |
| 3626 | ((*in >= 0x20) && (*in < '-')) || |
| 3627 | (*in == 0x09)) { |
| 3628 | in++; |
| 3629 | ccol++; |
| 3630 | } |
| 3631 | ctxt->input->col = ccol; |
| 3632 | if (*in == 0xA) { |
| 3633 | ctxt->input->line++; ctxt->input->col = 1; |
| 3634 | in++; |
| 3635 | while (*in == 0xA) { |
| 3636 | ctxt->input->line++; ctxt->input->col = 1; |
| 3637 | in++; |
| 3638 | } |
| 3639 | goto get_more; |
| 3640 | } |
| 3641 | nbchar = in - ctxt->input->cur; |
| 3642 | /* |
| 3643 | * save current set of data |
| 3644 | */ |
| 3645 | if (nbchar > 0) { |
| 3646 | if ((ctxt->sax != NULL) && |
| 3647 | (ctxt->sax->comment != NULL)) { |
| 3648 | if (buf == NULL) { |
| 3649 | if ((*in == '-') && (in[1] == '-')) |
| 3650 | size = nbchar + 1; |
| 3651 | else |
| 3652 | size = XML_PARSER_BUFFER_SIZE + nbchar; |
| 3653 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
| 3654 | if (buf == NULL) { |
| 3655 | xmlErrMemory(ctxt, NULL); |
| 3656 | ctxt->instate = state; |
| 3657 | return; |
| 3658 | } |
| 3659 | len = 0; |
| 3660 | } else if (len + nbchar + 1 >= size) { |
| 3661 | xmlChar *new_buf; |
| 3662 | size += len + nbchar + XML_PARSER_BUFFER_SIZE; |
| 3663 | new_buf = (xmlChar *) xmlRealloc(buf, |
| 3664 | size * sizeof(xmlChar)); |
| 3665 | if (new_buf == NULL) { |
| 3666 | xmlFree (buf); |
| 3667 | xmlErrMemory(ctxt, NULL); |
| 3668 | ctxt->instate = state; |
| 3669 | return; |
| 3670 | } |
| 3671 | buf = new_buf; |
| 3672 | } |
| 3673 | memcpy(&buf[len], ctxt->input->cur, nbchar); |
| 3674 | len += nbchar; |
| 3675 | buf[len] = 0; |
| 3676 | } |
| 3677 | } |
| 3678 | ctxt->input->cur = in; |
| 3679 | if (*in == 0xA) |
| 3680 | |
| 3681 | if (*in == 0xD) { |
| 3682 | in++; |
| 3683 | if (*in == 0xA) { |
| 3684 | ctxt->input->cur = in; |
| 3685 | in++; |
| 3686 | ctxt->input->line++; ctxt->input->col = 1; |
| 3687 | continue; /* while */ |
| 3688 | } |
| 3689 | in--; |
| 3690 | } |
| 3691 | SHRINK; |
| 3692 | GROW; |
| 3693 | in = ctxt->input->cur; |
| 3694 | if (*in == '-') { |
| 3695 | if (in[1] == '-') { |
| 3696 | if (in[2] == '>') { |
| 3697 | SKIP(3); |
| 3698 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 3699 | (!ctxt->disableSAX)) { |
| 3700 | if (buf != NULL) |
| 3701 | ctxt->sax->comment(ctxt->userData, buf); |
| 3702 | else |
| 3703 | ctxt->sax->comment(ctxt->userData, BAD_CAST ""); |
| 3704 | } |
| 3705 | if (buf != NULL) |
| 3706 | xmlFree(buf); |
| 3707 | ctxt->instate = state; |
| 3708 | return; |
| 3709 | } |
| 3710 | if (buf != NULL) |
| 3711 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 3712 | "Comment not terminated \n<!--%.50s\n", |
| 3713 | buf); |
| 3714 | else |
| 3715 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 3716 | "Comment not terminated \n", NULL); |
| 3717 | in++; |
| 3718 | ctxt->input->col++; |
| 3719 | } |
| 3720 | in++; |
| 3721 | ctxt->input->col++; |
| 3722 | goto get_more; |
| 3723 | } |
| 3724 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
| 3725 | xmlParseCommentComplex(ctxt, buf, len, size); |
| 3726 | ctxt->instate = state; |
| 3727 | return; |
| 3728 | } |
| 3729 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3730 | |
| 3731 | /** |
| 3732 | * xmlParsePITarget: |
| 3733 | * @ctxt: an XML parser context |
| 3734 | * |
| 3735 | * parse the name of a PI |
| 3736 | * |
| 3737 | * [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l')) |
| 3738 | * |
| 3739 | * Returns the PITarget name or NULL |
| 3740 | */ |
| 3741 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3742 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3743 | xmlParsePITarget(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3744 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3745 | |
| 3746 | name = xmlParseName(ctxt); |
| 3747 | if ((name != NULL) && |
| 3748 | ((name[0] == 'x') || (name[0] == 'X')) && |
| 3749 | ((name[1] == 'm') || (name[1] == 'M')) && |
| 3750 | ((name[2] == 'l') || (name[2] == 'L'))) { |
| 3751 | int i; |
| 3752 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 3753 | (name[2] == 'l') && (name[3] == 0)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3754 | xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3755 | "XML declaration allowed only at the start of the document\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3756 | return(name); |
| 3757 | } else if (name[3] == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3758 | xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3759 | return(name); |
| 3760 | } |
| 3761 | for (i = 0;;i++) { |
| 3762 | if (xmlW3CPIs[i] == NULL) break; |
| 3763 | if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i])) |
| 3764 | return(name); |
| 3765 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 3766 | xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
| 3767 | "xmlParsePITarget: invalid name prefix 'xml'\n", |
| 3768 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3769 | } |
| 3770 | return(name); |
| 3771 | } |
| 3772 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3773 | #ifdef LIBXML_CATALOG_ENABLED |
| 3774 | /** |
| 3775 | * xmlParseCatalogPI: |
| 3776 | * @ctxt: an XML parser context |
| 3777 | * @catalog: the PI value string |
| 3778 | * |
| 3779 | * parse an XML Catalog Processing Instruction. |
| 3780 | * |
| 3781 | * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?> |
| 3782 | * |
| 3783 | * Occurs only if allowed by the user and if happening in the Misc |
| 3784 | * part of the document before any doctype informations |
| 3785 | * This will add the given catalog to the parsing context in order |
| 3786 | * to be used if there is a resolution need further down in the document |
| 3787 | */ |
| 3788 | |
| 3789 | static void |
| 3790 | xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) { |
| 3791 | xmlChar *URL = NULL; |
| 3792 | const xmlChar *tmp, *base; |
| 3793 | xmlChar marker; |
| 3794 | |
| 3795 | tmp = catalog; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3796 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3797 | if (xmlStrncmp(tmp, BAD_CAST"catalog", 7)) |
| 3798 | goto error; |
| 3799 | tmp += 7; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3800 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3801 | if (*tmp != '=') { |
| 3802 | return; |
| 3803 | } |
| 3804 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3805 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3806 | marker = *tmp; |
| 3807 | if ((marker != '\'') && (marker != '"')) |
| 3808 | goto error; |
| 3809 | tmp++; |
| 3810 | base = tmp; |
| 3811 | while ((*tmp != 0) && (*tmp != marker)) tmp++; |
| 3812 | if (*tmp == 0) |
| 3813 | goto error; |
| 3814 | URL = xmlStrndup(base, tmp - base); |
| 3815 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3816 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3817 | if (*tmp != 0) |
| 3818 | goto error; |
| 3819 | |
| 3820 | if (URL != NULL) { |
| 3821 | ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL); |
| 3822 | xmlFree(URL); |
| 3823 | } |
| 3824 | return; |
| 3825 | |
| 3826 | error: |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 3827 | xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI, |
| 3828 | "Catalog PI syntax error: %s\n", |
| 3829 | catalog, NULL); |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3830 | if (URL != NULL) |
| 3831 | xmlFree(URL); |
| 3832 | } |
| 3833 | #endif |
| 3834 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3835 | /** |
| 3836 | * xmlParsePI: |
| 3837 | * @ctxt: an XML parser context |
| 3838 | * |
| 3839 | * parse an XML Processing Instruction. |
| 3840 | * |
| 3841 | * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>' |
| 3842 | * |
| 3843 | * The processing is transfered to SAX once parsed. |
| 3844 | */ |
| 3845 | |
| 3846 | void |
| 3847 | xmlParsePI(xmlParserCtxtPtr ctxt) { |
| 3848 | xmlChar *buf = NULL; |
| 3849 | int len = 0; |
| 3850 | int size = XML_PARSER_BUFFER_SIZE; |
| 3851 | int cur, l; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3852 | const xmlChar *target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3853 | xmlParserInputState state; |
| 3854 | int count = 0; |
| 3855 | |
| 3856 | if ((RAW == '<') && (NXT(1) == '?')) { |
| 3857 | xmlParserInputPtr input = ctxt->input; |
| 3858 | state = ctxt->instate; |
| 3859 | ctxt->instate = XML_PARSER_PI; |
| 3860 | /* |
| 3861 | * this is a Processing Instruction. |
| 3862 | */ |
| 3863 | SKIP(2); |
| 3864 | SHRINK; |
| 3865 | |
| 3866 | /* |
| 3867 | * Parse the target name and check for special support like |
| 3868 | * namespace. |
| 3869 | */ |
| 3870 | target = xmlParsePITarget(ctxt); |
| 3871 | if (target != NULL) { |
| 3872 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 3873 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3874 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 3875 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3876 | } |
| 3877 | SKIP(2); |
| 3878 | |
| 3879 | /* |
| 3880 | * SAX: PI detected. |
| 3881 | */ |
| 3882 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 3883 | (ctxt->sax->processingInstruction != NULL)) |
| 3884 | ctxt->sax->processingInstruction(ctxt->userData, |
| 3885 | target, NULL); |
| 3886 | ctxt->instate = state; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3887 | return; |
| 3888 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3889 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3890 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3891 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3892 | ctxt->instate = state; |
| 3893 | return; |
| 3894 | } |
| 3895 | cur = CUR; |
| 3896 | if (!IS_BLANK(cur)) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3897 | xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED, |
| 3898 | "ParsePI: PI %s space expected\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3899 | } |
| 3900 | SKIP_BLANKS; |
| 3901 | cur = CUR_CHAR(l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 3902 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3903 | ((cur != '?') || (NXT(1) != '>'))) { |
| 3904 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3905 | xmlChar *tmp; |
| 3906 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3907 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3908 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3909 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3910 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3911 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3912 | ctxt->instate = state; |
| 3913 | return; |
| 3914 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 3915 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3916 | } |
| 3917 | count++; |
| 3918 | if (count > 50) { |
| 3919 | GROW; |
| 3920 | count = 0; |
| 3921 | } |
| 3922 | COPY_BUF(l,buf,len,cur); |
| 3923 | NEXTL(l); |
| 3924 | cur = CUR_CHAR(l); |
| 3925 | if (cur == 0) { |
| 3926 | SHRINK; |
| 3927 | GROW; |
| 3928 | cur = CUR_CHAR(l); |
| 3929 | } |
| 3930 | } |
| 3931 | buf[len] = 0; |
| 3932 | if (cur != '?') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3933 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 3934 | "ParsePI: PI %s never end ...\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3935 | } else { |
| 3936 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3937 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 3938 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3939 | } |
| 3940 | SKIP(2); |
| 3941 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3942 | #ifdef LIBXML_CATALOG_ENABLED |
| 3943 | if (((state == XML_PARSER_MISC) || |
| 3944 | (state == XML_PARSER_START)) && |
| 3945 | (xmlStrEqual(target, XML_CATALOG_PI))) { |
| 3946 | xmlCatalogAllow allow = xmlCatalogGetDefaults(); |
| 3947 | if ((allow == XML_CATA_ALLOW_DOCUMENT) || |
| 3948 | (allow == XML_CATA_ALLOW_ALL)) |
| 3949 | xmlParseCatalogPI(ctxt, buf); |
| 3950 | } |
| 3951 | #endif |
| 3952 | |
| 3953 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3954 | /* |
| 3955 | * SAX: PI detected. |
| 3956 | */ |
| 3957 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 3958 | (ctxt->sax->processingInstruction != NULL)) |
| 3959 | ctxt->sax->processingInstruction(ctxt->userData, |
| 3960 | target, buf); |
| 3961 | } |
| 3962 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3963 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3964 | xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3965 | } |
| 3966 | ctxt->instate = state; |
| 3967 | } |
| 3968 | } |
| 3969 | |
| 3970 | /** |
| 3971 | * xmlParseNotationDecl: |
| 3972 | * @ctxt: an XML parser context |
| 3973 | * |
| 3974 | * parse a notation declaration |
| 3975 | * |
| 3976 | * [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>' |
| 3977 | * |
| 3978 | * Hence there is actually 3 choices: |
| 3979 | * 'PUBLIC' S PubidLiteral |
| 3980 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 3981 | * and 'SYSTEM' S SystemLiteral |
| 3982 | * |
| 3983 | * See the NOTE on xmlParseExternalID(). |
| 3984 | */ |
| 3985 | |
| 3986 | void |
| 3987 | xmlParseNotationDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3988 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3989 | xmlChar *Pubid; |
| 3990 | xmlChar *Systemid; |
| 3991 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 3992 | if (CMP10(CUR_PTR, '<', '!', 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3993 | xmlParserInputPtr input = ctxt->input; |
| 3994 | SHRINK; |
| 3995 | SKIP(10); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3996 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3997 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 3998 | "Space required after '<!NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3999 | return; |
| 4000 | } |
| 4001 | SKIP_BLANKS; |
| 4002 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4003 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4004 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4005 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4006 | return; |
| 4007 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4008 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4009 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4010 | "Space required after the NOTATION name'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4011 | return; |
| 4012 | } |
| 4013 | SKIP_BLANKS; |
| 4014 | |
| 4015 | /* |
| 4016 | * Parse the IDs. |
| 4017 | */ |
| 4018 | Systemid = xmlParseExternalID(ctxt, &Pubid, 0); |
| 4019 | SKIP_BLANKS; |
| 4020 | |
| 4021 | if (RAW == '>') { |
| 4022 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4023 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4024 | "Notation declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4025 | } |
| 4026 | NEXT; |
| 4027 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 4028 | (ctxt->sax->notationDecl != NULL)) |
| 4029 | ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid); |
| 4030 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4031 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4032 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4033 | if (Systemid != NULL) xmlFree(Systemid); |
| 4034 | if (Pubid != NULL) xmlFree(Pubid); |
| 4035 | } |
| 4036 | } |
| 4037 | |
| 4038 | /** |
| 4039 | * xmlParseEntityDecl: |
| 4040 | * @ctxt: an XML parser context |
| 4041 | * |
| 4042 | * parse <!ENTITY declarations |
| 4043 | * |
| 4044 | * [70] EntityDecl ::= GEDecl | PEDecl |
| 4045 | * |
| 4046 | * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>' |
| 4047 | * |
| 4048 | * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>' |
| 4049 | * |
| 4050 | * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?) |
| 4051 | * |
| 4052 | * [74] PEDef ::= EntityValue | ExternalID |
| 4053 | * |
| 4054 | * [76] NDataDecl ::= S 'NDATA' S Name |
| 4055 | * |
| 4056 | * [ VC: Notation Declared ] |
| 4057 | * The Name must match the declared name of a notation. |
| 4058 | */ |
| 4059 | |
| 4060 | void |
| 4061 | xmlParseEntityDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4062 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4063 | xmlChar *value = NULL; |
| 4064 | xmlChar *URI = NULL, *literal = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4065 | const xmlChar *ndata = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4066 | int isParameter = 0; |
| 4067 | xmlChar *orig = NULL; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 4068 | int skipped; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4069 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 4070 | /* GROW; done in the caller */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4071 | if (CMP8(CUR_PTR, '<', '!', 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4072 | xmlParserInputPtr input = ctxt->input; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4073 | SHRINK; |
| 4074 | SKIP(8); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 4075 | skipped = SKIP_BLANKS; |
| 4076 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4077 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4078 | "Space required after '<!ENTITY'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4079 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4080 | |
| 4081 | if (RAW == '%') { |
| 4082 | NEXT; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 4083 | skipped = SKIP_BLANKS; |
| 4084 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4085 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4086 | "Space required after '%'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4087 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4088 | isParameter = 1; |
| 4089 | } |
| 4090 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4091 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4092 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4093 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 4094 | "xmlParseEntityDecl: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4095 | return; |
| 4096 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 4097 | skipped = SKIP_BLANKS; |
| 4098 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4099 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4100 | "Space required after the entity name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4101 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4102 | |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 4103 | ctxt->instate = XML_PARSER_ENTITY_DECL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4104 | /* |
| 4105 | * handle the various case of definitions... |
| 4106 | */ |
| 4107 | if (isParameter) { |
| 4108 | if ((RAW == '"') || (RAW == '\'')) { |
| 4109 | value = xmlParseEntityValue(ctxt, &orig); |
| 4110 | if (value) { |
| 4111 | if ((ctxt->sax != NULL) && |
| 4112 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 4113 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 4114 | XML_INTERNAL_PARAMETER_ENTITY, |
| 4115 | NULL, NULL, value); |
| 4116 | } |
| 4117 | } else { |
| 4118 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 4119 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4120 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4121 | } |
| 4122 | if (URI) { |
| 4123 | xmlURIPtr uri; |
| 4124 | |
| 4125 | uri = xmlParseURI((const char *) URI); |
| 4126 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4127 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 4128 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4129 | /* |
| 4130 | * This really ought to be a well formedness error |
| 4131 | * but the XML Core WG decided otherwise c.f. issue |
| 4132 | * E26 of the XML erratas. |
| 4133 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4134 | } else { |
| 4135 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4136 | /* |
| 4137 | * Okay this is foolish to block those but not |
| 4138 | * invalid URIs. |
| 4139 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4140 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4141 | } else { |
| 4142 | if ((ctxt->sax != NULL) && |
| 4143 | (!ctxt->disableSAX) && |
| 4144 | (ctxt->sax->entityDecl != NULL)) |
| 4145 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 4146 | XML_EXTERNAL_PARAMETER_ENTITY, |
| 4147 | literal, URI, NULL); |
| 4148 | } |
| 4149 | xmlFreeURI(uri); |
| 4150 | } |
| 4151 | } |
| 4152 | } |
| 4153 | } else { |
| 4154 | if ((RAW == '"') || (RAW == '\'')) { |
| 4155 | value = xmlParseEntityValue(ctxt, &orig); |
| 4156 | if ((ctxt->sax != NULL) && |
| 4157 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 4158 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 4159 | XML_INTERNAL_GENERAL_ENTITY, |
| 4160 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 4161 | /* |
| 4162 | * For expat compatibility in SAX mode. |
| 4163 | */ |
| 4164 | if ((ctxt->myDoc == NULL) || |
| 4165 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 4166 | if (ctxt->myDoc == NULL) { |
| 4167 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
| 4168 | } |
| 4169 | if (ctxt->myDoc->intSubset == NULL) |
| 4170 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 4171 | BAD_CAST "fake", NULL, NULL); |
| 4172 | |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 4173 | xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY, |
| 4174 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 4175 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4176 | } else { |
| 4177 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 4178 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4179 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4180 | } |
| 4181 | if (URI) { |
| 4182 | xmlURIPtr uri; |
| 4183 | |
| 4184 | uri = xmlParseURI((const char *)URI); |
| 4185 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4186 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 4187 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4188 | /* |
| 4189 | * This really ought to be a well formedness error |
| 4190 | * but the XML Core WG decided otherwise c.f. issue |
| 4191 | * E26 of the XML erratas. |
| 4192 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4193 | } else { |
| 4194 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 4195 | /* |
| 4196 | * Okay this is foolish to block those but not |
| 4197 | * invalid URIs. |
| 4198 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4199 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4200 | } |
| 4201 | xmlFreeURI(uri); |
| 4202 | } |
| 4203 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4204 | if ((RAW != '>') && (!IS_BLANK_CH(CUR))) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4205 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4206 | "Space required before 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4207 | } |
| 4208 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4209 | if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4210 | SKIP(5); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4211 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4212 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4213 | "Space required after 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4214 | } |
| 4215 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4216 | ndata = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4217 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 4218 | (ctxt->sax->unparsedEntityDecl != NULL)) |
| 4219 | ctxt->sax->unparsedEntityDecl(ctxt->userData, name, |
| 4220 | literal, URI, ndata); |
| 4221 | } else { |
| 4222 | if ((ctxt->sax != NULL) && |
| 4223 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 4224 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 4225 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 4226 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 4227 | /* |
| 4228 | * For expat compatibility in SAX mode. |
| 4229 | * assuming the entity repalcement was asked for |
| 4230 | */ |
| 4231 | if ((ctxt->replaceEntities != 0) && |
| 4232 | ((ctxt->myDoc == NULL) || |
| 4233 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) { |
| 4234 | if (ctxt->myDoc == NULL) { |
| 4235 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
| 4236 | } |
| 4237 | |
| 4238 | if (ctxt->myDoc->intSubset == NULL) |
| 4239 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 4240 | BAD_CAST "fake", NULL, NULL); |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 4241 | xmlSAX2EntityDecl(ctxt, name, |
| 4242 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 4243 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 4244 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4245 | } |
| 4246 | } |
| 4247 | } |
| 4248 | SKIP_BLANKS; |
| 4249 | if (RAW != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 4250 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4251 | "xmlParseEntityDecl: entity %s not terminated\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4252 | } else { |
| 4253 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4254 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4255 | "Entity declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4256 | } |
| 4257 | NEXT; |
| 4258 | } |
| 4259 | if (orig != NULL) { |
| 4260 | /* |
| 4261 | * Ugly mechanism to save the raw entity value. |
| 4262 | */ |
| 4263 | xmlEntityPtr cur = NULL; |
| 4264 | |
| 4265 | if (isParameter) { |
| 4266 | if ((ctxt->sax != NULL) && |
| 4267 | (ctxt->sax->getParameterEntity != NULL)) |
| 4268 | cur = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 4269 | } else { |
| 4270 | if ((ctxt->sax != NULL) && |
| 4271 | (ctxt->sax->getEntity != NULL)) |
| 4272 | cur = ctxt->sax->getEntity(ctxt->userData, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 4273 | if ((cur == NULL) && (ctxt->userData==ctxt)) { |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 4274 | cur = xmlSAX2GetEntity(ctxt, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 4275 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4276 | } |
| 4277 | if (cur != NULL) { |
| 4278 | if (cur->orig != NULL) |
| 4279 | xmlFree(orig); |
| 4280 | else |
| 4281 | cur->orig = orig; |
| 4282 | } else |
| 4283 | xmlFree(orig); |
| 4284 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4285 | if (value != NULL) xmlFree(value); |
| 4286 | if (URI != NULL) xmlFree(URI); |
| 4287 | if (literal != NULL) xmlFree(literal); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4288 | } |
| 4289 | } |
| 4290 | |
| 4291 | /** |
| 4292 | * xmlParseDefaultDecl: |
| 4293 | * @ctxt: an XML parser context |
| 4294 | * @value: Receive a possible fixed default value for the attribute |
| 4295 | * |
| 4296 | * Parse an attribute default declaration |
| 4297 | * |
| 4298 | * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue) |
| 4299 | * |
| 4300 | * [ VC: Required Attribute ] |
| 4301 | * if the default declaration is the keyword #REQUIRED, then the |
| 4302 | * attribute must be specified for all elements of the type in the |
| 4303 | * attribute-list declaration. |
| 4304 | * |
| 4305 | * [ VC: Attribute Default Legal ] |
| 4306 | * The declared default value must meet the lexical constraints of |
| 4307 | * the declared attribute type c.f. xmlValidateAttributeDecl() |
| 4308 | * |
| 4309 | * [ VC: Fixed Attribute Default ] |
| 4310 | * if an attribute has a default value declared with the #FIXED |
| 4311 | * keyword, instances of that attribute must match the default value. |
| 4312 | * |
| 4313 | * [ WFC: No < in Attribute Values ] |
| 4314 | * handled in xmlParseAttValue() |
| 4315 | * |
| 4316 | * returns: XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED |
| 4317 | * or XML_ATTRIBUTE_FIXED. |
| 4318 | */ |
| 4319 | |
| 4320 | int |
| 4321 | xmlParseDefaultDecl(xmlParserCtxtPtr ctxt, xmlChar **value) { |
| 4322 | int val; |
| 4323 | xmlChar *ret; |
| 4324 | |
| 4325 | *value = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4326 | if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4327 | SKIP(9); |
| 4328 | return(XML_ATTRIBUTE_REQUIRED); |
| 4329 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4330 | if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4331 | SKIP(8); |
| 4332 | return(XML_ATTRIBUTE_IMPLIED); |
| 4333 | } |
| 4334 | val = XML_ATTRIBUTE_NONE; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4335 | if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4336 | SKIP(6); |
| 4337 | val = XML_ATTRIBUTE_FIXED; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4338 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4339 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4340 | "Space required after '#FIXED'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4341 | } |
| 4342 | SKIP_BLANKS; |
| 4343 | } |
| 4344 | ret = xmlParseAttValue(ctxt); |
| 4345 | ctxt->instate = XML_PARSER_DTD; |
| 4346 | if (ret == NULL) { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 4347 | xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4348 | "Attribute default value declaration error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4349 | } else |
| 4350 | *value = ret; |
| 4351 | return(val); |
| 4352 | } |
| 4353 | |
| 4354 | /** |
| 4355 | * xmlParseNotationType: |
| 4356 | * @ctxt: an XML parser context |
| 4357 | * |
| 4358 | * parse an Notation attribute type. |
| 4359 | * |
| 4360 | * Note: the leading 'NOTATION' S part has already being parsed... |
| 4361 | * |
| 4362 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 4363 | * |
| 4364 | * [ VC: Notation Attributes ] |
| 4365 | * Values of this type must match one of the notation names included |
| 4366 | * in the declaration; all notation names in the declaration must be declared. |
| 4367 | * |
| 4368 | * Returns: the notation attribute tree built while parsing |
| 4369 | */ |
| 4370 | |
| 4371 | xmlEnumerationPtr |
| 4372 | xmlParseNotationType(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4373 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4374 | xmlEnumerationPtr ret = NULL, last = NULL, cur; |
| 4375 | |
| 4376 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4377 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4378 | return(NULL); |
| 4379 | } |
| 4380 | SHRINK; |
| 4381 | do { |
| 4382 | NEXT; |
| 4383 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4384 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4385 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4386 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 4387 | "Name expected in NOTATION declaration\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4388 | return(ret); |
| 4389 | } |
| 4390 | cur = xmlCreateEnumeration(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4391 | if (cur == NULL) return(ret); |
| 4392 | if (last == NULL) ret = last = cur; |
| 4393 | else { |
| 4394 | last->next = cur; |
| 4395 | last = cur; |
| 4396 | } |
| 4397 | SKIP_BLANKS; |
| 4398 | } while (RAW == '|'); |
| 4399 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4400 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4401 | if ((last != NULL) && (last != ret)) |
| 4402 | xmlFreeEnumeration(last); |
| 4403 | return(ret); |
| 4404 | } |
| 4405 | NEXT; |
| 4406 | return(ret); |
| 4407 | } |
| 4408 | |
| 4409 | /** |
| 4410 | * xmlParseEnumerationType: |
| 4411 | * @ctxt: an XML parser context |
| 4412 | * |
| 4413 | * parse an Enumeration attribute type. |
| 4414 | * |
| 4415 | * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')' |
| 4416 | * |
| 4417 | * [ VC: Enumeration ] |
| 4418 | * Values of this type must match one of the Nmtoken tokens in |
| 4419 | * the declaration |
| 4420 | * |
| 4421 | * Returns: the enumeration attribute tree built while parsing |
| 4422 | */ |
| 4423 | |
| 4424 | xmlEnumerationPtr |
| 4425 | xmlParseEnumerationType(xmlParserCtxtPtr ctxt) { |
| 4426 | xmlChar *name; |
| 4427 | xmlEnumerationPtr ret = NULL, last = NULL, cur; |
| 4428 | |
| 4429 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4430 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4431 | return(NULL); |
| 4432 | } |
| 4433 | SHRINK; |
| 4434 | do { |
| 4435 | NEXT; |
| 4436 | SKIP_BLANKS; |
| 4437 | name = xmlParseNmtoken(ctxt); |
| 4438 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4439 | xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4440 | return(ret); |
| 4441 | } |
| 4442 | cur = xmlCreateEnumeration(name); |
| 4443 | xmlFree(name); |
| 4444 | if (cur == NULL) return(ret); |
| 4445 | if (last == NULL) ret = last = cur; |
| 4446 | else { |
| 4447 | last->next = cur; |
| 4448 | last = cur; |
| 4449 | } |
| 4450 | SKIP_BLANKS; |
| 4451 | } while (RAW == '|'); |
| 4452 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4453 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4454 | return(ret); |
| 4455 | } |
| 4456 | NEXT; |
| 4457 | return(ret); |
| 4458 | } |
| 4459 | |
| 4460 | /** |
| 4461 | * xmlParseEnumeratedType: |
| 4462 | * @ctxt: an XML parser context |
| 4463 | * @tree: the enumeration tree built while parsing |
| 4464 | * |
| 4465 | * parse an Enumerated attribute type. |
| 4466 | * |
| 4467 | * [57] EnumeratedType ::= NotationType | Enumeration |
| 4468 | * |
| 4469 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 4470 | * |
| 4471 | * |
| 4472 | * Returns: XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION |
| 4473 | */ |
| 4474 | |
| 4475 | int |
| 4476 | xmlParseEnumeratedType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4477 | if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4478 | SKIP(8); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4479 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4480 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4481 | "Space required after 'NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4482 | return(0); |
| 4483 | } |
| 4484 | SKIP_BLANKS; |
| 4485 | *tree = xmlParseNotationType(ctxt); |
| 4486 | if (*tree == NULL) return(0); |
| 4487 | return(XML_ATTRIBUTE_NOTATION); |
| 4488 | } |
| 4489 | *tree = xmlParseEnumerationType(ctxt); |
| 4490 | if (*tree == NULL) return(0); |
| 4491 | return(XML_ATTRIBUTE_ENUMERATION); |
| 4492 | } |
| 4493 | |
| 4494 | /** |
| 4495 | * xmlParseAttributeType: |
| 4496 | * @ctxt: an XML parser context |
| 4497 | * @tree: the enumeration tree built while parsing |
| 4498 | * |
| 4499 | * parse the Attribute list def for an element |
| 4500 | * |
| 4501 | * [54] AttType ::= StringType | TokenizedType | EnumeratedType |
| 4502 | * |
| 4503 | * [55] StringType ::= 'CDATA' |
| 4504 | * |
| 4505 | * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' | |
| 4506 | * 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS' |
| 4507 | * |
| 4508 | * Validity constraints for attribute values syntax are checked in |
| 4509 | * xmlValidateAttributeValue() |
| 4510 | * |
| 4511 | * [ VC: ID ] |
| 4512 | * Values of type ID must match the Name production. A name must not |
| 4513 | * appear more than once in an XML document as a value of this type; |
| 4514 | * i.e., ID values must uniquely identify the elements which bear them. |
| 4515 | * |
| 4516 | * [ VC: One ID per Element Type ] |
| 4517 | * No element type may have more than one ID attribute specified. |
| 4518 | * |
| 4519 | * [ VC: ID Attribute Default ] |
| 4520 | * An ID attribute must have a declared default of #IMPLIED or #REQUIRED. |
| 4521 | * |
| 4522 | * [ VC: IDREF ] |
| 4523 | * Values of type IDREF must match the Name production, and values |
| 4524 | * of type IDREFS must match Names; each IDREF Name must match the value |
| 4525 | * of an ID attribute on some element in the XML document; i.e. IDREF |
| 4526 | * values must match the value of some ID attribute. |
| 4527 | * |
| 4528 | * [ VC: Entity Name ] |
| 4529 | * Values of type ENTITY must match the Name production, values |
| 4530 | * of type ENTITIES must match Names; each Entity Name must match the |
| 4531 | * name of an unparsed entity declared in the DTD. |
| 4532 | * |
| 4533 | * [ VC: Name Token ] |
| 4534 | * Values of type NMTOKEN must match the Nmtoken production; values |
| 4535 | * of type NMTOKENS must match Nmtokens. |
| 4536 | * |
| 4537 | * Returns the attribute type |
| 4538 | */ |
| 4539 | int |
| 4540 | xmlParseAttributeType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
| 4541 | SHRINK; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4542 | if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4543 | SKIP(5); |
| 4544 | return(XML_ATTRIBUTE_CDATA); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4545 | } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4546 | SKIP(6); |
| 4547 | return(XML_ATTRIBUTE_IDREFS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4548 | } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4549 | SKIP(5); |
| 4550 | return(XML_ATTRIBUTE_IDREF); |
| 4551 | } else if ((RAW == 'I') && (NXT(1) == 'D')) { |
| 4552 | SKIP(2); |
| 4553 | return(XML_ATTRIBUTE_ID); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4554 | } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4555 | SKIP(6); |
| 4556 | return(XML_ATTRIBUTE_ENTITY); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4557 | } 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] | 4558 | SKIP(8); |
| 4559 | return(XML_ATTRIBUTE_ENTITIES); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4560 | } 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] | 4561 | SKIP(8); |
| 4562 | return(XML_ATTRIBUTE_NMTOKENS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4563 | } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4564 | SKIP(7); |
| 4565 | return(XML_ATTRIBUTE_NMTOKEN); |
| 4566 | } |
| 4567 | return(xmlParseEnumeratedType(ctxt, tree)); |
| 4568 | } |
| 4569 | |
| 4570 | /** |
| 4571 | * xmlParseAttributeListDecl: |
| 4572 | * @ctxt: an XML parser context |
| 4573 | * |
| 4574 | * : parse the Attribute list def for an element |
| 4575 | * |
| 4576 | * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>' |
| 4577 | * |
| 4578 | * [53] AttDef ::= S Name S AttType S DefaultDecl |
| 4579 | * |
| 4580 | */ |
| 4581 | void |
| 4582 | xmlParseAttributeListDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4583 | const xmlChar *elemName; |
| 4584 | const xmlChar *attrName; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4585 | xmlEnumerationPtr tree; |
| 4586 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4587 | if (CMP9(CUR_PTR, '<', '!', 'A', 'T', 'T', 'L', 'I', 'S', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4588 | xmlParserInputPtr input = ctxt->input; |
| 4589 | |
| 4590 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4591 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4592 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4593 | "Space required after '<!ATTLIST'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4594 | } |
| 4595 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4596 | elemName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4597 | if (elemName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4598 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 4599 | "ATTLIST: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4600 | return; |
| 4601 | } |
| 4602 | SKIP_BLANKS; |
| 4603 | GROW; |
| 4604 | while (RAW != '>') { |
| 4605 | const xmlChar *check = CUR_PTR; |
| 4606 | int type; |
| 4607 | int def; |
| 4608 | xmlChar *defaultValue = NULL; |
| 4609 | |
| 4610 | GROW; |
| 4611 | tree = NULL; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4612 | attrName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4613 | if (attrName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4614 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 4615 | "ATTLIST: no name for Attribute\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4616 | break; |
| 4617 | } |
| 4618 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4619 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4620 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4621 | "Space required after the attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4622 | if (defaultValue != NULL) |
| 4623 | xmlFree(defaultValue); |
| 4624 | break; |
| 4625 | } |
| 4626 | SKIP_BLANKS; |
| 4627 | |
| 4628 | type = xmlParseAttributeType(ctxt, &tree); |
| 4629 | if (type <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4630 | if (defaultValue != NULL) |
| 4631 | xmlFree(defaultValue); |
| 4632 | break; |
| 4633 | } |
| 4634 | |
| 4635 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4636 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4637 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4638 | "Space required after the attribute type\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4639 | if (defaultValue != NULL) |
| 4640 | xmlFree(defaultValue); |
| 4641 | if (tree != NULL) |
| 4642 | xmlFreeEnumeration(tree); |
| 4643 | break; |
| 4644 | } |
| 4645 | SKIP_BLANKS; |
| 4646 | |
| 4647 | def = xmlParseDefaultDecl(ctxt, &defaultValue); |
| 4648 | if (def <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4649 | if (defaultValue != NULL) |
| 4650 | xmlFree(defaultValue); |
| 4651 | if (tree != NULL) |
| 4652 | xmlFreeEnumeration(tree); |
| 4653 | break; |
| 4654 | } |
| 4655 | |
| 4656 | GROW; |
| 4657 | if (RAW != '>') { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4658 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4659 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4660 | "Space required after the attribute default value\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4661 | if (defaultValue != NULL) |
| 4662 | xmlFree(defaultValue); |
| 4663 | if (tree != NULL) |
| 4664 | xmlFreeEnumeration(tree); |
| 4665 | break; |
| 4666 | } |
| 4667 | SKIP_BLANKS; |
| 4668 | } |
| 4669 | if (check == CUR_PTR) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4670 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 4671 | "in xmlParseAttributeListDecl\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4672 | if (defaultValue != NULL) |
| 4673 | xmlFree(defaultValue); |
| 4674 | if (tree != NULL) |
| 4675 | xmlFreeEnumeration(tree); |
| 4676 | break; |
| 4677 | } |
| 4678 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 4679 | (ctxt->sax->attributeDecl != NULL)) |
| 4680 | ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName, |
| 4681 | type, def, defaultValue, tree); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4682 | else if (tree != NULL) |
| 4683 | xmlFreeEnumeration(tree); |
| 4684 | |
| 4685 | if ((ctxt->sax2) && (defaultValue != NULL) && |
| 4686 | (def != XML_ATTRIBUTE_IMPLIED) && |
| 4687 | (def != XML_ATTRIBUTE_REQUIRED)) { |
| 4688 | xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue); |
| 4689 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4690 | if ((ctxt->sax2) && (type != XML_ATTRIBUTE_CDATA)) { |
| 4691 | xmlAddSpecialAttr(ctxt, elemName, attrName, type); |
| 4692 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4693 | if (defaultValue != NULL) |
| 4694 | xmlFree(defaultValue); |
| 4695 | GROW; |
| 4696 | } |
| 4697 | if (RAW == '>') { |
| 4698 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4699 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4700 | "Attribute list declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4701 | } |
| 4702 | NEXT; |
| 4703 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4704 | } |
| 4705 | } |
| 4706 | |
| 4707 | /** |
| 4708 | * xmlParseElementMixedContentDecl: |
| 4709 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 4710 | * @inputchk: the input used for the current entity, needed for boundary checks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4711 | * |
| 4712 | * parse the declaration for a Mixed Element content |
| 4713 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
| 4714 | * |
| 4715 | * [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' | |
| 4716 | * '(' S? '#PCDATA' S? ')' |
| 4717 | * |
| 4718 | * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49]) |
| 4719 | * |
| 4720 | * [ VC: No Duplicate Types ] |
| 4721 | * The same name must not appear more than once in a single |
| 4722 | * mixed-content declaration. |
| 4723 | * |
| 4724 | * returns: the list of the xmlElementContentPtr describing the element choices |
| 4725 | */ |
| 4726 | xmlElementContentPtr |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4727 | xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4728 | xmlElementContentPtr ret = NULL, cur = NULL, n; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4729 | const xmlChar *elem = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4730 | |
| 4731 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4732 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4733 | SKIP(7); |
| 4734 | SKIP_BLANKS; |
| 4735 | SHRINK; |
| 4736 | if (RAW == ')') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4737 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 4738 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4739 | "Element content declaration doesn't start and stop in the same entity\n", |
| 4740 | NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 4741 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4742 | NEXT; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4743 | ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4744 | if (RAW == '*') { |
| 4745 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 4746 | NEXT; |
| 4747 | } |
| 4748 | return(ret); |
| 4749 | } |
| 4750 | if ((RAW == '(') || (RAW == '|')) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4751 | ret = cur = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4752 | if (ret == NULL) return(NULL); |
| 4753 | } |
| 4754 | while (RAW == '|') { |
| 4755 | NEXT; |
| 4756 | if (elem == NULL) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4757 | ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4758 | if (ret == NULL) return(NULL); |
| 4759 | ret->c1 = cur; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4760 | if (cur != NULL) |
| 4761 | cur->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4762 | cur = ret; |
| 4763 | } else { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4764 | n = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4765 | if (n == NULL) return(NULL); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4766 | n->c1 = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4767 | if (n->c1 != NULL) |
| 4768 | n->c1->parent = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4769 | cur->c2 = n; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4770 | if (n != NULL) |
| 4771 | n->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4772 | cur = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4773 | } |
| 4774 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4775 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4776 | if (elem == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4777 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4778 | "xmlParseElementMixedContentDecl : Name expected\n"); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4779 | xmlFreeDocElementContent(ctxt->myDoc, cur); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4780 | return(NULL); |
| 4781 | } |
| 4782 | SKIP_BLANKS; |
| 4783 | GROW; |
| 4784 | } |
| 4785 | if ((RAW == ')') && (NXT(1) == '*')) { |
| 4786 | if (elem != NULL) { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4787 | cur->c2 = xmlNewDocElementContent(ctxt->myDoc, elem, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4788 | XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4789 | if (cur->c2 != NULL) |
| 4790 | cur->c2->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4791 | } |
| 4792 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4793 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 4794 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4795 | "Element content declaration doesn't start and stop in the same entity\n", |
| 4796 | NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 4797 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4798 | SKIP(2); |
| 4799 | } else { |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4800 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4801 | xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4802 | return(NULL); |
| 4803 | } |
| 4804 | |
| 4805 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4806 | xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4807 | } |
| 4808 | return(ret); |
| 4809 | } |
| 4810 | |
| 4811 | /** |
| 4812 | * xmlParseElementChildrenContentDecl: |
| 4813 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 4814 | * @inputchk: the input used for the current entity, needed for boundary checks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4815 | * |
| 4816 | * parse the declaration for a Mixed Element content |
| 4817 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
| 4818 | * |
| 4819 | * |
| 4820 | * [47] children ::= (choice | seq) ('?' | '*' | '+')? |
| 4821 | * |
| 4822 | * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')? |
| 4823 | * |
| 4824 | * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')' |
| 4825 | * |
| 4826 | * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')' |
| 4827 | * |
| 4828 | * [ VC: Proper Group/PE Nesting ] applies to [49] and [50] |
| 4829 | * TODO Parameter-entity replacement text must be properly nested |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4830 | * with parenthesized groups. That is to say, if either of the |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4831 | * opening or closing parentheses in a choice, seq, or Mixed |
| 4832 | * construct is contained in the replacement text for a parameter |
| 4833 | * entity, both must be contained in the same replacement text. For |
| 4834 | * interoperability, if a parameter-entity reference appears in a |
| 4835 | * choice, seq, or Mixed construct, its replacement text should not |
| 4836 | * be empty, and neither the first nor last non-blank character of |
| 4837 | * the replacement text should be a connector (| or ,). |
| 4838 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 4839 | * Returns the tree of xmlElementContentPtr describing the element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4840 | * hierarchy. |
| 4841 | */ |
| 4842 | xmlElementContentPtr |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4843 | xmlParseElementChildrenContentDecl (xmlParserCtxtPtr ctxt, int inputchk) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4844 | xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4845 | const xmlChar *elem; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4846 | xmlChar type = 0; |
| 4847 | |
| 4848 | SKIP_BLANKS; |
| 4849 | GROW; |
| 4850 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4851 | int inputid = ctxt->input->id; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 4852 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4853 | /* Recurse on first child */ |
| 4854 | NEXT; |
| 4855 | SKIP_BLANKS; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4856 | cur = ret = xmlParseElementChildrenContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4857 | SKIP_BLANKS; |
| 4858 | GROW; |
| 4859 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4860 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4861 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4862 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4863 | return(NULL); |
| 4864 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4865 | cur = ret = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4866 | if (cur == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4867 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4868 | return(NULL); |
| 4869 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4870 | GROW; |
| 4871 | if (RAW == '?') { |
| 4872 | cur->ocur = XML_ELEMENT_CONTENT_OPT; |
| 4873 | NEXT; |
| 4874 | } else if (RAW == '*') { |
| 4875 | cur->ocur = XML_ELEMENT_CONTENT_MULT; |
| 4876 | NEXT; |
| 4877 | } else if (RAW == '+') { |
| 4878 | cur->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 4879 | NEXT; |
| 4880 | } else { |
| 4881 | cur->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 4882 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4883 | GROW; |
| 4884 | } |
| 4885 | SKIP_BLANKS; |
| 4886 | SHRINK; |
| 4887 | while (RAW != ')') { |
| 4888 | /* |
| 4889 | * Each loop we parse one separator and one element. |
| 4890 | */ |
| 4891 | if (RAW == ',') { |
| 4892 | if (type == 0) type = CUR; |
| 4893 | |
| 4894 | /* |
| 4895 | * Detect "Name | Name , Name" error |
| 4896 | */ |
| 4897 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4898 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4899 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4900 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 4901 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4902 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4903 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4904 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4905 | return(NULL); |
| 4906 | } |
| 4907 | NEXT; |
| 4908 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4909 | op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_SEQ); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4910 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 4911 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4912 | xmlFreeDocElementContent(ctxt->myDoc, last); |
| 4913 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4914 | return(NULL); |
| 4915 | } |
| 4916 | if (last == NULL) { |
| 4917 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4918 | if (ret != NULL) |
| 4919 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4920 | ret = cur = op; |
| 4921 | } else { |
| 4922 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4923 | if (op != NULL) |
| 4924 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4925 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4926 | if (last != NULL) |
| 4927 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4928 | cur =op; |
| 4929 | last = NULL; |
| 4930 | } |
| 4931 | } else if (RAW == '|') { |
| 4932 | if (type == 0) type = CUR; |
| 4933 | |
| 4934 | /* |
| 4935 | * Detect "Name , Name | Name" error |
| 4936 | */ |
| 4937 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4938 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4939 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4940 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 4941 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4942 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4943 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4944 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4945 | return(NULL); |
| 4946 | } |
| 4947 | NEXT; |
| 4948 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4949 | op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4950 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 4951 | if ((last != NULL) && (last != ret)) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4952 | xmlFreeDocElementContent(ctxt->myDoc, last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4953 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4954 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4955 | return(NULL); |
| 4956 | } |
| 4957 | if (last == NULL) { |
| 4958 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4959 | if (ret != NULL) |
| 4960 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4961 | ret = cur = op; |
| 4962 | } else { |
| 4963 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4964 | if (op != NULL) |
| 4965 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4966 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4967 | if (last != NULL) |
| 4968 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4969 | cur =op; |
| 4970 | last = NULL; |
| 4971 | } |
| 4972 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4973 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4974 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4975 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4976 | return(NULL); |
| 4977 | } |
| 4978 | GROW; |
| 4979 | SKIP_BLANKS; |
| 4980 | GROW; |
| 4981 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4982 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4983 | /* Recurse on second child */ |
| 4984 | NEXT; |
| 4985 | SKIP_BLANKS; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4986 | last = xmlParseElementChildrenContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4987 | SKIP_BLANKS; |
| 4988 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4989 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4990 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4991 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4992 | if (ret != NULL) |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4993 | xmlFreeDocElementContent(ctxt->myDoc, ret); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4994 | return(NULL); |
| 4995 | } |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4996 | last = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4997 | if (RAW == '?') { |
| 4998 | last->ocur = XML_ELEMENT_CONTENT_OPT; |
| 4999 | NEXT; |
| 5000 | } else if (RAW == '*') { |
| 5001 | last->ocur = XML_ELEMENT_CONTENT_MULT; |
| 5002 | NEXT; |
| 5003 | } else if (RAW == '+') { |
| 5004 | last->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 5005 | NEXT; |
| 5006 | } else { |
| 5007 | last->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 5008 | } |
| 5009 | } |
| 5010 | SKIP_BLANKS; |
| 5011 | GROW; |
| 5012 | } |
| 5013 | if ((cur != NULL) && (last != NULL)) { |
| 5014 | cur->c2 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 5015 | if (last != NULL) |
| 5016 | last->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5017 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5018 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5019 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5020 | "Element content declaration doesn't start and stop in the same entity\n", |
| 5021 | NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 5022 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5023 | NEXT; |
| 5024 | if (RAW == '?') { |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 5025 | if (ret != NULL) { |
| 5026 | if ((ret->ocur == XML_ELEMENT_CONTENT_PLUS) || |
| 5027 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 5028 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 5029 | else |
| 5030 | ret->ocur = XML_ELEMENT_CONTENT_OPT; |
| 5031 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5032 | NEXT; |
| 5033 | } else if (RAW == '*') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 5034 | if (ret != NULL) { |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 5035 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 5036 | cur = ret; |
| 5037 | /* |
| 5038 | * Some normalization: |
| 5039 | * (a | b* | c?)* == (a | b | c)* |
| 5040 | */ |
| 5041 | while (cur->type == XML_ELEMENT_CONTENT_OR) { |
| 5042 | if ((cur->c1 != NULL) && |
| 5043 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 5044 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 5045 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 5046 | if ((cur->c2 != NULL) && |
| 5047 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 5048 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 5049 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 5050 | cur = cur->c2; |
| 5051 | } |
| 5052 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5053 | NEXT; |
| 5054 | } else if (RAW == '+') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 5055 | if (ret != NULL) { |
| 5056 | int found = 0; |
| 5057 | |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 5058 | if ((ret->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 5059 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 5060 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
William M. Brack | eb8509c | 2004-05-14 03:48:02 +0000 | [diff] [blame] | 5061 | else |
| 5062 | ret->ocur = XML_ELEMENT_CONTENT_PLUS; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 5063 | /* |
| 5064 | * Some normalization: |
| 5065 | * (a | b*)+ == (a | b)* |
| 5066 | * (a | b?)+ == (a | b)* |
| 5067 | */ |
| 5068 | while (cur->type == XML_ELEMENT_CONTENT_OR) { |
| 5069 | if ((cur->c1 != NULL) && |
| 5070 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 5071 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 5072 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 5073 | found = 1; |
| 5074 | } |
| 5075 | if ((cur->c2 != NULL) && |
| 5076 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 5077 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 5078 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 5079 | found = 1; |
| 5080 | } |
| 5081 | cur = cur->c2; |
| 5082 | } |
| 5083 | if (found) |
| 5084 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 5085 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5086 | NEXT; |
| 5087 | } |
| 5088 | return(ret); |
| 5089 | } |
| 5090 | |
| 5091 | /** |
| 5092 | * xmlParseElementContentDecl: |
| 5093 | * @ctxt: an XML parser context |
| 5094 | * @name: the name of the element being defined. |
| 5095 | * @result: the Element Content pointer will be stored here if any |
| 5096 | * |
| 5097 | * parse the declaration for an Element content either Mixed or Children, |
| 5098 | * the cases EMPTY and ANY are handled directly in xmlParseElementDecl |
| 5099 | * |
| 5100 | * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children |
| 5101 | * |
| 5102 | * returns: the type of element content XML_ELEMENT_TYPE_xxx |
| 5103 | */ |
| 5104 | |
| 5105 | int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5106 | xmlParseElementContentDecl(xmlParserCtxtPtr ctxt, const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5107 | xmlElementContentPtr *result) { |
| 5108 | |
| 5109 | xmlElementContentPtr tree = NULL; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5110 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5111 | int res; |
| 5112 | |
| 5113 | *result = NULL; |
| 5114 | |
| 5115 | if (RAW != '(') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5116 | xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5117 | "xmlParseElementContentDecl : %s '(' expected\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5118 | return(-1); |
| 5119 | } |
| 5120 | NEXT; |
| 5121 | GROW; |
| 5122 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5123 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5124 | tree = xmlParseElementMixedContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5125 | res = XML_ELEMENT_TYPE_MIXED; |
| 5126 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5127 | tree = xmlParseElementChildrenContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5128 | res = XML_ELEMENT_TYPE_ELEMENT; |
| 5129 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5130 | SKIP_BLANKS; |
| 5131 | *result = tree; |
| 5132 | return(res); |
| 5133 | } |
| 5134 | |
| 5135 | /** |
| 5136 | * xmlParseElementDecl: |
| 5137 | * @ctxt: an XML parser context |
| 5138 | * |
| 5139 | * parse an Element declaration. |
| 5140 | * |
| 5141 | * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>' |
| 5142 | * |
| 5143 | * [ VC: Unique Element Type Declaration ] |
| 5144 | * No element type may be declared more than once |
| 5145 | * |
| 5146 | * Returns the type of the element, or -1 in case of error |
| 5147 | */ |
| 5148 | int |
| 5149 | xmlParseElementDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5150 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5151 | int ret = -1; |
| 5152 | xmlElementContentPtr content = NULL; |
| 5153 | |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5154 | /* GROW; done in the caller */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5155 | if (CMP9(CUR_PTR, '<', '!', 'E', 'L', 'E', 'M', 'E', 'N', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5156 | xmlParserInputPtr input = ctxt->input; |
| 5157 | |
| 5158 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5159 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5160 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5161 | "Space required after 'ELEMENT'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5162 | } |
| 5163 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5164 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5165 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5166 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5167 | "xmlParseElementDecl: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5168 | return(-1); |
| 5169 | } |
| 5170 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 5171 | xmlPopInput(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5172 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5173 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5174 | "Space required after the element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5175 | } |
| 5176 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5177 | if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5178 | SKIP(5); |
| 5179 | /* |
| 5180 | * Element must always be empty. |
| 5181 | */ |
| 5182 | ret = XML_ELEMENT_TYPE_EMPTY; |
| 5183 | } else if ((RAW == 'A') && (NXT(1) == 'N') && |
| 5184 | (NXT(2) == 'Y')) { |
| 5185 | SKIP(3); |
| 5186 | /* |
| 5187 | * Element is a generic container. |
| 5188 | */ |
| 5189 | ret = XML_ELEMENT_TYPE_ANY; |
| 5190 | } else if (RAW == '(') { |
| 5191 | ret = xmlParseElementContentDecl(ctxt, name, &content); |
| 5192 | } else { |
| 5193 | /* |
| 5194 | * [ WFC: PEs in Internal Subset ] error handling. |
| 5195 | */ |
| 5196 | if ((RAW == '%') && (ctxt->external == 0) && |
| 5197 | (ctxt->inputNr == 1)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5198 | xmlFatalErrMsg(ctxt, XML_ERR_PEREF_IN_INT_SUBSET, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5199 | "PEReference: forbidden within markup decl in internal subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5200 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5201 | xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5202 | "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n"); |
| 5203 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5204 | return(-1); |
| 5205 | } |
| 5206 | |
| 5207 | SKIP_BLANKS; |
| 5208 | /* |
| 5209 | * Pop-up of finished entities. |
| 5210 | */ |
| 5211 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 5212 | xmlPopInput(ctxt); |
| 5213 | SKIP_BLANKS; |
| 5214 | |
| 5215 | if (RAW != '>') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5216 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5217 | if (content != NULL) { |
| 5218 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 5219 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5220 | } else { |
| 5221 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5222 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5223 | "Element declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5224 | } |
| 5225 | |
| 5226 | NEXT; |
| 5227 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5228 | (ctxt->sax->elementDecl != NULL)) { |
| 5229 | if (content != NULL) |
| 5230 | content->parent = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5231 | ctxt->sax->elementDecl(ctxt->userData, name, ret, |
| 5232 | content); |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 5233 | if ((content != NULL) && (content->parent == NULL)) { |
| 5234 | /* |
| 5235 | * this is a trick: if xmlAddElementDecl is called, |
| 5236 | * instead of copying the full tree it is plugged directly |
| 5237 | * if called from the parser. Avoid duplicating the |
| 5238 | * interfaces or change the API/ABI |
| 5239 | */ |
| 5240 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 5241 | } |
| 5242 | } else if (content != NULL) { |
| 5243 | xmlFreeDocElementContent(ctxt->myDoc, content); |
| 5244 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5245 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5246 | } |
| 5247 | return(ret); |
| 5248 | } |
| 5249 | |
| 5250 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5251 | * xmlParseConditionalSections |
| 5252 | * @ctxt: an XML parser context |
| 5253 | * |
| 5254 | * [61] conditionalSect ::= includeSect | ignoreSect |
| 5255 | * [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>' |
| 5256 | * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>' |
| 5257 | * [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)* |
| 5258 | * [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*) |
| 5259 | */ |
| 5260 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5261 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5262 | xmlParseConditionalSections(xmlParserCtxtPtr ctxt) { |
| 5263 | SKIP(3); |
| 5264 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5265 | if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5266 | SKIP(7); |
| 5267 | SKIP_BLANKS; |
| 5268 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5269 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5270 | } else { |
| 5271 | NEXT; |
| 5272 | } |
| 5273 | if (xmlParserDebugEntities) { |
| 5274 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 5275 | xmlGenericError(xmlGenericErrorContext, |
| 5276 | "%s(%d): ", ctxt->input->filename, |
| 5277 | ctxt->input->line); |
| 5278 | xmlGenericError(xmlGenericErrorContext, |
| 5279 | "Entering INCLUDE Conditional Section\n"); |
| 5280 | } |
| 5281 | |
| 5282 | while ((RAW != 0) && ((RAW != ']') || (NXT(1) != ']') || |
| 5283 | (NXT(2) != '>'))) { |
| 5284 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 5285 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5286 | |
| 5287 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 5288 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5289 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5290 | NEXT; |
| 5291 | } else if (RAW == '%') { |
| 5292 | xmlParsePEReference(ctxt); |
| 5293 | } else |
| 5294 | xmlParseMarkupDecl(ctxt); |
| 5295 | |
| 5296 | /* |
| 5297 | * Pop-up of finished entities. |
| 5298 | */ |
| 5299 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 5300 | xmlPopInput(ctxt); |
| 5301 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5302 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5303 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5304 | break; |
| 5305 | } |
| 5306 | } |
| 5307 | if (xmlParserDebugEntities) { |
| 5308 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 5309 | xmlGenericError(xmlGenericErrorContext, |
| 5310 | "%s(%d): ", ctxt->input->filename, |
| 5311 | ctxt->input->line); |
| 5312 | xmlGenericError(xmlGenericErrorContext, |
| 5313 | "Leaving INCLUDE Conditional Section\n"); |
| 5314 | } |
| 5315 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5316 | } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5317 | int state; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 5318 | xmlParserInputState instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5319 | int depth = 0; |
| 5320 | |
| 5321 | SKIP(6); |
| 5322 | SKIP_BLANKS; |
| 5323 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5324 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5325 | } else { |
| 5326 | NEXT; |
| 5327 | } |
| 5328 | if (xmlParserDebugEntities) { |
| 5329 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 5330 | xmlGenericError(xmlGenericErrorContext, |
| 5331 | "%s(%d): ", ctxt->input->filename, |
| 5332 | ctxt->input->line); |
| 5333 | xmlGenericError(xmlGenericErrorContext, |
| 5334 | "Entering IGNORE Conditional Section\n"); |
| 5335 | } |
| 5336 | |
| 5337 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5338 | * Parse up to the end of the conditional section |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5339 | * But disable SAX event generating DTD building in the meantime |
| 5340 | */ |
| 5341 | state = ctxt->disableSAX; |
| 5342 | instate = ctxt->instate; |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 5343 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5344 | ctxt->instate = XML_PARSER_IGNORE; |
| 5345 | |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 5346 | while ((depth >= 0) && (RAW != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5347 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 5348 | depth++; |
| 5349 | SKIP(3); |
| 5350 | continue; |
| 5351 | } |
| 5352 | if ((RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) { |
| 5353 | if (--depth >= 0) SKIP(3); |
| 5354 | continue; |
| 5355 | } |
| 5356 | NEXT; |
| 5357 | continue; |
| 5358 | } |
| 5359 | |
| 5360 | ctxt->disableSAX = state; |
| 5361 | ctxt->instate = instate; |
| 5362 | |
| 5363 | if (xmlParserDebugEntities) { |
| 5364 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 5365 | xmlGenericError(xmlGenericErrorContext, |
| 5366 | "%s(%d): ", ctxt->input->filename, |
| 5367 | ctxt->input->line); |
| 5368 | xmlGenericError(xmlGenericErrorContext, |
| 5369 | "Leaving IGNORE Conditional Section\n"); |
| 5370 | } |
| 5371 | |
| 5372 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5373 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5374 | } |
| 5375 | |
| 5376 | if (RAW == 0) |
| 5377 | SHRINK; |
| 5378 | |
| 5379 | if (RAW == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5380 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5381 | } else { |
| 5382 | SKIP(3); |
| 5383 | } |
| 5384 | } |
| 5385 | |
| 5386 | /** |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5387 | * xmlParseMarkupDecl: |
| 5388 | * @ctxt: an XML parser context |
| 5389 | * |
| 5390 | * parse Markup declarations |
| 5391 | * |
| 5392 | * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl | |
| 5393 | * NotationDecl | PI | Comment |
| 5394 | * |
| 5395 | * [ VC: Proper Declaration/PE Nesting ] |
| 5396 | * Parameter-entity replacement text must be properly nested with |
| 5397 | * markup declarations. That is to say, if either the first character |
| 5398 | * or the last character of a markup declaration (markupdecl above) is |
| 5399 | * contained in the replacement text for a parameter-entity reference, |
| 5400 | * both must be contained in the same replacement text. |
| 5401 | * |
| 5402 | * [ WFC: PEs in Internal Subset ] |
| 5403 | * In the internal DTD subset, parameter-entity references can occur |
| 5404 | * only where markup declarations can occur, not within markup declarations. |
| 5405 | * (This does not apply to references that occur in external parameter |
| 5406 | * entities or to the external subset.) |
| 5407 | */ |
| 5408 | void |
| 5409 | xmlParseMarkupDecl(xmlParserCtxtPtr ctxt) { |
| 5410 | GROW; |
Daniel Veillard | 4c778d8 | 2005-01-23 17:37:44 +0000 | [diff] [blame] | 5411 | if (CUR == '<') { |
| 5412 | if (NXT(1) == '!') { |
| 5413 | switch (NXT(2)) { |
| 5414 | case 'E': |
| 5415 | if (NXT(3) == 'L') |
| 5416 | xmlParseElementDecl(ctxt); |
| 5417 | else if (NXT(3) == 'N') |
| 5418 | xmlParseEntityDecl(ctxt); |
| 5419 | break; |
| 5420 | case 'A': |
| 5421 | xmlParseAttributeListDecl(ctxt); |
| 5422 | break; |
| 5423 | case 'N': |
| 5424 | xmlParseNotationDecl(ctxt); |
| 5425 | break; |
| 5426 | case '-': |
| 5427 | xmlParseComment(ctxt); |
| 5428 | break; |
| 5429 | default: |
| 5430 | /* there is an error but it will be detected later */ |
| 5431 | break; |
| 5432 | } |
| 5433 | } else if (NXT(1) == '?') { |
| 5434 | xmlParsePI(ctxt); |
| 5435 | } |
| 5436 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5437 | /* |
| 5438 | * This is only for internal subset. On external entities, |
| 5439 | * the replacement is done before parsing stage |
| 5440 | */ |
| 5441 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 5442 | xmlParsePEReference(ctxt); |
| 5443 | |
| 5444 | /* |
| 5445 | * Conditional sections are allowed from entities included |
| 5446 | * by PE References in the internal subset. |
| 5447 | */ |
| 5448 | if ((ctxt->external == 0) && (ctxt->inputNr > 1)) { |
| 5449 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 5450 | xmlParseConditionalSections(ctxt); |
| 5451 | } |
| 5452 | } |
| 5453 | |
| 5454 | ctxt->instate = XML_PARSER_DTD; |
| 5455 | } |
| 5456 | |
| 5457 | /** |
| 5458 | * xmlParseTextDecl: |
| 5459 | * @ctxt: an XML parser context |
| 5460 | * |
| 5461 | * parse an XML declaration header for external entities |
| 5462 | * |
| 5463 | * [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>' |
| 5464 | * |
| 5465 | * Question: Seems that EncodingDecl is mandatory ? Is that a typo ? |
| 5466 | */ |
| 5467 | |
| 5468 | void |
| 5469 | xmlParseTextDecl(xmlParserCtxtPtr ctxt) { |
| 5470 | xmlChar *version; |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 5471 | const xmlChar *encoding; |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5472 | |
| 5473 | /* |
| 5474 | * We know that '<?xml' is here. |
| 5475 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5476 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5477 | SKIP(5); |
| 5478 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5479 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5480 | return; |
| 5481 | } |
| 5482 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5483 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5484 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5485 | "Space needed after '<?xml'\n"); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5486 | } |
| 5487 | SKIP_BLANKS; |
| 5488 | |
| 5489 | /* |
| 5490 | * We may have the VersionInfo here. |
| 5491 | */ |
| 5492 | version = xmlParseVersionInfo(ctxt); |
| 5493 | if (version == NULL) |
| 5494 | version = xmlCharStrdup(XML_DEFAULT_VERSION); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 5495 | else { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5496 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5497 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5498 | "Space needed here\n"); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 5499 | } |
| 5500 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5501 | ctxt->input->version = version; |
| 5502 | |
| 5503 | /* |
| 5504 | * We must have the encoding declaration |
| 5505 | */ |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 5506 | encoding = xmlParseEncodingDecl(ctxt); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5507 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 5508 | /* |
| 5509 | * The XML REC instructs us to stop parsing right here |
| 5510 | */ |
| 5511 | return; |
| 5512 | } |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 5513 | if ((encoding == NULL) && (ctxt->errNo == XML_ERR_OK)) { |
| 5514 | xmlFatalErrMsg(ctxt, XML_ERR_MISSING_ENCODING, |
| 5515 | "Missing encoding in text declaration\n"); |
| 5516 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5517 | |
| 5518 | SKIP_BLANKS; |
| 5519 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 5520 | SKIP(2); |
| 5521 | } else if (RAW == '>') { |
| 5522 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5523 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5524 | NEXT; |
| 5525 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5526 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5527 | MOVETO_ENDTAG(CUR_PTR); |
| 5528 | NEXT; |
| 5529 | } |
| 5530 | } |
| 5531 | |
| 5532 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5533 | * xmlParseExternalSubset: |
| 5534 | * @ctxt: an XML parser context |
| 5535 | * @ExternalID: the external identifier |
| 5536 | * @SystemID: the system identifier (or URL) |
| 5537 | * |
| 5538 | * parse Markup declarations from an external subset |
| 5539 | * |
| 5540 | * [30] extSubset ::= textDecl? extSubsetDecl |
| 5541 | * |
| 5542 | * [31] extSubsetDecl ::= (markupdecl | conditionalSect | PEReference | S) * |
| 5543 | */ |
| 5544 | void |
| 5545 | xmlParseExternalSubset(xmlParserCtxtPtr ctxt, const xmlChar *ExternalID, |
| 5546 | const xmlChar *SystemID) { |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 5547 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5548 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5549 | if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5550 | xmlParseTextDecl(ctxt); |
| 5551 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 5552 | /* |
| 5553 | * The XML REC instructs us to stop parsing right here |
| 5554 | */ |
| 5555 | ctxt->instate = XML_PARSER_EOF; |
| 5556 | return; |
| 5557 | } |
| 5558 | } |
| 5559 | if (ctxt->myDoc == NULL) { |
| 5560 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 5561 | } |
| 5562 | if ((ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset == NULL)) |
| 5563 | xmlCreateIntSubset(ctxt->myDoc, NULL, ExternalID, SystemID); |
| 5564 | |
| 5565 | ctxt->instate = XML_PARSER_DTD; |
| 5566 | ctxt->external = 1; |
| 5567 | while (((RAW == '<') && (NXT(1) == '?')) || |
| 5568 | ((RAW == '<') && (NXT(1) == '!')) || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5569 | (RAW == '%') || IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5570 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 5571 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5572 | |
| 5573 | GROW; |
| 5574 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 5575 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5576 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5577 | NEXT; |
| 5578 | } else if (RAW == '%') { |
| 5579 | xmlParsePEReference(ctxt); |
| 5580 | } else |
| 5581 | xmlParseMarkupDecl(ctxt); |
| 5582 | |
| 5583 | /* |
| 5584 | * Pop-up of finished entities. |
| 5585 | */ |
| 5586 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 5587 | xmlPopInput(ctxt); |
| 5588 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5589 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5590 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5591 | break; |
| 5592 | } |
| 5593 | } |
| 5594 | |
| 5595 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5596 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5597 | } |
| 5598 | |
| 5599 | } |
| 5600 | |
| 5601 | /** |
| 5602 | * xmlParseReference: |
| 5603 | * @ctxt: an XML parser context |
| 5604 | * |
| 5605 | * parse and handle entity references in content, depending on the SAX |
| 5606 | * interface, this may end-up in a call to character() if this is a |
| 5607 | * CharRef, a predefined entity, if there is no reference() callback. |
| 5608 | * or if the parser was asked to switch to that mode. |
| 5609 | * |
| 5610 | * [67] Reference ::= EntityRef | CharRef |
| 5611 | */ |
| 5612 | void |
| 5613 | xmlParseReference(xmlParserCtxtPtr ctxt) { |
| 5614 | xmlEntityPtr ent; |
| 5615 | xmlChar *val; |
| 5616 | if (RAW != '&') return; |
| 5617 | |
| 5618 | if (NXT(1) == '#') { |
| 5619 | int i = 0; |
| 5620 | xmlChar out[10]; |
| 5621 | int hex = NXT(2); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5622 | int value = xmlParseCharRef(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5623 | |
| 5624 | if (ctxt->charset != XML_CHAR_ENCODING_UTF8) { |
| 5625 | /* |
| 5626 | * So we are using non-UTF-8 buffers |
| 5627 | * Check that the char fit on 8bits, if not |
| 5628 | * generate a CharRef. |
| 5629 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5630 | if (value <= 0xFF) { |
| 5631 | out[0] = value; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5632 | out[1] = 0; |
| 5633 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 5634 | (!ctxt->disableSAX)) |
| 5635 | ctxt->sax->characters(ctxt->userData, out, 1); |
| 5636 | } else { |
| 5637 | if ((hex == 'x') || (hex == 'X')) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5638 | snprintf((char *)out, sizeof(out), "#x%X", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5639 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5640 | snprintf((char *)out, sizeof(out), "#%d", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5641 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 5642 | (!ctxt->disableSAX)) |
| 5643 | ctxt->sax->reference(ctxt->userData, out); |
| 5644 | } |
| 5645 | } else { |
| 5646 | /* |
| 5647 | * Just encode the value in UTF-8 |
| 5648 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5649 | COPY_BUF(0 ,out, i, value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5650 | out[i] = 0; |
| 5651 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 5652 | (!ctxt->disableSAX)) |
| 5653 | ctxt->sax->characters(ctxt->userData, out, i); |
| 5654 | } |
| 5655 | } else { |
| 5656 | ent = xmlParseEntityRef(ctxt); |
| 5657 | if (ent == NULL) return; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 5658 | if (!ctxt->wellFormed) |
| 5659 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5660 | if ((ent->name != NULL) && |
| 5661 | (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 5662 | xmlNodePtr list = NULL; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 5663 | xmlParserErrors ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5664 | |
| 5665 | |
| 5666 | /* |
| 5667 | * The first reference to the entity trigger a parsing phase |
| 5668 | * where the ent->children is filled with the result from |
| 5669 | * the parsing. |
| 5670 | */ |
| 5671 | if (ent->children == NULL) { |
| 5672 | xmlChar *value; |
| 5673 | value = ent->content; |
| 5674 | |
| 5675 | /* |
| 5676 | * Check that this entity is well formed |
| 5677 | */ |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 5678 | if ((value != NULL) && (value[0] != 0) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5679 | (value[1] == 0) && (value[0] == '<') && |
| 5680 | (xmlStrEqual(ent->name, BAD_CAST "lt"))) { |
| 5681 | /* |
| 5682 | * DONE: get definite answer on this !!! |
| 5683 | * Lots of entity decls are used to declare a single |
| 5684 | * char |
| 5685 | * <!ENTITY lt "<"> |
| 5686 | * Which seems to be valid since |
| 5687 | * 2.4: The ampersand character (&) and the left angle |
| 5688 | * bracket (<) may appear in their literal form only |
| 5689 | * when used ... They are also legal within the literal |
| 5690 | * entity value of an internal entity declaration;i |
| 5691 | * see "4.3.2 Well-Formed Parsed Entities". |
| 5692 | * IMHO 2.4 and 4.3.2 are directly in contradiction. |
| 5693 | * Looking at the OASIS test suite and James Clark |
| 5694 | * tests, this is broken. However the XML REC uses |
| 5695 | * it. Is the XML REC not well-formed ???? |
| 5696 | * This is a hack to avoid this problem |
| 5697 | * |
| 5698 | * ANSWER: since lt gt amp .. are already defined, |
| 5699 | * this is a redefinition and hence the fact that the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5700 | * content is not well balanced is not a Wf error, this |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5701 | * is lousy but acceptable. |
| 5702 | */ |
| 5703 | list = xmlNewDocText(ctxt->myDoc, value); |
| 5704 | if (list != NULL) { |
| 5705 | if ((ent->etype == XML_INTERNAL_GENERAL_ENTITY) && |
| 5706 | (ent->children == NULL)) { |
| 5707 | ent->children = list; |
| 5708 | ent->last = list; |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 5709 | ent->owner = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5710 | list->parent = (xmlNodePtr) ent; |
| 5711 | } else { |
| 5712 | xmlFreeNodeList(list); |
| 5713 | } |
| 5714 | } else if (list != NULL) { |
| 5715 | xmlFreeNodeList(list); |
| 5716 | } |
| 5717 | } else { |
| 5718 | /* |
| 5719 | * 4.3.2: An internal general parsed entity is well-formed |
| 5720 | * if its replacement text matches the production labeled |
| 5721 | * content. |
| 5722 | */ |
Daniel Veillard | b5a60ec | 2002-03-18 11:45:56 +0000 | [diff] [blame] | 5723 | |
| 5724 | void *user_data; |
| 5725 | /* |
| 5726 | * This is a bit hackish but this seems the best |
| 5727 | * way to make sure both SAX and DOM entity support |
| 5728 | * behaves okay. |
| 5729 | */ |
| 5730 | if (ctxt->userData == ctxt) |
| 5731 | user_data = NULL; |
| 5732 | else |
| 5733 | user_data = ctxt->userData; |
| 5734 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5735 | if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) { |
| 5736 | ctxt->depth++; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 5737 | ret = xmlParseBalancedChunkMemoryInternal(ctxt, |
| 5738 | value, user_data, &list); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5739 | ctxt->depth--; |
| 5740 | } else if (ent->etype == |
| 5741 | XML_EXTERNAL_GENERAL_PARSED_ENTITY) { |
| 5742 | ctxt->depth++; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 5743 | ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, |
Daniel Veillard | b5a60ec | 2002-03-18 11:45:56 +0000 | [diff] [blame] | 5744 | ctxt->sax, user_data, ctxt->depth, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 5745 | ent->URI, ent->ExternalID, &list); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5746 | ctxt->depth--; |
| 5747 | } else { |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 5748 | ret = XML_ERR_ENTITY_PE_INTERNAL; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5749 | xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5750 | "invalid entity type found\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5751 | } |
| 5752 | if (ret == XML_ERR_ENTITY_LOOP) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5753 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 5754 | return; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 5755 | } else if ((ret == XML_ERR_OK) && (list != NULL)) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5756 | if (((ent->etype == XML_INTERNAL_GENERAL_ENTITY) || |
| 5757 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY))&& |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5758 | (ent->children == NULL)) { |
| 5759 | ent->children = list; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5760 | if (ctxt->replaceEntities) { |
| 5761 | /* |
| 5762 | * Prune it directly in the generated document |
| 5763 | * except for single text nodes. |
| 5764 | */ |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 5765 | if (((list->type == XML_TEXT_NODE) && |
| 5766 | (list->next == NULL)) || |
| 5767 | (ctxt->parseMode == XML_PARSE_READER)) { |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5768 | list->parent = (xmlNodePtr) ent; |
| 5769 | list = NULL; |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 5770 | ent->owner = 1; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5771 | } else { |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 5772 | ent->owner = 0; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5773 | while (list != NULL) { |
| 5774 | list->parent = (xmlNodePtr) ctxt->node; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 5775 | list->doc = ctxt->myDoc; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5776 | if (list->next == NULL) |
| 5777 | ent->last = list; |
| 5778 | list = list->next; |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 5779 | } |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5780 | list = ent->children; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 5781 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 5782 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 5783 | xmlAddEntityReference(ent, list, NULL); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 5784 | #endif /* LIBXML_LEGACY_ENABLED */ |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5785 | } |
| 5786 | } else { |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 5787 | ent->owner = 1; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5788 | while (list != NULL) { |
| 5789 | list->parent = (xmlNodePtr) ent; |
| 5790 | if (list->next == NULL) |
| 5791 | ent->last = list; |
| 5792 | list = list->next; |
| 5793 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5794 | } |
| 5795 | } else { |
| 5796 | xmlFreeNodeList(list); |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5797 | list = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5798 | } |
William M. Brack | b670e2e | 2003-09-27 01:05:55 +0000 | [diff] [blame] | 5799 | } else if ((ret != XML_ERR_OK) && |
| 5800 | (ret != XML_WAR_UNDECLARED_ENTITY)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5801 | xmlFatalErr(ctxt, ret, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5802 | } else if (list != NULL) { |
| 5803 | xmlFreeNodeList(list); |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5804 | list = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5805 | } |
| 5806 | } |
| 5807 | } |
| 5808 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 5809 | (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) { |
| 5810 | /* |
| 5811 | * Create a node. |
| 5812 | */ |
| 5813 | ctxt->sax->reference(ctxt->userData, ent->name); |
| 5814 | return; |
| 5815 | } else if (ctxt->replaceEntities) { |
William M. Brack | 1227fb3 | 2004-10-25 23:17:53 +0000 | [diff] [blame] | 5816 | /* |
| 5817 | * There is a problem on the handling of _private for entities |
| 5818 | * (bug 155816): Should we copy the content of the field from |
| 5819 | * the entity (possibly overwriting some value set by the user |
| 5820 | * when a copy is created), should we leave it alone, or should |
| 5821 | * we try to take care of different situations? The problem |
| 5822 | * is exacerbated by the usage of this field by the xmlReader. |
| 5823 | * To fix this bug, we look at _private on the created node |
| 5824 | * and, if it's NULL, we copy in whatever was in the entity. |
| 5825 | * If it's not NULL we leave it alone. This is somewhat of a |
| 5826 | * hack - maybe we should have further tests to determine |
| 5827 | * what to do. |
| 5828 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5829 | if ((ctxt->node != NULL) && (ent->children != NULL)) { |
| 5830 | /* |
| 5831 | * Seems we are generating the DOM content, do |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5832 | * a simple tree copy for all references except the first |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 5833 | * In the first occurrence list contains the replacement. |
| 5834 | * progressive == 2 means we are operating on the Reader |
| 5835 | * and since nodes are discarded we must copy all the time. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5836 | */ |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 5837 | if (((list == NULL) && (ent->owner == 0)) || |
| 5838 | (ctxt->parseMode == XML_PARSE_READER)) { |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5839 | xmlNodePtr nw = NULL, cur, firstChild = NULL; |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 5840 | |
| 5841 | /* |
| 5842 | * when operating on a reader, the entities definitions |
| 5843 | * are always owning the entities subtree. |
| 5844 | if (ctxt->parseMode == XML_PARSE_READER) |
| 5845 | ent->owner = 1; |
| 5846 | */ |
| 5847 | |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5848 | cur = ent->children; |
| 5849 | while (cur != NULL) { |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 5850 | nw = xmlDocCopyNode(cur, ctxt->myDoc, 1); |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5851 | if (nw != NULL) { |
William M. Brack | 1227fb3 | 2004-10-25 23:17:53 +0000 | [diff] [blame] | 5852 | if (nw->_private == NULL) |
| 5853 | nw->_private = cur->_private; |
Daniel Veillard | 8f87244 | 2003-01-09 23:19:02 +0000 | [diff] [blame] | 5854 | if (firstChild == NULL){ |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5855 | firstChild = nw; |
Daniel Veillard | 8f87244 | 2003-01-09 23:19:02 +0000 | [diff] [blame] | 5856 | } |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 5857 | nw = xmlAddChild(ctxt->node, nw); |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 5858 | } |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 5859 | if (cur == ent->last) { |
| 5860 | /* |
| 5861 | * needed to detect some strange empty |
| 5862 | * node cases in the reader tests |
| 5863 | */ |
| 5864 | if ((ctxt->parseMode == XML_PARSE_READER) && |
| 5865 | (nw->type == XML_ELEMENT_NODE) && |
| 5866 | (nw->children == NULL)) |
| 5867 | nw->extra = 1; |
| 5868 | |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5869 | break; |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 5870 | } |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5871 | cur = cur->next; |
| 5872 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 5873 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 5874 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5875 | xmlAddEntityReference(ent, firstChild, nw); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 5876 | #endif /* LIBXML_LEGACY_ENABLED */ |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5877 | } else if (list == NULL) { |
| 5878 | xmlNodePtr nw = NULL, cur, next, last, |
| 5879 | firstChild = NULL; |
| 5880 | /* |
| 5881 | * Copy the entity child list and make it the new |
| 5882 | * entity child list. The goal is to make sure any |
| 5883 | * ID or REF referenced will be the one from the |
| 5884 | * document content and not the entity copy. |
| 5885 | */ |
| 5886 | cur = ent->children; |
| 5887 | ent->children = NULL; |
| 5888 | last = ent->last; |
| 5889 | ent->last = NULL; |
| 5890 | while (cur != NULL) { |
| 5891 | next = cur->next; |
| 5892 | cur->next = NULL; |
| 5893 | cur->parent = NULL; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 5894 | nw = xmlDocCopyNode(cur, ctxt->myDoc, 1); |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5895 | if (nw != NULL) { |
William M. Brack | 1227fb3 | 2004-10-25 23:17:53 +0000 | [diff] [blame] | 5896 | if (nw->_private == NULL) |
| 5897 | nw->_private = cur->_private; |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5898 | if (firstChild == NULL){ |
| 5899 | firstChild = cur; |
| 5900 | } |
| 5901 | xmlAddChild((xmlNodePtr) ent, nw); |
| 5902 | xmlAddChild(ctxt->node, cur); |
| 5903 | } |
| 5904 | if (cur == last) |
| 5905 | break; |
| 5906 | cur = next; |
| 5907 | } |
| 5908 | ent->owner = 1; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 5909 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5910 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 5911 | xmlAddEntityReference(ent, firstChild, nw); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 5912 | #endif /* LIBXML_LEGACY_ENABLED */ |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5913 | } else { |
Daniel Veillard | 370ba3d | 2004-10-25 16:23:56 +0000 | [diff] [blame] | 5914 | const xmlChar *nbktext; |
| 5915 | |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5916 | /* |
| 5917 | * the name change is to avoid coalescing of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5918 | * node with a possible previous text one which |
| 5919 | * would make ent->children a dangling pointer |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5920 | */ |
Daniel Veillard | 370ba3d | 2004-10-25 16:23:56 +0000 | [diff] [blame] | 5921 | nbktext = xmlDictLookup(ctxt->dict, BAD_CAST "nbktext", |
| 5922 | -1); |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5923 | if (ent->children->type == XML_TEXT_NODE) |
Daniel Veillard | 370ba3d | 2004-10-25 16:23:56 +0000 | [diff] [blame] | 5924 | ent->children->name = nbktext; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5925 | if ((ent->last != ent->children) && |
| 5926 | (ent->last->type == XML_TEXT_NODE)) |
Daniel Veillard | 370ba3d | 2004-10-25 16:23:56 +0000 | [diff] [blame] | 5927 | ent->last->name = nbktext; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5928 | xmlAddChildList(ctxt->node, ent->children); |
| 5929 | } |
| 5930 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5931 | /* |
| 5932 | * This is to avoid a nasty side effect, see |
| 5933 | * characters() in SAX.c |
| 5934 | */ |
| 5935 | ctxt->nodemem = 0; |
| 5936 | ctxt->nodelen = 0; |
| 5937 | return; |
| 5938 | } else { |
| 5939 | /* |
| 5940 | * Probably running in SAX mode |
| 5941 | */ |
| 5942 | xmlParserInputPtr input; |
| 5943 | |
| 5944 | input = xmlNewEntityInputStream(ctxt, ent); |
| 5945 | xmlPushInput(ctxt, input); |
| 5946 | if ((ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) && |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5947 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && |
| 5948 | (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5949 | xmlParseTextDecl(ctxt); |
| 5950 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 5951 | /* |
| 5952 | * The XML REC instructs us to stop parsing right here |
| 5953 | */ |
| 5954 | ctxt->instate = XML_PARSER_EOF; |
| 5955 | return; |
| 5956 | } |
| 5957 | if (input->standalone == 1) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5958 | xmlFatalErr(ctxt, XML_ERR_EXT_ENTITY_STANDALONE, |
| 5959 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5960 | } |
| 5961 | } |
| 5962 | return; |
| 5963 | } |
| 5964 | } |
| 5965 | } else { |
| 5966 | val = ent->content; |
| 5967 | if (val == NULL) return; |
| 5968 | /* |
| 5969 | * inline the entity. |
| 5970 | */ |
| 5971 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 5972 | (!ctxt->disableSAX)) |
| 5973 | ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val)); |
| 5974 | } |
| 5975 | } |
| 5976 | } |
| 5977 | |
| 5978 | /** |
| 5979 | * xmlParseEntityRef: |
| 5980 | * @ctxt: an XML parser context |
| 5981 | * |
| 5982 | * parse ENTITY references declarations |
| 5983 | * |
| 5984 | * [68] EntityRef ::= '&' Name ';' |
| 5985 | * |
| 5986 | * [ WFC: Entity Declared ] |
| 5987 | * In a document without any DTD, a document with only an internal DTD |
| 5988 | * subset which contains no parameter entity references, or a document |
| 5989 | * with "standalone='yes'", the Name given in the entity reference |
| 5990 | * must match that in an entity declaration, except that well-formed |
| 5991 | * documents need not declare any of the following entities: amp, lt, |
| 5992 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 5993 | * any reference to it. Similarly, the declaration of a general entity |
| 5994 | * must precede any reference to it which appears in a default value in an |
| 5995 | * attribute-list declaration. Note that if entities are declared in the |
| 5996 | * external subset or in external parameter entities, a non-validating |
| 5997 | * processor is not obligated to read and process their declarations; |
| 5998 | * for such documents, the rule that an entity must be declared is a |
| 5999 | * well-formedness constraint only if standalone='yes'. |
| 6000 | * |
| 6001 | * [ WFC: Parsed Entity ] |
| 6002 | * An entity reference must not contain the name of an unparsed entity |
| 6003 | * |
| 6004 | * Returns the xmlEntityPtr if found, or NULL otherwise. |
| 6005 | */ |
| 6006 | xmlEntityPtr |
| 6007 | xmlParseEntityRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6008 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6009 | xmlEntityPtr ent = NULL; |
| 6010 | |
| 6011 | GROW; |
| 6012 | |
| 6013 | if (RAW == '&') { |
| 6014 | NEXT; |
| 6015 | name = xmlParseName(ctxt); |
| 6016 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6017 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6018 | "xmlParseEntityRef: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6019 | } else { |
| 6020 | if (RAW == ';') { |
| 6021 | NEXT; |
| 6022 | /* |
| 6023 | * Ask first SAX for entity resolution, otherwise try the |
| 6024 | * predefined set. |
| 6025 | */ |
| 6026 | if (ctxt->sax != NULL) { |
| 6027 | if (ctxt->sax->getEntity != NULL) |
| 6028 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 6029 | if ((ctxt->wellFormed == 1 ) && (ent == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6030 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 6031 | if ((ctxt->wellFormed == 1 ) && (ent == NULL) && |
| 6032 | (ctxt->userData==ctxt)) { |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 6033 | ent = xmlSAX2GetEntity(ctxt, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 6034 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6035 | } |
| 6036 | /* |
| 6037 | * [ WFC: Entity Declared ] |
| 6038 | * In a document without any DTD, a document with only an |
| 6039 | * internal DTD subset which contains no parameter entity |
| 6040 | * references, or a document with "standalone='yes'", the |
| 6041 | * Name given in the entity reference must match that in an |
| 6042 | * entity declaration, except that well-formed documents |
| 6043 | * need not declare any of the following entities: amp, lt, |
| 6044 | * gt, apos, quot. |
| 6045 | * The declaration of a parameter entity must precede any |
| 6046 | * reference to it. |
| 6047 | * Similarly, the declaration of a general entity must |
| 6048 | * precede any reference to it which appears in a default |
| 6049 | * value in an attribute-list declaration. Note that if |
| 6050 | * entities are declared in the external subset or in |
| 6051 | * external parameter entities, a non-validating processor |
| 6052 | * is not obligated to read and process their declarations; |
| 6053 | * for such documents, the rule that an entity must be |
| 6054 | * declared is a well-formedness constraint only if |
| 6055 | * standalone='yes'. |
| 6056 | */ |
| 6057 | if (ent == NULL) { |
| 6058 | if ((ctxt->standalone == 1) || |
| 6059 | ((ctxt->hasExternalSubset == 0) && |
| 6060 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6061 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6062 | "Entity '%s' not defined\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6063 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6064 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6065 | "Entity '%s' not defined\n", name); |
| 6066 | } |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6067 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6068 | } |
| 6069 | |
| 6070 | /* |
| 6071 | * [ WFC: Parsed Entity ] |
| 6072 | * An entity reference must not contain the name of an |
| 6073 | * unparsed entity |
| 6074 | */ |
| 6075 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6076 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6077 | "Entity reference to unparsed entity %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6078 | } |
| 6079 | |
| 6080 | /* |
| 6081 | * [ WFC: No External Entity References ] |
| 6082 | * Attribute values cannot contain direct or indirect |
| 6083 | * entity references to external entities. |
| 6084 | */ |
| 6085 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 6086 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6087 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
| 6088 | "Attribute references external entity '%s'\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6089 | } |
| 6090 | /* |
| 6091 | * [ WFC: No < in Attribute Values ] |
| 6092 | * The replacement text of any entity referred to directly or |
| 6093 | * indirectly in an attribute value (other than "<") must |
| 6094 | * not contain a <. |
| 6095 | */ |
| 6096 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 6097 | (ent != NULL) && |
| 6098 | (!xmlStrEqual(ent->name, BAD_CAST "lt")) && |
| 6099 | (ent->content != NULL) && |
| 6100 | (xmlStrchr(ent->content, '<'))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6101 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6102 | "'<' in entity '%s' is not allowed in attributes values\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6103 | } |
| 6104 | |
| 6105 | /* |
| 6106 | * Internal check, no parameter entities here ... |
| 6107 | */ |
| 6108 | else { |
| 6109 | switch (ent->etype) { |
| 6110 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 6111 | case XML_EXTERNAL_PARAMETER_ENTITY: |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6112 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 6113 | "Attempt to reference the parameter entity '%s'\n", |
| 6114 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6115 | break; |
| 6116 | default: |
| 6117 | break; |
| 6118 | } |
| 6119 | } |
| 6120 | |
| 6121 | /* |
| 6122 | * [ WFC: No Recursion ] |
| 6123 | * A parsed entity must not contain a recursive reference |
| 6124 | * to itself, either directly or indirectly. |
| 6125 | * Done somewhere else |
| 6126 | */ |
| 6127 | |
| 6128 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6129 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6130 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6131 | } |
| 6132 | } |
| 6133 | return(ent); |
| 6134 | } |
| 6135 | |
| 6136 | /** |
| 6137 | * xmlParseStringEntityRef: |
| 6138 | * @ctxt: an XML parser context |
| 6139 | * @str: a pointer to an index in the string |
| 6140 | * |
| 6141 | * parse ENTITY references declarations, but this version parses it from |
| 6142 | * a string value. |
| 6143 | * |
| 6144 | * [68] EntityRef ::= '&' Name ';' |
| 6145 | * |
| 6146 | * [ WFC: Entity Declared ] |
| 6147 | * In a document without any DTD, a document with only an internal DTD |
| 6148 | * subset which contains no parameter entity references, or a document |
| 6149 | * with "standalone='yes'", the Name given in the entity reference |
| 6150 | * must match that in an entity declaration, except that well-formed |
| 6151 | * documents need not declare any of the following entities: amp, lt, |
| 6152 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 6153 | * any reference to it. Similarly, the declaration of a general entity |
| 6154 | * must precede any reference to it which appears in a default value in an |
| 6155 | * attribute-list declaration. Note that if entities are declared in the |
| 6156 | * external subset or in external parameter entities, a non-validating |
| 6157 | * processor is not obligated to read and process their declarations; |
| 6158 | * for such documents, the rule that an entity must be declared is a |
| 6159 | * well-formedness constraint only if standalone='yes'. |
| 6160 | * |
| 6161 | * [ WFC: Parsed Entity ] |
| 6162 | * An entity reference must not contain the name of an unparsed entity |
| 6163 | * |
| 6164 | * Returns the xmlEntityPtr if found, or NULL otherwise. The str pointer |
| 6165 | * is updated to the current location in the string. |
| 6166 | */ |
| 6167 | xmlEntityPtr |
| 6168 | xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) { |
| 6169 | xmlChar *name; |
| 6170 | const xmlChar *ptr; |
| 6171 | xmlChar cur; |
| 6172 | xmlEntityPtr ent = NULL; |
| 6173 | |
| 6174 | if ((str == NULL) || (*str == NULL)) |
| 6175 | return(NULL); |
| 6176 | ptr = *str; |
| 6177 | cur = *ptr; |
| 6178 | if (cur == '&') { |
| 6179 | ptr++; |
| 6180 | cur = *ptr; |
| 6181 | name = xmlParseStringName(ctxt, &ptr); |
| 6182 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6183 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6184 | "xmlParseStringEntityRef: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6185 | } else { |
| 6186 | if (*ptr == ';') { |
| 6187 | ptr++; |
| 6188 | /* |
| 6189 | * Ask first SAX for entity resolution, otherwise try the |
| 6190 | * predefined set. |
| 6191 | */ |
| 6192 | if (ctxt->sax != NULL) { |
| 6193 | if (ctxt->sax->getEntity != NULL) |
| 6194 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
| 6195 | if (ent == NULL) |
| 6196 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 6197 | if ((ent == NULL) && (ctxt->userData==ctxt)) { |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 6198 | ent = xmlSAX2GetEntity(ctxt, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 6199 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6200 | } |
| 6201 | /* |
| 6202 | * [ WFC: Entity Declared ] |
| 6203 | * In a document without any DTD, a document with only an |
| 6204 | * internal DTD subset which contains no parameter entity |
| 6205 | * references, or a document with "standalone='yes'", the |
| 6206 | * Name given in the entity reference must match that in an |
| 6207 | * entity declaration, except that well-formed documents |
| 6208 | * need not declare any of the following entities: amp, lt, |
| 6209 | * gt, apos, quot. |
| 6210 | * The declaration of a parameter entity must precede any |
| 6211 | * reference to it. |
| 6212 | * Similarly, the declaration of a general entity must |
| 6213 | * precede any reference to it which appears in a default |
| 6214 | * value in an attribute-list declaration. Note that if |
| 6215 | * entities are declared in the external subset or in |
| 6216 | * external parameter entities, a non-validating processor |
| 6217 | * is not obligated to read and process their declarations; |
| 6218 | * for such documents, the rule that an entity must be |
| 6219 | * declared is a well-formedness constraint only if |
| 6220 | * standalone='yes'. |
| 6221 | */ |
| 6222 | if (ent == NULL) { |
| 6223 | if ((ctxt->standalone == 1) || |
| 6224 | ((ctxt->hasExternalSubset == 0) && |
| 6225 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6226 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6227 | "Entity '%s' not defined\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6228 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6229 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6230 | "Entity '%s' not defined\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6231 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6232 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6233 | /* TODO ? check regressions ctxt->valid = 0; */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6234 | } |
| 6235 | |
| 6236 | /* |
| 6237 | * [ WFC: Parsed Entity ] |
| 6238 | * An entity reference must not contain the name of an |
| 6239 | * unparsed entity |
| 6240 | */ |
| 6241 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6242 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6243 | "Entity reference to unparsed entity %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6244 | } |
| 6245 | |
| 6246 | /* |
| 6247 | * [ WFC: No External Entity References ] |
| 6248 | * Attribute values cannot contain direct or indirect |
| 6249 | * entity references to external entities. |
| 6250 | */ |
| 6251 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 6252 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6253 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6254 | "Attribute references external entity '%s'\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6255 | } |
| 6256 | /* |
| 6257 | * [ WFC: No < in Attribute Values ] |
| 6258 | * The replacement text of any entity referred to directly or |
| 6259 | * indirectly in an attribute value (other than "<") must |
| 6260 | * not contain a <. |
| 6261 | */ |
| 6262 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 6263 | (ent != NULL) && |
| 6264 | (!xmlStrEqual(ent->name, BAD_CAST "lt")) && |
| 6265 | (ent->content != NULL) && |
| 6266 | (xmlStrchr(ent->content, '<'))) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6267 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
| 6268 | "'<' in entity '%s' is not allowed in attributes values\n", |
| 6269 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6270 | } |
| 6271 | |
| 6272 | /* |
| 6273 | * Internal check, no parameter entities here ... |
| 6274 | */ |
| 6275 | else { |
| 6276 | switch (ent->etype) { |
| 6277 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 6278 | case XML_EXTERNAL_PARAMETER_ENTITY: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6279 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 6280 | "Attempt to reference the parameter entity '%s'\n", |
| 6281 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6282 | break; |
| 6283 | default: |
| 6284 | break; |
| 6285 | } |
| 6286 | } |
| 6287 | |
| 6288 | /* |
| 6289 | * [ WFC: No Recursion ] |
| 6290 | * A parsed entity must not contain a recursive reference |
| 6291 | * to itself, either directly or indirectly. |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6292 | * Done somewhere else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6293 | */ |
| 6294 | |
| 6295 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6296 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6297 | } |
| 6298 | xmlFree(name); |
| 6299 | } |
| 6300 | } |
| 6301 | *str = ptr; |
| 6302 | return(ent); |
| 6303 | } |
| 6304 | |
| 6305 | /** |
| 6306 | * xmlParsePEReference: |
| 6307 | * @ctxt: an XML parser context |
| 6308 | * |
| 6309 | * parse PEReference declarations |
| 6310 | * The entity content is handled directly by pushing it's content as |
| 6311 | * a new input stream. |
| 6312 | * |
| 6313 | * [69] PEReference ::= '%' Name ';' |
| 6314 | * |
| 6315 | * [ WFC: No Recursion ] |
| 6316 | * A parsed entity must not contain a recursive |
| 6317 | * reference to itself, either directly or indirectly. |
| 6318 | * |
| 6319 | * [ WFC: Entity Declared ] |
| 6320 | * In a document without any DTD, a document with only an internal DTD |
| 6321 | * subset which contains no parameter entity references, or a document |
| 6322 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 6323 | * entity must precede any reference to it... |
| 6324 | * |
| 6325 | * [ VC: Entity Declared ] |
| 6326 | * In a document with an external subset or external parameter entities |
| 6327 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 6328 | * must precede any reference to it... |
| 6329 | * |
| 6330 | * [ WFC: In DTD ] |
| 6331 | * Parameter-entity references may only appear in the DTD. |
| 6332 | * NOTE: misleading but this is handled. |
| 6333 | */ |
| 6334 | void |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 6335 | xmlParsePEReference(xmlParserCtxtPtr ctxt) |
| 6336 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6337 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6338 | xmlEntityPtr entity = NULL; |
| 6339 | xmlParserInputPtr input; |
| 6340 | |
| 6341 | if (RAW == '%') { |
| 6342 | NEXT; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6343 | name = xmlParseName(ctxt); |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 6344 | if (name == NULL) { |
| 6345 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6346 | "xmlParsePEReference: no name\n"); |
| 6347 | } else { |
| 6348 | if (RAW == ';') { |
| 6349 | NEXT; |
| 6350 | if ((ctxt->sax != NULL) && |
| 6351 | (ctxt->sax->getParameterEntity != NULL)) |
| 6352 | entity = ctxt->sax->getParameterEntity(ctxt->userData, |
| 6353 | name); |
| 6354 | if (entity == NULL) { |
| 6355 | /* |
| 6356 | * [ WFC: Entity Declared ] |
| 6357 | * In a document without any DTD, a document with only an |
| 6358 | * internal DTD subset which contains no parameter entity |
| 6359 | * references, or a document with "standalone='yes'", ... |
| 6360 | * ... The declaration of a parameter entity must precede |
| 6361 | * any reference to it... |
| 6362 | */ |
| 6363 | if ((ctxt->standalone == 1) || |
| 6364 | ((ctxt->hasExternalSubset == 0) && |
| 6365 | (ctxt->hasPErefs == 0))) { |
| 6366 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 6367 | "PEReference: %%%s; not found\n", |
| 6368 | name); |
| 6369 | } else { |
| 6370 | /* |
| 6371 | * [ VC: Entity Declared ] |
| 6372 | * In a document with an external subset or external |
| 6373 | * parameter entities with "standalone='no'", ... |
| 6374 | * ... The declaration of a parameter entity must |
| 6375 | * precede any reference to it... |
| 6376 | */ |
| 6377 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 6378 | "PEReference: %%%s; not found\n", |
| 6379 | name, NULL); |
| 6380 | ctxt->valid = 0; |
| 6381 | } |
| 6382 | } else { |
| 6383 | /* |
| 6384 | * Internal checking in case the entity quest barfed |
| 6385 | */ |
| 6386 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 6387 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 6388 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 6389 | "Internal: %%%s; is not a parameter entity\n", |
| 6390 | name, NULL); |
| 6391 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 6392 | input = |
| 6393 | xmlNewBlanksWrapperInputStream(ctxt, entity); |
| 6394 | xmlPushInput(ctxt, input); |
| 6395 | } else { |
| 6396 | /* |
| 6397 | * TODO !!! |
| 6398 | * handle the extra spaces added before and after |
| 6399 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
| 6400 | */ |
| 6401 | input = xmlNewEntityInputStream(ctxt, entity); |
| 6402 | xmlPushInput(ctxt, input); |
| 6403 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6404 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6405 | (IS_BLANK_CH(NXT(5)))) { |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 6406 | xmlParseTextDecl(ctxt); |
| 6407 | if (ctxt->errNo == |
| 6408 | XML_ERR_UNSUPPORTED_ENCODING) { |
| 6409 | /* |
| 6410 | * The XML REC instructs us to stop parsing |
| 6411 | * right here |
| 6412 | */ |
| 6413 | ctxt->instate = XML_PARSER_EOF; |
| 6414 | return; |
| 6415 | } |
| 6416 | } |
| 6417 | } |
| 6418 | } |
| 6419 | ctxt->hasPErefs = 1; |
| 6420 | } else { |
| 6421 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 6422 | } |
| 6423 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6424 | } |
| 6425 | } |
| 6426 | |
| 6427 | /** |
| 6428 | * xmlParseStringPEReference: |
| 6429 | * @ctxt: an XML parser context |
| 6430 | * @str: a pointer to an index in the string |
| 6431 | * |
| 6432 | * parse PEReference declarations |
| 6433 | * |
| 6434 | * [69] PEReference ::= '%' Name ';' |
| 6435 | * |
| 6436 | * [ WFC: No Recursion ] |
| 6437 | * A parsed entity must not contain a recursive |
| 6438 | * reference to itself, either directly or indirectly. |
| 6439 | * |
| 6440 | * [ WFC: Entity Declared ] |
| 6441 | * In a document without any DTD, a document with only an internal DTD |
| 6442 | * subset which contains no parameter entity references, or a document |
| 6443 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 6444 | * entity must precede any reference to it... |
| 6445 | * |
| 6446 | * [ VC: Entity Declared ] |
| 6447 | * In a document with an external subset or external parameter entities |
| 6448 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 6449 | * must precede any reference to it... |
| 6450 | * |
| 6451 | * [ WFC: In DTD ] |
| 6452 | * Parameter-entity references may only appear in the DTD. |
| 6453 | * NOTE: misleading but this is handled. |
| 6454 | * |
| 6455 | * Returns the string of the entity content. |
| 6456 | * str is updated to the current value of the index |
| 6457 | */ |
| 6458 | xmlEntityPtr |
| 6459 | xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 6460 | const xmlChar *ptr; |
| 6461 | xmlChar cur; |
| 6462 | xmlChar *name; |
| 6463 | xmlEntityPtr entity = NULL; |
| 6464 | |
| 6465 | if ((str == NULL) || (*str == NULL)) return(NULL); |
| 6466 | ptr = *str; |
| 6467 | cur = *ptr; |
| 6468 | if (cur == '%') { |
| 6469 | ptr++; |
| 6470 | cur = *ptr; |
| 6471 | name = xmlParseStringName(ctxt, &ptr); |
| 6472 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6473 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6474 | "xmlParseStringPEReference: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6475 | } else { |
| 6476 | cur = *ptr; |
| 6477 | if (cur == ';') { |
| 6478 | ptr++; |
| 6479 | cur = *ptr; |
| 6480 | if ((ctxt->sax != NULL) && |
| 6481 | (ctxt->sax->getParameterEntity != NULL)) |
| 6482 | entity = ctxt->sax->getParameterEntity(ctxt->userData, |
| 6483 | name); |
| 6484 | if (entity == NULL) { |
| 6485 | /* |
| 6486 | * [ WFC: Entity Declared ] |
| 6487 | * In a document without any DTD, a document with only an |
| 6488 | * internal DTD subset which contains no parameter entity |
| 6489 | * references, or a document with "standalone='yes'", ... |
| 6490 | * ... The declaration of a parameter entity must precede |
| 6491 | * any reference to it... |
| 6492 | */ |
| 6493 | if ((ctxt->standalone == 1) || |
| 6494 | ((ctxt->hasExternalSubset == 0) && |
| 6495 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6496 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6497 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6498 | } else { |
| 6499 | /* |
| 6500 | * [ VC: Entity Declared ] |
| 6501 | * In a document with an external subset or external |
| 6502 | * parameter entities with "standalone='no'", ... |
| 6503 | * ... The declaration of a parameter entity must |
| 6504 | * precede any reference to it... |
| 6505 | */ |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6506 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 6507 | "PEReference: %%%s; not found\n", |
| 6508 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6509 | ctxt->valid = 0; |
| 6510 | } |
| 6511 | } else { |
| 6512 | /* |
| 6513 | * Internal checking in case the entity quest barfed |
| 6514 | */ |
| 6515 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 6516 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6517 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 6518 | "%%%s; is not a parameter entity\n", |
| 6519 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6520 | } |
| 6521 | } |
| 6522 | ctxt->hasPErefs = 1; |
| 6523 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6524 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6525 | } |
| 6526 | xmlFree(name); |
| 6527 | } |
| 6528 | } |
| 6529 | *str = ptr; |
| 6530 | return(entity); |
| 6531 | } |
| 6532 | |
| 6533 | /** |
| 6534 | * xmlParseDocTypeDecl: |
| 6535 | * @ctxt: an XML parser context |
| 6536 | * |
| 6537 | * parse a DOCTYPE declaration |
| 6538 | * |
| 6539 | * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? |
| 6540 | * ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 6541 | * |
| 6542 | * [ VC: Root Element Type ] |
| 6543 | * The Name in the document type declaration must match the element |
| 6544 | * type of the root element. |
| 6545 | */ |
| 6546 | |
| 6547 | void |
| 6548 | xmlParseDocTypeDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6549 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6550 | xmlChar *ExternalID = NULL; |
| 6551 | xmlChar *URI = NULL; |
| 6552 | |
| 6553 | /* |
| 6554 | * We know that '<!DOCTYPE' has been detected. |
| 6555 | */ |
| 6556 | SKIP(9); |
| 6557 | |
| 6558 | SKIP_BLANKS; |
| 6559 | |
| 6560 | /* |
| 6561 | * Parse the DOCTYPE name. |
| 6562 | */ |
| 6563 | name = xmlParseName(ctxt); |
| 6564 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6565 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6566 | "xmlParseDocTypeDecl : no DOCTYPE name !\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6567 | } |
| 6568 | ctxt->intSubName = name; |
| 6569 | |
| 6570 | SKIP_BLANKS; |
| 6571 | |
| 6572 | /* |
| 6573 | * Check for SystemID and ExternalID |
| 6574 | */ |
| 6575 | URI = xmlParseExternalID(ctxt, &ExternalID, 1); |
| 6576 | |
| 6577 | if ((URI != NULL) || (ExternalID != NULL)) { |
| 6578 | ctxt->hasExternalSubset = 1; |
| 6579 | } |
| 6580 | ctxt->extSubURI = URI; |
| 6581 | ctxt->extSubSystem = ExternalID; |
| 6582 | |
| 6583 | SKIP_BLANKS; |
| 6584 | |
| 6585 | /* |
| 6586 | * Create and update the internal subset. |
| 6587 | */ |
| 6588 | if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) && |
| 6589 | (!ctxt->disableSAX)) |
| 6590 | ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI); |
| 6591 | |
| 6592 | /* |
| 6593 | * Is there any internal subset declarations ? |
| 6594 | * they are handled separately in xmlParseInternalSubset() |
| 6595 | */ |
| 6596 | if (RAW == '[') |
| 6597 | return; |
| 6598 | |
| 6599 | /* |
| 6600 | * We should be at the end of the DOCTYPE declaration. |
| 6601 | */ |
| 6602 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6603 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6604 | } |
| 6605 | NEXT; |
| 6606 | } |
| 6607 | |
| 6608 | /** |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6609 | * xmlParseInternalSubset: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6610 | * @ctxt: an XML parser context |
| 6611 | * |
| 6612 | * parse the internal subset declaration |
| 6613 | * |
| 6614 | * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 6615 | */ |
| 6616 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6617 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6618 | xmlParseInternalSubset(xmlParserCtxtPtr ctxt) { |
| 6619 | /* |
| 6620 | * Is there any DTD definition ? |
| 6621 | */ |
| 6622 | if (RAW == '[') { |
| 6623 | ctxt->instate = XML_PARSER_DTD; |
| 6624 | NEXT; |
| 6625 | /* |
| 6626 | * Parse the succession of Markup declarations and |
| 6627 | * PEReferences. |
| 6628 | * Subsequence (markupdecl | PEReference | S)* |
| 6629 | */ |
| 6630 | while (RAW != ']') { |
| 6631 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 6632 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6633 | |
| 6634 | SKIP_BLANKS; |
| 6635 | xmlParseMarkupDecl(ctxt); |
| 6636 | xmlParsePEReference(ctxt); |
| 6637 | |
| 6638 | /* |
| 6639 | * Pop-up of finished entities. |
| 6640 | */ |
| 6641 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6642 | xmlPopInput(ctxt); |
| 6643 | |
| 6644 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6645 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6646 | "xmlParseInternalSubset: error detected in Markup declaration\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6647 | break; |
| 6648 | } |
| 6649 | } |
| 6650 | if (RAW == ']') { |
| 6651 | NEXT; |
| 6652 | SKIP_BLANKS; |
| 6653 | } |
| 6654 | } |
| 6655 | |
| 6656 | /* |
| 6657 | * We should be at the end of the DOCTYPE declaration. |
| 6658 | */ |
| 6659 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6660 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6661 | } |
| 6662 | NEXT; |
| 6663 | } |
| 6664 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 6665 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6666 | /** |
| 6667 | * xmlParseAttribute: |
| 6668 | * @ctxt: an XML parser context |
| 6669 | * @value: a xmlChar ** used to store the value of the attribute |
| 6670 | * |
| 6671 | * parse an attribute |
| 6672 | * |
| 6673 | * [41] Attribute ::= Name Eq AttValue |
| 6674 | * |
| 6675 | * [ WFC: No External Entity References ] |
| 6676 | * Attribute values cannot contain direct or indirect entity references |
| 6677 | * to external entities. |
| 6678 | * |
| 6679 | * [ WFC: No < in Attribute Values ] |
| 6680 | * The replacement text of any entity referred to directly or indirectly in |
| 6681 | * an attribute value (other than "<") must not contain a <. |
| 6682 | * |
| 6683 | * [ VC: Attribute Value Type ] |
| 6684 | * The attribute must have been declared; the value must be of the type |
| 6685 | * declared for it. |
| 6686 | * |
| 6687 | * [25] Eq ::= S? '=' S? |
| 6688 | * |
| 6689 | * With namespace: |
| 6690 | * |
| 6691 | * [NS 11] Attribute ::= QName Eq AttValue |
| 6692 | * |
| 6693 | * Also the case QName == xmlns:??? is handled independently as a namespace |
| 6694 | * definition. |
| 6695 | * |
| 6696 | * Returns the attribute name, and the value in *value. |
| 6697 | */ |
| 6698 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6699 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6700 | xmlParseAttribute(xmlParserCtxtPtr ctxt, xmlChar **value) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6701 | const xmlChar *name; |
| 6702 | xmlChar *val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6703 | |
| 6704 | *value = NULL; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6705 | GROW; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6706 | name = xmlParseName(ctxt); |
| 6707 | if (name == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6708 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6709 | "error parsing attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6710 | return(NULL); |
| 6711 | } |
| 6712 | |
| 6713 | /* |
| 6714 | * read the value |
| 6715 | */ |
| 6716 | SKIP_BLANKS; |
| 6717 | if (RAW == '=') { |
| 6718 | NEXT; |
| 6719 | SKIP_BLANKS; |
| 6720 | val = xmlParseAttValue(ctxt); |
| 6721 | ctxt->instate = XML_PARSER_CONTENT; |
| 6722 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6723 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6724 | "Specification mandate value for attribute %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6725 | return(NULL); |
| 6726 | } |
| 6727 | |
| 6728 | /* |
| 6729 | * Check that xml:lang conforms to the specification |
| 6730 | * No more registered as an error, just generate a warning now |
| 6731 | * since this was deprecated in XML second edition |
| 6732 | */ |
| 6733 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) { |
| 6734 | if (!xmlCheckLanguageID(val)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6735 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 6736 | "Malformed value for xml:lang : %s\n", |
| 6737 | val, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6738 | } |
| 6739 | } |
| 6740 | |
| 6741 | /* |
| 6742 | * Check that xml:space conforms to the specification |
| 6743 | */ |
| 6744 | if (xmlStrEqual(name, BAD_CAST "xml:space")) { |
| 6745 | if (xmlStrEqual(val, BAD_CAST "default")) |
| 6746 | *(ctxt->space) = 0; |
| 6747 | else if (xmlStrEqual(val, BAD_CAST "preserve")) |
| 6748 | *(ctxt->space) = 1; |
| 6749 | else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6750 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Daniel Veillard | 642104e | 2003-03-26 16:32:05 +0000 | [diff] [blame] | 6751 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6752 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6753 | } |
| 6754 | } |
| 6755 | |
| 6756 | *value = val; |
| 6757 | return(name); |
| 6758 | } |
| 6759 | |
| 6760 | /** |
| 6761 | * xmlParseStartTag: |
| 6762 | * @ctxt: an XML parser context |
| 6763 | * |
| 6764 | * parse a start of tag either for rule element or |
| 6765 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 6766 | * |
| 6767 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 6768 | * |
| 6769 | * [ WFC: Unique Att Spec ] |
| 6770 | * No attribute name may appear more than once in the same start-tag or |
| 6771 | * empty-element tag. |
| 6772 | * |
| 6773 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 6774 | * |
| 6775 | * [ WFC: Unique Att Spec ] |
| 6776 | * No attribute name may appear more than once in the same start-tag or |
| 6777 | * empty-element tag. |
| 6778 | * |
| 6779 | * With namespace: |
| 6780 | * |
| 6781 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 6782 | * |
| 6783 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 6784 | * |
| 6785 | * Returns the element name parsed |
| 6786 | */ |
| 6787 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6788 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6789 | xmlParseStartTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6790 | const xmlChar *name; |
| 6791 | const xmlChar *attname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6792 | xmlChar *attvalue; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6793 | const xmlChar **atts = ctxt->atts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6794 | int nbatts = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6795 | int maxatts = ctxt->maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6796 | int i; |
| 6797 | |
| 6798 | if (RAW != '<') return(NULL); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 6799 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6800 | |
| 6801 | name = xmlParseName(ctxt); |
| 6802 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6803 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6804 | "xmlParseStartTag: invalid element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6805 | return(NULL); |
| 6806 | } |
| 6807 | |
| 6808 | /* |
| 6809 | * Now parse the attributes, it ends up with the ending |
| 6810 | * |
| 6811 | * (S Attribute)* S? |
| 6812 | */ |
| 6813 | SKIP_BLANKS; |
| 6814 | GROW; |
| 6815 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 6816 | while ((RAW != '>') && |
| 6817 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 6818 | (IS_BYTE_CHAR(RAW))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6819 | const xmlChar *q = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 6820 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6821 | |
| 6822 | attname = xmlParseAttribute(ctxt, &attvalue); |
| 6823 | if ((attname != NULL) && (attvalue != NULL)) { |
| 6824 | /* |
| 6825 | * [ WFC: Unique Att Spec ] |
| 6826 | * No attribute name may appear more than once in the same |
| 6827 | * start-tag or empty-element tag. |
| 6828 | */ |
| 6829 | for (i = 0; i < nbatts;i += 2) { |
| 6830 | if (xmlStrEqual(atts[i], attname)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6831 | xmlErrAttributeDup(ctxt, NULL, attname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6832 | xmlFree(attvalue); |
| 6833 | goto failed; |
| 6834 | } |
| 6835 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6836 | /* |
| 6837 | * Add the pair to atts |
| 6838 | */ |
| 6839 | if (atts == NULL) { |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6840 | maxatts = 22; /* allow for 10 attrs by default */ |
| 6841 | atts = (const xmlChar **) |
| 6842 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6843 | if (atts == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6844 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6845 | if (attvalue != NULL) |
| 6846 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6847 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6848 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6849 | ctxt->atts = atts; |
| 6850 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6851 | } else if (nbatts + 4 > maxatts) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6852 | const xmlChar **n; |
| 6853 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6854 | maxatts *= 2; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6855 | n = (const xmlChar **) xmlRealloc((void *) atts, |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6856 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6857 | if (n == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6858 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6859 | if (attvalue != NULL) |
| 6860 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6861 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6862 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6863 | atts = n; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6864 | ctxt->atts = atts; |
| 6865 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6866 | } |
| 6867 | atts[nbatts++] = attname; |
| 6868 | atts[nbatts++] = attvalue; |
| 6869 | atts[nbatts] = NULL; |
| 6870 | atts[nbatts + 1] = NULL; |
| 6871 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6872 | if (attvalue != NULL) |
| 6873 | xmlFree(attvalue); |
| 6874 | } |
| 6875 | |
| 6876 | failed: |
| 6877 | |
Daniel Veillard | 3772de3 | 2002-12-17 10:31:45 +0000 | [diff] [blame] | 6878 | GROW |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6879 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 6880 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6881 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6882 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6883 | "attributes construct error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6884 | } |
| 6885 | SKIP_BLANKS; |
Daniel Veillard | 02111c1 | 2003-02-24 19:14:52 +0000 | [diff] [blame] | 6886 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 6887 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6888 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 6889 | "xmlParseStartTag: problem parsing attributes\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6890 | break; |
| 6891 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6892 | SHRINK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6893 | GROW; |
| 6894 | } |
| 6895 | |
| 6896 | /* |
| 6897 | * SAX: Start of Element ! |
| 6898 | */ |
| 6899 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) && |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6900 | (!ctxt->disableSAX)) { |
| 6901 | if (nbatts > 0) |
| 6902 | ctxt->sax->startElement(ctxt->userData, name, atts); |
| 6903 | else |
| 6904 | ctxt->sax->startElement(ctxt->userData, name, NULL); |
| 6905 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6906 | |
| 6907 | if (atts != NULL) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6908 | /* Free only the content strings */ |
| 6909 | for (i = 1;i < nbatts;i+=2) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6910 | if (atts[i] != NULL) |
| 6911 | xmlFree((xmlChar *) atts[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6912 | } |
| 6913 | return(name); |
| 6914 | } |
| 6915 | |
| 6916 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6917 | * xmlParseEndTag1: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6918 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6919 | * @line: line of the start tag |
| 6920 | * @nsNr: number of namespaces on the start tag |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6921 | * |
| 6922 | * parse an end of tag |
| 6923 | * |
| 6924 | * [42] ETag ::= '</' Name S? '>' |
| 6925 | * |
| 6926 | * With namespace |
| 6927 | * |
| 6928 | * [NS 9] ETag ::= '</' QName S? '>' |
| 6929 | */ |
| 6930 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 6931 | static void |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6932 | xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6933 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6934 | |
| 6935 | GROW; |
| 6936 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6937 | xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6938 | "xmlParseEndTag: '</' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6939 | return; |
| 6940 | } |
| 6941 | SKIP(2); |
| 6942 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 6943 | name = xmlParseNameAndCompare(ctxt,ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6944 | |
| 6945 | /* |
| 6946 | * We should definitely be at the ending "S? '>'" part |
| 6947 | */ |
| 6948 | GROW; |
| 6949 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 6950 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6951 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6952 | } else |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 6953 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6954 | |
| 6955 | /* |
| 6956 | * [ WFC: Element Type Match ] |
| 6957 | * The Name in an element's end-tag must match the element type in the |
| 6958 | * start-tag. |
| 6959 | * |
| 6960 | */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 6961 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6962 | if (name == NULL) name = BAD_CAST "unparseable"; |
| 6963 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 6964 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6965 | ctxt->name, line, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6966 | } |
| 6967 | |
| 6968 | /* |
| 6969 | * SAX: End of Tag |
| 6970 | */ |
| 6971 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 6972 | (!ctxt->disableSAX)) |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 6973 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6974 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6975 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6976 | spacePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6977 | return; |
| 6978 | } |
| 6979 | |
| 6980 | /** |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 6981 | * xmlParseEndTag: |
| 6982 | * @ctxt: an XML parser context |
| 6983 | * |
| 6984 | * parse an end of tag |
| 6985 | * |
| 6986 | * [42] ETag ::= '</' Name S? '>' |
| 6987 | * |
| 6988 | * With namespace |
| 6989 | * |
| 6990 | * [NS 9] ETag ::= '</' QName S? '>' |
| 6991 | */ |
| 6992 | |
| 6993 | void |
| 6994 | xmlParseEndTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6995 | xmlParseEndTag1(ctxt, 0); |
| 6996 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 6997 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6998 | |
| 6999 | /************************************************************************ |
| 7000 | * * |
| 7001 | * SAX 2 specific operations * |
| 7002 | * * |
| 7003 | ************************************************************************/ |
| 7004 | |
| 7005 | static const xmlChar * |
| 7006 | xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) { |
| 7007 | int len = 0, l; |
| 7008 | int c; |
| 7009 | int count = 0; |
| 7010 | |
| 7011 | /* |
| 7012 | * Handler for more complex cases |
| 7013 | */ |
| 7014 | GROW; |
| 7015 | c = CUR_CHAR(l); |
| 7016 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7017 | (!IS_LETTER(c) && (c != '_'))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7018 | return(NULL); |
| 7019 | } |
| 7020 | |
| 7021 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 7022 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7023 | (c == '.') || (c == '-') || (c == '_') || |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 7024 | (IS_COMBINING(c)) || |
| 7025 | (IS_EXTENDER(c)))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7026 | if (count++ > 100) { |
| 7027 | count = 0; |
| 7028 | GROW; |
| 7029 | } |
| 7030 | len += l; |
| 7031 | NEXTL(l); |
| 7032 | c = CUR_CHAR(l); |
| 7033 | } |
| 7034 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); |
| 7035 | } |
| 7036 | |
| 7037 | /* |
| 7038 | * xmlGetNamespace: |
| 7039 | * @ctxt: an XML parser context |
| 7040 | * @prefix: the prefix to lookup |
| 7041 | * |
| 7042 | * Lookup the namespace name for the @prefix (which ca be NULL) |
| 7043 | * The prefix must come from the @ctxt->dict dictionnary |
| 7044 | * |
| 7045 | * Returns the namespace name or NULL if not bound |
| 7046 | */ |
| 7047 | static const xmlChar * |
| 7048 | xmlGetNamespace(xmlParserCtxtPtr ctxt, const xmlChar *prefix) { |
| 7049 | int i; |
| 7050 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7051 | if (prefix == ctxt->str_xml) return(ctxt->str_xml_ns); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7052 | for (i = ctxt->nsNr - 2;i >= 0;i-=2) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7053 | if (ctxt->nsTab[i] == prefix) { |
| 7054 | if ((prefix == NULL) && (*ctxt->nsTab[i + 1] == 0)) |
| 7055 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7056 | return(ctxt->nsTab[i + 1]); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7057 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7058 | return(NULL); |
| 7059 | } |
| 7060 | |
| 7061 | /** |
| 7062 | * xmlParseNCName: |
| 7063 | * @ctxt: an XML parser context |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 7064 | * @len: lenght of the string parsed |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7065 | * |
| 7066 | * parse an XML name. |
| 7067 | * |
| 7068 | * [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 7069 | * CombiningChar | Extender |
| 7070 | * |
| 7071 | * [5NS] NCName ::= (Letter | '_') (NCNameChar)* |
| 7072 | * |
| 7073 | * Returns the Name parsed or NULL |
| 7074 | */ |
| 7075 | |
| 7076 | static const xmlChar * |
| 7077 | xmlParseNCName(xmlParserCtxtPtr ctxt) { |
| 7078 | const xmlChar *in; |
| 7079 | const xmlChar *ret; |
| 7080 | int count = 0; |
| 7081 | |
| 7082 | /* |
| 7083 | * Accelerator for simple ASCII names |
| 7084 | */ |
| 7085 | in = ctxt->input->cur; |
| 7086 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7087 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7088 | (*in == '_')) { |
| 7089 | in++; |
| 7090 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 7091 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 7092 | ((*in >= 0x30) && (*in <= 0x39)) || |
| 7093 | (*in == '_') || (*in == '-') || |
| 7094 | (*in == '.')) |
| 7095 | in++; |
| 7096 | if ((*in > 0) && (*in < 0x80)) { |
| 7097 | count = in - ctxt->input->cur; |
| 7098 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
| 7099 | ctxt->input->cur = in; |
| 7100 | ctxt->nbChars += count; |
| 7101 | ctxt->input->col += count; |
| 7102 | if (ret == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7103 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7104 | } |
| 7105 | return(ret); |
| 7106 | } |
| 7107 | } |
| 7108 | return(xmlParseNCNameComplex(ctxt)); |
| 7109 | } |
| 7110 | |
| 7111 | /** |
| 7112 | * xmlParseQName: |
| 7113 | * @ctxt: an XML parser context |
| 7114 | * @prefix: pointer to store the prefix part |
| 7115 | * |
| 7116 | * parse an XML Namespace QName |
| 7117 | * |
| 7118 | * [6] QName ::= (Prefix ':')? LocalPart |
| 7119 | * [7] Prefix ::= NCName |
| 7120 | * [8] LocalPart ::= NCName |
| 7121 | * |
| 7122 | * Returns the Name parsed or NULL |
| 7123 | */ |
| 7124 | |
| 7125 | static const xmlChar * |
| 7126 | xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) { |
| 7127 | const xmlChar *l, *p; |
| 7128 | |
| 7129 | GROW; |
| 7130 | |
| 7131 | l = xmlParseNCName(ctxt); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7132 | if (l == NULL) { |
| 7133 | if (CUR == ':') { |
| 7134 | l = xmlParseName(ctxt); |
| 7135 | if (l != NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7136 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 7137 | "Failed to parse QName '%s'\n", l, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7138 | *prefix = NULL; |
| 7139 | return(l); |
| 7140 | } |
| 7141 | } |
| 7142 | return(NULL); |
| 7143 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7144 | if (CUR == ':') { |
| 7145 | NEXT; |
| 7146 | p = l; |
| 7147 | l = xmlParseNCName(ctxt); |
| 7148 | if (l == NULL) { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7149 | xmlChar *tmp; |
| 7150 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7151 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 7152 | "Failed to parse QName '%s:'\n", p, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7153 | tmp = xmlBuildQName(BAD_CAST "", p, NULL, 0); |
| 7154 | p = xmlDictLookup(ctxt->dict, tmp, -1); |
| 7155 | if (tmp != NULL) xmlFree(tmp); |
| 7156 | *prefix = NULL; |
| 7157 | return(p); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7158 | } |
| 7159 | if (CUR == ':') { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7160 | xmlChar *tmp; |
| 7161 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7162 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 7163 | "Failed to parse QName '%s:%s:'\n", p, l, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7164 | NEXT; |
| 7165 | tmp = (xmlChar *) xmlParseName(ctxt); |
| 7166 | if (tmp != NULL) { |
| 7167 | tmp = xmlBuildQName(tmp, l, NULL, 0); |
| 7168 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 7169 | if (tmp != NULL) xmlFree(tmp); |
| 7170 | *prefix = p; |
| 7171 | return(l); |
| 7172 | } |
| 7173 | tmp = xmlBuildQName(BAD_CAST "", l, NULL, 0); |
| 7174 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 7175 | if (tmp != NULL) xmlFree(tmp); |
| 7176 | *prefix = p; |
| 7177 | return(l); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7178 | } |
| 7179 | *prefix = p; |
| 7180 | } else |
| 7181 | *prefix = NULL; |
| 7182 | return(l); |
| 7183 | } |
| 7184 | |
| 7185 | /** |
| 7186 | * xmlParseQNameAndCompare: |
| 7187 | * @ctxt: an XML parser context |
| 7188 | * @name: the localname |
| 7189 | * @prefix: the prefix, if any. |
| 7190 | * |
| 7191 | * parse an XML name and compares for match |
| 7192 | * (specialized for endtag parsing) |
| 7193 | * |
| 7194 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 7195 | * and the name for mismatch |
| 7196 | */ |
| 7197 | |
| 7198 | static const xmlChar * |
| 7199 | xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name, |
| 7200 | xmlChar const *prefix) { |
| 7201 | const xmlChar *cmp = name; |
| 7202 | const xmlChar *in; |
| 7203 | const xmlChar *ret; |
| 7204 | const xmlChar *prefix2; |
| 7205 | |
| 7206 | if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name)); |
| 7207 | |
| 7208 | GROW; |
| 7209 | in = ctxt->input->cur; |
| 7210 | |
| 7211 | cmp = prefix; |
| 7212 | while (*in != 0 && *in == *cmp) { |
| 7213 | ++in; |
| 7214 | ++cmp; |
| 7215 | } |
| 7216 | if ((*cmp == 0) && (*in == ':')) { |
| 7217 | in++; |
| 7218 | cmp = name; |
| 7219 | while (*in != 0 && *in == *cmp) { |
| 7220 | ++in; |
| 7221 | ++cmp; |
| 7222 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7223 | if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7224 | /* success */ |
| 7225 | ctxt->input->cur = in; |
| 7226 | return((const xmlChar*) 1); |
| 7227 | } |
| 7228 | } |
| 7229 | /* |
| 7230 | * all strings coms from the dictionary, equality can be done directly |
| 7231 | */ |
| 7232 | ret = xmlParseQName (ctxt, &prefix2); |
| 7233 | if ((ret == name) && (prefix == prefix2)) |
| 7234 | return((const xmlChar*) 1); |
| 7235 | return ret; |
| 7236 | } |
| 7237 | |
| 7238 | /** |
| 7239 | * xmlParseAttValueInternal: |
| 7240 | * @ctxt: an XML parser context |
| 7241 | * @len: attribute len result |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7242 | * @alloc: whether the attribute was reallocated as a new string |
| 7243 | * @normalize: if 1 then further non-CDATA normalization must be done |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7244 | * |
| 7245 | * parse a value for an attribute. |
| 7246 | * NOTE: if no normalization is needed, the routine will return pointers |
| 7247 | * directly from the data buffer. |
| 7248 | * |
| 7249 | * 3.3.3 Attribute-Value Normalization: |
| 7250 | * Before the value of an attribute is passed to the application or |
| 7251 | * checked for validity, the XML processor must normalize it as follows: |
| 7252 | * - a character reference is processed by appending the referenced |
| 7253 | * character to the attribute value |
| 7254 | * - an entity reference is processed by recursively processing the |
| 7255 | * replacement text of the entity |
| 7256 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 7257 | * appending #x20 to the normalized value, except that only a single |
| 7258 | * #x20 is appended for a "#xD#xA" sequence that is part of an external |
| 7259 | * parsed entity or the literal entity value of an internal parsed entity |
| 7260 | * - other characters are processed by appending them to the normalized value |
| 7261 | * If the declared value is not CDATA, then the XML processor must further |
| 7262 | * process the normalized attribute value by discarding any leading and |
| 7263 | * trailing space (#x20) characters, and by replacing sequences of space |
| 7264 | * (#x20) characters by a single space (#x20) character. |
| 7265 | * All attributes for which no declaration has been read should be treated |
| 7266 | * by a non-validating parser as if declared CDATA. |
| 7267 | * |
| 7268 | * Returns the AttValue parsed or NULL. The value has to be freed by the |
| 7269 | * caller if it was copied, this can be detected by val[*len] == 0. |
| 7270 | */ |
| 7271 | |
| 7272 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7273 | xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc, |
| 7274 | int normalize) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7275 | { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7276 | xmlChar limit = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7277 | const xmlChar *in = NULL, *start, *end, *last; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7278 | xmlChar *ret = NULL; |
| 7279 | |
| 7280 | GROW; |
| 7281 | in = (xmlChar *) CUR_PTR; |
| 7282 | if (*in != '"' && *in != '\'') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7283 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7284 | return (NULL); |
| 7285 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7286 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7287 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7288 | /* |
| 7289 | * try to handle in this routine the most common case where no |
| 7290 | * allocation of a new string is required and where content is |
| 7291 | * pure ASCII. |
| 7292 | */ |
| 7293 | limit = *in++; |
| 7294 | end = ctxt->input->end; |
| 7295 | start = in; |
| 7296 | if (in >= end) { |
| 7297 | const xmlChar *oldbase = ctxt->input->base; |
| 7298 | GROW; |
| 7299 | if (oldbase != ctxt->input->base) { |
| 7300 | long delta = ctxt->input->base - oldbase; |
| 7301 | start = start + delta; |
| 7302 | in = in + delta; |
| 7303 | } |
| 7304 | end = ctxt->input->end; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7305 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7306 | if (normalize) { |
| 7307 | /* |
| 7308 | * Skip any leading spaces |
| 7309 | */ |
| 7310 | while ((in < end) && (*in != limit) && |
| 7311 | ((*in == 0x20) || (*in == 0x9) || |
| 7312 | (*in == 0xA) || (*in == 0xD))) { |
| 7313 | in++; |
| 7314 | start = in; |
| 7315 | if (in >= end) { |
| 7316 | const xmlChar *oldbase = ctxt->input->base; |
| 7317 | GROW; |
| 7318 | if (oldbase != ctxt->input->base) { |
| 7319 | long delta = ctxt->input->base - oldbase; |
| 7320 | start = start + delta; |
| 7321 | in = in + delta; |
| 7322 | } |
| 7323 | end = ctxt->input->end; |
| 7324 | } |
| 7325 | } |
| 7326 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 7327 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 7328 | if ((*in++ == 0x20) && (*in == 0x20)) break; |
| 7329 | if (in >= end) { |
| 7330 | const xmlChar *oldbase = ctxt->input->base; |
| 7331 | GROW; |
| 7332 | if (oldbase != ctxt->input->base) { |
| 7333 | long delta = ctxt->input->base - oldbase; |
| 7334 | start = start + delta; |
| 7335 | in = in + delta; |
| 7336 | } |
| 7337 | end = ctxt->input->end; |
| 7338 | } |
| 7339 | } |
| 7340 | last = in; |
| 7341 | /* |
| 7342 | * skip the trailing blanks |
| 7343 | */ |
Daniel Veillard | c6e20e4 | 2003-09-11 16:30:26 +0000 | [diff] [blame] | 7344 | while ((last[-1] == 0x20) && (last > start)) last--; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7345 | while ((in < end) && (*in != limit) && |
| 7346 | ((*in == 0x20) || (*in == 0x9) || |
| 7347 | (*in == 0xA) || (*in == 0xD))) { |
| 7348 | in++; |
| 7349 | if (in >= end) { |
| 7350 | const xmlChar *oldbase = ctxt->input->base; |
| 7351 | GROW; |
| 7352 | if (oldbase != ctxt->input->base) { |
| 7353 | long delta = ctxt->input->base - oldbase; |
| 7354 | start = start + delta; |
| 7355 | in = in + delta; |
| 7356 | last = last + delta; |
| 7357 | } |
| 7358 | end = ctxt->input->end; |
| 7359 | } |
| 7360 | } |
| 7361 | if (*in != limit) goto need_complex; |
| 7362 | } else { |
| 7363 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 7364 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 7365 | in++; |
| 7366 | if (in >= end) { |
| 7367 | const xmlChar *oldbase = ctxt->input->base; |
| 7368 | GROW; |
| 7369 | if (oldbase != ctxt->input->base) { |
| 7370 | long delta = ctxt->input->base - oldbase; |
| 7371 | start = start + delta; |
| 7372 | in = in + delta; |
| 7373 | } |
| 7374 | end = ctxt->input->end; |
| 7375 | } |
| 7376 | } |
| 7377 | last = in; |
| 7378 | if (*in != limit) goto need_complex; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7379 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7380 | in++; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7381 | if (len != NULL) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7382 | *len = last - start; |
| 7383 | ret = (xmlChar *) start; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7384 | } else { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7385 | if (alloc) *alloc = 1; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7386 | ret = xmlStrndup(start, last - start); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7387 | } |
| 7388 | CUR_PTR = in; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7389 | if (alloc) *alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7390 | return ret; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7391 | need_complex: |
| 7392 | if (alloc) *alloc = 1; |
| 7393 | return xmlParseAttValueComplex(ctxt, len, normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7394 | } |
| 7395 | |
| 7396 | /** |
| 7397 | * xmlParseAttribute2: |
| 7398 | * @ctxt: an XML parser context |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7399 | * @pref: the element prefix |
| 7400 | * @elem: the element name |
| 7401 | * @prefix: a xmlChar ** used to store the value of the attribute prefix |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7402 | * @value: a xmlChar ** used to store the value of the attribute |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7403 | * @len: an int * to save the length of the attribute |
| 7404 | * @alloc: an int * to indicate if the attribute was allocated |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7405 | * |
| 7406 | * parse an attribute in the new SAX2 framework. |
| 7407 | * |
| 7408 | * Returns the attribute name, and the value in *value, . |
| 7409 | */ |
| 7410 | |
| 7411 | static const xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7412 | xmlParseAttribute2(xmlParserCtxtPtr ctxt, |
| 7413 | const xmlChar *pref, const xmlChar *elem, |
| 7414 | const xmlChar **prefix, xmlChar **value, |
| 7415 | int *len, int *alloc) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7416 | const xmlChar *name; |
| 7417 | xmlChar *val; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7418 | int normalize = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7419 | |
| 7420 | *value = NULL; |
| 7421 | GROW; |
| 7422 | name = xmlParseQName(ctxt, prefix); |
| 7423 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7424 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7425 | "error parsing attribute name\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7426 | return(NULL); |
| 7427 | } |
| 7428 | |
| 7429 | /* |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7430 | * get the type if needed |
| 7431 | */ |
| 7432 | if (ctxt->attsSpecial != NULL) { |
| 7433 | int type; |
| 7434 | |
| 7435 | type = (int) (long) xmlHashQLookup2(ctxt->attsSpecial, |
| 7436 | pref, elem, *prefix, name); |
| 7437 | if (type != 0) normalize = 1; |
| 7438 | } |
| 7439 | |
| 7440 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7441 | * read the value |
| 7442 | */ |
| 7443 | SKIP_BLANKS; |
| 7444 | if (RAW == '=') { |
| 7445 | NEXT; |
| 7446 | SKIP_BLANKS; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7447 | val = xmlParseAttValueInternal(ctxt, len, alloc, normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7448 | ctxt->instate = XML_PARSER_CONTENT; |
| 7449 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 7450 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7451 | "Specification mandate value for attribute %s\n", name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7452 | return(NULL); |
| 7453 | } |
| 7454 | |
| 7455 | /* |
| 7456 | * Check that xml:lang conforms to the specification |
| 7457 | * No more registered as an error, just generate a warning now |
| 7458 | * since this was deprecated in XML second edition |
| 7459 | */ |
| 7460 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) { |
| 7461 | if (!xmlCheckLanguageID(val)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 7462 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 7463 | "Malformed value for xml:lang : %s\n", |
| 7464 | val, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7465 | } |
| 7466 | } |
| 7467 | |
| 7468 | /* |
| 7469 | * Check that xml:space conforms to the specification |
| 7470 | */ |
| 7471 | if (xmlStrEqual(name, BAD_CAST "xml:space")) { |
| 7472 | if (xmlStrEqual(val, BAD_CAST "default")) |
| 7473 | *(ctxt->space) = 0; |
| 7474 | else if (xmlStrEqual(val, BAD_CAST "preserve")) |
| 7475 | *(ctxt->space) = 1; |
| 7476 | else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 7477 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7478 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
| 7479 | val); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7480 | } |
| 7481 | } |
| 7482 | |
| 7483 | *value = val; |
| 7484 | return(name); |
| 7485 | } |
| 7486 | |
| 7487 | /** |
| 7488 | * xmlParseStartTag2: |
| 7489 | * @ctxt: an XML parser context |
| 7490 | * |
| 7491 | * parse a start of tag either for rule element or |
| 7492 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 7493 | * This routine is called when running SAX2 parsing |
| 7494 | * |
| 7495 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 7496 | * |
| 7497 | * [ WFC: Unique Att Spec ] |
| 7498 | * No attribute name may appear more than once in the same start-tag or |
| 7499 | * empty-element tag. |
| 7500 | * |
| 7501 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 7502 | * |
| 7503 | * [ WFC: Unique Att Spec ] |
| 7504 | * No attribute name may appear more than once in the same start-tag or |
| 7505 | * empty-element tag. |
| 7506 | * |
| 7507 | * With namespace: |
| 7508 | * |
| 7509 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 7510 | * |
| 7511 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 7512 | * |
| 7513 | * Returns the element name parsed |
| 7514 | */ |
| 7515 | |
| 7516 | static const xmlChar * |
| 7517 | xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 7518 | const xmlChar **URI, int *tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7519 | const xmlChar *localname; |
| 7520 | const xmlChar *prefix; |
| 7521 | const xmlChar *attname; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7522 | const xmlChar *aprefix; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7523 | const xmlChar *nsname; |
| 7524 | xmlChar *attvalue; |
| 7525 | const xmlChar **atts = ctxt->atts; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7526 | int maxatts = ctxt->maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7527 | int nratts, nbatts, nbdef; |
| 7528 | int i, j, nbNs, attval; |
| 7529 | const xmlChar *base; |
| 7530 | unsigned long cur; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7531 | |
| 7532 | if (RAW != '<') return(NULL); |
| 7533 | NEXT1; |
| 7534 | |
| 7535 | /* |
| 7536 | * NOTE: it is crucial with the SAX2 API to never call SHRINK beyond that |
| 7537 | * point since the attribute values may be stored as pointers to |
| 7538 | * the buffer and calling SHRINK would destroy them ! |
| 7539 | * The Shrinking is only possible once the full set of attribute |
| 7540 | * callbacks have been done. |
| 7541 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7542 | reparse: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7543 | SHRINK; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7544 | base = ctxt->input->base; |
| 7545 | cur = ctxt->input->cur - ctxt->input->base; |
| 7546 | nbatts = 0; |
| 7547 | nratts = 0; |
| 7548 | nbdef = 0; |
| 7549 | nbNs = 0; |
| 7550 | attval = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7551 | |
| 7552 | localname = xmlParseQName(ctxt, &prefix); |
| 7553 | if (localname == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7554 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7555 | "StartTag: invalid element name\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7556 | return(NULL); |
| 7557 | } |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 7558 | *tlen = ctxt->input->cur - ctxt->input->base - cur; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7559 | |
| 7560 | /* |
| 7561 | * Now parse the attributes, it ends up with the ending |
| 7562 | * |
| 7563 | * (S Attribute)* S? |
| 7564 | */ |
| 7565 | SKIP_BLANKS; |
| 7566 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7567 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7568 | |
| 7569 | while ((RAW != '>') && |
| 7570 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 7571 | (IS_BYTE_CHAR(RAW))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7572 | const xmlChar *q = CUR_PTR; |
| 7573 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7574 | int len = -1, alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7575 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7576 | attname = xmlParseAttribute2(ctxt, prefix, localname, |
| 7577 | &aprefix, &attvalue, &len, &alloc); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7578 | if ((attname != NULL) && (attvalue != NULL)) { |
| 7579 | if (len < 0) len = xmlStrlen(attvalue); |
| 7580 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7581 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 7582 | xmlURIPtr uri; |
| 7583 | |
| 7584 | if (*URL != 0) { |
| 7585 | uri = xmlParseURI((const char *) URL); |
| 7586 | if (uri == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 7587 | xmlWarningMsg(ctxt, XML_WAR_NS_URI, |
| 7588 | "xmlns: %s not a valid URI\n", |
| 7589 | URL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7590 | } else { |
| 7591 | if (uri->scheme == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 7592 | xmlWarningMsg(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 7593 | "xmlns: URI %s is not absolute\n", |
| 7594 | URL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7595 | } |
| 7596 | xmlFreeURI(uri); |
| 7597 | } |
| 7598 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7599 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7600 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7601 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7602 | for (j = 1;j <= nbNs;j++) |
| 7603 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 7604 | break; |
| 7605 | if (j <= nbNs) |
| 7606 | xmlErrAttributeDup(ctxt, NULL, attname); |
| 7607 | else |
| 7608 | if (nsPush(ctxt, NULL, URL) > 0) nbNs++; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7609 | if (alloc != 0) xmlFree(attvalue); |
| 7610 | SKIP_BLANKS; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7611 | continue; |
| 7612 | } |
| 7613 | if (aprefix == ctxt->str_xmlns) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7614 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 7615 | xmlURIPtr uri; |
| 7616 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7617 | if (attname == ctxt->str_xml) { |
| 7618 | if (URL != ctxt->str_xml_ns) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7619 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 7620 | "xml namespace prefix mapped to wrong URI\n", |
| 7621 | NULL, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7622 | } |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7623 | /* |
| 7624 | * Do not keep a namespace definition node |
| 7625 | */ |
| 7626 | if (alloc != 0) xmlFree(attvalue); |
| 7627 | SKIP_BLANKS; |
| 7628 | continue; |
| 7629 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7630 | uri = xmlParseURI((const char *) URL); |
| 7631 | if (uri == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 7632 | xmlWarningMsg(ctxt, XML_WAR_NS_URI, |
| 7633 | "xmlns:%s: '%s' is not a valid URI\n", |
| 7634 | attname, URL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7635 | } else { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7636 | if ((ctxt->pedantic) && (uri->scheme == NULL)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 7637 | xmlWarningMsg(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 7638 | "xmlns:%s: URI %s is not absolute\n", |
| 7639 | attname, URL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7640 | } |
| 7641 | xmlFreeURI(uri); |
| 7642 | } |
| 7643 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7644 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7645 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7646 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7647 | for (j = 1;j <= nbNs;j++) |
| 7648 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 7649 | break; |
| 7650 | if (j <= nbNs) |
| 7651 | xmlErrAttributeDup(ctxt, aprefix, attname); |
| 7652 | else |
| 7653 | if (nsPush(ctxt, attname, URL) > 0) nbNs++; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7654 | if (alloc != 0) xmlFree(attvalue); |
| 7655 | SKIP_BLANKS; |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 7656 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7657 | continue; |
| 7658 | } |
| 7659 | |
| 7660 | /* |
| 7661 | * Add the pair to atts |
| 7662 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7663 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 7664 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7665 | if (attvalue[len] == 0) |
| 7666 | xmlFree(attvalue); |
| 7667 | goto failed; |
| 7668 | } |
| 7669 | maxatts = ctxt->maxatts; |
| 7670 | atts = ctxt->atts; |
| 7671 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7672 | ctxt->attallocs[nratts++] = alloc; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7673 | atts[nbatts++] = attname; |
| 7674 | atts[nbatts++] = aprefix; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7675 | atts[nbatts++] = NULL; /* the URI will be fetched later */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7676 | atts[nbatts++] = attvalue; |
| 7677 | attvalue += len; |
| 7678 | atts[nbatts++] = attvalue; |
| 7679 | /* |
| 7680 | * tag if some deallocation is needed |
| 7681 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7682 | if (alloc != 0) attval = 1; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7683 | } else { |
| 7684 | if ((attvalue != NULL) && (attvalue[len] == 0)) |
| 7685 | xmlFree(attvalue); |
| 7686 | } |
| 7687 | |
| 7688 | failed: |
| 7689 | |
| 7690 | GROW |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7691 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7692 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 7693 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7694 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7695 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 7696 | "attributes construct error\n"); |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 7697 | break; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7698 | } |
| 7699 | SKIP_BLANKS; |
| 7700 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 7701 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7702 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7703 | "xmlParseStartTag: problem parsing attributes\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7704 | break; |
| 7705 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7706 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7707 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7708 | } |
| 7709 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7710 | /* |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7711 | * The attributes defaulting |
| 7712 | */ |
| 7713 | if (ctxt->attsDefault != NULL) { |
| 7714 | xmlDefAttrsPtr defaults; |
| 7715 | |
| 7716 | defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix); |
| 7717 | if (defaults != NULL) { |
| 7718 | for (i = 0;i < defaults->nbAttrs;i++) { |
| 7719 | attname = defaults->values[4 * i]; |
| 7720 | aprefix = defaults->values[4 * i + 1]; |
| 7721 | |
| 7722 | /* |
| 7723 | * special work for namespaces defaulted defs |
| 7724 | */ |
| 7725 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
| 7726 | /* |
| 7727 | * check that it's not a defined namespace |
| 7728 | */ |
| 7729 | for (j = 1;j <= nbNs;j++) |
| 7730 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 7731 | break; |
| 7732 | if (j <= nbNs) continue; |
| 7733 | |
| 7734 | nsname = xmlGetNamespace(ctxt, NULL); |
| 7735 | if (nsname != defaults->values[4 * i + 2]) { |
| 7736 | if (nsPush(ctxt, NULL, |
| 7737 | defaults->values[4 * i + 2]) > 0) |
| 7738 | nbNs++; |
| 7739 | } |
| 7740 | } else if (aprefix == ctxt->str_xmlns) { |
| 7741 | /* |
| 7742 | * check that it's not a defined namespace |
| 7743 | */ |
| 7744 | for (j = 1;j <= nbNs;j++) |
| 7745 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 7746 | break; |
| 7747 | if (j <= nbNs) continue; |
| 7748 | |
| 7749 | nsname = xmlGetNamespace(ctxt, attname); |
| 7750 | if (nsname != defaults->values[2]) { |
| 7751 | if (nsPush(ctxt, attname, |
| 7752 | defaults->values[4 * i + 2]) > 0) |
| 7753 | nbNs++; |
| 7754 | } |
| 7755 | } else { |
| 7756 | /* |
| 7757 | * check that it's not a defined attribute |
| 7758 | */ |
| 7759 | for (j = 0;j < nbatts;j+=5) { |
| 7760 | if ((attname == atts[j]) && (aprefix == atts[j+1])) |
| 7761 | break; |
| 7762 | } |
| 7763 | if (j < nbatts) continue; |
| 7764 | |
| 7765 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 7766 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 7767 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7768 | } |
| 7769 | maxatts = ctxt->maxatts; |
| 7770 | atts = ctxt->atts; |
| 7771 | } |
| 7772 | atts[nbatts++] = attname; |
| 7773 | atts[nbatts++] = aprefix; |
| 7774 | if (aprefix == NULL) |
| 7775 | atts[nbatts++] = NULL; |
| 7776 | else |
| 7777 | atts[nbatts++] = xmlGetNamespace(ctxt, aprefix); |
| 7778 | atts[nbatts++] = defaults->values[4 * i + 2]; |
| 7779 | atts[nbatts++] = defaults->values[4 * i + 3]; |
| 7780 | nbdef++; |
| 7781 | } |
| 7782 | } |
| 7783 | } |
| 7784 | } |
| 7785 | |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 7786 | /* |
| 7787 | * The attributes checkings |
| 7788 | */ |
| 7789 | for (i = 0; i < nbatts;i += 5) { |
| 7790 | nsname = xmlGetNamespace(ctxt, atts[i + 1]); |
| 7791 | if ((atts[i + 1] != NULL) && (nsname == NULL)) { |
| 7792 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 7793 | "Namespace prefix %s for %s on %s is not defined\n", |
| 7794 | atts[i + 1], atts[i], localname); |
| 7795 | } |
| 7796 | atts[i + 2] = nsname; |
| 7797 | /* |
| 7798 | * [ WFC: Unique Att Spec ] |
| 7799 | * No attribute name may appear more than once in the same |
| 7800 | * start-tag or empty-element tag. |
| 7801 | * As extended by the Namespace in XML REC. |
| 7802 | */ |
| 7803 | for (j = 0; j < i;j += 5) { |
| 7804 | if (atts[i] == atts[j]) { |
| 7805 | if (atts[i+1] == atts[j+1]) { |
| 7806 | xmlErrAttributeDup(ctxt, atts[i+1], atts[i]); |
| 7807 | break; |
| 7808 | } |
| 7809 | if ((nsname != NULL) && (atts[j + 2] == nsname)) { |
| 7810 | xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED, |
| 7811 | "Namespaced Attribute %s in '%s' redefined\n", |
| 7812 | atts[i], nsname, NULL); |
| 7813 | break; |
| 7814 | } |
| 7815 | } |
| 7816 | } |
| 7817 | } |
| 7818 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7819 | nsname = xmlGetNamespace(ctxt, prefix); |
| 7820 | if ((prefix != NULL) && (nsname == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7821 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 7822 | "Namespace prefix %s on %s is not defined\n", |
| 7823 | prefix, localname, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7824 | } |
| 7825 | *pref = prefix; |
| 7826 | *URI = nsname; |
| 7827 | |
| 7828 | /* |
| 7829 | * SAX: Start of Element ! |
| 7830 | */ |
| 7831 | if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) && |
| 7832 | (!ctxt->disableSAX)) { |
| 7833 | if (nbNs > 0) |
| 7834 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 7835 | nsname, nbNs, &ctxt->nsTab[ctxt->nsNr - 2 * nbNs], |
| 7836 | nbatts / 5, nbdef, atts); |
| 7837 | else |
| 7838 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 7839 | nsname, 0, NULL, nbatts / 5, nbdef, atts); |
| 7840 | } |
| 7841 | |
| 7842 | /* |
| 7843 | * Free up attribute allocated strings if needed |
| 7844 | */ |
| 7845 | if (attval != 0) { |
| 7846 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 7847 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 7848 | xmlFree((xmlChar *) atts[i]); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7849 | } |
| 7850 | |
| 7851 | return(localname); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7852 | |
| 7853 | base_changed: |
| 7854 | /* |
| 7855 | * the attribute strings are valid iif the base didn't changed |
| 7856 | */ |
| 7857 | if (attval != 0) { |
| 7858 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 7859 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 7860 | xmlFree((xmlChar *) atts[i]); |
| 7861 | } |
| 7862 | ctxt->input->cur = ctxt->input->base + cur; |
| 7863 | if (ctxt->wellFormed == 1) { |
| 7864 | goto reparse; |
| 7865 | } |
| 7866 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7867 | } |
| 7868 | |
| 7869 | /** |
| 7870 | * xmlParseEndTag2: |
| 7871 | * @ctxt: an XML parser context |
| 7872 | * @line: line of the start tag |
| 7873 | * @nsNr: number of namespaces on the start tag |
| 7874 | * |
| 7875 | * parse an end of tag |
| 7876 | * |
| 7877 | * [42] ETag ::= '</' Name S? '>' |
| 7878 | * |
| 7879 | * With namespace |
| 7880 | * |
| 7881 | * [NS 9] ETag ::= '</' QName S? '>' |
| 7882 | */ |
| 7883 | |
| 7884 | static void |
| 7885 | xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlChar *prefix, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 7886 | const xmlChar *URI, int line, int nsNr, int tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7887 | const xmlChar *name; |
| 7888 | |
| 7889 | GROW; |
| 7890 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7891 | xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7892 | return; |
| 7893 | } |
| 7894 | SKIP(2); |
| 7895 | |
William M. Brack | 13dfa87 | 2004-09-18 04:52:08 +0000 | [diff] [blame] | 7896 | if ((tlen > 0) && (xmlStrncmp(ctxt->input->cur, ctxt->name, tlen) == 0)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 7897 | if (ctxt->input->cur[tlen] == '>') { |
| 7898 | ctxt->input->cur += tlen + 1; |
| 7899 | goto done; |
| 7900 | } |
| 7901 | ctxt->input->cur += tlen; |
| 7902 | name = (xmlChar*)1; |
| 7903 | } else { |
| 7904 | if (prefix == NULL) |
| 7905 | name = xmlParseNameAndCompare(ctxt, ctxt->name); |
| 7906 | else |
| 7907 | name = xmlParseQNameAndCompare(ctxt, ctxt->name, prefix); |
| 7908 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7909 | |
| 7910 | /* |
| 7911 | * We should definitely be at the ending "S? '>'" part |
| 7912 | */ |
| 7913 | GROW; |
| 7914 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 7915 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7916 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7917 | } else |
| 7918 | NEXT1; |
| 7919 | |
| 7920 | /* |
| 7921 | * [ WFC: Element Type Match ] |
| 7922 | * The Name in an element's end-tag must match the element type in the |
| 7923 | * start-tag. |
| 7924 | * |
| 7925 | */ |
| 7926 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 7927 | if (name == NULL) name = BAD_CAST "unparseable"; |
| 7928 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7929 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 7930 | ctxt->name, line, name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7931 | } |
| 7932 | |
| 7933 | /* |
| 7934 | * SAX: End of Tag |
| 7935 | */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 7936 | done: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7937 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 7938 | (!ctxt->disableSAX)) |
| 7939 | ctxt->sax->endElementNs(ctxt->userData, ctxt->name, prefix, URI); |
| 7940 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7941 | spacePop(ctxt); |
| 7942 | if (nsNr != 0) |
| 7943 | nsPop(ctxt, nsNr); |
| 7944 | return; |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 7945 | } |
| 7946 | |
| 7947 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7948 | * xmlParseCDSect: |
| 7949 | * @ctxt: an XML parser context |
| 7950 | * |
| 7951 | * Parse escaped pure raw content. |
| 7952 | * |
| 7953 | * [18] CDSect ::= CDStart CData CDEnd |
| 7954 | * |
| 7955 | * [19] CDStart ::= '<![CDATA[' |
| 7956 | * |
| 7957 | * [20] Data ::= (Char* - (Char* ']]>' Char*)) |
| 7958 | * |
| 7959 | * [21] CDEnd ::= ']]>' |
| 7960 | */ |
| 7961 | void |
| 7962 | xmlParseCDSect(xmlParserCtxtPtr ctxt) { |
| 7963 | xmlChar *buf = NULL; |
| 7964 | int len = 0; |
| 7965 | int size = XML_PARSER_BUFFER_SIZE; |
| 7966 | int r, rl; |
| 7967 | int s, sl; |
| 7968 | int cur, l; |
| 7969 | int count = 0; |
| 7970 | |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 7971 | /* Check 2.6.0 was NXT(0) not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 7972 | if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7973 | SKIP(9); |
| 7974 | } else |
| 7975 | return; |
| 7976 | |
| 7977 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
| 7978 | r = CUR_CHAR(rl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 7979 | if (!IS_CHAR(r)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7980 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7981 | ctxt->instate = XML_PARSER_CONTENT; |
| 7982 | return; |
| 7983 | } |
| 7984 | NEXTL(rl); |
| 7985 | s = CUR_CHAR(sl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 7986 | if (!IS_CHAR(s)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7987 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7988 | ctxt->instate = XML_PARSER_CONTENT; |
| 7989 | return; |
| 7990 | } |
| 7991 | NEXTL(sl); |
| 7992 | cur = CUR_CHAR(l); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 7993 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7994 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7995 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7996 | return; |
| 7997 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 7998 | while (IS_CHAR(cur) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7999 | ((r != ']') || (s != ']') || (cur != '>'))) { |
| 8000 | if (len + 5 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 8001 | xmlChar *tmp; |
| 8002 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8003 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 8004 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 8005 | if (tmp == NULL) { |
| 8006 | xmlFree(buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8007 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8008 | return; |
| 8009 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 8010 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8011 | } |
| 8012 | COPY_BUF(rl,buf,len,r); |
| 8013 | r = s; |
| 8014 | rl = sl; |
| 8015 | s = cur; |
| 8016 | sl = l; |
| 8017 | count++; |
| 8018 | if (count > 50) { |
| 8019 | GROW; |
| 8020 | count = 0; |
| 8021 | } |
| 8022 | NEXTL(l); |
| 8023 | cur = CUR_CHAR(l); |
| 8024 | } |
| 8025 | buf[len] = 0; |
| 8026 | ctxt->instate = XML_PARSER_CONTENT; |
| 8027 | if (cur != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 8028 | xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8029 | "CData section not finished\n%.50s\n", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8030 | xmlFree(buf); |
| 8031 | return; |
| 8032 | } |
| 8033 | NEXTL(l); |
| 8034 | |
| 8035 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8036 | * OK the buffer is to be consumed as cdata. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8037 | */ |
| 8038 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 8039 | if (ctxt->sax->cdataBlock != NULL) |
| 8040 | ctxt->sax->cdataBlock(ctxt->userData, buf, len); |
Daniel Veillard | 7583a59 | 2001-07-08 13:15:55 +0000 | [diff] [blame] | 8041 | else if (ctxt->sax->characters != NULL) |
| 8042 | ctxt->sax->characters(ctxt->userData, buf, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8043 | } |
| 8044 | xmlFree(buf); |
| 8045 | } |
| 8046 | |
| 8047 | /** |
| 8048 | * xmlParseContent: |
| 8049 | * @ctxt: an XML parser context |
| 8050 | * |
| 8051 | * Parse a content: |
| 8052 | * |
| 8053 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 8054 | */ |
| 8055 | |
| 8056 | void |
| 8057 | xmlParseContent(xmlParserCtxtPtr ctxt) { |
| 8058 | GROW; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 8059 | while ((RAW != 0) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8060 | ((RAW != '<') || (NXT(1) != '/'))) { |
| 8061 | const xmlChar *test = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 8062 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8063 | const xmlChar *cur = ctxt->input->cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8064 | |
| 8065 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8066 | * First case : a Processing Instruction. |
| 8067 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 8068 | if ((*cur == '<') && (cur[1] == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8069 | xmlParsePI(ctxt); |
| 8070 | } |
| 8071 | |
| 8072 | /* |
| 8073 | * Second case : a CDSection |
| 8074 | */ |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 8075 | /* 2.6.0 test was *cur not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 8076 | else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8077 | xmlParseCDSect(ctxt); |
| 8078 | } |
| 8079 | |
| 8080 | /* |
| 8081 | * Third case : a comment |
| 8082 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8083 | else if ((*cur == '<') && (NXT(1) == '!') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8084 | (NXT(2) == '-') && (NXT(3) == '-')) { |
| 8085 | xmlParseComment(ctxt); |
| 8086 | ctxt->instate = XML_PARSER_CONTENT; |
| 8087 | } |
| 8088 | |
| 8089 | /* |
| 8090 | * Fourth case : a sub-element. |
| 8091 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8092 | else if (*cur == '<') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8093 | xmlParseElement(ctxt); |
| 8094 | } |
| 8095 | |
| 8096 | /* |
| 8097 | * Fifth case : a reference. If if has not been resolved, |
| 8098 | * parsing returns it's Name, create the node |
| 8099 | */ |
| 8100 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8101 | else if (*cur == '&') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8102 | xmlParseReference(ctxt); |
| 8103 | } |
| 8104 | |
| 8105 | /* |
| 8106 | * Last case, text. Note that References are handled directly. |
| 8107 | */ |
| 8108 | else { |
| 8109 | xmlParseCharData(ctxt, 0); |
| 8110 | } |
| 8111 | |
| 8112 | GROW; |
| 8113 | /* |
| 8114 | * Pop-up of finished entities. |
| 8115 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 8116 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8117 | xmlPopInput(ctxt); |
| 8118 | SHRINK; |
| 8119 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 8120 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8121 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 8122 | "detected an error in element content\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8123 | ctxt->instate = XML_PARSER_EOF; |
| 8124 | break; |
| 8125 | } |
| 8126 | } |
| 8127 | } |
| 8128 | |
| 8129 | /** |
| 8130 | * xmlParseElement: |
| 8131 | * @ctxt: an XML parser context |
| 8132 | * |
| 8133 | * parse an XML element, this is highly recursive |
| 8134 | * |
| 8135 | * [39] element ::= EmptyElemTag | STag content ETag |
| 8136 | * |
| 8137 | * [ WFC: Element Type Match ] |
| 8138 | * The Name in an element's end-tag must match the element type in the |
| 8139 | * start-tag. |
| 8140 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8141 | */ |
| 8142 | |
| 8143 | void |
| 8144 | xmlParseElement(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 8145 | const xmlChar *name; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8146 | const xmlChar *prefix; |
| 8147 | const xmlChar *URI; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8148 | xmlParserNodeInfo node_info; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 8149 | int line, tlen; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8150 | xmlNodePtr ret; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8151 | int nsNr = ctxt->nsNr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8152 | |
| 8153 | /* Capture start position */ |
| 8154 | if (ctxt->record_info) { |
| 8155 | node_info.begin_pos = ctxt->input->consumed + |
| 8156 | (CUR_PTR - ctxt->input->base); |
| 8157 | node_info.begin_line = ctxt->input->line; |
| 8158 | } |
| 8159 | |
| 8160 | if (ctxt->spaceNr == 0) |
| 8161 | spacePush(ctxt, -1); |
| 8162 | else |
| 8163 | spacePush(ctxt, *ctxt->space); |
| 8164 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 8165 | line = ctxt->input->line; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8166 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8167 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8168 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 8169 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8170 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8171 | else |
| 8172 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8173 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8174 | if (name == NULL) { |
| 8175 | spacePop(ctxt); |
| 8176 | return; |
| 8177 | } |
| 8178 | namePush(ctxt, name); |
| 8179 | ret = ctxt->node; |
| 8180 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 8181 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8182 | /* |
| 8183 | * [ VC: Root Element Type ] |
| 8184 | * The Name in the document type declaration must match the element |
| 8185 | * type of the root element. |
| 8186 | */ |
| 8187 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 8188 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 8189 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 8190 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8191 | |
| 8192 | /* |
| 8193 | * Check for an Empty Element. |
| 8194 | */ |
| 8195 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 8196 | SKIP(2); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8197 | if (ctxt->sax2) { |
| 8198 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 8199 | (!ctxt->disableSAX)) |
| 8200 | ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8201 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8202 | } else { |
| 8203 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 8204 | (!ctxt->disableSAX)) |
| 8205 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8206 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8207 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8208 | namePop(ctxt); |
| 8209 | spacePop(ctxt); |
| 8210 | if (nsNr != ctxt->nsNr) |
| 8211 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8212 | if ( ret != NULL && ctxt->record_info ) { |
| 8213 | node_info.end_pos = ctxt->input->consumed + |
| 8214 | (CUR_PTR - ctxt->input->base); |
| 8215 | node_info.end_line = ctxt->input->line; |
| 8216 | node_info.node = ret; |
| 8217 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 8218 | } |
| 8219 | return; |
| 8220 | } |
| 8221 | if (RAW == '>') { |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 8222 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8223 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8224 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED, |
| 8225 | "Couldn't find end of Start Tag %s line %d\n", |
| 8226 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8227 | |
| 8228 | /* |
| 8229 | * end of parsing of this node. |
| 8230 | */ |
| 8231 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8232 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8233 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8234 | if (nsNr != ctxt->nsNr) |
| 8235 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8236 | |
| 8237 | /* |
| 8238 | * Capture end position and add node |
| 8239 | */ |
| 8240 | if ( ret != NULL && ctxt->record_info ) { |
| 8241 | node_info.end_pos = ctxt->input->consumed + |
| 8242 | (CUR_PTR - ctxt->input->base); |
| 8243 | node_info.end_line = ctxt->input->line; |
| 8244 | node_info.node = ret; |
| 8245 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 8246 | } |
| 8247 | return; |
| 8248 | } |
| 8249 | |
| 8250 | /* |
| 8251 | * Parse the content of the element: |
| 8252 | */ |
| 8253 | xmlParseContent(ctxt); |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 8254 | if (!IS_BYTE_CHAR(RAW)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8255 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED, |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 8256 | "Premature end of data in tag %s line %d\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8257 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8258 | |
| 8259 | /* |
| 8260 | * end of parsing of this node. |
| 8261 | */ |
| 8262 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8263 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8264 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8265 | if (nsNr != ctxt->nsNr) |
| 8266 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8267 | return; |
| 8268 | } |
| 8269 | |
| 8270 | /* |
| 8271 | * parse the end of tag: '</' should be here. |
| 8272 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8273 | if (ctxt->sax2) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 8274 | xmlParseEndTag2(ctxt, prefix, URI, line, ctxt->nsNr - nsNr, tlen); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8275 | namePop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8276 | } |
| 8277 | #ifdef LIBXML_SAX1_ENABLED |
| 8278 | else |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8279 | xmlParseEndTag1(ctxt, line); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8280 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8281 | |
| 8282 | /* |
| 8283 | * Capture end position and add node |
| 8284 | */ |
| 8285 | if ( ret != NULL && ctxt->record_info ) { |
| 8286 | node_info.end_pos = ctxt->input->consumed + |
| 8287 | (CUR_PTR - ctxt->input->base); |
| 8288 | node_info.end_line = ctxt->input->line; |
| 8289 | node_info.node = ret; |
| 8290 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 8291 | } |
| 8292 | } |
| 8293 | |
| 8294 | /** |
| 8295 | * xmlParseVersionNum: |
| 8296 | * @ctxt: an XML parser context |
| 8297 | * |
| 8298 | * parse the XML version value. |
| 8299 | * |
| 8300 | * [26] VersionNum ::= ([a-zA-Z0-9_.:] | '-')+ |
| 8301 | * |
| 8302 | * Returns the string giving the XML version number, or NULL |
| 8303 | */ |
| 8304 | xmlChar * |
| 8305 | xmlParseVersionNum(xmlParserCtxtPtr ctxt) { |
| 8306 | xmlChar *buf = NULL; |
| 8307 | int len = 0; |
| 8308 | int size = 10; |
| 8309 | xmlChar cur; |
| 8310 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8311 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8312 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8313 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8314 | return(NULL); |
| 8315 | } |
| 8316 | cur = CUR; |
| 8317 | while (((cur >= 'a') && (cur <= 'z')) || |
| 8318 | ((cur >= 'A') && (cur <= 'Z')) || |
| 8319 | ((cur >= '0') && (cur <= '9')) || |
| 8320 | (cur == '_') || (cur == '.') || |
| 8321 | (cur == ':') || (cur == '-')) { |
| 8322 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 8323 | xmlChar *tmp; |
| 8324 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8325 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 8326 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 8327 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8328 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8329 | return(NULL); |
| 8330 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 8331 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8332 | } |
| 8333 | buf[len++] = cur; |
| 8334 | NEXT; |
| 8335 | cur=CUR; |
| 8336 | } |
| 8337 | buf[len] = 0; |
| 8338 | return(buf); |
| 8339 | } |
| 8340 | |
| 8341 | /** |
| 8342 | * xmlParseVersionInfo: |
| 8343 | * @ctxt: an XML parser context |
| 8344 | * |
| 8345 | * parse the XML version. |
| 8346 | * |
| 8347 | * [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ") |
| 8348 | * |
| 8349 | * [25] Eq ::= S? '=' S? |
| 8350 | * |
| 8351 | * Returns the version string, e.g. "1.0" |
| 8352 | */ |
| 8353 | |
| 8354 | xmlChar * |
| 8355 | xmlParseVersionInfo(xmlParserCtxtPtr ctxt) { |
| 8356 | xmlChar *version = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8357 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 8358 | if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8359 | SKIP(7); |
| 8360 | SKIP_BLANKS; |
| 8361 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8362 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8363 | return(NULL); |
| 8364 | } |
| 8365 | NEXT; |
| 8366 | SKIP_BLANKS; |
| 8367 | if (RAW == '"') { |
| 8368 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8369 | version = xmlParseVersionNum(ctxt); |
| 8370 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8371 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8372 | } else |
| 8373 | NEXT; |
| 8374 | } else if (RAW == '\''){ |
| 8375 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8376 | version = xmlParseVersionNum(ctxt); |
| 8377 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8378 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8379 | } else |
| 8380 | NEXT; |
| 8381 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8382 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8383 | } |
| 8384 | } |
| 8385 | return(version); |
| 8386 | } |
| 8387 | |
| 8388 | /** |
| 8389 | * xmlParseEncName: |
| 8390 | * @ctxt: an XML parser context |
| 8391 | * |
| 8392 | * parse the XML encoding name |
| 8393 | * |
| 8394 | * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')* |
| 8395 | * |
| 8396 | * Returns the encoding name value or NULL |
| 8397 | */ |
| 8398 | xmlChar * |
| 8399 | xmlParseEncName(xmlParserCtxtPtr ctxt) { |
| 8400 | xmlChar *buf = NULL; |
| 8401 | int len = 0; |
| 8402 | int size = 10; |
| 8403 | xmlChar cur; |
| 8404 | |
| 8405 | cur = CUR; |
| 8406 | if (((cur >= 'a') && (cur <= 'z')) || |
| 8407 | ((cur >= 'A') && (cur <= 'Z'))) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8408 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8409 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8410 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8411 | return(NULL); |
| 8412 | } |
| 8413 | |
| 8414 | buf[len++] = cur; |
| 8415 | NEXT; |
| 8416 | cur = CUR; |
| 8417 | while (((cur >= 'a') && (cur <= 'z')) || |
| 8418 | ((cur >= 'A') && (cur <= 'Z')) || |
| 8419 | ((cur >= '0') && (cur <= '9')) || |
| 8420 | (cur == '.') || (cur == '_') || |
| 8421 | (cur == '-')) { |
| 8422 | if (len + 1 >= size) { |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 8423 | xmlChar *tmp; |
| 8424 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8425 | size *= 2; |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 8426 | tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 8427 | if (tmp == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8428 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 8429 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8430 | return(NULL); |
| 8431 | } |
Daniel Veillard | 2248ff1 | 2004-09-22 23:05:14 +0000 | [diff] [blame] | 8432 | buf = tmp; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8433 | } |
| 8434 | buf[len++] = cur; |
| 8435 | NEXT; |
| 8436 | cur = CUR; |
| 8437 | if (cur == 0) { |
| 8438 | SHRINK; |
| 8439 | GROW; |
| 8440 | cur = CUR; |
| 8441 | } |
| 8442 | } |
| 8443 | buf[len] = 0; |
| 8444 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8445 | xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8446 | } |
| 8447 | return(buf); |
| 8448 | } |
| 8449 | |
| 8450 | /** |
| 8451 | * xmlParseEncodingDecl: |
| 8452 | * @ctxt: an XML parser context |
| 8453 | * |
| 8454 | * parse the XML encoding declaration |
| 8455 | * |
| 8456 | * [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'") |
| 8457 | * |
| 8458 | * this setups the conversion filters. |
| 8459 | * |
| 8460 | * Returns the encoding value or NULL |
| 8461 | */ |
| 8462 | |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 8463 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8464 | xmlParseEncodingDecl(xmlParserCtxtPtr ctxt) { |
| 8465 | xmlChar *encoding = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8466 | |
| 8467 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 8468 | if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8469 | SKIP(8); |
| 8470 | SKIP_BLANKS; |
| 8471 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8472 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8473 | return(NULL); |
| 8474 | } |
| 8475 | NEXT; |
| 8476 | SKIP_BLANKS; |
| 8477 | if (RAW == '"') { |
| 8478 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8479 | encoding = xmlParseEncName(ctxt); |
| 8480 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8481 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8482 | } else |
| 8483 | NEXT; |
| 8484 | } else if (RAW == '\''){ |
| 8485 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8486 | encoding = xmlParseEncName(ctxt); |
| 8487 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8488 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8489 | } else |
| 8490 | NEXT; |
Daniel Veillard | 82ac6b0 | 2002-02-17 23:18:55 +0000 | [diff] [blame] | 8491 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8492 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8493 | } |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 8494 | /* |
| 8495 | * UTF-16 encoding stwich has already taken place at this stage, |
| 8496 | * more over the little-endian/big-endian selection is already done |
| 8497 | */ |
| 8498 | if ((encoding != NULL) && |
| 8499 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-16")) || |
| 8500 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF16")))) { |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 8501 | if (ctxt->encoding != NULL) |
| 8502 | xmlFree((xmlChar *) ctxt->encoding); |
| 8503 | ctxt->encoding = encoding; |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 8504 | } |
| 8505 | /* |
| 8506 | * UTF-8 encoding is handled natively |
| 8507 | */ |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 8508 | else if ((encoding != NULL) && |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 8509 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-8")) || |
| 8510 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF8")))) { |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 8511 | if (ctxt->encoding != NULL) |
| 8512 | xmlFree((xmlChar *) ctxt->encoding); |
| 8513 | ctxt->encoding = encoding; |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 8514 | } |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 8515 | else if (encoding != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8516 | xmlCharEncodingHandlerPtr handler; |
| 8517 | |
| 8518 | if (ctxt->input->encoding != NULL) |
| 8519 | xmlFree((xmlChar *) ctxt->input->encoding); |
| 8520 | ctxt->input->encoding = encoding; |
| 8521 | |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 8522 | handler = xmlFindCharEncodingHandler((const char *) encoding); |
| 8523 | if (handler != NULL) { |
| 8524 | xmlSwitchToEncoding(ctxt, handler); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8525 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8526 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 8527 | "Unsupported encoding %s\n", encoding); |
| 8528 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8529 | } |
| 8530 | } |
| 8531 | } |
| 8532 | return(encoding); |
| 8533 | } |
| 8534 | |
| 8535 | /** |
| 8536 | * xmlParseSDDecl: |
| 8537 | * @ctxt: an XML parser context |
| 8538 | * |
| 8539 | * parse the XML standalone declaration |
| 8540 | * |
| 8541 | * [32] SDDecl ::= S 'standalone' Eq |
| 8542 | * (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"')) |
| 8543 | * |
| 8544 | * [ VC: Standalone Document Declaration ] |
| 8545 | * TODO The standalone document declaration must have the value "no" |
| 8546 | * if any external markup declarations contain declarations of: |
| 8547 | * - attributes with default values, if elements to which these |
| 8548 | * attributes apply appear in the document without specifications |
| 8549 | * of values for these attributes, or |
| 8550 | * - entities (other than amp, lt, gt, apos, quot), if references |
| 8551 | * to those entities appear in the document, or |
| 8552 | * - attributes with values subject to normalization, where the |
| 8553 | * attribute appears in the document with a value which will change |
| 8554 | * as a result of normalization, or |
| 8555 | * - element types with element content, if white space occurs directly |
| 8556 | * within any instance of those types. |
| 8557 | * |
| 8558 | * Returns 1 if standalone, 0 otherwise |
| 8559 | */ |
| 8560 | |
| 8561 | int |
| 8562 | xmlParseSDDecl(xmlParserCtxtPtr ctxt) { |
| 8563 | int standalone = -1; |
| 8564 | |
| 8565 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 8566 | 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] | 8567 | SKIP(10); |
| 8568 | SKIP_BLANKS; |
| 8569 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8570 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8571 | return(standalone); |
| 8572 | } |
| 8573 | NEXT; |
| 8574 | SKIP_BLANKS; |
| 8575 | if (RAW == '\''){ |
| 8576 | NEXT; |
| 8577 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 8578 | standalone = 0; |
| 8579 | SKIP(2); |
| 8580 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 8581 | (NXT(2) == 's')) { |
| 8582 | standalone = 1; |
| 8583 | SKIP(3); |
| 8584 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8585 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8586 | } |
| 8587 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8588 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8589 | } else |
| 8590 | NEXT; |
| 8591 | } else if (RAW == '"'){ |
| 8592 | NEXT; |
| 8593 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 8594 | standalone = 0; |
| 8595 | SKIP(2); |
| 8596 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 8597 | (NXT(2) == 's')) { |
| 8598 | standalone = 1; |
| 8599 | SKIP(3); |
| 8600 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8601 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8602 | } |
| 8603 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8604 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8605 | } else |
| 8606 | NEXT; |
| 8607 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8608 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8609 | } |
| 8610 | } |
| 8611 | return(standalone); |
| 8612 | } |
| 8613 | |
| 8614 | /** |
| 8615 | * xmlParseXMLDecl: |
| 8616 | * @ctxt: an XML parser context |
| 8617 | * |
| 8618 | * parse an XML declaration header |
| 8619 | * |
| 8620 | * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' |
| 8621 | */ |
| 8622 | |
| 8623 | void |
| 8624 | xmlParseXMLDecl(xmlParserCtxtPtr ctxt) { |
| 8625 | xmlChar *version; |
| 8626 | |
| 8627 | /* |
| 8628 | * We know that '<?xml' is here. |
| 8629 | */ |
| 8630 | SKIP(5); |
| 8631 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8632 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8633 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 8634 | "Blank needed after '<?xml'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8635 | } |
| 8636 | SKIP_BLANKS; |
| 8637 | |
| 8638 | /* |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 8639 | * We must have the VersionInfo here. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8640 | */ |
| 8641 | version = xmlParseVersionInfo(ctxt); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 8642 | if (version == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8643 | xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 8644 | } else { |
| 8645 | if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) { |
| 8646 | /* |
| 8647 | * TODO: Blueberry should be detected here |
| 8648 | */ |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 8649 | xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION, |
| 8650 | "Unsupported version '%s'\n", |
| 8651 | version, NULL); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 8652 | } |
| 8653 | if (ctxt->version != NULL) |
Daniel Veillard | d3b0882 | 2001-12-05 12:03:33 +0000 | [diff] [blame] | 8654 | xmlFree((void *) ctxt->version); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 8655 | ctxt->version = version; |
Daniel Veillard | a050d23 | 2001-09-05 15:51:05 +0000 | [diff] [blame] | 8656 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8657 | |
| 8658 | /* |
| 8659 | * We may have the encoding declaration |
| 8660 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8661 | if (!IS_BLANK_CH(RAW)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8662 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 8663 | SKIP(2); |
| 8664 | return; |
| 8665 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8666 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8667 | } |
| 8668 | xmlParseEncodingDecl(ctxt); |
| 8669 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 8670 | /* |
| 8671 | * The XML REC instructs us to stop parsing right here |
| 8672 | */ |
| 8673 | return; |
| 8674 | } |
| 8675 | |
| 8676 | /* |
| 8677 | * We may have the standalone status. |
| 8678 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8679 | if ((ctxt->input->encoding != NULL) && (!IS_BLANK_CH(RAW))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8680 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 8681 | SKIP(2); |
| 8682 | return; |
| 8683 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8684 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8685 | } |
| 8686 | SKIP_BLANKS; |
| 8687 | ctxt->input->standalone = xmlParseSDDecl(ctxt); |
| 8688 | |
| 8689 | SKIP_BLANKS; |
| 8690 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 8691 | SKIP(2); |
| 8692 | } else if (RAW == '>') { |
| 8693 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8694 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8695 | NEXT; |
| 8696 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8697 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8698 | MOVETO_ENDTAG(CUR_PTR); |
| 8699 | NEXT; |
| 8700 | } |
| 8701 | } |
| 8702 | |
| 8703 | /** |
| 8704 | * xmlParseMisc: |
| 8705 | * @ctxt: an XML parser context |
| 8706 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8707 | * parse an XML Misc* optional field. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8708 | * |
| 8709 | * [27] Misc ::= Comment | PI | S |
| 8710 | */ |
| 8711 | |
| 8712 | void |
| 8713 | xmlParseMisc(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 8714 | while (((RAW == '<') && (NXT(1) == '?')) || |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 8715 | (CMP4(CUR_PTR, '<', '!', '-', '-')) || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8716 | IS_BLANK_CH(CUR)) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 8717 | if ((RAW == '<') && (NXT(1) == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8718 | xmlParsePI(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8719 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8720 | NEXT; |
| 8721 | } else |
| 8722 | xmlParseComment(ctxt); |
| 8723 | } |
| 8724 | } |
| 8725 | |
| 8726 | /** |
| 8727 | * xmlParseDocument: |
| 8728 | * @ctxt: an XML parser context |
| 8729 | * |
| 8730 | * parse an XML document (and build a tree if using the standard SAX |
| 8731 | * interface). |
| 8732 | * |
| 8733 | * [1] document ::= prolog element Misc* |
| 8734 | * |
| 8735 | * [22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)? |
| 8736 | * |
| 8737 | * Returns 0, -1 in case of error. the parser context is augmented |
| 8738 | * as a result of the parsing. |
| 8739 | */ |
| 8740 | |
| 8741 | int |
| 8742 | xmlParseDocument(xmlParserCtxtPtr ctxt) { |
| 8743 | xmlChar start[4]; |
| 8744 | xmlCharEncoding enc; |
| 8745 | |
| 8746 | xmlInitParser(); |
| 8747 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 8748 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
| 8749 | return(-1); |
| 8750 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8751 | GROW; |
| 8752 | |
| 8753 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8754 | * SAX: detecting the level. |
| 8755 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8756 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8757 | |
| 8758 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8759 | * SAX: beginning of the document processing. |
| 8760 | */ |
| 8761 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 8762 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 8763 | |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 8764 | if ((ctxt->encoding == (const xmlChar *)XML_CHAR_ENCODING_NONE) && |
| 8765 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 8766 | /* |
| 8767 | * Get the 4 first bytes and decode the charset |
| 8768 | * if enc != XML_CHAR_ENCODING_NONE |
| 8769 | * plug some encoding conversion routines. |
| 8770 | */ |
| 8771 | start[0] = RAW; |
| 8772 | start[1] = NXT(1); |
| 8773 | start[2] = NXT(2); |
| 8774 | start[3] = NXT(3); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 8775 | enc = xmlDetectCharEncoding(&start[0], 4); |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 8776 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 8777 | xmlSwitchEncoding(ctxt, enc); |
| 8778 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8779 | } |
| 8780 | |
| 8781 | |
| 8782 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8783 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8784 | } |
| 8785 | |
| 8786 | /* |
| 8787 | * Check for the XMLDecl in the Prolog. |
| 8788 | */ |
| 8789 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 8790 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8791 | |
| 8792 | /* |
| 8793 | * Note that we will switch encoding on the fly. |
| 8794 | */ |
| 8795 | xmlParseXMLDecl(ctxt); |
| 8796 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 8797 | /* |
| 8798 | * The XML REC instructs us to stop parsing right here |
| 8799 | */ |
| 8800 | return(-1); |
| 8801 | } |
| 8802 | ctxt->standalone = ctxt->input->standalone; |
| 8803 | SKIP_BLANKS; |
| 8804 | } else { |
| 8805 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 8806 | } |
| 8807 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 8808 | ctxt->sax->startDocument(ctxt->userData); |
| 8809 | |
| 8810 | /* |
| 8811 | * The Misc part of the Prolog |
| 8812 | */ |
| 8813 | GROW; |
| 8814 | xmlParseMisc(ctxt); |
| 8815 | |
| 8816 | /* |
| 8817 | * Then possibly doc type declaration(s) and more Misc |
| 8818 | * (doctypedecl Misc*)? |
| 8819 | */ |
| 8820 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 8821 | if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8822 | |
| 8823 | ctxt->inSubset = 1; |
| 8824 | xmlParseDocTypeDecl(ctxt); |
| 8825 | if (RAW == '[') { |
| 8826 | ctxt->instate = XML_PARSER_DTD; |
| 8827 | xmlParseInternalSubset(ctxt); |
| 8828 | } |
| 8829 | |
| 8830 | /* |
| 8831 | * Create and update the external subset. |
| 8832 | */ |
| 8833 | ctxt->inSubset = 2; |
| 8834 | if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) && |
| 8835 | (!ctxt->disableSAX)) |
| 8836 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 8837 | ctxt->extSubSystem, ctxt->extSubURI); |
| 8838 | ctxt->inSubset = 0; |
| 8839 | |
| 8840 | |
| 8841 | ctxt->instate = XML_PARSER_PROLOG; |
| 8842 | xmlParseMisc(ctxt); |
| 8843 | } |
| 8844 | |
| 8845 | /* |
| 8846 | * Time to start parsing the tree itself |
| 8847 | */ |
| 8848 | GROW; |
| 8849 | if (RAW != '<') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8850 | xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY, |
| 8851 | "Start tag expected, '<' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8852 | } else { |
| 8853 | ctxt->instate = XML_PARSER_CONTENT; |
| 8854 | xmlParseElement(ctxt); |
| 8855 | ctxt->instate = XML_PARSER_EPILOG; |
| 8856 | |
| 8857 | |
| 8858 | /* |
| 8859 | * The Misc part at the end |
| 8860 | */ |
| 8861 | xmlParseMisc(ctxt); |
| 8862 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 8863 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8864 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8865 | } |
| 8866 | ctxt->instate = XML_PARSER_EOF; |
| 8867 | } |
| 8868 | |
| 8869 | /* |
| 8870 | * SAX: end of the document processing. |
| 8871 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 8872 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8873 | ctxt->sax->endDocument(ctxt->userData); |
| 8874 | |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 8875 | /* |
| 8876 | * Remove locally kept entity definitions if the tree was not built |
| 8877 | */ |
| 8878 | if ((ctxt->myDoc != NULL) && |
| 8879 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 8880 | xmlFreeDoc(ctxt->myDoc); |
| 8881 | ctxt->myDoc = NULL; |
| 8882 | } |
| 8883 | |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 8884 | if (! ctxt->wellFormed) { |
| 8885 | ctxt->valid = 0; |
| 8886 | return(-1); |
| 8887 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8888 | return(0); |
| 8889 | } |
| 8890 | |
| 8891 | /** |
| 8892 | * xmlParseExtParsedEnt: |
| 8893 | * @ctxt: an XML parser context |
| 8894 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8895 | * parse a general parsed entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8896 | * An external general parsed entity is well-formed if it matches the |
| 8897 | * production labeled extParsedEnt. |
| 8898 | * |
| 8899 | * [78] extParsedEnt ::= TextDecl? content |
| 8900 | * |
| 8901 | * Returns 0, -1 in case of error. the parser context is augmented |
| 8902 | * as a result of the parsing. |
| 8903 | */ |
| 8904 | |
| 8905 | int |
| 8906 | xmlParseExtParsedEnt(xmlParserCtxtPtr ctxt) { |
| 8907 | xmlChar start[4]; |
| 8908 | xmlCharEncoding enc; |
| 8909 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 8910 | if ((ctxt == NULL) || (ctxt->input == NULL)) |
| 8911 | return(-1); |
| 8912 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8913 | xmlDefaultSAXHandlerInit(); |
| 8914 | |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 8915 | xmlDetectSAX2(ctxt); |
| 8916 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8917 | GROW; |
| 8918 | |
| 8919 | /* |
| 8920 | * SAX: beginning of the document processing. |
| 8921 | */ |
| 8922 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 8923 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 8924 | |
| 8925 | /* |
| 8926 | * Get the 4 first bytes and decode the charset |
| 8927 | * if enc != XML_CHAR_ENCODING_NONE |
| 8928 | * plug some encoding conversion routines. |
| 8929 | */ |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 8930 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 8931 | start[0] = RAW; |
| 8932 | start[1] = NXT(1); |
| 8933 | start[2] = NXT(2); |
| 8934 | start[3] = NXT(3); |
| 8935 | enc = xmlDetectCharEncoding(start, 4); |
| 8936 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 8937 | xmlSwitchEncoding(ctxt, enc); |
| 8938 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8939 | } |
| 8940 | |
| 8941 | |
| 8942 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8943 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8944 | } |
| 8945 | |
| 8946 | /* |
| 8947 | * Check for the XMLDecl in the Prolog. |
| 8948 | */ |
| 8949 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 8950 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8951 | |
| 8952 | /* |
| 8953 | * Note that we will switch encoding on the fly. |
| 8954 | */ |
| 8955 | xmlParseXMLDecl(ctxt); |
| 8956 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 8957 | /* |
| 8958 | * The XML REC instructs us to stop parsing right here |
| 8959 | */ |
| 8960 | return(-1); |
| 8961 | } |
| 8962 | SKIP_BLANKS; |
| 8963 | } else { |
| 8964 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 8965 | } |
| 8966 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 8967 | ctxt->sax->startDocument(ctxt->userData); |
| 8968 | |
| 8969 | /* |
| 8970 | * Doing validity checking on chunk doesn't make sense |
| 8971 | */ |
| 8972 | ctxt->instate = XML_PARSER_CONTENT; |
| 8973 | ctxt->validate = 0; |
| 8974 | ctxt->loadsubset = 0; |
| 8975 | ctxt->depth = 0; |
| 8976 | |
| 8977 | xmlParseContent(ctxt); |
| 8978 | |
| 8979 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8980 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8981 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8982 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8983 | } |
| 8984 | |
| 8985 | /* |
| 8986 | * SAX: end of the document processing. |
| 8987 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 8988 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8989 | ctxt->sax->endDocument(ctxt->userData); |
| 8990 | |
| 8991 | if (! ctxt->wellFormed) return(-1); |
| 8992 | return(0); |
| 8993 | } |
| 8994 | |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 8995 | #ifdef LIBXML_PUSH_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8996 | /************************************************************************ |
| 8997 | * * |
| 8998 | * Progressive parsing interfaces * |
| 8999 | * * |
| 9000 | ************************************************************************/ |
| 9001 | |
| 9002 | /** |
| 9003 | * xmlParseLookupSequence: |
| 9004 | * @ctxt: an XML parser context |
| 9005 | * @first: the first char to lookup |
| 9006 | * @next: the next char to lookup or zero |
| 9007 | * @third: the next char to lookup or zero |
| 9008 | * |
| 9009 | * Try to find if a sequence (first, next, third) or just (first next) or |
| 9010 | * (first) is available in the input stream. |
| 9011 | * This function has a side effect of (possibly) incrementing ctxt->checkIndex |
| 9012 | * to avoid rescanning sequences of bytes, it DOES change the state of the |
| 9013 | * parser, do not use liberally. |
| 9014 | * |
| 9015 | * Returns the index to the current parsing point if the full sequence |
| 9016 | * is available, -1 otherwise. |
| 9017 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 9018 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9019 | xmlParseLookupSequence(xmlParserCtxtPtr ctxt, xmlChar first, |
| 9020 | xmlChar next, xmlChar third) { |
| 9021 | int base, len; |
| 9022 | xmlParserInputPtr in; |
| 9023 | const xmlChar *buf; |
| 9024 | |
| 9025 | in = ctxt->input; |
| 9026 | if (in == NULL) return(-1); |
| 9027 | base = in->cur - in->base; |
| 9028 | if (base < 0) return(-1); |
| 9029 | if (ctxt->checkIndex > base) |
| 9030 | base = ctxt->checkIndex; |
| 9031 | if (in->buf == NULL) { |
| 9032 | buf = in->base; |
| 9033 | len = in->length; |
| 9034 | } else { |
| 9035 | buf = in->buf->buffer->content; |
| 9036 | len = in->buf->buffer->use; |
| 9037 | } |
| 9038 | /* take into account the sequence length */ |
| 9039 | if (third) len -= 2; |
| 9040 | else if (next) len --; |
| 9041 | for (;base < len;base++) { |
| 9042 | if (buf[base] == first) { |
| 9043 | if (third != 0) { |
| 9044 | if ((buf[base + 1] != next) || |
| 9045 | (buf[base + 2] != third)) continue; |
| 9046 | } else if (next != 0) { |
| 9047 | if (buf[base + 1] != next) continue; |
| 9048 | } |
| 9049 | ctxt->checkIndex = 0; |
| 9050 | #ifdef DEBUG_PUSH |
| 9051 | if (next == 0) |
| 9052 | xmlGenericError(xmlGenericErrorContext, |
| 9053 | "PP: lookup '%c' found at %d\n", |
| 9054 | first, base); |
| 9055 | else if (third == 0) |
| 9056 | xmlGenericError(xmlGenericErrorContext, |
| 9057 | "PP: lookup '%c%c' found at %d\n", |
| 9058 | first, next, base); |
| 9059 | else |
| 9060 | xmlGenericError(xmlGenericErrorContext, |
| 9061 | "PP: lookup '%c%c%c' found at %d\n", |
| 9062 | first, next, third, base); |
| 9063 | #endif |
| 9064 | return(base - (in->cur - in->base)); |
| 9065 | } |
| 9066 | } |
| 9067 | ctxt->checkIndex = base; |
| 9068 | #ifdef DEBUG_PUSH |
| 9069 | if (next == 0) |
| 9070 | xmlGenericError(xmlGenericErrorContext, |
| 9071 | "PP: lookup '%c' failed\n", first); |
| 9072 | else if (third == 0) |
| 9073 | xmlGenericError(xmlGenericErrorContext, |
| 9074 | "PP: lookup '%c%c' failed\n", first, next); |
| 9075 | else |
| 9076 | xmlGenericError(xmlGenericErrorContext, |
| 9077 | "PP: lookup '%c%c%c' failed\n", first, next, third); |
| 9078 | #endif |
| 9079 | return(-1); |
| 9080 | } |
| 9081 | |
| 9082 | /** |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9083 | * xmlParseGetLasts: |
| 9084 | * @ctxt: an XML parser context |
| 9085 | * @lastlt: pointer to store the last '<' from the input |
| 9086 | * @lastgt: pointer to store the last '>' from the input |
| 9087 | * |
| 9088 | * Lookup the last < and > in the current chunk |
| 9089 | */ |
| 9090 | static void |
| 9091 | xmlParseGetLasts(xmlParserCtxtPtr ctxt, const xmlChar **lastlt, |
| 9092 | const xmlChar **lastgt) { |
| 9093 | const xmlChar *tmp; |
| 9094 | |
| 9095 | if ((ctxt == NULL) || (lastlt == NULL) || (lastgt == NULL)) { |
| 9096 | xmlGenericError(xmlGenericErrorContext, |
| 9097 | "Internal error: xmlParseGetLasts\n"); |
| 9098 | return; |
| 9099 | } |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 9100 | if ((ctxt->progressive != 0) && (ctxt->inputNr == 1)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9101 | tmp = ctxt->input->end; |
| 9102 | tmp--; |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 9103 | while ((tmp >= ctxt->input->base) && (*tmp != '<')) tmp--; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9104 | if (tmp < ctxt->input->base) { |
| 9105 | *lastlt = NULL; |
| 9106 | *lastgt = NULL; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9107 | } else { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 9108 | *lastlt = tmp; |
| 9109 | tmp++; |
| 9110 | while ((tmp < ctxt->input->end) && (*tmp != '>')) { |
| 9111 | if (*tmp == '\'') { |
| 9112 | tmp++; |
| 9113 | while ((tmp < ctxt->input->end) && (*tmp != '\'')) tmp++; |
| 9114 | if (tmp < ctxt->input->end) tmp++; |
| 9115 | } else if (*tmp == '"') { |
| 9116 | tmp++; |
| 9117 | while ((tmp < ctxt->input->end) && (*tmp != '"')) tmp++; |
| 9118 | if (tmp < ctxt->input->end) tmp++; |
| 9119 | } else |
| 9120 | tmp++; |
| 9121 | } |
| 9122 | if (tmp < ctxt->input->end) |
| 9123 | *lastgt = tmp; |
| 9124 | else { |
| 9125 | tmp = *lastlt; |
| 9126 | tmp--; |
| 9127 | while ((tmp >= ctxt->input->base) && (*tmp != '>')) tmp--; |
| 9128 | if (tmp >= ctxt->input->base) |
| 9129 | *lastgt = tmp; |
| 9130 | else |
| 9131 | *lastgt = NULL; |
| 9132 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9133 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9134 | } else { |
| 9135 | *lastlt = NULL; |
| 9136 | *lastgt = NULL; |
| 9137 | } |
| 9138 | } |
| 9139 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9140 | * xmlParseTryOrFinish: |
| 9141 | * @ctxt: an XML parser context |
| 9142 | * @terminate: last chunk indicator |
| 9143 | * |
| 9144 | * Try to progress on parsing |
| 9145 | * |
| 9146 | * Returns zero if no parsing was possible |
| 9147 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 9148 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9149 | xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) { |
| 9150 | int ret = 0; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9151 | int avail, tlen; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9152 | xmlChar cur, next; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9153 | const xmlChar *lastlt, *lastgt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9154 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 9155 | if (ctxt->input == NULL) |
| 9156 | return(0); |
| 9157 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9158 | #ifdef DEBUG_PUSH |
| 9159 | switch (ctxt->instate) { |
| 9160 | case XML_PARSER_EOF: |
| 9161 | xmlGenericError(xmlGenericErrorContext, |
| 9162 | "PP: try EOF\n"); break; |
| 9163 | case XML_PARSER_START: |
| 9164 | xmlGenericError(xmlGenericErrorContext, |
| 9165 | "PP: try START\n"); break; |
| 9166 | case XML_PARSER_MISC: |
| 9167 | xmlGenericError(xmlGenericErrorContext, |
| 9168 | "PP: try MISC\n");break; |
| 9169 | case XML_PARSER_COMMENT: |
| 9170 | xmlGenericError(xmlGenericErrorContext, |
| 9171 | "PP: try COMMENT\n");break; |
| 9172 | case XML_PARSER_PROLOG: |
| 9173 | xmlGenericError(xmlGenericErrorContext, |
| 9174 | "PP: try PROLOG\n");break; |
| 9175 | case XML_PARSER_START_TAG: |
| 9176 | xmlGenericError(xmlGenericErrorContext, |
| 9177 | "PP: try START_TAG\n");break; |
| 9178 | case XML_PARSER_CONTENT: |
| 9179 | xmlGenericError(xmlGenericErrorContext, |
| 9180 | "PP: try CONTENT\n");break; |
| 9181 | case XML_PARSER_CDATA_SECTION: |
| 9182 | xmlGenericError(xmlGenericErrorContext, |
| 9183 | "PP: try CDATA_SECTION\n");break; |
| 9184 | case XML_PARSER_END_TAG: |
| 9185 | xmlGenericError(xmlGenericErrorContext, |
| 9186 | "PP: try END_TAG\n");break; |
| 9187 | case XML_PARSER_ENTITY_DECL: |
| 9188 | xmlGenericError(xmlGenericErrorContext, |
| 9189 | "PP: try ENTITY_DECL\n");break; |
| 9190 | case XML_PARSER_ENTITY_VALUE: |
| 9191 | xmlGenericError(xmlGenericErrorContext, |
| 9192 | "PP: try ENTITY_VALUE\n");break; |
| 9193 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 9194 | xmlGenericError(xmlGenericErrorContext, |
| 9195 | "PP: try ATTRIBUTE_VALUE\n");break; |
| 9196 | case XML_PARSER_DTD: |
| 9197 | xmlGenericError(xmlGenericErrorContext, |
| 9198 | "PP: try DTD\n");break; |
| 9199 | case XML_PARSER_EPILOG: |
| 9200 | xmlGenericError(xmlGenericErrorContext, |
| 9201 | "PP: try EPILOG\n");break; |
| 9202 | case XML_PARSER_PI: |
| 9203 | xmlGenericError(xmlGenericErrorContext, |
| 9204 | "PP: try PI\n");break; |
| 9205 | case XML_PARSER_IGNORE: |
| 9206 | xmlGenericError(xmlGenericErrorContext, |
| 9207 | "PP: try IGNORE\n");break; |
| 9208 | } |
| 9209 | #endif |
| 9210 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 9211 | if ((ctxt->input != NULL) && |
| 9212 | (ctxt->input->cur - ctxt->input->base > 4096)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9213 | xmlSHRINK(ctxt); |
| 9214 | ctxt->checkIndex = 0; |
| 9215 | } |
| 9216 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Aleksey Sanin | e48a318 | 2002-05-09 18:20:01 +0000 | [diff] [blame] | 9217 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9218 | while (1) { |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 9219 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 9220 | return(0); |
| 9221 | |
| 9222 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9223 | /* |
| 9224 | * Pop-up of finished entities. |
| 9225 | */ |
| 9226 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 9227 | xmlPopInput(ctxt); |
| 9228 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 9229 | if (ctxt->input == NULL) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9230 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9231 | avail = ctxt->input->length - |
| 9232 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 9233 | else { |
| 9234 | /* |
| 9235 | * If we are operating on converted input, try to flush |
| 9236 | * remainng chars to avoid them stalling in the non-converted |
| 9237 | * buffer. |
| 9238 | */ |
| 9239 | if ((ctxt->input->buf->raw != NULL) && |
| 9240 | (ctxt->input->buf->raw->use > 0)) { |
| 9241 | int base = ctxt->input->base - |
| 9242 | ctxt->input->buf->buffer->content; |
| 9243 | int current = ctxt->input->cur - ctxt->input->base; |
| 9244 | |
| 9245 | xmlParserInputBufferPush(ctxt->input->buf, 0, ""); |
| 9246 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 9247 | ctxt->input->cur = ctxt->input->base + current; |
| 9248 | ctxt->input->end = |
| 9249 | &ctxt->input->buf->buffer->content[ |
| 9250 | ctxt->input->buf->buffer->use]; |
| 9251 | } |
| 9252 | avail = ctxt->input->buf->buffer->use - |
| 9253 | (ctxt->input->cur - ctxt->input->base); |
| 9254 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9255 | if (avail < 1) |
| 9256 | goto done; |
| 9257 | switch (ctxt->instate) { |
| 9258 | case XML_PARSER_EOF: |
| 9259 | /* |
| 9260 | * Document parsing is done ! |
| 9261 | */ |
| 9262 | goto done; |
| 9263 | case XML_PARSER_START: |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 9264 | if (ctxt->charset == XML_CHAR_ENCODING_NONE) { |
| 9265 | xmlChar start[4]; |
| 9266 | xmlCharEncoding enc; |
| 9267 | |
| 9268 | /* |
| 9269 | * Very first chars read from the document flow. |
| 9270 | */ |
| 9271 | if (avail < 4) |
| 9272 | goto done; |
| 9273 | |
| 9274 | /* |
| 9275 | * Get the 4 first bytes and decode the charset |
| 9276 | * if enc != XML_CHAR_ENCODING_NONE |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 9277 | * plug some encoding conversion routines, |
| 9278 | * else xmlSwitchEncoding will set to (default) |
| 9279 | * UTF8. |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 9280 | */ |
| 9281 | start[0] = RAW; |
| 9282 | start[1] = NXT(1); |
| 9283 | start[2] = NXT(2); |
| 9284 | start[3] = NXT(3); |
| 9285 | enc = xmlDetectCharEncoding(start, 4); |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 9286 | xmlSwitchEncoding(ctxt, enc); |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 9287 | break; |
| 9288 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9289 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 9290 | if (avail < 2) |
| 9291 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9292 | cur = ctxt->input->cur[0]; |
| 9293 | next = ctxt->input->cur[1]; |
| 9294 | if (cur == 0) { |
| 9295 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 9296 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 9297 | &xmlDefaultSAXLocator); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9298 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9299 | ctxt->instate = XML_PARSER_EOF; |
| 9300 | #ifdef DEBUG_PUSH |
| 9301 | xmlGenericError(xmlGenericErrorContext, |
| 9302 | "PP: entering EOF\n"); |
| 9303 | #endif |
| 9304 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 9305 | ctxt->sax->endDocument(ctxt->userData); |
| 9306 | goto done; |
| 9307 | } |
| 9308 | if ((cur == '<') && (next == '?')) { |
| 9309 | /* PI or XML decl */ |
| 9310 | if (avail < 5) return(ret); |
| 9311 | if ((!terminate) && |
| 9312 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 9313 | return(ret); |
| 9314 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 9315 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 9316 | &xmlDefaultSAXLocator); |
| 9317 | if ((ctxt->input->cur[2] == 'x') && |
| 9318 | (ctxt->input->cur[3] == 'm') && |
| 9319 | (ctxt->input->cur[4] == 'l') && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9320 | (IS_BLANK_CH(ctxt->input->cur[5]))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9321 | ret += 5; |
| 9322 | #ifdef DEBUG_PUSH |
| 9323 | xmlGenericError(xmlGenericErrorContext, |
| 9324 | "PP: Parsing XML Decl\n"); |
| 9325 | #endif |
| 9326 | xmlParseXMLDecl(ctxt); |
| 9327 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 9328 | /* |
| 9329 | * The XML REC instructs us to stop parsing right |
| 9330 | * here |
| 9331 | */ |
| 9332 | ctxt->instate = XML_PARSER_EOF; |
| 9333 | return(0); |
| 9334 | } |
| 9335 | ctxt->standalone = ctxt->input->standalone; |
| 9336 | if ((ctxt->encoding == NULL) && |
| 9337 | (ctxt->input->encoding != NULL)) |
| 9338 | ctxt->encoding = xmlStrdup(ctxt->input->encoding); |
| 9339 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 9340 | (!ctxt->disableSAX)) |
| 9341 | ctxt->sax->startDocument(ctxt->userData); |
| 9342 | ctxt->instate = XML_PARSER_MISC; |
| 9343 | #ifdef DEBUG_PUSH |
| 9344 | xmlGenericError(xmlGenericErrorContext, |
| 9345 | "PP: entering MISC\n"); |
| 9346 | #endif |
| 9347 | } else { |
| 9348 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 9349 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 9350 | (!ctxt->disableSAX)) |
| 9351 | ctxt->sax->startDocument(ctxt->userData); |
| 9352 | ctxt->instate = XML_PARSER_MISC; |
| 9353 | #ifdef DEBUG_PUSH |
| 9354 | xmlGenericError(xmlGenericErrorContext, |
| 9355 | "PP: entering MISC\n"); |
| 9356 | #endif |
| 9357 | } |
| 9358 | } else { |
| 9359 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 9360 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 9361 | &xmlDefaultSAXLocator); |
| 9362 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 9363 | if (ctxt->version == NULL) { |
| 9364 | xmlErrMemory(ctxt, NULL); |
| 9365 | break; |
| 9366 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9367 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 9368 | (!ctxt->disableSAX)) |
| 9369 | ctxt->sax->startDocument(ctxt->userData); |
| 9370 | ctxt->instate = XML_PARSER_MISC; |
| 9371 | #ifdef DEBUG_PUSH |
| 9372 | xmlGenericError(xmlGenericErrorContext, |
| 9373 | "PP: entering MISC\n"); |
| 9374 | #endif |
| 9375 | } |
| 9376 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9377 | case XML_PARSER_START_TAG: { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9378 | const xmlChar *name; |
| 9379 | const xmlChar *prefix; |
| 9380 | const xmlChar *URI; |
| 9381 | int nsNr = ctxt->nsNr; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9382 | |
| 9383 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 9384 | goto done; |
| 9385 | cur = ctxt->input->cur[0]; |
| 9386 | if (cur != '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9387 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9388 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9389 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 9390 | ctxt->sax->endDocument(ctxt->userData); |
| 9391 | goto done; |
| 9392 | } |
| 9393 | if (!terminate) { |
| 9394 | if (ctxt->progressive) { |
Daniel Veillard | b374400 | 2004-02-18 14:28:22 +0000 | [diff] [blame] | 9395 | /* > can be found unescaped in attribute values */ |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 9396 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9397 | goto done; |
| 9398 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 9399 | goto done; |
| 9400 | } |
| 9401 | } |
| 9402 | if (ctxt->spaceNr == 0) |
| 9403 | spacePush(ctxt, -1); |
| 9404 | else |
| 9405 | spacePush(ctxt, *ctxt->space); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9406 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9407 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9408 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9409 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9410 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9411 | else |
| 9412 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9413 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9414 | if (name == NULL) { |
| 9415 | spacePop(ctxt); |
| 9416 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9417 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 9418 | ctxt->sax->endDocument(ctxt->userData); |
| 9419 | goto done; |
| 9420 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 9421 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9422 | /* |
| 9423 | * [ VC: Root Element Type ] |
| 9424 | * The Name in the document type declaration must match |
| 9425 | * the element type of the root element. |
| 9426 | */ |
| 9427 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 9428 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 9429 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 9430 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9431 | |
| 9432 | /* |
| 9433 | * Check for an Empty Element. |
| 9434 | */ |
| 9435 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 9436 | SKIP(2); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9437 | |
| 9438 | if (ctxt->sax2) { |
| 9439 | if ((ctxt->sax != NULL) && |
| 9440 | (ctxt->sax->endElementNs != NULL) && |
| 9441 | (!ctxt->disableSAX)) |
| 9442 | ctxt->sax->endElementNs(ctxt->userData, name, |
| 9443 | prefix, URI); |
Daniel Veillard | 48df961 | 2005-01-04 21:50:05 +0000 | [diff] [blame] | 9444 | if (ctxt->nsNr - nsNr > 0) |
| 9445 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9446 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9447 | } else { |
| 9448 | if ((ctxt->sax != NULL) && |
| 9449 | (ctxt->sax->endElement != NULL) && |
| 9450 | (!ctxt->disableSAX)) |
| 9451 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9452 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9453 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9454 | spacePop(ctxt); |
| 9455 | if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9456 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9457 | } else { |
| 9458 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9459 | } |
| 9460 | break; |
| 9461 | } |
| 9462 | if (RAW == '>') { |
| 9463 | NEXT; |
| 9464 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 9465 | xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED, |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9466 | "Couldn't find end of Start Tag %s\n", |
| 9467 | name); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9468 | nodePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9469 | spacePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9470 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9471 | if (ctxt->sax2) |
| 9472 | nameNsPush(ctxt, name, prefix, URI, ctxt->nsNr - nsNr); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9473 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9474 | else |
| 9475 | namePush(ctxt, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9476 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9477 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9478 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9479 | break; |
| 9480 | } |
| 9481 | case XML_PARSER_CONTENT: { |
| 9482 | const xmlChar *test; |
| 9483 | unsigned int cons; |
| 9484 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 9485 | goto done; |
| 9486 | cur = ctxt->input->cur[0]; |
| 9487 | next = ctxt->input->cur[1]; |
| 9488 | |
| 9489 | test = CUR_PTR; |
| 9490 | cons = ctxt->input->consumed; |
| 9491 | if ((cur == '<') && (next == '/')) { |
| 9492 | ctxt->instate = XML_PARSER_END_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9493 | break; |
| 9494 | } else if ((cur == '<') && (next == '?')) { |
| 9495 | if ((!terminate) && |
| 9496 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 9497 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9498 | xmlParsePI(ctxt); |
| 9499 | } else if ((cur == '<') && (next != '!')) { |
| 9500 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9501 | break; |
| 9502 | } else if ((cur == '<') && (next == '!') && |
| 9503 | (ctxt->input->cur[2] == '-') && |
| 9504 | (ctxt->input->cur[3] == '-')) { |
| 9505 | if ((!terminate) && |
| 9506 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 9507 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9508 | xmlParseComment(ctxt); |
| 9509 | ctxt->instate = XML_PARSER_CONTENT; |
| 9510 | } else if ((cur == '<') && (ctxt->input->cur[1] == '!') && |
| 9511 | (ctxt->input->cur[2] == '[') && |
| 9512 | (ctxt->input->cur[3] == 'C') && |
| 9513 | (ctxt->input->cur[4] == 'D') && |
| 9514 | (ctxt->input->cur[5] == 'A') && |
| 9515 | (ctxt->input->cur[6] == 'T') && |
| 9516 | (ctxt->input->cur[7] == 'A') && |
| 9517 | (ctxt->input->cur[8] == '[')) { |
| 9518 | SKIP(9); |
| 9519 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9520 | break; |
| 9521 | } else if ((cur == '<') && (next == '!') && |
| 9522 | (avail < 9)) { |
| 9523 | goto done; |
| 9524 | } else if (cur == '&') { |
| 9525 | if ((!terminate) && |
| 9526 | (xmlParseLookupSequence(ctxt, ';', 0, 0) < 0)) |
| 9527 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9528 | xmlParseReference(ctxt); |
| 9529 | } else { |
| 9530 | /* TODO Avoid the extra copy, handle directly !!! */ |
| 9531 | /* |
| 9532 | * Goal of the following test is: |
| 9533 | * - minimize calls to the SAX 'character' callback |
| 9534 | * when they are mergeable |
| 9535 | * - handle an problem for isBlank when we only parse |
| 9536 | * a sequence of blank chars and the next one is |
| 9537 | * not available to check against '<' presence. |
| 9538 | * - tries to homogenize the differences in SAX |
| 9539 | * callbacks between the push and pull versions |
| 9540 | * of the parser. |
| 9541 | */ |
| 9542 | if ((ctxt->inputNr == 1) && |
| 9543 | (avail < XML_PARSER_BIG_BUFFER_SIZE)) { |
| 9544 | if (!terminate) { |
| 9545 | if (ctxt->progressive) { |
| 9546 | if ((lastlt == NULL) || |
| 9547 | (ctxt->input->cur > lastlt)) |
| 9548 | goto done; |
| 9549 | } else if (xmlParseLookupSequence(ctxt, |
| 9550 | '<', 0, 0) < 0) { |
| 9551 | goto done; |
| 9552 | } |
| 9553 | } |
| 9554 | } |
| 9555 | ctxt->checkIndex = 0; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9556 | xmlParseCharData(ctxt, 0); |
| 9557 | } |
| 9558 | /* |
| 9559 | * Pop-up of finished entities. |
| 9560 | */ |
| 9561 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 9562 | xmlPopInput(ctxt); |
| 9563 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9564 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 9565 | "detected an error in element content\n"); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9566 | ctxt->instate = XML_PARSER_EOF; |
| 9567 | break; |
| 9568 | } |
| 9569 | break; |
| 9570 | } |
| 9571 | case XML_PARSER_END_TAG: |
| 9572 | if (avail < 2) |
| 9573 | goto done; |
| 9574 | if (!terminate) { |
| 9575 | if (ctxt->progressive) { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 9576 | /* > can be found unescaped in attribute values */ |
| 9577 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9578 | goto done; |
| 9579 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 9580 | goto done; |
| 9581 | } |
| 9582 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9583 | if (ctxt->sax2) { |
| 9584 | xmlParseEndTag2(ctxt, |
| 9585 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 3], |
| 9586 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 2], 0, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9587 | (int) (long) ctxt->pushTab[ctxt->nameNr * 3 - 1], 0); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9588 | nameNsPop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9589 | } |
| 9590 | #ifdef LIBXML_SAX1_ENABLED |
| 9591 | else |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9592 | xmlParseEndTag1(ctxt, 0); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9593 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9594 | if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9595 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9596 | } else { |
| 9597 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9598 | } |
| 9599 | break; |
| 9600 | case XML_PARSER_CDATA_SECTION: { |
| 9601 | /* |
| 9602 | * The Push mode need to have the SAX callback for |
| 9603 | * cdataBlock merge back contiguous callbacks. |
| 9604 | */ |
| 9605 | int base; |
| 9606 | |
| 9607 | base = xmlParseLookupSequence(ctxt, ']', ']', '>'); |
| 9608 | if (base < 0) { |
| 9609 | if (avail >= XML_PARSER_BIG_BUFFER_SIZE + 2) { |
| 9610 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 9611 | if (ctxt->sax->cdataBlock != NULL) |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 9612 | ctxt->sax->cdataBlock(ctxt->userData, |
| 9613 | ctxt->input->cur, |
| 9614 | XML_PARSER_BIG_BUFFER_SIZE); |
| 9615 | else if (ctxt->sax->characters != NULL) |
| 9616 | ctxt->sax->characters(ctxt->userData, |
| 9617 | ctxt->input->cur, |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9618 | XML_PARSER_BIG_BUFFER_SIZE); |
| 9619 | } |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 9620 | SKIPL(XML_PARSER_BIG_BUFFER_SIZE); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9621 | ctxt->checkIndex = 0; |
| 9622 | } |
| 9623 | goto done; |
| 9624 | } else { |
| 9625 | if ((ctxt->sax != NULL) && (base > 0) && |
| 9626 | (!ctxt->disableSAX)) { |
| 9627 | if (ctxt->sax->cdataBlock != NULL) |
| 9628 | ctxt->sax->cdataBlock(ctxt->userData, |
| 9629 | ctxt->input->cur, base); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 9630 | else if (ctxt->sax->characters != NULL) |
| 9631 | ctxt->sax->characters(ctxt->userData, |
| 9632 | ctxt->input->cur, base); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9633 | } |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 9634 | SKIPL(base + 3); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9635 | ctxt->checkIndex = 0; |
| 9636 | ctxt->instate = XML_PARSER_CONTENT; |
| 9637 | #ifdef DEBUG_PUSH |
| 9638 | xmlGenericError(xmlGenericErrorContext, |
| 9639 | "PP: entering CONTENT\n"); |
| 9640 | #endif |
| 9641 | } |
| 9642 | break; |
| 9643 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9644 | case XML_PARSER_MISC: |
| 9645 | SKIP_BLANKS; |
| 9646 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9647 | avail = ctxt->input->length - |
| 9648 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9649 | else |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9650 | avail = ctxt->input->buf->buffer->use - |
| 9651 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9652 | if (avail < 2) |
| 9653 | goto done; |
| 9654 | cur = ctxt->input->cur[0]; |
| 9655 | next = ctxt->input->cur[1]; |
| 9656 | if ((cur == '<') && (next == '?')) { |
| 9657 | if ((!terminate) && |
| 9658 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 9659 | goto done; |
| 9660 | #ifdef DEBUG_PUSH |
| 9661 | xmlGenericError(xmlGenericErrorContext, |
| 9662 | "PP: Parsing PI\n"); |
| 9663 | #endif |
| 9664 | xmlParsePI(ctxt); |
| 9665 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9666 | (ctxt->input->cur[2] == '-') && |
| 9667 | (ctxt->input->cur[3] == '-')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9668 | if ((!terminate) && |
| 9669 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 9670 | goto done; |
| 9671 | #ifdef DEBUG_PUSH |
| 9672 | xmlGenericError(xmlGenericErrorContext, |
| 9673 | "PP: Parsing Comment\n"); |
| 9674 | #endif |
| 9675 | xmlParseComment(ctxt); |
| 9676 | ctxt->instate = XML_PARSER_MISC; |
| 9677 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9678 | (ctxt->input->cur[2] == 'D') && |
| 9679 | (ctxt->input->cur[3] == 'O') && |
| 9680 | (ctxt->input->cur[4] == 'C') && |
| 9681 | (ctxt->input->cur[5] == 'T') && |
| 9682 | (ctxt->input->cur[6] == 'Y') && |
| 9683 | (ctxt->input->cur[7] == 'P') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9684 | (ctxt->input->cur[8] == 'E')) { |
| 9685 | if ((!terminate) && |
| 9686 | (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0)) |
| 9687 | goto done; |
| 9688 | #ifdef DEBUG_PUSH |
| 9689 | xmlGenericError(xmlGenericErrorContext, |
| 9690 | "PP: Parsing internal subset\n"); |
| 9691 | #endif |
| 9692 | ctxt->inSubset = 1; |
| 9693 | xmlParseDocTypeDecl(ctxt); |
| 9694 | if (RAW == '[') { |
| 9695 | ctxt->instate = XML_PARSER_DTD; |
| 9696 | #ifdef DEBUG_PUSH |
| 9697 | xmlGenericError(xmlGenericErrorContext, |
| 9698 | "PP: entering DTD\n"); |
| 9699 | #endif |
| 9700 | } else { |
| 9701 | /* |
| 9702 | * Create and update the external subset. |
| 9703 | */ |
| 9704 | ctxt->inSubset = 2; |
| 9705 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 9706 | (ctxt->sax->externalSubset != NULL)) |
| 9707 | ctxt->sax->externalSubset(ctxt->userData, |
| 9708 | ctxt->intSubName, ctxt->extSubSystem, |
| 9709 | ctxt->extSubURI); |
| 9710 | ctxt->inSubset = 0; |
| 9711 | ctxt->instate = XML_PARSER_PROLOG; |
| 9712 | #ifdef DEBUG_PUSH |
| 9713 | xmlGenericError(xmlGenericErrorContext, |
| 9714 | "PP: entering PROLOG\n"); |
| 9715 | #endif |
| 9716 | } |
| 9717 | } else if ((cur == '<') && (next == '!') && |
| 9718 | (avail < 9)) { |
| 9719 | goto done; |
| 9720 | } else { |
| 9721 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9722 | ctxt->progressive = 1; |
| 9723 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9724 | #ifdef DEBUG_PUSH |
| 9725 | xmlGenericError(xmlGenericErrorContext, |
| 9726 | "PP: entering START_TAG\n"); |
| 9727 | #endif |
| 9728 | } |
| 9729 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9730 | case XML_PARSER_PROLOG: |
| 9731 | SKIP_BLANKS; |
| 9732 | if (ctxt->input->buf == NULL) |
| 9733 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 9734 | else |
| 9735 | avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base); |
| 9736 | if (avail < 2) |
| 9737 | goto done; |
| 9738 | cur = ctxt->input->cur[0]; |
| 9739 | next = ctxt->input->cur[1]; |
| 9740 | if ((cur == '<') && (next == '?')) { |
| 9741 | if ((!terminate) && |
| 9742 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 9743 | goto done; |
| 9744 | #ifdef DEBUG_PUSH |
| 9745 | xmlGenericError(xmlGenericErrorContext, |
| 9746 | "PP: Parsing PI\n"); |
| 9747 | #endif |
| 9748 | xmlParsePI(ctxt); |
| 9749 | } else if ((cur == '<') && (next == '!') && |
| 9750 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 9751 | if ((!terminate) && |
| 9752 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 9753 | goto done; |
| 9754 | #ifdef DEBUG_PUSH |
| 9755 | xmlGenericError(xmlGenericErrorContext, |
| 9756 | "PP: Parsing Comment\n"); |
| 9757 | #endif |
| 9758 | xmlParseComment(ctxt); |
| 9759 | ctxt->instate = XML_PARSER_PROLOG; |
| 9760 | } else if ((cur == '<') && (next == '!') && |
| 9761 | (avail < 4)) { |
| 9762 | goto done; |
| 9763 | } else { |
| 9764 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 9765 | if (ctxt->progressive == 0) |
| 9766 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9767 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9768 | #ifdef DEBUG_PUSH |
| 9769 | xmlGenericError(xmlGenericErrorContext, |
| 9770 | "PP: entering START_TAG\n"); |
| 9771 | #endif |
| 9772 | } |
| 9773 | break; |
| 9774 | case XML_PARSER_EPILOG: |
| 9775 | SKIP_BLANKS; |
| 9776 | if (ctxt->input->buf == NULL) |
| 9777 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 9778 | else |
| 9779 | avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base); |
| 9780 | if (avail < 2) |
| 9781 | goto done; |
| 9782 | cur = ctxt->input->cur[0]; |
| 9783 | next = ctxt->input->cur[1]; |
| 9784 | if ((cur == '<') && (next == '?')) { |
| 9785 | if ((!terminate) && |
| 9786 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 9787 | goto done; |
| 9788 | #ifdef DEBUG_PUSH |
| 9789 | xmlGenericError(xmlGenericErrorContext, |
| 9790 | "PP: Parsing PI\n"); |
| 9791 | #endif |
| 9792 | xmlParsePI(ctxt); |
| 9793 | ctxt->instate = XML_PARSER_EPILOG; |
| 9794 | } else if ((cur == '<') && (next == '!') && |
| 9795 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 9796 | if ((!terminate) && |
| 9797 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 9798 | goto done; |
| 9799 | #ifdef DEBUG_PUSH |
| 9800 | xmlGenericError(xmlGenericErrorContext, |
| 9801 | "PP: Parsing Comment\n"); |
| 9802 | #endif |
| 9803 | xmlParseComment(ctxt); |
| 9804 | ctxt->instate = XML_PARSER_EPILOG; |
| 9805 | } else if ((cur == '<') && (next == '!') && |
| 9806 | (avail < 4)) { |
| 9807 | goto done; |
| 9808 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9809 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9810 | ctxt->instate = XML_PARSER_EOF; |
| 9811 | #ifdef DEBUG_PUSH |
| 9812 | xmlGenericError(xmlGenericErrorContext, |
| 9813 | "PP: entering EOF\n"); |
| 9814 | #endif |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 9815 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9816 | ctxt->sax->endDocument(ctxt->userData); |
| 9817 | goto done; |
| 9818 | } |
| 9819 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9820 | case XML_PARSER_DTD: { |
| 9821 | /* |
| 9822 | * Sorry but progressive parsing of the internal subset |
| 9823 | * is not expected to be supported. We first check that |
| 9824 | * the full content of the internal subset is available and |
| 9825 | * the parsing is launched only at that point. |
| 9826 | * Internal subset ends up with "']' S? '>'" in an unescaped |
| 9827 | * section and not in a ']]>' sequence which are conditional |
| 9828 | * sections (whoever argued to keep that crap in XML deserve |
| 9829 | * a place in hell !). |
| 9830 | */ |
| 9831 | int base, i; |
| 9832 | xmlChar *buf; |
| 9833 | xmlChar quote = 0; |
| 9834 | |
| 9835 | base = ctxt->input->cur - ctxt->input->base; |
| 9836 | if (base < 0) return(0); |
| 9837 | if (ctxt->checkIndex > base) |
| 9838 | base = ctxt->checkIndex; |
| 9839 | buf = ctxt->input->buf->buffer->content; |
| 9840 | for (;(unsigned int) base < ctxt->input->buf->buffer->use; |
| 9841 | base++) { |
| 9842 | if (quote != 0) { |
| 9843 | if (buf[base] == quote) |
| 9844 | quote = 0; |
| 9845 | continue; |
| 9846 | } |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 9847 | if ((quote == 0) && (buf[base] == '<')) { |
| 9848 | int found = 0; |
| 9849 | /* special handling of comments */ |
| 9850 | if (((unsigned int) base + 4 < |
| 9851 | ctxt->input->buf->buffer->use) && |
| 9852 | (buf[base + 1] == '!') && |
| 9853 | (buf[base + 2] == '-') && |
| 9854 | (buf[base + 3] == '-')) { |
| 9855 | for (;(unsigned int) base + 3 < |
| 9856 | ctxt->input->buf->buffer->use; base++) { |
| 9857 | if ((buf[base] == '-') && |
| 9858 | (buf[base + 1] == '-') && |
| 9859 | (buf[base + 2] == '>')) { |
| 9860 | found = 1; |
| 9861 | base += 2; |
| 9862 | break; |
| 9863 | } |
| 9864 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 9865 | if (!found) { |
| 9866 | #if 0 |
| 9867 | fprintf(stderr, "unfinished comment\n"); |
| 9868 | #endif |
| 9869 | break; /* for */ |
| 9870 | } |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 9871 | continue; |
| 9872 | } |
| 9873 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9874 | if (buf[base] == '"') { |
| 9875 | quote = '"'; |
| 9876 | continue; |
| 9877 | } |
| 9878 | if (buf[base] == '\'') { |
| 9879 | quote = '\''; |
| 9880 | continue; |
| 9881 | } |
| 9882 | if (buf[base] == ']') { |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 9883 | #if 0 |
| 9884 | fprintf(stderr, "%c%c%c%c: ", buf[base], |
| 9885 | buf[base + 1], buf[base + 2], buf[base + 3]); |
| 9886 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9887 | if ((unsigned int) base +1 >= |
| 9888 | ctxt->input->buf->buffer->use) |
| 9889 | break; |
| 9890 | if (buf[base + 1] == ']') { |
| 9891 | /* conditional crap, skip both ']' ! */ |
| 9892 | base++; |
| 9893 | continue; |
| 9894 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 9895 | for (i = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9896 | (unsigned int) base + i < ctxt->input->buf->buffer->use; |
| 9897 | i++) { |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 9898 | if (buf[base + i] == '>') { |
| 9899 | #if 0 |
| 9900 | fprintf(stderr, "found\n"); |
| 9901 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9902 | goto found_end_int_subset; |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 9903 | } |
| 9904 | if (!IS_BLANK_CH(buf[base + i])) { |
| 9905 | #if 0 |
| 9906 | fprintf(stderr, "not found\n"); |
| 9907 | #endif |
| 9908 | goto not_end_of_int_subset; |
| 9909 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9910 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 9911 | #if 0 |
| 9912 | fprintf(stderr, "end of stream\n"); |
| 9913 | #endif |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9914 | break; |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 9915 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9916 | } |
Daniel Veillard | 8f8a9dd | 2005-01-25 21:41:42 +0000 | [diff] [blame] | 9917 | not_end_of_int_subset: |
| 9918 | continue; /* for */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9919 | } |
| 9920 | /* |
| 9921 | * We didn't found the end of the Internal subset |
| 9922 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9923 | #ifdef DEBUG_PUSH |
| 9924 | if (next == 0) |
| 9925 | xmlGenericError(xmlGenericErrorContext, |
| 9926 | "PP: lookup of int subset end filed\n"); |
| 9927 | #endif |
| 9928 | goto done; |
| 9929 | |
| 9930 | found_end_int_subset: |
| 9931 | xmlParseInternalSubset(ctxt); |
| 9932 | ctxt->inSubset = 2; |
| 9933 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 9934 | (ctxt->sax->externalSubset != NULL)) |
| 9935 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 9936 | ctxt->extSubSystem, ctxt->extSubURI); |
| 9937 | ctxt->inSubset = 0; |
| 9938 | ctxt->instate = XML_PARSER_PROLOG; |
| 9939 | ctxt->checkIndex = 0; |
| 9940 | #ifdef DEBUG_PUSH |
| 9941 | xmlGenericError(xmlGenericErrorContext, |
| 9942 | "PP: entering PROLOG\n"); |
| 9943 | #endif |
| 9944 | break; |
| 9945 | } |
| 9946 | case XML_PARSER_COMMENT: |
| 9947 | xmlGenericError(xmlGenericErrorContext, |
| 9948 | "PP: internal error, state == COMMENT\n"); |
| 9949 | ctxt->instate = XML_PARSER_CONTENT; |
| 9950 | #ifdef DEBUG_PUSH |
| 9951 | xmlGenericError(xmlGenericErrorContext, |
| 9952 | "PP: entering CONTENT\n"); |
| 9953 | #endif |
| 9954 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9955 | case XML_PARSER_IGNORE: |
| 9956 | xmlGenericError(xmlGenericErrorContext, |
| 9957 | "PP: internal error, state == IGNORE"); |
| 9958 | ctxt->instate = XML_PARSER_DTD; |
| 9959 | #ifdef DEBUG_PUSH |
| 9960 | xmlGenericError(xmlGenericErrorContext, |
| 9961 | "PP: entering DTD\n"); |
| 9962 | #endif |
| 9963 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9964 | case XML_PARSER_PI: |
| 9965 | xmlGenericError(xmlGenericErrorContext, |
| 9966 | "PP: internal error, state == PI\n"); |
| 9967 | ctxt->instate = XML_PARSER_CONTENT; |
| 9968 | #ifdef DEBUG_PUSH |
| 9969 | xmlGenericError(xmlGenericErrorContext, |
| 9970 | "PP: entering CONTENT\n"); |
| 9971 | #endif |
| 9972 | break; |
| 9973 | case XML_PARSER_ENTITY_DECL: |
| 9974 | xmlGenericError(xmlGenericErrorContext, |
| 9975 | "PP: internal error, state == ENTITY_DECL\n"); |
| 9976 | ctxt->instate = XML_PARSER_DTD; |
| 9977 | #ifdef DEBUG_PUSH |
| 9978 | xmlGenericError(xmlGenericErrorContext, |
| 9979 | "PP: entering DTD\n"); |
| 9980 | #endif |
| 9981 | break; |
| 9982 | case XML_PARSER_ENTITY_VALUE: |
| 9983 | xmlGenericError(xmlGenericErrorContext, |
| 9984 | "PP: internal error, state == ENTITY_VALUE\n"); |
| 9985 | ctxt->instate = XML_PARSER_CONTENT; |
| 9986 | #ifdef DEBUG_PUSH |
| 9987 | xmlGenericError(xmlGenericErrorContext, |
| 9988 | "PP: entering DTD\n"); |
| 9989 | #endif |
| 9990 | break; |
| 9991 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 9992 | xmlGenericError(xmlGenericErrorContext, |
| 9993 | "PP: internal error, state == ATTRIBUTE_VALUE\n"); |
| 9994 | ctxt->instate = XML_PARSER_START_TAG; |
| 9995 | #ifdef DEBUG_PUSH |
| 9996 | xmlGenericError(xmlGenericErrorContext, |
| 9997 | "PP: entering START_TAG\n"); |
| 9998 | #endif |
| 9999 | break; |
| 10000 | case XML_PARSER_SYSTEM_LITERAL: |
| 10001 | xmlGenericError(xmlGenericErrorContext, |
| 10002 | "PP: internal error, state == SYSTEM_LITERAL\n"); |
| 10003 | ctxt->instate = XML_PARSER_START_TAG; |
| 10004 | #ifdef DEBUG_PUSH |
| 10005 | xmlGenericError(xmlGenericErrorContext, |
| 10006 | "PP: entering START_TAG\n"); |
| 10007 | #endif |
| 10008 | break; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 10009 | case XML_PARSER_PUBLIC_LITERAL: |
| 10010 | xmlGenericError(xmlGenericErrorContext, |
| 10011 | "PP: internal error, state == PUBLIC_LITERAL\n"); |
| 10012 | ctxt->instate = XML_PARSER_START_TAG; |
| 10013 | #ifdef DEBUG_PUSH |
| 10014 | xmlGenericError(xmlGenericErrorContext, |
| 10015 | "PP: entering START_TAG\n"); |
| 10016 | #endif |
| 10017 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10018 | } |
| 10019 | } |
| 10020 | done: |
| 10021 | #ifdef DEBUG_PUSH |
| 10022 | xmlGenericError(xmlGenericErrorContext, "PP: done %d\n", ret); |
| 10023 | #endif |
| 10024 | return(ret); |
| 10025 | } |
| 10026 | |
| 10027 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10028 | * xmlParseChunk: |
| 10029 | * @ctxt: an XML parser context |
| 10030 | * @chunk: an char array |
| 10031 | * @size: the size in byte of the chunk |
| 10032 | * @terminate: last chunk indicator |
| 10033 | * |
| 10034 | * Parse a Chunk of memory |
| 10035 | * |
| 10036 | * Returns zero if no error, the xmlParserErrors otherwise. |
| 10037 | */ |
| 10038 | int |
| 10039 | xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size, |
| 10040 | int terminate) { |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10041 | if (ctxt == NULL) |
| 10042 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 10043 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 10044 | return(ctxt->errNo); |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 10045 | if (ctxt->instate == XML_PARSER_START) |
| 10046 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10047 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 10048 | (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) { |
| 10049 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 10050 | int cur = ctxt->input->cur - ctxt->input->base; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 10051 | int res; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10052 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 10053 | res =xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
| 10054 | if (res < 0) { |
| 10055 | ctxt->errNo = XML_PARSER_EOF; |
| 10056 | ctxt->disableSAX = 1; |
| 10057 | return (XML_PARSER_EOF); |
| 10058 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10059 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 10060 | ctxt->input->cur = ctxt->input->base + cur; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 10061 | ctxt->input->end = |
| 10062 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10063 | #ifdef DEBUG_PUSH |
| 10064 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 10065 | #endif |
| 10066 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10067 | } else if (ctxt->instate != XML_PARSER_EOF) { |
| 10068 | if ((ctxt->input != NULL) && ctxt->input->buf != NULL) { |
| 10069 | xmlParserInputBufferPtr in = ctxt->input->buf; |
| 10070 | if ((in->encoder != NULL) && (in->buffer != NULL) && |
| 10071 | (in->raw != NULL)) { |
| 10072 | int nbchars; |
| 10073 | |
| 10074 | nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw); |
| 10075 | if (nbchars < 0) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 10076 | /* TODO 2.6.0 */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10077 | xmlGenericError(xmlGenericErrorContext, |
| 10078 | "xmlParseChunk: encoder error\n"); |
| 10079 | return(XML_ERR_INVALID_ENCODING); |
| 10080 | } |
| 10081 | } |
| 10082 | } |
| 10083 | } |
| 10084 | xmlParseTryOrFinish(ctxt, terminate); |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 10085 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 10086 | return(ctxt->errNo); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10087 | if (terminate) { |
| 10088 | /* |
| 10089 | * Check for termination |
| 10090 | */ |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10091 | int avail = 0; |
| 10092 | |
| 10093 | if (ctxt->input != NULL) { |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 10094 | if (ctxt->input->buf == NULL) |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 10095 | avail = ctxt->input->length - |
| 10096 | (ctxt->input->cur - ctxt->input->base); |
| 10097 | else |
| 10098 | avail = ctxt->input->buf->buffer->use - |
| 10099 | (ctxt->input->cur - ctxt->input->base); |
| 10100 | } |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 10101 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10102 | if ((ctxt->instate != XML_PARSER_EOF) && |
| 10103 | (ctxt->instate != XML_PARSER_EPILOG)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10104 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10105 | } |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 10106 | if ((ctxt->instate == XML_PARSER_EPILOG) && (avail > 0)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10107 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 10108 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10109 | if (ctxt->instate != XML_PARSER_EOF) { |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 10110 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10111 | ctxt->sax->endDocument(ctxt->userData); |
| 10112 | } |
| 10113 | ctxt->instate = XML_PARSER_EOF; |
| 10114 | } |
| 10115 | return((xmlParserErrors) ctxt->errNo); |
| 10116 | } |
| 10117 | |
| 10118 | /************************************************************************ |
| 10119 | * * |
| 10120 | * I/O front end functions to the parser * |
| 10121 | * * |
| 10122 | ************************************************************************/ |
| 10123 | |
| 10124 | /** |
| 10125 | * xmlStopParser: |
| 10126 | * @ctxt: an XML parser context |
| 10127 | * |
| 10128 | * Blocks further parser processing |
| 10129 | */ |
| 10130 | void |
| 10131 | xmlStopParser(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 10132 | if (ctxt == NULL) |
| 10133 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10134 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 10135 | ctxt->disableSAX = 1; |
William M. Brack | 230c550 | 2004-12-20 16:18:49 +0000 | [diff] [blame] | 10136 | if (ctxt->input != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10137 | ctxt->input->cur = BAD_CAST""; |
William M. Brack | 230c550 | 2004-12-20 16:18:49 +0000 | [diff] [blame] | 10138 | ctxt->input->base = ctxt->input->cur; |
| 10139 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10140 | } |
| 10141 | |
| 10142 | /** |
| 10143 | * xmlCreatePushParserCtxt: |
| 10144 | * @sax: a SAX handler |
| 10145 | * @user_data: The user data returned on SAX callbacks |
| 10146 | * @chunk: a pointer to an array of chars |
| 10147 | * @size: number of chars in the array |
| 10148 | * @filename: an optional file name or URI |
| 10149 | * |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 10150 | * Create a parser context for using the XML parser in push mode. |
| 10151 | * If @buffer and @size are non-NULL, the data is used to detect |
| 10152 | * the encoding. The remaining characters will be parsed so they |
| 10153 | * don't need to be fed in again through xmlParseChunk. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10154 | * To allow content encoding detection, @size should be >= 4 |
| 10155 | * The value of @filename is used for fetching external entities |
| 10156 | * and error/warning reports. |
| 10157 | * |
| 10158 | * Returns the new parser context or NULL |
| 10159 | */ |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 10160 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10161 | xmlParserCtxtPtr |
| 10162 | xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
| 10163 | const char *chunk, int size, const char *filename) { |
| 10164 | xmlParserCtxtPtr ctxt; |
| 10165 | xmlParserInputPtr inputStream; |
| 10166 | xmlParserInputBufferPtr buf; |
| 10167 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 10168 | |
| 10169 | /* |
| 10170 | * plug some encoding conversion routines |
| 10171 | */ |
| 10172 | if ((chunk != NULL) && (size >= 4)) |
| 10173 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 10174 | |
| 10175 | buf = xmlAllocParserInputBuffer(enc); |
| 10176 | if (buf == NULL) return(NULL); |
| 10177 | |
| 10178 | ctxt = xmlNewParserCtxt(); |
| 10179 | if (ctxt == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 10180 | xmlErrMemory(NULL, "creating parser: out of memory\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 10181 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10182 | return(NULL); |
| 10183 | } |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 10184 | ctxt->dictNames = 1; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10185 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * sizeof(xmlChar *)); |
| 10186 | if (ctxt->pushTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10187 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10188 | xmlFreeParserInputBuffer(buf); |
| 10189 | xmlFreeParserCtxt(ctxt); |
| 10190 | return(NULL); |
| 10191 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10192 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10193 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 10194 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10195 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10196 | xmlFree(ctxt->sax); |
| 10197 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 10198 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10199 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 10200 | xmlFreeParserInputBuffer(buf); |
| 10201 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10202 | return(NULL); |
| 10203 | } |
Daniel Veillard | 5ea30d7 | 2004-11-08 11:54:28 +0000 | [diff] [blame] | 10204 | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
| 10205 | if (sax->initialized == XML_SAX2_MAGIC) |
| 10206 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 10207 | else |
| 10208 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10209 | if (user_data != NULL) |
| 10210 | ctxt->userData = user_data; |
| 10211 | } |
| 10212 | if (filename == NULL) { |
| 10213 | ctxt->directory = NULL; |
| 10214 | } else { |
| 10215 | ctxt->directory = xmlParserGetDirectory(filename); |
| 10216 | } |
| 10217 | |
| 10218 | inputStream = xmlNewInputStream(ctxt); |
| 10219 | if (inputStream == NULL) { |
| 10220 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 10221 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10222 | return(NULL); |
| 10223 | } |
| 10224 | |
| 10225 | if (filename == NULL) |
| 10226 | inputStream->filename = NULL; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 10227 | else { |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 10228 | inputStream->filename = (char *) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 10229 | xmlCanonicPath((const xmlChar *) filename); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 10230 | if (inputStream->filename == NULL) { |
| 10231 | xmlFreeParserCtxt(ctxt); |
| 10232 | xmlFreeParserInputBuffer(buf); |
| 10233 | return(NULL); |
| 10234 | } |
| 10235 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10236 | inputStream->buf = buf; |
| 10237 | inputStream->base = inputStream->buf->buffer->content; |
| 10238 | inputStream->cur = inputStream->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 10239 | inputStream->end = |
| 10240 | &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10241 | |
| 10242 | inputPush(ctxt, inputStream); |
| 10243 | |
William M. Brack | 3a1cd21 | 2005-02-11 14:35:54 +0000 | [diff] [blame] | 10244 | /* |
| 10245 | * If the caller didn't provide an initial 'chunk' for determining |
| 10246 | * the encoding, we set the context to XML_CHAR_ENCODING_NONE so |
| 10247 | * that it can be automatically determined later |
| 10248 | */ |
| 10249 | if ((size == 0) || (chunk == NULL)) { |
| 10250 | ctxt->charset = XML_CHAR_ENCODING_NONE; |
| 10251 | } else if ((ctxt->input != NULL) && (ctxt->input->buf != NULL)) { |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 10252 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 10253 | int cur = ctxt->input->cur - ctxt->input->base; |
| 10254 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10255 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 10256 | |
| 10257 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 10258 | ctxt->input->cur = ctxt->input->base + cur; |
| 10259 | ctxt->input->end = |
| 10260 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10261 | #ifdef DEBUG_PUSH |
| 10262 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 10263 | #endif |
| 10264 | } |
| 10265 | |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 10266 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10267 | xmlSwitchEncoding(ctxt, enc); |
| 10268 | } |
| 10269 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10270 | return(ctxt); |
| 10271 | } |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 10272 | #endif /* LIBXML_PUSH_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10273 | |
| 10274 | /** |
| 10275 | * xmlCreateIOParserCtxt: |
| 10276 | * @sax: a SAX handler |
| 10277 | * @user_data: The user data returned on SAX callbacks |
| 10278 | * @ioread: an I/O read function |
| 10279 | * @ioclose: an I/O close function |
| 10280 | * @ioctx: an I/O handler |
| 10281 | * @enc: the charset encoding if known |
| 10282 | * |
| 10283 | * Create a parser context for using the XML parser with an existing |
| 10284 | * I/O stream |
| 10285 | * |
| 10286 | * Returns the new parser context or NULL |
| 10287 | */ |
| 10288 | xmlParserCtxtPtr |
| 10289 | xmlCreateIOParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
| 10290 | xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
| 10291 | void *ioctx, xmlCharEncoding enc) { |
| 10292 | xmlParserCtxtPtr ctxt; |
| 10293 | xmlParserInputPtr inputStream; |
| 10294 | xmlParserInputBufferPtr buf; |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 10295 | |
| 10296 | if (ioread == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10297 | |
| 10298 | buf = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, enc); |
| 10299 | if (buf == NULL) return(NULL); |
| 10300 | |
| 10301 | ctxt = xmlNewParserCtxt(); |
| 10302 | if (ctxt == NULL) { |
| 10303 | xmlFree(buf); |
| 10304 | return(NULL); |
| 10305 | } |
| 10306 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10307 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 10308 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10309 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10310 | xmlFree(ctxt->sax); |
| 10311 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 10312 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10313 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10314 | xmlFree(ctxt); |
| 10315 | return(NULL); |
| 10316 | } |
Daniel Veillard | 5ea30d7 | 2004-11-08 11:54:28 +0000 | [diff] [blame] | 10317 | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
| 10318 | if (sax->initialized == XML_SAX2_MAGIC) |
| 10319 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 10320 | else |
| 10321 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10322 | if (user_data != NULL) |
| 10323 | ctxt->userData = user_data; |
| 10324 | } |
| 10325 | |
| 10326 | inputStream = xmlNewIOInputStream(ctxt, buf, enc); |
| 10327 | if (inputStream == NULL) { |
| 10328 | xmlFreeParserCtxt(ctxt); |
| 10329 | return(NULL); |
| 10330 | } |
| 10331 | inputPush(ctxt, inputStream); |
| 10332 | |
| 10333 | return(ctxt); |
| 10334 | } |
| 10335 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10336 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10337 | /************************************************************************ |
| 10338 | * * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10339 | * Front ends when parsing a DTD * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10340 | * * |
| 10341 | ************************************************************************/ |
| 10342 | |
| 10343 | /** |
| 10344 | * xmlIOParseDTD: |
| 10345 | * @sax: the SAX handler block or NULL |
| 10346 | * @input: an Input Buffer |
| 10347 | * @enc: the charset encoding if known |
| 10348 | * |
| 10349 | * Load and parse a DTD |
| 10350 | * |
| 10351 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 10352 | * @input will be freed at parsing end. |
| 10353 | */ |
| 10354 | |
| 10355 | xmlDtdPtr |
| 10356 | xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input, |
| 10357 | xmlCharEncoding enc) { |
| 10358 | xmlDtdPtr ret = NULL; |
| 10359 | xmlParserCtxtPtr ctxt; |
| 10360 | xmlParserInputPtr pinput = NULL; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10361 | xmlChar start[4]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10362 | |
| 10363 | if (input == NULL) |
| 10364 | return(NULL); |
| 10365 | |
| 10366 | ctxt = xmlNewParserCtxt(); |
| 10367 | if (ctxt == NULL) { |
| 10368 | return(NULL); |
| 10369 | } |
| 10370 | |
| 10371 | /* |
| 10372 | * Set-up the SAX context |
| 10373 | */ |
| 10374 | if (sax != NULL) { |
| 10375 | if (ctxt->sax != NULL) |
| 10376 | xmlFree(ctxt->sax); |
| 10377 | ctxt->sax = sax; |
Daniel Veillard | 500a1de | 2004-03-22 15:22:58 +0000 | [diff] [blame] | 10378 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10379 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10380 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10381 | |
| 10382 | /* |
| 10383 | * generate a parser input from the I/O handler |
| 10384 | */ |
| 10385 | |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 10386 | pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10387 | if (pinput == NULL) { |
| 10388 | if (sax != NULL) ctxt->sax = NULL; |
| 10389 | xmlFreeParserCtxt(ctxt); |
| 10390 | return(NULL); |
| 10391 | } |
| 10392 | |
| 10393 | /* |
| 10394 | * plug some encoding conversion routines here. |
| 10395 | */ |
| 10396 | xmlPushInput(ctxt, pinput); |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 10397 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10398 | xmlSwitchEncoding(ctxt, enc); |
| 10399 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10400 | |
| 10401 | pinput->filename = NULL; |
| 10402 | pinput->line = 1; |
| 10403 | pinput->col = 1; |
| 10404 | pinput->base = ctxt->input->cur; |
| 10405 | pinput->cur = ctxt->input->cur; |
| 10406 | pinput->free = NULL; |
| 10407 | |
| 10408 | /* |
| 10409 | * let's parse that entity knowing it's an external subset. |
| 10410 | */ |
| 10411 | ctxt->inSubset = 2; |
| 10412 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 10413 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 10414 | BAD_CAST "none", BAD_CAST "none"); |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10415 | |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10416 | if ((enc == XML_CHAR_ENCODING_NONE) && |
| 10417 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10418 | /* |
| 10419 | * Get the 4 first bytes and decode the charset |
| 10420 | * if enc != XML_CHAR_ENCODING_NONE |
| 10421 | * plug some encoding conversion routines. |
| 10422 | */ |
| 10423 | start[0] = RAW; |
| 10424 | start[1] = NXT(1); |
| 10425 | start[2] = NXT(2); |
| 10426 | start[3] = NXT(3); |
| 10427 | enc = xmlDetectCharEncoding(start, 4); |
| 10428 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10429 | xmlSwitchEncoding(ctxt, enc); |
| 10430 | } |
| 10431 | } |
| 10432 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10433 | xmlParseExternalSubset(ctxt, BAD_CAST "none", BAD_CAST "none"); |
| 10434 | |
| 10435 | if (ctxt->myDoc != NULL) { |
| 10436 | if (ctxt->wellFormed) { |
| 10437 | ret = ctxt->myDoc->extSubset; |
| 10438 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | 329456a | 2003-04-26 21:21:00 +0000 | [diff] [blame] | 10439 | if (ret != NULL) { |
| 10440 | xmlNodePtr tmp; |
| 10441 | |
| 10442 | ret->doc = NULL; |
| 10443 | tmp = ret->children; |
| 10444 | while (tmp != NULL) { |
| 10445 | tmp->doc = NULL; |
| 10446 | tmp = tmp->next; |
| 10447 | } |
| 10448 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10449 | } else { |
| 10450 | ret = NULL; |
| 10451 | } |
| 10452 | xmlFreeDoc(ctxt->myDoc); |
| 10453 | ctxt->myDoc = NULL; |
| 10454 | } |
| 10455 | if (sax != NULL) ctxt->sax = NULL; |
| 10456 | xmlFreeParserCtxt(ctxt); |
| 10457 | |
| 10458 | return(ret); |
| 10459 | } |
| 10460 | |
| 10461 | /** |
| 10462 | * xmlSAXParseDTD: |
| 10463 | * @sax: the SAX handler block |
| 10464 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 10465 | * @SystemID: a NAME* containing the URL to the DTD |
| 10466 | * |
| 10467 | * Load and parse an external subset. |
| 10468 | * |
| 10469 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 10470 | */ |
| 10471 | |
| 10472 | xmlDtdPtr |
| 10473 | xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID, |
| 10474 | const xmlChar *SystemID) { |
| 10475 | xmlDtdPtr ret = NULL; |
| 10476 | xmlParserCtxtPtr ctxt; |
| 10477 | xmlParserInputPtr input = NULL; |
| 10478 | xmlCharEncoding enc; |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 10479 | xmlChar* systemIdCanonic; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10480 | |
| 10481 | if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL); |
| 10482 | |
| 10483 | ctxt = xmlNewParserCtxt(); |
| 10484 | if (ctxt == NULL) { |
| 10485 | return(NULL); |
| 10486 | } |
| 10487 | |
| 10488 | /* |
| 10489 | * Set-up the SAX context |
| 10490 | */ |
| 10491 | if (sax != NULL) { |
| 10492 | if (ctxt->sax != NULL) |
| 10493 | xmlFree(ctxt->sax); |
| 10494 | ctxt->sax = sax; |
Daniel Veillard | bf1e3d8 | 2003-08-14 23:57:26 +0000 | [diff] [blame] | 10495 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10496 | } |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 10497 | |
| 10498 | /* |
| 10499 | * Canonicalise the system ID |
| 10500 | */ |
| 10501 | systemIdCanonic = xmlCanonicPath(SystemID); |
Daniel Veillard | c93a19f | 2004-10-04 11:53:20 +0000 | [diff] [blame] | 10502 | if ((SystemID != NULL) && (systemIdCanonic == NULL)) { |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 10503 | xmlFreeParserCtxt(ctxt); |
| 10504 | return(NULL); |
| 10505 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10506 | |
| 10507 | /* |
| 10508 | * Ask the Entity resolver to load the damn thing |
| 10509 | */ |
| 10510 | |
| 10511 | if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL)) |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 10512 | input = ctxt->sax->resolveEntity(ctxt, ExternalID, systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10513 | if (input == NULL) { |
| 10514 | if (sax != NULL) ctxt->sax = NULL; |
| 10515 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 10516 | if (systemIdCanonic != NULL) |
| 10517 | xmlFree(systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10518 | return(NULL); |
| 10519 | } |
| 10520 | |
| 10521 | /* |
| 10522 | * plug some encoding conversion routines here. |
| 10523 | */ |
| 10524 | xmlPushInput(ctxt, input); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10525 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 10526 | enc = xmlDetectCharEncoding(ctxt->input->cur, 4); |
| 10527 | xmlSwitchEncoding(ctxt, enc); |
| 10528 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10529 | |
| 10530 | if (input->filename == NULL) |
Igor Zlatkovic | 07d5976 | 2004-08-24 19:12:51 +0000 | [diff] [blame] | 10531 | input->filename = (char *) systemIdCanonic; |
| 10532 | else |
| 10533 | xmlFree(systemIdCanonic); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10534 | input->line = 1; |
| 10535 | input->col = 1; |
| 10536 | input->base = ctxt->input->cur; |
| 10537 | input->cur = ctxt->input->cur; |
| 10538 | input->free = NULL; |
| 10539 | |
| 10540 | /* |
| 10541 | * let's parse that entity knowing it's an external subset. |
| 10542 | */ |
| 10543 | ctxt->inSubset = 2; |
| 10544 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 10545 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 10546 | ExternalID, SystemID); |
| 10547 | xmlParseExternalSubset(ctxt, ExternalID, SystemID); |
| 10548 | |
| 10549 | if (ctxt->myDoc != NULL) { |
| 10550 | if (ctxt->wellFormed) { |
| 10551 | ret = ctxt->myDoc->extSubset; |
| 10552 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | c557346 | 2003-04-25 16:43:49 +0000 | [diff] [blame] | 10553 | if (ret != NULL) { |
| 10554 | xmlNodePtr tmp; |
| 10555 | |
| 10556 | ret->doc = NULL; |
| 10557 | tmp = ret->children; |
| 10558 | while (tmp != NULL) { |
| 10559 | tmp->doc = NULL; |
| 10560 | tmp = tmp->next; |
| 10561 | } |
| 10562 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10563 | } else { |
| 10564 | ret = NULL; |
| 10565 | } |
| 10566 | xmlFreeDoc(ctxt->myDoc); |
| 10567 | ctxt->myDoc = NULL; |
| 10568 | } |
| 10569 | if (sax != NULL) ctxt->sax = NULL; |
| 10570 | xmlFreeParserCtxt(ctxt); |
| 10571 | |
| 10572 | return(ret); |
| 10573 | } |
| 10574 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10575 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10576 | /** |
| 10577 | * xmlParseDTD: |
| 10578 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 10579 | * @SystemID: a NAME* containing the URL to the DTD |
| 10580 | * |
| 10581 | * Load and parse an external subset. |
| 10582 | * |
| 10583 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 10584 | */ |
| 10585 | |
| 10586 | xmlDtdPtr |
| 10587 | xmlParseDTD(const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 10588 | return(xmlSAXParseDTD(NULL, ExternalID, SystemID)); |
| 10589 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10590 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10591 | |
| 10592 | /************************************************************************ |
| 10593 | * * |
| 10594 | * Front ends when parsing an Entity * |
| 10595 | * * |
| 10596 | ************************************************************************/ |
| 10597 | |
| 10598 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10599 | * xmlParseCtxtExternalEntity: |
| 10600 | * @ctx: the existing parsing context |
| 10601 | * @URL: the URL for the entity to load |
| 10602 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10603 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10604 | * |
| 10605 | * Parse an external general entity within an existing parsing context |
| 10606 | * An external general parsed entity is well-formed if it matches the |
| 10607 | * production labeled extParsedEnt. |
| 10608 | * |
| 10609 | * [78] extParsedEnt ::= TextDecl? content |
| 10610 | * |
| 10611 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 10612 | * the parser error code otherwise |
| 10613 | */ |
| 10614 | |
| 10615 | int |
| 10616 | xmlParseCtxtExternalEntity(xmlParserCtxtPtr ctx, const xmlChar *URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10617 | const xmlChar *ID, xmlNodePtr *lst) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10618 | xmlParserCtxtPtr ctxt; |
| 10619 | xmlDocPtr newDoc; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 10620 | xmlNodePtr newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10621 | xmlSAXHandlerPtr oldsax = NULL; |
| 10622 | int ret = 0; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10623 | xmlChar start[4]; |
| 10624 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10625 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 10626 | if (ctx == NULL) return(-1); |
| 10627 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10628 | if (ctx->depth > 40) { |
| 10629 | return(XML_ERR_ENTITY_LOOP); |
| 10630 | } |
| 10631 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10632 | if (lst != NULL) |
| 10633 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10634 | if ((URL == NULL) && (ID == NULL)) |
| 10635 | return(-1); |
| 10636 | if (ctx->myDoc == NULL) /* @@ relax but check for dereferences */ |
| 10637 | return(-1); |
| 10638 | |
| 10639 | |
| 10640 | ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL); |
| 10641 | if (ctxt == NULL) return(-1); |
| 10642 | ctxt->userData = ctxt; |
Daniel Veillard | e2830f1 | 2003-01-08 17:47:49 +0000 | [diff] [blame] | 10643 | ctxt->_private = ctx->_private; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10644 | oldsax = ctxt->sax; |
| 10645 | ctxt->sax = ctx->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10646 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10647 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 10648 | if (newDoc == NULL) { |
| 10649 | xmlFreeParserCtxt(ctxt); |
| 10650 | return(-1); |
| 10651 | } |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 10652 | if (ctx->myDoc->dict) { |
| 10653 | newDoc->dict = ctx->myDoc->dict; |
| 10654 | xmlDictReference(newDoc->dict); |
| 10655 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10656 | if (ctx->myDoc != NULL) { |
| 10657 | newDoc->intSubset = ctx->myDoc->intSubset; |
| 10658 | newDoc->extSubset = ctx->myDoc->extSubset; |
| 10659 | } |
| 10660 | if (ctx->myDoc->URL != NULL) { |
| 10661 | newDoc->URL = xmlStrdup(ctx->myDoc->URL); |
| 10662 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 10663 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 10664 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10665 | ctxt->sax = oldsax; |
| 10666 | xmlFreeParserCtxt(ctxt); |
| 10667 | newDoc->intSubset = NULL; |
| 10668 | newDoc->extSubset = NULL; |
| 10669 | xmlFreeDoc(newDoc); |
| 10670 | return(-1); |
| 10671 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 10672 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10673 | nodePush(ctxt, newDoc->children); |
| 10674 | if (ctx->myDoc == NULL) { |
| 10675 | ctxt->myDoc = newDoc; |
| 10676 | } else { |
| 10677 | ctxt->myDoc = ctx->myDoc; |
| 10678 | newDoc->children->doc = ctx->myDoc; |
| 10679 | } |
| 10680 | |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10681 | /* |
| 10682 | * Get the 4 first bytes and decode the charset |
| 10683 | * if enc != XML_CHAR_ENCODING_NONE |
| 10684 | * plug some encoding conversion routines. |
| 10685 | */ |
| 10686 | GROW |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10687 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 10688 | start[0] = RAW; |
| 10689 | start[1] = NXT(1); |
| 10690 | start[2] = NXT(2); |
| 10691 | start[3] = NXT(3); |
| 10692 | enc = xmlDetectCharEncoding(start, 4); |
| 10693 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10694 | xmlSwitchEncoding(ctxt, enc); |
| 10695 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10696 | } |
| 10697 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10698 | /* |
| 10699 | * Parse a possible text declaration first |
| 10700 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10701 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10702 | xmlParseTextDecl(ctxt); |
| 10703 | } |
| 10704 | |
| 10705 | /* |
| 10706 | * Doing validity checking on chunk doesn't make sense |
| 10707 | */ |
| 10708 | ctxt->instate = XML_PARSER_CONTENT; |
| 10709 | ctxt->validate = ctx->validate; |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 10710 | ctxt->valid = ctx->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10711 | ctxt->loadsubset = ctx->loadsubset; |
| 10712 | ctxt->depth = ctx->depth + 1; |
| 10713 | ctxt->replaceEntities = ctx->replaceEntities; |
| 10714 | if (ctxt->validate) { |
| 10715 | ctxt->vctxt.error = ctx->vctxt.error; |
| 10716 | ctxt->vctxt.warning = ctx->vctxt.warning; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10717 | } else { |
| 10718 | ctxt->vctxt.error = NULL; |
| 10719 | ctxt->vctxt.warning = NULL; |
| 10720 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 10721 | ctxt->vctxt.nodeTab = NULL; |
| 10722 | ctxt->vctxt.nodeNr = 0; |
| 10723 | ctxt->vctxt.nodeMax = 0; |
| 10724 | ctxt->vctxt.node = NULL; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 10725 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 10726 | ctxt->dict = ctx->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 10727 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 10728 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 10729 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 10730 | ctxt->dictNames = ctx->dictNames; |
| 10731 | ctxt->attsDefault = ctx->attsDefault; |
| 10732 | ctxt->attsSpecial = ctx->attsSpecial; |
William M. Brack | 503b610 | 2004-08-19 02:17:27 +0000 | [diff] [blame] | 10733 | ctxt->linenumbers = ctx->linenumbers; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10734 | |
| 10735 | xmlParseContent(ctxt); |
| 10736 | |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 10737 | ctx->validate = ctxt->validate; |
| 10738 | ctx->valid = ctxt->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10739 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10740 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10741 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10742 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10743 | } |
| 10744 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10745 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10746 | } |
| 10747 | |
| 10748 | if (!ctxt->wellFormed) { |
| 10749 | if (ctxt->errNo == 0) |
| 10750 | ret = 1; |
| 10751 | else |
| 10752 | ret = ctxt->errNo; |
| 10753 | } else { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10754 | if (lst != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10755 | xmlNodePtr cur; |
| 10756 | |
| 10757 | /* |
| 10758 | * Return the newly created nodeset after unlinking it from |
| 10759 | * they pseudo parent. |
| 10760 | */ |
| 10761 | cur = newDoc->children->children; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10762 | *lst = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10763 | while (cur != NULL) { |
| 10764 | cur->parent = NULL; |
| 10765 | cur = cur->next; |
| 10766 | } |
| 10767 | newDoc->children->children = NULL; |
| 10768 | } |
| 10769 | ret = 0; |
| 10770 | } |
| 10771 | ctxt->sax = oldsax; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 10772 | ctxt->dict = NULL; |
| 10773 | ctxt->attsDefault = NULL; |
| 10774 | ctxt->attsSpecial = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10775 | xmlFreeParserCtxt(ctxt); |
| 10776 | newDoc->intSubset = NULL; |
| 10777 | newDoc->extSubset = NULL; |
| 10778 | xmlFreeDoc(newDoc); |
| 10779 | |
| 10780 | return(ret); |
| 10781 | } |
| 10782 | |
| 10783 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 10784 | * xmlParseExternalEntityPrivate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10785 | * @doc: the document the chunk pertains to |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10786 | * @oldctxt: the previous parser context if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10787 | * @sax: the SAX handler bloc (possibly NULL) |
| 10788 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 10789 | * @depth: Used for loop detection, use 0 |
| 10790 | * @URL: the URL for the entity to load |
| 10791 | * @ID: the System ID for the entity to load |
| 10792 | * @list: the return value for the set of parsed nodes |
| 10793 | * |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 10794 | * Private version of xmlParseExternalEntity() |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10795 | * |
| 10796 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 10797 | * the parser error code otherwise |
| 10798 | */ |
| 10799 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10800 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10801 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 10802 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 10803 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10804 | const xmlChar *ID, xmlNodePtr *list) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10805 | xmlParserCtxtPtr ctxt; |
| 10806 | xmlDocPtr newDoc; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 10807 | xmlNodePtr newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10808 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10809 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10810 | xmlChar start[4]; |
| 10811 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10812 | |
| 10813 | if (depth > 40) { |
| 10814 | return(XML_ERR_ENTITY_LOOP); |
| 10815 | } |
| 10816 | |
| 10817 | |
| 10818 | |
| 10819 | if (list != NULL) |
| 10820 | *list = NULL; |
| 10821 | if ((URL == NULL) && (ID == NULL)) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10822 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10823 | if (doc == NULL) /* @@ relax but check for dereferences */ |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10824 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10825 | |
| 10826 | |
| 10827 | ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL); |
William M. Brack | b670e2e | 2003-09-27 01:05:55 +0000 | [diff] [blame] | 10828 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10829 | ctxt->userData = ctxt; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10830 | if (oldctxt != NULL) { |
| 10831 | ctxt->_private = oldctxt->_private; |
| 10832 | ctxt->loadsubset = oldctxt->loadsubset; |
| 10833 | ctxt->validate = oldctxt->validate; |
| 10834 | ctxt->external = oldctxt->external; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 10835 | ctxt->record_info = oldctxt->record_info; |
| 10836 | ctxt->node_seq.maximum = oldctxt->node_seq.maximum; |
| 10837 | ctxt->node_seq.length = oldctxt->node_seq.length; |
| 10838 | ctxt->node_seq.buffer = oldctxt->node_seq.buffer; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10839 | } else { |
| 10840 | /* |
| 10841 | * Doing validity checking on chunk without context |
| 10842 | * doesn't make sense |
| 10843 | */ |
| 10844 | ctxt->_private = NULL; |
| 10845 | ctxt->validate = 0; |
| 10846 | ctxt->external = 2; |
| 10847 | ctxt->loadsubset = 0; |
| 10848 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10849 | if (sax != NULL) { |
| 10850 | oldsax = ctxt->sax; |
| 10851 | ctxt->sax = sax; |
| 10852 | if (user_data != NULL) |
| 10853 | ctxt->userData = user_data; |
| 10854 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10855 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10856 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 10857 | if (newDoc == NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 10858 | ctxt->node_seq.maximum = 0; |
| 10859 | ctxt->node_seq.length = 0; |
| 10860 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10861 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10862 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10863 | } |
| 10864 | if (doc != NULL) { |
| 10865 | newDoc->intSubset = doc->intSubset; |
| 10866 | newDoc->extSubset = doc->extSubset; |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 10867 | newDoc->dict = doc->dict; |
| 10868 | } else if (oldctxt != NULL) { |
| 10869 | newDoc->dict = oldctxt->dict; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10870 | } |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 10871 | xmlDictReference(newDoc->dict); |
| 10872 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10873 | if (doc->URL != NULL) { |
| 10874 | newDoc->URL = xmlStrdup(doc->URL); |
| 10875 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 10876 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 10877 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10878 | if (sax != NULL) |
| 10879 | ctxt->sax = oldsax; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 10880 | ctxt->node_seq.maximum = 0; |
| 10881 | ctxt->node_seq.length = 0; |
| 10882 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10883 | xmlFreeParserCtxt(ctxt); |
| 10884 | newDoc->intSubset = NULL; |
| 10885 | newDoc->extSubset = NULL; |
| 10886 | xmlFreeDoc(newDoc); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10887 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10888 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 10889 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10890 | nodePush(ctxt, newDoc->children); |
| 10891 | if (doc == NULL) { |
| 10892 | ctxt->myDoc = newDoc; |
| 10893 | } else { |
| 10894 | ctxt->myDoc = doc; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 10895 | newRoot->doc = doc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10896 | } |
| 10897 | |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10898 | /* |
| 10899 | * Get the 4 first bytes and decode the charset |
| 10900 | * if enc != XML_CHAR_ENCODING_NONE |
| 10901 | * plug some encoding conversion routines. |
| 10902 | */ |
| 10903 | GROW; |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10904 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 10905 | start[0] = RAW; |
| 10906 | start[1] = NXT(1); |
| 10907 | start[2] = NXT(2); |
| 10908 | start[3] = NXT(3); |
| 10909 | enc = xmlDetectCharEncoding(start, 4); |
| 10910 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10911 | xmlSwitchEncoding(ctxt, enc); |
| 10912 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10913 | } |
| 10914 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10915 | /* |
| 10916 | * Parse a possible text declaration first |
| 10917 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10918 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10919 | xmlParseTextDecl(ctxt); |
| 10920 | } |
| 10921 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10922 | ctxt->instate = XML_PARSER_CONTENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10923 | ctxt->depth = depth; |
| 10924 | |
| 10925 | xmlParseContent(ctxt); |
| 10926 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10927 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10928 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10929 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10930 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10931 | } |
| 10932 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10933 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10934 | } |
| 10935 | |
| 10936 | if (!ctxt->wellFormed) { |
| 10937 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10938 | ret = XML_ERR_INTERNAL_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10939 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 10940 | ret = (xmlParserErrors)ctxt->errNo; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10941 | } else { |
| 10942 | if (list != NULL) { |
| 10943 | xmlNodePtr cur; |
| 10944 | |
| 10945 | /* |
| 10946 | * Return the newly created nodeset after unlinking it from |
| 10947 | * they pseudo parent. |
| 10948 | */ |
| 10949 | cur = newDoc->children->children; |
| 10950 | *list = cur; |
| 10951 | while (cur != NULL) { |
| 10952 | cur->parent = NULL; |
| 10953 | cur = cur->next; |
| 10954 | } |
| 10955 | newDoc->children->children = NULL; |
| 10956 | } |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10957 | ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10958 | } |
| 10959 | if (sax != NULL) |
| 10960 | ctxt->sax = oldsax; |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 10961 | oldctxt->node_seq.maximum = ctxt->node_seq.maximum; |
| 10962 | oldctxt->node_seq.length = ctxt->node_seq.length; |
| 10963 | oldctxt->node_seq.buffer = ctxt->node_seq.buffer; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 10964 | ctxt->node_seq.maximum = 0; |
| 10965 | ctxt->node_seq.length = 0; |
| 10966 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10967 | xmlFreeParserCtxt(ctxt); |
| 10968 | newDoc->intSubset = NULL; |
| 10969 | newDoc->extSubset = NULL; |
| 10970 | xmlFreeDoc(newDoc); |
| 10971 | |
| 10972 | return(ret); |
| 10973 | } |
| 10974 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10975 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10976 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 10977 | * xmlParseExternalEntity: |
| 10978 | * @doc: the document the chunk pertains to |
| 10979 | * @sax: the SAX handler bloc (possibly NULL) |
| 10980 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 10981 | * @depth: Used for loop detection, use 0 |
| 10982 | * @URL: the URL for the entity to load |
| 10983 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10984 | * @lst: the return value for the set of parsed nodes |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 10985 | * |
| 10986 | * Parse an external general entity |
| 10987 | * An external general parsed entity is well-formed if it matches the |
| 10988 | * production labeled extParsedEnt. |
| 10989 | * |
| 10990 | * [78] extParsedEnt ::= TextDecl? content |
| 10991 | * |
| 10992 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 10993 | * the parser error code otherwise |
| 10994 | */ |
| 10995 | |
| 10996 | int |
| 10997 | xmlParseExternalEntity(xmlDocPtr doc, xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10998 | int depth, const xmlChar *URL, const xmlChar *ID, xmlNodePtr *lst) { |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10999 | return(xmlParseExternalEntityPrivate(doc, NULL, sax, user_data, depth, URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 11000 | ID, lst)); |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 11001 | } |
| 11002 | |
| 11003 | /** |
Daniel Veillard | e020c3a | 2001-03-21 18:06:15 +0000 | [diff] [blame] | 11004 | * xmlParseBalancedChunkMemory: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11005 | * @doc: the document the chunk pertains to |
| 11006 | * @sax: the SAX handler bloc (possibly NULL) |
| 11007 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 11008 | * @depth: Used for loop detection, use 0 |
| 11009 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 11010 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11011 | * |
| 11012 | * Parse a well-balanced chunk of an XML document |
| 11013 | * called by the parser |
| 11014 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 11015 | * the content production in the XML grammar: |
| 11016 | * |
| 11017 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 11018 | * |
| 11019 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 11020 | * the parser error code otherwise |
| 11021 | */ |
| 11022 | |
| 11023 | int |
| 11024 | xmlParseBalancedChunkMemory(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 11025 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst) { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 11026 | return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data, |
| 11027 | depth, string, lst, 0 ); |
| 11028 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11029 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 11030 | |
| 11031 | /** |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11032 | * xmlParseBalancedChunkMemoryInternal: |
| 11033 | * @oldctxt: the existing parsing context |
| 11034 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 11035 | * @user_data: the user data field for the parser context |
| 11036 | * @lst: the return value for the set of parsed nodes |
| 11037 | * |
| 11038 | * |
| 11039 | * Parse a well-balanced chunk of an XML document |
| 11040 | * called by the parser |
| 11041 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 11042 | * the content production in the XML grammar: |
| 11043 | * |
| 11044 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 11045 | * |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 11046 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 11047 | * error code otherwise |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11048 | * |
| 11049 | * In case recover is set to 1, the nodelist will not be empty even if |
| 11050 | * the parsed chunk is not well balanced. |
| 11051 | */ |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 11052 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11053 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 11054 | const xmlChar *string, void *user_data, xmlNodePtr *lst) { |
| 11055 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 11056 | xmlDocPtr newDoc = NULL; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 11057 | xmlNodePtr newRoot; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11058 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 11059 | xmlNodePtr content = NULL; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 11060 | xmlNodePtr last = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11061 | int size; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 11062 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11063 | |
| 11064 | if (oldctxt->depth > 40) { |
| 11065 | return(XML_ERR_ENTITY_LOOP); |
| 11066 | } |
| 11067 | |
| 11068 | |
| 11069 | if (lst != NULL) |
| 11070 | *lst = NULL; |
| 11071 | if (string == NULL) |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 11072 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11073 | |
| 11074 | size = xmlStrlen(string); |
| 11075 | |
| 11076 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 11077 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11078 | if (user_data != NULL) |
| 11079 | ctxt->userData = user_data; |
| 11080 | else |
| 11081 | ctxt->userData = ctxt; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 11082 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 11083 | ctxt->dict = oldctxt->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 11084 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 11085 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 11086 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11087 | |
| 11088 | oldsax = ctxt->sax; |
| 11089 | ctxt->sax = oldctxt->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11090 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 11091 | ctxt->replaceEntities = oldctxt->replaceEntities; |
| 11092 | ctxt->options = oldctxt->options; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11093 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 11094 | ctxt->_private = oldctxt->_private; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11095 | if (oldctxt->myDoc == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 11096 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 11097 | if (newDoc == NULL) { |
| 11098 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 11099 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 11100 | xmlFreeParserCtxt(ctxt); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 11101 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 11102 | } |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 11103 | newDoc->dict = ctxt->dict; |
| 11104 | xmlDictReference(newDoc->dict); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11105 | ctxt->myDoc = newDoc; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 11106 | } else { |
| 11107 | ctxt->myDoc = oldctxt->myDoc; |
| 11108 | content = ctxt->myDoc->children; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 11109 | last = ctxt->myDoc->last; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11110 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 11111 | newRoot = xmlNewDocNode(ctxt->myDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 11112 | if (newRoot == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 11113 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 11114 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 11115 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 11116 | if (newDoc != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 11117 | xmlFreeDoc(newDoc); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 11118 | } |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 11119 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 11120 | } |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 11121 | ctxt->myDoc->children = NULL; |
| 11122 | ctxt->myDoc->last = NULL; |
| 11123 | xmlAddChild((xmlNodePtr) ctxt->myDoc, newRoot); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 11124 | nodePush(ctxt, ctxt->myDoc->children); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11125 | ctxt->instate = XML_PARSER_CONTENT; |
| 11126 | ctxt->depth = oldctxt->depth + 1; |
| 11127 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11128 | ctxt->validate = 0; |
| 11129 | ctxt->loadsubset = oldctxt->loadsubset; |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 11130 | if ((oldctxt->validate) || (oldctxt->replaceEntities != 0)) { |
| 11131 | /* |
| 11132 | * ID/IDREF registration will be done in xmlValidateElement below |
| 11133 | */ |
| 11134 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 11135 | } |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 11136 | ctxt->dictNames = oldctxt->dictNames; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 11137 | ctxt->attsDefault = oldctxt->attsDefault; |
| 11138 | ctxt->attsSpecial = oldctxt->attsSpecial; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11139 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 11140 | xmlParseContent(ctxt); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11141 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11142 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11143 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11144 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11145 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 11146 | if (ctxt->node != ctxt->myDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11147 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11148 | } |
| 11149 | |
| 11150 | if (!ctxt->wellFormed) { |
| 11151 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 11152 | ret = XML_ERR_INTERNAL_ERROR; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11153 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 11154 | ret = (xmlParserErrors)ctxt->errNo; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11155 | } else { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 11156 | ret = XML_ERR_OK; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11157 | } |
| 11158 | |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 11159 | if ((lst != NULL) && (ret == XML_ERR_OK)) { |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11160 | xmlNodePtr cur; |
| 11161 | |
| 11162 | /* |
| 11163 | * Return the newly created nodeset after unlinking it from |
| 11164 | * they pseudo parent. |
| 11165 | */ |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 11166 | cur = ctxt->myDoc->children->children; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11167 | *lst = cur; |
| 11168 | while (cur != NULL) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 11169 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | 8d58904 | 2003-02-04 15:07:21 +0000 | [diff] [blame] | 11170 | if (oldctxt->validate && oldctxt->wellFormed && |
| 11171 | oldctxt->myDoc && oldctxt->myDoc->intSubset) { |
| 11172 | oldctxt->valid &= xmlValidateElement(&oldctxt->vctxt, |
| 11173 | oldctxt->myDoc, cur); |
| 11174 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 11175 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11176 | cur->parent = NULL; |
| 11177 | cur = cur->next; |
| 11178 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 11179 | ctxt->myDoc->children->children = NULL; |
| 11180 | } |
| 11181 | if (ctxt->myDoc != NULL) { |
| 11182 | xmlFreeNode(ctxt->myDoc->children); |
| 11183 | ctxt->myDoc->children = content; |
Daniel Veillard | 8de5c0b | 2004-10-07 13:14:19 +0000 | [diff] [blame] | 11184 | ctxt->myDoc->last = last; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11185 | } |
| 11186 | |
| 11187 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 11188 | ctxt->dict = NULL; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 11189 | ctxt->attsDefault = NULL; |
| 11190 | ctxt->attsSpecial = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11191 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 11192 | if (newDoc != NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 11193 | xmlFreeDoc(newDoc); |
Daniel Veillard | 03a53c3 | 2004-10-26 16:06:51 +0000 | [diff] [blame] | 11194 | } |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11195 | |
| 11196 | return(ret); |
| 11197 | } |
| 11198 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 11199 | /** |
| 11200 | * xmlParseInNodeContext: |
| 11201 | * @node: the context node |
| 11202 | * @data: the input string |
| 11203 | * @datalen: the input string length in bytes |
| 11204 | * @options: a combination of xmlParserOption |
| 11205 | * @lst: the return value for the set of parsed nodes |
| 11206 | * |
| 11207 | * Parse a well-balanced chunk of an XML document |
| 11208 | * within the context (DTD, namespaces, etc ...) of the given node. |
| 11209 | * |
| 11210 | * The allowed sequence for the data is a Well Balanced Chunk defined by |
| 11211 | * the content production in the XML grammar: |
| 11212 | * |
| 11213 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 11214 | * |
| 11215 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 11216 | * error code otherwise |
| 11217 | */ |
| 11218 | xmlParserErrors |
| 11219 | xmlParseInNodeContext(xmlNodePtr node, const char *data, int datalen, |
| 11220 | int options, xmlNodePtr *lst) { |
| 11221 | #ifdef SAX2 |
| 11222 | xmlParserCtxtPtr ctxt; |
| 11223 | xmlDocPtr doc = NULL; |
| 11224 | xmlNodePtr fake, cur; |
| 11225 | int nsnr = 0; |
| 11226 | |
| 11227 | xmlParserErrors ret = XML_ERR_OK; |
| 11228 | |
| 11229 | /* |
| 11230 | * check all input parameters, grab the document |
| 11231 | */ |
| 11232 | if ((lst == NULL) || (node == NULL) || (data == NULL) || (datalen < 0)) |
| 11233 | return(XML_ERR_INTERNAL_ERROR); |
| 11234 | switch (node->type) { |
| 11235 | case XML_ELEMENT_NODE: |
| 11236 | case XML_ATTRIBUTE_NODE: |
| 11237 | case XML_TEXT_NODE: |
| 11238 | case XML_CDATA_SECTION_NODE: |
| 11239 | case XML_ENTITY_REF_NODE: |
| 11240 | case XML_PI_NODE: |
| 11241 | case XML_COMMENT_NODE: |
| 11242 | case XML_DOCUMENT_NODE: |
| 11243 | case XML_HTML_DOCUMENT_NODE: |
| 11244 | break; |
| 11245 | default: |
| 11246 | return(XML_ERR_INTERNAL_ERROR); |
| 11247 | |
| 11248 | } |
| 11249 | while ((node != NULL) && (node->type != XML_ELEMENT_NODE) && |
| 11250 | (node->type != XML_DOCUMENT_NODE) && |
| 11251 | (node->type != XML_HTML_DOCUMENT_NODE)) |
| 11252 | node = node->parent; |
| 11253 | if (node == NULL) |
| 11254 | return(XML_ERR_INTERNAL_ERROR); |
| 11255 | if (node->type == XML_ELEMENT_NODE) |
| 11256 | doc = node->doc; |
| 11257 | else |
| 11258 | doc = (xmlDocPtr) node; |
| 11259 | if (doc == NULL) |
| 11260 | return(XML_ERR_INTERNAL_ERROR); |
| 11261 | |
| 11262 | /* |
| 11263 | * allocate a context and set-up everything not related to the |
| 11264 | * node position in the tree |
| 11265 | */ |
| 11266 | if (doc->type == XML_DOCUMENT_NODE) |
| 11267 | ctxt = xmlCreateMemoryParserCtxt((char *) data, datalen); |
| 11268 | #ifdef LIBXML_HTML_ENABLED |
| 11269 | else if (doc->type == XML_HTML_DOCUMENT_NODE) |
| 11270 | ctxt = htmlCreateMemoryParserCtxt((char *) data, datalen); |
| 11271 | #endif |
| 11272 | else |
| 11273 | return(XML_ERR_INTERNAL_ERROR); |
| 11274 | |
| 11275 | if (ctxt == NULL) |
| 11276 | return(XML_ERR_NO_MEMORY); |
| 11277 | fake = xmlNewComment(NULL); |
| 11278 | if (fake == NULL) { |
| 11279 | xmlFreeParserCtxt(ctxt); |
| 11280 | return(XML_ERR_NO_MEMORY); |
| 11281 | } |
| 11282 | xmlAddChild(node, fake); |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 11283 | |
| 11284 | /* |
| 11285 | * Use input doc's dict if present, else assure XML_PARSE_NODICT is set. |
| 11286 | * We need a dictionary for xmlDetectSAX2, so if there's no doc dict |
| 11287 | * we must wait until the last moment to free the original one. |
| 11288 | */ |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 11289 | if (doc->dict != NULL) { |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 11290 | if (ctxt->dict != NULL) |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 11291 | xmlDictFree(ctxt->dict); |
| 11292 | ctxt->dict = doc->dict; |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 11293 | } else |
| 11294 | options |= XML_PARSE_NODICT; |
| 11295 | |
| 11296 | xmlCtxtUseOptions(ctxt, options); |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 11297 | xmlDetectSAX2(ctxt); |
| 11298 | ctxt->myDoc = doc; |
| 11299 | |
| 11300 | if (node->type == XML_ELEMENT_NODE) { |
| 11301 | nodePush(ctxt, node); |
| 11302 | /* |
| 11303 | * initialize the SAX2 namespaces stack |
| 11304 | */ |
| 11305 | cur = node; |
| 11306 | while ((cur != NULL) && (cur->type == XML_ELEMENT_NODE)) { |
| 11307 | xmlNsPtr ns = cur->nsDef; |
| 11308 | const xmlChar *iprefix, *ihref; |
| 11309 | |
| 11310 | while (ns != NULL) { |
| 11311 | if (ctxt->dict) { |
| 11312 | iprefix = xmlDictLookup(ctxt->dict, ns->prefix, -1); |
| 11313 | ihref = xmlDictLookup(ctxt->dict, ns->href, -1); |
| 11314 | } else { |
| 11315 | iprefix = ns->prefix; |
| 11316 | ihref = ns->href; |
| 11317 | } |
| 11318 | |
| 11319 | if (xmlGetNamespace(ctxt, iprefix) == NULL) { |
| 11320 | nsPush(ctxt, iprefix, ihref); |
| 11321 | nsnr++; |
| 11322 | } |
| 11323 | ns = ns->next; |
| 11324 | } |
| 11325 | cur = cur->parent; |
| 11326 | } |
| 11327 | ctxt->instate = XML_PARSER_CONTENT; |
| 11328 | } |
| 11329 | |
| 11330 | if ((ctxt->validate) || (ctxt->replaceEntities != 0)) { |
| 11331 | /* |
| 11332 | * ID/IDREF registration will be done in xmlValidateElement below |
| 11333 | */ |
| 11334 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 11335 | } |
| 11336 | |
| 11337 | xmlParseContent(ctxt); |
| 11338 | nsPop(ctxt, nsnr); |
| 11339 | if ((RAW == '<') && (NXT(1) == '/')) { |
| 11340 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 11341 | } else if (RAW != 0) { |
| 11342 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
| 11343 | } |
| 11344 | if ((ctxt->node != NULL) && (ctxt->node != node)) { |
| 11345 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 11346 | ctxt->wellFormed = 0; |
| 11347 | } |
| 11348 | |
| 11349 | if (!ctxt->wellFormed) { |
| 11350 | if (ctxt->errNo == 0) |
| 11351 | ret = XML_ERR_INTERNAL_ERROR; |
| 11352 | else |
| 11353 | ret = (xmlParserErrors)ctxt->errNo; |
| 11354 | } else { |
| 11355 | ret = XML_ERR_OK; |
| 11356 | } |
| 11357 | |
| 11358 | /* |
| 11359 | * Return the newly created nodeset after unlinking it from |
| 11360 | * the pseudo sibling. |
| 11361 | */ |
| 11362 | |
| 11363 | cur = fake->next; |
| 11364 | fake->next = NULL; |
| 11365 | node->last = fake; |
| 11366 | |
| 11367 | if (cur != NULL) { |
| 11368 | cur->prev = NULL; |
| 11369 | } |
| 11370 | |
| 11371 | *lst = cur; |
| 11372 | |
| 11373 | while (cur != NULL) { |
| 11374 | cur->parent = NULL; |
| 11375 | cur = cur->next; |
| 11376 | } |
| 11377 | |
| 11378 | xmlUnlinkNode(fake); |
| 11379 | xmlFreeNode(fake); |
| 11380 | |
| 11381 | |
| 11382 | if (ret != XML_ERR_OK) { |
| 11383 | xmlFreeNodeList(*lst); |
| 11384 | *lst = NULL; |
| 11385 | } |
William M. Brack | c3f8134 | 2004-10-03 01:22:44 +0000 | [diff] [blame] | 11386 | |
William M. Brack | b7b54de | 2004-10-06 16:38:01 +0000 | [diff] [blame] | 11387 | if (doc->dict != NULL) |
| 11388 | ctxt->dict = NULL; |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 11389 | xmlFreeParserCtxt(ctxt); |
| 11390 | |
| 11391 | return(ret); |
| 11392 | #else /* !SAX2 */ |
| 11393 | return(XML_ERR_INTERNAL_ERROR); |
| 11394 | #endif |
| 11395 | } |
| 11396 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11397 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11398 | /** |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 11399 | * xmlParseBalancedChunkMemoryRecover: |
| 11400 | * @doc: the document the chunk pertains to |
| 11401 | * @sax: the SAX handler bloc (possibly NULL) |
| 11402 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 11403 | * @depth: Used for loop detection, use 0 |
| 11404 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 11405 | * @lst: the return value for the set of parsed nodes |
| 11406 | * @recover: return nodes even if the data is broken (use 0) |
| 11407 | * |
| 11408 | * |
| 11409 | * Parse a well-balanced chunk of an XML document |
| 11410 | * called by the parser |
| 11411 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 11412 | * the content production in the XML grammar: |
| 11413 | * |
| 11414 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 11415 | * |
| 11416 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 11417 | * the parser error code otherwise |
| 11418 | * |
| 11419 | * In case recover is set to 1, the nodelist will not be empty even if |
| 11420 | * the parsed chunk is not well balanced. |
| 11421 | */ |
| 11422 | int |
| 11423 | xmlParseBalancedChunkMemoryRecover(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
| 11424 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst, |
| 11425 | int recover) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11426 | xmlParserCtxtPtr ctxt; |
| 11427 | xmlDocPtr newDoc; |
| 11428 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 11429 | xmlNodePtr content, newRoot; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11430 | int size; |
| 11431 | int ret = 0; |
| 11432 | |
| 11433 | if (depth > 40) { |
| 11434 | return(XML_ERR_ENTITY_LOOP); |
| 11435 | } |
| 11436 | |
| 11437 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 11438 | if (lst != NULL) |
| 11439 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11440 | if (string == NULL) |
| 11441 | return(-1); |
| 11442 | |
| 11443 | size = xmlStrlen(string); |
| 11444 | |
| 11445 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
| 11446 | if (ctxt == NULL) return(-1); |
| 11447 | ctxt->userData = ctxt; |
| 11448 | if (sax != NULL) { |
| 11449 | oldsax = ctxt->sax; |
| 11450 | ctxt->sax = sax; |
| 11451 | if (user_data != NULL) |
| 11452 | ctxt->userData = user_data; |
| 11453 | } |
| 11454 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 11455 | if (newDoc == NULL) { |
| 11456 | xmlFreeParserCtxt(ctxt); |
| 11457 | return(-1); |
| 11458 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 11459 | if ((doc != NULL) && (doc->dict != NULL)) { |
| 11460 | xmlDictFree(ctxt->dict); |
| 11461 | ctxt->dict = doc->dict; |
| 11462 | xmlDictReference(ctxt->dict); |
| 11463 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 11464 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 11465 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
| 11466 | ctxt->dictNames = 1; |
| 11467 | } else { |
| 11468 | xmlCtxtUseOptions(ctxt, XML_PARSE_NODICT); |
| 11469 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11470 | if (doc != NULL) { |
| 11471 | newDoc->intSubset = doc->intSubset; |
| 11472 | newDoc->extSubset = doc->extSubset; |
| 11473 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 11474 | newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 11475 | if (newRoot == NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11476 | if (sax != NULL) |
| 11477 | ctxt->sax = oldsax; |
| 11478 | xmlFreeParserCtxt(ctxt); |
| 11479 | newDoc->intSubset = NULL; |
| 11480 | newDoc->extSubset = NULL; |
| 11481 | xmlFreeDoc(newDoc); |
| 11482 | return(-1); |
| 11483 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 11484 | xmlAddChild((xmlNodePtr) newDoc, newRoot); |
| 11485 | nodePush(ctxt, newRoot); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11486 | if (doc == NULL) { |
| 11487 | ctxt->myDoc = newDoc; |
| 11488 | } else { |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11489 | ctxt->myDoc = newDoc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11490 | newDoc->children->doc = doc; |
| 11491 | } |
| 11492 | ctxt->instate = XML_PARSER_CONTENT; |
| 11493 | ctxt->depth = depth; |
| 11494 | |
| 11495 | /* |
| 11496 | * Doing validity checking on chunk doesn't make sense |
| 11497 | */ |
| 11498 | ctxt->validate = 0; |
| 11499 | ctxt->loadsubset = 0; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11500 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11501 | |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 11502 | if ( doc != NULL ){ |
| 11503 | content = doc->children; |
| 11504 | doc->children = NULL; |
| 11505 | xmlParseContent(ctxt); |
| 11506 | doc->children = content; |
| 11507 | } |
| 11508 | else { |
| 11509 | xmlParseContent(ctxt); |
| 11510 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11511 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11512 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11513 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11514 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11515 | } |
| 11516 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11517 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11518 | } |
| 11519 | |
| 11520 | if (!ctxt->wellFormed) { |
| 11521 | if (ctxt->errNo == 0) |
| 11522 | ret = 1; |
| 11523 | else |
| 11524 | ret = ctxt->errNo; |
| 11525 | } else { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 11526 | ret = 0; |
| 11527 | } |
| 11528 | |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 11529 | if ((lst != NULL) && ((ret == 0) || (recover == 1))) { |
| 11530 | xmlNodePtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11531 | |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 11532 | /* |
| 11533 | * Return the newly created nodeset after unlinking it from |
| 11534 | * they pseudo parent. |
| 11535 | */ |
| 11536 | cur = newDoc->children->children; |
| 11537 | *lst = cur; |
| 11538 | while (cur != NULL) { |
| 11539 | xmlSetTreeDoc(cur, doc); |
| 11540 | cur->parent = NULL; |
| 11541 | cur = cur->next; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11542 | } |
Daniel Veillard | acbe6cf | 2004-10-31 21:04:50 +0000 | [diff] [blame] | 11543 | newDoc->children->children = NULL; |
| 11544 | } |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 11545 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11546 | if (sax != NULL) |
| 11547 | ctxt->sax = oldsax; |
| 11548 | xmlFreeParserCtxt(ctxt); |
| 11549 | newDoc->intSubset = NULL; |
| 11550 | newDoc->extSubset = NULL; |
| 11551 | xmlFreeDoc(newDoc); |
| 11552 | |
| 11553 | return(ret); |
| 11554 | } |
| 11555 | |
| 11556 | /** |
| 11557 | * xmlSAXParseEntity: |
| 11558 | * @sax: the SAX handler block |
| 11559 | * @filename: the filename |
| 11560 | * |
| 11561 | * parse an XML external entity out of context and build a tree. |
| 11562 | * It use the given SAX function block to handle the parsing callback. |
| 11563 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 11564 | * |
| 11565 | * [78] extParsedEnt ::= TextDecl? content |
| 11566 | * |
| 11567 | * This correspond to a "Well Balanced" chunk |
| 11568 | * |
| 11569 | * Returns the resulting document tree |
| 11570 | */ |
| 11571 | |
| 11572 | xmlDocPtr |
| 11573 | xmlSAXParseEntity(xmlSAXHandlerPtr sax, const char *filename) { |
| 11574 | xmlDocPtr ret; |
| 11575 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11576 | |
| 11577 | ctxt = xmlCreateFileParserCtxt(filename); |
| 11578 | if (ctxt == NULL) { |
| 11579 | return(NULL); |
| 11580 | } |
| 11581 | if (sax != NULL) { |
| 11582 | if (ctxt->sax != NULL) |
| 11583 | xmlFree(ctxt->sax); |
| 11584 | ctxt->sax = sax; |
| 11585 | ctxt->userData = NULL; |
| 11586 | } |
| 11587 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11588 | xmlParseExtParsedEnt(ctxt); |
| 11589 | |
| 11590 | if (ctxt->wellFormed) |
| 11591 | ret = ctxt->myDoc; |
| 11592 | else { |
| 11593 | ret = NULL; |
| 11594 | xmlFreeDoc(ctxt->myDoc); |
| 11595 | ctxt->myDoc = NULL; |
| 11596 | } |
| 11597 | if (sax != NULL) |
| 11598 | ctxt->sax = NULL; |
| 11599 | xmlFreeParserCtxt(ctxt); |
| 11600 | |
| 11601 | return(ret); |
| 11602 | } |
| 11603 | |
| 11604 | /** |
| 11605 | * xmlParseEntity: |
| 11606 | * @filename: the filename |
| 11607 | * |
| 11608 | * parse an XML external entity out of context and build a tree. |
| 11609 | * |
| 11610 | * [78] extParsedEnt ::= TextDecl? content |
| 11611 | * |
| 11612 | * This correspond to a "Well Balanced" chunk |
| 11613 | * |
| 11614 | * Returns the resulting document tree |
| 11615 | */ |
| 11616 | |
| 11617 | xmlDocPtr |
| 11618 | xmlParseEntity(const char *filename) { |
| 11619 | return(xmlSAXParseEntity(NULL, filename)); |
| 11620 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11621 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11622 | |
| 11623 | /** |
| 11624 | * xmlCreateEntityParserCtxt: |
| 11625 | * @URL: the entity URL |
| 11626 | * @ID: the entity PUBLIC ID |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11627 | * @base: a possible base for the target URI |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11628 | * |
| 11629 | * Create a parser context for an external entity |
| 11630 | * Automatic support for ZLIB/Compress compressed document is provided |
| 11631 | * by default if found at compile-time. |
| 11632 | * |
| 11633 | * Returns the new parser context or NULL |
| 11634 | */ |
| 11635 | xmlParserCtxtPtr |
| 11636 | xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID, |
| 11637 | const xmlChar *base) { |
| 11638 | xmlParserCtxtPtr ctxt; |
| 11639 | xmlParserInputPtr inputStream; |
| 11640 | char *directory = NULL; |
| 11641 | xmlChar *uri; |
| 11642 | |
| 11643 | ctxt = xmlNewParserCtxt(); |
| 11644 | if (ctxt == NULL) { |
| 11645 | return(NULL); |
| 11646 | } |
| 11647 | |
| 11648 | uri = xmlBuildURI(URL, base); |
| 11649 | |
| 11650 | if (uri == NULL) { |
| 11651 | inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt); |
| 11652 | if (inputStream == NULL) { |
| 11653 | xmlFreeParserCtxt(ctxt); |
| 11654 | return(NULL); |
| 11655 | } |
| 11656 | |
| 11657 | inputPush(ctxt, inputStream); |
| 11658 | |
| 11659 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 11660 | directory = xmlParserGetDirectory((char *)URL); |
| 11661 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 11662 | ctxt->directory = directory; |
| 11663 | } else { |
| 11664 | inputStream = xmlLoadExternalEntity((char *)uri, (char *)ID, ctxt); |
| 11665 | if (inputStream == NULL) { |
| 11666 | xmlFree(uri); |
| 11667 | xmlFreeParserCtxt(ctxt); |
| 11668 | return(NULL); |
| 11669 | } |
| 11670 | |
| 11671 | inputPush(ctxt, inputStream); |
| 11672 | |
| 11673 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 11674 | directory = xmlParserGetDirectory((char *)uri); |
| 11675 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 11676 | ctxt->directory = directory; |
| 11677 | xmlFree(uri); |
| 11678 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11679 | return(ctxt); |
| 11680 | } |
| 11681 | |
| 11682 | /************************************************************************ |
| 11683 | * * |
| 11684 | * Front ends when parsing from a file * |
| 11685 | * * |
| 11686 | ************************************************************************/ |
| 11687 | |
| 11688 | /** |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 11689 | * xmlCreateURLParserCtxt: |
| 11690 | * @filename: the filename or URL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 11691 | * @options: a combination of xmlParserOption |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11692 | * |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 11693 | * Create a parser context for a file or URL content. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11694 | * Automatic support for ZLIB/Compress compressed document is provided |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 11695 | * by default if found at compile-time and for file accesses |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11696 | * |
| 11697 | * Returns the new parser context or NULL |
| 11698 | */ |
| 11699 | xmlParserCtxtPtr |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 11700 | xmlCreateURLParserCtxt(const char *filename, int options) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11701 | { |
| 11702 | xmlParserCtxtPtr ctxt; |
| 11703 | xmlParserInputPtr inputStream; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11704 | char *directory = NULL; |
| 11705 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11706 | ctxt = xmlNewParserCtxt(); |
| 11707 | if (ctxt == NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11708 | xmlErrMemory(NULL, "cannot allocate parser context"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11709 | return(NULL); |
| 11710 | } |
| 11711 | |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 11712 | if (options) |
| 11713 | xmlCtxtUseOptions(ctxt, options); |
| 11714 | ctxt->linenumbers = 1; |
Igor Zlatkovic | ce07616 | 2003-02-23 13:39:39 +0000 | [diff] [blame] | 11715 | |
Daniel Veillard | 4e9b1bc | 2003-06-09 10:30:33 +0000 | [diff] [blame] | 11716 | inputStream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11717 | if (inputStream == NULL) { |
| 11718 | xmlFreeParserCtxt(ctxt); |
| 11719 | return(NULL); |
| 11720 | } |
| 11721 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11722 | inputPush(ctxt, inputStream); |
| 11723 | if ((ctxt->directory == NULL) && (directory == NULL)) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 11724 | directory = xmlParserGetDirectory(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11725 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 11726 | ctxt->directory = directory; |
| 11727 | |
| 11728 | return(ctxt); |
| 11729 | } |
| 11730 | |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 11731 | /** |
| 11732 | * xmlCreateFileParserCtxt: |
| 11733 | * @filename: the filename |
| 11734 | * |
| 11735 | * Create a parser context for a file content. |
| 11736 | * Automatic support for ZLIB/Compress compressed document is provided |
| 11737 | * by default if found at compile-time. |
| 11738 | * |
| 11739 | * Returns the new parser context or NULL |
| 11740 | */ |
| 11741 | xmlParserCtxtPtr |
| 11742 | xmlCreateFileParserCtxt(const char *filename) |
| 11743 | { |
| 11744 | return(xmlCreateURLParserCtxt(filename, 0)); |
| 11745 | } |
| 11746 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11747 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11748 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11749 | * xmlSAXParseFileWithData: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11750 | * @sax: the SAX handler block |
| 11751 | * @filename: the filename |
| 11752 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 11753 | * documents |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11754 | * @data: the userdata |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11755 | * |
| 11756 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 11757 | * compressed document is provided by default if found at compile-time. |
| 11758 | * It use the given SAX function block to handle the parsing callback. |
| 11759 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 11760 | * |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 11761 | * User data (void *) is stored within the parser context in the |
| 11762 | * context's _private member, so it is available nearly everywhere in libxml |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11763 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11764 | * Returns the resulting document tree |
| 11765 | */ |
| 11766 | |
| 11767 | xmlDocPtr |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11768 | xmlSAXParseFileWithData(xmlSAXHandlerPtr sax, const char *filename, |
| 11769 | int recovery, void *data) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11770 | xmlDocPtr ret; |
| 11771 | xmlParserCtxtPtr ctxt; |
| 11772 | char *directory = NULL; |
| 11773 | |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 11774 | xmlInitParser(); |
| 11775 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11776 | ctxt = xmlCreateFileParserCtxt(filename); |
| 11777 | if (ctxt == NULL) { |
| 11778 | return(NULL); |
| 11779 | } |
| 11780 | if (sax != NULL) { |
| 11781 | if (ctxt->sax != NULL) |
| 11782 | xmlFree(ctxt->sax); |
| 11783 | ctxt->sax = sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11784 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11785 | xmlDetectSAX2(ctxt); |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11786 | if (data!=NULL) { |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 11787 | ctxt->_private = data; |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11788 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11789 | |
| 11790 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 11791 | directory = xmlParserGetDirectory(filename); |
| 11792 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 11793 | ctxt->directory = (char *) xmlStrdup((xmlChar *) directory); |
| 11794 | |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 11795 | ctxt->recovery = recovery; |
| 11796 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11797 | xmlParseDocument(ctxt); |
| 11798 | |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 11799 | if ((ctxt->wellFormed) || recovery) { |
| 11800 | ret = ctxt->myDoc; |
Daniel Veillard | b65e12e | 2003-10-08 21:33:28 +0000 | [diff] [blame] | 11801 | if (ret != NULL) { |
| 11802 | if (ctxt->input->buf->compressed > 0) |
| 11803 | ret->compression = 9; |
| 11804 | else |
| 11805 | ret->compression = ctxt->input->buf->compressed; |
| 11806 | } |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 11807 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11808 | else { |
| 11809 | ret = NULL; |
| 11810 | xmlFreeDoc(ctxt->myDoc); |
| 11811 | ctxt->myDoc = NULL; |
| 11812 | } |
| 11813 | if (sax != NULL) |
| 11814 | ctxt->sax = NULL; |
| 11815 | xmlFreeParserCtxt(ctxt); |
| 11816 | |
| 11817 | return(ret); |
| 11818 | } |
| 11819 | |
| 11820 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11821 | * xmlSAXParseFile: |
| 11822 | * @sax: the SAX handler block |
| 11823 | * @filename: the filename |
| 11824 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 11825 | * documents |
| 11826 | * |
| 11827 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 11828 | * compressed document is provided by default if found at compile-time. |
| 11829 | * It use the given SAX function block to handle the parsing callback. |
| 11830 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 11831 | * |
| 11832 | * Returns the resulting document tree |
| 11833 | */ |
| 11834 | |
| 11835 | xmlDocPtr |
| 11836 | xmlSAXParseFile(xmlSAXHandlerPtr sax, const char *filename, |
| 11837 | int recovery) { |
| 11838 | return(xmlSAXParseFileWithData(sax,filename,recovery,NULL)); |
| 11839 | } |
| 11840 | |
| 11841 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11842 | * xmlRecoverDoc: |
| 11843 | * @cur: a pointer to an array of xmlChar |
| 11844 | * |
| 11845 | * parse an XML in-memory document and build a tree. |
| 11846 | * In the case the document is not Well Formed, a tree is built anyway |
| 11847 | * |
| 11848 | * Returns the resulting document tree |
| 11849 | */ |
| 11850 | |
| 11851 | xmlDocPtr |
| 11852 | xmlRecoverDoc(xmlChar *cur) { |
| 11853 | return(xmlSAXParseDoc(NULL, cur, 1)); |
| 11854 | } |
| 11855 | |
| 11856 | /** |
| 11857 | * xmlParseFile: |
| 11858 | * @filename: the filename |
| 11859 | * |
| 11860 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 11861 | * compressed document is provided by default if found at compile-time. |
| 11862 | * |
Daniel Veillard | 5d96fff | 2001-08-31 14:55:30 +0000 | [diff] [blame] | 11863 | * Returns the resulting document tree if the file was wellformed, |
| 11864 | * NULL otherwise. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11865 | */ |
| 11866 | |
| 11867 | xmlDocPtr |
| 11868 | xmlParseFile(const char *filename) { |
| 11869 | return(xmlSAXParseFile(NULL, filename, 0)); |
| 11870 | } |
| 11871 | |
| 11872 | /** |
| 11873 | * xmlRecoverFile: |
| 11874 | * @filename: the filename |
| 11875 | * |
| 11876 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 11877 | * compressed document is provided by default if found at compile-time. |
| 11878 | * In the case the document is not Well Formed, a tree is built anyway |
| 11879 | * |
| 11880 | * Returns the resulting document tree |
| 11881 | */ |
| 11882 | |
| 11883 | xmlDocPtr |
| 11884 | xmlRecoverFile(const char *filename) { |
| 11885 | return(xmlSAXParseFile(NULL, filename, 1)); |
| 11886 | } |
| 11887 | |
| 11888 | |
| 11889 | /** |
| 11890 | * xmlSetupParserForBuffer: |
| 11891 | * @ctxt: an XML parser context |
| 11892 | * @buffer: a xmlChar * buffer |
| 11893 | * @filename: a file name |
| 11894 | * |
| 11895 | * Setup the parser context to parse a new buffer; Clears any prior |
| 11896 | * contents from the parser context. The buffer parameter must not be |
| 11897 | * NULL, but the filename parameter can be |
| 11898 | */ |
| 11899 | void |
| 11900 | xmlSetupParserForBuffer(xmlParserCtxtPtr ctxt, const xmlChar* buffer, |
| 11901 | const char* filename) |
| 11902 | { |
| 11903 | xmlParserInputPtr input; |
| 11904 | |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 11905 | if ((ctxt == NULL) || (buffer == NULL)) |
| 11906 | return; |
| 11907 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11908 | input = xmlNewInputStream(ctxt); |
| 11909 | if (input == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 11910 | xmlErrMemory(NULL, "parsing new buffer: out of memory\n"); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 11911 | xmlClearParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11912 | return; |
| 11913 | } |
| 11914 | |
| 11915 | xmlClearParserCtxt(ctxt); |
| 11916 | if (filename != NULL) |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 11917 | input->filename = (char *) xmlCanonicPath((const xmlChar *)filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11918 | input->base = buffer; |
| 11919 | input->cur = buffer; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 11920 | input->end = &buffer[xmlStrlen(buffer)]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11921 | inputPush(ctxt, input); |
| 11922 | } |
| 11923 | |
| 11924 | /** |
| 11925 | * xmlSAXUserParseFile: |
| 11926 | * @sax: a SAX handler |
| 11927 | * @user_data: The user data returned on SAX callbacks |
| 11928 | * @filename: a file name |
| 11929 | * |
| 11930 | * parse an XML file and call the given SAX handler routines. |
| 11931 | * Automatic support for ZLIB/Compress compressed document is provided |
| 11932 | * |
| 11933 | * Returns 0 in case of success or a error number otherwise |
| 11934 | */ |
| 11935 | int |
| 11936 | xmlSAXUserParseFile(xmlSAXHandlerPtr sax, void *user_data, |
| 11937 | const char *filename) { |
| 11938 | int ret = 0; |
| 11939 | xmlParserCtxtPtr ctxt; |
| 11940 | |
| 11941 | ctxt = xmlCreateFileParserCtxt(filename); |
| 11942 | if (ctxt == NULL) return -1; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11943 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 11944 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11945 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11946 | xmlFree(ctxt->sax); |
| 11947 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11948 | xmlDetectSAX2(ctxt); |
| 11949 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11950 | if (user_data != NULL) |
| 11951 | ctxt->userData = user_data; |
| 11952 | |
| 11953 | xmlParseDocument(ctxt); |
| 11954 | |
| 11955 | if (ctxt->wellFormed) |
| 11956 | ret = 0; |
| 11957 | else { |
| 11958 | if (ctxt->errNo != 0) |
| 11959 | ret = ctxt->errNo; |
| 11960 | else |
| 11961 | ret = -1; |
| 11962 | } |
| 11963 | if (sax != NULL) |
| 11964 | ctxt->sax = NULL; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 11965 | if (ctxt->myDoc != NULL) { |
| 11966 | xmlFreeDoc(ctxt->myDoc); |
| 11967 | ctxt->myDoc = NULL; |
| 11968 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11969 | xmlFreeParserCtxt(ctxt); |
| 11970 | |
| 11971 | return ret; |
| 11972 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11973 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11974 | |
| 11975 | /************************************************************************ |
| 11976 | * * |
| 11977 | * Front ends when parsing from memory * |
| 11978 | * * |
| 11979 | ************************************************************************/ |
| 11980 | |
| 11981 | /** |
| 11982 | * xmlCreateMemoryParserCtxt: |
| 11983 | * @buffer: a pointer to a char array |
| 11984 | * @size: the size of the array |
| 11985 | * |
| 11986 | * Create a parser context for an XML in-memory document. |
| 11987 | * |
| 11988 | * Returns the new parser context or NULL |
| 11989 | */ |
| 11990 | xmlParserCtxtPtr |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 11991 | xmlCreateMemoryParserCtxt(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11992 | xmlParserCtxtPtr ctxt; |
| 11993 | xmlParserInputPtr input; |
| 11994 | xmlParserInputBufferPtr buf; |
| 11995 | |
| 11996 | if (buffer == NULL) |
| 11997 | return(NULL); |
| 11998 | if (size <= 0) |
| 11999 | return(NULL); |
| 12000 | |
| 12001 | ctxt = xmlNewParserCtxt(); |
| 12002 | if (ctxt == NULL) |
| 12003 | return(NULL); |
| 12004 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 12005 | /* TODO: xmlParserInputBufferCreateStatic, requires some serious changes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12006 | buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 12007 | if (buf == NULL) { |
| 12008 | xmlFreeParserCtxt(ctxt); |
| 12009 | return(NULL); |
| 12010 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12011 | |
| 12012 | input = xmlNewInputStream(ctxt); |
| 12013 | if (input == NULL) { |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 12014 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12015 | xmlFreeParserCtxt(ctxt); |
| 12016 | return(NULL); |
| 12017 | } |
| 12018 | |
| 12019 | input->filename = NULL; |
| 12020 | input->buf = buf; |
| 12021 | input->base = input->buf->buffer->content; |
| 12022 | input->cur = input->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 12023 | input->end = &input->buf->buffer->content[input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12024 | |
| 12025 | inputPush(ctxt, input); |
| 12026 | return(ctxt); |
| 12027 | } |
| 12028 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12029 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12030 | /** |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 12031 | * xmlSAXParseMemoryWithData: |
| 12032 | * @sax: the SAX handler block |
| 12033 | * @buffer: an pointer to a char array |
| 12034 | * @size: the size of the array |
| 12035 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 12036 | * documents |
| 12037 | * @data: the userdata |
| 12038 | * |
| 12039 | * parse an XML in-memory block and use the given SAX function block |
| 12040 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 12041 | * DOM tree building routines. |
| 12042 | * |
| 12043 | * User data (void *) is stored within the parser context in the |
| 12044 | * context's _private member, so it is available nearly everywhere in libxml |
| 12045 | * |
| 12046 | * Returns the resulting document tree |
| 12047 | */ |
| 12048 | |
| 12049 | xmlDocPtr |
| 12050 | xmlSAXParseMemoryWithData(xmlSAXHandlerPtr sax, const char *buffer, |
| 12051 | int size, int recovery, void *data) { |
| 12052 | xmlDocPtr ret; |
| 12053 | xmlParserCtxtPtr ctxt; |
| 12054 | |
| 12055 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 12056 | if (ctxt == NULL) return(NULL); |
| 12057 | if (sax != NULL) { |
| 12058 | if (ctxt->sax != NULL) |
| 12059 | xmlFree(ctxt->sax); |
| 12060 | ctxt->sax = sax; |
| 12061 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12062 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 12063 | if (data!=NULL) { |
| 12064 | ctxt->_private=data; |
| 12065 | } |
| 12066 | |
Daniel Veillard | adba5f1 | 2003-04-04 16:09:01 +0000 | [diff] [blame] | 12067 | ctxt->recovery = recovery; |
| 12068 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 12069 | xmlParseDocument(ctxt); |
| 12070 | |
| 12071 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 12072 | else { |
| 12073 | ret = NULL; |
| 12074 | xmlFreeDoc(ctxt->myDoc); |
| 12075 | ctxt->myDoc = NULL; |
| 12076 | } |
| 12077 | if (sax != NULL) |
| 12078 | ctxt->sax = NULL; |
| 12079 | xmlFreeParserCtxt(ctxt); |
| 12080 | |
| 12081 | return(ret); |
| 12082 | } |
| 12083 | |
| 12084 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12085 | * xmlSAXParseMemory: |
| 12086 | * @sax: the SAX handler block |
| 12087 | * @buffer: an pointer to a char array |
| 12088 | * @size: the size of the array |
| 12089 | * @recovery: work in recovery mode, i.e. tries to read not Well Formed |
| 12090 | * documents |
| 12091 | * |
| 12092 | * parse an XML in-memory block and use the given SAX function block |
| 12093 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 12094 | * DOM tree building routines. |
| 12095 | * |
| 12096 | * Returns the resulting document tree |
| 12097 | */ |
| 12098 | xmlDocPtr |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 12099 | xmlSAXParseMemory(xmlSAXHandlerPtr sax, const char *buffer, |
| 12100 | int size, int recovery) { |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 12101 | return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12102 | } |
| 12103 | |
| 12104 | /** |
| 12105 | * xmlParseMemory: |
| 12106 | * @buffer: an pointer to a char array |
| 12107 | * @size: the size of the array |
| 12108 | * |
| 12109 | * parse an XML in-memory block and build a tree. |
| 12110 | * |
| 12111 | * Returns the resulting document tree |
| 12112 | */ |
| 12113 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 12114 | xmlDocPtr xmlParseMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12115 | return(xmlSAXParseMemory(NULL, buffer, size, 0)); |
| 12116 | } |
| 12117 | |
| 12118 | /** |
| 12119 | * xmlRecoverMemory: |
| 12120 | * @buffer: an pointer to a char array |
| 12121 | * @size: the size of the array |
| 12122 | * |
| 12123 | * parse an XML in-memory block and build a tree. |
| 12124 | * In the case the document is not Well Formed, a tree is built anyway |
| 12125 | * |
| 12126 | * Returns the resulting document tree |
| 12127 | */ |
| 12128 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 12129 | xmlDocPtr xmlRecoverMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12130 | return(xmlSAXParseMemory(NULL, buffer, size, 1)); |
| 12131 | } |
| 12132 | |
| 12133 | /** |
| 12134 | * xmlSAXUserParseMemory: |
| 12135 | * @sax: a SAX handler |
| 12136 | * @user_data: The user data returned on SAX callbacks |
| 12137 | * @buffer: an in-memory XML document input |
| 12138 | * @size: the length of the XML document in bytes |
| 12139 | * |
| 12140 | * A better SAX parsing routine. |
| 12141 | * parse an XML in-memory buffer and call the given SAX handler routines. |
| 12142 | * |
| 12143 | * Returns 0 in case of success or a error number otherwise |
| 12144 | */ |
| 12145 | int xmlSAXUserParseMemory(xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 12146 | const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12147 | int ret = 0; |
| 12148 | xmlParserCtxtPtr ctxt; |
| 12149 | xmlSAXHandlerPtr oldsax = NULL; |
| 12150 | |
Daniel Veillard | 9e92351 | 2002-08-14 08:48:52 +0000 | [diff] [blame] | 12151 | if (sax == NULL) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12152 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 12153 | if (ctxt == NULL) return -1; |
Daniel Veillard | 9e92351 | 2002-08-14 08:48:52 +0000 | [diff] [blame] | 12154 | oldsax = ctxt->sax; |
| 12155 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12156 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 30211a0 | 2001-04-26 09:33:18 +0000 | [diff] [blame] | 12157 | if (user_data != NULL) |
| 12158 | ctxt->userData = user_data; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12159 | |
| 12160 | xmlParseDocument(ctxt); |
| 12161 | |
| 12162 | if (ctxt->wellFormed) |
| 12163 | ret = 0; |
| 12164 | else { |
| 12165 | if (ctxt->errNo != 0) |
| 12166 | ret = ctxt->errNo; |
| 12167 | else |
| 12168 | ret = -1; |
| 12169 | } |
Daniel Veillard | 9e92351 | 2002-08-14 08:48:52 +0000 | [diff] [blame] | 12170 | ctxt->sax = oldsax; |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 12171 | if (ctxt->myDoc != NULL) { |
| 12172 | xmlFreeDoc(ctxt->myDoc); |
| 12173 | ctxt->myDoc = NULL; |
| 12174 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12175 | xmlFreeParserCtxt(ctxt); |
| 12176 | |
| 12177 | return ret; |
| 12178 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12179 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12180 | |
| 12181 | /** |
| 12182 | * xmlCreateDocParserCtxt: |
| 12183 | * @cur: a pointer to an array of xmlChar |
| 12184 | * |
| 12185 | * Creates a parser context for an XML in-memory document. |
| 12186 | * |
| 12187 | * Returns the new parser context or NULL |
| 12188 | */ |
| 12189 | xmlParserCtxtPtr |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12190 | xmlCreateDocParserCtxt(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12191 | int len; |
| 12192 | |
| 12193 | if (cur == NULL) |
| 12194 | return(NULL); |
| 12195 | len = xmlStrlen(cur); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12196 | return(xmlCreateMemoryParserCtxt((const char *)cur, len)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12197 | } |
| 12198 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12199 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12200 | /** |
| 12201 | * xmlSAXParseDoc: |
| 12202 | * @sax: the SAX handler block |
| 12203 | * @cur: a pointer to an array of xmlChar |
| 12204 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 12205 | * documents |
| 12206 | * |
| 12207 | * parse an XML in-memory document and build a tree. |
| 12208 | * It use the given SAX function block to handle the parsing callback. |
| 12209 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 12210 | * |
| 12211 | * Returns the resulting document tree |
| 12212 | */ |
| 12213 | |
| 12214 | xmlDocPtr |
Daniel Veillard | 7331e5c | 2005-03-31 14:59:00 +0000 | [diff] [blame] | 12215 | xmlSAXParseDoc(xmlSAXHandlerPtr sax, const xmlChar *cur, int recovery) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12216 | xmlDocPtr ret; |
| 12217 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 12218 | xmlSAXHandlerPtr oldsax = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12219 | |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 12220 | if (cur == NULL) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12221 | |
| 12222 | |
| 12223 | ctxt = xmlCreateDocParserCtxt(cur); |
| 12224 | if (ctxt == NULL) return(NULL); |
| 12225 | if (sax != NULL) { |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 12226 | oldsax = ctxt->sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12227 | ctxt->sax = sax; |
| 12228 | ctxt->userData = NULL; |
| 12229 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 12230 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12231 | |
| 12232 | xmlParseDocument(ctxt); |
| 12233 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 12234 | else { |
| 12235 | ret = NULL; |
| 12236 | xmlFreeDoc(ctxt->myDoc); |
| 12237 | ctxt->myDoc = NULL; |
| 12238 | } |
Daniel Veillard | 34099b4 | 2004-11-04 17:34:35 +0000 | [diff] [blame] | 12239 | if (sax != NULL) |
Daniel Veillard | 3893606 | 2004-11-04 17:45:11 +0000 | [diff] [blame] | 12240 | ctxt->sax = oldsax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12241 | xmlFreeParserCtxt(ctxt); |
| 12242 | |
| 12243 | return(ret); |
| 12244 | } |
| 12245 | |
| 12246 | /** |
| 12247 | * xmlParseDoc: |
| 12248 | * @cur: a pointer to an array of xmlChar |
| 12249 | * |
| 12250 | * parse an XML in-memory document and build a tree. |
| 12251 | * |
| 12252 | * Returns the resulting document tree |
| 12253 | */ |
| 12254 | |
| 12255 | xmlDocPtr |
Daniel Veillard | 7331e5c | 2005-03-31 14:59:00 +0000 | [diff] [blame] | 12256 | xmlParseDoc(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12257 | return(xmlSAXParseDoc(NULL, cur, 0)); |
| 12258 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12259 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12260 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12261 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 12262 | /************************************************************************ |
| 12263 | * * |
| 12264 | * Specific function to keep track of entities references * |
| 12265 | * and used by the XSLT debugger * |
| 12266 | * * |
| 12267 | ************************************************************************/ |
| 12268 | |
| 12269 | static xmlEntityReferenceFunc xmlEntityRefFunc = NULL; |
| 12270 | |
| 12271 | /** |
| 12272 | * xmlAddEntityReference: |
| 12273 | * @ent : A valid entity |
| 12274 | * @firstNode : A valid first node for children of entity |
| 12275 | * @lastNode : A valid last node of children entity |
| 12276 | * |
| 12277 | * Notify of a reference to an entity of type XML_EXTERNAL_GENERAL_PARSED_ENTITY |
| 12278 | */ |
| 12279 | static void |
| 12280 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 12281 | xmlNodePtr lastNode) |
| 12282 | { |
| 12283 | if (xmlEntityRefFunc != NULL) { |
| 12284 | (*xmlEntityRefFunc) (ent, firstNode, lastNode); |
| 12285 | } |
| 12286 | } |
| 12287 | |
| 12288 | |
| 12289 | /** |
| 12290 | * xmlSetEntityReferenceFunc: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 12291 | * @func: A valid function |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 12292 | * |
| 12293 | * Set the function to call call back when a xml reference has been made |
| 12294 | */ |
| 12295 | void |
| 12296 | xmlSetEntityReferenceFunc(xmlEntityReferenceFunc func) |
| 12297 | { |
| 12298 | xmlEntityRefFunc = func; |
| 12299 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12300 | #endif /* LIBXML_LEGACY_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12301 | |
| 12302 | /************************************************************************ |
| 12303 | * * |
| 12304 | * Miscellaneous * |
| 12305 | * * |
| 12306 | ************************************************************************/ |
| 12307 | |
| 12308 | #ifdef LIBXML_XPATH_ENABLED |
| 12309 | #include <libxml/xpath.h> |
| 12310 | #endif |
| 12311 | |
Daniel Veillard | db5850a | 2002-01-18 11:49:26 +0000 | [diff] [blame] | 12312 | extern void xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12313 | static int xmlParserInitialized = 0; |
| 12314 | |
| 12315 | /** |
| 12316 | * xmlInitParser: |
| 12317 | * |
| 12318 | * Initialization function for the XML parser. |
| 12319 | * This is not reentrant. Call once before processing in case of |
| 12320 | * use in multithreaded programs. |
| 12321 | */ |
| 12322 | |
| 12323 | void |
| 12324 | xmlInitParser(void) { |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 12325 | if (xmlParserInitialized != 0) |
| 12326 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12327 | |
Daniel Veillard | db5850a | 2002-01-18 11:49:26 +0000 | [diff] [blame] | 12328 | if ((xmlGenericError == xmlGenericErrorDefaultFunc) || |
| 12329 | (xmlGenericError == NULL)) |
| 12330 | initGenericErrorDefaultFunc(NULL); |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 12331 | xmlInitGlobals(); |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 12332 | xmlInitThreads(); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 12333 | xmlInitMemory(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12334 | xmlInitCharEncodingHandlers(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12335 | xmlDefaultSAXHandlerInit(); |
| 12336 | xmlRegisterDefaultInputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 12337 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12338 | xmlRegisterDefaultOutputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 12339 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12340 | #ifdef LIBXML_HTML_ENABLED |
| 12341 | htmlInitAutoClose(); |
| 12342 | htmlDefaultSAXHandlerInit(); |
| 12343 | #endif |
| 12344 | #ifdef LIBXML_XPATH_ENABLED |
| 12345 | xmlXPathInit(); |
| 12346 | #endif |
| 12347 | xmlParserInitialized = 1; |
| 12348 | } |
| 12349 | |
| 12350 | /** |
| 12351 | * xmlCleanupParser: |
| 12352 | * |
Daniel Veillard | d8cf906 | 2003-11-11 21:12:36 +0000 | [diff] [blame] | 12353 | * Cleanup function for the XML library. It tries to reclaim all |
| 12354 | * parsing related global memory allocated for the library processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12355 | * It doesn't deallocate any document related memory. Calling this |
Daniel Veillard | d8cf906 | 2003-11-11 21:12:36 +0000 | [diff] [blame] | 12356 | * function should not prevent reusing the library but one should |
| 12357 | * call xmlCleanupParser() only when the process has |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 12358 | * finished using the library or XML document built with it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12359 | */ |
| 12360 | |
| 12361 | void |
| 12362 | xmlCleanupParser(void) { |
Daniel Veillard | 7fb801f | 2003-08-17 21:07:26 +0000 | [diff] [blame] | 12363 | if (!xmlParserInitialized) |
| 12364 | return; |
| 12365 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12366 | xmlCleanupCharEncodingHandlers(); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 12367 | #ifdef LIBXML_CATALOG_ENABLED |
| 12368 | xmlCatalogCleanup(); |
| 12369 | #endif |
Daniel Veillard | 1441251 | 2005-01-21 23:53:26 +0000 | [diff] [blame] | 12370 | xmlDictCleanup(); |
Daniel Veillard | 04054be | 2003-10-15 10:48:54 +0000 | [diff] [blame] | 12371 | xmlCleanupInputCallbacks(); |
| 12372 | #ifdef LIBXML_OUTPUT_ENABLED |
| 12373 | xmlCleanupOutputCallbacks(); |
| 12374 | #endif |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12375 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 12376 | xmlSchemaCleanupTypes(); |
Daniel Veillard | dd6d300 | 2004-11-03 14:20:29 +0000 | [diff] [blame] | 12377 | xmlRelaxNGCleanupTypes(); |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12378 | #endif |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 12379 | xmlCleanupGlobals(); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 12380 | xmlResetLastError(); |
Daniel Veillard | 74c0e59 | 2003-11-25 07:01:38 +0000 | [diff] [blame] | 12381 | xmlCleanupThreads(); /* must be last if called not from the main thread */ |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 12382 | xmlCleanupMemory(); |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 12383 | xmlParserInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 12384 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12385 | |
| 12386 | /************************************************************************ |
| 12387 | * * |
| 12388 | * New set (2.6.0) of simpler and more flexible APIs * |
| 12389 | * * |
| 12390 | ************************************************************************/ |
| 12391 | |
| 12392 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12393 | * DICT_FREE: |
| 12394 | * @str: a string |
| 12395 | * |
| 12396 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 12397 | * current scope |
| 12398 | */ |
| 12399 | #define DICT_FREE(str) \ |
| 12400 | if ((str) && ((!dict) || \ |
| 12401 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 12402 | xmlFree((char *)(str)); |
| 12403 | |
| 12404 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12405 | * xmlCtxtReset: |
| 12406 | * @ctxt: an XML parser context |
| 12407 | * |
| 12408 | * Reset a parser context |
| 12409 | */ |
| 12410 | void |
| 12411 | xmlCtxtReset(xmlParserCtxtPtr ctxt) |
| 12412 | { |
| 12413 | xmlParserInputPtr input; |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12414 | xmlDictPtr dict; |
| 12415 | |
| 12416 | if (ctxt == NULL) |
| 12417 | return; |
| 12418 | |
| 12419 | dict = ctxt->dict; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12420 | |
| 12421 | while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */ |
| 12422 | xmlFreeInputStream(input); |
| 12423 | } |
| 12424 | ctxt->inputNr = 0; |
| 12425 | ctxt->input = NULL; |
| 12426 | |
| 12427 | ctxt->spaceNr = 0; |
| 12428 | ctxt->spaceTab[0] = -1; |
| 12429 | ctxt->space = &ctxt->spaceTab[0]; |
| 12430 | |
| 12431 | |
| 12432 | ctxt->nodeNr = 0; |
| 12433 | ctxt->node = NULL; |
| 12434 | |
| 12435 | ctxt->nameNr = 0; |
| 12436 | ctxt->name = NULL; |
| 12437 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12438 | DICT_FREE(ctxt->version); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12439 | ctxt->version = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12440 | DICT_FREE(ctxt->encoding); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12441 | ctxt->encoding = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12442 | DICT_FREE(ctxt->directory); |
| 12443 | ctxt->directory = NULL; |
| 12444 | DICT_FREE(ctxt->extSubURI); |
| 12445 | ctxt->extSubURI = NULL; |
| 12446 | DICT_FREE(ctxt->extSubSystem); |
| 12447 | ctxt->extSubSystem = NULL; |
| 12448 | if (ctxt->myDoc != NULL) |
| 12449 | xmlFreeDoc(ctxt->myDoc); |
| 12450 | ctxt->myDoc = NULL; |
| 12451 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12452 | ctxt->standalone = -1; |
| 12453 | ctxt->hasExternalSubset = 0; |
| 12454 | ctxt->hasPErefs = 0; |
| 12455 | ctxt->html = 0; |
| 12456 | ctxt->external = 0; |
| 12457 | ctxt->instate = XML_PARSER_START; |
| 12458 | ctxt->token = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12459 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12460 | ctxt->wellFormed = 1; |
| 12461 | ctxt->nsWellFormed = 1; |
Daniel Veillard | ae28918 | 2004-01-21 16:00:43 +0000 | [diff] [blame] | 12462 | ctxt->disableSAX = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12463 | ctxt->valid = 1; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 12464 | #if 0 |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12465 | ctxt->vctxt.userData = ctxt; |
| 12466 | ctxt->vctxt.error = xmlParserValidityError; |
| 12467 | ctxt->vctxt.warning = xmlParserValidityWarning; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 12468 | #endif |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12469 | ctxt->record_info = 0; |
| 12470 | ctxt->nbChars = 0; |
| 12471 | ctxt->checkIndex = 0; |
| 12472 | ctxt->inSubset = 0; |
| 12473 | ctxt->errNo = XML_ERR_OK; |
| 12474 | ctxt->depth = 0; |
| 12475 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 12476 | ctxt->catalogs = NULL; |
| 12477 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
| 12478 | |
| 12479 | if (ctxt->attsDefault != NULL) { |
| 12480 | xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree); |
| 12481 | ctxt->attsDefault = NULL; |
| 12482 | } |
| 12483 | if (ctxt->attsSpecial != NULL) { |
| 12484 | xmlHashFree(ctxt->attsSpecial, NULL); |
| 12485 | ctxt->attsSpecial = NULL; |
| 12486 | } |
| 12487 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12488 | #ifdef LIBXML_CATALOG_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12489 | if (ctxt->catalogs != NULL) |
| 12490 | xmlCatalogFreeLocal(ctxt->catalogs); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12491 | #endif |
Daniel Veillard | cc199e0 | 2003-10-24 21:11:48 +0000 | [diff] [blame] | 12492 | if (ctxt->lastError.code != XML_ERR_OK) |
| 12493 | xmlResetError(&ctxt->lastError); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12494 | } |
| 12495 | |
| 12496 | /** |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 12497 | * xmlCtxtResetPush: |
| 12498 | * @ctxt: an XML parser context |
| 12499 | * @chunk: a pointer to an array of chars |
| 12500 | * @size: number of chars in the array |
| 12501 | * @filename: an optional file name or URI |
| 12502 | * @encoding: the document encoding, or NULL |
| 12503 | * |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 12504 | * Reset a push parser context |
| 12505 | * |
| 12506 | * Returns 0 in case of success and 1 in case of error |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 12507 | */ |
| 12508 | int |
| 12509 | xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk, |
| 12510 | int size, const char *filename, const char *encoding) |
| 12511 | { |
| 12512 | xmlParserInputPtr inputStream; |
| 12513 | xmlParserInputBufferPtr buf; |
| 12514 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 12515 | |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 12516 | if (ctxt == NULL) |
| 12517 | return(1); |
| 12518 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 12519 | if ((encoding == NULL) && (chunk != NULL) && (size >= 4)) |
| 12520 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 12521 | |
| 12522 | buf = xmlAllocParserInputBuffer(enc); |
| 12523 | if (buf == NULL) |
| 12524 | return(1); |
| 12525 | |
| 12526 | if (ctxt == NULL) { |
| 12527 | xmlFreeParserInputBuffer(buf); |
| 12528 | return(1); |
| 12529 | } |
| 12530 | |
| 12531 | xmlCtxtReset(ctxt); |
| 12532 | |
| 12533 | if (ctxt->pushTab == NULL) { |
| 12534 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * |
| 12535 | sizeof(xmlChar *)); |
| 12536 | if (ctxt->pushTab == NULL) { |
| 12537 | xmlErrMemory(ctxt, NULL); |
| 12538 | xmlFreeParserInputBuffer(buf); |
| 12539 | return(1); |
| 12540 | } |
| 12541 | } |
| 12542 | |
| 12543 | if (filename == NULL) { |
| 12544 | ctxt->directory = NULL; |
| 12545 | } else { |
| 12546 | ctxt->directory = xmlParserGetDirectory(filename); |
| 12547 | } |
| 12548 | |
| 12549 | inputStream = xmlNewInputStream(ctxt); |
| 12550 | if (inputStream == NULL) { |
| 12551 | xmlFreeParserInputBuffer(buf); |
| 12552 | return(1); |
| 12553 | } |
| 12554 | |
| 12555 | if (filename == NULL) |
| 12556 | inputStream->filename = NULL; |
| 12557 | else |
| 12558 | inputStream->filename = (char *) |
| 12559 | xmlCanonicPath((const xmlChar *) filename); |
| 12560 | inputStream->buf = buf; |
| 12561 | inputStream->base = inputStream->buf->buffer->content; |
| 12562 | inputStream->cur = inputStream->buf->buffer->content; |
| 12563 | inputStream->end = |
| 12564 | &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; |
| 12565 | |
| 12566 | inputPush(ctxt, inputStream); |
| 12567 | |
| 12568 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 12569 | (ctxt->input->buf != NULL)) { |
| 12570 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 12571 | int cur = ctxt->input->cur - ctxt->input->base; |
| 12572 | |
| 12573 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
| 12574 | |
| 12575 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 12576 | ctxt->input->cur = ctxt->input->base + cur; |
| 12577 | ctxt->input->end = |
| 12578 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer-> |
| 12579 | use]; |
| 12580 | #ifdef DEBUG_PUSH |
| 12581 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 12582 | #endif |
| 12583 | } |
| 12584 | |
| 12585 | if (encoding != NULL) { |
| 12586 | xmlCharEncodingHandlerPtr hdlr; |
| 12587 | |
| 12588 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 12589 | if (hdlr != NULL) { |
| 12590 | xmlSwitchToEncoding(ctxt, hdlr); |
| 12591 | } else { |
| 12592 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 12593 | "Unsupported encoding %s\n", BAD_CAST encoding); |
| 12594 | } |
| 12595 | } else if (enc != XML_CHAR_ENCODING_NONE) { |
| 12596 | xmlSwitchEncoding(ctxt, enc); |
| 12597 | } |
| 12598 | |
| 12599 | return(0); |
| 12600 | } |
| 12601 | |
| 12602 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12603 | * xmlCtxtUseOptions: |
| 12604 | * @ctxt: an XML parser context |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12605 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12606 | * |
| 12607 | * Applies the options to the parser context |
| 12608 | * |
| 12609 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 12610 | * in case of error. |
| 12611 | */ |
| 12612 | int |
| 12613 | xmlCtxtUseOptions(xmlParserCtxtPtr ctxt, int options) |
| 12614 | { |
Daniel Veillard | 36e5cd5 | 2004-11-02 14:52:23 +0000 | [diff] [blame] | 12615 | if (ctxt == NULL) |
| 12616 | return(-1); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12617 | if (options & XML_PARSE_RECOVER) { |
| 12618 | ctxt->recovery = 1; |
| 12619 | options -= XML_PARSE_RECOVER; |
| 12620 | } else |
| 12621 | ctxt->recovery = 0; |
| 12622 | if (options & XML_PARSE_DTDLOAD) { |
| 12623 | ctxt->loadsubset = XML_DETECT_IDS; |
| 12624 | options -= XML_PARSE_DTDLOAD; |
| 12625 | } else |
| 12626 | ctxt->loadsubset = 0; |
| 12627 | if (options & XML_PARSE_DTDATTR) { |
| 12628 | ctxt->loadsubset |= XML_COMPLETE_ATTRS; |
| 12629 | options -= XML_PARSE_DTDATTR; |
| 12630 | } |
| 12631 | if (options & XML_PARSE_NOENT) { |
| 12632 | ctxt->replaceEntities = 1; |
| 12633 | /* ctxt->loadsubset |= XML_DETECT_IDS; */ |
| 12634 | options -= XML_PARSE_NOENT; |
| 12635 | } else |
| 12636 | ctxt->replaceEntities = 0; |
| 12637 | if (options & XML_PARSE_NOWARNING) { |
| 12638 | ctxt->sax->warning = NULL; |
| 12639 | options -= XML_PARSE_NOWARNING; |
| 12640 | } |
| 12641 | if (options & XML_PARSE_NOERROR) { |
| 12642 | ctxt->sax->error = NULL; |
| 12643 | ctxt->sax->fatalError = NULL; |
| 12644 | options -= XML_PARSE_NOERROR; |
| 12645 | } |
| 12646 | if (options & XML_PARSE_PEDANTIC) { |
| 12647 | ctxt->pedantic = 1; |
| 12648 | options -= XML_PARSE_PEDANTIC; |
| 12649 | } else |
| 12650 | ctxt->pedantic = 0; |
| 12651 | if (options & XML_PARSE_NOBLANKS) { |
| 12652 | ctxt->keepBlanks = 0; |
| 12653 | ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace; |
| 12654 | options -= XML_PARSE_NOBLANKS; |
| 12655 | } else |
| 12656 | ctxt->keepBlanks = 1; |
| 12657 | if (options & XML_PARSE_DTDVALID) { |
| 12658 | ctxt->validate = 1; |
| 12659 | if (options & XML_PARSE_NOWARNING) |
| 12660 | ctxt->vctxt.warning = NULL; |
| 12661 | if (options & XML_PARSE_NOERROR) |
| 12662 | ctxt->vctxt.error = NULL; |
| 12663 | options -= XML_PARSE_DTDVALID; |
| 12664 | } else |
| 12665 | ctxt->validate = 0; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12666 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12667 | if (options & XML_PARSE_SAX1) { |
| 12668 | ctxt->sax->startElement = xmlSAX2StartElement; |
| 12669 | ctxt->sax->endElement = xmlSAX2EndElement; |
| 12670 | ctxt->sax->startElementNs = NULL; |
| 12671 | ctxt->sax->endElementNs = NULL; |
| 12672 | ctxt->sax->initialized = 1; |
| 12673 | options -= XML_PARSE_SAX1; |
| 12674 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12675 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12676 | if (options & XML_PARSE_NODICT) { |
| 12677 | ctxt->dictNames = 0; |
| 12678 | options -= XML_PARSE_NODICT; |
| 12679 | } else { |
| 12680 | ctxt->dictNames = 1; |
| 12681 | } |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 12682 | if (options & XML_PARSE_NOCDATA) { |
| 12683 | ctxt->sax->cdataBlock = NULL; |
| 12684 | options -= XML_PARSE_NOCDATA; |
| 12685 | } |
| 12686 | if (options & XML_PARSE_NSCLEAN) { |
| 12687 | ctxt->options |= XML_PARSE_NSCLEAN; |
| 12688 | options -= XML_PARSE_NSCLEAN; |
| 12689 | } |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 12690 | if (options & XML_PARSE_NONET) { |
| 12691 | ctxt->options |= XML_PARSE_NONET; |
| 12692 | options -= XML_PARSE_NONET; |
| 12693 | } |
Daniel Veillard | 7ec2997 | 2003-10-31 14:36:36 +0000 | [diff] [blame] | 12694 | ctxt->linenumbers = 1; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12695 | return (options); |
| 12696 | } |
| 12697 | |
| 12698 | /** |
| 12699 | * xmlDoRead: |
| 12700 | * @ctxt: an XML parser context |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12701 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12702 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12703 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12704 | * @reuse: keep the context for reuse |
| 12705 | * |
| 12706 | * Common front-end for the xmlRead functions |
| 12707 | * |
| 12708 | * Returns the resulting document tree or NULL |
| 12709 | */ |
| 12710 | static xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12711 | xmlDoRead(xmlParserCtxtPtr ctxt, const char *URL, const char *encoding, |
| 12712 | int options, int reuse) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12713 | { |
| 12714 | xmlDocPtr ret; |
| 12715 | |
| 12716 | xmlCtxtUseOptions(ctxt, options); |
| 12717 | if (encoding != NULL) { |
| 12718 | xmlCharEncodingHandlerPtr hdlr; |
| 12719 | |
| 12720 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 12721 | if (hdlr != NULL) |
| 12722 | xmlSwitchToEncoding(ctxt, hdlr); |
| 12723 | } |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12724 | if ((URL != NULL) && (ctxt->input != NULL) && |
| 12725 | (ctxt->input->filename == NULL)) |
| 12726 | ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12727 | xmlParseDocument(ctxt); |
| 12728 | if ((ctxt->wellFormed) || ctxt->recovery) |
| 12729 | ret = ctxt->myDoc; |
| 12730 | else { |
| 12731 | ret = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12732 | if (ctxt->myDoc != NULL) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12733 | xmlFreeDoc(ctxt->myDoc); |
| 12734 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12735 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12736 | ctxt->myDoc = NULL; |
| 12737 | if (!reuse) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12738 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12739 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12740 | |
| 12741 | return (ret); |
| 12742 | } |
| 12743 | |
| 12744 | /** |
| 12745 | * xmlReadDoc: |
| 12746 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12747 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12748 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12749 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12750 | * |
| 12751 | * parse an XML in-memory document and build a tree. |
| 12752 | * |
| 12753 | * Returns the resulting document tree |
| 12754 | */ |
| 12755 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12756 | xmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12757 | { |
| 12758 | xmlParserCtxtPtr ctxt; |
| 12759 | |
| 12760 | if (cur == NULL) |
| 12761 | return (NULL); |
| 12762 | |
| 12763 | ctxt = xmlCreateDocParserCtxt(cur); |
| 12764 | if (ctxt == NULL) |
| 12765 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12766 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12767 | } |
| 12768 | |
| 12769 | /** |
| 12770 | * xmlReadFile: |
| 12771 | * @filename: a file or URL |
| 12772 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12773 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12774 | * |
| 12775 | * parse an XML file from the filesystem or the network. |
| 12776 | * |
| 12777 | * Returns the resulting document tree |
| 12778 | */ |
| 12779 | xmlDocPtr |
| 12780 | xmlReadFile(const char *filename, const char *encoding, int options) |
| 12781 | { |
| 12782 | xmlParserCtxtPtr ctxt; |
| 12783 | |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 12784 | ctxt = xmlCreateURLParserCtxt(filename, options); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12785 | if (ctxt == NULL) |
| 12786 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12787 | return (xmlDoRead(ctxt, NULL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12788 | } |
| 12789 | |
| 12790 | /** |
| 12791 | * xmlReadMemory: |
| 12792 | * @buffer: a pointer to a char array |
| 12793 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12794 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12795 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12796 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12797 | * |
| 12798 | * parse an XML in-memory document and build a tree. |
| 12799 | * |
| 12800 | * Returns the resulting document tree |
| 12801 | */ |
| 12802 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12803 | 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] | 12804 | { |
| 12805 | xmlParserCtxtPtr ctxt; |
| 12806 | |
| 12807 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 12808 | if (ctxt == NULL) |
| 12809 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12810 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12811 | } |
| 12812 | |
| 12813 | /** |
| 12814 | * xmlReadFd: |
| 12815 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12816 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12817 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12818 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12819 | * |
| 12820 | * parse an XML from a file descriptor and build a tree. |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 12821 | * NOTE that the file descriptor will not be closed when the |
| 12822 | * reader is closed or reset. |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12823 | * |
| 12824 | * Returns the resulting document tree |
| 12825 | */ |
| 12826 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12827 | xmlReadFd(int fd, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12828 | { |
| 12829 | xmlParserCtxtPtr ctxt; |
| 12830 | xmlParserInputBufferPtr input; |
| 12831 | xmlParserInputPtr stream; |
| 12832 | |
| 12833 | if (fd < 0) |
| 12834 | return (NULL); |
| 12835 | |
| 12836 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 12837 | if (input == NULL) |
| 12838 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 12839 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12840 | ctxt = xmlNewParserCtxt(); |
| 12841 | if (ctxt == NULL) { |
| 12842 | xmlFreeParserInputBuffer(input); |
| 12843 | return (NULL); |
| 12844 | } |
| 12845 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 12846 | if (stream == NULL) { |
| 12847 | xmlFreeParserInputBuffer(input); |
| 12848 | xmlFreeParserCtxt(ctxt); |
| 12849 | return (NULL); |
| 12850 | } |
| 12851 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12852 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12853 | } |
| 12854 | |
| 12855 | /** |
| 12856 | * xmlReadIO: |
| 12857 | * @ioread: an I/O read function |
| 12858 | * @ioclose: an I/O close function |
| 12859 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12860 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12861 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12862 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12863 | * |
| 12864 | * parse an XML document from I/O functions and source and build a tree. |
| 12865 | * |
| 12866 | * Returns the resulting document tree |
| 12867 | */ |
| 12868 | xmlDocPtr |
| 12869 | xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12870 | void *ioctx, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12871 | { |
| 12872 | xmlParserCtxtPtr ctxt; |
| 12873 | xmlParserInputBufferPtr input; |
| 12874 | xmlParserInputPtr stream; |
| 12875 | |
| 12876 | if (ioread == NULL) |
| 12877 | return (NULL); |
| 12878 | |
| 12879 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 12880 | XML_CHAR_ENCODING_NONE); |
| 12881 | if (input == NULL) |
| 12882 | return (NULL); |
| 12883 | ctxt = xmlNewParserCtxt(); |
| 12884 | if (ctxt == NULL) { |
| 12885 | xmlFreeParserInputBuffer(input); |
| 12886 | return (NULL); |
| 12887 | } |
| 12888 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 12889 | if (stream == NULL) { |
| 12890 | xmlFreeParserInputBuffer(input); |
| 12891 | xmlFreeParserCtxt(ctxt); |
| 12892 | return (NULL); |
| 12893 | } |
| 12894 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12895 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12896 | } |
| 12897 | |
| 12898 | /** |
| 12899 | * xmlCtxtReadDoc: |
| 12900 | * @ctxt: an XML parser context |
| 12901 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12902 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12903 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12904 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12905 | * |
| 12906 | * parse an XML in-memory document and build a tree. |
| 12907 | * This reuses the existing @ctxt parser context |
| 12908 | * |
| 12909 | * Returns the resulting document tree |
| 12910 | */ |
| 12911 | xmlDocPtr |
| 12912 | xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12913 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12914 | { |
| 12915 | xmlParserInputPtr stream; |
| 12916 | |
| 12917 | if (cur == NULL) |
| 12918 | return (NULL); |
| 12919 | if (ctxt == NULL) |
| 12920 | return (NULL); |
| 12921 | |
| 12922 | xmlCtxtReset(ctxt); |
| 12923 | |
| 12924 | stream = xmlNewStringInputStream(ctxt, cur); |
| 12925 | if (stream == NULL) { |
| 12926 | return (NULL); |
| 12927 | } |
| 12928 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12929 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12930 | } |
| 12931 | |
| 12932 | /** |
| 12933 | * xmlCtxtReadFile: |
| 12934 | * @ctxt: an XML parser context |
| 12935 | * @filename: a file or URL |
| 12936 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12937 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12938 | * |
| 12939 | * parse an XML file from the filesystem or the network. |
| 12940 | * This reuses the existing @ctxt parser context |
| 12941 | * |
| 12942 | * Returns the resulting document tree |
| 12943 | */ |
| 12944 | xmlDocPtr |
| 12945 | xmlCtxtReadFile(xmlParserCtxtPtr ctxt, const char *filename, |
| 12946 | const char *encoding, int options) |
| 12947 | { |
| 12948 | xmlParserInputPtr stream; |
| 12949 | |
| 12950 | if (filename == NULL) |
| 12951 | return (NULL); |
| 12952 | if (ctxt == NULL) |
| 12953 | return (NULL); |
| 12954 | |
| 12955 | xmlCtxtReset(ctxt); |
| 12956 | |
Daniel Veillard | 29614c7 | 2004-11-26 10:47:26 +0000 | [diff] [blame] | 12957 | stream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12958 | if (stream == NULL) { |
| 12959 | return (NULL); |
| 12960 | } |
| 12961 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12962 | return (xmlDoRead(ctxt, NULL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12963 | } |
| 12964 | |
| 12965 | /** |
| 12966 | * xmlCtxtReadMemory: |
| 12967 | * @ctxt: an XML parser context |
| 12968 | * @buffer: a pointer to a char array |
| 12969 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12970 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12971 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12972 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12973 | * |
| 12974 | * parse an XML in-memory document and build a tree. |
| 12975 | * This reuses the existing @ctxt parser context |
| 12976 | * |
| 12977 | * Returns the resulting document tree |
| 12978 | */ |
| 12979 | xmlDocPtr |
| 12980 | xmlCtxtReadMemory(xmlParserCtxtPtr ctxt, const char *buffer, int size, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12981 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12982 | { |
| 12983 | xmlParserInputBufferPtr input; |
| 12984 | xmlParserInputPtr stream; |
| 12985 | |
| 12986 | if (ctxt == NULL) |
| 12987 | return (NULL); |
| 12988 | if (buffer == NULL) |
| 12989 | return (NULL); |
| 12990 | |
| 12991 | xmlCtxtReset(ctxt); |
| 12992 | |
| 12993 | input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
| 12994 | if (input == NULL) { |
| 12995 | return(NULL); |
| 12996 | } |
| 12997 | |
| 12998 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 12999 | if (stream == NULL) { |
| 13000 | xmlFreeParserInputBuffer(input); |
| 13001 | return(NULL); |
| 13002 | } |
| 13003 | |
| 13004 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 13005 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13006 | } |
| 13007 | |
| 13008 | /** |
| 13009 | * xmlCtxtReadFd: |
| 13010 | * @ctxt: an XML parser context |
| 13011 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 13012 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13013 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 13014 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13015 | * |
| 13016 | * parse an XML from a file descriptor and build a tree. |
| 13017 | * This reuses the existing @ctxt parser context |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 13018 | * NOTE that the file descriptor will not be closed when the |
| 13019 | * reader is closed or reset. |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13020 | * |
| 13021 | * Returns the resulting document tree |
| 13022 | */ |
| 13023 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 13024 | xmlCtxtReadFd(xmlParserCtxtPtr ctxt, int fd, |
| 13025 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13026 | { |
| 13027 | xmlParserInputBufferPtr input; |
| 13028 | xmlParserInputPtr stream; |
| 13029 | |
| 13030 | if (fd < 0) |
| 13031 | return (NULL); |
| 13032 | if (ctxt == NULL) |
| 13033 | return (NULL); |
| 13034 | |
| 13035 | xmlCtxtReset(ctxt); |
| 13036 | |
| 13037 | |
| 13038 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 13039 | if (input == NULL) |
| 13040 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 13041 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13042 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 13043 | if (stream == NULL) { |
| 13044 | xmlFreeParserInputBuffer(input); |
| 13045 | return (NULL); |
| 13046 | } |
| 13047 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 13048 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13049 | } |
| 13050 | |
| 13051 | /** |
| 13052 | * xmlCtxtReadIO: |
| 13053 | * @ctxt: an XML parser context |
| 13054 | * @ioread: an I/O read function |
| 13055 | * @ioclose: an I/O close function |
| 13056 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 13057 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13058 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 13059 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13060 | * |
| 13061 | * parse an XML document from I/O functions and source and build a tree. |
| 13062 | * This reuses the existing @ctxt parser context |
| 13063 | * |
| 13064 | * Returns the resulting document tree |
| 13065 | */ |
| 13066 | xmlDocPtr |
| 13067 | xmlCtxtReadIO(xmlParserCtxtPtr ctxt, xmlInputReadCallback ioread, |
| 13068 | xmlInputCloseCallback ioclose, void *ioctx, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 13069 | const char *URL, |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13070 | const char *encoding, int options) |
| 13071 | { |
| 13072 | xmlParserInputBufferPtr input; |
| 13073 | xmlParserInputPtr stream; |
| 13074 | |
| 13075 | if (ioread == NULL) |
| 13076 | return (NULL); |
| 13077 | if (ctxt == NULL) |
| 13078 | return (NULL); |
| 13079 | |
| 13080 | xmlCtxtReset(ctxt); |
| 13081 | |
| 13082 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 13083 | XML_CHAR_ENCODING_NONE); |
| 13084 | if (input == NULL) |
| 13085 | return (NULL); |
| 13086 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 13087 | if (stream == NULL) { |
| 13088 | xmlFreeParserInputBuffer(input); |
| 13089 | return (NULL); |
| 13090 | } |
| 13091 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 13092 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 13093 | } |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 13094 | |
| 13095 | #define bottom_parser |
| 13096 | #include "elfgcchack.h" |