blob: 0fbf719d350305429b094546edc3b96b489a6411 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
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 Veillardcbaf3992001-12-31 16:16:02 +000015 * high level APIs to call the parser and a few miscellaneous functions.
Owen Taylor3473f882001-02-23 17:55:21 +000016 * 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 Veillardcbaf3992001-12-31 16:16:02 +000025 * from the SAX callbacks or as standalone functions using a preparsed
Owen Taylor3473f882001-02-23 17:55:21 +000026 * document.
27 *
28 * See Copyright for the status of this software.
29 *
Daniel Veillardc5d64342001-06-24 12:13:24 +000030 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000031 */
32
Daniel Veillard34ce8be2002-03-18 19:37:11 +000033#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000034#include "libxml.h"
35
Daniel Veillard3c5ed912002-01-08 10:36:16 +000036#if defined(WIN32) && !defined (__CYGWIN__)
Owen Taylor3473f882001-02-23 17:55:21 +000037#define XML_DIR_SEP '\\'
38#else
Owen Taylor3473f882001-02-23 17:55:21 +000039#define XML_DIR_SEP '/'
40#endif
41
Owen Taylor3473f882001-02-23 17:55:21 +000042#include <stdlib.h>
43#include <string.h>
Aleksey Sanine7acf432003-10-02 20:05:27 +000044#include <stdarg.h>
Owen Taylor3473f882001-02-23 17:55:21 +000045#include <libxml/xmlmemory.h>
Daniel Veillardd0463562001-10-13 09:15:48 +000046#include <libxml/threads.h>
47#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000048#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 Veillard5d90b6c2001-08-22 14:29:45 +000057#ifdef LIBXML_CATALOG_ENABLED
58#include <libxml/catalog.h>
59#endif
Owen Taylor3473f882001-02-23 17:55:21 +000060
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 Veillard3b2e4e12003-02-03 08:52:58 +000080/**
Daniel Veillard4aede2e2003-10-17 12:43:59 +000081 * xmlParserMaxDepth:
Daniel Veillard3b2e4e12003-02-03 08:52:58 +000082 *
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 Veillard4aede2e2003-10-17 12:43:59 +000087unsigned int xmlParserMaxDepth = 1024;
Owen Taylor3473f882001-02-23 17:55:21 +000088
Daniel Veillard0fb18932003-09-07 09:14:37 +000089#define SAX2 1
90
Daniel Veillard21a0f912001-02-25 19:54:14 +000091#define XML_PARSER_BIG_BUFFER_SIZE 300
Owen Taylor3473f882001-02-23 17:55:21 +000092#define XML_PARSER_BUFFER_SIZE 100
93
Daniel Veillard5997aca2002-03-18 18:36:20 +000094#define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document"
95
Owen Taylor3473f882001-02-23 17:55:21 +000096/*
Owen Taylor3473f882001-02-23 17:55:21 +000097 * List of XML prefixed PI allowed by W3C specs
98 */
99
Daniel Veillardb44025c2001-10-11 22:55:55 +0000100static const char *xmlW3CPIs[] = {
Owen Taylor3473f882001-02-23 17:55:21 +0000101 "xml-stylesheet",
102 NULL
103};
104
Daniel Veillarda07050d2003-10-19 14:46:32 +0000105
Owen Taylor3473f882001-02-23 17:55:21 +0000106/* DEPR void xmlParserHandleReference(xmlParserCtxtPtr ctxt); */
Owen Taylor3473f882001-02-23 17:55:21 +0000107xmlEntityPtr xmlParseStringPEReference(xmlParserCtxtPtr ctxt,
108 const xmlChar **str);
109
Daniel Veillard7d515752003-09-26 19:12:37 +0000110static xmlParserErrors
Daniel Veillarda97a19b2001-05-20 13:19:52 +0000111xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt,
112 xmlSAXHandlerPtr sax,
Daniel Veillard257d9102001-05-08 10:41:44 +0000113 void *user_data, int depth, const xmlChar *URL,
Daniel Veillarda97a19b2001-05-20 13:19:52 +0000114 const xmlChar *ID, xmlNodePtr *list);
Owen Taylor3473f882001-02-23 17:55:21 +0000115
Daniel Veillard81273902003-09-30 00:43:48 +0000116#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +0000117static void
118xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode,
119 xmlNodePtr lastNode);
Daniel Veillard81273902003-09-30 00:43:48 +0000120#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillard8107a222002-01-13 14:10:10 +0000121
Daniel Veillard7d515752003-09-26 19:12:37 +0000122static xmlParserErrors
Daniel Veillard328f48c2002-11-15 15:24:34 +0000123xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt,
124 const xmlChar *string, void *user_data, xmlNodePtr *lst);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000125
126/************************************************************************
127 * *
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000128 * Some factorized error routines *
129 * *
130 ************************************************************************/
131
132/**
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000133 * 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 */
140static void
141xmlErrAttributeDup(xmlParserCtxtPtr ctxt, const xmlChar * prefix,
142 const xmlChar * localname)
143{
Daniel Veillard157fee02003-10-31 10:36:03 +0000144 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
145 (ctxt->instate == XML_PARSER_EOF))
146 return;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000147 ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000148 if (prefix == NULL)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000149 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000150 ctxt->errNo, XML_ERR_FATAL, NULL, 0,
151 (const char *) localname, NULL, NULL, 0, 0,
152 "Attribute %s redefined\n", localname);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000153 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000154 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000155 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 Veillard1afc9f32003-09-13 12:44:05 +0000159 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 */
172static void
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000173xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info)
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000174{
175 const char *errmsg;
176
Daniel Veillard157fee02003-10-31 10:36:03 +0000177 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
178 (ctxt->instate == XML_PARSER_EOF))
179 return;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000180 switch (error) {
181 case XML_ERR_INVALID_HEX_CHARREF:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000182 errmsg = "CharRef: invalid hexadecimal value\n";
183 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000184 case XML_ERR_INVALID_DEC_CHARREF:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000185 errmsg = "CharRef: invalid decimal value\n";
186 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000187 case XML_ERR_INVALID_CHARREF:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000188 errmsg = "CharRef: invalid value\n";
189 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000190 case XML_ERR_INTERNAL_ERROR:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000191 errmsg = "internal error";
192 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000193 case XML_ERR_PEREF_AT_EOF:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000194 errmsg = "PEReference at end of document\n";
195 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000196 case XML_ERR_PEREF_IN_PROLOG:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000197 errmsg = "PEReference in prolog\n";
198 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000199 case XML_ERR_PEREF_IN_EPILOG:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000200 errmsg = "PEReference in epilog\n";
201 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000202 case XML_ERR_PEREF_NO_NAME:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000203 errmsg = "PEReference: no name\n";
204 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000205 case XML_ERR_PEREF_SEMICOL_MISSING:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000206 errmsg = "PEReference: expecting ';'\n";
207 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000208 case XML_ERR_ENTITY_LOOP:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000209 errmsg = "Detected an entity reference loop\n";
210 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000211 case XML_ERR_ENTITY_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000212 errmsg = "EntityValue: \" or ' expected\n";
213 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000214 case XML_ERR_ENTITY_PE_INTERNAL:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000215 errmsg = "PEReferences forbidden in internal subset\n";
216 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000217 case XML_ERR_ENTITY_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000218 errmsg = "EntityValue: \" or ' expected\n";
219 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000220 case XML_ERR_ATTRIBUTE_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000221 errmsg = "AttValue: \" or ' expected\n";
222 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000223 case XML_ERR_LT_IN_ATTRIBUTE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000224 errmsg = "Unescaped '<' not allowed in attributes values\n";
225 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000226 case XML_ERR_LITERAL_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000227 errmsg = "SystemLiteral \" or ' expected\n";
228 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000229 case XML_ERR_LITERAL_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000230 errmsg = "Unfinished System or Public ID \" or ' expected\n";
231 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000232 case XML_ERR_MISPLACED_CDATA_END:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000233 errmsg = "Sequence ']]>' not allowed in content\n";
234 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000235 case XML_ERR_URI_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000236 errmsg = "SYSTEM or PUBLIC, the URI is missing\n";
237 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000238 case XML_ERR_PUBID_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000239 errmsg = "PUBLIC, the Public Identifier is missing\n";
240 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000241 case XML_ERR_HYPHEN_IN_COMMENT:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000242 errmsg = "Comment must not contain '--' (double-hyphen)\n";
243 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000244 case XML_ERR_PI_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000245 errmsg = "xmlParsePI : no target name\n";
246 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000247 case XML_ERR_RESERVED_XML_NAME:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000248 errmsg = "Invalid PI name\n";
249 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000250 case XML_ERR_NOTATION_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000251 errmsg = "NOTATION: Name expected here\n";
252 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000253 case XML_ERR_NOTATION_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000254 errmsg = "'>' required to close NOTATION declaration\n";
255 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000256 case XML_ERR_VALUE_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000257 errmsg = "Entity value required\n";
258 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000259 case XML_ERR_URI_FRAGMENT:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000260 errmsg = "Fragment not allowed";
261 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000262 case XML_ERR_ATTLIST_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000263 errmsg = "'(' required to start ATTLIST enumeration\n";
264 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000265 case XML_ERR_NMTOKEN_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000266 errmsg = "NmToken expected in ATTLIST enumeration\n";
267 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000268 case XML_ERR_ATTLIST_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000269 errmsg = "')' required to finish ATTLIST enumeration\n";
270 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000271 case XML_ERR_MIXED_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000272 errmsg = "MixedContentDecl : '|' or ')*' expected\n";
273 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000274 case XML_ERR_PCDATA_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000275 errmsg = "MixedContentDecl : '#PCDATA' expected\n";
276 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000277 case XML_ERR_ELEMCONTENT_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000278 errmsg = "ContentDecl : Name or '(' expected\n";
279 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000280 case XML_ERR_ELEMCONTENT_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000281 errmsg = "ContentDecl : ',' '|' or ')' expected\n";
282 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000283 case XML_ERR_PEREF_IN_INT_SUBSET:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000284 errmsg =
285 "PEReference: forbidden within markup decl in internal subset\n";
286 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000287 case XML_ERR_GT_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000288 errmsg = "expected '>'\n";
289 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000290 case XML_ERR_CONDSEC_INVALID:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000291 errmsg = "XML conditional section '[' expected\n";
292 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000293 case XML_ERR_EXT_SUBSET_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000294 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 Veillard1afc9f32003-09-13 12:44:05 +0000300 case XML_ERR_CONDSEC_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000301 errmsg = "XML conditional section not closed\n";
302 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000303 case XML_ERR_XMLDECL_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000304 errmsg = "Text declaration '<?xml' required\n";
305 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000306 case XML_ERR_XMLDECL_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000307 errmsg = "parsing XML declaration: '?>' expected\n";
308 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000309 case XML_ERR_EXT_ENTITY_STANDALONE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000310 errmsg = "external parsed entities cannot be standalone\n";
311 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000312 case XML_ERR_ENTITYREF_SEMICOL_MISSING:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000313 errmsg = "EntityRef: expecting ';'\n";
314 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000315 case XML_ERR_DOCTYPE_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000316 errmsg = "DOCTYPE improperly terminated\n";
317 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000318 case XML_ERR_LTSLASH_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000319 errmsg = "EndTag: '</' not found\n";
320 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000321 case XML_ERR_EQUAL_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000322 errmsg = "expected '='\n";
323 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000324 case XML_ERR_STRING_NOT_CLOSED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000325 errmsg = "String not closed expecting \" or '\n";
326 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000327 case XML_ERR_STRING_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000328 errmsg = "String not started expecting ' or \"\n";
329 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000330 case XML_ERR_ENCODING_NAME:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000331 errmsg = "Invalid XML encoding name\n";
332 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000333 case XML_ERR_STANDALONE_VALUE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000334 errmsg = "standalone accepts only 'yes' or 'no'\n";
335 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000336 case XML_ERR_DOCUMENT_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000337 errmsg = "Document is empty\n";
338 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000339 case XML_ERR_DOCUMENT_END:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000340 errmsg = "Extra content at the end of the document\n";
341 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000342 case XML_ERR_NOT_WELL_BALANCED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000343 errmsg = "chunk is not well balanced\n";
344 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000345 case XML_ERR_EXTRA_CONTENT:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000346 errmsg = "extra content at the end of well balanced chunk\n";
347 break;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000348 case XML_ERR_VERSION_MISSING:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000349 errmsg = "Malformed declaration expecting version\n";
350 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000351#if 0
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000352 case:
353 errmsg = "\n";
354 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000355#endif
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000356 default:
357 errmsg = "Unregistered error message\n";
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000358 }
359 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000360 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000361 XML_ERR_FATAL, NULL, 0, info, NULL, NULL, 0, 0, errmsg,
362 info);
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000363 ctxt->wellFormed = 0;
364 if (ctxt->recovery == 0)
365 ctxt->disableSAX = 1;
366}
367
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000368/**
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 */
376static void
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000377xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
378 const char *msg)
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000379{
Daniel Veillard157fee02003-10-31 10:36:03 +0000380 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
381 (ctxt->instate == XML_PARSER_EOF))
382 return;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000383 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000384 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000385 XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, msg);
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000386 ctxt->wellFormed = 0;
387 if (ctxt->recovery == 0)
388 ctxt->disableSAX = 1;
389}
390
391/**
Daniel Veillard24eb9782003-10-04 21:08:09 +0000392 * 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 */
401static void
402xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
403 const char *msg, const xmlChar *str1, const xmlChar *str2)
404{
Daniel Veillard9bcc7c52003-10-11 10:57:05 +0000405 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardc790bf42003-10-11 10:50:10 +0000406
Daniel Veillard157fee02003-10-31 10:36:03 +0000407 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
408 (ctxt->instate == XML_PARSER_EOF))
409 return;
Daniel Veillard24eb9782003-10-04 21:08:09 +0000410 ctxt->errNo = error;
Daniel Veillardc790bf42003-10-11 10:50:10 +0000411 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
Daniel Veillard9bcc7c52003-10-11 10:57:05 +0000412 schannel = ctxt->sax->serror;
413 __xmlRaiseError(schannel,
Daniel Veillard659e71e2003-10-10 14:10:40 +0000414 (ctxt->sax) ? ctxt->sax->warning : NULL,
415 ctxt->userData,
Daniel Veillard24eb9782003-10-04 21:08:09 +0000416 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 Veillardf88d8cf2003-12-08 10:25:02 +0000429 * Handle a validity error.
Daniel Veillard24eb9782003-10-04 21:08:09 +0000430 */
431static void
432xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error,
433 const char *msg, const xmlChar *str1)
434{
Daniel Veillard9bcc7c52003-10-11 10:57:05 +0000435 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard157fee02003-10-31 10:36:03 +0000436
437 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
438 (ctxt->instate == XML_PARSER_EOF))
439 return;
Daniel Veillard24eb9782003-10-04 21:08:09 +0000440 ctxt->errNo = error;
Daniel Veillardc790bf42003-10-11 10:50:10 +0000441 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
Daniel Veillard9bcc7c52003-10-11 10:57:05 +0000442 schannel = ctxt->sax->serror;
Daniel Veillardc790bf42003-10-11 10:50:10 +0000443 __xmlRaiseError(schannel,
Daniel Veillard659e71e2003-10-10 14:10:40 +0000444 ctxt->vctxt.error, ctxt->vctxt.userData,
Daniel Veillard24eb9782003-10-04 21:08:09 +0000445 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 Veillardbdbe0d42003-09-14 19:56:14 +0000453 * 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 */
461static void
462xmlFatalErrMsgInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000463 const char *msg, int val)
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000464{
Daniel Veillard157fee02003-10-31 10:36:03 +0000465 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
466 (ctxt->instate == XML_PARSER_EOF))
467 return;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000468 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000469 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000470 ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
471 NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000472 ctxt->wellFormed = 0;
473 if (ctxt->recovery == 0)
474 ctxt->disableSAX = 1;
475}
476
477/**
Daniel Veillardf403d292003-10-05 13:51:35 +0000478 * 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 */
488static void
489xmlFatalErrMsgStrIntStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
490 const char *msg, const xmlChar *str1, int val,
491 const xmlChar *str2)
492{
Daniel Veillard157fee02003-10-31 10:36:03 +0000493 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
494 (ctxt->instate == XML_PARSER_EOF))
495 return;
Daniel Veillardf403d292003-10-05 13:51:35 +0000496 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000497 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardf403d292003-10-05 13:51:35 +0000498 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 Veillardbc92eca2003-09-15 09:48:06 +0000507 * 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 */
515static void
516xmlFatalErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000517 const char *msg, const xmlChar * val)
Daniel Veillardbc92eca2003-09-15 09:48:06 +0000518{
Daniel Veillard157fee02003-10-31 10:36:03 +0000519 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
520 (ctxt->instate == XML_PARSER_EOF))
521 return;
Daniel Veillardbc92eca2003-09-15 09:48:06 +0000522 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000523 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000524 XML_FROM_PARSER, error, XML_ERR_FATAL,
525 NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg,
526 val);
Daniel Veillardbc92eca2003-09-15 09:48:06 +0000527 ctxt->wellFormed = 0;
528 if (ctxt->recovery == 0)
529 ctxt->disableSAX = 1;
530}
531
532/**
Daniel Veillardf403d292003-10-05 13:51:35 +0000533 * 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 */
541static void
542xmlErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
543 const char *msg, const xmlChar * val)
544{
Daniel Veillard157fee02003-10-31 10:36:03 +0000545 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
546 (ctxt->instate == XML_PARSER_EOF))
547 return;
Daniel Veillardf403d292003-10-05 13:51:35 +0000548 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000549 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL,
Daniel Veillardf403d292003-10-05 13:51:35 +0000550 XML_FROM_PARSER, error, XML_ERR_ERROR,
551 NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg,
552 val);
553}
554
555/**
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000556 * 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 */
565static void
566xmlNsErr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
567 const char *msg,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000568 const xmlChar * info1, const xmlChar * info2,
569 const xmlChar * info3)
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000570{
Daniel Veillard157fee02003-10-31 10:36:03 +0000571 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
572 (ctxt->instate == XML_PARSER_EOF))
573 return;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000574 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000575 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000576 XML_ERR_ERROR, NULL, 0, (const char *) info1,
577 (const char *) info2, (const char *) info3, 0, 0, msg,
578 info1, info2, info3);
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000579 ctxt->nsWellFormed = 0;
580}
581
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000582/************************************************************************
583 * *
Daniel Veillarde57ec792003-09-10 10:50:59 +0000584 * 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 */
594static void
595xmlDetectSAX2(xmlParserCtxtPtr ctxt) {
596 if (ctxt == NULL) return;
Daniel Veillard81273902003-09-30 00:43:48 +0000597#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +0000598 if ((ctxt->sax) && (ctxt->sax->initialized == XML_SAX2_MAGIC) &&
599 ((ctxt->sax->startElementNs != NULL) ||
600 (ctxt->sax->endElementNs != NULL))) ctxt->sax2 = 1;
Daniel Veillard81273902003-09-30 00:43:48 +0000601#else
602 ctxt->sax2 = 1;
603#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +0000604
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);
608}
609
Daniel Veillarde57ec792003-09-10 10:50:59 +0000610typedef struct _xmlDefAttrs xmlDefAttrs;
611typedef xmlDefAttrs *xmlDefAttrsPtr;
612struct _xmlDefAttrs {
613 int nbAttrs; /* number of defaulted attributes on that element */
614 int maxAttrs; /* the size of the array */
615 const xmlChar *values[4]; /* array of localname/prefix/values */
616};
Daniel Veillarde57ec792003-09-10 10:50:59 +0000617
618/**
619 * xmlAddDefAttrs:
620 * @ctxt: an XML parser context
621 * @fullname: the element fullname
622 * @fullattr: the attribute fullname
623 * @value: the attribute value
624 *
625 * Add a defaulted attribute for an element
626 */
627static void
628xmlAddDefAttrs(xmlParserCtxtPtr ctxt,
629 const xmlChar *fullname,
630 const xmlChar *fullattr,
631 const xmlChar *value) {
632 xmlDefAttrsPtr defaults;
633 int len;
634 const xmlChar *name;
635 const xmlChar *prefix;
636
637 if (ctxt->attsDefault == NULL) {
638 ctxt->attsDefault = xmlHashCreate(10);
639 if (ctxt->attsDefault == NULL)
640 goto mem_error;
641 }
642
643 /*
644 * plit the element name into prefix:localname , the string found
645 * are within the DTD and hen not associated to namespace names.
646 */
647 name = xmlSplitQName3(fullname, &len);
648 if (name == NULL) {
649 name = xmlDictLookup(ctxt->dict, fullname, -1);
650 prefix = NULL;
651 } else {
652 name = xmlDictLookup(ctxt->dict, name, -1);
653 prefix = xmlDictLookup(ctxt->dict, fullname, len);
654 }
655
656 /*
657 * make sure there is some storage
658 */
659 defaults = xmlHashLookup2(ctxt->attsDefault, name, prefix);
660 if (defaults == NULL) {
661 defaults = (xmlDefAttrsPtr) xmlMalloc(sizeof(xmlDefAttrs) +
662 12 * sizeof(const xmlChar *));
663 if (defaults == NULL)
664 goto mem_error;
665 defaults->maxAttrs = 4;
666 defaults->nbAttrs = 0;
667 xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, defaults, NULL);
668 } else if (defaults->nbAttrs >= defaults->maxAttrs) {
669 defaults = (xmlDefAttrsPtr) xmlRealloc(defaults, sizeof(xmlDefAttrs) +
670 (2 * defaults->maxAttrs * 4) * sizeof(const xmlChar *));
671 if (defaults == NULL)
672 goto mem_error;
673 defaults->maxAttrs *= 2;
674 xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, defaults, NULL);
675 }
676
677 /*
678 * plit the element name into prefix:localname , the string found
679 * are within the DTD and hen not associated to namespace names.
680 */
681 name = xmlSplitQName3(fullattr, &len);
682 if (name == NULL) {
683 name = xmlDictLookup(ctxt->dict, fullattr, -1);
684 prefix = NULL;
685 } else {
686 name = xmlDictLookup(ctxt->dict, name, -1);
687 prefix = xmlDictLookup(ctxt->dict, fullattr, len);
688 }
689
690 defaults->values[4 * defaults->nbAttrs] = name;
691 defaults->values[4 * defaults->nbAttrs + 1] = prefix;
692 /* intern the string and precompute the end */
693 len = xmlStrlen(value);
694 value = xmlDictLookup(ctxt->dict, value, len);
695 defaults->values[4 * defaults->nbAttrs + 2] = value;
696 defaults->values[4 * defaults->nbAttrs + 3] = value + len;
697 defaults->nbAttrs++;
698
699 return;
700
701mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000702 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000703 return;
704}
705
Daniel Veillard8e36e6a2003-09-10 10:50:59 +0000706/**
707 * xmlAddSpecialAttr:
708 * @ctxt: an XML parser context
709 * @fullname: the element fullname
710 * @fullattr: the attribute fullname
711 * @type: the attribute type
712 *
713 * Register that this attribute is not CDATA
714 */
715static void
716xmlAddSpecialAttr(xmlParserCtxtPtr ctxt,
717 const xmlChar *fullname,
718 const xmlChar *fullattr,
719 int type)
720{
721 if (ctxt->attsSpecial == NULL) {
722 ctxt->attsSpecial = xmlHashCreate(10);
723 if (ctxt->attsSpecial == NULL)
724 goto mem_error;
725 }
726
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000727 xmlHashAddEntry2(ctxt->attsSpecial, fullname, fullattr,
728 (void *) (long) type);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +0000729 return;
730
731mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000732 xmlErrMemory(ctxt, NULL);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +0000733 return;
734}
735
Daniel Veillard4432df22003-09-28 18:58:27 +0000736/**
737 * xmlCheckLanguageID:
738 * @lang: pointer to the string value
739 *
740 * Checks that the value conforms to the LanguageID production:
741 *
742 * NOTE: this is somewhat deprecated, those productions were removed from
743 * the XML Second edition.
744 *
745 * [33] LanguageID ::= Langcode ('-' Subcode)*
746 * [34] Langcode ::= ISO639Code | IanaCode | UserCode
747 * [35] ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z])
748 * [36] IanaCode ::= ('i' | 'I') '-' ([a-z] | [A-Z])+
749 * [37] UserCode ::= ('x' | 'X') '-' ([a-z] | [A-Z])+
750 * [38] Subcode ::= ([a-z] | [A-Z])+
751 *
752 * Returns 1 if correct 0 otherwise
753 **/
754int
755xmlCheckLanguageID(const xmlChar * lang)
756{
757 const xmlChar *cur = lang;
758
759 if (cur == NULL)
760 return (0);
761 if (((cur[0] == 'i') && (cur[1] == '-')) ||
762 ((cur[0] == 'I') && (cur[1] == '-'))) {
763 /*
764 * IANA code
765 */
766 cur += 2;
767 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
768 ((cur[0] >= 'a') && (cur[0] <= 'z')))
769 cur++;
770 } else if (((cur[0] == 'x') && (cur[1] == '-')) ||
771 ((cur[0] == 'X') && (cur[1] == '-'))) {
772 /*
773 * User code
774 */
775 cur += 2;
776 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
777 ((cur[0] >= 'a') && (cur[0] <= 'z')))
778 cur++;
779 } else if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
780 ((cur[0] >= 'a') && (cur[0] <= 'z'))) {
781 /*
782 * ISO639
783 */
784 cur++;
785 if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
786 ((cur[0] >= 'a') && (cur[0] <= 'z')))
787 cur++;
788 else
789 return (0);
790 } else
791 return (0);
792 while (cur[0] != 0) { /* non input consuming */
793 if (cur[0] != '-')
794 return (0);
795 cur++;
796 if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
797 ((cur[0] >= 'a') && (cur[0] <= 'z')))
798 cur++;
799 else
800 return (0);
801 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
802 ((cur[0] >= 'a') && (cur[0] <= 'z')))
803 cur++;
804 }
805 return (1);
806}
807
Owen Taylor3473f882001-02-23 17:55:21 +0000808/************************************************************************
809 * *
810 * Parser stacks related functions and macros *
811 * *
812 ************************************************************************/
813
814xmlEntityPtr xmlParseStringEntityRef(xmlParserCtxtPtr ctxt,
815 const xmlChar ** str);
816
Daniel Veillard0fb18932003-09-07 09:14:37 +0000817#ifdef SAX2
818/**
819 * nsPush:
820 * @ctxt: an XML parser context
821 * @prefix: the namespace prefix or NULL
822 * @URL: the namespace name
823 *
824 * Pushes a new parser namespace on top of the ns stack
825 *
William M. Brack7b9154b2003-09-27 19:23:50 +0000826 * Returns -1 in case of error, -2 if the namespace should be discarded
827 * and the index in the stack otherwise.
Daniel Veillard0fb18932003-09-07 09:14:37 +0000828 */
829static int
830nsPush(xmlParserCtxtPtr ctxt, const xmlChar *prefix, const xmlChar *URL)
831{
Daniel Veillarddca8cc72003-09-26 13:53:14 +0000832 if (ctxt->options & XML_PARSE_NSCLEAN) {
833 int i;
834 for (i = 0;i < ctxt->nsNr;i += 2) {
835 if (ctxt->nsTab[i] == prefix) {
836 /* in scope */
837 if (ctxt->nsTab[i + 1] == URL)
838 return(-2);
839 /* out of scope keep it */
840 break;
841 }
842 }
843 }
Daniel Veillard0fb18932003-09-07 09:14:37 +0000844 if ((ctxt->nsMax == 0) || (ctxt->nsTab == NULL)) {
845 ctxt->nsMax = 10;
846 ctxt->nsNr = 0;
847 ctxt->nsTab = (const xmlChar **)
848 xmlMalloc(ctxt->nsMax * sizeof(xmlChar *));
849 if (ctxt->nsTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000850 xmlErrMemory(ctxt, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +0000851 ctxt->nsMax = 0;
852 return (-1);
853 }
854 } else if (ctxt->nsNr >= ctxt->nsMax) {
855 ctxt->nsMax *= 2;
856 ctxt->nsTab = (const xmlChar **)
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000857 xmlRealloc((char *) ctxt->nsTab,
Daniel Veillard0fb18932003-09-07 09:14:37 +0000858 ctxt->nsMax * sizeof(ctxt->nsTab[0]));
859 if (ctxt->nsTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000860 xmlErrMemory(ctxt, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +0000861 ctxt->nsMax /= 2;
862 return (-1);
863 }
864 }
865 ctxt->nsTab[ctxt->nsNr++] = prefix;
866 ctxt->nsTab[ctxt->nsNr++] = URL;
867 return (ctxt->nsNr);
868}
869/**
870 * nsPop:
871 * @ctxt: an XML parser context
872 * @nr: the number to pop
873 *
874 * Pops the top @nr parser prefix/namespace from the ns stack
875 *
876 * Returns the number of namespaces removed
877 */
878static int
879nsPop(xmlParserCtxtPtr ctxt, int nr)
880{
881 int i;
882
883 if (ctxt->nsTab == NULL) return(0);
884 if (ctxt->nsNr < nr) {
885 xmlGenericError(xmlGenericErrorContext, "Pbm popping %d NS\n", nr);
886 nr = ctxt->nsNr;
887 }
888 if (ctxt->nsNr <= 0)
889 return (0);
890
891 for (i = 0;i < nr;i++) {
892 ctxt->nsNr--;
893 ctxt->nsTab[ctxt->nsNr] = NULL;
894 }
895 return(nr);
896}
897#endif
898
899static int
900xmlCtxtGrowAttrs(xmlParserCtxtPtr ctxt, int nr) {
901 const xmlChar **atts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000902 int *attallocs;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000903 int maxatts;
904
905 if (ctxt->atts == NULL) {
Daniel Veillarde57ec792003-09-10 10:50:59 +0000906 maxatts = 55; /* allow for 10 attrs by default */
Daniel Veillard0fb18932003-09-07 09:14:37 +0000907 atts = (const xmlChar **)
908 xmlMalloc(maxatts * sizeof(xmlChar *));
Daniel Veillarde57ec792003-09-10 10:50:59 +0000909 if (atts == NULL) goto mem_error;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000910 ctxt->atts = atts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000911 attallocs = (int *) xmlMalloc((maxatts / 5) * sizeof(int));
912 if (attallocs == NULL) goto mem_error;
913 ctxt->attallocs = attallocs;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000914 ctxt->maxatts = maxatts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000915 } else if (nr + 5 > ctxt->maxatts) {
916 maxatts = (nr + 5) * 2;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000917 atts = (const xmlChar **) xmlRealloc((void *) ctxt->atts,
918 maxatts * sizeof(const xmlChar *));
Daniel Veillarde57ec792003-09-10 10:50:59 +0000919 if (atts == NULL) goto mem_error;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000920 ctxt->atts = atts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000921 attallocs = (int *) xmlRealloc((void *) ctxt->attallocs,
922 (maxatts / 5) * sizeof(int));
923 if (attallocs == NULL) goto mem_error;
924 ctxt->attallocs = attallocs;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000925 ctxt->maxatts = maxatts;
926 }
927 return(ctxt->maxatts);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000928mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000929 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000930 return(-1);
Daniel Veillard0fb18932003-09-07 09:14:37 +0000931}
932
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000933/**
934 * inputPush:
935 * @ctxt: an XML parser context
Daniel Veillard9d06d302002-01-22 18:15:52 +0000936 * @value: the parser input
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000937 *
938 * Pushes a new parser input on top of the input stack
Daniel Veillard9d06d302002-01-22 18:15:52 +0000939 *
940 * Returns 0 in case of error, the index in the stack otherwise
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000941 */
Daniel Veillard1c732d22002-11-30 11:22:59 +0000942extern int
943inputPush(xmlParserCtxtPtr ctxt, xmlParserInputPtr value)
944{
945 if (ctxt->inputNr >= ctxt->inputMax) {
946 ctxt->inputMax *= 2;
947 ctxt->inputTab =
948 (xmlParserInputPtr *) xmlRealloc(ctxt->inputTab,
949 ctxt->inputMax *
950 sizeof(ctxt->inputTab[0]));
951 if (ctxt->inputTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000952 xmlErrMemory(ctxt, NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +0000953 return (0);
954 }
955 }
956 ctxt->inputTab[ctxt->inputNr] = value;
957 ctxt->input = value;
958 return (ctxt->inputNr++);
959}
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000960/**
Daniel Veillard1c732d22002-11-30 11:22:59 +0000961 * inputPop:
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000962 * @ctxt: an XML parser context
963 *
Daniel Veillard1c732d22002-11-30 11:22:59 +0000964 * Pops the top parser input from the input stack
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000965 *
Daniel Veillard1c732d22002-11-30 11:22:59 +0000966 * Returns the input just removed
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000967 */
Daniel Veillard1c732d22002-11-30 11:22:59 +0000968extern xmlParserInputPtr
969inputPop(xmlParserCtxtPtr ctxt)
970{
971 xmlParserInputPtr ret;
972
973 if (ctxt->inputNr <= 0)
974 return (0);
975 ctxt->inputNr--;
976 if (ctxt->inputNr > 0)
977 ctxt->input = ctxt->inputTab[ctxt->inputNr - 1];
978 else
979 ctxt->input = NULL;
980 ret = ctxt->inputTab[ctxt->inputNr];
981 ctxt->inputTab[ctxt->inputNr] = 0;
982 return (ret);
983}
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000984/**
985 * nodePush:
986 * @ctxt: an XML parser context
Daniel Veillard9d06d302002-01-22 18:15:52 +0000987 * @value: the element node
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000988 *
989 * Pushes a new element node on top of the node stack
Daniel Veillard9d06d302002-01-22 18:15:52 +0000990 *
991 * Returns 0 in case of error, the index in the stack otherwise
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000992 */
Daniel Veillard1c732d22002-11-30 11:22:59 +0000993extern int
994nodePush(xmlParserCtxtPtr ctxt, xmlNodePtr value)
995{
996 if (ctxt->nodeNr >= ctxt->nodeMax) {
997 ctxt->nodeMax *= 2;
998 ctxt->nodeTab =
999 (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
1000 ctxt->nodeMax *
1001 sizeof(ctxt->nodeTab[0]));
1002 if (ctxt->nodeTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001003 xmlErrMemory(ctxt, NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +00001004 return (0);
1005 }
1006 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00001007 if (((unsigned int) ctxt->nodeNr) > xmlParserMaxDepth) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001008 xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard4aede2e2003-10-17 12:43:59 +00001009 "Excessive depth in document: change xmlParserMaxDepth = %d\n",
1010 xmlParserMaxDepth);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00001011 ctxt->instate = XML_PARSER_EOF;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00001012 return(0);
1013 }
Daniel Veillard1c732d22002-11-30 11:22:59 +00001014 ctxt->nodeTab[ctxt->nodeNr] = value;
1015 ctxt->node = value;
1016 return (ctxt->nodeNr++);
1017}
1018/**
1019 * nodePop:
1020 * @ctxt: an XML parser context
1021 *
1022 * Pops the top element node from the node stack
1023 *
1024 * Returns the node just removed
Owen Taylor3473f882001-02-23 17:55:21 +00001025 */
Daniel Veillard1c732d22002-11-30 11:22:59 +00001026extern xmlNodePtr
1027nodePop(xmlParserCtxtPtr ctxt)
1028{
1029 xmlNodePtr ret;
1030
1031 if (ctxt->nodeNr <= 0)
1032 return (0);
1033 ctxt->nodeNr--;
1034 if (ctxt->nodeNr > 0)
1035 ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
1036 else
1037 ctxt->node = NULL;
1038 ret = ctxt->nodeTab[ctxt->nodeNr];
1039 ctxt->nodeTab[ctxt->nodeNr] = 0;
1040 return (ret);
1041}
1042/**
Daniel Veillarde57ec792003-09-10 10:50:59 +00001043 * nameNsPush:
1044 * @ctxt: an XML parser context
1045 * @value: the element name
1046 * @prefix: the element prefix
1047 * @URI: the element namespace name
1048 *
1049 * Pushes a new element name/prefix/URL on top of the name stack
1050 *
1051 * Returns -1 in case of error, the index in the stack otherwise
1052 */
1053static int
1054nameNsPush(xmlParserCtxtPtr ctxt, const xmlChar * value,
1055 const xmlChar *prefix, const xmlChar *URI, int nsNr)
1056{
1057 if (ctxt->nameNr >= ctxt->nameMax) {
1058 const xmlChar * *tmp;
1059 void **tmp2;
1060 ctxt->nameMax *= 2;
1061 tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab,
1062 ctxt->nameMax *
1063 sizeof(ctxt->nameTab[0]));
1064 if (tmp == NULL) {
1065 ctxt->nameMax /= 2;
1066 goto mem_error;
1067 }
1068 ctxt->nameTab = tmp;
1069 tmp2 = (void **) xmlRealloc((void * *)ctxt->pushTab,
1070 ctxt->nameMax * 3 *
1071 sizeof(ctxt->pushTab[0]));
1072 if (tmp2 == NULL) {
1073 ctxt->nameMax /= 2;
1074 goto mem_error;
1075 }
1076 ctxt->pushTab = tmp2;
1077 }
1078 ctxt->nameTab[ctxt->nameNr] = value;
1079 ctxt->name = value;
1080 ctxt->pushTab[ctxt->nameNr * 3] = (void *) prefix;
1081 ctxt->pushTab[ctxt->nameNr * 3 + 1] = (void *) URI;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001082 ctxt->pushTab[ctxt->nameNr * 3 + 2] = (void *) (long) nsNr;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001083 return (ctxt->nameNr++);
1084mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001085 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001086 return (-1);
1087}
1088/**
1089 * nameNsPop:
1090 * @ctxt: an XML parser context
1091 *
1092 * Pops the top element/prefix/URI name from the name stack
1093 *
1094 * Returns the name just removed
1095 */
1096static const xmlChar *
1097nameNsPop(xmlParserCtxtPtr ctxt)
1098{
1099 const xmlChar *ret;
1100
1101 if (ctxt->nameNr <= 0)
1102 return (0);
1103 ctxt->nameNr--;
1104 if (ctxt->nameNr > 0)
1105 ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
1106 else
1107 ctxt->name = NULL;
1108 ret = ctxt->nameTab[ctxt->nameNr];
1109 ctxt->nameTab[ctxt->nameNr] = NULL;
1110 return (ret);
1111}
1112
1113/**
Daniel Veillard1c732d22002-11-30 11:22:59 +00001114 * namePush:
1115 * @ctxt: an XML parser context
1116 * @value: the element name
1117 *
1118 * Pushes a new element name on top of the name stack
1119 *
Daniel Veillarde57ec792003-09-10 10:50:59 +00001120 * Returns -1 in case of error, the index in the stack otherwise
Daniel Veillard1c732d22002-11-30 11:22:59 +00001121 */
1122extern int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001123namePush(xmlParserCtxtPtr ctxt, const xmlChar * value)
Daniel Veillard1c732d22002-11-30 11:22:59 +00001124{
1125 if (ctxt->nameNr >= ctxt->nameMax) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00001126 const xmlChar * *tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001127 ctxt->nameMax *= 2;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001128 tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab,
Daniel Veillard1c732d22002-11-30 11:22:59 +00001129 ctxt->nameMax *
1130 sizeof(ctxt->nameTab[0]));
Daniel Veillarde57ec792003-09-10 10:50:59 +00001131 if (tmp == NULL) {
1132 ctxt->nameMax /= 2;
1133 goto mem_error;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001134 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001135 ctxt->nameTab = tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001136 }
1137 ctxt->nameTab[ctxt->nameNr] = value;
1138 ctxt->name = value;
1139 return (ctxt->nameNr++);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001140mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001141 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001142 return (-1);
Daniel Veillard1c732d22002-11-30 11:22:59 +00001143}
1144/**
1145 * namePop:
1146 * @ctxt: an XML parser context
1147 *
1148 * Pops the top element name from the name stack
1149 *
1150 * Returns the name just removed
1151 */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001152extern const xmlChar *
Daniel Veillard1c732d22002-11-30 11:22:59 +00001153namePop(xmlParserCtxtPtr ctxt)
1154{
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001155 const xmlChar *ret;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001156
1157 if (ctxt->nameNr <= 0)
1158 return (0);
1159 ctxt->nameNr--;
1160 if (ctxt->nameNr > 0)
1161 ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
1162 else
1163 ctxt->name = NULL;
1164 ret = ctxt->nameTab[ctxt->nameNr];
1165 ctxt->nameTab[ctxt->nameNr] = 0;
1166 return (ret);
1167}
Owen Taylor3473f882001-02-23 17:55:21 +00001168
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001169static int spacePush(xmlParserCtxtPtr ctxt, int val) {
Owen Taylor3473f882001-02-23 17:55:21 +00001170 if (ctxt->spaceNr >= ctxt->spaceMax) {
1171 ctxt->spaceMax *= 2;
1172 ctxt->spaceTab = (int *) xmlRealloc(ctxt->spaceTab,
1173 ctxt->spaceMax * sizeof(ctxt->spaceTab[0]));
1174 if (ctxt->spaceTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001175 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001176 return(0);
1177 }
1178 }
1179 ctxt->spaceTab[ctxt->spaceNr] = val;
1180 ctxt->space = &ctxt->spaceTab[ctxt->spaceNr];
1181 return(ctxt->spaceNr++);
1182}
1183
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001184static int spacePop(xmlParserCtxtPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00001185 int ret;
1186 if (ctxt->spaceNr <= 0) return(0);
1187 ctxt->spaceNr--;
1188 if (ctxt->spaceNr > 0)
1189 ctxt->space = &ctxt->spaceTab[ctxt->spaceNr - 1];
1190 else
1191 ctxt->space = NULL;
1192 ret = ctxt->spaceTab[ctxt->spaceNr];
1193 ctxt->spaceTab[ctxt->spaceNr] = -1;
1194 return(ret);
1195}
1196
1197/*
1198 * Macros for accessing the content. Those should be used only by the parser,
1199 * and not exported.
1200 *
1201 * Dirty macros, i.e. one often need to make assumption on the context to
1202 * use them
1203 *
1204 * CUR_PTR return the current pointer to the xmlChar to be parsed.
1205 * To be used with extreme caution since operations consuming
1206 * characters may move the input buffer to a different location !
1207 * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled
1208 * This should be used internally by the parser
1209 * only to compare to ASCII values otherwise it would break when
1210 * running with UTF-8 encoding.
1211 * RAW same as CUR but in the input buffer, bypass any token
1212 * extraction that may have been done
1213 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
1214 * to compare on ASCII based substring.
1215 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
Daniel Veillard77a90a72003-03-22 00:04:05 +00001216 * strings without newlines within the parser.
1217 * NEXT1(l) Skip 1 xmlChar, and must also be used only to skip 1 non-newline ASCII
1218 * defined char within the parser.
Owen Taylor3473f882001-02-23 17:55:21 +00001219 * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
1220 *
1221 * NEXT Skip to the next character, this does the proper decoding
1222 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
Daniel Veillard77a90a72003-03-22 00:04:05 +00001223 * NEXTL(l) Skip the current unicode character of l xmlChars long.
Owen Taylor3473f882001-02-23 17:55:21 +00001224 * CUR_CHAR(l) returns the current unicode character (int), set l
1225 * to the number of xmlChars used for the encoding [0-5].
1226 * CUR_SCHAR same but operate on a string instead of the context
1227 * COPY_BUF copy the current unicode char to the target buffer, increment
1228 * the index
1229 * GROW, SHRINK handling of input buffers
1230 */
1231
Daniel Veillardfdc91562002-07-01 21:52:03 +00001232#define RAW (*ctxt->input->cur)
1233#define CUR (*ctxt->input->cur)
Owen Taylor3473f882001-02-23 17:55:21 +00001234#define NXT(val) ctxt->input->cur[(val)]
1235#define CUR_PTR ctxt->input->cur
1236
Daniel Veillarda07050d2003-10-19 14:46:32 +00001237#define CMP4( s, c1, c2, c3, c4 ) \
1238 ( ((unsigned char *) s)[ 0 ] == c1 && ((unsigned char *) s)[ 1 ] == c2 && \
1239 ((unsigned char *) s)[ 2 ] == c3 && ((unsigned char *) s)[ 3 ] == c4 )
1240#define CMP5( s, c1, c2, c3, c4, c5 ) \
1241 ( CMP4( s, c1, c2, c3, c4 ) && ((unsigned char *) s)[ 4 ] == c5 )
1242#define CMP6( s, c1, c2, c3, c4, c5, c6 ) \
1243 ( CMP5( s, c1, c2, c3, c4, c5 ) && ((unsigned char *) s)[ 5 ] == c6 )
1244#define CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) \
1245 ( CMP6( s, c1, c2, c3, c4, c5, c6 ) && ((unsigned char *) s)[ 6 ] == c7 )
1246#define CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) \
1247 ( CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) && ((unsigned char *) s)[ 7 ] == c8 )
1248#define CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) \
1249 ( CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) && \
1250 ((unsigned char *) s)[ 8 ] == c9 )
1251#define CMP10( s, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 ) \
1252 ( CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) && \
1253 ((unsigned char *) s)[ 9 ] == c10 )
1254
Owen Taylor3473f882001-02-23 17:55:21 +00001255#define SKIP(val) do { \
Daniel Veillard77a90a72003-03-22 00:04:05 +00001256 ctxt->nbChars += (val),ctxt->input->cur += (val),ctxt->input->col+=(val); \
Owen Taylor3473f882001-02-23 17:55:21 +00001257 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
Daniel Veillard561b7f82002-03-20 21:55:57 +00001258 if ((*ctxt->input->cur == 0) && \
Owen Taylor3473f882001-02-23 17:55:21 +00001259 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \
1260 xmlPopInput(ctxt); \
1261 } while (0)
1262
Daniel Veillard0b787f32004-03-26 17:29:53 +00001263#define SKIPL(val) do { \
1264 int skipl; \
1265 for(skipl=0; skipl<val; skipl++) { \
1266 if (*(ctxt->input->cur) == '\n') { \
1267 ctxt->input->line++; ctxt->input->col = 1; \
1268 } else ctxt->input->col++; \
1269 ctxt->nbChars++; \
1270 ctxt->input->cur++; \
1271 } \
1272 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
1273 if ((*ctxt->input->cur == 0) && \
1274 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \
1275 xmlPopInput(ctxt); \
1276 } while (0)
1277
Daniel Veillarda880b122003-04-21 21:36:41 +00001278#define SHRINK if ((ctxt->progressive == 0) && \
Daniel Veillard6155d8a2003-08-19 15:01:28 +00001279 (ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \
1280 (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \
Daniel Veillard46de64e2002-05-29 08:21:33 +00001281 xmlSHRINK (ctxt);
1282
1283static void xmlSHRINK (xmlParserCtxtPtr ctxt) {
1284 xmlParserInputShrink(ctxt->input);
1285 if ((*ctxt->input->cur == 0) &&
1286 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
1287 xmlPopInput(ctxt);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001288 }
Owen Taylor3473f882001-02-23 17:55:21 +00001289
Daniel Veillarda880b122003-04-21 21:36:41 +00001290#define GROW if ((ctxt->progressive == 0) && \
1291 (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \
Daniel Veillard46de64e2002-05-29 08:21:33 +00001292 xmlGROW (ctxt);
1293
1294static void xmlGROW (xmlParserCtxtPtr ctxt) {
1295 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1296 if ((*ctxt->input->cur == 0) &&
1297 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
1298 xmlPopInput(ctxt);
Daniel Veillarda880b122003-04-21 21:36:41 +00001299}
Owen Taylor3473f882001-02-23 17:55:21 +00001300
1301#define SKIP_BLANKS xmlSkipBlankChars(ctxt)
1302
1303#define NEXT xmlNextChar(ctxt)
1304
Daniel Veillard21a0f912001-02-25 19:54:14 +00001305#define NEXT1 { \
Daniel Veillard77a90a72003-03-22 00:04:05 +00001306 ctxt->input->col++; \
Daniel Veillard21a0f912001-02-25 19:54:14 +00001307 ctxt->input->cur++; \
1308 ctxt->nbChars++; \
Daniel Veillard561b7f82002-03-20 21:55:57 +00001309 if (*ctxt->input->cur == 0) \
Daniel Veillard21a0f912001-02-25 19:54:14 +00001310 xmlParserInputGrow(ctxt->input, INPUT_CHUNK); \
1311 }
1312
Owen Taylor3473f882001-02-23 17:55:21 +00001313#define NEXTL(l) do { \
1314 if (*(ctxt->input->cur) == '\n') { \
1315 ctxt->input->line++; ctxt->input->col = 1; \
1316 } else ctxt->input->col++; \
Daniel Veillardfdc91562002-07-01 21:52:03 +00001317 ctxt->input->cur += l; \
Owen Taylor3473f882001-02-23 17:55:21 +00001318 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
Owen Taylor3473f882001-02-23 17:55:21 +00001319 } while (0)
1320
1321#define CUR_CHAR(l) xmlCurrentChar(ctxt, &l)
1322#define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l)
1323
1324#define COPY_BUF(l,b,i,v) \
1325 if (l == 1) b[i++] = (xmlChar) v; \
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001326 else i += xmlCopyCharMultiByte(&b[i],v)
Owen Taylor3473f882001-02-23 17:55:21 +00001327
1328/**
1329 * xmlSkipBlankChars:
1330 * @ctxt: the XML parser context
1331 *
1332 * skip all blanks character found at that point in the input streams.
1333 * It pops up finished entities in the process if allowable at that point.
1334 *
1335 * Returns the number of space chars skipped
1336 */
1337
1338int
1339xmlSkipBlankChars(xmlParserCtxtPtr ctxt) {
Daniel Veillard02141ea2001-04-30 11:46:40 +00001340 int res = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001341
1342 /*
1343 * It's Okay to use CUR/NEXT here since all the blanks are on
1344 * the ASCII range.
1345 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00001346 if ((ctxt->inputNr == 1) && (ctxt->instate != XML_PARSER_DTD)) {
1347 const xmlChar *cur;
Owen Taylor3473f882001-02-23 17:55:21 +00001348 /*
Daniel Veillard02141ea2001-04-30 11:46:40 +00001349 * if we are in the document content, go really fast
Owen Taylor3473f882001-02-23 17:55:21 +00001350 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00001351 cur = ctxt->input->cur;
William M. Brack76e95df2003-10-18 16:20:14 +00001352 while (IS_BLANK_CH(*cur)) {
Daniel Veillard02141ea2001-04-30 11:46:40 +00001353 if (*cur == '\n') {
1354 ctxt->input->line++; ctxt->input->col = 1;
1355 }
1356 cur++;
1357 res++;
1358 if (*cur == 0) {
1359 ctxt->input->cur = cur;
1360 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1361 cur = ctxt->input->cur;
1362 }
1363 }
1364 ctxt->input->cur = cur;
1365 } else {
1366 int cur;
1367 do {
1368 cur = CUR;
1369 while (IS_BLANK(cur)) { /* CHECKED tstblanks.xml */
1370 NEXT;
1371 cur = CUR;
1372 res++;
1373 }
1374 while ((cur == 0) && (ctxt->inputNr > 1) &&
1375 (ctxt->instate != XML_PARSER_COMMENT)) {
1376 xmlPopInput(ctxt);
1377 cur = CUR;
1378 }
1379 /*
1380 * Need to handle support of entities branching here
1381 */
1382 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt);
1383 } while (IS_BLANK(cur)); /* CHECKED tstblanks.xml */
1384 }
Owen Taylor3473f882001-02-23 17:55:21 +00001385 return(res);
1386}
1387
1388/************************************************************************
1389 * *
1390 * Commodity functions to handle entities *
1391 * *
1392 ************************************************************************/
1393
1394/**
1395 * xmlPopInput:
1396 * @ctxt: an XML parser context
1397 *
1398 * xmlPopInput: the current input pointed by ctxt->input came to an end
1399 * pop it and return the next char.
1400 *
1401 * Returns the current xmlChar in the parser context
1402 */
1403xmlChar
1404xmlPopInput(xmlParserCtxtPtr ctxt) {
1405 if (ctxt->inputNr == 1) return(0); /* End of main Input */
1406 if (xmlParserDebugEntities)
1407 xmlGenericError(xmlGenericErrorContext,
1408 "Popping input %d\n", ctxt->inputNr);
1409 xmlFreeInputStream(inputPop(ctxt));
Daniel Veillard561b7f82002-03-20 21:55:57 +00001410 if ((*ctxt->input->cur == 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +00001411 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
1412 return(xmlPopInput(ctxt));
1413 return(CUR);
1414}
1415
1416/**
1417 * xmlPushInput:
1418 * @ctxt: an XML parser context
1419 * @input: an XML parser input fragment (entity, XML fragment ...).
1420 *
1421 * xmlPushInput: switch to a new input stream which is stacked on top
1422 * of the previous one(s).
1423 */
1424void
1425xmlPushInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr input) {
1426 if (input == NULL) return;
1427
1428 if (xmlParserDebugEntities) {
1429 if ((ctxt->input != NULL) && (ctxt->input->filename))
1430 xmlGenericError(xmlGenericErrorContext,
1431 "%s(%d): ", ctxt->input->filename,
1432 ctxt->input->line);
1433 xmlGenericError(xmlGenericErrorContext,
1434 "Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur);
1435 }
1436 inputPush(ctxt, input);
1437 GROW;
1438}
1439
1440/**
1441 * xmlParseCharRef:
1442 * @ctxt: an XML parser context
1443 *
1444 * parse Reference declarations
1445 *
1446 * [66] CharRef ::= '&#' [0-9]+ ';' |
1447 * '&#x' [0-9a-fA-F]+ ';'
1448 *
1449 * [ WFC: Legal Character ]
1450 * Characters referred to using character references must match the
1451 * production for Char.
1452 *
1453 * Returns the value parsed (as an int), 0 in case of error
1454 */
1455int
1456xmlParseCharRef(xmlParserCtxtPtr ctxt) {
Daniel Veillard50582112001-03-26 22:52:16 +00001457 unsigned int val = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001458 int count = 0;
1459
Owen Taylor3473f882001-02-23 17:55:21 +00001460 /*
1461 * Using RAW/CUR/NEXT is okay since we are working on ASCII range here
1462 */
Daniel Veillard561b7f82002-03-20 21:55:57 +00001463 if ((RAW == '&') && (NXT(1) == '#') &&
Owen Taylor3473f882001-02-23 17:55:21 +00001464 (NXT(2) == 'x')) {
1465 SKIP(3);
1466 GROW;
1467 while (RAW != ';') { /* loop blocked by count */
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00001468 if (count++ > 20) {
1469 count = 0;
1470 GROW;
1471 }
1472 if ((RAW >= '0') && (RAW <= '9'))
Owen Taylor3473f882001-02-23 17:55:21 +00001473 val = val * 16 + (CUR - '0');
1474 else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20))
1475 val = val * 16 + (CUR - 'a') + 10;
1476 else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20))
1477 val = val * 16 + (CUR - 'A') + 10;
1478 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001479 xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001480 val = 0;
1481 break;
1482 }
1483 NEXT;
1484 count++;
1485 }
1486 if (RAW == ';') {
1487 /* on purpose to avoid reentrancy problems with NEXT and SKIP */
Daniel Veillard77a90a72003-03-22 00:04:05 +00001488 ctxt->input->col++;
Owen Taylor3473f882001-02-23 17:55:21 +00001489 ctxt->nbChars ++;
1490 ctxt->input->cur++;
1491 }
Daniel Veillard561b7f82002-03-20 21:55:57 +00001492 } else if ((RAW == '&') && (NXT(1) == '#')) {
Owen Taylor3473f882001-02-23 17:55:21 +00001493 SKIP(2);
1494 GROW;
1495 while (RAW != ';') { /* loop blocked by count */
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00001496 if (count++ > 20) {
1497 count = 0;
1498 GROW;
1499 }
1500 if ((RAW >= '0') && (RAW <= '9'))
Owen Taylor3473f882001-02-23 17:55:21 +00001501 val = val * 10 + (CUR - '0');
1502 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001503 xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001504 val = 0;
1505 break;
1506 }
1507 NEXT;
1508 count++;
1509 }
1510 if (RAW == ';') {
1511 /* on purpose to avoid reentrancy problems with NEXT and SKIP */
Daniel Veillard77a90a72003-03-22 00:04:05 +00001512 ctxt->input->col++;
Owen Taylor3473f882001-02-23 17:55:21 +00001513 ctxt->nbChars ++;
1514 ctxt->input->cur++;
1515 }
1516 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001517 xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001518 }
1519
1520 /*
1521 * [ WFC: Legal Character ]
1522 * Characters referred to using character references must match the
1523 * production for Char.
1524 */
William M. Brack871611b2003-10-18 04:53:14 +00001525 if (IS_CHAR(val)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001526 return(val);
1527 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001528 xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
1529 "xmlParseCharRef: invalid xmlChar value %d\n",
1530 val);
Owen Taylor3473f882001-02-23 17:55:21 +00001531 }
1532 return(0);
1533}
1534
1535/**
1536 * xmlParseStringCharRef:
1537 * @ctxt: an XML parser context
1538 * @str: a pointer to an index in the string
1539 *
1540 * parse Reference declarations, variant parsing from a string rather
1541 * than an an input flow.
1542 *
1543 * [66] CharRef ::= '&#' [0-9]+ ';' |
1544 * '&#x' [0-9a-fA-F]+ ';'
1545 *
1546 * [ WFC: Legal Character ]
1547 * Characters referred to using character references must match the
1548 * production for Char.
1549 *
1550 * Returns the value parsed (as an int), 0 in case of error, str will be
1551 * updated to the current value of the index
1552 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001553static int
Owen Taylor3473f882001-02-23 17:55:21 +00001554xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) {
1555 const xmlChar *ptr;
1556 xmlChar cur;
1557 int val = 0;
1558
1559 if ((str == NULL) || (*str == NULL)) return(0);
1560 ptr = *str;
1561 cur = *ptr;
1562 if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) {
1563 ptr += 3;
1564 cur = *ptr;
1565 while (cur != ';') { /* Non input consuming loop */
1566 if ((cur >= '0') && (cur <= '9'))
1567 val = val * 16 + (cur - '0');
1568 else if ((cur >= 'a') && (cur <= 'f'))
1569 val = val * 16 + (cur - 'a') + 10;
1570 else if ((cur >= 'A') && (cur <= 'F'))
1571 val = val * 16 + (cur - 'A') + 10;
1572 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001573 xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001574 val = 0;
1575 break;
1576 }
1577 ptr++;
1578 cur = *ptr;
1579 }
1580 if (cur == ';')
1581 ptr++;
1582 } else if ((cur == '&') && (ptr[1] == '#')){
1583 ptr += 2;
1584 cur = *ptr;
1585 while (cur != ';') { /* Non input consuming loops */
1586 if ((cur >= '0') && (cur <= '9'))
1587 val = val * 10 + (cur - '0');
1588 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001589 xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001590 val = 0;
1591 break;
1592 }
1593 ptr++;
1594 cur = *ptr;
1595 }
1596 if (cur == ';')
1597 ptr++;
1598 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001599 xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001600 return(0);
1601 }
1602 *str = ptr;
1603
1604 /*
1605 * [ WFC: Legal Character ]
1606 * Characters referred to using character references must match the
1607 * production for Char.
1608 */
William M. Brack871611b2003-10-18 04:53:14 +00001609 if (IS_CHAR(val)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001610 return(val);
1611 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001612 xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
1613 "xmlParseStringCharRef: invalid xmlChar value %d\n",
1614 val);
Owen Taylor3473f882001-02-23 17:55:21 +00001615 }
1616 return(0);
1617}
1618
1619/**
Daniel Veillardf5582f12002-06-11 10:08:16 +00001620 * xmlNewBlanksWrapperInputStream:
1621 * @ctxt: an XML parser context
1622 * @entity: an Entity pointer
1623 *
1624 * Create a new input stream for wrapping
1625 * blanks around a PEReference
1626 *
1627 * Returns the new input stream or NULL
1628 */
1629
1630static void deallocblankswrapper (xmlChar *str) {xmlFree(str);}
1631
Daniel Veillardf4862f02002-09-10 11:13:43 +00001632static xmlParserInputPtr
Daniel Veillardf5582f12002-06-11 10:08:16 +00001633xmlNewBlanksWrapperInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
1634 xmlParserInputPtr input;
1635 xmlChar *buffer;
1636 size_t length;
1637 if (entity == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001638 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
1639 "xmlNewBlanksWrapperInputStream entity\n");
Daniel Veillardf5582f12002-06-11 10:08:16 +00001640 return(NULL);
1641 }
1642 if (xmlParserDebugEntities)
1643 xmlGenericError(xmlGenericErrorContext,
1644 "new blanks wrapper for entity: %s\n", entity->name);
1645 input = xmlNewInputStream(ctxt);
1646 if (input == NULL) {
1647 return(NULL);
1648 }
1649 length = xmlStrlen(entity->name) + 5;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001650 buffer = xmlMallocAtomic(length);
Daniel Veillardf5582f12002-06-11 10:08:16 +00001651 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001652 xmlErrMemory(ctxt, NULL);
Daniel Veillardf5582f12002-06-11 10:08:16 +00001653 return(NULL);
1654 }
1655 buffer [0] = ' ';
1656 buffer [1] = '%';
1657 buffer [length-3] = ';';
1658 buffer [length-2] = ' ';
1659 buffer [length-1] = 0;
1660 memcpy(buffer + 2, entity->name, length - 5);
1661 input->free = deallocblankswrapper;
1662 input->base = buffer;
1663 input->cur = buffer;
1664 input->length = length;
1665 input->end = &buffer[length];
1666 return(input);
1667}
1668
1669/**
Owen Taylor3473f882001-02-23 17:55:21 +00001670 * xmlParserHandlePEReference:
1671 * @ctxt: the parser context
1672 *
1673 * [69] PEReference ::= '%' Name ';'
1674 *
1675 * [ WFC: No Recursion ]
1676 * A parsed entity must not contain a recursive
1677 * reference to itself, either directly or indirectly.
1678 *
1679 * [ WFC: Entity Declared ]
1680 * In a document without any DTD, a document with only an internal DTD
1681 * subset which contains no parameter entity references, or a document
1682 * with "standalone='yes'", ... ... The declaration of a parameter
1683 * entity must precede any reference to it...
1684 *
1685 * [ VC: Entity Declared ]
1686 * In a document with an external subset or external parameter entities
1687 * with "standalone='no'", ... ... The declaration of a parameter entity
1688 * must precede any reference to it...
1689 *
1690 * [ WFC: In DTD ]
1691 * Parameter-entity references may only appear in the DTD.
1692 * NOTE: misleading but this is handled.
1693 *
1694 * A PEReference may have been detected in the current input stream
1695 * the handling is done accordingly to
1696 * http://www.w3.org/TR/REC-xml#entproc
1697 * i.e.
1698 * - Included in literal in entity values
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001699 * - Included as Parameter Entity reference within DTDs
Owen Taylor3473f882001-02-23 17:55:21 +00001700 */
1701void
1702xmlParserHandlePEReference(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001703 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00001704 xmlEntityPtr entity = NULL;
1705 xmlParserInputPtr input;
1706
Owen Taylor3473f882001-02-23 17:55:21 +00001707 if (RAW != '%') return;
1708 switch(ctxt->instate) {
1709 case XML_PARSER_CDATA_SECTION:
1710 return;
1711 case XML_PARSER_COMMENT:
1712 return;
1713 case XML_PARSER_START_TAG:
1714 return;
1715 case XML_PARSER_END_TAG:
1716 return;
1717 case XML_PARSER_EOF:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001718 xmlFatalErr(ctxt, XML_ERR_PEREF_AT_EOF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001719 return;
1720 case XML_PARSER_PROLOG:
1721 case XML_PARSER_START:
1722 case XML_PARSER_MISC:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001723 xmlFatalErr(ctxt, XML_ERR_PEREF_IN_PROLOG, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001724 return;
1725 case XML_PARSER_ENTITY_DECL:
1726 case XML_PARSER_CONTENT:
1727 case XML_PARSER_ATTRIBUTE_VALUE:
1728 case XML_PARSER_PI:
1729 case XML_PARSER_SYSTEM_LITERAL:
Daniel Veillard4a7ae502002-02-18 19:18:17 +00001730 case XML_PARSER_PUBLIC_LITERAL:
Owen Taylor3473f882001-02-23 17:55:21 +00001731 /* we just ignore it there */
1732 return;
1733 case XML_PARSER_EPILOG:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001734 xmlFatalErr(ctxt, XML_ERR_PEREF_IN_EPILOG, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001735 return;
1736 case XML_PARSER_ENTITY_VALUE:
1737 /*
1738 * NOTE: in the case of entity values, we don't do the
1739 * substitution here since we need the literal
1740 * entity value to be able to save the internal
1741 * subset of the document.
1742 * This will be handled by xmlStringDecodeEntities
1743 */
1744 return;
1745 case XML_PARSER_DTD:
1746 /*
1747 * [WFC: Well-Formedness Constraint: PEs in Internal Subset]
1748 * In the internal DTD subset, parameter-entity references
1749 * can occur only where markup declarations can occur, not
1750 * within markup declarations.
1751 * In that case this is handled in xmlParseMarkupDecl
1752 */
1753 if ((ctxt->external == 0) && (ctxt->inputNr == 1))
1754 return;
William M. Brack76e95df2003-10-18 16:20:14 +00001755 if (IS_BLANK_CH(NXT(1)) || NXT(1) == 0)
Daniel Veillardf5582f12002-06-11 10:08:16 +00001756 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001757 break;
1758 case XML_PARSER_IGNORE:
1759 return;
1760 }
1761
1762 NEXT;
1763 name = xmlParseName(ctxt);
1764 if (xmlParserDebugEntities)
1765 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001766 "PEReference: %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00001767 if (name == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001768 xmlFatalErr(ctxt, XML_ERR_PEREF_NO_NAME, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001769 } else {
1770 if (RAW == ';') {
1771 NEXT;
1772 if ((ctxt->sax != NULL) && (ctxt->sax->getParameterEntity != NULL))
1773 entity = ctxt->sax->getParameterEntity(ctxt->userData, name);
1774 if (entity == NULL) {
1775
1776 /*
1777 * [ WFC: Entity Declared ]
1778 * In a document without any DTD, a document with only an
1779 * internal DTD subset which contains no parameter entity
1780 * references, or a document with "standalone='yes'", ...
1781 * ... The declaration of a parameter entity must precede
1782 * any reference to it...
1783 */
1784 if ((ctxt->standalone == 1) ||
1785 ((ctxt->hasExternalSubset == 0) &&
1786 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00001787 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00001788 "PEReference: %%%s; not found\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00001789 } else {
1790 /*
1791 * [ VC: Entity Declared ]
1792 * In a document with an external subset or external
1793 * parameter entities with "standalone='no'", ...
1794 * ... The declaration of a parameter entity must precede
1795 * any reference to it...
1796 */
Daniel Veillard24eb9782003-10-04 21:08:09 +00001797 if ((ctxt->validate) && (ctxt->vctxt.error != NULL)) {
1798 xmlValidityError(ctxt, XML_WAR_UNDECLARED_ENTITY,
1799 "PEReference: %%%s; not found\n",
1800 name);
1801 } else
1802 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
1803 "PEReference: %%%s; not found\n",
1804 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001805 ctxt->valid = 0;
1806 }
Daniel Veillardf5582f12002-06-11 10:08:16 +00001807 } else if (ctxt->input->free != deallocblankswrapper) {
1808 input = xmlNewBlanksWrapperInputStream(ctxt, entity);
1809 xmlPushInput(ctxt, input);
Owen Taylor3473f882001-02-23 17:55:21 +00001810 } else {
1811 if ((entity->etype == XML_INTERNAL_PARAMETER_ENTITY) ||
1812 (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY)) {
Daniel Veillard87a764e2001-06-20 17:41:10 +00001813 xmlChar start[4];
1814 xmlCharEncoding enc;
1815
Owen Taylor3473f882001-02-23 17:55:21 +00001816 /*
1817 * handle the extra spaces added before and after
1818 * c.f. http://www.w3.org/TR/REC-xml#as-PE
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001819 * this is done independently.
Owen Taylor3473f882001-02-23 17:55:21 +00001820 */
1821 input = xmlNewEntityInputStream(ctxt, entity);
1822 xmlPushInput(ctxt, input);
Daniel Veillard87a764e2001-06-20 17:41:10 +00001823
1824 /*
1825 * Get the 4 first bytes and decode the charset
1826 * if enc != XML_CHAR_ENCODING_NONE
1827 * plug some encoding conversion routines.
William M. Bracka0c48ad2004-04-16 15:58:29 +00001828 * Note that, since we may have some non-UTF8
1829 * encoding (like UTF16, bug 135229), the 'length'
1830 * is not known, but we can calculate based upon
1831 * the amount of data in the buffer.
Daniel Veillard87a764e2001-06-20 17:41:10 +00001832 */
1833 GROW
William M. Bracka0c48ad2004-04-16 15:58:29 +00001834 if ((ctxt->input->end - ctxt->input->cur)>=4) {
Daniel Veillarde059b892002-06-13 15:32:10 +00001835 start[0] = RAW;
1836 start[1] = NXT(1);
1837 start[2] = NXT(2);
1838 start[3] = NXT(3);
1839 enc = xmlDetectCharEncoding(start, 4);
1840 if (enc != XML_CHAR_ENCODING_NONE) {
1841 xmlSwitchEncoding(ctxt, enc);
1842 }
Daniel Veillard87a764e2001-06-20 17:41:10 +00001843 }
1844
Owen Taylor3473f882001-02-23 17:55:21 +00001845 if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) &&
Daniel Veillarda07050d2003-10-19 14:46:32 +00001846 (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l' )) &&
1847 (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001848 xmlParseTextDecl(ctxt);
1849 }
Owen Taylor3473f882001-02-23 17:55:21 +00001850 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00001851 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER,
1852 "PEReference: %s is not a parameter entity\n",
1853 name);
Owen Taylor3473f882001-02-23 17:55:21 +00001854 }
1855 }
1856 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001857 xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001858 }
Owen Taylor3473f882001-02-23 17:55:21 +00001859 }
1860}
1861
1862/*
1863 * Macro used to grow the current buffer.
1864 */
1865#define growBuffer(buffer) { \
Daniel Veillardd3999c72004-03-10 16:27:03 +00001866 xmlChar *tmp; \
Owen Taylor3473f882001-02-23 17:55:21 +00001867 buffer##_size *= 2; \
Daniel Veillardd3999c72004-03-10 16:27:03 +00001868 tmp = (xmlChar *) \
Owen Taylor3473f882001-02-23 17:55:21 +00001869 xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \
Daniel Veillardd3999c72004-03-10 16:27:03 +00001870 if (tmp == NULL) goto mem_error; \
1871 buffer = tmp; \
Owen Taylor3473f882001-02-23 17:55:21 +00001872}
1873
1874/**
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00001875 * xmlStringLenDecodeEntities:
Owen Taylor3473f882001-02-23 17:55:21 +00001876 * @ctxt: the parser context
1877 * @str: the input string
Daniel Veillarde57ec792003-09-10 10:50:59 +00001878 * @len: the string length
Owen Taylor3473f882001-02-23 17:55:21 +00001879 * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
1880 * @end: an end marker xmlChar, 0 if none
1881 * @end2: an end marker xmlChar, 0 if none
1882 * @end3: an end marker xmlChar, 0 if none
1883 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001884 * Takes a entity string content and process to do the adequate substitutions.
Owen Taylor3473f882001-02-23 17:55:21 +00001885 *
1886 * [67] Reference ::= EntityRef | CharRef
1887 *
1888 * [69] PEReference ::= '%' Name ';'
1889 *
1890 * Returns A newly allocated string with the substitution done. The caller
1891 * must deallocate it !
1892 */
1893xmlChar *
Daniel Veillarde57ec792003-09-10 10:50:59 +00001894xmlStringLenDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int len,
1895 int what, xmlChar end, xmlChar end2, xmlChar end3) {
Owen Taylor3473f882001-02-23 17:55:21 +00001896 xmlChar *buffer = NULL;
1897 int buffer_size = 0;
1898
1899 xmlChar *current = NULL;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001900 const xmlChar *last;
Owen Taylor3473f882001-02-23 17:55:21 +00001901 xmlEntityPtr ent;
1902 int c,l;
1903 int nbchars = 0;
1904
Daniel Veillarde57ec792003-09-10 10:50:59 +00001905 if ((str == NULL) || (len < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00001906 return(NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001907 last = str + len;
Owen Taylor3473f882001-02-23 17:55:21 +00001908
1909 if (ctxt->depth > 40) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001910 xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001911 return(NULL);
1912 }
1913
1914 /*
1915 * allocate a translation buffer.
1916 */
1917 buffer_size = XML_PARSER_BIG_BUFFER_SIZE;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001918 buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar));
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00001919 if (buffer == NULL) goto mem_error;
Owen Taylor3473f882001-02-23 17:55:21 +00001920
1921 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001922 * OK loop until we reach one of the ending char or a size limit.
Owen Taylor3473f882001-02-23 17:55:21 +00001923 * we are operating on already parsed values.
1924 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001925 if (str < last)
1926 c = CUR_SCHAR(str, l);
1927 else
1928 c = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001929 while ((c != 0) && (c != end) && /* non input consuming loop */
1930 (c != end2) && (c != end3)) {
1931
1932 if (c == 0) break;
1933 if ((c == '&') && (str[1] == '#')) {
1934 int val = xmlParseStringCharRef(ctxt, &str);
1935 if (val != 0) {
1936 COPY_BUF(0,buffer,nbchars,val);
1937 }
1938 } else if ((c == '&') && (what & XML_SUBSTITUTE_REF)) {
1939 if (xmlParserDebugEntities)
1940 xmlGenericError(xmlGenericErrorContext,
1941 "String decoding Entity Reference: %.30s\n",
1942 str);
1943 ent = xmlParseStringEntityRef(ctxt, &str);
1944 if ((ent != NULL) &&
1945 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1946 if (ent->content != NULL) {
1947 COPY_BUF(0,buffer,nbchars,ent->content[0]);
1948 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00001949 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
1950 "predefined entity has no content\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001951 }
1952 } else if ((ent != NULL) && (ent->content != NULL)) {
1953 xmlChar *rep;
1954
1955 ctxt->depth++;
1956 rep = xmlStringDecodeEntities(ctxt, ent->content, what,
1957 0, 0, 0);
1958 ctxt->depth--;
1959 if (rep != NULL) {
1960 current = rep;
1961 while (*current != 0) { /* non input consuming loop */
1962 buffer[nbchars++] = *current++;
1963 if (nbchars >
1964 buffer_size - XML_PARSER_BUFFER_SIZE) {
1965 growBuffer(buffer);
1966 }
1967 }
1968 xmlFree(rep);
1969 }
1970 } else if (ent != NULL) {
1971 int i = xmlStrlen(ent->name);
1972 const xmlChar *cur = ent->name;
1973
1974 buffer[nbchars++] = '&';
1975 if (nbchars > buffer_size - i - XML_PARSER_BUFFER_SIZE) {
1976 growBuffer(buffer);
1977 }
1978 for (;i > 0;i--)
1979 buffer[nbchars++] = *cur++;
1980 buffer[nbchars++] = ';';
1981 }
1982 } else if (c == '%' && (what & XML_SUBSTITUTE_PEREF)) {
1983 if (xmlParserDebugEntities)
1984 xmlGenericError(xmlGenericErrorContext,
1985 "String decoding PE Reference: %.30s\n", str);
1986 ent = xmlParseStringPEReference(ctxt, &str);
1987 if (ent != NULL) {
1988 xmlChar *rep;
1989
1990 ctxt->depth++;
1991 rep = xmlStringDecodeEntities(ctxt, ent->content, what,
1992 0, 0, 0);
1993 ctxt->depth--;
1994 if (rep != NULL) {
1995 current = rep;
1996 while (*current != 0) { /* non input consuming loop */
1997 buffer[nbchars++] = *current++;
1998 if (nbchars >
1999 buffer_size - XML_PARSER_BUFFER_SIZE) {
2000 growBuffer(buffer);
2001 }
2002 }
2003 xmlFree(rep);
2004 }
2005 }
2006 } else {
2007 COPY_BUF(l,buffer,nbchars,c);
2008 str += l;
2009 if (nbchars > buffer_size - XML_PARSER_BUFFER_SIZE) {
2010 growBuffer(buffer);
2011 }
2012 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002013 if (str < last)
2014 c = CUR_SCHAR(str, l);
2015 else
2016 c = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002017 }
2018 buffer[nbchars++] = 0;
2019 return(buffer);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002020
2021mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002022 xmlErrMemory(ctxt, NULL);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002023 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002024}
2025
Daniel Veillarde57ec792003-09-10 10:50:59 +00002026/**
2027 * xmlStringDecodeEntities:
2028 * @ctxt: the parser context
2029 * @str: the input string
2030 * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
2031 * @end: an end marker xmlChar, 0 if none
2032 * @end2: an end marker xmlChar, 0 if none
2033 * @end3: an end marker xmlChar, 0 if none
2034 *
2035 * Takes a entity string content and process to do the adequate substitutions.
2036 *
2037 * [67] Reference ::= EntityRef | CharRef
2038 *
2039 * [69] PEReference ::= '%' Name ';'
2040 *
2041 * Returns A newly allocated string with the substitution done. The caller
2042 * must deallocate it !
2043 */
2044xmlChar *
2045xmlStringDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int what,
2046 xmlChar end, xmlChar end2, xmlChar end3) {
2047 return(xmlStringLenDecodeEntities(ctxt, str, xmlStrlen(str), what,
2048 end, end2, end3));
2049}
Owen Taylor3473f882001-02-23 17:55:21 +00002050
2051/************************************************************************
2052 * *
Owen Taylor3473f882001-02-23 17:55:21 +00002053 * Commodity functions, cleanup needed ? *
2054 * *
2055 ************************************************************************/
2056
2057/**
2058 * areBlanks:
2059 * @ctxt: an XML parser context
2060 * @str: a xmlChar *
2061 * @len: the size of @str
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002062 * @blank_chars: we know the chars are blanks
Owen Taylor3473f882001-02-23 17:55:21 +00002063 *
2064 * Is this a sequence of blank chars that one can ignore ?
2065 *
2066 * Returns 1 if ignorable 0 otherwise.
2067 */
2068
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002069static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len,
2070 int blank_chars) {
Owen Taylor3473f882001-02-23 17:55:21 +00002071 int i, ret;
2072 xmlNodePtr lastChild;
2073
Daniel Veillard05c13a22001-09-09 08:38:09 +00002074 /*
2075 * Don't spend time trying to differentiate them, the same callback is
2076 * used !
2077 */
2078 if (ctxt->sax->ignorableWhitespace == ctxt->sax->characters)
Daniel Veillard2f362242001-03-02 17:36:21 +00002079 return(0);
2080
Owen Taylor3473f882001-02-23 17:55:21 +00002081 /*
2082 * Check for xml:space value.
2083 */
2084 if (*(ctxt->space) == 1)
2085 return(0);
2086
2087 /*
2088 * Check that the string is made of blanks
2089 */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002090 if (blank_chars == 0) {
2091 for (i = 0;i < len;i++)
2092 if (!(IS_BLANK_CH(str[i]))) return(0);
2093 }
Owen Taylor3473f882001-02-23 17:55:21 +00002094
2095 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002096 * Look if the element is mixed content in the DTD if available
Owen Taylor3473f882001-02-23 17:55:21 +00002097 */
Daniel Veillard6dd398f2001-07-25 22:41:03 +00002098 if (ctxt->node == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002099 if (ctxt->myDoc != NULL) {
2100 ret = xmlIsMixedElement(ctxt->myDoc, ctxt->node->name);
2101 if (ret == 0) return(1);
2102 if (ret == 1) return(0);
2103 }
2104
2105 /*
2106 * Otherwise, heuristic :-\
2107 */
Daniel Veillard561b7f82002-03-20 21:55:57 +00002108 if (RAW != '<') return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002109 if ((ctxt->node->children == NULL) &&
2110 (RAW == '<') && (NXT(1) == '/')) return(0);
2111
2112 lastChild = xmlGetLastChild(ctxt->node);
2113 if (lastChild == NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002114 if ((ctxt->node->type != XML_ELEMENT_NODE) &&
2115 (ctxt->node->content != NULL)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002116 } else if (xmlNodeIsText(lastChild))
2117 return(0);
2118 else if ((ctxt->node->children != NULL) &&
2119 (xmlNodeIsText(ctxt->node->children)))
2120 return(0);
2121 return(1);
2122}
2123
Owen Taylor3473f882001-02-23 17:55:21 +00002124/************************************************************************
2125 * *
2126 * Extra stuff for namespace support *
2127 * Relates to http://www.w3.org/TR/WD-xml-names *
2128 * *
2129 ************************************************************************/
2130
2131/**
2132 * xmlSplitQName:
2133 * @ctxt: an XML parser context
2134 * @name: an XML parser context
2135 * @prefix: a xmlChar **
2136 *
2137 * parse an UTF8 encoded XML qualified name string
2138 *
2139 * [NS 5] QName ::= (Prefix ':')? LocalPart
2140 *
2141 * [NS 6] Prefix ::= NCName
2142 *
2143 * [NS 7] LocalPart ::= NCName
2144 *
2145 * Returns the local part, and prefix is updated
2146 * to get the Prefix if any.
2147 */
2148
2149xmlChar *
2150xmlSplitQName(xmlParserCtxtPtr ctxt, const xmlChar *name, xmlChar **prefix) {
2151 xmlChar buf[XML_MAX_NAMELEN + 5];
2152 xmlChar *buffer = NULL;
2153 int len = 0;
2154 int max = XML_MAX_NAMELEN;
2155 xmlChar *ret = NULL;
2156 const xmlChar *cur = name;
2157 int c;
2158
2159 *prefix = NULL;
2160
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002161 if (cur == NULL) return(NULL);
2162
Owen Taylor3473f882001-02-23 17:55:21 +00002163#ifndef XML_XML_NAMESPACE
2164 /* xml: prefix is not really a namespace */
2165 if ((cur[0] == 'x') && (cur[1] == 'm') &&
2166 (cur[2] == 'l') && (cur[3] == ':'))
2167 return(xmlStrdup(name));
2168#endif
2169
Daniel Veillard597bc482003-07-24 16:08:28 +00002170 /* nasty but well=formed */
Owen Taylor3473f882001-02-23 17:55:21 +00002171 if (cur[0] == ':')
2172 return(xmlStrdup(name));
2173
2174 c = *cur++;
2175 while ((c != 0) && (c != ':') && (len < max)) { /* tested bigname.xml */
2176 buf[len++] = c;
2177 c = *cur++;
2178 }
2179 if (len >= max) {
2180 /*
2181 * Okay someone managed to make a huge name, so he's ready to pay
2182 * for the processing speed.
2183 */
2184 max = len * 2;
2185
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002186 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002187 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002188 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002189 return(NULL);
2190 }
2191 memcpy(buffer, buf, len);
2192 while ((c != 0) && (c != ':')) { /* tested bigname.xml */
2193 if (len + 10 > max) {
2194 max *= 2;
2195 buffer = (xmlChar *) xmlRealloc(buffer,
2196 max * sizeof(xmlChar));
2197 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002198 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002199 return(NULL);
2200 }
2201 }
2202 buffer[len++] = c;
2203 c = *cur++;
2204 }
2205 buffer[len] = 0;
2206 }
2207
Daniel Veillard597bc482003-07-24 16:08:28 +00002208 /* nasty but well=formed
2209 if ((c == ':') && (*cur == 0)) {
2210 return(xmlStrdup(name));
2211 } */
2212
Owen Taylor3473f882001-02-23 17:55:21 +00002213 if (buffer == NULL)
2214 ret = xmlStrndup(buf, len);
2215 else {
2216 ret = buffer;
2217 buffer = NULL;
2218 max = XML_MAX_NAMELEN;
2219 }
2220
2221
2222 if (c == ':') {
Daniel Veillardbb284f42002-10-16 18:02:47 +00002223 c = *cur;
Owen Taylor3473f882001-02-23 17:55:21 +00002224 *prefix = ret;
Daniel Veillard597bc482003-07-24 16:08:28 +00002225 if (c == 0) {
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00002226 return(xmlStrndup(BAD_CAST "", 0));
Daniel Veillard597bc482003-07-24 16:08:28 +00002227 }
Owen Taylor3473f882001-02-23 17:55:21 +00002228 len = 0;
2229
Daniel Veillardbb284f42002-10-16 18:02:47 +00002230 /*
2231 * Check that the first character is proper to start
2232 * a new name
2233 */
2234 if (!(((c >= 0x61) && (c <= 0x7A)) ||
2235 ((c >= 0x41) && (c <= 0x5A)) ||
2236 (c == '_') || (c == ':'))) {
2237 int l;
2238 int first = CUR_SCHAR(cur, l);
2239
2240 if (!IS_LETTER(first) && (first != '_')) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00002241 xmlFatalErrMsgStr(ctxt, XML_NS_ERR_QNAME,
Daniel Veillardbb284f42002-10-16 18:02:47 +00002242 "Name %s is not XML Namespace compliant\n",
Daniel Veillardbc92eca2003-09-15 09:48:06 +00002243 name);
Daniel Veillardbb284f42002-10-16 18:02:47 +00002244 }
2245 }
2246 cur++;
2247
Owen Taylor3473f882001-02-23 17:55:21 +00002248 while ((c != 0) && (len < max)) { /* tested bigname2.xml */
2249 buf[len++] = c;
2250 c = *cur++;
2251 }
2252 if (len >= max) {
2253 /*
2254 * Okay someone managed to make a huge name, so he's ready to pay
2255 * for the processing speed.
2256 */
2257 max = len * 2;
2258
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002259 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002260 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002261 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002262 return(NULL);
2263 }
2264 memcpy(buffer, buf, len);
2265 while (c != 0) { /* tested bigname2.xml */
2266 if (len + 10 > max) {
2267 max *= 2;
2268 buffer = (xmlChar *) xmlRealloc(buffer,
2269 max * sizeof(xmlChar));
2270 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002271 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002272 return(NULL);
2273 }
2274 }
2275 buffer[len++] = c;
2276 c = *cur++;
2277 }
2278 buffer[len] = 0;
2279 }
2280
2281 if (buffer == NULL)
2282 ret = xmlStrndup(buf, len);
2283 else {
2284 ret = buffer;
2285 }
2286 }
2287
2288 return(ret);
2289}
2290
2291/************************************************************************
2292 * *
2293 * The parser itself *
2294 * Relates to http://www.w3.org/TR/REC-xml *
2295 * *
2296 ************************************************************************/
2297
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002298static const xmlChar * xmlParseNameComplex(xmlParserCtxtPtr ctxt);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002299static xmlChar * xmlParseAttValueInternal(xmlParserCtxtPtr ctxt,
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002300 int *len, int *alloc, int normalize);
Daniel Veillard0fb18932003-09-07 09:14:37 +00002301
Owen Taylor3473f882001-02-23 17:55:21 +00002302/**
2303 * xmlParseName:
2304 * @ctxt: an XML parser context
2305 *
2306 * parse an XML name.
2307 *
2308 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
2309 * CombiningChar | Extender
2310 *
2311 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
2312 *
2313 * [6] Names ::= Name (S Name)*
2314 *
2315 * Returns the Name parsed or NULL
2316 */
2317
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002318const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002319xmlParseName(xmlParserCtxtPtr ctxt) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00002320 const xmlChar *in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002321 const xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00002322 int count = 0;
2323
2324 GROW;
Daniel Veillard48b2f892001-02-25 16:11:03 +00002325
2326 /*
2327 * Accelerator for simple ASCII names
2328 */
2329 in = ctxt->input->cur;
2330 if (((*in >= 0x61) && (*in <= 0x7A)) ||
2331 ((*in >= 0x41) && (*in <= 0x5A)) ||
2332 (*in == '_') || (*in == ':')) {
2333 in++;
2334 while (((*in >= 0x61) && (*in <= 0x7A)) ||
2335 ((*in >= 0x41) && (*in <= 0x5A)) ||
2336 ((*in >= 0x30) && (*in <= 0x39)) ||
Daniel Veillard76d66f42001-05-16 21:05:17 +00002337 (*in == '_') || (*in == '-') ||
2338 (*in == ':') || (*in == '.'))
Daniel Veillard48b2f892001-02-25 16:11:03 +00002339 in++;
Daniel Veillard76d66f42001-05-16 21:05:17 +00002340 if ((*in > 0) && (*in < 0x80)) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00002341 count = in - ctxt->input->cur;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002342 ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002343 ctxt->input->cur = in;
Daniel Veillard77a90a72003-03-22 00:04:05 +00002344 ctxt->nbChars += count;
2345 ctxt->input->col += count;
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002346 if (ret == NULL)
2347 xmlErrMemory(ctxt, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002348 return(ret);
2349 }
2350 }
Daniel Veillard2f362242001-03-02 17:36:21 +00002351 return(xmlParseNameComplex(ctxt));
Daniel Veillard21a0f912001-02-25 19:54:14 +00002352}
Daniel Veillard48b2f892001-02-25 16:11:03 +00002353
Daniel Veillard46de64e2002-05-29 08:21:33 +00002354/**
2355 * xmlParseNameAndCompare:
2356 * @ctxt: an XML parser context
2357 *
2358 * parse an XML name and compares for match
2359 * (specialized for endtag parsing)
2360 *
Daniel Veillard46de64e2002-05-29 08:21:33 +00002361 * Returns NULL for an illegal name, (xmlChar*) 1 for success
2362 * and the name for mismatch
2363 */
2364
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002365static const xmlChar *
Daniel Veillard46de64e2002-05-29 08:21:33 +00002366xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002367 register const xmlChar *cmp = other;
2368 register const xmlChar *in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002369 const xmlChar *ret;
Daniel Veillard46de64e2002-05-29 08:21:33 +00002370
2371 GROW;
2372
2373 in = ctxt->input->cur;
2374 while (*in != 0 && *in == *cmp) {
2375 ++in;
2376 ++cmp;
2377 }
William M. Brack76e95df2003-10-18 16:20:14 +00002378 if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
Daniel Veillard46de64e2002-05-29 08:21:33 +00002379 /* success */
2380 ctxt->input->cur = in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002381 return (const xmlChar*) 1;
Daniel Veillard46de64e2002-05-29 08:21:33 +00002382 }
2383 /* failure (or end of input buffer), check with full function */
2384 ret = xmlParseName (ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00002385 /* strings coming from the dictionnary direct compare possible */
2386 if (ret == other) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002387 return (const xmlChar*) 1;
Daniel Veillard46de64e2002-05-29 08:21:33 +00002388 }
2389 return ret;
2390}
2391
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002392static const xmlChar *
Daniel Veillard21a0f912001-02-25 19:54:14 +00002393xmlParseNameComplex(xmlParserCtxtPtr ctxt) {
Daniel Veillard21a0f912001-02-25 19:54:14 +00002394 int len = 0, l;
2395 int c;
2396 int count = 0;
2397
2398 /*
2399 * Handler for more complex cases
2400 */
2401 GROW;
Owen Taylor3473f882001-02-23 17:55:21 +00002402 c = CUR_CHAR(l);
2403 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
2404 (!IS_LETTER(c) && (c != '_') &&
2405 (c != ':'))) {
2406 return(NULL);
2407 }
2408
2409 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
William M. Brack871611b2003-10-18 04:53:14 +00002410 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
Owen Taylor3473f882001-02-23 17:55:21 +00002411 (c == '.') || (c == '-') ||
2412 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002413 (IS_COMBINING(c)) ||
2414 (IS_EXTENDER(c)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002415 if (count++ > 100) {
2416 count = 0;
2417 GROW;
2418 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002419 len += l;
Owen Taylor3473f882001-02-23 17:55:21 +00002420 NEXTL(l);
2421 c = CUR_CHAR(l);
Owen Taylor3473f882001-02-23 17:55:21 +00002422 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002423 return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len));
Owen Taylor3473f882001-02-23 17:55:21 +00002424}
2425
2426/**
2427 * xmlParseStringName:
2428 * @ctxt: an XML parser context
2429 * @str: a pointer to the string pointer (IN/OUT)
2430 *
2431 * parse an XML name.
2432 *
2433 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
2434 * CombiningChar | Extender
2435 *
2436 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
2437 *
2438 * [6] Names ::= Name (S Name)*
2439 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002440 * Returns the Name parsed or NULL. The @str pointer
Owen Taylor3473f882001-02-23 17:55:21 +00002441 * is updated to the current location in the string.
2442 */
2443
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002444static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002445xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) {
2446 xmlChar buf[XML_MAX_NAMELEN + 5];
2447 const xmlChar *cur = *str;
2448 int len = 0, l;
2449 int c;
2450
2451 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +00002452 if (!IS_LETTER(c) && (c != '_') &&
Owen Taylor3473f882001-02-23 17:55:21 +00002453 (c != ':')) {
2454 return(NULL);
2455 }
2456
William M. Brack871611b2003-10-18 04:53:14 +00002457 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigentname.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002458 (c == '.') || (c == '-') ||
2459 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002460 (IS_COMBINING(c)) ||
2461 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002462 COPY_BUF(l,buf,len,c);
2463 cur += l;
2464 c = CUR_SCHAR(cur, l);
2465 if (len >= XML_MAX_NAMELEN) { /* test bigentname.xml */
2466 /*
2467 * Okay someone managed to make a huge name, so he's ready to pay
2468 * for the processing speed.
2469 */
2470 xmlChar *buffer;
2471 int max = len * 2;
2472
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002473 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002474 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002475 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002476 return(NULL);
2477 }
2478 memcpy(buffer, buf, len);
William M. Brack871611b2003-10-18 04:53:14 +00002479 while ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
Daniel Veillard73b013f2003-09-30 12:36:01 +00002480 /* test bigentname.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002481 (c == '.') || (c == '-') ||
2482 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002483 (IS_COMBINING(c)) ||
2484 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002485 if (len + 10 > max) {
2486 max *= 2;
2487 buffer = (xmlChar *) xmlRealloc(buffer,
2488 max * sizeof(xmlChar));
2489 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002490 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002491 return(NULL);
2492 }
2493 }
2494 COPY_BUF(l,buffer,len,c);
2495 cur += l;
2496 c = CUR_SCHAR(cur, l);
2497 }
2498 buffer[len] = 0;
2499 *str = cur;
2500 return(buffer);
2501 }
2502 }
2503 *str = cur;
2504 return(xmlStrndup(buf, len));
2505}
2506
2507/**
2508 * xmlParseNmtoken:
2509 * @ctxt: an XML parser context
2510 *
2511 * parse an XML Nmtoken.
2512 *
2513 * [7] Nmtoken ::= (NameChar)+
2514 *
2515 * [8] Nmtokens ::= Nmtoken (S Nmtoken)*
2516 *
2517 * Returns the Nmtoken parsed or NULL
2518 */
2519
2520xmlChar *
2521xmlParseNmtoken(xmlParserCtxtPtr ctxt) {
2522 xmlChar buf[XML_MAX_NAMELEN + 5];
2523 int len = 0, l;
2524 int c;
2525 int count = 0;
2526
2527 GROW;
2528 c = CUR_CHAR(l);
2529
William M. Brack871611b2003-10-18 04:53:14 +00002530 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigtoken.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002531 (c == '.') || (c == '-') ||
2532 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002533 (IS_COMBINING(c)) ||
2534 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002535 if (count++ > 100) {
2536 count = 0;
2537 GROW;
2538 }
2539 COPY_BUF(l,buf,len,c);
2540 NEXTL(l);
2541 c = CUR_CHAR(l);
2542 if (len >= XML_MAX_NAMELEN) {
2543 /*
2544 * Okay someone managed to make a huge token, so he's ready to pay
2545 * for the processing speed.
2546 */
2547 xmlChar *buffer;
2548 int max = len * 2;
2549
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002550 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002551 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002552 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002553 return(NULL);
2554 }
2555 memcpy(buffer, buf, len);
William M. Brack871611b2003-10-18 04:53:14 +00002556 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigtoken.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002557 (c == '.') || (c == '-') ||
2558 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002559 (IS_COMBINING(c)) ||
2560 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002561 if (count++ > 100) {
2562 count = 0;
2563 GROW;
2564 }
2565 if (len + 10 > max) {
2566 max *= 2;
2567 buffer = (xmlChar *) xmlRealloc(buffer,
2568 max * sizeof(xmlChar));
2569 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002570 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002571 return(NULL);
2572 }
2573 }
2574 COPY_BUF(l,buffer,len,c);
2575 NEXTL(l);
2576 c = CUR_CHAR(l);
2577 }
2578 buffer[len] = 0;
2579 return(buffer);
2580 }
2581 }
2582 if (len == 0)
2583 return(NULL);
2584 return(xmlStrndup(buf, len));
2585}
2586
2587/**
2588 * xmlParseEntityValue:
2589 * @ctxt: an XML parser context
2590 * @orig: if non-NULL store a copy of the original entity value
2591 *
2592 * parse a value for ENTITY declarations
2593 *
2594 * [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' |
2595 * "'" ([^%&'] | PEReference | Reference)* "'"
2596 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002597 * Returns the EntityValue parsed with reference substituted or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002598 */
2599
2600xmlChar *
2601xmlParseEntityValue(xmlParserCtxtPtr ctxt, xmlChar **orig) {
2602 xmlChar *buf = NULL;
2603 int len = 0;
2604 int size = XML_PARSER_BUFFER_SIZE;
2605 int c, l;
2606 xmlChar stop;
2607 xmlChar *ret = NULL;
2608 const xmlChar *cur = NULL;
2609 xmlParserInputPtr input;
2610
2611 if (RAW == '"') stop = '"';
2612 else if (RAW == '\'') stop = '\'';
2613 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002614 xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002615 return(NULL);
2616 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002617 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002618 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002619 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002620 return(NULL);
2621 }
2622
2623 /*
2624 * The content of the entity definition is copied in a buffer.
2625 */
2626
2627 ctxt->instate = XML_PARSER_ENTITY_VALUE;
2628 input = ctxt->input;
2629 GROW;
2630 NEXT;
2631 c = CUR_CHAR(l);
2632 /*
2633 * NOTE: 4.4.5 Included in Literal
2634 * When a parameter entity reference appears in a literal entity
2635 * value, ... a single or double quote character in the replacement
2636 * text is always treated as a normal data character and will not
2637 * terminate the literal.
2638 * In practice it means we stop the loop only when back at parsing
2639 * the initial entity and the quote is found
2640 */
William M. Brack871611b2003-10-18 04:53:14 +00002641 while ((IS_CHAR(c)) && ((c != stop) || /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00002642 (ctxt->input != input))) {
2643 if (len + 5 >= size) {
2644 size *= 2;
2645 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
2646 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002647 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002648 return(NULL);
2649 }
2650 }
2651 COPY_BUF(l,buf,len,c);
2652 NEXTL(l);
2653 /*
2654 * Pop-up of finished entities.
2655 */
2656 while ((RAW == 0) && (ctxt->inputNr > 1)) /* non input consuming */
2657 xmlPopInput(ctxt);
2658
2659 GROW;
2660 c = CUR_CHAR(l);
2661 if (c == 0) {
2662 GROW;
2663 c = CUR_CHAR(l);
2664 }
2665 }
2666 buf[len] = 0;
2667
2668 /*
2669 * Raise problem w.r.t. '&' and '%' being used in non-entities
2670 * reference constructs. Note Charref will be handled in
2671 * xmlStringDecodeEntities()
2672 */
2673 cur = buf;
2674 while (*cur != 0) { /* non input consuming */
2675 if ((*cur == '%') || ((*cur == '&') && (cur[1] != '#'))) {
2676 xmlChar *name;
2677 xmlChar tmp = *cur;
2678
2679 cur++;
2680 name = xmlParseStringName(ctxt, &cur);
2681 if ((name == NULL) || (*cur != ';')) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00002682 xmlFatalErrMsgInt(ctxt, XML_ERR_ENTITY_CHAR_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00002683 "EntityValue: '%c' forbidden except for entities references\n",
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00002684 tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002685 }
Daniel Veillard5151c062001-10-23 13:10:19 +00002686 if ((tmp == '%') && (ctxt->inSubset == 1) &&
2687 (ctxt->inputNr == 1)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002688 xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002689 }
2690 if (name != NULL)
2691 xmlFree(name);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002692 if (*cur == 0)
2693 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002694 }
2695 cur++;
2696 }
2697
2698 /*
2699 * Then PEReference entities are substituted.
2700 */
2701 if (c != stop) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002702 xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002703 xmlFree(buf);
2704 } else {
2705 NEXT;
2706 /*
2707 * NOTE: 4.4.7 Bypassed
2708 * When a general entity reference appears in the EntityValue in
2709 * an entity declaration, it is bypassed and left as is.
2710 * so XML_SUBSTITUTE_REF is not set here.
2711 */
2712 ret = xmlStringDecodeEntities(ctxt, buf, XML_SUBSTITUTE_PEREF,
2713 0, 0, 0);
2714 if (orig != NULL)
2715 *orig = buf;
2716 else
2717 xmlFree(buf);
2718 }
2719
2720 return(ret);
2721}
2722
2723/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002724 * xmlParseAttValueComplex:
2725 * @ctxt: an XML parser context
Daniel Veillard0fb18932003-09-07 09:14:37 +00002726 * @len: the resulting attribute len
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002727 * @normalize: wether to apply the inner normalization
Daniel Veillard01c13b52002-12-10 15:19:08 +00002728 *
2729 * parse a value for an attribute, this is the fallback function
2730 * of xmlParseAttValue() when the attribute parsing requires handling
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002731 * of non-ASCII characters, or normalization compaction.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002732 *
2733 * Returns the AttValue parsed or NULL. The value has to be freed by the caller.
2734 */
Daniel Veillard0fb18932003-09-07 09:14:37 +00002735static xmlChar *
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002736xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) {
Daniel Veillarde72c7562002-05-31 09:47:30 +00002737 xmlChar limit = 0;
2738 xmlChar *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002739 int len = 0;
2740 int buf_size = 0;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002741 int c, l, in_space = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002742 xmlChar *current = NULL;
2743 xmlEntityPtr ent;
2744
Owen Taylor3473f882001-02-23 17:55:21 +00002745 if (NXT(0) == '"') {
2746 ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
2747 limit = '"';
2748 NEXT;
2749 } else if (NXT(0) == '\'') {
2750 limit = '\'';
2751 ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
2752 NEXT;
2753 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002754 xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002755 return(NULL);
2756 }
2757
2758 /*
2759 * allocate a translation buffer.
2760 */
2761 buf_size = XML_PARSER_BUFFER_SIZE;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002762 buf = (xmlChar *) xmlMallocAtomic(buf_size * sizeof(xmlChar));
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002763 if (buf == NULL) goto mem_error;
Owen Taylor3473f882001-02-23 17:55:21 +00002764
2765 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002766 * OK loop until we reach one of the ending char or a size limit.
Owen Taylor3473f882001-02-23 17:55:21 +00002767 */
2768 c = CUR_CHAR(l);
Daniel Veillardfdc91562002-07-01 21:52:03 +00002769 while ((NXT(0) != limit) && /* checked */
2770 (c != '<')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002771 if (c == 0) break;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002772 if (c == '&') {
Daniel Veillard62998c02003-09-15 12:56:36 +00002773 in_space = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002774 if (NXT(1) == '#') {
2775 int val = xmlParseCharRef(ctxt);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002776
Owen Taylor3473f882001-02-23 17:55:21 +00002777 if (val == '&') {
Daniel Veillard319a7422001-09-11 09:27:09 +00002778 if (ctxt->replaceEntities) {
2779 if (len > buf_size - 10) {
2780 growBuffer(buf);
2781 }
2782 buf[len++] = '&';
2783 } else {
2784 /*
2785 * The reparsing will be done in xmlStringGetNodeList()
2786 * called by the attribute() function in SAX.c
2787 */
Daniel Veillard319a7422001-09-11 09:27:09 +00002788 if (len > buf_size - 10) {
2789 growBuffer(buf);
2790 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002791 buf[len++] = '&';
2792 buf[len++] = '#';
2793 buf[len++] = '3';
2794 buf[len++] = '8';
2795 buf[len++] = ';';
Owen Taylor3473f882001-02-23 17:55:21 +00002796 }
2797 } else {
Daniel Veillard0b6b55b2001-03-20 11:27:34 +00002798 if (len > buf_size - 10) {
2799 growBuffer(buf);
2800 }
Owen Taylor3473f882001-02-23 17:55:21 +00002801 len += xmlCopyChar(0, &buf[len], val);
2802 }
2803 } else {
2804 ent = xmlParseEntityRef(ctxt);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002805 if ((ent != NULL) &&
2806 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
2807 if (len > buf_size - 10) {
2808 growBuffer(buf);
2809 }
2810 if ((ctxt->replaceEntities == 0) &&
2811 (ent->content[0] == '&')) {
2812 buf[len++] = '&';
2813 buf[len++] = '#';
2814 buf[len++] = '3';
2815 buf[len++] = '8';
2816 buf[len++] = ';';
2817 } else {
2818 buf[len++] = ent->content[0];
2819 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002820 } else if ((ent != NULL) &&
2821 (ctxt->replaceEntities != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002822 xmlChar *rep;
2823
2824 if (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) {
2825 rep = xmlStringDecodeEntities(ctxt, ent->content,
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002826 XML_SUBSTITUTE_REF,
2827 0, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002828 if (rep != NULL) {
2829 current = rep;
2830 while (*current != 0) { /* non input consuming */
2831 buf[len++] = *current++;
2832 if (len > buf_size - 10) {
2833 growBuffer(buf);
2834 }
2835 }
2836 xmlFree(rep);
2837 }
2838 } else {
Daniel Veillard0b6b55b2001-03-20 11:27:34 +00002839 if (len > buf_size - 10) {
2840 growBuffer(buf);
2841 }
Owen Taylor3473f882001-02-23 17:55:21 +00002842 if (ent->content != NULL)
2843 buf[len++] = ent->content[0];
2844 }
2845 } else if (ent != NULL) {
2846 int i = xmlStrlen(ent->name);
2847 const xmlChar *cur = ent->name;
2848
2849 /*
2850 * This may look absurd but is needed to detect
2851 * entities problems
2852 */
2853 if ((ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) &&
2854 (ent->content != NULL)) {
2855 xmlChar *rep;
2856 rep = xmlStringDecodeEntities(ctxt, ent->content,
2857 XML_SUBSTITUTE_REF, 0, 0, 0);
2858 if (rep != NULL)
2859 xmlFree(rep);
2860 }
2861
2862 /*
2863 * Just output the reference
2864 */
2865 buf[len++] = '&';
2866 if (len > buf_size - i - 10) {
2867 growBuffer(buf);
2868 }
2869 for (;i > 0;i--)
2870 buf[len++] = *cur++;
2871 buf[len++] = ';';
2872 }
2873 }
2874 } else {
2875 if ((c == 0x20) || (c == 0xD) || (c == 0xA) || (c == 0x9)) {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002876 if ((len != 0) || (!normalize)) {
2877 if ((!normalize) || (!in_space)) {
2878 COPY_BUF(l,buf,len,0x20);
2879 if (len > buf_size - 10) {
2880 growBuffer(buf);
2881 }
2882 }
2883 in_space = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002884 }
2885 } else {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002886 in_space = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002887 COPY_BUF(l,buf,len,c);
2888 if (len > buf_size - 10) {
2889 growBuffer(buf);
2890 }
2891 }
2892 NEXTL(l);
2893 }
2894 GROW;
2895 c = CUR_CHAR(l);
2896 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002897 if ((in_space) && (normalize)) {
2898 while (buf[len - 1] == 0x20) len--;
2899 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002900 buf[len] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002901 if (RAW == '<') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002902 xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002903 } else if (RAW != limit) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00002904 xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
2905 "AttValue: ' expected\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002906 } else
2907 NEXT;
Daniel Veillard0fb18932003-09-07 09:14:37 +00002908 if (attlen != NULL) *attlen = len;
Owen Taylor3473f882001-02-23 17:55:21 +00002909 return(buf);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002910
2911mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002912 xmlErrMemory(ctxt, NULL);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002913 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002914}
2915
2916/**
Daniel Veillard0fb18932003-09-07 09:14:37 +00002917 * xmlParseAttValue:
2918 * @ctxt: an XML parser context
2919 *
2920 * parse a value for an attribute
2921 * Note: the parser won't do substitution of entities here, this
2922 * will be handled later in xmlStringGetNodeList
2923 *
2924 * [10] AttValue ::= '"' ([^<&"] | Reference)* '"' |
2925 * "'" ([^<&'] | Reference)* "'"
2926 *
2927 * 3.3.3 Attribute-Value Normalization:
2928 * Before the value of an attribute is passed to the application or
2929 * checked for validity, the XML processor must normalize it as follows:
2930 * - a character reference is processed by appending the referenced
2931 * character to the attribute value
2932 * - an entity reference is processed by recursively processing the
2933 * replacement text of the entity
2934 * - a whitespace character (#x20, #xD, #xA, #x9) is processed by
2935 * appending #x20 to the normalized value, except that only a single
2936 * #x20 is appended for a "#xD#xA" sequence that is part of an external
2937 * parsed entity or the literal entity value of an internal parsed entity
2938 * - other characters are processed by appending them to the normalized value
2939 * If the declared value is not CDATA, then the XML processor must further
2940 * process the normalized attribute value by discarding any leading and
2941 * trailing space (#x20) characters, and by replacing sequences of space
2942 * (#x20) characters by a single space (#x20) character.
2943 * All attributes for which no declaration has been read should be treated
2944 * by a non-validating parser as if declared CDATA.
2945 *
2946 * Returns the AttValue parsed or NULL. The value has to be freed by the caller.
2947 */
2948
2949
2950xmlChar *
2951xmlParseAttValue(xmlParserCtxtPtr ctxt) {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002952 return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0));
Daniel Veillard0fb18932003-09-07 09:14:37 +00002953}
2954
2955/**
Owen Taylor3473f882001-02-23 17:55:21 +00002956 * xmlParseSystemLiteral:
2957 * @ctxt: an XML parser context
2958 *
2959 * parse an XML Literal
2960 *
2961 * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
2962 *
2963 * Returns the SystemLiteral parsed or NULL
2964 */
2965
2966xmlChar *
2967xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) {
2968 xmlChar *buf = NULL;
2969 int len = 0;
2970 int size = XML_PARSER_BUFFER_SIZE;
2971 int cur, l;
2972 xmlChar stop;
2973 int state = ctxt->instate;
2974 int count = 0;
2975
2976 SHRINK;
2977 if (RAW == '"') {
2978 NEXT;
2979 stop = '"';
2980 } else if (RAW == '\'') {
2981 NEXT;
2982 stop = '\'';
2983 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002984 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002985 return(NULL);
2986 }
2987
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002988 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002989 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002990 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002991 return(NULL);
2992 }
2993 ctxt->instate = XML_PARSER_SYSTEM_LITERAL;
2994 cur = CUR_CHAR(l);
William M. Brack871611b2003-10-18 04:53:14 +00002995 while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00002996 if (len + 5 >= size) {
2997 size *= 2;
2998 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
2999 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003000 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003001 ctxt->instate = (xmlParserInputState) state;
3002 return(NULL);
3003 }
3004 }
3005 count++;
3006 if (count > 50) {
3007 GROW;
3008 count = 0;
3009 }
3010 COPY_BUF(l,buf,len,cur);
3011 NEXTL(l);
3012 cur = CUR_CHAR(l);
3013 if (cur == 0) {
3014 GROW;
3015 SHRINK;
3016 cur = CUR_CHAR(l);
3017 }
3018 }
3019 buf[len] = 0;
3020 ctxt->instate = (xmlParserInputState) state;
William M. Brack871611b2003-10-18 04:53:14 +00003021 if (!IS_CHAR(cur)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003022 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003023 } else {
3024 NEXT;
3025 }
3026 return(buf);
3027}
3028
3029/**
3030 * xmlParsePubidLiteral:
3031 * @ctxt: an XML parser context
3032 *
3033 * parse an XML public literal
3034 *
3035 * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
3036 *
3037 * Returns the PubidLiteral parsed or NULL.
3038 */
3039
3040xmlChar *
3041xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) {
3042 xmlChar *buf = NULL;
3043 int len = 0;
3044 int size = XML_PARSER_BUFFER_SIZE;
3045 xmlChar cur;
3046 xmlChar stop;
3047 int count = 0;
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003048 xmlParserInputState oldstate = ctxt->instate;
Owen Taylor3473f882001-02-23 17:55:21 +00003049
3050 SHRINK;
3051 if (RAW == '"') {
3052 NEXT;
3053 stop = '"';
3054 } else if (RAW == '\'') {
3055 NEXT;
3056 stop = '\'';
3057 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003058 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003059 return(NULL);
3060 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003061 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003062 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003063 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003064 return(NULL);
3065 }
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003066 ctxt->instate = XML_PARSER_PUBLIC_LITERAL;
Owen Taylor3473f882001-02-23 17:55:21 +00003067 cur = CUR;
William M. Brack76e95df2003-10-18 16:20:14 +00003068 while ((IS_PUBIDCHAR_CH(cur)) && (cur != stop)) { /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003069 if (len + 1 >= size) {
3070 size *= 2;
3071 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3072 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003073 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003074 return(NULL);
3075 }
3076 }
3077 buf[len++] = cur;
3078 count++;
3079 if (count > 50) {
3080 GROW;
3081 count = 0;
3082 }
3083 NEXT;
3084 cur = CUR;
3085 if (cur == 0) {
3086 GROW;
3087 SHRINK;
3088 cur = CUR;
3089 }
3090 }
3091 buf[len] = 0;
3092 if (cur != stop) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003093 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003094 } else {
3095 NEXT;
3096 }
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003097 ctxt->instate = oldstate;
Owen Taylor3473f882001-02-23 17:55:21 +00003098 return(buf);
3099}
3100
Daniel Veillard48b2f892001-02-25 16:11:03 +00003101void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata);
Owen Taylor3473f882001-02-23 17:55:21 +00003102/**
3103 * xmlParseCharData:
3104 * @ctxt: an XML parser context
3105 * @cdata: int indicating whether we are within a CDATA section
3106 *
3107 * parse a CharData section.
3108 * if we are within a CDATA section ']]>' marks an end of section.
3109 *
3110 * The right angle bracket (>) may be represented using the string "&gt;",
3111 * and must, for compatibility, be escaped using "&gt;" or a character
3112 * reference when it appears in the string "]]>" in content, when that
3113 * string is not marking the end of a CDATA section.
3114 *
3115 * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
3116 */
3117
3118void
3119xmlParseCharData(xmlParserCtxtPtr ctxt, int cdata) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00003120 const xmlChar *in;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003121 int nbchar = 0;
Daniel Veillard50582112001-03-26 22:52:16 +00003122 int line = ctxt->input->line;
3123 int col = ctxt->input->col;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003124
3125 SHRINK;
3126 GROW;
3127 /*
3128 * Accelerated common case where input don't need to be
3129 * modified before passing it to the handler.
3130 */
Daniel Veillardfdc91562002-07-01 21:52:03 +00003131 if (!cdata) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00003132 in = ctxt->input->cur;
3133 do {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003134get_more_space:
3135 while (*in == 0x20) in++;
3136 if (*in == 0xA) {
3137 ctxt->input->line++;
3138 in++;
3139 while (*in == 0xA) {
3140 ctxt->input->line++;
3141 in++;
3142 }
3143 goto get_more_space;
3144 }
3145 if (*in == '<') {
3146 nbchar = in - ctxt->input->cur;
3147 if (nbchar > 0) {
3148 const xmlChar *tmp = ctxt->input->cur;
3149 ctxt->input->cur = in;
3150
3151 if (ctxt->sax->ignorableWhitespace !=
3152 ctxt->sax->characters) {
3153 if (areBlanks(ctxt, tmp, nbchar, 1)) {
3154 ctxt->sax->ignorableWhitespace(ctxt->userData,
3155 tmp, nbchar);
3156 } else if (ctxt->sax->characters != NULL)
3157 ctxt->sax->characters(ctxt->userData,
3158 tmp, nbchar);
3159 } else if (ctxt->sax->characters != NULL) {
3160 ctxt->sax->characters(ctxt->userData,
3161 tmp, nbchar);
3162 }
3163 }
3164 return;
3165 }
Daniel Veillard3ed155f2001-04-29 19:56:59 +00003166get_more:
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003167 while (((*in > ']') && (*in <= 0x7F)) ||
3168 ((*in > '&') && (*in < '<')) ||
3169 ((*in > '<') && (*in < ']')) ||
3170 ((*in >= 0x20) && (*in < '&')) ||
3171 (*in == 0x09))
3172 in++;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003173 if (*in == 0xA) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00003174 ctxt->input->line++;
Daniel Veillard3ed155f2001-04-29 19:56:59 +00003175 in++;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003176 while (*in == 0xA) {
Daniel Veillard3ed155f2001-04-29 19:56:59 +00003177 ctxt->input->line++;
3178 in++;
3179 }
3180 goto get_more;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003181 }
3182 if (*in == ']') {
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003183 if ((in[1] == ']') && (in[2] == '>')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003184 xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003185 ctxt->input->cur = in;
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003186 return;
3187 }
3188 in++;
3189 goto get_more;
3190 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00003191 nbchar = in - ctxt->input->cur;
Daniel Veillard80f32572001-03-07 19:45:40 +00003192 if (nbchar > 0) {
Daniel Veillard40412cd2003-09-03 13:28:32 +00003193 if ((ctxt->sax->ignorableWhitespace !=
3194 ctxt->sax->characters) &&
William M. Brack76e95df2003-10-18 16:20:14 +00003195 (IS_BLANK_CH(*ctxt->input->cur))) {
Daniel Veillarda7374592001-05-10 14:17:55 +00003196 const xmlChar *tmp = ctxt->input->cur;
3197 ctxt->input->cur = in;
Daniel Veillard40412cd2003-09-03 13:28:32 +00003198
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003199 if (areBlanks(ctxt, tmp, nbchar, 0)) {
Daniel Veillard40412cd2003-09-03 13:28:32 +00003200 ctxt->sax->ignorableWhitespace(ctxt->userData,
3201 tmp, nbchar);
3202 } else if (ctxt->sax->characters != NULL)
3203 ctxt->sax->characters(ctxt->userData,
3204 tmp, nbchar);
Daniel Veillard3ed27bd2001-06-17 17:58:17 +00003205 line = ctxt->input->line;
3206 col = ctxt->input->col;
Daniel Veillard80f32572001-03-07 19:45:40 +00003207 } else {
3208 if (ctxt->sax->characters != NULL)
3209 ctxt->sax->characters(ctxt->userData,
3210 ctxt->input->cur, nbchar);
Daniel Veillard3ed27bd2001-06-17 17:58:17 +00003211 line = ctxt->input->line;
3212 col = ctxt->input->col;
Daniel Veillard80f32572001-03-07 19:45:40 +00003213 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00003214 }
3215 ctxt->input->cur = in;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003216 if (*in == 0xD) {
3217 in++;
3218 if (*in == 0xA) {
3219 ctxt->input->cur = in;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003220 in++;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003221 ctxt->input->line++;
3222 continue; /* while */
Daniel Veillard48b2f892001-02-25 16:11:03 +00003223 }
Daniel Veillard561b7f82002-03-20 21:55:57 +00003224 in--;
3225 }
3226 if (*in == '<') {
3227 return;
3228 }
3229 if (*in == '&') {
3230 return;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003231 }
3232 SHRINK;
3233 GROW;
3234 in = ctxt->input->cur;
William M. Brackc07329e2003-09-08 01:57:30 +00003235 } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09));
Daniel Veillard48b2f892001-02-25 16:11:03 +00003236 nbchar = 0;
3237 }
Daniel Veillard50582112001-03-26 22:52:16 +00003238 ctxt->input->line = line;
3239 ctxt->input->col = col;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003240 xmlParseCharDataComplex(ctxt, cdata);
3241}
3242
Daniel Veillard01c13b52002-12-10 15:19:08 +00003243/**
3244 * xmlParseCharDataComplex:
3245 * @ctxt: an XML parser context
3246 * @cdata: int indicating whether we are within a CDATA section
3247 *
3248 * parse a CharData section.this is the fallback function
3249 * of xmlParseCharData() when the parsing requires handling
3250 * of non-ASCII characters.
3251 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00003252void
3253xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata) {
Owen Taylor3473f882001-02-23 17:55:21 +00003254 xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5];
3255 int nbchar = 0;
3256 int cur, l;
3257 int count = 0;
3258
3259 SHRINK;
3260 GROW;
3261 cur = CUR_CHAR(l);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003262 while ((cur != '<') && /* checked */
3263 (cur != '&') &&
William M. Brack871611b2003-10-18 04:53:14 +00003264 (IS_CHAR(cur))) /* test also done in xmlCurrentChar() */ {
Owen Taylor3473f882001-02-23 17:55:21 +00003265 if ((cur == ']') && (NXT(1) == ']') &&
3266 (NXT(2) == '>')) {
3267 if (cdata) break;
3268 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003269 xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003270 }
3271 }
3272 COPY_BUF(l,buf,nbchar,cur);
3273 if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) {
Daniel Veillard092643b2003-09-25 14:29:29 +00003274 buf[nbchar] = 0;
3275
Owen Taylor3473f882001-02-23 17:55:21 +00003276 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003277 * OK the segment is to be consumed as chars.
Owen Taylor3473f882001-02-23 17:55:21 +00003278 */
3279 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003280 if (areBlanks(ctxt, buf, nbchar, 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003281 if (ctxt->sax->ignorableWhitespace != NULL)
3282 ctxt->sax->ignorableWhitespace(ctxt->userData,
3283 buf, nbchar);
3284 } else {
3285 if (ctxt->sax->characters != NULL)
3286 ctxt->sax->characters(ctxt->userData, buf, nbchar);
3287 }
3288 }
3289 nbchar = 0;
3290 }
3291 count++;
3292 if (count > 50) {
3293 GROW;
3294 count = 0;
3295 }
3296 NEXTL(l);
3297 cur = CUR_CHAR(l);
3298 }
3299 if (nbchar != 0) {
Daniel Veillard092643b2003-09-25 14:29:29 +00003300 buf[nbchar] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003301 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003302 * OK the segment is to be consumed as chars.
Owen Taylor3473f882001-02-23 17:55:21 +00003303 */
3304 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003305 if (areBlanks(ctxt, buf, nbchar, 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003306 if (ctxt->sax->ignorableWhitespace != NULL)
3307 ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar);
3308 } else {
3309 if (ctxt->sax->characters != NULL)
3310 ctxt->sax->characters(ctxt->userData, buf, nbchar);
3311 }
3312 }
3313 }
3314}
3315
3316/**
3317 * xmlParseExternalID:
3318 * @ctxt: an XML parser context
3319 * @publicID: a xmlChar** receiving PubidLiteral
3320 * @strict: indicate whether we should restrict parsing to only
3321 * production [75], see NOTE below
3322 *
3323 * Parse an External ID or a Public ID
3324 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003325 * NOTE: Productions [75] and [83] interact badly since [75] can generate
Owen Taylor3473f882001-02-23 17:55:21 +00003326 * 'PUBLIC' S PubidLiteral S SystemLiteral
3327 *
3328 * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
3329 * | 'PUBLIC' S PubidLiteral S SystemLiteral
3330 *
3331 * [83] PublicID ::= 'PUBLIC' S PubidLiteral
3332 *
3333 * Returns the function returns SystemLiteral and in the second
3334 * case publicID receives PubidLiteral, is strict is off
3335 * it is possible to return NULL and have publicID set.
3336 */
3337
3338xmlChar *
3339xmlParseExternalID(xmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) {
3340 xmlChar *URI = NULL;
3341
3342 SHRINK;
Daniel Veillard146c9122001-03-22 15:22:27 +00003343
3344 *publicID = NULL;
Daniel Veillarda07050d2003-10-19 14:46:32 +00003345 if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003346 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00003347 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003348 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3349 "Space required after 'SYSTEM'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003350 }
3351 SKIP_BLANKS;
3352 URI = xmlParseSystemLiteral(ctxt);
3353 if (URI == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003354 xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003355 }
Daniel Veillarda07050d2003-10-19 14:46:32 +00003356 } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003357 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00003358 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003359 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00003360 "Space required after 'PUBLIC'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003361 }
3362 SKIP_BLANKS;
3363 *publicID = xmlParsePubidLiteral(ctxt);
3364 if (*publicID == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003365 xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003366 }
3367 if (strict) {
3368 /*
3369 * We don't handle [83] so "S SystemLiteral" is required.
3370 */
William M. Brack76e95df2003-10-18 16:20:14 +00003371 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003372 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00003373 "Space required after the Public Identifier\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003374 }
3375 } else {
3376 /*
3377 * We handle [83] so we return immediately, if
3378 * "S SystemLiteral" is not detected. From a purely parsing
3379 * point of view that's a nice mess.
3380 */
3381 const xmlChar *ptr;
3382 GROW;
3383
3384 ptr = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00003385 if (!IS_BLANK_CH(*ptr)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003386
William M. Brack76e95df2003-10-18 16:20:14 +00003387 while (IS_BLANK_CH(*ptr)) ptr++; /* TODO: dangerous, fix ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003388 if ((*ptr != '\'') && (*ptr != '"')) return(NULL);
3389 }
3390 SKIP_BLANKS;
3391 URI = xmlParseSystemLiteral(ctxt);
3392 if (URI == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003393 xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003394 }
3395 }
3396 return(URI);
3397}
3398
3399/**
3400 * xmlParseComment:
3401 * @ctxt: an XML parser context
3402 *
3403 * Skip an XML (SGML) comment <!-- .... -->
3404 * The spec says that "For compatibility, the string "--" (double-hyphen)
3405 * must not occur within comments. "
3406 *
3407 * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
3408 */
3409void
3410xmlParseComment(xmlParserCtxtPtr ctxt) {
3411 xmlChar *buf = NULL;
3412 int len;
3413 int size = XML_PARSER_BUFFER_SIZE;
3414 int q, ql;
3415 int r, rl;
3416 int cur, l;
3417 xmlParserInputState state;
3418 xmlParserInputPtr input = ctxt->input;
3419 int count = 0;
3420
3421 /*
3422 * Check that there is a comment right here.
3423 */
3424 if ((RAW != '<') || (NXT(1) != '!') ||
3425 (NXT(2) != '-') || (NXT(3) != '-')) return;
3426
3427 state = ctxt->instate;
3428 ctxt->instate = XML_PARSER_COMMENT;
3429 SHRINK;
3430 SKIP(4);
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003431 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003432 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003433 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003434 ctxt->instate = state;
3435 return;
3436 }
3437 q = CUR_CHAR(ql);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003438 if (q == 0)
3439 goto not_terminated;
Owen Taylor3473f882001-02-23 17:55:21 +00003440 NEXTL(ql);
3441 r = CUR_CHAR(rl);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003442 if (r == 0)
3443 goto not_terminated;
Owen Taylor3473f882001-02-23 17:55:21 +00003444 NEXTL(rl);
3445 cur = CUR_CHAR(l);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003446 if (cur == 0)
3447 goto not_terminated;
Owen Taylor3473f882001-02-23 17:55:21 +00003448 len = 0;
William M. Brack871611b2003-10-18 04:53:14 +00003449 while (IS_CHAR(cur) && /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003450 ((cur != '>') ||
3451 (r != '-') || (q != '-'))) {
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003452 if ((r == '-') && (q == '-')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003453 xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003454 }
3455 if (len + 5 >= size) {
3456 size *= 2;
3457 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3458 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003459 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003460 ctxt->instate = state;
3461 return;
3462 }
3463 }
3464 COPY_BUF(ql,buf,len,q);
3465 q = r;
3466 ql = rl;
3467 r = cur;
3468 rl = l;
3469
3470 count++;
3471 if (count > 50) {
3472 GROW;
3473 count = 0;
3474 }
3475 NEXTL(l);
3476 cur = CUR_CHAR(l);
3477 if (cur == 0) {
3478 SHRINK;
3479 GROW;
3480 cur = CUR_CHAR(l);
3481 }
3482 }
3483 buf[len] = 0;
William M. Brack871611b2003-10-18 04:53:14 +00003484 if (!IS_CHAR(cur)) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00003485 xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
Owen Taylor3473f882001-02-23 17:55:21 +00003486 "Comment not terminated \n<!--%.50s\n", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003487 xmlFree(buf);
3488 } else {
3489 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003490 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
3491 "Comment doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003492 }
3493 NEXT;
3494 if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
3495 (!ctxt->disableSAX))
3496 ctxt->sax->comment(ctxt->userData, buf);
3497 xmlFree(buf);
3498 }
3499 ctxt->instate = state;
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003500 return;
3501not_terminated:
3502 xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
3503 "Comment not terminated\n", NULL);
3504 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003505}
3506
3507/**
3508 * xmlParsePITarget:
3509 * @ctxt: an XML parser context
3510 *
3511 * parse the name of a PI
3512 *
3513 * [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
3514 *
3515 * Returns the PITarget name or NULL
3516 */
3517
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003518const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00003519xmlParsePITarget(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003520 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003521
3522 name = xmlParseName(ctxt);
3523 if ((name != NULL) &&
3524 ((name[0] == 'x') || (name[0] == 'X')) &&
3525 ((name[1] == 'm') || (name[1] == 'M')) &&
3526 ((name[2] == 'l') || (name[2] == 'L'))) {
3527 int i;
3528 if ((name[0] == 'x') && (name[1] == 'm') &&
3529 (name[2] == 'l') && (name[3] == 0)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003530 xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
Owen Taylor3473f882001-02-23 17:55:21 +00003531 "XML declaration allowed only at the start of the document\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003532 return(name);
3533 } else if (name[3] == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003534 xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003535 return(name);
3536 }
3537 for (i = 0;;i++) {
3538 if (xmlW3CPIs[i] == NULL) break;
3539 if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i]))
3540 return(name);
3541 }
Daniel Veillard24eb9782003-10-04 21:08:09 +00003542 xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
3543 "xmlParsePITarget: invalid name prefix 'xml'\n",
3544 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003545 }
3546 return(name);
3547}
3548
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003549#ifdef LIBXML_CATALOG_ENABLED
3550/**
3551 * xmlParseCatalogPI:
3552 * @ctxt: an XML parser context
3553 * @catalog: the PI value string
3554 *
3555 * parse an XML Catalog Processing Instruction.
3556 *
3557 * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?>
3558 *
3559 * Occurs only if allowed by the user and if happening in the Misc
3560 * part of the document before any doctype informations
3561 * This will add the given catalog to the parsing context in order
3562 * to be used if there is a resolution need further down in the document
3563 */
3564
3565static void
3566xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) {
3567 xmlChar *URL = NULL;
3568 const xmlChar *tmp, *base;
3569 xmlChar marker;
3570
3571 tmp = catalog;
William M. Brack76e95df2003-10-18 16:20:14 +00003572 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003573 if (xmlStrncmp(tmp, BAD_CAST"catalog", 7))
3574 goto error;
3575 tmp += 7;
William M. Brack76e95df2003-10-18 16:20:14 +00003576 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003577 if (*tmp != '=') {
3578 return;
3579 }
3580 tmp++;
William M. Brack76e95df2003-10-18 16:20:14 +00003581 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003582 marker = *tmp;
3583 if ((marker != '\'') && (marker != '"'))
3584 goto error;
3585 tmp++;
3586 base = tmp;
3587 while ((*tmp != 0) && (*tmp != marker)) tmp++;
3588 if (*tmp == 0)
3589 goto error;
3590 URL = xmlStrndup(base, tmp - base);
3591 tmp++;
William M. Brack76e95df2003-10-18 16:20:14 +00003592 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003593 if (*tmp != 0)
3594 goto error;
3595
3596 if (URL != NULL) {
3597 ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
3598 xmlFree(URL);
3599 }
3600 return;
3601
3602error:
Daniel Veillard24eb9782003-10-04 21:08:09 +00003603 xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI,
3604 "Catalog PI syntax error: %s\n",
3605 catalog, NULL);
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003606 if (URL != NULL)
3607 xmlFree(URL);
3608}
3609#endif
3610
Owen Taylor3473f882001-02-23 17:55:21 +00003611/**
3612 * xmlParsePI:
3613 * @ctxt: an XML parser context
3614 *
3615 * parse an XML Processing Instruction.
3616 *
3617 * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
3618 *
3619 * The processing is transfered to SAX once parsed.
3620 */
3621
3622void
3623xmlParsePI(xmlParserCtxtPtr ctxt) {
3624 xmlChar *buf = NULL;
3625 int len = 0;
3626 int size = XML_PARSER_BUFFER_SIZE;
3627 int cur, l;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003628 const xmlChar *target;
Owen Taylor3473f882001-02-23 17:55:21 +00003629 xmlParserInputState state;
3630 int count = 0;
3631
3632 if ((RAW == '<') && (NXT(1) == '?')) {
3633 xmlParserInputPtr input = ctxt->input;
3634 state = ctxt->instate;
3635 ctxt->instate = XML_PARSER_PI;
3636 /*
3637 * this is a Processing Instruction.
3638 */
3639 SKIP(2);
3640 SHRINK;
3641
3642 /*
3643 * Parse the target name and check for special support like
3644 * namespace.
3645 */
3646 target = xmlParsePITarget(ctxt);
3647 if (target != NULL) {
3648 if ((RAW == '?') && (NXT(1) == '>')) {
3649 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003650 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
3651 "PI declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003652 }
3653 SKIP(2);
3654
3655 /*
3656 * SAX: PI detected.
3657 */
3658 if ((ctxt->sax) && (!ctxt->disableSAX) &&
3659 (ctxt->sax->processingInstruction != NULL))
3660 ctxt->sax->processingInstruction(ctxt->userData,
3661 target, NULL);
3662 ctxt->instate = state;
Owen Taylor3473f882001-02-23 17:55:21 +00003663 return;
3664 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003665 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003666 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003667 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003668 ctxt->instate = state;
3669 return;
3670 }
3671 cur = CUR;
3672 if (!IS_BLANK(cur)) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00003673 xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED,
3674 "ParsePI: PI %s space expected\n", target);
Owen Taylor3473f882001-02-23 17:55:21 +00003675 }
3676 SKIP_BLANKS;
3677 cur = CUR_CHAR(l);
William M. Brack871611b2003-10-18 04:53:14 +00003678 while (IS_CHAR(cur) && /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003679 ((cur != '?') || (NXT(1) != '>'))) {
3680 if (len + 5 >= size) {
3681 size *= 2;
3682 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3683 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003684 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003685 ctxt->instate = state;
3686 return;
3687 }
3688 }
3689 count++;
3690 if (count > 50) {
3691 GROW;
3692 count = 0;
3693 }
3694 COPY_BUF(l,buf,len,cur);
3695 NEXTL(l);
3696 cur = CUR_CHAR(l);
3697 if (cur == 0) {
3698 SHRINK;
3699 GROW;
3700 cur = CUR_CHAR(l);
3701 }
3702 }
3703 buf[len] = 0;
3704 if (cur != '?') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00003705 xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED,
3706 "ParsePI: PI %s never end ...\n", target);
Owen Taylor3473f882001-02-23 17:55:21 +00003707 } else {
3708 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003709 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3710 "PI declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003711 }
3712 SKIP(2);
3713
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003714#ifdef LIBXML_CATALOG_ENABLED
3715 if (((state == XML_PARSER_MISC) ||
3716 (state == XML_PARSER_START)) &&
3717 (xmlStrEqual(target, XML_CATALOG_PI))) {
3718 xmlCatalogAllow allow = xmlCatalogGetDefaults();
3719 if ((allow == XML_CATA_ALLOW_DOCUMENT) ||
3720 (allow == XML_CATA_ALLOW_ALL))
3721 xmlParseCatalogPI(ctxt, buf);
3722 }
3723#endif
3724
3725
Owen Taylor3473f882001-02-23 17:55:21 +00003726 /*
3727 * SAX: PI detected.
3728 */
3729 if ((ctxt->sax) && (!ctxt->disableSAX) &&
3730 (ctxt->sax->processingInstruction != NULL))
3731 ctxt->sax->processingInstruction(ctxt->userData,
3732 target, buf);
3733 }
3734 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003735 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003736 xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003737 }
3738 ctxt->instate = state;
3739 }
3740}
3741
3742/**
3743 * xmlParseNotationDecl:
3744 * @ctxt: an XML parser context
3745 *
3746 * parse a notation declaration
3747 *
3748 * [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>'
3749 *
3750 * Hence there is actually 3 choices:
3751 * 'PUBLIC' S PubidLiteral
3752 * 'PUBLIC' S PubidLiteral S SystemLiteral
3753 * and 'SYSTEM' S SystemLiteral
3754 *
3755 * See the NOTE on xmlParseExternalID().
3756 */
3757
3758void
3759xmlParseNotationDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003760 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003761 xmlChar *Pubid;
3762 xmlChar *Systemid;
3763
Daniel Veillarda07050d2003-10-19 14:46:32 +00003764 if (CMP10(CUR_PTR, '<', '!', 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003765 xmlParserInputPtr input = ctxt->input;
3766 SHRINK;
3767 SKIP(10);
William M. Brack76e95df2003-10-18 16:20:14 +00003768 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003769 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3770 "Space required after '<!NOTATION'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003771 return;
3772 }
3773 SKIP_BLANKS;
3774
Daniel Veillard76d66f42001-05-16 21:05:17 +00003775 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003776 if (name == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003777 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003778 return;
3779 }
William M. Brack76e95df2003-10-18 16:20:14 +00003780 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003781 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00003782 "Space required after the NOTATION name'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003783 return;
3784 }
3785 SKIP_BLANKS;
3786
3787 /*
3788 * Parse the IDs.
3789 */
3790 Systemid = xmlParseExternalID(ctxt, &Pubid, 0);
3791 SKIP_BLANKS;
3792
3793 if (RAW == '>') {
3794 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003795 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3796 "Notation declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003797 }
3798 NEXT;
3799 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
3800 (ctxt->sax->notationDecl != NULL))
3801 ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid);
3802 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003803 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003804 }
Owen Taylor3473f882001-02-23 17:55:21 +00003805 if (Systemid != NULL) xmlFree(Systemid);
3806 if (Pubid != NULL) xmlFree(Pubid);
3807 }
3808}
3809
3810/**
3811 * xmlParseEntityDecl:
3812 * @ctxt: an XML parser context
3813 *
3814 * parse <!ENTITY declarations
3815 *
3816 * [70] EntityDecl ::= GEDecl | PEDecl
3817 *
3818 * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>'
3819 *
3820 * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>'
3821 *
3822 * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?)
3823 *
3824 * [74] PEDef ::= EntityValue | ExternalID
3825 *
3826 * [76] NDataDecl ::= S 'NDATA' S Name
3827 *
3828 * [ VC: Notation Declared ]
3829 * The Name must match the declared name of a notation.
3830 */
3831
3832void
3833xmlParseEntityDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003834 const xmlChar *name = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003835 xmlChar *value = NULL;
3836 xmlChar *URI = NULL, *literal = NULL;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003837 const xmlChar *ndata = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003838 int isParameter = 0;
3839 xmlChar *orig = NULL;
Daniel Veillardf5582f12002-06-11 10:08:16 +00003840 int skipped;
Owen Taylor3473f882001-02-23 17:55:21 +00003841
3842 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00003843 if (CMP8(CUR_PTR, '<', '!', 'E', 'N', 'T', 'I', 'T', 'Y')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003844 xmlParserInputPtr input = ctxt->input;
Owen Taylor3473f882001-02-23 17:55:21 +00003845 SHRINK;
3846 SKIP(8);
Daniel Veillardf5582f12002-06-11 10:08:16 +00003847 skipped = SKIP_BLANKS;
3848 if (skipped == 0) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003849 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3850 "Space required after '<!ENTITY'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003851 }
Owen Taylor3473f882001-02-23 17:55:21 +00003852
3853 if (RAW == '%') {
3854 NEXT;
Daniel Veillardf5582f12002-06-11 10:08:16 +00003855 skipped = SKIP_BLANKS;
3856 if (skipped == 0) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003857 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3858 "Space required after '%'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003859 }
Owen Taylor3473f882001-02-23 17:55:21 +00003860 isParameter = 1;
3861 }
3862
Daniel Veillard76d66f42001-05-16 21:05:17 +00003863 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003864 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003865 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
3866 "xmlParseEntityDecl: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003867 return;
3868 }
Daniel Veillardf5582f12002-06-11 10:08:16 +00003869 skipped = SKIP_BLANKS;
3870 if (skipped == 0) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003871 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3872 "Space required after the entity name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003873 }
Owen Taylor3473f882001-02-23 17:55:21 +00003874
Daniel Veillardf5582f12002-06-11 10:08:16 +00003875 ctxt->instate = XML_PARSER_ENTITY_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00003876 /*
3877 * handle the various case of definitions...
3878 */
3879 if (isParameter) {
3880 if ((RAW == '"') || (RAW == '\'')) {
3881 value = xmlParseEntityValue(ctxt, &orig);
3882 if (value) {
3883 if ((ctxt->sax != NULL) &&
3884 (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
3885 ctxt->sax->entityDecl(ctxt->userData, name,
3886 XML_INTERNAL_PARAMETER_ENTITY,
3887 NULL, NULL, value);
3888 }
3889 } else {
3890 URI = xmlParseExternalID(ctxt, &literal, 1);
3891 if ((URI == NULL) && (literal == NULL)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003892 xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003893 }
3894 if (URI) {
3895 xmlURIPtr uri;
3896
3897 uri = xmlParseURI((const char *) URI);
3898 if (uri == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003899 xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI,
3900 "Invalid URI: %s\n", URI);
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003901 /*
3902 * This really ought to be a well formedness error
3903 * but the XML Core WG decided otherwise c.f. issue
3904 * E26 of the XML erratas.
3905 */
Owen Taylor3473f882001-02-23 17:55:21 +00003906 } else {
3907 if (uri->fragment != NULL) {
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003908 /*
3909 * Okay this is foolish to block those but not
3910 * invalid URIs.
3911 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003912 xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003913 } else {
3914 if ((ctxt->sax != NULL) &&
3915 (!ctxt->disableSAX) &&
3916 (ctxt->sax->entityDecl != NULL))
3917 ctxt->sax->entityDecl(ctxt->userData, name,
3918 XML_EXTERNAL_PARAMETER_ENTITY,
3919 literal, URI, NULL);
3920 }
3921 xmlFreeURI(uri);
3922 }
3923 }
3924 }
3925 } else {
3926 if ((RAW == '"') || (RAW == '\'')) {
3927 value = xmlParseEntityValue(ctxt, &orig);
3928 if ((ctxt->sax != NULL) &&
3929 (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
3930 ctxt->sax->entityDecl(ctxt->userData, name,
3931 XML_INTERNAL_GENERAL_ENTITY,
3932 NULL, NULL, value);
Daniel Veillard5997aca2002-03-18 18:36:20 +00003933 /*
3934 * For expat compatibility in SAX mode.
3935 */
3936 if ((ctxt->myDoc == NULL) ||
3937 (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) {
3938 if (ctxt->myDoc == NULL) {
3939 ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
3940 }
3941 if (ctxt->myDoc->intSubset == NULL)
3942 ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
3943 BAD_CAST "fake", NULL, NULL);
3944
Daniel Veillard1af9a412003-08-20 22:54:39 +00003945 xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY,
3946 NULL, NULL, value);
Daniel Veillard5997aca2002-03-18 18:36:20 +00003947 }
Owen Taylor3473f882001-02-23 17:55:21 +00003948 } else {
3949 URI = xmlParseExternalID(ctxt, &literal, 1);
3950 if ((URI == NULL) && (literal == NULL)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003951 xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003952 }
3953 if (URI) {
3954 xmlURIPtr uri;
3955
3956 uri = xmlParseURI((const char *)URI);
3957 if (uri == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003958 xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI,
3959 "Invalid URI: %s\n", URI);
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003960 /*
3961 * This really ought to be a well formedness error
3962 * but the XML Core WG decided otherwise c.f. issue
3963 * E26 of the XML erratas.
3964 */
Owen Taylor3473f882001-02-23 17:55:21 +00003965 } else {
3966 if (uri->fragment != NULL) {
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003967 /*
3968 * Okay this is foolish to block those but not
3969 * invalid URIs.
3970 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003971 xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003972 }
3973 xmlFreeURI(uri);
3974 }
3975 }
William M. Brack76e95df2003-10-18 16:20:14 +00003976 if ((RAW != '>') && (!IS_BLANK_CH(CUR))) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003977 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3978 "Space required before 'NDATA'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003979 }
3980 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00003981 if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003982 SKIP(5);
William M. Brack76e95df2003-10-18 16:20:14 +00003983 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003984 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3985 "Space required after 'NDATA'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003986 }
3987 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00003988 ndata = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003989 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
3990 (ctxt->sax->unparsedEntityDecl != NULL))
3991 ctxt->sax->unparsedEntityDecl(ctxt->userData, name,
3992 literal, URI, ndata);
3993 } else {
3994 if ((ctxt->sax != NULL) &&
3995 (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
3996 ctxt->sax->entityDecl(ctxt->userData, name,
3997 XML_EXTERNAL_GENERAL_PARSED_ENTITY,
3998 literal, URI, NULL);
Daniel Veillard5997aca2002-03-18 18:36:20 +00003999 /*
4000 * For expat compatibility in SAX mode.
4001 * assuming the entity repalcement was asked for
4002 */
4003 if ((ctxt->replaceEntities != 0) &&
4004 ((ctxt->myDoc == NULL) ||
4005 (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) {
4006 if (ctxt->myDoc == NULL) {
4007 ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
4008 }
4009
4010 if (ctxt->myDoc->intSubset == NULL)
4011 ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
4012 BAD_CAST "fake", NULL, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00004013 xmlSAX2EntityDecl(ctxt, name,
4014 XML_EXTERNAL_GENERAL_PARSED_ENTITY,
4015 literal, URI, NULL);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004016 }
Owen Taylor3473f882001-02-23 17:55:21 +00004017 }
4018 }
4019 }
4020 SKIP_BLANKS;
4021 if (RAW != '>') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00004022 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED,
Owen Taylor3473f882001-02-23 17:55:21 +00004023 "xmlParseEntityDecl: entity %s not terminated\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00004024 } else {
4025 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004026 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
4027 "Entity declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004028 }
4029 NEXT;
4030 }
4031 if (orig != NULL) {
4032 /*
4033 * Ugly mechanism to save the raw entity value.
4034 */
4035 xmlEntityPtr cur = NULL;
4036
4037 if (isParameter) {
4038 if ((ctxt->sax != NULL) &&
4039 (ctxt->sax->getParameterEntity != NULL))
4040 cur = ctxt->sax->getParameterEntity(ctxt->userData, name);
4041 } else {
4042 if ((ctxt->sax != NULL) &&
4043 (ctxt->sax->getEntity != NULL))
4044 cur = ctxt->sax->getEntity(ctxt->userData, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004045 if ((cur == NULL) && (ctxt->userData==ctxt)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00004046 cur = xmlSAX2GetEntity(ctxt, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004047 }
Owen Taylor3473f882001-02-23 17:55:21 +00004048 }
4049 if (cur != NULL) {
4050 if (cur->orig != NULL)
4051 xmlFree(orig);
4052 else
4053 cur->orig = orig;
4054 } else
4055 xmlFree(orig);
4056 }
Owen Taylor3473f882001-02-23 17:55:21 +00004057 if (value != NULL) xmlFree(value);
4058 if (URI != NULL) xmlFree(URI);
4059 if (literal != NULL) xmlFree(literal);
Owen Taylor3473f882001-02-23 17:55:21 +00004060 }
4061}
4062
4063/**
4064 * xmlParseDefaultDecl:
4065 * @ctxt: an XML parser context
4066 * @value: Receive a possible fixed default value for the attribute
4067 *
4068 * Parse an attribute default declaration
4069 *
4070 * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue)
4071 *
4072 * [ VC: Required Attribute ]
4073 * if the default declaration is the keyword #REQUIRED, then the
4074 * attribute must be specified for all elements of the type in the
4075 * attribute-list declaration.
4076 *
4077 * [ VC: Attribute Default Legal ]
4078 * The declared default value must meet the lexical constraints of
4079 * the declared attribute type c.f. xmlValidateAttributeDecl()
4080 *
4081 * [ VC: Fixed Attribute Default ]
4082 * if an attribute has a default value declared with the #FIXED
4083 * keyword, instances of that attribute must match the default value.
4084 *
4085 * [ WFC: No < in Attribute Values ]
4086 * handled in xmlParseAttValue()
4087 *
4088 * returns: XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED
4089 * or XML_ATTRIBUTE_FIXED.
4090 */
4091
4092int
4093xmlParseDefaultDecl(xmlParserCtxtPtr ctxt, xmlChar **value) {
4094 int val;
4095 xmlChar *ret;
4096
4097 *value = NULL;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004098 if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004099 SKIP(9);
4100 return(XML_ATTRIBUTE_REQUIRED);
4101 }
Daniel Veillarda07050d2003-10-19 14:46:32 +00004102 if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004103 SKIP(8);
4104 return(XML_ATTRIBUTE_IMPLIED);
4105 }
4106 val = XML_ATTRIBUTE_NONE;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004107 if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004108 SKIP(6);
4109 val = XML_ATTRIBUTE_FIXED;
William M. Brack76e95df2003-10-18 16:20:14 +00004110 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004111 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4112 "Space required after '#FIXED'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004113 }
4114 SKIP_BLANKS;
4115 }
4116 ret = xmlParseAttValue(ctxt);
4117 ctxt->instate = XML_PARSER_DTD;
4118 if (ret == NULL) {
William M. Brack7b9154b2003-09-27 19:23:50 +00004119 xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004120 "Attribute default value declaration error\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004121 } else
4122 *value = ret;
4123 return(val);
4124}
4125
4126/**
4127 * xmlParseNotationType:
4128 * @ctxt: an XML parser context
4129 *
4130 * parse an Notation attribute type.
4131 *
4132 * Note: the leading 'NOTATION' S part has already being parsed...
4133 *
4134 * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
4135 *
4136 * [ VC: Notation Attributes ]
4137 * Values of this type must match one of the notation names included
4138 * in the declaration; all notation names in the declaration must be declared.
4139 *
4140 * Returns: the notation attribute tree built while parsing
4141 */
4142
4143xmlEnumerationPtr
4144xmlParseNotationType(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004145 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00004146 xmlEnumerationPtr ret = NULL, last = NULL, cur;
4147
4148 if (RAW != '(') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004149 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004150 return(NULL);
4151 }
4152 SHRINK;
4153 do {
4154 NEXT;
4155 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004156 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004157 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004158 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4159 "Name expected in NOTATION declaration\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004160 return(ret);
4161 }
4162 cur = xmlCreateEnumeration(name);
Owen Taylor3473f882001-02-23 17:55:21 +00004163 if (cur == NULL) return(ret);
4164 if (last == NULL) ret = last = cur;
4165 else {
4166 last->next = cur;
4167 last = cur;
4168 }
4169 SKIP_BLANKS;
4170 } while (RAW == '|');
4171 if (RAW != ')') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004172 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004173 if ((last != NULL) && (last != ret))
4174 xmlFreeEnumeration(last);
4175 return(ret);
4176 }
4177 NEXT;
4178 return(ret);
4179}
4180
4181/**
4182 * xmlParseEnumerationType:
4183 * @ctxt: an XML parser context
4184 *
4185 * parse an Enumeration attribute type.
4186 *
4187 * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')'
4188 *
4189 * [ VC: Enumeration ]
4190 * Values of this type must match one of the Nmtoken tokens in
4191 * the declaration
4192 *
4193 * Returns: the enumeration attribute tree built while parsing
4194 */
4195
4196xmlEnumerationPtr
4197xmlParseEnumerationType(xmlParserCtxtPtr ctxt) {
4198 xmlChar *name;
4199 xmlEnumerationPtr ret = NULL, last = NULL, cur;
4200
4201 if (RAW != '(') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004202 xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004203 return(NULL);
4204 }
4205 SHRINK;
4206 do {
4207 NEXT;
4208 SKIP_BLANKS;
4209 name = xmlParseNmtoken(ctxt);
4210 if (name == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004211 xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004212 return(ret);
4213 }
4214 cur = xmlCreateEnumeration(name);
4215 xmlFree(name);
4216 if (cur == NULL) return(ret);
4217 if (last == NULL) ret = last = cur;
4218 else {
4219 last->next = cur;
4220 last = cur;
4221 }
4222 SKIP_BLANKS;
4223 } while (RAW == '|');
4224 if (RAW != ')') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004225 xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004226 return(ret);
4227 }
4228 NEXT;
4229 return(ret);
4230}
4231
4232/**
4233 * xmlParseEnumeratedType:
4234 * @ctxt: an XML parser context
4235 * @tree: the enumeration tree built while parsing
4236 *
4237 * parse an Enumerated attribute type.
4238 *
4239 * [57] EnumeratedType ::= NotationType | Enumeration
4240 *
4241 * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
4242 *
4243 *
4244 * Returns: XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION
4245 */
4246
4247int
4248xmlParseEnumeratedType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) {
Daniel Veillarda07050d2003-10-19 14:46:32 +00004249 if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004250 SKIP(8);
William M. Brack76e95df2003-10-18 16:20:14 +00004251 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004252 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4253 "Space required after 'NOTATION'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004254 return(0);
4255 }
4256 SKIP_BLANKS;
4257 *tree = xmlParseNotationType(ctxt);
4258 if (*tree == NULL) return(0);
4259 return(XML_ATTRIBUTE_NOTATION);
4260 }
4261 *tree = xmlParseEnumerationType(ctxt);
4262 if (*tree == NULL) return(0);
4263 return(XML_ATTRIBUTE_ENUMERATION);
4264}
4265
4266/**
4267 * xmlParseAttributeType:
4268 * @ctxt: an XML parser context
4269 * @tree: the enumeration tree built while parsing
4270 *
4271 * parse the Attribute list def for an element
4272 *
4273 * [54] AttType ::= StringType | TokenizedType | EnumeratedType
4274 *
4275 * [55] StringType ::= 'CDATA'
4276 *
4277 * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' |
4278 * 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS'
4279 *
4280 * Validity constraints for attribute values syntax are checked in
4281 * xmlValidateAttributeValue()
4282 *
4283 * [ VC: ID ]
4284 * Values of type ID must match the Name production. A name must not
4285 * appear more than once in an XML document as a value of this type;
4286 * i.e., ID values must uniquely identify the elements which bear them.
4287 *
4288 * [ VC: One ID per Element Type ]
4289 * No element type may have more than one ID attribute specified.
4290 *
4291 * [ VC: ID Attribute Default ]
4292 * An ID attribute must have a declared default of #IMPLIED or #REQUIRED.
4293 *
4294 * [ VC: IDREF ]
4295 * Values of type IDREF must match the Name production, and values
4296 * of type IDREFS must match Names; each IDREF Name must match the value
4297 * of an ID attribute on some element in the XML document; i.e. IDREF
4298 * values must match the value of some ID attribute.
4299 *
4300 * [ VC: Entity Name ]
4301 * Values of type ENTITY must match the Name production, values
4302 * of type ENTITIES must match Names; each Entity Name must match the
4303 * name of an unparsed entity declared in the DTD.
4304 *
4305 * [ VC: Name Token ]
4306 * Values of type NMTOKEN must match the Nmtoken production; values
4307 * of type NMTOKENS must match Nmtokens.
4308 *
4309 * Returns the attribute type
4310 */
4311int
4312xmlParseAttributeType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) {
4313 SHRINK;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004314 if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004315 SKIP(5);
4316 return(XML_ATTRIBUTE_CDATA);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004317 } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004318 SKIP(6);
4319 return(XML_ATTRIBUTE_IDREFS);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004320 } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004321 SKIP(5);
4322 return(XML_ATTRIBUTE_IDREF);
4323 } else if ((RAW == 'I') && (NXT(1) == 'D')) {
4324 SKIP(2);
4325 return(XML_ATTRIBUTE_ID);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004326 } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004327 SKIP(6);
4328 return(XML_ATTRIBUTE_ENTITY);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004329 } else if (CMP8(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'I', 'E', 'S')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004330 SKIP(8);
4331 return(XML_ATTRIBUTE_ENTITIES);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004332 } else if (CMP8(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N', 'S')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004333 SKIP(8);
4334 return(XML_ATTRIBUTE_NMTOKENS);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004335 } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004336 SKIP(7);
4337 return(XML_ATTRIBUTE_NMTOKEN);
4338 }
4339 return(xmlParseEnumeratedType(ctxt, tree));
4340}
4341
4342/**
4343 * xmlParseAttributeListDecl:
4344 * @ctxt: an XML parser context
4345 *
4346 * : parse the Attribute list def for an element
4347 *
4348 * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>'
4349 *
4350 * [53] AttDef ::= S Name S AttType S DefaultDecl
4351 *
4352 */
4353void
4354xmlParseAttributeListDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004355 const xmlChar *elemName;
4356 const xmlChar *attrName;
Owen Taylor3473f882001-02-23 17:55:21 +00004357 xmlEnumerationPtr tree;
4358
Daniel Veillarda07050d2003-10-19 14:46:32 +00004359 if (CMP9(CUR_PTR, '<', '!', 'A', 'T', 'T', 'L', 'I', 'S', 'T')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004360 xmlParserInputPtr input = ctxt->input;
4361
4362 SKIP(9);
William M. Brack76e95df2003-10-18 16:20:14 +00004363 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004364 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004365 "Space required after '<!ATTLIST'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004366 }
4367 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004368 elemName = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004369 if (elemName == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004370 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4371 "ATTLIST: no name for Element\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004372 return;
4373 }
4374 SKIP_BLANKS;
4375 GROW;
4376 while (RAW != '>') {
4377 const xmlChar *check = CUR_PTR;
4378 int type;
4379 int def;
4380 xmlChar *defaultValue = NULL;
4381
4382 GROW;
4383 tree = NULL;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004384 attrName = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004385 if (attrName == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004386 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4387 "ATTLIST: no name for Attribute\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004388 break;
4389 }
4390 GROW;
William M. Brack76e95df2003-10-18 16:20:14 +00004391 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004392 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004393 "Space required after the attribute name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004394 if (defaultValue != NULL)
4395 xmlFree(defaultValue);
4396 break;
4397 }
4398 SKIP_BLANKS;
4399
4400 type = xmlParseAttributeType(ctxt, &tree);
4401 if (type <= 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00004402 if (defaultValue != NULL)
4403 xmlFree(defaultValue);
4404 break;
4405 }
4406
4407 GROW;
William M. Brack76e95df2003-10-18 16:20:14 +00004408 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004409 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4410 "Space required after the attribute type\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004411 if (defaultValue != NULL)
4412 xmlFree(defaultValue);
4413 if (tree != NULL)
4414 xmlFreeEnumeration(tree);
4415 break;
4416 }
4417 SKIP_BLANKS;
4418
4419 def = xmlParseDefaultDecl(ctxt, &defaultValue);
4420 if (def <= 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00004421 if (defaultValue != NULL)
4422 xmlFree(defaultValue);
4423 if (tree != NULL)
4424 xmlFreeEnumeration(tree);
4425 break;
4426 }
4427
4428 GROW;
4429 if (RAW != '>') {
William M. Brack76e95df2003-10-18 16:20:14 +00004430 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004431 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004432 "Space required after the attribute default value\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004433 if (defaultValue != NULL)
4434 xmlFree(defaultValue);
4435 if (tree != NULL)
4436 xmlFreeEnumeration(tree);
4437 break;
4438 }
4439 SKIP_BLANKS;
4440 }
4441 if (check == CUR_PTR) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004442 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
4443 "in xmlParseAttributeListDecl\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004444 if (defaultValue != NULL)
4445 xmlFree(defaultValue);
4446 if (tree != NULL)
4447 xmlFreeEnumeration(tree);
4448 break;
4449 }
4450 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4451 (ctxt->sax->attributeDecl != NULL))
4452 ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName,
4453 type, def, defaultValue, tree);
Daniel Veillarde57ec792003-09-10 10:50:59 +00004454 else if (tree != NULL)
4455 xmlFreeEnumeration(tree);
4456
4457 if ((ctxt->sax2) && (defaultValue != NULL) &&
4458 (def != XML_ATTRIBUTE_IMPLIED) &&
4459 (def != XML_ATTRIBUTE_REQUIRED)) {
4460 xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue);
4461 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00004462 if ((ctxt->sax2) && (type != XML_ATTRIBUTE_CDATA)) {
4463 xmlAddSpecialAttr(ctxt, elemName, attrName, type);
4464 }
Owen Taylor3473f882001-02-23 17:55:21 +00004465 if (defaultValue != NULL)
4466 xmlFree(defaultValue);
4467 GROW;
4468 }
4469 if (RAW == '>') {
4470 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004471 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
4472 "Attribute list declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004473 }
4474 NEXT;
4475 }
Owen Taylor3473f882001-02-23 17:55:21 +00004476 }
4477}
4478
4479/**
4480 * xmlParseElementMixedContentDecl:
4481 * @ctxt: an XML parser context
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004482 * @inputchk: the input used for the current entity, needed for boundary checks
Owen Taylor3473f882001-02-23 17:55:21 +00004483 *
4484 * parse the declaration for a Mixed Element content
4485 * The leading '(' and spaces have been skipped in xmlParseElementContentDecl
4486 *
4487 * [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' |
4488 * '(' S? '#PCDATA' S? ')'
4489 *
4490 * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49])
4491 *
4492 * [ VC: No Duplicate Types ]
4493 * The same name must not appear more than once in a single
4494 * mixed-content declaration.
4495 *
4496 * returns: the list of the xmlElementContentPtr describing the element choices
4497 */
4498xmlElementContentPtr
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004499xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) {
Owen Taylor3473f882001-02-23 17:55:21 +00004500 xmlElementContentPtr ret = NULL, cur = NULL, n;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004501 const xmlChar *elem = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00004502
4503 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004504 if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004505 SKIP(7);
4506 SKIP_BLANKS;
4507 SHRINK;
4508 if (RAW == ')') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004509 if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00004510 xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
4511"Element content declaration doesn't start and stop in the same entity\n",
4512 NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004513 }
Owen Taylor3473f882001-02-23 17:55:21 +00004514 NEXT;
4515 ret = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_PCDATA);
4516 if (RAW == '*') {
4517 ret->ocur = XML_ELEMENT_CONTENT_MULT;
4518 NEXT;
4519 }
4520 return(ret);
4521 }
4522 if ((RAW == '(') || (RAW == '|')) {
4523 ret = cur = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_PCDATA);
4524 if (ret == NULL) return(NULL);
4525 }
4526 while (RAW == '|') {
4527 NEXT;
4528 if (elem == NULL) {
4529 ret = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR);
4530 if (ret == NULL) return(NULL);
4531 ret->c1 = cur;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004532 if (cur != NULL)
4533 cur->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +00004534 cur = ret;
4535 } else {
4536 n = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR);
4537 if (n == NULL) return(NULL);
4538 n->c1 = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004539 if (n->c1 != NULL)
4540 n->c1->parent = n;
Owen Taylor3473f882001-02-23 17:55:21 +00004541 cur->c2 = n;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004542 if (n != NULL)
4543 n->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004544 cur = n;
Owen Taylor3473f882001-02-23 17:55:21 +00004545 }
4546 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004547 elem = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004548 if (elem == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004549 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004550 "xmlParseElementMixedContentDecl : Name expected\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004551 xmlFreeElementContent(cur);
4552 return(NULL);
4553 }
4554 SKIP_BLANKS;
4555 GROW;
4556 }
4557 if ((RAW == ')') && (NXT(1) == '*')) {
4558 if (elem != NULL) {
4559 cur->c2 = xmlNewElementContent(elem,
4560 XML_ELEMENT_CONTENT_ELEMENT);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004561 if (cur->c2 != NULL)
4562 cur->c2->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004563 }
4564 ret->ocur = XML_ELEMENT_CONTENT_MULT;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004565 if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00004566 xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
4567"Element content declaration doesn't start and stop in the same entity\n",
4568 NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004569 }
Owen Taylor3473f882001-02-23 17:55:21 +00004570 SKIP(2);
4571 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00004572 xmlFreeElementContent(ret);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004573 xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004574 return(NULL);
4575 }
4576
4577 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004578 xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004579 }
4580 return(ret);
4581}
4582
4583/**
4584 * xmlParseElementChildrenContentDecl:
4585 * @ctxt: an XML parser context
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004586 * @inputchk: the input used for the current entity, needed for boundary checks
Owen Taylor3473f882001-02-23 17:55:21 +00004587 *
4588 * parse the declaration for a Mixed Element content
4589 * The leading '(' and spaces have been skipped in xmlParseElementContentDecl
4590 *
4591 *
4592 * [47] children ::= (choice | seq) ('?' | '*' | '+')?
4593 *
4594 * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')?
4595 *
4596 * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')'
4597 *
4598 * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')'
4599 *
4600 * [ VC: Proper Group/PE Nesting ] applies to [49] and [50]
4601 * TODO Parameter-entity replacement text must be properly nested
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004602 * with parenthesized groups. That is to say, if either of the
Owen Taylor3473f882001-02-23 17:55:21 +00004603 * opening or closing parentheses in a choice, seq, or Mixed
4604 * construct is contained in the replacement text for a parameter
4605 * entity, both must be contained in the same replacement text. For
4606 * interoperability, if a parameter-entity reference appears in a
4607 * choice, seq, or Mixed construct, its replacement text should not
4608 * be empty, and neither the first nor last non-blank character of
4609 * the replacement text should be a connector (| or ,).
4610 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00004611 * Returns the tree of xmlElementContentPtr describing the element
Owen Taylor3473f882001-02-23 17:55:21 +00004612 * hierarchy.
4613 */
4614xmlElementContentPtr
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004615xmlParseElementChildrenContentDecl (xmlParserCtxtPtr ctxt, int inputchk) {
Owen Taylor3473f882001-02-23 17:55:21 +00004616 xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004617 const xmlChar *elem;
Owen Taylor3473f882001-02-23 17:55:21 +00004618 xmlChar type = 0;
4619
4620 SKIP_BLANKS;
4621 GROW;
4622 if (RAW == '(') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004623 int inputid = ctxt->input->id;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004624
Owen Taylor3473f882001-02-23 17:55:21 +00004625 /* Recurse on first child */
4626 NEXT;
4627 SKIP_BLANKS;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004628 cur = ret = xmlParseElementChildrenContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004629 SKIP_BLANKS;
4630 GROW;
4631 } else {
Daniel Veillard76d66f42001-05-16 21:05:17 +00004632 elem = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004633 if (elem == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004634 xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004635 return(NULL);
4636 }
4637 cur = ret = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004638 if (cur == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004639 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004640 return(NULL);
4641 }
Owen Taylor3473f882001-02-23 17:55:21 +00004642 GROW;
4643 if (RAW == '?') {
4644 cur->ocur = XML_ELEMENT_CONTENT_OPT;
4645 NEXT;
4646 } else if (RAW == '*') {
4647 cur->ocur = XML_ELEMENT_CONTENT_MULT;
4648 NEXT;
4649 } else if (RAW == '+') {
4650 cur->ocur = XML_ELEMENT_CONTENT_PLUS;
4651 NEXT;
4652 } else {
4653 cur->ocur = XML_ELEMENT_CONTENT_ONCE;
4654 }
Owen Taylor3473f882001-02-23 17:55:21 +00004655 GROW;
4656 }
4657 SKIP_BLANKS;
4658 SHRINK;
4659 while (RAW != ')') {
4660 /*
4661 * Each loop we parse one separator and one element.
4662 */
4663 if (RAW == ',') {
4664 if (type == 0) type = CUR;
4665
4666 /*
4667 * Detect "Name | Name , Name" error
4668 */
4669 else if (type != CUR) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004670 xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004671 "xmlParseElementChildrenContentDecl : '%c' expected\n",
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004672 type);
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004673 if ((last != NULL) && (last != ret))
Owen Taylor3473f882001-02-23 17:55:21 +00004674 xmlFreeElementContent(last);
4675 if (ret != NULL)
4676 xmlFreeElementContent(ret);
4677 return(NULL);
4678 }
4679 NEXT;
4680
4681 op = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_SEQ);
4682 if (op == NULL) {
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004683 if ((last != NULL) && (last != ret))
4684 xmlFreeElementContent(last);
Owen Taylor3473f882001-02-23 17:55:21 +00004685 xmlFreeElementContent(ret);
4686 return(NULL);
4687 }
4688 if (last == NULL) {
4689 op->c1 = ret;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004690 if (ret != NULL)
4691 ret->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004692 ret = cur = op;
4693 } else {
4694 cur->c2 = op;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004695 if (op != NULL)
4696 op->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004697 op->c1 = last;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004698 if (last != NULL)
4699 last->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004700 cur =op;
4701 last = NULL;
4702 }
4703 } else if (RAW == '|') {
4704 if (type == 0) type = CUR;
4705
4706 /*
4707 * Detect "Name , Name | Name" error
4708 */
4709 else if (type != CUR) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004710 xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004711 "xmlParseElementChildrenContentDecl : '%c' expected\n",
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004712 type);
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004713 if ((last != NULL) && (last != ret))
Owen Taylor3473f882001-02-23 17:55:21 +00004714 xmlFreeElementContent(last);
4715 if (ret != NULL)
4716 xmlFreeElementContent(ret);
4717 return(NULL);
4718 }
4719 NEXT;
4720
4721 op = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR);
4722 if (op == NULL) {
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004723 if ((last != NULL) && (last != ret))
Owen Taylor3473f882001-02-23 17:55:21 +00004724 xmlFreeElementContent(last);
4725 if (ret != NULL)
4726 xmlFreeElementContent(ret);
4727 return(NULL);
4728 }
4729 if (last == NULL) {
4730 op->c1 = ret;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004731 if (ret != NULL)
4732 ret->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004733 ret = cur = op;
4734 } else {
4735 cur->c2 = op;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004736 if (op != NULL)
4737 op->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004738 op->c1 = last;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004739 if (last != NULL)
4740 last->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004741 cur =op;
4742 last = NULL;
4743 }
4744 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004745 xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004746 if (ret != NULL)
4747 xmlFreeElementContent(ret);
4748 return(NULL);
4749 }
4750 GROW;
4751 SKIP_BLANKS;
4752 GROW;
4753 if (RAW == '(') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004754 int inputid = ctxt->input->id;
Owen Taylor3473f882001-02-23 17:55:21 +00004755 /* Recurse on second child */
4756 NEXT;
4757 SKIP_BLANKS;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004758 last = xmlParseElementChildrenContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004759 SKIP_BLANKS;
4760 } else {
Daniel Veillard76d66f42001-05-16 21:05:17 +00004761 elem = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004762 if (elem == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004763 xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004764 if (ret != NULL)
4765 xmlFreeElementContent(ret);
4766 return(NULL);
4767 }
4768 last = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT);
Owen Taylor3473f882001-02-23 17:55:21 +00004769 if (RAW == '?') {
4770 last->ocur = XML_ELEMENT_CONTENT_OPT;
4771 NEXT;
4772 } else if (RAW == '*') {
4773 last->ocur = XML_ELEMENT_CONTENT_MULT;
4774 NEXT;
4775 } else if (RAW == '+') {
4776 last->ocur = XML_ELEMENT_CONTENT_PLUS;
4777 NEXT;
4778 } else {
4779 last->ocur = XML_ELEMENT_CONTENT_ONCE;
4780 }
4781 }
4782 SKIP_BLANKS;
4783 GROW;
4784 }
4785 if ((cur != NULL) && (last != NULL)) {
4786 cur->c2 = last;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004787 if (last != NULL)
4788 last->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004789 }
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004790 if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00004791 xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
4792"Element content declaration doesn't start and stop in the same entity\n",
4793 NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004794 }
Owen Taylor3473f882001-02-23 17:55:21 +00004795 NEXT;
4796 if (RAW == '?') {
Daniel Veillarde470df72001-04-18 21:41:07 +00004797 if (ret != NULL)
4798 ret->ocur = XML_ELEMENT_CONTENT_OPT;
Owen Taylor3473f882001-02-23 17:55:21 +00004799 NEXT;
4800 } else if (RAW == '*') {
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004801 if (ret != NULL) {
Daniel Veillarde470df72001-04-18 21:41:07 +00004802 ret->ocur = XML_ELEMENT_CONTENT_MULT;
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004803 cur = ret;
4804 /*
4805 * Some normalization:
4806 * (a | b* | c?)* == (a | b | c)*
4807 */
4808 while (cur->type == XML_ELEMENT_CONTENT_OR) {
4809 if ((cur->c1 != NULL) &&
4810 ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4811 (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT)))
4812 cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
4813 if ((cur->c2 != NULL) &&
4814 ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
4815 (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT)))
4816 cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
4817 cur = cur->c2;
4818 }
4819 }
Owen Taylor3473f882001-02-23 17:55:21 +00004820 NEXT;
4821 } else if (RAW == '+') {
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004822 if (ret != NULL) {
4823 int found = 0;
4824
Daniel Veillarde470df72001-04-18 21:41:07 +00004825 ret->ocur = XML_ELEMENT_CONTENT_PLUS;
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004826 /*
4827 * Some normalization:
4828 * (a | b*)+ == (a | b)*
4829 * (a | b?)+ == (a | b)*
4830 */
4831 while (cur->type == XML_ELEMENT_CONTENT_OR) {
4832 if ((cur->c1 != NULL) &&
4833 ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4834 (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4835 cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
4836 found = 1;
4837 }
4838 if ((cur->c2 != NULL) &&
4839 ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
4840 (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) {
4841 cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
4842 found = 1;
4843 }
4844 cur = cur->c2;
4845 }
4846 if (found)
4847 ret->ocur = XML_ELEMENT_CONTENT_MULT;
4848 }
Owen Taylor3473f882001-02-23 17:55:21 +00004849 NEXT;
4850 }
4851 return(ret);
4852}
4853
4854/**
4855 * xmlParseElementContentDecl:
4856 * @ctxt: an XML parser context
4857 * @name: the name of the element being defined.
4858 * @result: the Element Content pointer will be stored here if any
4859 *
4860 * parse the declaration for an Element content either Mixed or Children,
4861 * the cases EMPTY and ANY are handled directly in xmlParseElementDecl
4862 *
4863 * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children
4864 *
4865 * returns: the type of element content XML_ELEMENT_TYPE_xxx
4866 */
4867
4868int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004869xmlParseElementContentDecl(xmlParserCtxtPtr ctxt, const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00004870 xmlElementContentPtr *result) {
4871
4872 xmlElementContentPtr tree = NULL;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004873 int inputid = ctxt->input->id;
Owen Taylor3473f882001-02-23 17:55:21 +00004874 int res;
4875
4876 *result = NULL;
4877
4878 if (RAW != '(') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00004879 xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004880 "xmlParseElementContentDecl : %s '(' expected\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00004881 return(-1);
4882 }
4883 NEXT;
4884 GROW;
4885 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004886 if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004887 tree = xmlParseElementMixedContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004888 res = XML_ELEMENT_TYPE_MIXED;
4889 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004890 tree = xmlParseElementChildrenContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004891 res = XML_ELEMENT_TYPE_ELEMENT;
4892 }
Owen Taylor3473f882001-02-23 17:55:21 +00004893 SKIP_BLANKS;
4894 *result = tree;
4895 return(res);
4896}
4897
4898/**
4899 * xmlParseElementDecl:
4900 * @ctxt: an XML parser context
4901 *
4902 * parse an Element declaration.
4903 *
4904 * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>'
4905 *
4906 * [ VC: Unique Element Type Declaration ]
4907 * No element type may be declared more than once
4908 *
4909 * Returns the type of the element, or -1 in case of error
4910 */
4911int
4912xmlParseElementDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004913 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00004914 int ret = -1;
4915 xmlElementContentPtr content = NULL;
4916
4917 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004918 if (CMP9(CUR_PTR, '<', '!', 'E', 'L', 'E', 'M', 'E', 'N', 'T')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004919 xmlParserInputPtr input = ctxt->input;
4920
4921 SKIP(9);
William M. Brack76e95df2003-10-18 16:20:14 +00004922 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004923 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4924 "Space required after 'ELEMENT'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004925 }
4926 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004927 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004928 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004929 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4930 "xmlParseElementDecl: no name for Element\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004931 return(-1);
4932 }
4933 while ((RAW == 0) && (ctxt->inputNr > 1))
4934 xmlPopInput(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00004935 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004936 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4937 "Space required after the element name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004938 }
4939 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004940 if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004941 SKIP(5);
4942 /*
4943 * Element must always be empty.
4944 */
4945 ret = XML_ELEMENT_TYPE_EMPTY;
4946 } else if ((RAW == 'A') && (NXT(1) == 'N') &&
4947 (NXT(2) == 'Y')) {
4948 SKIP(3);
4949 /*
4950 * Element is a generic container.
4951 */
4952 ret = XML_ELEMENT_TYPE_ANY;
4953 } else if (RAW == '(') {
4954 ret = xmlParseElementContentDecl(ctxt, name, &content);
4955 } else {
4956 /*
4957 * [ WFC: PEs in Internal Subset ] error handling.
4958 */
4959 if ((RAW == '%') && (ctxt->external == 0) &&
4960 (ctxt->inputNr == 1)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004961 xmlFatalErrMsg(ctxt, XML_ERR_PEREF_IN_INT_SUBSET,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004962 "PEReference: forbidden within markup decl in internal subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004963 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00004964 xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
Owen Taylor3473f882001-02-23 17:55:21 +00004965 "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n");
4966 }
Owen Taylor3473f882001-02-23 17:55:21 +00004967 return(-1);
4968 }
4969
4970 SKIP_BLANKS;
4971 /*
4972 * Pop-up of finished entities.
4973 */
4974 while ((RAW == 0) && (ctxt->inputNr > 1))
4975 xmlPopInput(ctxt);
4976 SKIP_BLANKS;
4977
4978 if (RAW != '>') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004979 xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004980 } else {
4981 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004982 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
4983 "Element declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004984 }
4985
4986 NEXT;
4987 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4988 (ctxt->sax->elementDecl != NULL))
4989 ctxt->sax->elementDecl(ctxt->userData, name, ret,
4990 content);
4991 }
4992 if (content != NULL) {
4993 xmlFreeElementContent(content);
4994 }
Owen Taylor3473f882001-02-23 17:55:21 +00004995 }
4996 return(ret);
4997}
4998
4999/**
Owen Taylor3473f882001-02-23 17:55:21 +00005000 * xmlParseConditionalSections
5001 * @ctxt: an XML parser context
5002 *
5003 * [61] conditionalSect ::= includeSect | ignoreSect
5004 * [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>'
5005 * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>'
5006 * [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)*
5007 * [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*)
5008 */
5009
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005010static void
Owen Taylor3473f882001-02-23 17:55:21 +00005011xmlParseConditionalSections(xmlParserCtxtPtr ctxt) {
5012 SKIP(3);
5013 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00005014 if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005015 SKIP(7);
5016 SKIP_BLANKS;
5017 if (RAW != '[') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005018 xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005019 } else {
5020 NEXT;
5021 }
5022 if (xmlParserDebugEntities) {
5023 if ((ctxt->input != NULL) && (ctxt->input->filename))
5024 xmlGenericError(xmlGenericErrorContext,
5025 "%s(%d): ", ctxt->input->filename,
5026 ctxt->input->line);
5027 xmlGenericError(xmlGenericErrorContext,
5028 "Entering INCLUDE Conditional Section\n");
5029 }
5030
5031 while ((RAW != 0) && ((RAW != ']') || (NXT(1) != ']') ||
5032 (NXT(2) != '>'))) {
5033 const xmlChar *check = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00005034 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00005035
5036 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5037 xmlParseConditionalSections(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00005038 } else if (IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005039 NEXT;
5040 } else if (RAW == '%') {
5041 xmlParsePEReference(ctxt);
5042 } else
5043 xmlParseMarkupDecl(ctxt);
5044
5045 /*
5046 * Pop-up of finished entities.
5047 */
5048 while ((RAW == 0) && (ctxt->inputNr > 1))
5049 xmlPopInput(ctxt);
5050
Daniel Veillardfdc91562002-07-01 21:52:03 +00005051 if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005052 xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005053 break;
5054 }
5055 }
5056 if (xmlParserDebugEntities) {
5057 if ((ctxt->input != NULL) && (ctxt->input->filename))
5058 xmlGenericError(xmlGenericErrorContext,
5059 "%s(%d): ", ctxt->input->filename,
5060 ctxt->input->line);
5061 xmlGenericError(xmlGenericErrorContext,
5062 "Leaving INCLUDE Conditional Section\n");
5063 }
5064
Daniel Veillarda07050d2003-10-19 14:46:32 +00005065 } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005066 int state;
William M. Brack78637da2003-07-31 14:47:38 +00005067 xmlParserInputState instate;
Owen Taylor3473f882001-02-23 17:55:21 +00005068 int depth = 0;
5069
5070 SKIP(6);
5071 SKIP_BLANKS;
5072 if (RAW != '[') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005073 xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005074 } else {
5075 NEXT;
5076 }
5077 if (xmlParserDebugEntities) {
5078 if ((ctxt->input != NULL) && (ctxt->input->filename))
5079 xmlGenericError(xmlGenericErrorContext,
5080 "%s(%d): ", ctxt->input->filename,
5081 ctxt->input->line);
5082 xmlGenericError(xmlGenericErrorContext,
5083 "Entering IGNORE Conditional Section\n");
5084 }
5085
5086 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005087 * Parse up to the end of the conditional section
Owen Taylor3473f882001-02-23 17:55:21 +00005088 * But disable SAX event generating DTD building in the meantime
5089 */
5090 state = ctxt->disableSAX;
5091 instate = ctxt->instate;
Daniel Veillarddad3f682002-11-17 16:47:27 +00005092 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00005093 ctxt->instate = XML_PARSER_IGNORE;
5094
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00005095 while ((depth >= 0) && (RAW != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005096 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5097 depth++;
5098 SKIP(3);
5099 continue;
5100 }
5101 if ((RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) {
5102 if (--depth >= 0) SKIP(3);
5103 continue;
5104 }
5105 NEXT;
5106 continue;
5107 }
5108
5109 ctxt->disableSAX = state;
5110 ctxt->instate = instate;
5111
5112 if (xmlParserDebugEntities) {
5113 if ((ctxt->input != NULL) && (ctxt->input->filename))
5114 xmlGenericError(xmlGenericErrorContext,
5115 "%s(%d): ", ctxt->input->filename,
5116 ctxt->input->line);
5117 xmlGenericError(xmlGenericErrorContext,
5118 "Leaving IGNORE Conditional Section\n");
5119 }
5120
5121 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005122 xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005123 }
5124
5125 if (RAW == 0)
5126 SHRINK;
5127
5128 if (RAW == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005129 xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005130 } else {
5131 SKIP(3);
5132 }
5133}
5134
5135/**
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005136 * xmlParseMarkupDecl:
5137 * @ctxt: an XML parser context
5138 *
5139 * parse Markup declarations
5140 *
5141 * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl |
5142 * NotationDecl | PI | Comment
5143 *
5144 * [ VC: Proper Declaration/PE Nesting ]
5145 * Parameter-entity replacement text must be properly nested with
5146 * markup declarations. That is to say, if either the first character
5147 * or the last character of a markup declaration (markupdecl above) is
5148 * contained in the replacement text for a parameter-entity reference,
5149 * both must be contained in the same replacement text.
5150 *
5151 * [ WFC: PEs in Internal Subset ]
5152 * In the internal DTD subset, parameter-entity references can occur
5153 * only where markup declarations can occur, not within markup declarations.
5154 * (This does not apply to references that occur in external parameter
5155 * entities or to the external subset.)
5156 */
5157void
5158xmlParseMarkupDecl(xmlParserCtxtPtr ctxt) {
5159 GROW;
5160 xmlParseElementDecl(ctxt);
5161 xmlParseAttributeListDecl(ctxt);
5162 xmlParseEntityDecl(ctxt);
5163 xmlParseNotationDecl(ctxt);
5164 xmlParsePI(ctxt);
5165 xmlParseComment(ctxt);
5166 /*
5167 * This is only for internal subset. On external entities,
5168 * the replacement is done before parsing stage
5169 */
5170 if ((ctxt->external == 0) && (ctxt->inputNr == 1))
5171 xmlParsePEReference(ctxt);
5172
5173 /*
5174 * Conditional sections are allowed from entities included
5175 * by PE References in the internal subset.
5176 */
5177 if ((ctxt->external == 0) && (ctxt->inputNr > 1)) {
5178 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5179 xmlParseConditionalSections(ctxt);
5180 }
5181 }
5182
5183 ctxt->instate = XML_PARSER_DTD;
5184}
5185
5186/**
5187 * xmlParseTextDecl:
5188 * @ctxt: an XML parser context
5189 *
5190 * parse an XML declaration header for external entities
5191 *
5192 * [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>'
5193 *
5194 * Question: Seems that EncodingDecl is mandatory ? Is that a typo ?
5195 */
5196
5197void
5198xmlParseTextDecl(xmlParserCtxtPtr ctxt) {
5199 xmlChar *version;
Daniel Veillardf5cb3cd2003-10-28 13:58:13 +00005200 const xmlChar *encoding;
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005201
5202 /*
5203 * We know that '<?xml' is here.
5204 */
Daniel Veillarda07050d2003-10-19 14:46:32 +00005205 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005206 SKIP(5);
5207 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005208 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005209 return;
5210 }
5211
William M. Brack76e95df2003-10-18 16:20:14 +00005212 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005213 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5214 "Space needed after '<?xml'\n");
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005215 }
5216 SKIP_BLANKS;
5217
5218 /*
5219 * We may have the VersionInfo here.
5220 */
5221 version = xmlParseVersionInfo(ctxt);
5222 if (version == NULL)
5223 version = xmlCharStrdup(XML_DEFAULT_VERSION);
Daniel Veillard401c2112002-01-07 16:54:10 +00005224 else {
William M. Brack76e95df2003-10-18 16:20:14 +00005225 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005226 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5227 "Space needed here\n");
Daniel Veillard401c2112002-01-07 16:54:10 +00005228 }
5229 }
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005230 ctxt->input->version = version;
5231
5232 /*
5233 * We must have the encoding declaration
5234 */
Daniel Veillardf5cb3cd2003-10-28 13:58:13 +00005235 encoding = xmlParseEncodingDecl(ctxt);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005236 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
5237 /*
5238 * The XML REC instructs us to stop parsing right here
5239 */
5240 return;
5241 }
Daniel Veillardf5cb3cd2003-10-28 13:58:13 +00005242 if ((encoding == NULL) && (ctxt->errNo == XML_ERR_OK)) {
5243 xmlFatalErrMsg(ctxt, XML_ERR_MISSING_ENCODING,
5244 "Missing encoding in text declaration\n");
5245 }
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005246
5247 SKIP_BLANKS;
5248 if ((RAW == '?') && (NXT(1) == '>')) {
5249 SKIP(2);
5250 } else if (RAW == '>') {
5251 /* Deprecated old WD ... */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005252 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005253 NEXT;
5254 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005255 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005256 MOVETO_ENDTAG(CUR_PTR);
5257 NEXT;
5258 }
5259}
5260
5261/**
Owen Taylor3473f882001-02-23 17:55:21 +00005262 * xmlParseExternalSubset:
5263 * @ctxt: an XML parser context
5264 * @ExternalID: the external identifier
5265 * @SystemID: the system identifier (or URL)
5266 *
5267 * parse Markup declarations from an external subset
5268 *
5269 * [30] extSubset ::= textDecl? extSubsetDecl
5270 *
5271 * [31] extSubsetDecl ::= (markupdecl | conditionalSect | PEReference | S) *
5272 */
5273void
5274xmlParseExternalSubset(xmlParserCtxtPtr ctxt, const xmlChar *ExternalID,
5275 const xmlChar *SystemID) {
Daniel Veillard309f81d2003-09-23 09:02:53 +00005276 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005277 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00005278 if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005279 xmlParseTextDecl(ctxt);
5280 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
5281 /*
5282 * The XML REC instructs us to stop parsing right here
5283 */
5284 ctxt->instate = XML_PARSER_EOF;
5285 return;
5286 }
5287 }
5288 if (ctxt->myDoc == NULL) {
5289 ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
5290 }
5291 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset == NULL))
5292 xmlCreateIntSubset(ctxt->myDoc, NULL, ExternalID, SystemID);
5293
5294 ctxt->instate = XML_PARSER_DTD;
5295 ctxt->external = 1;
5296 while (((RAW == '<') && (NXT(1) == '?')) ||
5297 ((RAW == '<') && (NXT(1) == '!')) ||
William M. Brack76e95df2003-10-18 16:20:14 +00005298 (RAW == '%') || IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005299 const xmlChar *check = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00005300 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00005301
5302 GROW;
5303 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5304 xmlParseConditionalSections(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00005305 } else if (IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005306 NEXT;
5307 } else if (RAW == '%') {
5308 xmlParsePEReference(ctxt);
5309 } else
5310 xmlParseMarkupDecl(ctxt);
5311
5312 /*
5313 * Pop-up of finished entities.
5314 */
5315 while ((RAW == 0) && (ctxt->inputNr > 1))
5316 xmlPopInput(ctxt);
5317
Daniel Veillardfdc91562002-07-01 21:52:03 +00005318 if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005319 xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005320 break;
5321 }
5322 }
5323
5324 if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005325 xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005326 }
5327
5328}
5329
5330/**
5331 * xmlParseReference:
5332 * @ctxt: an XML parser context
5333 *
5334 * parse and handle entity references in content, depending on the SAX
5335 * interface, this may end-up in a call to character() if this is a
5336 * CharRef, a predefined entity, if there is no reference() callback.
5337 * or if the parser was asked to switch to that mode.
5338 *
5339 * [67] Reference ::= EntityRef | CharRef
5340 */
5341void
5342xmlParseReference(xmlParserCtxtPtr ctxt) {
5343 xmlEntityPtr ent;
5344 xmlChar *val;
5345 if (RAW != '&') return;
5346
5347 if (NXT(1) == '#') {
5348 int i = 0;
5349 xmlChar out[10];
5350 int hex = NXT(2);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005351 int value = xmlParseCharRef(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005352
5353 if (ctxt->charset != XML_CHAR_ENCODING_UTF8) {
5354 /*
5355 * So we are using non-UTF-8 buffers
5356 * Check that the char fit on 8bits, if not
5357 * generate a CharRef.
5358 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005359 if (value <= 0xFF) {
5360 out[0] = value;
Owen Taylor3473f882001-02-23 17:55:21 +00005361 out[1] = 0;
5362 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
5363 (!ctxt->disableSAX))
5364 ctxt->sax->characters(ctxt->userData, out, 1);
5365 } else {
5366 if ((hex == 'x') || (hex == 'X'))
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005367 snprintf((char *)out, sizeof(out), "#x%X", value);
Owen Taylor3473f882001-02-23 17:55:21 +00005368 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005369 snprintf((char *)out, sizeof(out), "#%d", value);
Owen Taylor3473f882001-02-23 17:55:21 +00005370 if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) &&
5371 (!ctxt->disableSAX))
5372 ctxt->sax->reference(ctxt->userData, out);
5373 }
5374 } else {
5375 /*
5376 * Just encode the value in UTF-8
5377 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005378 COPY_BUF(0 ,out, i, value);
Owen Taylor3473f882001-02-23 17:55:21 +00005379 out[i] = 0;
5380 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
5381 (!ctxt->disableSAX))
5382 ctxt->sax->characters(ctxt->userData, out, i);
5383 }
5384 } else {
5385 ent = xmlParseEntityRef(ctxt);
5386 if (ent == NULL) return;
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00005387 if (!ctxt->wellFormed)
5388 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005389 if ((ent->name != NULL) &&
5390 (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY)) {
5391 xmlNodePtr list = NULL;
Daniel Veillard7d515752003-09-26 19:12:37 +00005392 xmlParserErrors ret = XML_ERR_OK;
Owen Taylor3473f882001-02-23 17:55:21 +00005393
5394
5395 /*
5396 * The first reference to the entity trigger a parsing phase
5397 * where the ent->children is filled with the result from
5398 * the parsing.
5399 */
5400 if (ent->children == NULL) {
5401 xmlChar *value;
5402 value = ent->content;
5403
5404 /*
5405 * Check that this entity is well formed
5406 */
Daniel Veillard4aede2e2003-10-17 12:43:59 +00005407 if ((value != NULL) && (value[0] != 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +00005408 (value[1] == 0) && (value[0] == '<') &&
5409 (xmlStrEqual(ent->name, BAD_CAST "lt"))) {
5410 /*
5411 * DONE: get definite answer on this !!!
5412 * Lots of entity decls are used to declare a single
5413 * char
5414 * <!ENTITY lt "<">
5415 * Which seems to be valid since
5416 * 2.4: The ampersand character (&) and the left angle
5417 * bracket (<) may appear in their literal form only
5418 * when used ... They are also legal within the literal
5419 * entity value of an internal entity declaration;i
5420 * see "4.3.2 Well-Formed Parsed Entities".
5421 * IMHO 2.4 and 4.3.2 are directly in contradiction.
5422 * Looking at the OASIS test suite and James Clark
5423 * tests, this is broken. However the XML REC uses
5424 * it. Is the XML REC not well-formed ????
5425 * This is a hack to avoid this problem
5426 *
5427 * ANSWER: since lt gt amp .. are already defined,
5428 * this is a redefinition and hence the fact that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005429 * content is not well balanced is not a Wf error, this
Owen Taylor3473f882001-02-23 17:55:21 +00005430 * is lousy but acceptable.
5431 */
5432 list = xmlNewDocText(ctxt->myDoc, value);
5433 if (list != NULL) {
5434 if ((ent->etype == XML_INTERNAL_GENERAL_ENTITY) &&
5435 (ent->children == NULL)) {
5436 ent->children = list;
5437 ent->last = list;
Daniel Veillard2d84a892002-12-30 00:01:08 +00005438 ent->owner = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00005439 list->parent = (xmlNodePtr) ent;
5440 } else {
5441 xmlFreeNodeList(list);
5442 }
5443 } else if (list != NULL) {
5444 xmlFreeNodeList(list);
5445 }
5446 } else {
5447 /*
5448 * 4.3.2: An internal general parsed entity is well-formed
5449 * if its replacement text matches the production labeled
5450 * content.
5451 */
Daniel Veillardb5a60ec2002-03-18 11:45:56 +00005452
5453 void *user_data;
5454 /*
5455 * This is a bit hackish but this seems the best
5456 * way to make sure both SAX and DOM entity support
5457 * behaves okay.
5458 */
5459 if (ctxt->userData == ctxt)
5460 user_data = NULL;
5461 else
5462 user_data = ctxt->userData;
5463
Owen Taylor3473f882001-02-23 17:55:21 +00005464 if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) {
5465 ctxt->depth++;
Daniel Veillard328f48c2002-11-15 15:24:34 +00005466 ret = xmlParseBalancedChunkMemoryInternal(ctxt,
5467 value, user_data, &list);
Owen Taylor3473f882001-02-23 17:55:21 +00005468 ctxt->depth--;
5469 } else if (ent->etype ==
5470 XML_EXTERNAL_GENERAL_PARSED_ENTITY) {
5471 ctxt->depth++;
Daniel Veillarda97a19b2001-05-20 13:19:52 +00005472 ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt,
Daniel Veillardb5a60ec2002-03-18 11:45:56 +00005473 ctxt->sax, user_data, ctxt->depth,
Daniel Veillarda97a19b2001-05-20 13:19:52 +00005474 ent->URI, ent->ExternalID, &list);
Owen Taylor3473f882001-02-23 17:55:21 +00005475 ctxt->depth--;
5476 } else {
Daniel Veillard7d515752003-09-26 19:12:37 +00005477 ret = XML_ERR_ENTITY_PE_INTERNAL;
Daniel Veillardf403d292003-10-05 13:51:35 +00005478 xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR,
5479 "invalid entity type found\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005480 }
5481 if (ret == XML_ERR_ENTITY_LOOP) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005482 xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00005483 return;
Daniel Veillard7d515752003-09-26 19:12:37 +00005484 } else if ((ret == XML_ERR_OK) && (list != NULL)) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00005485 if (((ent->etype == XML_INTERNAL_GENERAL_ENTITY) ||
5486 (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY))&&
Owen Taylor3473f882001-02-23 17:55:21 +00005487 (ent->children == NULL)) {
5488 ent->children = list;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005489 if (ctxt->replaceEntities) {
5490 /*
5491 * Prune it directly in the generated document
5492 * except for single text nodes.
5493 */
5494 if ((list->type == XML_TEXT_NODE) &&
5495 (list->next == NULL)) {
5496 list->parent = (xmlNodePtr) ent;
5497 list = NULL;
Daniel Veillard2d84a892002-12-30 00:01:08 +00005498 ent->owner = 1;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005499 } else {
Daniel Veillard2d84a892002-12-30 00:01:08 +00005500 ent->owner = 0;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005501 while (list != NULL) {
5502 list->parent = (xmlNodePtr) ctxt->node;
Daniel Veillard68e9e742002-11-16 15:35:11 +00005503 list->doc = ctxt->myDoc;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005504 if (list->next == NULL)
5505 ent->last = list;
5506 list = list->next;
Daniel Veillard8107a222002-01-13 14:10:10 +00005507 }
Daniel Veillard62f313b2001-07-04 19:49:14 +00005508 list = ent->children;
Daniel Veillard81273902003-09-30 00:43:48 +00005509#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +00005510 if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
5511 xmlAddEntityReference(ent, list, NULL);
Daniel Veillard81273902003-09-30 00:43:48 +00005512#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillard62f313b2001-07-04 19:49:14 +00005513 }
5514 } else {
Daniel Veillard2d84a892002-12-30 00:01:08 +00005515 ent->owner = 1;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005516 while (list != NULL) {
5517 list->parent = (xmlNodePtr) ent;
5518 if (list->next == NULL)
5519 ent->last = list;
5520 list = list->next;
5521 }
Owen Taylor3473f882001-02-23 17:55:21 +00005522 }
5523 } else {
5524 xmlFreeNodeList(list);
Daniel Veillard62f313b2001-07-04 19:49:14 +00005525 list = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005526 }
William M. Brackb670e2e2003-09-27 01:05:55 +00005527 } else if ((ret != XML_ERR_OK) &&
5528 (ret != XML_WAR_UNDECLARED_ENTITY)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005529 xmlFatalErr(ctxt, ret, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005530 } else if (list != NULL) {
5531 xmlFreeNodeList(list);
Daniel Veillard62f313b2001-07-04 19:49:14 +00005532 list = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005533 }
5534 }
5535 }
5536 if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) &&
5537 (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) {
5538 /*
5539 * Create a node.
5540 */
5541 ctxt->sax->reference(ctxt->userData, ent->name);
5542 return;
5543 } else if (ctxt->replaceEntities) {
5544 if ((ctxt->node != NULL) && (ent->children != NULL)) {
5545 /*
5546 * Seems we are generating the DOM content, do
Daniel Veillard62f313b2001-07-04 19:49:14 +00005547 * a simple tree copy for all references except the first
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005548 * In the first occurrence list contains the replacement
Owen Taylor3473f882001-02-23 17:55:21 +00005549 */
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005550 if ((list == NULL) && (ent->owner == 0)) {
5551 xmlNodePtr nw = NULL, cur, firstChild = NULL;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005552 cur = ent->children;
5553 while (cur != NULL) {
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005554 nw = xmlCopyNode(cur, 1);
5555 if (nw != NULL) {
5556 nw->_private = cur->_private;
Daniel Veillard8f872442003-01-09 23:19:02 +00005557 if (firstChild == NULL){
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005558 firstChild = nw;
Daniel Veillard8f872442003-01-09 23:19:02 +00005559 }
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005560 xmlAddChild(ctxt->node, nw);
Daniel Veillard8107a222002-01-13 14:10:10 +00005561 }
Daniel Veillard62f313b2001-07-04 19:49:14 +00005562 if (cur == ent->last)
5563 break;
5564 cur = cur->next;
5565 }
Daniel Veillard81273902003-09-30 00:43:48 +00005566#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +00005567 if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005568 xmlAddEntityReference(ent, firstChild, nw);
Daniel Veillard81273902003-09-30 00:43:48 +00005569#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005570 } else if (list == NULL) {
5571 xmlNodePtr nw = NULL, cur, next, last,
5572 firstChild = NULL;
5573 /*
5574 * Copy the entity child list and make it the new
5575 * entity child list. The goal is to make sure any
5576 * ID or REF referenced will be the one from the
5577 * document content and not the entity copy.
5578 */
5579 cur = ent->children;
5580 ent->children = NULL;
5581 last = ent->last;
5582 ent->last = NULL;
5583 while (cur != NULL) {
5584 next = cur->next;
5585 cur->next = NULL;
5586 cur->parent = NULL;
5587 nw = xmlCopyNode(cur, 1);
5588 if (nw != NULL) {
5589 nw->_private = cur->_private;
5590 if (firstChild == NULL){
5591 firstChild = cur;
5592 }
5593 xmlAddChild((xmlNodePtr) ent, nw);
5594 xmlAddChild(ctxt->node, cur);
5595 }
5596 if (cur == last)
5597 break;
5598 cur = next;
5599 }
5600 ent->owner = 1;
Daniel Veillard81273902003-09-30 00:43:48 +00005601#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005602 if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
5603 xmlAddEntityReference(ent, firstChild, nw);
Daniel Veillard81273902003-09-30 00:43:48 +00005604#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillard62f313b2001-07-04 19:49:14 +00005605 } else {
5606 /*
5607 * the name change is to avoid coalescing of the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005608 * node with a possible previous text one which
5609 * would make ent->children a dangling pointer
Daniel Veillard62f313b2001-07-04 19:49:14 +00005610 */
5611 if (ent->children->type == XML_TEXT_NODE)
5612 ent->children->name = xmlStrdup(BAD_CAST "nbktext");
5613 if ((ent->last != ent->children) &&
5614 (ent->last->type == XML_TEXT_NODE))
5615 ent->last->name = xmlStrdup(BAD_CAST "nbktext");
5616 xmlAddChildList(ctxt->node, ent->children);
5617 }
5618
Owen Taylor3473f882001-02-23 17:55:21 +00005619 /*
5620 * This is to avoid a nasty side effect, see
5621 * characters() in SAX.c
5622 */
5623 ctxt->nodemem = 0;
5624 ctxt->nodelen = 0;
5625 return;
5626 } else {
5627 /*
5628 * Probably running in SAX mode
5629 */
5630 xmlParserInputPtr input;
5631
5632 input = xmlNewEntityInputStream(ctxt, ent);
5633 xmlPushInput(ctxt, input);
5634 if ((ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) &&
Daniel Veillarda07050d2003-10-19 14:46:32 +00005635 (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) &&
5636 (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00005637 xmlParseTextDecl(ctxt);
5638 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
5639 /*
5640 * The XML REC instructs us to stop parsing right here
5641 */
5642 ctxt->instate = XML_PARSER_EOF;
5643 return;
5644 }
5645 if (input->standalone == 1) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005646 xmlFatalErr(ctxt, XML_ERR_EXT_ENTITY_STANDALONE,
5647 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005648 }
5649 }
5650 return;
5651 }
5652 }
5653 } else {
5654 val = ent->content;
5655 if (val == NULL) return;
5656 /*
5657 * inline the entity.
5658 */
5659 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
5660 (!ctxt->disableSAX))
5661 ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val));
5662 }
5663 }
5664}
5665
5666/**
5667 * xmlParseEntityRef:
5668 * @ctxt: an XML parser context
5669 *
5670 * parse ENTITY references declarations
5671 *
5672 * [68] EntityRef ::= '&' Name ';'
5673 *
5674 * [ WFC: Entity Declared ]
5675 * In a document without any DTD, a document with only an internal DTD
5676 * subset which contains no parameter entity references, or a document
5677 * with "standalone='yes'", the Name given in the entity reference
5678 * must match that in an entity declaration, except that well-formed
5679 * documents need not declare any of the following entities: amp, lt,
5680 * gt, apos, quot. The declaration of a parameter entity must precede
5681 * any reference to it. Similarly, the declaration of a general entity
5682 * must precede any reference to it which appears in a default value in an
5683 * attribute-list declaration. Note that if entities are declared in the
5684 * external subset or in external parameter entities, a non-validating
5685 * processor is not obligated to read and process their declarations;
5686 * for such documents, the rule that an entity must be declared is a
5687 * well-formedness constraint only if standalone='yes'.
5688 *
5689 * [ WFC: Parsed Entity ]
5690 * An entity reference must not contain the name of an unparsed entity
5691 *
5692 * Returns the xmlEntityPtr if found, or NULL otherwise.
5693 */
5694xmlEntityPtr
5695xmlParseEntityRef(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00005696 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00005697 xmlEntityPtr ent = NULL;
5698
5699 GROW;
5700
5701 if (RAW == '&') {
5702 NEXT;
5703 name = xmlParseName(ctxt);
5704 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005705 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
5706 "xmlParseEntityRef: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005707 } else {
5708 if (RAW == ';') {
5709 NEXT;
5710 /*
5711 * Ask first SAX for entity resolution, otherwise try the
5712 * predefined set.
5713 */
5714 if (ctxt->sax != NULL) {
5715 if (ctxt->sax->getEntity != NULL)
5716 ent = ctxt->sax->getEntity(ctxt->userData, name);
Daniel Veillard39eb88b2003-03-11 11:21:28 +00005717 if ((ctxt->wellFormed == 1 ) && (ent == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00005718 ent = xmlGetPredefinedEntity(name);
Daniel Veillard39eb88b2003-03-11 11:21:28 +00005719 if ((ctxt->wellFormed == 1 ) && (ent == NULL) &&
5720 (ctxt->userData==ctxt)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00005721 ent = xmlSAX2GetEntity(ctxt, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00005722 }
Owen Taylor3473f882001-02-23 17:55:21 +00005723 }
5724 /*
5725 * [ WFC: Entity Declared ]
5726 * In a document without any DTD, a document with only an
5727 * internal DTD subset which contains no parameter entity
5728 * references, or a document with "standalone='yes'", the
5729 * Name given in the entity reference must match that in an
5730 * entity declaration, except that well-formed documents
5731 * need not declare any of the following entities: amp, lt,
5732 * gt, apos, quot.
5733 * The declaration of a parameter entity must precede any
5734 * reference to it.
5735 * Similarly, the declaration of a general entity must
5736 * precede any reference to it which appears in a default
5737 * value in an attribute-list declaration. Note that if
5738 * entities are declared in the external subset or in
5739 * external parameter entities, a non-validating processor
5740 * is not obligated to read and process their declarations;
5741 * for such documents, the rule that an entity must be
5742 * declared is a well-formedness constraint only if
5743 * standalone='yes'.
5744 */
5745 if (ent == NULL) {
5746 if ((ctxt->standalone == 1) ||
5747 ((ctxt->hasExternalSubset == 0) &&
5748 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005749 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00005750 "Entity '%s' not defined\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005751 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00005752 xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00005753 "Entity '%s' not defined\n", name);
5754 }
Daniel Veillardf403d292003-10-05 13:51:35 +00005755 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005756 }
5757
5758 /*
5759 * [ WFC: Parsed Entity ]
5760 * An entity reference must not contain the name of an
5761 * unparsed entity
5762 */
5763 else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005764 xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00005765 "Entity reference to unparsed entity %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005766 }
5767
5768 /*
5769 * [ WFC: No External Entity References ]
5770 * Attribute values cannot contain direct or indirect
5771 * entity references to external entities.
5772 */
5773 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
5774 (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005775 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL,
5776 "Attribute references external entity '%s'\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005777 }
5778 /*
5779 * [ WFC: No < in Attribute Values ]
5780 * The replacement text of any entity referred to directly or
5781 * indirectly in an attribute value (other than "&lt;") must
5782 * not contain a <.
5783 */
5784 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
5785 (ent != NULL) &&
5786 (!xmlStrEqual(ent->name, BAD_CAST "lt")) &&
5787 (ent->content != NULL) &&
5788 (xmlStrchr(ent->content, '<'))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005789 xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
Owen Taylor3473f882001-02-23 17:55:21 +00005790 "'<' in entity '%s' is not allowed in attributes values\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005791 }
5792
5793 /*
5794 * Internal check, no parameter entities here ...
5795 */
5796 else {
5797 switch (ent->etype) {
5798 case XML_INTERNAL_PARAMETER_ENTITY:
5799 case XML_EXTERNAL_PARAMETER_ENTITY:
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005800 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER,
5801 "Attempt to reference the parameter entity '%s'\n",
5802 name);
Owen Taylor3473f882001-02-23 17:55:21 +00005803 break;
5804 default:
5805 break;
5806 }
5807 }
5808
5809 /*
5810 * [ WFC: No Recursion ]
5811 * A parsed entity must not contain a recursive reference
5812 * to itself, either directly or indirectly.
5813 * Done somewhere else
5814 */
5815
5816 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005817 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005818 }
Owen Taylor3473f882001-02-23 17:55:21 +00005819 }
5820 }
5821 return(ent);
5822}
5823
5824/**
5825 * xmlParseStringEntityRef:
5826 * @ctxt: an XML parser context
5827 * @str: a pointer to an index in the string
5828 *
5829 * parse ENTITY references declarations, but this version parses it from
5830 * a string value.
5831 *
5832 * [68] EntityRef ::= '&' Name ';'
5833 *
5834 * [ WFC: Entity Declared ]
5835 * In a document without any DTD, a document with only an internal DTD
5836 * subset which contains no parameter entity references, or a document
5837 * with "standalone='yes'", the Name given in the entity reference
5838 * must match that in an entity declaration, except that well-formed
5839 * documents need not declare any of the following entities: amp, lt,
5840 * gt, apos, quot. The declaration of a parameter entity must precede
5841 * any reference to it. Similarly, the declaration of a general entity
5842 * must precede any reference to it which appears in a default value in an
5843 * attribute-list declaration. Note that if entities are declared in the
5844 * external subset or in external parameter entities, a non-validating
5845 * processor is not obligated to read and process their declarations;
5846 * for such documents, the rule that an entity must be declared is a
5847 * well-formedness constraint only if standalone='yes'.
5848 *
5849 * [ WFC: Parsed Entity ]
5850 * An entity reference must not contain the name of an unparsed entity
5851 *
5852 * Returns the xmlEntityPtr if found, or NULL otherwise. The str pointer
5853 * is updated to the current location in the string.
5854 */
5855xmlEntityPtr
5856xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) {
5857 xmlChar *name;
5858 const xmlChar *ptr;
5859 xmlChar cur;
5860 xmlEntityPtr ent = NULL;
5861
5862 if ((str == NULL) || (*str == NULL))
5863 return(NULL);
5864 ptr = *str;
5865 cur = *ptr;
5866 if (cur == '&') {
5867 ptr++;
5868 cur = *ptr;
5869 name = xmlParseStringName(ctxt, &ptr);
5870 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005871 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
5872 "xmlParseStringEntityRef: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005873 } else {
5874 if (*ptr == ';') {
5875 ptr++;
5876 /*
5877 * Ask first SAX for entity resolution, otherwise try the
5878 * predefined set.
5879 */
5880 if (ctxt->sax != NULL) {
5881 if (ctxt->sax->getEntity != NULL)
5882 ent = ctxt->sax->getEntity(ctxt->userData, name);
5883 if (ent == NULL)
5884 ent = xmlGetPredefinedEntity(name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00005885 if ((ent == NULL) && (ctxt->userData==ctxt)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00005886 ent = xmlSAX2GetEntity(ctxt, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00005887 }
Owen Taylor3473f882001-02-23 17:55:21 +00005888 }
5889 /*
5890 * [ WFC: Entity Declared ]
5891 * In a document without any DTD, a document with only an
5892 * internal DTD subset which contains no parameter entity
5893 * references, or a document with "standalone='yes'", the
5894 * Name given in the entity reference must match that in an
5895 * entity declaration, except that well-formed documents
5896 * need not declare any of the following entities: amp, lt,
5897 * gt, apos, quot.
5898 * The declaration of a parameter entity must precede any
5899 * reference to it.
5900 * Similarly, the declaration of a general entity must
5901 * precede any reference to it which appears in a default
5902 * value in an attribute-list declaration. Note that if
5903 * entities are declared in the external subset or in
5904 * external parameter entities, a non-validating processor
5905 * is not obligated to read and process their declarations;
5906 * for such documents, the rule that an entity must be
5907 * declared is a well-formedness constraint only if
5908 * standalone='yes'.
5909 */
5910 if (ent == NULL) {
5911 if ((ctxt->standalone == 1) ||
5912 ((ctxt->hasExternalSubset == 0) &&
5913 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005914 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00005915 "Entity '%s' not defined\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005916 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00005917 xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY,
Daniel Veillard24eb9782003-10-04 21:08:09 +00005918 "Entity '%s' not defined\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00005919 name);
Owen Taylor3473f882001-02-23 17:55:21 +00005920 }
Daniel Veillard24eb9782003-10-04 21:08:09 +00005921 /* TODO ? check regressions ctxt->valid = 0; */
Owen Taylor3473f882001-02-23 17:55:21 +00005922 }
5923
5924 /*
5925 * [ WFC: Parsed Entity ]
5926 * An entity reference must not contain the name of an
5927 * unparsed entity
5928 */
5929 else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005930 xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00005931 "Entity reference to unparsed entity %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005932 }
5933
5934 /*
5935 * [ WFC: No External Entity References ]
5936 * Attribute values cannot contain direct or indirect
5937 * entity references to external entities.
5938 */
5939 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
5940 (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005941 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL,
Owen Taylor3473f882001-02-23 17:55:21 +00005942 "Attribute references external entity '%s'\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005943 }
5944 /*
5945 * [ WFC: No < in Attribute Values ]
5946 * The replacement text of any entity referred to directly or
5947 * indirectly in an attribute value (other than "&lt;") must
5948 * not contain a <.
5949 */
5950 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
5951 (ent != NULL) &&
5952 (!xmlStrEqual(ent->name, BAD_CAST "lt")) &&
5953 (ent->content != NULL) &&
5954 (xmlStrchr(ent->content, '<'))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005955 xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
5956 "'<' in entity '%s' is not allowed in attributes values\n",
5957 name);
Owen Taylor3473f882001-02-23 17:55:21 +00005958 }
5959
5960 /*
5961 * Internal check, no parameter entities here ...
5962 */
5963 else {
5964 switch (ent->etype) {
5965 case XML_INTERNAL_PARAMETER_ENTITY:
5966 case XML_EXTERNAL_PARAMETER_ENTITY:
Daniel Veillardf403d292003-10-05 13:51:35 +00005967 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER,
5968 "Attempt to reference the parameter entity '%s'\n",
5969 name);
Owen Taylor3473f882001-02-23 17:55:21 +00005970 break;
5971 default:
5972 break;
5973 }
5974 }
5975
5976 /*
5977 * [ WFC: No Recursion ]
5978 * A parsed entity must not contain a recursive reference
5979 * to itself, either directly or indirectly.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005980 * Done somewhere else
Owen Taylor3473f882001-02-23 17:55:21 +00005981 */
5982
5983 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005984 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005985 }
5986 xmlFree(name);
5987 }
5988 }
5989 *str = ptr;
5990 return(ent);
5991}
5992
5993/**
5994 * xmlParsePEReference:
5995 * @ctxt: an XML parser context
5996 *
5997 * parse PEReference declarations
5998 * The entity content is handled directly by pushing it's content as
5999 * a new input stream.
6000 *
6001 * [69] PEReference ::= '%' Name ';'
6002 *
6003 * [ WFC: No Recursion ]
6004 * A parsed entity must not contain a recursive
6005 * reference to itself, either directly or indirectly.
6006 *
6007 * [ WFC: Entity Declared ]
6008 * In a document without any DTD, a document with only an internal DTD
6009 * subset which contains no parameter entity references, or a document
6010 * with "standalone='yes'", ... ... The declaration of a parameter
6011 * entity must precede any reference to it...
6012 *
6013 * [ VC: Entity Declared ]
6014 * In a document with an external subset or external parameter entities
6015 * with "standalone='no'", ... ... The declaration of a parameter entity
6016 * must precede any reference to it...
6017 *
6018 * [ WFC: In DTD ]
6019 * Parameter-entity references may only appear in the DTD.
6020 * NOTE: misleading but this is handled.
6021 */
6022void
Daniel Veillard8f597c32003-10-06 08:19:27 +00006023xmlParsePEReference(xmlParserCtxtPtr ctxt)
6024{
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006025 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006026 xmlEntityPtr entity = NULL;
6027 xmlParserInputPtr input;
6028
6029 if (RAW == '%') {
6030 NEXT;
Daniel Veillard76d66f42001-05-16 21:05:17 +00006031 name = xmlParseName(ctxt);
Daniel Veillard8f597c32003-10-06 08:19:27 +00006032 if (name == NULL) {
6033 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6034 "xmlParsePEReference: no name\n");
6035 } else {
6036 if (RAW == ';') {
6037 NEXT;
6038 if ((ctxt->sax != NULL) &&
6039 (ctxt->sax->getParameterEntity != NULL))
6040 entity = ctxt->sax->getParameterEntity(ctxt->userData,
6041 name);
6042 if (entity == NULL) {
6043 /*
6044 * [ WFC: Entity Declared ]
6045 * In a document without any DTD, a document with only an
6046 * internal DTD subset which contains no parameter entity
6047 * references, or a document with "standalone='yes'", ...
6048 * ... The declaration of a parameter entity must precede
6049 * any reference to it...
6050 */
6051 if ((ctxt->standalone == 1) ||
6052 ((ctxt->hasExternalSubset == 0) &&
6053 (ctxt->hasPErefs == 0))) {
6054 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
6055 "PEReference: %%%s; not found\n",
6056 name);
6057 } else {
6058 /*
6059 * [ VC: Entity Declared ]
6060 * In a document with an external subset or external
6061 * parameter entities with "standalone='no'", ...
6062 * ... The declaration of a parameter entity must
6063 * precede any reference to it...
6064 */
6065 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6066 "PEReference: %%%s; not found\n",
6067 name, NULL);
6068 ctxt->valid = 0;
6069 }
6070 } else {
6071 /*
6072 * Internal checking in case the entity quest barfed
6073 */
6074 if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
6075 (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
6076 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6077 "Internal: %%%s; is not a parameter entity\n",
6078 name, NULL);
6079 } else if (ctxt->input->free != deallocblankswrapper) {
6080 input =
6081 xmlNewBlanksWrapperInputStream(ctxt, entity);
6082 xmlPushInput(ctxt, input);
6083 } else {
6084 /*
6085 * TODO !!!
6086 * handle the extra spaces added before and after
6087 * c.f. http://www.w3.org/TR/REC-xml#as-PE
6088 */
6089 input = xmlNewEntityInputStream(ctxt, entity);
6090 xmlPushInput(ctxt, input);
6091 if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) &&
Daniel Veillarda07050d2003-10-19 14:46:32 +00006092 (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) &&
William M. Brack76e95df2003-10-18 16:20:14 +00006093 (IS_BLANK_CH(NXT(5)))) {
Daniel Veillard8f597c32003-10-06 08:19:27 +00006094 xmlParseTextDecl(ctxt);
6095 if (ctxt->errNo ==
6096 XML_ERR_UNSUPPORTED_ENCODING) {
6097 /*
6098 * The XML REC instructs us to stop parsing
6099 * right here
6100 */
6101 ctxt->instate = XML_PARSER_EOF;
6102 return;
6103 }
6104 }
6105 }
6106 }
6107 ctxt->hasPErefs = 1;
6108 } else {
6109 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
6110 }
6111 }
Owen Taylor3473f882001-02-23 17:55:21 +00006112 }
6113}
6114
6115/**
6116 * xmlParseStringPEReference:
6117 * @ctxt: an XML parser context
6118 * @str: a pointer to an index in the string
6119 *
6120 * parse PEReference declarations
6121 *
6122 * [69] PEReference ::= '%' Name ';'
6123 *
6124 * [ WFC: No Recursion ]
6125 * A parsed entity must not contain a recursive
6126 * reference to itself, either directly or indirectly.
6127 *
6128 * [ WFC: Entity Declared ]
6129 * In a document without any DTD, a document with only an internal DTD
6130 * subset which contains no parameter entity references, or a document
6131 * with "standalone='yes'", ... ... The declaration of a parameter
6132 * entity must precede any reference to it...
6133 *
6134 * [ VC: Entity Declared ]
6135 * In a document with an external subset or external parameter entities
6136 * with "standalone='no'", ... ... The declaration of a parameter entity
6137 * must precede any reference to it...
6138 *
6139 * [ WFC: In DTD ]
6140 * Parameter-entity references may only appear in the DTD.
6141 * NOTE: misleading but this is handled.
6142 *
6143 * Returns the string of the entity content.
6144 * str is updated to the current value of the index
6145 */
6146xmlEntityPtr
6147xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) {
6148 const xmlChar *ptr;
6149 xmlChar cur;
6150 xmlChar *name;
6151 xmlEntityPtr entity = NULL;
6152
6153 if ((str == NULL) || (*str == NULL)) return(NULL);
6154 ptr = *str;
6155 cur = *ptr;
6156 if (cur == '%') {
6157 ptr++;
6158 cur = *ptr;
6159 name = xmlParseStringName(ctxt, &ptr);
6160 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006161 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6162 "xmlParseStringPEReference: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006163 } else {
6164 cur = *ptr;
6165 if (cur == ';') {
6166 ptr++;
6167 cur = *ptr;
6168 if ((ctxt->sax != NULL) &&
6169 (ctxt->sax->getParameterEntity != NULL))
6170 entity = ctxt->sax->getParameterEntity(ctxt->userData,
6171 name);
6172 if (entity == NULL) {
6173 /*
6174 * [ WFC: Entity Declared ]
6175 * In a document without any DTD, a document with only an
6176 * internal DTD subset which contains no parameter entity
6177 * references, or a document with "standalone='yes'", ...
6178 * ... The declaration of a parameter entity must precede
6179 * any reference to it...
6180 */
6181 if ((ctxt->standalone == 1) ||
6182 ((ctxt->hasExternalSubset == 0) &&
6183 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006184 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00006185 "PEReference: %%%s; not found\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006186 } else {
6187 /*
6188 * [ VC: Entity Declared ]
6189 * In a document with an external subset or external
6190 * parameter entities with "standalone='no'", ...
6191 * ... The declaration of a parameter entity must
6192 * precede any reference to it...
6193 */
Daniel Veillard24eb9782003-10-04 21:08:09 +00006194 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6195 "PEReference: %%%s; not found\n",
6196 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006197 ctxt->valid = 0;
6198 }
6199 } else {
6200 /*
6201 * Internal checking in case the entity quest barfed
6202 */
6203 if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
6204 (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00006205 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6206 "%%%s; is not a parameter entity\n",
6207 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006208 }
6209 }
6210 ctxt->hasPErefs = 1;
6211 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006212 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006213 }
6214 xmlFree(name);
6215 }
6216 }
6217 *str = ptr;
6218 return(entity);
6219}
6220
6221/**
6222 * xmlParseDocTypeDecl:
6223 * @ctxt: an XML parser context
6224 *
6225 * parse a DOCTYPE declaration
6226 *
6227 * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
6228 * ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
6229 *
6230 * [ VC: Root Element Type ]
6231 * The Name in the document type declaration must match the element
6232 * type of the root element.
6233 */
6234
6235void
6236xmlParseDocTypeDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006237 const xmlChar *name = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00006238 xmlChar *ExternalID = NULL;
6239 xmlChar *URI = NULL;
6240
6241 /*
6242 * We know that '<!DOCTYPE' has been detected.
6243 */
6244 SKIP(9);
6245
6246 SKIP_BLANKS;
6247
6248 /*
6249 * Parse the DOCTYPE name.
6250 */
6251 name = xmlParseName(ctxt);
6252 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006253 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6254 "xmlParseDocTypeDecl : no DOCTYPE name !\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006255 }
6256 ctxt->intSubName = name;
6257
6258 SKIP_BLANKS;
6259
6260 /*
6261 * Check for SystemID and ExternalID
6262 */
6263 URI = xmlParseExternalID(ctxt, &ExternalID, 1);
6264
6265 if ((URI != NULL) || (ExternalID != NULL)) {
6266 ctxt->hasExternalSubset = 1;
6267 }
6268 ctxt->extSubURI = URI;
6269 ctxt->extSubSystem = ExternalID;
6270
6271 SKIP_BLANKS;
6272
6273 /*
6274 * Create and update the internal subset.
6275 */
6276 if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
6277 (!ctxt->disableSAX))
6278 ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
6279
6280 /*
6281 * Is there any internal subset declarations ?
6282 * they are handled separately in xmlParseInternalSubset()
6283 */
6284 if (RAW == '[')
6285 return;
6286
6287 /*
6288 * We should be at the end of the DOCTYPE declaration.
6289 */
6290 if (RAW != '>') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006291 xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006292 }
6293 NEXT;
6294}
6295
6296/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006297 * xmlParseInternalSubset:
Owen Taylor3473f882001-02-23 17:55:21 +00006298 * @ctxt: an XML parser context
6299 *
6300 * parse the internal subset declaration
6301 *
6302 * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
6303 */
6304
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006305static void
Owen Taylor3473f882001-02-23 17:55:21 +00006306xmlParseInternalSubset(xmlParserCtxtPtr ctxt) {
6307 /*
6308 * Is there any DTD definition ?
6309 */
6310 if (RAW == '[') {
6311 ctxt->instate = XML_PARSER_DTD;
6312 NEXT;
6313 /*
6314 * Parse the succession of Markup declarations and
6315 * PEReferences.
6316 * Subsequence (markupdecl | PEReference | S)*
6317 */
6318 while (RAW != ']') {
6319 const xmlChar *check = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00006320 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00006321
6322 SKIP_BLANKS;
6323 xmlParseMarkupDecl(ctxt);
6324 xmlParsePEReference(ctxt);
6325
6326 /*
6327 * Pop-up of finished entities.
6328 */
6329 while ((RAW == 0) && (ctxt->inputNr > 1))
6330 xmlPopInput(ctxt);
6331
6332 if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006333 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006334 "xmlParseInternalSubset: error detected in Markup declaration\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006335 break;
6336 }
6337 }
6338 if (RAW == ']') {
6339 NEXT;
6340 SKIP_BLANKS;
6341 }
6342 }
6343
6344 /*
6345 * We should be at the end of the DOCTYPE declaration.
6346 */
6347 if (RAW != '>') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006348 xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006349 }
6350 NEXT;
6351}
6352
Daniel Veillard81273902003-09-30 00:43:48 +00006353#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00006354/**
6355 * xmlParseAttribute:
6356 * @ctxt: an XML parser context
6357 * @value: a xmlChar ** used to store the value of the attribute
6358 *
6359 * parse an attribute
6360 *
6361 * [41] Attribute ::= Name Eq AttValue
6362 *
6363 * [ WFC: No External Entity References ]
6364 * Attribute values cannot contain direct or indirect entity references
6365 * to external entities.
6366 *
6367 * [ WFC: No < in Attribute Values ]
6368 * The replacement text of any entity referred to directly or indirectly in
6369 * an attribute value (other than "&lt;") must not contain a <.
6370 *
6371 * [ VC: Attribute Value Type ]
6372 * The attribute must have been declared; the value must be of the type
6373 * declared for it.
6374 *
6375 * [25] Eq ::= S? '=' S?
6376 *
6377 * With namespace:
6378 *
6379 * [NS 11] Attribute ::= QName Eq AttValue
6380 *
6381 * Also the case QName == xmlns:??? is handled independently as a namespace
6382 * definition.
6383 *
6384 * Returns the attribute name, and the value in *value.
6385 */
6386
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006387const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006388xmlParseAttribute(xmlParserCtxtPtr ctxt, xmlChar **value) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006389 const xmlChar *name;
6390 xmlChar *val;
Owen Taylor3473f882001-02-23 17:55:21 +00006391
6392 *value = NULL;
Daniel Veillard878eab02002-02-19 13:46:09 +00006393 GROW;
Owen Taylor3473f882001-02-23 17:55:21 +00006394 name = xmlParseName(ctxt);
6395 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006396 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006397 "error parsing attribute name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006398 return(NULL);
6399 }
6400
6401 /*
6402 * read the value
6403 */
6404 SKIP_BLANKS;
6405 if (RAW == '=') {
6406 NEXT;
6407 SKIP_BLANKS;
6408 val = xmlParseAttValue(ctxt);
6409 ctxt->instate = XML_PARSER_CONTENT;
6410 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006411 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Owen Taylor3473f882001-02-23 17:55:21 +00006412 "Specification mandate value for attribute %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006413 return(NULL);
6414 }
6415
6416 /*
6417 * Check that xml:lang conforms to the specification
6418 * No more registered as an error, just generate a warning now
6419 * since this was deprecated in XML second edition
6420 */
6421 if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) {
6422 if (!xmlCheckLanguageID(val)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00006423 xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
6424 "Malformed value for xml:lang : %s\n",
6425 val, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006426 }
6427 }
6428
6429 /*
6430 * Check that xml:space conforms to the specification
6431 */
6432 if (xmlStrEqual(name, BAD_CAST "xml:space")) {
6433 if (xmlStrEqual(val, BAD_CAST "default"))
6434 *(ctxt->space) = 0;
6435 else if (xmlStrEqual(val, BAD_CAST "preserve"))
6436 *(ctxt->space) = 1;
6437 else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006438 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Daniel Veillard642104e2003-03-26 16:32:05 +00006439"Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
Owen Taylor3473f882001-02-23 17:55:21 +00006440 val);
Owen Taylor3473f882001-02-23 17:55:21 +00006441 }
6442 }
6443
6444 *value = val;
6445 return(name);
6446}
6447
6448/**
6449 * xmlParseStartTag:
6450 * @ctxt: an XML parser context
6451 *
6452 * parse a start of tag either for rule element or
6453 * EmptyElement. In both case we don't parse the tag closing chars.
6454 *
6455 * [40] STag ::= '<' Name (S Attribute)* S? '>'
6456 *
6457 * [ WFC: Unique Att Spec ]
6458 * No attribute name may appear more than once in the same start-tag or
6459 * empty-element tag.
6460 *
6461 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
6462 *
6463 * [ WFC: Unique Att Spec ]
6464 * No attribute name may appear more than once in the same start-tag or
6465 * empty-element tag.
6466 *
6467 * With namespace:
6468 *
6469 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
6470 *
6471 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
6472 *
6473 * Returns the element name parsed
6474 */
6475
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006476const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006477xmlParseStartTag(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006478 const xmlChar *name;
6479 const xmlChar *attname;
Owen Taylor3473f882001-02-23 17:55:21 +00006480 xmlChar *attvalue;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006481 const xmlChar **atts = ctxt->atts;
Owen Taylor3473f882001-02-23 17:55:21 +00006482 int nbatts = 0;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006483 int maxatts = ctxt->maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00006484 int i;
6485
6486 if (RAW != '<') return(NULL);
Daniel Veillard21a0f912001-02-25 19:54:14 +00006487 NEXT1;
Owen Taylor3473f882001-02-23 17:55:21 +00006488
6489 name = xmlParseName(ctxt);
6490 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006491 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00006492 "xmlParseStartTag: invalid element name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006493 return(NULL);
6494 }
6495
6496 /*
6497 * Now parse the attributes, it ends up with the ending
6498 *
6499 * (S Attribute)* S?
6500 */
6501 SKIP_BLANKS;
6502 GROW;
6503
Daniel Veillard21a0f912001-02-25 19:54:14 +00006504 while ((RAW != '>') &&
6505 ((RAW != '/') || (NXT(1) != '>')) &&
Daniel Veillard73b013f2003-09-30 12:36:01 +00006506 (IS_BYTE_CHAR(RAW))) {
Owen Taylor3473f882001-02-23 17:55:21 +00006507 const xmlChar *q = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00006508 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00006509
6510 attname = xmlParseAttribute(ctxt, &attvalue);
6511 if ((attname != NULL) && (attvalue != NULL)) {
6512 /*
6513 * [ WFC: Unique Att Spec ]
6514 * No attribute name may appear more than once in the same
6515 * start-tag or empty-element tag.
6516 */
6517 for (i = 0; i < nbatts;i += 2) {
6518 if (xmlStrEqual(atts[i], attname)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006519 xmlErrAttributeDup(ctxt, NULL, attname);
Owen Taylor3473f882001-02-23 17:55:21 +00006520 xmlFree(attvalue);
6521 goto failed;
6522 }
6523 }
Owen Taylor3473f882001-02-23 17:55:21 +00006524 /*
6525 * Add the pair to atts
6526 */
6527 if (atts == NULL) {
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006528 maxatts = 22; /* allow for 10 attrs by default */
6529 atts = (const xmlChar **)
6530 xmlMalloc(maxatts * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00006531 if (atts == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006532 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006533 if (attvalue != NULL)
6534 xmlFree(attvalue);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006535 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00006536 }
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006537 ctxt->atts = atts;
6538 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00006539 } else if (nbatts + 4 > maxatts) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006540 const xmlChar **n;
6541
Owen Taylor3473f882001-02-23 17:55:21 +00006542 maxatts *= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006543 n = (const xmlChar **) xmlRealloc((void *) atts,
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006544 maxatts * sizeof(const xmlChar *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006545 if (n == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006546 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006547 if (attvalue != NULL)
6548 xmlFree(attvalue);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006549 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00006550 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006551 atts = n;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006552 ctxt->atts = atts;
6553 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00006554 }
6555 atts[nbatts++] = attname;
6556 atts[nbatts++] = attvalue;
6557 atts[nbatts] = NULL;
6558 atts[nbatts + 1] = NULL;
6559 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00006560 if (attvalue != NULL)
6561 xmlFree(attvalue);
6562 }
6563
6564failed:
6565
Daniel Veillard3772de32002-12-17 10:31:45 +00006566 GROW
Owen Taylor3473f882001-02-23 17:55:21 +00006567 if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
6568 break;
William M. Brack76e95df2003-10-18 16:20:14 +00006569 if (!IS_BLANK_CH(RAW)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006570 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6571 "attributes construct error\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006572 }
6573 SKIP_BLANKS;
Daniel Veillard02111c12003-02-24 19:14:52 +00006574 if ((cons == ctxt->input->consumed) && (q == CUR_PTR) &&
6575 (attname == NULL) && (attvalue == NULL)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006576 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
6577 "xmlParseStartTag: problem parsing attributes\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006578 break;
6579 }
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006580 SHRINK;
Owen Taylor3473f882001-02-23 17:55:21 +00006581 GROW;
6582 }
6583
6584 /*
6585 * SAX: Start of Element !
6586 */
6587 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) &&
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006588 (!ctxt->disableSAX)) {
6589 if (nbatts > 0)
6590 ctxt->sax->startElement(ctxt->userData, name, atts);
6591 else
6592 ctxt->sax->startElement(ctxt->userData, name, NULL);
6593 }
Owen Taylor3473f882001-02-23 17:55:21 +00006594
6595 if (atts != NULL) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006596 /* Free only the content strings */
6597 for (i = 1;i < nbatts;i+=2)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006598 if (atts[i] != NULL)
6599 xmlFree((xmlChar *) atts[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00006600 }
6601 return(name);
6602}
6603
6604/**
Daniel Veillard0fb18932003-09-07 09:14:37 +00006605 * xmlParseEndTag1:
Owen Taylor3473f882001-02-23 17:55:21 +00006606 * @ctxt: an XML parser context
Daniel Veillard0fb18932003-09-07 09:14:37 +00006607 * @line: line of the start tag
6608 * @nsNr: number of namespaces on the start tag
Owen Taylor3473f882001-02-23 17:55:21 +00006609 *
6610 * parse an end of tag
6611 *
6612 * [42] ETag ::= '</' Name S? '>'
6613 *
6614 * With namespace
6615 *
6616 * [NS 9] ETag ::= '</' QName S? '>'
6617 */
6618
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00006619static void
Daniel Veillard0fb18932003-09-07 09:14:37 +00006620xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006621 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006622
6623 GROW;
6624 if ((RAW != '<') || (NXT(1) != '/')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006625 xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006626 "xmlParseEndTag: '</' not found\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006627 return;
6628 }
6629 SKIP(2);
6630
Daniel Veillard46de64e2002-05-29 08:21:33 +00006631 name = xmlParseNameAndCompare(ctxt,ctxt->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006632
6633 /*
6634 * We should definitely be at the ending "S? '>'" part
6635 */
6636 GROW;
6637 SKIP_BLANKS;
Daniel Veillard73b013f2003-09-30 12:36:01 +00006638 if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006639 xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006640 } else
Daniel Veillard21a0f912001-02-25 19:54:14 +00006641 NEXT1;
Owen Taylor3473f882001-02-23 17:55:21 +00006642
6643 /*
6644 * [ WFC: Element Type Match ]
6645 * The Name in an element's end-tag must match the element type in the
6646 * start-tag.
6647 *
6648 */
Daniel Veillard46de64e2002-05-29 08:21:33 +00006649 if (name != (xmlChar*)1) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006650 if (name == NULL) name = BAD_CAST "unparseable";
6651 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00006652 "Opening and ending tag mismatch: %s line %d and %s\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00006653 ctxt->name, line, name);
Owen Taylor3473f882001-02-23 17:55:21 +00006654 }
6655
6656 /*
6657 * SAX: End of Tag
6658 */
6659 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
6660 (!ctxt->disableSAX))
Daniel Veillard46de64e2002-05-29 08:21:33 +00006661 ctxt->sax->endElement(ctxt->userData, ctxt->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006662
Daniel Veillarde57ec792003-09-10 10:50:59 +00006663 namePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006664 spacePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006665 return;
6666}
6667
6668/**
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00006669 * xmlParseEndTag:
6670 * @ctxt: an XML parser context
6671 *
6672 * parse an end of tag
6673 *
6674 * [42] ETag ::= '</' Name S? '>'
6675 *
6676 * With namespace
6677 *
6678 * [NS 9] ETag ::= '</' QName S? '>'
6679 */
6680
6681void
6682xmlParseEndTag(xmlParserCtxtPtr ctxt) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00006683 xmlParseEndTag1(ctxt, 0);
6684}
Daniel Veillard81273902003-09-30 00:43:48 +00006685#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard0fb18932003-09-07 09:14:37 +00006686
6687/************************************************************************
6688 * *
6689 * SAX 2 specific operations *
6690 * *
6691 ************************************************************************/
6692
6693static const xmlChar *
6694xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) {
6695 int len = 0, l;
6696 int c;
6697 int count = 0;
6698
6699 /*
6700 * Handler for more complex cases
6701 */
6702 GROW;
6703 c = CUR_CHAR(l);
6704 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006705 (!IS_LETTER(c) && (c != '_'))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00006706 return(NULL);
6707 }
6708
6709 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
William M. Brack871611b2003-10-18 04:53:14 +00006710 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006711 (c == '.') || (c == '-') || (c == '_') ||
William M. Brack871611b2003-10-18 04:53:14 +00006712 (IS_COMBINING(c)) ||
6713 (IS_EXTENDER(c)))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00006714 if (count++ > 100) {
6715 count = 0;
6716 GROW;
6717 }
6718 len += l;
6719 NEXTL(l);
6720 c = CUR_CHAR(l);
6721 }
6722 return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len));
6723}
6724
6725/*
6726 * xmlGetNamespace:
6727 * @ctxt: an XML parser context
6728 * @prefix: the prefix to lookup
6729 *
6730 * Lookup the namespace name for the @prefix (which ca be NULL)
6731 * The prefix must come from the @ctxt->dict dictionnary
6732 *
6733 * Returns the namespace name or NULL if not bound
6734 */
6735static const xmlChar *
6736xmlGetNamespace(xmlParserCtxtPtr ctxt, const xmlChar *prefix) {
6737 int i;
6738
Daniel Veillarde57ec792003-09-10 10:50:59 +00006739 if (prefix == ctxt->str_xml) return(ctxt->str_xml_ns);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006740 for (i = ctxt->nsNr - 2;i >= 0;i-=2)
Daniel Veillarde57ec792003-09-10 10:50:59 +00006741 if (ctxt->nsTab[i] == prefix) {
6742 if ((prefix == NULL) && (*ctxt->nsTab[i + 1] == 0))
6743 return(NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006744 return(ctxt->nsTab[i + 1]);
Daniel Veillarde57ec792003-09-10 10:50:59 +00006745 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00006746 return(NULL);
6747}
6748
6749/**
6750 * xmlParseNCName:
6751 * @ctxt: an XML parser context
Daniel Veillardc82c57e2004-01-12 16:24:34 +00006752 * @len: lenght of the string parsed
Daniel Veillard0fb18932003-09-07 09:14:37 +00006753 *
6754 * parse an XML name.
6755 *
6756 * [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' |
6757 * CombiningChar | Extender
6758 *
6759 * [5NS] NCName ::= (Letter | '_') (NCNameChar)*
6760 *
6761 * Returns the Name parsed or NULL
6762 */
6763
6764static const xmlChar *
6765xmlParseNCName(xmlParserCtxtPtr ctxt) {
6766 const xmlChar *in;
6767 const xmlChar *ret;
6768 int count = 0;
6769
6770 /*
6771 * Accelerator for simple ASCII names
6772 */
6773 in = ctxt->input->cur;
6774 if (((*in >= 0x61) && (*in <= 0x7A)) ||
6775 ((*in >= 0x41) && (*in <= 0x5A)) ||
6776 (*in == '_')) {
6777 in++;
6778 while (((*in >= 0x61) && (*in <= 0x7A)) ||
6779 ((*in >= 0x41) && (*in <= 0x5A)) ||
6780 ((*in >= 0x30) && (*in <= 0x39)) ||
6781 (*in == '_') || (*in == '-') ||
6782 (*in == '.'))
6783 in++;
6784 if ((*in > 0) && (*in < 0x80)) {
6785 count = in - ctxt->input->cur;
6786 ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
6787 ctxt->input->cur = in;
6788 ctxt->nbChars += count;
6789 ctxt->input->col += count;
6790 if (ret == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006791 xmlErrMemory(ctxt, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006792 }
6793 return(ret);
6794 }
6795 }
6796 return(xmlParseNCNameComplex(ctxt));
6797}
6798
6799/**
6800 * xmlParseQName:
6801 * @ctxt: an XML parser context
6802 * @prefix: pointer to store the prefix part
6803 *
6804 * parse an XML Namespace QName
6805 *
6806 * [6] QName ::= (Prefix ':')? LocalPart
6807 * [7] Prefix ::= NCName
6808 * [8] LocalPart ::= NCName
6809 *
6810 * Returns the Name parsed or NULL
6811 */
6812
6813static const xmlChar *
6814xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) {
6815 const xmlChar *l, *p;
6816
6817 GROW;
6818
6819 l = xmlParseNCName(ctxt);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006820 if (l == NULL) {
6821 if (CUR == ':') {
6822 l = xmlParseName(ctxt);
6823 if (l != NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006824 xmlNsErr(ctxt, XML_NS_ERR_QNAME,
6825 "Failed to parse QName '%s'\n", l, NULL, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006826 *prefix = NULL;
6827 return(l);
6828 }
6829 }
6830 return(NULL);
6831 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00006832 if (CUR == ':') {
6833 NEXT;
6834 p = l;
6835 l = xmlParseNCName(ctxt);
6836 if (l == NULL) {
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006837 xmlChar *tmp;
6838
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006839 xmlNsErr(ctxt, XML_NS_ERR_QNAME,
6840 "Failed to parse QName '%s:'\n", p, NULL, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006841 tmp = xmlBuildQName(BAD_CAST "", p, NULL, 0);
6842 p = xmlDictLookup(ctxt->dict, tmp, -1);
6843 if (tmp != NULL) xmlFree(tmp);
6844 *prefix = NULL;
6845 return(p);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006846 }
6847 if (CUR == ':') {
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006848 xmlChar *tmp;
6849
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006850 xmlNsErr(ctxt, XML_NS_ERR_QNAME,
6851 "Failed to parse QName '%s:%s:'\n", p, l, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006852 NEXT;
6853 tmp = (xmlChar *) xmlParseName(ctxt);
6854 if (tmp != NULL) {
6855 tmp = xmlBuildQName(tmp, l, NULL, 0);
6856 l = xmlDictLookup(ctxt->dict, tmp, -1);
6857 if (tmp != NULL) xmlFree(tmp);
6858 *prefix = p;
6859 return(l);
6860 }
6861 tmp = xmlBuildQName(BAD_CAST "", l, NULL, 0);
6862 l = xmlDictLookup(ctxt->dict, tmp, -1);
6863 if (tmp != NULL) xmlFree(tmp);
6864 *prefix = p;
6865 return(l);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006866 }
6867 *prefix = p;
6868 } else
6869 *prefix = NULL;
6870 return(l);
6871}
6872
6873/**
6874 * xmlParseQNameAndCompare:
6875 * @ctxt: an XML parser context
6876 * @name: the localname
6877 * @prefix: the prefix, if any.
6878 *
6879 * parse an XML name and compares for match
6880 * (specialized for endtag parsing)
6881 *
6882 * Returns NULL for an illegal name, (xmlChar*) 1 for success
6883 * and the name for mismatch
6884 */
6885
6886static const xmlChar *
6887xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name,
6888 xmlChar const *prefix) {
6889 const xmlChar *cmp = name;
6890 const xmlChar *in;
6891 const xmlChar *ret;
6892 const xmlChar *prefix2;
6893
6894 if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name));
6895
6896 GROW;
6897 in = ctxt->input->cur;
6898
6899 cmp = prefix;
6900 while (*in != 0 && *in == *cmp) {
6901 ++in;
6902 ++cmp;
6903 }
6904 if ((*cmp == 0) && (*in == ':')) {
6905 in++;
6906 cmp = name;
6907 while (*in != 0 && *in == *cmp) {
6908 ++in;
6909 ++cmp;
6910 }
William M. Brack76e95df2003-10-18 16:20:14 +00006911 if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00006912 /* success */
6913 ctxt->input->cur = in;
6914 return((const xmlChar*) 1);
6915 }
6916 }
6917 /*
6918 * all strings coms from the dictionary, equality can be done directly
6919 */
6920 ret = xmlParseQName (ctxt, &prefix2);
6921 if ((ret == name) && (prefix == prefix2))
6922 return((const xmlChar*) 1);
6923 return ret;
6924}
6925
6926/**
6927 * xmlParseAttValueInternal:
6928 * @ctxt: an XML parser context
6929 * @len: attribute len result
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00006930 * @alloc: whether the attribute was reallocated as a new string
6931 * @normalize: if 1 then further non-CDATA normalization must be done
Daniel Veillard0fb18932003-09-07 09:14:37 +00006932 *
6933 * parse a value for an attribute.
6934 * NOTE: if no normalization is needed, the routine will return pointers
6935 * directly from the data buffer.
6936 *
6937 * 3.3.3 Attribute-Value Normalization:
6938 * Before the value of an attribute is passed to the application or
6939 * checked for validity, the XML processor must normalize it as follows:
6940 * - a character reference is processed by appending the referenced
6941 * character to the attribute value
6942 * - an entity reference is processed by recursively processing the
6943 * replacement text of the entity
6944 * - a whitespace character (#x20, #xD, #xA, #x9) is processed by
6945 * appending #x20 to the normalized value, except that only a single
6946 * #x20 is appended for a "#xD#xA" sequence that is part of an external
6947 * parsed entity or the literal entity value of an internal parsed entity
6948 * - other characters are processed by appending them to the normalized value
6949 * If the declared value is not CDATA, then the XML processor must further
6950 * process the normalized attribute value by discarding any leading and
6951 * trailing space (#x20) characters, and by replacing sequences of space
6952 * (#x20) characters by a single space (#x20) character.
6953 * All attributes for which no declaration has been read should be treated
6954 * by a non-validating parser as if declared CDATA.
6955 *
6956 * Returns the AttValue parsed or NULL. The value has to be freed by the
6957 * caller if it was copied, this can be detected by val[*len] == 0.
6958 */
6959
6960static xmlChar *
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00006961xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc,
6962 int normalize)
Daniel Veillarde57ec792003-09-10 10:50:59 +00006963{
Daniel Veillard0fb18932003-09-07 09:14:37 +00006964 xmlChar limit = 0;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00006965 const xmlChar *in = NULL, *start, *end, *last;
Daniel Veillard0fb18932003-09-07 09:14:37 +00006966 xmlChar *ret = NULL;
6967
6968 GROW;
6969 in = (xmlChar *) CUR_PTR;
6970 if (*in != '"' && *in != '\'') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006971 xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00006972 return (NULL);
6973 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00006974 ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
Daniel Veillarde57ec792003-09-10 10:50:59 +00006975
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00006976 /*
6977 * try to handle in this routine the most common case where no
6978 * allocation of a new string is required and where content is
6979 * pure ASCII.
6980 */
6981 limit = *in++;
6982 end = ctxt->input->end;
6983 start = in;
6984 if (in >= end) {
6985 const xmlChar *oldbase = ctxt->input->base;
6986 GROW;
6987 if (oldbase != ctxt->input->base) {
6988 long delta = ctxt->input->base - oldbase;
6989 start = start + delta;
6990 in = in + delta;
6991 }
6992 end = ctxt->input->end;
Daniel Veillard0fb18932003-09-07 09:14:37 +00006993 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00006994 if (normalize) {
6995 /*
6996 * Skip any leading spaces
6997 */
6998 while ((in < end) && (*in != limit) &&
6999 ((*in == 0x20) || (*in == 0x9) ||
7000 (*in == 0xA) || (*in == 0xD))) {
7001 in++;
7002 start = in;
7003 if (in >= end) {
7004 const xmlChar *oldbase = ctxt->input->base;
7005 GROW;
7006 if (oldbase != ctxt->input->base) {
7007 long delta = ctxt->input->base - oldbase;
7008 start = start + delta;
7009 in = in + delta;
7010 }
7011 end = ctxt->input->end;
7012 }
7013 }
7014 while ((in < end) && (*in != limit) && (*in >= 0x20) &&
7015 (*in <= 0x7f) && (*in != '&') && (*in != '<')) {
7016 if ((*in++ == 0x20) && (*in == 0x20)) break;
7017 if (in >= end) {
7018 const xmlChar *oldbase = ctxt->input->base;
7019 GROW;
7020 if (oldbase != ctxt->input->base) {
7021 long delta = ctxt->input->base - oldbase;
7022 start = start + delta;
7023 in = in + delta;
7024 }
7025 end = ctxt->input->end;
7026 }
7027 }
7028 last = in;
7029 /*
7030 * skip the trailing blanks
7031 */
Daniel Veillardc6e20e42003-09-11 16:30:26 +00007032 while ((last[-1] == 0x20) && (last > start)) last--;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007033 while ((in < end) && (*in != limit) &&
7034 ((*in == 0x20) || (*in == 0x9) ||
7035 (*in == 0xA) || (*in == 0xD))) {
7036 in++;
7037 if (in >= end) {
7038 const xmlChar *oldbase = ctxt->input->base;
7039 GROW;
7040 if (oldbase != ctxt->input->base) {
7041 long delta = ctxt->input->base - oldbase;
7042 start = start + delta;
7043 in = in + delta;
7044 last = last + delta;
7045 }
7046 end = ctxt->input->end;
7047 }
7048 }
7049 if (*in != limit) goto need_complex;
7050 } else {
7051 while ((in < end) && (*in != limit) && (*in >= 0x20) &&
7052 (*in <= 0x7f) && (*in != '&') && (*in != '<')) {
7053 in++;
7054 if (in >= end) {
7055 const xmlChar *oldbase = ctxt->input->base;
7056 GROW;
7057 if (oldbase != ctxt->input->base) {
7058 long delta = ctxt->input->base - oldbase;
7059 start = start + delta;
7060 in = in + delta;
7061 }
7062 end = ctxt->input->end;
7063 }
7064 }
7065 last = in;
7066 if (*in != limit) goto need_complex;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007067 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007068 in++;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007069 if (len != NULL) {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007070 *len = last - start;
7071 ret = (xmlChar *) start;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007072 } else {
Daniel Veillarde57ec792003-09-10 10:50:59 +00007073 if (alloc) *alloc = 1;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007074 ret = xmlStrndup(start, last - start);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007075 }
7076 CUR_PTR = in;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007077 if (alloc) *alloc = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007078 return ret;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007079need_complex:
7080 if (alloc) *alloc = 1;
7081 return xmlParseAttValueComplex(ctxt, len, normalize);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007082}
7083
7084/**
7085 * xmlParseAttribute2:
7086 * @ctxt: an XML parser context
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007087 * @pref: the element prefix
7088 * @elem: the element name
7089 * @prefix: a xmlChar ** used to store the value of the attribute prefix
Daniel Veillard0fb18932003-09-07 09:14:37 +00007090 * @value: a xmlChar ** used to store the value of the attribute
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007091 * @len: an int * to save the length of the attribute
7092 * @alloc: an int * to indicate if the attribute was allocated
Daniel Veillard0fb18932003-09-07 09:14:37 +00007093 *
7094 * parse an attribute in the new SAX2 framework.
7095 *
7096 * Returns the attribute name, and the value in *value, .
7097 */
7098
7099static const xmlChar *
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007100xmlParseAttribute2(xmlParserCtxtPtr ctxt,
7101 const xmlChar *pref, const xmlChar *elem,
7102 const xmlChar **prefix, xmlChar **value,
7103 int *len, int *alloc) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007104 const xmlChar *name;
7105 xmlChar *val;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007106 int normalize = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007107
7108 *value = NULL;
7109 GROW;
7110 name = xmlParseQName(ctxt, prefix);
7111 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007112 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7113 "error parsing attribute name\n");
Daniel Veillard0fb18932003-09-07 09:14:37 +00007114 return(NULL);
7115 }
7116
7117 /*
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007118 * get the type if needed
7119 */
7120 if (ctxt->attsSpecial != NULL) {
7121 int type;
7122
7123 type = (int) (long) xmlHashQLookup2(ctxt->attsSpecial,
7124 pref, elem, *prefix, name);
7125 if (type != 0) normalize = 1;
7126 }
7127
7128 /*
Daniel Veillard0fb18932003-09-07 09:14:37 +00007129 * read the value
7130 */
7131 SKIP_BLANKS;
7132 if (RAW == '=') {
7133 NEXT;
7134 SKIP_BLANKS;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007135 val = xmlParseAttValueInternal(ctxt, len, alloc, normalize);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007136 ctxt->instate = XML_PARSER_CONTENT;
7137 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00007138 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007139 "Specification mandate value for attribute %s\n", name);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007140 return(NULL);
7141 }
7142
7143 /*
7144 * Check that xml:lang conforms to the specification
7145 * No more registered as an error, just generate a warning now
7146 * since this was deprecated in XML second edition
7147 */
7148 if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) {
7149 if (!xmlCheckLanguageID(val)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007150 xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
7151 "Malformed value for xml:lang : %s\n",
7152 val, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007153 }
7154 }
7155
7156 /*
7157 * Check that xml:space conforms to the specification
7158 */
7159 if (xmlStrEqual(name, BAD_CAST "xml:space")) {
7160 if (xmlStrEqual(val, BAD_CAST "default"))
7161 *(ctxt->space) = 0;
7162 else if (xmlStrEqual(val, BAD_CAST "preserve"))
7163 *(ctxt->space) = 1;
7164 else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00007165 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007166"Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
7167 val);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007168 }
7169 }
7170
7171 *value = val;
7172 return(name);
7173}
7174
7175/**
7176 * xmlParseStartTag2:
7177 * @ctxt: an XML parser context
7178 *
7179 * parse a start of tag either for rule element or
7180 * EmptyElement. In both case we don't parse the tag closing chars.
7181 * This routine is called when running SAX2 parsing
7182 *
7183 * [40] STag ::= '<' Name (S Attribute)* S? '>'
7184 *
7185 * [ WFC: Unique Att Spec ]
7186 * No attribute name may appear more than once in the same start-tag or
7187 * empty-element tag.
7188 *
7189 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
7190 *
7191 * [ WFC: Unique Att Spec ]
7192 * No attribute name may appear more than once in the same start-tag or
7193 * empty-element tag.
7194 *
7195 * With namespace:
7196 *
7197 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
7198 *
7199 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
7200 *
7201 * Returns the element name parsed
7202 */
7203
7204static const xmlChar *
7205xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref,
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007206 const xmlChar **URI, int *tlen) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007207 const xmlChar *localname;
7208 const xmlChar *prefix;
7209 const xmlChar *attname;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007210 const xmlChar *aprefix;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007211 const xmlChar *nsname;
7212 xmlChar *attvalue;
7213 const xmlChar **atts = ctxt->atts;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007214 int maxatts = ctxt->maxatts;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007215 int nratts, nbatts, nbdef;
7216 int i, j, nbNs, attval;
7217 const xmlChar *base;
7218 unsigned long cur;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007219
7220 if (RAW != '<') return(NULL);
7221 NEXT1;
7222
7223 /*
7224 * NOTE: it is crucial with the SAX2 API to never call SHRINK beyond that
7225 * point since the attribute values may be stored as pointers to
7226 * the buffer and calling SHRINK would destroy them !
7227 * The Shrinking is only possible once the full set of attribute
7228 * callbacks have been done.
7229 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00007230reparse:
Daniel Veillard0fb18932003-09-07 09:14:37 +00007231 SHRINK;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007232 base = ctxt->input->base;
7233 cur = ctxt->input->cur - ctxt->input->base;
7234 nbatts = 0;
7235 nratts = 0;
7236 nbdef = 0;
7237 nbNs = 0;
7238 attval = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007239
7240 localname = xmlParseQName(ctxt, &prefix);
7241 if (localname == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007242 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7243 "StartTag: invalid element name\n");
Daniel Veillard0fb18932003-09-07 09:14:37 +00007244 return(NULL);
7245 }
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007246 *tlen = ctxt->input->cur - ctxt->input->base - cur;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007247
7248 /*
7249 * Now parse the attributes, it ends up with the ending
7250 *
7251 * (S Attribute)* S?
7252 */
7253 SKIP_BLANKS;
7254 GROW;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007255 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007256
7257 while ((RAW != '>') &&
7258 ((RAW != '/') || (NXT(1) != '>')) &&
Daniel Veillard73b013f2003-09-30 12:36:01 +00007259 (IS_BYTE_CHAR(RAW))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007260 const xmlChar *q = CUR_PTR;
7261 unsigned int cons = ctxt->input->consumed;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007262 int len = -1, alloc = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007263
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007264 attname = xmlParseAttribute2(ctxt, prefix, localname,
7265 &aprefix, &attvalue, &len, &alloc);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007266 if ((attname != NULL) && (attvalue != NULL)) {
7267 if (len < 0) len = xmlStrlen(attvalue);
7268 if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00007269 const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len);
7270 xmlURIPtr uri;
7271
7272 if (*URL != 0) {
7273 uri = xmlParseURI((const char *) URL);
7274 if (uri == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007275 xmlWarningMsg(ctxt, XML_WAR_NS_URI,
7276 "xmlns: %s not a valid URI\n",
7277 URL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007278 } else {
7279 if (uri->scheme == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007280 xmlWarningMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
7281 "xmlns: URI %s is not absolute\n",
7282 URL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007283 }
7284 xmlFreeURI(uri);
7285 }
7286 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007287 /*
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007288 * check that it's not a defined namespace
Daniel Veillard0fb18932003-09-07 09:14:37 +00007289 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007290 for (j = 1;j <= nbNs;j++)
7291 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL)
7292 break;
7293 if (j <= nbNs)
7294 xmlErrAttributeDup(ctxt, NULL, attname);
7295 else
7296 if (nsPush(ctxt, NULL, URL) > 0) nbNs++;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007297 if (alloc != 0) xmlFree(attvalue);
7298 SKIP_BLANKS;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007299 continue;
7300 }
7301 if (aprefix == ctxt->str_xmlns) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00007302 const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len);
7303 xmlURIPtr uri;
7304
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007305 if (attname == ctxt->str_xml) {
7306 if (URL != ctxt->str_xml_ns) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007307 xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
7308 "xml namespace prefix mapped to wrong URI\n",
7309 NULL, NULL, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007310 }
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007311 /*
7312 * Do not keep a namespace definition node
7313 */
7314 if (alloc != 0) xmlFree(attvalue);
7315 SKIP_BLANKS;
7316 continue;
7317 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00007318 uri = xmlParseURI((const char *) URL);
7319 if (uri == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007320 xmlWarningMsg(ctxt, XML_WAR_NS_URI,
7321 "xmlns:%s: '%s' is not a valid URI\n",
7322 attname, URL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007323 } else {
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007324 if ((ctxt->pedantic) && (uri->scheme == NULL)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007325 xmlWarningMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
7326 "xmlns:%s: URI %s is not absolute\n",
7327 attname, URL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007328 }
7329 xmlFreeURI(uri);
7330 }
7331
Daniel Veillard0fb18932003-09-07 09:14:37 +00007332 /*
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007333 * check that it's not a defined namespace
Daniel Veillard0fb18932003-09-07 09:14:37 +00007334 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007335 for (j = 1;j <= nbNs;j++)
7336 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname)
7337 break;
7338 if (j <= nbNs)
7339 xmlErrAttributeDup(ctxt, aprefix, attname);
7340 else
7341 if (nsPush(ctxt, attname, URL) > 0) nbNs++;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007342 if (alloc != 0) xmlFree(attvalue);
7343 SKIP_BLANKS;
Daniel Veillardd3999c72004-03-10 16:27:03 +00007344 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007345 continue;
7346 }
7347
7348 /*
7349 * Add the pair to atts
7350 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00007351 if ((atts == NULL) || (nbatts + 5 > maxatts)) {
7352 if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007353 if (attvalue[len] == 0)
7354 xmlFree(attvalue);
7355 goto failed;
7356 }
7357 maxatts = ctxt->maxatts;
7358 atts = ctxt->atts;
7359 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00007360 ctxt->attallocs[nratts++] = alloc;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007361 atts[nbatts++] = attname;
7362 atts[nbatts++] = aprefix;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007363 atts[nbatts++] = NULL; /* the URI will be fetched later */
Daniel Veillard0fb18932003-09-07 09:14:37 +00007364 atts[nbatts++] = attvalue;
7365 attvalue += len;
7366 atts[nbatts++] = attvalue;
7367 /*
7368 * tag if some deallocation is needed
7369 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00007370 if (alloc != 0) attval = 1;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007371 } else {
7372 if ((attvalue != NULL) && (attvalue[len] == 0))
7373 xmlFree(attvalue);
7374 }
7375
7376failed:
7377
7378 GROW
Daniel Veillarde57ec792003-09-10 10:50:59 +00007379 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007380 if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
7381 break;
William M. Brack76e95df2003-10-18 16:20:14 +00007382 if (!IS_BLANK_CH(RAW)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007383 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
7384 "attributes construct error\n");
Daniel Veillard0fb18932003-09-07 09:14:37 +00007385 }
7386 SKIP_BLANKS;
7387 if ((cons == ctxt->input->consumed) && (q == CUR_PTR) &&
7388 (attname == NULL) && (attvalue == NULL)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007389 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007390 "xmlParseStartTag: problem parsing attributes\n");
Daniel Veillard0fb18932003-09-07 09:14:37 +00007391 break;
7392 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007393 GROW;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007394 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007395 }
7396
Daniel Veillard0fb18932003-09-07 09:14:37 +00007397 /*
Daniel Veillarde57ec792003-09-10 10:50:59 +00007398 * The attributes defaulting
7399 */
7400 if (ctxt->attsDefault != NULL) {
7401 xmlDefAttrsPtr defaults;
7402
7403 defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix);
7404 if (defaults != NULL) {
7405 for (i = 0;i < defaults->nbAttrs;i++) {
7406 attname = defaults->values[4 * i];
7407 aprefix = defaults->values[4 * i + 1];
7408
7409 /*
7410 * special work for namespaces defaulted defs
7411 */
7412 if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
7413 /*
7414 * check that it's not a defined namespace
7415 */
7416 for (j = 1;j <= nbNs;j++)
7417 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL)
7418 break;
7419 if (j <= nbNs) continue;
7420
7421 nsname = xmlGetNamespace(ctxt, NULL);
7422 if (nsname != defaults->values[4 * i + 2]) {
7423 if (nsPush(ctxt, NULL,
7424 defaults->values[4 * i + 2]) > 0)
7425 nbNs++;
7426 }
7427 } else if (aprefix == ctxt->str_xmlns) {
7428 /*
7429 * check that it's not a defined namespace
7430 */
7431 for (j = 1;j <= nbNs;j++)
7432 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname)
7433 break;
7434 if (j <= nbNs) continue;
7435
7436 nsname = xmlGetNamespace(ctxt, attname);
7437 if (nsname != defaults->values[2]) {
7438 if (nsPush(ctxt, attname,
7439 defaults->values[4 * i + 2]) > 0)
7440 nbNs++;
7441 }
7442 } else {
7443 /*
7444 * check that it's not a defined attribute
7445 */
7446 for (j = 0;j < nbatts;j+=5) {
7447 if ((attname == atts[j]) && (aprefix == atts[j+1]))
7448 break;
7449 }
7450 if (j < nbatts) continue;
7451
7452 if ((atts == NULL) || (nbatts + 5 > maxatts)) {
7453 if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00007454 return(NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007455 }
7456 maxatts = ctxt->maxatts;
7457 atts = ctxt->atts;
7458 }
7459 atts[nbatts++] = attname;
7460 atts[nbatts++] = aprefix;
7461 if (aprefix == NULL)
7462 atts[nbatts++] = NULL;
7463 else
7464 atts[nbatts++] = xmlGetNamespace(ctxt, aprefix);
7465 atts[nbatts++] = defaults->values[4 * i + 2];
7466 atts[nbatts++] = defaults->values[4 * i + 3];
7467 nbdef++;
7468 }
7469 }
7470 }
7471 }
7472
Daniel Veillarde70c8772003-11-25 07:21:18 +00007473 /*
7474 * The attributes checkings
7475 */
7476 for (i = 0; i < nbatts;i += 5) {
7477 nsname = xmlGetNamespace(ctxt, atts[i + 1]);
7478 if ((atts[i + 1] != NULL) && (nsname == NULL)) {
7479 xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
7480 "Namespace prefix %s for %s on %s is not defined\n",
7481 atts[i + 1], atts[i], localname);
7482 }
7483 atts[i + 2] = nsname;
7484 /*
7485 * [ WFC: Unique Att Spec ]
7486 * No attribute name may appear more than once in the same
7487 * start-tag or empty-element tag.
7488 * As extended by the Namespace in XML REC.
7489 */
7490 for (j = 0; j < i;j += 5) {
7491 if (atts[i] == atts[j]) {
7492 if (atts[i+1] == atts[j+1]) {
7493 xmlErrAttributeDup(ctxt, atts[i+1], atts[i]);
7494 break;
7495 }
7496 if ((nsname != NULL) && (atts[j + 2] == nsname)) {
7497 xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED,
7498 "Namespaced Attribute %s in '%s' redefined\n",
7499 atts[i], nsname, NULL);
7500 break;
7501 }
7502 }
7503 }
7504 }
7505
Daniel Veillarde57ec792003-09-10 10:50:59 +00007506 nsname = xmlGetNamespace(ctxt, prefix);
7507 if ((prefix != NULL) && (nsname == NULL)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007508 xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
7509 "Namespace prefix %s on %s is not defined\n",
7510 prefix, localname, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007511 }
7512 *pref = prefix;
7513 *URI = nsname;
7514
7515 /*
7516 * SAX: Start of Element !
7517 */
7518 if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) &&
7519 (!ctxt->disableSAX)) {
7520 if (nbNs > 0)
7521 ctxt->sax->startElementNs(ctxt->userData, localname, prefix,
7522 nsname, nbNs, &ctxt->nsTab[ctxt->nsNr - 2 * nbNs],
7523 nbatts / 5, nbdef, atts);
7524 else
7525 ctxt->sax->startElementNs(ctxt->userData, localname, prefix,
7526 nsname, 0, NULL, nbatts / 5, nbdef, atts);
7527 }
7528
7529 /*
7530 * Free up attribute allocated strings if needed
7531 */
7532 if (attval != 0) {
7533 for (i = 3,j = 0; j < nratts;i += 5,j++)
7534 if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL))
7535 xmlFree((xmlChar *) atts[i]);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007536 }
7537
7538 return(localname);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007539
7540base_changed:
7541 /*
7542 * the attribute strings are valid iif the base didn't changed
7543 */
7544 if (attval != 0) {
7545 for (i = 3,j = 0; j < nratts;i += 5,j++)
7546 if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL))
7547 xmlFree((xmlChar *) atts[i]);
7548 }
7549 ctxt->input->cur = ctxt->input->base + cur;
7550 if (ctxt->wellFormed == 1) {
7551 goto reparse;
7552 }
7553 return(NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007554}
7555
7556/**
7557 * xmlParseEndTag2:
7558 * @ctxt: an XML parser context
7559 * @line: line of the start tag
7560 * @nsNr: number of namespaces on the start tag
7561 *
7562 * parse an end of tag
7563 *
7564 * [42] ETag ::= '</' Name S? '>'
7565 *
7566 * With namespace
7567 *
7568 * [NS 9] ETag ::= '</' QName S? '>'
7569 */
7570
7571static void
7572xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlChar *prefix,
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007573 const xmlChar *URI, int line, int nsNr, int tlen) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007574 const xmlChar *name;
7575
7576 GROW;
7577 if ((RAW != '<') || (NXT(1) != '/')) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007578 xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007579 return;
7580 }
7581 SKIP(2);
7582
Daniel Veillard453e71b2004-04-20 17:44:46 +00007583 if ((tlen > 0) && (strncmp(ctxt->input->cur, ctxt->name, tlen) == 0)) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007584 if (ctxt->input->cur[tlen] == '>') {
7585 ctxt->input->cur += tlen + 1;
7586 goto done;
7587 }
7588 ctxt->input->cur += tlen;
7589 name = (xmlChar*)1;
7590 } else {
7591 if (prefix == NULL)
7592 name = xmlParseNameAndCompare(ctxt, ctxt->name);
7593 else
7594 name = xmlParseQNameAndCompare(ctxt, ctxt->name, prefix);
7595 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007596
7597 /*
7598 * We should definitely be at the ending "S? '>'" part
7599 */
7600 GROW;
7601 SKIP_BLANKS;
Daniel Veillard73b013f2003-09-30 12:36:01 +00007602 if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007603 xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007604 } else
7605 NEXT1;
7606
7607 /*
7608 * [ WFC: Element Type Match ]
7609 * The Name in an element's end-tag must match the element type in the
7610 * start-tag.
7611 *
7612 */
7613 if (name != (xmlChar*)1) {
Daniel Veillardf403d292003-10-05 13:51:35 +00007614 if (name == NULL) name = BAD_CAST "unparseable";
7615 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007616 "Opening and ending tag mismatch: %s line %d and %s\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00007617 ctxt->name, line, name);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007618 }
7619
7620 /*
7621 * SAX: End of Tag
7622 */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007623done:
Daniel Veillard0fb18932003-09-07 09:14:37 +00007624 if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
7625 (!ctxt->disableSAX))
7626 ctxt->sax->endElementNs(ctxt->userData, ctxt->name, prefix, URI);
7627
Daniel Veillard0fb18932003-09-07 09:14:37 +00007628 spacePop(ctxt);
7629 if (nsNr != 0)
7630 nsPop(ctxt, nsNr);
7631 return;
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00007632}
7633
7634/**
Owen Taylor3473f882001-02-23 17:55:21 +00007635 * xmlParseCDSect:
7636 * @ctxt: an XML parser context
7637 *
7638 * Parse escaped pure raw content.
7639 *
7640 * [18] CDSect ::= CDStart CData CDEnd
7641 *
7642 * [19] CDStart ::= '<![CDATA['
7643 *
7644 * [20] Data ::= (Char* - (Char* ']]>' Char*))
7645 *
7646 * [21] CDEnd ::= ']]>'
7647 */
7648void
7649xmlParseCDSect(xmlParserCtxtPtr ctxt) {
7650 xmlChar *buf = NULL;
7651 int len = 0;
7652 int size = XML_PARSER_BUFFER_SIZE;
7653 int r, rl;
7654 int s, sl;
7655 int cur, l;
7656 int count = 0;
7657
Daniel Veillard8f597c32003-10-06 08:19:27 +00007658 /* Check 2.6.0 was NXT(0) not RAW */
Daniel Veillarda07050d2003-10-19 14:46:32 +00007659 if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007660 SKIP(9);
7661 } else
7662 return;
7663
7664 ctxt->instate = XML_PARSER_CDATA_SECTION;
7665 r = CUR_CHAR(rl);
William M. Brack871611b2003-10-18 04:53:14 +00007666 if (!IS_CHAR(r)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007667 xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007668 ctxt->instate = XML_PARSER_CONTENT;
7669 return;
7670 }
7671 NEXTL(rl);
7672 s = CUR_CHAR(sl);
William M. Brack871611b2003-10-18 04:53:14 +00007673 if (!IS_CHAR(s)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007674 xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007675 ctxt->instate = XML_PARSER_CONTENT;
7676 return;
7677 }
7678 NEXTL(sl);
7679 cur = CUR_CHAR(l);
Daniel Veillard3c908dc2003-04-19 00:07:51 +00007680 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00007681 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007682 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007683 return;
7684 }
William M. Brack871611b2003-10-18 04:53:14 +00007685 while (IS_CHAR(cur) &&
Owen Taylor3473f882001-02-23 17:55:21 +00007686 ((r != ']') || (s != ']') || (cur != '>'))) {
7687 if (len + 5 >= size) {
7688 size *= 2;
7689 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
7690 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007691 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007692 return;
7693 }
7694 }
7695 COPY_BUF(rl,buf,len,r);
7696 r = s;
7697 rl = sl;
7698 s = cur;
7699 sl = l;
7700 count++;
7701 if (count > 50) {
7702 GROW;
7703 count = 0;
7704 }
7705 NEXTL(l);
7706 cur = CUR_CHAR(l);
7707 }
7708 buf[len] = 0;
7709 ctxt->instate = XML_PARSER_CONTENT;
7710 if (cur != '>') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00007711 xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED,
Owen Taylor3473f882001-02-23 17:55:21 +00007712 "CData section not finished\n%.50s\n", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00007713 xmlFree(buf);
7714 return;
7715 }
7716 NEXTL(l);
7717
7718 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00007719 * OK the buffer is to be consumed as cdata.
Owen Taylor3473f882001-02-23 17:55:21 +00007720 */
7721 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
7722 if (ctxt->sax->cdataBlock != NULL)
7723 ctxt->sax->cdataBlock(ctxt->userData, buf, len);
Daniel Veillard7583a592001-07-08 13:15:55 +00007724 else if (ctxt->sax->characters != NULL)
7725 ctxt->sax->characters(ctxt->userData, buf, len);
Owen Taylor3473f882001-02-23 17:55:21 +00007726 }
7727 xmlFree(buf);
7728}
7729
7730/**
7731 * xmlParseContent:
7732 * @ctxt: an XML parser context
7733 *
7734 * Parse a content:
7735 *
7736 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
7737 */
7738
7739void
7740xmlParseContent(xmlParserCtxtPtr ctxt) {
7741 GROW;
Daniel Veillardfdc91562002-07-01 21:52:03 +00007742 while ((RAW != 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +00007743 ((RAW != '<') || (NXT(1) != '/'))) {
7744 const xmlChar *test = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00007745 unsigned int cons = ctxt->input->consumed;
Daniel Veillard21a0f912001-02-25 19:54:14 +00007746 const xmlChar *cur = ctxt->input->cur;
Owen Taylor3473f882001-02-23 17:55:21 +00007747
7748 /*
Owen Taylor3473f882001-02-23 17:55:21 +00007749 * First case : a Processing Instruction.
7750 */
Daniel Veillardfdc91562002-07-01 21:52:03 +00007751 if ((*cur == '<') && (cur[1] == '?')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007752 xmlParsePI(ctxt);
7753 }
7754
7755 /*
7756 * Second case : a CDSection
7757 */
Daniel Veillard8f597c32003-10-06 08:19:27 +00007758 /* 2.6.0 test was *cur not RAW */
Daniel Veillarda07050d2003-10-19 14:46:32 +00007759 else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007760 xmlParseCDSect(ctxt);
7761 }
7762
7763 /*
7764 * Third case : a comment
7765 */
Daniel Veillard21a0f912001-02-25 19:54:14 +00007766 else if ((*cur == '<') && (NXT(1) == '!') &&
Owen Taylor3473f882001-02-23 17:55:21 +00007767 (NXT(2) == '-') && (NXT(3) == '-')) {
7768 xmlParseComment(ctxt);
7769 ctxt->instate = XML_PARSER_CONTENT;
7770 }
7771
7772 /*
7773 * Fourth case : a sub-element.
7774 */
Daniel Veillard21a0f912001-02-25 19:54:14 +00007775 else if (*cur == '<') {
Owen Taylor3473f882001-02-23 17:55:21 +00007776 xmlParseElement(ctxt);
7777 }
7778
7779 /*
7780 * Fifth case : a reference. If if has not been resolved,
7781 * parsing returns it's Name, create the node
7782 */
7783
Daniel Veillard21a0f912001-02-25 19:54:14 +00007784 else if (*cur == '&') {
Owen Taylor3473f882001-02-23 17:55:21 +00007785 xmlParseReference(ctxt);
7786 }
7787
7788 /*
7789 * Last case, text. Note that References are handled directly.
7790 */
7791 else {
7792 xmlParseCharData(ctxt, 0);
7793 }
7794
7795 GROW;
7796 /*
7797 * Pop-up of finished entities.
7798 */
Daniel Veillard561b7f82002-03-20 21:55:57 +00007799 while ((RAW == 0) && (ctxt->inputNr > 1))
Owen Taylor3473f882001-02-23 17:55:21 +00007800 xmlPopInput(ctxt);
7801 SHRINK;
7802
Daniel Veillardfdc91562002-07-01 21:52:03 +00007803 if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007804 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
7805 "detected an error in element content\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007806 ctxt->instate = XML_PARSER_EOF;
7807 break;
7808 }
7809 }
7810}
7811
7812/**
7813 * xmlParseElement:
7814 * @ctxt: an XML parser context
7815 *
7816 * parse an XML element, this is highly recursive
7817 *
7818 * [39] element ::= EmptyElemTag | STag content ETag
7819 *
7820 * [ WFC: Element Type Match ]
7821 * The Name in an element's end-tag must match the element type in the
7822 * start-tag.
7823 *
Owen Taylor3473f882001-02-23 17:55:21 +00007824 */
7825
7826void
7827xmlParseElement(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00007828 const xmlChar *name;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007829 const xmlChar *prefix;
7830 const xmlChar *URI;
Owen Taylor3473f882001-02-23 17:55:21 +00007831 xmlParserNodeInfo node_info;
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007832 int line, tlen;
Owen Taylor3473f882001-02-23 17:55:21 +00007833 xmlNodePtr ret;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007834 int nsNr = ctxt->nsNr;
Owen Taylor3473f882001-02-23 17:55:21 +00007835
7836 /* Capture start position */
7837 if (ctxt->record_info) {
7838 node_info.begin_pos = ctxt->input->consumed +
7839 (CUR_PTR - ctxt->input->base);
7840 node_info.begin_line = ctxt->input->line;
7841 }
7842
7843 if (ctxt->spaceNr == 0)
7844 spacePush(ctxt, -1);
7845 else
7846 spacePush(ctxt, *ctxt->space);
7847
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00007848 line = ctxt->input->line;
Daniel Veillard81273902003-09-30 00:43:48 +00007849#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard0fb18932003-09-07 09:14:37 +00007850 if (ctxt->sax2)
Daniel Veillard81273902003-09-30 00:43:48 +00007851#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007852 name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen);
Daniel Veillard81273902003-09-30 00:43:48 +00007853#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard0fb18932003-09-07 09:14:37 +00007854 else
7855 name = xmlParseStartTag(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00007856#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00007857 if (name == NULL) {
7858 spacePop(ctxt);
7859 return;
7860 }
7861 namePush(ctxt, name);
7862 ret = ctxt->node;
7863
Daniel Veillard4432df22003-09-28 18:58:27 +00007864#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00007865 /*
7866 * [ VC: Root Element Type ]
7867 * The Name in the document type declaration must match the element
7868 * type of the root element.
7869 */
7870 if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
7871 ctxt->node && (ctxt->node == ctxt->myDoc->children))
7872 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillard4432df22003-09-28 18:58:27 +00007873#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00007874
7875 /*
7876 * Check for an Empty Element.
7877 */
7878 if ((RAW == '/') && (NXT(1) == '>')) {
7879 SKIP(2);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007880 if (ctxt->sax2) {
7881 if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
7882 (!ctxt->disableSAX))
7883 ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI);
Daniel Veillard81273902003-09-30 00:43:48 +00007884#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard0fb18932003-09-07 09:14:37 +00007885 } else {
7886 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
7887 (!ctxt->disableSAX))
7888 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard81273902003-09-30 00:43:48 +00007889#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00007890 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007891 namePop(ctxt);
7892 spacePop(ctxt);
7893 if (nsNr != ctxt->nsNr)
7894 nsPop(ctxt, ctxt->nsNr - nsNr);
Owen Taylor3473f882001-02-23 17:55:21 +00007895 if ( ret != NULL && ctxt->record_info ) {
7896 node_info.end_pos = ctxt->input->consumed +
7897 (CUR_PTR - ctxt->input->base);
7898 node_info.end_line = ctxt->input->line;
7899 node_info.node = ret;
7900 xmlParserAddNodeInfo(ctxt, &node_info);
7901 }
7902 return;
7903 }
7904 if (RAW == '>') {
Daniel Veillard21a0f912001-02-25 19:54:14 +00007905 NEXT1;
Owen Taylor3473f882001-02-23 17:55:21 +00007906 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00007907 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED,
7908 "Couldn't find end of Start Tag %s line %d\n",
7909 name, line, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007910
7911 /*
7912 * end of parsing of this node.
7913 */
7914 nodePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007915 namePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007916 spacePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007917 if (nsNr != ctxt->nsNr)
7918 nsPop(ctxt, ctxt->nsNr - nsNr);
Owen Taylor3473f882001-02-23 17:55:21 +00007919
7920 /*
7921 * Capture end position and add node
7922 */
7923 if ( ret != NULL && ctxt->record_info ) {
7924 node_info.end_pos = ctxt->input->consumed +
7925 (CUR_PTR - ctxt->input->base);
7926 node_info.end_line = ctxt->input->line;
7927 node_info.node = ret;
7928 xmlParserAddNodeInfo(ctxt, &node_info);
7929 }
7930 return;
7931 }
7932
7933 /*
7934 * Parse the content of the element:
7935 */
7936 xmlParseContent(ctxt);
Daniel Veillard73b013f2003-09-30 12:36:01 +00007937 if (!IS_BYTE_CHAR(RAW)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00007938 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED,
Daniel Veillard2b0f8792003-10-10 19:36:36 +00007939 "Premature end of data in tag %s line %d\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00007940 name, line, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007941
7942 /*
7943 * end of parsing of this node.
7944 */
7945 nodePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007946 namePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007947 spacePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007948 if (nsNr != ctxt->nsNr)
7949 nsPop(ctxt, ctxt->nsNr - nsNr);
Owen Taylor3473f882001-02-23 17:55:21 +00007950 return;
7951 }
7952
7953 /*
7954 * parse the end of tag: '</' should be here.
7955 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00007956 if (ctxt->sax2) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007957 xmlParseEndTag2(ctxt, prefix, URI, line, ctxt->nsNr - nsNr, tlen);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007958 namePop(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00007959 }
7960#ifdef LIBXML_SAX1_ENABLED
7961 else
Daniel Veillard0fb18932003-09-07 09:14:37 +00007962 xmlParseEndTag1(ctxt, line);
Daniel Veillard81273902003-09-30 00:43:48 +00007963#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00007964
7965 /*
7966 * Capture end position and add node
7967 */
7968 if ( ret != NULL && ctxt->record_info ) {
7969 node_info.end_pos = ctxt->input->consumed +
7970 (CUR_PTR - ctxt->input->base);
7971 node_info.end_line = ctxt->input->line;
7972 node_info.node = ret;
7973 xmlParserAddNodeInfo(ctxt, &node_info);
7974 }
7975}
7976
7977/**
7978 * xmlParseVersionNum:
7979 * @ctxt: an XML parser context
7980 *
7981 * parse the XML version value.
7982 *
7983 * [26] VersionNum ::= ([a-zA-Z0-9_.:] | '-')+
7984 *
7985 * Returns the string giving the XML version number, or NULL
7986 */
7987xmlChar *
7988xmlParseVersionNum(xmlParserCtxtPtr ctxt) {
7989 xmlChar *buf = NULL;
7990 int len = 0;
7991 int size = 10;
7992 xmlChar cur;
7993
Daniel Veillard3c908dc2003-04-19 00:07:51 +00007994 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00007995 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007996 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007997 return(NULL);
7998 }
7999 cur = CUR;
8000 while (((cur >= 'a') && (cur <= 'z')) ||
8001 ((cur >= 'A') && (cur <= 'Z')) ||
8002 ((cur >= '0') && (cur <= '9')) ||
8003 (cur == '_') || (cur == '.') ||
8004 (cur == ':') || (cur == '-')) {
8005 if (len + 1 >= size) {
8006 size *= 2;
8007 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
8008 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008009 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008010 return(NULL);
8011 }
8012 }
8013 buf[len++] = cur;
8014 NEXT;
8015 cur=CUR;
8016 }
8017 buf[len] = 0;
8018 return(buf);
8019}
8020
8021/**
8022 * xmlParseVersionInfo:
8023 * @ctxt: an XML parser context
8024 *
8025 * parse the XML version.
8026 *
8027 * [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ")
8028 *
8029 * [25] Eq ::= S? '=' S?
8030 *
8031 * Returns the version string, e.g. "1.0"
8032 */
8033
8034xmlChar *
8035xmlParseVersionInfo(xmlParserCtxtPtr ctxt) {
8036 xmlChar *version = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00008037
Daniel Veillarda07050d2003-10-19 14:46:32 +00008038 if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008039 SKIP(7);
8040 SKIP_BLANKS;
8041 if (RAW != '=') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008042 xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008043 return(NULL);
8044 }
8045 NEXT;
8046 SKIP_BLANKS;
8047 if (RAW == '"') {
8048 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008049 version = xmlParseVersionNum(ctxt);
8050 if (RAW != '"') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008051 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008052 } else
8053 NEXT;
8054 } else if (RAW == '\''){
8055 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008056 version = xmlParseVersionNum(ctxt);
8057 if (RAW != '\'') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008058 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008059 } else
8060 NEXT;
8061 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008062 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008063 }
8064 }
8065 return(version);
8066}
8067
8068/**
8069 * xmlParseEncName:
8070 * @ctxt: an XML parser context
8071 *
8072 * parse the XML encoding name
8073 *
8074 * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
8075 *
8076 * Returns the encoding name value or NULL
8077 */
8078xmlChar *
8079xmlParseEncName(xmlParserCtxtPtr ctxt) {
8080 xmlChar *buf = NULL;
8081 int len = 0;
8082 int size = 10;
8083 xmlChar cur;
8084
8085 cur = CUR;
8086 if (((cur >= 'a') && (cur <= 'z')) ||
8087 ((cur >= 'A') && (cur <= 'Z'))) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008088 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00008089 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008090 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008091 return(NULL);
8092 }
8093
8094 buf[len++] = cur;
8095 NEXT;
8096 cur = CUR;
8097 while (((cur >= 'a') && (cur <= 'z')) ||
8098 ((cur >= 'A') && (cur <= 'Z')) ||
8099 ((cur >= '0') && (cur <= '9')) ||
8100 (cur == '.') || (cur == '_') ||
8101 (cur == '-')) {
8102 if (len + 1 >= size) {
8103 size *= 2;
8104 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
8105 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008106 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008107 return(NULL);
8108 }
8109 }
8110 buf[len++] = cur;
8111 NEXT;
8112 cur = CUR;
8113 if (cur == 0) {
8114 SHRINK;
8115 GROW;
8116 cur = CUR;
8117 }
8118 }
8119 buf[len] = 0;
8120 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008121 xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008122 }
8123 return(buf);
8124}
8125
8126/**
8127 * xmlParseEncodingDecl:
8128 * @ctxt: an XML parser context
8129 *
8130 * parse the XML encoding declaration
8131 *
8132 * [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'")
8133 *
8134 * this setups the conversion filters.
8135 *
8136 * Returns the encoding value or NULL
8137 */
8138
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008139const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00008140xmlParseEncodingDecl(xmlParserCtxtPtr ctxt) {
8141 xmlChar *encoding = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00008142
8143 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008144 if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008145 SKIP(8);
8146 SKIP_BLANKS;
8147 if (RAW != '=') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008148 xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008149 return(NULL);
8150 }
8151 NEXT;
8152 SKIP_BLANKS;
8153 if (RAW == '"') {
8154 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008155 encoding = xmlParseEncName(ctxt);
8156 if (RAW != '"') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008157 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008158 } else
8159 NEXT;
8160 } else if (RAW == '\''){
8161 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008162 encoding = xmlParseEncName(ctxt);
8163 if (RAW != '\'') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008164 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008165 } else
8166 NEXT;
Daniel Veillard82ac6b02002-02-17 23:18:55 +00008167 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008168 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008169 }
Daniel Veillard6b621b82003-08-11 15:03:34 +00008170 /*
8171 * UTF-16 encoding stwich has already taken place at this stage,
8172 * more over the little-endian/big-endian selection is already done
8173 */
8174 if ((encoding != NULL) &&
8175 ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-16")) ||
8176 (!xmlStrcasecmp(encoding, BAD_CAST "UTF16")))) {
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008177 if (ctxt->encoding != NULL)
8178 xmlFree((xmlChar *) ctxt->encoding);
8179 ctxt->encoding = encoding;
Daniel Veillardb19ba832003-08-14 00:33:46 +00008180 }
8181 /*
8182 * UTF-8 encoding is handled natively
8183 */
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008184 else if ((encoding != NULL) &&
Daniel Veillardb19ba832003-08-14 00:33:46 +00008185 ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-8")) ||
8186 (!xmlStrcasecmp(encoding, BAD_CAST "UTF8")))) {
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008187 if (ctxt->encoding != NULL)
8188 xmlFree((xmlChar *) ctxt->encoding);
8189 ctxt->encoding = encoding;
Daniel Veillard6b621b82003-08-11 15:03:34 +00008190 }
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008191 else if (encoding != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00008192 xmlCharEncodingHandlerPtr handler;
8193
8194 if (ctxt->input->encoding != NULL)
8195 xmlFree((xmlChar *) ctxt->input->encoding);
8196 ctxt->input->encoding = encoding;
8197
Daniel Veillarda6874ca2003-07-29 16:47:24 +00008198 handler = xmlFindCharEncodingHandler((const char *) encoding);
8199 if (handler != NULL) {
8200 xmlSwitchToEncoding(ctxt, handler);
Owen Taylor3473f882001-02-23 17:55:21 +00008201 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00008202 xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillarda6874ca2003-07-29 16:47:24 +00008203 "Unsupported encoding %s\n", encoding);
8204 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008205 }
8206 }
8207 }
8208 return(encoding);
8209}
8210
8211/**
8212 * xmlParseSDDecl:
8213 * @ctxt: an XML parser context
8214 *
8215 * parse the XML standalone declaration
8216 *
8217 * [32] SDDecl ::= S 'standalone' Eq
8218 * (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"'))
8219 *
8220 * [ VC: Standalone Document Declaration ]
8221 * TODO The standalone document declaration must have the value "no"
8222 * if any external markup declarations contain declarations of:
8223 * - attributes with default values, if elements to which these
8224 * attributes apply appear in the document without specifications
8225 * of values for these attributes, or
8226 * - entities (other than amp, lt, gt, apos, quot), if references
8227 * to those entities appear in the document, or
8228 * - attributes with values subject to normalization, where the
8229 * attribute appears in the document with a value which will change
8230 * as a result of normalization, or
8231 * - element types with element content, if white space occurs directly
8232 * within any instance of those types.
8233 *
8234 * Returns 1 if standalone, 0 otherwise
8235 */
8236
8237int
8238xmlParseSDDecl(xmlParserCtxtPtr ctxt) {
8239 int standalone = -1;
8240
8241 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008242 if (CMP10(CUR_PTR, 's', 't', 'a', 'n', 'd', 'a', 'l', 'o', 'n', 'e')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008243 SKIP(10);
8244 SKIP_BLANKS;
8245 if (RAW != '=') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008246 xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008247 return(standalone);
8248 }
8249 NEXT;
8250 SKIP_BLANKS;
8251 if (RAW == '\''){
8252 NEXT;
8253 if ((RAW == 'n') && (NXT(1) == 'o')) {
8254 standalone = 0;
8255 SKIP(2);
8256 } else if ((RAW == 'y') && (NXT(1) == 'e') &&
8257 (NXT(2) == 's')) {
8258 standalone = 1;
8259 SKIP(3);
8260 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008261 xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008262 }
8263 if (RAW != '\'') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008264 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008265 } else
8266 NEXT;
8267 } else if (RAW == '"'){
8268 NEXT;
8269 if ((RAW == 'n') && (NXT(1) == 'o')) {
8270 standalone = 0;
8271 SKIP(2);
8272 } else if ((RAW == 'y') && (NXT(1) == 'e') &&
8273 (NXT(2) == 's')) {
8274 standalone = 1;
8275 SKIP(3);
8276 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008277 xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008278 }
8279 if (RAW != '"') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008280 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008281 } else
8282 NEXT;
8283 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008284 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008285 }
8286 }
8287 return(standalone);
8288}
8289
8290/**
8291 * xmlParseXMLDecl:
8292 * @ctxt: an XML parser context
8293 *
8294 * parse an XML declaration header
8295 *
8296 * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
8297 */
8298
8299void
8300xmlParseXMLDecl(xmlParserCtxtPtr ctxt) {
8301 xmlChar *version;
8302
8303 /*
8304 * We know that '<?xml' is here.
8305 */
8306 SKIP(5);
8307
William M. Brack76e95df2003-10-18 16:20:14 +00008308 if (!IS_BLANK_CH(RAW)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008309 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
8310 "Blank needed after '<?xml'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008311 }
8312 SKIP_BLANKS;
8313
8314 /*
Daniel Veillard19840942001-11-29 16:11:38 +00008315 * We must have the VersionInfo here.
Owen Taylor3473f882001-02-23 17:55:21 +00008316 */
8317 version = xmlParseVersionInfo(ctxt);
Daniel Veillard19840942001-11-29 16:11:38 +00008318 if (version == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008319 xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL);
Daniel Veillard19840942001-11-29 16:11:38 +00008320 } else {
8321 if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) {
8322 /*
8323 * TODO: Blueberry should be detected here
8324 */
Daniel Veillard24eb9782003-10-04 21:08:09 +00008325 xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION,
8326 "Unsupported version '%s'\n",
8327 version, NULL);
Daniel Veillard19840942001-11-29 16:11:38 +00008328 }
8329 if (ctxt->version != NULL)
Daniel Veillardd3b08822001-12-05 12:03:33 +00008330 xmlFree((void *) ctxt->version);
Daniel Veillard19840942001-11-29 16:11:38 +00008331 ctxt->version = version;
Daniel Veillarda050d232001-09-05 15:51:05 +00008332 }
Owen Taylor3473f882001-02-23 17:55:21 +00008333
8334 /*
8335 * We may have the encoding declaration
8336 */
William M. Brack76e95df2003-10-18 16:20:14 +00008337 if (!IS_BLANK_CH(RAW)) {
Owen Taylor3473f882001-02-23 17:55:21 +00008338 if ((RAW == '?') && (NXT(1) == '>')) {
8339 SKIP(2);
8340 return;
8341 }
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008342 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008343 }
8344 xmlParseEncodingDecl(ctxt);
8345 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
8346 /*
8347 * The XML REC instructs us to stop parsing right here
8348 */
8349 return;
8350 }
8351
8352 /*
8353 * We may have the standalone status.
8354 */
William M. Brack76e95df2003-10-18 16:20:14 +00008355 if ((ctxt->input->encoding != NULL) && (!IS_BLANK_CH(RAW))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008356 if ((RAW == '?') && (NXT(1) == '>')) {
8357 SKIP(2);
8358 return;
8359 }
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008360 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008361 }
8362 SKIP_BLANKS;
8363 ctxt->input->standalone = xmlParseSDDecl(ctxt);
8364
8365 SKIP_BLANKS;
8366 if ((RAW == '?') && (NXT(1) == '>')) {
8367 SKIP(2);
8368 } else if (RAW == '>') {
8369 /* Deprecated old WD ... */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008370 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008371 NEXT;
8372 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008373 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008374 MOVETO_ENDTAG(CUR_PTR);
8375 NEXT;
8376 }
8377}
8378
8379/**
8380 * xmlParseMisc:
8381 * @ctxt: an XML parser context
8382 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00008383 * parse an XML Misc* optional field.
Owen Taylor3473f882001-02-23 17:55:21 +00008384 *
8385 * [27] Misc ::= Comment | PI | S
8386 */
8387
8388void
8389xmlParseMisc(xmlParserCtxtPtr ctxt) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00008390 while (((RAW == '<') && (NXT(1) == '?')) ||
Daniel Veillarda07050d2003-10-19 14:46:32 +00008391 (CMP4(CUR_PTR, '<', '!', '-', '-')) ||
William M. Brack76e95df2003-10-18 16:20:14 +00008392 IS_BLANK_CH(CUR)) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00008393 if ((RAW == '<') && (NXT(1) == '?')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008394 xmlParsePI(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00008395 } else if (IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00008396 NEXT;
8397 } else
8398 xmlParseComment(ctxt);
8399 }
8400}
8401
8402/**
8403 * xmlParseDocument:
8404 * @ctxt: an XML parser context
8405 *
8406 * parse an XML document (and build a tree if using the standard SAX
8407 * interface).
8408 *
8409 * [1] document ::= prolog element Misc*
8410 *
8411 * [22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)?
8412 *
8413 * Returns 0, -1 in case of error. the parser context is augmented
8414 * as a result of the parsing.
8415 */
8416
8417int
8418xmlParseDocument(xmlParserCtxtPtr ctxt) {
8419 xmlChar start[4];
8420 xmlCharEncoding enc;
8421
8422 xmlInitParser();
8423
8424 GROW;
8425
8426 /*
Daniel Veillard0fb18932003-09-07 09:14:37 +00008427 * SAX: detecting the level.
8428 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00008429 xmlDetectSAX2(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008430
8431 /*
Owen Taylor3473f882001-02-23 17:55:21 +00008432 * SAX: beginning of the document processing.
8433 */
8434 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
8435 ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
8436
Daniel Veillard4aede2e2003-10-17 12:43:59 +00008437 if ((ctxt->encoding == (const xmlChar *)XML_CHAR_ENCODING_NONE) &&
8438 ((ctxt->input->end - ctxt->input->cur) >= 4)) {
Daniel Veillard4aafa792001-07-28 17:21:12 +00008439 /*
8440 * Get the 4 first bytes and decode the charset
8441 * if enc != XML_CHAR_ENCODING_NONE
8442 * plug some encoding conversion routines.
8443 */
8444 start[0] = RAW;
8445 start[1] = NXT(1);
8446 start[2] = NXT(2);
8447 start[3] = NXT(3);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00008448 enc = xmlDetectCharEncoding(&start[0], 4);
Daniel Veillard4aafa792001-07-28 17:21:12 +00008449 if (enc != XML_CHAR_ENCODING_NONE) {
8450 xmlSwitchEncoding(ctxt, enc);
8451 }
Owen Taylor3473f882001-02-23 17:55:21 +00008452 }
8453
8454
8455 if (CUR == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008456 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008457 }
8458
8459 /*
8460 * Check for the XMLDecl in the Prolog.
8461 */
8462 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008463 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008464
8465 /*
8466 * Note that we will switch encoding on the fly.
8467 */
8468 xmlParseXMLDecl(ctxt);
8469 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
8470 /*
8471 * The XML REC instructs us to stop parsing right here
8472 */
8473 return(-1);
8474 }
8475 ctxt->standalone = ctxt->input->standalone;
8476 SKIP_BLANKS;
8477 } else {
8478 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
8479 }
8480 if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
8481 ctxt->sax->startDocument(ctxt->userData);
8482
8483 /*
8484 * The Misc part of the Prolog
8485 */
8486 GROW;
8487 xmlParseMisc(ctxt);
8488
8489 /*
8490 * Then possibly doc type declaration(s) and more Misc
8491 * (doctypedecl Misc*)?
8492 */
8493 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008494 if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008495
8496 ctxt->inSubset = 1;
8497 xmlParseDocTypeDecl(ctxt);
8498 if (RAW == '[') {
8499 ctxt->instate = XML_PARSER_DTD;
8500 xmlParseInternalSubset(ctxt);
8501 }
8502
8503 /*
8504 * Create and update the external subset.
8505 */
8506 ctxt->inSubset = 2;
8507 if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) &&
8508 (!ctxt->disableSAX))
8509 ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
8510 ctxt->extSubSystem, ctxt->extSubURI);
8511 ctxt->inSubset = 0;
8512
8513
8514 ctxt->instate = XML_PARSER_PROLOG;
8515 xmlParseMisc(ctxt);
8516 }
8517
8518 /*
8519 * Time to start parsing the tree itself
8520 */
8521 GROW;
8522 if (RAW != '<') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008523 xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY,
8524 "Start tag expected, '<' not found\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008525 } else {
8526 ctxt->instate = XML_PARSER_CONTENT;
8527 xmlParseElement(ctxt);
8528 ctxt->instate = XML_PARSER_EPILOG;
8529
8530
8531 /*
8532 * The Misc part at the end
8533 */
8534 xmlParseMisc(ctxt);
8535
Daniel Veillard561b7f82002-03-20 21:55:57 +00008536 if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008537 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008538 }
8539 ctxt->instate = XML_PARSER_EOF;
8540 }
8541
8542 /*
8543 * SAX: end of the document processing.
8544 */
Daniel Veillard8d24cc12002-03-05 15:41:29 +00008545 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00008546 ctxt->sax->endDocument(ctxt->userData);
8547
Daniel Veillard5997aca2002-03-18 18:36:20 +00008548 /*
8549 * Remove locally kept entity definitions if the tree was not built
8550 */
8551 if ((ctxt->myDoc != NULL) &&
8552 (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) {
8553 xmlFreeDoc(ctxt->myDoc);
8554 ctxt->myDoc = NULL;
8555 }
8556
Daniel Veillardc7612992002-02-17 22:47:37 +00008557 if (! ctxt->wellFormed) {
8558 ctxt->valid = 0;
8559 return(-1);
8560 }
Owen Taylor3473f882001-02-23 17:55:21 +00008561 return(0);
8562}
8563
8564/**
8565 * xmlParseExtParsedEnt:
8566 * @ctxt: an XML parser context
8567 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00008568 * parse a general parsed entity
Owen Taylor3473f882001-02-23 17:55:21 +00008569 * An external general parsed entity is well-formed if it matches the
8570 * production labeled extParsedEnt.
8571 *
8572 * [78] extParsedEnt ::= TextDecl? content
8573 *
8574 * Returns 0, -1 in case of error. the parser context is augmented
8575 * as a result of the parsing.
8576 */
8577
8578int
8579xmlParseExtParsedEnt(xmlParserCtxtPtr ctxt) {
8580 xmlChar start[4];
8581 xmlCharEncoding enc;
8582
8583 xmlDefaultSAXHandlerInit();
8584
Daniel Veillard309f81d2003-09-23 09:02:53 +00008585 xmlDetectSAX2(ctxt);
8586
Owen Taylor3473f882001-02-23 17:55:21 +00008587 GROW;
8588
8589 /*
8590 * SAX: beginning of the document processing.
8591 */
8592 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
8593 ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
8594
8595 /*
8596 * Get the 4 first bytes and decode the charset
8597 * if enc != XML_CHAR_ENCODING_NONE
8598 * plug some encoding conversion routines.
8599 */
Daniel Veillard4aede2e2003-10-17 12:43:59 +00008600 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
8601 start[0] = RAW;
8602 start[1] = NXT(1);
8603 start[2] = NXT(2);
8604 start[3] = NXT(3);
8605 enc = xmlDetectCharEncoding(start, 4);
8606 if (enc != XML_CHAR_ENCODING_NONE) {
8607 xmlSwitchEncoding(ctxt, enc);
8608 }
Owen Taylor3473f882001-02-23 17:55:21 +00008609 }
8610
8611
8612 if (CUR == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008613 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008614 }
8615
8616 /*
8617 * Check for the XMLDecl in the Prolog.
8618 */
8619 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008620 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008621
8622 /*
8623 * Note that we will switch encoding on the fly.
8624 */
8625 xmlParseXMLDecl(ctxt);
8626 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
8627 /*
8628 * The XML REC instructs us to stop parsing right here
8629 */
8630 return(-1);
8631 }
8632 SKIP_BLANKS;
8633 } else {
8634 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
8635 }
8636 if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
8637 ctxt->sax->startDocument(ctxt->userData);
8638
8639 /*
8640 * Doing validity checking on chunk doesn't make sense
8641 */
8642 ctxt->instate = XML_PARSER_CONTENT;
8643 ctxt->validate = 0;
8644 ctxt->loadsubset = 0;
8645 ctxt->depth = 0;
8646
8647 xmlParseContent(ctxt);
8648
8649 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008650 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008651 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008652 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008653 }
8654
8655 /*
8656 * SAX: end of the document processing.
8657 */
Daniel Veillard8d24cc12002-03-05 15:41:29 +00008658 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00008659 ctxt->sax->endDocument(ctxt->userData);
8660
8661 if (! ctxt->wellFormed) return(-1);
8662 return(0);
8663}
8664
Daniel Veillard73b013f2003-09-30 12:36:01 +00008665#ifdef LIBXML_PUSH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00008666/************************************************************************
8667 * *
8668 * Progressive parsing interfaces *
8669 * *
8670 ************************************************************************/
8671
8672/**
8673 * xmlParseLookupSequence:
8674 * @ctxt: an XML parser context
8675 * @first: the first char to lookup
8676 * @next: the next char to lookup or zero
8677 * @third: the next char to lookup or zero
8678 *
8679 * Try to find if a sequence (first, next, third) or just (first next) or
8680 * (first) is available in the input stream.
8681 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
8682 * to avoid rescanning sequences of bytes, it DOES change the state of the
8683 * parser, do not use liberally.
8684 *
8685 * Returns the index to the current parsing point if the full sequence
8686 * is available, -1 otherwise.
8687 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008688static int
Owen Taylor3473f882001-02-23 17:55:21 +00008689xmlParseLookupSequence(xmlParserCtxtPtr ctxt, xmlChar first,
8690 xmlChar next, xmlChar third) {
8691 int base, len;
8692 xmlParserInputPtr in;
8693 const xmlChar *buf;
8694
8695 in = ctxt->input;
8696 if (in == NULL) return(-1);
8697 base = in->cur - in->base;
8698 if (base < 0) return(-1);
8699 if (ctxt->checkIndex > base)
8700 base = ctxt->checkIndex;
8701 if (in->buf == NULL) {
8702 buf = in->base;
8703 len = in->length;
8704 } else {
8705 buf = in->buf->buffer->content;
8706 len = in->buf->buffer->use;
8707 }
8708 /* take into account the sequence length */
8709 if (third) len -= 2;
8710 else if (next) len --;
8711 for (;base < len;base++) {
8712 if (buf[base] == first) {
8713 if (third != 0) {
8714 if ((buf[base + 1] != next) ||
8715 (buf[base + 2] != third)) continue;
8716 } else if (next != 0) {
8717 if (buf[base + 1] != next) continue;
8718 }
8719 ctxt->checkIndex = 0;
8720#ifdef DEBUG_PUSH
8721 if (next == 0)
8722 xmlGenericError(xmlGenericErrorContext,
8723 "PP: lookup '%c' found at %d\n",
8724 first, base);
8725 else if (third == 0)
8726 xmlGenericError(xmlGenericErrorContext,
8727 "PP: lookup '%c%c' found at %d\n",
8728 first, next, base);
8729 else
8730 xmlGenericError(xmlGenericErrorContext,
8731 "PP: lookup '%c%c%c' found at %d\n",
8732 first, next, third, base);
8733#endif
8734 return(base - (in->cur - in->base));
8735 }
8736 }
8737 ctxt->checkIndex = base;
8738#ifdef DEBUG_PUSH
8739 if (next == 0)
8740 xmlGenericError(xmlGenericErrorContext,
8741 "PP: lookup '%c' failed\n", first);
8742 else if (third == 0)
8743 xmlGenericError(xmlGenericErrorContext,
8744 "PP: lookup '%c%c' failed\n", first, next);
8745 else
8746 xmlGenericError(xmlGenericErrorContext,
8747 "PP: lookup '%c%c%c' failed\n", first, next, third);
8748#endif
8749 return(-1);
8750}
8751
8752/**
Daniel Veillarda880b122003-04-21 21:36:41 +00008753 * xmlParseGetLasts:
8754 * @ctxt: an XML parser context
8755 * @lastlt: pointer to store the last '<' from the input
8756 * @lastgt: pointer to store the last '>' from the input
8757 *
8758 * Lookup the last < and > in the current chunk
8759 */
8760static void
8761xmlParseGetLasts(xmlParserCtxtPtr ctxt, const xmlChar **lastlt,
8762 const xmlChar **lastgt) {
8763 const xmlChar *tmp;
8764
8765 if ((ctxt == NULL) || (lastlt == NULL) || (lastgt == NULL)) {
8766 xmlGenericError(xmlGenericErrorContext,
8767 "Internal error: xmlParseGetLasts\n");
8768 return;
8769 }
8770 if ((ctxt->progressive == 1) && (ctxt->inputNr == 1)) {
8771 tmp = ctxt->input->end;
8772 tmp--;
8773 while ((tmp >= ctxt->input->base) && (*tmp != '<') &&
8774 (*tmp != '>')) tmp--;
8775 if (tmp < ctxt->input->base) {
8776 *lastlt = NULL;
8777 *lastgt = NULL;
8778 } else if (*tmp == '<') {
8779 *lastlt = tmp;
8780 tmp--;
8781 while ((tmp >= ctxt->input->base) && (*tmp != '>')) tmp--;
8782 if (tmp < ctxt->input->base)
8783 *lastgt = NULL;
8784 else
8785 *lastgt = tmp;
8786 } else {
8787 *lastgt = tmp;
8788 tmp--;
8789 while ((tmp >= ctxt->input->base) && (*tmp != '<')) tmp--;
8790 if (tmp < ctxt->input->base)
8791 *lastlt = NULL;
8792 else
8793 *lastlt = tmp;
8794 }
8795
8796 } else {
8797 *lastlt = NULL;
8798 *lastgt = NULL;
8799 }
8800}
8801/**
Owen Taylor3473f882001-02-23 17:55:21 +00008802 * xmlParseTryOrFinish:
8803 * @ctxt: an XML parser context
8804 * @terminate: last chunk indicator
8805 *
8806 * Try to progress on parsing
8807 *
8808 * Returns zero if no parsing was possible
8809 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008810static int
Owen Taylor3473f882001-02-23 17:55:21 +00008811xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) {
8812 int ret = 0;
Daniel Veillardc82c57e2004-01-12 16:24:34 +00008813 int avail, tlen;
Owen Taylor3473f882001-02-23 17:55:21 +00008814 xmlChar cur, next;
Daniel Veillarda880b122003-04-21 21:36:41 +00008815 const xmlChar *lastlt, *lastgt;
Owen Taylor3473f882001-02-23 17:55:21 +00008816
8817#ifdef DEBUG_PUSH
8818 switch (ctxt->instate) {
8819 case XML_PARSER_EOF:
8820 xmlGenericError(xmlGenericErrorContext,
8821 "PP: try EOF\n"); break;
8822 case XML_PARSER_START:
8823 xmlGenericError(xmlGenericErrorContext,
8824 "PP: try START\n"); break;
8825 case XML_PARSER_MISC:
8826 xmlGenericError(xmlGenericErrorContext,
8827 "PP: try MISC\n");break;
8828 case XML_PARSER_COMMENT:
8829 xmlGenericError(xmlGenericErrorContext,
8830 "PP: try COMMENT\n");break;
8831 case XML_PARSER_PROLOG:
8832 xmlGenericError(xmlGenericErrorContext,
8833 "PP: try PROLOG\n");break;
8834 case XML_PARSER_START_TAG:
8835 xmlGenericError(xmlGenericErrorContext,
8836 "PP: try START_TAG\n");break;
8837 case XML_PARSER_CONTENT:
8838 xmlGenericError(xmlGenericErrorContext,
8839 "PP: try CONTENT\n");break;
8840 case XML_PARSER_CDATA_SECTION:
8841 xmlGenericError(xmlGenericErrorContext,
8842 "PP: try CDATA_SECTION\n");break;
8843 case XML_PARSER_END_TAG:
8844 xmlGenericError(xmlGenericErrorContext,
8845 "PP: try END_TAG\n");break;
8846 case XML_PARSER_ENTITY_DECL:
8847 xmlGenericError(xmlGenericErrorContext,
8848 "PP: try ENTITY_DECL\n");break;
8849 case XML_PARSER_ENTITY_VALUE:
8850 xmlGenericError(xmlGenericErrorContext,
8851 "PP: try ENTITY_VALUE\n");break;
8852 case XML_PARSER_ATTRIBUTE_VALUE:
8853 xmlGenericError(xmlGenericErrorContext,
8854 "PP: try ATTRIBUTE_VALUE\n");break;
8855 case XML_PARSER_DTD:
8856 xmlGenericError(xmlGenericErrorContext,
8857 "PP: try DTD\n");break;
8858 case XML_PARSER_EPILOG:
8859 xmlGenericError(xmlGenericErrorContext,
8860 "PP: try EPILOG\n");break;
8861 case XML_PARSER_PI:
8862 xmlGenericError(xmlGenericErrorContext,
8863 "PP: try PI\n");break;
8864 case XML_PARSER_IGNORE:
8865 xmlGenericError(xmlGenericErrorContext,
8866 "PP: try IGNORE\n");break;
8867 }
8868#endif
8869
Daniel Veillard198c1bf2003-10-20 17:07:41 +00008870 if ((ctxt->input != NULL) &&
8871 (ctxt->input->cur - ctxt->input->base > 4096)) {
Daniel Veillarda880b122003-04-21 21:36:41 +00008872 xmlSHRINK(ctxt);
8873 ctxt->checkIndex = 0;
8874 }
8875 xmlParseGetLasts(ctxt, &lastlt, &lastgt);
Aleksey Sanine48a3182002-05-09 18:20:01 +00008876
Daniel Veillarda880b122003-04-21 21:36:41 +00008877 while (1) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00008878 if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
8879 return(0);
8880
8881
Owen Taylor3473f882001-02-23 17:55:21 +00008882 /*
8883 * Pop-up of finished entities.
8884 */
8885 while ((RAW == 0) && (ctxt->inputNr > 1))
8886 xmlPopInput(ctxt);
8887
Daniel Veillard198c1bf2003-10-20 17:07:41 +00008888 if (ctxt->input == NULL) break;
Owen Taylor3473f882001-02-23 17:55:21 +00008889 if (ctxt->input->buf == NULL)
Daniel Veillarda880b122003-04-21 21:36:41 +00008890 avail = ctxt->input->length -
8891 (ctxt->input->cur - ctxt->input->base);
Daniel Veillard158a4d22002-02-20 22:17:58 +00008892 else {
8893 /*
8894 * If we are operating on converted input, try to flush
8895 * remainng chars to avoid them stalling in the non-converted
8896 * buffer.
8897 */
8898 if ((ctxt->input->buf->raw != NULL) &&
8899 (ctxt->input->buf->raw->use > 0)) {
8900 int base = ctxt->input->base -
8901 ctxt->input->buf->buffer->content;
8902 int current = ctxt->input->cur - ctxt->input->base;
8903
8904 xmlParserInputBufferPush(ctxt->input->buf, 0, "");
8905 ctxt->input->base = ctxt->input->buf->buffer->content + base;
8906 ctxt->input->cur = ctxt->input->base + current;
8907 ctxt->input->end =
8908 &ctxt->input->buf->buffer->content[
8909 ctxt->input->buf->buffer->use];
8910 }
8911 avail = ctxt->input->buf->buffer->use -
8912 (ctxt->input->cur - ctxt->input->base);
8913 }
Owen Taylor3473f882001-02-23 17:55:21 +00008914 if (avail < 1)
8915 goto done;
8916 switch (ctxt->instate) {
8917 case XML_PARSER_EOF:
8918 /*
8919 * Document parsing is done !
8920 */
8921 goto done;
8922 case XML_PARSER_START:
Daniel Veillard0e4cd172001-06-28 12:13:56 +00008923 if (ctxt->charset == XML_CHAR_ENCODING_NONE) {
8924 xmlChar start[4];
8925 xmlCharEncoding enc;
8926
8927 /*
8928 * Very first chars read from the document flow.
8929 */
8930 if (avail < 4)
8931 goto done;
8932
8933 /*
8934 * Get the 4 first bytes and decode the charset
8935 * if enc != XML_CHAR_ENCODING_NONE
8936 * plug some encoding conversion routines.
8937 */
8938 start[0] = RAW;
8939 start[1] = NXT(1);
8940 start[2] = NXT(2);
8941 start[3] = NXT(3);
8942 enc = xmlDetectCharEncoding(start, 4);
8943 if (enc != XML_CHAR_ENCODING_NONE) {
8944 xmlSwitchEncoding(ctxt, enc);
8945 }
8946 break;
8947 }
Owen Taylor3473f882001-02-23 17:55:21 +00008948
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00008949 if (avail < 2)
8950 goto done;
Owen Taylor3473f882001-02-23 17:55:21 +00008951 cur = ctxt->input->cur[0];
8952 next = ctxt->input->cur[1];
8953 if (cur == 0) {
8954 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
8955 ctxt->sax->setDocumentLocator(ctxt->userData,
8956 &xmlDefaultSAXLocator);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008957 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008958 ctxt->instate = XML_PARSER_EOF;
8959#ifdef DEBUG_PUSH
8960 xmlGenericError(xmlGenericErrorContext,
8961 "PP: entering EOF\n");
8962#endif
8963 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
8964 ctxt->sax->endDocument(ctxt->userData);
8965 goto done;
8966 }
8967 if ((cur == '<') && (next == '?')) {
8968 /* PI or XML decl */
8969 if (avail < 5) return(ret);
8970 if ((!terminate) &&
8971 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
8972 return(ret);
8973 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
8974 ctxt->sax->setDocumentLocator(ctxt->userData,
8975 &xmlDefaultSAXLocator);
8976 if ((ctxt->input->cur[2] == 'x') &&
8977 (ctxt->input->cur[3] == 'm') &&
8978 (ctxt->input->cur[4] == 'l') &&
William M. Brack76e95df2003-10-18 16:20:14 +00008979 (IS_BLANK_CH(ctxt->input->cur[5]))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008980 ret += 5;
8981#ifdef DEBUG_PUSH
8982 xmlGenericError(xmlGenericErrorContext,
8983 "PP: Parsing XML Decl\n");
8984#endif
8985 xmlParseXMLDecl(ctxt);
8986 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
8987 /*
8988 * The XML REC instructs us to stop parsing right
8989 * here
8990 */
8991 ctxt->instate = XML_PARSER_EOF;
8992 return(0);
8993 }
8994 ctxt->standalone = ctxt->input->standalone;
8995 if ((ctxt->encoding == NULL) &&
8996 (ctxt->input->encoding != NULL))
8997 ctxt->encoding = xmlStrdup(ctxt->input->encoding);
8998 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
8999 (!ctxt->disableSAX))
9000 ctxt->sax->startDocument(ctxt->userData);
9001 ctxt->instate = XML_PARSER_MISC;
9002#ifdef DEBUG_PUSH
9003 xmlGenericError(xmlGenericErrorContext,
9004 "PP: entering MISC\n");
9005#endif
9006 } else {
9007 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
9008 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
9009 (!ctxt->disableSAX))
9010 ctxt->sax->startDocument(ctxt->userData);
9011 ctxt->instate = XML_PARSER_MISC;
9012#ifdef DEBUG_PUSH
9013 xmlGenericError(xmlGenericErrorContext,
9014 "PP: entering MISC\n");
9015#endif
9016 }
9017 } else {
9018 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
9019 ctxt->sax->setDocumentLocator(ctxt->userData,
9020 &xmlDefaultSAXLocator);
9021 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
9022 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
9023 (!ctxt->disableSAX))
9024 ctxt->sax->startDocument(ctxt->userData);
9025 ctxt->instate = XML_PARSER_MISC;
9026#ifdef DEBUG_PUSH
9027 xmlGenericError(xmlGenericErrorContext,
9028 "PP: entering MISC\n");
9029#endif
9030 }
9031 break;
Daniel Veillarda880b122003-04-21 21:36:41 +00009032 case XML_PARSER_START_TAG: {
Daniel Veillarde57ec792003-09-10 10:50:59 +00009033 const xmlChar *name;
9034 const xmlChar *prefix;
9035 const xmlChar *URI;
9036 int nsNr = ctxt->nsNr;
Daniel Veillarda880b122003-04-21 21:36:41 +00009037
9038 if ((avail < 2) && (ctxt->inputNr == 1))
9039 goto done;
9040 cur = ctxt->input->cur[0];
9041 if (cur != '<') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009042 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Daniel Veillarda880b122003-04-21 21:36:41 +00009043 ctxt->instate = XML_PARSER_EOF;
Daniel Veillarda880b122003-04-21 21:36:41 +00009044 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
9045 ctxt->sax->endDocument(ctxt->userData);
9046 goto done;
9047 }
9048 if (!terminate) {
9049 if (ctxt->progressive) {
Daniel Veillardb3744002004-02-18 14:28:22 +00009050 /* > can be found unescaped in attribute values */
9051 if ((lastlt == NULL) || (ctxt->input->cur >= lastlt))
Daniel Veillarda880b122003-04-21 21:36:41 +00009052 goto done;
9053 } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) {
9054 goto done;
9055 }
9056 }
9057 if (ctxt->spaceNr == 0)
9058 spacePush(ctxt, -1);
9059 else
9060 spacePush(ctxt, *ctxt->space);
Daniel Veillard81273902003-09-30 00:43:48 +00009061#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009062 if (ctxt->sax2)
Daniel Veillard81273902003-09-30 00:43:48 +00009063#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00009064 name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen);
Daniel Veillard81273902003-09-30 00:43:48 +00009065#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009066 else
9067 name = xmlParseStartTag(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00009068#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarda880b122003-04-21 21:36:41 +00009069 if (name == NULL) {
9070 spacePop(ctxt);
9071 ctxt->instate = XML_PARSER_EOF;
Daniel Veillarda880b122003-04-21 21:36:41 +00009072 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
9073 ctxt->sax->endDocument(ctxt->userData);
9074 goto done;
9075 }
Daniel Veillard4432df22003-09-28 18:58:27 +00009076#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda880b122003-04-21 21:36:41 +00009077 /*
9078 * [ VC: Root Element Type ]
9079 * The Name in the document type declaration must match
9080 * the element type of the root element.
9081 */
9082 if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
9083 ctxt->node && (ctxt->node == ctxt->myDoc->children))
9084 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillard4432df22003-09-28 18:58:27 +00009085#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda880b122003-04-21 21:36:41 +00009086
9087 /*
9088 * Check for an Empty Element.
9089 */
9090 if ((RAW == '/') && (NXT(1) == '>')) {
9091 SKIP(2);
Daniel Veillarde57ec792003-09-10 10:50:59 +00009092
9093 if (ctxt->sax2) {
9094 if ((ctxt->sax != NULL) &&
9095 (ctxt->sax->endElementNs != NULL) &&
9096 (!ctxt->disableSAX))
9097 ctxt->sax->endElementNs(ctxt->userData, name,
9098 prefix, URI);
Daniel Veillard81273902003-09-30 00:43:48 +00009099#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009100 } else {
9101 if ((ctxt->sax != NULL) &&
9102 (ctxt->sax->endElement != NULL) &&
9103 (!ctxt->disableSAX))
9104 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard81273902003-09-30 00:43:48 +00009105#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarda880b122003-04-21 21:36:41 +00009106 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009107 spacePop(ctxt);
9108 if (ctxt->nameNr == 0) {
Daniel Veillarda880b122003-04-21 21:36:41 +00009109 ctxt->instate = XML_PARSER_EPILOG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009110 } else {
9111 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillarda880b122003-04-21 21:36:41 +00009112 }
9113 break;
9114 }
9115 if (RAW == '>') {
9116 NEXT;
9117 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00009118 xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED,
Daniel Veillarda880b122003-04-21 21:36:41 +00009119 "Couldn't find end of Start Tag %s\n",
9120 name);
Daniel Veillarda880b122003-04-21 21:36:41 +00009121 nodePop(ctxt);
Daniel Veillarda880b122003-04-21 21:36:41 +00009122 spacePop(ctxt);
Daniel Veillarda880b122003-04-21 21:36:41 +00009123 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009124 if (ctxt->sax2)
9125 nameNsPush(ctxt, name, prefix, URI, ctxt->nsNr - nsNr);
Daniel Veillard81273902003-09-30 00:43:48 +00009126#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009127 else
9128 namePush(ctxt, name);
Daniel Veillard81273902003-09-30 00:43:48 +00009129#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00009130
Daniel Veillarda880b122003-04-21 21:36:41 +00009131 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillarda880b122003-04-21 21:36:41 +00009132 break;
9133 }
9134 case XML_PARSER_CONTENT: {
9135 const xmlChar *test;
9136 unsigned int cons;
9137 if ((avail < 2) && (ctxt->inputNr == 1))
9138 goto done;
9139 cur = ctxt->input->cur[0];
9140 next = ctxt->input->cur[1];
9141
9142 test = CUR_PTR;
9143 cons = ctxt->input->consumed;
9144 if ((cur == '<') && (next == '/')) {
9145 ctxt->instate = XML_PARSER_END_TAG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009146 break;
9147 } else if ((cur == '<') && (next == '?')) {
9148 if ((!terminate) &&
9149 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9150 goto done;
Daniel Veillarda880b122003-04-21 21:36:41 +00009151 xmlParsePI(ctxt);
9152 } else if ((cur == '<') && (next != '!')) {
9153 ctxt->instate = XML_PARSER_START_TAG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009154 break;
9155 } else if ((cur == '<') && (next == '!') &&
9156 (ctxt->input->cur[2] == '-') &&
9157 (ctxt->input->cur[3] == '-')) {
9158 if ((!terminate) &&
9159 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9160 goto done;
Daniel Veillarda880b122003-04-21 21:36:41 +00009161 xmlParseComment(ctxt);
9162 ctxt->instate = XML_PARSER_CONTENT;
9163 } else if ((cur == '<') && (ctxt->input->cur[1] == '!') &&
9164 (ctxt->input->cur[2] == '[') &&
9165 (ctxt->input->cur[3] == 'C') &&
9166 (ctxt->input->cur[4] == 'D') &&
9167 (ctxt->input->cur[5] == 'A') &&
9168 (ctxt->input->cur[6] == 'T') &&
9169 (ctxt->input->cur[7] == 'A') &&
9170 (ctxt->input->cur[8] == '[')) {
9171 SKIP(9);
9172 ctxt->instate = XML_PARSER_CDATA_SECTION;
Daniel Veillarda880b122003-04-21 21:36:41 +00009173 break;
9174 } else if ((cur == '<') && (next == '!') &&
9175 (avail < 9)) {
9176 goto done;
9177 } else if (cur == '&') {
9178 if ((!terminate) &&
9179 (xmlParseLookupSequence(ctxt, ';', 0, 0) < 0))
9180 goto done;
Daniel Veillarda880b122003-04-21 21:36:41 +00009181 xmlParseReference(ctxt);
9182 } else {
9183 /* TODO Avoid the extra copy, handle directly !!! */
9184 /*
9185 * Goal of the following test is:
9186 * - minimize calls to the SAX 'character' callback
9187 * when they are mergeable
9188 * - handle an problem for isBlank when we only parse
9189 * a sequence of blank chars and the next one is
9190 * not available to check against '<' presence.
9191 * - tries to homogenize the differences in SAX
9192 * callbacks between the push and pull versions
9193 * of the parser.
9194 */
9195 if ((ctxt->inputNr == 1) &&
9196 (avail < XML_PARSER_BIG_BUFFER_SIZE)) {
9197 if (!terminate) {
9198 if (ctxt->progressive) {
9199 if ((lastlt == NULL) ||
9200 (ctxt->input->cur > lastlt))
9201 goto done;
9202 } else if (xmlParseLookupSequence(ctxt,
9203 '<', 0, 0) < 0) {
9204 goto done;
9205 }
9206 }
9207 }
9208 ctxt->checkIndex = 0;
Daniel Veillarda880b122003-04-21 21:36:41 +00009209 xmlParseCharData(ctxt, 0);
9210 }
9211 /*
9212 * Pop-up of finished entities.
9213 */
9214 while ((RAW == 0) && (ctxt->inputNr > 1))
9215 xmlPopInput(ctxt);
9216 if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009217 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
9218 "detected an error in element content\n");
Daniel Veillarda880b122003-04-21 21:36:41 +00009219 ctxt->instate = XML_PARSER_EOF;
9220 break;
9221 }
9222 break;
9223 }
9224 case XML_PARSER_END_TAG:
9225 if (avail < 2)
9226 goto done;
9227 if (!terminate) {
9228 if (ctxt->progressive) {
9229 if ((lastgt == NULL) || (ctxt->input->cur > lastgt))
9230 goto done;
9231 } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) {
9232 goto done;
9233 }
9234 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009235 if (ctxt->sax2) {
9236 xmlParseEndTag2(ctxt,
9237 (void *) ctxt->pushTab[ctxt->nameNr * 3 - 3],
9238 (void *) ctxt->pushTab[ctxt->nameNr * 3 - 2], 0,
Daniel Veillardc82c57e2004-01-12 16:24:34 +00009239 (int) (long) ctxt->pushTab[ctxt->nameNr * 3 - 1], 0);
Daniel Veillarde57ec792003-09-10 10:50:59 +00009240 nameNsPop(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00009241 }
9242#ifdef LIBXML_SAX1_ENABLED
9243 else
Daniel Veillarde57ec792003-09-10 10:50:59 +00009244 xmlParseEndTag1(ctxt, 0);
Daniel Veillard81273902003-09-30 00:43:48 +00009245#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00009246 if (ctxt->nameNr == 0) {
Daniel Veillarda880b122003-04-21 21:36:41 +00009247 ctxt->instate = XML_PARSER_EPILOG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009248 } else {
9249 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillarda880b122003-04-21 21:36:41 +00009250 }
9251 break;
9252 case XML_PARSER_CDATA_SECTION: {
9253 /*
9254 * The Push mode need to have the SAX callback for
9255 * cdataBlock merge back contiguous callbacks.
9256 */
9257 int base;
9258
9259 base = xmlParseLookupSequence(ctxt, ']', ']', '>');
9260 if (base < 0) {
9261 if (avail >= XML_PARSER_BIG_BUFFER_SIZE + 2) {
9262 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
9263 if (ctxt->sax->cdataBlock != NULL)
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00009264 ctxt->sax->cdataBlock(ctxt->userData,
9265 ctxt->input->cur,
9266 XML_PARSER_BIG_BUFFER_SIZE);
9267 else if (ctxt->sax->characters != NULL)
9268 ctxt->sax->characters(ctxt->userData,
9269 ctxt->input->cur,
Daniel Veillarda880b122003-04-21 21:36:41 +00009270 XML_PARSER_BIG_BUFFER_SIZE);
9271 }
Daniel Veillard0b787f32004-03-26 17:29:53 +00009272 SKIPL(XML_PARSER_BIG_BUFFER_SIZE);
Daniel Veillarda880b122003-04-21 21:36:41 +00009273 ctxt->checkIndex = 0;
9274 }
9275 goto done;
9276 } else {
9277 if ((ctxt->sax != NULL) && (base > 0) &&
9278 (!ctxt->disableSAX)) {
9279 if (ctxt->sax->cdataBlock != NULL)
9280 ctxt->sax->cdataBlock(ctxt->userData,
9281 ctxt->input->cur, base);
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00009282 else if (ctxt->sax->characters != NULL)
9283 ctxt->sax->characters(ctxt->userData,
9284 ctxt->input->cur, base);
Daniel Veillarda880b122003-04-21 21:36:41 +00009285 }
Daniel Veillard0b787f32004-03-26 17:29:53 +00009286 SKIPL(base + 3);
Daniel Veillarda880b122003-04-21 21:36:41 +00009287 ctxt->checkIndex = 0;
9288 ctxt->instate = XML_PARSER_CONTENT;
9289#ifdef DEBUG_PUSH
9290 xmlGenericError(xmlGenericErrorContext,
9291 "PP: entering CONTENT\n");
9292#endif
9293 }
9294 break;
9295 }
Owen Taylor3473f882001-02-23 17:55:21 +00009296 case XML_PARSER_MISC:
9297 SKIP_BLANKS;
9298 if (ctxt->input->buf == NULL)
Daniel Veillarda880b122003-04-21 21:36:41 +00009299 avail = ctxt->input->length -
9300 (ctxt->input->cur - ctxt->input->base);
Owen Taylor3473f882001-02-23 17:55:21 +00009301 else
Daniel Veillarda880b122003-04-21 21:36:41 +00009302 avail = ctxt->input->buf->buffer->use -
9303 (ctxt->input->cur - ctxt->input->base);
Owen Taylor3473f882001-02-23 17:55:21 +00009304 if (avail < 2)
9305 goto done;
9306 cur = ctxt->input->cur[0];
9307 next = ctxt->input->cur[1];
9308 if ((cur == '<') && (next == '?')) {
9309 if ((!terminate) &&
9310 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9311 goto done;
9312#ifdef DEBUG_PUSH
9313 xmlGenericError(xmlGenericErrorContext,
9314 "PP: Parsing PI\n");
9315#endif
9316 xmlParsePI(ctxt);
9317 } else if ((cur == '<') && (next == '!') &&
Daniel Veillarda880b122003-04-21 21:36:41 +00009318 (ctxt->input->cur[2] == '-') &&
9319 (ctxt->input->cur[3] == '-')) {
Owen Taylor3473f882001-02-23 17:55:21 +00009320 if ((!terminate) &&
9321 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9322 goto done;
9323#ifdef DEBUG_PUSH
9324 xmlGenericError(xmlGenericErrorContext,
9325 "PP: Parsing Comment\n");
9326#endif
9327 xmlParseComment(ctxt);
9328 ctxt->instate = XML_PARSER_MISC;
9329 } else if ((cur == '<') && (next == '!') &&
Daniel Veillarda880b122003-04-21 21:36:41 +00009330 (ctxt->input->cur[2] == 'D') &&
9331 (ctxt->input->cur[3] == 'O') &&
9332 (ctxt->input->cur[4] == 'C') &&
9333 (ctxt->input->cur[5] == 'T') &&
9334 (ctxt->input->cur[6] == 'Y') &&
9335 (ctxt->input->cur[7] == 'P') &&
Owen Taylor3473f882001-02-23 17:55:21 +00009336 (ctxt->input->cur[8] == 'E')) {
9337 if ((!terminate) &&
9338 (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
9339 goto done;
9340#ifdef DEBUG_PUSH
9341 xmlGenericError(xmlGenericErrorContext,
9342 "PP: Parsing internal subset\n");
9343#endif
9344 ctxt->inSubset = 1;
9345 xmlParseDocTypeDecl(ctxt);
9346 if (RAW == '[') {
9347 ctxt->instate = XML_PARSER_DTD;
9348#ifdef DEBUG_PUSH
9349 xmlGenericError(xmlGenericErrorContext,
9350 "PP: entering DTD\n");
9351#endif
9352 } else {
9353 /*
9354 * Create and update the external subset.
9355 */
9356 ctxt->inSubset = 2;
9357 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
9358 (ctxt->sax->externalSubset != NULL))
9359 ctxt->sax->externalSubset(ctxt->userData,
9360 ctxt->intSubName, ctxt->extSubSystem,
9361 ctxt->extSubURI);
9362 ctxt->inSubset = 0;
9363 ctxt->instate = XML_PARSER_PROLOG;
9364#ifdef DEBUG_PUSH
9365 xmlGenericError(xmlGenericErrorContext,
9366 "PP: entering PROLOG\n");
9367#endif
9368 }
9369 } else if ((cur == '<') && (next == '!') &&
9370 (avail < 9)) {
9371 goto done;
9372 } else {
9373 ctxt->instate = XML_PARSER_START_TAG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009374 ctxt->progressive = 1;
9375 xmlParseGetLasts(ctxt, &lastlt, &lastgt);
Owen Taylor3473f882001-02-23 17:55:21 +00009376#ifdef DEBUG_PUSH
9377 xmlGenericError(xmlGenericErrorContext,
9378 "PP: entering START_TAG\n");
9379#endif
9380 }
9381 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009382 case XML_PARSER_PROLOG:
9383 SKIP_BLANKS;
9384 if (ctxt->input->buf == NULL)
9385 avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base);
9386 else
9387 avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base);
9388 if (avail < 2)
9389 goto done;
9390 cur = ctxt->input->cur[0];
9391 next = ctxt->input->cur[1];
9392 if ((cur == '<') && (next == '?')) {
9393 if ((!terminate) &&
9394 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9395 goto done;
9396#ifdef DEBUG_PUSH
9397 xmlGenericError(xmlGenericErrorContext,
9398 "PP: Parsing PI\n");
9399#endif
9400 xmlParsePI(ctxt);
9401 } else if ((cur == '<') && (next == '!') &&
9402 (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) {
9403 if ((!terminate) &&
9404 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9405 goto done;
9406#ifdef DEBUG_PUSH
9407 xmlGenericError(xmlGenericErrorContext,
9408 "PP: Parsing Comment\n");
9409#endif
9410 xmlParseComment(ctxt);
9411 ctxt->instate = XML_PARSER_PROLOG;
9412 } else if ((cur == '<') && (next == '!') &&
9413 (avail < 4)) {
9414 goto done;
9415 } else {
9416 ctxt->instate = XML_PARSER_START_TAG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009417 ctxt->progressive = 1;
9418 xmlParseGetLasts(ctxt, &lastlt, &lastgt);
Owen Taylor3473f882001-02-23 17:55:21 +00009419#ifdef DEBUG_PUSH
9420 xmlGenericError(xmlGenericErrorContext,
9421 "PP: entering START_TAG\n");
9422#endif
9423 }
9424 break;
9425 case XML_PARSER_EPILOG:
9426 SKIP_BLANKS;
9427 if (ctxt->input->buf == NULL)
9428 avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base);
9429 else
9430 avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base);
9431 if (avail < 2)
9432 goto done;
9433 cur = ctxt->input->cur[0];
9434 next = ctxt->input->cur[1];
9435 if ((cur == '<') && (next == '?')) {
9436 if ((!terminate) &&
9437 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9438 goto done;
9439#ifdef DEBUG_PUSH
9440 xmlGenericError(xmlGenericErrorContext,
9441 "PP: Parsing PI\n");
9442#endif
9443 xmlParsePI(ctxt);
9444 ctxt->instate = XML_PARSER_EPILOG;
9445 } else if ((cur == '<') && (next == '!') &&
9446 (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) {
9447 if ((!terminate) &&
9448 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9449 goto done;
9450#ifdef DEBUG_PUSH
9451 xmlGenericError(xmlGenericErrorContext,
9452 "PP: Parsing Comment\n");
9453#endif
9454 xmlParseComment(ctxt);
9455 ctxt->instate = XML_PARSER_EPILOG;
9456 } else if ((cur == '<') && (next == '!') &&
9457 (avail < 4)) {
9458 goto done;
9459 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009460 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00009461 ctxt->instate = XML_PARSER_EOF;
9462#ifdef DEBUG_PUSH
9463 xmlGenericError(xmlGenericErrorContext,
9464 "PP: entering EOF\n");
9465#endif
Daniel Veillard8d24cc12002-03-05 15:41:29 +00009466 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00009467 ctxt->sax->endDocument(ctxt->userData);
9468 goto done;
9469 }
9470 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009471 case XML_PARSER_DTD: {
9472 /*
9473 * Sorry but progressive parsing of the internal subset
9474 * is not expected to be supported. We first check that
9475 * the full content of the internal subset is available and
9476 * the parsing is launched only at that point.
9477 * Internal subset ends up with "']' S? '>'" in an unescaped
9478 * section and not in a ']]>' sequence which are conditional
9479 * sections (whoever argued to keep that crap in XML deserve
9480 * a place in hell !).
9481 */
9482 int base, i;
9483 xmlChar *buf;
9484 xmlChar quote = 0;
9485
9486 base = ctxt->input->cur - ctxt->input->base;
9487 if (base < 0) return(0);
9488 if (ctxt->checkIndex > base)
9489 base = ctxt->checkIndex;
9490 buf = ctxt->input->buf->buffer->content;
9491 for (;(unsigned int) base < ctxt->input->buf->buffer->use;
9492 base++) {
9493 if (quote != 0) {
9494 if (buf[base] == quote)
9495 quote = 0;
9496 continue;
9497 }
Daniel Veillard036143b2004-02-12 11:57:52 +00009498 if ((quote == 0) && (buf[base] == '<')) {
9499 int found = 0;
9500 /* special handling of comments */
9501 if (((unsigned int) base + 4 <
9502 ctxt->input->buf->buffer->use) &&
9503 (buf[base + 1] == '!') &&
9504 (buf[base + 2] == '-') &&
9505 (buf[base + 3] == '-')) {
9506 for (;(unsigned int) base + 3 <
9507 ctxt->input->buf->buffer->use; base++) {
9508 if ((buf[base] == '-') &&
9509 (buf[base + 1] == '-') &&
9510 (buf[base + 2] == '>')) {
9511 found = 1;
9512 base += 2;
9513 break;
9514 }
9515 }
9516 if (!found)
9517 break;
9518 continue;
9519 }
9520 }
Owen Taylor3473f882001-02-23 17:55:21 +00009521 if (buf[base] == '"') {
9522 quote = '"';
9523 continue;
9524 }
9525 if (buf[base] == '\'') {
9526 quote = '\'';
9527 continue;
9528 }
9529 if (buf[base] == ']') {
9530 if ((unsigned int) base +1 >=
9531 ctxt->input->buf->buffer->use)
9532 break;
9533 if (buf[base + 1] == ']') {
9534 /* conditional crap, skip both ']' ! */
9535 base++;
9536 continue;
9537 }
9538 for (i = 0;
9539 (unsigned int) base + i < ctxt->input->buf->buffer->use;
9540 i++) {
9541 if (buf[base + i] == '>')
9542 goto found_end_int_subset;
9543 }
9544 break;
9545 }
9546 }
9547 /*
9548 * We didn't found the end of the Internal subset
9549 */
9550 if (quote == 0)
9551 ctxt->checkIndex = base;
9552#ifdef DEBUG_PUSH
9553 if (next == 0)
9554 xmlGenericError(xmlGenericErrorContext,
9555 "PP: lookup of int subset end filed\n");
9556#endif
9557 goto done;
9558
9559found_end_int_subset:
9560 xmlParseInternalSubset(ctxt);
9561 ctxt->inSubset = 2;
9562 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
9563 (ctxt->sax->externalSubset != NULL))
9564 ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
9565 ctxt->extSubSystem, ctxt->extSubURI);
9566 ctxt->inSubset = 0;
9567 ctxt->instate = XML_PARSER_PROLOG;
9568 ctxt->checkIndex = 0;
9569#ifdef DEBUG_PUSH
9570 xmlGenericError(xmlGenericErrorContext,
9571 "PP: entering PROLOG\n");
9572#endif
9573 break;
9574 }
9575 case XML_PARSER_COMMENT:
9576 xmlGenericError(xmlGenericErrorContext,
9577 "PP: internal error, state == COMMENT\n");
9578 ctxt->instate = XML_PARSER_CONTENT;
9579#ifdef DEBUG_PUSH
9580 xmlGenericError(xmlGenericErrorContext,
9581 "PP: entering CONTENT\n");
9582#endif
9583 break;
Daniel Veillarda880b122003-04-21 21:36:41 +00009584 case XML_PARSER_IGNORE:
9585 xmlGenericError(xmlGenericErrorContext,
9586 "PP: internal error, state == IGNORE");
9587 ctxt->instate = XML_PARSER_DTD;
9588#ifdef DEBUG_PUSH
9589 xmlGenericError(xmlGenericErrorContext,
9590 "PP: entering DTD\n");
9591#endif
9592 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009593 case XML_PARSER_PI:
9594 xmlGenericError(xmlGenericErrorContext,
9595 "PP: internal error, state == PI\n");
9596 ctxt->instate = XML_PARSER_CONTENT;
9597#ifdef DEBUG_PUSH
9598 xmlGenericError(xmlGenericErrorContext,
9599 "PP: entering CONTENT\n");
9600#endif
9601 break;
9602 case XML_PARSER_ENTITY_DECL:
9603 xmlGenericError(xmlGenericErrorContext,
9604 "PP: internal error, state == ENTITY_DECL\n");
9605 ctxt->instate = XML_PARSER_DTD;
9606#ifdef DEBUG_PUSH
9607 xmlGenericError(xmlGenericErrorContext,
9608 "PP: entering DTD\n");
9609#endif
9610 break;
9611 case XML_PARSER_ENTITY_VALUE:
9612 xmlGenericError(xmlGenericErrorContext,
9613 "PP: internal error, state == ENTITY_VALUE\n");
9614 ctxt->instate = XML_PARSER_CONTENT;
9615#ifdef DEBUG_PUSH
9616 xmlGenericError(xmlGenericErrorContext,
9617 "PP: entering DTD\n");
9618#endif
9619 break;
9620 case XML_PARSER_ATTRIBUTE_VALUE:
9621 xmlGenericError(xmlGenericErrorContext,
9622 "PP: internal error, state == ATTRIBUTE_VALUE\n");
9623 ctxt->instate = XML_PARSER_START_TAG;
9624#ifdef DEBUG_PUSH
9625 xmlGenericError(xmlGenericErrorContext,
9626 "PP: entering START_TAG\n");
9627#endif
9628 break;
9629 case XML_PARSER_SYSTEM_LITERAL:
9630 xmlGenericError(xmlGenericErrorContext,
9631 "PP: internal error, state == SYSTEM_LITERAL\n");
9632 ctxt->instate = XML_PARSER_START_TAG;
9633#ifdef DEBUG_PUSH
9634 xmlGenericError(xmlGenericErrorContext,
9635 "PP: entering START_TAG\n");
9636#endif
9637 break;
Daniel Veillard4a7ae502002-02-18 19:18:17 +00009638 case XML_PARSER_PUBLIC_LITERAL:
9639 xmlGenericError(xmlGenericErrorContext,
9640 "PP: internal error, state == PUBLIC_LITERAL\n");
9641 ctxt->instate = XML_PARSER_START_TAG;
9642#ifdef DEBUG_PUSH
9643 xmlGenericError(xmlGenericErrorContext,
9644 "PP: entering START_TAG\n");
9645#endif
9646 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009647 }
9648 }
9649done:
9650#ifdef DEBUG_PUSH
9651 xmlGenericError(xmlGenericErrorContext, "PP: done %d\n", ret);
9652#endif
9653 return(ret);
9654}
9655
9656/**
Owen Taylor3473f882001-02-23 17:55:21 +00009657 * xmlParseChunk:
9658 * @ctxt: an XML parser context
9659 * @chunk: an char array
9660 * @size: the size in byte of the chunk
9661 * @terminate: last chunk indicator
9662 *
9663 * Parse a Chunk of memory
9664 *
9665 * Returns zero if no error, the xmlParserErrors otherwise.
9666 */
9667int
9668xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size,
9669 int terminate) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009670 if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
9671 return(ctxt->errNo);
Daniel Veillard309f81d2003-09-23 09:02:53 +00009672 if (ctxt->instate == XML_PARSER_START)
9673 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00009674 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
9675 (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) {
9676 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
9677 int cur = ctxt->input->cur - ctxt->input->base;
9678
9679 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
9680 ctxt->input->base = ctxt->input->buf->buffer->content + base;
9681 ctxt->input->cur = ctxt->input->base + cur;
Daniel Veillard48b2f892001-02-25 16:11:03 +00009682 ctxt->input->end =
9683 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00009684#ifdef DEBUG_PUSH
9685 xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
9686#endif
9687
Owen Taylor3473f882001-02-23 17:55:21 +00009688 } else if (ctxt->instate != XML_PARSER_EOF) {
9689 if ((ctxt->input != NULL) && ctxt->input->buf != NULL) {
9690 xmlParserInputBufferPtr in = ctxt->input->buf;
9691 if ((in->encoder != NULL) && (in->buffer != NULL) &&
9692 (in->raw != NULL)) {
9693 int nbchars;
9694
9695 nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
9696 if (nbchars < 0) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00009697 /* TODO 2.6.0 */
Owen Taylor3473f882001-02-23 17:55:21 +00009698 xmlGenericError(xmlGenericErrorContext,
9699 "xmlParseChunk: encoder error\n");
9700 return(XML_ERR_INVALID_ENCODING);
9701 }
9702 }
9703 }
9704 }
9705 xmlParseTryOrFinish(ctxt, terminate);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009706 if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
9707 return(ctxt->errNo);
Owen Taylor3473f882001-02-23 17:55:21 +00009708 if (terminate) {
9709 /*
9710 * Check for termination
9711 */
Daniel Veillard819d5cb2002-10-14 11:15:18 +00009712 int avail = 0;
9713 if (ctxt->input->buf == NULL)
9714 avail = ctxt->input->length -
9715 (ctxt->input->cur - ctxt->input->base);
9716 else
9717 avail = ctxt->input->buf->buffer->use -
9718 (ctxt->input->cur - ctxt->input->base);
9719
Owen Taylor3473f882001-02-23 17:55:21 +00009720 if ((ctxt->instate != XML_PARSER_EOF) &&
9721 (ctxt->instate != XML_PARSER_EPILOG)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009722 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00009723 }
Daniel Veillard819d5cb2002-10-14 11:15:18 +00009724 if ((ctxt->instate == XML_PARSER_EPILOG) && (avail > 0)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009725 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Daniel Veillard819d5cb2002-10-14 11:15:18 +00009726 }
Owen Taylor3473f882001-02-23 17:55:21 +00009727 if (ctxt->instate != XML_PARSER_EOF) {
Daniel Veillard8d24cc12002-03-05 15:41:29 +00009728 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00009729 ctxt->sax->endDocument(ctxt->userData);
9730 }
9731 ctxt->instate = XML_PARSER_EOF;
9732 }
9733 return((xmlParserErrors) ctxt->errNo);
9734}
9735
9736/************************************************************************
9737 * *
9738 * I/O front end functions to the parser *
9739 * *
9740 ************************************************************************/
9741
9742/**
9743 * xmlStopParser:
9744 * @ctxt: an XML parser context
9745 *
9746 * Blocks further parser processing
9747 */
9748void
9749xmlStopParser(xmlParserCtxtPtr ctxt) {
Daniel Veillard157fee02003-10-31 10:36:03 +00009750 if (ctxt == NULL)
9751 return;
Owen Taylor3473f882001-02-23 17:55:21 +00009752 ctxt->instate = XML_PARSER_EOF;
Daniel Veillard157fee02003-10-31 10:36:03 +00009753 ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00009754 if (ctxt->input != NULL)
9755 ctxt->input->cur = BAD_CAST"";
9756}
9757
9758/**
9759 * xmlCreatePushParserCtxt:
9760 * @sax: a SAX handler
9761 * @user_data: The user data returned on SAX callbacks
9762 * @chunk: a pointer to an array of chars
9763 * @size: number of chars in the array
9764 * @filename: an optional file name or URI
9765 *
Daniel Veillard176d99f2002-07-06 19:22:28 +00009766 * Create a parser context for using the XML parser in push mode.
9767 * If @buffer and @size are non-NULL, the data is used to detect
9768 * the encoding. The remaining characters will be parsed so they
9769 * don't need to be fed in again through xmlParseChunk.
Owen Taylor3473f882001-02-23 17:55:21 +00009770 * To allow content encoding detection, @size should be >= 4
9771 * The value of @filename is used for fetching external entities
9772 * and error/warning reports.
9773 *
9774 * Returns the new parser context or NULL
9775 */
Daniel Veillard176d99f2002-07-06 19:22:28 +00009776
Owen Taylor3473f882001-02-23 17:55:21 +00009777xmlParserCtxtPtr
9778xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
9779 const char *chunk, int size, const char *filename) {
9780 xmlParserCtxtPtr ctxt;
9781 xmlParserInputPtr inputStream;
9782 xmlParserInputBufferPtr buf;
9783 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
9784
9785 /*
9786 * plug some encoding conversion routines
9787 */
9788 if ((chunk != NULL) && (size >= 4))
9789 enc = xmlDetectCharEncoding((const xmlChar *) chunk, size);
9790
9791 buf = xmlAllocParserInputBuffer(enc);
9792 if (buf == NULL) return(NULL);
9793
9794 ctxt = xmlNewParserCtxt();
9795 if (ctxt == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00009796 xmlErrMemory(NULL, "creating parser: out of memory\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009797 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00009798 return(NULL);
9799 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009800 ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * sizeof(xmlChar *));
9801 if (ctxt->pushTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009802 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00009803 xmlFreeParserInputBuffer(buf);
9804 xmlFreeParserCtxt(ctxt);
9805 return(NULL);
9806 }
Owen Taylor3473f882001-02-23 17:55:21 +00009807 if (sax != NULL) {
Daniel Veillard81273902003-09-30 00:43:48 +00009808#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00009809 if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
Daniel Veillard81273902003-09-30 00:43:48 +00009810#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00009811 xmlFree(ctxt->sax);
9812 ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler));
9813 if (ctxt->sax == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009814 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009815 xmlFreeParserInputBuffer(buf);
9816 xmlFreeParserCtxt(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00009817 return(NULL);
9818 }
9819 memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler));
9820 if (user_data != NULL)
9821 ctxt->userData = user_data;
9822 }
9823 if (filename == NULL) {
9824 ctxt->directory = NULL;
9825 } else {
9826 ctxt->directory = xmlParserGetDirectory(filename);
9827 }
9828
9829 inputStream = xmlNewInputStream(ctxt);
9830 if (inputStream == NULL) {
9831 xmlFreeParserCtxt(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009832 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00009833 return(NULL);
9834 }
9835
9836 if (filename == NULL)
9837 inputStream->filename = NULL;
9838 else
Daniel Veillardf4862f02002-09-10 11:13:43 +00009839 inputStream->filename = (char *)
Igor Zlatkovic5f9fada2003-02-19 14:51:00 +00009840 xmlCanonicPath((const xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00009841 inputStream->buf = buf;
9842 inputStream->base = inputStream->buf->buffer->content;
9843 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00009844 inputStream->end =
9845 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00009846
9847 inputPush(ctxt, inputStream);
9848
9849 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
9850 (ctxt->input->buf != NULL)) {
Daniel Veillardaa39a0f2002-01-06 12:47:22 +00009851 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
9852 int cur = ctxt->input->cur - ctxt->input->base;
9853
Owen Taylor3473f882001-02-23 17:55:21 +00009854 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
Daniel Veillardaa39a0f2002-01-06 12:47:22 +00009855
9856 ctxt->input->base = ctxt->input->buf->buffer->content + base;
9857 ctxt->input->cur = ctxt->input->base + cur;
9858 ctxt->input->end =
9859 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00009860#ifdef DEBUG_PUSH
9861 xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
9862#endif
9863 }
9864
Daniel Veillard0e4cd172001-06-28 12:13:56 +00009865 if (enc != XML_CHAR_ENCODING_NONE) {
9866 xmlSwitchEncoding(ctxt, enc);
9867 }
9868
Owen Taylor3473f882001-02-23 17:55:21 +00009869 return(ctxt);
9870}
Daniel Veillard73b013f2003-09-30 12:36:01 +00009871#endif /* LIBXML_PUSH_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00009872
9873/**
9874 * xmlCreateIOParserCtxt:
9875 * @sax: a SAX handler
9876 * @user_data: The user data returned on SAX callbacks
9877 * @ioread: an I/O read function
9878 * @ioclose: an I/O close function
9879 * @ioctx: an I/O handler
9880 * @enc: the charset encoding if known
9881 *
9882 * Create a parser context for using the XML parser with an existing
9883 * I/O stream
9884 *
9885 * Returns the new parser context or NULL
9886 */
9887xmlParserCtxtPtr
9888xmlCreateIOParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
9889 xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
9890 void *ioctx, xmlCharEncoding enc) {
9891 xmlParserCtxtPtr ctxt;
9892 xmlParserInputPtr inputStream;
9893 xmlParserInputBufferPtr buf;
9894
9895 buf = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, enc);
9896 if (buf == NULL) return(NULL);
9897
9898 ctxt = xmlNewParserCtxt();
9899 if (ctxt == NULL) {
9900 xmlFree(buf);
9901 return(NULL);
9902 }
9903 if (sax != NULL) {
Daniel Veillard81273902003-09-30 00:43:48 +00009904#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00009905 if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
Daniel Veillard81273902003-09-30 00:43:48 +00009906#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00009907 xmlFree(ctxt->sax);
9908 ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler));
9909 if (ctxt->sax == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009910 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00009911 xmlFree(ctxt);
9912 return(NULL);
9913 }
9914 memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler));
9915 if (user_data != NULL)
9916 ctxt->userData = user_data;
9917 }
9918
9919 inputStream = xmlNewIOInputStream(ctxt, buf, enc);
9920 if (inputStream == NULL) {
9921 xmlFreeParserCtxt(ctxt);
9922 return(NULL);
9923 }
9924 inputPush(ctxt, inputStream);
9925
9926 return(ctxt);
9927}
9928
Daniel Veillard4432df22003-09-28 18:58:27 +00009929#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00009930/************************************************************************
9931 * *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00009932 * Front ends when parsing a DTD *
Owen Taylor3473f882001-02-23 17:55:21 +00009933 * *
9934 ************************************************************************/
9935
9936/**
9937 * xmlIOParseDTD:
9938 * @sax: the SAX handler block or NULL
9939 * @input: an Input Buffer
9940 * @enc: the charset encoding if known
9941 *
9942 * Load and parse a DTD
9943 *
9944 * Returns the resulting xmlDtdPtr or NULL in case of error.
9945 * @input will be freed at parsing end.
9946 */
9947
9948xmlDtdPtr
9949xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input,
9950 xmlCharEncoding enc) {
9951 xmlDtdPtr ret = NULL;
9952 xmlParserCtxtPtr ctxt;
9953 xmlParserInputPtr pinput = NULL;
Daniel Veillard87a764e2001-06-20 17:41:10 +00009954 xmlChar start[4];
Owen Taylor3473f882001-02-23 17:55:21 +00009955
9956 if (input == NULL)
9957 return(NULL);
9958
9959 ctxt = xmlNewParserCtxt();
9960 if (ctxt == NULL) {
9961 return(NULL);
9962 }
9963
9964 /*
9965 * Set-up the SAX context
9966 */
9967 if (sax != NULL) {
9968 if (ctxt->sax != NULL)
9969 xmlFree(ctxt->sax);
9970 ctxt->sax = sax;
Daniel Veillard500a1de2004-03-22 15:22:58 +00009971 ctxt->userData = ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00009972 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009973 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00009974
9975 /*
9976 * generate a parser input from the I/O handler
9977 */
9978
Daniel Veillard43caefb2003-12-07 19:32:22 +00009979 pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
Owen Taylor3473f882001-02-23 17:55:21 +00009980 if (pinput == NULL) {
9981 if (sax != NULL) ctxt->sax = NULL;
9982 xmlFreeParserCtxt(ctxt);
9983 return(NULL);
9984 }
9985
9986 /*
9987 * plug some encoding conversion routines here.
9988 */
9989 xmlPushInput(ctxt, pinput);
Daniel Veillard43caefb2003-12-07 19:32:22 +00009990 if (enc != XML_CHAR_ENCODING_NONE) {
9991 xmlSwitchEncoding(ctxt, enc);
9992 }
Owen Taylor3473f882001-02-23 17:55:21 +00009993
9994 pinput->filename = NULL;
9995 pinput->line = 1;
9996 pinput->col = 1;
9997 pinput->base = ctxt->input->cur;
9998 pinput->cur = ctxt->input->cur;
9999 pinput->free = NULL;
10000
10001 /*
10002 * let's parse that entity knowing it's an external subset.
10003 */
10004 ctxt->inSubset = 2;
10005 ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
10006 ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none",
10007 BAD_CAST "none", BAD_CAST "none");
Daniel Veillard87a764e2001-06-20 17:41:10 +000010008
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010009 if ((enc == XML_CHAR_ENCODING_NONE) &&
10010 ((ctxt->input->end - ctxt->input->cur) >= 4)) {
Daniel Veillard87a764e2001-06-20 17:41:10 +000010011 /*
10012 * Get the 4 first bytes and decode the charset
10013 * if enc != XML_CHAR_ENCODING_NONE
10014 * plug some encoding conversion routines.
10015 */
10016 start[0] = RAW;
10017 start[1] = NXT(1);
10018 start[2] = NXT(2);
10019 start[3] = NXT(3);
10020 enc = xmlDetectCharEncoding(start, 4);
10021 if (enc != XML_CHAR_ENCODING_NONE) {
10022 xmlSwitchEncoding(ctxt, enc);
10023 }
10024 }
10025
Owen Taylor3473f882001-02-23 17:55:21 +000010026 xmlParseExternalSubset(ctxt, BAD_CAST "none", BAD_CAST "none");
10027
10028 if (ctxt->myDoc != NULL) {
10029 if (ctxt->wellFormed) {
10030 ret = ctxt->myDoc->extSubset;
10031 ctxt->myDoc->extSubset = NULL;
Daniel Veillard329456a2003-04-26 21:21:00 +000010032 if (ret != NULL) {
10033 xmlNodePtr tmp;
10034
10035 ret->doc = NULL;
10036 tmp = ret->children;
10037 while (tmp != NULL) {
10038 tmp->doc = NULL;
10039 tmp = tmp->next;
10040 }
10041 }
Owen Taylor3473f882001-02-23 17:55:21 +000010042 } else {
10043 ret = NULL;
10044 }
10045 xmlFreeDoc(ctxt->myDoc);
10046 ctxt->myDoc = NULL;
10047 }
10048 if (sax != NULL) ctxt->sax = NULL;
10049 xmlFreeParserCtxt(ctxt);
10050
10051 return(ret);
10052}
10053
10054/**
10055 * xmlSAXParseDTD:
10056 * @sax: the SAX handler block
10057 * @ExternalID: a NAME* containing the External ID of the DTD
10058 * @SystemID: a NAME* containing the URL to the DTD
10059 *
10060 * Load and parse an external subset.
10061 *
10062 * Returns the resulting xmlDtdPtr or NULL in case of error.
10063 */
10064
10065xmlDtdPtr
10066xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID,
10067 const xmlChar *SystemID) {
10068 xmlDtdPtr ret = NULL;
10069 xmlParserCtxtPtr ctxt;
10070 xmlParserInputPtr input = NULL;
10071 xmlCharEncoding enc;
10072
10073 if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL);
10074
10075 ctxt = xmlNewParserCtxt();
10076 if (ctxt == NULL) {
10077 return(NULL);
10078 }
10079
10080 /*
10081 * Set-up the SAX context
10082 */
10083 if (sax != NULL) {
10084 if (ctxt->sax != NULL)
10085 xmlFree(ctxt->sax);
10086 ctxt->sax = sax;
Daniel Veillardbf1e3d82003-08-14 23:57:26 +000010087 ctxt->userData = ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +000010088 }
10089
10090 /*
10091 * Ask the Entity resolver to load the damn thing
10092 */
10093
10094 if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
Daniel Veillardc6abc3d2003-04-26 13:27:30 +000010095 input = ctxt->sax->resolveEntity(ctxt, ExternalID, SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +000010096 if (input == NULL) {
10097 if (sax != NULL) ctxt->sax = NULL;
10098 xmlFreeParserCtxt(ctxt);
10099 return(NULL);
10100 }
10101
10102 /*
10103 * plug some encoding conversion routines here.
10104 */
10105 xmlPushInput(ctxt, input);
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010106 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
10107 enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
10108 xmlSwitchEncoding(ctxt, enc);
10109 }
Owen Taylor3473f882001-02-23 17:55:21 +000010110
10111 if (input->filename == NULL)
Daniel Veillard85095e22003-04-23 13:56:44 +000010112 input->filename = (char *) xmlCanonicPath(SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +000010113 input->line = 1;
10114 input->col = 1;
10115 input->base = ctxt->input->cur;
10116 input->cur = ctxt->input->cur;
10117 input->free = NULL;
10118
10119 /*
10120 * let's parse that entity knowing it's an external subset.
10121 */
10122 ctxt->inSubset = 2;
10123 ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
10124 ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none",
10125 ExternalID, SystemID);
10126 xmlParseExternalSubset(ctxt, ExternalID, SystemID);
10127
10128 if (ctxt->myDoc != NULL) {
10129 if (ctxt->wellFormed) {
10130 ret = ctxt->myDoc->extSubset;
10131 ctxt->myDoc->extSubset = NULL;
Daniel Veillardc5573462003-04-25 16:43:49 +000010132 if (ret != NULL) {
10133 xmlNodePtr tmp;
10134
10135 ret->doc = NULL;
10136 tmp = ret->children;
10137 while (tmp != NULL) {
10138 tmp->doc = NULL;
10139 tmp = tmp->next;
10140 }
10141 }
Owen Taylor3473f882001-02-23 17:55:21 +000010142 } else {
10143 ret = NULL;
10144 }
10145 xmlFreeDoc(ctxt->myDoc);
10146 ctxt->myDoc = NULL;
10147 }
10148 if (sax != NULL) ctxt->sax = NULL;
10149 xmlFreeParserCtxt(ctxt);
10150
10151 return(ret);
10152}
10153
Daniel Veillard4432df22003-09-28 18:58:27 +000010154
Owen Taylor3473f882001-02-23 17:55:21 +000010155/**
10156 * xmlParseDTD:
10157 * @ExternalID: a NAME* containing the External ID of the DTD
10158 * @SystemID: a NAME* containing the URL to the DTD
10159 *
10160 * Load and parse an external subset.
10161 *
10162 * Returns the resulting xmlDtdPtr or NULL in case of error.
10163 */
10164
10165xmlDtdPtr
10166xmlParseDTD(const xmlChar *ExternalID, const xmlChar *SystemID) {
10167 return(xmlSAXParseDTD(NULL, ExternalID, SystemID));
10168}
Daniel Veillard4432df22003-09-28 18:58:27 +000010169#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000010170
10171/************************************************************************
10172 * *
10173 * Front ends when parsing an Entity *
10174 * *
10175 ************************************************************************/
10176
10177/**
Owen Taylor3473f882001-02-23 17:55:21 +000010178 * xmlParseCtxtExternalEntity:
10179 * @ctx: the existing parsing context
10180 * @URL: the URL for the entity to load
10181 * @ID: the System ID for the entity to load
Daniel Veillardcda96922001-08-21 10:56:31 +000010182 * @lst: the return value for the set of parsed nodes
Owen Taylor3473f882001-02-23 17:55:21 +000010183 *
10184 * Parse an external general entity within an existing parsing context
10185 * An external general parsed entity is well-formed if it matches the
10186 * production labeled extParsedEnt.
10187 *
10188 * [78] extParsedEnt ::= TextDecl? content
10189 *
10190 * Returns 0 if the entity is well formed, -1 in case of args problem and
10191 * the parser error code otherwise
10192 */
10193
10194int
10195xmlParseCtxtExternalEntity(xmlParserCtxtPtr ctx, const xmlChar *URL,
Daniel Veillardcda96922001-08-21 10:56:31 +000010196 const xmlChar *ID, xmlNodePtr *lst) {
Owen Taylor3473f882001-02-23 17:55:21 +000010197 xmlParserCtxtPtr ctxt;
10198 xmlDocPtr newDoc;
10199 xmlSAXHandlerPtr oldsax = NULL;
10200 int ret = 0;
Daniel Veillard87a764e2001-06-20 17:41:10 +000010201 xmlChar start[4];
10202 xmlCharEncoding enc;
Owen Taylor3473f882001-02-23 17:55:21 +000010203
10204 if (ctx->depth > 40) {
10205 return(XML_ERR_ENTITY_LOOP);
10206 }
10207
Daniel Veillardcda96922001-08-21 10:56:31 +000010208 if (lst != NULL)
10209 *lst = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010210 if ((URL == NULL) && (ID == NULL))
10211 return(-1);
10212 if (ctx->myDoc == NULL) /* @@ relax but check for dereferences */
10213 return(-1);
10214
10215
10216 ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL);
10217 if (ctxt == NULL) return(-1);
10218 ctxt->userData = ctxt;
Daniel Veillarde2830f12003-01-08 17:47:49 +000010219 ctxt->_private = ctx->_private;
Owen Taylor3473f882001-02-23 17:55:21 +000010220 oldsax = ctxt->sax;
10221 ctxt->sax = ctx->sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000010222 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010223 newDoc = xmlNewDoc(BAD_CAST "1.0");
10224 if (newDoc == NULL) {
10225 xmlFreeParserCtxt(ctxt);
10226 return(-1);
10227 }
10228 if (ctx->myDoc != NULL) {
10229 newDoc->intSubset = ctx->myDoc->intSubset;
10230 newDoc->extSubset = ctx->myDoc->extSubset;
10231 }
10232 if (ctx->myDoc->URL != NULL) {
10233 newDoc->URL = xmlStrdup(ctx->myDoc->URL);
10234 }
10235 newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
10236 if (newDoc->children == NULL) {
10237 ctxt->sax = oldsax;
10238 xmlFreeParserCtxt(ctxt);
10239 newDoc->intSubset = NULL;
10240 newDoc->extSubset = NULL;
10241 xmlFreeDoc(newDoc);
10242 return(-1);
10243 }
10244 nodePush(ctxt, newDoc->children);
10245 if (ctx->myDoc == NULL) {
10246 ctxt->myDoc = newDoc;
10247 } else {
10248 ctxt->myDoc = ctx->myDoc;
10249 newDoc->children->doc = ctx->myDoc;
10250 }
10251
Daniel Veillard87a764e2001-06-20 17:41:10 +000010252 /*
10253 * Get the 4 first bytes and decode the charset
10254 * if enc != XML_CHAR_ENCODING_NONE
10255 * plug some encoding conversion routines.
10256 */
10257 GROW
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010258 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
10259 start[0] = RAW;
10260 start[1] = NXT(1);
10261 start[2] = NXT(2);
10262 start[3] = NXT(3);
10263 enc = xmlDetectCharEncoding(start, 4);
10264 if (enc != XML_CHAR_ENCODING_NONE) {
10265 xmlSwitchEncoding(ctxt, enc);
10266 }
Daniel Veillard87a764e2001-06-20 17:41:10 +000010267 }
10268
Owen Taylor3473f882001-02-23 17:55:21 +000010269 /*
10270 * Parse a possible text declaration first
10271 */
Daniel Veillarda07050d2003-10-19 14:46:32 +000010272 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +000010273 xmlParseTextDecl(ctxt);
10274 }
10275
10276 /*
10277 * Doing validity checking on chunk doesn't make sense
10278 */
10279 ctxt->instate = XML_PARSER_CONTENT;
10280 ctxt->validate = ctx->validate;
Daniel Veillard5f8d1a32003-03-23 21:02:00 +000010281 ctxt->valid = ctx->valid;
Owen Taylor3473f882001-02-23 17:55:21 +000010282 ctxt->loadsubset = ctx->loadsubset;
10283 ctxt->depth = ctx->depth + 1;
10284 ctxt->replaceEntities = ctx->replaceEntities;
10285 if (ctxt->validate) {
10286 ctxt->vctxt.error = ctx->vctxt.error;
10287 ctxt->vctxt.warning = ctx->vctxt.warning;
Owen Taylor3473f882001-02-23 17:55:21 +000010288 } else {
10289 ctxt->vctxt.error = NULL;
10290 ctxt->vctxt.warning = NULL;
10291 }
Daniel Veillarda9142e72001-06-19 11:07:54 +000010292 ctxt->vctxt.nodeTab = NULL;
10293 ctxt->vctxt.nodeNr = 0;
10294 ctxt->vctxt.nodeMax = 0;
10295 ctxt->vctxt.node = NULL;
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000010296 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
10297 ctxt->dict = ctx->dict;
Daniel Veillardfd343dc2003-10-31 10:55:22 +000010298 ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
10299 ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
10300 ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000010301 ctxt->dictNames = ctx->dictNames;
10302 ctxt->attsDefault = ctx->attsDefault;
10303 ctxt->attsSpecial = ctx->attsSpecial;
Owen Taylor3473f882001-02-23 17:55:21 +000010304
10305 xmlParseContent(ctxt);
10306
Daniel Veillard5f8d1a32003-03-23 21:02:00 +000010307 ctx->validate = ctxt->validate;
10308 ctx->valid = ctxt->valid;
Owen Taylor3473f882001-02-23 17:55:21 +000010309 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010310 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010311 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010312 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010313 }
10314 if (ctxt->node != newDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010315 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010316 }
10317
10318 if (!ctxt->wellFormed) {
10319 if (ctxt->errNo == 0)
10320 ret = 1;
10321 else
10322 ret = ctxt->errNo;
10323 } else {
Daniel Veillardcda96922001-08-21 10:56:31 +000010324 if (lst != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +000010325 xmlNodePtr cur;
10326
10327 /*
10328 * Return the newly created nodeset after unlinking it from
10329 * they pseudo parent.
10330 */
10331 cur = newDoc->children->children;
Daniel Veillardcda96922001-08-21 10:56:31 +000010332 *lst = cur;
Owen Taylor3473f882001-02-23 17:55:21 +000010333 while (cur != NULL) {
10334 cur->parent = NULL;
10335 cur = cur->next;
10336 }
10337 newDoc->children->children = NULL;
10338 }
10339 ret = 0;
10340 }
10341 ctxt->sax = oldsax;
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000010342 ctxt->dict = NULL;
10343 ctxt->attsDefault = NULL;
10344 ctxt->attsSpecial = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010345 xmlFreeParserCtxt(ctxt);
10346 newDoc->intSubset = NULL;
10347 newDoc->extSubset = NULL;
10348 xmlFreeDoc(newDoc);
10349
10350 return(ret);
10351}
10352
10353/**
Daniel Veillard257d9102001-05-08 10:41:44 +000010354 * xmlParseExternalEntityPrivate:
Owen Taylor3473f882001-02-23 17:55:21 +000010355 * @doc: the document the chunk pertains to
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010356 * @oldctxt: the previous parser context if available
Owen Taylor3473f882001-02-23 17:55:21 +000010357 * @sax: the SAX handler bloc (possibly NULL)
10358 * @user_data: The user data returned on SAX callbacks (possibly NULL)
10359 * @depth: Used for loop detection, use 0
10360 * @URL: the URL for the entity to load
10361 * @ID: the System ID for the entity to load
10362 * @list: the return value for the set of parsed nodes
10363 *
Daniel Veillard257d9102001-05-08 10:41:44 +000010364 * Private version of xmlParseExternalEntity()
Owen Taylor3473f882001-02-23 17:55:21 +000010365 *
10366 * Returns 0 if the entity is well formed, -1 in case of args problem and
10367 * the parser error code otherwise
10368 */
10369
Daniel Veillard7d515752003-09-26 19:12:37 +000010370static xmlParserErrors
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010371xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt,
10372 xmlSAXHandlerPtr sax,
Daniel Veillard257d9102001-05-08 10:41:44 +000010373 void *user_data, int depth, const xmlChar *URL,
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010374 const xmlChar *ID, xmlNodePtr *list) {
Owen Taylor3473f882001-02-23 17:55:21 +000010375 xmlParserCtxtPtr ctxt;
10376 xmlDocPtr newDoc;
10377 xmlSAXHandlerPtr oldsax = NULL;
Daniel Veillard7d515752003-09-26 19:12:37 +000010378 xmlParserErrors ret = XML_ERR_OK;
Daniel Veillard87a764e2001-06-20 17:41:10 +000010379 xmlChar start[4];
10380 xmlCharEncoding enc;
Owen Taylor3473f882001-02-23 17:55:21 +000010381
10382 if (depth > 40) {
10383 return(XML_ERR_ENTITY_LOOP);
10384 }
10385
10386
10387
10388 if (list != NULL)
10389 *list = NULL;
10390 if ((URL == NULL) && (ID == NULL))
Daniel Veillard7d515752003-09-26 19:12:37 +000010391 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010392 if (doc == NULL) /* @@ relax but check for dereferences */
Daniel Veillard7d515752003-09-26 19:12:37 +000010393 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010394
10395
10396 ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL);
William M. Brackb670e2e2003-09-27 01:05:55 +000010397 if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY);
Owen Taylor3473f882001-02-23 17:55:21 +000010398 ctxt->userData = ctxt;
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010399 if (oldctxt != NULL) {
10400 ctxt->_private = oldctxt->_private;
10401 ctxt->loadsubset = oldctxt->loadsubset;
10402 ctxt->validate = oldctxt->validate;
10403 ctxt->external = oldctxt->external;
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010404 ctxt->record_info = oldctxt->record_info;
10405 ctxt->node_seq.maximum = oldctxt->node_seq.maximum;
10406 ctxt->node_seq.length = oldctxt->node_seq.length;
10407 ctxt->node_seq.buffer = oldctxt->node_seq.buffer;
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010408 } else {
10409 /*
10410 * Doing validity checking on chunk without context
10411 * doesn't make sense
10412 */
10413 ctxt->_private = NULL;
10414 ctxt->validate = 0;
10415 ctxt->external = 2;
10416 ctxt->loadsubset = 0;
10417 }
Owen Taylor3473f882001-02-23 17:55:21 +000010418 if (sax != NULL) {
10419 oldsax = ctxt->sax;
10420 ctxt->sax = sax;
10421 if (user_data != NULL)
10422 ctxt->userData = user_data;
10423 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000010424 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010425 newDoc = xmlNewDoc(BAD_CAST "1.0");
10426 if (newDoc == NULL) {
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010427 ctxt->node_seq.maximum = 0;
10428 ctxt->node_seq.length = 0;
10429 ctxt->node_seq.buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010430 xmlFreeParserCtxt(ctxt);
Daniel Veillard7d515752003-09-26 19:12:37 +000010431 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010432 }
10433 if (doc != NULL) {
10434 newDoc->intSubset = doc->intSubset;
10435 newDoc->extSubset = doc->extSubset;
10436 }
10437 if (doc->URL != NULL) {
10438 newDoc->URL = xmlStrdup(doc->URL);
10439 }
10440 newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
10441 if (newDoc->children == NULL) {
10442 if (sax != NULL)
10443 ctxt->sax = oldsax;
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010444 ctxt->node_seq.maximum = 0;
10445 ctxt->node_seq.length = 0;
10446 ctxt->node_seq.buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010447 xmlFreeParserCtxt(ctxt);
10448 newDoc->intSubset = NULL;
10449 newDoc->extSubset = NULL;
10450 xmlFreeDoc(newDoc);
Daniel Veillard7d515752003-09-26 19:12:37 +000010451 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010452 }
10453 nodePush(ctxt, newDoc->children);
10454 if (doc == NULL) {
10455 ctxt->myDoc = newDoc;
10456 } else {
10457 ctxt->myDoc = doc;
10458 newDoc->children->doc = doc;
10459 }
10460
Daniel Veillard87a764e2001-06-20 17:41:10 +000010461 /*
10462 * Get the 4 first bytes and decode the charset
10463 * if enc != XML_CHAR_ENCODING_NONE
10464 * plug some encoding conversion routines.
10465 */
10466 GROW;
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010467 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
10468 start[0] = RAW;
10469 start[1] = NXT(1);
10470 start[2] = NXT(2);
10471 start[3] = NXT(3);
10472 enc = xmlDetectCharEncoding(start, 4);
10473 if (enc != XML_CHAR_ENCODING_NONE) {
10474 xmlSwitchEncoding(ctxt, enc);
10475 }
Daniel Veillard87a764e2001-06-20 17:41:10 +000010476 }
10477
Owen Taylor3473f882001-02-23 17:55:21 +000010478 /*
10479 * Parse a possible text declaration first
10480 */
Daniel Veillarda07050d2003-10-19 14:46:32 +000010481 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +000010482 xmlParseTextDecl(ctxt);
10483 }
10484
Owen Taylor3473f882001-02-23 17:55:21 +000010485 ctxt->instate = XML_PARSER_CONTENT;
Owen Taylor3473f882001-02-23 17:55:21 +000010486 ctxt->depth = depth;
10487
10488 xmlParseContent(ctxt);
10489
Daniel Veillard561b7f82002-03-20 21:55:57 +000010490 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010491 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Daniel Veillard561b7f82002-03-20 21:55:57 +000010492 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010493 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010494 }
10495 if (ctxt->node != newDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010496 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010497 }
10498
10499 if (!ctxt->wellFormed) {
10500 if (ctxt->errNo == 0)
Daniel Veillard7d515752003-09-26 19:12:37 +000010501 ret = XML_ERR_INTERNAL_ERROR;
Owen Taylor3473f882001-02-23 17:55:21 +000010502 else
William M. Brack7b9154b2003-09-27 19:23:50 +000010503 ret = (xmlParserErrors)ctxt->errNo;
Owen Taylor3473f882001-02-23 17:55:21 +000010504 } else {
10505 if (list != NULL) {
10506 xmlNodePtr cur;
10507
10508 /*
10509 * Return the newly created nodeset after unlinking it from
10510 * they pseudo parent.
10511 */
10512 cur = newDoc->children->children;
10513 *list = cur;
10514 while (cur != NULL) {
10515 cur->parent = NULL;
10516 cur = cur->next;
10517 }
10518 newDoc->children->children = NULL;
10519 }
Daniel Veillard7d515752003-09-26 19:12:37 +000010520 ret = XML_ERR_OK;
Owen Taylor3473f882001-02-23 17:55:21 +000010521 }
10522 if (sax != NULL)
10523 ctxt->sax = oldsax;
Daniel Veillard0046c0f2003-02-23 13:52:30 +000010524 oldctxt->node_seq.maximum = ctxt->node_seq.maximum;
10525 oldctxt->node_seq.length = ctxt->node_seq.length;
10526 oldctxt->node_seq.buffer = ctxt->node_seq.buffer;
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010527 ctxt->node_seq.maximum = 0;
10528 ctxt->node_seq.length = 0;
10529 ctxt->node_seq.buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010530 xmlFreeParserCtxt(ctxt);
10531 newDoc->intSubset = NULL;
10532 newDoc->extSubset = NULL;
10533 xmlFreeDoc(newDoc);
10534
10535 return(ret);
10536}
10537
Daniel Veillard81273902003-09-30 00:43:48 +000010538#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000010539/**
Daniel Veillard257d9102001-05-08 10:41:44 +000010540 * xmlParseExternalEntity:
10541 * @doc: the document the chunk pertains to
10542 * @sax: the SAX handler bloc (possibly NULL)
10543 * @user_data: The user data returned on SAX callbacks (possibly NULL)
10544 * @depth: Used for loop detection, use 0
10545 * @URL: the URL for the entity to load
10546 * @ID: the System ID for the entity to load
Daniel Veillardcda96922001-08-21 10:56:31 +000010547 * @lst: the return value for the set of parsed nodes
Daniel Veillard257d9102001-05-08 10:41:44 +000010548 *
10549 * Parse an external general entity
10550 * An external general parsed entity is well-formed if it matches the
10551 * production labeled extParsedEnt.
10552 *
10553 * [78] extParsedEnt ::= TextDecl? content
10554 *
10555 * Returns 0 if the entity is well formed, -1 in case of args problem and
10556 * the parser error code otherwise
10557 */
10558
10559int
10560xmlParseExternalEntity(xmlDocPtr doc, xmlSAXHandlerPtr sax, void *user_data,
Daniel Veillardcda96922001-08-21 10:56:31 +000010561 int depth, const xmlChar *URL, const xmlChar *ID, xmlNodePtr *lst) {
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010562 return(xmlParseExternalEntityPrivate(doc, NULL, sax, user_data, depth, URL,
Daniel Veillardcda96922001-08-21 10:56:31 +000010563 ID, lst));
Daniel Veillard257d9102001-05-08 10:41:44 +000010564}
10565
10566/**
Daniel Veillarde020c3a2001-03-21 18:06:15 +000010567 * xmlParseBalancedChunkMemory:
Owen Taylor3473f882001-02-23 17:55:21 +000010568 * @doc: the document the chunk pertains to
10569 * @sax: the SAX handler bloc (possibly NULL)
10570 * @user_data: The user data returned on SAX callbacks (possibly NULL)
10571 * @depth: Used for loop detection, use 0
10572 * @string: the input string in UTF8 or ISO-Latin (zero terminated)
Daniel Veillardcda96922001-08-21 10:56:31 +000010573 * @lst: the return value for the set of parsed nodes
Owen Taylor3473f882001-02-23 17:55:21 +000010574 *
10575 * Parse a well-balanced chunk of an XML document
10576 * called by the parser
10577 * The allowed sequence for the Well Balanced Chunk is the one defined by
10578 * the content production in the XML grammar:
10579 *
10580 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
10581 *
10582 * Returns 0 if the chunk is well balanced, -1 in case of args problem and
10583 * the parser error code otherwise
10584 */
10585
10586int
10587xmlParseBalancedChunkMemory(xmlDocPtr doc, xmlSAXHandlerPtr sax,
Daniel Veillardcda96922001-08-21 10:56:31 +000010588 void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst) {
Daniel Veillard58e44c92002-08-02 22:19:49 +000010589 return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data,
10590 depth, string, lst, 0 );
10591}
Daniel Veillard81273902003-09-30 00:43:48 +000010592#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard58e44c92002-08-02 22:19:49 +000010593
10594/**
Daniel Veillard328f48c2002-11-15 15:24:34 +000010595 * xmlParseBalancedChunkMemoryInternal:
10596 * @oldctxt: the existing parsing context
10597 * @string: the input string in UTF8 or ISO-Latin (zero terminated)
10598 * @user_data: the user data field for the parser context
10599 * @lst: the return value for the set of parsed nodes
10600 *
10601 *
10602 * Parse a well-balanced chunk of an XML document
10603 * called by the parser
10604 * The allowed sequence for the Well Balanced Chunk is the one defined by
10605 * the content production in the XML grammar:
10606 *
10607 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
10608 *
Daniel Veillard7d515752003-09-26 19:12:37 +000010609 * Returns XML_ERR_OK if the chunk is well balanced, and the parser
10610 * error code otherwise
Daniel Veillard328f48c2002-11-15 15:24:34 +000010611 *
10612 * In case recover is set to 1, the nodelist will not be empty even if
10613 * the parsed chunk is not well balanced.
10614 */
Daniel Veillard7d515752003-09-26 19:12:37 +000010615static xmlParserErrors
Daniel Veillard328f48c2002-11-15 15:24:34 +000010616xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt,
10617 const xmlChar *string, void *user_data, xmlNodePtr *lst) {
10618 xmlParserCtxtPtr ctxt;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010619 xmlDocPtr newDoc = NULL;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010620 xmlSAXHandlerPtr oldsax = NULL;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010621 xmlNodePtr content = NULL;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010622 int size;
Daniel Veillard7d515752003-09-26 19:12:37 +000010623 xmlParserErrors ret = XML_ERR_OK;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010624
10625 if (oldctxt->depth > 40) {
10626 return(XML_ERR_ENTITY_LOOP);
10627 }
10628
10629
10630 if (lst != NULL)
10631 *lst = NULL;
10632 if (string == NULL)
William M. Brack7b9154b2003-09-27 19:23:50 +000010633 return(XML_ERR_INTERNAL_ERROR);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010634
10635 size = xmlStrlen(string);
10636
10637 ctxt = xmlCreateMemoryParserCtxt((char *) string, size);
William M. Brack7b9154b2003-09-27 19:23:50 +000010638 if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010639 if (user_data != NULL)
10640 ctxt->userData = user_data;
10641 else
10642 ctxt->userData = ctxt;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010643 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
10644 ctxt->dict = oldctxt->dict;
Daniel Veillardfd343dc2003-10-31 10:55:22 +000010645 ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
10646 ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
10647 ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010648
10649 oldsax = ctxt->sax;
10650 ctxt->sax = oldctxt->sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000010651 xmlDetectSAX2(ctxt);
Daniel Veillard87ab1c12003-12-21 13:01:56 +000010652 ctxt->replaceEntities = oldctxt->replaceEntities;
10653 ctxt->options = oldctxt->options;
Daniel Veillarde57ec792003-09-10 10:50:59 +000010654
Daniel Veillarde1ca5032002-12-09 14:13:43 +000010655 ctxt->_private = oldctxt->_private;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010656 if (oldctxt->myDoc == NULL) {
Daniel Veillard68e9e742002-11-16 15:35:11 +000010657 newDoc = xmlNewDoc(BAD_CAST "1.0");
10658 if (newDoc == NULL) {
10659 ctxt->sax = oldsax;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010660 ctxt->dict = NULL;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010661 xmlFreeParserCtxt(ctxt);
William M. Brack7b9154b2003-09-27 19:23:50 +000010662 return(XML_ERR_INTERNAL_ERROR);
Daniel Veillard68e9e742002-11-16 15:35:11 +000010663 }
Daniel Veillard328f48c2002-11-15 15:24:34 +000010664 ctxt->myDoc = newDoc;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010665 } else {
10666 ctxt->myDoc = oldctxt->myDoc;
10667 content = ctxt->myDoc->children;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010668 }
Daniel Veillard9bc53102002-11-25 13:20:04 +000010669 ctxt->myDoc->children = xmlNewDocNode(ctxt->myDoc, NULL,
Daniel Veillard68e9e742002-11-16 15:35:11 +000010670 BAD_CAST "pseudoroot", NULL);
10671 if (ctxt->myDoc->children == NULL) {
10672 ctxt->sax = oldsax;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010673 ctxt->dict = NULL;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010674 xmlFreeParserCtxt(ctxt);
10675 if (newDoc != NULL)
10676 xmlFreeDoc(newDoc);
William M. Brack7b9154b2003-09-27 19:23:50 +000010677 return(XML_ERR_INTERNAL_ERROR);
Daniel Veillard68e9e742002-11-16 15:35:11 +000010678 }
10679 nodePush(ctxt, ctxt->myDoc->children);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010680 ctxt->instate = XML_PARSER_CONTENT;
10681 ctxt->depth = oldctxt->depth + 1;
10682
Daniel Veillard328f48c2002-11-15 15:24:34 +000010683 ctxt->validate = 0;
10684 ctxt->loadsubset = oldctxt->loadsubset;
Daniel Veillardef8dd7b2003-03-23 12:02:56 +000010685 if ((oldctxt->validate) || (oldctxt->replaceEntities != 0)) {
10686 /*
10687 * ID/IDREF registration will be done in xmlValidateElement below
10688 */
10689 ctxt->loadsubset |= XML_SKIP_IDS;
10690 }
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010691 ctxt->dictNames = oldctxt->dictNames;
Daniel Veillard95d2d5b2003-10-27 14:54:49 +000010692 ctxt->attsDefault = oldctxt->attsDefault;
10693 ctxt->attsSpecial = oldctxt->attsSpecial;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010694
Daniel Veillard68e9e742002-11-16 15:35:11 +000010695 xmlParseContent(ctxt);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010696 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010697 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010698 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010699 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010700 }
Daniel Veillard68e9e742002-11-16 15:35:11 +000010701 if (ctxt->node != ctxt->myDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010702 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010703 }
10704
10705 if (!ctxt->wellFormed) {
10706 if (ctxt->errNo == 0)
Daniel Veillard7d515752003-09-26 19:12:37 +000010707 ret = XML_ERR_INTERNAL_ERROR;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010708 else
William M. Brack7b9154b2003-09-27 19:23:50 +000010709 ret = (xmlParserErrors)ctxt->errNo;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010710 } else {
William M. Brack7b9154b2003-09-27 19:23:50 +000010711 ret = XML_ERR_OK;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010712 }
10713
William M. Brack7b9154b2003-09-27 19:23:50 +000010714 if ((lst != NULL) && (ret == XML_ERR_OK)) {
Daniel Veillard328f48c2002-11-15 15:24:34 +000010715 xmlNodePtr cur;
10716
10717 /*
10718 * Return the newly created nodeset after unlinking it from
10719 * they pseudo parent.
10720 */
Daniel Veillard68e9e742002-11-16 15:35:11 +000010721 cur = ctxt->myDoc->children->children;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010722 *lst = cur;
10723 while (cur != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +000010724#ifdef LIBXML_VALID_ENABLED
Daniel Veillard8d589042003-02-04 15:07:21 +000010725 if (oldctxt->validate && oldctxt->wellFormed &&
10726 oldctxt->myDoc && oldctxt->myDoc->intSubset) {
10727 oldctxt->valid &= xmlValidateElement(&oldctxt->vctxt,
10728 oldctxt->myDoc, cur);
10729 }
Daniel Veillard4432df22003-09-28 18:58:27 +000010730#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard328f48c2002-11-15 15:24:34 +000010731 cur->parent = NULL;
10732 cur = cur->next;
10733 }
Daniel Veillard68e9e742002-11-16 15:35:11 +000010734 ctxt->myDoc->children->children = NULL;
10735 }
10736 if (ctxt->myDoc != NULL) {
10737 xmlFreeNode(ctxt->myDoc->children);
10738 ctxt->myDoc->children = content;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010739 }
10740
10741 ctxt->sax = oldsax;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010742 ctxt->dict = NULL;
Daniel Veillard95d2d5b2003-10-27 14:54:49 +000010743 ctxt->attsDefault = NULL;
10744 ctxt->attsSpecial = NULL;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010745 xmlFreeParserCtxt(ctxt);
Daniel Veillard68e9e742002-11-16 15:35:11 +000010746 if (newDoc != NULL)
10747 xmlFreeDoc(newDoc);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010748
10749 return(ret);
10750}
10751
Daniel Veillard81273902003-09-30 00:43:48 +000010752#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard328f48c2002-11-15 15:24:34 +000010753/**
Daniel Veillard58e44c92002-08-02 22:19:49 +000010754 * xmlParseBalancedChunkMemoryRecover:
10755 * @doc: the document the chunk pertains to
10756 * @sax: the SAX handler bloc (possibly NULL)
10757 * @user_data: The user data returned on SAX callbacks (possibly NULL)
10758 * @depth: Used for loop detection, use 0
10759 * @string: the input string in UTF8 or ISO-Latin (zero terminated)
10760 * @lst: the return value for the set of parsed nodes
10761 * @recover: return nodes even if the data is broken (use 0)
10762 *
10763 *
10764 * Parse a well-balanced chunk of an XML document
10765 * called by the parser
10766 * The allowed sequence for the Well Balanced Chunk is the one defined by
10767 * the content production in the XML grammar:
10768 *
10769 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
10770 *
10771 * Returns 0 if the chunk is well balanced, -1 in case of args problem and
10772 * the parser error code otherwise
10773 *
10774 * In case recover is set to 1, the nodelist will not be empty even if
10775 * the parsed chunk is not well balanced.
10776 */
10777int
10778xmlParseBalancedChunkMemoryRecover(xmlDocPtr doc, xmlSAXHandlerPtr sax,
10779 void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst,
10780 int recover) {
Owen Taylor3473f882001-02-23 17:55:21 +000010781 xmlParserCtxtPtr ctxt;
10782 xmlDocPtr newDoc;
10783 xmlSAXHandlerPtr oldsax = NULL;
Daniel Veillard935494a2002-10-22 14:22:46 +000010784 xmlNodePtr content;
Owen Taylor3473f882001-02-23 17:55:21 +000010785 int size;
10786 int ret = 0;
10787
10788 if (depth > 40) {
10789 return(XML_ERR_ENTITY_LOOP);
10790 }
10791
10792
Daniel Veillardcda96922001-08-21 10:56:31 +000010793 if (lst != NULL)
10794 *lst = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010795 if (string == NULL)
10796 return(-1);
10797
10798 size = xmlStrlen(string);
10799
10800 ctxt = xmlCreateMemoryParserCtxt((char *) string, size);
10801 if (ctxt == NULL) return(-1);
10802 ctxt->userData = ctxt;
10803 if (sax != NULL) {
10804 oldsax = ctxt->sax;
10805 ctxt->sax = sax;
10806 if (user_data != NULL)
10807 ctxt->userData = user_data;
10808 }
10809 newDoc = xmlNewDoc(BAD_CAST "1.0");
10810 if (newDoc == NULL) {
10811 xmlFreeParserCtxt(ctxt);
10812 return(-1);
10813 }
10814 if (doc != NULL) {
10815 newDoc->intSubset = doc->intSubset;
10816 newDoc->extSubset = doc->extSubset;
10817 }
10818 newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
10819 if (newDoc->children == NULL) {
10820 if (sax != NULL)
10821 ctxt->sax = oldsax;
10822 xmlFreeParserCtxt(ctxt);
10823 newDoc->intSubset = NULL;
10824 newDoc->extSubset = NULL;
10825 xmlFreeDoc(newDoc);
10826 return(-1);
10827 }
10828 nodePush(ctxt, newDoc->children);
10829 if (doc == NULL) {
10830 ctxt->myDoc = newDoc;
10831 } else {
Daniel Veillard42766c02002-08-22 20:52:17 +000010832 ctxt->myDoc = newDoc;
Owen Taylor3473f882001-02-23 17:55:21 +000010833 newDoc->children->doc = doc;
10834 }
10835 ctxt->instate = XML_PARSER_CONTENT;
10836 ctxt->depth = depth;
10837
10838 /*
10839 * Doing validity checking on chunk doesn't make sense
10840 */
10841 ctxt->validate = 0;
10842 ctxt->loadsubset = 0;
Daniel Veillarde57ec792003-09-10 10:50:59 +000010843 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010844
Daniel Veillardb39bc392002-10-26 19:29:51 +000010845 if ( doc != NULL ){
10846 content = doc->children;
10847 doc->children = NULL;
10848 xmlParseContent(ctxt);
10849 doc->children = content;
10850 }
10851 else {
10852 xmlParseContent(ctxt);
10853 }
Owen Taylor3473f882001-02-23 17:55:21 +000010854 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010855 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010856 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010857 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010858 }
10859 if (ctxt->node != newDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010860 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010861 }
10862
10863 if (!ctxt->wellFormed) {
10864 if (ctxt->errNo == 0)
10865 ret = 1;
10866 else
10867 ret = ctxt->errNo;
10868 } else {
Daniel Veillard58e44c92002-08-02 22:19:49 +000010869 ret = 0;
10870 }
10871
10872 if (lst != NULL && (ret == 0 || recover == 1)) {
10873 xmlNodePtr cur;
Owen Taylor3473f882001-02-23 17:55:21 +000010874
10875 /*
10876 * Return the newly created nodeset after unlinking it from
10877 * they pseudo parent.
10878 */
10879 cur = newDoc->children->children;
Daniel Veillardcda96922001-08-21 10:56:31 +000010880 *lst = cur;
Owen Taylor3473f882001-02-23 17:55:21 +000010881 while (cur != NULL) {
10882 cur->parent = NULL;
10883 cur = cur->next;
10884 }
10885 newDoc->children->children = NULL;
10886 }
Daniel Veillard58e44c92002-08-02 22:19:49 +000010887
Owen Taylor3473f882001-02-23 17:55:21 +000010888 if (sax != NULL)
10889 ctxt->sax = oldsax;
10890 xmlFreeParserCtxt(ctxt);
10891 newDoc->intSubset = NULL;
10892 newDoc->extSubset = NULL;
10893 xmlFreeDoc(newDoc);
10894
10895 return(ret);
10896}
10897
10898/**
10899 * xmlSAXParseEntity:
10900 * @sax: the SAX handler block
10901 * @filename: the filename
10902 *
10903 * parse an XML external entity out of context and build a tree.
10904 * It use the given SAX function block to handle the parsing callback.
10905 * If sax is NULL, fallback to the default DOM tree building routines.
10906 *
10907 * [78] extParsedEnt ::= TextDecl? content
10908 *
10909 * This correspond to a "Well Balanced" chunk
10910 *
10911 * Returns the resulting document tree
10912 */
10913
10914xmlDocPtr
10915xmlSAXParseEntity(xmlSAXHandlerPtr sax, const char *filename) {
10916 xmlDocPtr ret;
10917 xmlParserCtxtPtr ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +000010918
10919 ctxt = xmlCreateFileParserCtxt(filename);
10920 if (ctxt == NULL) {
10921 return(NULL);
10922 }
10923 if (sax != NULL) {
10924 if (ctxt->sax != NULL)
10925 xmlFree(ctxt->sax);
10926 ctxt->sax = sax;
10927 ctxt->userData = NULL;
10928 }
10929
Owen Taylor3473f882001-02-23 17:55:21 +000010930 xmlParseExtParsedEnt(ctxt);
10931
10932 if (ctxt->wellFormed)
10933 ret = ctxt->myDoc;
10934 else {
10935 ret = NULL;
10936 xmlFreeDoc(ctxt->myDoc);
10937 ctxt->myDoc = NULL;
10938 }
10939 if (sax != NULL)
10940 ctxt->sax = NULL;
10941 xmlFreeParserCtxt(ctxt);
10942
10943 return(ret);
10944}
10945
10946/**
10947 * xmlParseEntity:
10948 * @filename: the filename
10949 *
10950 * parse an XML external entity out of context and build a tree.
10951 *
10952 * [78] extParsedEnt ::= TextDecl? content
10953 *
10954 * This correspond to a "Well Balanced" chunk
10955 *
10956 * Returns the resulting document tree
10957 */
10958
10959xmlDocPtr
10960xmlParseEntity(const char *filename) {
10961 return(xmlSAXParseEntity(NULL, filename));
10962}
Daniel Veillard81273902003-09-30 00:43:48 +000010963#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000010964
10965/**
10966 * xmlCreateEntityParserCtxt:
10967 * @URL: the entity URL
10968 * @ID: the entity PUBLIC ID
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010969 * @base: a possible base for the target URI
Owen Taylor3473f882001-02-23 17:55:21 +000010970 *
10971 * Create a parser context for an external entity
10972 * Automatic support for ZLIB/Compress compressed document is provided
10973 * by default if found at compile-time.
10974 *
10975 * Returns the new parser context or NULL
10976 */
10977xmlParserCtxtPtr
10978xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID,
10979 const xmlChar *base) {
10980 xmlParserCtxtPtr ctxt;
10981 xmlParserInputPtr inputStream;
10982 char *directory = NULL;
10983 xmlChar *uri;
10984
10985 ctxt = xmlNewParserCtxt();
10986 if (ctxt == NULL) {
10987 return(NULL);
10988 }
10989
10990 uri = xmlBuildURI(URL, base);
10991
10992 if (uri == NULL) {
10993 inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt);
10994 if (inputStream == NULL) {
10995 xmlFreeParserCtxt(ctxt);
10996 return(NULL);
10997 }
10998
10999 inputPush(ctxt, inputStream);
11000
11001 if ((ctxt->directory == NULL) && (directory == NULL))
11002 directory = xmlParserGetDirectory((char *)URL);
11003 if ((ctxt->directory == NULL) && (directory != NULL))
11004 ctxt->directory = directory;
11005 } else {
11006 inputStream = xmlLoadExternalEntity((char *)uri, (char *)ID, ctxt);
11007 if (inputStream == NULL) {
11008 xmlFree(uri);
11009 xmlFreeParserCtxt(ctxt);
11010 return(NULL);
11011 }
11012
11013 inputPush(ctxt, inputStream);
11014
11015 if ((ctxt->directory == NULL) && (directory == NULL))
11016 directory = xmlParserGetDirectory((char *)uri);
11017 if ((ctxt->directory == NULL) && (directory != NULL))
11018 ctxt->directory = directory;
11019 xmlFree(uri);
11020 }
Owen Taylor3473f882001-02-23 17:55:21 +000011021 return(ctxt);
11022}
11023
11024/************************************************************************
11025 * *
11026 * Front ends when parsing from a file *
11027 * *
11028 ************************************************************************/
11029
11030/**
Daniel Veillard61b93382003-11-03 14:28:31 +000011031 * xmlCreateURLParserCtxt:
11032 * @filename: the filename or URL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000011033 * @options: a combination of xmlParserOption
Owen Taylor3473f882001-02-23 17:55:21 +000011034 *
Daniel Veillard61b93382003-11-03 14:28:31 +000011035 * Create a parser context for a file or URL content.
Owen Taylor3473f882001-02-23 17:55:21 +000011036 * Automatic support for ZLIB/Compress compressed document is provided
Daniel Veillard61b93382003-11-03 14:28:31 +000011037 * by default if found at compile-time and for file accesses
Owen Taylor3473f882001-02-23 17:55:21 +000011038 *
11039 * Returns the new parser context or NULL
11040 */
11041xmlParserCtxtPtr
Daniel Veillard61b93382003-11-03 14:28:31 +000011042xmlCreateURLParserCtxt(const char *filename, int options)
Owen Taylor3473f882001-02-23 17:55:21 +000011043{
11044 xmlParserCtxtPtr ctxt;
11045 xmlParserInputPtr inputStream;
Owen Taylor3473f882001-02-23 17:55:21 +000011046 char *directory = NULL;
11047
Owen Taylor3473f882001-02-23 17:55:21 +000011048 ctxt = xmlNewParserCtxt();
11049 if (ctxt == NULL) {
Daniel Veillard81273902003-09-30 00:43:48 +000011050 xmlErrMemory(NULL, "cannot allocate parser context");
Owen Taylor3473f882001-02-23 17:55:21 +000011051 return(NULL);
11052 }
11053
Daniel Veillard61b93382003-11-03 14:28:31 +000011054 if (options != 0)
11055 xmlCtxtUseOptions(ctxt, options);
Igor Zlatkovicce076162003-02-23 13:39:39 +000011056
Daniel Veillard4e9b1bc2003-06-09 10:30:33 +000011057 inputStream = xmlLoadExternalEntity(filename, NULL, ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011058 if (inputStream == NULL) {
11059 xmlFreeParserCtxt(ctxt);
11060 return(NULL);
11061 }
11062
Owen Taylor3473f882001-02-23 17:55:21 +000011063 inputPush(ctxt, inputStream);
11064 if ((ctxt->directory == NULL) && (directory == NULL))
Igor Zlatkovic5f9fada2003-02-19 14:51:00 +000011065 directory = xmlParserGetDirectory(filename);
Owen Taylor3473f882001-02-23 17:55:21 +000011066 if ((ctxt->directory == NULL) && (directory != NULL))
11067 ctxt->directory = directory;
11068
11069 return(ctxt);
11070}
11071
Daniel Veillard61b93382003-11-03 14:28:31 +000011072/**
11073 * xmlCreateFileParserCtxt:
11074 * @filename: the filename
11075 *
11076 * Create a parser context for a file content.
11077 * Automatic support for ZLIB/Compress compressed document is provided
11078 * by default if found at compile-time.
11079 *
11080 * Returns the new parser context or NULL
11081 */
11082xmlParserCtxtPtr
11083xmlCreateFileParserCtxt(const char *filename)
11084{
11085 return(xmlCreateURLParserCtxt(filename, 0));
11086}
11087
Daniel Veillard81273902003-09-30 00:43:48 +000011088#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000011089/**
Daniel Veillarda293c322001-10-02 13:54:14 +000011090 * xmlSAXParseFileWithData:
Owen Taylor3473f882001-02-23 17:55:21 +000011091 * @sax: the SAX handler block
11092 * @filename: the filename
11093 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11094 * documents
Daniel Veillarda293c322001-10-02 13:54:14 +000011095 * @data: the userdata
Owen Taylor3473f882001-02-23 17:55:21 +000011096 *
11097 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11098 * compressed document is provided by default if found at compile-time.
11099 * It use the given SAX function block to handle the parsing callback.
11100 * If sax is NULL, fallback to the default DOM tree building routines.
11101 *
Daniel Veillarde19fc232002-04-22 16:01:24 +000011102 * User data (void *) is stored within the parser context in the
11103 * context's _private member, so it is available nearly everywhere in libxml
Daniel Veillarda293c322001-10-02 13:54:14 +000011104 *
Owen Taylor3473f882001-02-23 17:55:21 +000011105 * Returns the resulting document tree
11106 */
11107
11108xmlDocPtr
Daniel Veillarda293c322001-10-02 13:54:14 +000011109xmlSAXParseFileWithData(xmlSAXHandlerPtr sax, const char *filename,
11110 int recovery, void *data) {
Owen Taylor3473f882001-02-23 17:55:21 +000011111 xmlDocPtr ret;
11112 xmlParserCtxtPtr ctxt;
11113 char *directory = NULL;
11114
Daniel Veillard635ef722001-10-29 11:48:19 +000011115 xmlInitParser();
11116
Owen Taylor3473f882001-02-23 17:55:21 +000011117 ctxt = xmlCreateFileParserCtxt(filename);
11118 if (ctxt == NULL) {
11119 return(NULL);
11120 }
11121 if (sax != NULL) {
11122 if (ctxt->sax != NULL)
11123 xmlFree(ctxt->sax);
11124 ctxt->sax = sax;
Owen Taylor3473f882001-02-23 17:55:21 +000011125 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000011126 xmlDetectSAX2(ctxt);
Daniel Veillarda293c322001-10-02 13:54:14 +000011127 if (data!=NULL) {
Daniel Veillardc790bf42003-10-11 10:50:10 +000011128 ctxt->_private = data;
Daniel Veillarda293c322001-10-02 13:54:14 +000011129 }
Owen Taylor3473f882001-02-23 17:55:21 +000011130
11131 if ((ctxt->directory == NULL) && (directory == NULL))
11132 directory = xmlParserGetDirectory(filename);
11133 if ((ctxt->directory == NULL) && (directory != NULL))
11134 ctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
11135
Daniel Veillarddad3f682002-11-17 16:47:27 +000011136 ctxt->recovery = recovery;
11137
Owen Taylor3473f882001-02-23 17:55:21 +000011138 xmlParseDocument(ctxt);
11139
William M. Brackc07329e2003-09-08 01:57:30 +000011140 if ((ctxt->wellFormed) || recovery) {
11141 ret = ctxt->myDoc;
Daniel Veillardb65e12e2003-10-08 21:33:28 +000011142 if (ret != NULL) {
11143 if (ctxt->input->buf->compressed > 0)
11144 ret->compression = 9;
11145 else
11146 ret->compression = ctxt->input->buf->compressed;
11147 }
William M. Brackc07329e2003-09-08 01:57:30 +000011148 }
Owen Taylor3473f882001-02-23 17:55:21 +000011149 else {
11150 ret = NULL;
11151 xmlFreeDoc(ctxt->myDoc);
11152 ctxt->myDoc = NULL;
11153 }
11154 if (sax != NULL)
11155 ctxt->sax = NULL;
11156 xmlFreeParserCtxt(ctxt);
11157
11158 return(ret);
11159}
11160
11161/**
Daniel Veillarda293c322001-10-02 13:54:14 +000011162 * xmlSAXParseFile:
11163 * @sax: the SAX handler block
11164 * @filename: the filename
11165 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11166 * documents
11167 *
11168 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11169 * compressed document is provided by default if found at compile-time.
11170 * It use the given SAX function block to handle the parsing callback.
11171 * If sax is NULL, fallback to the default DOM tree building routines.
11172 *
11173 * Returns the resulting document tree
11174 */
11175
11176xmlDocPtr
11177xmlSAXParseFile(xmlSAXHandlerPtr sax, const char *filename,
11178 int recovery) {
11179 return(xmlSAXParseFileWithData(sax,filename,recovery,NULL));
11180}
11181
11182/**
Owen Taylor3473f882001-02-23 17:55:21 +000011183 * xmlRecoverDoc:
11184 * @cur: a pointer to an array of xmlChar
11185 *
11186 * parse an XML in-memory document and build a tree.
11187 * In the case the document is not Well Formed, a tree is built anyway
11188 *
11189 * Returns the resulting document tree
11190 */
11191
11192xmlDocPtr
11193xmlRecoverDoc(xmlChar *cur) {
11194 return(xmlSAXParseDoc(NULL, cur, 1));
11195}
11196
11197/**
11198 * xmlParseFile:
11199 * @filename: the filename
11200 *
11201 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11202 * compressed document is provided by default if found at compile-time.
11203 *
Daniel Veillard5d96fff2001-08-31 14:55:30 +000011204 * Returns the resulting document tree if the file was wellformed,
11205 * NULL otherwise.
Owen Taylor3473f882001-02-23 17:55:21 +000011206 */
11207
11208xmlDocPtr
11209xmlParseFile(const char *filename) {
11210 return(xmlSAXParseFile(NULL, filename, 0));
11211}
11212
11213/**
11214 * xmlRecoverFile:
11215 * @filename: the filename
11216 *
11217 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11218 * compressed document is provided by default if found at compile-time.
11219 * In the case the document is not Well Formed, a tree is built anyway
11220 *
11221 * Returns the resulting document tree
11222 */
11223
11224xmlDocPtr
11225xmlRecoverFile(const char *filename) {
11226 return(xmlSAXParseFile(NULL, filename, 1));
11227}
11228
11229
11230/**
11231 * xmlSetupParserForBuffer:
11232 * @ctxt: an XML parser context
11233 * @buffer: a xmlChar * buffer
11234 * @filename: a file name
11235 *
11236 * Setup the parser context to parse a new buffer; Clears any prior
11237 * contents from the parser context. The buffer parameter must not be
11238 * NULL, but the filename parameter can be
11239 */
11240void
11241xmlSetupParserForBuffer(xmlParserCtxtPtr ctxt, const xmlChar* buffer,
11242 const char* filename)
11243{
11244 xmlParserInputPtr input;
11245
11246 input = xmlNewInputStream(ctxt);
11247 if (input == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +000011248 xmlErrMemory(NULL, "parsing new buffer: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +000011249 xmlFree(ctxt);
11250 return;
11251 }
11252
11253 xmlClearParserCtxt(ctxt);
11254 if (filename != NULL)
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +000011255 input->filename = (char *) xmlCanonicPath((const xmlChar *)filename);
Owen Taylor3473f882001-02-23 17:55:21 +000011256 input->base = buffer;
11257 input->cur = buffer;
Daniel Veillard48b2f892001-02-25 16:11:03 +000011258 input->end = &buffer[xmlStrlen(buffer)];
Owen Taylor3473f882001-02-23 17:55:21 +000011259 inputPush(ctxt, input);
11260}
11261
11262/**
11263 * xmlSAXUserParseFile:
11264 * @sax: a SAX handler
11265 * @user_data: The user data returned on SAX callbacks
11266 * @filename: a file name
11267 *
11268 * parse an XML file and call the given SAX handler routines.
11269 * Automatic support for ZLIB/Compress compressed document is provided
11270 *
11271 * Returns 0 in case of success or a error number otherwise
11272 */
11273int
11274xmlSAXUserParseFile(xmlSAXHandlerPtr sax, void *user_data,
11275 const char *filename) {
11276 int ret = 0;
11277 xmlParserCtxtPtr ctxt;
11278
11279 ctxt = xmlCreateFileParserCtxt(filename);
11280 if (ctxt == NULL) return -1;
Daniel Veillard81273902003-09-30 00:43:48 +000011281#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +000011282 if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
Daniel Veillard81273902003-09-30 00:43:48 +000011283#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011284 xmlFree(ctxt->sax);
11285 ctxt->sax = sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000011286 xmlDetectSAX2(ctxt);
11287
Owen Taylor3473f882001-02-23 17:55:21 +000011288 if (user_data != NULL)
11289 ctxt->userData = user_data;
11290
11291 xmlParseDocument(ctxt);
11292
11293 if (ctxt->wellFormed)
11294 ret = 0;
11295 else {
11296 if (ctxt->errNo != 0)
11297 ret = ctxt->errNo;
11298 else
11299 ret = -1;
11300 }
11301 if (sax != NULL)
11302 ctxt->sax = NULL;
11303 xmlFreeParserCtxt(ctxt);
11304
11305 return ret;
11306}
Daniel Veillard81273902003-09-30 00:43:48 +000011307#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011308
11309/************************************************************************
11310 * *
11311 * Front ends when parsing from memory *
11312 * *
11313 ************************************************************************/
11314
11315/**
11316 * xmlCreateMemoryParserCtxt:
11317 * @buffer: a pointer to a char array
11318 * @size: the size of the array
11319 *
11320 * Create a parser context for an XML in-memory document.
11321 *
11322 * Returns the new parser context or NULL
11323 */
11324xmlParserCtxtPtr
Daniel Veillardfd7ddca2001-05-16 10:57:35 +000011325xmlCreateMemoryParserCtxt(const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000011326 xmlParserCtxtPtr ctxt;
11327 xmlParserInputPtr input;
11328 xmlParserInputBufferPtr buf;
11329
11330 if (buffer == NULL)
11331 return(NULL);
11332 if (size <= 0)
11333 return(NULL);
11334
11335 ctxt = xmlNewParserCtxt();
11336 if (ctxt == NULL)
11337 return(NULL);
11338
Daniel Veillard53350552003-09-18 13:35:51 +000011339 /* TODO: xmlParserInputBufferCreateStatic, requires some serious changes */
Owen Taylor3473f882001-02-23 17:55:21 +000011340 buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
Daniel Veillarda7e05b42002-11-19 08:11:14 +000011341 if (buf == NULL) {
11342 xmlFreeParserCtxt(ctxt);
11343 return(NULL);
11344 }
Owen Taylor3473f882001-02-23 17:55:21 +000011345
11346 input = xmlNewInputStream(ctxt);
11347 if (input == NULL) {
Daniel Veillarda7e05b42002-11-19 08:11:14 +000011348 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +000011349 xmlFreeParserCtxt(ctxt);
11350 return(NULL);
11351 }
11352
11353 input->filename = NULL;
11354 input->buf = buf;
11355 input->base = input->buf->buffer->content;
11356 input->cur = input->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +000011357 input->end = &input->buf->buffer->content[input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +000011358
11359 inputPush(ctxt, input);
11360 return(ctxt);
11361}
11362
Daniel Veillard81273902003-09-30 00:43:48 +000011363#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000011364/**
Daniel Veillard8606bbb2002-11-12 12:36:52 +000011365 * xmlSAXParseMemoryWithData:
11366 * @sax: the SAX handler block
11367 * @buffer: an pointer to a char array
11368 * @size: the size of the array
11369 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11370 * documents
11371 * @data: the userdata
11372 *
11373 * parse an XML in-memory block and use the given SAX function block
11374 * to handle the parsing callback. If sax is NULL, fallback to the default
11375 * DOM tree building routines.
11376 *
11377 * User data (void *) is stored within the parser context in the
11378 * context's _private member, so it is available nearly everywhere in libxml
11379 *
11380 * Returns the resulting document tree
11381 */
11382
11383xmlDocPtr
11384xmlSAXParseMemoryWithData(xmlSAXHandlerPtr sax, const char *buffer,
11385 int size, int recovery, void *data) {
11386 xmlDocPtr ret;
11387 xmlParserCtxtPtr ctxt;
11388
11389 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
11390 if (ctxt == NULL) return(NULL);
11391 if (sax != NULL) {
11392 if (ctxt->sax != NULL)
11393 xmlFree(ctxt->sax);
11394 ctxt->sax = sax;
11395 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000011396 xmlDetectSAX2(ctxt);
Daniel Veillard8606bbb2002-11-12 12:36:52 +000011397 if (data!=NULL) {
11398 ctxt->_private=data;
11399 }
11400
Daniel Veillardadba5f12003-04-04 16:09:01 +000011401 ctxt->recovery = recovery;
11402
Daniel Veillard8606bbb2002-11-12 12:36:52 +000011403 xmlParseDocument(ctxt);
11404
11405 if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc;
11406 else {
11407 ret = NULL;
11408 xmlFreeDoc(ctxt->myDoc);
11409 ctxt->myDoc = NULL;
11410 }
11411 if (sax != NULL)
11412 ctxt->sax = NULL;
11413 xmlFreeParserCtxt(ctxt);
11414
11415 return(ret);
11416}
11417
11418/**
Owen Taylor3473f882001-02-23 17:55:21 +000011419 * xmlSAXParseMemory:
11420 * @sax: the SAX handler block
11421 * @buffer: an pointer to a char array
11422 * @size: the size of the array
11423 * @recovery: work in recovery mode, i.e. tries to read not Well Formed
11424 * documents
11425 *
11426 * parse an XML in-memory block and use the given SAX function block
11427 * to handle the parsing callback. If sax is NULL, fallback to the default
11428 * DOM tree building routines.
11429 *
11430 * Returns the resulting document tree
11431 */
11432xmlDocPtr
Daniel Veillard50822cb2001-07-26 20:05:51 +000011433xmlSAXParseMemory(xmlSAXHandlerPtr sax, const char *buffer,
11434 int size, int recovery) {
Daniel Veillard8606bbb2002-11-12 12:36:52 +000011435 return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000011436}
11437
11438/**
11439 * xmlParseMemory:
11440 * @buffer: an pointer to a char array
11441 * @size: the size of the array
11442 *
11443 * parse an XML in-memory block and build a tree.
11444 *
11445 * Returns the resulting document tree
11446 */
11447
Daniel Veillard50822cb2001-07-26 20:05:51 +000011448xmlDocPtr xmlParseMemory(const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000011449 return(xmlSAXParseMemory(NULL, buffer, size, 0));
11450}
11451
11452/**
11453 * xmlRecoverMemory:
11454 * @buffer: an pointer to a char array
11455 * @size: the size of the array
11456 *
11457 * parse an XML in-memory block 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
Daniel Veillard50822cb2001-07-26 20:05:51 +000011463xmlDocPtr xmlRecoverMemory(const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000011464 return(xmlSAXParseMemory(NULL, buffer, size, 1));
11465}
11466
11467/**
11468 * xmlSAXUserParseMemory:
11469 * @sax: a SAX handler
11470 * @user_data: The user data returned on SAX callbacks
11471 * @buffer: an in-memory XML document input
11472 * @size: the length of the XML document in bytes
11473 *
11474 * A better SAX parsing routine.
11475 * parse an XML in-memory buffer and call the given SAX handler routines.
11476 *
11477 * Returns 0 in case of success or a error number otherwise
11478 */
11479int xmlSAXUserParseMemory(xmlSAXHandlerPtr sax, void *user_data,
Daniel Veillardfd7ddca2001-05-16 10:57:35 +000011480 const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000011481 int ret = 0;
11482 xmlParserCtxtPtr ctxt;
11483 xmlSAXHandlerPtr oldsax = NULL;
11484
Daniel Veillard9e923512002-08-14 08:48:52 +000011485 if (sax == NULL) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +000011486 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
11487 if (ctxt == NULL) return -1;
Daniel Veillard9e923512002-08-14 08:48:52 +000011488 oldsax = ctxt->sax;
11489 ctxt->sax = sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000011490 xmlDetectSAX2(ctxt);
Daniel Veillard30211a02001-04-26 09:33:18 +000011491 if (user_data != NULL)
11492 ctxt->userData = user_data;
Owen Taylor3473f882001-02-23 17:55:21 +000011493
11494 xmlParseDocument(ctxt);
11495
11496 if (ctxt->wellFormed)
11497 ret = 0;
11498 else {
11499 if (ctxt->errNo != 0)
11500 ret = ctxt->errNo;
11501 else
11502 ret = -1;
11503 }
Daniel Veillard9e923512002-08-14 08:48:52 +000011504 ctxt->sax = oldsax;
Owen Taylor3473f882001-02-23 17:55:21 +000011505 xmlFreeParserCtxt(ctxt);
11506
11507 return ret;
11508}
Daniel Veillard81273902003-09-30 00:43:48 +000011509#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011510
11511/**
11512 * xmlCreateDocParserCtxt:
11513 * @cur: a pointer to an array of xmlChar
11514 *
11515 * Creates a parser context for an XML in-memory document.
11516 *
11517 * Returns the new parser context or NULL
11518 */
11519xmlParserCtxtPtr
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011520xmlCreateDocParserCtxt(const xmlChar *cur) {
Owen Taylor3473f882001-02-23 17:55:21 +000011521 int len;
11522
11523 if (cur == NULL)
11524 return(NULL);
11525 len = xmlStrlen(cur);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011526 return(xmlCreateMemoryParserCtxt((const char *)cur, len));
Owen Taylor3473f882001-02-23 17:55:21 +000011527}
11528
Daniel Veillard81273902003-09-30 00:43:48 +000011529#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000011530/**
11531 * xmlSAXParseDoc:
11532 * @sax: the SAX handler block
11533 * @cur: a pointer to an array of xmlChar
11534 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11535 * documents
11536 *
11537 * parse an XML in-memory document and build a tree.
11538 * It use the given SAX function block to handle the parsing callback.
11539 * If sax is NULL, fallback to the default DOM tree building routines.
11540 *
11541 * Returns the resulting document tree
11542 */
11543
11544xmlDocPtr
11545xmlSAXParseDoc(xmlSAXHandlerPtr sax, xmlChar *cur, int recovery) {
11546 xmlDocPtr ret;
11547 xmlParserCtxtPtr ctxt;
11548
11549 if (cur == NULL) return(NULL);
11550
11551
11552 ctxt = xmlCreateDocParserCtxt(cur);
11553 if (ctxt == NULL) return(NULL);
11554 if (sax != NULL) {
11555 ctxt->sax = sax;
11556 ctxt->userData = NULL;
11557 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000011558 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011559
11560 xmlParseDocument(ctxt);
11561 if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc;
11562 else {
11563 ret = NULL;
11564 xmlFreeDoc(ctxt->myDoc);
11565 ctxt->myDoc = NULL;
11566 }
11567 if (sax != NULL)
11568 ctxt->sax = NULL;
11569 xmlFreeParserCtxt(ctxt);
11570
11571 return(ret);
11572}
11573
11574/**
11575 * xmlParseDoc:
11576 * @cur: a pointer to an array of xmlChar
11577 *
11578 * parse an XML in-memory document and build a tree.
11579 *
11580 * Returns the resulting document tree
11581 */
11582
11583xmlDocPtr
11584xmlParseDoc(xmlChar *cur) {
11585 return(xmlSAXParseDoc(NULL, cur, 0));
11586}
Daniel Veillard81273902003-09-30 00:43:48 +000011587#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011588
Daniel Veillard81273902003-09-30 00:43:48 +000011589#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +000011590/************************************************************************
11591 * *
11592 * Specific function to keep track of entities references *
11593 * and used by the XSLT debugger *
11594 * *
11595 ************************************************************************/
11596
11597static xmlEntityReferenceFunc xmlEntityRefFunc = NULL;
11598
11599/**
11600 * xmlAddEntityReference:
11601 * @ent : A valid entity
11602 * @firstNode : A valid first node for children of entity
11603 * @lastNode : A valid last node of children entity
11604 *
11605 * Notify of a reference to an entity of type XML_EXTERNAL_GENERAL_PARSED_ENTITY
11606 */
11607static void
11608xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode,
11609 xmlNodePtr lastNode)
11610{
11611 if (xmlEntityRefFunc != NULL) {
11612 (*xmlEntityRefFunc) (ent, firstNode, lastNode);
11613 }
11614}
11615
11616
11617/**
11618 * xmlSetEntityReferenceFunc:
Daniel Veillard01c13b52002-12-10 15:19:08 +000011619 * @func: A valid function
Daniel Veillard8107a222002-01-13 14:10:10 +000011620 *
11621 * Set the function to call call back when a xml reference has been made
11622 */
11623void
11624xmlSetEntityReferenceFunc(xmlEntityReferenceFunc func)
11625{
11626 xmlEntityRefFunc = func;
11627}
Daniel Veillard81273902003-09-30 00:43:48 +000011628#endif /* LIBXML_LEGACY_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011629
11630/************************************************************************
11631 * *
11632 * Miscellaneous *
11633 * *
11634 ************************************************************************/
11635
11636#ifdef LIBXML_XPATH_ENABLED
11637#include <libxml/xpath.h>
11638#endif
11639
Daniel Veillarddb5850a2002-01-18 11:49:26 +000011640extern void xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...);
Owen Taylor3473f882001-02-23 17:55:21 +000011641static int xmlParserInitialized = 0;
11642
11643/**
11644 * xmlInitParser:
11645 *
11646 * Initialization function for the XML parser.
11647 * This is not reentrant. Call once before processing in case of
11648 * use in multithreaded programs.
11649 */
11650
11651void
11652xmlInitParser(void) {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000011653 if (xmlParserInitialized != 0)
11654 return;
Owen Taylor3473f882001-02-23 17:55:21 +000011655
Daniel Veillarddb5850a2002-01-18 11:49:26 +000011656 if ((xmlGenericError == xmlGenericErrorDefaultFunc) ||
11657 (xmlGenericError == NULL))
11658 initGenericErrorDefaultFunc(NULL);
Daniel Veillard781ac8b2003-05-15 22:11:36 +000011659 xmlInitGlobals();
Daniel Veillardd0463562001-10-13 09:15:48 +000011660 xmlInitThreads();
Daniel Veillard6f350292001-10-14 09:56:15 +000011661 xmlInitMemory();
Owen Taylor3473f882001-02-23 17:55:21 +000011662 xmlInitCharEncodingHandlers();
Owen Taylor3473f882001-02-23 17:55:21 +000011663 xmlDefaultSAXHandlerInit();
11664 xmlRegisterDefaultInputCallbacks();
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000011665#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000011666 xmlRegisterDefaultOutputCallbacks();
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000011667#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011668#ifdef LIBXML_HTML_ENABLED
11669 htmlInitAutoClose();
11670 htmlDefaultSAXHandlerInit();
11671#endif
11672#ifdef LIBXML_XPATH_ENABLED
11673 xmlXPathInit();
11674#endif
11675 xmlParserInitialized = 1;
11676}
11677
11678/**
11679 * xmlCleanupParser:
11680 *
Daniel Veillardd8cf9062003-11-11 21:12:36 +000011681 * Cleanup function for the XML library. It tries to reclaim all
11682 * parsing related global memory allocated for the library processing.
Owen Taylor3473f882001-02-23 17:55:21 +000011683 * It doesn't deallocate any document related memory. Calling this
Daniel Veillardd8cf9062003-11-11 21:12:36 +000011684 * function should not prevent reusing the library but one should
11685 * call xmlCleanupParser() only when the process has
Daniel Veillard7424eb62003-01-24 14:14:52 +000011686 * finished using the library or XML document built with it.
Owen Taylor3473f882001-02-23 17:55:21 +000011687 */
11688
11689void
11690xmlCleanupParser(void) {
Daniel Veillard7fb801f2003-08-17 21:07:26 +000011691 if (!xmlParserInitialized)
11692 return;
11693
Owen Taylor3473f882001-02-23 17:55:21 +000011694 xmlCleanupCharEncodingHandlers();
Daniel Veillarde2940dd2001-08-22 00:06:49 +000011695#ifdef LIBXML_CATALOG_ENABLED
11696 xmlCatalogCleanup();
11697#endif
Daniel Veillard04054be2003-10-15 10:48:54 +000011698 xmlCleanupInputCallbacks();
11699#ifdef LIBXML_OUTPUT_ENABLED
11700 xmlCleanupOutputCallbacks();
11701#endif
Daniel Veillard781ac8b2003-05-15 22:11:36 +000011702 xmlCleanupGlobals();
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000011703 xmlResetLastError();
Daniel Veillard74c0e592003-11-25 07:01:38 +000011704 xmlCleanupThreads(); /* must be last if called not from the main thread */
William M. Brack72ee48d2003-12-30 08:30:19 +000011705 xmlCleanupMemory();
Daniel Veillardd0463562001-10-13 09:15:48 +000011706 xmlParserInitialized = 0;
Owen Taylor3473f882001-02-23 17:55:21 +000011707}
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011708
11709/************************************************************************
11710 * *
11711 * New set (2.6.0) of simpler and more flexible APIs *
11712 * *
11713 ************************************************************************/
11714
11715/**
Daniel Veillarde96a2a42003-09-24 21:23:56 +000011716 * DICT_FREE:
11717 * @str: a string
11718 *
11719 * Free a string if it is not owned by the "dict" dictionnary in the
11720 * current scope
11721 */
11722#define DICT_FREE(str) \
11723 if ((str) && ((!dict) || \
11724 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
11725 xmlFree((char *)(str));
11726
11727/**
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011728 * xmlCtxtReset:
11729 * @ctxt: an XML parser context
11730 *
11731 * Reset a parser context
11732 */
11733void
11734xmlCtxtReset(xmlParserCtxtPtr ctxt)
11735{
11736 xmlParserInputPtr input;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000011737 xmlDictPtr dict = ctxt->dict;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011738
11739 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
11740 xmlFreeInputStream(input);
11741 }
11742 ctxt->inputNr = 0;
11743 ctxt->input = NULL;
11744
11745 ctxt->spaceNr = 0;
11746 ctxt->spaceTab[0] = -1;
11747 ctxt->space = &ctxt->spaceTab[0];
11748
11749
11750 ctxt->nodeNr = 0;
11751 ctxt->node = NULL;
11752
11753 ctxt->nameNr = 0;
11754 ctxt->name = NULL;
11755
Daniel Veillarde96a2a42003-09-24 21:23:56 +000011756 DICT_FREE(ctxt->version);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011757 ctxt->version = NULL;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000011758 DICT_FREE(ctxt->encoding);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011759 ctxt->encoding = NULL;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000011760 DICT_FREE(ctxt->directory);
11761 ctxt->directory = NULL;
11762 DICT_FREE(ctxt->extSubURI);
11763 ctxt->extSubURI = NULL;
11764 DICT_FREE(ctxt->extSubSystem);
11765 ctxt->extSubSystem = NULL;
11766 if (ctxt->myDoc != NULL)
11767 xmlFreeDoc(ctxt->myDoc);
11768 ctxt->myDoc = NULL;
11769
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011770 ctxt->standalone = -1;
11771 ctxt->hasExternalSubset = 0;
11772 ctxt->hasPErefs = 0;
11773 ctxt->html = 0;
11774 ctxt->external = 0;
11775 ctxt->instate = XML_PARSER_START;
11776 ctxt->token = 0;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011777
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011778 ctxt->wellFormed = 1;
11779 ctxt->nsWellFormed = 1;
Daniel Veillardae289182004-01-21 16:00:43 +000011780 ctxt->disableSAX = 0;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011781 ctxt->valid = 1;
Daniel Veillard766c4f92004-03-26 10:48:29 +000011782#if 0
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011783 ctxt->vctxt.userData = ctxt;
11784 ctxt->vctxt.error = xmlParserValidityError;
11785 ctxt->vctxt.warning = xmlParserValidityWarning;
Daniel Veillard766c4f92004-03-26 10:48:29 +000011786#endif
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011787 ctxt->record_info = 0;
11788 ctxt->nbChars = 0;
11789 ctxt->checkIndex = 0;
11790 ctxt->inSubset = 0;
11791 ctxt->errNo = XML_ERR_OK;
11792 ctxt->depth = 0;
11793 ctxt->charset = XML_CHAR_ENCODING_UTF8;
11794 ctxt->catalogs = NULL;
11795 xmlInitNodeInfoSeq(&ctxt->node_seq);
11796
11797 if (ctxt->attsDefault != NULL) {
11798 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
11799 ctxt->attsDefault = NULL;
11800 }
11801 if (ctxt->attsSpecial != NULL) {
11802 xmlHashFree(ctxt->attsSpecial, NULL);
11803 ctxt->attsSpecial = NULL;
11804 }
11805
Daniel Veillard4432df22003-09-28 18:58:27 +000011806#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011807 if (ctxt->catalogs != NULL)
11808 xmlCatalogFreeLocal(ctxt->catalogs);
Daniel Veillard4432df22003-09-28 18:58:27 +000011809#endif
Daniel Veillardcc199e02003-10-24 21:11:48 +000011810 if (ctxt->lastError.code != XML_ERR_OK)
11811 xmlResetError(&ctxt->lastError);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011812}
11813
11814/**
Daniel Veillard9ba8e382003-10-28 21:31:45 +000011815 * xmlCtxtResetPush:
11816 * @ctxt: an XML parser context
11817 * @chunk: a pointer to an array of chars
11818 * @size: number of chars in the array
11819 * @filename: an optional file name or URI
11820 * @encoding: the document encoding, or NULL
11821 *
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000011822 * Reset a push parser context
11823 *
11824 * Returns 0 in case of success and 1 in case of error
Daniel Veillard9ba8e382003-10-28 21:31:45 +000011825 */
11826int
11827xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk,
11828 int size, const char *filename, const char *encoding)
11829{
11830 xmlParserInputPtr inputStream;
11831 xmlParserInputBufferPtr buf;
11832 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
11833
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000011834 if (ctxt == NULL)
11835 return(1);
11836
Daniel Veillard9ba8e382003-10-28 21:31:45 +000011837 if ((encoding == NULL) && (chunk != NULL) && (size >= 4))
11838 enc = xmlDetectCharEncoding((const xmlChar *) chunk, size);
11839
11840 buf = xmlAllocParserInputBuffer(enc);
11841 if (buf == NULL)
11842 return(1);
11843
11844 if (ctxt == NULL) {
11845 xmlFreeParserInputBuffer(buf);
11846 return(1);
11847 }
11848
11849 xmlCtxtReset(ctxt);
11850
11851 if (ctxt->pushTab == NULL) {
11852 ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 *
11853 sizeof(xmlChar *));
11854 if (ctxt->pushTab == NULL) {
11855 xmlErrMemory(ctxt, NULL);
11856 xmlFreeParserInputBuffer(buf);
11857 return(1);
11858 }
11859 }
11860
11861 if (filename == NULL) {
11862 ctxt->directory = NULL;
11863 } else {
11864 ctxt->directory = xmlParserGetDirectory(filename);
11865 }
11866
11867 inputStream = xmlNewInputStream(ctxt);
11868 if (inputStream == NULL) {
11869 xmlFreeParserInputBuffer(buf);
11870 return(1);
11871 }
11872
11873 if (filename == NULL)
11874 inputStream->filename = NULL;
11875 else
11876 inputStream->filename = (char *)
11877 xmlCanonicPath((const xmlChar *) filename);
11878 inputStream->buf = buf;
11879 inputStream->base = inputStream->buf->buffer->content;
11880 inputStream->cur = inputStream->buf->buffer->content;
11881 inputStream->end =
11882 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
11883
11884 inputPush(ctxt, inputStream);
11885
11886 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
11887 (ctxt->input->buf != NULL)) {
11888 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
11889 int cur = ctxt->input->cur - ctxt->input->base;
11890
11891 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
11892
11893 ctxt->input->base = ctxt->input->buf->buffer->content + base;
11894 ctxt->input->cur = ctxt->input->base + cur;
11895 ctxt->input->end =
11896 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->
11897 use];
11898#ifdef DEBUG_PUSH
11899 xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
11900#endif
11901 }
11902
11903 if (encoding != NULL) {
11904 xmlCharEncodingHandlerPtr hdlr;
11905
11906 hdlr = xmlFindCharEncodingHandler(encoding);
11907 if (hdlr != NULL) {
11908 xmlSwitchToEncoding(ctxt, hdlr);
11909 } else {
11910 xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
11911 "Unsupported encoding %s\n", BAD_CAST encoding);
11912 }
11913 } else if (enc != XML_CHAR_ENCODING_NONE) {
11914 xmlSwitchEncoding(ctxt, enc);
11915 }
11916
11917 return(0);
11918}
11919
11920/**
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011921 * xmlCtxtUseOptions:
11922 * @ctxt: an XML parser context
Daniel Veillard87ab1c12003-12-21 13:01:56 +000011923 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011924 *
11925 * Applies the options to the parser context
11926 *
11927 * Returns 0 in case of success, the set of unknown or unimplemented options
11928 * in case of error.
11929 */
11930int
11931xmlCtxtUseOptions(xmlParserCtxtPtr ctxt, int options)
11932{
11933 if (options & XML_PARSE_RECOVER) {
11934 ctxt->recovery = 1;
11935 options -= XML_PARSE_RECOVER;
11936 } else
11937 ctxt->recovery = 0;
11938 if (options & XML_PARSE_DTDLOAD) {
11939 ctxt->loadsubset = XML_DETECT_IDS;
11940 options -= XML_PARSE_DTDLOAD;
11941 } else
11942 ctxt->loadsubset = 0;
11943 if (options & XML_PARSE_DTDATTR) {
11944 ctxt->loadsubset |= XML_COMPLETE_ATTRS;
11945 options -= XML_PARSE_DTDATTR;
11946 }
11947 if (options & XML_PARSE_NOENT) {
11948 ctxt->replaceEntities = 1;
11949 /* ctxt->loadsubset |= XML_DETECT_IDS; */
11950 options -= XML_PARSE_NOENT;
11951 } else
11952 ctxt->replaceEntities = 0;
11953 if (options & XML_PARSE_NOWARNING) {
11954 ctxt->sax->warning = NULL;
11955 options -= XML_PARSE_NOWARNING;
11956 }
11957 if (options & XML_PARSE_NOERROR) {
11958 ctxt->sax->error = NULL;
11959 ctxt->sax->fatalError = NULL;
11960 options -= XML_PARSE_NOERROR;
11961 }
11962 if (options & XML_PARSE_PEDANTIC) {
11963 ctxt->pedantic = 1;
11964 options -= XML_PARSE_PEDANTIC;
11965 } else
11966 ctxt->pedantic = 0;
11967 if (options & XML_PARSE_NOBLANKS) {
11968 ctxt->keepBlanks = 0;
11969 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
11970 options -= XML_PARSE_NOBLANKS;
11971 } else
11972 ctxt->keepBlanks = 1;
11973 if (options & XML_PARSE_DTDVALID) {
11974 ctxt->validate = 1;
11975 if (options & XML_PARSE_NOWARNING)
11976 ctxt->vctxt.warning = NULL;
11977 if (options & XML_PARSE_NOERROR)
11978 ctxt->vctxt.error = NULL;
11979 options -= XML_PARSE_DTDVALID;
11980 } else
11981 ctxt->validate = 0;
Daniel Veillard81273902003-09-30 00:43:48 +000011982#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011983 if (options & XML_PARSE_SAX1) {
11984 ctxt->sax->startElement = xmlSAX2StartElement;
11985 ctxt->sax->endElement = xmlSAX2EndElement;
11986 ctxt->sax->startElementNs = NULL;
11987 ctxt->sax->endElementNs = NULL;
11988 ctxt->sax->initialized = 1;
11989 options -= XML_PARSE_SAX1;
11990 }
Daniel Veillard81273902003-09-30 00:43:48 +000011991#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +000011992 if (options & XML_PARSE_NODICT) {
11993 ctxt->dictNames = 0;
11994 options -= XML_PARSE_NODICT;
11995 } else {
11996 ctxt->dictNames = 1;
11997 }
Daniel Veillarddca8cc72003-09-26 13:53:14 +000011998 if (options & XML_PARSE_NOCDATA) {
11999 ctxt->sax->cdataBlock = NULL;
12000 options -= XML_PARSE_NOCDATA;
12001 }
12002 if (options & XML_PARSE_NSCLEAN) {
12003 ctxt->options |= XML_PARSE_NSCLEAN;
12004 options -= XML_PARSE_NSCLEAN;
12005 }
Daniel Veillard61b93382003-11-03 14:28:31 +000012006 if (options & XML_PARSE_NONET) {
12007 ctxt->options |= XML_PARSE_NONET;
12008 options -= XML_PARSE_NONET;
12009 }
Daniel Veillard7ec29972003-10-31 14:36:36 +000012010 ctxt->linenumbers = 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012011 return (options);
12012}
12013
12014/**
12015 * xmlDoRead:
12016 * @ctxt: an XML parser context
Daniel Veillard60942de2003-09-25 21:05:58 +000012017 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012018 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012019 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012020 * @reuse: keep the context for reuse
12021 *
12022 * Common front-end for the xmlRead functions
12023 *
12024 * Returns the resulting document tree or NULL
12025 */
12026static xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012027xmlDoRead(xmlParserCtxtPtr ctxt, const char *URL, const char *encoding,
12028 int options, int reuse)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012029{
12030 xmlDocPtr ret;
12031
12032 xmlCtxtUseOptions(ctxt, options);
12033 if (encoding != NULL) {
12034 xmlCharEncodingHandlerPtr hdlr;
12035
12036 hdlr = xmlFindCharEncodingHandler(encoding);
12037 if (hdlr != NULL)
12038 xmlSwitchToEncoding(ctxt, hdlr);
12039 }
Daniel Veillard60942de2003-09-25 21:05:58 +000012040 if ((URL != NULL) && (ctxt->input != NULL) &&
12041 (ctxt->input->filename == NULL))
12042 ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012043 xmlParseDocument(ctxt);
12044 if ((ctxt->wellFormed) || ctxt->recovery)
12045 ret = ctxt->myDoc;
12046 else {
12047 ret = NULL;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012048 if (ctxt->myDoc != NULL) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012049 xmlFreeDoc(ctxt->myDoc);
12050 }
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012051 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012052 ctxt->myDoc = NULL;
12053 if (!reuse) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012054 xmlFreeParserCtxt(ctxt);
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012055 }
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012056
12057 return (ret);
12058}
12059
12060/**
12061 * xmlReadDoc:
12062 * @cur: a pointer to a zero terminated string
Daniel Veillard60942de2003-09-25 21:05:58 +000012063 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012064 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012065 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012066 *
12067 * parse an XML in-memory document and build a tree.
12068 *
12069 * Returns the resulting document tree
12070 */
12071xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012072xmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012073{
12074 xmlParserCtxtPtr ctxt;
12075
12076 if (cur == NULL)
12077 return (NULL);
12078
12079 ctxt = xmlCreateDocParserCtxt(cur);
12080 if (ctxt == NULL)
12081 return (NULL);
Daniel Veillard60942de2003-09-25 21:05:58 +000012082 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012083}
12084
12085/**
12086 * xmlReadFile:
12087 * @filename: a file or URL
12088 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012089 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012090 *
12091 * parse an XML file from the filesystem or the network.
12092 *
12093 * Returns the resulting document tree
12094 */
12095xmlDocPtr
12096xmlReadFile(const char *filename, const char *encoding, int options)
12097{
12098 xmlParserCtxtPtr ctxt;
12099
Daniel Veillard61b93382003-11-03 14:28:31 +000012100 ctxt = xmlCreateURLParserCtxt(filename, options);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012101 if (ctxt == NULL)
12102 return (NULL);
Daniel Veillard60942de2003-09-25 21:05:58 +000012103 return (xmlDoRead(ctxt, NULL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012104}
12105
12106/**
12107 * xmlReadMemory:
12108 * @buffer: a pointer to a char array
12109 * @size: the size of the array
Daniel Veillard60942de2003-09-25 21:05:58 +000012110 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012111 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012112 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012113 *
12114 * parse an XML in-memory document and build a tree.
12115 *
12116 * Returns the resulting document tree
12117 */
12118xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012119xmlReadMemory(const char *buffer, int size, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012120{
12121 xmlParserCtxtPtr ctxt;
12122
12123 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
12124 if (ctxt == NULL)
12125 return (NULL);
Daniel Veillard60942de2003-09-25 21:05:58 +000012126 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012127}
12128
12129/**
12130 * xmlReadFd:
12131 * @fd: an open file descriptor
Daniel Veillard60942de2003-09-25 21:05:58 +000012132 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012133 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012134 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012135 *
12136 * parse an XML from a file descriptor and build a tree.
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012137 * NOTE that the file descriptor will not be closed when the
12138 * reader is closed or reset.
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012139 *
12140 * Returns the resulting document tree
12141 */
12142xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012143xmlReadFd(int fd, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012144{
12145 xmlParserCtxtPtr ctxt;
12146 xmlParserInputBufferPtr input;
12147 xmlParserInputPtr stream;
12148
12149 if (fd < 0)
12150 return (NULL);
12151
12152 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
12153 if (input == NULL)
12154 return (NULL);
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012155 input->closecallback = NULL;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012156 ctxt = xmlNewParserCtxt();
12157 if (ctxt == NULL) {
12158 xmlFreeParserInputBuffer(input);
12159 return (NULL);
12160 }
12161 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12162 if (stream == NULL) {
12163 xmlFreeParserInputBuffer(input);
12164 xmlFreeParserCtxt(ctxt);
12165 return (NULL);
12166 }
12167 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012168 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012169}
12170
12171/**
12172 * xmlReadIO:
12173 * @ioread: an I/O read function
12174 * @ioclose: an I/O close function
12175 * @ioctx: an I/O handler
Daniel Veillard60942de2003-09-25 21:05:58 +000012176 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012177 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012178 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012179 *
12180 * parse an XML document from I/O functions and source and build a tree.
12181 *
12182 * Returns the resulting document tree
12183 */
12184xmlDocPtr
12185xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
Daniel Veillard60942de2003-09-25 21:05:58 +000012186 void *ioctx, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012187{
12188 xmlParserCtxtPtr ctxt;
12189 xmlParserInputBufferPtr input;
12190 xmlParserInputPtr stream;
12191
12192 if (ioread == NULL)
12193 return (NULL);
12194
12195 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
12196 XML_CHAR_ENCODING_NONE);
12197 if (input == NULL)
12198 return (NULL);
12199 ctxt = xmlNewParserCtxt();
12200 if (ctxt == NULL) {
12201 xmlFreeParserInputBuffer(input);
12202 return (NULL);
12203 }
12204 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12205 if (stream == NULL) {
12206 xmlFreeParserInputBuffer(input);
12207 xmlFreeParserCtxt(ctxt);
12208 return (NULL);
12209 }
12210 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012211 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012212}
12213
12214/**
12215 * xmlCtxtReadDoc:
12216 * @ctxt: an XML parser context
12217 * @cur: a pointer to a zero terminated string
Daniel Veillard60942de2003-09-25 21:05:58 +000012218 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012219 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012220 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012221 *
12222 * parse an XML in-memory document and build a tree.
12223 * This reuses the existing @ctxt parser context
12224 *
12225 * Returns the resulting document tree
12226 */
12227xmlDocPtr
12228xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur,
Daniel Veillard60942de2003-09-25 21:05:58 +000012229 const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012230{
12231 xmlParserInputPtr stream;
12232
12233 if (cur == NULL)
12234 return (NULL);
12235 if (ctxt == NULL)
12236 return (NULL);
12237
12238 xmlCtxtReset(ctxt);
12239
12240 stream = xmlNewStringInputStream(ctxt, cur);
12241 if (stream == NULL) {
12242 return (NULL);
12243 }
12244 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012245 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012246}
12247
12248/**
12249 * xmlCtxtReadFile:
12250 * @ctxt: an XML parser context
12251 * @filename: a file or URL
12252 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012253 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012254 *
12255 * parse an XML file from the filesystem or the network.
12256 * This reuses the existing @ctxt parser context
12257 *
12258 * Returns the resulting document tree
12259 */
12260xmlDocPtr
12261xmlCtxtReadFile(xmlParserCtxtPtr ctxt, const char *filename,
12262 const char *encoding, int options)
12263{
12264 xmlParserInputPtr stream;
12265
12266 if (filename == NULL)
12267 return (NULL);
12268 if (ctxt == NULL)
12269 return (NULL);
12270
12271 xmlCtxtReset(ctxt);
12272
12273 stream = xmlNewInputFromFile(ctxt, filename);
12274 if (stream == NULL) {
12275 return (NULL);
12276 }
12277 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012278 return (xmlDoRead(ctxt, NULL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012279}
12280
12281/**
12282 * xmlCtxtReadMemory:
12283 * @ctxt: an XML parser context
12284 * @buffer: a pointer to a char array
12285 * @size: the size of the array
Daniel Veillard60942de2003-09-25 21:05:58 +000012286 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012287 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012288 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012289 *
12290 * parse an XML in-memory document and build a tree.
12291 * This reuses the existing @ctxt parser context
12292 *
12293 * Returns the resulting document tree
12294 */
12295xmlDocPtr
12296xmlCtxtReadMemory(xmlParserCtxtPtr ctxt, const char *buffer, int size,
Daniel Veillard60942de2003-09-25 21:05:58 +000012297 const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012298{
12299 xmlParserInputBufferPtr input;
12300 xmlParserInputPtr stream;
12301
12302 if (ctxt == NULL)
12303 return (NULL);
12304 if (buffer == NULL)
12305 return (NULL);
12306
12307 xmlCtxtReset(ctxt);
12308
12309 input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
12310 if (input == NULL) {
12311 return(NULL);
12312 }
12313
12314 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12315 if (stream == NULL) {
12316 xmlFreeParserInputBuffer(input);
12317 return(NULL);
12318 }
12319
12320 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012321 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012322}
12323
12324/**
12325 * xmlCtxtReadFd:
12326 * @ctxt: an XML parser context
12327 * @fd: an open file descriptor
Daniel Veillard60942de2003-09-25 21:05:58 +000012328 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012329 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012330 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012331 *
12332 * parse an XML from a file descriptor and build a tree.
12333 * This reuses the existing @ctxt parser context
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012334 * NOTE that the file descriptor will not be closed when the
12335 * reader is closed or reset.
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012336 *
12337 * Returns the resulting document tree
12338 */
12339xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012340xmlCtxtReadFd(xmlParserCtxtPtr ctxt, int fd,
12341 const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012342{
12343 xmlParserInputBufferPtr input;
12344 xmlParserInputPtr stream;
12345
12346 if (fd < 0)
12347 return (NULL);
12348 if (ctxt == NULL)
12349 return (NULL);
12350
12351 xmlCtxtReset(ctxt);
12352
12353
12354 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
12355 if (input == NULL)
12356 return (NULL);
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012357 input->closecallback = NULL;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012358 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12359 if (stream == NULL) {
12360 xmlFreeParserInputBuffer(input);
12361 return (NULL);
12362 }
12363 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012364 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012365}
12366
12367/**
12368 * xmlCtxtReadIO:
12369 * @ctxt: an XML parser context
12370 * @ioread: an I/O read function
12371 * @ioclose: an I/O close function
12372 * @ioctx: an I/O handler
Daniel Veillard60942de2003-09-25 21:05:58 +000012373 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012374 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012375 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012376 *
12377 * parse an XML document from I/O functions and source and build a tree.
12378 * This reuses the existing @ctxt parser context
12379 *
12380 * Returns the resulting document tree
12381 */
12382xmlDocPtr
12383xmlCtxtReadIO(xmlParserCtxtPtr ctxt, xmlInputReadCallback ioread,
12384 xmlInputCloseCallback ioclose, void *ioctx,
Daniel Veillard60942de2003-09-25 21:05:58 +000012385 const char *URL,
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012386 const char *encoding, int options)
12387{
12388 xmlParserInputBufferPtr input;
12389 xmlParserInputPtr stream;
12390
12391 if (ioread == NULL)
12392 return (NULL);
12393 if (ctxt == NULL)
12394 return (NULL);
12395
12396 xmlCtxtReset(ctxt);
12397
12398 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
12399 XML_CHAR_ENCODING_NONE);
12400 if (input == NULL)
12401 return (NULL);
12402 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12403 if (stream == NULL) {
12404 xmlFreeParserInputBuffer(input);
12405 return (NULL);
12406 }
12407 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012408 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012409}