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 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 60 | |
| 61 | #ifdef HAVE_CTYPE_H |
| 62 | #include <ctype.h> |
| 63 | #endif |
| 64 | #ifdef HAVE_STDLIB_H |
| 65 | #include <stdlib.h> |
| 66 | #endif |
| 67 | #ifdef HAVE_SYS_STAT_H |
| 68 | #include <sys/stat.h> |
| 69 | #endif |
| 70 | #ifdef HAVE_FCNTL_H |
| 71 | #include <fcntl.h> |
| 72 | #endif |
| 73 | #ifdef HAVE_UNISTD_H |
| 74 | #include <unistd.h> |
| 75 | #endif |
| 76 | #ifdef HAVE_ZLIB_H |
| 77 | #include <zlib.h> |
| 78 | #endif |
| 79 | |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 80 | /** |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 81 | * xmlParserMaxDepth: |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 82 | * |
| 83 | * arbitrary depth limit for the XML documents that we allow to |
| 84 | * process. This is not a limitation of the parser but a safety |
| 85 | * boundary feature. |
| 86 | */ |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 87 | unsigned int xmlParserMaxDepth = 1024; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 88 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 89 | #define SAX2 1 |
| 90 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 91 | #define XML_PARSER_BIG_BUFFER_SIZE 300 |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 92 | #define XML_PARSER_BUFFER_SIZE 100 |
| 93 | |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 94 | #define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document" |
| 95 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 96 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 97 | * List of XML prefixed PI allowed by W3C specs |
| 98 | */ |
| 99 | |
Daniel Veillard | b44025c | 2001-10-11 22:55:55 +0000 | [diff] [blame] | 100 | static const char *xmlW3CPIs[] = { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 101 | "xml-stylesheet", |
| 102 | NULL |
| 103 | }; |
| 104 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 105 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 106 | /* DEPR void xmlParserHandleReference(xmlParserCtxtPtr ctxt); */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 107 | xmlEntityPtr xmlParseStringPEReference(xmlParserCtxtPtr ctxt, |
| 108 | const xmlChar **str); |
| 109 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 110 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 111 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 112 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 113 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 114 | const xmlChar *ID, xmlNodePtr *list); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 115 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 116 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 117 | static void |
| 118 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 119 | xmlNodePtr lastNode); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 120 | #endif /* LIBXML_LEGACY_ENABLED */ |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 121 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 122 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 123 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 124 | const xmlChar *string, void *user_data, xmlNodePtr *lst); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 125 | |
| 126 | /************************************************************************ |
| 127 | * * |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 128 | * Some factorized error routines * |
| 129 | * * |
| 130 | ************************************************************************/ |
| 131 | |
| 132 | /** |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 133 | * xmlErrAttributeDup: |
| 134 | * @ctxt: an XML parser context |
| 135 | * @prefix: the attribute prefix |
| 136 | * @localname: the attribute localname |
| 137 | * |
| 138 | * Handle a redefinition of attribute error |
| 139 | */ |
| 140 | static void |
| 141 | xmlErrAttributeDup(xmlParserCtxtPtr ctxt, const xmlChar * prefix, |
| 142 | const xmlChar * localname) |
| 143 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 144 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 145 | (ctxt->instate == XML_PARSER_EOF)) |
| 146 | return; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 147 | ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED; |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 148 | if (prefix == NULL) |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 149 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 150 | ctxt->errNo, XML_ERR_FATAL, NULL, 0, |
| 151 | (const char *) localname, NULL, NULL, 0, 0, |
| 152 | "Attribute %s redefined\n", localname); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 153 | else |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 154 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 155 | ctxt->errNo, XML_ERR_FATAL, NULL, 0, |
| 156 | (const char *) prefix, (const char *) localname, |
| 157 | NULL, 0, 0, "Attribute %s:%s redefined\n", prefix, |
| 158 | localname); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 159 | ctxt->wellFormed = 0; |
| 160 | if (ctxt->recovery == 0) |
| 161 | ctxt->disableSAX = 1; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * xmlFatalErr: |
| 166 | * @ctxt: an XML parser context |
| 167 | * @error: the error number |
| 168 | * @extra: extra information string |
| 169 | * |
| 170 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 171 | */ |
| 172 | static void |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 173 | xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info) |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 174 | { |
| 175 | const char *errmsg; |
| 176 | |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 177 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 178 | (ctxt->instate == XML_PARSER_EOF)) |
| 179 | return; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 180 | switch (error) { |
| 181 | case XML_ERR_INVALID_HEX_CHARREF: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 182 | errmsg = "CharRef: invalid hexadecimal value\n"; |
| 183 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 184 | case XML_ERR_INVALID_DEC_CHARREF: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 185 | errmsg = "CharRef: invalid decimal value\n"; |
| 186 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 187 | case XML_ERR_INVALID_CHARREF: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 188 | errmsg = "CharRef: invalid value\n"; |
| 189 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 190 | case XML_ERR_INTERNAL_ERROR: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 191 | errmsg = "internal error"; |
| 192 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 193 | case XML_ERR_PEREF_AT_EOF: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 194 | errmsg = "PEReference at end of document\n"; |
| 195 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 196 | case XML_ERR_PEREF_IN_PROLOG: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 197 | errmsg = "PEReference in prolog\n"; |
| 198 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 199 | case XML_ERR_PEREF_IN_EPILOG: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 200 | errmsg = "PEReference in epilog\n"; |
| 201 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 202 | case XML_ERR_PEREF_NO_NAME: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 203 | errmsg = "PEReference: no name\n"; |
| 204 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 205 | case XML_ERR_PEREF_SEMICOL_MISSING: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 206 | errmsg = "PEReference: expecting ';'\n"; |
| 207 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 208 | case XML_ERR_ENTITY_LOOP: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 209 | errmsg = "Detected an entity reference loop\n"; |
| 210 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 211 | case XML_ERR_ENTITY_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 212 | errmsg = "EntityValue: \" or ' expected\n"; |
| 213 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 214 | case XML_ERR_ENTITY_PE_INTERNAL: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 215 | errmsg = "PEReferences forbidden in internal subset\n"; |
| 216 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 217 | case XML_ERR_ENTITY_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 218 | errmsg = "EntityValue: \" or ' expected\n"; |
| 219 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 220 | case XML_ERR_ATTRIBUTE_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 221 | errmsg = "AttValue: \" or ' expected\n"; |
| 222 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 223 | case XML_ERR_LT_IN_ATTRIBUTE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 224 | errmsg = "Unescaped '<' not allowed in attributes values\n"; |
| 225 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 226 | case XML_ERR_LITERAL_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 227 | errmsg = "SystemLiteral \" or ' expected\n"; |
| 228 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 229 | case XML_ERR_LITERAL_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 230 | errmsg = "Unfinished System or Public ID \" or ' expected\n"; |
| 231 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 232 | case XML_ERR_MISPLACED_CDATA_END: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 233 | errmsg = "Sequence ']]>' not allowed in content\n"; |
| 234 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 235 | case XML_ERR_URI_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 236 | errmsg = "SYSTEM or PUBLIC, the URI is missing\n"; |
| 237 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 238 | case XML_ERR_PUBID_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 239 | errmsg = "PUBLIC, the Public Identifier is missing\n"; |
| 240 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 241 | case XML_ERR_HYPHEN_IN_COMMENT: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 242 | errmsg = "Comment must not contain '--' (double-hyphen)\n"; |
| 243 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 244 | case XML_ERR_PI_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 245 | errmsg = "xmlParsePI : no target name\n"; |
| 246 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 247 | case XML_ERR_RESERVED_XML_NAME: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 248 | errmsg = "Invalid PI name\n"; |
| 249 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 250 | case XML_ERR_NOTATION_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 251 | errmsg = "NOTATION: Name expected here\n"; |
| 252 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 253 | case XML_ERR_NOTATION_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 254 | errmsg = "'>' required to close NOTATION declaration\n"; |
| 255 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 256 | case XML_ERR_VALUE_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 257 | errmsg = "Entity value required\n"; |
| 258 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 259 | case XML_ERR_URI_FRAGMENT: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 260 | errmsg = "Fragment not allowed"; |
| 261 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 262 | case XML_ERR_ATTLIST_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 263 | errmsg = "'(' required to start ATTLIST enumeration\n"; |
| 264 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 265 | case XML_ERR_NMTOKEN_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 266 | errmsg = "NmToken expected in ATTLIST enumeration\n"; |
| 267 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 268 | case XML_ERR_ATTLIST_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 269 | errmsg = "')' required to finish ATTLIST enumeration\n"; |
| 270 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 271 | case XML_ERR_MIXED_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 272 | errmsg = "MixedContentDecl : '|' or ')*' expected\n"; |
| 273 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 274 | case XML_ERR_PCDATA_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 275 | errmsg = "MixedContentDecl : '#PCDATA' expected\n"; |
| 276 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 277 | case XML_ERR_ELEMCONTENT_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 278 | errmsg = "ContentDecl : Name or '(' expected\n"; |
| 279 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 280 | case XML_ERR_ELEMCONTENT_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 281 | errmsg = "ContentDecl : ',' '|' or ')' expected\n"; |
| 282 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 283 | case XML_ERR_PEREF_IN_INT_SUBSET: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 284 | errmsg = |
| 285 | "PEReference: forbidden within markup decl in internal subset\n"; |
| 286 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 287 | case XML_ERR_GT_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 288 | errmsg = "expected '>'\n"; |
| 289 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 290 | case XML_ERR_CONDSEC_INVALID: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 291 | errmsg = "XML conditional section '[' expected\n"; |
| 292 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 293 | case XML_ERR_EXT_SUBSET_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 294 | errmsg = "Content error in the external subset\n"; |
| 295 | break; |
| 296 | case XML_ERR_CONDSEC_INVALID_KEYWORD: |
| 297 | errmsg = |
| 298 | "conditional section INCLUDE or IGNORE keyword expected\n"; |
| 299 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 300 | case XML_ERR_CONDSEC_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 301 | errmsg = "XML conditional section not closed\n"; |
| 302 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 303 | case XML_ERR_XMLDECL_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 304 | errmsg = "Text declaration '<?xml' required\n"; |
| 305 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 306 | case XML_ERR_XMLDECL_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 307 | errmsg = "parsing XML declaration: '?>' expected\n"; |
| 308 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 309 | case XML_ERR_EXT_ENTITY_STANDALONE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 310 | errmsg = "external parsed entities cannot be standalone\n"; |
| 311 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 312 | case XML_ERR_ENTITYREF_SEMICOL_MISSING: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 313 | errmsg = "EntityRef: expecting ';'\n"; |
| 314 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 315 | case XML_ERR_DOCTYPE_NOT_FINISHED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 316 | errmsg = "DOCTYPE improperly terminated\n"; |
| 317 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 318 | case XML_ERR_LTSLASH_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 319 | errmsg = "EndTag: '</' not found\n"; |
| 320 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 321 | case XML_ERR_EQUAL_REQUIRED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 322 | errmsg = "expected '='\n"; |
| 323 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 324 | case XML_ERR_STRING_NOT_CLOSED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 325 | errmsg = "String not closed expecting \" or '\n"; |
| 326 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 327 | case XML_ERR_STRING_NOT_STARTED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 328 | errmsg = "String not started expecting ' or \"\n"; |
| 329 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 330 | case XML_ERR_ENCODING_NAME: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 331 | errmsg = "Invalid XML encoding name\n"; |
| 332 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 333 | case XML_ERR_STANDALONE_VALUE: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 334 | errmsg = "standalone accepts only 'yes' or 'no'\n"; |
| 335 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 336 | case XML_ERR_DOCUMENT_EMPTY: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 337 | errmsg = "Document is empty\n"; |
| 338 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 339 | case XML_ERR_DOCUMENT_END: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 340 | errmsg = "Extra content at the end of the document\n"; |
| 341 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 342 | case XML_ERR_NOT_WELL_BALANCED: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 343 | errmsg = "chunk is not well balanced\n"; |
| 344 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 345 | case XML_ERR_EXTRA_CONTENT: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 346 | errmsg = "extra content at the end of well balanced chunk\n"; |
| 347 | break; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 348 | case XML_ERR_VERSION_MISSING: |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 349 | errmsg = "Malformed declaration expecting version\n"; |
| 350 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 351 | #if 0 |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 352 | case: |
| 353 | errmsg = "\n"; |
| 354 | break; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 355 | #endif |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 356 | default: |
| 357 | errmsg = "Unregistered error message\n"; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 358 | } |
| 359 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 360 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 361 | XML_ERR_FATAL, NULL, 0, info, NULL, NULL, 0, 0, errmsg, |
| 362 | info); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 363 | ctxt->wellFormed = 0; |
| 364 | if (ctxt->recovery == 0) |
| 365 | ctxt->disableSAX = 1; |
| 366 | } |
| 367 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 368 | /** |
| 369 | * xmlFatalErrMsg: |
| 370 | * @ctxt: an XML parser context |
| 371 | * @error: the error number |
| 372 | * @msg: the error message |
| 373 | * |
| 374 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 375 | */ |
| 376 | static void |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 377 | xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 378 | const char *msg) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 379 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 380 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 381 | (ctxt->instate == XML_PARSER_EOF)) |
| 382 | return; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 383 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 384 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 385 | XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, msg); |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 386 | ctxt->wellFormed = 0; |
| 387 | if (ctxt->recovery == 0) |
| 388 | ctxt->disableSAX = 1; |
| 389 | } |
| 390 | |
| 391 | /** |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 392 | * xmlWarningMsg: |
| 393 | * @ctxt: an XML parser context |
| 394 | * @error: the error number |
| 395 | * @msg: the error message |
| 396 | * @str1: extra data |
| 397 | * @str2: extra data |
| 398 | * |
| 399 | * Handle a warning. |
| 400 | */ |
| 401 | static void |
| 402 | xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 403 | const char *msg, const xmlChar *str1, const xmlChar *str2) |
| 404 | { |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 405 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 406 | |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 407 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 408 | (ctxt->instate == XML_PARSER_EOF)) |
| 409 | return; |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 410 | ctxt->errNo = error; |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 411 | if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 412 | schannel = ctxt->sax->serror; |
| 413 | __xmlRaiseError(schannel, |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 414 | (ctxt->sax) ? ctxt->sax->warning : NULL, |
| 415 | ctxt->userData, |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 416 | ctxt, NULL, XML_FROM_PARSER, error, |
| 417 | XML_ERR_WARNING, NULL, 0, |
| 418 | (const char *) str1, (const char *) str2, NULL, 0, 0, |
| 419 | msg, (const char *) str1, (const char *) str2); |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * xmlValidityError: |
| 424 | * @ctxt: an XML parser context |
| 425 | * @error: the error number |
| 426 | * @msg: the error message |
| 427 | * @str1: extra data |
| 428 | * |
Daniel Veillard | f88d8cf | 2003-12-08 10:25:02 +0000 | [diff] [blame] | 429 | * Handle a validity error. |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 430 | */ |
| 431 | static void |
| 432 | xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 433 | const char *msg, const xmlChar *str1) |
| 434 | { |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 435 | xmlStructuredErrorFunc schannel = NULL; |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 436 | |
| 437 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 438 | (ctxt->instate == XML_PARSER_EOF)) |
| 439 | return; |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 440 | ctxt->errNo = error; |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 441 | if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC)) |
Daniel Veillard | 9bcc7c5 | 2003-10-11 10:57:05 +0000 | [diff] [blame] | 442 | schannel = ctxt->sax->serror; |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 443 | __xmlRaiseError(schannel, |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 444 | ctxt->vctxt.error, ctxt->vctxt.userData, |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 445 | ctxt, NULL, XML_FROM_DTD, error, |
| 446 | XML_ERR_ERROR, NULL, 0, (const char *) str1, |
| 447 | NULL, NULL, 0, 0, |
| 448 | msg, (const char *) str1); |
| 449 | ctxt->valid = 0; |
| 450 | } |
| 451 | |
| 452 | /** |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 453 | * xmlFatalErrMsgInt: |
| 454 | * @ctxt: an XML parser context |
| 455 | * @error: the error number |
| 456 | * @msg: the error message |
| 457 | * @val: an integer value |
| 458 | * |
| 459 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 460 | */ |
| 461 | static void |
| 462 | xmlFatalErrMsgInt(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 463 | const char *msg, int val) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 464 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 465 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 466 | (ctxt->instate == XML_PARSER_EOF)) |
| 467 | return; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 468 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 469 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 470 | ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 471 | NULL, 0, NULL, NULL, NULL, val, 0, msg, val); |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 472 | ctxt->wellFormed = 0; |
| 473 | if (ctxt->recovery == 0) |
| 474 | ctxt->disableSAX = 1; |
| 475 | } |
| 476 | |
| 477 | /** |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 478 | * xmlFatalErrMsgStrIntStr: |
| 479 | * @ctxt: an XML parser context |
| 480 | * @error: the error number |
| 481 | * @msg: the error message |
| 482 | * @str1: an string info |
| 483 | * @val: an integer value |
| 484 | * @str2: an string info |
| 485 | * |
| 486 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 487 | */ |
| 488 | static void |
| 489 | xmlFatalErrMsgStrIntStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 490 | const char *msg, const xmlChar *str1, int val, |
| 491 | const xmlChar *str2) |
| 492 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 493 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 494 | (ctxt->instate == XML_PARSER_EOF)) |
| 495 | return; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 496 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 497 | __xmlRaiseError(NULL, NULL, NULL, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 498 | ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 499 | NULL, 0, (const char *) str1, (const char *) str2, |
| 500 | NULL, val, 0, msg, str1, val, str2); |
| 501 | ctxt->wellFormed = 0; |
| 502 | if (ctxt->recovery == 0) |
| 503 | ctxt->disableSAX = 1; |
| 504 | } |
| 505 | |
| 506 | /** |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 507 | * xmlFatalErrMsgStr: |
| 508 | * @ctxt: an XML parser context |
| 509 | * @error: the error number |
| 510 | * @msg: the error message |
| 511 | * @val: a string value |
| 512 | * |
| 513 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 514 | */ |
| 515 | static void |
| 516 | xmlFatalErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 517 | const char *msg, const xmlChar * val) |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 518 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 519 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 520 | (ctxt->instate == XML_PARSER_EOF)) |
| 521 | return; |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 522 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 523 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 524 | XML_FROM_PARSER, error, XML_ERR_FATAL, |
| 525 | NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg, |
| 526 | val); |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 527 | ctxt->wellFormed = 0; |
| 528 | if (ctxt->recovery == 0) |
| 529 | ctxt->disableSAX = 1; |
| 530 | } |
| 531 | |
| 532 | /** |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 533 | * xmlErrMsgStr: |
| 534 | * @ctxt: an XML parser context |
| 535 | * @error: the error number |
| 536 | * @msg: the error message |
| 537 | * @val: a string value |
| 538 | * |
| 539 | * Handle a non fatal parser error |
| 540 | */ |
| 541 | static void |
| 542 | xmlErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 543 | const char *msg, const xmlChar * val) |
| 544 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 545 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 546 | (ctxt->instate == XML_PARSER_EOF)) |
| 547 | return; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 548 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 549 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 550 | XML_FROM_PARSER, error, XML_ERR_ERROR, |
| 551 | NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg, |
| 552 | val); |
| 553 | } |
| 554 | |
| 555 | /** |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 556 | * xmlNsErr: |
| 557 | * @ctxt: an XML parser context |
| 558 | * @error: the error number |
| 559 | * @msg: the message |
| 560 | * @info1: extra information string |
| 561 | * @info2: extra information string |
| 562 | * |
| 563 | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
| 564 | */ |
| 565 | static void |
| 566 | xmlNsErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, |
| 567 | const char *msg, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 568 | const xmlChar * info1, const xmlChar * info2, |
| 569 | const xmlChar * info3) |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 570 | { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 571 | if ((ctxt != NULL) && (ctxt->disableSAX != 0) && |
| 572 | (ctxt->instate == XML_PARSER_EOF)) |
| 573 | return; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 574 | ctxt->errNo = error; |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 575 | __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error, |
Daniel Veillard | bb5abab | 2003-10-03 22:21:51 +0000 | [diff] [blame] | 576 | XML_ERR_ERROR, NULL, 0, (const char *) info1, |
| 577 | (const char *) info2, (const char *) info3, 0, 0, msg, |
| 578 | info1, info2, info3); |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 579 | ctxt->nsWellFormed = 0; |
| 580 | } |
| 581 | |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 582 | /************************************************************************ |
| 583 | * * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 584 | * SAX2 defaulted attributes handling * |
| 585 | * * |
| 586 | ************************************************************************/ |
| 587 | |
| 588 | /** |
| 589 | * xmlDetectSAX2: |
| 590 | * @ctxt: an XML parser context |
| 591 | * |
| 592 | * Do the SAX2 detection and specific intialization |
| 593 | */ |
| 594 | static void |
| 595 | xmlDetectSAX2(xmlParserCtxtPtr ctxt) { |
| 596 | if (ctxt == NULL) return; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 597 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 598 | if ((ctxt->sax) && (ctxt->sax->initialized == XML_SAX2_MAGIC) && |
| 599 | ((ctxt->sax->startElementNs != NULL) || |
| 600 | (ctxt->sax->endElementNs != NULL))) ctxt->sax2 = 1; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 601 | #else |
| 602 | ctxt->sax2 = 1; |
| 603 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 604 | |
| 605 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 606 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 607 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
William M. Brack | 9f797ab | 2004-07-28 07:40:12 +0000 | [diff] [blame] | 608 | if ((ctxt->str_xml==NULL) || (ctxt->str_xmlns==NULL) || |
| 609 | (ctxt->str_xml_ns == NULL)) { |
| 610 | xmlErrMemory(ctxt, NULL); |
| 611 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 614 | typedef struct _xmlDefAttrs xmlDefAttrs; |
| 615 | typedef xmlDefAttrs *xmlDefAttrsPtr; |
| 616 | struct _xmlDefAttrs { |
| 617 | int nbAttrs; /* number of defaulted attributes on that element */ |
| 618 | int maxAttrs; /* the size of the array */ |
| 619 | const xmlChar *values[4]; /* array of localname/prefix/values */ |
| 620 | }; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 621 | |
| 622 | /** |
| 623 | * xmlAddDefAttrs: |
| 624 | * @ctxt: an XML parser context |
| 625 | * @fullname: the element fullname |
| 626 | * @fullattr: the attribute fullname |
| 627 | * @value: the attribute value |
| 628 | * |
| 629 | * Add a defaulted attribute for an element |
| 630 | */ |
| 631 | static void |
| 632 | xmlAddDefAttrs(xmlParserCtxtPtr ctxt, |
| 633 | const xmlChar *fullname, |
| 634 | const xmlChar *fullattr, |
| 635 | const xmlChar *value) { |
| 636 | xmlDefAttrsPtr defaults; |
| 637 | int len; |
| 638 | const xmlChar *name; |
| 639 | const xmlChar *prefix; |
| 640 | |
| 641 | if (ctxt->attsDefault == NULL) { |
| 642 | ctxt->attsDefault = xmlHashCreate(10); |
| 643 | if (ctxt->attsDefault == NULL) |
| 644 | goto mem_error; |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | * plit the element name into prefix:localname , the string found |
| 649 | * are within the DTD and hen not associated to namespace names. |
| 650 | */ |
| 651 | name = xmlSplitQName3(fullname, &len); |
| 652 | if (name == NULL) { |
| 653 | name = xmlDictLookup(ctxt->dict, fullname, -1); |
| 654 | prefix = NULL; |
| 655 | } else { |
| 656 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 657 | prefix = xmlDictLookup(ctxt->dict, fullname, len); |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * make sure there is some storage |
| 662 | */ |
| 663 | defaults = xmlHashLookup2(ctxt->attsDefault, name, prefix); |
| 664 | if (defaults == NULL) { |
| 665 | defaults = (xmlDefAttrsPtr) xmlMalloc(sizeof(xmlDefAttrs) + |
| 666 | 12 * sizeof(const xmlChar *)); |
| 667 | if (defaults == NULL) |
| 668 | goto mem_error; |
| 669 | defaults->maxAttrs = 4; |
| 670 | defaults->nbAttrs = 0; |
| 671 | xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, defaults, NULL); |
| 672 | } else if (defaults->nbAttrs >= defaults->maxAttrs) { |
| 673 | defaults = (xmlDefAttrsPtr) xmlRealloc(defaults, sizeof(xmlDefAttrs) + |
| 674 | (2 * defaults->maxAttrs * 4) * sizeof(const xmlChar *)); |
| 675 | if (defaults == NULL) |
| 676 | goto mem_error; |
| 677 | defaults->maxAttrs *= 2; |
| 678 | xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, defaults, NULL); |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * plit the element name into prefix:localname , the string found |
| 683 | * are within the DTD and hen not associated to namespace names. |
| 684 | */ |
| 685 | name = xmlSplitQName3(fullattr, &len); |
| 686 | if (name == NULL) { |
| 687 | name = xmlDictLookup(ctxt->dict, fullattr, -1); |
| 688 | prefix = NULL; |
| 689 | } else { |
| 690 | name = xmlDictLookup(ctxt->dict, name, -1); |
| 691 | prefix = xmlDictLookup(ctxt->dict, fullattr, len); |
| 692 | } |
| 693 | |
| 694 | defaults->values[4 * defaults->nbAttrs] = name; |
| 695 | defaults->values[4 * defaults->nbAttrs + 1] = prefix; |
| 696 | /* intern the string and precompute the end */ |
| 697 | len = xmlStrlen(value); |
| 698 | value = xmlDictLookup(ctxt->dict, value, len); |
| 699 | defaults->values[4 * defaults->nbAttrs + 2] = value; |
| 700 | defaults->values[4 * defaults->nbAttrs + 3] = value + len; |
| 701 | defaults->nbAttrs++; |
| 702 | |
| 703 | return; |
| 704 | |
| 705 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 706 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 707 | return; |
| 708 | } |
| 709 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 710 | /** |
| 711 | * xmlAddSpecialAttr: |
| 712 | * @ctxt: an XML parser context |
| 713 | * @fullname: the element fullname |
| 714 | * @fullattr: the attribute fullname |
| 715 | * @type: the attribute type |
| 716 | * |
| 717 | * Register that this attribute is not CDATA |
| 718 | */ |
| 719 | static void |
| 720 | xmlAddSpecialAttr(xmlParserCtxtPtr ctxt, |
| 721 | const xmlChar *fullname, |
| 722 | const xmlChar *fullattr, |
| 723 | int type) |
| 724 | { |
| 725 | if (ctxt->attsSpecial == NULL) { |
| 726 | ctxt->attsSpecial = xmlHashCreate(10); |
| 727 | if (ctxt->attsSpecial == NULL) |
| 728 | goto mem_error; |
| 729 | } |
| 730 | |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 731 | xmlHashAddEntry2(ctxt->attsSpecial, fullname, fullattr, |
| 732 | (void *) (long) type); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 733 | return; |
| 734 | |
| 735 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 736 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 737 | return; |
| 738 | } |
| 739 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 740 | /** |
| 741 | * xmlCheckLanguageID: |
| 742 | * @lang: pointer to the string value |
| 743 | * |
| 744 | * Checks that the value conforms to the LanguageID production: |
| 745 | * |
| 746 | * NOTE: this is somewhat deprecated, those productions were removed from |
| 747 | * the XML Second edition. |
| 748 | * |
| 749 | * [33] LanguageID ::= Langcode ('-' Subcode)* |
| 750 | * [34] Langcode ::= ISO639Code | IanaCode | UserCode |
| 751 | * [35] ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z]) |
| 752 | * [36] IanaCode ::= ('i' | 'I') '-' ([a-z] | [A-Z])+ |
| 753 | * [37] UserCode ::= ('x' | 'X') '-' ([a-z] | [A-Z])+ |
| 754 | * [38] Subcode ::= ([a-z] | [A-Z])+ |
| 755 | * |
| 756 | * Returns 1 if correct 0 otherwise |
| 757 | **/ |
| 758 | int |
| 759 | xmlCheckLanguageID(const xmlChar * lang) |
| 760 | { |
| 761 | const xmlChar *cur = lang; |
| 762 | |
| 763 | if (cur == NULL) |
| 764 | return (0); |
| 765 | if (((cur[0] == 'i') && (cur[1] == '-')) || |
| 766 | ((cur[0] == 'I') && (cur[1] == '-'))) { |
| 767 | /* |
| 768 | * IANA code |
| 769 | */ |
| 770 | cur += 2; |
| 771 | while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */ |
| 772 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 773 | cur++; |
| 774 | } else if (((cur[0] == 'x') && (cur[1] == '-')) || |
| 775 | ((cur[0] == 'X') && (cur[1] == '-'))) { |
| 776 | /* |
| 777 | * User code |
| 778 | */ |
| 779 | cur += 2; |
| 780 | while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */ |
| 781 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 782 | cur++; |
| 783 | } else if (((cur[0] >= 'A') && (cur[0] <= 'Z')) || |
| 784 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) { |
| 785 | /* |
| 786 | * ISO639 |
| 787 | */ |
| 788 | cur++; |
| 789 | if (((cur[0] >= 'A') && (cur[0] <= 'Z')) || |
| 790 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 791 | cur++; |
| 792 | else |
| 793 | return (0); |
| 794 | } else |
| 795 | return (0); |
| 796 | while (cur[0] != 0) { /* non input consuming */ |
| 797 | if (cur[0] != '-') |
| 798 | return (0); |
| 799 | cur++; |
| 800 | if (((cur[0] >= 'A') && (cur[0] <= 'Z')) || |
| 801 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 802 | cur++; |
| 803 | else |
| 804 | return (0); |
| 805 | while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */ |
| 806 | ((cur[0] >= 'a') && (cur[0] <= 'z'))) |
| 807 | cur++; |
| 808 | } |
| 809 | return (1); |
| 810 | } |
| 811 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 812 | /************************************************************************ |
| 813 | * * |
| 814 | * Parser stacks related functions and macros * |
| 815 | * * |
| 816 | ************************************************************************/ |
| 817 | |
| 818 | xmlEntityPtr xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, |
| 819 | const xmlChar ** str); |
| 820 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 821 | #ifdef SAX2 |
| 822 | /** |
| 823 | * nsPush: |
| 824 | * @ctxt: an XML parser context |
| 825 | * @prefix: the namespace prefix or NULL |
| 826 | * @URL: the namespace name |
| 827 | * |
| 828 | * Pushes a new parser namespace on top of the ns stack |
| 829 | * |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 830 | * Returns -1 in case of error, -2 if the namespace should be discarded |
| 831 | * and the index in the stack otherwise. |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 832 | */ |
| 833 | static int |
| 834 | nsPush(xmlParserCtxtPtr ctxt, const xmlChar *prefix, const xmlChar *URL) |
| 835 | { |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 836 | if (ctxt->options & XML_PARSE_NSCLEAN) { |
| 837 | int i; |
| 838 | for (i = 0;i < ctxt->nsNr;i += 2) { |
| 839 | if (ctxt->nsTab[i] == prefix) { |
| 840 | /* in scope */ |
| 841 | if (ctxt->nsTab[i + 1] == URL) |
| 842 | return(-2); |
| 843 | /* out of scope keep it */ |
| 844 | break; |
| 845 | } |
| 846 | } |
| 847 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 848 | if ((ctxt->nsMax == 0) || (ctxt->nsTab == NULL)) { |
| 849 | ctxt->nsMax = 10; |
| 850 | ctxt->nsNr = 0; |
| 851 | ctxt->nsTab = (const xmlChar **) |
| 852 | xmlMalloc(ctxt->nsMax * sizeof(xmlChar *)); |
| 853 | if (ctxt->nsTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 854 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 855 | ctxt->nsMax = 0; |
| 856 | return (-1); |
| 857 | } |
| 858 | } else if (ctxt->nsNr >= ctxt->nsMax) { |
| 859 | ctxt->nsMax *= 2; |
| 860 | ctxt->nsTab = (const xmlChar **) |
Daniel Veillard | 5bb9ccd | 2004-02-09 12:39:02 +0000 | [diff] [blame] | 861 | xmlRealloc((char *) ctxt->nsTab, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 862 | ctxt->nsMax * sizeof(ctxt->nsTab[0])); |
| 863 | if (ctxt->nsTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 864 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 865 | ctxt->nsMax /= 2; |
| 866 | return (-1); |
| 867 | } |
| 868 | } |
| 869 | ctxt->nsTab[ctxt->nsNr++] = prefix; |
| 870 | ctxt->nsTab[ctxt->nsNr++] = URL; |
| 871 | return (ctxt->nsNr); |
| 872 | } |
| 873 | /** |
| 874 | * nsPop: |
| 875 | * @ctxt: an XML parser context |
| 876 | * @nr: the number to pop |
| 877 | * |
| 878 | * Pops the top @nr parser prefix/namespace from the ns stack |
| 879 | * |
| 880 | * Returns the number of namespaces removed |
| 881 | */ |
| 882 | static int |
| 883 | nsPop(xmlParserCtxtPtr ctxt, int nr) |
| 884 | { |
| 885 | int i; |
| 886 | |
| 887 | if (ctxt->nsTab == NULL) return(0); |
| 888 | if (ctxt->nsNr < nr) { |
| 889 | xmlGenericError(xmlGenericErrorContext, "Pbm popping %d NS\n", nr); |
| 890 | nr = ctxt->nsNr; |
| 891 | } |
| 892 | if (ctxt->nsNr <= 0) |
| 893 | return (0); |
| 894 | |
| 895 | for (i = 0;i < nr;i++) { |
| 896 | ctxt->nsNr--; |
| 897 | ctxt->nsTab[ctxt->nsNr] = NULL; |
| 898 | } |
| 899 | return(nr); |
| 900 | } |
| 901 | #endif |
| 902 | |
| 903 | static int |
| 904 | xmlCtxtGrowAttrs(xmlParserCtxtPtr ctxt, int nr) { |
| 905 | const xmlChar **atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 906 | int *attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 907 | int maxatts; |
| 908 | |
| 909 | if (ctxt->atts == NULL) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 910 | maxatts = 55; /* allow for 10 attrs by default */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 911 | atts = (const xmlChar **) |
| 912 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 913 | if (atts == NULL) goto mem_error; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 914 | ctxt->atts = atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 915 | attallocs = (int *) xmlMalloc((maxatts / 5) * sizeof(int)); |
| 916 | if (attallocs == NULL) goto mem_error; |
| 917 | ctxt->attallocs = attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 918 | ctxt->maxatts = maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 919 | } else if (nr + 5 > ctxt->maxatts) { |
| 920 | maxatts = (nr + 5) * 2; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 921 | atts = (const xmlChar **) xmlRealloc((void *) ctxt->atts, |
| 922 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 923 | if (atts == NULL) goto mem_error; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 924 | ctxt->atts = atts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 925 | attallocs = (int *) xmlRealloc((void *) ctxt->attallocs, |
| 926 | (maxatts / 5) * sizeof(int)); |
| 927 | if (attallocs == NULL) goto mem_error; |
| 928 | ctxt->attallocs = attallocs; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 929 | ctxt->maxatts = maxatts; |
| 930 | } |
| 931 | return(ctxt->maxatts); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 932 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 933 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 934 | return(-1); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 937 | /** |
| 938 | * inputPush: |
| 939 | * @ctxt: an XML parser context |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 940 | * @value: the parser input |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 941 | * |
| 942 | * Pushes a new parser input on top of the input stack |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 943 | * |
| 944 | * Returns 0 in case of error, the index in the stack otherwise |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 945 | */ |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 946 | extern int |
| 947 | inputPush(xmlParserCtxtPtr ctxt, xmlParserInputPtr value) |
| 948 | { |
| 949 | if (ctxt->inputNr >= ctxt->inputMax) { |
| 950 | ctxt->inputMax *= 2; |
| 951 | ctxt->inputTab = |
| 952 | (xmlParserInputPtr *) xmlRealloc(ctxt->inputTab, |
| 953 | ctxt->inputMax * |
| 954 | sizeof(ctxt->inputTab[0])); |
| 955 | if (ctxt->inputTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 956 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 957 | return (0); |
| 958 | } |
| 959 | } |
| 960 | ctxt->inputTab[ctxt->inputNr] = value; |
| 961 | ctxt->input = value; |
| 962 | return (ctxt->inputNr++); |
| 963 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 964 | /** |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 965 | * inputPop: |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 966 | * @ctxt: an XML parser context |
| 967 | * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 968 | * Pops the top parser input from the input stack |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 969 | * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 970 | * Returns the input just removed |
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 | extern xmlParserInputPtr |
| 973 | inputPop(xmlParserCtxtPtr ctxt) |
| 974 | { |
| 975 | xmlParserInputPtr ret; |
| 976 | |
| 977 | if (ctxt->inputNr <= 0) |
| 978 | return (0); |
| 979 | ctxt->inputNr--; |
| 980 | if (ctxt->inputNr > 0) |
| 981 | ctxt->input = ctxt->inputTab[ctxt->inputNr - 1]; |
| 982 | else |
| 983 | ctxt->input = NULL; |
| 984 | ret = ctxt->inputTab[ctxt->inputNr]; |
| 985 | ctxt->inputTab[ctxt->inputNr] = 0; |
| 986 | return (ret); |
| 987 | } |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 988 | /** |
| 989 | * nodePush: |
| 990 | * @ctxt: an XML parser context |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 991 | * @value: the element node |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 992 | * |
| 993 | * Pushes a new element node on top of the node stack |
Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 994 | * |
| 995 | * Returns 0 in case of error, the index in the stack otherwise |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 996 | */ |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 997 | extern int |
| 998 | nodePush(xmlParserCtxtPtr ctxt, xmlNodePtr value) |
| 999 | { |
| 1000 | if (ctxt->nodeNr >= ctxt->nodeMax) { |
| 1001 | ctxt->nodeMax *= 2; |
| 1002 | ctxt->nodeTab = |
| 1003 | (xmlNodePtr *) xmlRealloc(ctxt->nodeTab, |
| 1004 | ctxt->nodeMax * |
| 1005 | sizeof(ctxt->nodeTab[0])); |
| 1006 | if (ctxt->nodeTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1007 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1008 | return (0); |
| 1009 | } |
| 1010 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 1011 | if (((unsigned int) ctxt->nodeNr) > xmlParserMaxDepth) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 1012 | xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 1013 | "Excessive depth in document: change xmlParserMaxDepth = %d\n", |
| 1014 | xmlParserMaxDepth); |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 1015 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | 3b2e4e1 | 2003-02-03 08:52:58 +0000 | [diff] [blame] | 1016 | return(0); |
| 1017 | } |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1018 | ctxt->nodeTab[ctxt->nodeNr] = value; |
| 1019 | ctxt->node = value; |
| 1020 | return (ctxt->nodeNr++); |
| 1021 | } |
| 1022 | /** |
| 1023 | * nodePop: |
| 1024 | * @ctxt: an XML parser context |
| 1025 | * |
| 1026 | * Pops the top element node from the node stack |
| 1027 | * |
| 1028 | * Returns the node just removed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1029 | */ |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1030 | extern xmlNodePtr |
| 1031 | nodePop(xmlParserCtxtPtr ctxt) |
| 1032 | { |
| 1033 | xmlNodePtr ret; |
| 1034 | |
| 1035 | if (ctxt->nodeNr <= 0) |
| 1036 | return (0); |
| 1037 | ctxt->nodeNr--; |
| 1038 | if (ctxt->nodeNr > 0) |
| 1039 | ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1]; |
| 1040 | else |
| 1041 | ctxt->node = NULL; |
| 1042 | ret = ctxt->nodeTab[ctxt->nodeNr]; |
| 1043 | ctxt->nodeTab[ctxt->nodeNr] = 0; |
| 1044 | return (ret); |
| 1045 | } |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1046 | |
| 1047 | #ifdef LIBXML_PUSH_ENABLED |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1048 | /** |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1049 | * nameNsPush: |
| 1050 | * @ctxt: an XML parser context |
| 1051 | * @value: the element name |
| 1052 | * @prefix: the element prefix |
| 1053 | * @URI: the element namespace name |
| 1054 | * |
| 1055 | * Pushes a new element name/prefix/URL on top of the name stack |
| 1056 | * |
| 1057 | * Returns -1 in case of error, the index in the stack otherwise |
| 1058 | */ |
| 1059 | static int |
| 1060 | nameNsPush(xmlParserCtxtPtr ctxt, const xmlChar * value, |
| 1061 | const xmlChar *prefix, const xmlChar *URI, int nsNr) |
| 1062 | { |
| 1063 | if (ctxt->nameNr >= ctxt->nameMax) { |
| 1064 | const xmlChar * *tmp; |
| 1065 | void **tmp2; |
| 1066 | ctxt->nameMax *= 2; |
| 1067 | tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab, |
| 1068 | ctxt->nameMax * |
| 1069 | sizeof(ctxt->nameTab[0])); |
| 1070 | if (tmp == NULL) { |
| 1071 | ctxt->nameMax /= 2; |
| 1072 | goto mem_error; |
| 1073 | } |
| 1074 | ctxt->nameTab = tmp; |
| 1075 | tmp2 = (void **) xmlRealloc((void * *)ctxt->pushTab, |
| 1076 | ctxt->nameMax * 3 * |
| 1077 | sizeof(ctxt->pushTab[0])); |
| 1078 | if (tmp2 == NULL) { |
| 1079 | ctxt->nameMax /= 2; |
| 1080 | goto mem_error; |
| 1081 | } |
| 1082 | ctxt->pushTab = tmp2; |
| 1083 | } |
| 1084 | ctxt->nameTab[ctxt->nameNr] = value; |
| 1085 | ctxt->name = value; |
| 1086 | ctxt->pushTab[ctxt->nameNr * 3] = (void *) prefix; |
| 1087 | ctxt->pushTab[ctxt->nameNr * 3 + 1] = (void *) URI; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 1088 | ctxt->pushTab[ctxt->nameNr * 3 + 2] = (void *) (long) nsNr; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1089 | return (ctxt->nameNr++); |
| 1090 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1091 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1092 | return (-1); |
| 1093 | } |
| 1094 | /** |
| 1095 | * nameNsPop: |
| 1096 | * @ctxt: an XML parser context |
| 1097 | * |
| 1098 | * Pops the top element/prefix/URI name from the name stack |
| 1099 | * |
| 1100 | * Returns the name just removed |
| 1101 | */ |
| 1102 | static const xmlChar * |
| 1103 | nameNsPop(xmlParserCtxtPtr ctxt) |
| 1104 | { |
| 1105 | const xmlChar *ret; |
| 1106 | |
| 1107 | if (ctxt->nameNr <= 0) |
| 1108 | return (0); |
| 1109 | ctxt->nameNr--; |
| 1110 | if (ctxt->nameNr > 0) |
| 1111 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 1112 | else |
| 1113 | ctxt->name = NULL; |
| 1114 | ret = ctxt->nameTab[ctxt->nameNr]; |
| 1115 | ctxt->nameTab[ctxt->nameNr] = NULL; |
| 1116 | return (ret); |
| 1117 | } |
Daniel Veillard | a235132 | 2004-06-27 12:08:10 +0000 | [diff] [blame] | 1118 | #endif /* LIBXML_PUSH_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1119 | |
| 1120 | /** |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1121 | * namePush: |
| 1122 | * @ctxt: an XML parser context |
| 1123 | * @value: the element name |
| 1124 | * |
| 1125 | * Pushes a new element name on top of the name stack |
| 1126 | * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1127 | * Returns -1 in case of error, the index in the stack otherwise |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1128 | */ |
| 1129 | extern int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1130 | namePush(xmlParserCtxtPtr ctxt, const xmlChar * value) |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1131 | { |
| 1132 | if (ctxt->nameNr >= ctxt->nameMax) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1133 | const xmlChar * *tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1134 | ctxt->nameMax *= 2; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1135 | tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab, |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1136 | ctxt->nameMax * |
| 1137 | sizeof(ctxt->nameTab[0])); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1138 | if (tmp == NULL) { |
| 1139 | ctxt->nameMax /= 2; |
| 1140 | goto mem_error; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1141 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1142 | ctxt->nameTab = tmp; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1143 | } |
| 1144 | ctxt->nameTab[ctxt->nameNr] = value; |
| 1145 | ctxt->name = value; |
| 1146 | return (ctxt->nameNr++); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1147 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1148 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1149 | return (-1); |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1150 | } |
| 1151 | /** |
| 1152 | * namePop: |
| 1153 | * @ctxt: an XML parser context |
| 1154 | * |
| 1155 | * Pops the top element name from the name stack |
| 1156 | * |
| 1157 | * Returns the name just removed |
| 1158 | */ |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1159 | extern const xmlChar * |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1160 | namePop(xmlParserCtxtPtr ctxt) |
| 1161 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1162 | const xmlChar *ret; |
Daniel Veillard | 1c732d2 | 2002-11-30 11:22:59 +0000 | [diff] [blame] | 1163 | |
| 1164 | if (ctxt->nameNr <= 0) |
| 1165 | return (0); |
| 1166 | ctxt->nameNr--; |
| 1167 | if (ctxt->nameNr > 0) |
| 1168 | ctxt->name = ctxt->nameTab[ctxt->nameNr - 1]; |
| 1169 | else |
| 1170 | ctxt->name = NULL; |
| 1171 | ret = ctxt->nameTab[ctxt->nameNr]; |
| 1172 | ctxt->nameTab[ctxt->nameNr] = 0; |
| 1173 | return (ret); |
| 1174 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1175 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1176 | static int spacePush(xmlParserCtxtPtr ctxt, int val) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1177 | if (ctxt->spaceNr >= ctxt->spaceMax) { |
| 1178 | ctxt->spaceMax *= 2; |
| 1179 | ctxt->spaceTab = (int *) xmlRealloc(ctxt->spaceTab, |
| 1180 | ctxt->spaceMax * sizeof(ctxt->spaceTab[0])); |
| 1181 | if (ctxt->spaceTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1182 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1183 | return(0); |
| 1184 | } |
| 1185 | } |
| 1186 | ctxt->spaceTab[ctxt->spaceNr] = val; |
| 1187 | ctxt->space = &ctxt->spaceTab[ctxt->spaceNr]; |
| 1188 | return(ctxt->spaceNr++); |
| 1189 | } |
| 1190 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1191 | static int spacePop(xmlParserCtxtPtr ctxt) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1192 | int ret; |
| 1193 | if (ctxt->spaceNr <= 0) return(0); |
| 1194 | ctxt->spaceNr--; |
| 1195 | if (ctxt->spaceNr > 0) |
| 1196 | ctxt->space = &ctxt->spaceTab[ctxt->spaceNr - 1]; |
| 1197 | else |
| 1198 | ctxt->space = NULL; |
| 1199 | ret = ctxt->spaceTab[ctxt->spaceNr]; |
| 1200 | ctxt->spaceTab[ctxt->spaceNr] = -1; |
| 1201 | return(ret); |
| 1202 | } |
| 1203 | |
| 1204 | /* |
| 1205 | * Macros for accessing the content. Those should be used only by the parser, |
| 1206 | * and not exported. |
| 1207 | * |
| 1208 | * Dirty macros, i.e. one often need to make assumption on the context to |
| 1209 | * use them |
| 1210 | * |
| 1211 | * CUR_PTR return the current pointer to the xmlChar to be parsed. |
| 1212 | * To be used with extreme caution since operations consuming |
| 1213 | * characters may move the input buffer to a different location ! |
| 1214 | * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled |
| 1215 | * This should be used internally by the parser |
| 1216 | * only to compare to ASCII values otherwise it would break when |
| 1217 | * running with UTF-8 encoding. |
| 1218 | * RAW same as CUR but in the input buffer, bypass any token |
| 1219 | * extraction that may have been done |
| 1220 | * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only |
| 1221 | * to compare on ASCII based substring. |
| 1222 | * 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] | 1223 | * strings without newlines within the parser. |
| 1224 | * NEXT1(l) Skip 1 xmlChar, and must also be used only to skip 1 non-newline ASCII |
| 1225 | * defined char within the parser. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1226 | * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding |
| 1227 | * |
| 1228 | * NEXT Skip to the next character, this does the proper decoding |
| 1229 | * 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] | 1230 | * NEXTL(l) Skip the current unicode character of l xmlChars long. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1231 | * CUR_CHAR(l) returns the current unicode character (int), set l |
| 1232 | * to the number of xmlChars used for the encoding [0-5]. |
| 1233 | * CUR_SCHAR same but operate on a string instead of the context |
| 1234 | * COPY_BUF copy the current unicode char to the target buffer, increment |
| 1235 | * the index |
| 1236 | * GROW, SHRINK handling of input buffers |
| 1237 | */ |
| 1238 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1239 | #define RAW (*ctxt->input->cur) |
| 1240 | #define CUR (*ctxt->input->cur) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1241 | #define NXT(val) ctxt->input->cur[(val)] |
| 1242 | #define CUR_PTR ctxt->input->cur |
| 1243 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 1244 | #define CMP4( s, c1, c2, c3, c4 ) \ |
| 1245 | ( ((unsigned char *) s)[ 0 ] == c1 && ((unsigned char *) s)[ 1 ] == c2 && \ |
| 1246 | ((unsigned char *) s)[ 2 ] == c3 && ((unsigned char *) s)[ 3 ] == c4 ) |
| 1247 | #define CMP5( s, c1, c2, c3, c4, c5 ) \ |
| 1248 | ( CMP4( s, c1, c2, c3, c4 ) && ((unsigned char *) s)[ 4 ] == c5 ) |
| 1249 | #define CMP6( s, c1, c2, c3, c4, c5, c6 ) \ |
| 1250 | ( CMP5( s, c1, c2, c3, c4, c5 ) && ((unsigned char *) s)[ 5 ] == c6 ) |
| 1251 | #define CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) \ |
| 1252 | ( CMP6( s, c1, c2, c3, c4, c5, c6 ) && ((unsigned char *) s)[ 6 ] == c7 ) |
| 1253 | #define CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) \ |
| 1254 | ( CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) && ((unsigned char *) s)[ 7 ] == c8 ) |
| 1255 | #define CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) \ |
| 1256 | ( CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) && \ |
| 1257 | ((unsigned char *) s)[ 8 ] == c9 ) |
| 1258 | #define CMP10( s, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 ) \ |
| 1259 | ( CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) && \ |
| 1260 | ((unsigned char *) s)[ 9 ] == c10 ) |
| 1261 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1262 | #define SKIP(val) do { \ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1263 | ctxt->nbChars += (val),ctxt->input->cur += (val),ctxt->input->col+=(val); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1264 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1265 | if ((*ctxt->input->cur == 0) && \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1266 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \ |
| 1267 | xmlPopInput(ctxt); \ |
| 1268 | } while (0) |
| 1269 | |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 1270 | #define SKIPL(val) do { \ |
| 1271 | int skipl; \ |
| 1272 | for(skipl=0; skipl<val; skipl++) { \ |
| 1273 | if (*(ctxt->input->cur) == '\n') { \ |
| 1274 | ctxt->input->line++; ctxt->input->col = 1; \ |
| 1275 | } else ctxt->input->col++; \ |
| 1276 | ctxt->nbChars++; \ |
| 1277 | ctxt->input->cur++; \ |
| 1278 | } \ |
| 1279 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
| 1280 | if ((*ctxt->input->cur == 0) && \ |
| 1281 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \ |
| 1282 | xmlPopInput(ctxt); \ |
| 1283 | } while (0) |
| 1284 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1285 | #define SHRINK if ((ctxt->progressive == 0) && \ |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 1286 | (ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \ |
| 1287 | (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1288 | xmlSHRINK (ctxt); |
| 1289 | |
| 1290 | static void xmlSHRINK (xmlParserCtxtPtr ctxt) { |
| 1291 | xmlParserInputShrink(ctxt->input); |
| 1292 | if ((*ctxt->input->cur == 0) && |
| 1293 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 1294 | xmlPopInput(ctxt); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 1295 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1296 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1297 | #define GROW if ((ctxt->progressive == 0) && \ |
| 1298 | (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 1299 | xmlGROW (ctxt); |
| 1300 | |
| 1301 | static void xmlGROW (xmlParserCtxtPtr ctxt) { |
| 1302 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 1303 | if ((*ctxt->input->cur == 0) && |
| 1304 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 1305 | xmlPopInput(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 1306 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1307 | |
| 1308 | #define SKIP_BLANKS xmlSkipBlankChars(ctxt) |
| 1309 | |
| 1310 | #define NEXT xmlNextChar(ctxt) |
| 1311 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 1312 | #define NEXT1 { \ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1313 | ctxt->input->col++; \ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 1314 | ctxt->input->cur++; \ |
| 1315 | ctxt->nbChars++; \ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1316 | if (*ctxt->input->cur == 0) \ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 1317 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); \ |
| 1318 | } |
| 1319 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1320 | #define NEXTL(l) do { \ |
| 1321 | if (*(ctxt->input->cur) == '\n') { \ |
| 1322 | ctxt->input->line++; ctxt->input->col = 1; \ |
| 1323 | } else ctxt->input->col++; \ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1324 | ctxt->input->cur += l; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1325 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1326 | } while (0) |
| 1327 | |
| 1328 | #define CUR_CHAR(l) xmlCurrentChar(ctxt, &l) |
| 1329 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l) |
| 1330 | |
| 1331 | #define COPY_BUF(l,b,i,v) \ |
| 1332 | if (l == 1) b[i++] = (xmlChar) v; \ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1333 | else i += xmlCopyCharMultiByte(&b[i],v) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1334 | |
| 1335 | /** |
| 1336 | * xmlSkipBlankChars: |
| 1337 | * @ctxt: the XML parser context |
| 1338 | * |
| 1339 | * skip all blanks character found at that point in the input streams. |
| 1340 | * It pops up finished entities in the process if allowable at that point. |
| 1341 | * |
| 1342 | * Returns the number of space chars skipped |
| 1343 | */ |
| 1344 | |
| 1345 | int |
| 1346 | xmlSkipBlankChars(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1347 | int res = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1348 | |
| 1349 | /* |
| 1350 | * It's Okay to use CUR/NEXT here since all the blanks are on |
| 1351 | * the ASCII range. |
| 1352 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1353 | if ((ctxt->inputNr == 1) && (ctxt->instate != XML_PARSER_DTD)) { |
| 1354 | const xmlChar *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1355 | /* |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1356 | * if we are in the document content, go really fast |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1357 | */ |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1358 | cur = ctxt->input->cur; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1359 | while (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 02141ea | 2001-04-30 11:46:40 +0000 | [diff] [blame] | 1360 | if (*cur == '\n') { |
| 1361 | ctxt->input->line++; ctxt->input->col = 1; |
| 1362 | } |
| 1363 | cur++; |
| 1364 | res++; |
| 1365 | if (*cur == 0) { |
| 1366 | ctxt->input->cur = cur; |
| 1367 | xmlParserInputGrow(ctxt->input, INPUT_CHUNK); |
| 1368 | cur = ctxt->input->cur; |
| 1369 | } |
| 1370 | } |
| 1371 | ctxt->input->cur = cur; |
| 1372 | } else { |
| 1373 | int cur; |
| 1374 | do { |
| 1375 | cur = CUR; |
| 1376 | while (IS_BLANK(cur)) { /* CHECKED tstblanks.xml */ |
| 1377 | NEXT; |
| 1378 | cur = CUR; |
| 1379 | res++; |
| 1380 | } |
| 1381 | while ((cur == 0) && (ctxt->inputNr > 1) && |
| 1382 | (ctxt->instate != XML_PARSER_COMMENT)) { |
| 1383 | xmlPopInput(ctxt); |
| 1384 | cur = CUR; |
| 1385 | } |
| 1386 | /* |
| 1387 | * Need to handle support of entities branching here |
| 1388 | */ |
| 1389 | if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); |
| 1390 | } while (IS_BLANK(cur)); /* CHECKED tstblanks.xml */ |
| 1391 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1392 | return(res); |
| 1393 | } |
| 1394 | |
| 1395 | /************************************************************************ |
| 1396 | * * |
| 1397 | * Commodity functions to handle entities * |
| 1398 | * * |
| 1399 | ************************************************************************/ |
| 1400 | |
| 1401 | /** |
| 1402 | * xmlPopInput: |
| 1403 | * @ctxt: an XML parser context |
| 1404 | * |
| 1405 | * xmlPopInput: the current input pointed by ctxt->input came to an end |
| 1406 | * pop it and return the next char. |
| 1407 | * |
| 1408 | * Returns the current xmlChar in the parser context |
| 1409 | */ |
| 1410 | xmlChar |
| 1411 | xmlPopInput(xmlParserCtxtPtr ctxt) { |
| 1412 | if (ctxt->inputNr == 1) return(0); /* End of main Input */ |
| 1413 | if (xmlParserDebugEntities) |
| 1414 | xmlGenericError(xmlGenericErrorContext, |
| 1415 | "Popping input %d\n", ctxt->inputNr); |
| 1416 | xmlFreeInputStream(inputPop(ctxt)); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1417 | if ((*ctxt->input->cur == 0) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1418 | (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) |
| 1419 | return(xmlPopInput(ctxt)); |
| 1420 | return(CUR); |
| 1421 | } |
| 1422 | |
| 1423 | /** |
| 1424 | * xmlPushInput: |
| 1425 | * @ctxt: an XML parser context |
| 1426 | * @input: an XML parser input fragment (entity, XML fragment ...). |
| 1427 | * |
| 1428 | * xmlPushInput: switch to a new input stream which is stacked on top |
| 1429 | * of the previous one(s). |
| 1430 | */ |
| 1431 | void |
| 1432 | xmlPushInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr input) { |
| 1433 | if (input == NULL) return; |
| 1434 | |
| 1435 | if (xmlParserDebugEntities) { |
| 1436 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 1437 | xmlGenericError(xmlGenericErrorContext, |
| 1438 | "%s(%d): ", ctxt->input->filename, |
| 1439 | ctxt->input->line); |
| 1440 | xmlGenericError(xmlGenericErrorContext, |
| 1441 | "Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur); |
| 1442 | } |
| 1443 | inputPush(ctxt, input); |
| 1444 | GROW; |
| 1445 | } |
| 1446 | |
| 1447 | /** |
| 1448 | * xmlParseCharRef: |
| 1449 | * @ctxt: an XML parser context |
| 1450 | * |
| 1451 | * parse Reference declarations |
| 1452 | * |
| 1453 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 1454 | * '&#x' [0-9a-fA-F]+ ';' |
| 1455 | * |
| 1456 | * [ WFC: Legal Character ] |
| 1457 | * Characters referred to using character references must match the |
| 1458 | * production for Char. |
| 1459 | * |
| 1460 | * Returns the value parsed (as an int), 0 in case of error |
| 1461 | */ |
| 1462 | int |
| 1463 | xmlParseCharRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 1464 | unsigned int val = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1465 | int count = 0; |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 1466 | unsigned int outofrange = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1467 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1468 | /* |
| 1469 | * Using RAW/CUR/NEXT is okay since we are working on ASCII range here |
| 1470 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1471 | if ((RAW == '&') && (NXT(1) == '#') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1472 | (NXT(2) == 'x')) { |
| 1473 | SKIP(3); |
| 1474 | GROW; |
| 1475 | while (RAW != ';') { /* loop blocked by count */ |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 1476 | if (count++ > 20) { |
| 1477 | count = 0; |
| 1478 | GROW; |
| 1479 | } |
| 1480 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1481 | val = val * 16 + (CUR - '0'); |
| 1482 | else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20)) |
| 1483 | val = val * 16 + (CUR - 'a') + 10; |
| 1484 | else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20)) |
| 1485 | val = val * 16 + (CUR - 'A') + 10; |
| 1486 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1487 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1488 | val = 0; |
| 1489 | break; |
| 1490 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 1491 | if (val > 0x10FFFF) |
| 1492 | outofrange = val; |
| 1493 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1494 | NEXT; |
| 1495 | count++; |
| 1496 | } |
| 1497 | if (RAW == ';') { |
| 1498 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1499 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1500 | ctxt->nbChars ++; |
| 1501 | ctxt->input->cur++; |
| 1502 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 1503 | } else if ((RAW == '&') && (NXT(1) == '#')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1504 | SKIP(2); |
| 1505 | GROW; |
| 1506 | while (RAW != ';') { /* loop blocked by count */ |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 1507 | if (count++ > 20) { |
| 1508 | count = 0; |
| 1509 | GROW; |
| 1510 | } |
| 1511 | if ((RAW >= '0') && (RAW <= '9')) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1512 | val = val * 10 + (CUR - '0'); |
| 1513 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1514 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1515 | val = 0; |
| 1516 | break; |
| 1517 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 1518 | if (val > 0x10FFFF) |
| 1519 | outofrange = val; |
| 1520 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1521 | NEXT; |
| 1522 | count++; |
| 1523 | } |
| 1524 | if (RAW == ';') { |
| 1525 | /* on purpose to avoid reentrancy problems with NEXT and SKIP */ |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 1526 | ctxt->input->col++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1527 | ctxt->nbChars ++; |
| 1528 | ctxt->input->cur++; |
| 1529 | } |
| 1530 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1531 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | /* |
| 1535 | * [ WFC: Legal Character ] |
| 1536 | * Characters referred to using character references must match the |
| 1537 | * production for Char. |
| 1538 | */ |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 1539 | if ((IS_CHAR(val) && (outofrange == 0))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1540 | return(val); |
| 1541 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 1542 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 1543 | "xmlParseCharRef: invalid xmlChar value %d\n", |
| 1544 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1545 | } |
| 1546 | return(0); |
| 1547 | } |
| 1548 | |
| 1549 | /** |
| 1550 | * xmlParseStringCharRef: |
| 1551 | * @ctxt: an XML parser context |
| 1552 | * @str: a pointer to an index in the string |
| 1553 | * |
| 1554 | * parse Reference declarations, variant parsing from a string rather |
| 1555 | * than an an input flow. |
| 1556 | * |
| 1557 | * [66] CharRef ::= '&#' [0-9]+ ';' | |
| 1558 | * '&#x' [0-9a-fA-F]+ ';' |
| 1559 | * |
| 1560 | * [ WFC: Legal Character ] |
| 1561 | * Characters referred to using character references must match the |
| 1562 | * production for Char. |
| 1563 | * |
| 1564 | * Returns the value parsed (as an int), 0 in case of error, str will be |
| 1565 | * updated to the current value of the index |
| 1566 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1567 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1568 | xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 1569 | const xmlChar *ptr; |
| 1570 | xmlChar cur; |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 1571 | unsigned int val = 0; |
| 1572 | unsigned int outofrange = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1573 | |
| 1574 | if ((str == NULL) || (*str == NULL)) return(0); |
| 1575 | ptr = *str; |
| 1576 | cur = *ptr; |
| 1577 | if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) { |
| 1578 | ptr += 3; |
| 1579 | cur = *ptr; |
| 1580 | while (cur != ';') { /* Non input consuming loop */ |
| 1581 | if ((cur >= '0') && (cur <= '9')) |
| 1582 | val = val * 16 + (cur - '0'); |
| 1583 | else if ((cur >= 'a') && (cur <= 'f')) |
| 1584 | val = val * 16 + (cur - 'a') + 10; |
| 1585 | else if ((cur >= 'A') && (cur <= 'F')) |
| 1586 | val = val * 16 + (cur - 'A') + 10; |
| 1587 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1588 | xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1589 | val = 0; |
| 1590 | break; |
| 1591 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 1592 | if (val > 0x10FFFF) |
| 1593 | outofrange = val; |
| 1594 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1595 | ptr++; |
| 1596 | cur = *ptr; |
| 1597 | } |
| 1598 | if (cur == ';') |
| 1599 | ptr++; |
| 1600 | } else if ((cur == '&') && (ptr[1] == '#')){ |
| 1601 | ptr += 2; |
| 1602 | cur = *ptr; |
| 1603 | while (cur != ';') { /* Non input consuming loops */ |
| 1604 | if ((cur >= '0') && (cur <= '9')) |
| 1605 | val = val * 10 + (cur - '0'); |
| 1606 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1607 | xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1608 | val = 0; |
| 1609 | break; |
| 1610 | } |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 1611 | if (val > 0x10FFFF) |
| 1612 | outofrange = val; |
| 1613 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1614 | ptr++; |
| 1615 | cur = *ptr; |
| 1616 | } |
| 1617 | if (cur == ';') |
| 1618 | ptr++; |
| 1619 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1620 | xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1621 | return(0); |
| 1622 | } |
| 1623 | *str = ptr; |
| 1624 | |
| 1625 | /* |
| 1626 | * [ WFC: Legal Character ] |
| 1627 | * Characters referred to using character references must match the |
| 1628 | * production for Char. |
| 1629 | */ |
Daniel Veillard | 37fd307 | 2004-06-03 11:22:31 +0000 | [diff] [blame] | 1630 | if ((IS_CHAR(val) && (outofrange == 0))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1631 | return(val); |
| 1632 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 1633 | xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR, |
| 1634 | "xmlParseStringCharRef: invalid xmlChar value %d\n", |
| 1635 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1636 | } |
| 1637 | return(0); |
| 1638 | } |
| 1639 | |
| 1640 | /** |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1641 | * xmlNewBlanksWrapperInputStream: |
| 1642 | * @ctxt: an XML parser context |
| 1643 | * @entity: an Entity pointer |
| 1644 | * |
| 1645 | * Create a new input stream for wrapping |
| 1646 | * blanks around a PEReference |
| 1647 | * |
| 1648 | * Returns the new input stream or NULL |
| 1649 | */ |
| 1650 | |
| 1651 | static void deallocblankswrapper (xmlChar *str) {xmlFree(str);} |
| 1652 | |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 1653 | static xmlParserInputPtr |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1654 | xmlNewBlanksWrapperInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) { |
| 1655 | xmlParserInputPtr input; |
| 1656 | xmlChar *buffer; |
| 1657 | size_t length; |
| 1658 | if (entity == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1659 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1660 | "xmlNewBlanksWrapperInputStream entity\n"); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1661 | return(NULL); |
| 1662 | } |
| 1663 | if (xmlParserDebugEntities) |
| 1664 | xmlGenericError(xmlGenericErrorContext, |
| 1665 | "new blanks wrapper for entity: %s\n", entity->name); |
| 1666 | input = xmlNewInputStream(ctxt); |
| 1667 | if (input == NULL) { |
| 1668 | return(NULL); |
| 1669 | } |
| 1670 | length = xmlStrlen(entity->name) + 5; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 1671 | buffer = xmlMallocAtomic(length); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1672 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1673 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1674 | return(NULL); |
| 1675 | } |
| 1676 | buffer [0] = ' '; |
| 1677 | buffer [1] = '%'; |
| 1678 | buffer [length-3] = ';'; |
| 1679 | buffer [length-2] = ' '; |
| 1680 | buffer [length-1] = 0; |
| 1681 | memcpy(buffer + 2, entity->name, length - 5); |
| 1682 | input->free = deallocblankswrapper; |
| 1683 | input->base = buffer; |
| 1684 | input->cur = buffer; |
| 1685 | input->length = length; |
| 1686 | input->end = &buffer[length]; |
| 1687 | return(input); |
| 1688 | } |
| 1689 | |
| 1690 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1691 | * xmlParserHandlePEReference: |
| 1692 | * @ctxt: the parser context |
| 1693 | * |
| 1694 | * [69] PEReference ::= '%' Name ';' |
| 1695 | * |
| 1696 | * [ WFC: No Recursion ] |
| 1697 | * A parsed entity must not contain a recursive |
| 1698 | * reference to itself, either directly or indirectly. |
| 1699 | * |
| 1700 | * [ WFC: Entity Declared ] |
| 1701 | * In a document without any DTD, a document with only an internal DTD |
| 1702 | * subset which contains no parameter entity references, or a document |
| 1703 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 1704 | * entity must precede any reference to it... |
| 1705 | * |
| 1706 | * [ VC: Entity Declared ] |
| 1707 | * In a document with an external subset or external parameter entities |
| 1708 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 1709 | * must precede any reference to it... |
| 1710 | * |
| 1711 | * [ WFC: In DTD ] |
| 1712 | * Parameter-entity references may only appear in the DTD. |
| 1713 | * NOTE: misleading but this is handled. |
| 1714 | * |
| 1715 | * A PEReference may have been detected in the current input stream |
| 1716 | * the handling is done accordingly to |
| 1717 | * http://www.w3.org/TR/REC-xml#entproc |
| 1718 | * i.e. |
| 1719 | * - Included in literal in entity values |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1720 | * - Included as Parameter Entity reference within DTDs |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1721 | */ |
| 1722 | void |
| 1723 | xmlParserHandlePEReference(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 1724 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1725 | xmlEntityPtr entity = NULL; |
| 1726 | xmlParserInputPtr input; |
| 1727 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1728 | if (RAW != '%') return; |
| 1729 | switch(ctxt->instate) { |
| 1730 | case XML_PARSER_CDATA_SECTION: |
| 1731 | return; |
| 1732 | case XML_PARSER_COMMENT: |
| 1733 | return; |
| 1734 | case XML_PARSER_START_TAG: |
| 1735 | return; |
| 1736 | case XML_PARSER_END_TAG: |
| 1737 | return; |
| 1738 | case XML_PARSER_EOF: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1739 | xmlFatalErr(ctxt, XML_ERR_PEREF_AT_EOF, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1740 | return; |
| 1741 | case XML_PARSER_PROLOG: |
| 1742 | case XML_PARSER_START: |
| 1743 | case XML_PARSER_MISC: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1744 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_PROLOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1745 | return; |
| 1746 | case XML_PARSER_ENTITY_DECL: |
| 1747 | case XML_PARSER_CONTENT: |
| 1748 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 1749 | case XML_PARSER_PI: |
| 1750 | case XML_PARSER_SYSTEM_LITERAL: |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 1751 | case XML_PARSER_PUBLIC_LITERAL: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1752 | /* we just ignore it there */ |
| 1753 | return; |
| 1754 | case XML_PARSER_EPILOG: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1755 | xmlFatalErr(ctxt, XML_ERR_PEREF_IN_EPILOG, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1756 | return; |
| 1757 | case XML_PARSER_ENTITY_VALUE: |
| 1758 | /* |
| 1759 | * NOTE: in the case of entity values, we don't do the |
| 1760 | * substitution here since we need the literal |
| 1761 | * entity value to be able to save the internal |
| 1762 | * subset of the document. |
| 1763 | * This will be handled by xmlStringDecodeEntities |
| 1764 | */ |
| 1765 | return; |
| 1766 | case XML_PARSER_DTD: |
| 1767 | /* |
| 1768 | * [WFC: Well-Formedness Constraint: PEs in Internal Subset] |
| 1769 | * In the internal DTD subset, parameter-entity references |
| 1770 | * can occur only where markup declarations can occur, not |
| 1771 | * within markup declarations. |
| 1772 | * In that case this is handled in xmlParseMarkupDecl |
| 1773 | */ |
| 1774 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 1775 | return; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1776 | if (IS_BLANK_CH(NXT(1)) || NXT(1) == 0) |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1777 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1778 | break; |
| 1779 | case XML_PARSER_IGNORE: |
| 1780 | return; |
| 1781 | } |
| 1782 | |
| 1783 | NEXT; |
| 1784 | name = xmlParseName(ctxt); |
| 1785 | if (xmlParserDebugEntities) |
| 1786 | xmlGenericError(xmlGenericErrorContext, |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1787 | "PEReference: %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1788 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1789 | xmlFatalErr(ctxt, XML_ERR_PEREF_NO_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1790 | } else { |
| 1791 | if (RAW == ';') { |
| 1792 | NEXT; |
| 1793 | if ((ctxt->sax != NULL) && (ctxt->sax->getParameterEntity != NULL)) |
| 1794 | entity = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 1795 | if (entity == NULL) { |
| 1796 | |
| 1797 | /* |
| 1798 | * [ WFC: Entity Declared ] |
| 1799 | * In a document without any DTD, a document with only an |
| 1800 | * internal DTD subset which contains no parameter entity |
| 1801 | * references, or a document with "standalone='yes'", ... |
| 1802 | * ... The declaration of a parameter entity must precede |
| 1803 | * any reference to it... |
| 1804 | */ |
| 1805 | if ((ctxt->standalone == 1) || |
| 1806 | ((ctxt->hasExternalSubset == 0) && |
| 1807 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 1808 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1809 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1810 | } else { |
| 1811 | /* |
| 1812 | * [ VC: Entity Declared ] |
| 1813 | * In a document with an external subset or external |
| 1814 | * parameter entities with "standalone='no'", ... |
| 1815 | * ... The declaration of a parameter entity must precede |
| 1816 | * any reference to it... |
| 1817 | */ |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 1818 | if ((ctxt->validate) && (ctxt->vctxt.error != NULL)) { |
| 1819 | xmlValidityError(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 1820 | "PEReference: %%%s; not found\n", |
| 1821 | name); |
| 1822 | } else |
| 1823 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 1824 | "PEReference: %%%s; not found\n", |
| 1825 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1826 | ctxt->valid = 0; |
| 1827 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 1828 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 1829 | input = xmlNewBlanksWrapperInputStream(ctxt, entity); |
| 1830 | xmlPushInput(ctxt, input); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1831 | } else { |
| 1832 | if ((entity->etype == XML_INTERNAL_PARAMETER_ENTITY) || |
| 1833 | (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY)) { |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 1834 | xmlChar start[4]; |
| 1835 | xmlCharEncoding enc; |
| 1836 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1837 | /* |
| 1838 | * handle the extra spaces added before and after |
| 1839 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1840 | * this is done independently. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1841 | */ |
| 1842 | input = xmlNewEntityInputStream(ctxt, entity); |
| 1843 | xmlPushInput(ctxt, input); |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 1844 | |
| 1845 | /* |
| 1846 | * Get the 4 first bytes and decode the charset |
| 1847 | * if enc != XML_CHAR_ENCODING_NONE |
| 1848 | * plug some encoding conversion routines. |
William M. Brack | a0c48ad | 2004-04-16 15:58:29 +0000 | [diff] [blame] | 1849 | * Note that, since we may have some non-UTF8 |
| 1850 | * encoding (like UTF16, bug 135229), the 'length' |
| 1851 | * is not known, but we can calculate based upon |
| 1852 | * the amount of data in the buffer. |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 1853 | */ |
| 1854 | GROW |
William M. Brack | a0c48ad | 2004-04-16 15:58:29 +0000 | [diff] [blame] | 1855 | if ((ctxt->input->end - ctxt->input->cur)>=4) { |
Daniel Veillard | e059b89 | 2002-06-13 15:32:10 +0000 | [diff] [blame] | 1856 | start[0] = RAW; |
| 1857 | start[1] = NXT(1); |
| 1858 | start[2] = NXT(2); |
| 1859 | start[3] = NXT(3); |
| 1860 | enc = xmlDetectCharEncoding(start, 4); |
| 1861 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 1862 | xmlSwitchEncoding(ctxt, enc); |
| 1863 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1866 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 1867 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l' )) && |
| 1868 | (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1869 | xmlParseTextDecl(ctxt); |
| 1870 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1871 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 1872 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 1873 | "PEReference: %s is not a parameter entity\n", |
| 1874 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1875 | } |
| 1876 | } |
| 1877 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1878 | xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1879 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | /* |
| 1884 | * Macro used to grow the current buffer. |
| 1885 | */ |
| 1886 | #define growBuffer(buffer) { \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 1887 | xmlChar *tmp; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1888 | buffer##_size *= 2; \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 1889 | tmp = (xmlChar *) \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1890 | xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \ |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 1891 | if (tmp == NULL) goto mem_error; \ |
| 1892 | buffer = tmp; \ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | /** |
Daniel Veillard | 7a02cfe | 2003-09-25 12:18:34 +0000 | [diff] [blame] | 1896 | * xmlStringLenDecodeEntities: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1897 | * @ctxt: the parser context |
| 1898 | * @str: the input string |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1899 | * @len: the string length |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1900 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 1901 | * @end: an end marker xmlChar, 0 if none |
| 1902 | * @end2: an end marker xmlChar, 0 if none |
| 1903 | * @end3: an end marker xmlChar, 0 if none |
| 1904 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1905 | * Takes a entity string content and process to do the adequate substitutions. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1906 | * |
| 1907 | * [67] Reference ::= EntityRef | CharRef |
| 1908 | * |
| 1909 | * [69] PEReference ::= '%' Name ';' |
| 1910 | * |
| 1911 | * Returns A newly allocated string with the substitution done. The caller |
| 1912 | * must deallocate it ! |
| 1913 | */ |
| 1914 | xmlChar * |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1915 | xmlStringLenDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, |
| 1916 | int what, xmlChar end, xmlChar end2, xmlChar end3) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1917 | xmlChar *buffer = NULL; |
| 1918 | int buffer_size = 0; |
| 1919 | |
| 1920 | xmlChar *current = NULL; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1921 | const xmlChar *last; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1922 | xmlEntityPtr ent; |
| 1923 | int c,l; |
| 1924 | int nbchars = 0; |
| 1925 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1926 | if ((str == NULL) || (len < 0)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1927 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1928 | last = str + len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1929 | |
| 1930 | if (ctxt->depth > 40) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 1931 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1932 | return(NULL); |
| 1933 | } |
| 1934 | |
| 1935 | /* |
| 1936 | * allocate a translation buffer. |
| 1937 | */ |
| 1938 | buffer_size = XML_PARSER_BIG_BUFFER_SIZE; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 1939 | buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar)); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1940 | if (buffer == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1941 | |
| 1942 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 1943 | * 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] | 1944 | * we are operating on already parsed values. |
| 1945 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 1946 | if (str < last) |
| 1947 | c = CUR_SCHAR(str, l); |
| 1948 | else |
| 1949 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1950 | while ((c != 0) && (c != end) && /* non input consuming loop */ |
| 1951 | (c != end2) && (c != end3)) { |
| 1952 | |
| 1953 | if (c == 0) break; |
| 1954 | if ((c == '&') && (str[1] == '#')) { |
| 1955 | int val = xmlParseStringCharRef(ctxt, &str); |
| 1956 | if (val != 0) { |
| 1957 | COPY_BUF(0,buffer,nbchars,val); |
| 1958 | } |
| 1959 | } else if ((c == '&') && (what & XML_SUBSTITUTE_REF)) { |
| 1960 | if (xmlParserDebugEntities) |
| 1961 | xmlGenericError(xmlGenericErrorContext, |
| 1962 | "String decoding Entity Reference: %.30s\n", |
| 1963 | str); |
| 1964 | ent = xmlParseStringEntityRef(ctxt, &str); |
| 1965 | if ((ent != NULL) && |
| 1966 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 1967 | if (ent->content != NULL) { |
| 1968 | COPY_BUF(0,buffer,nbchars,ent->content[0]); |
| 1969 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 1970 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 1971 | "predefined entity has no content\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1972 | } |
| 1973 | } else if ((ent != NULL) && (ent->content != NULL)) { |
| 1974 | xmlChar *rep; |
| 1975 | |
| 1976 | ctxt->depth++; |
| 1977 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 1978 | 0, 0, 0); |
| 1979 | ctxt->depth--; |
| 1980 | if (rep != NULL) { |
| 1981 | current = rep; |
| 1982 | while (*current != 0) { /* non input consuming loop */ |
| 1983 | buffer[nbchars++] = *current++; |
| 1984 | if (nbchars > |
| 1985 | buffer_size - XML_PARSER_BUFFER_SIZE) { |
| 1986 | growBuffer(buffer); |
| 1987 | } |
| 1988 | } |
| 1989 | xmlFree(rep); |
| 1990 | } |
| 1991 | } else if (ent != NULL) { |
| 1992 | int i = xmlStrlen(ent->name); |
| 1993 | const xmlChar *cur = ent->name; |
| 1994 | |
| 1995 | buffer[nbchars++] = '&'; |
| 1996 | if (nbchars > buffer_size - i - XML_PARSER_BUFFER_SIZE) { |
| 1997 | growBuffer(buffer); |
| 1998 | } |
| 1999 | for (;i > 0;i--) |
| 2000 | buffer[nbchars++] = *cur++; |
| 2001 | buffer[nbchars++] = ';'; |
| 2002 | } |
| 2003 | } else if (c == '%' && (what & XML_SUBSTITUTE_PEREF)) { |
| 2004 | if (xmlParserDebugEntities) |
| 2005 | xmlGenericError(xmlGenericErrorContext, |
| 2006 | "String decoding PE Reference: %.30s\n", str); |
| 2007 | ent = xmlParseStringPEReference(ctxt, &str); |
| 2008 | if (ent != NULL) { |
| 2009 | xmlChar *rep; |
| 2010 | |
| 2011 | ctxt->depth++; |
| 2012 | rep = xmlStringDecodeEntities(ctxt, ent->content, what, |
| 2013 | 0, 0, 0); |
| 2014 | ctxt->depth--; |
| 2015 | if (rep != NULL) { |
| 2016 | current = rep; |
| 2017 | while (*current != 0) { /* non input consuming loop */ |
| 2018 | buffer[nbchars++] = *current++; |
| 2019 | if (nbchars > |
| 2020 | buffer_size - XML_PARSER_BUFFER_SIZE) { |
| 2021 | growBuffer(buffer); |
| 2022 | } |
| 2023 | } |
| 2024 | xmlFree(rep); |
| 2025 | } |
| 2026 | } |
| 2027 | } else { |
| 2028 | COPY_BUF(l,buffer,nbchars,c); |
| 2029 | str += l; |
| 2030 | if (nbchars > buffer_size - XML_PARSER_BUFFER_SIZE) { |
| 2031 | growBuffer(buffer); |
| 2032 | } |
| 2033 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2034 | if (str < last) |
| 2035 | c = CUR_SCHAR(str, l); |
| 2036 | else |
| 2037 | c = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2038 | } |
| 2039 | buffer[nbchars++] = 0; |
| 2040 | return(buffer); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2041 | |
| 2042 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2043 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2044 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2047 | /** |
| 2048 | * xmlStringDecodeEntities: |
| 2049 | * @ctxt: the parser context |
| 2050 | * @str: the input string |
| 2051 | * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF |
| 2052 | * @end: an end marker xmlChar, 0 if none |
| 2053 | * @end2: an end marker xmlChar, 0 if none |
| 2054 | * @end3: an end marker xmlChar, 0 if none |
| 2055 | * |
| 2056 | * Takes a entity string content and process to do the adequate substitutions. |
| 2057 | * |
| 2058 | * [67] Reference ::= EntityRef | CharRef |
| 2059 | * |
| 2060 | * [69] PEReference ::= '%' Name ';' |
| 2061 | * |
| 2062 | * Returns A newly allocated string with the substitution done. The caller |
| 2063 | * must deallocate it ! |
| 2064 | */ |
| 2065 | xmlChar * |
| 2066 | xmlStringDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int what, |
| 2067 | xmlChar end, xmlChar end2, xmlChar end3) { |
| 2068 | return(xmlStringLenDecodeEntities(ctxt, str, xmlStrlen(str), what, |
| 2069 | end, end2, end3)); |
| 2070 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2071 | |
| 2072 | /************************************************************************ |
| 2073 | * * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2074 | * Commodity functions, cleanup needed ? * |
| 2075 | * * |
| 2076 | ************************************************************************/ |
| 2077 | |
| 2078 | /** |
| 2079 | * areBlanks: |
| 2080 | * @ctxt: an XML parser context |
| 2081 | * @str: a xmlChar * |
| 2082 | * @len: the size of @str |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2083 | * @blank_chars: we know the chars are blanks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2084 | * |
| 2085 | * Is this a sequence of blank chars that one can ignore ? |
| 2086 | * |
| 2087 | * Returns 1 if ignorable 0 otherwise. |
| 2088 | */ |
| 2089 | |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2090 | static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len, |
| 2091 | int blank_chars) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2092 | int i, ret; |
| 2093 | xmlNodePtr lastChild; |
| 2094 | |
Daniel Veillard | 05c13a2 | 2001-09-09 08:38:09 +0000 | [diff] [blame] | 2095 | /* |
| 2096 | * Don't spend time trying to differentiate them, the same callback is |
| 2097 | * used ! |
| 2098 | */ |
| 2099 | if (ctxt->sax->ignorableWhitespace == ctxt->sax->characters) |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 2100 | return(0); |
| 2101 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2102 | /* |
| 2103 | * Check for xml:space value. |
| 2104 | */ |
| 2105 | if (*(ctxt->space) == 1) |
| 2106 | return(0); |
| 2107 | |
| 2108 | /* |
| 2109 | * Check that the string is made of blanks |
| 2110 | */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2111 | if (blank_chars == 0) { |
| 2112 | for (i = 0;i < len;i++) |
| 2113 | if (!(IS_BLANK_CH(str[i]))) return(0); |
| 2114 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2115 | |
| 2116 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2117 | * Look if the element is mixed content in the DTD if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2118 | */ |
Daniel Veillard | 6dd398f | 2001-07-25 22:41:03 +0000 | [diff] [blame] | 2119 | if (ctxt->node == NULL) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2120 | if (ctxt->myDoc != NULL) { |
| 2121 | ret = xmlIsMixedElement(ctxt->myDoc, ctxt->node->name); |
| 2122 | if (ret == 0) return(1); |
| 2123 | if (ret == 1) return(0); |
| 2124 | } |
| 2125 | |
| 2126 | /* |
| 2127 | * Otherwise, heuristic :-\ |
| 2128 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 2129 | if (RAW != '<') return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2130 | if ((ctxt->node->children == NULL) && |
| 2131 | (RAW == '<') && (NXT(1) == '/')) return(0); |
| 2132 | |
| 2133 | lastChild = xmlGetLastChild(ctxt->node); |
| 2134 | if (lastChild == NULL) { |
Daniel Veillard | 7db3773 | 2001-07-12 01:20:08 +0000 | [diff] [blame] | 2135 | if ((ctxt->node->type != XML_ELEMENT_NODE) && |
| 2136 | (ctxt->node->content != NULL)) return(0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2137 | } else if (xmlNodeIsText(lastChild)) |
| 2138 | return(0); |
| 2139 | else if ((ctxt->node->children != NULL) && |
| 2140 | (xmlNodeIsText(ctxt->node->children))) |
| 2141 | return(0); |
| 2142 | return(1); |
| 2143 | } |
| 2144 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2145 | /************************************************************************ |
| 2146 | * * |
| 2147 | * Extra stuff for namespace support * |
| 2148 | * Relates to http://www.w3.org/TR/WD-xml-names * |
| 2149 | * * |
| 2150 | ************************************************************************/ |
| 2151 | |
| 2152 | /** |
| 2153 | * xmlSplitQName: |
| 2154 | * @ctxt: an XML parser context |
| 2155 | * @name: an XML parser context |
| 2156 | * @prefix: a xmlChar ** |
| 2157 | * |
| 2158 | * parse an UTF8 encoded XML qualified name string |
| 2159 | * |
| 2160 | * [NS 5] QName ::= (Prefix ':')? LocalPart |
| 2161 | * |
| 2162 | * [NS 6] Prefix ::= NCName |
| 2163 | * |
| 2164 | * [NS 7] LocalPart ::= NCName |
| 2165 | * |
| 2166 | * Returns the local part, and prefix is updated |
| 2167 | * to get the Prefix if any. |
| 2168 | */ |
| 2169 | |
| 2170 | xmlChar * |
| 2171 | xmlSplitQName(xmlParserCtxtPtr ctxt, const xmlChar *name, xmlChar **prefix) { |
| 2172 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 2173 | xmlChar *buffer = NULL; |
| 2174 | int len = 0; |
| 2175 | int max = XML_MAX_NAMELEN; |
| 2176 | xmlChar *ret = NULL; |
| 2177 | const xmlChar *cur = name; |
| 2178 | int c; |
| 2179 | |
| 2180 | *prefix = NULL; |
| 2181 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2182 | if (cur == NULL) return(NULL); |
| 2183 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2184 | #ifndef XML_XML_NAMESPACE |
| 2185 | /* xml: prefix is not really a namespace */ |
| 2186 | if ((cur[0] == 'x') && (cur[1] == 'm') && |
| 2187 | (cur[2] == 'l') && (cur[3] == ':')) |
| 2188 | return(xmlStrdup(name)); |
| 2189 | #endif |
| 2190 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2191 | /* nasty but well=formed */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2192 | if (cur[0] == ':') |
| 2193 | return(xmlStrdup(name)); |
| 2194 | |
| 2195 | c = *cur++; |
| 2196 | while ((c != 0) && (c != ':') && (len < max)) { /* tested bigname.xml */ |
| 2197 | buf[len++] = c; |
| 2198 | c = *cur++; |
| 2199 | } |
| 2200 | if (len >= max) { |
| 2201 | /* |
| 2202 | * Okay someone managed to make a huge name, so he's ready to pay |
| 2203 | * for the processing speed. |
| 2204 | */ |
| 2205 | max = len * 2; |
| 2206 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2207 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2208 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2209 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2210 | return(NULL); |
| 2211 | } |
| 2212 | memcpy(buffer, buf, len); |
| 2213 | while ((c != 0) && (c != ':')) { /* tested bigname.xml */ |
| 2214 | if (len + 10 > max) { |
| 2215 | max *= 2; |
| 2216 | buffer = (xmlChar *) xmlRealloc(buffer, |
| 2217 | max * sizeof(xmlChar)); |
| 2218 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2219 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2220 | return(NULL); |
| 2221 | } |
| 2222 | } |
| 2223 | buffer[len++] = c; |
| 2224 | c = *cur++; |
| 2225 | } |
| 2226 | buffer[len] = 0; |
| 2227 | } |
| 2228 | |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2229 | /* nasty but well=formed |
| 2230 | if ((c == ':') && (*cur == 0)) { |
| 2231 | return(xmlStrdup(name)); |
| 2232 | } */ |
| 2233 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2234 | if (buffer == NULL) |
| 2235 | ret = xmlStrndup(buf, len); |
| 2236 | else { |
| 2237 | ret = buffer; |
| 2238 | buffer = NULL; |
| 2239 | max = XML_MAX_NAMELEN; |
| 2240 | } |
| 2241 | |
| 2242 | |
| 2243 | if (c == ':') { |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2244 | c = *cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2245 | *prefix = ret; |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2246 | if (c == 0) { |
Daniel Veillard | 8d73bcb | 2003-08-04 01:06:15 +0000 | [diff] [blame] | 2247 | return(xmlStrndup(BAD_CAST "", 0)); |
Daniel Veillard | 597bc48 | 2003-07-24 16:08:28 +0000 | [diff] [blame] | 2248 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2249 | len = 0; |
| 2250 | |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2251 | /* |
| 2252 | * Check that the first character is proper to start |
| 2253 | * a new name |
| 2254 | */ |
| 2255 | if (!(((c >= 0x61) && (c <= 0x7A)) || |
| 2256 | ((c >= 0x41) && (c <= 0x5A)) || |
| 2257 | (c == '_') || (c == ':'))) { |
| 2258 | int l; |
| 2259 | int first = CUR_SCHAR(cur, l); |
| 2260 | |
| 2261 | if (!IS_LETTER(first) && (first != '_')) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2262 | xmlFatalErrMsgStr(ctxt, XML_NS_ERR_QNAME, |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2263 | "Name %s is not XML Namespace compliant\n", |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 2264 | name); |
Daniel Veillard | bb284f4 | 2002-10-16 18:02:47 +0000 | [diff] [blame] | 2265 | } |
| 2266 | } |
| 2267 | cur++; |
| 2268 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2269 | while ((c != 0) && (len < max)) { /* tested bigname2.xml */ |
| 2270 | buf[len++] = c; |
| 2271 | c = *cur++; |
| 2272 | } |
| 2273 | if (len >= max) { |
| 2274 | /* |
| 2275 | * Okay someone managed to make a huge name, so he's ready to pay |
| 2276 | * for the processing speed. |
| 2277 | */ |
| 2278 | max = len * 2; |
| 2279 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2280 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2281 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2282 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2283 | return(NULL); |
| 2284 | } |
| 2285 | memcpy(buffer, buf, len); |
| 2286 | while (c != 0) { /* tested bigname2.xml */ |
| 2287 | if (len + 10 > max) { |
| 2288 | max *= 2; |
| 2289 | buffer = (xmlChar *) xmlRealloc(buffer, |
| 2290 | max * sizeof(xmlChar)); |
| 2291 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2292 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2293 | return(NULL); |
| 2294 | } |
| 2295 | } |
| 2296 | buffer[len++] = c; |
| 2297 | c = *cur++; |
| 2298 | } |
| 2299 | buffer[len] = 0; |
| 2300 | } |
| 2301 | |
| 2302 | if (buffer == NULL) |
| 2303 | ret = xmlStrndup(buf, len); |
| 2304 | else { |
| 2305 | ret = buffer; |
| 2306 | } |
| 2307 | } |
| 2308 | |
| 2309 | return(ret); |
| 2310 | } |
| 2311 | |
| 2312 | /************************************************************************ |
| 2313 | * * |
| 2314 | * The parser itself * |
| 2315 | * Relates to http://www.w3.org/TR/REC-xml * |
| 2316 | * * |
| 2317 | ************************************************************************/ |
| 2318 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2319 | static const xmlChar * xmlParseNameComplex(xmlParserCtxtPtr ctxt); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2320 | static xmlChar * xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2321 | int *len, int *alloc, int normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2322 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2323 | /** |
| 2324 | * xmlParseName: |
| 2325 | * @ctxt: an XML parser context |
| 2326 | * |
| 2327 | * parse an XML name. |
| 2328 | * |
| 2329 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 2330 | * CombiningChar | Extender |
| 2331 | * |
| 2332 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 2333 | * |
| 2334 | * [6] Names ::= Name (S Name)* |
| 2335 | * |
| 2336 | * Returns the Name parsed or NULL |
| 2337 | */ |
| 2338 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2339 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2340 | xmlParseName(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2341 | const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2342 | const xmlChar *ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2343 | int count = 0; |
| 2344 | |
| 2345 | GROW; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2346 | |
| 2347 | /* |
| 2348 | * Accelerator for simple ASCII names |
| 2349 | */ |
| 2350 | in = ctxt->input->cur; |
| 2351 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 2352 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 2353 | (*in == '_') || (*in == ':')) { |
| 2354 | in++; |
| 2355 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 2356 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 2357 | ((*in >= 0x30) && (*in <= 0x39)) || |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 2358 | (*in == '_') || (*in == '-') || |
| 2359 | (*in == ':') || (*in == '.')) |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2360 | in++; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 2361 | if ((*in > 0) && (*in < 0x80)) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2362 | count = in - ctxt->input->cur; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2363 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2364 | ctxt->input->cur = in; |
Daniel Veillard | 77a90a7 | 2003-03-22 00:04:05 +0000 | [diff] [blame] | 2365 | ctxt->nbChars += count; |
| 2366 | ctxt->input->col += count; |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2367 | if (ret == NULL) |
| 2368 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2369 | return(ret); |
| 2370 | } |
| 2371 | } |
Daniel Veillard | 2f36224 | 2001-03-02 17:36:21 +0000 | [diff] [blame] | 2372 | return(xmlParseNameComplex(ctxt)); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2373 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 2374 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2375 | /** |
| 2376 | * xmlParseNameAndCompare: |
| 2377 | * @ctxt: an XML parser context |
| 2378 | * |
| 2379 | * parse an XML name and compares for match |
| 2380 | * (specialized for endtag parsing) |
| 2381 | * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2382 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 2383 | * and the name for mismatch |
| 2384 | */ |
| 2385 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2386 | static const xmlChar * |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2387 | xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 2388 | register const xmlChar *cmp = other; |
| 2389 | register const xmlChar *in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2390 | const xmlChar *ret; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2391 | |
| 2392 | GROW; |
| 2393 | |
| 2394 | in = ctxt->input->cur; |
| 2395 | while (*in != 0 && *in == *cmp) { |
| 2396 | ++in; |
| 2397 | ++cmp; |
| 2398 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2399 | if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) { |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2400 | /* success */ |
| 2401 | ctxt->input->cur = in; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2402 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2403 | } |
| 2404 | /* failure (or end of input buffer), check with full function */ |
| 2405 | ret = xmlParseName (ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2406 | /* strings coming from the dictionnary direct compare possible */ |
| 2407 | if (ret == other) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2408 | return (const xmlChar*) 1; |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 2409 | } |
| 2410 | return ret; |
| 2411 | } |
| 2412 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2413 | static const xmlChar * |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2414 | xmlParseNameComplex(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 2415 | int len = 0, l; |
| 2416 | int c; |
| 2417 | int count = 0; |
| 2418 | |
| 2419 | /* |
| 2420 | * Handler for more complex cases |
| 2421 | */ |
| 2422 | GROW; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2423 | c = CUR_CHAR(l); |
| 2424 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
| 2425 | (!IS_LETTER(c) && (c != '_') && |
| 2426 | (c != ':'))) { |
| 2427 | return(NULL); |
| 2428 | } |
| 2429 | |
| 2430 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2431 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2432 | (c == '.') || (c == '-') || |
| 2433 | (c == '_') || (c == ':') || |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2434 | (IS_COMBINING(c)) || |
| 2435 | (IS_EXTENDER(c)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2436 | if (count++ > 100) { |
| 2437 | count = 0; |
| 2438 | GROW; |
| 2439 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2440 | len += l; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2441 | NEXTL(l); |
| 2442 | c = CUR_CHAR(l); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2443 | } |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 2444 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
| 2447 | /** |
| 2448 | * xmlParseStringName: |
| 2449 | * @ctxt: an XML parser context |
| 2450 | * @str: a pointer to the string pointer (IN/OUT) |
| 2451 | * |
| 2452 | * parse an XML name. |
| 2453 | * |
| 2454 | * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | |
| 2455 | * CombiningChar | Extender |
| 2456 | * |
| 2457 | * [5] Name ::= (Letter | '_' | ':') (NameChar)* |
| 2458 | * |
| 2459 | * [6] Names ::= Name (S Name)* |
| 2460 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2461 | * Returns the Name parsed or NULL. The @str pointer |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2462 | * is updated to the current location in the string. |
| 2463 | */ |
| 2464 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 2465 | static xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2466 | xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) { |
| 2467 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 2468 | const xmlChar *cur = *str; |
| 2469 | int len = 0, l; |
| 2470 | int c; |
| 2471 | |
| 2472 | c = CUR_SCHAR(cur, l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2473 | if (!IS_LETTER(c) && (c != '_') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2474 | (c != ':')) { |
| 2475 | return(NULL); |
| 2476 | } |
| 2477 | |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2478 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigentname.xml */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2479 | (c == '.') || (c == '-') || |
| 2480 | (c == '_') || (c == ':') || |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2481 | (IS_COMBINING(c)) || |
| 2482 | (IS_EXTENDER(c))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2483 | COPY_BUF(l,buf,len,c); |
| 2484 | cur += l; |
| 2485 | c = CUR_SCHAR(cur, l); |
| 2486 | if (len >= XML_MAX_NAMELEN) { /* test bigentname.xml */ |
| 2487 | /* |
| 2488 | * Okay someone managed to make a huge name, so he's ready to pay |
| 2489 | * for the processing speed. |
| 2490 | */ |
| 2491 | xmlChar *buffer; |
| 2492 | int max = len * 2; |
| 2493 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2494 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2495 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2496 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2497 | return(NULL); |
| 2498 | } |
| 2499 | memcpy(buffer, buf, len); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2500 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 2501 | /* test bigentname.xml */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2502 | (c == '.') || (c == '-') || |
| 2503 | (c == '_') || (c == ':') || |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2504 | (IS_COMBINING(c)) || |
| 2505 | (IS_EXTENDER(c))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2506 | if (len + 10 > max) { |
| 2507 | max *= 2; |
| 2508 | buffer = (xmlChar *) xmlRealloc(buffer, |
| 2509 | max * sizeof(xmlChar)); |
| 2510 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2511 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2512 | return(NULL); |
| 2513 | } |
| 2514 | } |
| 2515 | COPY_BUF(l,buffer,len,c); |
| 2516 | cur += l; |
| 2517 | c = CUR_SCHAR(cur, l); |
| 2518 | } |
| 2519 | buffer[len] = 0; |
| 2520 | *str = cur; |
| 2521 | return(buffer); |
| 2522 | } |
| 2523 | } |
| 2524 | *str = cur; |
| 2525 | return(xmlStrndup(buf, len)); |
| 2526 | } |
| 2527 | |
| 2528 | /** |
| 2529 | * xmlParseNmtoken: |
| 2530 | * @ctxt: an XML parser context |
| 2531 | * |
| 2532 | * parse an XML Nmtoken. |
| 2533 | * |
| 2534 | * [7] Nmtoken ::= (NameChar)+ |
| 2535 | * |
| 2536 | * [8] Nmtokens ::= Nmtoken (S Nmtoken)* |
| 2537 | * |
| 2538 | * Returns the Nmtoken parsed or NULL |
| 2539 | */ |
| 2540 | |
| 2541 | xmlChar * |
| 2542 | xmlParseNmtoken(xmlParserCtxtPtr ctxt) { |
| 2543 | xmlChar buf[XML_MAX_NAMELEN + 5]; |
| 2544 | int len = 0, l; |
| 2545 | int c; |
| 2546 | int count = 0; |
| 2547 | |
| 2548 | GROW; |
| 2549 | c = CUR_CHAR(l); |
| 2550 | |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2551 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigtoken.xml */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2552 | (c == '.') || (c == '-') || |
| 2553 | (c == '_') || (c == ':') || |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2554 | (IS_COMBINING(c)) || |
| 2555 | (IS_EXTENDER(c))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2556 | if (count++ > 100) { |
| 2557 | count = 0; |
| 2558 | GROW; |
| 2559 | } |
| 2560 | COPY_BUF(l,buf,len,c); |
| 2561 | NEXTL(l); |
| 2562 | c = CUR_CHAR(l); |
| 2563 | if (len >= XML_MAX_NAMELEN) { |
| 2564 | /* |
| 2565 | * Okay someone managed to make a huge token, so he's ready to pay |
| 2566 | * for the processing speed. |
| 2567 | */ |
| 2568 | xmlChar *buffer; |
| 2569 | int max = len * 2; |
| 2570 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2571 | buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2572 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2573 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2574 | return(NULL); |
| 2575 | } |
| 2576 | memcpy(buffer, buf, len); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2577 | while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigtoken.xml */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2578 | (c == '.') || (c == '-') || |
| 2579 | (c == '_') || (c == ':') || |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2580 | (IS_COMBINING(c)) || |
| 2581 | (IS_EXTENDER(c))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2582 | if (count++ > 100) { |
| 2583 | count = 0; |
| 2584 | GROW; |
| 2585 | } |
| 2586 | if (len + 10 > max) { |
| 2587 | max *= 2; |
| 2588 | buffer = (xmlChar *) xmlRealloc(buffer, |
| 2589 | max * sizeof(xmlChar)); |
| 2590 | if (buffer == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2591 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2592 | return(NULL); |
| 2593 | } |
| 2594 | } |
| 2595 | COPY_BUF(l,buffer,len,c); |
| 2596 | NEXTL(l); |
| 2597 | c = CUR_CHAR(l); |
| 2598 | } |
| 2599 | buffer[len] = 0; |
| 2600 | return(buffer); |
| 2601 | } |
| 2602 | } |
| 2603 | if (len == 0) |
| 2604 | return(NULL); |
| 2605 | return(xmlStrndup(buf, len)); |
| 2606 | } |
| 2607 | |
| 2608 | /** |
| 2609 | * xmlParseEntityValue: |
| 2610 | * @ctxt: an XML parser context |
| 2611 | * @orig: if non-NULL store a copy of the original entity value |
| 2612 | * |
| 2613 | * parse a value for ENTITY declarations |
| 2614 | * |
| 2615 | * [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' | |
| 2616 | * "'" ([^%&'] | PEReference | Reference)* "'" |
| 2617 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2618 | * Returns the EntityValue parsed with reference substituted or NULL |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2619 | */ |
| 2620 | |
| 2621 | xmlChar * |
| 2622 | xmlParseEntityValue(xmlParserCtxtPtr ctxt, xmlChar **orig) { |
| 2623 | xmlChar *buf = NULL; |
| 2624 | int len = 0; |
| 2625 | int size = XML_PARSER_BUFFER_SIZE; |
| 2626 | int c, l; |
| 2627 | xmlChar stop; |
| 2628 | xmlChar *ret = NULL; |
| 2629 | const xmlChar *cur = NULL; |
| 2630 | xmlParserInputPtr input; |
| 2631 | |
| 2632 | if (RAW == '"') stop = '"'; |
| 2633 | else if (RAW == '\'') stop = '\''; |
| 2634 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2635 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2636 | return(NULL); |
| 2637 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2638 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2639 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2640 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2641 | return(NULL); |
| 2642 | } |
| 2643 | |
| 2644 | /* |
| 2645 | * The content of the entity definition is copied in a buffer. |
| 2646 | */ |
| 2647 | |
| 2648 | ctxt->instate = XML_PARSER_ENTITY_VALUE; |
| 2649 | input = ctxt->input; |
| 2650 | GROW; |
| 2651 | NEXT; |
| 2652 | c = CUR_CHAR(l); |
| 2653 | /* |
| 2654 | * NOTE: 4.4.5 Included in Literal |
| 2655 | * When a parameter entity reference appears in a literal entity |
| 2656 | * value, ... a single or double quote character in the replacement |
| 2657 | * text is always treated as a normal data character and will not |
| 2658 | * terminate the literal. |
| 2659 | * In practice it means we stop the loop only when back at parsing |
| 2660 | * the initial entity and the quote is found |
| 2661 | */ |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2662 | while ((IS_CHAR(c)) && ((c != stop) || /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2663 | (ctxt->input != input))) { |
| 2664 | if (len + 5 >= size) { |
| 2665 | size *= 2; |
| 2666 | buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 2667 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2668 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2669 | return(NULL); |
| 2670 | } |
| 2671 | } |
| 2672 | COPY_BUF(l,buf,len,c); |
| 2673 | NEXTL(l); |
| 2674 | /* |
| 2675 | * Pop-up of finished entities. |
| 2676 | */ |
| 2677 | while ((RAW == 0) && (ctxt->inputNr > 1)) /* non input consuming */ |
| 2678 | xmlPopInput(ctxt); |
| 2679 | |
| 2680 | GROW; |
| 2681 | c = CUR_CHAR(l); |
| 2682 | if (c == 0) { |
| 2683 | GROW; |
| 2684 | c = CUR_CHAR(l); |
| 2685 | } |
| 2686 | } |
| 2687 | buf[len] = 0; |
| 2688 | |
| 2689 | /* |
| 2690 | * Raise problem w.r.t. '&' and '%' being used in non-entities |
| 2691 | * reference constructs. Note Charref will be handled in |
| 2692 | * xmlStringDecodeEntities() |
| 2693 | */ |
| 2694 | cur = buf; |
| 2695 | while (*cur != 0) { /* non input consuming */ |
| 2696 | if ((*cur == '%') || ((*cur == '&') && (cur[1] != '#'))) { |
| 2697 | xmlChar *name; |
| 2698 | xmlChar tmp = *cur; |
| 2699 | |
| 2700 | cur++; |
| 2701 | name = xmlParseStringName(ctxt, &cur); |
| 2702 | if ((name == NULL) || (*cur != ';')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2703 | xmlFatalErrMsgInt(ctxt, XML_ERR_ENTITY_CHAR_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2704 | "EntityValue: '%c' forbidden except for entities references\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2705 | tmp); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2706 | } |
Daniel Veillard | 5151c06 | 2001-10-23 13:10:19 +0000 | [diff] [blame] | 2707 | if ((tmp == '%') && (ctxt->inSubset == 1) && |
| 2708 | (ctxt->inputNr == 1)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2709 | xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2710 | } |
| 2711 | if (name != NULL) |
| 2712 | xmlFree(name); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 2713 | if (*cur == 0) |
| 2714 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2715 | } |
| 2716 | cur++; |
| 2717 | } |
| 2718 | |
| 2719 | /* |
| 2720 | * Then PEReference entities are substituted. |
| 2721 | */ |
| 2722 | if (c != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2723 | xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2724 | xmlFree(buf); |
| 2725 | } else { |
| 2726 | NEXT; |
| 2727 | /* |
| 2728 | * NOTE: 4.4.7 Bypassed |
| 2729 | * When a general entity reference appears in the EntityValue in |
| 2730 | * an entity declaration, it is bypassed and left as is. |
| 2731 | * so XML_SUBSTITUTE_REF is not set here. |
| 2732 | */ |
| 2733 | ret = xmlStringDecodeEntities(ctxt, buf, XML_SUBSTITUTE_PEREF, |
| 2734 | 0, 0, 0); |
| 2735 | if (orig != NULL) |
| 2736 | *orig = buf; |
| 2737 | else |
| 2738 | xmlFree(buf); |
| 2739 | } |
| 2740 | |
| 2741 | return(ret); |
| 2742 | } |
| 2743 | |
| 2744 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2745 | * xmlParseAttValueComplex: |
| 2746 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2747 | * @len: the resulting attribute len |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2748 | * @normalize: wether to apply the inner normalization |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2749 | * |
| 2750 | * parse a value for an attribute, this is the fallback function |
| 2751 | * of xmlParseAttValue() when the attribute parsing requires handling |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2752 | * of non-ASCII characters, or normalization compaction. |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2753 | * |
| 2754 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 2755 | */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2756 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2757 | xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) { |
Daniel Veillard | e72c756 | 2002-05-31 09:47:30 +0000 | [diff] [blame] | 2758 | xmlChar limit = 0; |
| 2759 | xmlChar *buf = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2760 | int len = 0; |
| 2761 | int buf_size = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2762 | int c, l, in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2763 | xmlChar *current = NULL; |
| 2764 | xmlEntityPtr ent; |
| 2765 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2766 | if (NXT(0) == '"') { |
| 2767 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 2768 | limit = '"'; |
| 2769 | NEXT; |
| 2770 | } else if (NXT(0) == '\'') { |
| 2771 | limit = '\''; |
| 2772 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
| 2773 | NEXT; |
| 2774 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2775 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2776 | return(NULL); |
| 2777 | } |
| 2778 | |
| 2779 | /* |
| 2780 | * allocate a translation buffer. |
| 2781 | */ |
| 2782 | buf_size = XML_PARSER_BUFFER_SIZE; |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 2783 | buf = (xmlChar *) xmlMallocAtomic(buf_size * sizeof(xmlChar)); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2784 | if (buf == NULL) goto mem_error; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2785 | |
| 2786 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2787 | * 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] | 2788 | */ |
| 2789 | c = CUR_CHAR(l); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 2790 | while ((NXT(0) != limit) && /* checked */ |
| 2791 | (c != '<')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2792 | if (c == 0) break; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 2793 | if (c == '&') { |
Daniel Veillard | 62998c0 | 2003-09-15 12:56:36 +0000 | [diff] [blame] | 2794 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2795 | if (NXT(1) == '#') { |
| 2796 | int val = xmlParseCharRef(ctxt); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2797 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2798 | if (val == '&') { |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 2799 | if (ctxt->replaceEntities) { |
| 2800 | if (len > buf_size - 10) { |
| 2801 | growBuffer(buf); |
| 2802 | } |
| 2803 | buf[len++] = '&'; |
| 2804 | } else { |
| 2805 | /* |
| 2806 | * The reparsing will be done in xmlStringGetNodeList() |
| 2807 | * called by the attribute() function in SAX.c |
| 2808 | */ |
Daniel Veillard | 319a742 | 2001-09-11 09:27:09 +0000 | [diff] [blame] | 2809 | if (len > buf_size - 10) { |
| 2810 | growBuffer(buf); |
| 2811 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2812 | buf[len++] = '&'; |
| 2813 | buf[len++] = '#'; |
| 2814 | buf[len++] = '3'; |
| 2815 | buf[len++] = '8'; |
| 2816 | buf[len++] = ';'; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2817 | } |
| 2818 | } else { |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 2819 | if (len > buf_size - 10) { |
| 2820 | growBuffer(buf); |
| 2821 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2822 | len += xmlCopyChar(0, &buf[len], val); |
| 2823 | } |
| 2824 | } else { |
| 2825 | ent = xmlParseEntityRef(ctxt); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2826 | if ((ent != NULL) && |
| 2827 | (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 2828 | if (len > buf_size - 10) { |
| 2829 | growBuffer(buf); |
| 2830 | } |
| 2831 | if ((ctxt->replaceEntities == 0) && |
| 2832 | (ent->content[0] == '&')) { |
| 2833 | buf[len++] = '&'; |
| 2834 | buf[len++] = '#'; |
| 2835 | buf[len++] = '3'; |
| 2836 | buf[len++] = '8'; |
| 2837 | buf[len++] = ';'; |
| 2838 | } else { |
| 2839 | buf[len++] = ent->content[0]; |
| 2840 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2841 | } else if ((ent != NULL) && |
| 2842 | (ctxt->replaceEntities != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2843 | xmlChar *rep; |
| 2844 | |
| 2845 | if (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) { |
| 2846 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2847 | XML_SUBSTITUTE_REF, |
| 2848 | 0, 0, 0); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2849 | if (rep != NULL) { |
| 2850 | current = rep; |
| 2851 | while (*current != 0) { /* non input consuming */ |
| 2852 | buf[len++] = *current++; |
| 2853 | if (len > buf_size - 10) { |
| 2854 | growBuffer(buf); |
| 2855 | } |
| 2856 | } |
| 2857 | xmlFree(rep); |
| 2858 | } |
| 2859 | } else { |
Daniel Veillard | 0b6b55b | 2001-03-20 11:27:34 +0000 | [diff] [blame] | 2860 | if (len > buf_size - 10) { |
| 2861 | growBuffer(buf); |
| 2862 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2863 | if (ent->content != NULL) |
| 2864 | buf[len++] = ent->content[0]; |
| 2865 | } |
| 2866 | } else if (ent != NULL) { |
| 2867 | int i = xmlStrlen(ent->name); |
| 2868 | const xmlChar *cur = ent->name; |
| 2869 | |
| 2870 | /* |
| 2871 | * This may look absurd but is needed to detect |
| 2872 | * entities problems |
| 2873 | */ |
| 2874 | if ((ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) && |
| 2875 | (ent->content != NULL)) { |
| 2876 | xmlChar *rep; |
| 2877 | rep = xmlStringDecodeEntities(ctxt, ent->content, |
| 2878 | XML_SUBSTITUTE_REF, 0, 0, 0); |
| 2879 | if (rep != NULL) |
| 2880 | xmlFree(rep); |
| 2881 | } |
| 2882 | |
| 2883 | /* |
| 2884 | * Just output the reference |
| 2885 | */ |
| 2886 | buf[len++] = '&'; |
| 2887 | if (len > buf_size - i - 10) { |
| 2888 | growBuffer(buf); |
| 2889 | } |
| 2890 | for (;i > 0;i--) |
| 2891 | buf[len++] = *cur++; |
| 2892 | buf[len++] = ';'; |
| 2893 | } |
| 2894 | } |
| 2895 | } else { |
| 2896 | if ((c == 0x20) || (c == 0xD) || (c == 0xA) || (c == 0x9)) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2897 | if ((len != 0) || (!normalize)) { |
| 2898 | if ((!normalize) || (!in_space)) { |
| 2899 | COPY_BUF(l,buf,len,0x20); |
| 2900 | if (len > buf_size - 10) { |
| 2901 | growBuffer(buf); |
| 2902 | } |
| 2903 | } |
| 2904 | in_space = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2905 | } |
| 2906 | } else { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2907 | in_space = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2908 | COPY_BUF(l,buf,len,c); |
| 2909 | if (len > buf_size - 10) { |
| 2910 | growBuffer(buf); |
| 2911 | } |
| 2912 | } |
| 2913 | NEXTL(l); |
| 2914 | } |
| 2915 | GROW; |
| 2916 | c = CUR_CHAR(l); |
| 2917 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2918 | if ((in_space) && (normalize)) { |
| 2919 | while (buf[len - 1] == 0x20) len--; |
| 2920 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2921 | buf[len] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2922 | if (RAW == '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2923 | xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2924 | } else if (RAW != limit) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 2925 | xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED, |
| 2926 | "AttValue: ' expected\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2927 | } else |
| 2928 | NEXT; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2929 | if (attlen != NULL) *attlen = len; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2930 | return(buf); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2931 | |
| 2932 | mem_error: |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 2933 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2934 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2938 | * xmlParseAttValue: |
| 2939 | * @ctxt: an XML parser context |
| 2940 | * |
| 2941 | * parse a value for an attribute |
| 2942 | * Note: the parser won't do substitution of entities here, this |
| 2943 | * will be handled later in xmlStringGetNodeList |
| 2944 | * |
| 2945 | * [10] AttValue ::= '"' ([^<&"] | Reference)* '"' | |
| 2946 | * "'" ([^<&'] | Reference)* "'" |
| 2947 | * |
| 2948 | * 3.3.3 Attribute-Value Normalization: |
| 2949 | * Before the value of an attribute is passed to the application or |
| 2950 | * checked for validity, the XML processor must normalize it as follows: |
| 2951 | * - a character reference is processed by appending the referenced |
| 2952 | * character to the attribute value |
| 2953 | * - an entity reference is processed by recursively processing the |
| 2954 | * replacement text of the entity |
| 2955 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 2956 | * appending #x20 to the normalized value, except that only a single |
| 2957 | * #x20 is appended for a "#xD#xA" sequence that is part of an external |
| 2958 | * parsed entity or the literal entity value of an internal parsed entity |
| 2959 | * - other characters are processed by appending them to the normalized value |
| 2960 | * If the declared value is not CDATA, then the XML processor must further |
| 2961 | * process the normalized attribute value by discarding any leading and |
| 2962 | * trailing space (#x20) characters, and by replacing sequences of space |
| 2963 | * (#x20) characters by a single space (#x20) character. |
| 2964 | * All attributes for which no declaration has been read should be treated |
| 2965 | * by a non-validating parser as if declared CDATA. |
| 2966 | * |
| 2967 | * Returns the AttValue parsed or NULL. The value has to be freed by the caller. |
| 2968 | */ |
| 2969 | |
| 2970 | |
| 2971 | xmlChar * |
| 2972 | xmlParseAttValue(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 2973 | return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0)); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2977 | * xmlParseSystemLiteral: |
| 2978 | * @ctxt: an XML parser context |
| 2979 | * |
| 2980 | * parse an XML Literal |
| 2981 | * |
| 2982 | * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'") |
| 2983 | * |
| 2984 | * Returns the SystemLiteral parsed or NULL |
| 2985 | */ |
| 2986 | |
| 2987 | xmlChar * |
| 2988 | xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) { |
| 2989 | xmlChar *buf = NULL; |
| 2990 | int len = 0; |
| 2991 | int size = XML_PARSER_BUFFER_SIZE; |
| 2992 | int cur, l; |
| 2993 | xmlChar stop; |
| 2994 | int state = ctxt->instate; |
| 2995 | int count = 0; |
| 2996 | |
| 2997 | SHRINK; |
| 2998 | if (RAW == '"') { |
| 2999 | NEXT; |
| 3000 | stop = '"'; |
| 3001 | } else if (RAW == '\'') { |
| 3002 | NEXT; |
| 3003 | stop = '\''; |
| 3004 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3005 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3006 | return(NULL); |
| 3007 | } |
| 3008 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3009 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3010 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3011 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3012 | return(NULL); |
| 3013 | } |
| 3014 | ctxt->instate = XML_PARSER_SYSTEM_LITERAL; |
| 3015 | cur = CUR_CHAR(l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 3016 | while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3017 | if (len + 5 >= size) { |
| 3018 | size *= 2; |
| 3019 | buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3020 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3021 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3022 | ctxt->instate = (xmlParserInputState) state; |
| 3023 | return(NULL); |
| 3024 | } |
| 3025 | } |
| 3026 | count++; |
| 3027 | if (count > 50) { |
| 3028 | GROW; |
| 3029 | count = 0; |
| 3030 | } |
| 3031 | COPY_BUF(l,buf,len,cur); |
| 3032 | NEXTL(l); |
| 3033 | cur = CUR_CHAR(l); |
| 3034 | if (cur == 0) { |
| 3035 | GROW; |
| 3036 | SHRINK; |
| 3037 | cur = CUR_CHAR(l); |
| 3038 | } |
| 3039 | } |
| 3040 | buf[len] = 0; |
| 3041 | ctxt->instate = (xmlParserInputState) state; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 3042 | if (!IS_CHAR(cur)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3043 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3044 | } else { |
| 3045 | NEXT; |
| 3046 | } |
| 3047 | return(buf); |
| 3048 | } |
| 3049 | |
| 3050 | /** |
| 3051 | * xmlParsePubidLiteral: |
| 3052 | * @ctxt: an XML parser context |
| 3053 | * |
| 3054 | * parse an XML public literal |
| 3055 | * |
| 3056 | * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'" |
| 3057 | * |
| 3058 | * Returns the PubidLiteral parsed or NULL. |
| 3059 | */ |
| 3060 | |
| 3061 | xmlChar * |
| 3062 | xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) { |
| 3063 | xmlChar *buf = NULL; |
| 3064 | int len = 0; |
| 3065 | int size = XML_PARSER_BUFFER_SIZE; |
| 3066 | xmlChar cur; |
| 3067 | xmlChar stop; |
| 3068 | int count = 0; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 3069 | xmlParserInputState oldstate = ctxt->instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3070 | |
| 3071 | SHRINK; |
| 3072 | if (RAW == '"') { |
| 3073 | NEXT; |
| 3074 | stop = '"'; |
| 3075 | } else if (RAW == '\'') { |
| 3076 | NEXT; |
| 3077 | stop = '\''; |
| 3078 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3079 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3080 | return(NULL); |
| 3081 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3082 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3083 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3084 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3085 | return(NULL); |
| 3086 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 3087 | ctxt->instate = XML_PARSER_PUBLIC_LITERAL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3088 | cur = CUR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3089 | while ((IS_PUBIDCHAR_CH(cur)) && (cur != stop)) { /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3090 | if (len + 1 >= size) { |
| 3091 | size *= 2; |
| 3092 | buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3093 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3094 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3095 | return(NULL); |
| 3096 | } |
| 3097 | } |
| 3098 | buf[len++] = cur; |
| 3099 | count++; |
| 3100 | if (count > 50) { |
| 3101 | GROW; |
| 3102 | count = 0; |
| 3103 | } |
| 3104 | NEXT; |
| 3105 | cur = CUR; |
| 3106 | if (cur == 0) { |
| 3107 | GROW; |
| 3108 | SHRINK; |
| 3109 | cur = CUR; |
| 3110 | } |
| 3111 | } |
| 3112 | buf[len] = 0; |
| 3113 | if (cur != stop) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3114 | xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3115 | } else { |
| 3116 | NEXT; |
| 3117 | } |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 3118 | ctxt->instate = oldstate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3119 | return(buf); |
| 3120 | } |
| 3121 | |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3122 | void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3123 | /** |
| 3124 | * xmlParseCharData: |
| 3125 | * @ctxt: an XML parser context |
| 3126 | * @cdata: int indicating whether we are within a CDATA section |
| 3127 | * |
| 3128 | * parse a CharData section. |
| 3129 | * if we are within a CDATA section ']]>' marks an end of section. |
| 3130 | * |
| 3131 | * The right angle bracket (>) may be represented using the string ">", |
| 3132 | * and must, for compatibility, be escaped using ">" or a character |
| 3133 | * reference when it appears in the string "]]>" in content, when that |
| 3134 | * string is not marking the end of a CDATA section. |
| 3135 | * |
| 3136 | * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*) |
| 3137 | */ |
| 3138 | |
| 3139 | void |
| 3140 | xmlParseCharData(xmlParserCtxtPtr ctxt, int cdata) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3141 | const xmlChar *in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3142 | int nbchar = 0; |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 3143 | int line = ctxt->input->line; |
| 3144 | int col = ctxt->input->col; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3145 | |
| 3146 | SHRINK; |
| 3147 | GROW; |
| 3148 | /* |
| 3149 | * Accelerated common case where input don't need to be |
| 3150 | * modified before passing it to the handler. |
| 3151 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3152 | if (!cdata) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3153 | in = ctxt->input->cur; |
| 3154 | do { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3155 | get_more_space: |
| 3156 | while (*in == 0x20) in++; |
| 3157 | if (*in == 0xA) { |
| 3158 | ctxt->input->line++; |
| 3159 | in++; |
| 3160 | while (*in == 0xA) { |
| 3161 | ctxt->input->line++; |
| 3162 | in++; |
| 3163 | } |
| 3164 | goto get_more_space; |
| 3165 | } |
| 3166 | if (*in == '<') { |
| 3167 | nbchar = in - ctxt->input->cur; |
| 3168 | if (nbchar > 0) { |
| 3169 | const xmlChar *tmp = ctxt->input->cur; |
| 3170 | ctxt->input->cur = in; |
| 3171 | |
| 3172 | if (ctxt->sax->ignorableWhitespace != |
| 3173 | ctxt->sax->characters) { |
| 3174 | if (areBlanks(ctxt, tmp, nbchar, 1)) { |
| 3175 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 3176 | tmp, nbchar); |
| 3177 | } else if (ctxt->sax->characters != NULL) |
| 3178 | ctxt->sax->characters(ctxt->userData, |
| 3179 | tmp, nbchar); |
| 3180 | } else if (ctxt->sax->characters != NULL) { |
| 3181 | ctxt->sax->characters(ctxt->userData, |
| 3182 | tmp, nbchar); |
| 3183 | } |
| 3184 | } |
| 3185 | return; |
| 3186 | } |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 3187 | get_more: |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3188 | while (((*in > ']') && (*in <= 0x7F)) || |
| 3189 | ((*in > '&') && (*in < '<')) || |
| 3190 | ((*in > '<') && (*in < ']')) || |
| 3191 | ((*in >= 0x20) && (*in < '&')) || |
| 3192 | (*in == 0x09)) |
| 3193 | in++; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3194 | if (*in == 0xA) { |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3195 | ctxt->input->line++; |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 3196 | in++; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3197 | while (*in == 0xA) { |
Daniel Veillard | 3ed155f | 2001-04-29 19:56:59 +0000 | [diff] [blame] | 3198 | ctxt->input->line++; |
| 3199 | in++; |
| 3200 | } |
| 3201 | goto get_more; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3202 | } |
| 3203 | if (*in == ']') { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 3204 | if ((in[1] == ']') && (in[2] == '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3205 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 3206 | ctxt->input->cur = in; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 3207 | return; |
| 3208 | } |
| 3209 | in++; |
| 3210 | goto get_more; |
| 3211 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3212 | nbchar = in - ctxt->input->cur; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 3213 | if (nbchar > 0) { |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 3214 | if ((ctxt->sax->ignorableWhitespace != |
| 3215 | ctxt->sax->characters) && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3216 | (IS_BLANK_CH(*ctxt->input->cur))) { |
Daniel Veillard | a737459 | 2001-05-10 14:17:55 +0000 | [diff] [blame] | 3217 | const xmlChar *tmp = ctxt->input->cur; |
| 3218 | ctxt->input->cur = in; |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 3219 | |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3220 | if (areBlanks(ctxt, tmp, nbchar, 0)) { |
Daniel Veillard | 40412cd | 2003-09-03 13:28:32 +0000 | [diff] [blame] | 3221 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 3222 | tmp, nbchar); |
| 3223 | } else if (ctxt->sax->characters != NULL) |
| 3224 | ctxt->sax->characters(ctxt->userData, |
| 3225 | tmp, nbchar); |
Daniel Veillard | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 3226 | line = ctxt->input->line; |
| 3227 | col = ctxt->input->col; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 3228 | } else { |
| 3229 | if (ctxt->sax->characters != NULL) |
| 3230 | ctxt->sax->characters(ctxt->userData, |
| 3231 | ctxt->input->cur, nbchar); |
Daniel Veillard | 3ed27bd | 2001-06-17 17:58:17 +0000 | [diff] [blame] | 3232 | line = ctxt->input->line; |
| 3233 | col = ctxt->input->col; |
Daniel Veillard | 80f3257 | 2001-03-07 19:45:40 +0000 | [diff] [blame] | 3234 | } |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3235 | } |
| 3236 | ctxt->input->cur = in; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3237 | if (*in == 0xD) { |
| 3238 | in++; |
| 3239 | if (*in == 0xA) { |
| 3240 | ctxt->input->cur = in; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3241 | in++; |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3242 | ctxt->input->line++; |
| 3243 | continue; /* while */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3244 | } |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3245 | in--; |
| 3246 | } |
| 3247 | if (*in == '<') { |
| 3248 | return; |
| 3249 | } |
| 3250 | if (*in == '&') { |
| 3251 | return; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3252 | } |
| 3253 | SHRINK; |
| 3254 | GROW; |
| 3255 | in = ctxt->input->cur; |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 3256 | } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09)); |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3257 | nbchar = 0; |
| 3258 | } |
Daniel Veillard | 5058211 | 2001-03-26 22:52:16 +0000 | [diff] [blame] | 3259 | ctxt->input->line = line; |
| 3260 | ctxt->input->col = col; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3261 | xmlParseCharDataComplex(ctxt, cdata); |
| 3262 | } |
| 3263 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3264 | /** |
| 3265 | * xmlParseCharDataComplex: |
| 3266 | * @ctxt: an XML parser context |
| 3267 | * @cdata: int indicating whether we are within a CDATA section |
| 3268 | * |
| 3269 | * parse a CharData section.this is the fallback function |
| 3270 | * of xmlParseCharData() when the parsing requires handling |
| 3271 | * of non-ASCII characters. |
| 3272 | */ |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 3273 | void |
| 3274 | xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3275 | xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5]; |
| 3276 | int nbchar = 0; |
| 3277 | int cur, l; |
| 3278 | int count = 0; |
| 3279 | |
| 3280 | SHRINK; |
| 3281 | GROW; |
| 3282 | cur = CUR_CHAR(l); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3283 | while ((cur != '<') && /* checked */ |
| 3284 | (cur != '&') && |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 3285 | (IS_CHAR(cur))) /* test also done in xmlCurrentChar() */ { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3286 | if ((cur == ']') && (NXT(1) == ']') && |
| 3287 | (NXT(2) == '>')) { |
| 3288 | if (cdata) break; |
| 3289 | else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3290 | xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3291 | } |
| 3292 | } |
| 3293 | COPY_BUF(l,buf,nbchar,cur); |
| 3294 | if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 3295 | buf[nbchar] = 0; |
| 3296 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3297 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3298 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3299 | */ |
| 3300 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3301 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3302 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 3303 | ctxt->sax->ignorableWhitespace(ctxt->userData, |
| 3304 | buf, nbchar); |
| 3305 | } else { |
| 3306 | if (ctxt->sax->characters != NULL) |
| 3307 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
| 3308 | } |
| 3309 | } |
| 3310 | nbchar = 0; |
| 3311 | } |
| 3312 | count++; |
| 3313 | if (count > 50) { |
| 3314 | GROW; |
| 3315 | count = 0; |
| 3316 | } |
| 3317 | NEXTL(l); |
| 3318 | cur = CUR_CHAR(l); |
| 3319 | } |
| 3320 | if (nbchar != 0) { |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 3321 | buf[nbchar] = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3322 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3323 | * OK the segment is to be consumed as chars. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3324 | */ |
| 3325 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 3326 | if (areBlanks(ctxt, buf, nbchar, 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3327 | if (ctxt->sax->ignorableWhitespace != NULL) |
| 3328 | ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar); |
| 3329 | } else { |
| 3330 | if (ctxt->sax->characters != NULL) |
| 3331 | ctxt->sax->characters(ctxt->userData, buf, nbchar); |
| 3332 | } |
| 3333 | } |
| 3334 | } |
| 3335 | } |
| 3336 | |
| 3337 | /** |
| 3338 | * xmlParseExternalID: |
| 3339 | * @ctxt: an XML parser context |
| 3340 | * @publicID: a xmlChar** receiving PubidLiteral |
| 3341 | * @strict: indicate whether we should restrict parsing to only |
| 3342 | * production [75], see NOTE below |
| 3343 | * |
| 3344 | * Parse an External ID or a Public ID |
| 3345 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3346 | * NOTE: Productions [75] and [83] interact badly since [75] can generate |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3347 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 3348 | * |
| 3349 | * [75] ExternalID ::= 'SYSTEM' S SystemLiteral |
| 3350 | * | 'PUBLIC' S PubidLiteral S SystemLiteral |
| 3351 | * |
| 3352 | * [83] PublicID ::= 'PUBLIC' S PubidLiteral |
| 3353 | * |
| 3354 | * Returns the function returns SystemLiteral and in the second |
| 3355 | * case publicID receives PubidLiteral, is strict is off |
| 3356 | * it is possible to return NULL and have publicID set. |
| 3357 | */ |
| 3358 | |
| 3359 | xmlChar * |
| 3360 | xmlParseExternalID(xmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) { |
| 3361 | xmlChar *URI = NULL; |
| 3362 | |
| 3363 | SHRINK; |
Daniel Veillard | 146c912 | 2001-03-22 15:22:27 +0000 | [diff] [blame] | 3364 | |
| 3365 | *publicID = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 3366 | if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3367 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3368 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3369 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 3370 | "Space required after 'SYSTEM'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3371 | } |
| 3372 | SKIP_BLANKS; |
| 3373 | URI = xmlParseSystemLiteral(ctxt); |
| 3374 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3375 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3376 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 3377 | } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3378 | SKIP(6); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3379 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3380 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3381 | "Space required after 'PUBLIC'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3382 | } |
| 3383 | SKIP_BLANKS; |
| 3384 | *publicID = xmlParsePubidLiteral(ctxt); |
| 3385 | if (*publicID == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3386 | xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3387 | } |
| 3388 | if (strict) { |
| 3389 | /* |
| 3390 | * We don't handle [83] so "S SystemLiteral" is required. |
| 3391 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3392 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3393 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3394 | "Space required after the Public Identifier\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3395 | } |
| 3396 | } else { |
| 3397 | /* |
| 3398 | * We handle [83] so we return immediately, if |
| 3399 | * "S SystemLiteral" is not detected. From a purely parsing |
| 3400 | * point of view that's a nice mess. |
| 3401 | */ |
| 3402 | const xmlChar *ptr; |
| 3403 | GROW; |
| 3404 | |
| 3405 | ptr = CUR_PTR; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3406 | if (!IS_BLANK_CH(*ptr)) return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3407 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3408 | while (IS_BLANK_CH(*ptr)) ptr++; /* TODO: dangerous, fix ! */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3409 | if ((*ptr != '\'') && (*ptr != '"')) return(NULL); |
| 3410 | } |
| 3411 | SKIP_BLANKS; |
| 3412 | URI = xmlParseSystemLiteral(ctxt); |
| 3413 | if (URI == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3414 | xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3415 | } |
| 3416 | } |
| 3417 | return(URI); |
| 3418 | } |
| 3419 | |
| 3420 | /** |
| 3421 | * xmlParseComment: |
| 3422 | * @ctxt: an XML parser context |
| 3423 | * |
| 3424 | * Skip an XML (SGML) comment <!-- .... --> |
| 3425 | * The spec says that "For compatibility, the string "--" (double-hyphen) |
| 3426 | * must not occur within comments. " |
| 3427 | * |
| 3428 | * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->' |
| 3429 | */ |
| 3430 | void |
| 3431 | xmlParseComment(xmlParserCtxtPtr ctxt) { |
| 3432 | xmlChar *buf = NULL; |
| 3433 | int len; |
| 3434 | int size = XML_PARSER_BUFFER_SIZE; |
| 3435 | int q, ql; |
| 3436 | int r, rl; |
| 3437 | int cur, l; |
| 3438 | xmlParserInputState state; |
| 3439 | xmlParserInputPtr input = ctxt->input; |
| 3440 | int count = 0; |
| 3441 | |
| 3442 | /* |
| 3443 | * Check that there is a comment right here. |
| 3444 | */ |
| 3445 | if ((RAW != '<') || (NXT(1) != '!') || |
| 3446 | (NXT(2) != '-') || (NXT(3) != '-')) return; |
| 3447 | |
| 3448 | state = ctxt->instate; |
| 3449 | ctxt->instate = XML_PARSER_COMMENT; |
| 3450 | SHRINK; |
| 3451 | SKIP(4); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3452 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3453 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3454 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3455 | ctxt->instate = state; |
| 3456 | return; |
| 3457 | } |
| 3458 | q = CUR_CHAR(ql); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3459 | if (q == 0) |
| 3460 | goto not_terminated; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3461 | NEXTL(ql); |
| 3462 | r = CUR_CHAR(rl); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3463 | if (r == 0) |
| 3464 | goto not_terminated; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3465 | NEXTL(rl); |
| 3466 | cur = CUR_CHAR(l); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3467 | if (cur == 0) |
| 3468 | goto not_terminated; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3469 | len = 0; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 3470 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3471 | ((cur != '>') || |
| 3472 | (r != '-') || (q != '-'))) { |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 3473 | if ((r == '-') && (q == '-')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3474 | xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3475 | } |
| 3476 | if (len + 5 >= size) { |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 3477 | xmlChar *new_buf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3478 | size *= 2; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 3479 | new_buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3480 | if (new_buf == NULL) { |
| 3481 | xmlFree (buf); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3482 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3483 | ctxt->instate = state; |
| 3484 | return; |
| 3485 | } |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 3486 | buf = new_buf; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3487 | } |
| 3488 | COPY_BUF(ql,buf,len,q); |
| 3489 | q = r; |
| 3490 | ql = rl; |
| 3491 | r = cur; |
| 3492 | rl = l; |
| 3493 | |
| 3494 | count++; |
| 3495 | if (count > 50) { |
| 3496 | GROW; |
| 3497 | count = 0; |
| 3498 | } |
| 3499 | NEXTL(l); |
| 3500 | cur = CUR_CHAR(l); |
| 3501 | if (cur == 0) { |
| 3502 | SHRINK; |
| 3503 | GROW; |
| 3504 | cur = CUR_CHAR(l); |
| 3505 | } |
| 3506 | } |
| 3507 | buf[len] = 0; |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 3508 | if (!IS_CHAR(cur)) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3509 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3510 | "Comment not terminated \n<!--%.50s\n", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3511 | xmlFree(buf); |
| 3512 | } else { |
| 3513 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3514 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 3515 | "Comment doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3516 | } |
| 3517 | NEXT; |
| 3518 | if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) && |
| 3519 | (!ctxt->disableSAX)) |
| 3520 | ctxt->sax->comment(ctxt->userData, buf); |
| 3521 | xmlFree(buf); |
| 3522 | } |
| 3523 | ctxt->instate = state; |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3524 | return; |
| 3525 | not_terminated: |
| 3526 | xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED, |
| 3527 | "Comment not terminated\n", NULL); |
| 3528 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | /** |
| 3532 | * xmlParsePITarget: |
| 3533 | * @ctxt: an XML parser context |
| 3534 | * |
| 3535 | * parse the name of a PI |
| 3536 | * |
| 3537 | * [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l')) |
| 3538 | * |
| 3539 | * Returns the PITarget name or NULL |
| 3540 | */ |
| 3541 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3542 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3543 | xmlParsePITarget(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3544 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3545 | |
| 3546 | name = xmlParseName(ctxt); |
| 3547 | if ((name != NULL) && |
| 3548 | ((name[0] == 'x') || (name[0] == 'X')) && |
| 3549 | ((name[1] == 'm') || (name[1] == 'M')) && |
| 3550 | ((name[2] == 'l') || (name[2] == 'L'))) { |
| 3551 | int i; |
| 3552 | if ((name[0] == 'x') && (name[1] == 'm') && |
| 3553 | (name[2] == 'l') && (name[3] == 0)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3554 | xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3555 | "XML declaration allowed only at the start of the document\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3556 | return(name); |
| 3557 | } else if (name[3] == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3558 | xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3559 | return(name); |
| 3560 | } |
| 3561 | for (i = 0;;i++) { |
| 3562 | if (xmlW3CPIs[i] == NULL) break; |
| 3563 | if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i])) |
| 3564 | return(name); |
| 3565 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 3566 | xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME, |
| 3567 | "xmlParsePITarget: invalid name prefix 'xml'\n", |
| 3568 | NULL, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3569 | } |
| 3570 | return(name); |
| 3571 | } |
| 3572 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3573 | #ifdef LIBXML_CATALOG_ENABLED |
| 3574 | /** |
| 3575 | * xmlParseCatalogPI: |
| 3576 | * @ctxt: an XML parser context |
| 3577 | * @catalog: the PI value string |
| 3578 | * |
| 3579 | * parse an XML Catalog Processing Instruction. |
| 3580 | * |
| 3581 | * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?> |
| 3582 | * |
| 3583 | * Occurs only if allowed by the user and if happening in the Misc |
| 3584 | * part of the document before any doctype informations |
| 3585 | * This will add the given catalog to the parsing context in order |
| 3586 | * to be used if there is a resolution need further down in the document |
| 3587 | */ |
| 3588 | |
| 3589 | static void |
| 3590 | xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) { |
| 3591 | xmlChar *URL = NULL; |
| 3592 | const xmlChar *tmp, *base; |
| 3593 | xmlChar marker; |
| 3594 | |
| 3595 | tmp = catalog; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3596 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3597 | if (xmlStrncmp(tmp, BAD_CAST"catalog", 7)) |
| 3598 | goto error; |
| 3599 | tmp += 7; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3600 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3601 | if (*tmp != '=') { |
| 3602 | return; |
| 3603 | } |
| 3604 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3605 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3606 | marker = *tmp; |
| 3607 | if ((marker != '\'') && (marker != '"')) |
| 3608 | goto error; |
| 3609 | tmp++; |
| 3610 | base = tmp; |
| 3611 | while ((*tmp != 0) && (*tmp != marker)) tmp++; |
| 3612 | if (*tmp == 0) |
| 3613 | goto error; |
| 3614 | URL = xmlStrndup(base, tmp - base); |
| 3615 | tmp++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3616 | while (IS_BLANK_CH(*tmp)) tmp++; |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3617 | if (*tmp != 0) |
| 3618 | goto error; |
| 3619 | |
| 3620 | if (URL != NULL) { |
| 3621 | ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL); |
| 3622 | xmlFree(URL); |
| 3623 | } |
| 3624 | return; |
| 3625 | |
| 3626 | error: |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 3627 | xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI, |
| 3628 | "Catalog PI syntax error: %s\n", |
| 3629 | catalog, NULL); |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3630 | if (URL != NULL) |
| 3631 | xmlFree(URL); |
| 3632 | } |
| 3633 | #endif |
| 3634 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3635 | /** |
| 3636 | * xmlParsePI: |
| 3637 | * @ctxt: an XML parser context |
| 3638 | * |
| 3639 | * parse an XML Processing Instruction. |
| 3640 | * |
| 3641 | * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>' |
| 3642 | * |
| 3643 | * The processing is transfered to SAX once parsed. |
| 3644 | */ |
| 3645 | |
| 3646 | void |
| 3647 | xmlParsePI(xmlParserCtxtPtr ctxt) { |
| 3648 | xmlChar *buf = NULL; |
| 3649 | int len = 0; |
| 3650 | int size = XML_PARSER_BUFFER_SIZE; |
| 3651 | int cur, l; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3652 | const xmlChar *target; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3653 | xmlParserInputState state; |
| 3654 | int count = 0; |
| 3655 | |
| 3656 | if ((RAW == '<') && (NXT(1) == '?')) { |
| 3657 | xmlParserInputPtr input = ctxt->input; |
| 3658 | state = ctxt->instate; |
| 3659 | ctxt->instate = XML_PARSER_PI; |
| 3660 | /* |
| 3661 | * this is a Processing Instruction. |
| 3662 | */ |
| 3663 | SKIP(2); |
| 3664 | SHRINK; |
| 3665 | |
| 3666 | /* |
| 3667 | * Parse the target name and check for special support like |
| 3668 | * namespace. |
| 3669 | */ |
| 3670 | target = xmlParsePITarget(ctxt); |
| 3671 | if (target != NULL) { |
| 3672 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 3673 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3674 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 3675 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3676 | } |
| 3677 | SKIP(2); |
| 3678 | |
| 3679 | /* |
| 3680 | * SAX: PI detected. |
| 3681 | */ |
| 3682 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 3683 | (ctxt->sax->processingInstruction != NULL)) |
| 3684 | ctxt->sax->processingInstruction(ctxt->userData, |
| 3685 | target, NULL); |
| 3686 | ctxt->instate = state; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3687 | return; |
| 3688 | } |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3689 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3690 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3691 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3692 | ctxt->instate = state; |
| 3693 | return; |
| 3694 | } |
| 3695 | cur = CUR; |
| 3696 | if (!IS_BLANK(cur)) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3697 | xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED, |
| 3698 | "ParsePI: PI %s space expected\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3699 | } |
| 3700 | SKIP_BLANKS; |
| 3701 | cur = CUR_CHAR(l); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 3702 | while (IS_CHAR(cur) && /* checked */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3703 | ((cur != '?') || (NXT(1) != '>'))) { |
| 3704 | if (len + 5 >= size) { |
| 3705 | size *= 2; |
| 3706 | buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 3707 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3708 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3709 | ctxt->instate = state; |
| 3710 | return; |
| 3711 | } |
| 3712 | } |
| 3713 | count++; |
| 3714 | if (count > 50) { |
| 3715 | GROW; |
| 3716 | count = 0; |
| 3717 | } |
| 3718 | COPY_BUF(l,buf,len,cur); |
| 3719 | NEXTL(l); |
| 3720 | cur = CUR_CHAR(l); |
| 3721 | if (cur == 0) { |
| 3722 | SHRINK; |
| 3723 | GROW; |
| 3724 | cur = CUR_CHAR(l); |
| 3725 | } |
| 3726 | } |
| 3727 | buf[len] = 0; |
| 3728 | if (cur != '?') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 3729 | xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED, |
| 3730 | "ParsePI: PI %s never end ...\n", target); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3731 | } else { |
| 3732 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3733 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 3734 | "PI declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3735 | } |
| 3736 | SKIP(2); |
| 3737 | |
Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3738 | #ifdef LIBXML_CATALOG_ENABLED |
| 3739 | if (((state == XML_PARSER_MISC) || |
| 3740 | (state == XML_PARSER_START)) && |
| 3741 | (xmlStrEqual(target, XML_CATALOG_PI))) { |
| 3742 | xmlCatalogAllow allow = xmlCatalogGetDefaults(); |
| 3743 | if ((allow == XML_CATA_ALLOW_DOCUMENT) || |
| 3744 | (allow == XML_CATA_ALLOW_ALL)) |
| 3745 | xmlParseCatalogPI(ctxt, buf); |
| 3746 | } |
| 3747 | #endif |
| 3748 | |
| 3749 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3750 | /* |
| 3751 | * SAX: PI detected. |
| 3752 | */ |
| 3753 | if ((ctxt->sax) && (!ctxt->disableSAX) && |
| 3754 | (ctxt->sax->processingInstruction != NULL)) |
| 3755 | ctxt->sax->processingInstruction(ctxt->userData, |
| 3756 | target, buf); |
| 3757 | } |
| 3758 | xmlFree(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3759 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3760 | xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3761 | } |
| 3762 | ctxt->instate = state; |
| 3763 | } |
| 3764 | } |
| 3765 | |
| 3766 | /** |
| 3767 | * xmlParseNotationDecl: |
| 3768 | * @ctxt: an XML parser context |
| 3769 | * |
| 3770 | * parse a notation declaration |
| 3771 | * |
| 3772 | * [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>' |
| 3773 | * |
| 3774 | * Hence there is actually 3 choices: |
| 3775 | * 'PUBLIC' S PubidLiteral |
| 3776 | * 'PUBLIC' S PubidLiteral S SystemLiteral |
| 3777 | * and 'SYSTEM' S SystemLiteral |
| 3778 | * |
| 3779 | * See the NOTE on xmlParseExternalID(). |
| 3780 | */ |
| 3781 | |
| 3782 | void |
| 3783 | xmlParseNotationDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3784 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3785 | xmlChar *Pubid; |
| 3786 | xmlChar *Systemid; |
| 3787 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 3788 | if (CMP10(CUR_PTR, '<', '!', 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3789 | xmlParserInputPtr input = ctxt->input; |
| 3790 | SHRINK; |
| 3791 | SKIP(10); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3792 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3793 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 3794 | "Space required after '<!NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3795 | return; |
| 3796 | } |
| 3797 | SKIP_BLANKS; |
| 3798 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3799 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3800 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3801 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3802 | return; |
| 3803 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3804 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3805 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3806 | "Space required after the NOTATION name'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3807 | return; |
| 3808 | } |
| 3809 | SKIP_BLANKS; |
| 3810 | |
| 3811 | /* |
| 3812 | * Parse the IDs. |
| 3813 | */ |
| 3814 | Systemid = xmlParseExternalID(ctxt, &Pubid, 0); |
| 3815 | SKIP_BLANKS; |
| 3816 | |
| 3817 | if (RAW == '>') { |
| 3818 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3819 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 3820 | "Notation declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3821 | } |
| 3822 | NEXT; |
| 3823 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 3824 | (ctxt->sax->notationDecl != NULL)) |
| 3825 | ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid); |
| 3826 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3827 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3828 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3829 | if (Systemid != NULL) xmlFree(Systemid); |
| 3830 | if (Pubid != NULL) xmlFree(Pubid); |
| 3831 | } |
| 3832 | } |
| 3833 | |
| 3834 | /** |
| 3835 | * xmlParseEntityDecl: |
| 3836 | * @ctxt: an XML parser context |
| 3837 | * |
| 3838 | * parse <!ENTITY declarations |
| 3839 | * |
| 3840 | * [70] EntityDecl ::= GEDecl | PEDecl |
| 3841 | * |
| 3842 | * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>' |
| 3843 | * |
| 3844 | * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>' |
| 3845 | * |
| 3846 | * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?) |
| 3847 | * |
| 3848 | * [74] PEDef ::= EntityValue | ExternalID |
| 3849 | * |
| 3850 | * [76] NDataDecl ::= S 'NDATA' S Name |
| 3851 | * |
| 3852 | * [ VC: Notation Declared ] |
| 3853 | * The Name must match the declared name of a notation. |
| 3854 | */ |
| 3855 | |
| 3856 | void |
| 3857 | xmlParseEntityDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3858 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3859 | xmlChar *value = NULL; |
| 3860 | xmlChar *URI = NULL, *literal = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 3861 | const xmlChar *ndata = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3862 | int isParameter = 0; |
| 3863 | xmlChar *orig = NULL; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 3864 | int skipped; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3865 | |
| 3866 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 3867 | if (CMP8(CUR_PTR, '<', '!', 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3868 | xmlParserInputPtr input = ctxt->input; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3869 | SHRINK; |
| 3870 | SKIP(8); |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 3871 | skipped = SKIP_BLANKS; |
| 3872 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3873 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 3874 | "Space required after '<!ENTITY'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3875 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3876 | |
| 3877 | if (RAW == '%') { |
| 3878 | NEXT; |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 3879 | skipped = SKIP_BLANKS; |
| 3880 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3881 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 3882 | "Space required after '%'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3883 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3884 | isParameter = 1; |
| 3885 | } |
| 3886 | |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 3887 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3888 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3889 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 3890 | "xmlParseEntityDecl: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3891 | return; |
| 3892 | } |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 3893 | skipped = SKIP_BLANKS; |
| 3894 | if (skipped == 0) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 3895 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 3896 | "Space required after the entity name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3897 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3898 | |
Daniel Veillard | f5582f1 | 2002-06-11 10:08:16 +0000 | [diff] [blame] | 3899 | ctxt->instate = XML_PARSER_ENTITY_DECL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3900 | /* |
| 3901 | * handle the various case of definitions... |
| 3902 | */ |
| 3903 | if (isParameter) { |
| 3904 | if ((RAW == '"') || (RAW == '\'')) { |
| 3905 | value = xmlParseEntityValue(ctxt, &orig); |
| 3906 | if (value) { |
| 3907 | if ((ctxt->sax != NULL) && |
| 3908 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 3909 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 3910 | XML_INTERNAL_PARAMETER_ENTITY, |
| 3911 | NULL, NULL, value); |
| 3912 | } |
| 3913 | } else { |
| 3914 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 3915 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3916 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3917 | } |
| 3918 | if (URI) { |
| 3919 | xmlURIPtr uri; |
| 3920 | |
| 3921 | uri = xmlParseURI((const char *) URI); |
| 3922 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3923 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 3924 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 3925 | /* |
| 3926 | * This really ought to be a well formedness error |
| 3927 | * but the XML Core WG decided otherwise c.f. issue |
| 3928 | * E26 of the XML erratas. |
| 3929 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3930 | } else { |
| 3931 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 3932 | /* |
| 3933 | * Okay this is foolish to block those but not |
| 3934 | * invalid URIs. |
| 3935 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3936 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3937 | } else { |
| 3938 | if ((ctxt->sax != NULL) && |
| 3939 | (!ctxt->disableSAX) && |
| 3940 | (ctxt->sax->entityDecl != NULL)) |
| 3941 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 3942 | XML_EXTERNAL_PARAMETER_ENTITY, |
| 3943 | literal, URI, NULL); |
| 3944 | } |
| 3945 | xmlFreeURI(uri); |
| 3946 | } |
| 3947 | } |
| 3948 | } |
| 3949 | } else { |
| 3950 | if ((RAW == '"') || (RAW == '\'')) { |
| 3951 | value = xmlParseEntityValue(ctxt, &orig); |
| 3952 | if ((ctxt->sax != NULL) && |
| 3953 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 3954 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 3955 | XML_INTERNAL_GENERAL_ENTITY, |
| 3956 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 3957 | /* |
| 3958 | * For expat compatibility in SAX mode. |
| 3959 | */ |
| 3960 | if ((ctxt->myDoc == NULL) || |
| 3961 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 3962 | if (ctxt->myDoc == NULL) { |
| 3963 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
| 3964 | } |
| 3965 | if (ctxt->myDoc->intSubset == NULL) |
| 3966 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 3967 | BAD_CAST "fake", NULL, NULL); |
| 3968 | |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 3969 | xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY, |
| 3970 | NULL, NULL, value); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 3971 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3972 | } else { |
| 3973 | URI = xmlParseExternalID(ctxt, &literal, 1); |
| 3974 | if ((URI == NULL) && (literal == NULL)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3975 | xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3976 | } |
| 3977 | if (URI) { |
| 3978 | xmlURIPtr uri; |
| 3979 | |
| 3980 | uri = xmlParseURI((const char *)URI); |
| 3981 | if (uri == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 3982 | xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI, |
| 3983 | "Invalid URI: %s\n", URI); |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 3984 | /* |
| 3985 | * This really ought to be a well formedness error |
| 3986 | * but the XML Core WG decided otherwise c.f. issue |
| 3987 | * E26 of the XML erratas. |
| 3988 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3989 | } else { |
| 3990 | if (uri->fragment != NULL) { |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 3991 | /* |
| 3992 | * Okay this is foolish to block those but not |
| 3993 | * invalid URIs. |
| 3994 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 3995 | xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3996 | } |
| 3997 | xmlFreeURI(uri); |
| 3998 | } |
| 3999 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4000 | if ((RAW != '>') && (!IS_BLANK_CH(CUR))) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4001 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4002 | "Space required before 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4003 | } |
| 4004 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4005 | if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4006 | SKIP(5); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4007 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4008 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4009 | "Space required after 'NDATA'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4010 | } |
| 4011 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4012 | ndata = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4013 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 4014 | (ctxt->sax->unparsedEntityDecl != NULL)) |
| 4015 | ctxt->sax->unparsedEntityDecl(ctxt->userData, name, |
| 4016 | literal, URI, ndata); |
| 4017 | } else { |
| 4018 | if ((ctxt->sax != NULL) && |
| 4019 | (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL)) |
| 4020 | ctxt->sax->entityDecl(ctxt->userData, name, |
| 4021 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 4022 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 4023 | /* |
| 4024 | * For expat compatibility in SAX mode. |
| 4025 | * assuming the entity repalcement was asked for |
| 4026 | */ |
| 4027 | if ((ctxt->replaceEntities != 0) && |
| 4028 | ((ctxt->myDoc == NULL) || |
| 4029 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) { |
| 4030 | if (ctxt->myDoc == NULL) { |
| 4031 | ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE); |
| 4032 | } |
| 4033 | |
| 4034 | if (ctxt->myDoc->intSubset == NULL) |
| 4035 | ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc, |
| 4036 | BAD_CAST "fake", NULL, NULL); |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 4037 | xmlSAX2EntityDecl(ctxt, name, |
| 4038 | XML_EXTERNAL_GENERAL_PARSED_ENTITY, |
| 4039 | literal, URI, NULL); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 4040 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4041 | } |
| 4042 | } |
| 4043 | } |
| 4044 | SKIP_BLANKS; |
| 4045 | if (RAW != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 4046 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4047 | "xmlParseEntityDecl: entity %s not terminated\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4048 | } else { |
| 4049 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4050 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4051 | "Entity declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4052 | } |
| 4053 | NEXT; |
| 4054 | } |
| 4055 | if (orig != NULL) { |
| 4056 | /* |
| 4057 | * Ugly mechanism to save the raw entity value. |
| 4058 | */ |
| 4059 | xmlEntityPtr cur = NULL; |
| 4060 | |
| 4061 | if (isParameter) { |
| 4062 | if ((ctxt->sax != NULL) && |
| 4063 | (ctxt->sax->getParameterEntity != NULL)) |
| 4064 | cur = ctxt->sax->getParameterEntity(ctxt->userData, name); |
| 4065 | } else { |
| 4066 | if ((ctxt->sax != NULL) && |
| 4067 | (ctxt->sax->getEntity != NULL)) |
| 4068 | cur = ctxt->sax->getEntity(ctxt->userData, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 4069 | if ((cur == NULL) && (ctxt->userData==ctxt)) { |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 4070 | cur = xmlSAX2GetEntity(ctxt, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 4071 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4072 | } |
| 4073 | if (cur != NULL) { |
| 4074 | if (cur->orig != NULL) |
| 4075 | xmlFree(orig); |
| 4076 | else |
| 4077 | cur->orig = orig; |
| 4078 | } else |
| 4079 | xmlFree(orig); |
| 4080 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4081 | if (value != NULL) xmlFree(value); |
| 4082 | if (URI != NULL) xmlFree(URI); |
| 4083 | if (literal != NULL) xmlFree(literal); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4084 | } |
| 4085 | } |
| 4086 | |
| 4087 | /** |
| 4088 | * xmlParseDefaultDecl: |
| 4089 | * @ctxt: an XML parser context |
| 4090 | * @value: Receive a possible fixed default value for the attribute |
| 4091 | * |
| 4092 | * Parse an attribute default declaration |
| 4093 | * |
| 4094 | * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue) |
| 4095 | * |
| 4096 | * [ VC: Required Attribute ] |
| 4097 | * if the default declaration is the keyword #REQUIRED, then the |
| 4098 | * attribute must be specified for all elements of the type in the |
| 4099 | * attribute-list declaration. |
| 4100 | * |
| 4101 | * [ VC: Attribute Default Legal ] |
| 4102 | * The declared default value must meet the lexical constraints of |
| 4103 | * the declared attribute type c.f. xmlValidateAttributeDecl() |
| 4104 | * |
| 4105 | * [ VC: Fixed Attribute Default ] |
| 4106 | * if an attribute has a default value declared with the #FIXED |
| 4107 | * keyword, instances of that attribute must match the default value. |
| 4108 | * |
| 4109 | * [ WFC: No < in Attribute Values ] |
| 4110 | * handled in xmlParseAttValue() |
| 4111 | * |
| 4112 | * returns: XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED |
| 4113 | * or XML_ATTRIBUTE_FIXED. |
| 4114 | */ |
| 4115 | |
| 4116 | int |
| 4117 | xmlParseDefaultDecl(xmlParserCtxtPtr ctxt, xmlChar **value) { |
| 4118 | int val; |
| 4119 | xmlChar *ret; |
| 4120 | |
| 4121 | *value = NULL; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4122 | if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4123 | SKIP(9); |
| 4124 | return(XML_ATTRIBUTE_REQUIRED); |
| 4125 | } |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4126 | if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4127 | SKIP(8); |
| 4128 | return(XML_ATTRIBUTE_IMPLIED); |
| 4129 | } |
| 4130 | val = XML_ATTRIBUTE_NONE; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4131 | if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4132 | SKIP(6); |
| 4133 | val = XML_ATTRIBUTE_FIXED; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4134 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4135 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4136 | "Space required after '#FIXED'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4137 | } |
| 4138 | SKIP_BLANKS; |
| 4139 | } |
| 4140 | ret = xmlParseAttValue(ctxt); |
| 4141 | ctxt->instate = XML_PARSER_DTD; |
| 4142 | if (ret == NULL) { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 4143 | xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4144 | "Attribute default value declaration error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4145 | } else |
| 4146 | *value = ret; |
| 4147 | return(val); |
| 4148 | } |
| 4149 | |
| 4150 | /** |
| 4151 | * xmlParseNotationType: |
| 4152 | * @ctxt: an XML parser context |
| 4153 | * |
| 4154 | * parse an Notation attribute type. |
| 4155 | * |
| 4156 | * Note: the leading 'NOTATION' S part has already being parsed... |
| 4157 | * |
| 4158 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 4159 | * |
| 4160 | * [ VC: Notation Attributes ] |
| 4161 | * Values of this type must match one of the notation names included |
| 4162 | * in the declaration; all notation names in the declaration must be declared. |
| 4163 | * |
| 4164 | * Returns: the notation attribute tree built while parsing |
| 4165 | */ |
| 4166 | |
| 4167 | xmlEnumerationPtr |
| 4168 | xmlParseNotationType(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4169 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4170 | xmlEnumerationPtr ret = NULL, last = NULL, cur; |
| 4171 | |
| 4172 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4173 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4174 | return(NULL); |
| 4175 | } |
| 4176 | SHRINK; |
| 4177 | do { |
| 4178 | NEXT; |
| 4179 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4180 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4181 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4182 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 4183 | "Name expected in NOTATION declaration\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4184 | return(ret); |
| 4185 | } |
| 4186 | cur = xmlCreateEnumeration(name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4187 | if (cur == NULL) return(ret); |
| 4188 | if (last == NULL) ret = last = cur; |
| 4189 | else { |
| 4190 | last->next = cur; |
| 4191 | last = cur; |
| 4192 | } |
| 4193 | SKIP_BLANKS; |
| 4194 | } while (RAW == '|'); |
| 4195 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4196 | xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4197 | if ((last != NULL) && (last != ret)) |
| 4198 | xmlFreeEnumeration(last); |
| 4199 | return(ret); |
| 4200 | } |
| 4201 | NEXT; |
| 4202 | return(ret); |
| 4203 | } |
| 4204 | |
| 4205 | /** |
| 4206 | * xmlParseEnumerationType: |
| 4207 | * @ctxt: an XML parser context |
| 4208 | * |
| 4209 | * parse an Enumeration attribute type. |
| 4210 | * |
| 4211 | * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')' |
| 4212 | * |
| 4213 | * [ VC: Enumeration ] |
| 4214 | * Values of this type must match one of the Nmtoken tokens in |
| 4215 | * the declaration |
| 4216 | * |
| 4217 | * Returns: the enumeration attribute tree built while parsing |
| 4218 | */ |
| 4219 | |
| 4220 | xmlEnumerationPtr |
| 4221 | xmlParseEnumerationType(xmlParserCtxtPtr ctxt) { |
| 4222 | xmlChar *name; |
| 4223 | xmlEnumerationPtr ret = NULL, last = NULL, cur; |
| 4224 | |
| 4225 | if (RAW != '(') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4226 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4227 | return(NULL); |
| 4228 | } |
| 4229 | SHRINK; |
| 4230 | do { |
| 4231 | NEXT; |
| 4232 | SKIP_BLANKS; |
| 4233 | name = xmlParseNmtoken(ctxt); |
| 4234 | if (name == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4235 | xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4236 | return(ret); |
| 4237 | } |
| 4238 | cur = xmlCreateEnumeration(name); |
| 4239 | xmlFree(name); |
| 4240 | if (cur == NULL) return(ret); |
| 4241 | if (last == NULL) ret = last = cur; |
| 4242 | else { |
| 4243 | last->next = cur; |
| 4244 | last = cur; |
| 4245 | } |
| 4246 | SKIP_BLANKS; |
| 4247 | } while (RAW == '|'); |
| 4248 | if (RAW != ')') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4249 | xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4250 | return(ret); |
| 4251 | } |
| 4252 | NEXT; |
| 4253 | return(ret); |
| 4254 | } |
| 4255 | |
| 4256 | /** |
| 4257 | * xmlParseEnumeratedType: |
| 4258 | * @ctxt: an XML parser context |
| 4259 | * @tree: the enumeration tree built while parsing |
| 4260 | * |
| 4261 | * parse an Enumerated attribute type. |
| 4262 | * |
| 4263 | * [57] EnumeratedType ::= NotationType | Enumeration |
| 4264 | * |
| 4265 | * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')' |
| 4266 | * |
| 4267 | * |
| 4268 | * Returns: XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION |
| 4269 | */ |
| 4270 | |
| 4271 | int |
| 4272 | xmlParseEnumeratedType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4273 | if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4274 | SKIP(8); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4275 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4276 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4277 | "Space required after 'NOTATION'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4278 | return(0); |
| 4279 | } |
| 4280 | SKIP_BLANKS; |
| 4281 | *tree = xmlParseNotationType(ctxt); |
| 4282 | if (*tree == NULL) return(0); |
| 4283 | return(XML_ATTRIBUTE_NOTATION); |
| 4284 | } |
| 4285 | *tree = xmlParseEnumerationType(ctxt); |
| 4286 | if (*tree == NULL) return(0); |
| 4287 | return(XML_ATTRIBUTE_ENUMERATION); |
| 4288 | } |
| 4289 | |
| 4290 | /** |
| 4291 | * xmlParseAttributeType: |
| 4292 | * @ctxt: an XML parser context |
| 4293 | * @tree: the enumeration tree built while parsing |
| 4294 | * |
| 4295 | * parse the Attribute list def for an element |
| 4296 | * |
| 4297 | * [54] AttType ::= StringType | TokenizedType | EnumeratedType |
| 4298 | * |
| 4299 | * [55] StringType ::= 'CDATA' |
| 4300 | * |
| 4301 | * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' | |
| 4302 | * 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS' |
| 4303 | * |
| 4304 | * Validity constraints for attribute values syntax are checked in |
| 4305 | * xmlValidateAttributeValue() |
| 4306 | * |
| 4307 | * [ VC: ID ] |
| 4308 | * Values of type ID must match the Name production. A name must not |
| 4309 | * appear more than once in an XML document as a value of this type; |
| 4310 | * i.e., ID values must uniquely identify the elements which bear them. |
| 4311 | * |
| 4312 | * [ VC: One ID per Element Type ] |
| 4313 | * No element type may have more than one ID attribute specified. |
| 4314 | * |
| 4315 | * [ VC: ID Attribute Default ] |
| 4316 | * An ID attribute must have a declared default of #IMPLIED or #REQUIRED. |
| 4317 | * |
| 4318 | * [ VC: IDREF ] |
| 4319 | * Values of type IDREF must match the Name production, and values |
| 4320 | * of type IDREFS must match Names; each IDREF Name must match the value |
| 4321 | * of an ID attribute on some element in the XML document; i.e. IDREF |
| 4322 | * values must match the value of some ID attribute. |
| 4323 | * |
| 4324 | * [ VC: Entity Name ] |
| 4325 | * Values of type ENTITY must match the Name production, values |
| 4326 | * of type ENTITIES must match Names; each Entity Name must match the |
| 4327 | * name of an unparsed entity declared in the DTD. |
| 4328 | * |
| 4329 | * [ VC: Name Token ] |
| 4330 | * Values of type NMTOKEN must match the Nmtoken production; values |
| 4331 | * of type NMTOKENS must match Nmtokens. |
| 4332 | * |
| 4333 | * Returns the attribute type |
| 4334 | */ |
| 4335 | int |
| 4336 | xmlParseAttributeType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) { |
| 4337 | SHRINK; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4338 | if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4339 | SKIP(5); |
| 4340 | return(XML_ATTRIBUTE_CDATA); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4341 | } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4342 | SKIP(6); |
| 4343 | return(XML_ATTRIBUTE_IDREFS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4344 | } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4345 | SKIP(5); |
| 4346 | return(XML_ATTRIBUTE_IDREF); |
| 4347 | } else if ((RAW == 'I') && (NXT(1) == 'D')) { |
| 4348 | SKIP(2); |
| 4349 | return(XML_ATTRIBUTE_ID); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4350 | } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4351 | SKIP(6); |
| 4352 | return(XML_ATTRIBUTE_ENTITY); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4353 | } 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] | 4354 | SKIP(8); |
| 4355 | return(XML_ATTRIBUTE_ENTITIES); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4356 | } 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] | 4357 | SKIP(8); |
| 4358 | return(XML_ATTRIBUTE_NMTOKENS); |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4359 | } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4360 | SKIP(7); |
| 4361 | return(XML_ATTRIBUTE_NMTOKEN); |
| 4362 | } |
| 4363 | return(xmlParseEnumeratedType(ctxt, tree)); |
| 4364 | } |
| 4365 | |
| 4366 | /** |
| 4367 | * xmlParseAttributeListDecl: |
| 4368 | * @ctxt: an XML parser context |
| 4369 | * |
| 4370 | * : parse the Attribute list def for an element |
| 4371 | * |
| 4372 | * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>' |
| 4373 | * |
| 4374 | * [53] AttDef ::= S Name S AttType S DefaultDecl |
| 4375 | * |
| 4376 | */ |
| 4377 | void |
| 4378 | xmlParseAttributeListDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4379 | const xmlChar *elemName; |
| 4380 | const xmlChar *attrName; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4381 | xmlEnumerationPtr tree; |
| 4382 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4383 | if (CMP9(CUR_PTR, '<', '!', 'A', 'T', 'T', 'L', 'I', 'S', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4384 | xmlParserInputPtr input = ctxt->input; |
| 4385 | |
| 4386 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4387 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4388 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4389 | "Space required after '<!ATTLIST'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4390 | } |
| 4391 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4392 | elemName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4393 | if (elemName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4394 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 4395 | "ATTLIST: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4396 | return; |
| 4397 | } |
| 4398 | SKIP_BLANKS; |
| 4399 | GROW; |
| 4400 | while (RAW != '>') { |
| 4401 | const xmlChar *check = CUR_PTR; |
| 4402 | int type; |
| 4403 | int def; |
| 4404 | xmlChar *defaultValue = NULL; |
| 4405 | |
| 4406 | GROW; |
| 4407 | tree = NULL; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4408 | attrName = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4409 | if (attrName == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4410 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 4411 | "ATTLIST: no name for Attribute\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4412 | break; |
| 4413 | } |
| 4414 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4415 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4416 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4417 | "Space required after the attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4418 | if (defaultValue != NULL) |
| 4419 | xmlFree(defaultValue); |
| 4420 | break; |
| 4421 | } |
| 4422 | SKIP_BLANKS; |
| 4423 | |
| 4424 | type = xmlParseAttributeType(ctxt, &tree); |
| 4425 | if (type <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4426 | if (defaultValue != NULL) |
| 4427 | xmlFree(defaultValue); |
| 4428 | break; |
| 4429 | } |
| 4430 | |
| 4431 | GROW; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4432 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4433 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4434 | "Space required after the attribute type\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4435 | if (defaultValue != NULL) |
| 4436 | xmlFree(defaultValue); |
| 4437 | if (tree != NULL) |
| 4438 | xmlFreeEnumeration(tree); |
| 4439 | break; |
| 4440 | } |
| 4441 | SKIP_BLANKS; |
| 4442 | |
| 4443 | def = xmlParseDefaultDecl(ctxt, &defaultValue); |
| 4444 | if (def <= 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4445 | if (defaultValue != NULL) |
| 4446 | xmlFree(defaultValue); |
| 4447 | if (tree != NULL) |
| 4448 | xmlFreeEnumeration(tree); |
| 4449 | break; |
| 4450 | } |
| 4451 | |
| 4452 | GROW; |
| 4453 | if (RAW != '>') { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4454 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4455 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4456 | "Space required after the attribute default value\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4457 | if (defaultValue != NULL) |
| 4458 | xmlFree(defaultValue); |
| 4459 | if (tree != NULL) |
| 4460 | xmlFreeEnumeration(tree); |
| 4461 | break; |
| 4462 | } |
| 4463 | SKIP_BLANKS; |
| 4464 | } |
| 4465 | if (check == CUR_PTR) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4466 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 4467 | "in xmlParseAttributeListDecl\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4468 | if (defaultValue != NULL) |
| 4469 | xmlFree(defaultValue); |
| 4470 | if (tree != NULL) |
| 4471 | xmlFreeEnumeration(tree); |
| 4472 | break; |
| 4473 | } |
| 4474 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 4475 | (ctxt->sax->attributeDecl != NULL)) |
| 4476 | ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName, |
| 4477 | type, def, defaultValue, tree); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4478 | else if (tree != NULL) |
| 4479 | xmlFreeEnumeration(tree); |
| 4480 | |
| 4481 | if ((ctxt->sax2) && (defaultValue != NULL) && |
| 4482 | (def != XML_ATTRIBUTE_IMPLIED) && |
| 4483 | (def != XML_ATTRIBUTE_REQUIRED)) { |
| 4484 | xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue); |
| 4485 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 4486 | if ((ctxt->sax2) && (type != XML_ATTRIBUTE_CDATA)) { |
| 4487 | xmlAddSpecialAttr(ctxt, elemName, attrName, type); |
| 4488 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4489 | if (defaultValue != NULL) |
| 4490 | xmlFree(defaultValue); |
| 4491 | GROW; |
| 4492 | } |
| 4493 | if (RAW == '>') { |
| 4494 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4495 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4496 | "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] | 4497 | } |
| 4498 | NEXT; |
| 4499 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4500 | } |
| 4501 | } |
| 4502 | |
| 4503 | /** |
| 4504 | * xmlParseElementMixedContentDecl: |
| 4505 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 4506 | * @inputchk: the input used for the current entity, needed for boundary checks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4507 | * |
| 4508 | * parse the declaration for a Mixed Element content |
| 4509 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
| 4510 | * |
| 4511 | * [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' | |
| 4512 | * '(' S? '#PCDATA' S? ')' |
| 4513 | * |
| 4514 | * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49]) |
| 4515 | * |
| 4516 | * [ VC: No Duplicate Types ] |
| 4517 | * The same name must not appear more than once in a single |
| 4518 | * mixed-content declaration. |
| 4519 | * |
| 4520 | * returns: the list of the xmlElementContentPtr describing the element choices |
| 4521 | */ |
| 4522 | xmlElementContentPtr |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4523 | xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4524 | xmlElementContentPtr ret = NULL, cur = NULL, n; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4525 | const xmlChar *elem = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4526 | |
| 4527 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4528 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4529 | SKIP(7); |
| 4530 | SKIP_BLANKS; |
| 4531 | SHRINK; |
| 4532 | if (RAW == ')') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4533 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 4534 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4535 | "Element content declaration doesn't start and stop in the same entity\n", |
| 4536 | NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 4537 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4538 | NEXT; |
| 4539 | ret = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_PCDATA); |
| 4540 | if (RAW == '*') { |
| 4541 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 4542 | NEXT; |
| 4543 | } |
| 4544 | return(ret); |
| 4545 | } |
| 4546 | if ((RAW == '(') || (RAW == '|')) { |
| 4547 | ret = cur = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_PCDATA); |
| 4548 | if (ret == NULL) return(NULL); |
| 4549 | } |
| 4550 | while (RAW == '|') { |
| 4551 | NEXT; |
| 4552 | if (elem == NULL) { |
| 4553 | ret = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR); |
| 4554 | if (ret == NULL) return(NULL); |
| 4555 | ret->c1 = cur; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4556 | if (cur != NULL) |
| 4557 | cur->parent = ret; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4558 | cur = ret; |
| 4559 | } else { |
| 4560 | n = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR); |
| 4561 | if (n == NULL) return(NULL); |
| 4562 | n->c1 = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4563 | if (n->c1 != NULL) |
| 4564 | n->c1->parent = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4565 | cur->c2 = n; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4566 | if (n != NULL) |
| 4567 | n->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4568 | cur = n; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4569 | } |
| 4570 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4571 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4572 | if (elem == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4573 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4574 | "xmlParseElementMixedContentDecl : Name expected\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4575 | xmlFreeElementContent(cur); |
| 4576 | return(NULL); |
| 4577 | } |
| 4578 | SKIP_BLANKS; |
| 4579 | GROW; |
| 4580 | } |
| 4581 | if ((RAW == ')') && (NXT(1) == '*')) { |
| 4582 | if (elem != NULL) { |
| 4583 | cur->c2 = xmlNewElementContent(elem, |
| 4584 | XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4585 | if (cur->c2 != NULL) |
| 4586 | cur->c2->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4587 | } |
| 4588 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4589 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 4590 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4591 | "Element content declaration doesn't start and stop in the same entity\n", |
| 4592 | NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 4593 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4594 | SKIP(2); |
| 4595 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4596 | xmlFreeElementContent(ret); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4597 | xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4598 | return(NULL); |
| 4599 | } |
| 4600 | |
| 4601 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4602 | xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4603 | } |
| 4604 | return(ret); |
| 4605 | } |
| 4606 | |
| 4607 | /** |
| 4608 | * xmlParseElementChildrenContentDecl: |
| 4609 | * @ctxt: an XML parser context |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 4610 | * @inputchk: the input used for the current entity, needed for boundary checks |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4611 | * |
| 4612 | * parse the declaration for a Mixed Element content |
| 4613 | * The leading '(' and spaces have been skipped in xmlParseElementContentDecl |
| 4614 | * |
| 4615 | * |
| 4616 | * [47] children ::= (choice | seq) ('?' | '*' | '+')? |
| 4617 | * |
| 4618 | * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')? |
| 4619 | * |
| 4620 | * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')' |
| 4621 | * |
| 4622 | * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')' |
| 4623 | * |
| 4624 | * [ VC: Proper Group/PE Nesting ] applies to [49] and [50] |
| 4625 | * TODO Parameter-entity replacement text must be properly nested |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 4626 | * with parenthesized groups. That is to say, if either of the |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4627 | * opening or closing parentheses in a choice, seq, or Mixed |
| 4628 | * construct is contained in the replacement text for a parameter |
| 4629 | * entity, both must be contained in the same replacement text. For |
| 4630 | * interoperability, if a parameter-entity reference appears in a |
| 4631 | * choice, seq, or Mixed construct, its replacement text should not |
| 4632 | * be empty, and neither the first nor last non-blank character of |
| 4633 | * the replacement text should be a connector (| or ,). |
| 4634 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 4635 | * Returns the tree of xmlElementContentPtr describing the element |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4636 | * hierarchy. |
| 4637 | */ |
| 4638 | xmlElementContentPtr |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4639 | xmlParseElementChildrenContentDecl (xmlParserCtxtPtr ctxt, int inputchk) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4640 | xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL; |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4641 | const xmlChar *elem; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4642 | xmlChar type = 0; |
| 4643 | |
| 4644 | SKIP_BLANKS; |
| 4645 | GROW; |
| 4646 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4647 | int inputid = ctxt->input->id; |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 4648 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4649 | /* Recurse on first child */ |
| 4650 | NEXT; |
| 4651 | SKIP_BLANKS; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4652 | cur = ret = xmlParseElementChildrenContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4653 | SKIP_BLANKS; |
| 4654 | GROW; |
| 4655 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4656 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4657 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4658 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4659 | return(NULL); |
| 4660 | } |
| 4661 | cur = ret = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4662 | if (cur == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4663 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4664 | return(NULL); |
| 4665 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4666 | GROW; |
| 4667 | if (RAW == '?') { |
| 4668 | cur->ocur = XML_ELEMENT_CONTENT_OPT; |
| 4669 | NEXT; |
| 4670 | } else if (RAW == '*') { |
| 4671 | cur->ocur = XML_ELEMENT_CONTENT_MULT; |
| 4672 | NEXT; |
| 4673 | } else if (RAW == '+') { |
| 4674 | cur->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 4675 | NEXT; |
| 4676 | } else { |
| 4677 | cur->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 4678 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4679 | GROW; |
| 4680 | } |
| 4681 | SKIP_BLANKS; |
| 4682 | SHRINK; |
| 4683 | while (RAW != ')') { |
| 4684 | /* |
| 4685 | * Each loop we parse one separator and one element. |
| 4686 | */ |
| 4687 | if (RAW == ',') { |
| 4688 | if (type == 0) type = CUR; |
| 4689 | |
| 4690 | /* |
| 4691 | * Detect "Name | Name , Name" error |
| 4692 | */ |
| 4693 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4694 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4695 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4696 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 4697 | if ((last != NULL) && (last != ret)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4698 | xmlFreeElementContent(last); |
| 4699 | if (ret != NULL) |
| 4700 | xmlFreeElementContent(ret); |
| 4701 | return(NULL); |
| 4702 | } |
| 4703 | NEXT; |
| 4704 | |
| 4705 | op = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_SEQ); |
| 4706 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 4707 | if ((last != NULL) && (last != ret)) |
| 4708 | xmlFreeElementContent(last); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4709 | xmlFreeElementContent(ret); |
| 4710 | return(NULL); |
| 4711 | } |
| 4712 | if (last == NULL) { |
| 4713 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4714 | if (ret != NULL) |
| 4715 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4716 | ret = cur = op; |
| 4717 | } else { |
| 4718 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4719 | if (op != NULL) |
| 4720 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4721 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4722 | if (last != NULL) |
| 4723 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4724 | cur =op; |
| 4725 | last = NULL; |
| 4726 | } |
| 4727 | } else if (RAW == '|') { |
| 4728 | if (type == 0) type = CUR; |
| 4729 | |
| 4730 | /* |
| 4731 | * Detect "Name , Name | Name" error |
| 4732 | */ |
| 4733 | else if (type != CUR) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4734 | xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4735 | "xmlParseElementChildrenContentDecl : '%c' expected\n", |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4736 | type); |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 4737 | if ((last != NULL) && (last != ret)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4738 | xmlFreeElementContent(last); |
| 4739 | if (ret != NULL) |
| 4740 | xmlFreeElementContent(ret); |
| 4741 | return(NULL); |
| 4742 | } |
| 4743 | NEXT; |
| 4744 | |
| 4745 | op = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR); |
| 4746 | if (op == NULL) { |
Daniel Veillard | d54fa3e | 2002-02-20 16:48:52 +0000 | [diff] [blame] | 4747 | if ((last != NULL) && (last != ret)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4748 | xmlFreeElementContent(last); |
| 4749 | if (ret != NULL) |
| 4750 | xmlFreeElementContent(ret); |
| 4751 | return(NULL); |
| 4752 | } |
| 4753 | if (last == NULL) { |
| 4754 | op->c1 = ret; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4755 | if (ret != NULL) |
| 4756 | ret->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4757 | ret = cur = op; |
| 4758 | } else { |
| 4759 | cur->c2 = op; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4760 | if (op != NULL) |
| 4761 | op->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4762 | op->c1 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4763 | if (last != NULL) |
| 4764 | last->parent = op; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4765 | cur =op; |
| 4766 | last = NULL; |
| 4767 | } |
| 4768 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4769 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4770 | if (ret != NULL) |
| 4771 | xmlFreeElementContent(ret); |
| 4772 | return(NULL); |
| 4773 | } |
| 4774 | GROW; |
| 4775 | SKIP_BLANKS; |
| 4776 | GROW; |
| 4777 | if (RAW == '(') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4778 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4779 | /* Recurse on second child */ |
| 4780 | NEXT; |
| 4781 | SKIP_BLANKS; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4782 | last = xmlParseElementChildrenContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4783 | SKIP_BLANKS; |
| 4784 | } else { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4785 | elem = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4786 | if (elem == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 4787 | xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4788 | if (ret != NULL) |
| 4789 | xmlFreeElementContent(ret); |
| 4790 | return(NULL); |
| 4791 | } |
| 4792 | last = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4793 | if (RAW == '?') { |
| 4794 | last->ocur = XML_ELEMENT_CONTENT_OPT; |
| 4795 | NEXT; |
| 4796 | } else if (RAW == '*') { |
| 4797 | last->ocur = XML_ELEMENT_CONTENT_MULT; |
| 4798 | NEXT; |
| 4799 | } else if (RAW == '+') { |
| 4800 | last->ocur = XML_ELEMENT_CONTENT_PLUS; |
| 4801 | NEXT; |
| 4802 | } else { |
| 4803 | last->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 4804 | } |
| 4805 | } |
| 4806 | SKIP_BLANKS; |
| 4807 | GROW; |
| 4808 | } |
| 4809 | if ((cur != NULL) && (last != NULL)) { |
| 4810 | cur->c2 = last; |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 4811 | if (last != NULL) |
| 4812 | last->parent = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4813 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4814 | if ((ctxt->validate) && (ctxt->input->id != inputchk)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 4815 | xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 4816 | "Element content declaration doesn't start and stop in the same entity\n", |
| 4817 | NULL); |
Daniel Veillard | 8dc16a6 | 2002-02-19 21:08:48 +0000 | [diff] [blame] | 4818 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4819 | NEXT; |
| 4820 | if (RAW == '?') { |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 4821 | if (ret != NULL) { |
| 4822 | if ((ret->ocur == XML_ELEMENT_CONTENT_PLUS) || |
| 4823 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 4824 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 4825 | else |
| 4826 | ret->ocur = XML_ELEMENT_CONTENT_OPT; |
| 4827 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4828 | NEXT; |
| 4829 | } else if (RAW == '*') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 4830 | if (ret != NULL) { |
Daniel Veillard | e470df7 | 2001-04-18 21:41:07 +0000 | [diff] [blame] | 4831 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 4832 | cur = ret; |
| 4833 | /* |
| 4834 | * Some normalization: |
| 4835 | * (a | b* | c?)* == (a | b | c)* |
| 4836 | */ |
| 4837 | while (cur->type == XML_ELEMENT_CONTENT_OR) { |
| 4838 | if ((cur->c1 != NULL) && |
| 4839 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 4840 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 4841 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 4842 | if ((cur->c2 != NULL) && |
| 4843 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 4844 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) |
| 4845 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 4846 | cur = cur->c2; |
| 4847 | } |
| 4848 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4849 | NEXT; |
| 4850 | } else if (RAW == '+') { |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 4851 | if (ret != NULL) { |
| 4852 | int found = 0; |
| 4853 | |
William M. Brack | f8f2e8f | 2004-05-14 04:37:41 +0000 | [diff] [blame] | 4854 | if ((ret->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 4855 | (ret->ocur == XML_ELEMENT_CONTENT_MULT)) |
| 4856 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
William M. Brack | eb8509c | 2004-05-14 03:48:02 +0000 | [diff] [blame] | 4857 | else |
| 4858 | ret->ocur = XML_ELEMENT_CONTENT_PLUS; |
Daniel Veillard | ce2c2f0 | 2001-10-18 14:57:24 +0000 | [diff] [blame] | 4859 | /* |
| 4860 | * Some normalization: |
| 4861 | * (a | b*)+ == (a | b)* |
| 4862 | * (a | b?)+ == (a | b)* |
| 4863 | */ |
| 4864 | while (cur->type == XML_ELEMENT_CONTENT_OR) { |
| 4865 | if ((cur->c1 != NULL) && |
| 4866 | ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 4867 | (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 4868 | cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 4869 | found = 1; |
| 4870 | } |
| 4871 | if ((cur->c2 != NULL) && |
| 4872 | ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) || |
| 4873 | (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) { |
| 4874 | cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE; |
| 4875 | found = 1; |
| 4876 | } |
| 4877 | cur = cur->c2; |
| 4878 | } |
| 4879 | if (found) |
| 4880 | ret->ocur = XML_ELEMENT_CONTENT_MULT; |
| 4881 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4882 | NEXT; |
| 4883 | } |
| 4884 | return(ret); |
| 4885 | } |
| 4886 | |
| 4887 | /** |
| 4888 | * xmlParseElementContentDecl: |
| 4889 | * @ctxt: an XML parser context |
| 4890 | * @name: the name of the element being defined. |
| 4891 | * @result: the Element Content pointer will be stored here if any |
| 4892 | * |
| 4893 | * parse the declaration for an Element content either Mixed or Children, |
| 4894 | * the cases EMPTY and ANY are handled directly in xmlParseElementDecl |
| 4895 | * |
| 4896 | * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children |
| 4897 | * |
| 4898 | * returns: the type of element content XML_ELEMENT_TYPE_xxx |
| 4899 | */ |
| 4900 | |
| 4901 | int |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4902 | xmlParseElementContentDecl(xmlParserCtxtPtr ctxt, const xmlChar *name, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4903 | xmlElementContentPtr *result) { |
| 4904 | |
| 4905 | xmlElementContentPtr tree = NULL; |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4906 | int inputid = ctxt->input->id; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4907 | int res; |
| 4908 | |
| 4909 | *result = NULL; |
| 4910 | |
| 4911 | if (RAW != '(') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 4912 | xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 4913 | "xmlParseElementContentDecl : %s '(' expected\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4914 | return(-1); |
| 4915 | } |
| 4916 | NEXT; |
| 4917 | GROW; |
| 4918 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4919 | if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4920 | tree = xmlParseElementMixedContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4921 | res = XML_ELEMENT_TYPE_MIXED; |
| 4922 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4923 | tree = xmlParseElementChildrenContentDecl(ctxt, inputid); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4924 | res = XML_ELEMENT_TYPE_ELEMENT; |
| 4925 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4926 | SKIP_BLANKS; |
| 4927 | *result = tree; |
| 4928 | return(res); |
| 4929 | } |
| 4930 | |
| 4931 | /** |
| 4932 | * xmlParseElementDecl: |
| 4933 | * @ctxt: an XML parser context |
| 4934 | * |
| 4935 | * parse an Element declaration. |
| 4936 | * |
| 4937 | * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>' |
| 4938 | * |
| 4939 | * [ VC: Unique Element Type Declaration ] |
| 4940 | * No element type may be declared more than once |
| 4941 | * |
| 4942 | * Returns the type of the element, or -1 in case of error |
| 4943 | */ |
| 4944 | int |
| 4945 | xmlParseElementDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 4946 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4947 | int ret = -1; |
| 4948 | xmlElementContentPtr content = NULL; |
| 4949 | |
| 4950 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4951 | if (CMP9(CUR_PTR, '<', '!', 'E', 'L', 'E', 'M', 'E', 'N', 'T')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4952 | xmlParserInputPtr input = ctxt->input; |
| 4953 | |
| 4954 | SKIP(9); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4955 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4956 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4957 | "Space required after 'ELEMENT'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4958 | } |
| 4959 | SKIP_BLANKS; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 4960 | name = xmlParseName(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4961 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4962 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 4963 | "xmlParseElementDecl: no name for Element\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4964 | return(-1); |
| 4965 | } |
| 4966 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 4967 | xmlPopInput(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4968 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4969 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 4970 | "Space required after the element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4971 | } |
| 4972 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 4973 | if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4974 | SKIP(5); |
| 4975 | /* |
| 4976 | * Element must always be empty. |
| 4977 | */ |
| 4978 | ret = XML_ELEMENT_TYPE_EMPTY; |
| 4979 | } else if ((RAW == 'A') && (NXT(1) == 'N') && |
| 4980 | (NXT(2) == 'Y')) { |
| 4981 | SKIP(3); |
| 4982 | /* |
| 4983 | * Element is a generic container. |
| 4984 | */ |
| 4985 | ret = XML_ELEMENT_TYPE_ANY; |
| 4986 | } else if (RAW == '(') { |
| 4987 | ret = xmlParseElementContentDecl(ctxt, name, &content); |
| 4988 | } else { |
| 4989 | /* |
| 4990 | * [ WFC: PEs in Internal Subset ] error handling. |
| 4991 | */ |
| 4992 | if ((RAW == '%') && (ctxt->external == 0) && |
| 4993 | (ctxt->inputNr == 1)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4994 | xmlFatalErrMsg(ctxt, XML_ERR_PEREF_IN_INT_SUBSET, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 4995 | "PEReference: forbidden within markup decl in internal subset\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4996 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 4997 | xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 4998 | "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n"); |
| 4999 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5000 | return(-1); |
| 5001 | } |
| 5002 | |
| 5003 | SKIP_BLANKS; |
| 5004 | /* |
| 5005 | * Pop-up of finished entities. |
| 5006 | */ |
| 5007 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 5008 | xmlPopInput(ctxt); |
| 5009 | SKIP_BLANKS; |
| 5010 | |
| 5011 | if (RAW != '>') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5012 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5013 | } else { |
| 5014 | if (input != ctxt->input) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5015 | xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY, |
| 5016 | "Element declaration doesn't start and stop in the same entity\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
| 5019 | NEXT; |
| 5020 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 5021 | (ctxt->sax->elementDecl != NULL)) |
| 5022 | ctxt->sax->elementDecl(ctxt->userData, name, ret, |
| 5023 | content); |
| 5024 | } |
| 5025 | if (content != NULL) { |
| 5026 | xmlFreeElementContent(content); |
| 5027 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5028 | } |
| 5029 | return(ret); |
| 5030 | } |
| 5031 | |
| 5032 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5033 | * xmlParseConditionalSections |
| 5034 | * @ctxt: an XML parser context |
| 5035 | * |
| 5036 | * [61] conditionalSect ::= includeSect | ignoreSect |
| 5037 | * [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>' |
| 5038 | * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>' |
| 5039 | * [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)* |
| 5040 | * [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*) |
| 5041 | */ |
| 5042 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5043 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5044 | xmlParseConditionalSections(xmlParserCtxtPtr ctxt) { |
| 5045 | SKIP(3); |
| 5046 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5047 | if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5048 | SKIP(7); |
| 5049 | SKIP_BLANKS; |
| 5050 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5051 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5052 | } else { |
| 5053 | NEXT; |
| 5054 | } |
| 5055 | if (xmlParserDebugEntities) { |
| 5056 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 5057 | xmlGenericError(xmlGenericErrorContext, |
| 5058 | "%s(%d): ", ctxt->input->filename, |
| 5059 | ctxt->input->line); |
| 5060 | xmlGenericError(xmlGenericErrorContext, |
| 5061 | "Entering INCLUDE Conditional Section\n"); |
| 5062 | } |
| 5063 | |
| 5064 | while ((RAW != 0) && ((RAW != ']') || (NXT(1) != ']') || |
| 5065 | (NXT(2) != '>'))) { |
| 5066 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 5067 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5068 | |
| 5069 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 5070 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5071 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5072 | NEXT; |
| 5073 | } else if (RAW == '%') { |
| 5074 | xmlParsePEReference(ctxt); |
| 5075 | } else |
| 5076 | xmlParseMarkupDecl(ctxt); |
| 5077 | |
| 5078 | /* |
| 5079 | * Pop-up of finished entities. |
| 5080 | */ |
| 5081 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 5082 | xmlPopInput(ctxt); |
| 5083 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5084 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5085 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5086 | break; |
| 5087 | } |
| 5088 | } |
| 5089 | if (xmlParserDebugEntities) { |
| 5090 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 5091 | xmlGenericError(xmlGenericErrorContext, |
| 5092 | "%s(%d): ", ctxt->input->filename, |
| 5093 | ctxt->input->line); |
| 5094 | xmlGenericError(xmlGenericErrorContext, |
| 5095 | "Leaving INCLUDE Conditional Section\n"); |
| 5096 | } |
| 5097 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5098 | } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5099 | int state; |
William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 5100 | xmlParserInputState instate; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5101 | int depth = 0; |
| 5102 | |
| 5103 | SKIP(6); |
| 5104 | SKIP_BLANKS; |
| 5105 | if (RAW != '[') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5106 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5107 | } else { |
| 5108 | NEXT; |
| 5109 | } |
| 5110 | if (xmlParserDebugEntities) { |
| 5111 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 5112 | xmlGenericError(xmlGenericErrorContext, |
| 5113 | "%s(%d): ", ctxt->input->filename, |
| 5114 | ctxt->input->line); |
| 5115 | xmlGenericError(xmlGenericErrorContext, |
| 5116 | "Entering IGNORE Conditional Section\n"); |
| 5117 | } |
| 5118 | |
| 5119 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5120 | * Parse up to the end of the conditional section |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5121 | * But disable SAX event generating DTD building in the meantime |
| 5122 | */ |
| 5123 | state = ctxt->disableSAX; |
| 5124 | instate = ctxt->instate; |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 5125 | if (ctxt->recovery == 0) ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5126 | ctxt->instate = XML_PARSER_IGNORE; |
| 5127 | |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 5128 | while ((depth >= 0) && (RAW != 0)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5129 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 5130 | depth++; |
| 5131 | SKIP(3); |
| 5132 | continue; |
| 5133 | } |
| 5134 | if ((RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) { |
| 5135 | if (--depth >= 0) SKIP(3); |
| 5136 | continue; |
| 5137 | } |
| 5138 | NEXT; |
| 5139 | continue; |
| 5140 | } |
| 5141 | |
| 5142 | ctxt->disableSAX = state; |
| 5143 | ctxt->instate = instate; |
| 5144 | |
| 5145 | if (xmlParserDebugEntities) { |
| 5146 | if ((ctxt->input != NULL) && (ctxt->input->filename)) |
| 5147 | xmlGenericError(xmlGenericErrorContext, |
| 5148 | "%s(%d): ", ctxt->input->filename, |
| 5149 | ctxt->input->line); |
| 5150 | xmlGenericError(xmlGenericErrorContext, |
| 5151 | "Leaving IGNORE Conditional Section\n"); |
| 5152 | } |
| 5153 | |
| 5154 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5155 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5156 | } |
| 5157 | |
| 5158 | if (RAW == 0) |
| 5159 | SHRINK; |
| 5160 | |
| 5161 | if (RAW == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5162 | xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5163 | } else { |
| 5164 | SKIP(3); |
| 5165 | } |
| 5166 | } |
| 5167 | |
| 5168 | /** |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5169 | * xmlParseMarkupDecl: |
| 5170 | * @ctxt: an XML parser context |
| 5171 | * |
| 5172 | * parse Markup declarations |
| 5173 | * |
| 5174 | * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl | |
| 5175 | * NotationDecl | PI | Comment |
| 5176 | * |
| 5177 | * [ VC: Proper Declaration/PE Nesting ] |
| 5178 | * Parameter-entity replacement text must be properly nested with |
| 5179 | * markup declarations. That is to say, if either the first character |
| 5180 | * or the last character of a markup declaration (markupdecl above) is |
| 5181 | * contained in the replacement text for a parameter-entity reference, |
| 5182 | * both must be contained in the same replacement text. |
| 5183 | * |
| 5184 | * [ WFC: PEs in Internal Subset ] |
| 5185 | * In the internal DTD subset, parameter-entity references can occur |
| 5186 | * only where markup declarations can occur, not within markup declarations. |
| 5187 | * (This does not apply to references that occur in external parameter |
| 5188 | * entities or to the external subset.) |
| 5189 | */ |
| 5190 | void |
| 5191 | xmlParseMarkupDecl(xmlParserCtxtPtr ctxt) { |
| 5192 | GROW; |
| 5193 | xmlParseElementDecl(ctxt); |
| 5194 | xmlParseAttributeListDecl(ctxt); |
| 5195 | xmlParseEntityDecl(ctxt); |
| 5196 | xmlParseNotationDecl(ctxt); |
| 5197 | xmlParsePI(ctxt); |
| 5198 | xmlParseComment(ctxt); |
| 5199 | /* |
| 5200 | * This is only for internal subset. On external entities, |
| 5201 | * the replacement is done before parsing stage |
| 5202 | */ |
| 5203 | if ((ctxt->external == 0) && (ctxt->inputNr == 1)) |
| 5204 | xmlParsePEReference(ctxt); |
| 5205 | |
| 5206 | /* |
| 5207 | * Conditional sections are allowed from entities included |
| 5208 | * by PE References in the internal subset. |
| 5209 | */ |
| 5210 | if ((ctxt->external == 0) && (ctxt->inputNr > 1)) { |
| 5211 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 5212 | xmlParseConditionalSections(ctxt); |
| 5213 | } |
| 5214 | } |
| 5215 | |
| 5216 | ctxt->instate = XML_PARSER_DTD; |
| 5217 | } |
| 5218 | |
| 5219 | /** |
| 5220 | * xmlParseTextDecl: |
| 5221 | * @ctxt: an XML parser context |
| 5222 | * |
| 5223 | * parse an XML declaration header for external entities |
| 5224 | * |
| 5225 | * [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>' |
| 5226 | * |
| 5227 | * Question: Seems that EncodingDecl is mandatory ? Is that a typo ? |
| 5228 | */ |
| 5229 | |
| 5230 | void |
| 5231 | xmlParseTextDecl(xmlParserCtxtPtr ctxt) { |
| 5232 | xmlChar *version; |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 5233 | const xmlChar *encoding; |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5234 | |
| 5235 | /* |
| 5236 | * We know that '<?xml' is here. |
| 5237 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5238 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5239 | SKIP(5); |
| 5240 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5241 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5242 | return; |
| 5243 | } |
| 5244 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5245 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5246 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5247 | "Space needed after '<?xml'\n"); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5248 | } |
| 5249 | SKIP_BLANKS; |
| 5250 | |
| 5251 | /* |
| 5252 | * We may have the VersionInfo here. |
| 5253 | */ |
| 5254 | version = xmlParseVersionInfo(ctxt); |
| 5255 | if (version == NULL) |
| 5256 | version = xmlCharStrdup(XML_DEFAULT_VERSION); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 5257 | else { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5258 | if (!IS_BLANK_CH(CUR)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5259 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 5260 | "Space needed here\n"); |
Daniel Veillard | 401c211 | 2002-01-07 16:54:10 +0000 | [diff] [blame] | 5261 | } |
| 5262 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5263 | ctxt->input->version = version; |
| 5264 | |
| 5265 | /* |
| 5266 | * We must have the encoding declaration |
| 5267 | */ |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 5268 | encoding = xmlParseEncodingDecl(ctxt); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5269 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 5270 | /* |
| 5271 | * The XML REC instructs us to stop parsing right here |
| 5272 | */ |
| 5273 | return; |
| 5274 | } |
Daniel Veillard | f5cb3cd | 2003-10-28 13:58:13 +0000 | [diff] [blame] | 5275 | if ((encoding == NULL) && (ctxt->errNo == XML_ERR_OK)) { |
| 5276 | xmlFatalErrMsg(ctxt, XML_ERR_MISSING_ENCODING, |
| 5277 | "Missing encoding in text declaration\n"); |
| 5278 | } |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5279 | |
| 5280 | SKIP_BLANKS; |
| 5281 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 5282 | SKIP(2); |
| 5283 | } else if (RAW == '>') { |
| 5284 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5285 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5286 | NEXT; |
| 5287 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5288 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Daniel Veillard | 5e3eecb | 2001-07-31 15:10:53 +0000 | [diff] [blame] | 5289 | MOVETO_ENDTAG(CUR_PTR); |
| 5290 | NEXT; |
| 5291 | } |
| 5292 | } |
| 5293 | |
| 5294 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5295 | * xmlParseExternalSubset: |
| 5296 | * @ctxt: an XML parser context |
| 5297 | * @ExternalID: the external identifier |
| 5298 | * @SystemID: the system identifier (or URL) |
| 5299 | * |
| 5300 | * parse Markup declarations from an external subset |
| 5301 | * |
| 5302 | * [30] extSubset ::= textDecl? extSubsetDecl |
| 5303 | * |
| 5304 | * [31] extSubsetDecl ::= (markupdecl | conditionalSect | PEReference | S) * |
| 5305 | */ |
| 5306 | void |
| 5307 | xmlParseExternalSubset(xmlParserCtxtPtr ctxt, const xmlChar *ExternalID, |
| 5308 | const xmlChar *SystemID) { |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 5309 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5310 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5311 | if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5312 | xmlParseTextDecl(ctxt); |
| 5313 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 5314 | /* |
| 5315 | * The XML REC instructs us to stop parsing right here |
| 5316 | */ |
| 5317 | ctxt->instate = XML_PARSER_EOF; |
| 5318 | return; |
| 5319 | } |
| 5320 | } |
| 5321 | if (ctxt->myDoc == NULL) { |
| 5322 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 5323 | } |
| 5324 | if ((ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset == NULL)) |
| 5325 | xmlCreateIntSubset(ctxt->myDoc, NULL, ExternalID, SystemID); |
| 5326 | |
| 5327 | ctxt->instate = XML_PARSER_DTD; |
| 5328 | ctxt->external = 1; |
| 5329 | while (((RAW == '<') && (NXT(1) == '?')) || |
| 5330 | ((RAW == '<') && (NXT(1) == '!')) || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5331 | (RAW == '%') || IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5332 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 5333 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5334 | |
| 5335 | GROW; |
| 5336 | if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) { |
| 5337 | xmlParseConditionalSections(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 5338 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5339 | NEXT; |
| 5340 | } else if (RAW == '%') { |
| 5341 | xmlParsePEReference(ctxt); |
| 5342 | } else |
| 5343 | xmlParseMarkupDecl(ctxt); |
| 5344 | |
| 5345 | /* |
| 5346 | * Pop-up of finished entities. |
| 5347 | */ |
| 5348 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 5349 | xmlPopInput(ctxt); |
| 5350 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 5351 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5352 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5353 | break; |
| 5354 | } |
| 5355 | } |
| 5356 | |
| 5357 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5358 | xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5359 | } |
| 5360 | |
| 5361 | } |
| 5362 | |
| 5363 | /** |
| 5364 | * xmlParseReference: |
| 5365 | * @ctxt: an XML parser context |
| 5366 | * |
| 5367 | * parse and handle entity references in content, depending on the SAX |
| 5368 | * interface, this may end-up in a call to character() if this is a |
| 5369 | * CharRef, a predefined entity, if there is no reference() callback. |
| 5370 | * or if the parser was asked to switch to that mode. |
| 5371 | * |
| 5372 | * [67] Reference ::= EntityRef | CharRef |
| 5373 | */ |
| 5374 | void |
| 5375 | xmlParseReference(xmlParserCtxtPtr ctxt) { |
| 5376 | xmlEntityPtr ent; |
| 5377 | xmlChar *val; |
| 5378 | if (RAW != '&') return; |
| 5379 | |
| 5380 | if (NXT(1) == '#') { |
| 5381 | int i = 0; |
| 5382 | xmlChar out[10]; |
| 5383 | int hex = NXT(2); |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5384 | int value = xmlParseCharRef(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5385 | |
| 5386 | if (ctxt->charset != XML_CHAR_ENCODING_UTF8) { |
| 5387 | /* |
| 5388 | * So we are using non-UTF-8 buffers |
| 5389 | * Check that the char fit on 8bits, if not |
| 5390 | * generate a CharRef. |
| 5391 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5392 | if (value <= 0xFF) { |
| 5393 | out[0] = value; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5394 | out[1] = 0; |
| 5395 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 5396 | (!ctxt->disableSAX)) |
| 5397 | ctxt->sax->characters(ctxt->userData, out, 1); |
| 5398 | } else { |
| 5399 | if ((hex == 'x') || (hex == 'X')) |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5400 | snprintf((char *)out, sizeof(out), "#x%X", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5401 | else |
Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 5402 | snprintf((char *)out, sizeof(out), "#%d", value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5403 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 5404 | (!ctxt->disableSAX)) |
| 5405 | ctxt->sax->reference(ctxt->userData, out); |
| 5406 | } |
| 5407 | } else { |
| 5408 | /* |
| 5409 | * Just encode the value in UTF-8 |
| 5410 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 5411 | COPY_BUF(0 ,out, i, value); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5412 | out[i] = 0; |
| 5413 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 5414 | (!ctxt->disableSAX)) |
| 5415 | ctxt->sax->characters(ctxt->userData, out, i); |
| 5416 | } |
| 5417 | } else { |
| 5418 | ent = xmlParseEntityRef(ctxt); |
| 5419 | if (ent == NULL) return; |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 5420 | if (!ctxt->wellFormed) |
| 5421 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5422 | if ((ent->name != NULL) && |
| 5423 | (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY)) { |
| 5424 | xmlNodePtr list = NULL; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 5425 | xmlParserErrors ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5426 | |
| 5427 | |
| 5428 | /* |
| 5429 | * The first reference to the entity trigger a parsing phase |
| 5430 | * where the ent->children is filled with the result from |
| 5431 | * the parsing. |
| 5432 | */ |
| 5433 | if (ent->children == NULL) { |
| 5434 | xmlChar *value; |
| 5435 | value = ent->content; |
| 5436 | |
| 5437 | /* |
| 5438 | * Check that this entity is well formed |
| 5439 | */ |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 5440 | if ((value != NULL) && (value[0] != 0) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5441 | (value[1] == 0) && (value[0] == '<') && |
| 5442 | (xmlStrEqual(ent->name, BAD_CAST "lt"))) { |
| 5443 | /* |
| 5444 | * DONE: get definite answer on this !!! |
| 5445 | * Lots of entity decls are used to declare a single |
| 5446 | * char |
| 5447 | * <!ENTITY lt "<"> |
| 5448 | * Which seems to be valid since |
| 5449 | * 2.4: The ampersand character (&) and the left angle |
| 5450 | * bracket (<) may appear in their literal form only |
| 5451 | * when used ... They are also legal within the literal |
| 5452 | * entity value of an internal entity declaration;i |
| 5453 | * see "4.3.2 Well-Formed Parsed Entities". |
| 5454 | * IMHO 2.4 and 4.3.2 are directly in contradiction. |
| 5455 | * Looking at the OASIS test suite and James Clark |
| 5456 | * tests, this is broken. However the XML REC uses |
| 5457 | * it. Is the XML REC not well-formed ???? |
| 5458 | * This is a hack to avoid this problem |
| 5459 | * |
| 5460 | * ANSWER: since lt gt amp .. are already defined, |
| 5461 | * this is a redefinition and hence the fact that the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5462 | * content is not well balanced is not a Wf error, this |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5463 | * is lousy but acceptable. |
| 5464 | */ |
| 5465 | list = xmlNewDocText(ctxt->myDoc, value); |
| 5466 | if (list != NULL) { |
| 5467 | if ((ent->etype == XML_INTERNAL_GENERAL_ENTITY) && |
| 5468 | (ent->children == NULL)) { |
| 5469 | ent->children = list; |
| 5470 | ent->last = list; |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 5471 | ent->owner = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5472 | list->parent = (xmlNodePtr) ent; |
| 5473 | } else { |
| 5474 | xmlFreeNodeList(list); |
| 5475 | } |
| 5476 | } else if (list != NULL) { |
| 5477 | xmlFreeNodeList(list); |
| 5478 | } |
| 5479 | } else { |
| 5480 | /* |
| 5481 | * 4.3.2: An internal general parsed entity is well-formed |
| 5482 | * if its replacement text matches the production labeled |
| 5483 | * content. |
| 5484 | */ |
Daniel Veillard | b5a60ec | 2002-03-18 11:45:56 +0000 | [diff] [blame] | 5485 | |
| 5486 | void *user_data; |
| 5487 | /* |
| 5488 | * This is a bit hackish but this seems the best |
| 5489 | * way to make sure both SAX and DOM entity support |
| 5490 | * behaves okay. |
| 5491 | */ |
| 5492 | if (ctxt->userData == ctxt) |
| 5493 | user_data = NULL; |
| 5494 | else |
| 5495 | user_data = ctxt->userData; |
| 5496 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5497 | if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) { |
| 5498 | ctxt->depth++; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 5499 | ret = xmlParseBalancedChunkMemoryInternal(ctxt, |
| 5500 | value, user_data, &list); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5501 | ctxt->depth--; |
| 5502 | } else if (ent->etype == |
| 5503 | XML_EXTERNAL_GENERAL_PARSED_ENTITY) { |
| 5504 | ctxt->depth++; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 5505 | ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt, |
Daniel Veillard | b5a60ec | 2002-03-18 11:45:56 +0000 | [diff] [blame] | 5506 | ctxt->sax, user_data, ctxt->depth, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 5507 | ent->URI, ent->ExternalID, &list); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5508 | ctxt->depth--; |
| 5509 | } else { |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 5510 | ret = XML_ERR_ENTITY_PE_INTERNAL; |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5511 | xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 5512 | "invalid entity type found\n", NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5513 | } |
| 5514 | if (ret == XML_ERR_ENTITY_LOOP) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5515 | xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL); |
Daniel Veillard | bb7ddb3 | 2002-02-17 21:26:33 +0000 | [diff] [blame] | 5516 | return; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 5517 | } else if ((ret == XML_ERR_OK) && (list != NULL)) { |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 5518 | if (((ent->etype == XML_INTERNAL_GENERAL_ENTITY) || |
| 5519 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY))&& |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5520 | (ent->children == NULL)) { |
| 5521 | ent->children = list; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5522 | if (ctxt->replaceEntities) { |
| 5523 | /* |
| 5524 | * Prune it directly in the generated document |
| 5525 | * except for single text nodes. |
| 5526 | */ |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 5527 | if (((list->type == XML_TEXT_NODE) && |
| 5528 | (list->next == NULL)) || |
| 5529 | (ctxt->parseMode == XML_PARSE_READER)) { |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5530 | list->parent = (xmlNodePtr) ent; |
| 5531 | list = NULL; |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 5532 | ent->owner = 1; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5533 | } else { |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 5534 | ent->owner = 0; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5535 | while (list != NULL) { |
| 5536 | list->parent = (xmlNodePtr) ctxt->node; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 5537 | list->doc = ctxt->myDoc; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5538 | if (list->next == NULL) |
| 5539 | ent->last = list; |
| 5540 | list = list->next; |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 5541 | } |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5542 | list = ent->children; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 5543 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 5544 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 5545 | xmlAddEntityReference(ent, list, NULL); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 5546 | #endif /* LIBXML_LEGACY_ENABLED */ |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5547 | } |
| 5548 | } else { |
Daniel Veillard | 2d84a89 | 2002-12-30 00:01:08 +0000 | [diff] [blame] | 5549 | ent->owner = 1; |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5550 | while (list != NULL) { |
| 5551 | list->parent = (xmlNodePtr) ent; |
| 5552 | if (list->next == NULL) |
| 5553 | ent->last = list; |
| 5554 | list = list->next; |
| 5555 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5556 | } |
| 5557 | } else { |
| 5558 | xmlFreeNodeList(list); |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5559 | list = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5560 | } |
William M. Brack | b670e2e | 2003-09-27 01:05:55 +0000 | [diff] [blame] | 5561 | } else if ((ret != XML_ERR_OK) && |
| 5562 | (ret != XML_WAR_UNDECLARED_ENTITY)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 5563 | xmlFatalErr(ctxt, ret, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5564 | } else if (list != NULL) { |
| 5565 | xmlFreeNodeList(list); |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5566 | list = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5567 | } |
| 5568 | } |
| 5569 | } |
| 5570 | if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) && |
| 5571 | (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) { |
| 5572 | /* |
| 5573 | * Create a node. |
| 5574 | */ |
| 5575 | ctxt->sax->reference(ctxt->userData, ent->name); |
| 5576 | return; |
| 5577 | } else if (ctxt->replaceEntities) { |
| 5578 | if ((ctxt->node != NULL) && (ent->children != NULL)) { |
| 5579 | /* |
| 5580 | * Seems we are generating the DOM content, do |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5581 | * a simple tree copy for all references except the first |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 5582 | * In the first occurrence list contains the replacement. |
| 5583 | * progressive == 2 means we are operating on the Reader |
| 5584 | * and since nodes are discarded we must copy all the time. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5585 | */ |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 5586 | if (((list == NULL) && (ent->owner == 0)) || |
| 5587 | (ctxt->parseMode == XML_PARSE_READER)) { |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5588 | xmlNodePtr nw = NULL, cur, firstChild = NULL; |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 5589 | |
| 5590 | /* |
| 5591 | * when operating on a reader, the entities definitions |
| 5592 | * are always owning the entities subtree. |
| 5593 | if (ctxt->parseMode == XML_PARSE_READER) |
| 5594 | ent->owner = 1; |
| 5595 | */ |
| 5596 | |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5597 | cur = ent->children; |
| 5598 | while (cur != NULL) { |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5599 | nw = xmlCopyNode(cur, 1); |
| 5600 | if (nw != NULL) { |
| 5601 | nw->_private = cur->_private; |
Daniel Veillard | 8f87244 | 2003-01-09 23:19:02 +0000 | [diff] [blame] | 5602 | if (firstChild == NULL){ |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5603 | firstChild = nw; |
Daniel Veillard | 8f87244 | 2003-01-09 23:19:02 +0000 | [diff] [blame] | 5604 | } |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 5605 | nw = xmlAddChild(ctxt->node, nw); |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 5606 | } |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 5607 | if (cur == ent->last) { |
| 5608 | /* |
| 5609 | * needed to detect some strange empty |
| 5610 | * node cases in the reader tests |
| 5611 | */ |
| 5612 | if ((ctxt->parseMode == XML_PARSE_READER) && |
| 5613 | (nw->type == XML_ELEMENT_NODE) && |
| 5614 | (nw->children == NULL)) |
| 5615 | nw->extra = 1; |
| 5616 | |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5617 | break; |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 5618 | } |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5619 | cur = cur->next; |
| 5620 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 5621 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 5622 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5623 | xmlAddEntityReference(ent, firstChild, nw); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 5624 | #endif /* LIBXML_LEGACY_ENABLED */ |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5625 | } else if (list == NULL) { |
| 5626 | xmlNodePtr nw = NULL, cur, next, last, |
| 5627 | firstChild = NULL; |
| 5628 | /* |
| 5629 | * Copy the entity child list and make it the new |
| 5630 | * entity child list. The goal is to make sure any |
| 5631 | * ID or REF referenced will be the one from the |
| 5632 | * document content and not the entity copy. |
| 5633 | */ |
| 5634 | cur = ent->children; |
| 5635 | ent->children = NULL; |
| 5636 | last = ent->last; |
| 5637 | ent->last = NULL; |
| 5638 | while (cur != NULL) { |
| 5639 | next = cur->next; |
| 5640 | cur->next = NULL; |
| 5641 | cur->parent = NULL; |
| 5642 | nw = xmlCopyNode(cur, 1); |
| 5643 | if (nw != NULL) { |
| 5644 | nw->_private = cur->_private; |
| 5645 | if (firstChild == NULL){ |
| 5646 | firstChild = cur; |
| 5647 | } |
| 5648 | xmlAddChild((xmlNodePtr) ent, nw); |
| 5649 | xmlAddChild(ctxt->node, cur); |
| 5650 | } |
| 5651 | if (cur == last) |
| 5652 | break; |
| 5653 | cur = next; |
| 5654 | } |
| 5655 | ent->owner = 1; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 5656 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 5657 | if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) |
| 5658 | xmlAddEntityReference(ent, firstChild, nw); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 5659 | #endif /* LIBXML_LEGACY_ENABLED */ |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5660 | } else { |
| 5661 | /* |
| 5662 | * the name change is to avoid coalescing of the |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 5663 | * node with a possible previous text one which |
| 5664 | * would make ent->children a dangling pointer |
Daniel Veillard | 62f313b | 2001-07-04 19:49:14 +0000 | [diff] [blame] | 5665 | */ |
| 5666 | if (ent->children->type == XML_TEXT_NODE) |
| 5667 | ent->children->name = xmlStrdup(BAD_CAST "nbktext"); |
| 5668 | if ((ent->last != ent->children) && |
| 5669 | (ent->last->type == XML_TEXT_NODE)) |
| 5670 | ent->last->name = xmlStrdup(BAD_CAST "nbktext"); |
| 5671 | xmlAddChildList(ctxt->node, ent->children); |
| 5672 | } |
| 5673 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5674 | /* |
| 5675 | * This is to avoid a nasty side effect, see |
| 5676 | * characters() in SAX.c |
| 5677 | */ |
| 5678 | ctxt->nodemem = 0; |
| 5679 | ctxt->nodelen = 0; |
| 5680 | return; |
| 5681 | } else { |
| 5682 | /* |
| 5683 | * Probably running in SAX mode |
| 5684 | */ |
| 5685 | xmlParserInputPtr input; |
| 5686 | |
| 5687 | input = xmlNewEntityInputStream(ctxt, ent); |
| 5688 | xmlPushInput(ctxt, input); |
| 5689 | if ((ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) && |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 5690 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && |
| 5691 | (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5692 | xmlParseTextDecl(ctxt); |
| 5693 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 5694 | /* |
| 5695 | * The XML REC instructs us to stop parsing right here |
| 5696 | */ |
| 5697 | ctxt->instate = XML_PARSER_EOF; |
| 5698 | return; |
| 5699 | } |
| 5700 | if (input->standalone == 1) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5701 | xmlFatalErr(ctxt, XML_ERR_EXT_ENTITY_STANDALONE, |
| 5702 | NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5703 | } |
| 5704 | } |
| 5705 | return; |
| 5706 | } |
| 5707 | } |
| 5708 | } else { |
| 5709 | val = ent->content; |
| 5710 | if (val == NULL) return; |
| 5711 | /* |
| 5712 | * inline the entity. |
| 5713 | */ |
| 5714 | if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) && |
| 5715 | (!ctxt->disableSAX)) |
| 5716 | ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val)); |
| 5717 | } |
| 5718 | } |
| 5719 | } |
| 5720 | |
| 5721 | /** |
| 5722 | * xmlParseEntityRef: |
| 5723 | * @ctxt: an XML parser context |
| 5724 | * |
| 5725 | * parse ENTITY references declarations |
| 5726 | * |
| 5727 | * [68] EntityRef ::= '&' Name ';' |
| 5728 | * |
| 5729 | * [ WFC: Entity Declared ] |
| 5730 | * In a document without any DTD, a document with only an internal DTD |
| 5731 | * subset which contains no parameter entity references, or a document |
| 5732 | * with "standalone='yes'", the Name given in the entity reference |
| 5733 | * must match that in an entity declaration, except that well-formed |
| 5734 | * documents need not declare any of the following entities: amp, lt, |
| 5735 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 5736 | * any reference to it. Similarly, the declaration of a general entity |
| 5737 | * must precede any reference to it which appears in a default value in an |
| 5738 | * attribute-list declaration. Note that if entities are declared in the |
| 5739 | * external subset or in external parameter entities, a non-validating |
| 5740 | * processor is not obligated to read and process their declarations; |
| 5741 | * for such documents, the rule that an entity must be declared is a |
| 5742 | * well-formedness constraint only if standalone='yes'. |
| 5743 | * |
| 5744 | * [ WFC: Parsed Entity ] |
| 5745 | * An entity reference must not contain the name of an unparsed entity |
| 5746 | * |
| 5747 | * Returns the xmlEntityPtr if found, or NULL otherwise. |
| 5748 | */ |
| 5749 | xmlEntityPtr |
| 5750 | xmlParseEntityRef(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 5751 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5752 | xmlEntityPtr ent = NULL; |
| 5753 | |
| 5754 | GROW; |
| 5755 | |
| 5756 | if (RAW == '&') { |
| 5757 | NEXT; |
| 5758 | name = xmlParseName(ctxt); |
| 5759 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5760 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5761 | "xmlParseEntityRef: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5762 | } else { |
| 5763 | if (RAW == ';') { |
| 5764 | NEXT; |
| 5765 | /* |
| 5766 | * Ask first SAX for entity resolution, otherwise try the |
| 5767 | * predefined set. |
| 5768 | */ |
| 5769 | if (ctxt->sax != NULL) { |
| 5770 | if (ctxt->sax->getEntity != NULL) |
| 5771 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 5772 | if ((ctxt->wellFormed == 1 ) && (ent == NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5773 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 39eb88b | 2003-03-11 11:21:28 +0000 | [diff] [blame] | 5774 | if ((ctxt->wellFormed == 1 ) && (ent == NULL) && |
| 5775 | (ctxt->userData==ctxt)) { |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5776 | ent = xmlSAX2GetEntity(ctxt, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5777 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5778 | } |
| 5779 | /* |
| 5780 | * [ WFC: Entity Declared ] |
| 5781 | * In a document without any DTD, a document with only an |
| 5782 | * internal DTD subset which contains no parameter entity |
| 5783 | * references, or a document with "standalone='yes'", the |
| 5784 | * Name given in the entity reference must match that in an |
| 5785 | * entity declaration, except that well-formed documents |
| 5786 | * need not declare any of the following entities: amp, lt, |
| 5787 | * gt, apos, quot. |
| 5788 | * The declaration of a parameter entity must precede any |
| 5789 | * reference to it. |
| 5790 | * Similarly, the declaration of a general entity must |
| 5791 | * precede any reference to it which appears in a default |
| 5792 | * value in an attribute-list declaration. Note that if |
| 5793 | * entities are declared in the external subset or in |
| 5794 | * external parameter entities, a non-validating processor |
| 5795 | * is not obligated to read and process their declarations; |
| 5796 | * for such documents, the rule that an entity must be |
| 5797 | * declared is a well-formedness constraint only if |
| 5798 | * standalone='yes'. |
| 5799 | */ |
| 5800 | if (ent == NULL) { |
| 5801 | if ((ctxt->standalone == 1) || |
| 5802 | ((ctxt->hasExternalSubset == 0) && |
| 5803 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5804 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5805 | "Entity '%s' not defined\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5806 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5807 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5808 | "Entity '%s' not defined\n", name); |
| 5809 | } |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5810 | ctxt->valid = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5811 | } |
| 5812 | |
| 5813 | /* |
| 5814 | * [ WFC: Parsed Entity ] |
| 5815 | * An entity reference must not contain the name of an |
| 5816 | * unparsed entity |
| 5817 | */ |
| 5818 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5819 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5820 | "Entity reference to unparsed entity %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5821 | } |
| 5822 | |
| 5823 | /* |
| 5824 | * [ WFC: No External Entity References ] |
| 5825 | * Attribute values cannot contain direct or indirect |
| 5826 | * entity references to external entities. |
| 5827 | */ |
| 5828 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 5829 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5830 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
| 5831 | "Attribute references external entity '%s'\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5832 | } |
| 5833 | /* |
| 5834 | * [ WFC: No < in Attribute Values ] |
| 5835 | * The replacement text of any entity referred to directly or |
| 5836 | * indirectly in an attribute value (other than "<") must |
| 5837 | * not contain a <. |
| 5838 | */ |
| 5839 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 5840 | (ent != NULL) && |
| 5841 | (!xmlStrEqual(ent->name, BAD_CAST "lt")) && |
| 5842 | (ent->content != NULL) && |
| 5843 | (xmlStrchr(ent->content, '<'))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5844 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5845 | "'<' in entity '%s' is not allowed in attributes values\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5846 | } |
| 5847 | |
| 5848 | /* |
| 5849 | * Internal check, no parameter entities here ... |
| 5850 | */ |
| 5851 | else { |
| 5852 | switch (ent->etype) { |
| 5853 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 5854 | case XML_EXTERNAL_PARAMETER_ENTITY: |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5855 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 5856 | "Attempt to reference the parameter entity '%s'\n", |
| 5857 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5858 | break; |
| 5859 | default: |
| 5860 | break; |
| 5861 | } |
| 5862 | } |
| 5863 | |
| 5864 | /* |
| 5865 | * [ WFC: No Recursion ] |
| 5866 | * A parsed entity must not contain a recursive reference |
| 5867 | * to itself, either directly or indirectly. |
| 5868 | * Done somewhere else |
| 5869 | */ |
| 5870 | |
| 5871 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5872 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5873 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5874 | } |
| 5875 | } |
| 5876 | return(ent); |
| 5877 | } |
| 5878 | |
| 5879 | /** |
| 5880 | * xmlParseStringEntityRef: |
| 5881 | * @ctxt: an XML parser context |
| 5882 | * @str: a pointer to an index in the string |
| 5883 | * |
| 5884 | * parse ENTITY references declarations, but this version parses it from |
| 5885 | * a string value. |
| 5886 | * |
| 5887 | * [68] EntityRef ::= '&' Name ';' |
| 5888 | * |
| 5889 | * [ WFC: Entity Declared ] |
| 5890 | * In a document without any DTD, a document with only an internal DTD |
| 5891 | * subset which contains no parameter entity references, or a document |
| 5892 | * with "standalone='yes'", the Name given in the entity reference |
| 5893 | * must match that in an entity declaration, except that well-formed |
| 5894 | * documents need not declare any of the following entities: amp, lt, |
| 5895 | * gt, apos, quot. The declaration of a parameter entity must precede |
| 5896 | * any reference to it. Similarly, the declaration of a general entity |
| 5897 | * must precede any reference to it which appears in a default value in an |
| 5898 | * attribute-list declaration. Note that if entities are declared in the |
| 5899 | * external subset or in external parameter entities, a non-validating |
| 5900 | * processor is not obligated to read and process their declarations; |
| 5901 | * for such documents, the rule that an entity must be declared is a |
| 5902 | * well-formedness constraint only if standalone='yes'. |
| 5903 | * |
| 5904 | * [ WFC: Parsed Entity ] |
| 5905 | * An entity reference must not contain the name of an unparsed entity |
| 5906 | * |
| 5907 | * Returns the xmlEntityPtr if found, or NULL otherwise. The str pointer |
| 5908 | * is updated to the current location in the string. |
| 5909 | */ |
| 5910 | xmlEntityPtr |
| 5911 | xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) { |
| 5912 | xmlChar *name; |
| 5913 | const xmlChar *ptr; |
| 5914 | xmlChar cur; |
| 5915 | xmlEntityPtr ent = NULL; |
| 5916 | |
| 5917 | if ((str == NULL) || (*str == NULL)) |
| 5918 | return(NULL); |
| 5919 | ptr = *str; |
| 5920 | cur = *ptr; |
| 5921 | if (cur == '&') { |
| 5922 | ptr++; |
| 5923 | cur = *ptr; |
| 5924 | name = xmlParseStringName(ctxt, &ptr); |
| 5925 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 5926 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 5927 | "xmlParseStringEntityRef: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5928 | } else { |
| 5929 | if (*ptr == ';') { |
| 5930 | ptr++; |
| 5931 | /* |
| 5932 | * Ask first SAX for entity resolution, otherwise try the |
| 5933 | * predefined set. |
| 5934 | */ |
| 5935 | if (ctxt->sax != NULL) { |
| 5936 | if (ctxt->sax->getEntity != NULL) |
| 5937 | ent = ctxt->sax->getEntity(ctxt->userData, name); |
| 5938 | if (ent == NULL) |
| 5939 | ent = xmlGetPredefinedEntity(name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5940 | if ((ent == NULL) && (ctxt->userData==ctxt)) { |
Daniel Veillard | 1af9a41 | 2003-08-20 22:54:39 +0000 | [diff] [blame] | 5941 | ent = xmlSAX2GetEntity(ctxt, name); |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 5942 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5943 | } |
| 5944 | /* |
| 5945 | * [ WFC: Entity Declared ] |
| 5946 | * In a document without any DTD, a document with only an |
| 5947 | * internal DTD subset which contains no parameter entity |
| 5948 | * references, or a document with "standalone='yes'", the |
| 5949 | * Name given in the entity reference must match that in an |
| 5950 | * entity declaration, except that well-formed documents |
| 5951 | * need not declare any of the following entities: amp, lt, |
| 5952 | * gt, apos, quot. |
| 5953 | * The declaration of a parameter entity must precede any |
| 5954 | * reference to it. |
| 5955 | * Similarly, the declaration of a general entity must |
| 5956 | * precede any reference to it which appears in a default |
| 5957 | * value in an attribute-list declaration. Note that if |
| 5958 | * entities are declared in the external subset or in |
| 5959 | * external parameter entities, a non-validating processor |
| 5960 | * is not obligated to read and process their declarations; |
| 5961 | * for such documents, the rule that an entity must be |
| 5962 | * declared is a well-formedness constraint only if |
| 5963 | * standalone='yes'. |
| 5964 | */ |
| 5965 | if (ent == NULL) { |
| 5966 | if ((ctxt->standalone == 1) || |
| 5967 | ((ctxt->hasExternalSubset == 0) && |
| 5968 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 5969 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5970 | "Entity '%s' not defined\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5971 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5972 | xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY, |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5973 | "Entity '%s' not defined\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5974 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5975 | } |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 5976 | /* TODO ? check regressions ctxt->valid = 0; */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5977 | } |
| 5978 | |
| 5979 | /* |
| 5980 | * [ WFC: Parsed Entity ] |
| 5981 | * An entity reference must not contain the name of an |
| 5982 | * unparsed entity |
| 5983 | */ |
| 5984 | else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5985 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5986 | "Entity reference to unparsed entity %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5987 | } |
| 5988 | |
| 5989 | /* |
| 5990 | * [ WFC: No External Entity References ] |
| 5991 | * Attribute values cannot contain direct or indirect |
| 5992 | * entity references to external entities. |
| 5993 | */ |
| 5994 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 5995 | (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 5996 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5997 | "Attribute references external entity '%s'\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 5998 | } |
| 5999 | /* |
| 6000 | * [ WFC: No < in Attribute Values ] |
| 6001 | * The replacement text of any entity referred to directly or |
| 6002 | * indirectly in an attribute value (other than "<") must |
| 6003 | * not contain a <. |
| 6004 | */ |
| 6005 | else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) && |
| 6006 | (ent != NULL) && |
| 6007 | (!xmlStrEqual(ent->name, BAD_CAST "lt")) && |
| 6008 | (ent->content != NULL) && |
| 6009 | (xmlStrchr(ent->content, '<'))) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6010 | xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, |
| 6011 | "'<' in entity '%s' is not allowed in attributes values\n", |
| 6012 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6013 | } |
| 6014 | |
| 6015 | /* |
| 6016 | * Internal check, no parameter entities here ... |
| 6017 | */ |
| 6018 | else { |
| 6019 | switch (ent->etype) { |
| 6020 | case XML_INTERNAL_PARAMETER_ENTITY: |
| 6021 | case XML_EXTERNAL_PARAMETER_ENTITY: |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6022 | xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER, |
| 6023 | "Attempt to reference the parameter entity '%s'\n", |
| 6024 | name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6025 | break; |
| 6026 | default: |
| 6027 | break; |
| 6028 | } |
| 6029 | } |
| 6030 | |
| 6031 | /* |
| 6032 | * [ WFC: No Recursion ] |
| 6033 | * A parsed entity must not contain a recursive reference |
| 6034 | * to itself, either directly or indirectly. |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6035 | * Done somewhere else |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6036 | */ |
| 6037 | |
| 6038 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6039 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6040 | } |
| 6041 | xmlFree(name); |
| 6042 | } |
| 6043 | } |
| 6044 | *str = ptr; |
| 6045 | return(ent); |
| 6046 | } |
| 6047 | |
| 6048 | /** |
| 6049 | * xmlParsePEReference: |
| 6050 | * @ctxt: an XML parser context |
| 6051 | * |
| 6052 | * parse PEReference declarations |
| 6053 | * The entity content is handled directly by pushing it's content as |
| 6054 | * a new input stream. |
| 6055 | * |
| 6056 | * [69] PEReference ::= '%' Name ';' |
| 6057 | * |
| 6058 | * [ WFC: No Recursion ] |
| 6059 | * A parsed entity must not contain a recursive |
| 6060 | * reference to itself, either directly or indirectly. |
| 6061 | * |
| 6062 | * [ WFC: Entity Declared ] |
| 6063 | * In a document without any DTD, a document with only an internal DTD |
| 6064 | * subset which contains no parameter entity references, or a document |
| 6065 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 6066 | * entity must precede any reference to it... |
| 6067 | * |
| 6068 | * [ VC: Entity Declared ] |
| 6069 | * In a document with an external subset or external parameter entities |
| 6070 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 6071 | * must precede any reference to it... |
| 6072 | * |
| 6073 | * [ WFC: In DTD ] |
| 6074 | * Parameter-entity references may only appear in the DTD. |
| 6075 | * NOTE: misleading but this is handled. |
| 6076 | */ |
| 6077 | void |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 6078 | xmlParsePEReference(xmlParserCtxtPtr ctxt) |
| 6079 | { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6080 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6081 | xmlEntityPtr entity = NULL; |
| 6082 | xmlParserInputPtr input; |
| 6083 | |
| 6084 | if (RAW == '%') { |
| 6085 | NEXT; |
Daniel Veillard | 76d66f4 | 2001-05-16 21:05:17 +0000 | [diff] [blame] | 6086 | name = xmlParseName(ctxt); |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 6087 | if (name == NULL) { |
| 6088 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6089 | "xmlParsePEReference: no name\n"); |
| 6090 | } else { |
| 6091 | if (RAW == ';') { |
| 6092 | NEXT; |
| 6093 | if ((ctxt->sax != NULL) && |
| 6094 | (ctxt->sax->getParameterEntity != NULL)) |
| 6095 | entity = ctxt->sax->getParameterEntity(ctxt->userData, |
| 6096 | name); |
| 6097 | if (entity == NULL) { |
| 6098 | /* |
| 6099 | * [ WFC: Entity Declared ] |
| 6100 | * In a document without any DTD, a document with only an |
| 6101 | * internal DTD subset which contains no parameter entity |
| 6102 | * references, or a document with "standalone='yes'", ... |
| 6103 | * ... The declaration of a parameter entity must precede |
| 6104 | * any reference to it... |
| 6105 | */ |
| 6106 | if ((ctxt->standalone == 1) || |
| 6107 | ((ctxt->hasExternalSubset == 0) && |
| 6108 | (ctxt->hasPErefs == 0))) { |
| 6109 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
| 6110 | "PEReference: %%%s; not found\n", |
| 6111 | name); |
| 6112 | } else { |
| 6113 | /* |
| 6114 | * [ VC: Entity Declared ] |
| 6115 | * In a document with an external subset or external |
| 6116 | * parameter entities with "standalone='no'", ... |
| 6117 | * ... The declaration of a parameter entity must |
| 6118 | * precede any reference to it... |
| 6119 | */ |
| 6120 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 6121 | "PEReference: %%%s; not found\n", |
| 6122 | name, NULL); |
| 6123 | ctxt->valid = 0; |
| 6124 | } |
| 6125 | } else { |
| 6126 | /* |
| 6127 | * Internal checking in case the entity quest barfed |
| 6128 | */ |
| 6129 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 6130 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
| 6131 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 6132 | "Internal: %%%s; is not a parameter entity\n", |
| 6133 | name, NULL); |
| 6134 | } else if (ctxt->input->free != deallocblankswrapper) { |
| 6135 | input = |
| 6136 | xmlNewBlanksWrapperInputStream(ctxt, entity); |
| 6137 | xmlPushInput(ctxt, input); |
| 6138 | } else { |
| 6139 | /* |
| 6140 | * TODO !!! |
| 6141 | * handle the extra spaces added before and after |
| 6142 | * c.f. http://www.w3.org/TR/REC-xml#as-PE |
| 6143 | */ |
| 6144 | input = xmlNewEntityInputStream(ctxt, entity); |
| 6145 | xmlPushInput(ctxt, input); |
| 6146 | if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) && |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 6147 | (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6148 | (IS_BLANK_CH(NXT(5)))) { |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 6149 | xmlParseTextDecl(ctxt); |
| 6150 | if (ctxt->errNo == |
| 6151 | XML_ERR_UNSUPPORTED_ENCODING) { |
| 6152 | /* |
| 6153 | * The XML REC instructs us to stop parsing |
| 6154 | * right here |
| 6155 | */ |
| 6156 | ctxt->instate = XML_PARSER_EOF; |
| 6157 | return; |
| 6158 | } |
| 6159 | } |
| 6160 | } |
| 6161 | } |
| 6162 | ctxt->hasPErefs = 1; |
| 6163 | } else { |
| 6164 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
| 6165 | } |
| 6166 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6167 | } |
| 6168 | } |
| 6169 | |
| 6170 | /** |
| 6171 | * xmlParseStringPEReference: |
| 6172 | * @ctxt: an XML parser context |
| 6173 | * @str: a pointer to an index in the string |
| 6174 | * |
| 6175 | * parse PEReference declarations |
| 6176 | * |
| 6177 | * [69] PEReference ::= '%' Name ';' |
| 6178 | * |
| 6179 | * [ WFC: No Recursion ] |
| 6180 | * A parsed entity must not contain a recursive |
| 6181 | * reference to itself, either directly or indirectly. |
| 6182 | * |
| 6183 | * [ WFC: Entity Declared ] |
| 6184 | * In a document without any DTD, a document with only an internal DTD |
| 6185 | * subset which contains no parameter entity references, or a document |
| 6186 | * with "standalone='yes'", ... ... The declaration of a parameter |
| 6187 | * entity must precede any reference to it... |
| 6188 | * |
| 6189 | * [ VC: Entity Declared ] |
| 6190 | * In a document with an external subset or external parameter entities |
| 6191 | * with "standalone='no'", ... ... The declaration of a parameter entity |
| 6192 | * must precede any reference to it... |
| 6193 | * |
| 6194 | * [ WFC: In DTD ] |
| 6195 | * Parameter-entity references may only appear in the DTD. |
| 6196 | * NOTE: misleading but this is handled. |
| 6197 | * |
| 6198 | * Returns the string of the entity content. |
| 6199 | * str is updated to the current value of the index |
| 6200 | */ |
| 6201 | xmlEntityPtr |
| 6202 | xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) { |
| 6203 | const xmlChar *ptr; |
| 6204 | xmlChar cur; |
| 6205 | xmlChar *name; |
| 6206 | xmlEntityPtr entity = NULL; |
| 6207 | |
| 6208 | if ((str == NULL) || (*str == NULL)) return(NULL); |
| 6209 | ptr = *str; |
| 6210 | cur = *ptr; |
| 6211 | if (cur == '%') { |
| 6212 | ptr++; |
| 6213 | cur = *ptr; |
| 6214 | name = xmlParseStringName(ctxt, &ptr); |
| 6215 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6216 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6217 | "xmlParseStringPEReference: no name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6218 | } else { |
| 6219 | cur = *ptr; |
| 6220 | if (cur == ';') { |
| 6221 | ptr++; |
| 6222 | cur = *ptr; |
| 6223 | if ((ctxt->sax != NULL) && |
| 6224 | (ctxt->sax->getParameterEntity != NULL)) |
| 6225 | entity = ctxt->sax->getParameterEntity(ctxt->userData, |
| 6226 | name); |
| 6227 | if (entity == NULL) { |
| 6228 | /* |
| 6229 | * [ WFC: Entity Declared ] |
| 6230 | * In a document without any DTD, a document with only an |
| 6231 | * internal DTD subset which contains no parameter entity |
| 6232 | * references, or a document with "standalone='yes'", ... |
| 6233 | * ... The declaration of a parameter entity must precede |
| 6234 | * any reference to it... |
| 6235 | */ |
| 6236 | if ((ctxt->standalone == 1) || |
| 6237 | ((ctxt->hasExternalSubset == 0) && |
| 6238 | (ctxt->hasPErefs == 0))) { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6239 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6240 | "PEReference: %%%s; not found\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6241 | } else { |
| 6242 | /* |
| 6243 | * [ VC: Entity Declared ] |
| 6244 | * In a document with an external subset or external |
| 6245 | * parameter entities with "standalone='no'", ... |
| 6246 | * ... The declaration of a parameter entity must |
| 6247 | * precede any reference to it... |
| 6248 | */ |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6249 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 6250 | "PEReference: %%%s; not found\n", |
| 6251 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6252 | ctxt->valid = 0; |
| 6253 | } |
| 6254 | } else { |
| 6255 | /* |
| 6256 | * Internal checking in case the entity quest barfed |
| 6257 | */ |
| 6258 | if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && |
| 6259 | (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6260 | xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY, |
| 6261 | "%%%s; is not a parameter entity\n", |
| 6262 | name, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6263 | } |
| 6264 | } |
| 6265 | ctxt->hasPErefs = 1; |
| 6266 | } else { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6267 | xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6268 | } |
| 6269 | xmlFree(name); |
| 6270 | } |
| 6271 | } |
| 6272 | *str = ptr; |
| 6273 | return(entity); |
| 6274 | } |
| 6275 | |
| 6276 | /** |
| 6277 | * xmlParseDocTypeDecl: |
| 6278 | * @ctxt: an XML parser context |
| 6279 | * |
| 6280 | * parse a DOCTYPE declaration |
| 6281 | * |
| 6282 | * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S? |
| 6283 | * ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 6284 | * |
| 6285 | * [ VC: Root Element Type ] |
| 6286 | * The Name in the document type declaration must match the element |
| 6287 | * type of the root element. |
| 6288 | */ |
| 6289 | |
| 6290 | void |
| 6291 | xmlParseDocTypeDecl(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6292 | const xmlChar *name = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6293 | xmlChar *ExternalID = NULL; |
| 6294 | xmlChar *URI = NULL; |
| 6295 | |
| 6296 | /* |
| 6297 | * We know that '<!DOCTYPE' has been detected. |
| 6298 | */ |
| 6299 | SKIP(9); |
| 6300 | |
| 6301 | SKIP_BLANKS; |
| 6302 | |
| 6303 | /* |
| 6304 | * Parse the DOCTYPE name. |
| 6305 | */ |
| 6306 | name = xmlParseName(ctxt); |
| 6307 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6308 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 6309 | "xmlParseDocTypeDecl : no DOCTYPE name !\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6310 | } |
| 6311 | ctxt->intSubName = name; |
| 6312 | |
| 6313 | SKIP_BLANKS; |
| 6314 | |
| 6315 | /* |
| 6316 | * Check for SystemID and ExternalID |
| 6317 | */ |
| 6318 | URI = xmlParseExternalID(ctxt, &ExternalID, 1); |
| 6319 | |
| 6320 | if ((URI != NULL) || (ExternalID != NULL)) { |
| 6321 | ctxt->hasExternalSubset = 1; |
| 6322 | } |
| 6323 | ctxt->extSubURI = URI; |
| 6324 | ctxt->extSubSystem = ExternalID; |
| 6325 | |
| 6326 | SKIP_BLANKS; |
| 6327 | |
| 6328 | /* |
| 6329 | * Create and update the internal subset. |
| 6330 | */ |
| 6331 | if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) && |
| 6332 | (!ctxt->disableSAX)) |
| 6333 | ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI); |
| 6334 | |
| 6335 | /* |
| 6336 | * Is there any internal subset declarations ? |
| 6337 | * they are handled separately in xmlParseInternalSubset() |
| 6338 | */ |
| 6339 | if (RAW == '[') |
| 6340 | return; |
| 6341 | |
| 6342 | /* |
| 6343 | * We should be at the end of the DOCTYPE declaration. |
| 6344 | */ |
| 6345 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6346 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6347 | } |
| 6348 | NEXT; |
| 6349 | } |
| 6350 | |
| 6351 | /** |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 6352 | * xmlParseInternalSubset: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6353 | * @ctxt: an XML parser context |
| 6354 | * |
| 6355 | * parse the internal subset declaration |
| 6356 | * |
| 6357 | * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>' |
| 6358 | */ |
| 6359 | |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 6360 | static void |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6361 | xmlParseInternalSubset(xmlParserCtxtPtr ctxt) { |
| 6362 | /* |
| 6363 | * Is there any DTD definition ? |
| 6364 | */ |
| 6365 | if (RAW == '[') { |
| 6366 | ctxt->instate = XML_PARSER_DTD; |
| 6367 | NEXT; |
| 6368 | /* |
| 6369 | * Parse the succession of Markup declarations and |
| 6370 | * PEReferences. |
| 6371 | * Subsequence (markupdecl | PEReference | S)* |
| 6372 | */ |
| 6373 | while (RAW != ']') { |
| 6374 | const xmlChar *check = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 6375 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6376 | |
| 6377 | SKIP_BLANKS; |
| 6378 | xmlParseMarkupDecl(ctxt); |
| 6379 | xmlParsePEReference(ctxt); |
| 6380 | |
| 6381 | /* |
| 6382 | * Pop-up of finished entities. |
| 6383 | */ |
| 6384 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 6385 | xmlPopInput(ctxt); |
| 6386 | |
| 6387 | if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6388 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6389 | "xmlParseInternalSubset: error detected in Markup declaration\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6390 | break; |
| 6391 | } |
| 6392 | } |
| 6393 | if (RAW == ']') { |
| 6394 | NEXT; |
| 6395 | SKIP_BLANKS; |
| 6396 | } |
| 6397 | } |
| 6398 | |
| 6399 | /* |
| 6400 | * We should be at the end of the DOCTYPE declaration. |
| 6401 | */ |
| 6402 | if (RAW != '>') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6403 | xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6404 | } |
| 6405 | NEXT; |
| 6406 | } |
| 6407 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 6408 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6409 | /** |
| 6410 | * xmlParseAttribute: |
| 6411 | * @ctxt: an XML parser context |
| 6412 | * @value: a xmlChar ** used to store the value of the attribute |
| 6413 | * |
| 6414 | * parse an attribute |
| 6415 | * |
| 6416 | * [41] Attribute ::= Name Eq AttValue |
| 6417 | * |
| 6418 | * [ WFC: No External Entity References ] |
| 6419 | * Attribute values cannot contain direct or indirect entity references |
| 6420 | * to external entities. |
| 6421 | * |
| 6422 | * [ WFC: No < in Attribute Values ] |
| 6423 | * The replacement text of any entity referred to directly or indirectly in |
| 6424 | * an attribute value (other than "<") must not contain a <. |
| 6425 | * |
| 6426 | * [ VC: Attribute Value Type ] |
| 6427 | * The attribute must have been declared; the value must be of the type |
| 6428 | * declared for it. |
| 6429 | * |
| 6430 | * [25] Eq ::= S? '=' S? |
| 6431 | * |
| 6432 | * With namespace: |
| 6433 | * |
| 6434 | * [NS 11] Attribute ::= QName Eq AttValue |
| 6435 | * |
| 6436 | * Also the case QName == xmlns:??? is handled independently as a namespace |
| 6437 | * definition. |
| 6438 | * |
| 6439 | * Returns the attribute name, and the value in *value. |
| 6440 | */ |
| 6441 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6442 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6443 | xmlParseAttribute(xmlParserCtxtPtr ctxt, xmlChar **value) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6444 | const xmlChar *name; |
| 6445 | xmlChar *val; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6446 | |
| 6447 | *value = NULL; |
Daniel Veillard | 878eab0 | 2002-02-19 13:46:09 +0000 | [diff] [blame] | 6448 | GROW; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6449 | name = xmlParseName(ctxt); |
| 6450 | if (name == NULL) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6451 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6452 | "error parsing attribute name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6453 | return(NULL); |
| 6454 | } |
| 6455 | |
| 6456 | /* |
| 6457 | * read the value |
| 6458 | */ |
| 6459 | SKIP_BLANKS; |
| 6460 | if (RAW == '=') { |
| 6461 | NEXT; |
| 6462 | SKIP_BLANKS; |
| 6463 | val = xmlParseAttValue(ctxt); |
| 6464 | ctxt->instate = XML_PARSER_CONTENT; |
| 6465 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6466 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6467 | "Specification mandate value for attribute %s\n", name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6468 | return(NULL); |
| 6469 | } |
| 6470 | |
| 6471 | /* |
| 6472 | * Check that xml:lang conforms to the specification |
| 6473 | * No more registered as an error, just generate a warning now |
| 6474 | * since this was deprecated in XML second edition |
| 6475 | */ |
| 6476 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) { |
| 6477 | if (!xmlCheckLanguageID(val)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 6478 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 6479 | "Malformed value for xml:lang : %s\n", |
| 6480 | val, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6481 | } |
| 6482 | } |
| 6483 | |
| 6484 | /* |
| 6485 | * Check that xml:space conforms to the specification |
| 6486 | */ |
| 6487 | if (xmlStrEqual(name, BAD_CAST "xml:space")) { |
| 6488 | if (xmlStrEqual(val, BAD_CAST "default")) |
| 6489 | *(ctxt->space) = 0; |
| 6490 | else if (xmlStrEqual(val, BAD_CAST "preserve")) |
| 6491 | *(ctxt->space) = 1; |
| 6492 | else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 6493 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Daniel Veillard | 642104e | 2003-03-26 16:32:05 +0000 | [diff] [blame] | 6494 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6495 | val); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6496 | } |
| 6497 | } |
| 6498 | |
| 6499 | *value = val; |
| 6500 | return(name); |
| 6501 | } |
| 6502 | |
| 6503 | /** |
| 6504 | * xmlParseStartTag: |
| 6505 | * @ctxt: an XML parser context |
| 6506 | * |
| 6507 | * parse a start of tag either for rule element or |
| 6508 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 6509 | * |
| 6510 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 6511 | * |
| 6512 | * [ WFC: Unique Att Spec ] |
| 6513 | * No attribute name may appear more than once in the same start-tag or |
| 6514 | * empty-element tag. |
| 6515 | * |
| 6516 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 6517 | * |
| 6518 | * [ WFC: Unique Att Spec ] |
| 6519 | * No attribute name may appear more than once in the same start-tag or |
| 6520 | * empty-element tag. |
| 6521 | * |
| 6522 | * With namespace: |
| 6523 | * |
| 6524 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 6525 | * |
| 6526 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 6527 | * |
| 6528 | * Returns the element name parsed |
| 6529 | */ |
| 6530 | |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6531 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6532 | xmlParseStartTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6533 | const xmlChar *name; |
| 6534 | const xmlChar *attname; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6535 | xmlChar *attvalue; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6536 | const xmlChar **atts = ctxt->atts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6537 | int nbatts = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6538 | int maxatts = ctxt->maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6539 | int i; |
| 6540 | |
| 6541 | if (RAW != '<') return(NULL); |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 6542 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6543 | |
| 6544 | name = xmlParseName(ctxt); |
| 6545 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6546 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6547 | "xmlParseStartTag: invalid element name\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6548 | return(NULL); |
| 6549 | } |
| 6550 | |
| 6551 | /* |
| 6552 | * Now parse the attributes, it ends up with the ending |
| 6553 | * |
| 6554 | * (S Attribute)* S? |
| 6555 | */ |
| 6556 | SKIP_BLANKS; |
| 6557 | GROW; |
| 6558 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 6559 | while ((RAW != '>') && |
| 6560 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 6561 | (IS_BYTE_CHAR(RAW))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6562 | const xmlChar *q = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 6563 | unsigned int cons = ctxt->input->consumed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6564 | |
| 6565 | attname = xmlParseAttribute(ctxt, &attvalue); |
| 6566 | if ((attname != NULL) && (attvalue != NULL)) { |
| 6567 | /* |
| 6568 | * [ WFC: Unique Att Spec ] |
| 6569 | * No attribute name may appear more than once in the same |
| 6570 | * start-tag or empty-element tag. |
| 6571 | */ |
| 6572 | for (i = 0; i < nbatts;i += 2) { |
| 6573 | if (xmlStrEqual(atts[i], attname)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6574 | xmlErrAttributeDup(ctxt, NULL, attname); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6575 | xmlFree(attvalue); |
| 6576 | goto failed; |
| 6577 | } |
| 6578 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6579 | /* |
| 6580 | * Add the pair to atts |
| 6581 | */ |
| 6582 | if (atts == NULL) { |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6583 | maxatts = 22; /* allow for 10 attrs by default */ |
| 6584 | atts = (const xmlChar **) |
| 6585 | xmlMalloc(maxatts * sizeof(xmlChar *)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6586 | if (atts == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6587 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6588 | if (attvalue != NULL) |
| 6589 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6590 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6591 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6592 | ctxt->atts = atts; |
| 6593 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6594 | } else if (nbatts + 4 > maxatts) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6595 | const xmlChar **n; |
| 6596 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6597 | maxatts *= 2; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6598 | n = (const xmlChar **) xmlRealloc((void *) atts, |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6599 | maxatts * sizeof(const xmlChar *)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6600 | if (n == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6601 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6602 | if (attvalue != NULL) |
| 6603 | xmlFree(attvalue); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6604 | goto failed; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6605 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6606 | atts = n; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6607 | ctxt->atts = atts; |
| 6608 | ctxt->maxatts = maxatts; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6609 | } |
| 6610 | atts[nbatts++] = attname; |
| 6611 | atts[nbatts++] = attvalue; |
| 6612 | atts[nbatts] = NULL; |
| 6613 | atts[nbatts + 1] = NULL; |
| 6614 | } else { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6615 | if (attvalue != NULL) |
| 6616 | xmlFree(attvalue); |
| 6617 | } |
| 6618 | |
| 6619 | failed: |
| 6620 | |
Daniel Veillard | 3772de3 | 2002-12-17 10:31:45 +0000 | [diff] [blame] | 6621 | GROW |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6622 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 6623 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6624 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6625 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 6626 | "attributes construct error\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6627 | } |
| 6628 | SKIP_BLANKS; |
Daniel Veillard | 02111c1 | 2003-02-24 19:14:52 +0000 | [diff] [blame] | 6629 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 6630 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6631 | xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR, |
| 6632 | "xmlParseStartTag: problem parsing attributes\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6633 | break; |
| 6634 | } |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6635 | SHRINK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6636 | GROW; |
| 6637 | } |
| 6638 | |
| 6639 | /* |
| 6640 | * SAX: Start of Element ! |
| 6641 | */ |
| 6642 | if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) && |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 6643 | (!ctxt->disableSAX)) { |
| 6644 | if (nbatts > 0) |
| 6645 | ctxt->sax->startElement(ctxt->userData, name, atts); |
| 6646 | else |
| 6647 | ctxt->sax->startElement(ctxt->userData, name, NULL); |
| 6648 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6649 | |
| 6650 | if (atts != NULL) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6651 | /* Free only the content strings */ |
| 6652 | for (i = 1;i < nbatts;i+=2) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6653 | if (atts[i] != NULL) |
| 6654 | xmlFree((xmlChar *) atts[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6655 | } |
| 6656 | return(name); |
| 6657 | } |
| 6658 | |
| 6659 | /** |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6660 | * xmlParseEndTag1: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6661 | * @ctxt: an XML parser context |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6662 | * @line: line of the start tag |
| 6663 | * @nsNr: number of namespaces on the start tag |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6664 | * |
| 6665 | * parse an end of tag |
| 6666 | * |
| 6667 | * [42] ETag ::= '</' Name S? '>' |
| 6668 | * |
| 6669 | * With namespace |
| 6670 | * |
| 6671 | * [NS 9] ETag ::= '</' QName S? '>' |
| 6672 | */ |
| 6673 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 6674 | static void |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6675 | xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 6676 | const xmlChar *name; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6677 | |
| 6678 | GROW; |
| 6679 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6680 | xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED, |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6681 | "xmlParseEndTag: '</' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6682 | return; |
| 6683 | } |
| 6684 | SKIP(2); |
| 6685 | |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 6686 | name = xmlParseNameAndCompare(ctxt,ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6687 | |
| 6688 | /* |
| 6689 | * We should definitely be at the ending "S? '>'" part |
| 6690 | */ |
| 6691 | GROW; |
| 6692 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 6693 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6694 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6695 | } else |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 6696 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6697 | |
| 6698 | /* |
| 6699 | * [ WFC: Element Type Match ] |
| 6700 | * The Name in an element's end-tag must match the element type in the |
| 6701 | * start-tag. |
| 6702 | * |
| 6703 | */ |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 6704 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6705 | if (name == NULL) name = BAD_CAST "unparseable"; |
| 6706 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 6707 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 6708 | ctxt->name, line, name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6709 | } |
| 6710 | |
| 6711 | /* |
| 6712 | * SAX: End of Tag |
| 6713 | */ |
| 6714 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 6715 | (!ctxt->disableSAX)) |
Daniel Veillard | 46de64e | 2002-05-29 08:21:33 +0000 | [diff] [blame] | 6716 | ctxt->sax->endElement(ctxt->userData, ctxt->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6717 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6718 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6719 | spacePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 6720 | return; |
| 6721 | } |
| 6722 | |
| 6723 | /** |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 6724 | * xmlParseEndTag: |
| 6725 | * @ctxt: an XML parser context |
| 6726 | * |
| 6727 | * parse an end of tag |
| 6728 | * |
| 6729 | * [42] ETag ::= '</' Name S? '>' |
| 6730 | * |
| 6731 | * With namespace |
| 6732 | * |
| 6733 | * [NS 9] ETag ::= '</' QName S? '>' |
| 6734 | */ |
| 6735 | |
| 6736 | void |
| 6737 | xmlParseEndTag(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6738 | xmlParseEndTag1(ctxt, 0); |
| 6739 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 6740 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6741 | |
| 6742 | /************************************************************************ |
| 6743 | * * |
| 6744 | * SAX 2 specific operations * |
| 6745 | * * |
| 6746 | ************************************************************************/ |
| 6747 | |
| 6748 | static const xmlChar * |
| 6749 | xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) { |
| 6750 | int len = 0, l; |
| 6751 | int c; |
| 6752 | int count = 0; |
| 6753 | |
| 6754 | /* |
| 6755 | * Handler for more complex cases |
| 6756 | */ |
| 6757 | GROW; |
| 6758 | c = CUR_CHAR(l); |
| 6759 | if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */ |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 6760 | (!IS_LETTER(c) && (c != '_'))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6761 | return(NULL); |
| 6762 | } |
| 6763 | |
| 6764 | while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */ |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 6765 | ((IS_LETTER(c)) || (IS_DIGIT(c)) || |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 6766 | (c == '.') || (c == '-') || (c == '_') || |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 6767 | (IS_COMBINING(c)) || |
| 6768 | (IS_EXTENDER(c)))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6769 | if (count++ > 100) { |
| 6770 | count = 0; |
| 6771 | GROW; |
| 6772 | } |
| 6773 | len += l; |
| 6774 | NEXTL(l); |
| 6775 | c = CUR_CHAR(l); |
| 6776 | } |
| 6777 | return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len)); |
| 6778 | } |
| 6779 | |
| 6780 | /* |
| 6781 | * xmlGetNamespace: |
| 6782 | * @ctxt: an XML parser context |
| 6783 | * @prefix: the prefix to lookup |
| 6784 | * |
| 6785 | * Lookup the namespace name for the @prefix (which ca be NULL) |
| 6786 | * The prefix must come from the @ctxt->dict dictionnary |
| 6787 | * |
| 6788 | * Returns the namespace name or NULL if not bound |
| 6789 | */ |
| 6790 | static const xmlChar * |
| 6791 | xmlGetNamespace(xmlParserCtxtPtr ctxt, const xmlChar *prefix) { |
| 6792 | int i; |
| 6793 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6794 | if (prefix == ctxt->str_xml) return(ctxt->str_xml_ns); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6795 | for (i = ctxt->nsNr - 2;i >= 0;i-=2) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6796 | if (ctxt->nsTab[i] == prefix) { |
| 6797 | if ((prefix == NULL) && (*ctxt->nsTab[i + 1] == 0)) |
| 6798 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6799 | return(ctxt->nsTab[i + 1]); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6800 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6801 | return(NULL); |
| 6802 | } |
| 6803 | |
| 6804 | /** |
| 6805 | * xmlParseNCName: |
| 6806 | * @ctxt: an XML parser context |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 6807 | * @len: lenght of the string parsed |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6808 | * |
| 6809 | * parse an XML name. |
| 6810 | * |
| 6811 | * [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' | |
| 6812 | * CombiningChar | Extender |
| 6813 | * |
| 6814 | * [5NS] NCName ::= (Letter | '_') (NCNameChar)* |
| 6815 | * |
| 6816 | * Returns the Name parsed or NULL |
| 6817 | */ |
| 6818 | |
| 6819 | static const xmlChar * |
| 6820 | xmlParseNCName(xmlParserCtxtPtr ctxt) { |
| 6821 | const xmlChar *in; |
| 6822 | const xmlChar *ret; |
| 6823 | int count = 0; |
| 6824 | |
| 6825 | /* |
| 6826 | * Accelerator for simple ASCII names |
| 6827 | */ |
| 6828 | in = ctxt->input->cur; |
| 6829 | if (((*in >= 0x61) && (*in <= 0x7A)) || |
| 6830 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 6831 | (*in == '_')) { |
| 6832 | in++; |
| 6833 | while (((*in >= 0x61) && (*in <= 0x7A)) || |
| 6834 | ((*in >= 0x41) && (*in <= 0x5A)) || |
| 6835 | ((*in >= 0x30) && (*in <= 0x39)) || |
| 6836 | (*in == '_') || (*in == '-') || |
| 6837 | (*in == '.')) |
| 6838 | in++; |
| 6839 | if ((*in > 0) && (*in < 0x80)) { |
| 6840 | count = in - ctxt->input->cur; |
| 6841 | ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count); |
| 6842 | ctxt->input->cur = in; |
| 6843 | ctxt->nbChars += count; |
| 6844 | ctxt->input->col += count; |
| 6845 | if (ret == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 6846 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6847 | } |
| 6848 | return(ret); |
| 6849 | } |
| 6850 | } |
| 6851 | return(xmlParseNCNameComplex(ctxt)); |
| 6852 | } |
| 6853 | |
| 6854 | /** |
| 6855 | * xmlParseQName: |
| 6856 | * @ctxt: an XML parser context |
| 6857 | * @prefix: pointer to store the prefix part |
| 6858 | * |
| 6859 | * parse an XML Namespace QName |
| 6860 | * |
| 6861 | * [6] QName ::= (Prefix ':')? LocalPart |
| 6862 | * [7] Prefix ::= NCName |
| 6863 | * [8] LocalPart ::= NCName |
| 6864 | * |
| 6865 | * Returns the Name parsed or NULL |
| 6866 | */ |
| 6867 | |
| 6868 | static const xmlChar * |
| 6869 | xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) { |
| 6870 | const xmlChar *l, *p; |
| 6871 | |
| 6872 | GROW; |
| 6873 | |
| 6874 | l = xmlParseNCName(ctxt); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 6875 | if (l == NULL) { |
| 6876 | if (CUR == ':') { |
| 6877 | l = xmlParseName(ctxt); |
| 6878 | if (l != NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6879 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 6880 | "Failed to parse QName '%s'\n", l, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 6881 | *prefix = NULL; |
| 6882 | return(l); |
| 6883 | } |
| 6884 | } |
| 6885 | return(NULL); |
| 6886 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6887 | if (CUR == ':') { |
| 6888 | NEXT; |
| 6889 | p = l; |
| 6890 | l = xmlParseNCName(ctxt); |
| 6891 | if (l == NULL) { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 6892 | xmlChar *tmp; |
| 6893 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6894 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 6895 | "Failed to parse QName '%s:'\n", p, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 6896 | tmp = xmlBuildQName(BAD_CAST "", p, NULL, 0); |
| 6897 | p = xmlDictLookup(ctxt->dict, tmp, -1); |
| 6898 | if (tmp != NULL) xmlFree(tmp); |
| 6899 | *prefix = NULL; |
| 6900 | return(p); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6901 | } |
| 6902 | if (CUR == ':') { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 6903 | xmlChar *tmp; |
| 6904 | |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 6905 | xmlNsErr(ctxt, XML_NS_ERR_QNAME, |
| 6906 | "Failed to parse QName '%s:%s:'\n", p, l, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 6907 | NEXT; |
| 6908 | tmp = (xmlChar *) xmlParseName(ctxt); |
| 6909 | if (tmp != NULL) { |
| 6910 | tmp = xmlBuildQName(tmp, l, NULL, 0); |
| 6911 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 6912 | if (tmp != NULL) xmlFree(tmp); |
| 6913 | *prefix = p; |
| 6914 | return(l); |
| 6915 | } |
| 6916 | tmp = xmlBuildQName(BAD_CAST "", l, NULL, 0); |
| 6917 | l = xmlDictLookup(ctxt->dict, tmp, -1); |
| 6918 | if (tmp != NULL) xmlFree(tmp); |
| 6919 | *prefix = p; |
| 6920 | return(l); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6921 | } |
| 6922 | *prefix = p; |
| 6923 | } else |
| 6924 | *prefix = NULL; |
| 6925 | return(l); |
| 6926 | } |
| 6927 | |
| 6928 | /** |
| 6929 | * xmlParseQNameAndCompare: |
| 6930 | * @ctxt: an XML parser context |
| 6931 | * @name: the localname |
| 6932 | * @prefix: the prefix, if any. |
| 6933 | * |
| 6934 | * parse an XML name and compares for match |
| 6935 | * (specialized for endtag parsing) |
| 6936 | * |
| 6937 | * Returns NULL for an illegal name, (xmlChar*) 1 for success |
| 6938 | * and the name for mismatch |
| 6939 | */ |
| 6940 | |
| 6941 | static const xmlChar * |
| 6942 | xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name, |
| 6943 | xmlChar const *prefix) { |
| 6944 | const xmlChar *cmp = name; |
| 6945 | const xmlChar *in; |
| 6946 | const xmlChar *ret; |
| 6947 | const xmlChar *prefix2; |
| 6948 | |
| 6949 | if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name)); |
| 6950 | |
| 6951 | GROW; |
| 6952 | in = ctxt->input->cur; |
| 6953 | |
| 6954 | cmp = prefix; |
| 6955 | while (*in != 0 && *in == *cmp) { |
| 6956 | ++in; |
| 6957 | ++cmp; |
| 6958 | } |
| 6959 | if ((*cmp == 0) && (*in == ':')) { |
| 6960 | in++; |
| 6961 | cmp = name; |
| 6962 | while (*in != 0 && *in == *cmp) { |
| 6963 | ++in; |
| 6964 | ++cmp; |
| 6965 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 6966 | if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6967 | /* success */ |
| 6968 | ctxt->input->cur = in; |
| 6969 | return((const xmlChar*) 1); |
| 6970 | } |
| 6971 | } |
| 6972 | /* |
| 6973 | * all strings coms from the dictionary, equality can be done directly |
| 6974 | */ |
| 6975 | ret = xmlParseQName (ctxt, &prefix2); |
| 6976 | if ((ret == name) && (prefix == prefix2)) |
| 6977 | return((const xmlChar*) 1); |
| 6978 | return ret; |
| 6979 | } |
| 6980 | |
| 6981 | /** |
| 6982 | * xmlParseAttValueInternal: |
| 6983 | * @ctxt: an XML parser context |
| 6984 | * @len: attribute len result |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 6985 | * @alloc: whether the attribute was reallocated as a new string |
| 6986 | * @normalize: if 1 then further non-CDATA normalization must be done |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 6987 | * |
| 6988 | * parse a value for an attribute. |
| 6989 | * NOTE: if no normalization is needed, the routine will return pointers |
| 6990 | * directly from the data buffer. |
| 6991 | * |
| 6992 | * 3.3.3 Attribute-Value Normalization: |
| 6993 | * Before the value of an attribute is passed to the application or |
| 6994 | * checked for validity, the XML processor must normalize it as follows: |
| 6995 | * - a character reference is processed by appending the referenced |
| 6996 | * character to the attribute value |
| 6997 | * - an entity reference is processed by recursively processing the |
| 6998 | * replacement text of the entity |
| 6999 | * - a whitespace character (#x20, #xD, #xA, #x9) is processed by |
| 7000 | * appending #x20 to the normalized value, except that only a single |
| 7001 | * #x20 is appended for a "#xD#xA" sequence that is part of an external |
| 7002 | * parsed entity or the literal entity value of an internal parsed entity |
| 7003 | * - other characters are processed by appending them to the normalized value |
| 7004 | * If the declared value is not CDATA, then the XML processor must further |
| 7005 | * process the normalized attribute value by discarding any leading and |
| 7006 | * trailing space (#x20) characters, and by replacing sequences of space |
| 7007 | * (#x20) characters by a single space (#x20) character. |
| 7008 | * All attributes for which no declaration has been read should be treated |
| 7009 | * by a non-validating parser as if declared CDATA. |
| 7010 | * |
| 7011 | * Returns the AttValue parsed or NULL. The value has to be freed by the |
| 7012 | * caller if it was copied, this can be detected by val[*len] == 0. |
| 7013 | */ |
| 7014 | |
| 7015 | static xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7016 | xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc, |
| 7017 | int normalize) |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7018 | { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7019 | xmlChar limit = 0; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7020 | const xmlChar *in = NULL, *start, *end, *last; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7021 | xmlChar *ret = NULL; |
| 7022 | |
| 7023 | GROW; |
| 7024 | in = (xmlChar *) CUR_PTR; |
| 7025 | if (*in != '"' && *in != '\'') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7026 | xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7027 | return (NULL); |
| 7028 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7029 | ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7030 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7031 | /* |
| 7032 | * try to handle in this routine the most common case where no |
| 7033 | * allocation of a new string is required and where content is |
| 7034 | * pure ASCII. |
| 7035 | */ |
| 7036 | limit = *in++; |
| 7037 | end = ctxt->input->end; |
| 7038 | start = in; |
| 7039 | if (in >= end) { |
| 7040 | const xmlChar *oldbase = ctxt->input->base; |
| 7041 | GROW; |
| 7042 | if (oldbase != ctxt->input->base) { |
| 7043 | long delta = ctxt->input->base - oldbase; |
| 7044 | start = start + delta; |
| 7045 | in = in + delta; |
| 7046 | } |
| 7047 | end = ctxt->input->end; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7048 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7049 | if (normalize) { |
| 7050 | /* |
| 7051 | * Skip any leading spaces |
| 7052 | */ |
| 7053 | while ((in < end) && (*in != limit) && |
| 7054 | ((*in == 0x20) || (*in == 0x9) || |
| 7055 | (*in == 0xA) || (*in == 0xD))) { |
| 7056 | in++; |
| 7057 | start = in; |
| 7058 | if (in >= end) { |
| 7059 | const xmlChar *oldbase = ctxt->input->base; |
| 7060 | GROW; |
| 7061 | if (oldbase != ctxt->input->base) { |
| 7062 | long delta = ctxt->input->base - oldbase; |
| 7063 | start = start + delta; |
| 7064 | in = in + delta; |
| 7065 | } |
| 7066 | end = ctxt->input->end; |
| 7067 | } |
| 7068 | } |
| 7069 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 7070 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 7071 | if ((*in++ == 0x20) && (*in == 0x20)) break; |
| 7072 | if (in >= end) { |
| 7073 | const xmlChar *oldbase = ctxt->input->base; |
| 7074 | GROW; |
| 7075 | if (oldbase != ctxt->input->base) { |
| 7076 | long delta = ctxt->input->base - oldbase; |
| 7077 | start = start + delta; |
| 7078 | in = in + delta; |
| 7079 | } |
| 7080 | end = ctxt->input->end; |
| 7081 | } |
| 7082 | } |
| 7083 | last = in; |
| 7084 | /* |
| 7085 | * skip the trailing blanks |
| 7086 | */ |
Daniel Veillard | c6e20e4 | 2003-09-11 16:30:26 +0000 | [diff] [blame] | 7087 | while ((last[-1] == 0x20) && (last > start)) last--; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7088 | while ((in < end) && (*in != limit) && |
| 7089 | ((*in == 0x20) || (*in == 0x9) || |
| 7090 | (*in == 0xA) || (*in == 0xD))) { |
| 7091 | in++; |
| 7092 | if (in >= end) { |
| 7093 | const xmlChar *oldbase = ctxt->input->base; |
| 7094 | GROW; |
| 7095 | if (oldbase != ctxt->input->base) { |
| 7096 | long delta = ctxt->input->base - oldbase; |
| 7097 | start = start + delta; |
| 7098 | in = in + delta; |
| 7099 | last = last + delta; |
| 7100 | } |
| 7101 | end = ctxt->input->end; |
| 7102 | } |
| 7103 | } |
| 7104 | if (*in != limit) goto need_complex; |
| 7105 | } else { |
| 7106 | while ((in < end) && (*in != limit) && (*in >= 0x20) && |
| 7107 | (*in <= 0x7f) && (*in != '&') && (*in != '<')) { |
| 7108 | in++; |
| 7109 | if (in >= end) { |
| 7110 | const xmlChar *oldbase = ctxt->input->base; |
| 7111 | GROW; |
| 7112 | if (oldbase != ctxt->input->base) { |
| 7113 | long delta = ctxt->input->base - oldbase; |
| 7114 | start = start + delta; |
| 7115 | in = in + delta; |
| 7116 | } |
| 7117 | end = ctxt->input->end; |
| 7118 | } |
| 7119 | } |
| 7120 | last = in; |
| 7121 | if (*in != limit) goto need_complex; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7122 | } |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7123 | in++; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7124 | if (len != NULL) { |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7125 | *len = last - start; |
| 7126 | ret = (xmlChar *) start; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7127 | } else { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7128 | if (alloc) *alloc = 1; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7129 | ret = xmlStrndup(start, last - start); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7130 | } |
| 7131 | CUR_PTR = in; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7132 | if (alloc) *alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7133 | return ret; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7134 | need_complex: |
| 7135 | if (alloc) *alloc = 1; |
| 7136 | return xmlParseAttValueComplex(ctxt, len, normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7137 | } |
| 7138 | |
| 7139 | /** |
| 7140 | * xmlParseAttribute2: |
| 7141 | * @ctxt: an XML parser context |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7142 | * @pref: the element prefix |
| 7143 | * @elem: the element name |
| 7144 | * @prefix: a xmlChar ** used to store the value of the attribute prefix |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7145 | * @value: a xmlChar ** used to store the value of the attribute |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7146 | * @len: an int * to save the length of the attribute |
| 7147 | * @alloc: an int * to indicate if the attribute was allocated |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7148 | * |
| 7149 | * parse an attribute in the new SAX2 framework. |
| 7150 | * |
| 7151 | * Returns the attribute name, and the value in *value, . |
| 7152 | */ |
| 7153 | |
| 7154 | static const xmlChar * |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7155 | xmlParseAttribute2(xmlParserCtxtPtr ctxt, |
| 7156 | const xmlChar *pref, const xmlChar *elem, |
| 7157 | const xmlChar **prefix, xmlChar **value, |
| 7158 | int *len, int *alloc) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7159 | const xmlChar *name; |
| 7160 | xmlChar *val; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7161 | int normalize = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7162 | |
| 7163 | *value = NULL; |
| 7164 | GROW; |
| 7165 | name = xmlParseQName(ctxt, prefix); |
| 7166 | if (name == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7167 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7168 | "error parsing attribute name\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7169 | return(NULL); |
| 7170 | } |
| 7171 | |
| 7172 | /* |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7173 | * get the type if needed |
| 7174 | */ |
| 7175 | if (ctxt->attsSpecial != NULL) { |
| 7176 | int type; |
| 7177 | |
| 7178 | type = (int) (long) xmlHashQLookup2(ctxt->attsSpecial, |
| 7179 | pref, elem, *prefix, name); |
| 7180 | if (type != 0) normalize = 1; |
| 7181 | } |
| 7182 | |
| 7183 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7184 | * read the value |
| 7185 | */ |
| 7186 | SKIP_BLANKS; |
| 7187 | if (RAW == '=') { |
| 7188 | NEXT; |
| 7189 | SKIP_BLANKS; |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7190 | val = xmlParseAttValueInternal(ctxt, len, alloc, normalize); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7191 | ctxt->instate = XML_PARSER_CONTENT; |
| 7192 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 7193 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7194 | "Specification mandate value for attribute %s\n", name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7195 | return(NULL); |
| 7196 | } |
| 7197 | |
| 7198 | /* |
| 7199 | * Check that xml:lang conforms to the specification |
| 7200 | * No more registered as an error, just generate a warning now |
| 7201 | * since this was deprecated in XML second edition |
| 7202 | */ |
| 7203 | if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) { |
| 7204 | if (!xmlCheckLanguageID(val)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 7205 | xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE, |
| 7206 | "Malformed value for xml:lang : %s\n", |
| 7207 | val, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7208 | } |
| 7209 | } |
| 7210 | |
| 7211 | /* |
| 7212 | * Check that xml:space conforms to the specification |
| 7213 | */ |
| 7214 | if (xmlStrEqual(name, BAD_CAST "xml:space")) { |
| 7215 | if (xmlStrEqual(val, BAD_CAST "default")) |
| 7216 | *(ctxt->space) = 0; |
| 7217 | else if (xmlStrEqual(val, BAD_CAST "preserve")) |
| 7218 | *(ctxt->space) = 1; |
| 7219 | else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 7220 | xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7221 | "Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n", |
| 7222 | val); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7223 | } |
| 7224 | } |
| 7225 | |
| 7226 | *value = val; |
| 7227 | return(name); |
| 7228 | } |
| 7229 | |
| 7230 | /** |
| 7231 | * xmlParseStartTag2: |
| 7232 | * @ctxt: an XML parser context |
| 7233 | * |
| 7234 | * parse a start of tag either for rule element or |
| 7235 | * EmptyElement. In both case we don't parse the tag closing chars. |
| 7236 | * This routine is called when running SAX2 parsing |
| 7237 | * |
| 7238 | * [40] STag ::= '<' Name (S Attribute)* S? '>' |
| 7239 | * |
| 7240 | * [ WFC: Unique Att Spec ] |
| 7241 | * No attribute name may appear more than once in the same start-tag or |
| 7242 | * empty-element tag. |
| 7243 | * |
| 7244 | * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>' |
| 7245 | * |
| 7246 | * [ WFC: Unique Att Spec ] |
| 7247 | * No attribute name may appear more than once in the same start-tag or |
| 7248 | * empty-element tag. |
| 7249 | * |
| 7250 | * With namespace: |
| 7251 | * |
| 7252 | * [NS 8] STag ::= '<' QName (S Attribute)* S? '>' |
| 7253 | * |
| 7254 | * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>' |
| 7255 | * |
| 7256 | * Returns the element name parsed |
| 7257 | */ |
| 7258 | |
| 7259 | static const xmlChar * |
| 7260 | xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 7261 | const xmlChar **URI, int *tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7262 | const xmlChar *localname; |
| 7263 | const xmlChar *prefix; |
| 7264 | const xmlChar *attname; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7265 | const xmlChar *aprefix; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7266 | const xmlChar *nsname; |
| 7267 | xmlChar *attvalue; |
| 7268 | const xmlChar **atts = ctxt->atts; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7269 | int maxatts = ctxt->maxatts; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7270 | int nratts, nbatts, nbdef; |
| 7271 | int i, j, nbNs, attval; |
| 7272 | const xmlChar *base; |
| 7273 | unsigned long cur; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7274 | |
| 7275 | if (RAW != '<') return(NULL); |
| 7276 | NEXT1; |
| 7277 | |
| 7278 | /* |
| 7279 | * NOTE: it is crucial with the SAX2 API to never call SHRINK beyond that |
| 7280 | * point since the attribute values may be stored as pointers to |
| 7281 | * the buffer and calling SHRINK would destroy them ! |
| 7282 | * The Shrinking is only possible once the full set of attribute |
| 7283 | * callbacks have been done. |
| 7284 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7285 | reparse: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7286 | SHRINK; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7287 | base = ctxt->input->base; |
| 7288 | cur = ctxt->input->cur - ctxt->input->base; |
| 7289 | nbatts = 0; |
| 7290 | nratts = 0; |
| 7291 | nbdef = 0; |
| 7292 | nbNs = 0; |
| 7293 | attval = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7294 | |
| 7295 | localname = xmlParseQName(ctxt, &prefix); |
| 7296 | if (localname == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7297 | xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED, |
| 7298 | "StartTag: invalid element name\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7299 | return(NULL); |
| 7300 | } |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 7301 | *tlen = ctxt->input->cur - ctxt->input->base - cur; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7302 | |
| 7303 | /* |
| 7304 | * Now parse the attributes, it ends up with the ending |
| 7305 | * |
| 7306 | * (S Attribute)* S? |
| 7307 | */ |
| 7308 | SKIP_BLANKS; |
| 7309 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7310 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7311 | |
| 7312 | while ((RAW != '>') && |
| 7313 | ((RAW != '/') || (NXT(1) != '>')) && |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 7314 | (IS_BYTE_CHAR(RAW))) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7315 | const xmlChar *q = CUR_PTR; |
| 7316 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7317 | int len = -1, alloc = 0; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7318 | |
Daniel Veillard | 8e36e6a | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7319 | attname = xmlParseAttribute2(ctxt, prefix, localname, |
| 7320 | &aprefix, &attvalue, &len, &alloc); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7321 | if ((attname != NULL) && (attvalue != NULL)) { |
| 7322 | if (len < 0) len = xmlStrlen(attvalue); |
| 7323 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7324 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 7325 | xmlURIPtr uri; |
| 7326 | |
| 7327 | if (*URL != 0) { |
| 7328 | uri = xmlParseURI((const char *) URL); |
| 7329 | if (uri == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 7330 | xmlWarningMsg(ctxt, XML_WAR_NS_URI, |
| 7331 | "xmlns: %s not a valid URI\n", |
| 7332 | URL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7333 | } else { |
| 7334 | if (uri->scheme == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 7335 | xmlWarningMsg(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 7336 | "xmlns: URI %s is not absolute\n", |
| 7337 | URL, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7338 | } |
| 7339 | xmlFreeURI(uri); |
| 7340 | } |
| 7341 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7342 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7343 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7344 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7345 | for (j = 1;j <= nbNs;j++) |
| 7346 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 7347 | break; |
| 7348 | if (j <= nbNs) |
| 7349 | xmlErrAttributeDup(ctxt, NULL, attname); |
| 7350 | else |
| 7351 | if (nsPush(ctxt, NULL, URL) > 0) nbNs++; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7352 | if (alloc != 0) xmlFree(attvalue); |
| 7353 | SKIP_BLANKS; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7354 | continue; |
| 7355 | } |
| 7356 | if (aprefix == ctxt->str_xmlns) { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7357 | const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len); |
| 7358 | xmlURIPtr uri; |
| 7359 | |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7360 | if (attname == ctxt->str_xml) { |
| 7361 | if (URL != ctxt->str_xml_ns) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7362 | xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE, |
| 7363 | "xml namespace prefix mapped to wrong URI\n", |
| 7364 | NULL, NULL, NULL); |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7365 | } |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7366 | /* |
| 7367 | * Do not keep a namespace definition node |
| 7368 | */ |
| 7369 | if (alloc != 0) xmlFree(attvalue); |
| 7370 | SKIP_BLANKS; |
| 7371 | continue; |
| 7372 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7373 | uri = xmlParseURI((const char *) URL); |
| 7374 | if (uri == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 7375 | xmlWarningMsg(ctxt, XML_WAR_NS_URI, |
| 7376 | "xmlns:%s: '%s' is not a valid URI\n", |
| 7377 | attname, URL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7378 | } else { |
Daniel Veillard | 3b7840c | 2003-09-11 23:42:01 +0000 | [diff] [blame] | 7379 | if ((ctxt->pedantic) && (uri->scheme == NULL)) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 7380 | xmlWarningMsg(ctxt, XML_WAR_NS_URI_RELATIVE, |
| 7381 | "xmlns:%s: URI %s is not absolute\n", |
| 7382 | attname, URL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7383 | } |
| 7384 | xmlFreeURI(uri); |
| 7385 | } |
| 7386 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7387 | /* |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7388 | * check that it's not a defined namespace |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7389 | */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7390 | for (j = 1;j <= nbNs;j++) |
| 7391 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 7392 | break; |
| 7393 | if (j <= nbNs) |
| 7394 | xmlErrAttributeDup(ctxt, aprefix, attname); |
| 7395 | else |
| 7396 | if (nsPush(ctxt, attname, URL) > 0) nbNs++; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7397 | if (alloc != 0) xmlFree(attvalue); |
| 7398 | SKIP_BLANKS; |
Daniel Veillard | d3999c7 | 2004-03-10 16:27:03 +0000 | [diff] [blame] | 7399 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7400 | continue; |
| 7401 | } |
| 7402 | |
| 7403 | /* |
| 7404 | * Add the pair to atts |
| 7405 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7406 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 7407 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7408 | if (attvalue[len] == 0) |
| 7409 | xmlFree(attvalue); |
| 7410 | goto failed; |
| 7411 | } |
| 7412 | maxatts = ctxt->maxatts; |
| 7413 | atts = ctxt->atts; |
| 7414 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7415 | ctxt->attallocs[nratts++] = alloc; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7416 | atts[nbatts++] = attname; |
| 7417 | atts[nbatts++] = aprefix; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7418 | atts[nbatts++] = NULL; /* the URI will be fetched later */ |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7419 | atts[nbatts++] = attvalue; |
| 7420 | attvalue += len; |
| 7421 | atts[nbatts++] = attvalue; |
| 7422 | /* |
| 7423 | * tag if some deallocation is needed |
| 7424 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7425 | if (alloc != 0) attval = 1; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7426 | } else { |
| 7427 | if ((attvalue != NULL) && (attvalue[len] == 0)) |
| 7428 | xmlFree(attvalue); |
| 7429 | } |
| 7430 | |
| 7431 | failed: |
| 7432 | |
| 7433 | GROW |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7434 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7435 | if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>')))) |
| 7436 | break; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 7437 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7438 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 7439 | "attributes construct error\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7440 | } |
| 7441 | SKIP_BLANKS; |
| 7442 | if ((cons == ctxt->input->consumed) && (q == CUR_PTR) && |
| 7443 | (attname == NULL) && (attvalue == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7444 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7445 | "xmlParseStartTag: problem parsing attributes\n"); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7446 | break; |
| 7447 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7448 | GROW; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7449 | if (ctxt->input->base != base) goto base_changed; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7450 | } |
| 7451 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7452 | /* |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7453 | * The attributes defaulting |
| 7454 | */ |
| 7455 | if (ctxt->attsDefault != NULL) { |
| 7456 | xmlDefAttrsPtr defaults; |
| 7457 | |
| 7458 | defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix); |
| 7459 | if (defaults != NULL) { |
| 7460 | for (i = 0;i < defaults->nbAttrs;i++) { |
| 7461 | attname = defaults->values[4 * i]; |
| 7462 | aprefix = defaults->values[4 * i + 1]; |
| 7463 | |
| 7464 | /* |
| 7465 | * special work for namespaces defaulted defs |
| 7466 | */ |
| 7467 | if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) { |
| 7468 | /* |
| 7469 | * check that it's not a defined namespace |
| 7470 | */ |
| 7471 | for (j = 1;j <= nbNs;j++) |
| 7472 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL) |
| 7473 | break; |
| 7474 | if (j <= nbNs) continue; |
| 7475 | |
| 7476 | nsname = xmlGetNamespace(ctxt, NULL); |
| 7477 | if (nsname != defaults->values[4 * i + 2]) { |
| 7478 | if (nsPush(ctxt, NULL, |
| 7479 | defaults->values[4 * i + 2]) > 0) |
| 7480 | nbNs++; |
| 7481 | } |
| 7482 | } else if (aprefix == ctxt->str_xmlns) { |
| 7483 | /* |
| 7484 | * check that it's not a defined namespace |
| 7485 | */ |
| 7486 | for (j = 1;j <= nbNs;j++) |
| 7487 | if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname) |
| 7488 | break; |
| 7489 | if (j <= nbNs) continue; |
| 7490 | |
| 7491 | nsname = xmlGetNamespace(ctxt, attname); |
| 7492 | if (nsname != defaults->values[2]) { |
| 7493 | if (nsPush(ctxt, attname, |
| 7494 | defaults->values[4 * i + 2]) > 0) |
| 7495 | nbNs++; |
| 7496 | } |
| 7497 | } else { |
| 7498 | /* |
| 7499 | * check that it's not a defined attribute |
| 7500 | */ |
| 7501 | for (j = 0;j < nbatts;j+=5) { |
| 7502 | if ((attname == atts[j]) && (aprefix == atts[j+1])) |
| 7503 | break; |
| 7504 | } |
| 7505 | if (j < nbatts) continue; |
| 7506 | |
| 7507 | if ((atts == NULL) || (nbatts + 5 > maxatts)) { |
| 7508 | if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) { |
Daniel Veillard | 9ee35f3 | 2003-09-28 00:19:54 +0000 | [diff] [blame] | 7509 | return(NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7510 | } |
| 7511 | maxatts = ctxt->maxatts; |
| 7512 | atts = ctxt->atts; |
| 7513 | } |
| 7514 | atts[nbatts++] = attname; |
| 7515 | atts[nbatts++] = aprefix; |
| 7516 | if (aprefix == NULL) |
| 7517 | atts[nbatts++] = NULL; |
| 7518 | else |
| 7519 | atts[nbatts++] = xmlGetNamespace(ctxt, aprefix); |
| 7520 | atts[nbatts++] = defaults->values[4 * i + 2]; |
| 7521 | atts[nbatts++] = defaults->values[4 * i + 3]; |
| 7522 | nbdef++; |
| 7523 | } |
| 7524 | } |
| 7525 | } |
| 7526 | } |
| 7527 | |
Daniel Veillard | e70c877 | 2003-11-25 07:21:18 +0000 | [diff] [blame] | 7528 | /* |
| 7529 | * The attributes checkings |
| 7530 | */ |
| 7531 | for (i = 0; i < nbatts;i += 5) { |
| 7532 | nsname = xmlGetNamespace(ctxt, atts[i + 1]); |
| 7533 | if ((atts[i + 1] != NULL) && (nsname == NULL)) { |
| 7534 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 7535 | "Namespace prefix %s for %s on %s is not defined\n", |
| 7536 | atts[i + 1], atts[i], localname); |
| 7537 | } |
| 7538 | atts[i + 2] = nsname; |
| 7539 | /* |
| 7540 | * [ WFC: Unique Att Spec ] |
| 7541 | * No attribute name may appear more than once in the same |
| 7542 | * start-tag or empty-element tag. |
| 7543 | * As extended by the Namespace in XML REC. |
| 7544 | */ |
| 7545 | for (j = 0; j < i;j += 5) { |
| 7546 | if (atts[i] == atts[j]) { |
| 7547 | if (atts[i+1] == atts[j+1]) { |
| 7548 | xmlErrAttributeDup(ctxt, atts[i+1], atts[i]); |
| 7549 | break; |
| 7550 | } |
| 7551 | if ((nsname != NULL) && (atts[j + 2] == nsname)) { |
| 7552 | xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED, |
| 7553 | "Namespaced Attribute %s in '%s' redefined\n", |
| 7554 | atts[i], nsname, NULL); |
| 7555 | break; |
| 7556 | } |
| 7557 | } |
| 7558 | } |
| 7559 | } |
| 7560 | |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7561 | nsname = xmlGetNamespace(ctxt, prefix); |
| 7562 | if ((prefix != NULL) && (nsname == NULL)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7563 | xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE, |
| 7564 | "Namespace prefix %s on %s is not defined\n", |
| 7565 | prefix, localname, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7566 | } |
| 7567 | *pref = prefix; |
| 7568 | *URI = nsname; |
| 7569 | |
| 7570 | /* |
| 7571 | * SAX: Start of Element ! |
| 7572 | */ |
| 7573 | if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) && |
| 7574 | (!ctxt->disableSAX)) { |
| 7575 | if (nbNs > 0) |
| 7576 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 7577 | nsname, nbNs, &ctxt->nsTab[ctxt->nsNr - 2 * nbNs], |
| 7578 | nbatts / 5, nbdef, atts); |
| 7579 | else |
| 7580 | ctxt->sax->startElementNs(ctxt->userData, localname, prefix, |
| 7581 | nsname, 0, NULL, nbatts / 5, nbdef, atts); |
| 7582 | } |
| 7583 | |
| 7584 | /* |
| 7585 | * Free up attribute allocated strings if needed |
| 7586 | */ |
| 7587 | if (attval != 0) { |
| 7588 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 7589 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 7590 | xmlFree((xmlChar *) atts[i]); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7591 | } |
| 7592 | |
| 7593 | return(localname); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 7594 | |
| 7595 | base_changed: |
| 7596 | /* |
| 7597 | * the attribute strings are valid iif the base didn't changed |
| 7598 | */ |
| 7599 | if (attval != 0) { |
| 7600 | for (i = 3,j = 0; j < nratts;i += 5,j++) |
| 7601 | if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL)) |
| 7602 | xmlFree((xmlChar *) atts[i]); |
| 7603 | } |
| 7604 | ctxt->input->cur = ctxt->input->base + cur; |
| 7605 | if (ctxt->wellFormed == 1) { |
| 7606 | goto reparse; |
| 7607 | } |
| 7608 | return(NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7609 | } |
| 7610 | |
| 7611 | /** |
| 7612 | * xmlParseEndTag2: |
| 7613 | * @ctxt: an XML parser context |
| 7614 | * @line: line of the start tag |
| 7615 | * @nsNr: number of namespaces on the start tag |
| 7616 | * |
| 7617 | * parse an end of tag |
| 7618 | * |
| 7619 | * [42] ETag ::= '</' Name S? '>' |
| 7620 | * |
| 7621 | * With namespace |
| 7622 | * |
| 7623 | * [NS 9] ETag ::= '</' QName S? '>' |
| 7624 | */ |
| 7625 | |
| 7626 | static void |
| 7627 | xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlChar *prefix, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 7628 | const xmlChar *URI, int line, int nsNr, int tlen) { |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7629 | const xmlChar *name; |
| 7630 | |
| 7631 | GROW; |
| 7632 | if ((RAW != '<') || (NXT(1) != '/')) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 7633 | xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7634 | return; |
| 7635 | } |
| 7636 | SKIP(2); |
| 7637 | |
Daniel Veillard | 453e71b | 2004-04-20 17:44:46 +0000 | [diff] [blame] | 7638 | if ((tlen > 0) && (strncmp(ctxt->input->cur, ctxt->name, tlen) == 0)) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 7639 | if (ctxt->input->cur[tlen] == '>') { |
| 7640 | ctxt->input->cur += tlen + 1; |
| 7641 | goto done; |
| 7642 | } |
| 7643 | ctxt->input->cur += tlen; |
| 7644 | name = (xmlChar*)1; |
| 7645 | } else { |
| 7646 | if (prefix == NULL) |
| 7647 | name = xmlParseNameAndCompare(ctxt, ctxt->name); |
| 7648 | else |
| 7649 | name = xmlParseQNameAndCompare(ctxt, ctxt->name, prefix); |
| 7650 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7651 | |
| 7652 | /* |
| 7653 | * We should definitely be at the ending "S? '>'" part |
| 7654 | */ |
| 7655 | GROW; |
| 7656 | SKIP_BLANKS; |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 7657 | if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7658 | xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7659 | } else |
| 7660 | NEXT1; |
| 7661 | |
| 7662 | /* |
| 7663 | * [ WFC: Element Type Match ] |
| 7664 | * The Name in an element's end-tag must match the element type in the |
| 7665 | * start-tag. |
| 7666 | * |
| 7667 | */ |
| 7668 | if (name != (xmlChar*)1) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 7669 | if (name == NULL) name = BAD_CAST "unparseable"; |
| 7670 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH, |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7671 | "Opening and ending tag mismatch: %s line %d and %s\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 7672 | ctxt->name, line, name); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7673 | } |
| 7674 | |
| 7675 | /* |
| 7676 | * SAX: End of Tag |
| 7677 | */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 7678 | done: |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7679 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 7680 | (!ctxt->disableSAX)) |
| 7681 | ctxt->sax->endElementNs(ctxt->userData, ctxt->name, prefix, URI); |
| 7682 | |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7683 | spacePop(ctxt); |
| 7684 | if (nsNr != 0) |
| 7685 | nsPop(ctxt, nsNr); |
| 7686 | return; |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 7687 | } |
| 7688 | |
| 7689 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7690 | * xmlParseCDSect: |
| 7691 | * @ctxt: an XML parser context |
| 7692 | * |
| 7693 | * Parse escaped pure raw content. |
| 7694 | * |
| 7695 | * [18] CDSect ::= CDStart CData CDEnd |
| 7696 | * |
| 7697 | * [19] CDStart ::= '<![CDATA[' |
| 7698 | * |
| 7699 | * [20] Data ::= (Char* - (Char* ']]>' Char*)) |
| 7700 | * |
| 7701 | * [21] CDEnd ::= ']]>' |
| 7702 | */ |
| 7703 | void |
| 7704 | xmlParseCDSect(xmlParserCtxtPtr ctxt) { |
| 7705 | xmlChar *buf = NULL; |
| 7706 | int len = 0; |
| 7707 | int size = XML_PARSER_BUFFER_SIZE; |
| 7708 | int r, rl; |
| 7709 | int s, sl; |
| 7710 | int cur, l; |
| 7711 | int count = 0; |
| 7712 | |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 7713 | /* Check 2.6.0 was NXT(0) not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 7714 | if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7715 | SKIP(9); |
| 7716 | } else |
| 7717 | return; |
| 7718 | |
| 7719 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
| 7720 | r = CUR_CHAR(rl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 7721 | if (!IS_CHAR(r)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7722 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7723 | ctxt->instate = XML_PARSER_CONTENT; |
| 7724 | return; |
| 7725 | } |
| 7726 | NEXTL(rl); |
| 7727 | s = CUR_CHAR(sl); |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 7728 | if (!IS_CHAR(s)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7729 | xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7730 | ctxt->instate = XML_PARSER_CONTENT; |
| 7731 | return; |
| 7732 | } |
| 7733 | NEXTL(sl); |
| 7734 | cur = CUR_CHAR(l); |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 7735 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7736 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7737 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7738 | return; |
| 7739 | } |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 7740 | while (IS_CHAR(cur) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7741 | ((r != ']') || (s != ']') || (cur != '>'))) { |
| 7742 | if (len + 5 >= size) { |
| 7743 | size *= 2; |
| 7744 | buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 7745 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7746 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7747 | return; |
| 7748 | } |
| 7749 | } |
| 7750 | COPY_BUF(rl,buf,len,r); |
| 7751 | r = s; |
| 7752 | rl = sl; |
| 7753 | s = cur; |
| 7754 | sl = l; |
| 7755 | count++; |
| 7756 | if (count > 50) { |
| 7757 | GROW; |
| 7758 | count = 0; |
| 7759 | } |
| 7760 | NEXTL(l); |
| 7761 | cur = CUR_CHAR(l); |
| 7762 | } |
| 7763 | buf[len] = 0; |
| 7764 | ctxt->instate = XML_PARSER_CONTENT; |
| 7765 | if (cur != '>') { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 7766 | xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7767 | "CData section not finished\n%.50s\n", buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7768 | xmlFree(buf); |
| 7769 | return; |
| 7770 | } |
| 7771 | NEXTL(l); |
| 7772 | |
| 7773 | /* |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 7774 | * OK the buffer is to be consumed as cdata. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7775 | */ |
| 7776 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 7777 | if (ctxt->sax->cdataBlock != NULL) |
| 7778 | ctxt->sax->cdataBlock(ctxt->userData, buf, len); |
Daniel Veillard | 7583a59 | 2001-07-08 13:15:55 +0000 | [diff] [blame] | 7779 | else if (ctxt->sax->characters != NULL) |
| 7780 | ctxt->sax->characters(ctxt->userData, buf, len); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7781 | } |
| 7782 | xmlFree(buf); |
| 7783 | } |
| 7784 | |
| 7785 | /** |
| 7786 | * xmlParseContent: |
| 7787 | * @ctxt: an XML parser context |
| 7788 | * |
| 7789 | * Parse a content: |
| 7790 | * |
| 7791 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 7792 | */ |
| 7793 | |
| 7794 | void |
| 7795 | xmlParseContent(xmlParserCtxtPtr ctxt) { |
| 7796 | GROW; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 7797 | while ((RAW != 0) && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7798 | ((RAW != '<') || (NXT(1) != '/'))) { |
| 7799 | const xmlChar *test = CUR_PTR; |
Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 7800 | unsigned int cons = ctxt->input->consumed; |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 7801 | const xmlChar *cur = ctxt->input->cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7802 | |
| 7803 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7804 | * First case : a Processing Instruction. |
| 7805 | */ |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 7806 | if ((*cur == '<') && (cur[1] == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7807 | xmlParsePI(ctxt); |
| 7808 | } |
| 7809 | |
| 7810 | /* |
| 7811 | * Second case : a CDSection |
| 7812 | */ |
Daniel Veillard | 8f597c3 | 2003-10-06 08:19:27 +0000 | [diff] [blame] | 7813 | /* 2.6.0 test was *cur not RAW */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 7814 | else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7815 | xmlParseCDSect(ctxt); |
| 7816 | } |
| 7817 | |
| 7818 | /* |
| 7819 | * Third case : a comment |
| 7820 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 7821 | else if ((*cur == '<') && (NXT(1) == '!') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7822 | (NXT(2) == '-') && (NXT(3) == '-')) { |
| 7823 | xmlParseComment(ctxt); |
| 7824 | ctxt->instate = XML_PARSER_CONTENT; |
| 7825 | } |
| 7826 | |
| 7827 | /* |
| 7828 | * Fourth case : a sub-element. |
| 7829 | */ |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 7830 | else if (*cur == '<') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7831 | xmlParseElement(ctxt); |
| 7832 | } |
| 7833 | |
| 7834 | /* |
| 7835 | * Fifth case : a reference. If if has not been resolved, |
| 7836 | * parsing returns it's Name, create the node |
| 7837 | */ |
| 7838 | |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 7839 | else if (*cur == '&') { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7840 | xmlParseReference(ctxt); |
| 7841 | } |
| 7842 | |
| 7843 | /* |
| 7844 | * Last case, text. Note that References are handled directly. |
| 7845 | */ |
| 7846 | else { |
| 7847 | xmlParseCharData(ctxt, 0); |
| 7848 | } |
| 7849 | |
| 7850 | GROW; |
| 7851 | /* |
| 7852 | * Pop-up of finished entities. |
| 7853 | */ |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 7854 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7855 | xmlPopInput(ctxt); |
| 7856 | SHRINK; |
| 7857 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 7858 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 7859 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 7860 | "detected an error in element content\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7861 | ctxt->instate = XML_PARSER_EOF; |
| 7862 | break; |
| 7863 | } |
| 7864 | } |
| 7865 | } |
| 7866 | |
| 7867 | /** |
| 7868 | * xmlParseElement: |
| 7869 | * @ctxt: an XML parser context |
| 7870 | * |
| 7871 | * parse an XML element, this is highly recursive |
| 7872 | * |
| 7873 | * [39] element ::= EmptyElemTag | STag content ETag |
| 7874 | * |
| 7875 | * [ WFC: Element Type Match ] |
| 7876 | * The Name in an element's end-tag must match the element type in the |
| 7877 | * start-tag. |
| 7878 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7879 | */ |
| 7880 | |
| 7881 | void |
| 7882 | xmlParseElement(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 2fdbd32 | 2003-08-18 12:15:38 +0000 | [diff] [blame] | 7883 | const xmlChar *name; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7884 | const xmlChar *prefix; |
| 7885 | const xmlChar *URI; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7886 | xmlParserNodeInfo node_info; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 7887 | int line, tlen; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7888 | xmlNodePtr ret; |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7889 | int nsNr = ctxt->nsNr; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7890 | |
| 7891 | /* Capture start position */ |
| 7892 | if (ctxt->record_info) { |
| 7893 | node_info.begin_pos = ctxt->input->consumed + |
| 7894 | (CUR_PTR - ctxt->input->base); |
| 7895 | node_info.begin_line = ctxt->input->line; |
| 7896 | } |
| 7897 | |
| 7898 | if (ctxt->spaceNr == 0) |
| 7899 | spacePush(ctxt, -1); |
| 7900 | else |
| 7901 | spacePush(ctxt, *ctxt->space); |
| 7902 | |
Daniel Veillard | 6c5b2d3 | 2003-03-27 14:55:52 +0000 | [diff] [blame] | 7903 | line = ctxt->input->line; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 7904 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7905 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 7906 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 7907 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 7908 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7909 | else |
| 7910 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 7911 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7912 | if (name == NULL) { |
| 7913 | spacePop(ctxt); |
| 7914 | return; |
| 7915 | } |
| 7916 | namePush(ctxt, name); |
| 7917 | ret = ctxt->node; |
| 7918 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 7919 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7920 | /* |
| 7921 | * [ VC: Root Element Type ] |
| 7922 | * The Name in the document type declaration must match the element |
| 7923 | * type of the root element. |
| 7924 | */ |
| 7925 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 7926 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 7927 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 7928 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7929 | |
| 7930 | /* |
| 7931 | * Check for an Empty Element. |
| 7932 | */ |
| 7933 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 7934 | SKIP(2); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7935 | if (ctxt->sax2) { |
| 7936 | if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) && |
| 7937 | (!ctxt->disableSAX)) |
| 7938 | ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 7939 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7940 | } else { |
| 7941 | if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) && |
| 7942 | (!ctxt->disableSAX)) |
| 7943 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 7944 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7945 | } |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7946 | namePop(ctxt); |
| 7947 | spacePop(ctxt); |
| 7948 | if (nsNr != ctxt->nsNr) |
| 7949 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7950 | if ( ret != NULL && ctxt->record_info ) { |
| 7951 | node_info.end_pos = ctxt->input->consumed + |
| 7952 | (CUR_PTR - ctxt->input->base); |
| 7953 | node_info.end_line = ctxt->input->line; |
| 7954 | node_info.node = ret; |
| 7955 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 7956 | } |
| 7957 | return; |
| 7958 | } |
| 7959 | if (RAW == '>') { |
Daniel Veillard | 21a0f91 | 2001-02-25 19:54:14 +0000 | [diff] [blame] | 7960 | NEXT1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7961 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 7962 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED, |
| 7963 | "Couldn't find end of Start Tag %s line %d\n", |
| 7964 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7965 | |
| 7966 | /* |
| 7967 | * end of parsing of this node. |
| 7968 | */ |
| 7969 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7970 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7971 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 7972 | if (nsNr != ctxt->nsNr) |
| 7973 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7974 | |
| 7975 | /* |
| 7976 | * Capture end position and add node |
| 7977 | */ |
| 7978 | if ( ret != NULL && ctxt->record_info ) { |
| 7979 | node_info.end_pos = ctxt->input->consumed + |
| 7980 | (CUR_PTR - ctxt->input->base); |
| 7981 | node_info.end_line = ctxt->input->line; |
| 7982 | node_info.node = ret; |
| 7983 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 7984 | } |
| 7985 | return; |
| 7986 | } |
| 7987 | |
| 7988 | /* |
| 7989 | * Parse the content of the element: |
| 7990 | */ |
| 7991 | xmlParseContent(ctxt); |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 7992 | if (!IS_BYTE_CHAR(RAW)) { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 7993 | xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED, |
Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 7994 | "Premature end of data in tag %s line %d\n", |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 7995 | name, line, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7996 | |
| 7997 | /* |
| 7998 | * end of parsing of this node. |
| 7999 | */ |
| 8000 | nodePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8001 | namePop(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8002 | spacePop(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8003 | if (nsNr != ctxt->nsNr) |
| 8004 | nsPop(ctxt, ctxt->nsNr - nsNr); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8005 | return; |
| 8006 | } |
| 8007 | |
| 8008 | /* |
| 8009 | * parse the end of tag: '</' should be here. |
| 8010 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8011 | if (ctxt->sax2) { |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 8012 | xmlParseEndTag2(ctxt, prefix, URI, line, ctxt->nsNr - nsNr, tlen); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8013 | namePop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8014 | } |
| 8015 | #ifdef LIBXML_SAX1_ENABLED |
| 8016 | else |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8017 | xmlParseEndTag1(ctxt, line); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 8018 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8019 | |
| 8020 | /* |
| 8021 | * Capture end position and add node |
| 8022 | */ |
| 8023 | if ( ret != NULL && ctxt->record_info ) { |
| 8024 | node_info.end_pos = ctxt->input->consumed + |
| 8025 | (CUR_PTR - ctxt->input->base); |
| 8026 | node_info.end_line = ctxt->input->line; |
| 8027 | node_info.node = ret; |
| 8028 | xmlParserAddNodeInfo(ctxt, &node_info); |
| 8029 | } |
| 8030 | } |
| 8031 | |
| 8032 | /** |
| 8033 | * xmlParseVersionNum: |
| 8034 | * @ctxt: an XML parser context |
| 8035 | * |
| 8036 | * parse the XML version value. |
| 8037 | * |
| 8038 | * [26] VersionNum ::= ([a-zA-Z0-9_.:] | '-')+ |
| 8039 | * |
| 8040 | * Returns the string giving the XML version number, or NULL |
| 8041 | */ |
| 8042 | xmlChar * |
| 8043 | xmlParseVersionNum(xmlParserCtxtPtr ctxt) { |
| 8044 | xmlChar *buf = NULL; |
| 8045 | int len = 0; |
| 8046 | int size = 10; |
| 8047 | xmlChar cur; |
| 8048 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8049 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8050 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8051 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8052 | return(NULL); |
| 8053 | } |
| 8054 | cur = CUR; |
| 8055 | while (((cur >= 'a') && (cur <= 'z')) || |
| 8056 | ((cur >= 'A') && (cur <= 'Z')) || |
| 8057 | ((cur >= '0') && (cur <= '9')) || |
| 8058 | (cur == '_') || (cur == '.') || |
| 8059 | (cur == ':') || (cur == '-')) { |
| 8060 | if (len + 1 >= size) { |
| 8061 | size *= 2; |
| 8062 | buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 8063 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8064 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8065 | return(NULL); |
| 8066 | } |
| 8067 | } |
| 8068 | buf[len++] = cur; |
| 8069 | NEXT; |
| 8070 | cur=CUR; |
| 8071 | } |
| 8072 | buf[len] = 0; |
| 8073 | return(buf); |
| 8074 | } |
| 8075 | |
| 8076 | /** |
| 8077 | * xmlParseVersionInfo: |
| 8078 | * @ctxt: an XML parser context |
| 8079 | * |
| 8080 | * parse the XML version. |
| 8081 | * |
| 8082 | * [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ") |
| 8083 | * |
| 8084 | * [25] Eq ::= S? '=' S? |
| 8085 | * |
| 8086 | * Returns the version string, e.g. "1.0" |
| 8087 | */ |
| 8088 | |
| 8089 | xmlChar * |
| 8090 | xmlParseVersionInfo(xmlParserCtxtPtr ctxt) { |
| 8091 | xmlChar *version = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8092 | |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 8093 | if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8094 | SKIP(7); |
| 8095 | SKIP_BLANKS; |
| 8096 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8097 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8098 | return(NULL); |
| 8099 | } |
| 8100 | NEXT; |
| 8101 | SKIP_BLANKS; |
| 8102 | if (RAW == '"') { |
| 8103 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8104 | version = xmlParseVersionNum(ctxt); |
| 8105 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8106 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8107 | } else |
| 8108 | NEXT; |
| 8109 | } else if (RAW == '\''){ |
| 8110 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8111 | version = xmlParseVersionNum(ctxt); |
| 8112 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8113 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8114 | } else |
| 8115 | NEXT; |
| 8116 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8117 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8118 | } |
| 8119 | } |
| 8120 | return(version); |
| 8121 | } |
| 8122 | |
| 8123 | /** |
| 8124 | * xmlParseEncName: |
| 8125 | * @ctxt: an XML parser context |
| 8126 | * |
| 8127 | * parse the XML encoding name |
| 8128 | * |
| 8129 | * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')* |
| 8130 | * |
| 8131 | * Returns the encoding name value or NULL |
| 8132 | */ |
| 8133 | xmlChar * |
| 8134 | xmlParseEncName(xmlParserCtxtPtr ctxt) { |
| 8135 | xmlChar *buf = NULL; |
| 8136 | int len = 0; |
| 8137 | int size = 10; |
| 8138 | xmlChar cur; |
| 8139 | |
| 8140 | cur = CUR; |
| 8141 | if (((cur >= 'a') && (cur <= 'z')) || |
| 8142 | ((cur >= 'A') && (cur <= 'Z'))) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 8143 | buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8144 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8145 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8146 | return(NULL); |
| 8147 | } |
| 8148 | |
| 8149 | buf[len++] = cur; |
| 8150 | NEXT; |
| 8151 | cur = CUR; |
| 8152 | while (((cur >= 'a') && (cur <= 'z')) || |
| 8153 | ((cur >= 'A') && (cur <= 'Z')) || |
| 8154 | ((cur >= '0') && (cur <= '9')) || |
| 8155 | (cur == '.') || (cur == '_') || |
| 8156 | (cur == '-')) { |
| 8157 | if (len + 1 >= size) { |
| 8158 | size *= 2; |
| 8159 | buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar)); |
| 8160 | if (buf == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8161 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8162 | return(NULL); |
| 8163 | } |
| 8164 | } |
| 8165 | buf[len++] = cur; |
| 8166 | NEXT; |
| 8167 | cur = CUR; |
| 8168 | if (cur == 0) { |
| 8169 | SHRINK; |
| 8170 | GROW; |
| 8171 | cur = CUR; |
| 8172 | } |
| 8173 | } |
| 8174 | buf[len] = 0; |
| 8175 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8176 | xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8177 | } |
| 8178 | return(buf); |
| 8179 | } |
| 8180 | |
| 8181 | /** |
| 8182 | * xmlParseEncodingDecl: |
| 8183 | * @ctxt: an XML parser context |
| 8184 | * |
| 8185 | * parse the XML encoding declaration |
| 8186 | * |
| 8187 | * [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'") |
| 8188 | * |
| 8189 | * this setups the conversion filters. |
| 8190 | * |
| 8191 | * Returns the encoding value or NULL |
| 8192 | */ |
| 8193 | |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 8194 | const xmlChar * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8195 | xmlParseEncodingDecl(xmlParserCtxtPtr ctxt) { |
| 8196 | xmlChar *encoding = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8197 | |
| 8198 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 8199 | if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8200 | SKIP(8); |
| 8201 | SKIP_BLANKS; |
| 8202 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8203 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8204 | return(NULL); |
| 8205 | } |
| 8206 | NEXT; |
| 8207 | SKIP_BLANKS; |
| 8208 | if (RAW == '"') { |
| 8209 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8210 | encoding = xmlParseEncName(ctxt); |
| 8211 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8212 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8213 | } else |
| 8214 | NEXT; |
| 8215 | } else if (RAW == '\''){ |
| 8216 | NEXT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8217 | encoding = xmlParseEncName(ctxt); |
| 8218 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8219 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8220 | } else |
| 8221 | NEXT; |
Daniel Veillard | 82ac6b0 | 2002-02-17 23:18:55 +0000 | [diff] [blame] | 8222 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8223 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8224 | } |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 8225 | /* |
| 8226 | * UTF-16 encoding stwich has already taken place at this stage, |
| 8227 | * more over the little-endian/big-endian selection is already done |
| 8228 | */ |
| 8229 | if ((encoding != NULL) && |
| 8230 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-16")) || |
| 8231 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF16")))) { |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 8232 | if (ctxt->encoding != NULL) |
| 8233 | xmlFree((xmlChar *) ctxt->encoding); |
| 8234 | ctxt->encoding = encoding; |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 8235 | } |
| 8236 | /* |
| 8237 | * UTF-8 encoding is handled natively |
| 8238 | */ |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 8239 | else if ((encoding != NULL) && |
Daniel Veillard | b19ba83 | 2003-08-14 00:33:46 +0000 | [diff] [blame] | 8240 | ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-8")) || |
| 8241 | (!xmlStrcasecmp(encoding, BAD_CAST "UTF8")))) { |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 8242 | if (ctxt->encoding != NULL) |
| 8243 | xmlFree((xmlChar *) ctxt->encoding); |
| 8244 | ctxt->encoding = encoding; |
Daniel Veillard | 6b621b8 | 2003-08-11 15:03:34 +0000 | [diff] [blame] | 8245 | } |
Daniel Veillard | ab1ae3a | 2003-08-14 12:19:54 +0000 | [diff] [blame] | 8246 | else if (encoding != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8247 | xmlCharEncodingHandlerPtr handler; |
| 8248 | |
| 8249 | if (ctxt->input->encoding != NULL) |
| 8250 | xmlFree((xmlChar *) ctxt->input->encoding); |
| 8251 | ctxt->input->encoding = encoding; |
| 8252 | |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 8253 | handler = xmlFindCharEncodingHandler((const char *) encoding); |
| 8254 | if (handler != NULL) { |
| 8255 | xmlSwitchToEncoding(ctxt, handler); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8256 | } else { |
Daniel Veillard | f403d29 | 2003-10-05 13:51:35 +0000 | [diff] [blame] | 8257 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
Daniel Veillard | a6874ca | 2003-07-29 16:47:24 +0000 | [diff] [blame] | 8258 | "Unsupported encoding %s\n", encoding); |
| 8259 | return(NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8260 | } |
| 8261 | } |
| 8262 | } |
| 8263 | return(encoding); |
| 8264 | } |
| 8265 | |
| 8266 | /** |
| 8267 | * xmlParseSDDecl: |
| 8268 | * @ctxt: an XML parser context |
| 8269 | * |
| 8270 | * parse the XML standalone declaration |
| 8271 | * |
| 8272 | * [32] SDDecl ::= S 'standalone' Eq |
| 8273 | * (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"')) |
| 8274 | * |
| 8275 | * [ VC: Standalone Document Declaration ] |
| 8276 | * TODO The standalone document declaration must have the value "no" |
| 8277 | * if any external markup declarations contain declarations of: |
| 8278 | * - attributes with default values, if elements to which these |
| 8279 | * attributes apply appear in the document without specifications |
| 8280 | * of values for these attributes, or |
| 8281 | * - entities (other than amp, lt, gt, apos, quot), if references |
| 8282 | * to those entities appear in the document, or |
| 8283 | * - attributes with values subject to normalization, where the |
| 8284 | * attribute appears in the document with a value which will change |
| 8285 | * as a result of normalization, or |
| 8286 | * - element types with element content, if white space occurs directly |
| 8287 | * within any instance of those types. |
| 8288 | * |
| 8289 | * Returns 1 if standalone, 0 otherwise |
| 8290 | */ |
| 8291 | |
| 8292 | int |
| 8293 | xmlParseSDDecl(xmlParserCtxtPtr ctxt) { |
| 8294 | int standalone = -1; |
| 8295 | |
| 8296 | SKIP_BLANKS; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 8297 | 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] | 8298 | SKIP(10); |
| 8299 | SKIP_BLANKS; |
| 8300 | if (RAW != '=') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8301 | xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8302 | return(standalone); |
| 8303 | } |
| 8304 | NEXT; |
| 8305 | SKIP_BLANKS; |
| 8306 | if (RAW == '\''){ |
| 8307 | NEXT; |
| 8308 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 8309 | standalone = 0; |
| 8310 | SKIP(2); |
| 8311 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 8312 | (NXT(2) == 's')) { |
| 8313 | standalone = 1; |
| 8314 | SKIP(3); |
| 8315 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8316 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8317 | } |
| 8318 | if (RAW != '\'') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8319 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8320 | } else |
| 8321 | NEXT; |
| 8322 | } else if (RAW == '"'){ |
| 8323 | NEXT; |
| 8324 | if ((RAW == 'n') && (NXT(1) == 'o')) { |
| 8325 | standalone = 0; |
| 8326 | SKIP(2); |
| 8327 | } else if ((RAW == 'y') && (NXT(1) == 'e') && |
| 8328 | (NXT(2) == 's')) { |
| 8329 | standalone = 1; |
| 8330 | SKIP(3); |
| 8331 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8332 | xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8333 | } |
| 8334 | if (RAW != '"') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8335 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8336 | } else |
| 8337 | NEXT; |
| 8338 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8339 | xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8340 | } |
| 8341 | } |
| 8342 | return(standalone); |
| 8343 | } |
| 8344 | |
| 8345 | /** |
| 8346 | * xmlParseXMLDecl: |
| 8347 | * @ctxt: an XML parser context |
| 8348 | * |
| 8349 | * parse an XML declaration header |
| 8350 | * |
| 8351 | * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>' |
| 8352 | */ |
| 8353 | |
| 8354 | void |
| 8355 | xmlParseXMLDecl(xmlParserCtxtPtr ctxt) { |
| 8356 | xmlChar *version; |
| 8357 | |
| 8358 | /* |
| 8359 | * We know that '<?xml' is here. |
| 8360 | */ |
| 8361 | SKIP(5); |
| 8362 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8363 | if (!IS_BLANK_CH(RAW)) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8364 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, |
| 8365 | "Blank needed after '<?xml'\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8366 | } |
| 8367 | SKIP_BLANKS; |
| 8368 | |
| 8369 | /* |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 8370 | * We must have the VersionInfo here. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8371 | */ |
| 8372 | version = xmlParseVersionInfo(ctxt); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 8373 | if (version == NULL) { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8374 | xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 8375 | } else { |
| 8376 | if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) { |
| 8377 | /* |
| 8378 | * TODO: Blueberry should be detected here |
| 8379 | */ |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 8380 | xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION, |
| 8381 | "Unsupported version '%s'\n", |
| 8382 | version, NULL); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 8383 | } |
| 8384 | if (ctxt->version != NULL) |
Daniel Veillard | d3b0882 | 2001-12-05 12:03:33 +0000 | [diff] [blame] | 8385 | xmlFree((void *) ctxt->version); |
Daniel Veillard | 1984094 | 2001-11-29 16:11:38 +0000 | [diff] [blame] | 8386 | ctxt->version = version; |
Daniel Veillard | a050d23 | 2001-09-05 15:51:05 +0000 | [diff] [blame] | 8387 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8388 | |
| 8389 | /* |
| 8390 | * We may have the encoding declaration |
| 8391 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8392 | if (!IS_BLANK_CH(RAW)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8393 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 8394 | SKIP(2); |
| 8395 | return; |
| 8396 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8397 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8398 | } |
| 8399 | xmlParseEncodingDecl(ctxt); |
| 8400 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 8401 | /* |
| 8402 | * The XML REC instructs us to stop parsing right here |
| 8403 | */ |
| 8404 | return; |
| 8405 | } |
| 8406 | |
| 8407 | /* |
| 8408 | * We may have the standalone status. |
| 8409 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8410 | if ((ctxt->input->encoding != NULL) && (!IS_BLANK_CH(RAW))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8411 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 8412 | SKIP(2); |
| 8413 | return; |
| 8414 | } |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8415 | xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8416 | } |
| 8417 | SKIP_BLANKS; |
| 8418 | ctxt->input->standalone = xmlParseSDDecl(ctxt); |
| 8419 | |
| 8420 | SKIP_BLANKS; |
| 8421 | if ((RAW == '?') && (NXT(1) == '>')) { |
| 8422 | SKIP(2); |
| 8423 | } else if (RAW == '>') { |
| 8424 | /* Deprecated old WD ... */ |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8425 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8426 | NEXT; |
| 8427 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8428 | xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8429 | MOVETO_ENDTAG(CUR_PTR); |
| 8430 | NEXT; |
| 8431 | } |
| 8432 | } |
| 8433 | |
| 8434 | /** |
| 8435 | * xmlParseMisc: |
| 8436 | * @ctxt: an XML parser context |
| 8437 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8438 | * parse an XML Misc* optional field. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8439 | * |
| 8440 | * [27] Misc ::= Comment | PI | S |
| 8441 | */ |
| 8442 | |
| 8443 | void |
| 8444 | xmlParseMisc(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 8445 | while (((RAW == '<') && (NXT(1) == '?')) || |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 8446 | (CMP4(CUR_PTR, '<', '!', '-', '-')) || |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8447 | IS_BLANK_CH(CUR)) { |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 8448 | if ((RAW == '<') && (NXT(1) == '?')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8449 | xmlParsePI(ctxt); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 8450 | } else if (IS_BLANK_CH(CUR)) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8451 | NEXT; |
| 8452 | } else |
| 8453 | xmlParseComment(ctxt); |
| 8454 | } |
| 8455 | } |
| 8456 | |
| 8457 | /** |
| 8458 | * xmlParseDocument: |
| 8459 | * @ctxt: an XML parser context |
| 8460 | * |
| 8461 | * parse an XML document (and build a tree if using the standard SAX |
| 8462 | * interface). |
| 8463 | * |
| 8464 | * [1] document ::= prolog element Misc* |
| 8465 | * |
| 8466 | * [22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)? |
| 8467 | * |
| 8468 | * Returns 0, -1 in case of error. the parser context is augmented |
| 8469 | * as a result of the parsing. |
| 8470 | */ |
| 8471 | |
| 8472 | int |
| 8473 | xmlParseDocument(xmlParserCtxtPtr ctxt) { |
| 8474 | xmlChar start[4]; |
| 8475 | xmlCharEncoding enc; |
| 8476 | |
| 8477 | xmlInitParser(); |
| 8478 | |
| 8479 | GROW; |
| 8480 | |
| 8481 | /* |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8482 | * SAX: detecting the level. |
| 8483 | */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 8484 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 0fb1893 | 2003-09-07 09:14:37 +0000 | [diff] [blame] | 8485 | |
| 8486 | /* |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8487 | * SAX: beginning of the document processing. |
| 8488 | */ |
| 8489 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 8490 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 8491 | |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 8492 | if ((ctxt->encoding == (const xmlChar *)XML_CHAR_ENCODING_NONE) && |
| 8493 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 8494 | /* |
| 8495 | * Get the 4 first bytes and decode the charset |
| 8496 | * if enc != XML_CHAR_ENCODING_NONE |
| 8497 | * plug some encoding conversion routines. |
| 8498 | */ |
| 8499 | start[0] = RAW; |
| 8500 | start[1] = NXT(1); |
| 8501 | start[2] = NXT(2); |
| 8502 | start[3] = NXT(3); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 8503 | enc = xmlDetectCharEncoding(&start[0], 4); |
Daniel Veillard | 4aafa79 | 2001-07-28 17:21:12 +0000 | [diff] [blame] | 8504 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 8505 | xmlSwitchEncoding(ctxt, enc); |
| 8506 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8507 | } |
| 8508 | |
| 8509 | |
| 8510 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8511 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8512 | } |
| 8513 | |
| 8514 | /* |
| 8515 | * Check for the XMLDecl in the Prolog. |
| 8516 | */ |
| 8517 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 8518 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8519 | |
| 8520 | /* |
| 8521 | * Note that we will switch encoding on the fly. |
| 8522 | */ |
| 8523 | xmlParseXMLDecl(ctxt); |
| 8524 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 8525 | /* |
| 8526 | * The XML REC instructs us to stop parsing right here |
| 8527 | */ |
| 8528 | return(-1); |
| 8529 | } |
| 8530 | ctxt->standalone = ctxt->input->standalone; |
| 8531 | SKIP_BLANKS; |
| 8532 | } else { |
| 8533 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 8534 | } |
| 8535 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 8536 | ctxt->sax->startDocument(ctxt->userData); |
| 8537 | |
| 8538 | /* |
| 8539 | * The Misc part of the Prolog |
| 8540 | */ |
| 8541 | GROW; |
| 8542 | xmlParseMisc(ctxt); |
| 8543 | |
| 8544 | /* |
| 8545 | * Then possibly doc type declaration(s) and more Misc |
| 8546 | * (doctypedecl Misc*)? |
| 8547 | */ |
| 8548 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 8549 | if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8550 | |
| 8551 | ctxt->inSubset = 1; |
| 8552 | xmlParseDocTypeDecl(ctxt); |
| 8553 | if (RAW == '[') { |
| 8554 | ctxt->instate = XML_PARSER_DTD; |
| 8555 | xmlParseInternalSubset(ctxt); |
| 8556 | } |
| 8557 | |
| 8558 | /* |
| 8559 | * Create and update the external subset. |
| 8560 | */ |
| 8561 | ctxt->inSubset = 2; |
| 8562 | if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) && |
| 8563 | (!ctxt->disableSAX)) |
| 8564 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 8565 | ctxt->extSubSystem, ctxt->extSubURI); |
| 8566 | ctxt->inSubset = 0; |
| 8567 | |
| 8568 | |
| 8569 | ctxt->instate = XML_PARSER_PROLOG; |
| 8570 | xmlParseMisc(ctxt); |
| 8571 | } |
| 8572 | |
| 8573 | /* |
| 8574 | * Time to start parsing the tree itself |
| 8575 | */ |
| 8576 | GROW; |
| 8577 | if (RAW != '<') { |
Daniel Veillard | bdbe0d4 | 2003-09-14 19:56:14 +0000 | [diff] [blame] | 8578 | xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY, |
| 8579 | "Start tag expected, '<' not found\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8580 | } else { |
| 8581 | ctxt->instate = XML_PARSER_CONTENT; |
| 8582 | xmlParseElement(ctxt); |
| 8583 | ctxt->instate = XML_PARSER_EPILOG; |
| 8584 | |
| 8585 | |
| 8586 | /* |
| 8587 | * The Misc part at the end |
| 8588 | */ |
| 8589 | xmlParseMisc(ctxt); |
| 8590 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 8591 | if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8592 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8593 | } |
| 8594 | ctxt->instate = XML_PARSER_EOF; |
| 8595 | } |
| 8596 | |
| 8597 | /* |
| 8598 | * SAX: end of the document processing. |
| 8599 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 8600 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8601 | ctxt->sax->endDocument(ctxt->userData); |
| 8602 | |
Daniel Veillard | 5997aca | 2002-03-18 18:36:20 +0000 | [diff] [blame] | 8603 | /* |
| 8604 | * Remove locally kept entity definitions if the tree was not built |
| 8605 | */ |
| 8606 | if ((ctxt->myDoc != NULL) && |
| 8607 | (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) { |
| 8608 | xmlFreeDoc(ctxt->myDoc); |
| 8609 | ctxt->myDoc = NULL; |
| 8610 | } |
| 8611 | |
Daniel Veillard | c761299 | 2002-02-17 22:47:37 +0000 | [diff] [blame] | 8612 | if (! ctxt->wellFormed) { |
| 8613 | ctxt->valid = 0; |
| 8614 | return(-1); |
| 8615 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8616 | return(0); |
| 8617 | } |
| 8618 | |
| 8619 | /** |
| 8620 | * xmlParseExtParsedEnt: |
| 8621 | * @ctxt: an XML parser context |
| 8622 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 8623 | * parse a general parsed entity |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8624 | * An external general parsed entity is well-formed if it matches the |
| 8625 | * production labeled extParsedEnt. |
| 8626 | * |
| 8627 | * [78] extParsedEnt ::= TextDecl? content |
| 8628 | * |
| 8629 | * Returns 0, -1 in case of error. the parser context is augmented |
| 8630 | * as a result of the parsing. |
| 8631 | */ |
| 8632 | |
| 8633 | int |
| 8634 | xmlParseExtParsedEnt(xmlParserCtxtPtr ctxt) { |
| 8635 | xmlChar start[4]; |
| 8636 | xmlCharEncoding enc; |
| 8637 | |
| 8638 | xmlDefaultSAXHandlerInit(); |
| 8639 | |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 8640 | xmlDetectSAX2(ctxt); |
| 8641 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8642 | GROW; |
| 8643 | |
| 8644 | /* |
| 8645 | * SAX: beginning of the document processing. |
| 8646 | */ |
| 8647 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 8648 | ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator); |
| 8649 | |
| 8650 | /* |
| 8651 | * Get the 4 first bytes and decode the charset |
| 8652 | * if enc != XML_CHAR_ENCODING_NONE |
| 8653 | * plug some encoding conversion routines. |
| 8654 | */ |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 8655 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 8656 | start[0] = RAW; |
| 8657 | start[1] = NXT(1); |
| 8658 | start[2] = NXT(2); |
| 8659 | start[3] = NXT(3); |
| 8660 | enc = xmlDetectCharEncoding(start, 4); |
| 8661 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 8662 | xmlSwitchEncoding(ctxt, enc); |
| 8663 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8664 | } |
| 8665 | |
| 8666 | |
| 8667 | if (CUR == 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8668 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8669 | } |
| 8670 | |
| 8671 | /* |
| 8672 | * Check for the XMLDecl in the Prolog. |
| 8673 | */ |
| 8674 | GROW; |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 8675 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8676 | |
| 8677 | /* |
| 8678 | * Note that we will switch encoding on the fly. |
| 8679 | */ |
| 8680 | xmlParseXMLDecl(ctxt); |
| 8681 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 8682 | /* |
| 8683 | * The XML REC instructs us to stop parsing right here |
| 8684 | */ |
| 8685 | return(-1); |
| 8686 | } |
| 8687 | SKIP_BLANKS; |
| 8688 | } else { |
| 8689 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 8690 | } |
| 8691 | if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX)) |
| 8692 | ctxt->sax->startDocument(ctxt->userData); |
| 8693 | |
| 8694 | /* |
| 8695 | * Doing validity checking on chunk doesn't make sense |
| 8696 | */ |
| 8697 | ctxt->instate = XML_PARSER_CONTENT; |
| 8698 | ctxt->validate = 0; |
| 8699 | ctxt->loadsubset = 0; |
| 8700 | ctxt->depth = 0; |
| 8701 | |
| 8702 | xmlParseContent(ctxt); |
| 8703 | |
| 8704 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8705 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8706 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 8707 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8708 | } |
| 8709 | |
| 8710 | /* |
| 8711 | * SAX: end of the document processing. |
| 8712 | */ |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 8713 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8714 | ctxt->sax->endDocument(ctxt->userData); |
| 8715 | |
| 8716 | if (! ctxt->wellFormed) return(-1); |
| 8717 | return(0); |
| 8718 | } |
| 8719 | |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 8720 | #ifdef LIBXML_PUSH_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8721 | /************************************************************************ |
| 8722 | * * |
| 8723 | * Progressive parsing interfaces * |
| 8724 | * * |
| 8725 | ************************************************************************/ |
| 8726 | |
| 8727 | /** |
| 8728 | * xmlParseLookupSequence: |
| 8729 | * @ctxt: an XML parser context |
| 8730 | * @first: the first char to lookup |
| 8731 | * @next: the next char to lookup or zero |
| 8732 | * @third: the next char to lookup or zero |
| 8733 | * |
| 8734 | * Try to find if a sequence (first, next, third) or just (first next) or |
| 8735 | * (first) is available in the input stream. |
| 8736 | * This function has a side effect of (possibly) incrementing ctxt->checkIndex |
| 8737 | * to avoid rescanning sequences of bytes, it DOES change the state of the |
| 8738 | * parser, do not use liberally. |
| 8739 | * |
| 8740 | * Returns the index to the current parsing point if the full sequence |
| 8741 | * is available, -1 otherwise. |
| 8742 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8743 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8744 | xmlParseLookupSequence(xmlParserCtxtPtr ctxt, xmlChar first, |
| 8745 | xmlChar next, xmlChar third) { |
| 8746 | int base, len; |
| 8747 | xmlParserInputPtr in; |
| 8748 | const xmlChar *buf; |
| 8749 | |
| 8750 | in = ctxt->input; |
| 8751 | if (in == NULL) return(-1); |
| 8752 | base = in->cur - in->base; |
| 8753 | if (base < 0) return(-1); |
| 8754 | if (ctxt->checkIndex > base) |
| 8755 | base = ctxt->checkIndex; |
| 8756 | if (in->buf == NULL) { |
| 8757 | buf = in->base; |
| 8758 | len = in->length; |
| 8759 | } else { |
| 8760 | buf = in->buf->buffer->content; |
| 8761 | len = in->buf->buffer->use; |
| 8762 | } |
| 8763 | /* take into account the sequence length */ |
| 8764 | if (third) len -= 2; |
| 8765 | else if (next) len --; |
| 8766 | for (;base < len;base++) { |
| 8767 | if (buf[base] == first) { |
| 8768 | if (third != 0) { |
| 8769 | if ((buf[base + 1] != next) || |
| 8770 | (buf[base + 2] != third)) continue; |
| 8771 | } else if (next != 0) { |
| 8772 | if (buf[base + 1] != next) continue; |
| 8773 | } |
| 8774 | ctxt->checkIndex = 0; |
| 8775 | #ifdef DEBUG_PUSH |
| 8776 | if (next == 0) |
| 8777 | xmlGenericError(xmlGenericErrorContext, |
| 8778 | "PP: lookup '%c' found at %d\n", |
| 8779 | first, base); |
| 8780 | else if (third == 0) |
| 8781 | xmlGenericError(xmlGenericErrorContext, |
| 8782 | "PP: lookup '%c%c' found at %d\n", |
| 8783 | first, next, base); |
| 8784 | else |
| 8785 | xmlGenericError(xmlGenericErrorContext, |
| 8786 | "PP: lookup '%c%c%c' found at %d\n", |
| 8787 | first, next, third, base); |
| 8788 | #endif |
| 8789 | return(base - (in->cur - in->base)); |
| 8790 | } |
| 8791 | } |
| 8792 | ctxt->checkIndex = base; |
| 8793 | #ifdef DEBUG_PUSH |
| 8794 | if (next == 0) |
| 8795 | xmlGenericError(xmlGenericErrorContext, |
| 8796 | "PP: lookup '%c' failed\n", first); |
| 8797 | else if (third == 0) |
| 8798 | xmlGenericError(xmlGenericErrorContext, |
| 8799 | "PP: lookup '%c%c' failed\n", first, next); |
| 8800 | else |
| 8801 | xmlGenericError(xmlGenericErrorContext, |
| 8802 | "PP: lookup '%c%c%c' failed\n", first, next, third); |
| 8803 | #endif |
| 8804 | return(-1); |
| 8805 | } |
| 8806 | |
| 8807 | /** |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 8808 | * xmlParseGetLasts: |
| 8809 | * @ctxt: an XML parser context |
| 8810 | * @lastlt: pointer to store the last '<' from the input |
| 8811 | * @lastgt: pointer to store the last '>' from the input |
| 8812 | * |
| 8813 | * Lookup the last < and > in the current chunk |
| 8814 | */ |
| 8815 | static void |
| 8816 | xmlParseGetLasts(xmlParserCtxtPtr ctxt, const xmlChar **lastlt, |
| 8817 | const xmlChar **lastgt) { |
| 8818 | const xmlChar *tmp; |
| 8819 | |
| 8820 | if ((ctxt == NULL) || (lastlt == NULL) || (lastgt == NULL)) { |
| 8821 | xmlGenericError(xmlGenericErrorContext, |
| 8822 | "Internal error: xmlParseGetLasts\n"); |
| 8823 | return; |
| 8824 | } |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 8825 | if ((ctxt->progressive != 0) && (ctxt->inputNr == 1)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 8826 | tmp = ctxt->input->end; |
| 8827 | tmp--; |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 8828 | while ((tmp >= ctxt->input->base) && (*tmp != '<')) tmp--; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 8829 | if (tmp < ctxt->input->base) { |
| 8830 | *lastlt = NULL; |
| 8831 | *lastgt = NULL; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 8832 | } else { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 8833 | *lastlt = tmp; |
| 8834 | tmp++; |
| 8835 | while ((tmp < ctxt->input->end) && (*tmp != '>')) { |
| 8836 | if (*tmp == '\'') { |
| 8837 | tmp++; |
| 8838 | while ((tmp < ctxt->input->end) && (*tmp != '\'')) tmp++; |
| 8839 | if (tmp < ctxt->input->end) tmp++; |
| 8840 | } else if (*tmp == '"') { |
| 8841 | tmp++; |
| 8842 | while ((tmp < ctxt->input->end) && (*tmp != '"')) tmp++; |
| 8843 | if (tmp < ctxt->input->end) tmp++; |
| 8844 | } else |
| 8845 | tmp++; |
| 8846 | } |
| 8847 | if (tmp < ctxt->input->end) |
| 8848 | *lastgt = tmp; |
| 8849 | else { |
| 8850 | tmp = *lastlt; |
| 8851 | tmp--; |
| 8852 | while ((tmp >= ctxt->input->base) && (*tmp != '>')) tmp--; |
| 8853 | if (tmp >= ctxt->input->base) |
| 8854 | *lastgt = tmp; |
| 8855 | else |
| 8856 | *lastgt = NULL; |
| 8857 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 8858 | } |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 8859 | } else { |
| 8860 | *lastlt = NULL; |
| 8861 | *lastgt = NULL; |
| 8862 | } |
| 8863 | } |
| 8864 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8865 | * xmlParseTryOrFinish: |
| 8866 | * @ctxt: an XML parser context |
| 8867 | * @terminate: last chunk indicator |
| 8868 | * |
| 8869 | * Try to progress on parsing |
| 8870 | * |
| 8871 | * Returns zero if no parsing was possible |
| 8872 | */ |
Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 8873 | static int |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8874 | xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) { |
| 8875 | int ret = 0; |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 8876 | int avail, tlen; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8877 | xmlChar cur, next; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 8878 | const xmlChar *lastlt, *lastgt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8879 | |
| 8880 | #ifdef DEBUG_PUSH |
| 8881 | switch (ctxt->instate) { |
| 8882 | case XML_PARSER_EOF: |
| 8883 | xmlGenericError(xmlGenericErrorContext, |
| 8884 | "PP: try EOF\n"); break; |
| 8885 | case XML_PARSER_START: |
| 8886 | xmlGenericError(xmlGenericErrorContext, |
| 8887 | "PP: try START\n"); break; |
| 8888 | case XML_PARSER_MISC: |
| 8889 | xmlGenericError(xmlGenericErrorContext, |
| 8890 | "PP: try MISC\n");break; |
| 8891 | case XML_PARSER_COMMENT: |
| 8892 | xmlGenericError(xmlGenericErrorContext, |
| 8893 | "PP: try COMMENT\n");break; |
| 8894 | case XML_PARSER_PROLOG: |
| 8895 | xmlGenericError(xmlGenericErrorContext, |
| 8896 | "PP: try PROLOG\n");break; |
| 8897 | case XML_PARSER_START_TAG: |
| 8898 | xmlGenericError(xmlGenericErrorContext, |
| 8899 | "PP: try START_TAG\n");break; |
| 8900 | case XML_PARSER_CONTENT: |
| 8901 | xmlGenericError(xmlGenericErrorContext, |
| 8902 | "PP: try CONTENT\n");break; |
| 8903 | case XML_PARSER_CDATA_SECTION: |
| 8904 | xmlGenericError(xmlGenericErrorContext, |
| 8905 | "PP: try CDATA_SECTION\n");break; |
| 8906 | case XML_PARSER_END_TAG: |
| 8907 | xmlGenericError(xmlGenericErrorContext, |
| 8908 | "PP: try END_TAG\n");break; |
| 8909 | case XML_PARSER_ENTITY_DECL: |
| 8910 | xmlGenericError(xmlGenericErrorContext, |
| 8911 | "PP: try ENTITY_DECL\n");break; |
| 8912 | case XML_PARSER_ENTITY_VALUE: |
| 8913 | xmlGenericError(xmlGenericErrorContext, |
| 8914 | "PP: try ENTITY_VALUE\n");break; |
| 8915 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 8916 | xmlGenericError(xmlGenericErrorContext, |
| 8917 | "PP: try ATTRIBUTE_VALUE\n");break; |
| 8918 | case XML_PARSER_DTD: |
| 8919 | xmlGenericError(xmlGenericErrorContext, |
| 8920 | "PP: try DTD\n");break; |
| 8921 | case XML_PARSER_EPILOG: |
| 8922 | xmlGenericError(xmlGenericErrorContext, |
| 8923 | "PP: try EPILOG\n");break; |
| 8924 | case XML_PARSER_PI: |
| 8925 | xmlGenericError(xmlGenericErrorContext, |
| 8926 | "PP: try PI\n");break; |
| 8927 | case XML_PARSER_IGNORE: |
| 8928 | xmlGenericError(xmlGenericErrorContext, |
| 8929 | "PP: try IGNORE\n");break; |
| 8930 | } |
| 8931 | #endif |
| 8932 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 8933 | if ((ctxt->input != NULL) && |
| 8934 | (ctxt->input->cur - ctxt->input->base > 4096)) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 8935 | xmlSHRINK(ctxt); |
| 8936 | ctxt->checkIndex = 0; |
| 8937 | } |
| 8938 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Aleksey Sanin | e48a318 | 2002-05-09 18:20:01 +0000 | [diff] [blame] | 8939 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 8940 | while (1) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 8941 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
| 8942 | return(0); |
| 8943 | |
| 8944 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8945 | /* |
| 8946 | * Pop-up of finished entities. |
| 8947 | */ |
| 8948 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 8949 | xmlPopInput(ctxt); |
| 8950 | |
Daniel Veillard | 198c1bf | 2003-10-20 17:07:41 +0000 | [diff] [blame] | 8951 | if (ctxt->input == NULL) break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8952 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 8953 | avail = ctxt->input->length - |
| 8954 | (ctxt->input->cur - ctxt->input->base); |
Daniel Veillard | 158a4d2 | 2002-02-20 22:17:58 +0000 | [diff] [blame] | 8955 | else { |
| 8956 | /* |
| 8957 | * If we are operating on converted input, try to flush |
| 8958 | * remainng chars to avoid them stalling in the non-converted |
| 8959 | * buffer. |
| 8960 | */ |
| 8961 | if ((ctxt->input->buf->raw != NULL) && |
| 8962 | (ctxt->input->buf->raw->use > 0)) { |
| 8963 | int base = ctxt->input->base - |
| 8964 | ctxt->input->buf->buffer->content; |
| 8965 | int current = ctxt->input->cur - ctxt->input->base; |
| 8966 | |
| 8967 | xmlParserInputBufferPush(ctxt->input->buf, 0, ""); |
| 8968 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 8969 | ctxt->input->cur = ctxt->input->base + current; |
| 8970 | ctxt->input->end = |
| 8971 | &ctxt->input->buf->buffer->content[ |
| 8972 | ctxt->input->buf->buffer->use]; |
| 8973 | } |
| 8974 | avail = ctxt->input->buf->buffer->use - |
| 8975 | (ctxt->input->cur - ctxt->input->base); |
| 8976 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 8977 | if (avail < 1) |
| 8978 | goto done; |
| 8979 | switch (ctxt->instate) { |
| 8980 | case XML_PARSER_EOF: |
| 8981 | /* |
| 8982 | * Document parsing is done ! |
| 8983 | */ |
| 8984 | goto done; |
| 8985 | case XML_PARSER_START: |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 8986 | if (ctxt->charset == XML_CHAR_ENCODING_NONE) { |
| 8987 | xmlChar start[4]; |
| 8988 | xmlCharEncoding enc; |
| 8989 | |
| 8990 | /* |
| 8991 | * Very first chars read from the document flow. |
| 8992 | */ |
| 8993 | if (avail < 4) |
| 8994 | goto done; |
| 8995 | |
| 8996 | /* |
| 8997 | * Get the 4 first bytes and decode the charset |
| 8998 | * if enc != XML_CHAR_ENCODING_NONE |
| 8999 | * plug some encoding conversion routines. |
| 9000 | */ |
| 9001 | start[0] = RAW; |
| 9002 | start[1] = NXT(1); |
| 9003 | start[2] = NXT(2); |
| 9004 | start[3] = NXT(3); |
| 9005 | enc = xmlDetectCharEncoding(start, 4); |
| 9006 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 9007 | xmlSwitchEncoding(ctxt, enc); |
| 9008 | } |
| 9009 | break; |
| 9010 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9011 | |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 9012 | if (avail < 2) |
| 9013 | goto done; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9014 | cur = ctxt->input->cur[0]; |
| 9015 | next = ctxt->input->cur[1]; |
| 9016 | if (cur == 0) { |
| 9017 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 9018 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 9019 | &xmlDefaultSAXLocator); |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9020 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9021 | ctxt->instate = XML_PARSER_EOF; |
| 9022 | #ifdef DEBUG_PUSH |
| 9023 | xmlGenericError(xmlGenericErrorContext, |
| 9024 | "PP: entering EOF\n"); |
| 9025 | #endif |
| 9026 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 9027 | ctxt->sax->endDocument(ctxt->userData); |
| 9028 | goto done; |
| 9029 | } |
| 9030 | if ((cur == '<') && (next == '?')) { |
| 9031 | /* PI or XML decl */ |
| 9032 | if (avail < 5) return(ret); |
| 9033 | if ((!terminate) && |
| 9034 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 9035 | return(ret); |
| 9036 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 9037 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 9038 | &xmlDefaultSAXLocator); |
| 9039 | if ((ctxt->input->cur[2] == 'x') && |
| 9040 | (ctxt->input->cur[3] == 'm') && |
| 9041 | (ctxt->input->cur[4] == 'l') && |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 9042 | (IS_BLANK_CH(ctxt->input->cur[5]))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9043 | ret += 5; |
| 9044 | #ifdef DEBUG_PUSH |
| 9045 | xmlGenericError(xmlGenericErrorContext, |
| 9046 | "PP: Parsing XML Decl\n"); |
| 9047 | #endif |
| 9048 | xmlParseXMLDecl(ctxt); |
| 9049 | if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) { |
| 9050 | /* |
| 9051 | * The XML REC instructs us to stop parsing right |
| 9052 | * here |
| 9053 | */ |
| 9054 | ctxt->instate = XML_PARSER_EOF; |
| 9055 | return(0); |
| 9056 | } |
| 9057 | ctxt->standalone = ctxt->input->standalone; |
| 9058 | if ((ctxt->encoding == NULL) && |
| 9059 | (ctxt->input->encoding != NULL)) |
| 9060 | ctxt->encoding = xmlStrdup(ctxt->input->encoding); |
| 9061 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 9062 | (!ctxt->disableSAX)) |
| 9063 | ctxt->sax->startDocument(ctxt->userData); |
| 9064 | ctxt->instate = XML_PARSER_MISC; |
| 9065 | #ifdef DEBUG_PUSH |
| 9066 | xmlGenericError(xmlGenericErrorContext, |
| 9067 | "PP: entering MISC\n"); |
| 9068 | #endif |
| 9069 | } else { |
| 9070 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
| 9071 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 9072 | (!ctxt->disableSAX)) |
| 9073 | ctxt->sax->startDocument(ctxt->userData); |
| 9074 | ctxt->instate = XML_PARSER_MISC; |
| 9075 | #ifdef DEBUG_PUSH |
| 9076 | xmlGenericError(xmlGenericErrorContext, |
| 9077 | "PP: entering MISC\n"); |
| 9078 | #endif |
| 9079 | } |
| 9080 | } else { |
| 9081 | if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) |
| 9082 | ctxt->sax->setDocumentLocator(ctxt->userData, |
| 9083 | &xmlDefaultSAXLocator); |
| 9084 | ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 9085 | if (ctxt->version == NULL) { |
| 9086 | xmlErrMemory(ctxt, NULL); |
| 9087 | break; |
| 9088 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9089 | if ((ctxt->sax) && (ctxt->sax->startDocument) && |
| 9090 | (!ctxt->disableSAX)) |
| 9091 | ctxt->sax->startDocument(ctxt->userData); |
| 9092 | ctxt->instate = XML_PARSER_MISC; |
| 9093 | #ifdef DEBUG_PUSH |
| 9094 | xmlGenericError(xmlGenericErrorContext, |
| 9095 | "PP: entering MISC\n"); |
| 9096 | #endif |
| 9097 | } |
| 9098 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9099 | case XML_PARSER_START_TAG: { |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9100 | const xmlChar *name; |
| 9101 | const xmlChar *prefix; |
| 9102 | const xmlChar *URI; |
| 9103 | int nsNr = ctxt->nsNr; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9104 | |
| 9105 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 9106 | goto done; |
| 9107 | cur = ctxt->input->cur[0]; |
| 9108 | if (cur != '<') { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9109 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9110 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9111 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 9112 | ctxt->sax->endDocument(ctxt->userData); |
| 9113 | goto done; |
| 9114 | } |
| 9115 | if (!terminate) { |
| 9116 | if (ctxt->progressive) { |
Daniel Veillard | b374400 | 2004-02-18 14:28:22 +0000 | [diff] [blame] | 9117 | /* > can be found unescaped in attribute values */ |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 9118 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9119 | goto done; |
| 9120 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 9121 | goto done; |
| 9122 | } |
| 9123 | } |
| 9124 | if (ctxt->spaceNr == 0) |
| 9125 | spacePush(ctxt, -1); |
| 9126 | else |
| 9127 | spacePush(ctxt, *ctxt->space); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9128 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9129 | if (ctxt->sax2) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9130 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9131 | name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9132 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9133 | else |
| 9134 | name = xmlParseStartTag(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9135 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9136 | if (name == NULL) { |
| 9137 | spacePop(ctxt); |
| 9138 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9139 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
| 9140 | ctxt->sax->endDocument(ctxt->userData); |
| 9141 | goto done; |
| 9142 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 9143 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9144 | /* |
| 9145 | * [ VC: Root Element Type ] |
| 9146 | * The Name in the document type declaration must match |
| 9147 | * the element type of the root element. |
| 9148 | */ |
| 9149 | if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc && |
| 9150 | ctxt->node && (ctxt->node == ctxt->myDoc->children)) |
| 9151 | ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 9152 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9153 | |
| 9154 | /* |
| 9155 | * Check for an Empty Element. |
| 9156 | */ |
| 9157 | if ((RAW == '/') && (NXT(1) == '>')) { |
| 9158 | SKIP(2); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9159 | |
| 9160 | if (ctxt->sax2) { |
| 9161 | if ((ctxt->sax != NULL) && |
| 9162 | (ctxt->sax->endElementNs != NULL) && |
| 9163 | (!ctxt->disableSAX)) |
| 9164 | ctxt->sax->endElementNs(ctxt->userData, name, |
| 9165 | prefix, URI); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9166 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9167 | } else { |
| 9168 | if ((ctxt->sax != NULL) && |
| 9169 | (ctxt->sax->endElement != NULL) && |
| 9170 | (!ctxt->disableSAX)) |
| 9171 | ctxt->sax->endElement(ctxt->userData, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9172 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9173 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9174 | spacePop(ctxt); |
| 9175 | if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9176 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9177 | } else { |
| 9178 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9179 | } |
| 9180 | break; |
| 9181 | } |
| 9182 | if (RAW == '>') { |
| 9183 | NEXT; |
| 9184 | } else { |
Daniel Veillard | bc92eca | 2003-09-15 09:48:06 +0000 | [diff] [blame] | 9185 | xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED, |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9186 | "Couldn't find end of Start Tag %s\n", |
| 9187 | name); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9188 | nodePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9189 | spacePop(ctxt); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9190 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9191 | if (ctxt->sax2) |
| 9192 | nameNsPush(ctxt, name, prefix, URI, ctxt->nsNr - nsNr); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9193 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9194 | else |
| 9195 | namePush(ctxt, name); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9196 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9197 | |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9198 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9199 | break; |
| 9200 | } |
| 9201 | case XML_PARSER_CONTENT: { |
| 9202 | const xmlChar *test; |
| 9203 | unsigned int cons; |
| 9204 | if ((avail < 2) && (ctxt->inputNr == 1)) |
| 9205 | goto done; |
| 9206 | cur = ctxt->input->cur[0]; |
| 9207 | next = ctxt->input->cur[1]; |
| 9208 | |
| 9209 | test = CUR_PTR; |
| 9210 | cons = ctxt->input->consumed; |
| 9211 | if ((cur == '<') && (next == '/')) { |
| 9212 | ctxt->instate = XML_PARSER_END_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9213 | break; |
| 9214 | } else if ((cur == '<') && (next == '?')) { |
| 9215 | if ((!terminate) && |
| 9216 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 9217 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9218 | xmlParsePI(ctxt); |
| 9219 | } else if ((cur == '<') && (next != '!')) { |
| 9220 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9221 | break; |
| 9222 | } else if ((cur == '<') && (next == '!') && |
| 9223 | (ctxt->input->cur[2] == '-') && |
| 9224 | (ctxt->input->cur[3] == '-')) { |
| 9225 | if ((!terminate) && |
| 9226 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 9227 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9228 | xmlParseComment(ctxt); |
| 9229 | ctxt->instate = XML_PARSER_CONTENT; |
| 9230 | } else if ((cur == '<') && (ctxt->input->cur[1] == '!') && |
| 9231 | (ctxt->input->cur[2] == '[') && |
| 9232 | (ctxt->input->cur[3] == 'C') && |
| 9233 | (ctxt->input->cur[4] == 'D') && |
| 9234 | (ctxt->input->cur[5] == 'A') && |
| 9235 | (ctxt->input->cur[6] == 'T') && |
| 9236 | (ctxt->input->cur[7] == 'A') && |
| 9237 | (ctxt->input->cur[8] == '[')) { |
| 9238 | SKIP(9); |
| 9239 | ctxt->instate = XML_PARSER_CDATA_SECTION; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9240 | break; |
| 9241 | } else if ((cur == '<') && (next == '!') && |
| 9242 | (avail < 9)) { |
| 9243 | goto done; |
| 9244 | } else if (cur == '&') { |
| 9245 | if ((!terminate) && |
| 9246 | (xmlParseLookupSequence(ctxt, ';', 0, 0) < 0)) |
| 9247 | goto done; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9248 | xmlParseReference(ctxt); |
| 9249 | } else { |
| 9250 | /* TODO Avoid the extra copy, handle directly !!! */ |
| 9251 | /* |
| 9252 | * Goal of the following test is: |
| 9253 | * - minimize calls to the SAX 'character' callback |
| 9254 | * when they are mergeable |
| 9255 | * - handle an problem for isBlank when we only parse |
| 9256 | * a sequence of blank chars and the next one is |
| 9257 | * not available to check against '<' presence. |
| 9258 | * - tries to homogenize the differences in SAX |
| 9259 | * callbacks between the push and pull versions |
| 9260 | * of the parser. |
| 9261 | */ |
| 9262 | if ((ctxt->inputNr == 1) && |
| 9263 | (avail < XML_PARSER_BIG_BUFFER_SIZE)) { |
| 9264 | if (!terminate) { |
| 9265 | if (ctxt->progressive) { |
| 9266 | if ((lastlt == NULL) || |
| 9267 | (ctxt->input->cur > lastlt)) |
| 9268 | goto done; |
| 9269 | } else if (xmlParseLookupSequence(ctxt, |
| 9270 | '<', 0, 0) < 0) { |
| 9271 | goto done; |
| 9272 | } |
| 9273 | } |
| 9274 | } |
| 9275 | ctxt->checkIndex = 0; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9276 | xmlParseCharData(ctxt, 0); |
| 9277 | } |
| 9278 | /* |
| 9279 | * Pop-up of finished entities. |
| 9280 | */ |
| 9281 | while ((RAW == 0) && (ctxt->inputNr > 1)) |
| 9282 | xmlPopInput(ctxt); |
| 9283 | if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9284 | xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, |
| 9285 | "detected an error in element content\n"); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9286 | ctxt->instate = XML_PARSER_EOF; |
| 9287 | break; |
| 9288 | } |
| 9289 | break; |
| 9290 | } |
| 9291 | case XML_PARSER_END_TAG: |
| 9292 | if (avail < 2) |
| 9293 | goto done; |
| 9294 | if (!terminate) { |
| 9295 | if (ctxt->progressive) { |
Daniel Veillard | eb70f93 | 2004-07-05 16:46:09 +0000 | [diff] [blame] | 9296 | /* > can be found unescaped in attribute values */ |
| 9297 | if ((lastgt == NULL) || (ctxt->input->cur >= lastgt)) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9298 | goto done; |
| 9299 | } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) { |
| 9300 | goto done; |
| 9301 | } |
| 9302 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9303 | if (ctxt->sax2) { |
| 9304 | xmlParseEndTag2(ctxt, |
| 9305 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 3], |
| 9306 | (void *) ctxt->pushTab[ctxt->nameNr * 3 - 2], 0, |
Daniel Veillard | c82c57e | 2004-01-12 16:24:34 +0000 | [diff] [blame] | 9307 | (int) (long) ctxt->pushTab[ctxt->nameNr * 3 - 1], 0); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9308 | nameNsPop(ctxt); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9309 | } |
| 9310 | #ifdef LIBXML_SAX1_ENABLED |
| 9311 | else |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9312 | xmlParseEndTag1(ctxt, 0); |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9313 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9314 | if (ctxt->nameNr == 0) { |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9315 | ctxt->instate = XML_PARSER_EPILOG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9316 | } else { |
| 9317 | ctxt->instate = XML_PARSER_CONTENT; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9318 | } |
| 9319 | break; |
| 9320 | case XML_PARSER_CDATA_SECTION: { |
| 9321 | /* |
| 9322 | * The Push mode need to have the SAX callback for |
| 9323 | * cdataBlock merge back contiguous callbacks. |
| 9324 | */ |
| 9325 | int base; |
| 9326 | |
| 9327 | base = xmlParseLookupSequence(ctxt, ']', ']', '>'); |
| 9328 | if (base < 0) { |
| 9329 | if (avail >= XML_PARSER_BIG_BUFFER_SIZE + 2) { |
| 9330 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) { |
| 9331 | if (ctxt->sax->cdataBlock != NULL) |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 9332 | ctxt->sax->cdataBlock(ctxt->userData, |
| 9333 | ctxt->input->cur, |
| 9334 | XML_PARSER_BIG_BUFFER_SIZE); |
| 9335 | else if (ctxt->sax->characters != NULL) |
| 9336 | ctxt->sax->characters(ctxt->userData, |
| 9337 | ctxt->input->cur, |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9338 | XML_PARSER_BIG_BUFFER_SIZE); |
| 9339 | } |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 9340 | SKIPL(XML_PARSER_BIG_BUFFER_SIZE); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9341 | ctxt->checkIndex = 0; |
| 9342 | } |
| 9343 | goto done; |
| 9344 | } else { |
| 9345 | if ((ctxt->sax != NULL) && (base > 0) && |
| 9346 | (!ctxt->disableSAX)) { |
| 9347 | if (ctxt->sax->cdataBlock != NULL) |
| 9348 | ctxt->sax->cdataBlock(ctxt->userData, |
| 9349 | ctxt->input->cur, base); |
Daniel Veillard | d9d32ae | 2003-07-05 20:32:43 +0000 | [diff] [blame] | 9350 | else if (ctxt->sax->characters != NULL) |
| 9351 | ctxt->sax->characters(ctxt->userData, |
| 9352 | ctxt->input->cur, base); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9353 | } |
Daniel Veillard | 0b787f3 | 2004-03-26 17:29:53 +0000 | [diff] [blame] | 9354 | SKIPL(base + 3); |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9355 | ctxt->checkIndex = 0; |
| 9356 | ctxt->instate = XML_PARSER_CONTENT; |
| 9357 | #ifdef DEBUG_PUSH |
| 9358 | xmlGenericError(xmlGenericErrorContext, |
| 9359 | "PP: entering CONTENT\n"); |
| 9360 | #endif |
| 9361 | } |
| 9362 | break; |
| 9363 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9364 | case XML_PARSER_MISC: |
| 9365 | SKIP_BLANKS; |
| 9366 | if (ctxt->input->buf == NULL) |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9367 | avail = ctxt->input->length - |
| 9368 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9369 | else |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9370 | avail = ctxt->input->buf->buffer->use - |
| 9371 | (ctxt->input->cur - ctxt->input->base); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9372 | if (avail < 2) |
| 9373 | goto done; |
| 9374 | cur = ctxt->input->cur[0]; |
| 9375 | next = ctxt->input->cur[1]; |
| 9376 | if ((cur == '<') && (next == '?')) { |
| 9377 | if ((!terminate) && |
| 9378 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 9379 | goto done; |
| 9380 | #ifdef DEBUG_PUSH |
| 9381 | xmlGenericError(xmlGenericErrorContext, |
| 9382 | "PP: Parsing PI\n"); |
| 9383 | #endif |
| 9384 | xmlParsePI(ctxt); |
| 9385 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9386 | (ctxt->input->cur[2] == '-') && |
| 9387 | (ctxt->input->cur[3] == '-')) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9388 | if ((!terminate) && |
| 9389 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 9390 | goto done; |
| 9391 | #ifdef DEBUG_PUSH |
| 9392 | xmlGenericError(xmlGenericErrorContext, |
| 9393 | "PP: Parsing Comment\n"); |
| 9394 | #endif |
| 9395 | xmlParseComment(ctxt); |
| 9396 | ctxt->instate = XML_PARSER_MISC; |
| 9397 | } else if ((cur == '<') && (next == '!') && |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9398 | (ctxt->input->cur[2] == 'D') && |
| 9399 | (ctxt->input->cur[3] == 'O') && |
| 9400 | (ctxt->input->cur[4] == 'C') && |
| 9401 | (ctxt->input->cur[5] == 'T') && |
| 9402 | (ctxt->input->cur[6] == 'Y') && |
| 9403 | (ctxt->input->cur[7] == 'P') && |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9404 | (ctxt->input->cur[8] == 'E')) { |
| 9405 | if ((!terminate) && |
| 9406 | (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0)) |
| 9407 | goto done; |
| 9408 | #ifdef DEBUG_PUSH |
| 9409 | xmlGenericError(xmlGenericErrorContext, |
| 9410 | "PP: Parsing internal subset\n"); |
| 9411 | #endif |
| 9412 | ctxt->inSubset = 1; |
| 9413 | xmlParseDocTypeDecl(ctxt); |
| 9414 | if (RAW == '[') { |
| 9415 | ctxt->instate = XML_PARSER_DTD; |
| 9416 | #ifdef DEBUG_PUSH |
| 9417 | xmlGenericError(xmlGenericErrorContext, |
| 9418 | "PP: entering DTD\n"); |
| 9419 | #endif |
| 9420 | } else { |
| 9421 | /* |
| 9422 | * Create and update the external subset. |
| 9423 | */ |
| 9424 | ctxt->inSubset = 2; |
| 9425 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 9426 | (ctxt->sax->externalSubset != NULL)) |
| 9427 | ctxt->sax->externalSubset(ctxt->userData, |
| 9428 | ctxt->intSubName, ctxt->extSubSystem, |
| 9429 | ctxt->extSubURI); |
| 9430 | ctxt->inSubset = 0; |
| 9431 | ctxt->instate = XML_PARSER_PROLOG; |
| 9432 | #ifdef DEBUG_PUSH |
| 9433 | xmlGenericError(xmlGenericErrorContext, |
| 9434 | "PP: entering PROLOG\n"); |
| 9435 | #endif |
| 9436 | } |
| 9437 | } else if ((cur == '<') && (next == '!') && |
| 9438 | (avail < 9)) { |
| 9439 | goto done; |
| 9440 | } else { |
| 9441 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9442 | ctxt->progressive = 1; |
| 9443 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9444 | #ifdef DEBUG_PUSH |
| 9445 | xmlGenericError(xmlGenericErrorContext, |
| 9446 | "PP: entering START_TAG\n"); |
| 9447 | #endif |
| 9448 | } |
| 9449 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9450 | case XML_PARSER_PROLOG: |
| 9451 | SKIP_BLANKS; |
| 9452 | if (ctxt->input->buf == NULL) |
| 9453 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 9454 | else |
| 9455 | avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base); |
| 9456 | if (avail < 2) |
| 9457 | goto done; |
| 9458 | cur = ctxt->input->cur[0]; |
| 9459 | next = ctxt->input->cur[1]; |
| 9460 | if ((cur == '<') && (next == '?')) { |
| 9461 | if ((!terminate) && |
| 9462 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 9463 | goto done; |
| 9464 | #ifdef DEBUG_PUSH |
| 9465 | xmlGenericError(xmlGenericErrorContext, |
| 9466 | "PP: Parsing PI\n"); |
| 9467 | #endif |
| 9468 | xmlParsePI(ctxt); |
| 9469 | } else if ((cur == '<') && (next == '!') && |
| 9470 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 9471 | if ((!terminate) && |
| 9472 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 9473 | goto done; |
| 9474 | #ifdef DEBUG_PUSH |
| 9475 | xmlGenericError(xmlGenericErrorContext, |
| 9476 | "PP: Parsing Comment\n"); |
| 9477 | #endif |
| 9478 | xmlParseComment(ctxt); |
| 9479 | ctxt->instate = XML_PARSER_PROLOG; |
| 9480 | } else if ((cur == '<') && (next == '!') && |
| 9481 | (avail < 4)) { |
| 9482 | goto done; |
| 9483 | } else { |
| 9484 | ctxt->instate = XML_PARSER_START_TAG; |
Daniel Veillard | 0df3bc3 | 2004-06-08 12:03:41 +0000 | [diff] [blame] | 9485 | if (ctxt->progressive == 0) |
| 9486 | ctxt->progressive = 1; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9487 | xmlParseGetLasts(ctxt, &lastlt, &lastgt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9488 | #ifdef DEBUG_PUSH |
| 9489 | xmlGenericError(xmlGenericErrorContext, |
| 9490 | "PP: entering START_TAG\n"); |
| 9491 | #endif |
| 9492 | } |
| 9493 | break; |
| 9494 | case XML_PARSER_EPILOG: |
| 9495 | SKIP_BLANKS; |
| 9496 | if (ctxt->input->buf == NULL) |
| 9497 | avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base); |
| 9498 | else |
| 9499 | avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base); |
| 9500 | if (avail < 2) |
| 9501 | goto done; |
| 9502 | cur = ctxt->input->cur[0]; |
| 9503 | next = ctxt->input->cur[1]; |
| 9504 | if ((cur == '<') && (next == '?')) { |
| 9505 | if ((!terminate) && |
| 9506 | (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0)) |
| 9507 | goto done; |
| 9508 | #ifdef DEBUG_PUSH |
| 9509 | xmlGenericError(xmlGenericErrorContext, |
| 9510 | "PP: Parsing PI\n"); |
| 9511 | #endif |
| 9512 | xmlParsePI(ctxt); |
| 9513 | ctxt->instate = XML_PARSER_EPILOG; |
| 9514 | } else if ((cur == '<') && (next == '!') && |
| 9515 | (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) { |
| 9516 | if ((!terminate) && |
| 9517 | (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0)) |
| 9518 | goto done; |
| 9519 | #ifdef DEBUG_PUSH |
| 9520 | xmlGenericError(xmlGenericErrorContext, |
| 9521 | "PP: Parsing Comment\n"); |
| 9522 | #endif |
| 9523 | xmlParseComment(ctxt); |
| 9524 | ctxt->instate = XML_PARSER_EPILOG; |
| 9525 | } else if ((cur == '<') && (next == '!') && |
| 9526 | (avail < 4)) { |
| 9527 | goto done; |
| 9528 | } else { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9529 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9530 | ctxt->instate = XML_PARSER_EOF; |
| 9531 | #ifdef DEBUG_PUSH |
| 9532 | xmlGenericError(xmlGenericErrorContext, |
| 9533 | "PP: entering EOF\n"); |
| 9534 | #endif |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 9535 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9536 | ctxt->sax->endDocument(ctxt->userData); |
| 9537 | goto done; |
| 9538 | } |
| 9539 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9540 | case XML_PARSER_DTD: { |
| 9541 | /* |
| 9542 | * Sorry but progressive parsing of the internal subset |
| 9543 | * is not expected to be supported. We first check that |
| 9544 | * the full content of the internal subset is available and |
| 9545 | * the parsing is launched only at that point. |
| 9546 | * Internal subset ends up with "']' S? '>'" in an unescaped |
| 9547 | * section and not in a ']]>' sequence which are conditional |
| 9548 | * sections (whoever argued to keep that crap in XML deserve |
| 9549 | * a place in hell !). |
| 9550 | */ |
| 9551 | int base, i; |
| 9552 | xmlChar *buf; |
| 9553 | xmlChar quote = 0; |
| 9554 | |
| 9555 | base = ctxt->input->cur - ctxt->input->base; |
| 9556 | if (base < 0) return(0); |
| 9557 | if (ctxt->checkIndex > base) |
| 9558 | base = ctxt->checkIndex; |
| 9559 | buf = ctxt->input->buf->buffer->content; |
| 9560 | for (;(unsigned int) base < ctxt->input->buf->buffer->use; |
| 9561 | base++) { |
| 9562 | if (quote != 0) { |
| 9563 | if (buf[base] == quote) |
| 9564 | quote = 0; |
| 9565 | continue; |
| 9566 | } |
Daniel Veillard | 036143b | 2004-02-12 11:57:52 +0000 | [diff] [blame] | 9567 | if ((quote == 0) && (buf[base] == '<')) { |
| 9568 | int found = 0; |
| 9569 | /* special handling of comments */ |
| 9570 | if (((unsigned int) base + 4 < |
| 9571 | ctxt->input->buf->buffer->use) && |
| 9572 | (buf[base + 1] == '!') && |
| 9573 | (buf[base + 2] == '-') && |
| 9574 | (buf[base + 3] == '-')) { |
| 9575 | for (;(unsigned int) base + 3 < |
| 9576 | ctxt->input->buf->buffer->use; base++) { |
| 9577 | if ((buf[base] == '-') && |
| 9578 | (buf[base + 1] == '-') && |
| 9579 | (buf[base + 2] == '>')) { |
| 9580 | found = 1; |
| 9581 | base += 2; |
| 9582 | break; |
| 9583 | } |
| 9584 | } |
| 9585 | if (!found) |
| 9586 | break; |
| 9587 | continue; |
| 9588 | } |
| 9589 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9590 | if (buf[base] == '"') { |
| 9591 | quote = '"'; |
| 9592 | continue; |
| 9593 | } |
| 9594 | if (buf[base] == '\'') { |
| 9595 | quote = '\''; |
| 9596 | continue; |
| 9597 | } |
| 9598 | if (buf[base] == ']') { |
| 9599 | if ((unsigned int) base +1 >= |
| 9600 | ctxt->input->buf->buffer->use) |
| 9601 | break; |
| 9602 | if (buf[base + 1] == ']') { |
| 9603 | /* conditional crap, skip both ']' ! */ |
| 9604 | base++; |
| 9605 | continue; |
| 9606 | } |
| 9607 | for (i = 0; |
| 9608 | (unsigned int) base + i < ctxt->input->buf->buffer->use; |
| 9609 | i++) { |
| 9610 | if (buf[base + i] == '>') |
| 9611 | goto found_end_int_subset; |
| 9612 | } |
| 9613 | break; |
| 9614 | } |
| 9615 | } |
| 9616 | /* |
| 9617 | * We didn't found the end of the Internal subset |
| 9618 | */ |
| 9619 | if (quote == 0) |
| 9620 | ctxt->checkIndex = base; |
| 9621 | #ifdef DEBUG_PUSH |
| 9622 | if (next == 0) |
| 9623 | xmlGenericError(xmlGenericErrorContext, |
| 9624 | "PP: lookup of int subset end filed\n"); |
| 9625 | #endif |
| 9626 | goto done; |
| 9627 | |
| 9628 | found_end_int_subset: |
| 9629 | xmlParseInternalSubset(ctxt); |
| 9630 | ctxt->inSubset = 2; |
| 9631 | if ((ctxt->sax != NULL) && (!ctxt->disableSAX) && |
| 9632 | (ctxt->sax->externalSubset != NULL)) |
| 9633 | ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName, |
| 9634 | ctxt->extSubSystem, ctxt->extSubURI); |
| 9635 | ctxt->inSubset = 0; |
| 9636 | ctxt->instate = XML_PARSER_PROLOG; |
| 9637 | ctxt->checkIndex = 0; |
| 9638 | #ifdef DEBUG_PUSH |
| 9639 | xmlGenericError(xmlGenericErrorContext, |
| 9640 | "PP: entering PROLOG\n"); |
| 9641 | #endif |
| 9642 | break; |
| 9643 | } |
| 9644 | case XML_PARSER_COMMENT: |
| 9645 | xmlGenericError(xmlGenericErrorContext, |
| 9646 | "PP: internal error, state == COMMENT\n"); |
| 9647 | ctxt->instate = XML_PARSER_CONTENT; |
| 9648 | #ifdef DEBUG_PUSH |
| 9649 | xmlGenericError(xmlGenericErrorContext, |
| 9650 | "PP: entering CONTENT\n"); |
| 9651 | #endif |
| 9652 | break; |
Daniel Veillard | a880b12 | 2003-04-21 21:36:41 +0000 | [diff] [blame] | 9653 | case XML_PARSER_IGNORE: |
| 9654 | xmlGenericError(xmlGenericErrorContext, |
| 9655 | "PP: internal error, state == IGNORE"); |
| 9656 | ctxt->instate = XML_PARSER_DTD; |
| 9657 | #ifdef DEBUG_PUSH |
| 9658 | xmlGenericError(xmlGenericErrorContext, |
| 9659 | "PP: entering DTD\n"); |
| 9660 | #endif |
| 9661 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9662 | case XML_PARSER_PI: |
| 9663 | xmlGenericError(xmlGenericErrorContext, |
| 9664 | "PP: internal error, state == PI\n"); |
| 9665 | ctxt->instate = XML_PARSER_CONTENT; |
| 9666 | #ifdef DEBUG_PUSH |
| 9667 | xmlGenericError(xmlGenericErrorContext, |
| 9668 | "PP: entering CONTENT\n"); |
| 9669 | #endif |
| 9670 | break; |
| 9671 | case XML_PARSER_ENTITY_DECL: |
| 9672 | xmlGenericError(xmlGenericErrorContext, |
| 9673 | "PP: internal error, state == ENTITY_DECL\n"); |
| 9674 | ctxt->instate = XML_PARSER_DTD; |
| 9675 | #ifdef DEBUG_PUSH |
| 9676 | xmlGenericError(xmlGenericErrorContext, |
| 9677 | "PP: entering DTD\n"); |
| 9678 | #endif |
| 9679 | break; |
| 9680 | case XML_PARSER_ENTITY_VALUE: |
| 9681 | xmlGenericError(xmlGenericErrorContext, |
| 9682 | "PP: internal error, state == ENTITY_VALUE\n"); |
| 9683 | ctxt->instate = XML_PARSER_CONTENT; |
| 9684 | #ifdef DEBUG_PUSH |
| 9685 | xmlGenericError(xmlGenericErrorContext, |
| 9686 | "PP: entering DTD\n"); |
| 9687 | #endif |
| 9688 | break; |
| 9689 | case XML_PARSER_ATTRIBUTE_VALUE: |
| 9690 | xmlGenericError(xmlGenericErrorContext, |
| 9691 | "PP: internal error, state == ATTRIBUTE_VALUE\n"); |
| 9692 | ctxt->instate = XML_PARSER_START_TAG; |
| 9693 | #ifdef DEBUG_PUSH |
| 9694 | xmlGenericError(xmlGenericErrorContext, |
| 9695 | "PP: entering START_TAG\n"); |
| 9696 | #endif |
| 9697 | break; |
| 9698 | case XML_PARSER_SYSTEM_LITERAL: |
| 9699 | xmlGenericError(xmlGenericErrorContext, |
| 9700 | "PP: internal error, state == SYSTEM_LITERAL\n"); |
| 9701 | ctxt->instate = XML_PARSER_START_TAG; |
| 9702 | #ifdef DEBUG_PUSH |
| 9703 | xmlGenericError(xmlGenericErrorContext, |
| 9704 | "PP: entering START_TAG\n"); |
| 9705 | #endif |
| 9706 | break; |
Daniel Veillard | 4a7ae50 | 2002-02-18 19:18:17 +0000 | [diff] [blame] | 9707 | case XML_PARSER_PUBLIC_LITERAL: |
| 9708 | xmlGenericError(xmlGenericErrorContext, |
| 9709 | "PP: internal error, state == PUBLIC_LITERAL\n"); |
| 9710 | ctxt->instate = XML_PARSER_START_TAG; |
| 9711 | #ifdef DEBUG_PUSH |
| 9712 | xmlGenericError(xmlGenericErrorContext, |
| 9713 | "PP: entering START_TAG\n"); |
| 9714 | #endif |
| 9715 | break; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9716 | } |
| 9717 | } |
| 9718 | done: |
| 9719 | #ifdef DEBUG_PUSH |
| 9720 | xmlGenericError(xmlGenericErrorContext, "PP: done %d\n", ret); |
| 9721 | #endif |
| 9722 | return(ret); |
| 9723 | } |
| 9724 | |
| 9725 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9726 | * xmlParseChunk: |
| 9727 | * @ctxt: an XML parser context |
| 9728 | * @chunk: an char array |
| 9729 | * @size: the size in byte of the chunk |
| 9730 | * @terminate: last chunk indicator |
| 9731 | * |
| 9732 | * Parse a Chunk of memory |
| 9733 | * |
| 9734 | * Returns zero if no error, the xmlParserErrors otherwise. |
| 9735 | */ |
| 9736 | int |
| 9737 | xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size, |
| 9738 | int terminate) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 9739 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
| 9740 | return(ctxt->errNo); |
Daniel Veillard | 309f81d | 2003-09-23 09:02:53 +0000 | [diff] [blame] | 9741 | if (ctxt->instate == XML_PARSER_START) |
| 9742 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9743 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 9744 | (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) { |
| 9745 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 9746 | int cur = ctxt->input->cur - ctxt->input->base; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 9747 | int res; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9748 | |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 9749 | res =xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
| 9750 | if (res < 0) { |
| 9751 | ctxt->errNo = XML_PARSER_EOF; |
| 9752 | ctxt->disableSAX = 1; |
| 9753 | return (XML_PARSER_EOF); |
| 9754 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9755 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 9756 | ctxt->input->cur = ctxt->input->base + cur; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 9757 | ctxt->input->end = |
| 9758 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9759 | #ifdef DEBUG_PUSH |
| 9760 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 9761 | #endif |
| 9762 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9763 | } else if (ctxt->instate != XML_PARSER_EOF) { |
| 9764 | if ((ctxt->input != NULL) && ctxt->input->buf != NULL) { |
| 9765 | xmlParserInputBufferPtr in = ctxt->input->buf; |
| 9766 | if ((in->encoder != NULL) && (in->buffer != NULL) && |
| 9767 | (in->raw != NULL)) { |
| 9768 | int nbchars; |
| 9769 | |
| 9770 | nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw); |
| 9771 | if (nbchars < 0) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 9772 | /* TODO 2.6.0 */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9773 | xmlGenericError(xmlGenericErrorContext, |
| 9774 | "xmlParseChunk: encoder error\n"); |
| 9775 | return(XML_ERR_INVALID_ENCODING); |
| 9776 | } |
| 9777 | } |
| 9778 | } |
| 9779 | } |
| 9780 | xmlParseTryOrFinish(ctxt, terminate); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 9781 | if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1)) |
| 9782 | return(ctxt->errNo); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9783 | if (terminate) { |
| 9784 | /* |
| 9785 | * Check for termination |
| 9786 | */ |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 9787 | int avail = 0; |
| 9788 | if (ctxt->input->buf == NULL) |
| 9789 | avail = ctxt->input->length - |
| 9790 | (ctxt->input->cur - ctxt->input->base); |
| 9791 | else |
| 9792 | avail = ctxt->input->buf->buffer->use - |
| 9793 | (ctxt->input->cur - ctxt->input->base); |
| 9794 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9795 | if ((ctxt->instate != XML_PARSER_EOF) && |
| 9796 | (ctxt->instate != XML_PARSER_EPILOG)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9797 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9798 | } |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 9799 | if ((ctxt->instate == XML_PARSER_EPILOG) && (avail > 0)) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9800 | xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); |
Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 9801 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9802 | if (ctxt->instate != XML_PARSER_EOF) { |
Daniel Veillard | 8d24cc1 | 2002-03-05 15:41:29 +0000 | [diff] [blame] | 9803 | if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9804 | ctxt->sax->endDocument(ctxt->userData); |
| 9805 | } |
| 9806 | ctxt->instate = XML_PARSER_EOF; |
| 9807 | } |
| 9808 | return((xmlParserErrors) ctxt->errNo); |
| 9809 | } |
| 9810 | |
| 9811 | /************************************************************************ |
| 9812 | * * |
| 9813 | * I/O front end functions to the parser * |
| 9814 | * * |
| 9815 | ************************************************************************/ |
| 9816 | |
| 9817 | /** |
| 9818 | * xmlStopParser: |
| 9819 | * @ctxt: an XML parser context |
| 9820 | * |
| 9821 | * Blocks further parser processing |
| 9822 | */ |
| 9823 | void |
| 9824 | xmlStopParser(xmlParserCtxtPtr ctxt) { |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 9825 | if (ctxt == NULL) |
| 9826 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9827 | ctxt->instate = XML_PARSER_EOF; |
Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame] | 9828 | ctxt->disableSAX = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9829 | if (ctxt->input != NULL) |
| 9830 | ctxt->input->cur = BAD_CAST""; |
| 9831 | } |
| 9832 | |
| 9833 | /** |
| 9834 | * xmlCreatePushParserCtxt: |
| 9835 | * @sax: a SAX handler |
| 9836 | * @user_data: The user data returned on SAX callbacks |
| 9837 | * @chunk: a pointer to an array of chars |
| 9838 | * @size: number of chars in the array |
| 9839 | * @filename: an optional file name or URI |
| 9840 | * |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 9841 | * Create a parser context for using the XML parser in push mode. |
| 9842 | * If @buffer and @size are non-NULL, the data is used to detect |
| 9843 | * the encoding. The remaining characters will be parsed so they |
| 9844 | * don't need to be fed in again through xmlParseChunk. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9845 | * To allow content encoding detection, @size should be >= 4 |
| 9846 | * The value of @filename is used for fetching external entities |
| 9847 | * and error/warning reports. |
| 9848 | * |
| 9849 | * Returns the new parser context or NULL |
| 9850 | */ |
Daniel Veillard | 176d99f | 2002-07-06 19:22:28 +0000 | [diff] [blame] | 9851 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9852 | xmlParserCtxtPtr |
| 9853 | xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
| 9854 | const char *chunk, int size, const char *filename) { |
| 9855 | xmlParserCtxtPtr ctxt; |
| 9856 | xmlParserInputPtr inputStream; |
| 9857 | xmlParserInputBufferPtr buf; |
| 9858 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 9859 | |
| 9860 | /* |
| 9861 | * plug some encoding conversion routines |
| 9862 | */ |
| 9863 | if ((chunk != NULL) && (size >= 4)) |
| 9864 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 9865 | |
| 9866 | buf = xmlAllocParserInputBuffer(enc); |
| 9867 | if (buf == NULL) return(NULL); |
| 9868 | |
| 9869 | ctxt = xmlNewParserCtxt(); |
| 9870 | if (ctxt == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 9871 | xmlErrMemory(NULL, "creating parser: out of memory\n"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 9872 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9873 | return(NULL); |
| 9874 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9875 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * sizeof(xmlChar *)); |
| 9876 | if (ctxt->pushTab == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9877 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 9878 | xmlFreeParserInputBuffer(buf); |
| 9879 | xmlFreeParserCtxt(ctxt); |
| 9880 | return(NULL); |
| 9881 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9882 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9883 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 9884 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9885 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9886 | xmlFree(ctxt->sax); |
| 9887 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 9888 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9889 | xmlErrMemory(ctxt, NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 9890 | xmlFreeParserInputBuffer(buf); |
| 9891 | xmlFreeParserCtxt(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9892 | return(NULL); |
| 9893 | } |
| 9894 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 9895 | if (user_data != NULL) |
| 9896 | ctxt->userData = user_data; |
| 9897 | } |
| 9898 | if (filename == NULL) { |
| 9899 | ctxt->directory = NULL; |
| 9900 | } else { |
| 9901 | ctxt->directory = xmlParserGetDirectory(filename); |
| 9902 | } |
| 9903 | |
| 9904 | inputStream = xmlNewInputStream(ctxt); |
| 9905 | if (inputStream == NULL) { |
| 9906 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 9907 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9908 | return(NULL); |
| 9909 | } |
| 9910 | |
| 9911 | if (filename == NULL) |
| 9912 | inputStream->filename = NULL; |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 9913 | else { |
Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 9914 | inputStream->filename = (char *) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 9915 | xmlCanonicPath((const xmlChar *) filename); |
William M. Brack | a3215c7 | 2004-07-31 16:24:01 +0000 | [diff] [blame] | 9916 | if (inputStream->filename == NULL) { |
| 9917 | xmlFreeParserCtxt(ctxt); |
| 9918 | xmlFreeParserInputBuffer(buf); |
| 9919 | return(NULL); |
| 9920 | } |
| 9921 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9922 | inputStream->buf = buf; |
| 9923 | inputStream->base = inputStream->buf->buffer->content; |
| 9924 | inputStream->cur = inputStream->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 9925 | inputStream->end = |
| 9926 | &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9927 | |
| 9928 | inputPush(ctxt, inputStream); |
| 9929 | |
| 9930 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 9931 | (ctxt->input->buf != NULL)) { |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 9932 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 9933 | int cur = ctxt->input->cur - ctxt->input->base; |
| 9934 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9935 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
Daniel Veillard | aa39a0f | 2002-01-06 12:47:22 +0000 | [diff] [blame] | 9936 | |
| 9937 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 9938 | ctxt->input->cur = ctxt->input->base + cur; |
| 9939 | ctxt->input->end = |
| 9940 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9941 | #ifdef DEBUG_PUSH |
| 9942 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 9943 | #endif |
| 9944 | } |
| 9945 | |
Daniel Veillard | 0e4cd17 | 2001-06-28 12:13:56 +0000 | [diff] [blame] | 9946 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 9947 | xmlSwitchEncoding(ctxt, enc); |
| 9948 | } |
| 9949 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9950 | return(ctxt); |
| 9951 | } |
Daniel Veillard | 73b013f | 2003-09-30 12:36:01 +0000 | [diff] [blame] | 9952 | #endif /* LIBXML_PUSH_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9953 | |
| 9954 | /** |
| 9955 | * xmlCreateIOParserCtxt: |
| 9956 | * @sax: a SAX handler |
| 9957 | * @user_data: The user data returned on SAX callbacks |
| 9958 | * @ioread: an I/O read function |
| 9959 | * @ioclose: an I/O close function |
| 9960 | * @ioctx: an I/O handler |
| 9961 | * @enc: the charset encoding if known |
| 9962 | * |
| 9963 | * Create a parser context for using the XML parser with an existing |
| 9964 | * I/O stream |
| 9965 | * |
| 9966 | * Returns the new parser context or NULL |
| 9967 | */ |
| 9968 | xmlParserCtxtPtr |
| 9969 | xmlCreateIOParserCtxt(xmlSAXHandlerPtr sax, void *user_data, |
| 9970 | xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
| 9971 | void *ioctx, xmlCharEncoding enc) { |
| 9972 | xmlParserCtxtPtr ctxt; |
| 9973 | xmlParserInputPtr inputStream; |
| 9974 | xmlParserInputBufferPtr buf; |
| 9975 | |
| 9976 | buf = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, enc); |
| 9977 | if (buf == NULL) return(NULL); |
| 9978 | |
| 9979 | ctxt = xmlNewParserCtxt(); |
| 9980 | if (ctxt == NULL) { |
| 9981 | xmlFree(buf); |
| 9982 | return(NULL); |
| 9983 | } |
| 9984 | if (sax != NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9985 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 9986 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 9987 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9988 | xmlFree(ctxt->sax); |
| 9989 | ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler)); |
| 9990 | if (ctxt->sax == NULL) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 9991 | xmlErrMemory(ctxt, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 9992 | xmlFree(ctxt); |
| 9993 | return(NULL); |
| 9994 | } |
| 9995 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
| 9996 | if (user_data != NULL) |
| 9997 | ctxt->userData = user_data; |
| 9998 | } |
| 9999 | |
| 10000 | inputStream = xmlNewIOInputStream(ctxt, buf, enc); |
| 10001 | if (inputStream == NULL) { |
| 10002 | xmlFreeParserCtxt(ctxt); |
| 10003 | return(NULL); |
| 10004 | } |
| 10005 | inputPush(ctxt, inputStream); |
| 10006 | |
| 10007 | return(ctxt); |
| 10008 | } |
| 10009 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10010 | #ifdef LIBXML_VALID_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10011 | /************************************************************************ |
| 10012 | * * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 10013 | * Front ends when parsing a DTD * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10014 | * * |
| 10015 | ************************************************************************/ |
| 10016 | |
| 10017 | /** |
| 10018 | * xmlIOParseDTD: |
| 10019 | * @sax: the SAX handler block or NULL |
| 10020 | * @input: an Input Buffer |
| 10021 | * @enc: the charset encoding if known |
| 10022 | * |
| 10023 | * Load and parse a DTD |
| 10024 | * |
| 10025 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 10026 | * @input will be freed at parsing end. |
| 10027 | */ |
| 10028 | |
| 10029 | xmlDtdPtr |
| 10030 | xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input, |
| 10031 | xmlCharEncoding enc) { |
| 10032 | xmlDtdPtr ret = NULL; |
| 10033 | xmlParserCtxtPtr ctxt; |
| 10034 | xmlParserInputPtr pinput = NULL; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10035 | xmlChar start[4]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10036 | |
| 10037 | if (input == NULL) |
| 10038 | return(NULL); |
| 10039 | |
| 10040 | ctxt = xmlNewParserCtxt(); |
| 10041 | if (ctxt == NULL) { |
| 10042 | return(NULL); |
| 10043 | } |
| 10044 | |
| 10045 | /* |
| 10046 | * Set-up the SAX context |
| 10047 | */ |
| 10048 | if (sax != NULL) { |
| 10049 | if (ctxt->sax != NULL) |
| 10050 | xmlFree(ctxt->sax); |
| 10051 | ctxt->sax = sax; |
Daniel Veillard | 500a1de | 2004-03-22 15:22:58 +0000 | [diff] [blame] | 10052 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10053 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10054 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10055 | |
| 10056 | /* |
| 10057 | * generate a parser input from the I/O handler |
| 10058 | */ |
| 10059 | |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 10060 | pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10061 | if (pinput == NULL) { |
| 10062 | if (sax != NULL) ctxt->sax = NULL; |
| 10063 | xmlFreeParserCtxt(ctxt); |
| 10064 | return(NULL); |
| 10065 | } |
| 10066 | |
| 10067 | /* |
| 10068 | * plug some encoding conversion routines here. |
| 10069 | */ |
| 10070 | xmlPushInput(ctxt, pinput); |
Daniel Veillard | 43caefb | 2003-12-07 19:32:22 +0000 | [diff] [blame] | 10071 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10072 | xmlSwitchEncoding(ctxt, enc); |
| 10073 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10074 | |
| 10075 | pinput->filename = NULL; |
| 10076 | pinput->line = 1; |
| 10077 | pinput->col = 1; |
| 10078 | pinput->base = ctxt->input->cur; |
| 10079 | pinput->cur = ctxt->input->cur; |
| 10080 | pinput->free = NULL; |
| 10081 | |
| 10082 | /* |
| 10083 | * let's parse that entity knowing it's an external subset. |
| 10084 | */ |
| 10085 | ctxt->inSubset = 2; |
| 10086 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 10087 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 10088 | BAD_CAST "none", BAD_CAST "none"); |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10089 | |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10090 | if ((enc == XML_CHAR_ENCODING_NONE) && |
| 10091 | ((ctxt->input->end - ctxt->input->cur) >= 4)) { |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10092 | /* |
| 10093 | * Get the 4 first bytes and decode the charset |
| 10094 | * if enc != XML_CHAR_ENCODING_NONE |
| 10095 | * plug some encoding conversion routines. |
| 10096 | */ |
| 10097 | start[0] = RAW; |
| 10098 | start[1] = NXT(1); |
| 10099 | start[2] = NXT(2); |
| 10100 | start[3] = NXT(3); |
| 10101 | enc = xmlDetectCharEncoding(start, 4); |
| 10102 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10103 | xmlSwitchEncoding(ctxt, enc); |
| 10104 | } |
| 10105 | } |
| 10106 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10107 | xmlParseExternalSubset(ctxt, BAD_CAST "none", BAD_CAST "none"); |
| 10108 | |
| 10109 | if (ctxt->myDoc != NULL) { |
| 10110 | if (ctxt->wellFormed) { |
| 10111 | ret = ctxt->myDoc->extSubset; |
| 10112 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | 329456a | 2003-04-26 21:21:00 +0000 | [diff] [blame] | 10113 | if (ret != NULL) { |
| 10114 | xmlNodePtr tmp; |
| 10115 | |
| 10116 | ret->doc = NULL; |
| 10117 | tmp = ret->children; |
| 10118 | while (tmp != NULL) { |
| 10119 | tmp->doc = NULL; |
| 10120 | tmp = tmp->next; |
| 10121 | } |
| 10122 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10123 | } else { |
| 10124 | ret = NULL; |
| 10125 | } |
| 10126 | xmlFreeDoc(ctxt->myDoc); |
| 10127 | ctxt->myDoc = NULL; |
| 10128 | } |
| 10129 | if (sax != NULL) ctxt->sax = NULL; |
| 10130 | xmlFreeParserCtxt(ctxt); |
| 10131 | |
| 10132 | return(ret); |
| 10133 | } |
| 10134 | |
| 10135 | /** |
| 10136 | * xmlSAXParseDTD: |
| 10137 | * @sax: the SAX handler block |
| 10138 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 10139 | * @SystemID: a NAME* containing the URL to the DTD |
| 10140 | * |
| 10141 | * Load and parse an external subset. |
| 10142 | * |
| 10143 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 10144 | */ |
| 10145 | |
| 10146 | xmlDtdPtr |
| 10147 | xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID, |
| 10148 | const xmlChar *SystemID) { |
| 10149 | xmlDtdPtr ret = NULL; |
| 10150 | xmlParserCtxtPtr ctxt; |
| 10151 | xmlParserInputPtr input = NULL; |
| 10152 | xmlCharEncoding enc; |
| 10153 | |
| 10154 | if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL); |
| 10155 | |
| 10156 | ctxt = xmlNewParserCtxt(); |
| 10157 | if (ctxt == NULL) { |
| 10158 | return(NULL); |
| 10159 | } |
| 10160 | |
| 10161 | /* |
| 10162 | * Set-up the SAX context |
| 10163 | */ |
| 10164 | if (sax != NULL) { |
| 10165 | if (ctxt->sax != NULL) |
| 10166 | xmlFree(ctxt->sax); |
| 10167 | ctxt->sax = sax; |
Daniel Veillard | bf1e3d8 | 2003-08-14 23:57:26 +0000 | [diff] [blame] | 10168 | ctxt->userData = ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10169 | } |
| 10170 | |
| 10171 | /* |
| 10172 | * Ask the Entity resolver to load the damn thing |
| 10173 | */ |
| 10174 | |
| 10175 | if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL)) |
Daniel Veillard | c6abc3d | 2003-04-26 13:27:30 +0000 | [diff] [blame] | 10176 | input = ctxt->sax->resolveEntity(ctxt, ExternalID, SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10177 | if (input == NULL) { |
| 10178 | if (sax != NULL) ctxt->sax = NULL; |
| 10179 | xmlFreeParserCtxt(ctxt); |
| 10180 | return(NULL); |
| 10181 | } |
| 10182 | |
| 10183 | /* |
| 10184 | * plug some encoding conversion routines here. |
| 10185 | */ |
| 10186 | xmlPushInput(ctxt, input); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10187 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 10188 | enc = xmlDetectCharEncoding(ctxt->input->cur, 4); |
| 10189 | xmlSwitchEncoding(ctxt, enc); |
| 10190 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10191 | |
| 10192 | if (input->filename == NULL) |
Daniel Veillard | 85095e2 | 2003-04-23 13:56:44 +0000 | [diff] [blame] | 10193 | input->filename = (char *) xmlCanonicPath(SystemID); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10194 | input->line = 1; |
| 10195 | input->col = 1; |
| 10196 | input->base = ctxt->input->cur; |
| 10197 | input->cur = ctxt->input->cur; |
| 10198 | input->free = NULL; |
| 10199 | |
| 10200 | /* |
| 10201 | * let's parse that entity knowing it's an external subset. |
| 10202 | */ |
| 10203 | ctxt->inSubset = 2; |
| 10204 | ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 10205 | ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none", |
| 10206 | ExternalID, SystemID); |
| 10207 | xmlParseExternalSubset(ctxt, ExternalID, SystemID); |
| 10208 | |
| 10209 | if (ctxt->myDoc != NULL) { |
| 10210 | if (ctxt->wellFormed) { |
| 10211 | ret = ctxt->myDoc->extSubset; |
| 10212 | ctxt->myDoc->extSubset = NULL; |
Daniel Veillard | c557346 | 2003-04-25 16:43:49 +0000 | [diff] [blame] | 10213 | if (ret != NULL) { |
| 10214 | xmlNodePtr tmp; |
| 10215 | |
| 10216 | ret->doc = NULL; |
| 10217 | tmp = ret->children; |
| 10218 | while (tmp != NULL) { |
| 10219 | tmp->doc = NULL; |
| 10220 | tmp = tmp->next; |
| 10221 | } |
| 10222 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10223 | } else { |
| 10224 | ret = NULL; |
| 10225 | } |
| 10226 | xmlFreeDoc(ctxt->myDoc); |
| 10227 | ctxt->myDoc = NULL; |
| 10228 | } |
| 10229 | if (sax != NULL) ctxt->sax = NULL; |
| 10230 | xmlFreeParserCtxt(ctxt); |
| 10231 | |
| 10232 | return(ret); |
| 10233 | } |
| 10234 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10235 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10236 | /** |
| 10237 | * xmlParseDTD: |
| 10238 | * @ExternalID: a NAME* containing the External ID of the DTD |
| 10239 | * @SystemID: a NAME* containing the URL to the DTD |
| 10240 | * |
| 10241 | * Load and parse an external subset. |
| 10242 | * |
| 10243 | * Returns the resulting xmlDtdPtr or NULL in case of error. |
| 10244 | */ |
| 10245 | |
| 10246 | xmlDtdPtr |
| 10247 | xmlParseDTD(const xmlChar *ExternalID, const xmlChar *SystemID) { |
| 10248 | return(xmlSAXParseDTD(NULL, ExternalID, SystemID)); |
| 10249 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10250 | #endif /* LIBXML_VALID_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10251 | |
| 10252 | /************************************************************************ |
| 10253 | * * |
| 10254 | * Front ends when parsing an Entity * |
| 10255 | * * |
| 10256 | ************************************************************************/ |
| 10257 | |
| 10258 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10259 | * xmlParseCtxtExternalEntity: |
| 10260 | * @ctx: the existing parsing context |
| 10261 | * @URL: the URL for the entity to load |
| 10262 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10263 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10264 | * |
| 10265 | * Parse an external general entity within an existing parsing context |
| 10266 | * An external general parsed entity is well-formed if it matches the |
| 10267 | * production labeled extParsedEnt. |
| 10268 | * |
| 10269 | * [78] extParsedEnt ::= TextDecl? content |
| 10270 | * |
| 10271 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 10272 | * the parser error code otherwise |
| 10273 | */ |
| 10274 | |
| 10275 | int |
| 10276 | xmlParseCtxtExternalEntity(xmlParserCtxtPtr ctx, const xmlChar *URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10277 | const xmlChar *ID, xmlNodePtr *lst) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10278 | xmlParserCtxtPtr ctxt; |
| 10279 | xmlDocPtr newDoc; |
| 10280 | xmlSAXHandlerPtr oldsax = NULL; |
| 10281 | int ret = 0; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10282 | xmlChar start[4]; |
| 10283 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10284 | |
| 10285 | if (ctx->depth > 40) { |
| 10286 | return(XML_ERR_ENTITY_LOOP); |
| 10287 | } |
| 10288 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10289 | if (lst != NULL) |
| 10290 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10291 | if ((URL == NULL) && (ID == NULL)) |
| 10292 | return(-1); |
| 10293 | if (ctx->myDoc == NULL) /* @@ relax but check for dereferences */ |
| 10294 | return(-1); |
| 10295 | |
| 10296 | |
| 10297 | ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL); |
| 10298 | if (ctxt == NULL) return(-1); |
| 10299 | ctxt->userData = ctxt; |
Daniel Veillard | e2830f1 | 2003-01-08 17:47:49 +0000 | [diff] [blame] | 10300 | ctxt->_private = ctx->_private; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10301 | oldsax = ctxt->sax; |
| 10302 | ctxt->sax = ctx->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10303 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10304 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 10305 | if (newDoc == NULL) { |
| 10306 | xmlFreeParserCtxt(ctxt); |
| 10307 | return(-1); |
| 10308 | } |
| 10309 | if (ctx->myDoc != NULL) { |
| 10310 | newDoc->intSubset = ctx->myDoc->intSubset; |
| 10311 | newDoc->extSubset = ctx->myDoc->extSubset; |
| 10312 | } |
| 10313 | if (ctx->myDoc->URL != NULL) { |
| 10314 | newDoc->URL = xmlStrdup(ctx->myDoc->URL); |
| 10315 | } |
| 10316 | newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 10317 | if (newDoc->children == NULL) { |
| 10318 | ctxt->sax = oldsax; |
| 10319 | xmlFreeParserCtxt(ctxt); |
| 10320 | newDoc->intSubset = NULL; |
| 10321 | newDoc->extSubset = NULL; |
| 10322 | xmlFreeDoc(newDoc); |
| 10323 | return(-1); |
| 10324 | } |
| 10325 | nodePush(ctxt, newDoc->children); |
| 10326 | if (ctx->myDoc == NULL) { |
| 10327 | ctxt->myDoc = newDoc; |
| 10328 | } else { |
| 10329 | ctxt->myDoc = ctx->myDoc; |
| 10330 | newDoc->children->doc = ctx->myDoc; |
| 10331 | } |
| 10332 | |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10333 | /* |
| 10334 | * Get the 4 first bytes and decode the charset |
| 10335 | * if enc != XML_CHAR_ENCODING_NONE |
| 10336 | * plug some encoding conversion routines. |
| 10337 | */ |
| 10338 | GROW |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10339 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 10340 | start[0] = RAW; |
| 10341 | start[1] = NXT(1); |
| 10342 | start[2] = NXT(2); |
| 10343 | start[3] = NXT(3); |
| 10344 | enc = xmlDetectCharEncoding(start, 4); |
| 10345 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10346 | xmlSwitchEncoding(ctxt, enc); |
| 10347 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10348 | } |
| 10349 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10350 | /* |
| 10351 | * Parse a possible text declaration first |
| 10352 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10353 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10354 | xmlParseTextDecl(ctxt); |
| 10355 | } |
| 10356 | |
| 10357 | /* |
| 10358 | * Doing validity checking on chunk doesn't make sense |
| 10359 | */ |
| 10360 | ctxt->instate = XML_PARSER_CONTENT; |
| 10361 | ctxt->validate = ctx->validate; |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 10362 | ctxt->valid = ctx->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10363 | ctxt->loadsubset = ctx->loadsubset; |
| 10364 | ctxt->depth = ctx->depth + 1; |
| 10365 | ctxt->replaceEntities = ctx->replaceEntities; |
| 10366 | if (ctxt->validate) { |
| 10367 | ctxt->vctxt.error = ctx->vctxt.error; |
| 10368 | ctxt->vctxt.warning = ctx->vctxt.warning; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10369 | } else { |
| 10370 | ctxt->vctxt.error = NULL; |
| 10371 | ctxt->vctxt.warning = NULL; |
| 10372 | } |
Daniel Veillard | a9142e7 | 2001-06-19 11:07:54 +0000 | [diff] [blame] | 10373 | ctxt->vctxt.nodeTab = NULL; |
| 10374 | ctxt->vctxt.nodeNr = 0; |
| 10375 | ctxt->vctxt.nodeMax = 0; |
| 10376 | ctxt->vctxt.node = NULL; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 10377 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 10378 | ctxt->dict = ctx->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 10379 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 10380 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 10381 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 10382 | ctxt->dictNames = ctx->dictNames; |
| 10383 | ctxt->attsDefault = ctx->attsDefault; |
| 10384 | ctxt->attsSpecial = ctx->attsSpecial; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10385 | |
| 10386 | xmlParseContent(ctxt); |
| 10387 | |
Daniel Veillard | 5f8d1a3 | 2003-03-23 21:02:00 +0000 | [diff] [blame] | 10388 | ctx->validate = ctxt->validate; |
| 10389 | ctx->valid = ctxt->valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10390 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10391 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10392 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10393 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10394 | } |
| 10395 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10396 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10397 | } |
| 10398 | |
| 10399 | if (!ctxt->wellFormed) { |
| 10400 | if (ctxt->errNo == 0) |
| 10401 | ret = 1; |
| 10402 | else |
| 10403 | ret = ctxt->errNo; |
| 10404 | } else { |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10405 | if (lst != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10406 | xmlNodePtr cur; |
| 10407 | |
| 10408 | /* |
| 10409 | * Return the newly created nodeset after unlinking it from |
| 10410 | * they pseudo parent. |
| 10411 | */ |
| 10412 | cur = newDoc->children->children; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10413 | *lst = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10414 | while (cur != NULL) { |
| 10415 | cur->parent = NULL; |
| 10416 | cur = cur->next; |
| 10417 | } |
| 10418 | newDoc->children->children = NULL; |
| 10419 | } |
| 10420 | ret = 0; |
| 10421 | } |
| 10422 | ctxt->sax = oldsax; |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 10423 | ctxt->dict = NULL; |
| 10424 | ctxt->attsDefault = NULL; |
| 10425 | ctxt->attsSpecial = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10426 | xmlFreeParserCtxt(ctxt); |
| 10427 | newDoc->intSubset = NULL; |
| 10428 | newDoc->extSubset = NULL; |
| 10429 | xmlFreeDoc(newDoc); |
| 10430 | |
| 10431 | return(ret); |
| 10432 | } |
| 10433 | |
| 10434 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 10435 | * xmlParseExternalEntityPrivate: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10436 | * @doc: the document the chunk pertains to |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10437 | * @oldctxt: the previous parser context if available |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10438 | * @sax: the SAX handler bloc (possibly NULL) |
| 10439 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 10440 | * @depth: Used for loop detection, use 0 |
| 10441 | * @URL: the URL for the entity to load |
| 10442 | * @ID: the System ID for the entity to load |
| 10443 | * @list: the return value for the set of parsed nodes |
| 10444 | * |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 10445 | * Private version of xmlParseExternalEntity() |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10446 | * |
| 10447 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 10448 | * the parser error code otherwise |
| 10449 | */ |
| 10450 | |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10451 | static xmlParserErrors |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10452 | xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt, |
| 10453 | xmlSAXHandlerPtr sax, |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 10454 | void *user_data, int depth, const xmlChar *URL, |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10455 | const xmlChar *ID, xmlNodePtr *list) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10456 | xmlParserCtxtPtr ctxt; |
| 10457 | xmlDocPtr newDoc; |
| 10458 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10459 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10460 | xmlChar start[4]; |
| 10461 | xmlCharEncoding enc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10462 | |
| 10463 | if (depth > 40) { |
| 10464 | return(XML_ERR_ENTITY_LOOP); |
| 10465 | } |
| 10466 | |
| 10467 | |
| 10468 | |
| 10469 | if (list != NULL) |
| 10470 | *list = NULL; |
| 10471 | if ((URL == NULL) && (ID == NULL)) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10472 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10473 | if (doc == NULL) /* @@ relax but check for dereferences */ |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10474 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10475 | |
| 10476 | |
| 10477 | ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL); |
William M. Brack | b670e2e | 2003-09-27 01:05:55 +0000 | [diff] [blame] | 10478 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10479 | ctxt->userData = ctxt; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10480 | if (oldctxt != NULL) { |
| 10481 | ctxt->_private = oldctxt->_private; |
| 10482 | ctxt->loadsubset = oldctxt->loadsubset; |
| 10483 | ctxt->validate = oldctxt->validate; |
| 10484 | ctxt->external = oldctxt->external; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 10485 | ctxt->record_info = oldctxt->record_info; |
| 10486 | ctxt->node_seq.maximum = oldctxt->node_seq.maximum; |
| 10487 | ctxt->node_seq.length = oldctxt->node_seq.length; |
| 10488 | ctxt->node_seq.buffer = oldctxt->node_seq.buffer; |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10489 | } else { |
| 10490 | /* |
| 10491 | * Doing validity checking on chunk without context |
| 10492 | * doesn't make sense |
| 10493 | */ |
| 10494 | ctxt->_private = NULL; |
| 10495 | ctxt->validate = 0; |
| 10496 | ctxt->external = 2; |
| 10497 | ctxt->loadsubset = 0; |
| 10498 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10499 | if (sax != NULL) { |
| 10500 | oldsax = ctxt->sax; |
| 10501 | ctxt->sax = sax; |
| 10502 | if (user_data != NULL) |
| 10503 | ctxt->userData = user_data; |
| 10504 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10505 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10506 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 10507 | if (newDoc == NULL) { |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 10508 | ctxt->node_seq.maximum = 0; |
| 10509 | ctxt->node_seq.length = 0; |
| 10510 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10511 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10512 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10513 | } |
| 10514 | if (doc != NULL) { |
| 10515 | newDoc->intSubset = doc->intSubset; |
| 10516 | newDoc->extSubset = doc->extSubset; |
| 10517 | } |
| 10518 | if (doc->URL != NULL) { |
| 10519 | newDoc->URL = xmlStrdup(doc->URL); |
| 10520 | } |
| 10521 | newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 10522 | if (newDoc->children == NULL) { |
| 10523 | if (sax != NULL) |
| 10524 | ctxt->sax = oldsax; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 10525 | ctxt->node_seq.maximum = 0; |
| 10526 | ctxt->node_seq.length = 0; |
| 10527 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10528 | xmlFreeParserCtxt(ctxt); |
| 10529 | newDoc->intSubset = NULL; |
| 10530 | newDoc->extSubset = NULL; |
| 10531 | xmlFreeDoc(newDoc); |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10532 | return(XML_ERR_INTERNAL_ERROR); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10533 | } |
| 10534 | nodePush(ctxt, newDoc->children); |
| 10535 | if (doc == NULL) { |
| 10536 | ctxt->myDoc = newDoc; |
| 10537 | } else { |
| 10538 | ctxt->myDoc = doc; |
| 10539 | newDoc->children->doc = doc; |
| 10540 | } |
| 10541 | |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10542 | /* |
| 10543 | * Get the 4 first bytes and decode the charset |
| 10544 | * if enc != XML_CHAR_ENCODING_NONE |
| 10545 | * plug some encoding conversion routines. |
| 10546 | */ |
| 10547 | GROW; |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 10548 | if ((ctxt->input->end - ctxt->input->cur) >= 4) { |
| 10549 | start[0] = RAW; |
| 10550 | start[1] = NXT(1); |
| 10551 | start[2] = NXT(2); |
| 10552 | start[3] = NXT(3); |
| 10553 | enc = xmlDetectCharEncoding(start, 4); |
| 10554 | if (enc != XML_CHAR_ENCODING_NONE) { |
| 10555 | xmlSwitchEncoding(ctxt, enc); |
| 10556 | } |
Daniel Veillard | 87a764e | 2001-06-20 17:41:10 +0000 | [diff] [blame] | 10557 | } |
| 10558 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10559 | /* |
| 10560 | * Parse a possible text declaration first |
| 10561 | */ |
Daniel Veillard | a07050d | 2003-10-19 14:46:32 +0000 | [diff] [blame] | 10562 | if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10563 | xmlParseTextDecl(ctxt); |
| 10564 | } |
| 10565 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10566 | ctxt->instate = XML_PARSER_CONTENT; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10567 | ctxt->depth = depth; |
| 10568 | |
| 10569 | xmlParseContent(ctxt); |
| 10570 | |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10571 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10572 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 10573 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10574 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10575 | } |
| 10576 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10577 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10578 | } |
| 10579 | |
| 10580 | if (!ctxt->wellFormed) { |
| 10581 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10582 | ret = XML_ERR_INTERNAL_ERROR; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10583 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 10584 | ret = (xmlParserErrors)ctxt->errNo; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10585 | } else { |
| 10586 | if (list != NULL) { |
| 10587 | xmlNodePtr cur; |
| 10588 | |
| 10589 | /* |
| 10590 | * Return the newly created nodeset after unlinking it from |
| 10591 | * they pseudo parent. |
| 10592 | */ |
| 10593 | cur = newDoc->children->children; |
| 10594 | *list = cur; |
| 10595 | while (cur != NULL) { |
| 10596 | cur->parent = NULL; |
| 10597 | cur = cur->next; |
| 10598 | } |
| 10599 | newDoc->children->children = NULL; |
| 10600 | } |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10601 | ret = XML_ERR_OK; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10602 | } |
| 10603 | if (sax != NULL) |
| 10604 | ctxt->sax = oldsax; |
Daniel Veillard | 0046c0f | 2003-02-23 13:52:30 +0000 | [diff] [blame] | 10605 | oldctxt->node_seq.maximum = ctxt->node_seq.maximum; |
| 10606 | oldctxt->node_seq.length = ctxt->node_seq.length; |
| 10607 | oldctxt->node_seq.buffer = ctxt->node_seq.buffer; |
Daniel Veillard | 44e1dd0 | 2003-02-21 23:23:28 +0000 | [diff] [blame] | 10608 | ctxt->node_seq.maximum = 0; |
| 10609 | ctxt->node_seq.length = 0; |
| 10610 | ctxt->node_seq.buffer = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10611 | xmlFreeParserCtxt(ctxt); |
| 10612 | newDoc->intSubset = NULL; |
| 10613 | newDoc->extSubset = NULL; |
| 10614 | xmlFreeDoc(newDoc); |
| 10615 | |
| 10616 | return(ret); |
| 10617 | } |
| 10618 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10619 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10620 | /** |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 10621 | * xmlParseExternalEntity: |
| 10622 | * @doc: the document the chunk pertains to |
| 10623 | * @sax: the SAX handler bloc (possibly NULL) |
| 10624 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 10625 | * @depth: Used for loop detection, use 0 |
| 10626 | * @URL: the URL for the entity to load |
| 10627 | * @ID: the System ID for the entity to load |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10628 | * @lst: the return value for the set of parsed nodes |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 10629 | * |
| 10630 | * Parse an external general entity |
| 10631 | * An external general parsed entity is well-formed if it matches the |
| 10632 | * production labeled extParsedEnt. |
| 10633 | * |
| 10634 | * [78] extParsedEnt ::= TextDecl? content |
| 10635 | * |
| 10636 | * Returns 0 if the entity is well formed, -1 in case of args problem and |
| 10637 | * the parser error code otherwise |
| 10638 | */ |
| 10639 | |
| 10640 | int |
| 10641 | xmlParseExternalEntity(xmlDocPtr doc, xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10642 | int depth, const xmlChar *URL, const xmlChar *ID, xmlNodePtr *lst) { |
Daniel Veillard | a97a19b | 2001-05-20 13:19:52 +0000 | [diff] [blame] | 10643 | return(xmlParseExternalEntityPrivate(doc, NULL, sax, user_data, depth, URL, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10644 | ID, lst)); |
Daniel Veillard | 257d910 | 2001-05-08 10:41:44 +0000 | [diff] [blame] | 10645 | } |
| 10646 | |
| 10647 | /** |
Daniel Veillard | e020c3a | 2001-03-21 18:06:15 +0000 | [diff] [blame] | 10648 | * xmlParseBalancedChunkMemory: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10649 | * @doc: the document the chunk pertains to |
| 10650 | * @sax: the SAX handler bloc (possibly NULL) |
| 10651 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 10652 | * @depth: Used for loop detection, use 0 |
| 10653 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10654 | * @lst: the return value for the set of parsed nodes |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 10655 | * |
| 10656 | * Parse a well-balanced chunk of an XML document |
| 10657 | * called by the parser |
| 10658 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 10659 | * the content production in the XML grammar: |
| 10660 | * |
| 10661 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 10662 | * |
| 10663 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 10664 | * the parser error code otherwise |
| 10665 | */ |
| 10666 | |
| 10667 | int |
| 10668 | xmlParseBalancedChunkMemory(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 10669 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst) { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 10670 | return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data, |
| 10671 | depth, string, lst, 0 ); |
| 10672 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 10673 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 10674 | |
| 10675 | /** |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10676 | * xmlParseBalancedChunkMemoryInternal: |
| 10677 | * @oldctxt: the existing parsing context |
| 10678 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 10679 | * @user_data: the user data field for the parser context |
| 10680 | * @lst: the return value for the set of parsed nodes |
| 10681 | * |
| 10682 | * |
| 10683 | * Parse a well-balanced chunk of an XML document |
| 10684 | * called by the parser |
| 10685 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 10686 | * the content production in the XML grammar: |
| 10687 | * |
| 10688 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 10689 | * |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10690 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 10691 | * error code otherwise |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10692 | * |
| 10693 | * In case recover is set to 1, the nodelist will not be empty even if |
| 10694 | * the parsed chunk is not well balanced. |
| 10695 | */ |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10696 | static xmlParserErrors |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10697 | xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt, |
| 10698 | const xmlChar *string, void *user_data, xmlNodePtr *lst) { |
| 10699 | xmlParserCtxtPtr ctxt; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10700 | xmlDocPtr newDoc = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10701 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10702 | xmlNodePtr content = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10703 | int size; |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10704 | xmlParserErrors ret = XML_ERR_OK; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10705 | |
| 10706 | if (oldctxt->depth > 40) { |
| 10707 | return(XML_ERR_ENTITY_LOOP); |
| 10708 | } |
| 10709 | |
| 10710 | |
| 10711 | if (lst != NULL) |
| 10712 | *lst = NULL; |
| 10713 | if (string == NULL) |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 10714 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10715 | |
| 10716 | size = xmlStrlen(string); |
| 10717 | |
| 10718 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 10719 | if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10720 | if (user_data != NULL) |
| 10721 | ctxt->userData = user_data; |
| 10722 | else |
| 10723 | ctxt->userData = ctxt; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 10724 | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
| 10725 | ctxt->dict = oldctxt->dict; |
Daniel Veillard | fd343dc | 2003-10-31 10:55:22 +0000 | [diff] [blame] | 10726 | ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3); |
| 10727 | ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5); |
| 10728 | ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10729 | |
| 10730 | oldsax = ctxt->sax; |
| 10731 | ctxt->sax = oldctxt->sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10732 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 10733 | ctxt->replaceEntities = oldctxt->replaceEntities; |
| 10734 | ctxt->options = oldctxt->options; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 10735 | |
Daniel Veillard | e1ca503 | 2002-12-09 14:13:43 +0000 | [diff] [blame] | 10736 | ctxt->_private = oldctxt->_private; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10737 | if (oldctxt->myDoc == NULL) { |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10738 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 10739 | if (newDoc == NULL) { |
| 10740 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 10741 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10742 | xmlFreeParserCtxt(ctxt); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 10743 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10744 | } |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10745 | ctxt->myDoc = newDoc; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10746 | } else { |
| 10747 | ctxt->myDoc = oldctxt->myDoc; |
| 10748 | content = ctxt->myDoc->children; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10749 | } |
Daniel Veillard | 9bc5310 | 2002-11-25 13:20:04 +0000 | [diff] [blame] | 10750 | ctxt->myDoc->children = xmlNewDocNode(ctxt->myDoc, NULL, |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10751 | BAD_CAST "pseudoroot", NULL); |
| 10752 | if (ctxt->myDoc->children == NULL) { |
| 10753 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 10754 | ctxt->dict = NULL; |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10755 | xmlFreeParserCtxt(ctxt); |
| 10756 | if (newDoc != NULL) |
| 10757 | xmlFreeDoc(newDoc); |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 10758 | return(XML_ERR_INTERNAL_ERROR); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10759 | } |
| 10760 | nodePush(ctxt, ctxt->myDoc->children); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10761 | ctxt->instate = XML_PARSER_CONTENT; |
| 10762 | ctxt->depth = oldctxt->depth + 1; |
| 10763 | |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10764 | ctxt->validate = 0; |
| 10765 | ctxt->loadsubset = oldctxt->loadsubset; |
Daniel Veillard | ef8dd7b | 2003-03-23 12:02:56 +0000 | [diff] [blame] | 10766 | if ((oldctxt->validate) || (oldctxt->replaceEntities != 0)) { |
| 10767 | /* |
| 10768 | * ID/IDREF registration will be done in xmlValidateElement below |
| 10769 | */ |
| 10770 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 10771 | } |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 10772 | ctxt->dictNames = oldctxt->dictNames; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 10773 | ctxt->attsDefault = oldctxt->attsDefault; |
| 10774 | ctxt->attsSpecial = oldctxt->attsSpecial; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10775 | |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10776 | xmlParseContent(ctxt); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10777 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10778 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10779 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10780 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10781 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10782 | if (ctxt->node != ctxt->myDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 10783 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10784 | } |
| 10785 | |
| 10786 | if (!ctxt->wellFormed) { |
| 10787 | if (ctxt->errNo == 0) |
Daniel Veillard | 7d51575 | 2003-09-26 19:12:37 +0000 | [diff] [blame] | 10788 | ret = XML_ERR_INTERNAL_ERROR; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10789 | else |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 10790 | ret = (xmlParserErrors)ctxt->errNo; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10791 | } else { |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 10792 | ret = XML_ERR_OK; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10793 | } |
| 10794 | |
William M. Brack | 7b9154b | 2003-09-27 19:23:50 +0000 | [diff] [blame] | 10795 | if ((lst != NULL) && (ret == XML_ERR_OK)) { |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10796 | xmlNodePtr cur; |
| 10797 | |
| 10798 | /* |
| 10799 | * Return the newly created nodeset after unlinking it from |
| 10800 | * they pseudo parent. |
| 10801 | */ |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10802 | cur = ctxt->myDoc->children->children; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10803 | *lst = cur; |
| 10804 | while (cur != NULL) { |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10805 | #ifdef LIBXML_VALID_ENABLED |
Daniel Veillard | 8d58904 | 2003-02-04 15:07:21 +0000 | [diff] [blame] | 10806 | if (oldctxt->validate && oldctxt->wellFormed && |
| 10807 | oldctxt->myDoc && oldctxt->myDoc->intSubset) { |
| 10808 | oldctxt->valid &= xmlValidateElement(&oldctxt->vctxt, |
| 10809 | oldctxt->myDoc, cur); |
| 10810 | } |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 10811 | #endif /* LIBXML_VALID_ENABLED */ |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10812 | cur->parent = NULL; |
| 10813 | cur = cur->next; |
| 10814 | } |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10815 | ctxt->myDoc->children->children = NULL; |
| 10816 | } |
| 10817 | if (ctxt->myDoc != NULL) { |
| 10818 | xmlFreeNode(ctxt->myDoc->children); |
| 10819 | ctxt->myDoc->children = content; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10820 | } |
| 10821 | |
| 10822 | ctxt->sax = oldsax; |
Daniel Veillard | 9f7eb0b | 2003-09-17 10:26:25 +0000 | [diff] [blame] | 10823 | ctxt->dict = NULL; |
Daniel Veillard | 95d2d5b | 2003-10-27 14:54:49 +0000 | [diff] [blame] | 10824 | ctxt->attsDefault = NULL; |
| 10825 | ctxt->attsSpecial = NULL; |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10826 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | 68e9e74 | 2002-11-16 15:35:11 +0000 | [diff] [blame] | 10827 | if (newDoc != NULL) |
| 10828 | xmlFreeDoc(newDoc); |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 10829 | |
| 10830 | return(ret); |
| 10831 | } |
| 10832 | |
Daniel Veillard | 29b1748 | 2004-08-16 00:39:03 +0000 | [diff] [blame] | 10833 | /** |
| 10834 | * xmlParseInNodeContext: |
| 10835 | * @node: the context node |
| 10836 | * @data: the input string |
| 10837 | * @datalen: the input string length in bytes |
| 10838 | * @options: a combination of xmlParserOption |
| 10839 | * @lst: the return value for the set of parsed nodes |
| 10840 | * |
| 10841 | * Parse a well-balanced chunk of an XML document |
| 10842 | * within the context (DTD, namespaces, etc ...) of the given node. |
| 10843 | * |
| 10844 | * The allowed sequence for the data is a Well Balanced Chunk defined by |
| 10845 | * the content production in the XML grammar: |
| 10846 | * |
| 10847 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 10848 | * |
| 10849 | * Returns XML_ERR_OK if the chunk is well balanced, and the parser |
| 10850 | * error code otherwise |
| 10851 | */ |
| 10852 | xmlParserErrors |
| 10853 | xmlParseInNodeContext(xmlNodePtr node, const char *data, int datalen, |
| 10854 | int options, xmlNodePtr *lst) { |
| 10855 | #ifdef SAX2 |
| 10856 | xmlParserCtxtPtr ctxt; |
| 10857 | xmlDocPtr doc = NULL; |
| 10858 | xmlNodePtr fake, cur; |
| 10859 | int nsnr = 0; |
| 10860 | |
| 10861 | xmlParserErrors ret = XML_ERR_OK; |
| 10862 | |
| 10863 | /* |
| 10864 | * check all input parameters, grab the document |
| 10865 | */ |
| 10866 | if ((lst == NULL) || (node == NULL) || (data == NULL) || (datalen < 0)) |
| 10867 | return(XML_ERR_INTERNAL_ERROR); |
| 10868 | switch (node->type) { |
| 10869 | case XML_ELEMENT_NODE: |
| 10870 | case XML_ATTRIBUTE_NODE: |
| 10871 | case XML_TEXT_NODE: |
| 10872 | case XML_CDATA_SECTION_NODE: |
| 10873 | case XML_ENTITY_REF_NODE: |
| 10874 | case XML_PI_NODE: |
| 10875 | case XML_COMMENT_NODE: |
| 10876 | case XML_DOCUMENT_NODE: |
| 10877 | case XML_HTML_DOCUMENT_NODE: |
| 10878 | break; |
| 10879 | default: |
| 10880 | return(XML_ERR_INTERNAL_ERROR); |
| 10881 | |
| 10882 | } |
| 10883 | while ((node != NULL) && (node->type != XML_ELEMENT_NODE) && |
| 10884 | (node->type != XML_DOCUMENT_NODE) && |
| 10885 | (node->type != XML_HTML_DOCUMENT_NODE)) |
| 10886 | node = node->parent; |
| 10887 | if (node == NULL) |
| 10888 | return(XML_ERR_INTERNAL_ERROR); |
| 10889 | if (node->type == XML_ELEMENT_NODE) |
| 10890 | doc = node->doc; |
| 10891 | else |
| 10892 | doc = (xmlDocPtr) node; |
| 10893 | if (doc == NULL) |
| 10894 | return(XML_ERR_INTERNAL_ERROR); |
| 10895 | |
| 10896 | /* |
| 10897 | * allocate a context and set-up everything not related to the |
| 10898 | * node position in the tree |
| 10899 | */ |
| 10900 | if (doc->type == XML_DOCUMENT_NODE) |
| 10901 | ctxt = xmlCreateMemoryParserCtxt((char *) data, datalen); |
| 10902 | #ifdef LIBXML_HTML_ENABLED |
| 10903 | else if (doc->type == XML_HTML_DOCUMENT_NODE) |
| 10904 | ctxt = htmlCreateMemoryParserCtxt((char *) data, datalen); |
| 10905 | #endif |
| 10906 | else |
| 10907 | return(XML_ERR_INTERNAL_ERROR); |
| 10908 | |
| 10909 | if (ctxt == NULL) |
| 10910 | return(XML_ERR_NO_MEMORY); |
| 10911 | fake = xmlNewComment(NULL); |
| 10912 | if (fake == NULL) { |
| 10913 | xmlFreeParserCtxt(ctxt); |
| 10914 | return(XML_ERR_NO_MEMORY); |
| 10915 | } |
| 10916 | xmlAddChild(node, fake); |
| 10917 | |
| 10918 | xmlCtxtUseOptions(ctxt, options); |
| 10919 | if (doc->dict != NULL) { |
| 10920 | if (ctxt->dict != NULL) |
| 10921 | xmlDictFree(ctxt->dict); |
| 10922 | ctxt->dict = doc->dict; |
| 10923 | } |
| 10924 | xmlDetectSAX2(ctxt); |
| 10925 | ctxt->myDoc = doc; |
| 10926 | |
| 10927 | if (node->type == XML_ELEMENT_NODE) { |
| 10928 | nodePush(ctxt, node); |
| 10929 | /* |
| 10930 | * initialize the SAX2 namespaces stack |
| 10931 | */ |
| 10932 | cur = node; |
| 10933 | while ((cur != NULL) && (cur->type == XML_ELEMENT_NODE)) { |
| 10934 | xmlNsPtr ns = cur->nsDef; |
| 10935 | const xmlChar *iprefix, *ihref; |
| 10936 | |
| 10937 | while (ns != NULL) { |
| 10938 | if (ctxt->dict) { |
| 10939 | iprefix = xmlDictLookup(ctxt->dict, ns->prefix, -1); |
| 10940 | ihref = xmlDictLookup(ctxt->dict, ns->href, -1); |
| 10941 | } else { |
| 10942 | iprefix = ns->prefix; |
| 10943 | ihref = ns->href; |
| 10944 | } |
| 10945 | |
| 10946 | if (xmlGetNamespace(ctxt, iprefix) == NULL) { |
| 10947 | nsPush(ctxt, iprefix, ihref); |
| 10948 | nsnr++; |
| 10949 | } |
| 10950 | ns = ns->next; |
| 10951 | } |
| 10952 | cur = cur->parent; |
| 10953 | } |
| 10954 | ctxt->instate = XML_PARSER_CONTENT; |
| 10955 | } |
| 10956 | |
| 10957 | if ((ctxt->validate) || (ctxt->replaceEntities != 0)) { |
| 10958 | /* |
| 10959 | * ID/IDREF registration will be done in xmlValidateElement below |
| 10960 | */ |
| 10961 | ctxt->loadsubset |= XML_SKIP_IDS; |
| 10962 | } |
| 10963 | |
| 10964 | xmlParseContent(ctxt); |
| 10965 | nsPop(ctxt, nsnr); |
| 10966 | if ((RAW == '<') && (NXT(1) == '/')) { |
| 10967 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 10968 | } else if (RAW != 0) { |
| 10969 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
| 10970 | } |
| 10971 | if ((ctxt->node != NULL) && (ctxt->node != node)) { |
| 10972 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
| 10973 | ctxt->wellFormed = 0; |
| 10974 | } |
| 10975 | |
| 10976 | if (!ctxt->wellFormed) { |
| 10977 | if (ctxt->errNo == 0) |
| 10978 | ret = XML_ERR_INTERNAL_ERROR; |
| 10979 | else |
| 10980 | ret = (xmlParserErrors)ctxt->errNo; |
| 10981 | } else { |
| 10982 | ret = XML_ERR_OK; |
| 10983 | } |
| 10984 | |
| 10985 | /* |
| 10986 | * Return the newly created nodeset after unlinking it from |
| 10987 | * the pseudo sibling. |
| 10988 | */ |
| 10989 | |
| 10990 | cur = fake->next; |
| 10991 | fake->next = NULL; |
| 10992 | node->last = fake; |
| 10993 | |
| 10994 | if (cur != NULL) { |
| 10995 | cur->prev = NULL; |
| 10996 | } |
| 10997 | |
| 10998 | *lst = cur; |
| 10999 | |
| 11000 | while (cur != NULL) { |
| 11001 | cur->parent = NULL; |
| 11002 | cur = cur->next; |
| 11003 | } |
| 11004 | |
| 11005 | xmlUnlinkNode(fake); |
| 11006 | xmlFreeNode(fake); |
| 11007 | |
| 11008 | |
| 11009 | if (ret != XML_ERR_OK) { |
| 11010 | xmlFreeNodeList(*lst); |
| 11011 | *lst = NULL; |
| 11012 | } |
| 11013 | |
| 11014 | ctxt->dict = NULL; |
| 11015 | xmlFreeParserCtxt(ctxt); |
| 11016 | |
| 11017 | return(ret); |
| 11018 | #else /* !SAX2 */ |
| 11019 | return(XML_ERR_INTERNAL_ERROR); |
| 11020 | #endif |
| 11021 | } |
| 11022 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11023 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 328f48c | 2002-11-15 15:24:34 +0000 | [diff] [blame] | 11024 | /** |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 11025 | * xmlParseBalancedChunkMemoryRecover: |
| 11026 | * @doc: the document the chunk pertains to |
| 11027 | * @sax: the SAX handler bloc (possibly NULL) |
| 11028 | * @user_data: The user data returned on SAX callbacks (possibly NULL) |
| 11029 | * @depth: Used for loop detection, use 0 |
| 11030 | * @string: the input string in UTF8 or ISO-Latin (zero terminated) |
| 11031 | * @lst: the return value for the set of parsed nodes |
| 11032 | * @recover: return nodes even if the data is broken (use 0) |
| 11033 | * |
| 11034 | * |
| 11035 | * Parse a well-balanced chunk of an XML document |
| 11036 | * called by the parser |
| 11037 | * The allowed sequence for the Well Balanced Chunk is the one defined by |
| 11038 | * the content production in the XML grammar: |
| 11039 | * |
| 11040 | * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)* |
| 11041 | * |
| 11042 | * Returns 0 if the chunk is well balanced, -1 in case of args problem and |
| 11043 | * the parser error code otherwise |
| 11044 | * |
| 11045 | * In case recover is set to 1, the nodelist will not be empty even if |
| 11046 | * the parsed chunk is not well balanced. |
| 11047 | */ |
| 11048 | int |
| 11049 | xmlParseBalancedChunkMemoryRecover(xmlDocPtr doc, xmlSAXHandlerPtr sax, |
| 11050 | void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst, |
| 11051 | int recover) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11052 | xmlParserCtxtPtr ctxt; |
| 11053 | xmlDocPtr newDoc; |
| 11054 | xmlSAXHandlerPtr oldsax = NULL; |
Daniel Veillard | 935494a | 2002-10-22 14:22:46 +0000 | [diff] [blame] | 11055 | xmlNodePtr content; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11056 | int size; |
| 11057 | int ret = 0; |
| 11058 | |
| 11059 | if (depth > 40) { |
| 11060 | return(XML_ERR_ENTITY_LOOP); |
| 11061 | } |
| 11062 | |
| 11063 | |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 11064 | if (lst != NULL) |
| 11065 | *lst = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11066 | if (string == NULL) |
| 11067 | return(-1); |
| 11068 | |
| 11069 | size = xmlStrlen(string); |
| 11070 | |
| 11071 | ctxt = xmlCreateMemoryParserCtxt((char *) string, size); |
| 11072 | if (ctxt == NULL) return(-1); |
| 11073 | ctxt->userData = ctxt; |
| 11074 | if (sax != NULL) { |
| 11075 | oldsax = ctxt->sax; |
| 11076 | ctxt->sax = sax; |
| 11077 | if (user_data != NULL) |
| 11078 | ctxt->userData = user_data; |
| 11079 | } |
| 11080 | newDoc = xmlNewDoc(BAD_CAST "1.0"); |
| 11081 | if (newDoc == NULL) { |
| 11082 | xmlFreeParserCtxt(ctxt); |
| 11083 | return(-1); |
| 11084 | } |
| 11085 | if (doc != NULL) { |
| 11086 | newDoc->intSubset = doc->intSubset; |
| 11087 | newDoc->extSubset = doc->extSubset; |
| 11088 | } |
| 11089 | newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL); |
| 11090 | if (newDoc->children == NULL) { |
| 11091 | if (sax != NULL) |
| 11092 | ctxt->sax = oldsax; |
| 11093 | xmlFreeParserCtxt(ctxt); |
| 11094 | newDoc->intSubset = NULL; |
| 11095 | newDoc->extSubset = NULL; |
| 11096 | xmlFreeDoc(newDoc); |
| 11097 | return(-1); |
| 11098 | } |
| 11099 | nodePush(ctxt, newDoc->children); |
| 11100 | if (doc == NULL) { |
| 11101 | ctxt->myDoc = newDoc; |
| 11102 | } else { |
Daniel Veillard | 42766c0 | 2002-08-22 20:52:17 +0000 | [diff] [blame] | 11103 | ctxt->myDoc = newDoc; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11104 | newDoc->children->doc = doc; |
| 11105 | } |
| 11106 | ctxt->instate = XML_PARSER_CONTENT; |
| 11107 | ctxt->depth = depth; |
| 11108 | |
| 11109 | /* |
| 11110 | * Doing validity checking on chunk doesn't make sense |
| 11111 | */ |
| 11112 | ctxt->validate = 0; |
| 11113 | ctxt->loadsubset = 0; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11114 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11115 | |
Daniel Veillard | b39bc39 | 2002-10-26 19:29:51 +0000 | [diff] [blame] | 11116 | if ( doc != NULL ){ |
| 11117 | content = doc->children; |
| 11118 | doc->children = NULL; |
| 11119 | xmlParseContent(ctxt); |
| 11120 | doc->children = content; |
| 11121 | } |
| 11122 | else { |
| 11123 | xmlParseContent(ctxt); |
| 11124 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11125 | if ((RAW == '<') && (NXT(1) == '/')) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11126 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11127 | } else if (RAW != 0) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11128 | xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11129 | } |
| 11130 | if (ctxt->node != newDoc->children) { |
Daniel Veillard | 1afc9f3 | 2003-09-13 12:44:05 +0000 | [diff] [blame] | 11131 | xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11132 | } |
| 11133 | |
| 11134 | if (!ctxt->wellFormed) { |
| 11135 | if (ctxt->errNo == 0) |
| 11136 | ret = 1; |
| 11137 | else |
| 11138 | ret = ctxt->errNo; |
| 11139 | } else { |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 11140 | ret = 0; |
| 11141 | } |
| 11142 | |
| 11143 | if (lst != NULL && (ret == 0 || recover == 1)) { |
| 11144 | xmlNodePtr cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11145 | |
| 11146 | /* |
| 11147 | * Return the newly created nodeset after unlinking it from |
| 11148 | * they pseudo parent. |
| 11149 | */ |
| 11150 | cur = newDoc->children->children; |
Daniel Veillard | cda9692 | 2001-08-21 10:56:31 +0000 | [diff] [blame] | 11151 | *lst = cur; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11152 | while (cur != NULL) { |
| 11153 | cur->parent = NULL; |
| 11154 | cur = cur->next; |
| 11155 | } |
| 11156 | newDoc->children->children = NULL; |
| 11157 | } |
Daniel Veillard | 58e44c9 | 2002-08-02 22:19:49 +0000 | [diff] [blame] | 11158 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11159 | if (sax != NULL) |
| 11160 | ctxt->sax = oldsax; |
| 11161 | xmlFreeParserCtxt(ctxt); |
| 11162 | newDoc->intSubset = NULL; |
| 11163 | newDoc->extSubset = NULL; |
| 11164 | xmlFreeDoc(newDoc); |
| 11165 | |
| 11166 | return(ret); |
| 11167 | } |
| 11168 | |
| 11169 | /** |
| 11170 | * xmlSAXParseEntity: |
| 11171 | * @sax: the SAX handler block |
| 11172 | * @filename: the filename |
| 11173 | * |
| 11174 | * parse an XML external entity out of context and build a tree. |
| 11175 | * It use the given SAX function block to handle the parsing callback. |
| 11176 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 11177 | * |
| 11178 | * [78] extParsedEnt ::= TextDecl? content |
| 11179 | * |
| 11180 | * This correspond to a "Well Balanced" chunk |
| 11181 | * |
| 11182 | * Returns the resulting document tree |
| 11183 | */ |
| 11184 | |
| 11185 | xmlDocPtr |
| 11186 | xmlSAXParseEntity(xmlSAXHandlerPtr sax, const char *filename) { |
| 11187 | xmlDocPtr ret; |
| 11188 | xmlParserCtxtPtr ctxt; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11189 | |
| 11190 | ctxt = xmlCreateFileParserCtxt(filename); |
| 11191 | if (ctxt == NULL) { |
| 11192 | return(NULL); |
| 11193 | } |
| 11194 | if (sax != NULL) { |
| 11195 | if (ctxt->sax != NULL) |
| 11196 | xmlFree(ctxt->sax); |
| 11197 | ctxt->sax = sax; |
| 11198 | ctxt->userData = NULL; |
| 11199 | } |
| 11200 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11201 | xmlParseExtParsedEnt(ctxt); |
| 11202 | |
| 11203 | if (ctxt->wellFormed) |
| 11204 | ret = ctxt->myDoc; |
| 11205 | else { |
| 11206 | ret = NULL; |
| 11207 | xmlFreeDoc(ctxt->myDoc); |
| 11208 | ctxt->myDoc = NULL; |
| 11209 | } |
| 11210 | if (sax != NULL) |
| 11211 | ctxt->sax = NULL; |
| 11212 | xmlFreeParserCtxt(ctxt); |
| 11213 | |
| 11214 | return(ret); |
| 11215 | } |
| 11216 | |
| 11217 | /** |
| 11218 | * xmlParseEntity: |
| 11219 | * @filename: the filename |
| 11220 | * |
| 11221 | * parse an XML external entity out of context and build a tree. |
| 11222 | * |
| 11223 | * [78] extParsedEnt ::= TextDecl? content |
| 11224 | * |
| 11225 | * This correspond to a "Well Balanced" chunk |
| 11226 | * |
| 11227 | * Returns the resulting document tree |
| 11228 | */ |
| 11229 | |
| 11230 | xmlDocPtr |
| 11231 | xmlParseEntity(const char *filename) { |
| 11232 | return(xmlSAXParseEntity(NULL, filename)); |
| 11233 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11234 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11235 | |
| 11236 | /** |
| 11237 | * xmlCreateEntityParserCtxt: |
| 11238 | * @URL: the entity URL |
| 11239 | * @ID: the entity PUBLIC ID |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 11240 | * @base: a possible base for the target URI |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11241 | * |
| 11242 | * Create a parser context for an external entity |
| 11243 | * Automatic support for ZLIB/Compress compressed document is provided |
| 11244 | * by default if found at compile-time. |
| 11245 | * |
| 11246 | * Returns the new parser context or NULL |
| 11247 | */ |
| 11248 | xmlParserCtxtPtr |
| 11249 | xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID, |
| 11250 | const xmlChar *base) { |
| 11251 | xmlParserCtxtPtr ctxt; |
| 11252 | xmlParserInputPtr inputStream; |
| 11253 | char *directory = NULL; |
| 11254 | xmlChar *uri; |
| 11255 | |
| 11256 | ctxt = xmlNewParserCtxt(); |
| 11257 | if (ctxt == NULL) { |
| 11258 | return(NULL); |
| 11259 | } |
| 11260 | |
| 11261 | uri = xmlBuildURI(URL, base); |
| 11262 | |
| 11263 | if (uri == NULL) { |
| 11264 | inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt); |
| 11265 | if (inputStream == NULL) { |
| 11266 | xmlFreeParserCtxt(ctxt); |
| 11267 | return(NULL); |
| 11268 | } |
| 11269 | |
| 11270 | inputPush(ctxt, inputStream); |
| 11271 | |
| 11272 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 11273 | directory = xmlParserGetDirectory((char *)URL); |
| 11274 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 11275 | ctxt->directory = directory; |
| 11276 | } else { |
| 11277 | inputStream = xmlLoadExternalEntity((char *)uri, (char *)ID, ctxt); |
| 11278 | if (inputStream == NULL) { |
| 11279 | xmlFree(uri); |
| 11280 | xmlFreeParserCtxt(ctxt); |
| 11281 | return(NULL); |
| 11282 | } |
| 11283 | |
| 11284 | inputPush(ctxt, inputStream); |
| 11285 | |
| 11286 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 11287 | directory = xmlParserGetDirectory((char *)uri); |
| 11288 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 11289 | ctxt->directory = directory; |
| 11290 | xmlFree(uri); |
| 11291 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11292 | return(ctxt); |
| 11293 | } |
| 11294 | |
| 11295 | /************************************************************************ |
| 11296 | * * |
| 11297 | * Front ends when parsing from a file * |
| 11298 | * * |
| 11299 | ************************************************************************/ |
| 11300 | |
| 11301 | /** |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 11302 | * xmlCreateURLParserCtxt: |
| 11303 | * @filename: the filename or URL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 11304 | * @options: a combination of xmlParserOption |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11305 | * |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 11306 | * Create a parser context for a file or URL content. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11307 | * Automatic support for ZLIB/Compress compressed document is provided |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 11308 | * by default if found at compile-time and for file accesses |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11309 | * |
| 11310 | * Returns the new parser context or NULL |
| 11311 | */ |
| 11312 | xmlParserCtxtPtr |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 11313 | xmlCreateURLParserCtxt(const char *filename, int options) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11314 | { |
| 11315 | xmlParserCtxtPtr ctxt; |
| 11316 | xmlParserInputPtr inputStream; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11317 | char *directory = NULL; |
| 11318 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11319 | ctxt = xmlNewParserCtxt(); |
| 11320 | if (ctxt == NULL) { |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11321 | xmlErrMemory(NULL, "cannot allocate parser context"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11322 | return(NULL); |
| 11323 | } |
| 11324 | |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 11325 | if (options != 0) |
| 11326 | xmlCtxtUseOptions(ctxt, options); |
Igor Zlatkovic | ce07616 | 2003-02-23 13:39:39 +0000 | [diff] [blame] | 11327 | |
Daniel Veillard | 4e9b1bc | 2003-06-09 10:30:33 +0000 | [diff] [blame] | 11328 | inputStream = xmlLoadExternalEntity(filename, NULL, ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11329 | if (inputStream == NULL) { |
| 11330 | xmlFreeParserCtxt(ctxt); |
| 11331 | return(NULL); |
| 11332 | } |
| 11333 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11334 | inputPush(ctxt, inputStream); |
| 11335 | if ((ctxt->directory == NULL) && (directory == NULL)) |
Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 11336 | directory = xmlParserGetDirectory(filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11337 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 11338 | ctxt->directory = directory; |
| 11339 | |
| 11340 | return(ctxt); |
| 11341 | } |
| 11342 | |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 11343 | /** |
| 11344 | * xmlCreateFileParserCtxt: |
| 11345 | * @filename: the filename |
| 11346 | * |
| 11347 | * Create a parser context for a file content. |
| 11348 | * Automatic support for ZLIB/Compress compressed document is provided |
| 11349 | * by default if found at compile-time. |
| 11350 | * |
| 11351 | * Returns the new parser context or NULL |
| 11352 | */ |
| 11353 | xmlParserCtxtPtr |
| 11354 | xmlCreateFileParserCtxt(const char *filename) |
| 11355 | { |
| 11356 | return(xmlCreateURLParserCtxt(filename, 0)); |
| 11357 | } |
| 11358 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11359 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11360 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11361 | * xmlSAXParseFileWithData: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11362 | * @sax: the SAX handler block |
| 11363 | * @filename: the filename |
| 11364 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 11365 | * documents |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11366 | * @data: the userdata |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11367 | * |
| 11368 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 11369 | * compressed document is provided by default if found at compile-time. |
| 11370 | * It use the given SAX function block to handle the parsing callback. |
| 11371 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 11372 | * |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 11373 | * User data (void *) is stored within the parser context in the |
| 11374 | * context's _private member, so it is available nearly everywhere in libxml |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11375 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11376 | * Returns the resulting document tree |
| 11377 | */ |
| 11378 | |
| 11379 | xmlDocPtr |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11380 | xmlSAXParseFileWithData(xmlSAXHandlerPtr sax, const char *filename, |
| 11381 | int recovery, void *data) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11382 | xmlDocPtr ret; |
| 11383 | xmlParserCtxtPtr ctxt; |
| 11384 | char *directory = NULL; |
| 11385 | |
Daniel Veillard | 635ef72 | 2001-10-29 11:48:19 +0000 | [diff] [blame] | 11386 | xmlInitParser(); |
| 11387 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11388 | ctxt = xmlCreateFileParserCtxt(filename); |
| 11389 | if (ctxt == NULL) { |
| 11390 | return(NULL); |
| 11391 | } |
| 11392 | if (sax != NULL) { |
| 11393 | if (ctxt->sax != NULL) |
| 11394 | xmlFree(ctxt->sax); |
| 11395 | ctxt->sax = sax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11396 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11397 | xmlDetectSAX2(ctxt); |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11398 | if (data!=NULL) { |
Daniel Veillard | c790bf4 | 2003-10-11 10:50:10 +0000 | [diff] [blame] | 11399 | ctxt->_private = data; |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11400 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11401 | |
| 11402 | if ((ctxt->directory == NULL) && (directory == NULL)) |
| 11403 | directory = xmlParserGetDirectory(filename); |
| 11404 | if ((ctxt->directory == NULL) && (directory != NULL)) |
| 11405 | ctxt->directory = (char *) xmlStrdup((xmlChar *) directory); |
| 11406 | |
Daniel Veillard | dad3f68 | 2002-11-17 16:47:27 +0000 | [diff] [blame] | 11407 | ctxt->recovery = recovery; |
| 11408 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11409 | xmlParseDocument(ctxt); |
| 11410 | |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 11411 | if ((ctxt->wellFormed) || recovery) { |
| 11412 | ret = ctxt->myDoc; |
Daniel Veillard | b65e12e | 2003-10-08 21:33:28 +0000 | [diff] [blame] | 11413 | if (ret != NULL) { |
| 11414 | if (ctxt->input->buf->compressed > 0) |
| 11415 | ret->compression = 9; |
| 11416 | else |
| 11417 | ret->compression = ctxt->input->buf->compressed; |
| 11418 | } |
William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 11419 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11420 | else { |
| 11421 | ret = NULL; |
| 11422 | xmlFreeDoc(ctxt->myDoc); |
| 11423 | ctxt->myDoc = NULL; |
| 11424 | } |
| 11425 | if (sax != NULL) |
| 11426 | ctxt->sax = NULL; |
| 11427 | xmlFreeParserCtxt(ctxt); |
| 11428 | |
| 11429 | return(ret); |
| 11430 | } |
| 11431 | |
| 11432 | /** |
Daniel Veillard | a293c32 | 2001-10-02 13:54:14 +0000 | [diff] [blame] | 11433 | * xmlSAXParseFile: |
| 11434 | * @sax: the SAX handler block |
| 11435 | * @filename: the filename |
| 11436 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 11437 | * documents |
| 11438 | * |
| 11439 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 11440 | * compressed document is provided by default if found at compile-time. |
| 11441 | * It use the given SAX function block to handle the parsing callback. |
| 11442 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 11443 | * |
| 11444 | * Returns the resulting document tree |
| 11445 | */ |
| 11446 | |
| 11447 | xmlDocPtr |
| 11448 | xmlSAXParseFile(xmlSAXHandlerPtr sax, const char *filename, |
| 11449 | int recovery) { |
| 11450 | return(xmlSAXParseFileWithData(sax,filename,recovery,NULL)); |
| 11451 | } |
| 11452 | |
| 11453 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11454 | * xmlRecoverDoc: |
| 11455 | * @cur: a pointer to an array of xmlChar |
| 11456 | * |
| 11457 | * parse an XML in-memory document and build a tree. |
| 11458 | * In the case the document is not Well Formed, a tree is built anyway |
| 11459 | * |
| 11460 | * Returns the resulting document tree |
| 11461 | */ |
| 11462 | |
| 11463 | xmlDocPtr |
| 11464 | xmlRecoverDoc(xmlChar *cur) { |
| 11465 | return(xmlSAXParseDoc(NULL, cur, 1)); |
| 11466 | } |
| 11467 | |
| 11468 | /** |
| 11469 | * xmlParseFile: |
| 11470 | * @filename: the filename |
| 11471 | * |
| 11472 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 11473 | * compressed document is provided by default if found at compile-time. |
| 11474 | * |
Daniel Veillard | 5d96fff | 2001-08-31 14:55:30 +0000 | [diff] [blame] | 11475 | * Returns the resulting document tree if the file was wellformed, |
| 11476 | * NULL otherwise. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11477 | */ |
| 11478 | |
| 11479 | xmlDocPtr |
| 11480 | xmlParseFile(const char *filename) { |
| 11481 | return(xmlSAXParseFile(NULL, filename, 0)); |
| 11482 | } |
| 11483 | |
| 11484 | /** |
| 11485 | * xmlRecoverFile: |
| 11486 | * @filename: the filename |
| 11487 | * |
| 11488 | * parse an XML file and build a tree. Automatic support for ZLIB/Compress |
| 11489 | * compressed document is provided by default if found at compile-time. |
| 11490 | * In the case the document is not Well Formed, a tree is built anyway |
| 11491 | * |
| 11492 | * Returns the resulting document tree |
| 11493 | */ |
| 11494 | |
| 11495 | xmlDocPtr |
| 11496 | xmlRecoverFile(const char *filename) { |
| 11497 | return(xmlSAXParseFile(NULL, filename, 1)); |
| 11498 | } |
| 11499 | |
| 11500 | |
| 11501 | /** |
| 11502 | * xmlSetupParserForBuffer: |
| 11503 | * @ctxt: an XML parser context |
| 11504 | * @buffer: a xmlChar * buffer |
| 11505 | * @filename: a file name |
| 11506 | * |
| 11507 | * Setup the parser context to parse a new buffer; Clears any prior |
| 11508 | * contents from the parser context. The buffer parameter must not be |
| 11509 | * NULL, but the filename parameter can be |
| 11510 | */ |
| 11511 | void |
| 11512 | xmlSetupParserForBuffer(xmlParserCtxtPtr ctxt, const xmlChar* buffer, |
| 11513 | const char* filename) |
| 11514 | { |
| 11515 | xmlParserInputPtr input; |
| 11516 | |
| 11517 | input = xmlNewInputStream(ctxt); |
| 11518 | if (input == NULL) { |
Daniel Veillard | 24eb978 | 2003-10-04 21:08:09 +0000 | [diff] [blame] | 11519 | xmlErrMemory(NULL, "parsing new buffer: out of memory\n"); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11520 | xmlFree(ctxt); |
| 11521 | return; |
| 11522 | } |
| 11523 | |
| 11524 | xmlClearParserCtxt(ctxt); |
| 11525 | if (filename != NULL) |
Daniel Veillard | c3ca5ba | 2003-05-09 22:26:28 +0000 | [diff] [blame] | 11526 | input->filename = (char *) xmlCanonicPath((const xmlChar *)filename); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11527 | input->base = buffer; |
| 11528 | input->cur = buffer; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 11529 | input->end = &buffer[xmlStrlen(buffer)]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11530 | inputPush(ctxt, input); |
| 11531 | } |
| 11532 | |
| 11533 | /** |
| 11534 | * xmlSAXUserParseFile: |
| 11535 | * @sax: a SAX handler |
| 11536 | * @user_data: The user data returned on SAX callbacks |
| 11537 | * @filename: a file name |
| 11538 | * |
| 11539 | * parse an XML file and call the given SAX handler routines. |
| 11540 | * Automatic support for ZLIB/Compress compressed document is provided |
| 11541 | * |
| 11542 | * Returns 0 in case of success or a error number otherwise |
| 11543 | */ |
| 11544 | int |
| 11545 | xmlSAXUserParseFile(xmlSAXHandlerPtr sax, void *user_data, |
| 11546 | const char *filename) { |
| 11547 | int ret = 0; |
| 11548 | xmlParserCtxtPtr ctxt; |
| 11549 | |
| 11550 | ctxt = xmlCreateFileParserCtxt(filename); |
| 11551 | if (ctxt == NULL) return -1; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11552 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 092643b | 2003-09-25 14:29:29 +0000 | [diff] [blame] | 11553 | if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler) |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11554 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11555 | xmlFree(ctxt->sax); |
| 11556 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11557 | xmlDetectSAX2(ctxt); |
| 11558 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11559 | if (user_data != NULL) |
| 11560 | ctxt->userData = user_data; |
| 11561 | |
| 11562 | xmlParseDocument(ctxt); |
| 11563 | |
| 11564 | if (ctxt->wellFormed) |
| 11565 | ret = 0; |
| 11566 | else { |
| 11567 | if (ctxt->errNo != 0) |
| 11568 | ret = ctxt->errNo; |
| 11569 | else |
| 11570 | ret = -1; |
| 11571 | } |
| 11572 | if (sax != NULL) |
| 11573 | ctxt->sax = NULL; |
| 11574 | xmlFreeParserCtxt(ctxt); |
| 11575 | |
| 11576 | return ret; |
| 11577 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11578 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11579 | |
| 11580 | /************************************************************************ |
| 11581 | * * |
| 11582 | * Front ends when parsing from memory * |
| 11583 | * * |
| 11584 | ************************************************************************/ |
| 11585 | |
| 11586 | /** |
| 11587 | * xmlCreateMemoryParserCtxt: |
| 11588 | * @buffer: a pointer to a char array |
| 11589 | * @size: the size of the array |
| 11590 | * |
| 11591 | * Create a parser context for an XML in-memory document. |
| 11592 | * |
| 11593 | * Returns the new parser context or NULL |
| 11594 | */ |
| 11595 | xmlParserCtxtPtr |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 11596 | xmlCreateMemoryParserCtxt(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11597 | xmlParserCtxtPtr ctxt; |
| 11598 | xmlParserInputPtr input; |
| 11599 | xmlParserInputBufferPtr buf; |
| 11600 | |
| 11601 | if (buffer == NULL) |
| 11602 | return(NULL); |
| 11603 | if (size <= 0) |
| 11604 | return(NULL); |
| 11605 | |
| 11606 | ctxt = xmlNewParserCtxt(); |
| 11607 | if (ctxt == NULL) |
| 11608 | return(NULL); |
| 11609 | |
Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 11610 | /* TODO: xmlParserInputBufferCreateStatic, requires some serious changes */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11611 | buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 11612 | if (buf == NULL) { |
| 11613 | xmlFreeParserCtxt(ctxt); |
| 11614 | return(NULL); |
| 11615 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11616 | |
| 11617 | input = xmlNewInputStream(ctxt); |
| 11618 | if (input == NULL) { |
Daniel Veillard | a7e05b4 | 2002-11-19 08:11:14 +0000 | [diff] [blame] | 11619 | xmlFreeParserInputBuffer(buf); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11620 | xmlFreeParserCtxt(ctxt); |
| 11621 | return(NULL); |
| 11622 | } |
| 11623 | |
| 11624 | input->filename = NULL; |
| 11625 | input->buf = buf; |
| 11626 | input->base = input->buf->buffer->content; |
| 11627 | input->cur = input->buf->buffer->content; |
Daniel Veillard | 48b2f89 | 2001-02-25 16:11:03 +0000 | [diff] [blame] | 11628 | input->end = &input->buf->buffer->content[input->buf->buffer->use]; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11629 | |
| 11630 | inputPush(ctxt, input); |
| 11631 | return(ctxt); |
| 11632 | } |
| 11633 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11634 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11635 | /** |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 11636 | * xmlSAXParseMemoryWithData: |
| 11637 | * @sax: the SAX handler block |
| 11638 | * @buffer: an pointer to a char array |
| 11639 | * @size: the size of the array |
| 11640 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 11641 | * documents |
| 11642 | * @data: the userdata |
| 11643 | * |
| 11644 | * parse an XML in-memory block and use the given SAX function block |
| 11645 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 11646 | * DOM tree building routines. |
| 11647 | * |
| 11648 | * User data (void *) is stored within the parser context in the |
| 11649 | * context's _private member, so it is available nearly everywhere in libxml |
| 11650 | * |
| 11651 | * Returns the resulting document tree |
| 11652 | */ |
| 11653 | |
| 11654 | xmlDocPtr |
| 11655 | xmlSAXParseMemoryWithData(xmlSAXHandlerPtr sax, const char *buffer, |
| 11656 | int size, int recovery, void *data) { |
| 11657 | xmlDocPtr ret; |
| 11658 | xmlParserCtxtPtr ctxt; |
| 11659 | |
| 11660 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 11661 | if (ctxt == NULL) return(NULL); |
| 11662 | if (sax != NULL) { |
| 11663 | if (ctxt->sax != NULL) |
| 11664 | xmlFree(ctxt->sax); |
| 11665 | ctxt->sax = sax; |
| 11666 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11667 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 11668 | if (data!=NULL) { |
| 11669 | ctxt->_private=data; |
| 11670 | } |
| 11671 | |
Daniel Veillard | adba5f1 | 2003-04-04 16:09:01 +0000 | [diff] [blame] | 11672 | ctxt->recovery = recovery; |
| 11673 | |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 11674 | xmlParseDocument(ctxt); |
| 11675 | |
| 11676 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 11677 | else { |
| 11678 | ret = NULL; |
| 11679 | xmlFreeDoc(ctxt->myDoc); |
| 11680 | ctxt->myDoc = NULL; |
| 11681 | } |
| 11682 | if (sax != NULL) |
| 11683 | ctxt->sax = NULL; |
| 11684 | xmlFreeParserCtxt(ctxt); |
| 11685 | |
| 11686 | return(ret); |
| 11687 | } |
| 11688 | |
| 11689 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11690 | * xmlSAXParseMemory: |
| 11691 | * @sax: the SAX handler block |
| 11692 | * @buffer: an pointer to a char array |
| 11693 | * @size: the size of the array |
| 11694 | * @recovery: work in recovery mode, i.e. tries to read not Well Formed |
| 11695 | * documents |
| 11696 | * |
| 11697 | * parse an XML in-memory block and use the given SAX function block |
| 11698 | * to handle the parsing callback. If sax is NULL, fallback to the default |
| 11699 | * DOM tree building routines. |
| 11700 | * |
| 11701 | * Returns the resulting document tree |
| 11702 | */ |
| 11703 | xmlDocPtr |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 11704 | xmlSAXParseMemory(xmlSAXHandlerPtr sax, const char *buffer, |
| 11705 | int size, int recovery) { |
Daniel Veillard | 8606bbb | 2002-11-12 12:36:52 +0000 | [diff] [blame] | 11706 | return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11707 | } |
| 11708 | |
| 11709 | /** |
| 11710 | * xmlParseMemory: |
| 11711 | * @buffer: an pointer to a char array |
| 11712 | * @size: the size of the array |
| 11713 | * |
| 11714 | * parse an XML in-memory block and build a tree. |
| 11715 | * |
| 11716 | * Returns the resulting document tree |
| 11717 | */ |
| 11718 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 11719 | xmlDocPtr xmlParseMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11720 | return(xmlSAXParseMemory(NULL, buffer, size, 0)); |
| 11721 | } |
| 11722 | |
| 11723 | /** |
| 11724 | * xmlRecoverMemory: |
| 11725 | * @buffer: an pointer to a char array |
| 11726 | * @size: the size of the array |
| 11727 | * |
| 11728 | * parse an XML in-memory block and build a tree. |
| 11729 | * In the case the document is not Well Formed, a tree is built anyway |
| 11730 | * |
| 11731 | * Returns the resulting document tree |
| 11732 | */ |
| 11733 | |
Daniel Veillard | 50822cb | 2001-07-26 20:05:51 +0000 | [diff] [blame] | 11734 | xmlDocPtr xmlRecoverMemory(const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11735 | return(xmlSAXParseMemory(NULL, buffer, size, 1)); |
| 11736 | } |
| 11737 | |
| 11738 | /** |
| 11739 | * xmlSAXUserParseMemory: |
| 11740 | * @sax: a SAX handler |
| 11741 | * @user_data: The user data returned on SAX callbacks |
| 11742 | * @buffer: an in-memory XML document input |
| 11743 | * @size: the length of the XML document in bytes |
| 11744 | * |
| 11745 | * A better SAX parsing routine. |
| 11746 | * parse an XML in-memory buffer and call the given SAX handler routines. |
| 11747 | * |
| 11748 | * Returns 0 in case of success or a error number otherwise |
| 11749 | */ |
| 11750 | int xmlSAXUserParseMemory(xmlSAXHandlerPtr sax, void *user_data, |
Daniel Veillard | fd7ddca | 2001-05-16 10:57:35 +0000 | [diff] [blame] | 11751 | const char *buffer, int size) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11752 | int ret = 0; |
| 11753 | xmlParserCtxtPtr ctxt; |
| 11754 | xmlSAXHandlerPtr oldsax = NULL; |
| 11755 | |
Daniel Veillard | 9e92351 | 2002-08-14 08:48:52 +0000 | [diff] [blame] | 11756 | if (sax == NULL) return -1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11757 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 11758 | if (ctxt == NULL) return -1; |
Daniel Veillard | 9e92351 | 2002-08-14 08:48:52 +0000 | [diff] [blame] | 11759 | oldsax = ctxt->sax; |
| 11760 | ctxt->sax = sax; |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11761 | xmlDetectSAX2(ctxt); |
Daniel Veillard | 30211a0 | 2001-04-26 09:33:18 +0000 | [diff] [blame] | 11762 | if (user_data != NULL) |
| 11763 | ctxt->userData = user_data; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11764 | |
| 11765 | xmlParseDocument(ctxt); |
| 11766 | |
| 11767 | if (ctxt->wellFormed) |
| 11768 | ret = 0; |
| 11769 | else { |
| 11770 | if (ctxt->errNo != 0) |
| 11771 | ret = ctxt->errNo; |
| 11772 | else |
| 11773 | ret = -1; |
| 11774 | } |
Daniel Veillard | 9e92351 | 2002-08-14 08:48:52 +0000 | [diff] [blame] | 11775 | ctxt->sax = oldsax; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11776 | xmlFreeParserCtxt(ctxt); |
| 11777 | |
| 11778 | return ret; |
| 11779 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11780 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11781 | |
| 11782 | /** |
| 11783 | * xmlCreateDocParserCtxt: |
| 11784 | * @cur: a pointer to an array of xmlChar |
| 11785 | * |
| 11786 | * Creates a parser context for an XML in-memory document. |
| 11787 | * |
| 11788 | * Returns the new parser context or NULL |
| 11789 | */ |
| 11790 | xmlParserCtxtPtr |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 11791 | xmlCreateDocParserCtxt(const xmlChar *cur) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11792 | int len; |
| 11793 | |
| 11794 | if (cur == NULL) |
| 11795 | return(NULL); |
| 11796 | len = xmlStrlen(cur); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 11797 | return(xmlCreateMemoryParserCtxt((const char *)cur, len)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11798 | } |
| 11799 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11800 | #ifdef LIBXML_SAX1_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11801 | /** |
| 11802 | * xmlSAXParseDoc: |
| 11803 | * @sax: the SAX handler block |
| 11804 | * @cur: a pointer to an array of xmlChar |
| 11805 | * @recovery: work in recovery mode, i.e. tries to read no Well Formed |
| 11806 | * documents |
| 11807 | * |
| 11808 | * parse an XML in-memory document and build a tree. |
| 11809 | * It use the given SAX function block to handle the parsing callback. |
| 11810 | * If sax is NULL, fallback to the default DOM tree building routines. |
| 11811 | * |
| 11812 | * Returns the resulting document tree |
| 11813 | */ |
| 11814 | |
| 11815 | xmlDocPtr |
| 11816 | xmlSAXParseDoc(xmlSAXHandlerPtr sax, xmlChar *cur, int recovery) { |
| 11817 | xmlDocPtr ret; |
| 11818 | xmlParserCtxtPtr ctxt; |
| 11819 | |
| 11820 | if (cur == NULL) return(NULL); |
| 11821 | |
| 11822 | |
| 11823 | ctxt = xmlCreateDocParserCtxt(cur); |
| 11824 | if (ctxt == NULL) return(NULL); |
| 11825 | if (sax != NULL) { |
| 11826 | ctxt->sax = sax; |
| 11827 | ctxt->userData = NULL; |
| 11828 | } |
Daniel Veillard | e57ec79 | 2003-09-10 10:50:59 +0000 | [diff] [blame] | 11829 | xmlDetectSAX2(ctxt); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11830 | |
| 11831 | xmlParseDocument(ctxt); |
| 11832 | if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc; |
| 11833 | else { |
| 11834 | ret = NULL; |
| 11835 | xmlFreeDoc(ctxt->myDoc); |
| 11836 | ctxt->myDoc = NULL; |
| 11837 | } |
| 11838 | if (sax != NULL) |
| 11839 | ctxt->sax = NULL; |
| 11840 | xmlFreeParserCtxt(ctxt); |
| 11841 | |
| 11842 | return(ret); |
| 11843 | } |
| 11844 | |
| 11845 | /** |
| 11846 | * xmlParseDoc: |
| 11847 | * @cur: a pointer to an array of xmlChar |
| 11848 | * |
| 11849 | * parse an XML in-memory document and build a tree. |
| 11850 | * |
| 11851 | * Returns the resulting document tree |
| 11852 | */ |
| 11853 | |
| 11854 | xmlDocPtr |
| 11855 | xmlParseDoc(xmlChar *cur) { |
| 11856 | return(xmlSAXParseDoc(NULL, cur, 0)); |
| 11857 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11858 | #endif /* LIBXML_SAX1_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11859 | |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11860 | #ifdef LIBXML_LEGACY_ENABLED |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 11861 | /************************************************************************ |
| 11862 | * * |
| 11863 | * Specific function to keep track of entities references * |
| 11864 | * and used by the XSLT debugger * |
| 11865 | * * |
| 11866 | ************************************************************************/ |
| 11867 | |
| 11868 | static xmlEntityReferenceFunc xmlEntityRefFunc = NULL; |
| 11869 | |
| 11870 | /** |
| 11871 | * xmlAddEntityReference: |
| 11872 | * @ent : A valid entity |
| 11873 | * @firstNode : A valid first node for children of entity |
| 11874 | * @lastNode : A valid last node of children entity |
| 11875 | * |
| 11876 | * Notify of a reference to an entity of type XML_EXTERNAL_GENERAL_PARSED_ENTITY |
| 11877 | */ |
| 11878 | static void |
| 11879 | xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode, |
| 11880 | xmlNodePtr lastNode) |
| 11881 | { |
| 11882 | if (xmlEntityRefFunc != NULL) { |
| 11883 | (*xmlEntityRefFunc) (ent, firstNode, lastNode); |
| 11884 | } |
| 11885 | } |
| 11886 | |
| 11887 | |
| 11888 | /** |
| 11889 | * xmlSetEntityReferenceFunc: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 11890 | * @func: A valid function |
Daniel Veillard | 8107a22 | 2002-01-13 14:10:10 +0000 | [diff] [blame] | 11891 | * |
| 11892 | * Set the function to call call back when a xml reference has been made |
| 11893 | */ |
| 11894 | void |
| 11895 | xmlSetEntityReferenceFunc(xmlEntityReferenceFunc func) |
| 11896 | { |
| 11897 | xmlEntityRefFunc = func; |
| 11898 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 11899 | #endif /* LIBXML_LEGACY_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11900 | |
| 11901 | /************************************************************************ |
| 11902 | * * |
| 11903 | * Miscellaneous * |
| 11904 | * * |
| 11905 | ************************************************************************/ |
| 11906 | |
| 11907 | #ifdef LIBXML_XPATH_ENABLED |
| 11908 | #include <libxml/xpath.h> |
| 11909 | #endif |
| 11910 | |
Daniel Veillard | db5850a | 2002-01-18 11:49:26 +0000 | [diff] [blame] | 11911 | extern void xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11912 | static int xmlParserInitialized = 0; |
| 11913 | |
| 11914 | /** |
| 11915 | * xmlInitParser: |
| 11916 | * |
| 11917 | * Initialization function for the XML parser. |
| 11918 | * This is not reentrant. Call once before processing in case of |
| 11919 | * use in multithreaded programs. |
| 11920 | */ |
| 11921 | |
| 11922 | void |
| 11923 | xmlInitParser(void) { |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 11924 | if (xmlParserInitialized != 0) |
| 11925 | return; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11926 | |
Daniel Veillard | db5850a | 2002-01-18 11:49:26 +0000 | [diff] [blame] | 11927 | if ((xmlGenericError == xmlGenericErrorDefaultFunc) || |
| 11928 | (xmlGenericError == NULL)) |
| 11929 | initGenericErrorDefaultFunc(NULL); |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 11930 | xmlInitGlobals(); |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 11931 | xmlInitThreads(); |
Daniel Veillard | 6f35029 | 2001-10-14 09:56:15 +0000 | [diff] [blame] | 11932 | xmlInitMemory(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11933 | xmlInitCharEncodingHandlers(); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11934 | xmlDefaultSAXHandlerInit(); |
| 11935 | xmlRegisterDefaultInputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 11936 | #ifdef LIBXML_OUTPUT_ENABLED |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11937 | xmlRegisterDefaultOutputCallbacks(); |
Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 11938 | #endif /* LIBXML_OUTPUT_ENABLED */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11939 | #ifdef LIBXML_HTML_ENABLED |
| 11940 | htmlInitAutoClose(); |
| 11941 | htmlDefaultSAXHandlerInit(); |
| 11942 | #endif |
| 11943 | #ifdef LIBXML_XPATH_ENABLED |
| 11944 | xmlXPathInit(); |
| 11945 | #endif |
| 11946 | xmlParserInitialized = 1; |
| 11947 | } |
| 11948 | |
| 11949 | /** |
| 11950 | * xmlCleanupParser: |
| 11951 | * |
Daniel Veillard | d8cf906 | 2003-11-11 21:12:36 +0000 | [diff] [blame] | 11952 | * Cleanup function for the XML library. It tries to reclaim all |
| 11953 | * parsing related global memory allocated for the library processing. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11954 | * It doesn't deallocate any document related memory. Calling this |
Daniel Veillard | d8cf906 | 2003-11-11 21:12:36 +0000 | [diff] [blame] | 11955 | * function should not prevent reusing the library but one should |
| 11956 | * call xmlCleanupParser() only when the process has |
Daniel Veillard | 7424eb6 | 2003-01-24 14:14:52 +0000 | [diff] [blame] | 11957 | * finished using the library or XML document built with it. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11958 | */ |
| 11959 | |
| 11960 | void |
| 11961 | xmlCleanupParser(void) { |
Daniel Veillard | 7fb801f | 2003-08-17 21:07:26 +0000 | [diff] [blame] | 11962 | if (!xmlParserInitialized) |
| 11963 | return; |
| 11964 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11965 | xmlCleanupCharEncodingHandlers(); |
Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 11966 | #ifdef LIBXML_CATALOG_ENABLED |
| 11967 | xmlCatalogCleanup(); |
| 11968 | #endif |
Daniel Veillard | 04054be | 2003-10-15 10:48:54 +0000 | [diff] [blame] | 11969 | xmlCleanupInputCallbacks(); |
| 11970 | #ifdef LIBXML_OUTPUT_ENABLED |
| 11971 | xmlCleanupOutputCallbacks(); |
| 11972 | #endif |
Daniel Veillard | 781ac8b | 2003-05-15 22:11:36 +0000 | [diff] [blame] | 11973 | xmlCleanupGlobals(); |
Daniel Veillard | 2b8c4a1 | 2003-10-02 22:28:19 +0000 | [diff] [blame] | 11974 | xmlResetLastError(); |
Daniel Veillard | 74c0e59 | 2003-11-25 07:01:38 +0000 | [diff] [blame] | 11975 | xmlCleanupThreads(); /* must be last if called not from the main thread */ |
William M. Brack | 72ee48d | 2003-12-30 08:30:19 +0000 | [diff] [blame] | 11976 | xmlCleanupMemory(); |
Daniel Veillard | d046356 | 2001-10-13 09:15:48 +0000 | [diff] [blame] | 11977 | xmlParserInitialized = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 11978 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 11979 | |
| 11980 | /************************************************************************ |
| 11981 | * * |
| 11982 | * New set (2.6.0) of simpler and more flexible APIs * |
| 11983 | * * |
| 11984 | ************************************************************************/ |
| 11985 | |
| 11986 | /** |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 11987 | * DICT_FREE: |
| 11988 | * @str: a string |
| 11989 | * |
| 11990 | * Free a string if it is not owned by the "dict" dictionnary in the |
| 11991 | * current scope |
| 11992 | */ |
| 11993 | #define DICT_FREE(str) \ |
| 11994 | if ((str) && ((!dict) || \ |
| 11995 | (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \ |
| 11996 | xmlFree((char *)(str)); |
| 11997 | |
| 11998 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 11999 | * xmlCtxtReset: |
| 12000 | * @ctxt: an XML parser context |
| 12001 | * |
| 12002 | * Reset a parser context |
| 12003 | */ |
| 12004 | void |
| 12005 | xmlCtxtReset(xmlParserCtxtPtr ctxt) |
| 12006 | { |
| 12007 | xmlParserInputPtr input; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12008 | xmlDictPtr dict = ctxt->dict; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12009 | |
| 12010 | while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */ |
| 12011 | xmlFreeInputStream(input); |
| 12012 | } |
| 12013 | ctxt->inputNr = 0; |
| 12014 | ctxt->input = NULL; |
| 12015 | |
| 12016 | ctxt->spaceNr = 0; |
| 12017 | ctxt->spaceTab[0] = -1; |
| 12018 | ctxt->space = &ctxt->spaceTab[0]; |
| 12019 | |
| 12020 | |
| 12021 | ctxt->nodeNr = 0; |
| 12022 | ctxt->node = NULL; |
| 12023 | |
| 12024 | ctxt->nameNr = 0; |
| 12025 | ctxt->name = NULL; |
| 12026 | |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12027 | DICT_FREE(ctxt->version); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12028 | ctxt->version = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12029 | DICT_FREE(ctxt->encoding); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12030 | ctxt->encoding = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12031 | DICT_FREE(ctxt->directory); |
| 12032 | ctxt->directory = NULL; |
| 12033 | DICT_FREE(ctxt->extSubURI); |
| 12034 | ctxt->extSubURI = NULL; |
| 12035 | DICT_FREE(ctxt->extSubSystem); |
| 12036 | ctxt->extSubSystem = NULL; |
| 12037 | if (ctxt->myDoc != NULL) |
| 12038 | xmlFreeDoc(ctxt->myDoc); |
| 12039 | ctxt->myDoc = NULL; |
| 12040 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12041 | ctxt->standalone = -1; |
| 12042 | ctxt->hasExternalSubset = 0; |
| 12043 | ctxt->hasPErefs = 0; |
| 12044 | ctxt->html = 0; |
| 12045 | ctxt->external = 0; |
| 12046 | ctxt->instate = XML_PARSER_START; |
| 12047 | ctxt->token = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12048 | |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12049 | ctxt->wellFormed = 1; |
| 12050 | ctxt->nsWellFormed = 1; |
Daniel Veillard | ae28918 | 2004-01-21 16:00:43 +0000 | [diff] [blame] | 12051 | ctxt->disableSAX = 0; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12052 | ctxt->valid = 1; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 12053 | #if 0 |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12054 | ctxt->vctxt.userData = ctxt; |
| 12055 | ctxt->vctxt.error = xmlParserValidityError; |
| 12056 | ctxt->vctxt.warning = xmlParserValidityWarning; |
Daniel Veillard | 766c4f9 | 2004-03-26 10:48:29 +0000 | [diff] [blame] | 12057 | #endif |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12058 | ctxt->record_info = 0; |
| 12059 | ctxt->nbChars = 0; |
| 12060 | ctxt->checkIndex = 0; |
| 12061 | ctxt->inSubset = 0; |
| 12062 | ctxt->errNo = XML_ERR_OK; |
| 12063 | ctxt->depth = 0; |
| 12064 | ctxt->charset = XML_CHAR_ENCODING_UTF8; |
| 12065 | ctxt->catalogs = NULL; |
| 12066 | xmlInitNodeInfoSeq(&ctxt->node_seq); |
| 12067 | |
| 12068 | if (ctxt->attsDefault != NULL) { |
| 12069 | xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree); |
| 12070 | ctxt->attsDefault = NULL; |
| 12071 | } |
| 12072 | if (ctxt->attsSpecial != NULL) { |
| 12073 | xmlHashFree(ctxt->attsSpecial, NULL); |
| 12074 | ctxt->attsSpecial = NULL; |
| 12075 | } |
| 12076 | |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12077 | #ifdef LIBXML_CATALOG_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12078 | if (ctxt->catalogs != NULL) |
| 12079 | xmlCatalogFreeLocal(ctxt->catalogs); |
Daniel Veillard | 4432df2 | 2003-09-28 18:58:27 +0000 | [diff] [blame] | 12080 | #endif |
Daniel Veillard | cc199e0 | 2003-10-24 21:11:48 +0000 | [diff] [blame] | 12081 | if (ctxt->lastError.code != XML_ERR_OK) |
| 12082 | xmlResetError(&ctxt->lastError); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12083 | } |
| 12084 | |
| 12085 | /** |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 12086 | * xmlCtxtResetPush: |
| 12087 | * @ctxt: an XML parser context |
| 12088 | * @chunk: a pointer to an array of chars |
| 12089 | * @size: number of chars in the array |
| 12090 | * @filename: an optional file name or URI |
| 12091 | * @encoding: the document encoding, or NULL |
| 12092 | * |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 12093 | * Reset a push parser context |
| 12094 | * |
| 12095 | * Returns 0 in case of success and 1 in case of error |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 12096 | */ |
| 12097 | int |
| 12098 | xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk, |
| 12099 | int size, const char *filename, const char *encoding) |
| 12100 | { |
| 12101 | xmlParserInputPtr inputStream; |
| 12102 | xmlParserInputBufferPtr buf; |
| 12103 | xmlCharEncoding enc = XML_CHAR_ENCODING_NONE; |
| 12104 | |
Daniel Veillard | e4e3f5d | 2003-10-28 23:06:32 +0000 | [diff] [blame] | 12105 | if (ctxt == NULL) |
| 12106 | return(1); |
| 12107 | |
Daniel Veillard | 9ba8e38 | 2003-10-28 21:31:45 +0000 | [diff] [blame] | 12108 | if ((encoding == NULL) && (chunk != NULL) && (size >= 4)) |
| 12109 | enc = xmlDetectCharEncoding((const xmlChar *) chunk, size); |
| 12110 | |
| 12111 | buf = xmlAllocParserInputBuffer(enc); |
| 12112 | if (buf == NULL) |
| 12113 | return(1); |
| 12114 | |
| 12115 | if (ctxt == NULL) { |
| 12116 | xmlFreeParserInputBuffer(buf); |
| 12117 | return(1); |
| 12118 | } |
| 12119 | |
| 12120 | xmlCtxtReset(ctxt); |
| 12121 | |
| 12122 | if (ctxt->pushTab == NULL) { |
| 12123 | ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * |
| 12124 | sizeof(xmlChar *)); |
| 12125 | if (ctxt->pushTab == NULL) { |
| 12126 | xmlErrMemory(ctxt, NULL); |
| 12127 | xmlFreeParserInputBuffer(buf); |
| 12128 | return(1); |
| 12129 | } |
| 12130 | } |
| 12131 | |
| 12132 | if (filename == NULL) { |
| 12133 | ctxt->directory = NULL; |
| 12134 | } else { |
| 12135 | ctxt->directory = xmlParserGetDirectory(filename); |
| 12136 | } |
| 12137 | |
| 12138 | inputStream = xmlNewInputStream(ctxt); |
| 12139 | if (inputStream == NULL) { |
| 12140 | xmlFreeParserInputBuffer(buf); |
| 12141 | return(1); |
| 12142 | } |
| 12143 | |
| 12144 | if (filename == NULL) |
| 12145 | inputStream->filename = NULL; |
| 12146 | else |
| 12147 | inputStream->filename = (char *) |
| 12148 | xmlCanonicPath((const xmlChar *) filename); |
| 12149 | inputStream->buf = buf; |
| 12150 | inputStream->base = inputStream->buf->buffer->content; |
| 12151 | inputStream->cur = inputStream->buf->buffer->content; |
| 12152 | inputStream->end = |
| 12153 | &inputStream->buf->buffer->content[inputStream->buf->buffer->use]; |
| 12154 | |
| 12155 | inputPush(ctxt, inputStream); |
| 12156 | |
| 12157 | if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) && |
| 12158 | (ctxt->input->buf != NULL)) { |
| 12159 | int base = ctxt->input->base - ctxt->input->buf->buffer->content; |
| 12160 | int cur = ctxt->input->cur - ctxt->input->base; |
| 12161 | |
| 12162 | xmlParserInputBufferPush(ctxt->input->buf, size, chunk); |
| 12163 | |
| 12164 | ctxt->input->base = ctxt->input->buf->buffer->content + base; |
| 12165 | ctxt->input->cur = ctxt->input->base + cur; |
| 12166 | ctxt->input->end = |
| 12167 | &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer-> |
| 12168 | use]; |
| 12169 | #ifdef DEBUG_PUSH |
| 12170 | xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size); |
| 12171 | #endif |
| 12172 | } |
| 12173 | |
| 12174 | if (encoding != NULL) { |
| 12175 | xmlCharEncodingHandlerPtr hdlr; |
| 12176 | |
| 12177 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 12178 | if (hdlr != NULL) { |
| 12179 | xmlSwitchToEncoding(ctxt, hdlr); |
| 12180 | } else { |
| 12181 | xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING, |
| 12182 | "Unsupported encoding %s\n", BAD_CAST encoding); |
| 12183 | } |
| 12184 | } else if (enc != XML_CHAR_ENCODING_NONE) { |
| 12185 | xmlSwitchEncoding(ctxt, enc); |
| 12186 | } |
| 12187 | |
| 12188 | return(0); |
| 12189 | } |
| 12190 | |
| 12191 | /** |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12192 | * xmlCtxtUseOptions: |
| 12193 | * @ctxt: an XML parser context |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12194 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12195 | * |
| 12196 | * Applies the options to the parser context |
| 12197 | * |
| 12198 | * Returns 0 in case of success, the set of unknown or unimplemented options |
| 12199 | * in case of error. |
| 12200 | */ |
| 12201 | int |
| 12202 | xmlCtxtUseOptions(xmlParserCtxtPtr ctxt, int options) |
| 12203 | { |
| 12204 | if (options & XML_PARSE_RECOVER) { |
| 12205 | ctxt->recovery = 1; |
| 12206 | options -= XML_PARSE_RECOVER; |
| 12207 | } else |
| 12208 | ctxt->recovery = 0; |
| 12209 | if (options & XML_PARSE_DTDLOAD) { |
| 12210 | ctxt->loadsubset = XML_DETECT_IDS; |
| 12211 | options -= XML_PARSE_DTDLOAD; |
| 12212 | } else |
| 12213 | ctxt->loadsubset = 0; |
| 12214 | if (options & XML_PARSE_DTDATTR) { |
| 12215 | ctxt->loadsubset |= XML_COMPLETE_ATTRS; |
| 12216 | options -= XML_PARSE_DTDATTR; |
| 12217 | } |
| 12218 | if (options & XML_PARSE_NOENT) { |
| 12219 | ctxt->replaceEntities = 1; |
| 12220 | /* ctxt->loadsubset |= XML_DETECT_IDS; */ |
| 12221 | options -= XML_PARSE_NOENT; |
| 12222 | } else |
| 12223 | ctxt->replaceEntities = 0; |
| 12224 | if (options & XML_PARSE_NOWARNING) { |
| 12225 | ctxt->sax->warning = NULL; |
| 12226 | options -= XML_PARSE_NOWARNING; |
| 12227 | } |
| 12228 | if (options & XML_PARSE_NOERROR) { |
| 12229 | ctxt->sax->error = NULL; |
| 12230 | ctxt->sax->fatalError = NULL; |
| 12231 | options -= XML_PARSE_NOERROR; |
| 12232 | } |
| 12233 | if (options & XML_PARSE_PEDANTIC) { |
| 12234 | ctxt->pedantic = 1; |
| 12235 | options -= XML_PARSE_PEDANTIC; |
| 12236 | } else |
| 12237 | ctxt->pedantic = 0; |
| 12238 | if (options & XML_PARSE_NOBLANKS) { |
| 12239 | ctxt->keepBlanks = 0; |
| 12240 | ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace; |
| 12241 | options -= XML_PARSE_NOBLANKS; |
| 12242 | } else |
| 12243 | ctxt->keepBlanks = 1; |
| 12244 | if (options & XML_PARSE_DTDVALID) { |
| 12245 | ctxt->validate = 1; |
| 12246 | if (options & XML_PARSE_NOWARNING) |
| 12247 | ctxt->vctxt.warning = NULL; |
| 12248 | if (options & XML_PARSE_NOERROR) |
| 12249 | ctxt->vctxt.error = NULL; |
| 12250 | options -= XML_PARSE_DTDVALID; |
| 12251 | } else |
| 12252 | ctxt->validate = 0; |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12253 | #ifdef LIBXML_SAX1_ENABLED |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12254 | if (options & XML_PARSE_SAX1) { |
| 12255 | ctxt->sax->startElement = xmlSAX2StartElement; |
| 12256 | ctxt->sax->endElement = xmlSAX2EndElement; |
| 12257 | ctxt->sax->startElementNs = NULL; |
| 12258 | ctxt->sax->endElementNs = NULL; |
| 12259 | ctxt->sax->initialized = 1; |
| 12260 | options -= XML_PARSE_SAX1; |
| 12261 | } |
Daniel Veillard | 8127390 | 2003-09-30 00:43:48 +0000 | [diff] [blame] | 12262 | #endif /* LIBXML_SAX1_ENABLED */ |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12263 | if (options & XML_PARSE_NODICT) { |
| 12264 | ctxt->dictNames = 0; |
| 12265 | options -= XML_PARSE_NODICT; |
| 12266 | } else { |
| 12267 | ctxt->dictNames = 1; |
| 12268 | } |
Daniel Veillard | dca8cc7 | 2003-09-26 13:53:14 +0000 | [diff] [blame] | 12269 | if (options & XML_PARSE_NOCDATA) { |
| 12270 | ctxt->sax->cdataBlock = NULL; |
| 12271 | options -= XML_PARSE_NOCDATA; |
| 12272 | } |
| 12273 | if (options & XML_PARSE_NSCLEAN) { |
| 12274 | ctxt->options |= XML_PARSE_NSCLEAN; |
| 12275 | options -= XML_PARSE_NSCLEAN; |
| 12276 | } |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 12277 | if (options & XML_PARSE_NONET) { |
| 12278 | ctxt->options |= XML_PARSE_NONET; |
| 12279 | options -= XML_PARSE_NONET; |
| 12280 | } |
Daniel Veillard | 7ec2997 | 2003-10-31 14:36:36 +0000 | [diff] [blame] | 12281 | ctxt->linenumbers = 1; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12282 | return (options); |
| 12283 | } |
| 12284 | |
| 12285 | /** |
| 12286 | * xmlDoRead: |
| 12287 | * @ctxt: an XML parser context |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12288 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12289 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12290 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12291 | * @reuse: keep the context for reuse |
| 12292 | * |
| 12293 | * Common front-end for the xmlRead functions |
| 12294 | * |
| 12295 | * Returns the resulting document tree or NULL |
| 12296 | */ |
| 12297 | static xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12298 | xmlDoRead(xmlParserCtxtPtr ctxt, const char *URL, const char *encoding, |
| 12299 | int options, int reuse) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12300 | { |
| 12301 | xmlDocPtr ret; |
| 12302 | |
| 12303 | xmlCtxtUseOptions(ctxt, options); |
| 12304 | if (encoding != NULL) { |
| 12305 | xmlCharEncodingHandlerPtr hdlr; |
| 12306 | |
| 12307 | hdlr = xmlFindCharEncodingHandler(encoding); |
| 12308 | if (hdlr != NULL) |
| 12309 | xmlSwitchToEncoding(ctxt, hdlr); |
| 12310 | } |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12311 | if ((URL != NULL) && (ctxt->input != NULL) && |
| 12312 | (ctxt->input->filename == NULL)) |
| 12313 | ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12314 | xmlParseDocument(ctxt); |
| 12315 | if ((ctxt->wellFormed) || ctxt->recovery) |
| 12316 | ret = ctxt->myDoc; |
| 12317 | else { |
| 12318 | ret = NULL; |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12319 | if (ctxt->myDoc != NULL) { |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12320 | xmlFreeDoc(ctxt->myDoc); |
| 12321 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12322 | } |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12323 | ctxt->myDoc = NULL; |
| 12324 | if (!reuse) { |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12325 | xmlFreeParserCtxt(ctxt); |
Daniel Veillard | e96a2a4 | 2003-09-24 21:23:56 +0000 | [diff] [blame] | 12326 | } |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12327 | |
| 12328 | return (ret); |
| 12329 | } |
| 12330 | |
| 12331 | /** |
| 12332 | * xmlReadDoc: |
| 12333 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12334 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12335 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12336 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12337 | * |
| 12338 | * parse an XML in-memory document and build a tree. |
| 12339 | * |
| 12340 | * Returns the resulting document tree |
| 12341 | */ |
| 12342 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12343 | xmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12344 | { |
| 12345 | xmlParserCtxtPtr ctxt; |
| 12346 | |
| 12347 | if (cur == NULL) |
| 12348 | return (NULL); |
| 12349 | |
| 12350 | ctxt = xmlCreateDocParserCtxt(cur); |
| 12351 | if (ctxt == NULL) |
| 12352 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12353 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12354 | } |
| 12355 | |
| 12356 | /** |
| 12357 | * xmlReadFile: |
| 12358 | * @filename: a file or URL |
| 12359 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12360 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12361 | * |
| 12362 | * parse an XML file from the filesystem or the network. |
| 12363 | * |
| 12364 | * Returns the resulting document tree |
| 12365 | */ |
| 12366 | xmlDocPtr |
| 12367 | xmlReadFile(const char *filename, const char *encoding, int options) |
| 12368 | { |
| 12369 | xmlParserCtxtPtr ctxt; |
| 12370 | |
Daniel Veillard | 61b9338 | 2003-11-03 14:28:31 +0000 | [diff] [blame] | 12371 | ctxt = xmlCreateURLParserCtxt(filename, options); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12372 | if (ctxt == NULL) |
| 12373 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12374 | return (xmlDoRead(ctxt, NULL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12375 | } |
| 12376 | |
| 12377 | /** |
| 12378 | * xmlReadMemory: |
| 12379 | * @buffer: a pointer to a char array |
| 12380 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12381 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12382 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12383 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12384 | * |
| 12385 | * parse an XML in-memory document and build a tree. |
| 12386 | * |
| 12387 | * Returns the resulting document tree |
| 12388 | */ |
| 12389 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12390 | 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] | 12391 | { |
| 12392 | xmlParserCtxtPtr ctxt; |
| 12393 | |
| 12394 | ctxt = xmlCreateMemoryParserCtxt(buffer, size); |
| 12395 | if (ctxt == NULL) |
| 12396 | return (NULL); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12397 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12398 | } |
| 12399 | |
| 12400 | /** |
| 12401 | * xmlReadFd: |
| 12402 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12403 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12404 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12405 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12406 | * |
| 12407 | * parse an XML from a file descriptor and build a tree. |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 12408 | * NOTE that the file descriptor will not be closed when the |
| 12409 | * reader is closed or reset. |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12410 | * |
| 12411 | * Returns the resulting document tree |
| 12412 | */ |
| 12413 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12414 | xmlReadFd(int fd, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12415 | { |
| 12416 | xmlParserCtxtPtr ctxt; |
| 12417 | xmlParserInputBufferPtr input; |
| 12418 | xmlParserInputPtr stream; |
| 12419 | |
| 12420 | if (fd < 0) |
| 12421 | return (NULL); |
| 12422 | |
| 12423 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 12424 | if (input == NULL) |
| 12425 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 12426 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12427 | ctxt = xmlNewParserCtxt(); |
| 12428 | if (ctxt == NULL) { |
| 12429 | xmlFreeParserInputBuffer(input); |
| 12430 | return (NULL); |
| 12431 | } |
| 12432 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 12433 | if (stream == NULL) { |
| 12434 | xmlFreeParserInputBuffer(input); |
| 12435 | xmlFreeParserCtxt(ctxt); |
| 12436 | return (NULL); |
| 12437 | } |
| 12438 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12439 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12440 | } |
| 12441 | |
| 12442 | /** |
| 12443 | * xmlReadIO: |
| 12444 | * @ioread: an I/O read function |
| 12445 | * @ioclose: an I/O close function |
| 12446 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12447 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12448 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12449 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12450 | * |
| 12451 | * parse an XML document from I/O functions and source and build a tree. |
| 12452 | * |
| 12453 | * Returns the resulting document tree |
| 12454 | */ |
| 12455 | xmlDocPtr |
| 12456 | xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12457 | void *ioctx, const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12458 | { |
| 12459 | xmlParserCtxtPtr ctxt; |
| 12460 | xmlParserInputBufferPtr input; |
| 12461 | xmlParserInputPtr stream; |
| 12462 | |
| 12463 | if (ioread == NULL) |
| 12464 | return (NULL); |
| 12465 | |
| 12466 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 12467 | XML_CHAR_ENCODING_NONE); |
| 12468 | if (input == NULL) |
| 12469 | return (NULL); |
| 12470 | ctxt = xmlNewParserCtxt(); |
| 12471 | if (ctxt == NULL) { |
| 12472 | xmlFreeParserInputBuffer(input); |
| 12473 | return (NULL); |
| 12474 | } |
| 12475 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 12476 | if (stream == NULL) { |
| 12477 | xmlFreeParserInputBuffer(input); |
| 12478 | xmlFreeParserCtxt(ctxt); |
| 12479 | return (NULL); |
| 12480 | } |
| 12481 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12482 | return (xmlDoRead(ctxt, URL, encoding, options, 0)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12483 | } |
| 12484 | |
| 12485 | /** |
| 12486 | * xmlCtxtReadDoc: |
| 12487 | * @ctxt: an XML parser context |
| 12488 | * @cur: a pointer to a zero terminated string |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12489 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12490 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12491 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12492 | * |
| 12493 | * parse an XML in-memory document and build a tree. |
| 12494 | * This reuses the existing @ctxt parser context |
| 12495 | * |
| 12496 | * Returns the resulting document tree |
| 12497 | */ |
| 12498 | xmlDocPtr |
| 12499 | xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12500 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12501 | { |
| 12502 | xmlParserInputPtr stream; |
| 12503 | |
| 12504 | if (cur == NULL) |
| 12505 | return (NULL); |
| 12506 | if (ctxt == NULL) |
| 12507 | return (NULL); |
| 12508 | |
| 12509 | xmlCtxtReset(ctxt); |
| 12510 | |
| 12511 | stream = xmlNewStringInputStream(ctxt, cur); |
| 12512 | if (stream == NULL) { |
| 12513 | return (NULL); |
| 12514 | } |
| 12515 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12516 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12517 | } |
| 12518 | |
| 12519 | /** |
| 12520 | * xmlCtxtReadFile: |
| 12521 | * @ctxt: an XML parser context |
| 12522 | * @filename: a file or URL |
| 12523 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12524 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12525 | * |
| 12526 | * parse an XML file from the filesystem or the network. |
| 12527 | * This reuses the existing @ctxt parser context |
| 12528 | * |
| 12529 | * Returns the resulting document tree |
| 12530 | */ |
| 12531 | xmlDocPtr |
| 12532 | xmlCtxtReadFile(xmlParserCtxtPtr ctxt, const char *filename, |
| 12533 | const char *encoding, int options) |
| 12534 | { |
| 12535 | xmlParserInputPtr stream; |
| 12536 | |
| 12537 | if (filename == NULL) |
| 12538 | return (NULL); |
| 12539 | if (ctxt == NULL) |
| 12540 | return (NULL); |
| 12541 | |
| 12542 | xmlCtxtReset(ctxt); |
| 12543 | |
| 12544 | stream = xmlNewInputFromFile(ctxt, filename); |
| 12545 | if (stream == NULL) { |
| 12546 | return (NULL); |
| 12547 | } |
| 12548 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12549 | return (xmlDoRead(ctxt, NULL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12550 | } |
| 12551 | |
| 12552 | /** |
| 12553 | * xmlCtxtReadMemory: |
| 12554 | * @ctxt: an XML parser context |
| 12555 | * @buffer: a pointer to a char array |
| 12556 | * @size: the size of the array |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12557 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12558 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12559 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12560 | * |
| 12561 | * parse an XML in-memory document and build a tree. |
| 12562 | * This reuses the existing @ctxt parser context |
| 12563 | * |
| 12564 | * Returns the resulting document tree |
| 12565 | */ |
| 12566 | xmlDocPtr |
| 12567 | xmlCtxtReadMemory(xmlParserCtxtPtr ctxt, const char *buffer, int size, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12568 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12569 | { |
| 12570 | xmlParserInputBufferPtr input; |
| 12571 | xmlParserInputPtr stream; |
| 12572 | |
| 12573 | if (ctxt == NULL) |
| 12574 | return (NULL); |
| 12575 | if (buffer == NULL) |
| 12576 | return (NULL); |
| 12577 | |
| 12578 | xmlCtxtReset(ctxt); |
| 12579 | |
| 12580 | input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); |
| 12581 | if (input == NULL) { |
| 12582 | return(NULL); |
| 12583 | } |
| 12584 | |
| 12585 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 12586 | if (stream == NULL) { |
| 12587 | xmlFreeParserInputBuffer(input); |
| 12588 | return(NULL); |
| 12589 | } |
| 12590 | |
| 12591 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12592 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12593 | } |
| 12594 | |
| 12595 | /** |
| 12596 | * xmlCtxtReadFd: |
| 12597 | * @ctxt: an XML parser context |
| 12598 | * @fd: an open file descriptor |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12599 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12600 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12601 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12602 | * |
| 12603 | * parse an XML from a file descriptor and build a tree. |
| 12604 | * This reuses the existing @ctxt parser context |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 12605 | * NOTE that the file descriptor will not be closed when the |
| 12606 | * reader is closed or reset. |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12607 | * |
| 12608 | * Returns the resulting document tree |
| 12609 | */ |
| 12610 | xmlDocPtr |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12611 | xmlCtxtReadFd(xmlParserCtxtPtr ctxt, int fd, |
| 12612 | const char *URL, const char *encoding, int options) |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12613 | { |
| 12614 | xmlParserInputBufferPtr input; |
| 12615 | xmlParserInputPtr stream; |
| 12616 | |
| 12617 | if (fd < 0) |
| 12618 | return (NULL); |
| 12619 | if (ctxt == NULL) |
| 12620 | return (NULL); |
| 12621 | |
| 12622 | xmlCtxtReset(ctxt); |
| 12623 | |
| 12624 | |
| 12625 | input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
| 12626 | if (input == NULL) |
| 12627 | return (NULL); |
Daniel Veillard | 4bc5f43 | 2003-12-22 18:13:12 +0000 | [diff] [blame] | 12628 | input->closecallback = NULL; |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12629 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 12630 | if (stream == NULL) { |
| 12631 | xmlFreeParserInputBuffer(input); |
| 12632 | return (NULL); |
| 12633 | } |
| 12634 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12635 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12636 | } |
| 12637 | |
| 12638 | /** |
| 12639 | * xmlCtxtReadIO: |
| 12640 | * @ctxt: an XML parser context |
| 12641 | * @ioread: an I/O read function |
| 12642 | * @ioclose: an I/O close function |
| 12643 | * @ioctx: an I/O handler |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12644 | * @URL: the base URL to use for the document |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12645 | * @encoding: the document encoding, or NULL |
Daniel Veillard | 87ab1c1 | 2003-12-21 13:01:56 +0000 | [diff] [blame] | 12646 | * @options: a combination of xmlParserOption |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12647 | * |
| 12648 | * parse an XML document from I/O functions and source and build a tree. |
| 12649 | * This reuses the existing @ctxt parser context |
| 12650 | * |
| 12651 | * Returns the resulting document tree |
| 12652 | */ |
| 12653 | xmlDocPtr |
| 12654 | xmlCtxtReadIO(xmlParserCtxtPtr ctxt, xmlInputReadCallback ioread, |
| 12655 | xmlInputCloseCallback ioclose, void *ioctx, |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12656 | const char *URL, |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12657 | const char *encoding, int options) |
| 12658 | { |
| 12659 | xmlParserInputBufferPtr input; |
| 12660 | xmlParserInputPtr stream; |
| 12661 | |
| 12662 | if (ioread == NULL) |
| 12663 | return (NULL); |
| 12664 | if (ctxt == NULL) |
| 12665 | return (NULL); |
| 12666 | |
| 12667 | xmlCtxtReset(ctxt); |
| 12668 | |
| 12669 | input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, |
| 12670 | XML_CHAR_ENCODING_NONE); |
| 12671 | if (input == NULL) |
| 12672 | return (NULL); |
| 12673 | stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); |
| 12674 | if (stream == NULL) { |
| 12675 | xmlFreeParserInputBuffer(input); |
| 12676 | return (NULL); |
| 12677 | } |
| 12678 | inputPush(ctxt, stream); |
Daniel Veillard | 60942de | 2003-09-25 21:05:58 +0000 | [diff] [blame] | 12679 | return (xmlDoRead(ctxt, URL, encoding, options, 1)); |
Daniel Veillard | 16fa96c | 2003-09-23 21:50:54 +0000 | [diff] [blame] | 12680 | } |