blob: fc8e59d28a4587d8be2962c9f8d91096b66653cc [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);
William M. Brack9f797ab2004-07-28 07:40:12 +0000608 if ((ctxt->str_xml==NULL) || (ctxt->str_xmlns==NULL) ||
609 (ctxt->str_xml_ns == NULL)) {
610 xmlErrMemory(ctxt, NULL);
611 }
Daniel Veillarde57ec792003-09-10 10:50:59 +0000612}
613
Daniel Veillarde57ec792003-09-10 10:50:59 +0000614typedef struct _xmlDefAttrs xmlDefAttrs;
615typedef xmlDefAttrs *xmlDefAttrsPtr;
616struct _xmlDefAttrs {
617 int nbAttrs; /* number of defaulted attributes on that element */
618 int maxAttrs; /* the size of the array */
619 const xmlChar *values[4]; /* array of localname/prefix/values */
620};
Daniel Veillarde57ec792003-09-10 10:50:59 +0000621
622/**
623 * xmlAddDefAttrs:
624 * @ctxt: an XML parser context
625 * @fullname: the element fullname
626 * @fullattr: the attribute fullname
627 * @value: the attribute value
628 *
629 * Add a defaulted attribute for an element
630 */
631static void
632xmlAddDefAttrs(xmlParserCtxtPtr ctxt,
633 const xmlChar *fullname,
634 const xmlChar *fullattr,
635 const xmlChar *value) {
636 xmlDefAttrsPtr defaults;
637 int len;
638 const xmlChar *name;
639 const xmlChar *prefix;
640
641 if (ctxt->attsDefault == NULL) {
642 ctxt->attsDefault = xmlHashCreate(10);
643 if (ctxt->attsDefault == NULL)
644 goto mem_error;
645 }
646
647 /*
Daniel Veillard079f6a72004-09-23 13:15:03 +0000648 * split the element name into prefix:localname , the string found
649 * are within the DTD and then not associated to namespace names.
Daniel Veillarde57ec792003-09-10 10:50:59 +0000650 */
651 name = xmlSplitQName3(fullname, &len);
652 if (name == NULL) {
653 name = xmlDictLookup(ctxt->dict, fullname, -1);
654 prefix = NULL;
655 } else {
656 name = xmlDictLookup(ctxt->dict, name, -1);
657 prefix = xmlDictLookup(ctxt->dict, fullname, len);
658 }
659
660 /*
661 * make sure there is some storage
662 */
663 defaults = xmlHashLookup2(ctxt->attsDefault, name, prefix);
664 if (defaults == NULL) {
665 defaults = (xmlDefAttrsPtr) xmlMalloc(sizeof(xmlDefAttrs) +
Daniel Veillard079f6a72004-09-23 13:15:03 +0000666 (4 * 4) * sizeof(const xmlChar *));
Daniel Veillarde57ec792003-09-10 10:50:59 +0000667 if (defaults == NULL)
668 goto mem_error;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000669 defaults->nbAttrs = 0;
Daniel Veillard079f6a72004-09-23 13:15:03 +0000670 defaults->maxAttrs = 4;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000671 xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, defaults, NULL);
672 } else if (defaults->nbAttrs >= defaults->maxAttrs) {
Daniel Veillard079f6a72004-09-23 13:15:03 +0000673 xmlDefAttrsPtr temp;
674
675 temp = (xmlDefAttrsPtr) xmlRealloc(defaults, sizeof(xmlDefAttrs) +
Daniel Veillarde57ec792003-09-10 10:50:59 +0000676 (2 * defaults->maxAttrs * 4) * sizeof(const xmlChar *));
Daniel Veillard079f6a72004-09-23 13:15:03 +0000677 if (temp == NULL)
Daniel Veillarde57ec792003-09-10 10:50:59 +0000678 goto mem_error;
Daniel Veillard079f6a72004-09-23 13:15:03 +0000679 defaults = temp;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000680 defaults->maxAttrs *= 2;
681 xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, defaults, NULL);
682 }
683
684 /*
685 * plit the element name into prefix:localname , the string found
686 * are within the DTD and hen not associated to namespace names.
687 */
688 name = xmlSplitQName3(fullattr, &len);
689 if (name == NULL) {
690 name = xmlDictLookup(ctxt->dict, fullattr, -1);
691 prefix = NULL;
692 } else {
693 name = xmlDictLookup(ctxt->dict, name, -1);
694 prefix = xmlDictLookup(ctxt->dict, fullattr, len);
695 }
696
697 defaults->values[4 * defaults->nbAttrs] = name;
698 defaults->values[4 * defaults->nbAttrs + 1] = prefix;
699 /* intern the string and precompute the end */
700 len = xmlStrlen(value);
701 value = xmlDictLookup(ctxt->dict, value, len);
702 defaults->values[4 * defaults->nbAttrs + 2] = value;
703 defaults->values[4 * defaults->nbAttrs + 3] = value + len;
704 defaults->nbAttrs++;
705
706 return;
707
708mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000709 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000710 return;
711}
712
Daniel Veillard8e36e6a2003-09-10 10:50:59 +0000713/**
714 * xmlAddSpecialAttr:
715 * @ctxt: an XML parser context
716 * @fullname: the element fullname
717 * @fullattr: the attribute fullname
718 * @type: the attribute type
719 *
720 * Register that this attribute is not CDATA
721 */
722static void
723xmlAddSpecialAttr(xmlParserCtxtPtr ctxt,
724 const xmlChar *fullname,
725 const xmlChar *fullattr,
726 int type)
727{
728 if (ctxt->attsSpecial == NULL) {
729 ctxt->attsSpecial = xmlHashCreate(10);
730 if (ctxt->attsSpecial == NULL)
731 goto mem_error;
732 }
733
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000734 xmlHashAddEntry2(ctxt->attsSpecial, fullname, fullattr,
735 (void *) (long) type);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +0000736 return;
737
738mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000739 xmlErrMemory(ctxt, NULL);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +0000740 return;
741}
742
Daniel Veillard4432df22003-09-28 18:58:27 +0000743/**
744 * xmlCheckLanguageID:
745 * @lang: pointer to the string value
746 *
747 * Checks that the value conforms to the LanguageID production:
748 *
749 * NOTE: this is somewhat deprecated, those productions were removed from
750 * the XML Second edition.
751 *
752 * [33] LanguageID ::= Langcode ('-' Subcode)*
753 * [34] Langcode ::= ISO639Code | IanaCode | UserCode
754 * [35] ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z])
755 * [36] IanaCode ::= ('i' | 'I') '-' ([a-z] | [A-Z])+
756 * [37] UserCode ::= ('x' | 'X') '-' ([a-z] | [A-Z])+
757 * [38] Subcode ::= ([a-z] | [A-Z])+
758 *
759 * Returns 1 if correct 0 otherwise
760 **/
761int
762xmlCheckLanguageID(const xmlChar * lang)
763{
764 const xmlChar *cur = lang;
765
766 if (cur == NULL)
767 return (0);
768 if (((cur[0] == 'i') && (cur[1] == '-')) ||
769 ((cur[0] == 'I') && (cur[1] == '-'))) {
770 /*
771 * IANA code
772 */
773 cur += 2;
774 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
775 ((cur[0] >= 'a') && (cur[0] <= 'z')))
776 cur++;
777 } else if (((cur[0] == 'x') && (cur[1] == '-')) ||
778 ((cur[0] == 'X') && (cur[1] == '-'))) {
779 /*
780 * User code
781 */
782 cur += 2;
783 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
784 ((cur[0] >= 'a') && (cur[0] <= 'z')))
785 cur++;
786 } else if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
787 ((cur[0] >= 'a') && (cur[0] <= 'z'))) {
788 /*
789 * ISO639
790 */
791 cur++;
792 if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
793 ((cur[0] >= 'a') && (cur[0] <= 'z')))
794 cur++;
795 else
796 return (0);
797 } else
798 return (0);
799 while (cur[0] != 0) { /* non input consuming */
800 if (cur[0] != '-')
801 return (0);
802 cur++;
803 if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
804 ((cur[0] >= 'a') && (cur[0] <= 'z')))
805 cur++;
806 else
807 return (0);
808 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
809 ((cur[0] >= 'a') && (cur[0] <= 'z')))
810 cur++;
811 }
812 return (1);
813}
814
Owen Taylor3473f882001-02-23 17:55:21 +0000815/************************************************************************
816 * *
817 * Parser stacks related functions and macros *
818 * *
819 ************************************************************************/
820
821xmlEntityPtr xmlParseStringEntityRef(xmlParserCtxtPtr ctxt,
822 const xmlChar ** str);
823
Daniel Veillard0fb18932003-09-07 09:14:37 +0000824#ifdef SAX2
825/**
826 * nsPush:
827 * @ctxt: an XML parser context
828 * @prefix: the namespace prefix or NULL
829 * @URL: the namespace name
830 *
831 * Pushes a new parser namespace on top of the ns stack
832 *
William M. Brack7b9154b2003-09-27 19:23:50 +0000833 * Returns -1 in case of error, -2 if the namespace should be discarded
834 * and the index in the stack otherwise.
Daniel Veillard0fb18932003-09-07 09:14:37 +0000835 */
836static int
837nsPush(xmlParserCtxtPtr ctxt, const xmlChar *prefix, const xmlChar *URL)
838{
Daniel Veillarddca8cc72003-09-26 13:53:14 +0000839 if (ctxt->options & XML_PARSE_NSCLEAN) {
840 int i;
841 for (i = 0;i < ctxt->nsNr;i += 2) {
842 if (ctxt->nsTab[i] == prefix) {
843 /* in scope */
844 if (ctxt->nsTab[i + 1] == URL)
845 return(-2);
846 /* out of scope keep it */
847 break;
848 }
849 }
850 }
Daniel Veillard0fb18932003-09-07 09:14:37 +0000851 if ((ctxt->nsMax == 0) || (ctxt->nsTab == NULL)) {
852 ctxt->nsMax = 10;
853 ctxt->nsNr = 0;
854 ctxt->nsTab = (const xmlChar **)
855 xmlMalloc(ctxt->nsMax * sizeof(xmlChar *));
856 if (ctxt->nsTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000857 xmlErrMemory(ctxt, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +0000858 ctxt->nsMax = 0;
859 return (-1);
860 }
861 } else if (ctxt->nsNr >= ctxt->nsMax) {
862 ctxt->nsMax *= 2;
863 ctxt->nsTab = (const xmlChar **)
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000864 xmlRealloc((char *) ctxt->nsTab,
Daniel Veillard0fb18932003-09-07 09:14:37 +0000865 ctxt->nsMax * sizeof(ctxt->nsTab[0]));
866 if (ctxt->nsTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000867 xmlErrMemory(ctxt, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +0000868 ctxt->nsMax /= 2;
869 return (-1);
870 }
871 }
872 ctxt->nsTab[ctxt->nsNr++] = prefix;
873 ctxt->nsTab[ctxt->nsNr++] = URL;
874 return (ctxt->nsNr);
875}
876/**
877 * nsPop:
878 * @ctxt: an XML parser context
879 * @nr: the number to pop
880 *
881 * Pops the top @nr parser prefix/namespace from the ns stack
882 *
883 * Returns the number of namespaces removed
884 */
885static int
886nsPop(xmlParserCtxtPtr ctxt, int nr)
887{
888 int i;
889
890 if (ctxt->nsTab == NULL) return(0);
891 if (ctxt->nsNr < nr) {
892 xmlGenericError(xmlGenericErrorContext, "Pbm popping %d NS\n", nr);
893 nr = ctxt->nsNr;
894 }
895 if (ctxt->nsNr <= 0)
896 return (0);
897
898 for (i = 0;i < nr;i++) {
899 ctxt->nsNr--;
900 ctxt->nsTab[ctxt->nsNr] = NULL;
901 }
902 return(nr);
903}
904#endif
905
906static int
907xmlCtxtGrowAttrs(xmlParserCtxtPtr ctxt, int nr) {
908 const xmlChar **atts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000909 int *attallocs;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000910 int maxatts;
911
912 if (ctxt->atts == NULL) {
Daniel Veillarde57ec792003-09-10 10:50:59 +0000913 maxatts = 55; /* allow for 10 attrs by default */
Daniel Veillard0fb18932003-09-07 09:14:37 +0000914 atts = (const xmlChar **)
915 xmlMalloc(maxatts * sizeof(xmlChar *));
Daniel Veillarde57ec792003-09-10 10:50:59 +0000916 if (atts == NULL) goto mem_error;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000917 ctxt->atts = atts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000918 attallocs = (int *) xmlMalloc((maxatts / 5) * sizeof(int));
919 if (attallocs == NULL) goto mem_error;
920 ctxt->attallocs = attallocs;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000921 ctxt->maxatts = maxatts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000922 } else if (nr + 5 > ctxt->maxatts) {
923 maxatts = (nr + 5) * 2;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000924 atts = (const xmlChar **) xmlRealloc((void *) ctxt->atts,
925 maxatts * sizeof(const xmlChar *));
Daniel Veillarde57ec792003-09-10 10:50:59 +0000926 if (atts == NULL) goto mem_error;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000927 ctxt->atts = atts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000928 attallocs = (int *) xmlRealloc((void *) ctxt->attallocs,
929 (maxatts / 5) * sizeof(int));
930 if (attallocs == NULL) goto mem_error;
931 ctxt->attallocs = attallocs;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000932 ctxt->maxatts = maxatts;
933 }
934 return(ctxt->maxatts);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000935mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000936 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000937 return(-1);
Daniel Veillard0fb18932003-09-07 09:14:37 +0000938}
939
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000940/**
941 * inputPush:
942 * @ctxt: an XML parser context
Daniel Veillard9d06d302002-01-22 18:15:52 +0000943 * @value: the parser input
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000944 *
945 * Pushes a new parser input on top of the input stack
Daniel Veillard9d06d302002-01-22 18:15:52 +0000946 *
947 * Returns 0 in case of error, the index in the stack otherwise
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000948 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000949int
Daniel Veillard1c732d22002-11-30 11:22:59 +0000950inputPush(xmlParserCtxtPtr ctxt, xmlParserInputPtr value)
951{
952 if (ctxt->inputNr >= ctxt->inputMax) {
953 ctxt->inputMax *= 2;
954 ctxt->inputTab =
955 (xmlParserInputPtr *) xmlRealloc(ctxt->inputTab,
956 ctxt->inputMax *
957 sizeof(ctxt->inputTab[0]));
958 if (ctxt->inputTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000959 xmlErrMemory(ctxt, NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +0000960 return (0);
961 }
962 }
963 ctxt->inputTab[ctxt->inputNr] = value;
964 ctxt->input = value;
965 return (ctxt->inputNr++);
966}
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000967/**
Daniel Veillard1c732d22002-11-30 11:22:59 +0000968 * inputPop:
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000969 * @ctxt: an XML parser context
970 *
Daniel Veillard1c732d22002-11-30 11:22:59 +0000971 * Pops the top parser input from the input stack
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000972 *
Daniel Veillard1c732d22002-11-30 11:22:59 +0000973 * Returns the input just removed
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000974 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000975xmlParserInputPtr
Daniel Veillard1c732d22002-11-30 11:22:59 +0000976inputPop(xmlParserCtxtPtr ctxt)
977{
978 xmlParserInputPtr ret;
979
980 if (ctxt->inputNr <= 0)
981 return (0);
982 ctxt->inputNr--;
983 if (ctxt->inputNr > 0)
984 ctxt->input = ctxt->inputTab[ctxt->inputNr - 1];
985 else
986 ctxt->input = NULL;
987 ret = ctxt->inputTab[ctxt->inputNr];
988 ctxt->inputTab[ctxt->inputNr] = 0;
989 return (ret);
990}
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000991/**
992 * nodePush:
993 * @ctxt: an XML parser context
Daniel Veillard9d06d302002-01-22 18:15:52 +0000994 * @value: the element node
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000995 *
996 * Pushes a new element node on top of the node stack
Daniel Veillard9d06d302002-01-22 18:15:52 +0000997 *
998 * Returns 0 in case of error, the index in the stack otherwise
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000999 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +00001000int
Daniel Veillard1c732d22002-11-30 11:22:59 +00001001nodePush(xmlParserCtxtPtr ctxt, xmlNodePtr value)
1002{
1003 if (ctxt->nodeNr >= ctxt->nodeMax) {
1004 ctxt->nodeMax *= 2;
1005 ctxt->nodeTab =
1006 (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
1007 ctxt->nodeMax *
1008 sizeof(ctxt->nodeTab[0]));
1009 if (ctxt->nodeTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001010 xmlErrMemory(ctxt, NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +00001011 return (0);
1012 }
1013 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00001014 if (((unsigned int) ctxt->nodeNr) > xmlParserMaxDepth) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001015 xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard4aede2e2003-10-17 12:43:59 +00001016 "Excessive depth in document: change xmlParserMaxDepth = %d\n",
1017 xmlParserMaxDepth);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00001018 ctxt->instate = XML_PARSER_EOF;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00001019 return(0);
1020 }
Daniel Veillard1c732d22002-11-30 11:22:59 +00001021 ctxt->nodeTab[ctxt->nodeNr] = value;
1022 ctxt->node = value;
1023 return (ctxt->nodeNr++);
1024}
1025/**
1026 * nodePop:
1027 * @ctxt: an XML parser context
1028 *
1029 * Pops the top element node from the node stack
1030 *
1031 * Returns the node just removed
Owen Taylor3473f882001-02-23 17:55:21 +00001032 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +00001033xmlNodePtr
Daniel Veillard1c732d22002-11-30 11:22:59 +00001034nodePop(xmlParserCtxtPtr ctxt)
1035{
1036 xmlNodePtr ret;
1037
1038 if (ctxt->nodeNr <= 0)
1039 return (0);
1040 ctxt->nodeNr--;
1041 if (ctxt->nodeNr > 0)
1042 ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
1043 else
1044 ctxt->node = NULL;
1045 ret = ctxt->nodeTab[ctxt->nodeNr];
1046 ctxt->nodeTab[ctxt->nodeNr] = 0;
1047 return (ret);
1048}
Daniel Veillarda2351322004-06-27 12:08:10 +00001049
1050#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard1c732d22002-11-30 11:22:59 +00001051/**
Daniel Veillarde57ec792003-09-10 10:50:59 +00001052 * nameNsPush:
1053 * @ctxt: an XML parser context
1054 * @value: the element name
1055 * @prefix: the element prefix
1056 * @URI: the element namespace name
1057 *
1058 * Pushes a new element name/prefix/URL on top of the name stack
1059 *
1060 * Returns -1 in case of error, the index in the stack otherwise
1061 */
1062static int
1063nameNsPush(xmlParserCtxtPtr ctxt, const xmlChar * value,
1064 const xmlChar *prefix, const xmlChar *URI, int nsNr)
1065{
1066 if (ctxt->nameNr >= ctxt->nameMax) {
1067 const xmlChar * *tmp;
1068 void **tmp2;
1069 ctxt->nameMax *= 2;
1070 tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab,
1071 ctxt->nameMax *
1072 sizeof(ctxt->nameTab[0]));
1073 if (tmp == NULL) {
1074 ctxt->nameMax /= 2;
1075 goto mem_error;
1076 }
1077 ctxt->nameTab = tmp;
1078 tmp2 = (void **) xmlRealloc((void * *)ctxt->pushTab,
1079 ctxt->nameMax * 3 *
1080 sizeof(ctxt->pushTab[0]));
1081 if (tmp2 == NULL) {
1082 ctxt->nameMax /= 2;
1083 goto mem_error;
1084 }
1085 ctxt->pushTab = tmp2;
1086 }
1087 ctxt->nameTab[ctxt->nameNr] = value;
1088 ctxt->name = value;
1089 ctxt->pushTab[ctxt->nameNr * 3] = (void *) prefix;
1090 ctxt->pushTab[ctxt->nameNr * 3 + 1] = (void *) URI;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001091 ctxt->pushTab[ctxt->nameNr * 3 + 2] = (void *) (long) nsNr;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001092 return (ctxt->nameNr++);
1093mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001094 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001095 return (-1);
1096}
1097/**
1098 * nameNsPop:
1099 * @ctxt: an XML parser context
1100 *
1101 * Pops the top element/prefix/URI name from the name stack
1102 *
1103 * Returns the name just removed
1104 */
1105static const xmlChar *
1106nameNsPop(xmlParserCtxtPtr ctxt)
1107{
1108 const xmlChar *ret;
1109
1110 if (ctxt->nameNr <= 0)
1111 return (0);
1112 ctxt->nameNr--;
1113 if (ctxt->nameNr > 0)
1114 ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
1115 else
1116 ctxt->name = NULL;
1117 ret = ctxt->nameTab[ctxt->nameNr];
1118 ctxt->nameTab[ctxt->nameNr] = NULL;
1119 return (ret);
1120}
Daniel Veillarda2351322004-06-27 12:08:10 +00001121#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001122
1123/**
Daniel Veillard1c732d22002-11-30 11:22:59 +00001124 * namePush:
1125 * @ctxt: an XML parser context
1126 * @value: the element name
1127 *
1128 * Pushes a new element name on top of the name stack
1129 *
Daniel Veillarde57ec792003-09-10 10:50:59 +00001130 * Returns -1 in case of error, the index in the stack otherwise
Daniel Veillard1c732d22002-11-30 11:22:59 +00001131 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +00001132int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001133namePush(xmlParserCtxtPtr ctxt, const xmlChar * value)
Daniel Veillard1c732d22002-11-30 11:22:59 +00001134{
1135 if (ctxt->nameNr >= ctxt->nameMax) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00001136 const xmlChar * *tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001137 ctxt->nameMax *= 2;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001138 tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab,
Daniel Veillard1c732d22002-11-30 11:22:59 +00001139 ctxt->nameMax *
1140 sizeof(ctxt->nameTab[0]));
Daniel Veillarde57ec792003-09-10 10:50:59 +00001141 if (tmp == NULL) {
1142 ctxt->nameMax /= 2;
1143 goto mem_error;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001144 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001145 ctxt->nameTab = tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001146 }
1147 ctxt->nameTab[ctxt->nameNr] = value;
1148 ctxt->name = value;
1149 return (ctxt->nameNr++);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001150mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001151 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001152 return (-1);
Daniel Veillard1c732d22002-11-30 11:22:59 +00001153}
1154/**
1155 * namePop:
1156 * @ctxt: an XML parser context
1157 *
1158 * Pops the top element name from the name stack
1159 *
1160 * Returns the name just removed
1161 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +00001162const xmlChar *
Daniel Veillard1c732d22002-11-30 11:22:59 +00001163namePop(xmlParserCtxtPtr ctxt)
1164{
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001165 const xmlChar *ret;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001166
1167 if (ctxt->nameNr <= 0)
1168 return (0);
1169 ctxt->nameNr--;
1170 if (ctxt->nameNr > 0)
1171 ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
1172 else
1173 ctxt->name = NULL;
1174 ret = ctxt->nameTab[ctxt->nameNr];
1175 ctxt->nameTab[ctxt->nameNr] = 0;
1176 return (ret);
1177}
Owen Taylor3473f882001-02-23 17:55:21 +00001178
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001179static int spacePush(xmlParserCtxtPtr ctxt, int val) {
Owen Taylor3473f882001-02-23 17:55:21 +00001180 if (ctxt->spaceNr >= ctxt->spaceMax) {
1181 ctxt->spaceMax *= 2;
1182 ctxt->spaceTab = (int *) xmlRealloc(ctxt->spaceTab,
1183 ctxt->spaceMax * sizeof(ctxt->spaceTab[0]));
1184 if (ctxt->spaceTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001185 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001186 return(0);
1187 }
1188 }
1189 ctxt->spaceTab[ctxt->spaceNr] = val;
1190 ctxt->space = &ctxt->spaceTab[ctxt->spaceNr];
1191 return(ctxt->spaceNr++);
1192}
1193
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001194static int spacePop(xmlParserCtxtPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00001195 int ret;
1196 if (ctxt->spaceNr <= 0) return(0);
1197 ctxt->spaceNr--;
1198 if (ctxt->spaceNr > 0)
1199 ctxt->space = &ctxt->spaceTab[ctxt->spaceNr - 1];
1200 else
1201 ctxt->space = NULL;
1202 ret = ctxt->spaceTab[ctxt->spaceNr];
1203 ctxt->spaceTab[ctxt->spaceNr] = -1;
1204 return(ret);
1205}
1206
1207/*
1208 * Macros for accessing the content. Those should be used only by the parser,
1209 * and not exported.
1210 *
1211 * Dirty macros, i.e. one often need to make assumption on the context to
1212 * use them
1213 *
1214 * CUR_PTR return the current pointer to the xmlChar to be parsed.
1215 * To be used with extreme caution since operations consuming
1216 * characters may move the input buffer to a different location !
1217 * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled
1218 * This should be used internally by the parser
1219 * only to compare to ASCII values otherwise it would break when
1220 * running with UTF-8 encoding.
1221 * RAW same as CUR but in the input buffer, bypass any token
1222 * extraction that may have been done
1223 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
1224 * to compare on ASCII based substring.
1225 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
Daniel Veillard77a90a72003-03-22 00:04:05 +00001226 * strings without newlines within the parser.
1227 * NEXT1(l) Skip 1 xmlChar, and must also be used only to skip 1 non-newline ASCII
1228 * defined char within the parser.
Owen Taylor3473f882001-02-23 17:55:21 +00001229 * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
1230 *
1231 * NEXT Skip to the next character, this does the proper decoding
1232 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
Daniel Veillard77a90a72003-03-22 00:04:05 +00001233 * NEXTL(l) Skip the current unicode character of l xmlChars long.
Owen Taylor3473f882001-02-23 17:55:21 +00001234 * CUR_CHAR(l) returns the current unicode character (int), set l
1235 * to the number of xmlChars used for the encoding [0-5].
1236 * CUR_SCHAR same but operate on a string instead of the context
1237 * COPY_BUF copy the current unicode char to the target buffer, increment
1238 * the index
1239 * GROW, SHRINK handling of input buffers
1240 */
1241
Daniel Veillardfdc91562002-07-01 21:52:03 +00001242#define RAW (*ctxt->input->cur)
1243#define CUR (*ctxt->input->cur)
Owen Taylor3473f882001-02-23 17:55:21 +00001244#define NXT(val) ctxt->input->cur[(val)]
1245#define CUR_PTR ctxt->input->cur
1246
Daniel Veillarda07050d2003-10-19 14:46:32 +00001247#define CMP4( s, c1, c2, c3, c4 ) \
1248 ( ((unsigned char *) s)[ 0 ] == c1 && ((unsigned char *) s)[ 1 ] == c2 && \
1249 ((unsigned char *) s)[ 2 ] == c3 && ((unsigned char *) s)[ 3 ] == c4 )
1250#define CMP5( s, c1, c2, c3, c4, c5 ) \
1251 ( CMP4( s, c1, c2, c3, c4 ) && ((unsigned char *) s)[ 4 ] == c5 )
1252#define CMP6( s, c1, c2, c3, c4, c5, c6 ) \
1253 ( CMP5( s, c1, c2, c3, c4, c5 ) && ((unsigned char *) s)[ 5 ] == c6 )
1254#define CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) \
1255 ( CMP6( s, c1, c2, c3, c4, c5, c6 ) && ((unsigned char *) s)[ 6 ] == c7 )
1256#define CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) \
1257 ( CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) && ((unsigned char *) s)[ 7 ] == c8 )
1258#define CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) \
1259 ( CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) && \
1260 ((unsigned char *) s)[ 8 ] == c9 )
1261#define CMP10( s, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 ) \
1262 ( CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) && \
1263 ((unsigned char *) s)[ 9 ] == c10 )
1264
Owen Taylor3473f882001-02-23 17:55:21 +00001265#define SKIP(val) do { \
Daniel Veillard77a90a72003-03-22 00:04:05 +00001266 ctxt->nbChars += (val),ctxt->input->cur += (val),ctxt->input->col+=(val); \
Owen Taylor3473f882001-02-23 17:55:21 +00001267 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
Daniel Veillard561b7f82002-03-20 21:55:57 +00001268 if ((*ctxt->input->cur == 0) && \
Owen Taylor3473f882001-02-23 17:55:21 +00001269 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \
1270 xmlPopInput(ctxt); \
1271 } while (0)
1272
Daniel Veillard0b787f32004-03-26 17:29:53 +00001273#define SKIPL(val) do { \
1274 int skipl; \
1275 for(skipl=0; skipl<val; skipl++) { \
1276 if (*(ctxt->input->cur) == '\n') { \
1277 ctxt->input->line++; ctxt->input->col = 1; \
1278 } else ctxt->input->col++; \
1279 ctxt->nbChars++; \
1280 ctxt->input->cur++; \
1281 } \
1282 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
1283 if ((*ctxt->input->cur == 0) && \
1284 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \
1285 xmlPopInput(ctxt); \
1286 } while (0)
1287
Daniel Veillarda880b122003-04-21 21:36:41 +00001288#define SHRINK if ((ctxt->progressive == 0) && \
Daniel Veillard6155d8a2003-08-19 15:01:28 +00001289 (ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \
1290 (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \
Daniel Veillard46de64e2002-05-29 08:21:33 +00001291 xmlSHRINK (ctxt);
1292
1293static void xmlSHRINK (xmlParserCtxtPtr ctxt) {
1294 xmlParserInputShrink(ctxt->input);
1295 if ((*ctxt->input->cur == 0) &&
1296 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
1297 xmlPopInput(ctxt);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001298 }
Owen Taylor3473f882001-02-23 17:55:21 +00001299
Daniel Veillarda880b122003-04-21 21:36:41 +00001300#define GROW if ((ctxt->progressive == 0) && \
1301 (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \
Daniel Veillard46de64e2002-05-29 08:21:33 +00001302 xmlGROW (ctxt);
1303
1304static void xmlGROW (xmlParserCtxtPtr ctxt) {
1305 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1306 if ((*ctxt->input->cur == 0) &&
1307 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
1308 xmlPopInput(ctxt);
Daniel Veillarda880b122003-04-21 21:36:41 +00001309}
Owen Taylor3473f882001-02-23 17:55:21 +00001310
1311#define SKIP_BLANKS xmlSkipBlankChars(ctxt)
1312
1313#define NEXT xmlNextChar(ctxt)
1314
Daniel Veillard21a0f912001-02-25 19:54:14 +00001315#define NEXT1 { \
Daniel Veillard77a90a72003-03-22 00:04:05 +00001316 ctxt->input->col++; \
Daniel Veillard21a0f912001-02-25 19:54:14 +00001317 ctxt->input->cur++; \
1318 ctxt->nbChars++; \
Daniel Veillard561b7f82002-03-20 21:55:57 +00001319 if (*ctxt->input->cur == 0) \
Daniel Veillard21a0f912001-02-25 19:54:14 +00001320 xmlParserInputGrow(ctxt->input, INPUT_CHUNK); \
1321 }
1322
Owen Taylor3473f882001-02-23 17:55:21 +00001323#define NEXTL(l) do { \
1324 if (*(ctxt->input->cur) == '\n') { \
1325 ctxt->input->line++; ctxt->input->col = 1; \
1326 } else ctxt->input->col++; \
Daniel Veillardfdc91562002-07-01 21:52:03 +00001327 ctxt->input->cur += l; \
Owen Taylor3473f882001-02-23 17:55:21 +00001328 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
Owen Taylor3473f882001-02-23 17:55:21 +00001329 } while (0)
1330
1331#define CUR_CHAR(l) xmlCurrentChar(ctxt, &l)
1332#define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l)
1333
1334#define COPY_BUF(l,b,i,v) \
1335 if (l == 1) b[i++] = (xmlChar) v; \
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001336 else i += xmlCopyCharMultiByte(&b[i],v)
Owen Taylor3473f882001-02-23 17:55:21 +00001337
1338/**
1339 * xmlSkipBlankChars:
1340 * @ctxt: the XML parser context
1341 *
1342 * skip all blanks character found at that point in the input streams.
1343 * It pops up finished entities in the process if allowable at that point.
1344 *
1345 * Returns the number of space chars skipped
1346 */
1347
1348int
1349xmlSkipBlankChars(xmlParserCtxtPtr ctxt) {
Daniel Veillard02141ea2001-04-30 11:46:40 +00001350 int res = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001351
1352 /*
1353 * It's Okay to use CUR/NEXT here since all the blanks are on
1354 * the ASCII range.
1355 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00001356 if ((ctxt->inputNr == 1) && (ctxt->instate != XML_PARSER_DTD)) {
1357 const xmlChar *cur;
Owen Taylor3473f882001-02-23 17:55:21 +00001358 /*
Daniel Veillard02141ea2001-04-30 11:46:40 +00001359 * if we are in the document content, go really fast
Owen Taylor3473f882001-02-23 17:55:21 +00001360 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00001361 cur = ctxt->input->cur;
William M. Brack76e95df2003-10-18 16:20:14 +00001362 while (IS_BLANK_CH(*cur)) {
Daniel Veillard02141ea2001-04-30 11:46:40 +00001363 if (*cur == '\n') {
1364 ctxt->input->line++; ctxt->input->col = 1;
1365 }
1366 cur++;
1367 res++;
1368 if (*cur == 0) {
1369 ctxt->input->cur = cur;
1370 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1371 cur = ctxt->input->cur;
1372 }
1373 }
1374 ctxt->input->cur = cur;
1375 } else {
1376 int cur;
1377 do {
1378 cur = CUR;
1379 while (IS_BLANK(cur)) { /* CHECKED tstblanks.xml */
1380 NEXT;
1381 cur = CUR;
1382 res++;
1383 }
1384 while ((cur == 0) && (ctxt->inputNr > 1) &&
1385 (ctxt->instate != XML_PARSER_COMMENT)) {
1386 xmlPopInput(ctxt);
1387 cur = CUR;
1388 }
1389 /*
1390 * Need to handle support of entities branching here
1391 */
1392 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt);
1393 } while (IS_BLANK(cur)); /* CHECKED tstblanks.xml */
1394 }
Owen Taylor3473f882001-02-23 17:55:21 +00001395 return(res);
1396}
1397
1398/************************************************************************
1399 * *
1400 * Commodity functions to handle entities *
1401 * *
1402 ************************************************************************/
1403
1404/**
1405 * xmlPopInput:
1406 * @ctxt: an XML parser context
1407 *
1408 * xmlPopInput: the current input pointed by ctxt->input came to an end
1409 * pop it and return the next char.
1410 *
1411 * Returns the current xmlChar in the parser context
1412 */
1413xmlChar
1414xmlPopInput(xmlParserCtxtPtr ctxt) {
1415 if (ctxt->inputNr == 1) return(0); /* End of main Input */
1416 if (xmlParserDebugEntities)
1417 xmlGenericError(xmlGenericErrorContext,
1418 "Popping input %d\n", ctxt->inputNr);
1419 xmlFreeInputStream(inputPop(ctxt));
Daniel Veillard561b7f82002-03-20 21:55:57 +00001420 if ((*ctxt->input->cur == 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +00001421 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
1422 return(xmlPopInput(ctxt));
1423 return(CUR);
1424}
1425
1426/**
1427 * xmlPushInput:
1428 * @ctxt: an XML parser context
1429 * @input: an XML parser input fragment (entity, XML fragment ...).
1430 *
1431 * xmlPushInput: switch to a new input stream which is stacked on top
1432 * of the previous one(s).
1433 */
1434void
1435xmlPushInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr input) {
1436 if (input == NULL) return;
1437
1438 if (xmlParserDebugEntities) {
1439 if ((ctxt->input != NULL) && (ctxt->input->filename))
1440 xmlGenericError(xmlGenericErrorContext,
1441 "%s(%d): ", ctxt->input->filename,
1442 ctxt->input->line);
1443 xmlGenericError(xmlGenericErrorContext,
1444 "Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur);
1445 }
1446 inputPush(ctxt, input);
1447 GROW;
1448}
1449
1450/**
1451 * xmlParseCharRef:
1452 * @ctxt: an XML parser context
1453 *
1454 * parse Reference declarations
1455 *
1456 * [66] CharRef ::= '&#' [0-9]+ ';' |
1457 * '&#x' [0-9a-fA-F]+ ';'
1458 *
1459 * [ WFC: Legal Character ]
1460 * Characters referred to using character references must match the
1461 * production for Char.
1462 *
1463 * Returns the value parsed (as an int), 0 in case of error
1464 */
1465int
1466xmlParseCharRef(xmlParserCtxtPtr ctxt) {
Daniel Veillard50582112001-03-26 22:52:16 +00001467 unsigned int val = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001468 int count = 0;
Daniel Veillard37fd3072004-06-03 11:22:31 +00001469 unsigned int outofrange = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001470
Owen Taylor3473f882001-02-23 17:55:21 +00001471 /*
1472 * Using RAW/CUR/NEXT is okay since we are working on ASCII range here
1473 */
Daniel Veillard561b7f82002-03-20 21:55:57 +00001474 if ((RAW == '&') && (NXT(1) == '#') &&
Owen Taylor3473f882001-02-23 17:55:21 +00001475 (NXT(2) == 'x')) {
1476 SKIP(3);
1477 GROW;
1478 while (RAW != ';') { /* loop blocked by count */
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00001479 if (count++ > 20) {
1480 count = 0;
1481 GROW;
1482 }
1483 if ((RAW >= '0') && (RAW <= '9'))
Owen Taylor3473f882001-02-23 17:55:21 +00001484 val = val * 16 + (CUR - '0');
1485 else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20))
1486 val = val * 16 + (CUR - 'a') + 10;
1487 else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20))
1488 val = val * 16 + (CUR - 'A') + 10;
1489 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001490 xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001491 val = 0;
1492 break;
1493 }
Daniel Veillard37fd3072004-06-03 11:22:31 +00001494 if (val > 0x10FFFF)
1495 outofrange = val;
1496
Owen Taylor3473f882001-02-23 17:55:21 +00001497 NEXT;
1498 count++;
1499 }
1500 if (RAW == ';') {
1501 /* on purpose to avoid reentrancy problems with NEXT and SKIP */
Daniel Veillard77a90a72003-03-22 00:04:05 +00001502 ctxt->input->col++;
Owen Taylor3473f882001-02-23 17:55:21 +00001503 ctxt->nbChars ++;
1504 ctxt->input->cur++;
1505 }
Daniel Veillard561b7f82002-03-20 21:55:57 +00001506 } else if ((RAW == '&') && (NXT(1) == '#')) {
Owen Taylor3473f882001-02-23 17:55:21 +00001507 SKIP(2);
1508 GROW;
1509 while (RAW != ';') { /* loop blocked by count */
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00001510 if (count++ > 20) {
1511 count = 0;
1512 GROW;
1513 }
1514 if ((RAW >= '0') && (RAW <= '9'))
Owen Taylor3473f882001-02-23 17:55:21 +00001515 val = val * 10 + (CUR - '0');
1516 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001517 xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001518 val = 0;
1519 break;
1520 }
Daniel Veillard37fd3072004-06-03 11:22:31 +00001521 if (val > 0x10FFFF)
1522 outofrange = val;
1523
Owen Taylor3473f882001-02-23 17:55:21 +00001524 NEXT;
1525 count++;
1526 }
1527 if (RAW == ';') {
1528 /* on purpose to avoid reentrancy problems with NEXT and SKIP */
Daniel Veillard77a90a72003-03-22 00:04:05 +00001529 ctxt->input->col++;
Owen Taylor3473f882001-02-23 17:55:21 +00001530 ctxt->nbChars ++;
1531 ctxt->input->cur++;
1532 }
1533 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001534 xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001535 }
1536
1537 /*
1538 * [ WFC: Legal Character ]
1539 * Characters referred to using character references must match the
1540 * production for Char.
1541 */
Daniel Veillard37fd3072004-06-03 11:22:31 +00001542 if ((IS_CHAR(val) && (outofrange == 0))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001543 return(val);
1544 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001545 xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
1546 "xmlParseCharRef: invalid xmlChar value %d\n",
1547 val);
Owen Taylor3473f882001-02-23 17:55:21 +00001548 }
1549 return(0);
1550}
1551
1552/**
1553 * xmlParseStringCharRef:
1554 * @ctxt: an XML parser context
1555 * @str: a pointer to an index in the string
1556 *
1557 * parse Reference declarations, variant parsing from a string rather
1558 * than an an input flow.
1559 *
1560 * [66] CharRef ::= '&#' [0-9]+ ';' |
1561 * '&#x' [0-9a-fA-F]+ ';'
1562 *
1563 * [ WFC: Legal Character ]
1564 * Characters referred to using character references must match the
1565 * production for Char.
1566 *
1567 * Returns the value parsed (as an int), 0 in case of error, str will be
1568 * updated to the current value of the index
1569 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001570static int
Owen Taylor3473f882001-02-23 17:55:21 +00001571xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) {
1572 const xmlChar *ptr;
1573 xmlChar cur;
Daniel Veillard37fd3072004-06-03 11:22:31 +00001574 unsigned int val = 0;
1575 unsigned int outofrange = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001576
1577 if ((str == NULL) || (*str == NULL)) return(0);
1578 ptr = *str;
1579 cur = *ptr;
1580 if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) {
1581 ptr += 3;
1582 cur = *ptr;
1583 while (cur != ';') { /* Non input consuming loop */
1584 if ((cur >= '0') && (cur <= '9'))
1585 val = val * 16 + (cur - '0');
1586 else if ((cur >= 'a') && (cur <= 'f'))
1587 val = val * 16 + (cur - 'a') + 10;
1588 else if ((cur >= 'A') && (cur <= 'F'))
1589 val = val * 16 + (cur - 'A') + 10;
1590 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001591 xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001592 val = 0;
1593 break;
1594 }
Daniel Veillard37fd3072004-06-03 11:22:31 +00001595 if (val > 0x10FFFF)
1596 outofrange = val;
1597
Owen Taylor3473f882001-02-23 17:55:21 +00001598 ptr++;
1599 cur = *ptr;
1600 }
1601 if (cur == ';')
1602 ptr++;
1603 } else if ((cur == '&') && (ptr[1] == '#')){
1604 ptr += 2;
1605 cur = *ptr;
1606 while (cur != ';') { /* Non input consuming loops */
1607 if ((cur >= '0') && (cur <= '9'))
1608 val = val * 10 + (cur - '0');
1609 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001610 xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001611 val = 0;
1612 break;
1613 }
Daniel Veillard37fd3072004-06-03 11:22:31 +00001614 if (val > 0x10FFFF)
1615 outofrange = val;
1616
Owen Taylor3473f882001-02-23 17:55:21 +00001617 ptr++;
1618 cur = *ptr;
1619 }
1620 if (cur == ';')
1621 ptr++;
1622 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001623 xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001624 return(0);
1625 }
1626 *str = ptr;
1627
1628 /*
1629 * [ WFC: Legal Character ]
1630 * Characters referred to using character references must match the
1631 * production for Char.
1632 */
Daniel Veillard37fd3072004-06-03 11:22:31 +00001633 if ((IS_CHAR(val) && (outofrange == 0))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001634 return(val);
1635 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001636 xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
1637 "xmlParseStringCharRef: invalid xmlChar value %d\n",
1638 val);
Owen Taylor3473f882001-02-23 17:55:21 +00001639 }
1640 return(0);
1641}
1642
1643/**
Daniel Veillardf5582f12002-06-11 10:08:16 +00001644 * xmlNewBlanksWrapperInputStream:
1645 * @ctxt: an XML parser context
1646 * @entity: an Entity pointer
1647 *
1648 * Create a new input stream for wrapping
1649 * blanks around a PEReference
1650 *
1651 * Returns the new input stream or NULL
1652 */
1653
1654static void deallocblankswrapper (xmlChar *str) {xmlFree(str);}
1655
Daniel Veillardf4862f02002-09-10 11:13:43 +00001656static xmlParserInputPtr
Daniel Veillardf5582f12002-06-11 10:08:16 +00001657xmlNewBlanksWrapperInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
1658 xmlParserInputPtr input;
1659 xmlChar *buffer;
1660 size_t length;
1661 if (entity == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001662 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
1663 "xmlNewBlanksWrapperInputStream entity\n");
Daniel Veillardf5582f12002-06-11 10:08:16 +00001664 return(NULL);
1665 }
1666 if (xmlParserDebugEntities)
1667 xmlGenericError(xmlGenericErrorContext,
1668 "new blanks wrapper for entity: %s\n", entity->name);
1669 input = xmlNewInputStream(ctxt);
1670 if (input == NULL) {
1671 return(NULL);
1672 }
1673 length = xmlStrlen(entity->name) + 5;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001674 buffer = xmlMallocAtomic(length);
Daniel Veillardf5582f12002-06-11 10:08:16 +00001675 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001676 xmlErrMemory(ctxt, NULL);
Daniel Veillardf5582f12002-06-11 10:08:16 +00001677 return(NULL);
1678 }
1679 buffer [0] = ' ';
1680 buffer [1] = '%';
1681 buffer [length-3] = ';';
1682 buffer [length-2] = ' ';
1683 buffer [length-1] = 0;
1684 memcpy(buffer + 2, entity->name, length - 5);
1685 input->free = deallocblankswrapper;
1686 input->base = buffer;
1687 input->cur = buffer;
1688 input->length = length;
1689 input->end = &buffer[length];
1690 return(input);
1691}
1692
1693/**
Owen Taylor3473f882001-02-23 17:55:21 +00001694 * xmlParserHandlePEReference:
1695 * @ctxt: the parser context
1696 *
1697 * [69] PEReference ::= '%' Name ';'
1698 *
1699 * [ WFC: No Recursion ]
1700 * A parsed entity must not contain a recursive
1701 * reference to itself, either directly or indirectly.
1702 *
1703 * [ WFC: Entity Declared ]
1704 * In a document without any DTD, a document with only an internal DTD
1705 * subset which contains no parameter entity references, or a document
1706 * with "standalone='yes'", ... ... The declaration of a parameter
1707 * entity must precede any reference to it...
1708 *
1709 * [ VC: Entity Declared ]
1710 * In a document with an external subset or external parameter entities
1711 * with "standalone='no'", ... ... The declaration of a parameter entity
1712 * must precede any reference to it...
1713 *
1714 * [ WFC: In DTD ]
1715 * Parameter-entity references may only appear in the DTD.
1716 * NOTE: misleading but this is handled.
1717 *
1718 * A PEReference may have been detected in the current input stream
1719 * the handling is done accordingly to
1720 * http://www.w3.org/TR/REC-xml#entproc
1721 * i.e.
1722 * - Included in literal in entity values
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001723 * - Included as Parameter Entity reference within DTDs
Owen Taylor3473f882001-02-23 17:55:21 +00001724 */
1725void
1726xmlParserHandlePEReference(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001727 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00001728 xmlEntityPtr entity = NULL;
1729 xmlParserInputPtr input;
1730
Owen Taylor3473f882001-02-23 17:55:21 +00001731 if (RAW != '%') return;
1732 switch(ctxt->instate) {
1733 case XML_PARSER_CDATA_SECTION:
1734 return;
1735 case XML_PARSER_COMMENT:
1736 return;
1737 case XML_PARSER_START_TAG:
1738 return;
1739 case XML_PARSER_END_TAG:
1740 return;
1741 case XML_PARSER_EOF:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001742 xmlFatalErr(ctxt, XML_ERR_PEREF_AT_EOF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001743 return;
1744 case XML_PARSER_PROLOG:
1745 case XML_PARSER_START:
1746 case XML_PARSER_MISC:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001747 xmlFatalErr(ctxt, XML_ERR_PEREF_IN_PROLOG, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001748 return;
1749 case XML_PARSER_ENTITY_DECL:
1750 case XML_PARSER_CONTENT:
1751 case XML_PARSER_ATTRIBUTE_VALUE:
1752 case XML_PARSER_PI:
1753 case XML_PARSER_SYSTEM_LITERAL:
Daniel Veillard4a7ae502002-02-18 19:18:17 +00001754 case XML_PARSER_PUBLIC_LITERAL:
Owen Taylor3473f882001-02-23 17:55:21 +00001755 /* we just ignore it there */
1756 return;
1757 case XML_PARSER_EPILOG:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001758 xmlFatalErr(ctxt, XML_ERR_PEREF_IN_EPILOG, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001759 return;
1760 case XML_PARSER_ENTITY_VALUE:
1761 /*
1762 * NOTE: in the case of entity values, we don't do the
1763 * substitution here since we need the literal
1764 * entity value to be able to save the internal
1765 * subset of the document.
1766 * This will be handled by xmlStringDecodeEntities
1767 */
1768 return;
1769 case XML_PARSER_DTD:
1770 /*
1771 * [WFC: Well-Formedness Constraint: PEs in Internal Subset]
1772 * In the internal DTD subset, parameter-entity references
1773 * can occur only where markup declarations can occur, not
1774 * within markup declarations.
1775 * In that case this is handled in xmlParseMarkupDecl
1776 */
1777 if ((ctxt->external == 0) && (ctxt->inputNr == 1))
1778 return;
William M. Brack76e95df2003-10-18 16:20:14 +00001779 if (IS_BLANK_CH(NXT(1)) || NXT(1) == 0)
Daniel Veillardf5582f12002-06-11 10:08:16 +00001780 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001781 break;
1782 case XML_PARSER_IGNORE:
1783 return;
1784 }
1785
1786 NEXT;
1787 name = xmlParseName(ctxt);
1788 if (xmlParserDebugEntities)
1789 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001790 "PEReference: %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00001791 if (name == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001792 xmlFatalErr(ctxt, XML_ERR_PEREF_NO_NAME, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001793 } else {
1794 if (RAW == ';') {
1795 NEXT;
1796 if ((ctxt->sax != NULL) && (ctxt->sax->getParameterEntity != NULL))
1797 entity = ctxt->sax->getParameterEntity(ctxt->userData, name);
1798 if (entity == NULL) {
1799
1800 /*
1801 * [ WFC: Entity Declared ]
1802 * In a document without any DTD, a document with only an
1803 * internal DTD subset which contains no parameter entity
1804 * references, or a document with "standalone='yes'", ...
1805 * ... The declaration of a parameter entity must precede
1806 * any reference to it...
1807 */
1808 if ((ctxt->standalone == 1) ||
1809 ((ctxt->hasExternalSubset == 0) &&
1810 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00001811 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00001812 "PEReference: %%%s; not found\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00001813 } else {
1814 /*
1815 * [ VC: Entity Declared ]
1816 * In a document with an external subset or external
1817 * parameter entities with "standalone='no'", ...
1818 * ... The declaration of a parameter entity must precede
1819 * any reference to it...
1820 */
Daniel Veillard24eb9782003-10-04 21:08:09 +00001821 if ((ctxt->validate) && (ctxt->vctxt.error != NULL)) {
1822 xmlValidityError(ctxt, XML_WAR_UNDECLARED_ENTITY,
1823 "PEReference: %%%s; not found\n",
1824 name);
1825 } else
1826 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
1827 "PEReference: %%%s; not found\n",
1828 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001829 ctxt->valid = 0;
1830 }
Daniel Veillardf5582f12002-06-11 10:08:16 +00001831 } else if (ctxt->input->free != deallocblankswrapper) {
1832 input = xmlNewBlanksWrapperInputStream(ctxt, entity);
1833 xmlPushInput(ctxt, input);
Owen Taylor3473f882001-02-23 17:55:21 +00001834 } else {
1835 if ((entity->etype == XML_INTERNAL_PARAMETER_ENTITY) ||
1836 (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY)) {
Daniel Veillard87a764e2001-06-20 17:41:10 +00001837 xmlChar start[4];
1838 xmlCharEncoding enc;
1839
Owen Taylor3473f882001-02-23 17:55:21 +00001840 /*
1841 * handle the extra spaces added before and after
1842 * c.f. http://www.w3.org/TR/REC-xml#as-PE
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001843 * this is done independently.
Owen Taylor3473f882001-02-23 17:55:21 +00001844 */
1845 input = xmlNewEntityInputStream(ctxt, entity);
1846 xmlPushInput(ctxt, input);
Daniel Veillard87a764e2001-06-20 17:41:10 +00001847
1848 /*
1849 * Get the 4 first bytes and decode the charset
1850 * if enc != XML_CHAR_ENCODING_NONE
1851 * plug some encoding conversion routines.
William M. Bracka0c48ad2004-04-16 15:58:29 +00001852 * Note that, since we may have some non-UTF8
1853 * encoding (like UTF16, bug 135229), the 'length'
1854 * is not known, but we can calculate based upon
1855 * the amount of data in the buffer.
Daniel Veillard87a764e2001-06-20 17:41:10 +00001856 */
1857 GROW
William M. Bracka0c48ad2004-04-16 15:58:29 +00001858 if ((ctxt->input->end - ctxt->input->cur)>=4) {
Daniel Veillarde059b892002-06-13 15:32:10 +00001859 start[0] = RAW;
1860 start[1] = NXT(1);
1861 start[2] = NXT(2);
1862 start[3] = NXT(3);
1863 enc = xmlDetectCharEncoding(start, 4);
1864 if (enc != XML_CHAR_ENCODING_NONE) {
1865 xmlSwitchEncoding(ctxt, enc);
1866 }
Daniel Veillard87a764e2001-06-20 17:41:10 +00001867 }
1868
Owen Taylor3473f882001-02-23 17:55:21 +00001869 if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) &&
Daniel Veillarda07050d2003-10-19 14:46:32 +00001870 (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l' )) &&
1871 (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001872 xmlParseTextDecl(ctxt);
1873 }
Owen Taylor3473f882001-02-23 17:55:21 +00001874 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00001875 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER,
1876 "PEReference: %s is not a parameter entity\n",
1877 name);
Owen Taylor3473f882001-02-23 17:55:21 +00001878 }
1879 }
1880 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001881 xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001882 }
Owen Taylor3473f882001-02-23 17:55:21 +00001883 }
1884}
1885
1886/*
1887 * Macro used to grow the current buffer.
1888 */
1889#define growBuffer(buffer) { \
Daniel Veillardd3999c72004-03-10 16:27:03 +00001890 xmlChar *tmp; \
Owen Taylor3473f882001-02-23 17:55:21 +00001891 buffer##_size *= 2; \
Daniel Veillardd3999c72004-03-10 16:27:03 +00001892 tmp = (xmlChar *) \
Owen Taylor3473f882001-02-23 17:55:21 +00001893 xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \
Daniel Veillardd3999c72004-03-10 16:27:03 +00001894 if (tmp == NULL) goto mem_error; \
1895 buffer = tmp; \
Owen Taylor3473f882001-02-23 17:55:21 +00001896}
1897
1898/**
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00001899 * xmlStringLenDecodeEntities:
Owen Taylor3473f882001-02-23 17:55:21 +00001900 * @ctxt: the parser context
1901 * @str: the input string
Daniel Veillarde57ec792003-09-10 10:50:59 +00001902 * @len: the string length
Owen Taylor3473f882001-02-23 17:55:21 +00001903 * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
1904 * @end: an end marker xmlChar, 0 if none
1905 * @end2: an end marker xmlChar, 0 if none
1906 * @end3: an end marker xmlChar, 0 if none
1907 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001908 * Takes a entity string content and process to do the adequate substitutions.
Owen Taylor3473f882001-02-23 17:55:21 +00001909 *
1910 * [67] Reference ::= EntityRef | CharRef
1911 *
1912 * [69] PEReference ::= '%' Name ';'
1913 *
1914 * Returns A newly allocated string with the substitution done. The caller
1915 * must deallocate it !
1916 */
1917xmlChar *
Daniel Veillarde57ec792003-09-10 10:50:59 +00001918xmlStringLenDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int len,
1919 int what, xmlChar end, xmlChar end2, xmlChar end3) {
Owen Taylor3473f882001-02-23 17:55:21 +00001920 xmlChar *buffer = NULL;
1921 int buffer_size = 0;
1922
1923 xmlChar *current = NULL;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001924 const xmlChar *last;
Owen Taylor3473f882001-02-23 17:55:21 +00001925 xmlEntityPtr ent;
1926 int c,l;
1927 int nbchars = 0;
1928
Daniel Veillarde57ec792003-09-10 10:50:59 +00001929 if ((str == NULL) || (len < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00001930 return(NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001931 last = str + len;
Owen Taylor3473f882001-02-23 17:55:21 +00001932
1933 if (ctxt->depth > 40) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001934 xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001935 return(NULL);
1936 }
1937
1938 /*
1939 * allocate a translation buffer.
1940 */
1941 buffer_size = XML_PARSER_BIG_BUFFER_SIZE;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001942 buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar));
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00001943 if (buffer == NULL) goto mem_error;
Owen Taylor3473f882001-02-23 17:55:21 +00001944
1945 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001946 * OK loop until we reach one of the ending char or a size limit.
Owen Taylor3473f882001-02-23 17:55:21 +00001947 * we are operating on already parsed values.
1948 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001949 if (str < last)
1950 c = CUR_SCHAR(str, l);
1951 else
1952 c = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001953 while ((c != 0) && (c != end) && /* non input consuming loop */
1954 (c != end2) && (c != end3)) {
1955
1956 if (c == 0) break;
1957 if ((c == '&') && (str[1] == '#')) {
1958 int val = xmlParseStringCharRef(ctxt, &str);
1959 if (val != 0) {
1960 COPY_BUF(0,buffer,nbchars,val);
1961 }
1962 } else if ((c == '&') && (what & XML_SUBSTITUTE_REF)) {
1963 if (xmlParserDebugEntities)
1964 xmlGenericError(xmlGenericErrorContext,
1965 "String decoding Entity Reference: %.30s\n",
1966 str);
1967 ent = xmlParseStringEntityRef(ctxt, &str);
1968 if ((ent != NULL) &&
1969 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1970 if (ent->content != NULL) {
1971 COPY_BUF(0,buffer,nbchars,ent->content[0]);
1972 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00001973 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
1974 "predefined entity has no content\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001975 }
1976 } else if ((ent != NULL) && (ent->content != NULL)) {
1977 xmlChar *rep;
1978
1979 ctxt->depth++;
1980 rep = xmlStringDecodeEntities(ctxt, ent->content, what,
1981 0, 0, 0);
1982 ctxt->depth--;
1983 if (rep != NULL) {
1984 current = rep;
1985 while (*current != 0) { /* non input consuming loop */
1986 buffer[nbchars++] = *current++;
1987 if (nbchars >
1988 buffer_size - XML_PARSER_BUFFER_SIZE) {
1989 growBuffer(buffer);
1990 }
1991 }
1992 xmlFree(rep);
1993 }
1994 } else if (ent != NULL) {
1995 int i = xmlStrlen(ent->name);
1996 const xmlChar *cur = ent->name;
1997
1998 buffer[nbchars++] = '&';
1999 if (nbchars > buffer_size - i - XML_PARSER_BUFFER_SIZE) {
2000 growBuffer(buffer);
2001 }
2002 for (;i > 0;i--)
2003 buffer[nbchars++] = *cur++;
2004 buffer[nbchars++] = ';';
2005 }
2006 } else if (c == '%' && (what & XML_SUBSTITUTE_PEREF)) {
2007 if (xmlParserDebugEntities)
2008 xmlGenericError(xmlGenericErrorContext,
2009 "String decoding PE Reference: %.30s\n", str);
2010 ent = xmlParseStringPEReference(ctxt, &str);
2011 if (ent != NULL) {
2012 xmlChar *rep;
2013
2014 ctxt->depth++;
2015 rep = xmlStringDecodeEntities(ctxt, ent->content, what,
2016 0, 0, 0);
2017 ctxt->depth--;
2018 if (rep != NULL) {
2019 current = rep;
2020 while (*current != 0) { /* non input consuming loop */
2021 buffer[nbchars++] = *current++;
2022 if (nbchars >
2023 buffer_size - XML_PARSER_BUFFER_SIZE) {
2024 growBuffer(buffer);
2025 }
2026 }
2027 xmlFree(rep);
2028 }
2029 }
2030 } else {
2031 COPY_BUF(l,buffer,nbchars,c);
2032 str += l;
2033 if (nbchars > buffer_size - XML_PARSER_BUFFER_SIZE) {
2034 growBuffer(buffer);
2035 }
2036 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002037 if (str < last)
2038 c = CUR_SCHAR(str, l);
2039 else
2040 c = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002041 }
2042 buffer[nbchars++] = 0;
2043 return(buffer);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002044
2045mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002046 xmlErrMemory(ctxt, NULL);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002047 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002048}
2049
Daniel Veillarde57ec792003-09-10 10:50:59 +00002050/**
2051 * xmlStringDecodeEntities:
2052 * @ctxt: the parser context
2053 * @str: the input string
2054 * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
2055 * @end: an end marker xmlChar, 0 if none
2056 * @end2: an end marker xmlChar, 0 if none
2057 * @end3: an end marker xmlChar, 0 if none
2058 *
2059 * Takes a entity string content and process to do the adequate substitutions.
2060 *
2061 * [67] Reference ::= EntityRef | CharRef
2062 *
2063 * [69] PEReference ::= '%' Name ';'
2064 *
2065 * Returns A newly allocated string with the substitution done. The caller
2066 * must deallocate it !
2067 */
2068xmlChar *
2069xmlStringDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int what,
2070 xmlChar end, xmlChar end2, xmlChar end3) {
2071 return(xmlStringLenDecodeEntities(ctxt, str, xmlStrlen(str), what,
2072 end, end2, end3));
2073}
Owen Taylor3473f882001-02-23 17:55:21 +00002074
2075/************************************************************************
2076 * *
Owen Taylor3473f882001-02-23 17:55:21 +00002077 * Commodity functions, cleanup needed ? *
2078 * *
2079 ************************************************************************/
2080
2081/**
2082 * areBlanks:
2083 * @ctxt: an XML parser context
2084 * @str: a xmlChar *
2085 * @len: the size of @str
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002086 * @blank_chars: we know the chars are blanks
Owen Taylor3473f882001-02-23 17:55:21 +00002087 *
2088 * Is this a sequence of blank chars that one can ignore ?
2089 *
2090 * Returns 1 if ignorable 0 otherwise.
2091 */
2092
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002093static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len,
2094 int blank_chars) {
Owen Taylor3473f882001-02-23 17:55:21 +00002095 int i, ret;
2096 xmlNodePtr lastChild;
2097
Daniel Veillard05c13a22001-09-09 08:38:09 +00002098 /*
2099 * Don't spend time trying to differentiate them, the same callback is
2100 * used !
2101 */
2102 if (ctxt->sax->ignorableWhitespace == ctxt->sax->characters)
Daniel Veillard2f362242001-03-02 17:36:21 +00002103 return(0);
2104
Owen Taylor3473f882001-02-23 17:55:21 +00002105 /*
2106 * Check for xml:space value.
2107 */
2108 if (*(ctxt->space) == 1)
2109 return(0);
2110
2111 /*
2112 * Check that the string is made of blanks
2113 */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002114 if (blank_chars == 0) {
2115 for (i = 0;i < len;i++)
2116 if (!(IS_BLANK_CH(str[i]))) return(0);
2117 }
Owen Taylor3473f882001-02-23 17:55:21 +00002118
2119 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002120 * Look if the element is mixed content in the DTD if available
Owen Taylor3473f882001-02-23 17:55:21 +00002121 */
Daniel Veillard6dd398f2001-07-25 22:41:03 +00002122 if (ctxt->node == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002123 if (ctxt->myDoc != NULL) {
2124 ret = xmlIsMixedElement(ctxt->myDoc, ctxt->node->name);
2125 if (ret == 0) return(1);
2126 if (ret == 1) return(0);
2127 }
2128
2129 /*
2130 * Otherwise, heuristic :-\
2131 */
Daniel Veillard561b7f82002-03-20 21:55:57 +00002132 if (RAW != '<') return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002133 if ((ctxt->node->children == NULL) &&
2134 (RAW == '<') && (NXT(1) == '/')) return(0);
2135
2136 lastChild = xmlGetLastChild(ctxt->node);
2137 if (lastChild == NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002138 if ((ctxt->node->type != XML_ELEMENT_NODE) &&
2139 (ctxt->node->content != NULL)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002140 } else if (xmlNodeIsText(lastChild))
2141 return(0);
2142 else if ((ctxt->node->children != NULL) &&
2143 (xmlNodeIsText(ctxt->node->children)))
2144 return(0);
2145 return(1);
2146}
2147
Owen Taylor3473f882001-02-23 17:55:21 +00002148/************************************************************************
2149 * *
2150 * Extra stuff for namespace support *
2151 * Relates to http://www.w3.org/TR/WD-xml-names *
2152 * *
2153 ************************************************************************/
2154
2155/**
2156 * xmlSplitQName:
2157 * @ctxt: an XML parser context
2158 * @name: an XML parser context
2159 * @prefix: a xmlChar **
2160 *
2161 * parse an UTF8 encoded XML qualified name string
2162 *
2163 * [NS 5] QName ::= (Prefix ':')? LocalPart
2164 *
2165 * [NS 6] Prefix ::= NCName
2166 *
2167 * [NS 7] LocalPart ::= NCName
2168 *
2169 * Returns the local part, and prefix is updated
2170 * to get the Prefix if any.
2171 */
2172
2173xmlChar *
2174xmlSplitQName(xmlParserCtxtPtr ctxt, const xmlChar *name, xmlChar **prefix) {
2175 xmlChar buf[XML_MAX_NAMELEN + 5];
2176 xmlChar *buffer = NULL;
2177 int len = 0;
2178 int max = XML_MAX_NAMELEN;
2179 xmlChar *ret = NULL;
2180 const xmlChar *cur = name;
2181 int c;
2182
2183 *prefix = NULL;
2184
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002185 if (cur == NULL) return(NULL);
2186
Owen Taylor3473f882001-02-23 17:55:21 +00002187#ifndef XML_XML_NAMESPACE
2188 /* xml: prefix is not really a namespace */
2189 if ((cur[0] == 'x') && (cur[1] == 'm') &&
2190 (cur[2] == 'l') && (cur[3] == ':'))
2191 return(xmlStrdup(name));
2192#endif
2193
Daniel Veillard597bc482003-07-24 16:08:28 +00002194 /* nasty but well=formed */
Owen Taylor3473f882001-02-23 17:55:21 +00002195 if (cur[0] == ':')
2196 return(xmlStrdup(name));
2197
2198 c = *cur++;
2199 while ((c != 0) && (c != ':') && (len < max)) { /* tested bigname.xml */
2200 buf[len++] = c;
2201 c = *cur++;
2202 }
2203 if (len >= max) {
2204 /*
2205 * Okay someone managed to make a huge name, so he's ready to pay
2206 * for the processing speed.
2207 */
2208 max = len * 2;
2209
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002210 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002211 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002212 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002213 return(NULL);
2214 }
2215 memcpy(buffer, buf, len);
2216 while ((c != 0) && (c != ':')) { /* tested bigname.xml */
2217 if (len + 10 > max) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00002218 xmlChar *tmp;
2219
Owen Taylor3473f882001-02-23 17:55:21 +00002220 max *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00002221 tmp = (xmlChar *) xmlRealloc(buffer,
Owen Taylor3473f882001-02-23 17:55:21 +00002222 max * sizeof(xmlChar));
Daniel Veillard2248ff12004-09-22 23:05:14 +00002223 if (tmp == NULL) {
2224 xmlFree(tmp);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002225 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002226 return(NULL);
2227 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00002228 buffer = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002229 }
2230 buffer[len++] = c;
2231 c = *cur++;
2232 }
2233 buffer[len] = 0;
2234 }
2235
Daniel Veillard597bc482003-07-24 16:08:28 +00002236 /* nasty but well=formed
2237 if ((c == ':') && (*cur == 0)) {
2238 return(xmlStrdup(name));
2239 } */
2240
Owen Taylor3473f882001-02-23 17:55:21 +00002241 if (buffer == NULL)
2242 ret = xmlStrndup(buf, len);
2243 else {
2244 ret = buffer;
2245 buffer = NULL;
2246 max = XML_MAX_NAMELEN;
2247 }
2248
2249
2250 if (c == ':') {
Daniel Veillardbb284f42002-10-16 18:02:47 +00002251 c = *cur;
Owen Taylor3473f882001-02-23 17:55:21 +00002252 *prefix = ret;
Daniel Veillard597bc482003-07-24 16:08:28 +00002253 if (c == 0) {
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00002254 return(xmlStrndup(BAD_CAST "", 0));
Daniel Veillard597bc482003-07-24 16:08:28 +00002255 }
Owen Taylor3473f882001-02-23 17:55:21 +00002256 len = 0;
2257
Daniel Veillardbb284f42002-10-16 18:02:47 +00002258 /*
2259 * Check that the first character is proper to start
2260 * a new name
2261 */
2262 if (!(((c >= 0x61) && (c <= 0x7A)) ||
2263 ((c >= 0x41) && (c <= 0x5A)) ||
2264 (c == '_') || (c == ':'))) {
2265 int l;
2266 int first = CUR_SCHAR(cur, l);
2267
2268 if (!IS_LETTER(first) && (first != '_')) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00002269 xmlFatalErrMsgStr(ctxt, XML_NS_ERR_QNAME,
Daniel Veillardbb284f42002-10-16 18:02:47 +00002270 "Name %s is not XML Namespace compliant\n",
Daniel Veillardbc92eca2003-09-15 09:48:06 +00002271 name);
Daniel Veillardbb284f42002-10-16 18:02:47 +00002272 }
2273 }
2274 cur++;
2275
Owen Taylor3473f882001-02-23 17:55:21 +00002276 while ((c != 0) && (len < max)) { /* tested bigname2.xml */
2277 buf[len++] = c;
2278 c = *cur++;
2279 }
2280 if (len >= max) {
2281 /*
2282 * Okay someone managed to make a huge name, so he's ready to pay
2283 * for the processing speed.
2284 */
2285 max = len * 2;
2286
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002287 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002288 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002289 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002290 return(NULL);
2291 }
2292 memcpy(buffer, buf, len);
2293 while (c != 0) { /* tested bigname2.xml */
2294 if (len + 10 > max) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00002295 xmlChar *tmp;
2296
Owen Taylor3473f882001-02-23 17:55:21 +00002297 max *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00002298 tmp = (xmlChar *) xmlRealloc(buffer,
Owen Taylor3473f882001-02-23 17:55:21 +00002299 max * sizeof(xmlChar));
Daniel Veillard2248ff12004-09-22 23:05:14 +00002300 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002301 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00002302 xmlFree(buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00002303 return(NULL);
2304 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00002305 buffer = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002306 }
2307 buffer[len++] = c;
2308 c = *cur++;
2309 }
2310 buffer[len] = 0;
2311 }
2312
2313 if (buffer == NULL)
2314 ret = xmlStrndup(buf, len);
2315 else {
2316 ret = buffer;
2317 }
2318 }
2319
2320 return(ret);
2321}
2322
2323/************************************************************************
2324 * *
2325 * The parser itself *
2326 * Relates to http://www.w3.org/TR/REC-xml *
2327 * *
2328 ************************************************************************/
2329
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002330static const xmlChar * xmlParseNameComplex(xmlParserCtxtPtr ctxt);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002331static xmlChar * xmlParseAttValueInternal(xmlParserCtxtPtr ctxt,
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002332 int *len, int *alloc, int normalize);
Daniel Veillard0fb18932003-09-07 09:14:37 +00002333
Owen Taylor3473f882001-02-23 17:55:21 +00002334/**
2335 * xmlParseName:
2336 * @ctxt: an XML parser context
2337 *
2338 * parse an XML name.
2339 *
2340 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
2341 * CombiningChar | Extender
2342 *
2343 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
2344 *
Daniel Veillard807b4de2004-09-26 14:42:56 +00002345 * [6] Names ::= Name (#x20 Name)*
Owen Taylor3473f882001-02-23 17:55:21 +00002346 *
2347 * Returns the Name parsed or NULL
2348 */
2349
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002350const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002351xmlParseName(xmlParserCtxtPtr ctxt) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00002352 const xmlChar *in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002353 const xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00002354 int count = 0;
2355
2356 GROW;
Daniel Veillard48b2f892001-02-25 16:11:03 +00002357
2358 /*
2359 * Accelerator for simple ASCII names
2360 */
2361 in = ctxt->input->cur;
2362 if (((*in >= 0x61) && (*in <= 0x7A)) ||
2363 ((*in >= 0x41) && (*in <= 0x5A)) ||
2364 (*in == '_') || (*in == ':')) {
2365 in++;
2366 while (((*in >= 0x61) && (*in <= 0x7A)) ||
2367 ((*in >= 0x41) && (*in <= 0x5A)) ||
2368 ((*in >= 0x30) && (*in <= 0x39)) ||
Daniel Veillard76d66f42001-05-16 21:05:17 +00002369 (*in == '_') || (*in == '-') ||
2370 (*in == ':') || (*in == '.'))
Daniel Veillard48b2f892001-02-25 16:11:03 +00002371 in++;
Daniel Veillard76d66f42001-05-16 21:05:17 +00002372 if ((*in > 0) && (*in < 0x80)) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00002373 count = in - ctxt->input->cur;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002374 ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002375 ctxt->input->cur = in;
Daniel Veillard77a90a72003-03-22 00:04:05 +00002376 ctxt->nbChars += count;
2377 ctxt->input->col += count;
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002378 if (ret == NULL)
2379 xmlErrMemory(ctxt, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002380 return(ret);
2381 }
2382 }
Daniel Veillard2f362242001-03-02 17:36:21 +00002383 return(xmlParseNameComplex(ctxt));
Daniel Veillard21a0f912001-02-25 19:54:14 +00002384}
Daniel Veillard48b2f892001-02-25 16:11:03 +00002385
Daniel Veillard46de64e2002-05-29 08:21:33 +00002386/**
2387 * xmlParseNameAndCompare:
2388 * @ctxt: an XML parser context
2389 *
2390 * parse an XML name and compares for match
2391 * (specialized for endtag parsing)
2392 *
Daniel Veillard46de64e2002-05-29 08:21:33 +00002393 * Returns NULL for an illegal name, (xmlChar*) 1 for success
2394 * and the name for mismatch
2395 */
2396
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002397static const xmlChar *
Daniel Veillard46de64e2002-05-29 08:21:33 +00002398xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002399 register const xmlChar *cmp = other;
2400 register const xmlChar *in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002401 const xmlChar *ret;
Daniel Veillard46de64e2002-05-29 08:21:33 +00002402
2403 GROW;
2404
2405 in = ctxt->input->cur;
2406 while (*in != 0 && *in == *cmp) {
2407 ++in;
2408 ++cmp;
2409 }
William M. Brack76e95df2003-10-18 16:20:14 +00002410 if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
Daniel Veillard46de64e2002-05-29 08:21:33 +00002411 /* success */
2412 ctxt->input->cur = in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002413 return (const xmlChar*) 1;
Daniel Veillard46de64e2002-05-29 08:21:33 +00002414 }
2415 /* failure (or end of input buffer), check with full function */
2416 ret = xmlParseName (ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00002417 /* strings coming from the dictionnary direct compare possible */
2418 if (ret == other) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002419 return (const xmlChar*) 1;
Daniel Veillard46de64e2002-05-29 08:21:33 +00002420 }
2421 return ret;
2422}
2423
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002424static const xmlChar *
Daniel Veillard21a0f912001-02-25 19:54:14 +00002425xmlParseNameComplex(xmlParserCtxtPtr ctxt) {
Daniel Veillard21a0f912001-02-25 19:54:14 +00002426 int len = 0, l;
2427 int c;
2428 int count = 0;
2429
2430 /*
2431 * Handler for more complex cases
2432 */
2433 GROW;
Owen Taylor3473f882001-02-23 17:55:21 +00002434 c = CUR_CHAR(l);
2435 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
2436 (!IS_LETTER(c) && (c != '_') &&
2437 (c != ':'))) {
2438 return(NULL);
2439 }
2440
2441 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
William M. Brack871611b2003-10-18 04:53:14 +00002442 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
Owen Taylor3473f882001-02-23 17:55:21 +00002443 (c == '.') || (c == '-') ||
2444 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002445 (IS_COMBINING(c)) ||
2446 (IS_EXTENDER(c)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002447 if (count++ > 100) {
2448 count = 0;
2449 GROW;
2450 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002451 len += l;
Owen Taylor3473f882001-02-23 17:55:21 +00002452 NEXTL(l);
2453 c = CUR_CHAR(l);
Owen Taylor3473f882001-02-23 17:55:21 +00002454 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002455 return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len));
Owen Taylor3473f882001-02-23 17:55:21 +00002456}
2457
2458/**
2459 * xmlParseStringName:
2460 * @ctxt: an XML parser context
2461 * @str: a pointer to the string pointer (IN/OUT)
2462 *
2463 * parse an XML name.
2464 *
2465 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
2466 * CombiningChar | Extender
2467 *
2468 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
2469 *
Daniel Veillard807b4de2004-09-26 14:42:56 +00002470 * [6] Names ::= Name (#x20 Name)*
Owen Taylor3473f882001-02-23 17:55:21 +00002471 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002472 * Returns the Name parsed or NULL. The @str pointer
Owen Taylor3473f882001-02-23 17:55:21 +00002473 * is updated to the current location in the string.
2474 */
2475
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002476static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002477xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) {
2478 xmlChar buf[XML_MAX_NAMELEN + 5];
2479 const xmlChar *cur = *str;
2480 int len = 0, l;
2481 int c;
2482
2483 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +00002484 if (!IS_LETTER(c) && (c != '_') &&
Owen Taylor3473f882001-02-23 17:55:21 +00002485 (c != ':')) {
2486 return(NULL);
2487 }
2488
William M. Brack871611b2003-10-18 04:53:14 +00002489 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigentname.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002490 (c == '.') || (c == '-') ||
2491 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002492 (IS_COMBINING(c)) ||
2493 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002494 COPY_BUF(l,buf,len,c);
2495 cur += l;
2496 c = CUR_SCHAR(cur, l);
2497 if (len >= XML_MAX_NAMELEN) { /* test bigentname.xml */
2498 /*
2499 * Okay someone managed to make a huge name, so he's ready to pay
2500 * for the processing speed.
2501 */
2502 xmlChar *buffer;
2503 int max = len * 2;
2504
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002505 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002506 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002507 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002508 return(NULL);
2509 }
2510 memcpy(buffer, buf, len);
William M. Brack871611b2003-10-18 04:53:14 +00002511 while ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
Daniel Veillard73b013f2003-09-30 12:36:01 +00002512 /* test bigentname.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002513 (c == '.') || (c == '-') ||
2514 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002515 (IS_COMBINING(c)) ||
2516 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002517 if (len + 10 > max) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00002518 xmlChar *tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002519 max *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00002520 tmp = (xmlChar *) xmlRealloc(buffer,
Owen Taylor3473f882001-02-23 17:55:21 +00002521 max * sizeof(xmlChar));
Daniel Veillard2248ff12004-09-22 23:05:14 +00002522 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002523 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00002524 xmlFree(buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00002525 return(NULL);
2526 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00002527 buffer = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002528 }
2529 COPY_BUF(l,buffer,len,c);
2530 cur += l;
2531 c = CUR_SCHAR(cur, l);
2532 }
2533 buffer[len] = 0;
2534 *str = cur;
2535 return(buffer);
2536 }
2537 }
2538 *str = cur;
2539 return(xmlStrndup(buf, len));
2540}
2541
2542/**
2543 * xmlParseNmtoken:
2544 * @ctxt: an XML parser context
2545 *
2546 * parse an XML Nmtoken.
2547 *
2548 * [7] Nmtoken ::= (NameChar)+
2549 *
Daniel Veillard807b4de2004-09-26 14:42:56 +00002550 * [8] Nmtokens ::= Nmtoken (#x20 Nmtoken)*
Owen Taylor3473f882001-02-23 17:55:21 +00002551 *
2552 * Returns the Nmtoken parsed or NULL
2553 */
2554
2555xmlChar *
2556xmlParseNmtoken(xmlParserCtxtPtr ctxt) {
2557 xmlChar buf[XML_MAX_NAMELEN + 5];
2558 int len = 0, l;
2559 int c;
2560 int count = 0;
2561
2562 GROW;
2563 c = CUR_CHAR(l);
2564
William M. Brack871611b2003-10-18 04:53:14 +00002565 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigtoken.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002566 (c == '.') || (c == '-') ||
2567 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002568 (IS_COMBINING(c)) ||
2569 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002570 if (count++ > 100) {
2571 count = 0;
2572 GROW;
2573 }
2574 COPY_BUF(l,buf,len,c);
2575 NEXTL(l);
2576 c = CUR_CHAR(l);
2577 if (len >= XML_MAX_NAMELEN) {
2578 /*
2579 * Okay someone managed to make a huge token, so he's ready to pay
2580 * for the processing speed.
2581 */
2582 xmlChar *buffer;
2583 int max = len * 2;
2584
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002585 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002586 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002587 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002588 return(NULL);
2589 }
2590 memcpy(buffer, buf, len);
William M. Brack871611b2003-10-18 04:53:14 +00002591 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigtoken.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002592 (c == '.') || (c == '-') ||
2593 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002594 (IS_COMBINING(c)) ||
2595 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002596 if (count++ > 100) {
2597 count = 0;
2598 GROW;
2599 }
2600 if (len + 10 > max) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00002601 xmlChar *tmp;
2602
Owen Taylor3473f882001-02-23 17:55:21 +00002603 max *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00002604 tmp = (xmlChar *) xmlRealloc(buffer,
Owen Taylor3473f882001-02-23 17:55:21 +00002605 max * sizeof(xmlChar));
Daniel Veillard2248ff12004-09-22 23:05:14 +00002606 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002607 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00002608 xmlFree(buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00002609 return(NULL);
2610 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00002611 buffer = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002612 }
2613 COPY_BUF(l,buffer,len,c);
2614 NEXTL(l);
2615 c = CUR_CHAR(l);
2616 }
2617 buffer[len] = 0;
2618 return(buffer);
2619 }
2620 }
2621 if (len == 0)
2622 return(NULL);
2623 return(xmlStrndup(buf, len));
2624}
2625
2626/**
2627 * xmlParseEntityValue:
2628 * @ctxt: an XML parser context
2629 * @orig: if non-NULL store a copy of the original entity value
2630 *
2631 * parse a value for ENTITY declarations
2632 *
2633 * [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' |
2634 * "'" ([^%&'] | PEReference | Reference)* "'"
2635 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002636 * Returns the EntityValue parsed with reference substituted or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002637 */
2638
2639xmlChar *
2640xmlParseEntityValue(xmlParserCtxtPtr ctxt, xmlChar **orig) {
2641 xmlChar *buf = NULL;
2642 int len = 0;
2643 int size = XML_PARSER_BUFFER_SIZE;
2644 int c, l;
2645 xmlChar stop;
2646 xmlChar *ret = NULL;
2647 const xmlChar *cur = NULL;
2648 xmlParserInputPtr input;
2649
2650 if (RAW == '"') stop = '"';
2651 else if (RAW == '\'') stop = '\'';
2652 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002653 xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002654 return(NULL);
2655 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002656 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002657 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002658 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002659 return(NULL);
2660 }
2661
2662 /*
2663 * The content of the entity definition is copied in a buffer.
2664 */
2665
2666 ctxt->instate = XML_PARSER_ENTITY_VALUE;
2667 input = ctxt->input;
2668 GROW;
2669 NEXT;
2670 c = CUR_CHAR(l);
2671 /*
2672 * NOTE: 4.4.5 Included in Literal
2673 * When a parameter entity reference appears in a literal entity
2674 * value, ... a single or double quote character in the replacement
2675 * text is always treated as a normal data character and will not
2676 * terminate the literal.
2677 * In practice it means we stop the loop only when back at parsing
2678 * the initial entity and the quote is found
2679 */
William M. Brack871611b2003-10-18 04:53:14 +00002680 while ((IS_CHAR(c)) && ((c != stop) || /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00002681 (ctxt->input != input))) {
2682 if (len + 5 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00002683 xmlChar *tmp;
2684
Owen Taylor3473f882001-02-23 17:55:21 +00002685 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00002686 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
2687 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002688 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00002689 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00002690 return(NULL);
2691 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00002692 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002693 }
2694 COPY_BUF(l,buf,len,c);
2695 NEXTL(l);
2696 /*
2697 * Pop-up of finished entities.
2698 */
2699 while ((RAW == 0) && (ctxt->inputNr > 1)) /* non input consuming */
2700 xmlPopInput(ctxt);
2701
2702 GROW;
2703 c = CUR_CHAR(l);
2704 if (c == 0) {
2705 GROW;
2706 c = CUR_CHAR(l);
2707 }
2708 }
2709 buf[len] = 0;
2710
2711 /*
2712 * Raise problem w.r.t. '&' and '%' being used in non-entities
2713 * reference constructs. Note Charref will be handled in
2714 * xmlStringDecodeEntities()
2715 */
2716 cur = buf;
2717 while (*cur != 0) { /* non input consuming */
2718 if ((*cur == '%') || ((*cur == '&') && (cur[1] != '#'))) {
2719 xmlChar *name;
2720 xmlChar tmp = *cur;
2721
2722 cur++;
2723 name = xmlParseStringName(ctxt, &cur);
2724 if ((name == NULL) || (*cur != ';')) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00002725 xmlFatalErrMsgInt(ctxt, XML_ERR_ENTITY_CHAR_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00002726 "EntityValue: '%c' forbidden except for entities references\n",
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00002727 tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002728 }
Daniel Veillard5151c062001-10-23 13:10:19 +00002729 if ((tmp == '%') && (ctxt->inSubset == 1) &&
2730 (ctxt->inputNr == 1)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002731 xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002732 }
2733 if (name != NULL)
2734 xmlFree(name);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002735 if (*cur == 0)
2736 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002737 }
2738 cur++;
2739 }
2740
2741 /*
2742 * Then PEReference entities are substituted.
2743 */
2744 if (c != stop) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002745 xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002746 xmlFree(buf);
2747 } else {
2748 NEXT;
2749 /*
2750 * NOTE: 4.4.7 Bypassed
2751 * When a general entity reference appears in the EntityValue in
2752 * an entity declaration, it is bypassed and left as is.
2753 * so XML_SUBSTITUTE_REF is not set here.
2754 */
2755 ret = xmlStringDecodeEntities(ctxt, buf, XML_SUBSTITUTE_PEREF,
2756 0, 0, 0);
2757 if (orig != NULL)
2758 *orig = buf;
2759 else
2760 xmlFree(buf);
2761 }
2762
2763 return(ret);
2764}
2765
2766/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002767 * xmlParseAttValueComplex:
2768 * @ctxt: an XML parser context
Daniel Veillard0fb18932003-09-07 09:14:37 +00002769 * @len: the resulting attribute len
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002770 * @normalize: wether to apply the inner normalization
Daniel Veillard01c13b52002-12-10 15:19:08 +00002771 *
2772 * parse a value for an attribute, this is the fallback function
2773 * of xmlParseAttValue() when the attribute parsing requires handling
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002774 * of non-ASCII characters, or normalization compaction.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002775 *
2776 * Returns the AttValue parsed or NULL. The value has to be freed by the caller.
2777 */
Daniel Veillard0fb18932003-09-07 09:14:37 +00002778static xmlChar *
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002779xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) {
Daniel Veillarde72c7562002-05-31 09:47:30 +00002780 xmlChar limit = 0;
2781 xmlChar *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002782 int len = 0;
2783 int buf_size = 0;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002784 int c, l, in_space = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002785 xmlChar *current = NULL;
2786 xmlEntityPtr ent;
2787
Owen Taylor3473f882001-02-23 17:55:21 +00002788 if (NXT(0) == '"') {
2789 ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
2790 limit = '"';
2791 NEXT;
2792 } else if (NXT(0) == '\'') {
2793 limit = '\'';
2794 ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
2795 NEXT;
2796 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002797 xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002798 return(NULL);
2799 }
2800
2801 /*
2802 * allocate a translation buffer.
2803 */
2804 buf_size = XML_PARSER_BUFFER_SIZE;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002805 buf = (xmlChar *) xmlMallocAtomic(buf_size * sizeof(xmlChar));
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002806 if (buf == NULL) goto mem_error;
Owen Taylor3473f882001-02-23 17:55:21 +00002807
2808 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002809 * OK loop until we reach one of the ending char or a size limit.
Owen Taylor3473f882001-02-23 17:55:21 +00002810 */
2811 c = CUR_CHAR(l);
Daniel Veillardfdc91562002-07-01 21:52:03 +00002812 while ((NXT(0) != limit) && /* checked */
2813 (c != '<')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002814 if (c == 0) break;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002815 if (c == '&') {
Daniel Veillard62998c02003-09-15 12:56:36 +00002816 in_space = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002817 if (NXT(1) == '#') {
2818 int val = xmlParseCharRef(ctxt);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002819
Owen Taylor3473f882001-02-23 17:55:21 +00002820 if (val == '&') {
Daniel Veillard319a7422001-09-11 09:27:09 +00002821 if (ctxt->replaceEntities) {
2822 if (len > buf_size - 10) {
2823 growBuffer(buf);
2824 }
2825 buf[len++] = '&';
2826 } else {
2827 /*
2828 * The reparsing will be done in xmlStringGetNodeList()
2829 * called by the attribute() function in SAX.c
2830 */
Daniel Veillard319a7422001-09-11 09:27:09 +00002831 if (len > buf_size - 10) {
2832 growBuffer(buf);
2833 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002834 buf[len++] = '&';
2835 buf[len++] = '#';
2836 buf[len++] = '3';
2837 buf[len++] = '8';
2838 buf[len++] = ';';
Owen Taylor3473f882001-02-23 17:55:21 +00002839 }
2840 } else {
Daniel Veillard0b6b55b2001-03-20 11:27:34 +00002841 if (len > buf_size - 10) {
2842 growBuffer(buf);
2843 }
Owen Taylor3473f882001-02-23 17:55:21 +00002844 len += xmlCopyChar(0, &buf[len], val);
2845 }
2846 } else {
2847 ent = xmlParseEntityRef(ctxt);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002848 if ((ent != NULL) &&
2849 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
2850 if (len > buf_size - 10) {
2851 growBuffer(buf);
2852 }
2853 if ((ctxt->replaceEntities == 0) &&
2854 (ent->content[0] == '&')) {
2855 buf[len++] = '&';
2856 buf[len++] = '#';
2857 buf[len++] = '3';
2858 buf[len++] = '8';
2859 buf[len++] = ';';
2860 } else {
2861 buf[len++] = ent->content[0];
2862 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002863 } else if ((ent != NULL) &&
2864 (ctxt->replaceEntities != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002865 xmlChar *rep;
2866
2867 if (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) {
2868 rep = xmlStringDecodeEntities(ctxt, ent->content,
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002869 XML_SUBSTITUTE_REF,
2870 0, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002871 if (rep != NULL) {
2872 current = rep;
2873 while (*current != 0) { /* non input consuming */
2874 buf[len++] = *current++;
2875 if (len > buf_size - 10) {
2876 growBuffer(buf);
2877 }
2878 }
2879 xmlFree(rep);
2880 }
2881 } else {
Daniel Veillard0b6b55b2001-03-20 11:27:34 +00002882 if (len > buf_size - 10) {
2883 growBuffer(buf);
2884 }
Owen Taylor3473f882001-02-23 17:55:21 +00002885 if (ent->content != NULL)
2886 buf[len++] = ent->content[0];
2887 }
2888 } else if (ent != NULL) {
2889 int i = xmlStrlen(ent->name);
2890 const xmlChar *cur = ent->name;
2891
2892 /*
2893 * This may look absurd but is needed to detect
2894 * entities problems
2895 */
2896 if ((ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) &&
2897 (ent->content != NULL)) {
2898 xmlChar *rep;
2899 rep = xmlStringDecodeEntities(ctxt, ent->content,
2900 XML_SUBSTITUTE_REF, 0, 0, 0);
2901 if (rep != NULL)
2902 xmlFree(rep);
2903 }
2904
2905 /*
2906 * Just output the reference
2907 */
2908 buf[len++] = '&';
2909 if (len > buf_size - i - 10) {
2910 growBuffer(buf);
2911 }
2912 for (;i > 0;i--)
2913 buf[len++] = *cur++;
2914 buf[len++] = ';';
2915 }
2916 }
2917 } else {
2918 if ((c == 0x20) || (c == 0xD) || (c == 0xA) || (c == 0x9)) {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002919 if ((len != 0) || (!normalize)) {
2920 if ((!normalize) || (!in_space)) {
2921 COPY_BUF(l,buf,len,0x20);
2922 if (len > buf_size - 10) {
2923 growBuffer(buf);
2924 }
2925 }
2926 in_space = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002927 }
2928 } else {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002929 in_space = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002930 COPY_BUF(l,buf,len,c);
2931 if (len > buf_size - 10) {
2932 growBuffer(buf);
2933 }
2934 }
2935 NEXTL(l);
2936 }
2937 GROW;
2938 c = CUR_CHAR(l);
2939 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002940 if ((in_space) && (normalize)) {
2941 while (buf[len - 1] == 0x20) len--;
2942 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002943 buf[len] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002944 if (RAW == '<') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002945 xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002946 } else if (RAW != limit) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00002947 xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
2948 "AttValue: ' expected\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002949 } else
2950 NEXT;
Daniel Veillard0fb18932003-09-07 09:14:37 +00002951 if (attlen != NULL) *attlen = len;
Owen Taylor3473f882001-02-23 17:55:21 +00002952 return(buf);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002953
2954mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002955 xmlErrMemory(ctxt, NULL);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002956 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002957}
2958
2959/**
Daniel Veillard0fb18932003-09-07 09:14:37 +00002960 * xmlParseAttValue:
2961 * @ctxt: an XML parser context
2962 *
2963 * parse a value for an attribute
2964 * Note: the parser won't do substitution of entities here, this
2965 * will be handled later in xmlStringGetNodeList
2966 *
2967 * [10] AttValue ::= '"' ([^<&"] | Reference)* '"' |
2968 * "'" ([^<&'] | Reference)* "'"
2969 *
2970 * 3.3.3 Attribute-Value Normalization:
2971 * Before the value of an attribute is passed to the application or
2972 * checked for validity, the XML processor must normalize it as follows:
2973 * - a character reference is processed by appending the referenced
2974 * character to the attribute value
2975 * - an entity reference is processed by recursively processing the
2976 * replacement text of the entity
2977 * - a whitespace character (#x20, #xD, #xA, #x9) is processed by
2978 * appending #x20 to the normalized value, except that only a single
2979 * #x20 is appended for a "#xD#xA" sequence that is part of an external
2980 * parsed entity or the literal entity value of an internal parsed entity
2981 * - other characters are processed by appending them to the normalized value
2982 * If the declared value is not CDATA, then the XML processor must further
2983 * process the normalized attribute value by discarding any leading and
2984 * trailing space (#x20) characters, and by replacing sequences of space
2985 * (#x20) characters by a single space (#x20) character.
2986 * All attributes for which no declaration has been read should be treated
2987 * by a non-validating parser as if declared CDATA.
2988 *
2989 * Returns the AttValue parsed or NULL. The value has to be freed by the caller.
2990 */
2991
2992
2993xmlChar *
2994xmlParseAttValue(xmlParserCtxtPtr ctxt) {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002995 return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0));
Daniel Veillard0fb18932003-09-07 09:14:37 +00002996}
2997
2998/**
Owen Taylor3473f882001-02-23 17:55:21 +00002999 * xmlParseSystemLiteral:
3000 * @ctxt: an XML parser context
3001 *
3002 * parse an XML Literal
3003 *
3004 * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
3005 *
3006 * Returns the SystemLiteral parsed or NULL
3007 */
3008
3009xmlChar *
3010xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) {
3011 xmlChar *buf = NULL;
3012 int len = 0;
3013 int size = XML_PARSER_BUFFER_SIZE;
3014 int cur, l;
3015 xmlChar stop;
3016 int state = ctxt->instate;
3017 int count = 0;
3018
3019 SHRINK;
3020 if (RAW == '"') {
3021 NEXT;
3022 stop = '"';
3023 } else if (RAW == '\'') {
3024 NEXT;
3025 stop = '\'';
3026 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003027 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003028 return(NULL);
3029 }
3030
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003031 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003032 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003033 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003034 return(NULL);
3035 }
3036 ctxt->instate = XML_PARSER_SYSTEM_LITERAL;
3037 cur = CUR_CHAR(l);
William M. Brack871611b2003-10-18 04:53:14 +00003038 while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003039 if (len + 5 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00003040 xmlChar *tmp;
3041
Owen Taylor3473f882001-02-23 17:55:21 +00003042 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00003043 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3044 if (tmp == NULL) {
3045 xmlFree(buf);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003046 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003047 ctxt->instate = (xmlParserInputState) state;
3048 return(NULL);
3049 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00003050 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00003051 }
3052 count++;
3053 if (count > 50) {
3054 GROW;
3055 count = 0;
3056 }
3057 COPY_BUF(l,buf,len,cur);
3058 NEXTL(l);
3059 cur = CUR_CHAR(l);
3060 if (cur == 0) {
3061 GROW;
3062 SHRINK;
3063 cur = CUR_CHAR(l);
3064 }
3065 }
3066 buf[len] = 0;
3067 ctxt->instate = (xmlParserInputState) state;
William M. Brack871611b2003-10-18 04:53:14 +00003068 if (!IS_CHAR(cur)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003069 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003070 } else {
3071 NEXT;
3072 }
3073 return(buf);
3074}
3075
3076/**
3077 * xmlParsePubidLiteral:
3078 * @ctxt: an XML parser context
3079 *
3080 * parse an XML public literal
3081 *
3082 * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
3083 *
3084 * Returns the PubidLiteral parsed or NULL.
3085 */
3086
3087xmlChar *
3088xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) {
3089 xmlChar *buf = NULL;
3090 int len = 0;
3091 int size = XML_PARSER_BUFFER_SIZE;
3092 xmlChar cur;
3093 xmlChar stop;
3094 int count = 0;
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003095 xmlParserInputState oldstate = ctxt->instate;
Owen Taylor3473f882001-02-23 17:55:21 +00003096
3097 SHRINK;
3098 if (RAW == '"') {
3099 NEXT;
3100 stop = '"';
3101 } else if (RAW == '\'') {
3102 NEXT;
3103 stop = '\'';
3104 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003105 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003106 return(NULL);
3107 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003108 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003109 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003110 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003111 return(NULL);
3112 }
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003113 ctxt->instate = XML_PARSER_PUBLIC_LITERAL;
Owen Taylor3473f882001-02-23 17:55:21 +00003114 cur = CUR;
William M. Brack76e95df2003-10-18 16:20:14 +00003115 while ((IS_PUBIDCHAR_CH(cur)) && (cur != stop)) { /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003116 if (len + 1 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00003117 xmlChar *tmp;
3118
Owen Taylor3473f882001-02-23 17:55:21 +00003119 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00003120 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3121 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003122 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00003123 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003124 return(NULL);
3125 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00003126 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00003127 }
3128 buf[len++] = cur;
3129 count++;
3130 if (count > 50) {
3131 GROW;
3132 count = 0;
3133 }
3134 NEXT;
3135 cur = CUR;
3136 if (cur == 0) {
3137 GROW;
3138 SHRINK;
3139 cur = CUR;
3140 }
3141 }
3142 buf[len] = 0;
3143 if (cur != stop) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003144 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003145 } else {
3146 NEXT;
3147 }
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003148 ctxt->instate = oldstate;
Owen Taylor3473f882001-02-23 17:55:21 +00003149 return(buf);
3150}
3151
Daniel Veillard48b2f892001-02-25 16:11:03 +00003152void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata);
Owen Taylor3473f882001-02-23 17:55:21 +00003153/**
3154 * xmlParseCharData:
3155 * @ctxt: an XML parser context
3156 * @cdata: int indicating whether we are within a CDATA section
3157 *
3158 * parse a CharData section.
3159 * if we are within a CDATA section ']]>' marks an end of section.
3160 *
3161 * The right angle bracket (>) may be represented using the string "&gt;",
3162 * and must, for compatibility, be escaped using "&gt;" or a character
3163 * reference when it appears in the string "]]>" in content, when that
3164 * string is not marking the end of a CDATA section.
3165 *
3166 * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
3167 */
3168
3169void
3170xmlParseCharData(xmlParserCtxtPtr ctxt, int cdata) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00003171 const xmlChar *in;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003172 int nbchar = 0;
Daniel Veillard50582112001-03-26 22:52:16 +00003173 int line = ctxt->input->line;
3174 int col = ctxt->input->col;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003175
3176 SHRINK;
3177 GROW;
3178 /*
3179 * Accelerated common case where input don't need to be
3180 * modified before passing it to the handler.
3181 */
Daniel Veillardfdc91562002-07-01 21:52:03 +00003182 if (!cdata) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00003183 in = ctxt->input->cur;
3184 do {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003185get_more_space:
3186 while (*in == 0x20) in++;
3187 if (*in == 0xA) {
3188 ctxt->input->line++;
3189 in++;
3190 while (*in == 0xA) {
3191 ctxt->input->line++;
3192 in++;
3193 }
3194 goto get_more_space;
3195 }
3196 if (*in == '<') {
3197 nbchar = in - ctxt->input->cur;
3198 if (nbchar > 0) {
3199 const xmlChar *tmp = ctxt->input->cur;
3200 ctxt->input->cur = in;
3201
3202 if (ctxt->sax->ignorableWhitespace !=
3203 ctxt->sax->characters) {
3204 if (areBlanks(ctxt, tmp, nbchar, 1)) {
3205 ctxt->sax->ignorableWhitespace(ctxt->userData,
3206 tmp, nbchar);
3207 } else if (ctxt->sax->characters != NULL)
3208 ctxt->sax->characters(ctxt->userData,
3209 tmp, nbchar);
3210 } else if (ctxt->sax->characters != NULL) {
3211 ctxt->sax->characters(ctxt->userData,
3212 tmp, nbchar);
3213 }
3214 }
3215 return;
3216 }
Daniel Veillard3ed155f2001-04-29 19:56:59 +00003217get_more:
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003218 while (((*in > ']') && (*in <= 0x7F)) ||
3219 ((*in > '&') && (*in < '<')) ||
3220 ((*in > '<') && (*in < ']')) ||
3221 ((*in >= 0x20) && (*in < '&')) ||
3222 (*in == 0x09))
3223 in++;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003224 if (*in == 0xA) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00003225 ctxt->input->line++;
Daniel Veillard3ed155f2001-04-29 19:56:59 +00003226 in++;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003227 while (*in == 0xA) {
Daniel Veillard3ed155f2001-04-29 19:56:59 +00003228 ctxt->input->line++;
3229 in++;
3230 }
3231 goto get_more;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003232 }
3233 if (*in == ']') {
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003234 if ((in[1] == ']') && (in[2] == '>')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003235 xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003236 ctxt->input->cur = in;
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003237 return;
3238 }
3239 in++;
3240 goto get_more;
3241 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00003242 nbchar = in - ctxt->input->cur;
Daniel Veillard80f32572001-03-07 19:45:40 +00003243 if (nbchar > 0) {
Daniel Veillard40412cd2003-09-03 13:28:32 +00003244 if ((ctxt->sax->ignorableWhitespace !=
3245 ctxt->sax->characters) &&
William M. Brack76e95df2003-10-18 16:20:14 +00003246 (IS_BLANK_CH(*ctxt->input->cur))) {
Daniel Veillarda7374592001-05-10 14:17:55 +00003247 const xmlChar *tmp = ctxt->input->cur;
3248 ctxt->input->cur = in;
Daniel Veillard40412cd2003-09-03 13:28:32 +00003249
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003250 if (areBlanks(ctxt, tmp, nbchar, 0)) {
Daniel Veillard40412cd2003-09-03 13:28:32 +00003251 ctxt->sax->ignorableWhitespace(ctxt->userData,
3252 tmp, nbchar);
3253 } else if (ctxt->sax->characters != NULL)
3254 ctxt->sax->characters(ctxt->userData,
3255 tmp, nbchar);
Daniel Veillard3ed27bd2001-06-17 17:58:17 +00003256 line = ctxt->input->line;
3257 col = ctxt->input->col;
Daniel Veillard80f32572001-03-07 19:45:40 +00003258 } else {
3259 if (ctxt->sax->characters != NULL)
3260 ctxt->sax->characters(ctxt->userData,
3261 ctxt->input->cur, nbchar);
Daniel Veillard3ed27bd2001-06-17 17:58:17 +00003262 line = ctxt->input->line;
3263 col = ctxt->input->col;
Daniel Veillard80f32572001-03-07 19:45:40 +00003264 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00003265 }
3266 ctxt->input->cur = in;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003267 if (*in == 0xD) {
3268 in++;
3269 if (*in == 0xA) {
3270 ctxt->input->cur = in;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003271 in++;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003272 ctxt->input->line++;
3273 continue; /* while */
Daniel Veillard48b2f892001-02-25 16:11:03 +00003274 }
Daniel Veillard561b7f82002-03-20 21:55:57 +00003275 in--;
3276 }
3277 if (*in == '<') {
3278 return;
3279 }
3280 if (*in == '&') {
3281 return;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003282 }
3283 SHRINK;
3284 GROW;
3285 in = ctxt->input->cur;
William M. Brackc07329e2003-09-08 01:57:30 +00003286 } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09));
Daniel Veillard48b2f892001-02-25 16:11:03 +00003287 nbchar = 0;
3288 }
Daniel Veillard50582112001-03-26 22:52:16 +00003289 ctxt->input->line = line;
3290 ctxt->input->col = col;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003291 xmlParseCharDataComplex(ctxt, cdata);
3292}
3293
Daniel Veillard01c13b52002-12-10 15:19:08 +00003294/**
3295 * xmlParseCharDataComplex:
3296 * @ctxt: an XML parser context
3297 * @cdata: int indicating whether we are within a CDATA section
3298 *
3299 * parse a CharData section.this is the fallback function
3300 * of xmlParseCharData() when the parsing requires handling
3301 * of non-ASCII characters.
3302 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00003303void
3304xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata) {
Owen Taylor3473f882001-02-23 17:55:21 +00003305 xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5];
3306 int nbchar = 0;
3307 int cur, l;
3308 int count = 0;
3309
3310 SHRINK;
3311 GROW;
3312 cur = CUR_CHAR(l);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003313 while ((cur != '<') && /* checked */
3314 (cur != '&') &&
William M. Brack871611b2003-10-18 04:53:14 +00003315 (IS_CHAR(cur))) /* test also done in xmlCurrentChar() */ {
Owen Taylor3473f882001-02-23 17:55:21 +00003316 if ((cur == ']') && (NXT(1) == ']') &&
3317 (NXT(2) == '>')) {
3318 if (cdata) break;
3319 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003320 xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003321 }
3322 }
3323 COPY_BUF(l,buf,nbchar,cur);
3324 if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) {
Daniel Veillard092643b2003-09-25 14:29:29 +00003325 buf[nbchar] = 0;
3326
Owen Taylor3473f882001-02-23 17:55:21 +00003327 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003328 * OK the segment is to be consumed as chars.
Owen Taylor3473f882001-02-23 17:55:21 +00003329 */
3330 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003331 if (areBlanks(ctxt, buf, nbchar, 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003332 if (ctxt->sax->ignorableWhitespace != NULL)
3333 ctxt->sax->ignorableWhitespace(ctxt->userData,
3334 buf, nbchar);
3335 } else {
3336 if (ctxt->sax->characters != NULL)
3337 ctxt->sax->characters(ctxt->userData, buf, nbchar);
3338 }
3339 }
3340 nbchar = 0;
3341 }
3342 count++;
3343 if (count > 50) {
3344 GROW;
3345 count = 0;
3346 }
3347 NEXTL(l);
3348 cur = CUR_CHAR(l);
3349 }
3350 if (nbchar != 0) {
Daniel Veillard092643b2003-09-25 14:29:29 +00003351 buf[nbchar] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003352 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003353 * OK the segment is to be consumed as chars.
Owen Taylor3473f882001-02-23 17:55:21 +00003354 */
3355 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003356 if (areBlanks(ctxt, buf, nbchar, 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003357 if (ctxt->sax->ignorableWhitespace != NULL)
3358 ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar);
3359 } else {
3360 if (ctxt->sax->characters != NULL)
3361 ctxt->sax->characters(ctxt->userData, buf, nbchar);
3362 }
3363 }
3364 }
3365}
3366
3367/**
3368 * xmlParseExternalID:
3369 * @ctxt: an XML parser context
3370 * @publicID: a xmlChar** receiving PubidLiteral
3371 * @strict: indicate whether we should restrict parsing to only
3372 * production [75], see NOTE below
3373 *
3374 * Parse an External ID or a Public ID
3375 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003376 * NOTE: Productions [75] and [83] interact badly since [75] can generate
Owen Taylor3473f882001-02-23 17:55:21 +00003377 * 'PUBLIC' S PubidLiteral S SystemLiteral
3378 *
3379 * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
3380 * | 'PUBLIC' S PubidLiteral S SystemLiteral
3381 *
3382 * [83] PublicID ::= 'PUBLIC' S PubidLiteral
3383 *
3384 * Returns the function returns SystemLiteral and in the second
3385 * case publicID receives PubidLiteral, is strict is off
3386 * it is possible to return NULL and have publicID set.
3387 */
3388
3389xmlChar *
3390xmlParseExternalID(xmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) {
3391 xmlChar *URI = NULL;
3392
3393 SHRINK;
Daniel Veillard146c9122001-03-22 15:22:27 +00003394
3395 *publicID = NULL;
Daniel Veillarda07050d2003-10-19 14:46:32 +00003396 if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003397 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00003398 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003399 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3400 "Space required after 'SYSTEM'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003401 }
3402 SKIP_BLANKS;
3403 URI = xmlParseSystemLiteral(ctxt);
3404 if (URI == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003405 xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003406 }
Daniel Veillarda07050d2003-10-19 14:46:32 +00003407 } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003408 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00003409 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003410 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00003411 "Space required after 'PUBLIC'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003412 }
3413 SKIP_BLANKS;
3414 *publicID = xmlParsePubidLiteral(ctxt);
3415 if (*publicID == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003416 xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003417 }
3418 if (strict) {
3419 /*
3420 * We don't handle [83] so "S SystemLiteral" is required.
3421 */
William M. Brack76e95df2003-10-18 16:20:14 +00003422 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003423 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00003424 "Space required after the Public Identifier\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003425 }
3426 } else {
3427 /*
3428 * We handle [83] so we return immediately, if
3429 * "S SystemLiteral" is not detected. From a purely parsing
3430 * point of view that's a nice mess.
3431 */
3432 const xmlChar *ptr;
3433 GROW;
3434
3435 ptr = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00003436 if (!IS_BLANK_CH(*ptr)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003437
William M. Brack76e95df2003-10-18 16:20:14 +00003438 while (IS_BLANK_CH(*ptr)) ptr++; /* TODO: dangerous, fix ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003439 if ((*ptr != '\'') && (*ptr != '"')) return(NULL);
3440 }
3441 SKIP_BLANKS;
3442 URI = xmlParseSystemLiteral(ctxt);
3443 if (URI == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003444 xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003445 }
3446 }
3447 return(URI);
3448}
3449
3450/**
3451 * xmlParseComment:
3452 * @ctxt: an XML parser context
3453 *
3454 * Skip an XML (SGML) comment <!-- .... -->
3455 * The spec says that "For compatibility, the string "--" (double-hyphen)
3456 * must not occur within comments. "
3457 *
3458 * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
3459 */
3460void
3461xmlParseComment(xmlParserCtxtPtr ctxt) {
3462 xmlChar *buf = NULL;
3463 int len;
3464 int size = XML_PARSER_BUFFER_SIZE;
3465 int q, ql;
3466 int r, rl;
3467 int cur, l;
3468 xmlParserInputState state;
3469 xmlParserInputPtr input = ctxt->input;
3470 int count = 0;
3471
3472 /*
3473 * Check that there is a comment right here.
3474 */
3475 if ((RAW != '<') || (NXT(1) != '!') ||
3476 (NXT(2) != '-') || (NXT(3) != '-')) return;
3477
3478 state = ctxt->instate;
3479 ctxt->instate = XML_PARSER_COMMENT;
3480 SHRINK;
3481 SKIP(4);
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003482 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003483 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003484 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003485 ctxt->instate = state;
3486 return;
3487 }
3488 q = CUR_CHAR(ql);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003489 if (q == 0)
3490 goto not_terminated;
Owen Taylor3473f882001-02-23 17:55:21 +00003491 NEXTL(ql);
3492 r = CUR_CHAR(rl);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003493 if (r == 0)
3494 goto not_terminated;
Owen Taylor3473f882001-02-23 17:55:21 +00003495 NEXTL(rl);
3496 cur = CUR_CHAR(l);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003497 if (cur == 0)
3498 goto not_terminated;
Owen Taylor3473f882001-02-23 17:55:21 +00003499 len = 0;
William M. Brack871611b2003-10-18 04:53:14 +00003500 while (IS_CHAR(cur) && /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003501 ((cur != '>') ||
3502 (r != '-') || (q != '-'))) {
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003503 if ((r == '-') && (q == '-')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003504 xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003505 }
3506 if (len + 5 >= size) {
William M. Bracka3215c72004-07-31 16:24:01 +00003507 xmlChar *new_buf;
Owen Taylor3473f882001-02-23 17:55:21 +00003508 size *= 2;
William M. Bracka3215c72004-07-31 16:24:01 +00003509 new_buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3510 if (new_buf == NULL) {
3511 xmlFree (buf);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003512 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003513 ctxt->instate = state;
3514 return;
3515 }
William M. Bracka3215c72004-07-31 16:24:01 +00003516 buf = new_buf;
Owen Taylor3473f882001-02-23 17:55:21 +00003517 }
3518 COPY_BUF(ql,buf,len,q);
3519 q = r;
3520 ql = rl;
3521 r = cur;
3522 rl = l;
3523
3524 count++;
3525 if (count > 50) {
3526 GROW;
3527 count = 0;
3528 }
3529 NEXTL(l);
3530 cur = CUR_CHAR(l);
3531 if (cur == 0) {
3532 SHRINK;
3533 GROW;
3534 cur = CUR_CHAR(l);
3535 }
3536 }
3537 buf[len] = 0;
William M. Brack871611b2003-10-18 04:53:14 +00003538 if (!IS_CHAR(cur)) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00003539 xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
Owen Taylor3473f882001-02-23 17:55:21 +00003540 "Comment not terminated \n<!--%.50s\n", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003541 xmlFree(buf);
3542 } else {
3543 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003544 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
3545 "Comment doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003546 }
3547 NEXT;
3548 if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
3549 (!ctxt->disableSAX))
3550 ctxt->sax->comment(ctxt->userData, buf);
3551 xmlFree(buf);
3552 }
3553 ctxt->instate = state;
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003554 return;
3555not_terminated:
3556 xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
3557 "Comment not terminated\n", NULL);
3558 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003559}
3560
3561/**
3562 * xmlParsePITarget:
3563 * @ctxt: an XML parser context
3564 *
3565 * parse the name of a PI
3566 *
3567 * [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
3568 *
3569 * Returns the PITarget name or NULL
3570 */
3571
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003572const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00003573xmlParsePITarget(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003574 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003575
3576 name = xmlParseName(ctxt);
3577 if ((name != NULL) &&
3578 ((name[0] == 'x') || (name[0] == 'X')) &&
3579 ((name[1] == 'm') || (name[1] == 'M')) &&
3580 ((name[2] == 'l') || (name[2] == 'L'))) {
3581 int i;
3582 if ((name[0] == 'x') && (name[1] == 'm') &&
3583 (name[2] == 'l') && (name[3] == 0)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003584 xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
Owen Taylor3473f882001-02-23 17:55:21 +00003585 "XML declaration allowed only at the start of the document\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003586 return(name);
3587 } else if (name[3] == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003588 xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003589 return(name);
3590 }
3591 for (i = 0;;i++) {
3592 if (xmlW3CPIs[i] == NULL) break;
3593 if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i]))
3594 return(name);
3595 }
Daniel Veillard24eb9782003-10-04 21:08:09 +00003596 xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
3597 "xmlParsePITarget: invalid name prefix 'xml'\n",
3598 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003599 }
3600 return(name);
3601}
3602
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003603#ifdef LIBXML_CATALOG_ENABLED
3604/**
3605 * xmlParseCatalogPI:
3606 * @ctxt: an XML parser context
3607 * @catalog: the PI value string
3608 *
3609 * parse an XML Catalog Processing Instruction.
3610 *
3611 * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?>
3612 *
3613 * Occurs only if allowed by the user and if happening in the Misc
3614 * part of the document before any doctype informations
3615 * This will add the given catalog to the parsing context in order
3616 * to be used if there is a resolution need further down in the document
3617 */
3618
3619static void
3620xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) {
3621 xmlChar *URL = NULL;
3622 const xmlChar *tmp, *base;
3623 xmlChar marker;
3624
3625 tmp = catalog;
William M. Brack76e95df2003-10-18 16:20:14 +00003626 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003627 if (xmlStrncmp(tmp, BAD_CAST"catalog", 7))
3628 goto error;
3629 tmp += 7;
William M. Brack76e95df2003-10-18 16:20:14 +00003630 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003631 if (*tmp != '=') {
3632 return;
3633 }
3634 tmp++;
William M. Brack76e95df2003-10-18 16:20:14 +00003635 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003636 marker = *tmp;
3637 if ((marker != '\'') && (marker != '"'))
3638 goto error;
3639 tmp++;
3640 base = tmp;
3641 while ((*tmp != 0) && (*tmp != marker)) tmp++;
3642 if (*tmp == 0)
3643 goto error;
3644 URL = xmlStrndup(base, tmp - base);
3645 tmp++;
William M. Brack76e95df2003-10-18 16:20:14 +00003646 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003647 if (*tmp != 0)
3648 goto error;
3649
3650 if (URL != NULL) {
3651 ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
3652 xmlFree(URL);
3653 }
3654 return;
3655
3656error:
Daniel Veillard24eb9782003-10-04 21:08:09 +00003657 xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI,
3658 "Catalog PI syntax error: %s\n",
3659 catalog, NULL);
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003660 if (URL != NULL)
3661 xmlFree(URL);
3662}
3663#endif
3664
Owen Taylor3473f882001-02-23 17:55:21 +00003665/**
3666 * xmlParsePI:
3667 * @ctxt: an XML parser context
3668 *
3669 * parse an XML Processing Instruction.
3670 *
3671 * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
3672 *
3673 * The processing is transfered to SAX once parsed.
3674 */
3675
3676void
3677xmlParsePI(xmlParserCtxtPtr ctxt) {
3678 xmlChar *buf = NULL;
3679 int len = 0;
3680 int size = XML_PARSER_BUFFER_SIZE;
3681 int cur, l;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003682 const xmlChar *target;
Owen Taylor3473f882001-02-23 17:55:21 +00003683 xmlParserInputState state;
3684 int count = 0;
3685
3686 if ((RAW == '<') && (NXT(1) == '?')) {
3687 xmlParserInputPtr input = ctxt->input;
3688 state = ctxt->instate;
3689 ctxt->instate = XML_PARSER_PI;
3690 /*
3691 * this is a Processing Instruction.
3692 */
3693 SKIP(2);
3694 SHRINK;
3695
3696 /*
3697 * Parse the target name and check for special support like
3698 * namespace.
3699 */
3700 target = xmlParsePITarget(ctxt);
3701 if (target != NULL) {
3702 if ((RAW == '?') && (NXT(1) == '>')) {
3703 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003704 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
3705 "PI declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003706 }
3707 SKIP(2);
3708
3709 /*
3710 * SAX: PI detected.
3711 */
3712 if ((ctxt->sax) && (!ctxt->disableSAX) &&
3713 (ctxt->sax->processingInstruction != NULL))
3714 ctxt->sax->processingInstruction(ctxt->userData,
3715 target, NULL);
3716 ctxt->instate = state;
Owen Taylor3473f882001-02-23 17:55:21 +00003717 return;
3718 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003719 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003720 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003721 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003722 ctxt->instate = state;
3723 return;
3724 }
3725 cur = CUR;
3726 if (!IS_BLANK(cur)) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00003727 xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED,
3728 "ParsePI: PI %s space expected\n", target);
Owen Taylor3473f882001-02-23 17:55:21 +00003729 }
3730 SKIP_BLANKS;
3731 cur = CUR_CHAR(l);
William M. Brack871611b2003-10-18 04:53:14 +00003732 while (IS_CHAR(cur) && /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003733 ((cur != '?') || (NXT(1) != '>'))) {
3734 if (len + 5 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00003735 xmlChar *tmp;
3736
Owen Taylor3473f882001-02-23 17:55:21 +00003737 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00003738 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3739 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003740 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00003741 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003742 ctxt->instate = state;
3743 return;
3744 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00003745 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00003746 }
3747 count++;
3748 if (count > 50) {
3749 GROW;
3750 count = 0;
3751 }
3752 COPY_BUF(l,buf,len,cur);
3753 NEXTL(l);
3754 cur = CUR_CHAR(l);
3755 if (cur == 0) {
3756 SHRINK;
3757 GROW;
3758 cur = CUR_CHAR(l);
3759 }
3760 }
3761 buf[len] = 0;
3762 if (cur != '?') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00003763 xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED,
3764 "ParsePI: PI %s never end ...\n", target);
Owen Taylor3473f882001-02-23 17:55:21 +00003765 } else {
3766 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003767 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3768 "PI declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003769 }
3770 SKIP(2);
3771
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003772#ifdef LIBXML_CATALOG_ENABLED
3773 if (((state == XML_PARSER_MISC) ||
3774 (state == XML_PARSER_START)) &&
3775 (xmlStrEqual(target, XML_CATALOG_PI))) {
3776 xmlCatalogAllow allow = xmlCatalogGetDefaults();
3777 if ((allow == XML_CATA_ALLOW_DOCUMENT) ||
3778 (allow == XML_CATA_ALLOW_ALL))
3779 xmlParseCatalogPI(ctxt, buf);
3780 }
3781#endif
3782
3783
Owen Taylor3473f882001-02-23 17:55:21 +00003784 /*
3785 * SAX: PI detected.
3786 */
3787 if ((ctxt->sax) && (!ctxt->disableSAX) &&
3788 (ctxt->sax->processingInstruction != NULL))
3789 ctxt->sax->processingInstruction(ctxt->userData,
3790 target, buf);
3791 }
3792 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003793 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003794 xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003795 }
3796 ctxt->instate = state;
3797 }
3798}
3799
3800/**
3801 * xmlParseNotationDecl:
3802 * @ctxt: an XML parser context
3803 *
3804 * parse a notation declaration
3805 *
3806 * [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>'
3807 *
3808 * Hence there is actually 3 choices:
3809 * 'PUBLIC' S PubidLiteral
3810 * 'PUBLIC' S PubidLiteral S SystemLiteral
3811 * and 'SYSTEM' S SystemLiteral
3812 *
3813 * See the NOTE on xmlParseExternalID().
3814 */
3815
3816void
3817xmlParseNotationDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003818 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003819 xmlChar *Pubid;
3820 xmlChar *Systemid;
3821
Daniel Veillarda07050d2003-10-19 14:46:32 +00003822 if (CMP10(CUR_PTR, '<', '!', 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003823 xmlParserInputPtr input = ctxt->input;
3824 SHRINK;
3825 SKIP(10);
William M. Brack76e95df2003-10-18 16:20:14 +00003826 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003827 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3828 "Space required after '<!NOTATION'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003829 return;
3830 }
3831 SKIP_BLANKS;
3832
Daniel Veillard76d66f42001-05-16 21:05:17 +00003833 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003834 if (name == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003835 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003836 return;
3837 }
William M. Brack76e95df2003-10-18 16:20:14 +00003838 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003839 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00003840 "Space required after the NOTATION name'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003841 return;
3842 }
3843 SKIP_BLANKS;
3844
3845 /*
3846 * Parse the IDs.
3847 */
3848 Systemid = xmlParseExternalID(ctxt, &Pubid, 0);
3849 SKIP_BLANKS;
3850
3851 if (RAW == '>') {
3852 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003853 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3854 "Notation declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003855 }
3856 NEXT;
3857 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
3858 (ctxt->sax->notationDecl != NULL))
3859 ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid);
3860 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003861 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003862 }
Owen Taylor3473f882001-02-23 17:55:21 +00003863 if (Systemid != NULL) xmlFree(Systemid);
3864 if (Pubid != NULL) xmlFree(Pubid);
3865 }
3866}
3867
3868/**
3869 * xmlParseEntityDecl:
3870 * @ctxt: an XML parser context
3871 *
3872 * parse <!ENTITY declarations
3873 *
3874 * [70] EntityDecl ::= GEDecl | PEDecl
3875 *
3876 * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>'
3877 *
3878 * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>'
3879 *
3880 * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?)
3881 *
3882 * [74] PEDef ::= EntityValue | ExternalID
3883 *
3884 * [76] NDataDecl ::= S 'NDATA' S Name
3885 *
3886 * [ VC: Notation Declared ]
3887 * The Name must match the declared name of a notation.
3888 */
3889
3890void
3891xmlParseEntityDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003892 const xmlChar *name = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003893 xmlChar *value = NULL;
3894 xmlChar *URI = NULL, *literal = NULL;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003895 const xmlChar *ndata = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003896 int isParameter = 0;
3897 xmlChar *orig = NULL;
Daniel Veillardf5582f12002-06-11 10:08:16 +00003898 int skipped;
Owen Taylor3473f882001-02-23 17:55:21 +00003899
3900 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00003901 if (CMP8(CUR_PTR, '<', '!', 'E', 'N', 'T', 'I', 'T', 'Y')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003902 xmlParserInputPtr input = ctxt->input;
Owen Taylor3473f882001-02-23 17:55:21 +00003903 SHRINK;
3904 SKIP(8);
Daniel Veillardf5582f12002-06-11 10:08:16 +00003905 skipped = SKIP_BLANKS;
3906 if (skipped == 0) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003907 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3908 "Space required after '<!ENTITY'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003909 }
Owen Taylor3473f882001-02-23 17:55:21 +00003910
3911 if (RAW == '%') {
3912 NEXT;
Daniel Veillardf5582f12002-06-11 10:08:16 +00003913 skipped = SKIP_BLANKS;
3914 if (skipped == 0) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003915 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3916 "Space required after '%'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003917 }
Owen Taylor3473f882001-02-23 17:55:21 +00003918 isParameter = 1;
3919 }
3920
Daniel Veillard76d66f42001-05-16 21:05:17 +00003921 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003922 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003923 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
3924 "xmlParseEntityDecl: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003925 return;
3926 }
Daniel Veillardf5582f12002-06-11 10:08:16 +00003927 skipped = SKIP_BLANKS;
3928 if (skipped == 0) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003929 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3930 "Space required after the entity name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003931 }
Owen Taylor3473f882001-02-23 17:55:21 +00003932
Daniel Veillardf5582f12002-06-11 10:08:16 +00003933 ctxt->instate = XML_PARSER_ENTITY_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00003934 /*
3935 * handle the various case of definitions...
3936 */
3937 if (isParameter) {
3938 if ((RAW == '"') || (RAW == '\'')) {
3939 value = xmlParseEntityValue(ctxt, &orig);
3940 if (value) {
3941 if ((ctxt->sax != NULL) &&
3942 (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
3943 ctxt->sax->entityDecl(ctxt->userData, name,
3944 XML_INTERNAL_PARAMETER_ENTITY,
3945 NULL, NULL, value);
3946 }
3947 } else {
3948 URI = xmlParseExternalID(ctxt, &literal, 1);
3949 if ((URI == NULL) && (literal == NULL)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003950 xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003951 }
3952 if (URI) {
3953 xmlURIPtr uri;
3954
3955 uri = xmlParseURI((const char *) URI);
3956 if (uri == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003957 xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI,
3958 "Invalid URI: %s\n", URI);
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003959 /*
3960 * This really ought to be a well formedness error
3961 * but the XML Core WG decided otherwise c.f. issue
3962 * E26 of the XML erratas.
3963 */
Owen Taylor3473f882001-02-23 17:55:21 +00003964 } else {
3965 if (uri->fragment != NULL) {
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003966 /*
3967 * Okay this is foolish to block those but not
3968 * invalid URIs.
3969 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003970 xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003971 } else {
3972 if ((ctxt->sax != NULL) &&
3973 (!ctxt->disableSAX) &&
3974 (ctxt->sax->entityDecl != NULL))
3975 ctxt->sax->entityDecl(ctxt->userData, name,
3976 XML_EXTERNAL_PARAMETER_ENTITY,
3977 literal, URI, NULL);
3978 }
3979 xmlFreeURI(uri);
3980 }
3981 }
3982 }
3983 } else {
3984 if ((RAW == '"') || (RAW == '\'')) {
3985 value = xmlParseEntityValue(ctxt, &orig);
3986 if ((ctxt->sax != NULL) &&
3987 (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
3988 ctxt->sax->entityDecl(ctxt->userData, name,
3989 XML_INTERNAL_GENERAL_ENTITY,
3990 NULL, NULL, value);
Daniel Veillard5997aca2002-03-18 18:36:20 +00003991 /*
3992 * For expat compatibility in SAX mode.
3993 */
3994 if ((ctxt->myDoc == NULL) ||
3995 (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) {
3996 if (ctxt->myDoc == NULL) {
3997 ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
3998 }
3999 if (ctxt->myDoc->intSubset == NULL)
4000 ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
4001 BAD_CAST "fake", NULL, NULL);
4002
Daniel Veillard1af9a412003-08-20 22:54:39 +00004003 xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY,
4004 NULL, NULL, value);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004005 }
Owen Taylor3473f882001-02-23 17:55:21 +00004006 } else {
4007 URI = xmlParseExternalID(ctxt, &literal, 1);
4008 if ((URI == NULL) && (literal == NULL)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004009 xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004010 }
4011 if (URI) {
4012 xmlURIPtr uri;
4013
4014 uri = xmlParseURI((const char *)URI);
4015 if (uri == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004016 xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI,
4017 "Invalid URI: %s\n", URI);
Daniel Veillard4a7ae502002-02-18 19:18:17 +00004018 /*
4019 * This really ought to be a well formedness error
4020 * but the XML Core WG decided otherwise c.f. issue
4021 * E26 of the XML erratas.
4022 */
Owen Taylor3473f882001-02-23 17:55:21 +00004023 } else {
4024 if (uri->fragment != NULL) {
Daniel Veillard4a7ae502002-02-18 19:18:17 +00004025 /*
4026 * Okay this is foolish to block those but not
4027 * invalid URIs.
4028 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004029 xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004030 }
4031 xmlFreeURI(uri);
4032 }
4033 }
William M. Brack76e95df2003-10-18 16:20:14 +00004034 if ((RAW != '>') && (!IS_BLANK_CH(CUR))) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004035 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4036 "Space required before 'NDATA'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004037 }
4038 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004039 if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004040 SKIP(5);
William M. Brack76e95df2003-10-18 16:20:14 +00004041 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004042 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4043 "Space required after 'NDATA'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004044 }
4045 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004046 ndata = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004047 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4048 (ctxt->sax->unparsedEntityDecl != NULL))
4049 ctxt->sax->unparsedEntityDecl(ctxt->userData, name,
4050 literal, URI, ndata);
4051 } else {
4052 if ((ctxt->sax != NULL) &&
4053 (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
4054 ctxt->sax->entityDecl(ctxt->userData, name,
4055 XML_EXTERNAL_GENERAL_PARSED_ENTITY,
4056 literal, URI, NULL);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004057 /*
4058 * For expat compatibility in SAX mode.
4059 * assuming the entity repalcement was asked for
4060 */
4061 if ((ctxt->replaceEntities != 0) &&
4062 ((ctxt->myDoc == NULL) ||
4063 (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) {
4064 if (ctxt->myDoc == NULL) {
4065 ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
4066 }
4067
4068 if (ctxt->myDoc->intSubset == NULL)
4069 ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
4070 BAD_CAST "fake", NULL, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00004071 xmlSAX2EntityDecl(ctxt, name,
4072 XML_EXTERNAL_GENERAL_PARSED_ENTITY,
4073 literal, URI, NULL);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004074 }
Owen Taylor3473f882001-02-23 17:55:21 +00004075 }
4076 }
4077 }
4078 SKIP_BLANKS;
4079 if (RAW != '>') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00004080 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED,
Owen Taylor3473f882001-02-23 17:55:21 +00004081 "xmlParseEntityDecl: entity %s not terminated\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00004082 } else {
4083 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004084 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
4085 "Entity declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004086 }
4087 NEXT;
4088 }
4089 if (orig != NULL) {
4090 /*
4091 * Ugly mechanism to save the raw entity value.
4092 */
4093 xmlEntityPtr cur = NULL;
4094
4095 if (isParameter) {
4096 if ((ctxt->sax != NULL) &&
4097 (ctxt->sax->getParameterEntity != NULL))
4098 cur = ctxt->sax->getParameterEntity(ctxt->userData, name);
4099 } else {
4100 if ((ctxt->sax != NULL) &&
4101 (ctxt->sax->getEntity != NULL))
4102 cur = ctxt->sax->getEntity(ctxt->userData, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004103 if ((cur == NULL) && (ctxt->userData==ctxt)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00004104 cur = xmlSAX2GetEntity(ctxt, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004105 }
Owen Taylor3473f882001-02-23 17:55:21 +00004106 }
4107 if (cur != NULL) {
4108 if (cur->orig != NULL)
4109 xmlFree(orig);
4110 else
4111 cur->orig = orig;
4112 } else
4113 xmlFree(orig);
4114 }
Owen Taylor3473f882001-02-23 17:55:21 +00004115 if (value != NULL) xmlFree(value);
4116 if (URI != NULL) xmlFree(URI);
4117 if (literal != NULL) xmlFree(literal);
Owen Taylor3473f882001-02-23 17:55:21 +00004118 }
4119}
4120
4121/**
4122 * xmlParseDefaultDecl:
4123 * @ctxt: an XML parser context
4124 * @value: Receive a possible fixed default value for the attribute
4125 *
4126 * Parse an attribute default declaration
4127 *
4128 * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue)
4129 *
4130 * [ VC: Required Attribute ]
4131 * if the default declaration is the keyword #REQUIRED, then the
4132 * attribute must be specified for all elements of the type in the
4133 * attribute-list declaration.
4134 *
4135 * [ VC: Attribute Default Legal ]
4136 * The declared default value must meet the lexical constraints of
4137 * the declared attribute type c.f. xmlValidateAttributeDecl()
4138 *
4139 * [ VC: Fixed Attribute Default ]
4140 * if an attribute has a default value declared with the #FIXED
4141 * keyword, instances of that attribute must match the default value.
4142 *
4143 * [ WFC: No < in Attribute Values ]
4144 * handled in xmlParseAttValue()
4145 *
4146 * returns: XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED
4147 * or XML_ATTRIBUTE_FIXED.
4148 */
4149
4150int
4151xmlParseDefaultDecl(xmlParserCtxtPtr ctxt, xmlChar **value) {
4152 int val;
4153 xmlChar *ret;
4154
4155 *value = NULL;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004156 if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004157 SKIP(9);
4158 return(XML_ATTRIBUTE_REQUIRED);
4159 }
Daniel Veillarda07050d2003-10-19 14:46:32 +00004160 if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004161 SKIP(8);
4162 return(XML_ATTRIBUTE_IMPLIED);
4163 }
4164 val = XML_ATTRIBUTE_NONE;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004165 if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004166 SKIP(6);
4167 val = XML_ATTRIBUTE_FIXED;
William M. Brack76e95df2003-10-18 16:20:14 +00004168 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004169 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4170 "Space required after '#FIXED'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004171 }
4172 SKIP_BLANKS;
4173 }
4174 ret = xmlParseAttValue(ctxt);
4175 ctxt->instate = XML_PARSER_DTD;
4176 if (ret == NULL) {
William M. Brack7b9154b2003-09-27 19:23:50 +00004177 xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004178 "Attribute default value declaration error\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004179 } else
4180 *value = ret;
4181 return(val);
4182}
4183
4184/**
4185 * xmlParseNotationType:
4186 * @ctxt: an XML parser context
4187 *
4188 * parse an Notation attribute type.
4189 *
4190 * Note: the leading 'NOTATION' S part has already being parsed...
4191 *
4192 * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
4193 *
4194 * [ VC: Notation Attributes ]
4195 * Values of this type must match one of the notation names included
4196 * in the declaration; all notation names in the declaration must be declared.
4197 *
4198 * Returns: the notation attribute tree built while parsing
4199 */
4200
4201xmlEnumerationPtr
4202xmlParseNotationType(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004203 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00004204 xmlEnumerationPtr ret = NULL, last = NULL, cur;
4205
4206 if (RAW != '(') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004207 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004208 return(NULL);
4209 }
4210 SHRINK;
4211 do {
4212 NEXT;
4213 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004214 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004215 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004216 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4217 "Name expected in NOTATION declaration\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004218 return(ret);
4219 }
4220 cur = xmlCreateEnumeration(name);
Owen Taylor3473f882001-02-23 17:55:21 +00004221 if (cur == NULL) return(ret);
4222 if (last == NULL) ret = last = cur;
4223 else {
4224 last->next = cur;
4225 last = cur;
4226 }
4227 SKIP_BLANKS;
4228 } while (RAW == '|');
4229 if (RAW != ')') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004230 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004231 if ((last != NULL) && (last != ret))
4232 xmlFreeEnumeration(last);
4233 return(ret);
4234 }
4235 NEXT;
4236 return(ret);
4237}
4238
4239/**
4240 * xmlParseEnumerationType:
4241 * @ctxt: an XML parser context
4242 *
4243 * parse an Enumeration attribute type.
4244 *
4245 * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')'
4246 *
4247 * [ VC: Enumeration ]
4248 * Values of this type must match one of the Nmtoken tokens in
4249 * the declaration
4250 *
4251 * Returns: the enumeration attribute tree built while parsing
4252 */
4253
4254xmlEnumerationPtr
4255xmlParseEnumerationType(xmlParserCtxtPtr ctxt) {
4256 xmlChar *name;
4257 xmlEnumerationPtr ret = NULL, last = NULL, cur;
4258
4259 if (RAW != '(') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004260 xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004261 return(NULL);
4262 }
4263 SHRINK;
4264 do {
4265 NEXT;
4266 SKIP_BLANKS;
4267 name = xmlParseNmtoken(ctxt);
4268 if (name == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004269 xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004270 return(ret);
4271 }
4272 cur = xmlCreateEnumeration(name);
4273 xmlFree(name);
4274 if (cur == NULL) return(ret);
4275 if (last == NULL) ret = last = cur;
4276 else {
4277 last->next = cur;
4278 last = cur;
4279 }
4280 SKIP_BLANKS;
4281 } while (RAW == '|');
4282 if (RAW != ')') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004283 xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004284 return(ret);
4285 }
4286 NEXT;
4287 return(ret);
4288}
4289
4290/**
4291 * xmlParseEnumeratedType:
4292 * @ctxt: an XML parser context
4293 * @tree: the enumeration tree built while parsing
4294 *
4295 * parse an Enumerated attribute type.
4296 *
4297 * [57] EnumeratedType ::= NotationType | Enumeration
4298 *
4299 * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
4300 *
4301 *
4302 * Returns: XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION
4303 */
4304
4305int
4306xmlParseEnumeratedType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) {
Daniel Veillarda07050d2003-10-19 14:46:32 +00004307 if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004308 SKIP(8);
William M. Brack76e95df2003-10-18 16:20:14 +00004309 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004310 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4311 "Space required after 'NOTATION'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004312 return(0);
4313 }
4314 SKIP_BLANKS;
4315 *tree = xmlParseNotationType(ctxt);
4316 if (*tree == NULL) return(0);
4317 return(XML_ATTRIBUTE_NOTATION);
4318 }
4319 *tree = xmlParseEnumerationType(ctxt);
4320 if (*tree == NULL) return(0);
4321 return(XML_ATTRIBUTE_ENUMERATION);
4322}
4323
4324/**
4325 * xmlParseAttributeType:
4326 * @ctxt: an XML parser context
4327 * @tree: the enumeration tree built while parsing
4328 *
4329 * parse the Attribute list def for an element
4330 *
4331 * [54] AttType ::= StringType | TokenizedType | EnumeratedType
4332 *
4333 * [55] StringType ::= 'CDATA'
4334 *
4335 * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' |
4336 * 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS'
4337 *
4338 * Validity constraints for attribute values syntax are checked in
4339 * xmlValidateAttributeValue()
4340 *
4341 * [ VC: ID ]
4342 * Values of type ID must match the Name production. A name must not
4343 * appear more than once in an XML document as a value of this type;
4344 * i.e., ID values must uniquely identify the elements which bear them.
4345 *
4346 * [ VC: One ID per Element Type ]
4347 * No element type may have more than one ID attribute specified.
4348 *
4349 * [ VC: ID Attribute Default ]
4350 * An ID attribute must have a declared default of #IMPLIED or #REQUIRED.
4351 *
4352 * [ VC: IDREF ]
4353 * Values of type IDREF must match the Name production, and values
4354 * of type IDREFS must match Names; each IDREF Name must match the value
4355 * of an ID attribute on some element in the XML document; i.e. IDREF
4356 * values must match the value of some ID attribute.
4357 *
4358 * [ VC: Entity Name ]
4359 * Values of type ENTITY must match the Name production, values
4360 * of type ENTITIES must match Names; each Entity Name must match the
4361 * name of an unparsed entity declared in the DTD.
4362 *
4363 * [ VC: Name Token ]
4364 * Values of type NMTOKEN must match the Nmtoken production; values
4365 * of type NMTOKENS must match Nmtokens.
4366 *
4367 * Returns the attribute type
4368 */
4369int
4370xmlParseAttributeType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) {
4371 SHRINK;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004372 if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004373 SKIP(5);
4374 return(XML_ATTRIBUTE_CDATA);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004375 } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004376 SKIP(6);
4377 return(XML_ATTRIBUTE_IDREFS);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004378 } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004379 SKIP(5);
4380 return(XML_ATTRIBUTE_IDREF);
4381 } else if ((RAW == 'I') && (NXT(1) == 'D')) {
4382 SKIP(2);
4383 return(XML_ATTRIBUTE_ID);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004384 } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004385 SKIP(6);
4386 return(XML_ATTRIBUTE_ENTITY);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004387 } else if (CMP8(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'I', 'E', 'S')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004388 SKIP(8);
4389 return(XML_ATTRIBUTE_ENTITIES);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004390 } else if (CMP8(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N', 'S')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004391 SKIP(8);
4392 return(XML_ATTRIBUTE_NMTOKENS);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004393 } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004394 SKIP(7);
4395 return(XML_ATTRIBUTE_NMTOKEN);
4396 }
4397 return(xmlParseEnumeratedType(ctxt, tree));
4398}
4399
4400/**
4401 * xmlParseAttributeListDecl:
4402 * @ctxt: an XML parser context
4403 *
4404 * : parse the Attribute list def for an element
4405 *
4406 * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>'
4407 *
4408 * [53] AttDef ::= S Name S AttType S DefaultDecl
4409 *
4410 */
4411void
4412xmlParseAttributeListDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004413 const xmlChar *elemName;
4414 const xmlChar *attrName;
Owen Taylor3473f882001-02-23 17:55:21 +00004415 xmlEnumerationPtr tree;
4416
Daniel Veillarda07050d2003-10-19 14:46:32 +00004417 if (CMP9(CUR_PTR, '<', '!', 'A', 'T', 'T', 'L', 'I', 'S', 'T')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004418 xmlParserInputPtr input = ctxt->input;
4419
4420 SKIP(9);
William M. Brack76e95df2003-10-18 16:20:14 +00004421 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004422 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004423 "Space required after '<!ATTLIST'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004424 }
4425 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004426 elemName = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004427 if (elemName == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004428 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4429 "ATTLIST: no name for Element\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004430 return;
4431 }
4432 SKIP_BLANKS;
4433 GROW;
4434 while (RAW != '>') {
4435 const xmlChar *check = CUR_PTR;
4436 int type;
4437 int def;
4438 xmlChar *defaultValue = NULL;
4439
4440 GROW;
4441 tree = NULL;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004442 attrName = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004443 if (attrName == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004444 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4445 "ATTLIST: no name for Attribute\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004446 break;
4447 }
4448 GROW;
William M. Brack76e95df2003-10-18 16:20:14 +00004449 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004450 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004451 "Space required after the attribute name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004452 if (defaultValue != NULL)
4453 xmlFree(defaultValue);
4454 break;
4455 }
4456 SKIP_BLANKS;
4457
4458 type = xmlParseAttributeType(ctxt, &tree);
4459 if (type <= 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00004460 if (defaultValue != NULL)
4461 xmlFree(defaultValue);
4462 break;
4463 }
4464
4465 GROW;
William M. Brack76e95df2003-10-18 16:20:14 +00004466 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004467 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4468 "Space required after the attribute type\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004469 if (defaultValue != NULL)
4470 xmlFree(defaultValue);
4471 if (tree != NULL)
4472 xmlFreeEnumeration(tree);
4473 break;
4474 }
4475 SKIP_BLANKS;
4476
4477 def = xmlParseDefaultDecl(ctxt, &defaultValue);
4478 if (def <= 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00004479 if (defaultValue != NULL)
4480 xmlFree(defaultValue);
4481 if (tree != NULL)
4482 xmlFreeEnumeration(tree);
4483 break;
4484 }
4485
4486 GROW;
4487 if (RAW != '>') {
William M. Brack76e95df2003-10-18 16:20:14 +00004488 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004489 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004490 "Space required after the attribute default value\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004491 if (defaultValue != NULL)
4492 xmlFree(defaultValue);
4493 if (tree != NULL)
4494 xmlFreeEnumeration(tree);
4495 break;
4496 }
4497 SKIP_BLANKS;
4498 }
4499 if (check == CUR_PTR) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004500 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
4501 "in xmlParseAttributeListDecl\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004502 if (defaultValue != NULL)
4503 xmlFree(defaultValue);
4504 if (tree != NULL)
4505 xmlFreeEnumeration(tree);
4506 break;
4507 }
4508 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4509 (ctxt->sax->attributeDecl != NULL))
4510 ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName,
4511 type, def, defaultValue, tree);
Daniel Veillarde57ec792003-09-10 10:50:59 +00004512 else if (tree != NULL)
4513 xmlFreeEnumeration(tree);
4514
4515 if ((ctxt->sax2) && (defaultValue != NULL) &&
4516 (def != XML_ATTRIBUTE_IMPLIED) &&
4517 (def != XML_ATTRIBUTE_REQUIRED)) {
4518 xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue);
4519 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00004520 if ((ctxt->sax2) && (type != XML_ATTRIBUTE_CDATA)) {
4521 xmlAddSpecialAttr(ctxt, elemName, attrName, type);
4522 }
Owen Taylor3473f882001-02-23 17:55:21 +00004523 if (defaultValue != NULL)
4524 xmlFree(defaultValue);
4525 GROW;
4526 }
4527 if (RAW == '>') {
4528 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004529 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
4530 "Attribute list declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004531 }
4532 NEXT;
4533 }
Owen Taylor3473f882001-02-23 17:55:21 +00004534 }
4535}
4536
4537/**
4538 * xmlParseElementMixedContentDecl:
4539 * @ctxt: an XML parser context
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004540 * @inputchk: the input used for the current entity, needed for boundary checks
Owen Taylor3473f882001-02-23 17:55:21 +00004541 *
4542 * parse the declaration for a Mixed Element content
4543 * The leading '(' and spaces have been skipped in xmlParseElementContentDecl
4544 *
4545 * [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' |
4546 * '(' S? '#PCDATA' S? ')'
4547 *
4548 * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49])
4549 *
4550 * [ VC: No Duplicate Types ]
4551 * The same name must not appear more than once in a single
4552 * mixed-content declaration.
4553 *
4554 * returns: the list of the xmlElementContentPtr describing the element choices
4555 */
4556xmlElementContentPtr
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004557xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) {
Owen Taylor3473f882001-02-23 17:55:21 +00004558 xmlElementContentPtr ret = NULL, cur = NULL, n;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004559 const xmlChar *elem = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00004560
4561 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004562 if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004563 SKIP(7);
4564 SKIP_BLANKS;
4565 SHRINK;
4566 if (RAW == ')') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004567 if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00004568 xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
4569"Element content declaration doesn't start and stop in the same entity\n",
4570 NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004571 }
Owen Taylor3473f882001-02-23 17:55:21 +00004572 NEXT;
4573 ret = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_PCDATA);
4574 if (RAW == '*') {
4575 ret->ocur = XML_ELEMENT_CONTENT_MULT;
4576 NEXT;
4577 }
4578 return(ret);
4579 }
4580 if ((RAW == '(') || (RAW == '|')) {
4581 ret = cur = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_PCDATA);
4582 if (ret == NULL) return(NULL);
4583 }
4584 while (RAW == '|') {
4585 NEXT;
4586 if (elem == NULL) {
4587 ret = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR);
4588 if (ret == NULL) return(NULL);
4589 ret->c1 = cur;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004590 if (cur != NULL)
4591 cur->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +00004592 cur = ret;
4593 } else {
4594 n = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR);
4595 if (n == NULL) return(NULL);
4596 n->c1 = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004597 if (n->c1 != NULL)
4598 n->c1->parent = n;
Owen Taylor3473f882001-02-23 17:55:21 +00004599 cur->c2 = n;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004600 if (n != NULL)
4601 n->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004602 cur = n;
Owen Taylor3473f882001-02-23 17:55:21 +00004603 }
4604 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004605 elem = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004606 if (elem == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004607 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004608 "xmlParseElementMixedContentDecl : Name expected\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004609 xmlFreeElementContent(cur);
4610 return(NULL);
4611 }
4612 SKIP_BLANKS;
4613 GROW;
4614 }
4615 if ((RAW == ')') && (NXT(1) == '*')) {
4616 if (elem != NULL) {
4617 cur->c2 = xmlNewElementContent(elem,
4618 XML_ELEMENT_CONTENT_ELEMENT);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004619 if (cur->c2 != NULL)
4620 cur->c2->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004621 }
4622 ret->ocur = XML_ELEMENT_CONTENT_MULT;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004623 if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00004624 xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
4625"Element content declaration doesn't start and stop in the same entity\n",
4626 NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004627 }
Owen Taylor3473f882001-02-23 17:55:21 +00004628 SKIP(2);
4629 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00004630 xmlFreeElementContent(ret);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004631 xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004632 return(NULL);
4633 }
4634
4635 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004636 xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004637 }
4638 return(ret);
4639}
4640
4641/**
4642 * xmlParseElementChildrenContentDecl:
4643 * @ctxt: an XML parser context
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004644 * @inputchk: the input used for the current entity, needed for boundary checks
Owen Taylor3473f882001-02-23 17:55:21 +00004645 *
4646 * parse the declaration for a Mixed Element content
4647 * The leading '(' and spaces have been skipped in xmlParseElementContentDecl
4648 *
4649 *
4650 * [47] children ::= (choice | seq) ('?' | '*' | '+')?
4651 *
4652 * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')?
4653 *
4654 * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')'
4655 *
4656 * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')'
4657 *
4658 * [ VC: Proper Group/PE Nesting ] applies to [49] and [50]
4659 * TODO Parameter-entity replacement text must be properly nested
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004660 * with parenthesized groups. That is to say, if either of the
Owen Taylor3473f882001-02-23 17:55:21 +00004661 * opening or closing parentheses in a choice, seq, or Mixed
4662 * construct is contained in the replacement text for a parameter
4663 * entity, both must be contained in the same replacement text. For
4664 * interoperability, if a parameter-entity reference appears in a
4665 * choice, seq, or Mixed construct, its replacement text should not
4666 * be empty, and neither the first nor last non-blank character of
4667 * the replacement text should be a connector (| or ,).
4668 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00004669 * Returns the tree of xmlElementContentPtr describing the element
Owen Taylor3473f882001-02-23 17:55:21 +00004670 * hierarchy.
4671 */
4672xmlElementContentPtr
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004673xmlParseElementChildrenContentDecl (xmlParserCtxtPtr ctxt, int inputchk) {
Owen Taylor3473f882001-02-23 17:55:21 +00004674 xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004675 const xmlChar *elem;
Owen Taylor3473f882001-02-23 17:55:21 +00004676 xmlChar type = 0;
4677
4678 SKIP_BLANKS;
4679 GROW;
4680 if (RAW == '(') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004681 int inputid = ctxt->input->id;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004682
Owen Taylor3473f882001-02-23 17:55:21 +00004683 /* Recurse on first child */
4684 NEXT;
4685 SKIP_BLANKS;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004686 cur = ret = xmlParseElementChildrenContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004687 SKIP_BLANKS;
4688 GROW;
4689 } else {
Daniel Veillard76d66f42001-05-16 21:05:17 +00004690 elem = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004691 if (elem == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004692 xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004693 return(NULL);
4694 }
4695 cur = ret = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004696 if (cur == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004697 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004698 return(NULL);
4699 }
Owen Taylor3473f882001-02-23 17:55:21 +00004700 GROW;
4701 if (RAW == '?') {
4702 cur->ocur = XML_ELEMENT_CONTENT_OPT;
4703 NEXT;
4704 } else if (RAW == '*') {
4705 cur->ocur = XML_ELEMENT_CONTENT_MULT;
4706 NEXT;
4707 } else if (RAW == '+') {
4708 cur->ocur = XML_ELEMENT_CONTENT_PLUS;
4709 NEXT;
4710 } else {
4711 cur->ocur = XML_ELEMENT_CONTENT_ONCE;
4712 }
Owen Taylor3473f882001-02-23 17:55:21 +00004713 GROW;
4714 }
4715 SKIP_BLANKS;
4716 SHRINK;
4717 while (RAW != ')') {
4718 /*
4719 * Each loop we parse one separator and one element.
4720 */
4721 if (RAW == ',') {
4722 if (type == 0) type = CUR;
4723
4724 /*
4725 * Detect "Name | Name , Name" error
4726 */
4727 else if (type != CUR) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004728 xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004729 "xmlParseElementChildrenContentDecl : '%c' expected\n",
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004730 type);
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004731 if ((last != NULL) && (last != ret))
Owen Taylor3473f882001-02-23 17:55:21 +00004732 xmlFreeElementContent(last);
4733 if (ret != NULL)
4734 xmlFreeElementContent(ret);
4735 return(NULL);
4736 }
4737 NEXT;
4738
4739 op = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_SEQ);
4740 if (op == NULL) {
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004741 if ((last != NULL) && (last != ret))
4742 xmlFreeElementContent(last);
Owen Taylor3473f882001-02-23 17:55:21 +00004743 xmlFreeElementContent(ret);
4744 return(NULL);
4745 }
4746 if (last == NULL) {
4747 op->c1 = ret;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004748 if (ret != NULL)
4749 ret->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004750 ret = cur = op;
4751 } else {
4752 cur->c2 = op;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004753 if (op != NULL)
4754 op->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004755 op->c1 = last;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004756 if (last != NULL)
4757 last->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004758 cur =op;
4759 last = NULL;
4760 }
4761 } else if (RAW == '|') {
4762 if (type == 0) type = CUR;
4763
4764 /*
4765 * Detect "Name , Name | Name" error
4766 */
4767 else if (type != CUR) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004768 xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004769 "xmlParseElementChildrenContentDecl : '%c' expected\n",
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004770 type);
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004771 if ((last != NULL) && (last != ret))
Owen Taylor3473f882001-02-23 17:55:21 +00004772 xmlFreeElementContent(last);
4773 if (ret != NULL)
4774 xmlFreeElementContent(ret);
4775 return(NULL);
4776 }
4777 NEXT;
4778
4779 op = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR);
4780 if (op == NULL) {
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004781 if ((last != NULL) && (last != ret))
Owen Taylor3473f882001-02-23 17:55:21 +00004782 xmlFreeElementContent(last);
4783 if (ret != NULL)
4784 xmlFreeElementContent(ret);
4785 return(NULL);
4786 }
4787 if (last == NULL) {
4788 op->c1 = ret;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004789 if (ret != NULL)
4790 ret->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004791 ret = cur = op;
4792 } else {
4793 cur->c2 = op;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004794 if (op != NULL)
4795 op->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004796 op->c1 = last;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004797 if (last != NULL)
4798 last->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004799 cur =op;
4800 last = NULL;
4801 }
4802 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004803 xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004804 if (ret != NULL)
4805 xmlFreeElementContent(ret);
4806 return(NULL);
4807 }
4808 GROW;
4809 SKIP_BLANKS;
4810 GROW;
4811 if (RAW == '(') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004812 int inputid = ctxt->input->id;
Owen Taylor3473f882001-02-23 17:55:21 +00004813 /* Recurse on second child */
4814 NEXT;
4815 SKIP_BLANKS;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004816 last = xmlParseElementChildrenContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004817 SKIP_BLANKS;
4818 } else {
Daniel Veillard76d66f42001-05-16 21:05:17 +00004819 elem = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004820 if (elem == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004821 xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004822 if (ret != NULL)
4823 xmlFreeElementContent(ret);
4824 return(NULL);
4825 }
4826 last = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT);
Owen Taylor3473f882001-02-23 17:55:21 +00004827 if (RAW == '?') {
4828 last->ocur = XML_ELEMENT_CONTENT_OPT;
4829 NEXT;
4830 } else if (RAW == '*') {
4831 last->ocur = XML_ELEMENT_CONTENT_MULT;
4832 NEXT;
4833 } else if (RAW == '+') {
4834 last->ocur = XML_ELEMENT_CONTENT_PLUS;
4835 NEXT;
4836 } else {
4837 last->ocur = XML_ELEMENT_CONTENT_ONCE;
4838 }
4839 }
4840 SKIP_BLANKS;
4841 GROW;
4842 }
4843 if ((cur != NULL) && (last != NULL)) {
4844 cur->c2 = last;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004845 if (last != NULL)
4846 last->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004847 }
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004848 if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00004849 xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
4850"Element content declaration doesn't start and stop in the same entity\n",
4851 NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004852 }
Owen Taylor3473f882001-02-23 17:55:21 +00004853 NEXT;
4854 if (RAW == '?') {
William M. Brackf8f2e8f2004-05-14 04:37:41 +00004855 if (ret != NULL) {
4856 if ((ret->ocur == XML_ELEMENT_CONTENT_PLUS) ||
4857 (ret->ocur == XML_ELEMENT_CONTENT_MULT))
4858 ret->ocur = XML_ELEMENT_CONTENT_MULT;
4859 else
4860 ret->ocur = XML_ELEMENT_CONTENT_OPT;
4861 }
Owen Taylor3473f882001-02-23 17:55:21 +00004862 NEXT;
4863 } else if (RAW == '*') {
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004864 if (ret != NULL) {
Daniel Veillarde470df72001-04-18 21:41:07 +00004865 ret->ocur = XML_ELEMENT_CONTENT_MULT;
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004866 cur = ret;
4867 /*
4868 * Some normalization:
4869 * (a | b* | c?)* == (a | b | c)*
4870 */
4871 while (cur->type == XML_ELEMENT_CONTENT_OR) {
4872 if ((cur->c1 != NULL) &&
4873 ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4874 (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT)))
4875 cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
4876 if ((cur->c2 != NULL) &&
4877 ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
4878 (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT)))
4879 cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
4880 cur = cur->c2;
4881 }
4882 }
Owen Taylor3473f882001-02-23 17:55:21 +00004883 NEXT;
4884 } else if (RAW == '+') {
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004885 if (ret != NULL) {
4886 int found = 0;
4887
William M. Brackf8f2e8f2004-05-14 04:37:41 +00004888 if ((ret->ocur == XML_ELEMENT_CONTENT_OPT) ||
4889 (ret->ocur == XML_ELEMENT_CONTENT_MULT))
4890 ret->ocur = XML_ELEMENT_CONTENT_MULT;
William M. Brackeb8509c2004-05-14 03:48:02 +00004891 else
4892 ret->ocur = XML_ELEMENT_CONTENT_PLUS;
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004893 /*
4894 * Some normalization:
4895 * (a | b*)+ == (a | b)*
4896 * (a | b?)+ == (a | b)*
4897 */
4898 while (cur->type == XML_ELEMENT_CONTENT_OR) {
4899 if ((cur->c1 != NULL) &&
4900 ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4901 (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4902 cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
4903 found = 1;
4904 }
4905 if ((cur->c2 != NULL) &&
4906 ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
4907 (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) {
4908 cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
4909 found = 1;
4910 }
4911 cur = cur->c2;
4912 }
4913 if (found)
4914 ret->ocur = XML_ELEMENT_CONTENT_MULT;
4915 }
Owen Taylor3473f882001-02-23 17:55:21 +00004916 NEXT;
4917 }
4918 return(ret);
4919}
4920
4921/**
4922 * xmlParseElementContentDecl:
4923 * @ctxt: an XML parser context
4924 * @name: the name of the element being defined.
4925 * @result: the Element Content pointer will be stored here if any
4926 *
4927 * parse the declaration for an Element content either Mixed or Children,
4928 * the cases EMPTY and ANY are handled directly in xmlParseElementDecl
4929 *
4930 * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children
4931 *
4932 * returns: the type of element content XML_ELEMENT_TYPE_xxx
4933 */
4934
4935int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004936xmlParseElementContentDecl(xmlParserCtxtPtr ctxt, const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00004937 xmlElementContentPtr *result) {
4938
4939 xmlElementContentPtr tree = NULL;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004940 int inputid = ctxt->input->id;
Owen Taylor3473f882001-02-23 17:55:21 +00004941 int res;
4942
4943 *result = NULL;
4944
4945 if (RAW != '(') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00004946 xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004947 "xmlParseElementContentDecl : %s '(' expected\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00004948 return(-1);
4949 }
4950 NEXT;
4951 GROW;
4952 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004953 if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004954 tree = xmlParseElementMixedContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004955 res = XML_ELEMENT_TYPE_MIXED;
4956 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004957 tree = xmlParseElementChildrenContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004958 res = XML_ELEMENT_TYPE_ELEMENT;
4959 }
Owen Taylor3473f882001-02-23 17:55:21 +00004960 SKIP_BLANKS;
4961 *result = tree;
4962 return(res);
4963}
4964
4965/**
4966 * xmlParseElementDecl:
4967 * @ctxt: an XML parser context
4968 *
4969 * parse an Element declaration.
4970 *
4971 * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>'
4972 *
4973 * [ VC: Unique Element Type Declaration ]
4974 * No element type may be declared more than once
4975 *
4976 * Returns the type of the element, or -1 in case of error
4977 */
4978int
4979xmlParseElementDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004980 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00004981 int ret = -1;
4982 xmlElementContentPtr content = NULL;
4983
4984 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004985 if (CMP9(CUR_PTR, '<', '!', 'E', 'L', 'E', 'M', 'E', 'N', 'T')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004986 xmlParserInputPtr input = ctxt->input;
4987
4988 SKIP(9);
William M. Brack76e95df2003-10-18 16:20:14 +00004989 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004990 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4991 "Space required after 'ELEMENT'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004992 }
4993 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004994 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004995 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004996 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4997 "xmlParseElementDecl: no name for Element\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004998 return(-1);
4999 }
5000 while ((RAW == 0) && (ctxt->inputNr > 1))
5001 xmlPopInput(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00005002 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005003 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5004 "Space required after the element name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005005 }
5006 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00005007 if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005008 SKIP(5);
5009 /*
5010 * Element must always be empty.
5011 */
5012 ret = XML_ELEMENT_TYPE_EMPTY;
5013 } else if ((RAW == 'A') && (NXT(1) == 'N') &&
5014 (NXT(2) == 'Y')) {
5015 SKIP(3);
5016 /*
5017 * Element is a generic container.
5018 */
5019 ret = XML_ELEMENT_TYPE_ANY;
5020 } else if (RAW == '(') {
5021 ret = xmlParseElementContentDecl(ctxt, name, &content);
5022 } else {
5023 /*
5024 * [ WFC: PEs in Internal Subset ] error handling.
5025 */
5026 if ((RAW == '%') && (ctxt->external == 0) &&
5027 (ctxt->inputNr == 1)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005028 xmlFatalErrMsg(ctxt, XML_ERR_PEREF_IN_INT_SUBSET,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005029 "PEReference: forbidden within markup decl in internal subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005030 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00005031 xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
Owen Taylor3473f882001-02-23 17:55:21 +00005032 "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n");
5033 }
Owen Taylor3473f882001-02-23 17:55:21 +00005034 return(-1);
5035 }
5036
5037 SKIP_BLANKS;
5038 /*
5039 * Pop-up of finished entities.
5040 */
5041 while ((RAW == 0) && (ctxt->inputNr > 1))
5042 xmlPopInput(ctxt);
5043 SKIP_BLANKS;
5044
5045 if (RAW != '>') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005046 xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005047 } else {
5048 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005049 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
5050 "Element declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005051 }
5052
5053 NEXT;
5054 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
5055 (ctxt->sax->elementDecl != NULL))
5056 ctxt->sax->elementDecl(ctxt->userData, name, ret,
5057 content);
5058 }
5059 if (content != NULL) {
5060 xmlFreeElementContent(content);
5061 }
Owen Taylor3473f882001-02-23 17:55:21 +00005062 }
5063 return(ret);
5064}
5065
5066/**
Owen Taylor3473f882001-02-23 17:55:21 +00005067 * xmlParseConditionalSections
5068 * @ctxt: an XML parser context
5069 *
5070 * [61] conditionalSect ::= includeSect | ignoreSect
5071 * [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>'
5072 * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>'
5073 * [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)*
5074 * [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*)
5075 */
5076
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005077static void
Owen Taylor3473f882001-02-23 17:55:21 +00005078xmlParseConditionalSections(xmlParserCtxtPtr ctxt) {
5079 SKIP(3);
5080 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00005081 if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005082 SKIP(7);
5083 SKIP_BLANKS;
5084 if (RAW != '[') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005085 xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005086 } else {
5087 NEXT;
5088 }
5089 if (xmlParserDebugEntities) {
5090 if ((ctxt->input != NULL) && (ctxt->input->filename))
5091 xmlGenericError(xmlGenericErrorContext,
5092 "%s(%d): ", ctxt->input->filename,
5093 ctxt->input->line);
5094 xmlGenericError(xmlGenericErrorContext,
5095 "Entering INCLUDE Conditional Section\n");
5096 }
5097
5098 while ((RAW != 0) && ((RAW != ']') || (NXT(1) != ']') ||
5099 (NXT(2) != '>'))) {
5100 const xmlChar *check = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00005101 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00005102
5103 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5104 xmlParseConditionalSections(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00005105 } else if (IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005106 NEXT;
5107 } else if (RAW == '%') {
5108 xmlParsePEReference(ctxt);
5109 } else
5110 xmlParseMarkupDecl(ctxt);
5111
5112 /*
5113 * Pop-up of finished entities.
5114 */
5115 while ((RAW == 0) && (ctxt->inputNr > 1))
5116 xmlPopInput(ctxt);
5117
Daniel Veillardfdc91562002-07-01 21:52:03 +00005118 if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005119 xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005120 break;
5121 }
5122 }
5123 if (xmlParserDebugEntities) {
5124 if ((ctxt->input != NULL) && (ctxt->input->filename))
5125 xmlGenericError(xmlGenericErrorContext,
5126 "%s(%d): ", ctxt->input->filename,
5127 ctxt->input->line);
5128 xmlGenericError(xmlGenericErrorContext,
5129 "Leaving INCLUDE Conditional Section\n");
5130 }
5131
Daniel Veillarda07050d2003-10-19 14:46:32 +00005132 } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005133 int state;
William M. Brack78637da2003-07-31 14:47:38 +00005134 xmlParserInputState instate;
Owen Taylor3473f882001-02-23 17:55:21 +00005135 int depth = 0;
5136
5137 SKIP(6);
5138 SKIP_BLANKS;
5139 if (RAW != '[') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005140 xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005141 } else {
5142 NEXT;
5143 }
5144 if (xmlParserDebugEntities) {
5145 if ((ctxt->input != NULL) && (ctxt->input->filename))
5146 xmlGenericError(xmlGenericErrorContext,
5147 "%s(%d): ", ctxt->input->filename,
5148 ctxt->input->line);
5149 xmlGenericError(xmlGenericErrorContext,
5150 "Entering IGNORE Conditional Section\n");
5151 }
5152
5153 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005154 * Parse up to the end of the conditional section
Owen Taylor3473f882001-02-23 17:55:21 +00005155 * But disable SAX event generating DTD building in the meantime
5156 */
5157 state = ctxt->disableSAX;
5158 instate = ctxt->instate;
Daniel Veillarddad3f682002-11-17 16:47:27 +00005159 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00005160 ctxt->instate = XML_PARSER_IGNORE;
5161
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00005162 while ((depth >= 0) && (RAW != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005163 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5164 depth++;
5165 SKIP(3);
5166 continue;
5167 }
5168 if ((RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) {
5169 if (--depth >= 0) SKIP(3);
5170 continue;
5171 }
5172 NEXT;
5173 continue;
5174 }
5175
5176 ctxt->disableSAX = state;
5177 ctxt->instate = instate;
5178
5179 if (xmlParserDebugEntities) {
5180 if ((ctxt->input != NULL) && (ctxt->input->filename))
5181 xmlGenericError(xmlGenericErrorContext,
5182 "%s(%d): ", ctxt->input->filename,
5183 ctxt->input->line);
5184 xmlGenericError(xmlGenericErrorContext,
5185 "Leaving IGNORE Conditional Section\n");
5186 }
5187
5188 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005189 xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005190 }
5191
5192 if (RAW == 0)
5193 SHRINK;
5194
5195 if (RAW == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005196 xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005197 } else {
5198 SKIP(3);
5199 }
5200}
5201
5202/**
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005203 * xmlParseMarkupDecl:
5204 * @ctxt: an XML parser context
5205 *
5206 * parse Markup declarations
5207 *
5208 * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl |
5209 * NotationDecl | PI | Comment
5210 *
5211 * [ VC: Proper Declaration/PE Nesting ]
5212 * Parameter-entity replacement text must be properly nested with
5213 * markup declarations. That is to say, if either the first character
5214 * or the last character of a markup declaration (markupdecl above) is
5215 * contained in the replacement text for a parameter-entity reference,
5216 * both must be contained in the same replacement text.
5217 *
5218 * [ WFC: PEs in Internal Subset ]
5219 * In the internal DTD subset, parameter-entity references can occur
5220 * only where markup declarations can occur, not within markup declarations.
5221 * (This does not apply to references that occur in external parameter
5222 * entities or to the external subset.)
5223 */
5224void
5225xmlParseMarkupDecl(xmlParserCtxtPtr ctxt) {
5226 GROW;
5227 xmlParseElementDecl(ctxt);
5228 xmlParseAttributeListDecl(ctxt);
5229 xmlParseEntityDecl(ctxt);
5230 xmlParseNotationDecl(ctxt);
5231 xmlParsePI(ctxt);
5232 xmlParseComment(ctxt);
5233 /*
5234 * This is only for internal subset. On external entities,
5235 * the replacement is done before parsing stage
5236 */
5237 if ((ctxt->external == 0) && (ctxt->inputNr == 1))
5238 xmlParsePEReference(ctxt);
5239
5240 /*
5241 * Conditional sections are allowed from entities included
5242 * by PE References in the internal subset.
5243 */
5244 if ((ctxt->external == 0) && (ctxt->inputNr > 1)) {
5245 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5246 xmlParseConditionalSections(ctxt);
5247 }
5248 }
5249
5250 ctxt->instate = XML_PARSER_DTD;
5251}
5252
5253/**
5254 * xmlParseTextDecl:
5255 * @ctxt: an XML parser context
5256 *
5257 * parse an XML declaration header for external entities
5258 *
5259 * [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>'
5260 *
5261 * Question: Seems that EncodingDecl is mandatory ? Is that a typo ?
5262 */
5263
5264void
5265xmlParseTextDecl(xmlParserCtxtPtr ctxt) {
5266 xmlChar *version;
Daniel Veillardf5cb3cd2003-10-28 13:58:13 +00005267 const xmlChar *encoding;
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005268
5269 /*
5270 * We know that '<?xml' is here.
5271 */
Daniel Veillarda07050d2003-10-19 14:46:32 +00005272 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005273 SKIP(5);
5274 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005275 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005276 return;
5277 }
5278
William M. Brack76e95df2003-10-18 16:20:14 +00005279 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005280 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5281 "Space needed after '<?xml'\n");
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005282 }
5283 SKIP_BLANKS;
5284
5285 /*
5286 * We may have the VersionInfo here.
5287 */
5288 version = xmlParseVersionInfo(ctxt);
5289 if (version == NULL)
5290 version = xmlCharStrdup(XML_DEFAULT_VERSION);
Daniel Veillard401c2112002-01-07 16:54:10 +00005291 else {
William M. Brack76e95df2003-10-18 16:20:14 +00005292 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005293 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5294 "Space needed here\n");
Daniel Veillard401c2112002-01-07 16:54:10 +00005295 }
5296 }
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005297 ctxt->input->version = version;
5298
5299 /*
5300 * We must have the encoding declaration
5301 */
Daniel Veillardf5cb3cd2003-10-28 13:58:13 +00005302 encoding = xmlParseEncodingDecl(ctxt);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005303 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
5304 /*
5305 * The XML REC instructs us to stop parsing right here
5306 */
5307 return;
5308 }
Daniel Veillardf5cb3cd2003-10-28 13:58:13 +00005309 if ((encoding == NULL) && (ctxt->errNo == XML_ERR_OK)) {
5310 xmlFatalErrMsg(ctxt, XML_ERR_MISSING_ENCODING,
5311 "Missing encoding in text declaration\n");
5312 }
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005313
5314 SKIP_BLANKS;
5315 if ((RAW == '?') && (NXT(1) == '>')) {
5316 SKIP(2);
5317 } else if (RAW == '>') {
5318 /* Deprecated old WD ... */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005319 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005320 NEXT;
5321 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005322 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005323 MOVETO_ENDTAG(CUR_PTR);
5324 NEXT;
5325 }
5326}
5327
5328/**
Owen Taylor3473f882001-02-23 17:55:21 +00005329 * xmlParseExternalSubset:
5330 * @ctxt: an XML parser context
5331 * @ExternalID: the external identifier
5332 * @SystemID: the system identifier (or URL)
5333 *
5334 * parse Markup declarations from an external subset
5335 *
5336 * [30] extSubset ::= textDecl? extSubsetDecl
5337 *
5338 * [31] extSubsetDecl ::= (markupdecl | conditionalSect | PEReference | S) *
5339 */
5340void
5341xmlParseExternalSubset(xmlParserCtxtPtr ctxt, const xmlChar *ExternalID,
5342 const xmlChar *SystemID) {
Daniel Veillard309f81d2003-09-23 09:02:53 +00005343 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005344 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00005345 if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005346 xmlParseTextDecl(ctxt);
5347 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
5348 /*
5349 * The XML REC instructs us to stop parsing right here
5350 */
5351 ctxt->instate = XML_PARSER_EOF;
5352 return;
5353 }
5354 }
5355 if (ctxt->myDoc == NULL) {
5356 ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
5357 }
5358 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset == NULL))
5359 xmlCreateIntSubset(ctxt->myDoc, NULL, ExternalID, SystemID);
5360
5361 ctxt->instate = XML_PARSER_DTD;
5362 ctxt->external = 1;
5363 while (((RAW == '<') && (NXT(1) == '?')) ||
5364 ((RAW == '<') && (NXT(1) == '!')) ||
William M. Brack76e95df2003-10-18 16:20:14 +00005365 (RAW == '%') || IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005366 const xmlChar *check = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00005367 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00005368
5369 GROW;
5370 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5371 xmlParseConditionalSections(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00005372 } else if (IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005373 NEXT;
5374 } else if (RAW == '%') {
5375 xmlParsePEReference(ctxt);
5376 } else
5377 xmlParseMarkupDecl(ctxt);
5378
5379 /*
5380 * Pop-up of finished entities.
5381 */
5382 while ((RAW == 0) && (ctxt->inputNr > 1))
5383 xmlPopInput(ctxt);
5384
Daniel Veillardfdc91562002-07-01 21:52:03 +00005385 if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005386 xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005387 break;
5388 }
5389 }
5390
5391 if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005392 xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005393 }
5394
5395}
5396
5397/**
5398 * xmlParseReference:
5399 * @ctxt: an XML parser context
5400 *
5401 * parse and handle entity references in content, depending on the SAX
5402 * interface, this may end-up in a call to character() if this is a
5403 * CharRef, a predefined entity, if there is no reference() callback.
5404 * or if the parser was asked to switch to that mode.
5405 *
5406 * [67] Reference ::= EntityRef | CharRef
5407 */
5408void
5409xmlParseReference(xmlParserCtxtPtr ctxt) {
5410 xmlEntityPtr ent;
5411 xmlChar *val;
5412 if (RAW != '&') return;
5413
5414 if (NXT(1) == '#') {
5415 int i = 0;
5416 xmlChar out[10];
5417 int hex = NXT(2);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005418 int value = xmlParseCharRef(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005419
5420 if (ctxt->charset != XML_CHAR_ENCODING_UTF8) {
5421 /*
5422 * So we are using non-UTF-8 buffers
5423 * Check that the char fit on 8bits, if not
5424 * generate a CharRef.
5425 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005426 if (value <= 0xFF) {
5427 out[0] = value;
Owen Taylor3473f882001-02-23 17:55:21 +00005428 out[1] = 0;
5429 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
5430 (!ctxt->disableSAX))
5431 ctxt->sax->characters(ctxt->userData, out, 1);
5432 } else {
5433 if ((hex == 'x') || (hex == 'X'))
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005434 snprintf((char *)out, sizeof(out), "#x%X", value);
Owen Taylor3473f882001-02-23 17:55:21 +00005435 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005436 snprintf((char *)out, sizeof(out), "#%d", value);
Owen Taylor3473f882001-02-23 17:55:21 +00005437 if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) &&
5438 (!ctxt->disableSAX))
5439 ctxt->sax->reference(ctxt->userData, out);
5440 }
5441 } else {
5442 /*
5443 * Just encode the value in UTF-8
5444 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005445 COPY_BUF(0 ,out, i, value);
Owen Taylor3473f882001-02-23 17:55:21 +00005446 out[i] = 0;
5447 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
5448 (!ctxt->disableSAX))
5449 ctxt->sax->characters(ctxt->userData, out, i);
5450 }
5451 } else {
5452 ent = xmlParseEntityRef(ctxt);
5453 if (ent == NULL) return;
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00005454 if (!ctxt->wellFormed)
5455 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005456 if ((ent->name != NULL) &&
5457 (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY)) {
5458 xmlNodePtr list = NULL;
Daniel Veillard7d515752003-09-26 19:12:37 +00005459 xmlParserErrors ret = XML_ERR_OK;
Owen Taylor3473f882001-02-23 17:55:21 +00005460
5461
5462 /*
5463 * The first reference to the entity trigger a parsing phase
5464 * where the ent->children is filled with the result from
5465 * the parsing.
5466 */
5467 if (ent->children == NULL) {
5468 xmlChar *value;
5469 value = ent->content;
5470
5471 /*
5472 * Check that this entity is well formed
5473 */
Daniel Veillard4aede2e2003-10-17 12:43:59 +00005474 if ((value != NULL) && (value[0] != 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +00005475 (value[1] == 0) && (value[0] == '<') &&
5476 (xmlStrEqual(ent->name, BAD_CAST "lt"))) {
5477 /*
5478 * DONE: get definite answer on this !!!
5479 * Lots of entity decls are used to declare a single
5480 * char
5481 * <!ENTITY lt "<">
5482 * Which seems to be valid since
5483 * 2.4: The ampersand character (&) and the left angle
5484 * bracket (<) may appear in their literal form only
5485 * when used ... They are also legal within the literal
5486 * entity value of an internal entity declaration;i
5487 * see "4.3.2 Well-Formed Parsed Entities".
5488 * IMHO 2.4 and 4.3.2 are directly in contradiction.
5489 * Looking at the OASIS test suite and James Clark
5490 * tests, this is broken. However the XML REC uses
5491 * it. Is the XML REC not well-formed ????
5492 * This is a hack to avoid this problem
5493 *
5494 * ANSWER: since lt gt amp .. are already defined,
5495 * this is a redefinition and hence the fact that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005496 * content is not well balanced is not a Wf error, this
Owen Taylor3473f882001-02-23 17:55:21 +00005497 * is lousy but acceptable.
5498 */
5499 list = xmlNewDocText(ctxt->myDoc, value);
5500 if (list != NULL) {
5501 if ((ent->etype == XML_INTERNAL_GENERAL_ENTITY) &&
5502 (ent->children == NULL)) {
5503 ent->children = list;
5504 ent->last = list;
Daniel Veillard2d84a892002-12-30 00:01:08 +00005505 ent->owner = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00005506 list->parent = (xmlNodePtr) ent;
5507 } else {
5508 xmlFreeNodeList(list);
5509 }
5510 } else if (list != NULL) {
5511 xmlFreeNodeList(list);
5512 }
5513 } else {
5514 /*
5515 * 4.3.2: An internal general parsed entity is well-formed
5516 * if its replacement text matches the production labeled
5517 * content.
5518 */
Daniel Veillardb5a60ec2002-03-18 11:45:56 +00005519
5520 void *user_data;
5521 /*
5522 * This is a bit hackish but this seems the best
5523 * way to make sure both SAX and DOM entity support
5524 * behaves okay.
5525 */
5526 if (ctxt->userData == ctxt)
5527 user_data = NULL;
5528 else
5529 user_data = ctxt->userData;
5530
Owen Taylor3473f882001-02-23 17:55:21 +00005531 if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) {
5532 ctxt->depth++;
Daniel Veillard328f48c2002-11-15 15:24:34 +00005533 ret = xmlParseBalancedChunkMemoryInternal(ctxt,
5534 value, user_data, &list);
Owen Taylor3473f882001-02-23 17:55:21 +00005535 ctxt->depth--;
5536 } else if (ent->etype ==
5537 XML_EXTERNAL_GENERAL_PARSED_ENTITY) {
5538 ctxt->depth++;
Daniel Veillarda97a19b2001-05-20 13:19:52 +00005539 ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt,
Daniel Veillardb5a60ec2002-03-18 11:45:56 +00005540 ctxt->sax, user_data, ctxt->depth,
Daniel Veillarda97a19b2001-05-20 13:19:52 +00005541 ent->URI, ent->ExternalID, &list);
Owen Taylor3473f882001-02-23 17:55:21 +00005542 ctxt->depth--;
5543 } else {
Daniel Veillard7d515752003-09-26 19:12:37 +00005544 ret = XML_ERR_ENTITY_PE_INTERNAL;
Daniel Veillardf403d292003-10-05 13:51:35 +00005545 xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR,
5546 "invalid entity type found\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005547 }
5548 if (ret == XML_ERR_ENTITY_LOOP) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005549 xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00005550 return;
Daniel Veillard7d515752003-09-26 19:12:37 +00005551 } else if ((ret == XML_ERR_OK) && (list != NULL)) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00005552 if (((ent->etype == XML_INTERNAL_GENERAL_ENTITY) ||
5553 (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY))&&
Owen Taylor3473f882001-02-23 17:55:21 +00005554 (ent->children == NULL)) {
5555 ent->children = list;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005556 if (ctxt->replaceEntities) {
5557 /*
5558 * Prune it directly in the generated document
5559 * except for single text nodes.
5560 */
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005561 if (((list->type == XML_TEXT_NODE) &&
5562 (list->next == NULL)) ||
5563 (ctxt->parseMode == XML_PARSE_READER)) {
Daniel Veillard62f313b2001-07-04 19:49:14 +00005564 list->parent = (xmlNodePtr) ent;
5565 list = NULL;
Daniel Veillard2d84a892002-12-30 00:01:08 +00005566 ent->owner = 1;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005567 } else {
Daniel Veillard2d84a892002-12-30 00:01:08 +00005568 ent->owner = 0;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005569 while (list != NULL) {
5570 list->parent = (xmlNodePtr) ctxt->node;
Daniel Veillard68e9e742002-11-16 15:35:11 +00005571 list->doc = ctxt->myDoc;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005572 if (list->next == NULL)
5573 ent->last = list;
5574 list = list->next;
Daniel Veillard8107a222002-01-13 14:10:10 +00005575 }
Daniel Veillard62f313b2001-07-04 19:49:14 +00005576 list = ent->children;
Daniel Veillard81273902003-09-30 00:43:48 +00005577#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +00005578 if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
5579 xmlAddEntityReference(ent, list, NULL);
Daniel Veillard81273902003-09-30 00:43:48 +00005580#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillard62f313b2001-07-04 19:49:14 +00005581 }
5582 } else {
Daniel Veillard2d84a892002-12-30 00:01:08 +00005583 ent->owner = 1;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005584 while (list != NULL) {
5585 list->parent = (xmlNodePtr) ent;
5586 if (list->next == NULL)
5587 ent->last = list;
5588 list = list->next;
5589 }
Owen Taylor3473f882001-02-23 17:55:21 +00005590 }
5591 } else {
5592 xmlFreeNodeList(list);
Daniel Veillard62f313b2001-07-04 19:49:14 +00005593 list = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005594 }
William M. Brackb670e2e2003-09-27 01:05:55 +00005595 } else if ((ret != XML_ERR_OK) &&
5596 (ret != XML_WAR_UNDECLARED_ENTITY)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005597 xmlFatalErr(ctxt, ret, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005598 } else if (list != NULL) {
5599 xmlFreeNodeList(list);
Daniel Veillard62f313b2001-07-04 19:49:14 +00005600 list = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005601 }
5602 }
5603 }
5604 if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) &&
5605 (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) {
5606 /*
5607 * Create a node.
5608 */
5609 ctxt->sax->reference(ctxt->userData, ent->name);
5610 return;
5611 } else if (ctxt->replaceEntities) {
5612 if ((ctxt->node != NULL) && (ent->children != NULL)) {
5613 /*
5614 * Seems we are generating the DOM content, do
Daniel Veillard62f313b2001-07-04 19:49:14 +00005615 * a simple tree copy for all references except the first
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005616 * In the first occurrence list contains the replacement.
5617 * progressive == 2 means we are operating on the Reader
5618 * and since nodes are discarded we must copy all the time.
Owen Taylor3473f882001-02-23 17:55:21 +00005619 */
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005620 if (((list == NULL) && (ent->owner == 0)) ||
5621 (ctxt->parseMode == XML_PARSE_READER)) {
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005622 xmlNodePtr nw = NULL, cur, firstChild = NULL;
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005623
5624 /*
5625 * when operating on a reader, the entities definitions
5626 * are always owning the entities subtree.
5627 if (ctxt->parseMode == XML_PARSE_READER)
5628 ent->owner = 1;
5629 */
5630
Daniel Veillard62f313b2001-07-04 19:49:14 +00005631 cur = ent->children;
5632 while (cur != NULL) {
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005633 nw = xmlCopyNode(cur, 1);
5634 if (nw != NULL) {
5635 nw->_private = cur->_private;
Daniel Veillard8f872442003-01-09 23:19:02 +00005636 if (firstChild == NULL){
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005637 firstChild = nw;
Daniel Veillard8f872442003-01-09 23:19:02 +00005638 }
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005639 nw = xmlAddChild(ctxt->node, nw);
Daniel Veillard8107a222002-01-13 14:10:10 +00005640 }
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005641 if (cur == ent->last) {
5642 /*
5643 * needed to detect some strange empty
5644 * node cases in the reader tests
5645 */
5646 if ((ctxt->parseMode == XML_PARSE_READER) &&
5647 (nw->type == XML_ELEMENT_NODE) &&
5648 (nw->children == NULL))
5649 nw->extra = 1;
5650
Daniel Veillard62f313b2001-07-04 19:49:14 +00005651 break;
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005652 }
Daniel Veillard62f313b2001-07-04 19:49:14 +00005653 cur = cur->next;
5654 }
Daniel Veillard81273902003-09-30 00:43:48 +00005655#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +00005656 if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005657 xmlAddEntityReference(ent, firstChild, nw);
Daniel Veillard81273902003-09-30 00:43:48 +00005658#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005659 } else if (list == NULL) {
5660 xmlNodePtr nw = NULL, cur, next, last,
5661 firstChild = NULL;
5662 /*
5663 * Copy the entity child list and make it the new
5664 * entity child list. The goal is to make sure any
5665 * ID or REF referenced will be the one from the
5666 * document content and not the entity copy.
5667 */
5668 cur = ent->children;
5669 ent->children = NULL;
5670 last = ent->last;
5671 ent->last = NULL;
5672 while (cur != NULL) {
5673 next = cur->next;
5674 cur->next = NULL;
5675 cur->parent = NULL;
5676 nw = xmlCopyNode(cur, 1);
5677 if (nw != NULL) {
5678 nw->_private = cur->_private;
5679 if (firstChild == NULL){
5680 firstChild = cur;
5681 }
5682 xmlAddChild((xmlNodePtr) ent, nw);
5683 xmlAddChild(ctxt->node, cur);
5684 }
5685 if (cur == last)
5686 break;
5687 cur = next;
5688 }
5689 ent->owner = 1;
Daniel Veillard81273902003-09-30 00:43:48 +00005690#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005691 if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
5692 xmlAddEntityReference(ent, firstChild, nw);
Daniel Veillard81273902003-09-30 00:43:48 +00005693#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillard62f313b2001-07-04 19:49:14 +00005694 } else {
5695 /*
5696 * the name change is to avoid coalescing of the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005697 * node with a possible previous text one which
5698 * would make ent->children a dangling pointer
Daniel Veillard62f313b2001-07-04 19:49:14 +00005699 */
5700 if (ent->children->type == XML_TEXT_NODE)
5701 ent->children->name = xmlStrdup(BAD_CAST "nbktext");
5702 if ((ent->last != ent->children) &&
5703 (ent->last->type == XML_TEXT_NODE))
5704 ent->last->name = xmlStrdup(BAD_CAST "nbktext");
5705 xmlAddChildList(ctxt->node, ent->children);
5706 }
5707
Owen Taylor3473f882001-02-23 17:55:21 +00005708 /*
5709 * This is to avoid a nasty side effect, see
5710 * characters() in SAX.c
5711 */
5712 ctxt->nodemem = 0;
5713 ctxt->nodelen = 0;
5714 return;
5715 } else {
5716 /*
5717 * Probably running in SAX mode
5718 */
5719 xmlParserInputPtr input;
5720
5721 input = xmlNewEntityInputStream(ctxt, ent);
5722 xmlPushInput(ctxt, input);
5723 if ((ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) &&
Daniel Veillarda07050d2003-10-19 14:46:32 +00005724 (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) &&
5725 (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00005726 xmlParseTextDecl(ctxt);
5727 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
5728 /*
5729 * The XML REC instructs us to stop parsing right here
5730 */
5731 ctxt->instate = XML_PARSER_EOF;
5732 return;
5733 }
5734 if (input->standalone == 1) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005735 xmlFatalErr(ctxt, XML_ERR_EXT_ENTITY_STANDALONE,
5736 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005737 }
5738 }
5739 return;
5740 }
5741 }
5742 } else {
5743 val = ent->content;
5744 if (val == NULL) return;
5745 /*
5746 * inline the entity.
5747 */
5748 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
5749 (!ctxt->disableSAX))
5750 ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val));
5751 }
5752 }
5753}
5754
5755/**
5756 * xmlParseEntityRef:
5757 * @ctxt: an XML parser context
5758 *
5759 * parse ENTITY references declarations
5760 *
5761 * [68] EntityRef ::= '&' Name ';'
5762 *
5763 * [ WFC: Entity Declared ]
5764 * In a document without any DTD, a document with only an internal DTD
5765 * subset which contains no parameter entity references, or a document
5766 * with "standalone='yes'", the Name given in the entity reference
5767 * must match that in an entity declaration, except that well-formed
5768 * documents need not declare any of the following entities: amp, lt,
5769 * gt, apos, quot. The declaration of a parameter entity must precede
5770 * any reference to it. Similarly, the declaration of a general entity
5771 * must precede any reference to it which appears in a default value in an
5772 * attribute-list declaration. Note that if entities are declared in the
5773 * external subset or in external parameter entities, a non-validating
5774 * processor is not obligated to read and process their declarations;
5775 * for such documents, the rule that an entity must be declared is a
5776 * well-formedness constraint only if standalone='yes'.
5777 *
5778 * [ WFC: Parsed Entity ]
5779 * An entity reference must not contain the name of an unparsed entity
5780 *
5781 * Returns the xmlEntityPtr if found, or NULL otherwise.
5782 */
5783xmlEntityPtr
5784xmlParseEntityRef(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00005785 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00005786 xmlEntityPtr ent = NULL;
5787
5788 GROW;
5789
5790 if (RAW == '&') {
5791 NEXT;
5792 name = xmlParseName(ctxt);
5793 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005794 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
5795 "xmlParseEntityRef: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005796 } else {
5797 if (RAW == ';') {
5798 NEXT;
5799 /*
5800 * Ask first SAX for entity resolution, otherwise try the
5801 * predefined set.
5802 */
5803 if (ctxt->sax != NULL) {
5804 if (ctxt->sax->getEntity != NULL)
5805 ent = ctxt->sax->getEntity(ctxt->userData, name);
Daniel Veillard39eb88b2003-03-11 11:21:28 +00005806 if ((ctxt->wellFormed == 1 ) && (ent == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00005807 ent = xmlGetPredefinedEntity(name);
Daniel Veillard39eb88b2003-03-11 11:21:28 +00005808 if ((ctxt->wellFormed == 1 ) && (ent == NULL) &&
5809 (ctxt->userData==ctxt)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00005810 ent = xmlSAX2GetEntity(ctxt, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00005811 }
Owen Taylor3473f882001-02-23 17:55:21 +00005812 }
5813 /*
5814 * [ WFC: Entity Declared ]
5815 * In a document without any DTD, a document with only an
5816 * internal DTD subset which contains no parameter entity
5817 * references, or a document with "standalone='yes'", the
5818 * Name given in the entity reference must match that in an
5819 * entity declaration, except that well-formed documents
5820 * need not declare any of the following entities: amp, lt,
5821 * gt, apos, quot.
5822 * The declaration of a parameter entity must precede any
5823 * reference to it.
5824 * Similarly, the declaration of a general entity must
5825 * precede any reference to it which appears in a default
5826 * value in an attribute-list declaration. Note that if
5827 * entities are declared in the external subset or in
5828 * external parameter entities, a non-validating processor
5829 * is not obligated to read and process their declarations;
5830 * for such documents, the rule that an entity must be
5831 * declared is a well-formedness constraint only if
5832 * standalone='yes'.
5833 */
5834 if (ent == NULL) {
5835 if ((ctxt->standalone == 1) ||
5836 ((ctxt->hasExternalSubset == 0) &&
5837 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005838 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00005839 "Entity '%s' not defined\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005840 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00005841 xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00005842 "Entity '%s' not defined\n", name);
5843 }
Daniel Veillardf403d292003-10-05 13:51:35 +00005844 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005845 }
5846
5847 /*
5848 * [ WFC: Parsed Entity ]
5849 * An entity reference must not contain the name of an
5850 * unparsed entity
5851 */
5852 else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005853 xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00005854 "Entity reference to unparsed entity %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005855 }
5856
5857 /*
5858 * [ WFC: No External Entity References ]
5859 * Attribute values cannot contain direct or indirect
5860 * entity references to external entities.
5861 */
5862 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
5863 (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005864 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL,
5865 "Attribute references external entity '%s'\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005866 }
5867 /*
5868 * [ WFC: No < in Attribute Values ]
5869 * The replacement text of any entity referred to directly or
5870 * indirectly in an attribute value (other than "&lt;") must
5871 * not contain a <.
5872 */
5873 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
5874 (ent != NULL) &&
5875 (!xmlStrEqual(ent->name, BAD_CAST "lt")) &&
5876 (ent->content != NULL) &&
5877 (xmlStrchr(ent->content, '<'))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005878 xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
Owen Taylor3473f882001-02-23 17:55:21 +00005879 "'<' in entity '%s' is not allowed in attributes values\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005880 }
5881
5882 /*
5883 * Internal check, no parameter entities here ...
5884 */
5885 else {
5886 switch (ent->etype) {
5887 case XML_INTERNAL_PARAMETER_ENTITY:
5888 case XML_EXTERNAL_PARAMETER_ENTITY:
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005889 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER,
5890 "Attempt to reference the parameter entity '%s'\n",
5891 name);
Owen Taylor3473f882001-02-23 17:55:21 +00005892 break;
5893 default:
5894 break;
5895 }
5896 }
5897
5898 /*
5899 * [ WFC: No Recursion ]
5900 * A parsed entity must not contain a recursive reference
5901 * to itself, either directly or indirectly.
5902 * Done somewhere else
5903 */
5904
5905 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005906 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005907 }
Owen Taylor3473f882001-02-23 17:55:21 +00005908 }
5909 }
5910 return(ent);
5911}
5912
5913/**
5914 * xmlParseStringEntityRef:
5915 * @ctxt: an XML parser context
5916 * @str: a pointer to an index in the string
5917 *
5918 * parse ENTITY references declarations, but this version parses it from
5919 * a string value.
5920 *
5921 * [68] EntityRef ::= '&' Name ';'
5922 *
5923 * [ WFC: Entity Declared ]
5924 * In a document without any DTD, a document with only an internal DTD
5925 * subset which contains no parameter entity references, or a document
5926 * with "standalone='yes'", the Name given in the entity reference
5927 * must match that in an entity declaration, except that well-formed
5928 * documents need not declare any of the following entities: amp, lt,
5929 * gt, apos, quot. The declaration of a parameter entity must precede
5930 * any reference to it. Similarly, the declaration of a general entity
5931 * must precede any reference to it which appears in a default value in an
5932 * attribute-list declaration. Note that if entities are declared in the
5933 * external subset or in external parameter entities, a non-validating
5934 * processor is not obligated to read and process their declarations;
5935 * for such documents, the rule that an entity must be declared is a
5936 * well-formedness constraint only if standalone='yes'.
5937 *
5938 * [ WFC: Parsed Entity ]
5939 * An entity reference must not contain the name of an unparsed entity
5940 *
5941 * Returns the xmlEntityPtr if found, or NULL otherwise. The str pointer
5942 * is updated to the current location in the string.
5943 */
5944xmlEntityPtr
5945xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) {
5946 xmlChar *name;
5947 const xmlChar *ptr;
5948 xmlChar cur;
5949 xmlEntityPtr ent = NULL;
5950
5951 if ((str == NULL) || (*str == NULL))
5952 return(NULL);
5953 ptr = *str;
5954 cur = *ptr;
5955 if (cur == '&') {
5956 ptr++;
5957 cur = *ptr;
5958 name = xmlParseStringName(ctxt, &ptr);
5959 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005960 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
5961 "xmlParseStringEntityRef: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005962 } else {
5963 if (*ptr == ';') {
5964 ptr++;
5965 /*
5966 * Ask first SAX for entity resolution, otherwise try the
5967 * predefined set.
5968 */
5969 if (ctxt->sax != NULL) {
5970 if (ctxt->sax->getEntity != NULL)
5971 ent = ctxt->sax->getEntity(ctxt->userData, name);
5972 if (ent == NULL)
5973 ent = xmlGetPredefinedEntity(name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00005974 if ((ent == NULL) && (ctxt->userData==ctxt)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00005975 ent = xmlSAX2GetEntity(ctxt, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00005976 }
Owen Taylor3473f882001-02-23 17:55:21 +00005977 }
5978 /*
5979 * [ WFC: Entity Declared ]
5980 * In a document without any DTD, a document with only an
5981 * internal DTD subset which contains no parameter entity
5982 * references, or a document with "standalone='yes'", the
5983 * Name given in the entity reference must match that in an
5984 * entity declaration, except that well-formed documents
5985 * need not declare any of the following entities: amp, lt,
5986 * gt, apos, quot.
5987 * The declaration of a parameter entity must precede any
5988 * reference to it.
5989 * Similarly, the declaration of a general entity must
5990 * precede any reference to it which appears in a default
5991 * value in an attribute-list declaration. Note that if
5992 * entities are declared in the external subset or in
5993 * external parameter entities, a non-validating processor
5994 * is not obligated to read and process their declarations;
5995 * for such documents, the rule that an entity must be
5996 * declared is a well-formedness constraint only if
5997 * standalone='yes'.
5998 */
5999 if (ent == NULL) {
6000 if ((ctxt->standalone == 1) ||
6001 ((ctxt->hasExternalSubset == 0) &&
6002 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006003 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00006004 "Entity '%s' not defined\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006005 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00006006 xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY,
Daniel Veillard24eb9782003-10-04 21:08:09 +00006007 "Entity '%s' not defined\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00006008 name);
Owen Taylor3473f882001-02-23 17:55:21 +00006009 }
Daniel Veillard24eb9782003-10-04 21:08:09 +00006010 /* TODO ? check regressions ctxt->valid = 0; */
Owen Taylor3473f882001-02-23 17:55:21 +00006011 }
6012
6013 /*
6014 * [ WFC: Parsed Entity ]
6015 * An entity reference must not contain the name of an
6016 * unparsed entity
6017 */
6018 else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006019 xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00006020 "Entity reference to unparsed entity %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006021 }
6022
6023 /*
6024 * [ WFC: No External Entity References ]
6025 * Attribute values cannot contain direct or indirect
6026 * entity references to external entities.
6027 */
6028 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
6029 (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006030 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL,
Owen Taylor3473f882001-02-23 17:55:21 +00006031 "Attribute references external entity '%s'\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006032 }
6033 /*
6034 * [ WFC: No < in Attribute Values ]
6035 * The replacement text of any entity referred to directly or
6036 * indirectly in an attribute value (other than "&lt;") must
6037 * not contain a <.
6038 */
6039 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
6040 (ent != NULL) &&
6041 (!xmlStrEqual(ent->name, BAD_CAST "lt")) &&
6042 (ent->content != NULL) &&
6043 (xmlStrchr(ent->content, '<'))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006044 xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
6045 "'<' in entity '%s' is not allowed in attributes values\n",
6046 name);
Owen Taylor3473f882001-02-23 17:55:21 +00006047 }
6048
6049 /*
6050 * Internal check, no parameter entities here ...
6051 */
6052 else {
6053 switch (ent->etype) {
6054 case XML_INTERNAL_PARAMETER_ENTITY:
6055 case XML_EXTERNAL_PARAMETER_ENTITY:
Daniel Veillardf403d292003-10-05 13:51:35 +00006056 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER,
6057 "Attempt to reference the parameter entity '%s'\n",
6058 name);
Owen Taylor3473f882001-02-23 17:55:21 +00006059 break;
6060 default:
6061 break;
6062 }
6063 }
6064
6065 /*
6066 * [ WFC: No Recursion ]
6067 * A parsed entity must not contain a recursive reference
6068 * to itself, either directly or indirectly.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006069 * Done somewhere else
Owen Taylor3473f882001-02-23 17:55:21 +00006070 */
6071
6072 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006073 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006074 }
6075 xmlFree(name);
6076 }
6077 }
6078 *str = ptr;
6079 return(ent);
6080}
6081
6082/**
6083 * xmlParsePEReference:
6084 * @ctxt: an XML parser context
6085 *
6086 * parse PEReference declarations
6087 * The entity content is handled directly by pushing it's content as
6088 * a new input stream.
6089 *
6090 * [69] PEReference ::= '%' Name ';'
6091 *
6092 * [ WFC: No Recursion ]
6093 * A parsed entity must not contain a recursive
6094 * reference to itself, either directly or indirectly.
6095 *
6096 * [ WFC: Entity Declared ]
6097 * In a document without any DTD, a document with only an internal DTD
6098 * subset which contains no parameter entity references, or a document
6099 * with "standalone='yes'", ... ... The declaration of a parameter
6100 * entity must precede any reference to it...
6101 *
6102 * [ VC: Entity Declared ]
6103 * In a document with an external subset or external parameter entities
6104 * with "standalone='no'", ... ... The declaration of a parameter entity
6105 * must precede any reference to it...
6106 *
6107 * [ WFC: In DTD ]
6108 * Parameter-entity references may only appear in the DTD.
6109 * NOTE: misleading but this is handled.
6110 */
6111void
Daniel Veillard8f597c32003-10-06 08:19:27 +00006112xmlParsePEReference(xmlParserCtxtPtr ctxt)
6113{
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006114 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006115 xmlEntityPtr entity = NULL;
6116 xmlParserInputPtr input;
6117
6118 if (RAW == '%') {
6119 NEXT;
Daniel Veillard76d66f42001-05-16 21:05:17 +00006120 name = xmlParseName(ctxt);
Daniel Veillard8f597c32003-10-06 08:19:27 +00006121 if (name == NULL) {
6122 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6123 "xmlParsePEReference: no name\n");
6124 } else {
6125 if (RAW == ';') {
6126 NEXT;
6127 if ((ctxt->sax != NULL) &&
6128 (ctxt->sax->getParameterEntity != NULL))
6129 entity = ctxt->sax->getParameterEntity(ctxt->userData,
6130 name);
6131 if (entity == NULL) {
6132 /*
6133 * [ WFC: Entity Declared ]
6134 * In a document without any DTD, a document with only an
6135 * internal DTD subset which contains no parameter entity
6136 * references, or a document with "standalone='yes'", ...
6137 * ... The declaration of a parameter entity must precede
6138 * any reference to it...
6139 */
6140 if ((ctxt->standalone == 1) ||
6141 ((ctxt->hasExternalSubset == 0) &&
6142 (ctxt->hasPErefs == 0))) {
6143 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
6144 "PEReference: %%%s; not found\n",
6145 name);
6146 } else {
6147 /*
6148 * [ VC: Entity Declared ]
6149 * In a document with an external subset or external
6150 * parameter entities with "standalone='no'", ...
6151 * ... The declaration of a parameter entity must
6152 * precede any reference to it...
6153 */
6154 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6155 "PEReference: %%%s; not found\n",
6156 name, NULL);
6157 ctxt->valid = 0;
6158 }
6159 } else {
6160 /*
6161 * Internal checking in case the entity quest barfed
6162 */
6163 if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
6164 (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
6165 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6166 "Internal: %%%s; is not a parameter entity\n",
6167 name, NULL);
6168 } else if (ctxt->input->free != deallocblankswrapper) {
6169 input =
6170 xmlNewBlanksWrapperInputStream(ctxt, entity);
6171 xmlPushInput(ctxt, input);
6172 } else {
6173 /*
6174 * TODO !!!
6175 * handle the extra spaces added before and after
6176 * c.f. http://www.w3.org/TR/REC-xml#as-PE
6177 */
6178 input = xmlNewEntityInputStream(ctxt, entity);
6179 xmlPushInput(ctxt, input);
6180 if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) &&
Daniel Veillarda07050d2003-10-19 14:46:32 +00006181 (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) &&
William M. Brack76e95df2003-10-18 16:20:14 +00006182 (IS_BLANK_CH(NXT(5)))) {
Daniel Veillard8f597c32003-10-06 08:19:27 +00006183 xmlParseTextDecl(ctxt);
6184 if (ctxt->errNo ==
6185 XML_ERR_UNSUPPORTED_ENCODING) {
6186 /*
6187 * The XML REC instructs us to stop parsing
6188 * right here
6189 */
6190 ctxt->instate = XML_PARSER_EOF;
6191 return;
6192 }
6193 }
6194 }
6195 }
6196 ctxt->hasPErefs = 1;
6197 } else {
6198 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
6199 }
6200 }
Owen Taylor3473f882001-02-23 17:55:21 +00006201 }
6202}
6203
6204/**
6205 * xmlParseStringPEReference:
6206 * @ctxt: an XML parser context
6207 * @str: a pointer to an index in the string
6208 *
6209 * parse PEReference declarations
6210 *
6211 * [69] PEReference ::= '%' Name ';'
6212 *
6213 * [ WFC: No Recursion ]
6214 * A parsed entity must not contain a recursive
6215 * reference to itself, either directly or indirectly.
6216 *
6217 * [ WFC: Entity Declared ]
6218 * In a document without any DTD, a document with only an internal DTD
6219 * subset which contains no parameter entity references, or a document
6220 * with "standalone='yes'", ... ... The declaration of a parameter
6221 * entity must precede any reference to it...
6222 *
6223 * [ VC: Entity Declared ]
6224 * In a document with an external subset or external parameter entities
6225 * with "standalone='no'", ... ... The declaration of a parameter entity
6226 * must precede any reference to it...
6227 *
6228 * [ WFC: In DTD ]
6229 * Parameter-entity references may only appear in the DTD.
6230 * NOTE: misleading but this is handled.
6231 *
6232 * Returns the string of the entity content.
6233 * str is updated to the current value of the index
6234 */
6235xmlEntityPtr
6236xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) {
6237 const xmlChar *ptr;
6238 xmlChar cur;
6239 xmlChar *name;
6240 xmlEntityPtr entity = NULL;
6241
6242 if ((str == NULL) || (*str == NULL)) return(NULL);
6243 ptr = *str;
6244 cur = *ptr;
6245 if (cur == '%') {
6246 ptr++;
6247 cur = *ptr;
6248 name = xmlParseStringName(ctxt, &ptr);
6249 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006250 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6251 "xmlParseStringPEReference: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006252 } else {
6253 cur = *ptr;
6254 if (cur == ';') {
6255 ptr++;
6256 cur = *ptr;
6257 if ((ctxt->sax != NULL) &&
6258 (ctxt->sax->getParameterEntity != NULL))
6259 entity = ctxt->sax->getParameterEntity(ctxt->userData,
6260 name);
6261 if (entity == NULL) {
6262 /*
6263 * [ WFC: Entity Declared ]
6264 * In a document without any DTD, a document with only an
6265 * internal DTD subset which contains no parameter entity
6266 * references, or a document with "standalone='yes'", ...
6267 * ... The declaration of a parameter entity must precede
6268 * any reference to it...
6269 */
6270 if ((ctxt->standalone == 1) ||
6271 ((ctxt->hasExternalSubset == 0) &&
6272 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006273 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00006274 "PEReference: %%%s; not found\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006275 } else {
6276 /*
6277 * [ VC: Entity Declared ]
6278 * In a document with an external subset or external
6279 * parameter entities with "standalone='no'", ...
6280 * ... The declaration of a parameter entity must
6281 * precede any reference to it...
6282 */
Daniel Veillard24eb9782003-10-04 21:08:09 +00006283 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6284 "PEReference: %%%s; not found\n",
6285 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006286 ctxt->valid = 0;
6287 }
6288 } else {
6289 /*
6290 * Internal checking in case the entity quest barfed
6291 */
6292 if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
6293 (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00006294 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6295 "%%%s; is not a parameter entity\n",
6296 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006297 }
6298 }
6299 ctxt->hasPErefs = 1;
6300 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006301 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006302 }
6303 xmlFree(name);
6304 }
6305 }
6306 *str = ptr;
6307 return(entity);
6308}
6309
6310/**
6311 * xmlParseDocTypeDecl:
6312 * @ctxt: an XML parser context
6313 *
6314 * parse a DOCTYPE declaration
6315 *
6316 * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
6317 * ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
6318 *
6319 * [ VC: Root Element Type ]
6320 * The Name in the document type declaration must match the element
6321 * type of the root element.
6322 */
6323
6324void
6325xmlParseDocTypeDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006326 const xmlChar *name = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00006327 xmlChar *ExternalID = NULL;
6328 xmlChar *URI = NULL;
6329
6330 /*
6331 * We know that '<!DOCTYPE' has been detected.
6332 */
6333 SKIP(9);
6334
6335 SKIP_BLANKS;
6336
6337 /*
6338 * Parse the DOCTYPE name.
6339 */
6340 name = xmlParseName(ctxt);
6341 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006342 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6343 "xmlParseDocTypeDecl : no DOCTYPE name !\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006344 }
6345 ctxt->intSubName = name;
6346
6347 SKIP_BLANKS;
6348
6349 /*
6350 * Check for SystemID and ExternalID
6351 */
6352 URI = xmlParseExternalID(ctxt, &ExternalID, 1);
6353
6354 if ((URI != NULL) || (ExternalID != NULL)) {
6355 ctxt->hasExternalSubset = 1;
6356 }
6357 ctxt->extSubURI = URI;
6358 ctxt->extSubSystem = ExternalID;
6359
6360 SKIP_BLANKS;
6361
6362 /*
6363 * Create and update the internal subset.
6364 */
6365 if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
6366 (!ctxt->disableSAX))
6367 ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
6368
6369 /*
6370 * Is there any internal subset declarations ?
6371 * they are handled separately in xmlParseInternalSubset()
6372 */
6373 if (RAW == '[')
6374 return;
6375
6376 /*
6377 * We should be at the end of the DOCTYPE declaration.
6378 */
6379 if (RAW != '>') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006380 xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006381 }
6382 NEXT;
6383}
6384
6385/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006386 * xmlParseInternalSubset:
Owen Taylor3473f882001-02-23 17:55:21 +00006387 * @ctxt: an XML parser context
6388 *
6389 * parse the internal subset declaration
6390 *
6391 * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
6392 */
6393
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006394static void
Owen Taylor3473f882001-02-23 17:55:21 +00006395xmlParseInternalSubset(xmlParserCtxtPtr ctxt) {
6396 /*
6397 * Is there any DTD definition ?
6398 */
6399 if (RAW == '[') {
6400 ctxt->instate = XML_PARSER_DTD;
6401 NEXT;
6402 /*
6403 * Parse the succession of Markup declarations and
6404 * PEReferences.
6405 * Subsequence (markupdecl | PEReference | S)*
6406 */
6407 while (RAW != ']') {
6408 const xmlChar *check = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00006409 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00006410
6411 SKIP_BLANKS;
6412 xmlParseMarkupDecl(ctxt);
6413 xmlParsePEReference(ctxt);
6414
6415 /*
6416 * Pop-up of finished entities.
6417 */
6418 while ((RAW == 0) && (ctxt->inputNr > 1))
6419 xmlPopInput(ctxt);
6420
6421 if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006422 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006423 "xmlParseInternalSubset: error detected in Markup declaration\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006424 break;
6425 }
6426 }
6427 if (RAW == ']') {
6428 NEXT;
6429 SKIP_BLANKS;
6430 }
6431 }
6432
6433 /*
6434 * We should be at the end of the DOCTYPE declaration.
6435 */
6436 if (RAW != '>') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006437 xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006438 }
6439 NEXT;
6440}
6441
Daniel Veillard81273902003-09-30 00:43:48 +00006442#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00006443/**
6444 * xmlParseAttribute:
6445 * @ctxt: an XML parser context
6446 * @value: a xmlChar ** used to store the value of the attribute
6447 *
6448 * parse an attribute
6449 *
6450 * [41] Attribute ::= Name Eq AttValue
6451 *
6452 * [ WFC: No External Entity References ]
6453 * Attribute values cannot contain direct or indirect entity references
6454 * to external entities.
6455 *
6456 * [ WFC: No < in Attribute Values ]
6457 * The replacement text of any entity referred to directly or indirectly in
6458 * an attribute value (other than "&lt;") must not contain a <.
6459 *
6460 * [ VC: Attribute Value Type ]
6461 * The attribute must have been declared; the value must be of the type
6462 * declared for it.
6463 *
6464 * [25] Eq ::= S? '=' S?
6465 *
6466 * With namespace:
6467 *
6468 * [NS 11] Attribute ::= QName Eq AttValue
6469 *
6470 * Also the case QName == xmlns:??? is handled independently as a namespace
6471 * definition.
6472 *
6473 * Returns the attribute name, and the value in *value.
6474 */
6475
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006476const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006477xmlParseAttribute(xmlParserCtxtPtr ctxt, xmlChar **value) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006478 const xmlChar *name;
6479 xmlChar *val;
Owen Taylor3473f882001-02-23 17:55:21 +00006480
6481 *value = NULL;
Daniel Veillard878eab02002-02-19 13:46:09 +00006482 GROW;
Owen Taylor3473f882001-02-23 17:55:21 +00006483 name = xmlParseName(ctxt);
6484 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006485 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006486 "error parsing attribute name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006487 return(NULL);
6488 }
6489
6490 /*
6491 * read the value
6492 */
6493 SKIP_BLANKS;
6494 if (RAW == '=') {
6495 NEXT;
6496 SKIP_BLANKS;
6497 val = xmlParseAttValue(ctxt);
6498 ctxt->instate = XML_PARSER_CONTENT;
6499 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006500 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Owen Taylor3473f882001-02-23 17:55:21 +00006501 "Specification mandate value for attribute %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006502 return(NULL);
6503 }
6504
6505 /*
6506 * Check that xml:lang conforms to the specification
6507 * No more registered as an error, just generate a warning now
6508 * since this was deprecated in XML second edition
6509 */
6510 if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) {
6511 if (!xmlCheckLanguageID(val)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00006512 xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
6513 "Malformed value for xml:lang : %s\n",
6514 val, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006515 }
6516 }
6517
6518 /*
6519 * Check that xml:space conforms to the specification
6520 */
6521 if (xmlStrEqual(name, BAD_CAST "xml:space")) {
6522 if (xmlStrEqual(val, BAD_CAST "default"))
6523 *(ctxt->space) = 0;
6524 else if (xmlStrEqual(val, BAD_CAST "preserve"))
6525 *(ctxt->space) = 1;
6526 else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006527 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Daniel Veillard642104e2003-03-26 16:32:05 +00006528"Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
Owen Taylor3473f882001-02-23 17:55:21 +00006529 val);
Owen Taylor3473f882001-02-23 17:55:21 +00006530 }
6531 }
6532
6533 *value = val;
6534 return(name);
6535}
6536
6537/**
6538 * xmlParseStartTag:
6539 * @ctxt: an XML parser context
6540 *
6541 * parse a start of tag either for rule element or
6542 * EmptyElement. In both case we don't parse the tag closing chars.
6543 *
6544 * [40] STag ::= '<' Name (S Attribute)* S? '>'
6545 *
6546 * [ WFC: Unique Att Spec ]
6547 * No attribute name may appear more than once in the same start-tag or
6548 * empty-element tag.
6549 *
6550 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
6551 *
6552 * [ WFC: Unique Att Spec ]
6553 * No attribute name may appear more than once in the same start-tag or
6554 * empty-element tag.
6555 *
6556 * With namespace:
6557 *
6558 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
6559 *
6560 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
6561 *
6562 * Returns the element name parsed
6563 */
6564
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006565const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006566xmlParseStartTag(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006567 const xmlChar *name;
6568 const xmlChar *attname;
Owen Taylor3473f882001-02-23 17:55:21 +00006569 xmlChar *attvalue;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006570 const xmlChar **atts = ctxt->atts;
Owen Taylor3473f882001-02-23 17:55:21 +00006571 int nbatts = 0;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006572 int maxatts = ctxt->maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00006573 int i;
6574
6575 if (RAW != '<') return(NULL);
Daniel Veillard21a0f912001-02-25 19:54:14 +00006576 NEXT1;
Owen Taylor3473f882001-02-23 17:55:21 +00006577
6578 name = xmlParseName(ctxt);
6579 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006580 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00006581 "xmlParseStartTag: invalid element name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006582 return(NULL);
6583 }
6584
6585 /*
6586 * Now parse the attributes, it ends up with the ending
6587 *
6588 * (S Attribute)* S?
6589 */
6590 SKIP_BLANKS;
6591 GROW;
6592
Daniel Veillard21a0f912001-02-25 19:54:14 +00006593 while ((RAW != '>') &&
6594 ((RAW != '/') || (NXT(1) != '>')) &&
Daniel Veillard73b013f2003-09-30 12:36:01 +00006595 (IS_BYTE_CHAR(RAW))) {
Owen Taylor3473f882001-02-23 17:55:21 +00006596 const xmlChar *q = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00006597 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00006598
6599 attname = xmlParseAttribute(ctxt, &attvalue);
6600 if ((attname != NULL) && (attvalue != NULL)) {
6601 /*
6602 * [ WFC: Unique Att Spec ]
6603 * No attribute name may appear more than once in the same
6604 * start-tag or empty-element tag.
6605 */
6606 for (i = 0; i < nbatts;i += 2) {
6607 if (xmlStrEqual(atts[i], attname)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006608 xmlErrAttributeDup(ctxt, NULL, attname);
Owen Taylor3473f882001-02-23 17:55:21 +00006609 xmlFree(attvalue);
6610 goto failed;
6611 }
6612 }
Owen Taylor3473f882001-02-23 17:55:21 +00006613 /*
6614 * Add the pair to atts
6615 */
6616 if (atts == NULL) {
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006617 maxatts = 22; /* allow for 10 attrs by default */
6618 atts = (const xmlChar **)
6619 xmlMalloc(maxatts * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00006620 if (atts == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006621 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006622 if (attvalue != NULL)
6623 xmlFree(attvalue);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006624 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00006625 }
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006626 ctxt->atts = atts;
6627 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00006628 } else if (nbatts + 4 > maxatts) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006629 const xmlChar **n;
6630
Owen Taylor3473f882001-02-23 17:55:21 +00006631 maxatts *= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006632 n = (const xmlChar **) xmlRealloc((void *) atts,
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006633 maxatts * sizeof(const xmlChar *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006634 if (n == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006635 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006636 if (attvalue != NULL)
6637 xmlFree(attvalue);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006638 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00006639 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006640 atts = n;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006641 ctxt->atts = atts;
6642 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00006643 }
6644 atts[nbatts++] = attname;
6645 atts[nbatts++] = attvalue;
6646 atts[nbatts] = NULL;
6647 atts[nbatts + 1] = NULL;
6648 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00006649 if (attvalue != NULL)
6650 xmlFree(attvalue);
6651 }
6652
6653failed:
6654
Daniel Veillard3772de32002-12-17 10:31:45 +00006655 GROW
Owen Taylor3473f882001-02-23 17:55:21 +00006656 if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
6657 break;
William M. Brack76e95df2003-10-18 16:20:14 +00006658 if (!IS_BLANK_CH(RAW)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006659 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6660 "attributes construct error\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006661 }
6662 SKIP_BLANKS;
Daniel Veillard02111c12003-02-24 19:14:52 +00006663 if ((cons == ctxt->input->consumed) && (q == CUR_PTR) &&
6664 (attname == NULL) && (attvalue == NULL)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006665 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
6666 "xmlParseStartTag: problem parsing attributes\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006667 break;
6668 }
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006669 SHRINK;
Owen Taylor3473f882001-02-23 17:55:21 +00006670 GROW;
6671 }
6672
6673 /*
6674 * SAX: Start of Element !
6675 */
6676 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) &&
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006677 (!ctxt->disableSAX)) {
6678 if (nbatts > 0)
6679 ctxt->sax->startElement(ctxt->userData, name, atts);
6680 else
6681 ctxt->sax->startElement(ctxt->userData, name, NULL);
6682 }
Owen Taylor3473f882001-02-23 17:55:21 +00006683
6684 if (atts != NULL) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006685 /* Free only the content strings */
6686 for (i = 1;i < nbatts;i+=2)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006687 if (atts[i] != NULL)
6688 xmlFree((xmlChar *) atts[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00006689 }
6690 return(name);
6691}
6692
6693/**
Daniel Veillard0fb18932003-09-07 09:14:37 +00006694 * xmlParseEndTag1:
Owen Taylor3473f882001-02-23 17:55:21 +00006695 * @ctxt: an XML parser context
Daniel Veillard0fb18932003-09-07 09:14:37 +00006696 * @line: line of the start tag
6697 * @nsNr: number of namespaces on the start tag
Owen Taylor3473f882001-02-23 17:55:21 +00006698 *
6699 * parse an end of tag
6700 *
6701 * [42] ETag ::= '</' Name S? '>'
6702 *
6703 * With namespace
6704 *
6705 * [NS 9] ETag ::= '</' QName S? '>'
6706 */
6707
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00006708static void
Daniel Veillard0fb18932003-09-07 09:14:37 +00006709xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006710 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006711
6712 GROW;
6713 if ((RAW != '<') || (NXT(1) != '/')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006714 xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006715 "xmlParseEndTag: '</' not found\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006716 return;
6717 }
6718 SKIP(2);
6719
Daniel Veillard46de64e2002-05-29 08:21:33 +00006720 name = xmlParseNameAndCompare(ctxt,ctxt->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006721
6722 /*
6723 * We should definitely be at the ending "S? '>'" part
6724 */
6725 GROW;
6726 SKIP_BLANKS;
Daniel Veillard73b013f2003-09-30 12:36:01 +00006727 if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006728 xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006729 } else
Daniel Veillard21a0f912001-02-25 19:54:14 +00006730 NEXT1;
Owen Taylor3473f882001-02-23 17:55:21 +00006731
6732 /*
6733 * [ WFC: Element Type Match ]
6734 * The Name in an element's end-tag must match the element type in the
6735 * start-tag.
6736 *
6737 */
Daniel Veillard46de64e2002-05-29 08:21:33 +00006738 if (name != (xmlChar*)1) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006739 if (name == NULL) name = BAD_CAST "unparseable";
6740 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00006741 "Opening and ending tag mismatch: %s line %d and %s\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00006742 ctxt->name, line, name);
Owen Taylor3473f882001-02-23 17:55:21 +00006743 }
6744
6745 /*
6746 * SAX: End of Tag
6747 */
6748 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
6749 (!ctxt->disableSAX))
Daniel Veillard46de64e2002-05-29 08:21:33 +00006750 ctxt->sax->endElement(ctxt->userData, ctxt->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006751
Daniel Veillarde57ec792003-09-10 10:50:59 +00006752 namePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006753 spacePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006754 return;
6755}
6756
6757/**
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00006758 * xmlParseEndTag:
6759 * @ctxt: an XML parser context
6760 *
6761 * parse an end of tag
6762 *
6763 * [42] ETag ::= '</' Name S? '>'
6764 *
6765 * With namespace
6766 *
6767 * [NS 9] ETag ::= '</' QName S? '>'
6768 */
6769
6770void
6771xmlParseEndTag(xmlParserCtxtPtr ctxt) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00006772 xmlParseEndTag1(ctxt, 0);
6773}
Daniel Veillard81273902003-09-30 00:43:48 +00006774#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard0fb18932003-09-07 09:14:37 +00006775
6776/************************************************************************
6777 * *
6778 * SAX 2 specific operations *
6779 * *
6780 ************************************************************************/
6781
6782static const xmlChar *
6783xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) {
6784 int len = 0, l;
6785 int c;
6786 int count = 0;
6787
6788 /*
6789 * Handler for more complex cases
6790 */
6791 GROW;
6792 c = CUR_CHAR(l);
6793 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006794 (!IS_LETTER(c) && (c != '_'))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00006795 return(NULL);
6796 }
6797
6798 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
William M. Brack871611b2003-10-18 04:53:14 +00006799 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006800 (c == '.') || (c == '-') || (c == '_') ||
William M. Brack871611b2003-10-18 04:53:14 +00006801 (IS_COMBINING(c)) ||
6802 (IS_EXTENDER(c)))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00006803 if (count++ > 100) {
6804 count = 0;
6805 GROW;
6806 }
6807 len += l;
6808 NEXTL(l);
6809 c = CUR_CHAR(l);
6810 }
6811 return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len));
6812}
6813
6814/*
6815 * xmlGetNamespace:
6816 * @ctxt: an XML parser context
6817 * @prefix: the prefix to lookup
6818 *
6819 * Lookup the namespace name for the @prefix (which ca be NULL)
6820 * The prefix must come from the @ctxt->dict dictionnary
6821 *
6822 * Returns the namespace name or NULL if not bound
6823 */
6824static const xmlChar *
6825xmlGetNamespace(xmlParserCtxtPtr ctxt, const xmlChar *prefix) {
6826 int i;
6827
Daniel Veillarde57ec792003-09-10 10:50:59 +00006828 if (prefix == ctxt->str_xml) return(ctxt->str_xml_ns);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006829 for (i = ctxt->nsNr - 2;i >= 0;i-=2)
Daniel Veillarde57ec792003-09-10 10:50:59 +00006830 if (ctxt->nsTab[i] == prefix) {
6831 if ((prefix == NULL) && (*ctxt->nsTab[i + 1] == 0))
6832 return(NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006833 return(ctxt->nsTab[i + 1]);
Daniel Veillarde57ec792003-09-10 10:50:59 +00006834 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00006835 return(NULL);
6836}
6837
6838/**
6839 * xmlParseNCName:
6840 * @ctxt: an XML parser context
Daniel Veillardc82c57e2004-01-12 16:24:34 +00006841 * @len: lenght of the string parsed
Daniel Veillard0fb18932003-09-07 09:14:37 +00006842 *
6843 * parse an XML name.
6844 *
6845 * [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' |
6846 * CombiningChar | Extender
6847 *
6848 * [5NS] NCName ::= (Letter | '_') (NCNameChar)*
6849 *
6850 * Returns the Name parsed or NULL
6851 */
6852
6853static const xmlChar *
6854xmlParseNCName(xmlParserCtxtPtr ctxt) {
6855 const xmlChar *in;
6856 const xmlChar *ret;
6857 int count = 0;
6858
6859 /*
6860 * Accelerator for simple ASCII names
6861 */
6862 in = ctxt->input->cur;
6863 if (((*in >= 0x61) && (*in <= 0x7A)) ||
6864 ((*in >= 0x41) && (*in <= 0x5A)) ||
6865 (*in == '_')) {
6866 in++;
6867 while (((*in >= 0x61) && (*in <= 0x7A)) ||
6868 ((*in >= 0x41) && (*in <= 0x5A)) ||
6869 ((*in >= 0x30) && (*in <= 0x39)) ||
6870 (*in == '_') || (*in == '-') ||
6871 (*in == '.'))
6872 in++;
6873 if ((*in > 0) && (*in < 0x80)) {
6874 count = in - ctxt->input->cur;
6875 ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
6876 ctxt->input->cur = in;
6877 ctxt->nbChars += count;
6878 ctxt->input->col += count;
6879 if (ret == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006880 xmlErrMemory(ctxt, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006881 }
6882 return(ret);
6883 }
6884 }
6885 return(xmlParseNCNameComplex(ctxt));
6886}
6887
6888/**
6889 * xmlParseQName:
6890 * @ctxt: an XML parser context
6891 * @prefix: pointer to store the prefix part
6892 *
6893 * parse an XML Namespace QName
6894 *
6895 * [6] QName ::= (Prefix ':')? LocalPart
6896 * [7] Prefix ::= NCName
6897 * [8] LocalPart ::= NCName
6898 *
6899 * Returns the Name parsed or NULL
6900 */
6901
6902static const xmlChar *
6903xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) {
6904 const xmlChar *l, *p;
6905
6906 GROW;
6907
6908 l = xmlParseNCName(ctxt);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006909 if (l == NULL) {
6910 if (CUR == ':') {
6911 l = xmlParseName(ctxt);
6912 if (l != NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006913 xmlNsErr(ctxt, XML_NS_ERR_QNAME,
6914 "Failed to parse QName '%s'\n", l, NULL, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006915 *prefix = NULL;
6916 return(l);
6917 }
6918 }
6919 return(NULL);
6920 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00006921 if (CUR == ':') {
6922 NEXT;
6923 p = l;
6924 l = xmlParseNCName(ctxt);
6925 if (l == NULL) {
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006926 xmlChar *tmp;
6927
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006928 xmlNsErr(ctxt, XML_NS_ERR_QNAME,
6929 "Failed to parse QName '%s:'\n", p, NULL, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006930 tmp = xmlBuildQName(BAD_CAST "", p, NULL, 0);
6931 p = xmlDictLookup(ctxt->dict, tmp, -1);
6932 if (tmp != NULL) xmlFree(tmp);
6933 *prefix = NULL;
6934 return(p);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006935 }
6936 if (CUR == ':') {
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006937 xmlChar *tmp;
6938
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006939 xmlNsErr(ctxt, XML_NS_ERR_QNAME,
6940 "Failed to parse QName '%s:%s:'\n", p, l, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006941 NEXT;
6942 tmp = (xmlChar *) xmlParseName(ctxt);
6943 if (tmp != NULL) {
6944 tmp = xmlBuildQName(tmp, l, NULL, 0);
6945 l = xmlDictLookup(ctxt->dict, tmp, -1);
6946 if (tmp != NULL) xmlFree(tmp);
6947 *prefix = p;
6948 return(l);
6949 }
6950 tmp = xmlBuildQName(BAD_CAST "", l, NULL, 0);
6951 l = xmlDictLookup(ctxt->dict, tmp, -1);
6952 if (tmp != NULL) xmlFree(tmp);
6953 *prefix = p;
6954 return(l);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006955 }
6956 *prefix = p;
6957 } else
6958 *prefix = NULL;
6959 return(l);
6960}
6961
6962/**
6963 * xmlParseQNameAndCompare:
6964 * @ctxt: an XML parser context
6965 * @name: the localname
6966 * @prefix: the prefix, if any.
6967 *
6968 * parse an XML name and compares for match
6969 * (specialized for endtag parsing)
6970 *
6971 * Returns NULL for an illegal name, (xmlChar*) 1 for success
6972 * and the name for mismatch
6973 */
6974
6975static const xmlChar *
6976xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name,
6977 xmlChar const *prefix) {
6978 const xmlChar *cmp = name;
6979 const xmlChar *in;
6980 const xmlChar *ret;
6981 const xmlChar *prefix2;
6982
6983 if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name));
6984
6985 GROW;
6986 in = ctxt->input->cur;
6987
6988 cmp = prefix;
6989 while (*in != 0 && *in == *cmp) {
6990 ++in;
6991 ++cmp;
6992 }
6993 if ((*cmp == 0) && (*in == ':')) {
6994 in++;
6995 cmp = name;
6996 while (*in != 0 && *in == *cmp) {
6997 ++in;
6998 ++cmp;
6999 }
William M. Brack76e95df2003-10-18 16:20:14 +00007000 if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007001 /* success */
7002 ctxt->input->cur = in;
7003 return((const xmlChar*) 1);
7004 }
7005 }
7006 /*
7007 * all strings coms from the dictionary, equality can be done directly
7008 */
7009 ret = xmlParseQName (ctxt, &prefix2);
7010 if ((ret == name) && (prefix == prefix2))
7011 return((const xmlChar*) 1);
7012 return ret;
7013}
7014
7015/**
7016 * xmlParseAttValueInternal:
7017 * @ctxt: an XML parser context
7018 * @len: attribute len result
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007019 * @alloc: whether the attribute was reallocated as a new string
7020 * @normalize: if 1 then further non-CDATA normalization must be done
Daniel Veillard0fb18932003-09-07 09:14:37 +00007021 *
7022 * parse a value for an attribute.
7023 * NOTE: if no normalization is needed, the routine will return pointers
7024 * directly from the data buffer.
7025 *
7026 * 3.3.3 Attribute-Value Normalization:
7027 * Before the value of an attribute is passed to the application or
7028 * checked for validity, the XML processor must normalize it as follows:
7029 * - a character reference is processed by appending the referenced
7030 * character to the attribute value
7031 * - an entity reference is processed by recursively processing the
7032 * replacement text of the entity
7033 * - a whitespace character (#x20, #xD, #xA, #x9) is processed by
7034 * appending #x20 to the normalized value, except that only a single
7035 * #x20 is appended for a "#xD#xA" sequence that is part of an external
7036 * parsed entity or the literal entity value of an internal parsed entity
7037 * - other characters are processed by appending them to the normalized value
7038 * If the declared value is not CDATA, then the XML processor must further
7039 * process the normalized attribute value by discarding any leading and
7040 * trailing space (#x20) characters, and by replacing sequences of space
7041 * (#x20) characters by a single space (#x20) character.
7042 * All attributes for which no declaration has been read should be treated
7043 * by a non-validating parser as if declared CDATA.
7044 *
7045 * Returns the AttValue parsed or NULL. The value has to be freed by the
7046 * caller if it was copied, this can be detected by val[*len] == 0.
7047 */
7048
7049static xmlChar *
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007050xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc,
7051 int normalize)
Daniel Veillarde57ec792003-09-10 10:50:59 +00007052{
Daniel Veillard0fb18932003-09-07 09:14:37 +00007053 xmlChar limit = 0;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007054 const xmlChar *in = NULL, *start, *end, *last;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007055 xmlChar *ret = NULL;
7056
7057 GROW;
7058 in = (xmlChar *) CUR_PTR;
7059 if (*in != '"' && *in != '\'') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007060 xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007061 return (NULL);
7062 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007063 ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007064
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007065 /*
7066 * try to handle in this routine the most common case where no
7067 * allocation of a new string is required and where content is
7068 * pure ASCII.
7069 */
7070 limit = *in++;
7071 end = ctxt->input->end;
7072 start = in;
7073 if (in >= end) {
7074 const xmlChar *oldbase = ctxt->input->base;
7075 GROW;
7076 if (oldbase != ctxt->input->base) {
7077 long delta = ctxt->input->base - oldbase;
7078 start = start + delta;
7079 in = in + delta;
7080 }
7081 end = ctxt->input->end;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007082 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007083 if (normalize) {
7084 /*
7085 * Skip any leading spaces
7086 */
7087 while ((in < end) && (*in != limit) &&
7088 ((*in == 0x20) || (*in == 0x9) ||
7089 (*in == 0xA) || (*in == 0xD))) {
7090 in++;
7091 start = in;
7092 if (in >= end) {
7093 const xmlChar *oldbase = ctxt->input->base;
7094 GROW;
7095 if (oldbase != ctxt->input->base) {
7096 long delta = ctxt->input->base - oldbase;
7097 start = start + delta;
7098 in = in + delta;
7099 }
7100 end = ctxt->input->end;
7101 }
7102 }
7103 while ((in < end) && (*in != limit) && (*in >= 0x20) &&
7104 (*in <= 0x7f) && (*in != '&') && (*in != '<')) {
7105 if ((*in++ == 0x20) && (*in == 0x20)) break;
7106 if (in >= end) {
7107 const xmlChar *oldbase = ctxt->input->base;
7108 GROW;
7109 if (oldbase != ctxt->input->base) {
7110 long delta = ctxt->input->base - oldbase;
7111 start = start + delta;
7112 in = in + delta;
7113 }
7114 end = ctxt->input->end;
7115 }
7116 }
7117 last = in;
7118 /*
7119 * skip the trailing blanks
7120 */
Daniel Veillardc6e20e42003-09-11 16:30:26 +00007121 while ((last[-1] == 0x20) && (last > start)) last--;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007122 while ((in < end) && (*in != limit) &&
7123 ((*in == 0x20) || (*in == 0x9) ||
7124 (*in == 0xA) || (*in == 0xD))) {
7125 in++;
7126 if (in >= end) {
7127 const xmlChar *oldbase = ctxt->input->base;
7128 GROW;
7129 if (oldbase != ctxt->input->base) {
7130 long delta = ctxt->input->base - oldbase;
7131 start = start + delta;
7132 in = in + delta;
7133 last = last + delta;
7134 }
7135 end = ctxt->input->end;
7136 }
7137 }
7138 if (*in != limit) goto need_complex;
7139 } else {
7140 while ((in < end) && (*in != limit) && (*in >= 0x20) &&
7141 (*in <= 0x7f) && (*in != '&') && (*in != '<')) {
7142 in++;
7143 if (in >= end) {
7144 const xmlChar *oldbase = ctxt->input->base;
7145 GROW;
7146 if (oldbase != ctxt->input->base) {
7147 long delta = ctxt->input->base - oldbase;
7148 start = start + delta;
7149 in = in + delta;
7150 }
7151 end = ctxt->input->end;
7152 }
7153 }
7154 last = in;
7155 if (*in != limit) goto need_complex;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007156 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007157 in++;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007158 if (len != NULL) {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007159 *len = last - start;
7160 ret = (xmlChar *) start;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007161 } else {
Daniel Veillarde57ec792003-09-10 10:50:59 +00007162 if (alloc) *alloc = 1;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007163 ret = xmlStrndup(start, last - start);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007164 }
7165 CUR_PTR = in;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007166 if (alloc) *alloc = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007167 return ret;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007168need_complex:
7169 if (alloc) *alloc = 1;
7170 return xmlParseAttValueComplex(ctxt, len, normalize);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007171}
7172
7173/**
7174 * xmlParseAttribute2:
7175 * @ctxt: an XML parser context
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007176 * @pref: the element prefix
7177 * @elem: the element name
7178 * @prefix: a xmlChar ** used to store the value of the attribute prefix
Daniel Veillard0fb18932003-09-07 09:14:37 +00007179 * @value: a xmlChar ** used to store the value of the attribute
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007180 * @len: an int * to save the length of the attribute
7181 * @alloc: an int * to indicate if the attribute was allocated
Daniel Veillard0fb18932003-09-07 09:14:37 +00007182 *
7183 * parse an attribute in the new SAX2 framework.
7184 *
7185 * Returns the attribute name, and the value in *value, .
7186 */
7187
7188static const xmlChar *
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007189xmlParseAttribute2(xmlParserCtxtPtr ctxt,
7190 const xmlChar *pref, const xmlChar *elem,
7191 const xmlChar **prefix, xmlChar **value,
7192 int *len, int *alloc) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007193 const xmlChar *name;
7194 xmlChar *val;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007195 int normalize = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007196
7197 *value = NULL;
7198 GROW;
7199 name = xmlParseQName(ctxt, prefix);
7200 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007201 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7202 "error parsing attribute name\n");
Daniel Veillard0fb18932003-09-07 09:14:37 +00007203 return(NULL);
7204 }
7205
7206 /*
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007207 * get the type if needed
7208 */
7209 if (ctxt->attsSpecial != NULL) {
7210 int type;
7211
7212 type = (int) (long) xmlHashQLookup2(ctxt->attsSpecial,
7213 pref, elem, *prefix, name);
7214 if (type != 0) normalize = 1;
7215 }
7216
7217 /*
Daniel Veillard0fb18932003-09-07 09:14:37 +00007218 * read the value
7219 */
7220 SKIP_BLANKS;
7221 if (RAW == '=') {
7222 NEXT;
7223 SKIP_BLANKS;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007224 val = xmlParseAttValueInternal(ctxt, len, alloc, normalize);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007225 ctxt->instate = XML_PARSER_CONTENT;
7226 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00007227 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007228 "Specification mandate value for attribute %s\n", name);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007229 return(NULL);
7230 }
7231
7232 /*
7233 * Check that xml:lang conforms to the specification
7234 * No more registered as an error, just generate a warning now
7235 * since this was deprecated in XML second edition
7236 */
7237 if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) {
7238 if (!xmlCheckLanguageID(val)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007239 xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
7240 "Malformed value for xml:lang : %s\n",
7241 val, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007242 }
7243 }
7244
7245 /*
7246 * Check that xml:space conforms to the specification
7247 */
7248 if (xmlStrEqual(name, BAD_CAST "xml:space")) {
7249 if (xmlStrEqual(val, BAD_CAST "default"))
7250 *(ctxt->space) = 0;
7251 else if (xmlStrEqual(val, BAD_CAST "preserve"))
7252 *(ctxt->space) = 1;
7253 else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00007254 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007255"Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
7256 val);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007257 }
7258 }
7259
7260 *value = val;
7261 return(name);
7262}
7263
7264/**
7265 * xmlParseStartTag2:
7266 * @ctxt: an XML parser context
7267 *
7268 * parse a start of tag either for rule element or
7269 * EmptyElement. In both case we don't parse the tag closing chars.
7270 * This routine is called when running SAX2 parsing
7271 *
7272 * [40] STag ::= '<' Name (S Attribute)* S? '>'
7273 *
7274 * [ WFC: Unique Att Spec ]
7275 * No attribute name may appear more than once in the same start-tag or
7276 * empty-element tag.
7277 *
7278 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
7279 *
7280 * [ WFC: Unique Att Spec ]
7281 * No attribute name may appear more than once in the same start-tag or
7282 * empty-element tag.
7283 *
7284 * With namespace:
7285 *
7286 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
7287 *
7288 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
7289 *
7290 * Returns the element name parsed
7291 */
7292
7293static const xmlChar *
7294xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref,
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007295 const xmlChar **URI, int *tlen) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007296 const xmlChar *localname;
7297 const xmlChar *prefix;
7298 const xmlChar *attname;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007299 const xmlChar *aprefix;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007300 const xmlChar *nsname;
7301 xmlChar *attvalue;
7302 const xmlChar **atts = ctxt->atts;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007303 int maxatts = ctxt->maxatts;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007304 int nratts, nbatts, nbdef;
7305 int i, j, nbNs, attval;
7306 const xmlChar *base;
7307 unsigned long cur;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007308
7309 if (RAW != '<') return(NULL);
7310 NEXT1;
7311
7312 /*
7313 * NOTE: it is crucial with the SAX2 API to never call SHRINK beyond that
7314 * point since the attribute values may be stored as pointers to
7315 * the buffer and calling SHRINK would destroy them !
7316 * The Shrinking is only possible once the full set of attribute
7317 * callbacks have been done.
7318 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00007319reparse:
Daniel Veillard0fb18932003-09-07 09:14:37 +00007320 SHRINK;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007321 base = ctxt->input->base;
7322 cur = ctxt->input->cur - ctxt->input->base;
7323 nbatts = 0;
7324 nratts = 0;
7325 nbdef = 0;
7326 nbNs = 0;
7327 attval = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007328
7329 localname = xmlParseQName(ctxt, &prefix);
7330 if (localname == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007331 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7332 "StartTag: invalid element name\n");
Daniel Veillard0fb18932003-09-07 09:14:37 +00007333 return(NULL);
7334 }
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007335 *tlen = ctxt->input->cur - ctxt->input->base - cur;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007336
7337 /*
7338 * Now parse the attributes, it ends up with the ending
7339 *
7340 * (S Attribute)* S?
7341 */
7342 SKIP_BLANKS;
7343 GROW;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007344 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007345
7346 while ((RAW != '>') &&
7347 ((RAW != '/') || (NXT(1) != '>')) &&
Daniel Veillard73b013f2003-09-30 12:36:01 +00007348 (IS_BYTE_CHAR(RAW))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007349 const xmlChar *q = CUR_PTR;
7350 unsigned int cons = ctxt->input->consumed;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007351 int len = -1, alloc = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007352
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007353 attname = xmlParseAttribute2(ctxt, prefix, localname,
7354 &aprefix, &attvalue, &len, &alloc);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007355 if ((attname != NULL) && (attvalue != NULL)) {
7356 if (len < 0) len = xmlStrlen(attvalue);
7357 if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00007358 const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len);
7359 xmlURIPtr uri;
7360
7361 if (*URL != 0) {
7362 uri = xmlParseURI((const char *) URL);
7363 if (uri == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007364 xmlWarningMsg(ctxt, XML_WAR_NS_URI,
7365 "xmlns: %s not a valid URI\n",
7366 URL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007367 } else {
7368 if (uri->scheme == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007369 xmlWarningMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
7370 "xmlns: URI %s is not absolute\n",
7371 URL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007372 }
7373 xmlFreeURI(uri);
7374 }
7375 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007376 /*
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007377 * check that it's not a defined namespace
Daniel Veillard0fb18932003-09-07 09:14:37 +00007378 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007379 for (j = 1;j <= nbNs;j++)
7380 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL)
7381 break;
7382 if (j <= nbNs)
7383 xmlErrAttributeDup(ctxt, NULL, attname);
7384 else
7385 if (nsPush(ctxt, NULL, URL) > 0) nbNs++;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007386 if (alloc != 0) xmlFree(attvalue);
7387 SKIP_BLANKS;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007388 continue;
7389 }
7390 if (aprefix == ctxt->str_xmlns) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00007391 const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len);
7392 xmlURIPtr uri;
7393
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007394 if (attname == ctxt->str_xml) {
7395 if (URL != ctxt->str_xml_ns) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007396 xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
7397 "xml namespace prefix mapped to wrong URI\n",
7398 NULL, NULL, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007399 }
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007400 /*
7401 * Do not keep a namespace definition node
7402 */
7403 if (alloc != 0) xmlFree(attvalue);
7404 SKIP_BLANKS;
7405 continue;
7406 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00007407 uri = xmlParseURI((const char *) URL);
7408 if (uri == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007409 xmlWarningMsg(ctxt, XML_WAR_NS_URI,
7410 "xmlns:%s: '%s' is not a valid URI\n",
7411 attname, URL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007412 } else {
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007413 if ((ctxt->pedantic) && (uri->scheme == NULL)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007414 xmlWarningMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
7415 "xmlns:%s: URI %s is not absolute\n",
7416 attname, URL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007417 }
7418 xmlFreeURI(uri);
7419 }
7420
Daniel Veillard0fb18932003-09-07 09:14:37 +00007421 /*
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007422 * check that it's not a defined namespace
Daniel Veillard0fb18932003-09-07 09:14:37 +00007423 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007424 for (j = 1;j <= nbNs;j++)
7425 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname)
7426 break;
7427 if (j <= nbNs)
7428 xmlErrAttributeDup(ctxt, aprefix, attname);
7429 else
7430 if (nsPush(ctxt, attname, URL) > 0) nbNs++;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007431 if (alloc != 0) xmlFree(attvalue);
7432 SKIP_BLANKS;
Daniel Veillardd3999c72004-03-10 16:27:03 +00007433 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007434 continue;
7435 }
7436
7437 /*
7438 * Add the pair to atts
7439 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00007440 if ((atts == NULL) || (nbatts + 5 > maxatts)) {
7441 if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007442 if (attvalue[len] == 0)
7443 xmlFree(attvalue);
7444 goto failed;
7445 }
7446 maxatts = ctxt->maxatts;
7447 atts = ctxt->atts;
7448 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00007449 ctxt->attallocs[nratts++] = alloc;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007450 atts[nbatts++] = attname;
7451 atts[nbatts++] = aprefix;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007452 atts[nbatts++] = NULL; /* the URI will be fetched later */
Daniel Veillard0fb18932003-09-07 09:14:37 +00007453 atts[nbatts++] = attvalue;
7454 attvalue += len;
7455 atts[nbatts++] = attvalue;
7456 /*
7457 * tag if some deallocation is needed
7458 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00007459 if (alloc != 0) attval = 1;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007460 } else {
7461 if ((attvalue != NULL) && (attvalue[len] == 0))
7462 xmlFree(attvalue);
7463 }
7464
7465failed:
7466
7467 GROW
Daniel Veillarde57ec792003-09-10 10:50:59 +00007468 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007469 if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
7470 break;
William M. Brack76e95df2003-10-18 16:20:14 +00007471 if (!IS_BLANK_CH(RAW)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007472 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
7473 "attributes construct error\n");
William M. Brack13dfa872004-09-18 04:52:08 +00007474 break;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007475 }
7476 SKIP_BLANKS;
7477 if ((cons == ctxt->input->consumed) && (q == CUR_PTR) &&
7478 (attname == NULL) && (attvalue == NULL)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007479 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007480 "xmlParseStartTag: problem parsing attributes\n");
Daniel Veillard0fb18932003-09-07 09:14:37 +00007481 break;
7482 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007483 GROW;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007484 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007485 }
7486
Daniel Veillard0fb18932003-09-07 09:14:37 +00007487 /*
Daniel Veillarde57ec792003-09-10 10:50:59 +00007488 * The attributes defaulting
7489 */
7490 if (ctxt->attsDefault != NULL) {
7491 xmlDefAttrsPtr defaults;
7492
7493 defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix);
7494 if (defaults != NULL) {
7495 for (i = 0;i < defaults->nbAttrs;i++) {
7496 attname = defaults->values[4 * i];
7497 aprefix = defaults->values[4 * i + 1];
7498
7499 /*
7500 * special work for namespaces defaulted defs
7501 */
7502 if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
7503 /*
7504 * check that it's not a defined namespace
7505 */
7506 for (j = 1;j <= nbNs;j++)
7507 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL)
7508 break;
7509 if (j <= nbNs) continue;
7510
7511 nsname = xmlGetNamespace(ctxt, NULL);
7512 if (nsname != defaults->values[4 * i + 2]) {
7513 if (nsPush(ctxt, NULL,
7514 defaults->values[4 * i + 2]) > 0)
7515 nbNs++;
7516 }
7517 } else if (aprefix == ctxt->str_xmlns) {
7518 /*
7519 * check that it's not a defined namespace
7520 */
7521 for (j = 1;j <= nbNs;j++)
7522 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname)
7523 break;
7524 if (j <= nbNs) continue;
7525
7526 nsname = xmlGetNamespace(ctxt, attname);
7527 if (nsname != defaults->values[2]) {
7528 if (nsPush(ctxt, attname,
7529 defaults->values[4 * i + 2]) > 0)
7530 nbNs++;
7531 }
7532 } else {
7533 /*
7534 * check that it's not a defined attribute
7535 */
7536 for (j = 0;j < nbatts;j+=5) {
7537 if ((attname == atts[j]) && (aprefix == atts[j+1]))
7538 break;
7539 }
7540 if (j < nbatts) continue;
7541
7542 if ((atts == NULL) || (nbatts + 5 > maxatts)) {
7543 if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00007544 return(NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007545 }
7546 maxatts = ctxt->maxatts;
7547 atts = ctxt->atts;
7548 }
7549 atts[nbatts++] = attname;
7550 atts[nbatts++] = aprefix;
7551 if (aprefix == NULL)
7552 atts[nbatts++] = NULL;
7553 else
7554 atts[nbatts++] = xmlGetNamespace(ctxt, aprefix);
7555 atts[nbatts++] = defaults->values[4 * i + 2];
7556 atts[nbatts++] = defaults->values[4 * i + 3];
7557 nbdef++;
7558 }
7559 }
7560 }
7561 }
7562
Daniel Veillarde70c8772003-11-25 07:21:18 +00007563 /*
7564 * The attributes checkings
7565 */
7566 for (i = 0; i < nbatts;i += 5) {
7567 nsname = xmlGetNamespace(ctxt, atts[i + 1]);
7568 if ((atts[i + 1] != NULL) && (nsname == NULL)) {
7569 xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
7570 "Namespace prefix %s for %s on %s is not defined\n",
7571 atts[i + 1], atts[i], localname);
7572 }
7573 atts[i + 2] = nsname;
7574 /*
7575 * [ WFC: Unique Att Spec ]
7576 * No attribute name may appear more than once in the same
7577 * start-tag or empty-element tag.
7578 * As extended by the Namespace in XML REC.
7579 */
7580 for (j = 0; j < i;j += 5) {
7581 if (atts[i] == atts[j]) {
7582 if (atts[i+1] == atts[j+1]) {
7583 xmlErrAttributeDup(ctxt, atts[i+1], atts[i]);
7584 break;
7585 }
7586 if ((nsname != NULL) && (atts[j + 2] == nsname)) {
7587 xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED,
7588 "Namespaced Attribute %s in '%s' redefined\n",
7589 atts[i], nsname, NULL);
7590 break;
7591 }
7592 }
7593 }
7594 }
7595
Daniel Veillarde57ec792003-09-10 10:50:59 +00007596 nsname = xmlGetNamespace(ctxt, prefix);
7597 if ((prefix != NULL) && (nsname == NULL)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007598 xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
7599 "Namespace prefix %s on %s is not defined\n",
7600 prefix, localname, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007601 }
7602 *pref = prefix;
7603 *URI = nsname;
7604
7605 /*
7606 * SAX: Start of Element !
7607 */
7608 if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) &&
7609 (!ctxt->disableSAX)) {
7610 if (nbNs > 0)
7611 ctxt->sax->startElementNs(ctxt->userData, localname, prefix,
7612 nsname, nbNs, &ctxt->nsTab[ctxt->nsNr - 2 * nbNs],
7613 nbatts / 5, nbdef, atts);
7614 else
7615 ctxt->sax->startElementNs(ctxt->userData, localname, prefix,
7616 nsname, 0, NULL, nbatts / 5, nbdef, atts);
7617 }
7618
7619 /*
7620 * Free up attribute allocated strings if needed
7621 */
7622 if (attval != 0) {
7623 for (i = 3,j = 0; j < nratts;i += 5,j++)
7624 if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL))
7625 xmlFree((xmlChar *) atts[i]);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007626 }
7627
7628 return(localname);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007629
7630base_changed:
7631 /*
7632 * the attribute strings are valid iif the base didn't changed
7633 */
7634 if (attval != 0) {
7635 for (i = 3,j = 0; j < nratts;i += 5,j++)
7636 if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL))
7637 xmlFree((xmlChar *) atts[i]);
7638 }
7639 ctxt->input->cur = ctxt->input->base + cur;
7640 if (ctxt->wellFormed == 1) {
7641 goto reparse;
7642 }
7643 return(NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007644}
7645
7646/**
7647 * xmlParseEndTag2:
7648 * @ctxt: an XML parser context
7649 * @line: line of the start tag
7650 * @nsNr: number of namespaces on the start tag
7651 *
7652 * parse an end of tag
7653 *
7654 * [42] ETag ::= '</' Name S? '>'
7655 *
7656 * With namespace
7657 *
7658 * [NS 9] ETag ::= '</' QName S? '>'
7659 */
7660
7661static void
7662xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlChar *prefix,
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007663 const xmlChar *URI, int line, int nsNr, int tlen) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007664 const xmlChar *name;
7665
7666 GROW;
7667 if ((RAW != '<') || (NXT(1) != '/')) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007668 xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007669 return;
7670 }
7671 SKIP(2);
7672
William M. Brack13dfa872004-09-18 04:52:08 +00007673 if ((tlen > 0) && (xmlStrncmp(ctxt->input->cur, ctxt->name, tlen) == 0)) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007674 if (ctxt->input->cur[tlen] == '>') {
7675 ctxt->input->cur += tlen + 1;
7676 goto done;
7677 }
7678 ctxt->input->cur += tlen;
7679 name = (xmlChar*)1;
7680 } else {
7681 if (prefix == NULL)
7682 name = xmlParseNameAndCompare(ctxt, ctxt->name);
7683 else
7684 name = xmlParseQNameAndCompare(ctxt, ctxt->name, prefix);
7685 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007686
7687 /*
7688 * We should definitely be at the ending "S? '>'" part
7689 */
7690 GROW;
7691 SKIP_BLANKS;
Daniel Veillard73b013f2003-09-30 12:36:01 +00007692 if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007693 xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007694 } else
7695 NEXT1;
7696
7697 /*
7698 * [ WFC: Element Type Match ]
7699 * The Name in an element's end-tag must match the element type in the
7700 * start-tag.
7701 *
7702 */
7703 if (name != (xmlChar*)1) {
Daniel Veillardf403d292003-10-05 13:51:35 +00007704 if (name == NULL) name = BAD_CAST "unparseable";
7705 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007706 "Opening and ending tag mismatch: %s line %d and %s\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00007707 ctxt->name, line, name);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007708 }
7709
7710 /*
7711 * SAX: End of Tag
7712 */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007713done:
Daniel Veillard0fb18932003-09-07 09:14:37 +00007714 if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
7715 (!ctxt->disableSAX))
7716 ctxt->sax->endElementNs(ctxt->userData, ctxt->name, prefix, URI);
7717
Daniel Veillard0fb18932003-09-07 09:14:37 +00007718 spacePop(ctxt);
7719 if (nsNr != 0)
7720 nsPop(ctxt, nsNr);
7721 return;
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00007722}
7723
7724/**
Owen Taylor3473f882001-02-23 17:55:21 +00007725 * xmlParseCDSect:
7726 * @ctxt: an XML parser context
7727 *
7728 * Parse escaped pure raw content.
7729 *
7730 * [18] CDSect ::= CDStart CData CDEnd
7731 *
7732 * [19] CDStart ::= '<![CDATA['
7733 *
7734 * [20] Data ::= (Char* - (Char* ']]>' Char*))
7735 *
7736 * [21] CDEnd ::= ']]>'
7737 */
7738void
7739xmlParseCDSect(xmlParserCtxtPtr ctxt) {
7740 xmlChar *buf = NULL;
7741 int len = 0;
7742 int size = XML_PARSER_BUFFER_SIZE;
7743 int r, rl;
7744 int s, sl;
7745 int cur, l;
7746 int count = 0;
7747
Daniel Veillard8f597c32003-10-06 08:19:27 +00007748 /* Check 2.6.0 was NXT(0) not RAW */
Daniel Veillarda07050d2003-10-19 14:46:32 +00007749 if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007750 SKIP(9);
7751 } else
7752 return;
7753
7754 ctxt->instate = XML_PARSER_CDATA_SECTION;
7755 r = CUR_CHAR(rl);
William M. Brack871611b2003-10-18 04:53:14 +00007756 if (!IS_CHAR(r)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007757 xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007758 ctxt->instate = XML_PARSER_CONTENT;
7759 return;
7760 }
7761 NEXTL(rl);
7762 s = CUR_CHAR(sl);
William M. Brack871611b2003-10-18 04:53:14 +00007763 if (!IS_CHAR(s)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007764 xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007765 ctxt->instate = XML_PARSER_CONTENT;
7766 return;
7767 }
7768 NEXTL(sl);
7769 cur = CUR_CHAR(l);
Daniel Veillard3c908dc2003-04-19 00:07:51 +00007770 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00007771 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007772 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007773 return;
7774 }
William M. Brack871611b2003-10-18 04:53:14 +00007775 while (IS_CHAR(cur) &&
Owen Taylor3473f882001-02-23 17:55:21 +00007776 ((r != ']') || (s != ']') || (cur != '>'))) {
7777 if (len + 5 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00007778 xmlChar *tmp;
7779
Owen Taylor3473f882001-02-23 17:55:21 +00007780 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00007781 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
7782 if (tmp == NULL) {
7783 xmlFree(buf);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007784 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007785 return;
7786 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00007787 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00007788 }
7789 COPY_BUF(rl,buf,len,r);
7790 r = s;
7791 rl = sl;
7792 s = cur;
7793 sl = l;
7794 count++;
7795 if (count > 50) {
7796 GROW;
7797 count = 0;
7798 }
7799 NEXTL(l);
7800 cur = CUR_CHAR(l);
7801 }
7802 buf[len] = 0;
7803 ctxt->instate = XML_PARSER_CONTENT;
7804 if (cur != '>') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00007805 xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED,
Owen Taylor3473f882001-02-23 17:55:21 +00007806 "CData section not finished\n%.50s\n", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00007807 xmlFree(buf);
7808 return;
7809 }
7810 NEXTL(l);
7811
7812 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00007813 * OK the buffer is to be consumed as cdata.
Owen Taylor3473f882001-02-23 17:55:21 +00007814 */
7815 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
7816 if (ctxt->sax->cdataBlock != NULL)
7817 ctxt->sax->cdataBlock(ctxt->userData, buf, len);
Daniel Veillard7583a592001-07-08 13:15:55 +00007818 else if (ctxt->sax->characters != NULL)
7819 ctxt->sax->characters(ctxt->userData, buf, len);
Owen Taylor3473f882001-02-23 17:55:21 +00007820 }
7821 xmlFree(buf);
7822}
7823
7824/**
7825 * xmlParseContent:
7826 * @ctxt: an XML parser context
7827 *
7828 * Parse a content:
7829 *
7830 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
7831 */
7832
7833void
7834xmlParseContent(xmlParserCtxtPtr ctxt) {
7835 GROW;
Daniel Veillardfdc91562002-07-01 21:52:03 +00007836 while ((RAW != 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +00007837 ((RAW != '<') || (NXT(1) != '/'))) {
7838 const xmlChar *test = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00007839 unsigned int cons = ctxt->input->consumed;
Daniel Veillard21a0f912001-02-25 19:54:14 +00007840 const xmlChar *cur = ctxt->input->cur;
Owen Taylor3473f882001-02-23 17:55:21 +00007841
7842 /*
Owen Taylor3473f882001-02-23 17:55:21 +00007843 * First case : a Processing Instruction.
7844 */
Daniel Veillardfdc91562002-07-01 21:52:03 +00007845 if ((*cur == '<') && (cur[1] == '?')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007846 xmlParsePI(ctxt);
7847 }
7848
7849 /*
7850 * Second case : a CDSection
7851 */
Daniel Veillard8f597c32003-10-06 08:19:27 +00007852 /* 2.6.0 test was *cur not RAW */
Daniel Veillarda07050d2003-10-19 14:46:32 +00007853 else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007854 xmlParseCDSect(ctxt);
7855 }
7856
7857 /*
7858 * Third case : a comment
7859 */
Daniel Veillard21a0f912001-02-25 19:54:14 +00007860 else if ((*cur == '<') && (NXT(1) == '!') &&
Owen Taylor3473f882001-02-23 17:55:21 +00007861 (NXT(2) == '-') && (NXT(3) == '-')) {
7862 xmlParseComment(ctxt);
7863 ctxt->instate = XML_PARSER_CONTENT;
7864 }
7865
7866 /*
7867 * Fourth case : a sub-element.
7868 */
Daniel Veillard21a0f912001-02-25 19:54:14 +00007869 else if (*cur == '<') {
Owen Taylor3473f882001-02-23 17:55:21 +00007870 xmlParseElement(ctxt);
7871 }
7872
7873 /*
7874 * Fifth case : a reference. If if has not been resolved,
7875 * parsing returns it's Name, create the node
7876 */
7877
Daniel Veillard21a0f912001-02-25 19:54:14 +00007878 else if (*cur == '&') {
Owen Taylor3473f882001-02-23 17:55:21 +00007879 xmlParseReference(ctxt);
7880 }
7881
7882 /*
7883 * Last case, text. Note that References are handled directly.
7884 */
7885 else {
7886 xmlParseCharData(ctxt, 0);
7887 }
7888
7889 GROW;
7890 /*
7891 * Pop-up of finished entities.
7892 */
Daniel Veillard561b7f82002-03-20 21:55:57 +00007893 while ((RAW == 0) && (ctxt->inputNr > 1))
Owen Taylor3473f882001-02-23 17:55:21 +00007894 xmlPopInput(ctxt);
7895 SHRINK;
7896
Daniel Veillardfdc91562002-07-01 21:52:03 +00007897 if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007898 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
7899 "detected an error in element content\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007900 ctxt->instate = XML_PARSER_EOF;
7901 break;
7902 }
7903 }
7904}
7905
7906/**
7907 * xmlParseElement:
7908 * @ctxt: an XML parser context
7909 *
7910 * parse an XML element, this is highly recursive
7911 *
7912 * [39] element ::= EmptyElemTag | STag content ETag
7913 *
7914 * [ WFC: Element Type Match ]
7915 * The Name in an element's end-tag must match the element type in the
7916 * start-tag.
7917 *
Owen Taylor3473f882001-02-23 17:55:21 +00007918 */
7919
7920void
7921xmlParseElement(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00007922 const xmlChar *name;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007923 const xmlChar *prefix;
7924 const xmlChar *URI;
Owen Taylor3473f882001-02-23 17:55:21 +00007925 xmlParserNodeInfo node_info;
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007926 int line, tlen;
Owen Taylor3473f882001-02-23 17:55:21 +00007927 xmlNodePtr ret;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007928 int nsNr = ctxt->nsNr;
Owen Taylor3473f882001-02-23 17:55:21 +00007929
7930 /* Capture start position */
7931 if (ctxt->record_info) {
7932 node_info.begin_pos = ctxt->input->consumed +
7933 (CUR_PTR - ctxt->input->base);
7934 node_info.begin_line = ctxt->input->line;
7935 }
7936
7937 if (ctxt->spaceNr == 0)
7938 spacePush(ctxt, -1);
7939 else
7940 spacePush(ctxt, *ctxt->space);
7941
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00007942 line = ctxt->input->line;
Daniel Veillard81273902003-09-30 00:43:48 +00007943#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard0fb18932003-09-07 09:14:37 +00007944 if (ctxt->sax2)
Daniel Veillard81273902003-09-30 00:43:48 +00007945#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007946 name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen);
Daniel Veillard81273902003-09-30 00:43:48 +00007947#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard0fb18932003-09-07 09:14:37 +00007948 else
7949 name = xmlParseStartTag(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00007950#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00007951 if (name == NULL) {
7952 spacePop(ctxt);
7953 return;
7954 }
7955 namePush(ctxt, name);
7956 ret = ctxt->node;
7957
Daniel Veillard4432df22003-09-28 18:58:27 +00007958#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00007959 /*
7960 * [ VC: Root Element Type ]
7961 * The Name in the document type declaration must match the element
7962 * type of the root element.
7963 */
7964 if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
7965 ctxt->node && (ctxt->node == ctxt->myDoc->children))
7966 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillard4432df22003-09-28 18:58:27 +00007967#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00007968
7969 /*
7970 * Check for an Empty Element.
7971 */
7972 if ((RAW == '/') && (NXT(1) == '>')) {
7973 SKIP(2);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007974 if (ctxt->sax2) {
7975 if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
7976 (!ctxt->disableSAX))
7977 ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI);
Daniel Veillard81273902003-09-30 00:43:48 +00007978#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard0fb18932003-09-07 09:14:37 +00007979 } else {
7980 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
7981 (!ctxt->disableSAX))
7982 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard81273902003-09-30 00:43:48 +00007983#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00007984 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007985 namePop(ctxt);
7986 spacePop(ctxt);
7987 if (nsNr != ctxt->nsNr)
7988 nsPop(ctxt, ctxt->nsNr - nsNr);
Owen Taylor3473f882001-02-23 17:55:21 +00007989 if ( ret != NULL && ctxt->record_info ) {
7990 node_info.end_pos = ctxt->input->consumed +
7991 (CUR_PTR - ctxt->input->base);
7992 node_info.end_line = ctxt->input->line;
7993 node_info.node = ret;
7994 xmlParserAddNodeInfo(ctxt, &node_info);
7995 }
7996 return;
7997 }
7998 if (RAW == '>') {
Daniel Veillard21a0f912001-02-25 19:54:14 +00007999 NEXT1;
Owen Taylor3473f882001-02-23 17:55:21 +00008000 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00008001 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED,
8002 "Couldn't find end of Start Tag %s line %d\n",
8003 name, line, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008004
8005 /*
8006 * end of parsing of this node.
8007 */
8008 nodePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008009 namePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008010 spacePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008011 if (nsNr != ctxt->nsNr)
8012 nsPop(ctxt, ctxt->nsNr - nsNr);
Owen Taylor3473f882001-02-23 17:55:21 +00008013
8014 /*
8015 * Capture end position and add node
8016 */
8017 if ( ret != NULL && ctxt->record_info ) {
8018 node_info.end_pos = ctxt->input->consumed +
8019 (CUR_PTR - ctxt->input->base);
8020 node_info.end_line = ctxt->input->line;
8021 node_info.node = ret;
8022 xmlParserAddNodeInfo(ctxt, &node_info);
8023 }
8024 return;
8025 }
8026
8027 /*
8028 * Parse the content of the element:
8029 */
8030 xmlParseContent(ctxt);
Daniel Veillard73b013f2003-09-30 12:36:01 +00008031 if (!IS_BYTE_CHAR(RAW)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00008032 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED,
Daniel Veillard2b0f8792003-10-10 19:36:36 +00008033 "Premature end of data in tag %s line %d\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00008034 name, line, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008035
8036 /*
8037 * end of parsing of this node.
8038 */
8039 nodePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008040 namePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008041 spacePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008042 if (nsNr != ctxt->nsNr)
8043 nsPop(ctxt, ctxt->nsNr - nsNr);
Owen Taylor3473f882001-02-23 17:55:21 +00008044 return;
8045 }
8046
8047 /*
8048 * parse the end of tag: '</' should be here.
8049 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00008050 if (ctxt->sax2) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00008051 xmlParseEndTag2(ctxt, prefix, URI, line, ctxt->nsNr - nsNr, tlen);
Daniel Veillarde57ec792003-09-10 10:50:59 +00008052 namePop(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00008053 }
8054#ifdef LIBXML_SAX1_ENABLED
8055 else
Daniel Veillard0fb18932003-09-07 09:14:37 +00008056 xmlParseEndTag1(ctxt, line);
Daniel Veillard81273902003-09-30 00:43:48 +00008057#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00008058
8059 /*
8060 * Capture end position and add node
8061 */
8062 if ( ret != NULL && ctxt->record_info ) {
8063 node_info.end_pos = ctxt->input->consumed +
8064 (CUR_PTR - ctxt->input->base);
8065 node_info.end_line = ctxt->input->line;
8066 node_info.node = ret;
8067 xmlParserAddNodeInfo(ctxt, &node_info);
8068 }
8069}
8070
8071/**
8072 * xmlParseVersionNum:
8073 * @ctxt: an XML parser context
8074 *
8075 * parse the XML version value.
8076 *
8077 * [26] VersionNum ::= ([a-zA-Z0-9_.:] | '-')+
8078 *
8079 * Returns the string giving the XML version number, or NULL
8080 */
8081xmlChar *
8082xmlParseVersionNum(xmlParserCtxtPtr ctxt) {
8083 xmlChar *buf = NULL;
8084 int len = 0;
8085 int size = 10;
8086 xmlChar cur;
8087
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 cur = CUR;
8094 while (((cur >= 'a') && (cur <= 'z')) ||
8095 ((cur >= 'A') && (cur <= 'Z')) ||
8096 ((cur >= '0') && (cur <= '9')) ||
8097 (cur == '_') || (cur == '.') ||
8098 (cur == ':') || (cur == '-')) {
8099 if (len + 1 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00008100 xmlChar *tmp;
8101
Owen Taylor3473f882001-02-23 17:55:21 +00008102 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00008103 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
8104 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008105 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008106 return(NULL);
8107 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00008108 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00008109 }
8110 buf[len++] = cur;
8111 NEXT;
8112 cur=CUR;
8113 }
8114 buf[len] = 0;
8115 return(buf);
8116}
8117
8118/**
8119 * xmlParseVersionInfo:
8120 * @ctxt: an XML parser context
8121 *
8122 * parse the XML version.
8123 *
8124 * [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ")
8125 *
8126 * [25] Eq ::= S? '=' S?
8127 *
8128 * Returns the version string, e.g. "1.0"
8129 */
8130
8131xmlChar *
8132xmlParseVersionInfo(xmlParserCtxtPtr ctxt) {
8133 xmlChar *version = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00008134
Daniel Veillarda07050d2003-10-19 14:46:32 +00008135 if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008136 SKIP(7);
8137 SKIP_BLANKS;
8138 if (RAW != '=') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008139 xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008140 return(NULL);
8141 }
8142 NEXT;
8143 SKIP_BLANKS;
8144 if (RAW == '"') {
8145 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008146 version = xmlParseVersionNum(ctxt);
8147 if (RAW != '"') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008148 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008149 } else
8150 NEXT;
8151 } else if (RAW == '\''){
8152 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008153 version = xmlParseVersionNum(ctxt);
8154 if (RAW != '\'') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008155 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008156 } else
8157 NEXT;
8158 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008159 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008160 }
8161 }
8162 return(version);
8163}
8164
8165/**
8166 * xmlParseEncName:
8167 * @ctxt: an XML parser context
8168 *
8169 * parse the XML encoding name
8170 *
8171 * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
8172 *
8173 * Returns the encoding name value or NULL
8174 */
8175xmlChar *
8176xmlParseEncName(xmlParserCtxtPtr ctxt) {
8177 xmlChar *buf = NULL;
8178 int len = 0;
8179 int size = 10;
8180 xmlChar cur;
8181
8182 cur = CUR;
8183 if (((cur >= 'a') && (cur <= 'z')) ||
8184 ((cur >= 'A') && (cur <= 'Z'))) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008185 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00008186 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008187 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008188 return(NULL);
8189 }
8190
8191 buf[len++] = cur;
8192 NEXT;
8193 cur = CUR;
8194 while (((cur >= 'a') && (cur <= 'z')) ||
8195 ((cur >= 'A') && (cur <= 'Z')) ||
8196 ((cur >= '0') && (cur <= '9')) ||
8197 (cur == '.') || (cur == '_') ||
8198 (cur == '-')) {
8199 if (len + 1 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00008200 xmlChar *tmp;
8201
Owen Taylor3473f882001-02-23 17:55:21 +00008202 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00008203 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
8204 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008205 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00008206 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00008207 return(NULL);
8208 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00008209 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00008210 }
8211 buf[len++] = cur;
8212 NEXT;
8213 cur = CUR;
8214 if (cur == 0) {
8215 SHRINK;
8216 GROW;
8217 cur = CUR;
8218 }
8219 }
8220 buf[len] = 0;
8221 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008222 xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008223 }
8224 return(buf);
8225}
8226
8227/**
8228 * xmlParseEncodingDecl:
8229 * @ctxt: an XML parser context
8230 *
8231 * parse the XML encoding declaration
8232 *
8233 * [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'")
8234 *
8235 * this setups the conversion filters.
8236 *
8237 * Returns the encoding value or NULL
8238 */
8239
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008240const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00008241xmlParseEncodingDecl(xmlParserCtxtPtr ctxt) {
8242 xmlChar *encoding = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00008243
8244 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008245 if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008246 SKIP(8);
8247 SKIP_BLANKS;
8248 if (RAW != '=') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008249 xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008250 return(NULL);
8251 }
8252 NEXT;
8253 SKIP_BLANKS;
8254 if (RAW == '"') {
8255 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008256 encoding = xmlParseEncName(ctxt);
8257 if (RAW != '"') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008258 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008259 } else
8260 NEXT;
8261 } else if (RAW == '\''){
8262 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008263 encoding = xmlParseEncName(ctxt);
8264 if (RAW != '\'') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008265 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008266 } else
8267 NEXT;
Daniel Veillard82ac6b02002-02-17 23:18:55 +00008268 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008269 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008270 }
Daniel Veillard6b621b82003-08-11 15:03:34 +00008271 /*
8272 * UTF-16 encoding stwich has already taken place at this stage,
8273 * more over the little-endian/big-endian selection is already done
8274 */
8275 if ((encoding != NULL) &&
8276 ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-16")) ||
8277 (!xmlStrcasecmp(encoding, BAD_CAST "UTF16")))) {
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008278 if (ctxt->encoding != NULL)
8279 xmlFree((xmlChar *) ctxt->encoding);
8280 ctxt->encoding = encoding;
Daniel Veillardb19ba832003-08-14 00:33:46 +00008281 }
8282 /*
8283 * UTF-8 encoding is handled natively
8284 */
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008285 else if ((encoding != NULL) &&
Daniel Veillardb19ba832003-08-14 00:33:46 +00008286 ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-8")) ||
8287 (!xmlStrcasecmp(encoding, BAD_CAST "UTF8")))) {
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008288 if (ctxt->encoding != NULL)
8289 xmlFree((xmlChar *) ctxt->encoding);
8290 ctxt->encoding = encoding;
Daniel Veillard6b621b82003-08-11 15:03:34 +00008291 }
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008292 else if (encoding != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00008293 xmlCharEncodingHandlerPtr handler;
8294
8295 if (ctxt->input->encoding != NULL)
8296 xmlFree((xmlChar *) ctxt->input->encoding);
8297 ctxt->input->encoding = encoding;
8298
Daniel Veillarda6874ca2003-07-29 16:47:24 +00008299 handler = xmlFindCharEncodingHandler((const char *) encoding);
8300 if (handler != NULL) {
8301 xmlSwitchToEncoding(ctxt, handler);
Owen Taylor3473f882001-02-23 17:55:21 +00008302 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00008303 xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillarda6874ca2003-07-29 16:47:24 +00008304 "Unsupported encoding %s\n", encoding);
8305 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008306 }
8307 }
8308 }
8309 return(encoding);
8310}
8311
8312/**
8313 * xmlParseSDDecl:
8314 * @ctxt: an XML parser context
8315 *
8316 * parse the XML standalone declaration
8317 *
8318 * [32] SDDecl ::= S 'standalone' Eq
8319 * (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"'))
8320 *
8321 * [ VC: Standalone Document Declaration ]
8322 * TODO The standalone document declaration must have the value "no"
8323 * if any external markup declarations contain declarations of:
8324 * - attributes with default values, if elements to which these
8325 * attributes apply appear in the document without specifications
8326 * of values for these attributes, or
8327 * - entities (other than amp, lt, gt, apos, quot), if references
8328 * to those entities appear in the document, or
8329 * - attributes with values subject to normalization, where the
8330 * attribute appears in the document with a value which will change
8331 * as a result of normalization, or
8332 * - element types with element content, if white space occurs directly
8333 * within any instance of those types.
8334 *
8335 * Returns 1 if standalone, 0 otherwise
8336 */
8337
8338int
8339xmlParseSDDecl(xmlParserCtxtPtr ctxt) {
8340 int standalone = -1;
8341
8342 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008343 if (CMP10(CUR_PTR, 's', 't', 'a', 'n', 'd', 'a', 'l', 'o', 'n', 'e')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008344 SKIP(10);
8345 SKIP_BLANKS;
8346 if (RAW != '=') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008347 xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008348 return(standalone);
8349 }
8350 NEXT;
8351 SKIP_BLANKS;
8352 if (RAW == '\''){
8353 NEXT;
8354 if ((RAW == 'n') && (NXT(1) == 'o')) {
8355 standalone = 0;
8356 SKIP(2);
8357 } else if ((RAW == 'y') && (NXT(1) == 'e') &&
8358 (NXT(2) == 's')) {
8359 standalone = 1;
8360 SKIP(3);
8361 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008362 xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008363 }
8364 if (RAW != '\'') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008365 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008366 } else
8367 NEXT;
8368 } else if (RAW == '"'){
8369 NEXT;
8370 if ((RAW == 'n') && (NXT(1) == 'o')) {
8371 standalone = 0;
8372 SKIP(2);
8373 } else if ((RAW == 'y') && (NXT(1) == 'e') &&
8374 (NXT(2) == 's')) {
8375 standalone = 1;
8376 SKIP(3);
8377 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008378 xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008379 }
8380 if (RAW != '"') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008381 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008382 } else
8383 NEXT;
8384 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008385 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008386 }
8387 }
8388 return(standalone);
8389}
8390
8391/**
8392 * xmlParseXMLDecl:
8393 * @ctxt: an XML parser context
8394 *
8395 * parse an XML declaration header
8396 *
8397 * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
8398 */
8399
8400void
8401xmlParseXMLDecl(xmlParserCtxtPtr ctxt) {
8402 xmlChar *version;
8403
8404 /*
8405 * We know that '<?xml' is here.
8406 */
8407 SKIP(5);
8408
William M. Brack76e95df2003-10-18 16:20:14 +00008409 if (!IS_BLANK_CH(RAW)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008410 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
8411 "Blank needed after '<?xml'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008412 }
8413 SKIP_BLANKS;
8414
8415 /*
Daniel Veillard19840942001-11-29 16:11:38 +00008416 * We must have the VersionInfo here.
Owen Taylor3473f882001-02-23 17:55:21 +00008417 */
8418 version = xmlParseVersionInfo(ctxt);
Daniel Veillard19840942001-11-29 16:11:38 +00008419 if (version == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008420 xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL);
Daniel Veillard19840942001-11-29 16:11:38 +00008421 } else {
8422 if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) {
8423 /*
8424 * TODO: Blueberry should be detected here
8425 */
Daniel Veillard24eb9782003-10-04 21:08:09 +00008426 xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION,
8427 "Unsupported version '%s'\n",
8428 version, NULL);
Daniel Veillard19840942001-11-29 16:11:38 +00008429 }
8430 if (ctxt->version != NULL)
Daniel Veillardd3b08822001-12-05 12:03:33 +00008431 xmlFree((void *) ctxt->version);
Daniel Veillard19840942001-11-29 16:11:38 +00008432 ctxt->version = version;
Daniel Veillarda050d232001-09-05 15:51:05 +00008433 }
Owen Taylor3473f882001-02-23 17:55:21 +00008434
8435 /*
8436 * We may have the encoding declaration
8437 */
William M. Brack76e95df2003-10-18 16:20:14 +00008438 if (!IS_BLANK_CH(RAW)) {
Owen Taylor3473f882001-02-23 17:55:21 +00008439 if ((RAW == '?') && (NXT(1) == '>')) {
8440 SKIP(2);
8441 return;
8442 }
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008443 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008444 }
8445 xmlParseEncodingDecl(ctxt);
8446 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
8447 /*
8448 * The XML REC instructs us to stop parsing right here
8449 */
8450 return;
8451 }
8452
8453 /*
8454 * We may have the standalone status.
8455 */
William M. Brack76e95df2003-10-18 16:20:14 +00008456 if ((ctxt->input->encoding != NULL) && (!IS_BLANK_CH(RAW))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008457 if ((RAW == '?') && (NXT(1) == '>')) {
8458 SKIP(2);
8459 return;
8460 }
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008461 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008462 }
8463 SKIP_BLANKS;
8464 ctxt->input->standalone = xmlParseSDDecl(ctxt);
8465
8466 SKIP_BLANKS;
8467 if ((RAW == '?') && (NXT(1) == '>')) {
8468 SKIP(2);
8469 } else if (RAW == '>') {
8470 /* Deprecated old WD ... */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008471 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008472 NEXT;
8473 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008474 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008475 MOVETO_ENDTAG(CUR_PTR);
8476 NEXT;
8477 }
8478}
8479
8480/**
8481 * xmlParseMisc:
8482 * @ctxt: an XML parser context
8483 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00008484 * parse an XML Misc* optional field.
Owen Taylor3473f882001-02-23 17:55:21 +00008485 *
8486 * [27] Misc ::= Comment | PI | S
8487 */
8488
8489void
8490xmlParseMisc(xmlParserCtxtPtr ctxt) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00008491 while (((RAW == '<') && (NXT(1) == '?')) ||
Daniel Veillarda07050d2003-10-19 14:46:32 +00008492 (CMP4(CUR_PTR, '<', '!', '-', '-')) ||
William M. Brack76e95df2003-10-18 16:20:14 +00008493 IS_BLANK_CH(CUR)) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00008494 if ((RAW == '<') && (NXT(1) == '?')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008495 xmlParsePI(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00008496 } else if (IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00008497 NEXT;
8498 } else
8499 xmlParseComment(ctxt);
8500 }
8501}
8502
8503/**
8504 * xmlParseDocument:
8505 * @ctxt: an XML parser context
8506 *
8507 * parse an XML document (and build a tree if using the standard SAX
8508 * interface).
8509 *
8510 * [1] document ::= prolog element Misc*
8511 *
8512 * [22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)?
8513 *
8514 * Returns 0, -1 in case of error. the parser context is augmented
8515 * as a result of the parsing.
8516 */
8517
8518int
8519xmlParseDocument(xmlParserCtxtPtr ctxt) {
8520 xmlChar start[4];
8521 xmlCharEncoding enc;
8522
8523 xmlInitParser();
8524
8525 GROW;
8526
8527 /*
Daniel Veillard0fb18932003-09-07 09:14:37 +00008528 * SAX: detecting the level.
8529 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00008530 xmlDetectSAX2(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008531
8532 /*
Owen Taylor3473f882001-02-23 17:55:21 +00008533 * SAX: beginning of the document processing.
8534 */
8535 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
8536 ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
8537
Daniel Veillard4aede2e2003-10-17 12:43:59 +00008538 if ((ctxt->encoding == (const xmlChar *)XML_CHAR_ENCODING_NONE) &&
8539 ((ctxt->input->end - ctxt->input->cur) >= 4)) {
Daniel Veillard4aafa792001-07-28 17:21:12 +00008540 /*
8541 * Get the 4 first bytes and decode the charset
8542 * if enc != XML_CHAR_ENCODING_NONE
8543 * plug some encoding conversion routines.
8544 */
8545 start[0] = RAW;
8546 start[1] = NXT(1);
8547 start[2] = NXT(2);
8548 start[3] = NXT(3);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00008549 enc = xmlDetectCharEncoding(&start[0], 4);
Daniel Veillard4aafa792001-07-28 17:21:12 +00008550 if (enc != XML_CHAR_ENCODING_NONE) {
8551 xmlSwitchEncoding(ctxt, enc);
8552 }
Owen Taylor3473f882001-02-23 17:55:21 +00008553 }
8554
8555
8556 if (CUR == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008557 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008558 }
8559
8560 /*
8561 * Check for the XMLDecl in the Prolog.
8562 */
8563 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008564 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008565
8566 /*
8567 * Note that we will switch encoding on the fly.
8568 */
8569 xmlParseXMLDecl(ctxt);
8570 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
8571 /*
8572 * The XML REC instructs us to stop parsing right here
8573 */
8574 return(-1);
8575 }
8576 ctxt->standalone = ctxt->input->standalone;
8577 SKIP_BLANKS;
8578 } else {
8579 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
8580 }
8581 if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
8582 ctxt->sax->startDocument(ctxt->userData);
8583
8584 /*
8585 * The Misc part of the Prolog
8586 */
8587 GROW;
8588 xmlParseMisc(ctxt);
8589
8590 /*
8591 * Then possibly doc type declaration(s) and more Misc
8592 * (doctypedecl Misc*)?
8593 */
8594 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008595 if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008596
8597 ctxt->inSubset = 1;
8598 xmlParseDocTypeDecl(ctxt);
8599 if (RAW == '[') {
8600 ctxt->instate = XML_PARSER_DTD;
8601 xmlParseInternalSubset(ctxt);
8602 }
8603
8604 /*
8605 * Create and update the external subset.
8606 */
8607 ctxt->inSubset = 2;
8608 if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) &&
8609 (!ctxt->disableSAX))
8610 ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
8611 ctxt->extSubSystem, ctxt->extSubURI);
8612 ctxt->inSubset = 0;
8613
8614
8615 ctxt->instate = XML_PARSER_PROLOG;
8616 xmlParseMisc(ctxt);
8617 }
8618
8619 /*
8620 * Time to start parsing the tree itself
8621 */
8622 GROW;
8623 if (RAW != '<') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008624 xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY,
8625 "Start tag expected, '<' not found\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008626 } else {
8627 ctxt->instate = XML_PARSER_CONTENT;
8628 xmlParseElement(ctxt);
8629 ctxt->instate = XML_PARSER_EPILOG;
8630
8631
8632 /*
8633 * The Misc part at the end
8634 */
8635 xmlParseMisc(ctxt);
8636
Daniel Veillard561b7f82002-03-20 21:55:57 +00008637 if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008638 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008639 }
8640 ctxt->instate = XML_PARSER_EOF;
8641 }
8642
8643 /*
8644 * SAX: end of the document processing.
8645 */
Daniel Veillard8d24cc12002-03-05 15:41:29 +00008646 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00008647 ctxt->sax->endDocument(ctxt->userData);
8648
Daniel Veillard5997aca2002-03-18 18:36:20 +00008649 /*
8650 * Remove locally kept entity definitions if the tree was not built
8651 */
8652 if ((ctxt->myDoc != NULL) &&
8653 (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) {
8654 xmlFreeDoc(ctxt->myDoc);
8655 ctxt->myDoc = NULL;
8656 }
8657
Daniel Veillardc7612992002-02-17 22:47:37 +00008658 if (! ctxt->wellFormed) {
8659 ctxt->valid = 0;
8660 return(-1);
8661 }
Owen Taylor3473f882001-02-23 17:55:21 +00008662 return(0);
8663}
8664
8665/**
8666 * xmlParseExtParsedEnt:
8667 * @ctxt: an XML parser context
8668 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00008669 * parse a general parsed entity
Owen Taylor3473f882001-02-23 17:55:21 +00008670 * An external general parsed entity is well-formed if it matches the
8671 * production labeled extParsedEnt.
8672 *
8673 * [78] extParsedEnt ::= TextDecl? content
8674 *
8675 * Returns 0, -1 in case of error. the parser context is augmented
8676 * as a result of the parsing.
8677 */
8678
8679int
8680xmlParseExtParsedEnt(xmlParserCtxtPtr ctxt) {
8681 xmlChar start[4];
8682 xmlCharEncoding enc;
8683
8684 xmlDefaultSAXHandlerInit();
8685
Daniel Veillard309f81d2003-09-23 09:02:53 +00008686 xmlDetectSAX2(ctxt);
8687
Owen Taylor3473f882001-02-23 17:55:21 +00008688 GROW;
8689
8690 /*
8691 * SAX: beginning of the document processing.
8692 */
8693 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
8694 ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
8695
8696 /*
8697 * Get the 4 first bytes and decode the charset
8698 * if enc != XML_CHAR_ENCODING_NONE
8699 * plug some encoding conversion routines.
8700 */
Daniel Veillard4aede2e2003-10-17 12:43:59 +00008701 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
8702 start[0] = RAW;
8703 start[1] = NXT(1);
8704 start[2] = NXT(2);
8705 start[3] = NXT(3);
8706 enc = xmlDetectCharEncoding(start, 4);
8707 if (enc != XML_CHAR_ENCODING_NONE) {
8708 xmlSwitchEncoding(ctxt, enc);
8709 }
Owen Taylor3473f882001-02-23 17:55:21 +00008710 }
8711
8712
8713 if (CUR == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008714 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008715 }
8716
8717 /*
8718 * Check for the XMLDecl in the Prolog.
8719 */
8720 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008721 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008722
8723 /*
8724 * Note that we will switch encoding on the fly.
8725 */
8726 xmlParseXMLDecl(ctxt);
8727 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
8728 /*
8729 * The XML REC instructs us to stop parsing right here
8730 */
8731 return(-1);
8732 }
8733 SKIP_BLANKS;
8734 } else {
8735 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
8736 }
8737 if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
8738 ctxt->sax->startDocument(ctxt->userData);
8739
8740 /*
8741 * Doing validity checking on chunk doesn't make sense
8742 */
8743 ctxt->instate = XML_PARSER_CONTENT;
8744 ctxt->validate = 0;
8745 ctxt->loadsubset = 0;
8746 ctxt->depth = 0;
8747
8748 xmlParseContent(ctxt);
8749
8750 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008751 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008752 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008753 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008754 }
8755
8756 /*
8757 * SAX: end of the document processing.
8758 */
Daniel Veillard8d24cc12002-03-05 15:41:29 +00008759 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00008760 ctxt->sax->endDocument(ctxt->userData);
8761
8762 if (! ctxt->wellFormed) return(-1);
8763 return(0);
8764}
8765
Daniel Veillard73b013f2003-09-30 12:36:01 +00008766#ifdef LIBXML_PUSH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00008767/************************************************************************
8768 * *
8769 * Progressive parsing interfaces *
8770 * *
8771 ************************************************************************/
8772
8773/**
8774 * xmlParseLookupSequence:
8775 * @ctxt: an XML parser context
8776 * @first: the first char to lookup
8777 * @next: the next char to lookup or zero
8778 * @third: the next char to lookup or zero
8779 *
8780 * Try to find if a sequence (first, next, third) or just (first next) or
8781 * (first) is available in the input stream.
8782 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
8783 * to avoid rescanning sequences of bytes, it DOES change the state of the
8784 * parser, do not use liberally.
8785 *
8786 * Returns the index to the current parsing point if the full sequence
8787 * is available, -1 otherwise.
8788 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008789static int
Owen Taylor3473f882001-02-23 17:55:21 +00008790xmlParseLookupSequence(xmlParserCtxtPtr ctxt, xmlChar first,
8791 xmlChar next, xmlChar third) {
8792 int base, len;
8793 xmlParserInputPtr in;
8794 const xmlChar *buf;
8795
8796 in = ctxt->input;
8797 if (in == NULL) return(-1);
8798 base = in->cur - in->base;
8799 if (base < 0) return(-1);
8800 if (ctxt->checkIndex > base)
8801 base = ctxt->checkIndex;
8802 if (in->buf == NULL) {
8803 buf = in->base;
8804 len = in->length;
8805 } else {
8806 buf = in->buf->buffer->content;
8807 len = in->buf->buffer->use;
8808 }
8809 /* take into account the sequence length */
8810 if (third) len -= 2;
8811 else if (next) len --;
8812 for (;base < len;base++) {
8813 if (buf[base] == first) {
8814 if (third != 0) {
8815 if ((buf[base + 1] != next) ||
8816 (buf[base + 2] != third)) continue;
8817 } else if (next != 0) {
8818 if (buf[base + 1] != next) continue;
8819 }
8820 ctxt->checkIndex = 0;
8821#ifdef DEBUG_PUSH
8822 if (next == 0)
8823 xmlGenericError(xmlGenericErrorContext,
8824 "PP: lookup '%c' found at %d\n",
8825 first, base);
8826 else if (third == 0)
8827 xmlGenericError(xmlGenericErrorContext,
8828 "PP: lookup '%c%c' found at %d\n",
8829 first, next, base);
8830 else
8831 xmlGenericError(xmlGenericErrorContext,
8832 "PP: lookup '%c%c%c' found at %d\n",
8833 first, next, third, base);
8834#endif
8835 return(base - (in->cur - in->base));
8836 }
8837 }
8838 ctxt->checkIndex = base;
8839#ifdef DEBUG_PUSH
8840 if (next == 0)
8841 xmlGenericError(xmlGenericErrorContext,
8842 "PP: lookup '%c' failed\n", first);
8843 else if (third == 0)
8844 xmlGenericError(xmlGenericErrorContext,
8845 "PP: lookup '%c%c' failed\n", first, next);
8846 else
8847 xmlGenericError(xmlGenericErrorContext,
8848 "PP: lookup '%c%c%c' failed\n", first, next, third);
8849#endif
8850 return(-1);
8851}
8852
8853/**
Daniel Veillarda880b122003-04-21 21:36:41 +00008854 * xmlParseGetLasts:
8855 * @ctxt: an XML parser context
8856 * @lastlt: pointer to store the last '<' from the input
8857 * @lastgt: pointer to store the last '>' from the input
8858 *
8859 * Lookup the last < and > in the current chunk
8860 */
8861static void
8862xmlParseGetLasts(xmlParserCtxtPtr ctxt, const xmlChar **lastlt,
8863 const xmlChar **lastgt) {
8864 const xmlChar *tmp;
8865
8866 if ((ctxt == NULL) || (lastlt == NULL) || (lastgt == NULL)) {
8867 xmlGenericError(xmlGenericErrorContext,
8868 "Internal error: xmlParseGetLasts\n");
8869 return;
8870 }
Daniel Veillard0df3bc32004-06-08 12:03:41 +00008871 if ((ctxt->progressive != 0) && (ctxt->inputNr == 1)) {
Daniel Veillarda880b122003-04-21 21:36:41 +00008872 tmp = ctxt->input->end;
8873 tmp--;
Daniel Veillardeb70f932004-07-05 16:46:09 +00008874 while ((tmp >= ctxt->input->base) && (*tmp != '<')) tmp--;
Daniel Veillarda880b122003-04-21 21:36:41 +00008875 if (tmp < ctxt->input->base) {
8876 *lastlt = NULL;
8877 *lastgt = NULL;
Daniel Veillarda880b122003-04-21 21:36:41 +00008878 } else {
Daniel Veillardeb70f932004-07-05 16:46:09 +00008879 *lastlt = tmp;
8880 tmp++;
8881 while ((tmp < ctxt->input->end) && (*tmp != '>')) {
8882 if (*tmp == '\'') {
8883 tmp++;
8884 while ((tmp < ctxt->input->end) && (*tmp != '\'')) tmp++;
8885 if (tmp < ctxt->input->end) tmp++;
8886 } else if (*tmp == '"') {
8887 tmp++;
8888 while ((tmp < ctxt->input->end) && (*tmp != '"')) tmp++;
8889 if (tmp < ctxt->input->end) tmp++;
8890 } else
8891 tmp++;
8892 }
8893 if (tmp < ctxt->input->end)
8894 *lastgt = tmp;
8895 else {
8896 tmp = *lastlt;
8897 tmp--;
8898 while ((tmp >= ctxt->input->base) && (*tmp != '>')) tmp--;
8899 if (tmp >= ctxt->input->base)
8900 *lastgt = tmp;
8901 else
8902 *lastgt = NULL;
8903 }
Daniel Veillarda880b122003-04-21 21:36:41 +00008904 }
Daniel Veillarda880b122003-04-21 21:36:41 +00008905 } else {
8906 *lastlt = NULL;
8907 *lastgt = NULL;
8908 }
8909}
8910/**
Owen Taylor3473f882001-02-23 17:55:21 +00008911 * xmlParseTryOrFinish:
8912 * @ctxt: an XML parser context
8913 * @terminate: last chunk indicator
8914 *
8915 * Try to progress on parsing
8916 *
8917 * Returns zero if no parsing was possible
8918 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008919static int
Owen Taylor3473f882001-02-23 17:55:21 +00008920xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) {
8921 int ret = 0;
Daniel Veillardc82c57e2004-01-12 16:24:34 +00008922 int avail, tlen;
Owen Taylor3473f882001-02-23 17:55:21 +00008923 xmlChar cur, next;
Daniel Veillarda880b122003-04-21 21:36:41 +00008924 const xmlChar *lastlt, *lastgt;
Owen Taylor3473f882001-02-23 17:55:21 +00008925
8926#ifdef DEBUG_PUSH
8927 switch (ctxt->instate) {
8928 case XML_PARSER_EOF:
8929 xmlGenericError(xmlGenericErrorContext,
8930 "PP: try EOF\n"); break;
8931 case XML_PARSER_START:
8932 xmlGenericError(xmlGenericErrorContext,
8933 "PP: try START\n"); break;
8934 case XML_PARSER_MISC:
8935 xmlGenericError(xmlGenericErrorContext,
8936 "PP: try MISC\n");break;
8937 case XML_PARSER_COMMENT:
8938 xmlGenericError(xmlGenericErrorContext,
8939 "PP: try COMMENT\n");break;
8940 case XML_PARSER_PROLOG:
8941 xmlGenericError(xmlGenericErrorContext,
8942 "PP: try PROLOG\n");break;
8943 case XML_PARSER_START_TAG:
8944 xmlGenericError(xmlGenericErrorContext,
8945 "PP: try START_TAG\n");break;
8946 case XML_PARSER_CONTENT:
8947 xmlGenericError(xmlGenericErrorContext,
8948 "PP: try CONTENT\n");break;
8949 case XML_PARSER_CDATA_SECTION:
8950 xmlGenericError(xmlGenericErrorContext,
8951 "PP: try CDATA_SECTION\n");break;
8952 case XML_PARSER_END_TAG:
8953 xmlGenericError(xmlGenericErrorContext,
8954 "PP: try END_TAG\n");break;
8955 case XML_PARSER_ENTITY_DECL:
8956 xmlGenericError(xmlGenericErrorContext,
8957 "PP: try ENTITY_DECL\n");break;
8958 case XML_PARSER_ENTITY_VALUE:
8959 xmlGenericError(xmlGenericErrorContext,
8960 "PP: try ENTITY_VALUE\n");break;
8961 case XML_PARSER_ATTRIBUTE_VALUE:
8962 xmlGenericError(xmlGenericErrorContext,
8963 "PP: try ATTRIBUTE_VALUE\n");break;
8964 case XML_PARSER_DTD:
8965 xmlGenericError(xmlGenericErrorContext,
8966 "PP: try DTD\n");break;
8967 case XML_PARSER_EPILOG:
8968 xmlGenericError(xmlGenericErrorContext,
8969 "PP: try EPILOG\n");break;
8970 case XML_PARSER_PI:
8971 xmlGenericError(xmlGenericErrorContext,
8972 "PP: try PI\n");break;
8973 case XML_PARSER_IGNORE:
8974 xmlGenericError(xmlGenericErrorContext,
8975 "PP: try IGNORE\n");break;
8976 }
8977#endif
8978
Daniel Veillard198c1bf2003-10-20 17:07:41 +00008979 if ((ctxt->input != NULL) &&
8980 (ctxt->input->cur - ctxt->input->base > 4096)) {
Daniel Veillarda880b122003-04-21 21:36:41 +00008981 xmlSHRINK(ctxt);
8982 ctxt->checkIndex = 0;
8983 }
8984 xmlParseGetLasts(ctxt, &lastlt, &lastgt);
Aleksey Sanine48a3182002-05-09 18:20:01 +00008985
Daniel Veillarda880b122003-04-21 21:36:41 +00008986 while (1) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00008987 if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
8988 return(0);
8989
8990
Owen Taylor3473f882001-02-23 17:55:21 +00008991 /*
8992 * Pop-up of finished entities.
8993 */
8994 while ((RAW == 0) && (ctxt->inputNr > 1))
8995 xmlPopInput(ctxt);
8996
Daniel Veillard198c1bf2003-10-20 17:07:41 +00008997 if (ctxt->input == NULL) break;
Owen Taylor3473f882001-02-23 17:55:21 +00008998 if (ctxt->input->buf == NULL)
Daniel Veillarda880b122003-04-21 21:36:41 +00008999 avail = ctxt->input->length -
9000 (ctxt->input->cur - ctxt->input->base);
Daniel Veillard158a4d22002-02-20 22:17:58 +00009001 else {
9002 /*
9003 * If we are operating on converted input, try to flush
9004 * remainng chars to avoid them stalling in the non-converted
9005 * buffer.
9006 */
9007 if ((ctxt->input->buf->raw != NULL) &&
9008 (ctxt->input->buf->raw->use > 0)) {
9009 int base = ctxt->input->base -
9010 ctxt->input->buf->buffer->content;
9011 int current = ctxt->input->cur - ctxt->input->base;
9012
9013 xmlParserInputBufferPush(ctxt->input->buf, 0, "");
9014 ctxt->input->base = ctxt->input->buf->buffer->content + base;
9015 ctxt->input->cur = ctxt->input->base + current;
9016 ctxt->input->end =
9017 &ctxt->input->buf->buffer->content[
9018 ctxt->input->buf->buffer->use];
9019 }
9020 avail = ctxt->input->buf->buffer->use -
9021 (ctxt->input->cur - ctxt->input->base);
9022 }
Owen Taylor3473f882001-02-23 17:55:21 +00009023 if (avail < 1)
9024 goto done;
9025 switch (ctxt->instate) {
9026 case XML_PARSER_EOF:
9027 /*
9028 * Document parsing is done !
9029 */
9030 goto done;
9031 case XML_PARSER_START:
Daniel Veillard0e4cd172001-06-28 12:13:56 +00009032 if (ctxt->charset == XML_CHAR_ENCODING_NONE) {
9033 xmlChar start[4];
9034 xmlCharEncoding enc;
9035
9036 /*
9037 * Very first chars read from the document flow.
9038 */
9039 if (avail < 4)
9040 goto done;
9041
9042 /*
9043 * Get the 4 first bytes and decode the charset
9044 * if enc != XML_CHAR_ENCODING_NONE
9045 * plug some encoding conversion routines.
9046 */
9047 start[0] = RAW;
9048 start[1] = NXT(1);
9049 start[2] = NXT(2);
9050 start[3] = NXT(3);
9051 enc = xmlDetectCharEncoding(start, 4);
9052 if (enc != XML_CHAR_ENCODING_NONE) {
9053 xmlSwitchEncoding(ctxt, enc);
9054 }
9055 break;
9056 }
Owen Taylor3473f882001-02-23 17:55:21 +00009057
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00009058 if (avail < 2)
9059 goto done;
Owen Taylor3473f882001-02-23 17:55:21 +00009060 cur = ctxt->input->cur[0];
9061 next = ctxt->input->cur[1];
9062 if (cur == 0) {
9063 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
9064 ctxt->sax->setDocumentLocator(ctxt->userData,
9065 &xmlDefaultSAXLocator);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009066 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00009067 ctxt->instate = XML_PARSER_EOF;
9068#ifdef DEBUG_PUSH
9069 xmlGenericError(xmlGenericErrorContext,
9070 "PP: entering EOF\n");
9071#endif
9072 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
9073 ctxt->sax->endDocument(ctxt->userData);
9074 goto done;
9075 }
9076 if ((cur == '<') && (next == '?')) {
9077 /* PI or XML decl */
9078 if (avail < 5) return(ret);
9079 if ((!terminate) &&
9080 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9081 return(ret);
9082 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
9083 ctxt->sax->setDocumentLocator(ctxt->userData,
9084 &xmlDefaultSAXLocator);
9085 if ((ctxt->input->cur[2] == 'x') &&
9086 (ctxt->input->cur[3] == 'm') &&
9087 (ctxt->input->cur[4] == 'l') &&
William M. Brack76e95df2003-10-18 16:20:14 +00009088 (IS_BLANK_CH(ctxt->input->cur[5]))) {
Owen Taylor3473f882001-02-23 17:55:21 +00009089 ret += 5;
9090#ifdef DEBUG_PUSH
9091 xmlGenericError(xmlGenericErrorContext,
9092 "PP: Parsing XML Decl\n");
9093#endif
9094 xmlParseXMLDecl(ctxt);
9095 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
9096 /*
9097 * The XML REC instructs us to stop parsing right
9098 * here
9099 */
9100 ctxt->instate = XML_PARSER_EOF;
9101 return(0);
9102 }
9103 ctxt->standalone = ctxt->input->standalone;
9104 if ((ctxt->encoding == NULL) &&
9105 (ctxt->input->encoding != NULL))
9106 ctxt->encoding = xmlStrdup(ctxt->input->encoding);
9107 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
9108 (!ctxt->disableSAX))
9109 ctxt->sax->startDocument(ctxt->userData);
9110 ctxt->instate = XML_PARSER_MISC;
9111#ifdef DEBUG_PUSH
9112 xmlGenericError(xmlGenericErrorContext,
9113 "PP: entering MISC\n");
9114#endif
9115 } else {
9116 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
9117 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
9118 (!ctxt->disableSAX))
9119 ctxt->sax->startDocument(ctxt->userData);
9120 ctxt->instate = XML_PARSER_MISC;
9121#ifdef DEBUG_PUSH
9122 xmlGenericError(xmlGenericErrorContext,
9123 "PP: entering MISC\n");
9124#endif
9125 }
9126 } else {
9127 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
9128 ctxt->sax->setDocumentLocator(ctxt->userData,
9129 &xmlDefaultSAXLocator);
9130 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
William M. Bracka3215c72004-07-31 16:24:01 +00009131 if (ctxt->version == NULL) {
9132 xmlErrMemory(ctxt, NULL);
9133 break;
9134 }
Owen Taylor3473f882001-02-23 17:55:21 +00009135 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
9136 (!ctxt->disableSAX))
9137 ctxt->sax->startDocument(ctxt->userData);
9138 ctxt->instate = XML_PARSER_MISC;
9139#ifdef DEBUG_PUSH
9140 xmlGenericError(xmlGenericErrorContext,
9141 "PP: entering MISC\n");
9142#endif
9143 }
9144 break;
Daniel Veillarda880b122003-04-21 21:36:41 +00009145 case XML_PARSER_START_TAG: {
Daniel Veillarde57ec792003-09-10 10:50:59 +00009146 const xmlChar *name;
9147 const xmlChar *prefix;
9148 const xmlChar *URI;
9149 int nsNr = ctxt->nsNr;
Daniel Veillarda880b122003-04-21 21:36:41 +00009150
9151 if ((avail < 2) && (ctxt->inputNr == 1))
9152 goto done;
9153 cur = ctxt->input->cur[0];
9154 if (cur != '<') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009155 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Daniel Veillarda880b122003-04-21 21:36:41 +00009156 ctxt->instate = XML_PARSER_EOF;
Daniel Veillarda880b122003-04-21 21:36:41 +00009157 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
9158 ctxt->sax->endDocument(ctxt->userData);
9159 goto done;
9160 }
9161 if (!terminate) {
9162 if (ctxt->progressive) {
Daniel Veillardb3744002004-02-18 14:28:22 +00009163 /* > can be found unescaped in attribute values */
Daniel Veillardeb70f932004-07-05 16:46:09 +00009164 if ((lastgt == NULL) || (ctxt->input->cur >= lastgt))
Daniel Veillarda880b122003-04-21 21:36:41 +00009165 goto done;
9166 } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) {
9167 goto done;
9168 }
9169 }
9170 if (ctxt->spaceNr == 0)
9171 spacePush(ctxt, -1);
9172 else
9173 spacePush(ctxt, *ctxt->space);
Daniel Veillard81273902003-09-30 00:43:48 +00009174#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009175 if (ctxt->sax2)
Daniel Veillard81273902003-09-30 00:43:48 +00009176#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00009177 name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen);
Daniel Veillard81273902003-09-30 00:43:48 +00009178#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009179 else
9180 name = xmlParseStartTag(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00009181#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarda880b122003-04-21 21:36:41 +00009182 if (name == NULL) {
9183 spacePop(ctxt);
9184 ctxt->instate = XML_PARSER_EOF;
Daniel Veillarda880b122003-04-21 21:36:41 +00009185 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
9186 ctxt->sax->endDocument(ctxt->userData);
9187 goto done;
9188 }
Daniel Veillard4432df22003-09-28 18:58:27 +00009189#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda880b122003-04-21 21:36:41 +00009190 /*
9191 * [ VC: Root Element Type ]
9192 * The Name in the document type declaration must match
9193 * the element type of the root element.
9194 */
9195 if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
9196 ctxt->node && (ctxt->node == ctxt->myDoc->children))
9197 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillard4432df22003-09-28 18:58:27 +00009198#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda880b122003-04-21 21:36:41 +00009199
9200 /*
9201 * Check for an Empty Element.
9202 */
9203 if ((RAW == '/') && (NXT(1) == '>')) {
9204 SKIP(2);
Daniel Veillarde57ec792003-09-10 10:50:59 +00009205
9206 if (ctxt->sax2) {
9207 if ((ctxt->sax != NULL) &&
9208 (ctxt->sax->endElementNs != NULL) &&
9209 (!ctxt->disableSAX))
9210 ctxt->sax->endElementNs(ctxt->userData, name,
9211 prefix, URI);
Daniel Veillard81273902003-09-30 00:43:48 +00009212#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009213 } else {
9214 if ((ctxt->sax != NULL) &&
9215 (ctxt->sax->endElement != NULL) &&
9216 (!ctxt->disableSAX))
9217 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard81273902003-09-30 00:43:48 +00009218#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarda880b122003-04-21 21:36:41 +00009219 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009220 spacePop(ctxt);
9221 if (ctxt->nameNr == 0) {
Daniel Veillarda880b122003-04-21 21:36:41 +00009222 ctxt->instate = XML_PARSER_EPILOG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009223 } else {
9224 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillarda880b122003-04-21 21:36:41 +00009225 }
9226 break;
9227 }
9228 if (RAW == '>') {
9229 NEXT;
9230 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00009231 xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED,
Daniel Veillarda880b122003-04-21 21:36:41 +00009232 "Couldn't find end of Start Tag %s\n",
9233 name);
Daniel Veillarda880b122003-04-21 21:36:41 +00009234 nodePop(ctxt);
Daniel Veillarda880b122003-04-21 21:36:41 +00009235 spacePop(ctxt);
Daniel Veillarda880b122003-04-21 21:36:41 +00009236 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009237 if (ctxt->sax2)
9238 nameNsPush(ctxt, name, prefix, URI, ctxt->nsNr - nsNr);
Daniel Veillard81273902003-09-30 00:43:48 +00009239#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009240 else
9241 namePush(ctxt, name);
Daniel Veillard81273902003-09-30 00:43:48 +00009242#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00009243
Daniel Veillarda880b122003-04-21 21:36:41 +00009244 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillarda880b122003-04-21 21:36:41 +00009245 break;
9246 }
9247 case XML_PARSER_CONTENT: {
9248 const xmlChar *test;
9249 unsigned int cons;
9250 if ((avail < 2) && (ctxt->inputNr == 1))
9251 goto done;
9252 cur = ctxt->input->cur[0];
9253 next = ctxt->input->cur[1];
9254
9255 test = CUR_PTR;
9256 cons = ctxt->input->consumed;
9257 if ((cur == '<') && (next == '/')) {
9258 ctxt->instate = XML_PARSER_END_TAG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009259 break;
9260 } else if ((cur == '<') && (next == '?')) {
9261 if ((!terminate) &&
9262 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9263 goto done;
Daniel Veillarda880b122003-04-21 21:36:41 +00009264 xmlParsePI(ctxt);
9265 } else if ((cur == '<') && (next != '!')) {
9266 ctxt->instate = XML_PARSER_START_TAG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009267 break;
9268 } else if ((cur == '<') && (next == '!') &&
9269 (ctxt->input->cur[2] == '-') &&
9270 (ctxt->input->cur[3] == '-')) {
9271 if ((!terminate) &&
9272 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9273 goto done;
Daniel Veillarda880b122003-04-21 21:36:41 +00009274 xmlParseComment(ctxt);
9275 ctxt->instate = XML_PARSER_CONTENT;
9276 } else if ((cur == '<') && (ctxt->input->cur[1] == '!') &&
9277 (ctxt->input->cur[2] == '[') &&
9278 (ctxt->input->cur[3] == 'C') &&
9279 (ctxt->input->cur[4] == 'D') &&
9280 (ctxt->input->cur[5] == 'A') &&
9281 (ctxt->input->cur[6] == 'T') &&
9282 (ctxt->input->cur[7] == 'A') &&
9283 (ctxt->input->cur[8] == '[')) {
9284 SKIP(9);
9285 ctxt->instate = XML_PARSER_CDATA_SECTION;
Daniel Veillarda880b122003-04-21 21:36:41 +00009286 break;
9287 } else if ((cur == '<') && (next == '!') &&
9288 (avail < 9)) {
9289 goto done;
9290 } else if (cur == '&') {
9291 if ((!terminate) &&
9292 (xmlParseLookupSequence(ctxt, ';', 0, 0) < 0))
9293 goto done;
Daniel Veillarda880b122003-04-21 21:36:41 +00009294 xmlParseReference(ctxt);
9295 } else {
9296 /* TODO Avoid the extra copy, handle directly !!! */
9297 /*
9298 * Goal of the following test is:
9299 * - minimize calls to the SAX 'character' callback
9300 * when they are mergeable
9301 * - handle an problem for isBlank when we only parse
9302 * a sequence of blank chars and the next one is
9303 * not available to check against '<' presence.
9304 * - tries to homogenize the differences in SAX
9305 * callbacks between the push and pull versions
9306 * of the parser.
9307 */
9308 if ((ctxt->inputNr == 1) &&
9309 (avail < XML_PARSER_BIG_BUFFER_SIZE)) {
9310 if (!terminate) {
9311 if (ctxt->progressive) {
9312 if ((lastlt == NULL) ||
9313 (ctxt->input->cur > lastlt))
9314 goto done;
9315 } else if (xmlParseLookupSequence(ctxt,
9316 '<', 0, 0) < 0) {
9317 goto done;
9318 }
9319 }
9320 }
9321 ctxt->checkIndex = 0;
Daniel Veillarda880b122003-04-21 21:36:41 +00009322 xmlParseCharData(ctxt, 0);
9323 }
9324 /*
9325 * Pop-up of finished entities.
9326 */
9327 while ((RAW == 0) && (ctxt->inputNr > 1))
9328 xmlPopInput(ctxt);
9329 if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009330 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
9331 "detected an error in element content\n");
Daniel Veillarda880b122003-04-21 21:36:41 +00009332 ctxt->instate = XML_PARSER_EOF;
9333 break;
9334 }
9335 break;
9336 }
9337 case XML_PARSER_END_TAG:
9338 if (avail < 2)
9339 goto done;
9340 if (!terminate) {
9341 if (ctxt->progressive) {
Daniel Veillardeb70f932004-07-05 16:46:09 +00009342 /* > can be found unescaped in attribute values */
9343 if ((lastgt == NULL) || (ctxt->input->cur >= lastgt))
Daniel Veillarda880b122003-04-21 21:36:41 +00009344 goto done;
9345 } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) {
9346 goto done;
9347 }
9348 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009349 if (ctxt->sax2) {
9350 xmlParseEndTag2(ctxt,
9351 (void *) ctxt->pushTab[ctxt->nameNr * 3 - 3],
9352 (void *) ctxt->pushTab[ctxt->nameNr * 3 - 2], 0,
Daniel Veillardc82c57e2004-01-12 16:24:34 +00009353 (int) (long) ctxt->pushTab[ctxt->nameNr * 3 - 1], 0);
Daniel Veillarde57ec792003-09-10 10:50:59 +00009354 nameNsPop(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00009355 }
9356#ifdef LIBXML_SAX1_ENABLED
9357 else
Daniel Veillarde57ec792003-09-10 10:50:59 +00009358 xmlParseEndTag1(ctxt, 0);
Daniel Veillard81273902003-09-30 00:43:48 +00009359#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00009360 if (ctxt->nameNr == 0) {
Daniel Veillarda880b122003-04-21 21:36:41 +00009361 ctxt->instate = XML_PARSER_EPILOG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009362 } else {
9363 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillarda880b122003-04-21 21:36:41 +00009364 }
9365 break;
9366 case XML_PARSER_CDATA_SECTION: {
9367 /*
9368 * The Push mode need to have the SAX callback for
9369 * cdataBlock merge back contiguous callbacks.
9370 */
9371 int base;
9372
9373 base = xmlParseLookupSequence(ctxt, ']', ']', '>');
9374 if (base < 0) {
9375 if (avail >= XML_PARSER_BIG_BUFFER_SIZE + 2) {
9376 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
9377 if (ctxt->sax->cdataBlock != NULL)
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00009378 ctxt->sax->cdataBlock(ctxt->userData,
9379 ctxt->input->cur,
9380 XML_PARSER_BIG_BUFFER_SIZE);
9381 else if (ctxt->sax->characters != NULL)
9382 ctxt->sax->characters(ctxt->userData,
9383 ctxt->input->cur,
Daniel Veillarda880b122003-04-21 21:36:41 +00009384 XML_PARSER_BIG_BUFFER_SIZE);
9385 }
Daniel Veillard0b787f32004-03-26 17:29:53 +00009386 SKIPL(XML_PARSER_BIG_BUFFER_SIZE);
Daniel Veillarda880b122003-04-21 21:36:41 +00009387 ctxt->checkIndex = 0;
9388 }
9389 goto done;
9390 } else {
9391 if ((ctxt->sax != NULL) && (base > 0) &&
9392 (!ctxt->disableSAX)) {
9393 if (ctxt->sax->cdataBlock != NULL)
9394 ctxt->sax->cdataBlock(ctxt->userData,
9395 ctxt->input->cur, base);
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00009396 else if (ctxt->sax->characters != NULL)
9397 ctxt->sax->characters(ctxt->userData,
9398 ctxt->input->cur, base);
Daniel Veillarda880b122003-04-21 21:36:41 +00009399 }
Daniel Veillard0b787f32004-03-26 17:29:53 +00009400 SKIPL(base + 3);
Daniel Veillarda880b122003-04-21 21:36:41 +00009401 ctxt->checkIndex = 0;
9402 ctxt->instate = XML_PARSER_CONTENT;
9403#ifdef DEBUG_PUSH
9404 xmlGenericError(xmlGenericErrorContext,
9405 "PP: entering CONTENT\n");
9406#endif
9407 }
9408 break;
9409 }
Owen Taylor3473f882001-02-23 17:55:21 +00009410 case XML_PARSER_MISC:
9411 SKIP_BLANKS;
9412 if (ctxt->input->buf == NULL)
Daniel Veillarda880b122003-04-21 21:36:41 +00009413 avail = ctxt->input->length -
9414 (ctxt->input->cur - ctxt->input->base);
Owen Taylor3473f882001-02-23 17:55:21 +00009415 else
Daniel Veillarda880b122003-04-21 21:36:41 +00009416 avail = ctxt->input->buf->buffer->use -
9417 (ctxt->input->cur - ctxt->input->base);
Owen Taylor3473f882001-02-23 17:55:21 +00009418 if (avail < 2)
9419 goto done;
9420 cur = ctxt->input->cur[0];
9421 next = ctxt->input->cur[1];
9422 if ((cur == '<') && (next == '?')) {
9423 if ((!terminate) &&
9424 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9425 goto done;
9426#ifdef DEBUG_PUSH
9427 xmlGenericError(xmlGenericErrorContext,
9428 "PP: Parsing PI\n");
9429#endif
9430 xmlParsePI(ctxt);
9431 } else if ((cur == '<') && (next == '!') &&
Daniel Veillarda880b122003-04-21 21:36:41 +00009432 (ctxt->input->cur[2] == '-') &&
9433 (ctxt->input->cur[3] == '-')) {
Owen Taylor3473f882001-02-23 17:55:21 +00009434 if ((!terminate) &&
9435 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9436 goto done;
9437#ifdef DEBUG_PUSH
9438 xmlGenericError(xmlGenericErrorContext,
9439 "PP: Parsing Comment\n");
9440#endif
9441 xmlParseComment(ctxt);
9442 ctxt->instate = XML_PARSER_MISC;
9443 } else if ((cur == '<') && (next == '!') &&
Daniel Veillarda880b122003-04-21 21:36:41 +00009444 (ctxt->input->cur[2] == 'D') &&
9445 (ctxt->input->cur[3] == 'O') &&
9446 (ctxt->input->cur[4] == 'C') &&
9447 (ctxt->input->cur[5] == 'T') &&
9448 (ctxt->input->cur[6] == 'Y') &&
9449 (ctxt->input->cur[7] == 'P') &&
Owen Taylor3473f882001-02-23 17:55:21 +00009450 (ctxt->input->cur[8] == 'E')) {
9451 if ((!terminate) &&
9452 (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
9453 goto done;
9454#ifdef DEBUG_PUSH
9455 xmlGenericError(xmlGenericErrorContext,
9456 "PP: Parsing internal subset\n");
9457#endif
9458 ctxt->inSubset = 1;
9459 xmlParseDocTypeDecl(ctxt);
9460 if (RAW == '[') {
9461 ctxt->instate = XML_PARSER_DTD;
9462#ifdef DEBUG_PUSH
9463 xmlGenericError(xmlGenericErrorContext,
9464 "PP: entering DTD\n");
9465#endif
9466 } else {
9467 /*
9468 * Create and update the external subset.
9469 */
9470 ctxt->inSubset = 2;
9471 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
9472 (ctxt->sax->externalSubset != NULL))
9473 ctxt->sax->externalSubset(ctxt->userData,
9474 ctxt->intSubName, ctxt->extSubSystem,
9475 ctxt->extSubURI);
9476 ctxt->inSubset = 0;
9477 ctxt->instate = XML_PARSER_PROLOG;
9478#ifdef DEBUG_PUSH
9479 xmlGenericError(xmlGenericErrorContext,
9480 "PP: entering PROLOG\n");
9481#endif
9482 }
9483 } else if ((cur == '<') && (next == '!') &&
9484 (avail < 9)) {
9485 goto done;
9486 } else {
9487 ctxt->instate = XML_PARSER_START_TAG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009488 ctxt->progressive = 1;
9489 xmlParseGetLasts(ctxt, &lastlt, &lastgt);
Owen Taylor3473f882001-02-23 17:55:21 +00009490#ifdef DEBUG_PUSH
9491 xmlGenericError(xmlGenericErrorContext,
9492 "PP: entering START_TAG\n");
9493#endif
9494 }
9495 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009496 case XML_PARSER_PROLOG:
9497 SKIP_BLANKS;
9498 if (ctxt->input->buf == NULL)
9499 avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base);
9500 else
9501 avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base);
9502 if (avail < 2)
9503 goto done;
9504 cur = ctxt->input->cur[0];
9505 next = ctxt->input->cur[1];
9506 if ((cur == '<') && (next == '?')) {
9507 if ((!terminate) &&
9508 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9509 goto done;
9510#ifdef DEBUG_PUSH
9511 xmlGenericError(xmlGenericErrorContext,
9512 "PP: Parsing PI\n");
9513#endif
9514 xmlParsePI(ctxt);
9515 } else if ((cur == '<') && (next == '!') &&
9516 (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) {
9517 if ((!terminate) &&
9518 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9519 goto done;
9520#ifdef DEBUG_PUSH
9521 xmlGenericError(xmlGenericErrorContext,
9522 "PP: Parsing Comment\n");
9523#endif
9524 xmlParseComment(ctxt);
9525 ctxt->instate = XML_PARSER_PROLOG;
9526 } else if ((cur == '<') && (next == '!') &&
9527 (avail < 4)) {
9528 goto done;
9529 } else {
9530 ctxt->instate = XML_PARSER_START_TAG;
Daniel Veillard0df3bc32004-06-08 12:03:41 +00009531 if (ctxt->progressive == 0)
9532 ctxt->progressive = 1;
Daniel Veillarda880b122003-04-21 21:36:41 +00009533 xmlParseGetLasts(ctxt, &lastlt, &lastgt);
Owen Taylor3473f882001-02-23 17:55:21 +00009534#ifdef DEBUG_PUSH
9535 xmlGenericError(xmlGenericErrorContext,
9536 "PP: entering START_TAG\n");
9537#endif
9538 }
9539 break;
9540 case XML_PARSER_EPILOG:
9541 SKIP_BLANKS;
9542 if (ctxt->input->buf == NULL)
9543 avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base);
9544 else
9545 avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base);
9546 if (avail < 2)
9547 goto done;
9548 cur = ctxt->input->cur[0];
9549 next = ctxt->input->cur[1];
9550 if ((cur == '<') && (next == '?')) {
9551 if ((!terminate) &&
9552 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9553 goto done;
9554#ifdef DEBUG_PUSH
9555 xmlGenericError(xmlGenericErrorContext,
9556 "PP: Parsing PI\n");
9557#endif
9558 xmlParsePI(ctxt);
9559 ctxt->instate = XML_PARSER_EPILOG;
9560 } else if ((cur == '<') && (next == '!') &&
9561 (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) {
9562 if ((!terminate) &&
9563 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9564 goto done;
9565#ifdef DEBUG_PUSH
9566 xmlGenericError(xmlGenericErrorContext,
9567 "PP: Parsing Comment\n");
9568#endif
9569 xmlParseComment(ctxt);
9570 ctxt->instate = XML_PARSER_EPILOG;
9571 } else if ((cur == '<') && (next == '!') &&
9572 (avail < 4)) {
9573 goto done;
9574 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009575 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00009576 ctxt->instate = XML_PARSER_EOF;
9577#ifdef DEBUG_PUSH
9578 xmlGenericError(xmlGenericErrorContext,
9579 "PP: entering EOF\n");
9580#endif
Daniel Veillard8d24cc12002-03-05 15:41:29 +00009581 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00009582 ctxt->sax->endDocument(ctxt->userData);
9583 goto done;
9584 }
9585 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009586 case XML_PARSER_DTD: {
9587 /*
9588 * Sorry but progressive parsing of the internal subset
9589 * is not expected to be supported. We first check that
9590 * the full content of the internal subset is available and
9591 * the parsing is launched only at that point.
9592 * Internal subset ends up with "']' S? '>'" in an unescaped
9593 * section and not in a ']]>' sequence which are conditional
9594 * sections (whoever argued to keep that crap in XML deserve
9595 * a place in hell !).
9596 */
9597 int base, i;
9598 xmlChar *buf;
9599 xmlChar quote = 0;
9600
9601 base = ctxt->input->cur - ctxt->input->base;
9602 if (base < 0) return(0);
9603 if (ctxt->checkIndex > base)
9604 base = ctxt->checkIndex;
9605 buf = ctxt->input->buf->buffer->content;
9606 for (;(unsigned int) base < ctxt->input->buf->buffer->use;
9607 base++) {
9608 if (quote != 0) {
9609 if (buf[base] == quote)
9610 quote = 0;
9611 continue;
9612 }
Daniel Veillard036143b2004-02-12 11:57:52 +00009613 if ((quote == 0) && (buf[base] == '<')) {
9614 int found = 0;
9615 /* special handling of comments */
9616 if (((unsigned int) base + 4 <
9617 ctxt->input->buf->buffer->use) &&
9618 (buf[base + 1] == '!') &&
9619 (buf[base + 2] == '-') &&
9620 (buf[base + 3] == '-')) {
9621 for (;(unsigned int) base + 3 <
9622 ctxt->input->buf->buffer->use; base++) {
9623 if ((buf[base] == '-') &&
9624 (buf[base + 1] == '-') &&
9625 (buf[base + 2] == '>')) {
9626 found = 1;
9627 base += 2;
9628 break;
9629 }
9630 }
9631 if (!found)
9632 break;
9633 continue;
9634 }
9635 }
Owen Taylor3473f882001-02-23 17:55:21 +00009636 if (buf[base] == '"') {
9637 quote = '"';
9638 continue;
9639 }
9640 if (buf[base] == '\'') {
9641 quote = '\'';
9642 continue;
9643 }
9644 if (buf[base] == ']') {
9645 if ((unsigned int) base +1 >=
9646 ctxt->input->buf->buffer->use)
9647 break;
9648 if (buf[base + 1] == ']') {
9649 /* conditional crap, skip both ']' ! */
9650 base++;
9651 continue;
9652 }
9653 for (i = 0;
9654 (unsigned int) base + i < ctxt->input->buf->buffer->use;
9655 i++) {
9656 if (buf[base + i] == '>')
9657 goto found_end_int_subset;
9658 }
9659 break;
9660 }
9661 }
9662 /*
9663 * We didn't found the end of the Internal subset
9664 */
9665 if (quote == 0)
9666 ctxt->checkIndex = base;
9667#ifdef DEBUG_PUSH
9668 if (next == 0)
9669 xmlGenericError(xmlGenericErrorContext,
9670 "PP: lookup of int subset end filed\n");
9671#endif
9672 goto done;
9673
9674found_end_int_subset:
9675 xmlParseInternalSubset(ctxt);
9676 ctxt->inSubset = 2;
9677 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
9678 (ctxt->sax->externalSubset != NULL))
9679 ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
9680 ctxt->extSubSystem, ctxt->extSubURI);
9681 ctxt->inSubset = 0;
9682 ctxt->instate = XML_PARSER_PROLOG;
9683 ctxt->checkIndex = 0;
9684#ifdef DEBUG_PUSH
9685 xmlGenericError(xmlGenericErrorContext,
9686 "PP: entering PROLOG\n");
9687#endif
9688 break;
9689 }
9690 case XML_PARSER_COMMENT:
9691 xmlGenericError(xmlGenericErrorContext,
9692 "PP: internal error, state == COMMENT\n");
9693 ctxt->instate = XML_PARSER_CONTENT;
9694#ifdef DEBUG_PUSH
9695 xmlGenericError(xmlGenericErrorContext,
9696 "PP: entering CONTENT\n");
9697#endif
9698 break;
Daniel Veillarda880b122003-04-21 21:36:41 +00009699 case XML_PARSER_IGNORE:
9700 xmlGenericError(xmlGenericErrorContext,
9701 "PP: internal error, state == IGNORE");
9702 ctxt->instate = XML_PARSER_DTD;
9703#ifdef DEBUG_PUSH
9704 xmlGenericError(xmlGenericErrorContext,
9705 "PP: entering DTD\n");
9706#endif
9707 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009708 case XML_PARSER_PI:
9709 xmlGenericError(xmlGenericErrorContext,
9710 "PP: internal error, state == PI\n");
9711 ctxt->instate = XML_PARSER_CONTENT;
9712#ifdef DEBUG_PUSH
9713 xmlGenericError(xmlGenericErrorContext,
9714 "PP: entering CONTENT\n");
9715#endif
9716 break;
9717 case XML_PARSER_ENTITY_DECL:
9718 xmlGenericError(xmlGenericErrorContext,
9719 "PP: internal error, state == ENTITY_DECL\n");
9720 ctxt->instate = XML_PARSER_DTD;
9721#ifdef DEBUG_PUSH
9722 xmlGenericError(xmlGenericErrorContext,
9723 "PP: entering DTD\n");
9724#endif
9725 break;
9726 case XML_PARSER_ENTITY_VALUE:
9727 xmlGenericError(xmlGenericErrorContext,
9728 "PP: internal error, state == ENTITY_VALUE\n");
9729 ctxt->instate = XML_PARSER_CONTENT;
9730#ifdef DEBUG_PUSH
9731 xmlGenericError(xmlGenericErrorContext,
9732 "PP: entering DTD\n");
9733#endif
9734 break;
9735 case XML_PARSER_ATTRIBUTE_VALUE:
9736 xmlGenericError(xmlGenericErrorContext,
9737 "PP: internal error, state == ATTRIBUTE_VALUE\n");
9738 ctxt->instate = XML_PARSER_START_TAG;
9739#ifdef DEBUG_PUSH
9740 xmlGenericError(xmlGenericErrorContext,
9741 "PP: entering START_TAG\n");
9742#endif
9743 break;
9744 case XML_PARSER_SYSTEM_LITERAL:
9745 xmlGenericError(xmlGenericErrorContext,
9746 "PP: internal error, state == SYSTEM_LITERAL\n");
9747 ctxt->instate = XML_PARSER_START_TAG;
9748#ifdef DEBUG_PUSH
9749 xmlGenericError(xmlGenericErrorContext,
9750 "PP: entering START_TAG\n");
9751#endif
9752 break;
Daniel Veillard4a7ae502002-02-18 19:18:17 +00009753 case XML_PARSER_PUBLIC_LITERAL:
9754 xmlGenericError(xmlGenericErrorContext,
9755 "PP: internal error, state == PUBLIC_LITERAL\n");
9756 ctxt->instate = XML_PARSER_START_TAG;
9757#ifdef DEBUG_PUSH
9758 xmlGenericError(xmlGenericErrorContext,
9759 "PP: entering START_TAG\n");
9760#endif
9761 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009762 }
9763 }
9764done:
9765#ifdef DEBUG_PUSH
9766 xmlGenericError(xmlGenericErrorContext, "PP: done %d\n", ret);
9767#endif
9768 return(ret);
9769}
9770
9771/**
Owen Taylor3473f882001-02-23 17:55:21 +00009772 * xmlParseChunk:
9773 * @ctxt: an XML parser context
9774 * @chunk: an char array
9775 * @size: the size in byte of the chunk
9776 * @terminate: last chunk indicator
9777 *
9778 * Parse a Chunk of memory
9779 *
9780 * Returns zero if no error, the xmlParserErrors otherwise.
9781 */
9782int
9783xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size,
9784 int terminate) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009785 if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
9786 return(ctxt->errNo);
Daniel Veillard309f81d2003-09-23 09:02:53 +00009787 if (ctxt->instate == XML_PARSER_START)
9788 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00009789 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
9790 (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) {
9791 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
9792 int cur = ctxt->input->cur - ctxt->input->base;
William M. Bracka3215c72004-07-31 16:24:01 +00009793 int res;
Owen Taylor3473f882001-02-23 17:55:21 +00009794
William M. Bracka3215c72004-07-31 16:24:01 +00009795 res =xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
9796 if (res < 0) {
9797 ctxt->errNo = XML_PARSER_EOF;
9798 ctxt->disableSAX = 1;
9799 return (XML_PARSER_EOF);
9800 }
Owen Taylor3473f882001-02-23 17:55:21 +00009801 ctxt->input->base = ctxt->input->buf->buffer->content + base;
9802 ctxt->input->cur = ctxt->input->base + cur;
Daniel Veillard48b2f892001-02-25 16:11:03 +00009803 ctxt->input->end =
9804 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00009805#ifdef DEBUG_PUSH
9806 xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
9807#endif
9808
Owen Taylor3473f882001-02-23 17:55:21 +00009809 } else if (ctxt->instate != XML_PARSER_EOF) {
9810 if ((ctxt->input != NULL) && ctxt->input->buf != NULL) {
9811 xmlParserInputBufferPtr in = ctxt->input->buf;
9812 if ((in->encoder != NULL) && (in->buffer != NULL) &&
9813 (in->raw != NULL)) {
9814 int nbchars;
9815
9816 nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
9817 if (nbchars < 0) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00009818 /* TODO 2.6.0 */
Owen Taylor3473f882001-02-23 17:55:21 +00009819 xmlGenericError(xmlGenericErrorContext,
9820 "xmlParseChunk: encoder error\n");
9821 return(XML_ERR_INVALID_ENCODING);
9822 }
9823 }
9824 }
9825 }
9826 xmlParseTryOrFinish(ctxt, terminate);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009827 if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
9828 return(ctxt->errNo);
Owen Taylor3473f882001-02-23 17:55:21 +00009829 if (terminate) {
9830 /*
9831 * Check for termination
9832 */
Daniel Veillard819d5cb2002-10-14 11:15:18 +00009833 int avail = 0;
9834 if (ctxt->input->buf == NULL)
9835 avail = ctxt->input->length -
9836 (ctxt->input->cur - ctxt->input->base);
9837 else
9838 avail = ctxt->input->buf->buffer->use -
9839 (ctxt->input->cur - ctxt->input->base);
9840
Owen Taylor3473f882001-02-23 17:55:21 +00009841 if ((ctxt->instate != XML_PARSER_EOF) &&
9842 (ctxt->instate != XML_PARSER_EPILOG)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009843 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00009844 }
Daniel Veillard819d5cb2002-10-14 11:15:18 +00009845 if ((ctxt->instate == XML_PARSER_EPILOG) && (avail > 0)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009846 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Daniel Veillard819d5cb2002-10-14 11:15:18 +00009847 }
Owen Taylor3473f882001-02-23 17:55:21 +00009848 if (ctxt->instate != XML_PARSER_EOF) {
Daniel Veillard8d24cc12002-03-05 15:41:29 +00009849 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00009850 ctxt->sax->endDocument(ctxt->userData);
9851 }
9852 ctxt->instate = XML_PARSER_EOF;
9853 }
9854 return((xmlParserErrors) ctxt->errNo);
9855}
9856
9857/************************************************************************
9858 * *
9859 * I/O front end functions to the parser *
9860 * *
9861 ************************************************************************/
9862
9863/**
9864 * xmlStopParser:
9865 * @ctxt: an XML parser context
9866 *
9867 * Blocks further parser processing
9868 */
9869void
9870xmlStopParser(xmlParserCtxtPtr ctxt) {
Daniel Veillard157fee02003-10-31 10:36:03 +00009871 if (ctxt == NULL)
9872 return;
Owen Taylor3473f882001-02-23 17:55:21 +00009873 ctxt->instate = XML_PARSER_EOF;
Daniel Veillard157fee02003-10-31 10:36:03 +00009874 ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00009875 if (ctxt->input != NULL)
9876 ctxt->input->cur = BAD_CAST"";
9877}
9878
9879/**
9880 * xmlCreatePushParserCtxt:
9881 * @sax: a SAX handler
9882 * @user_data: The user data returned on SAX callbacks
9883 * @chunk: a pointer to an array of chars
9884 * @size: number of chars in the array
9885 * @filename: an optional file name or URI
9886 *
Daniel Veillard176d99f2002-07-06 19:22:28 +00009887 * Create a parser context for using the XML parser in push mode.
9888 * If @buffer and @size are non-NULL, the data is used to detect
9889 * the encoding. The remaining characters will be parsed so they
9890 * don't need to be fed in again through xmlParseChunk.
Owen Taylor3473f882001-02-23 17:55:21 +00009891 * To allow content encoding detection, @size should be >= 4
9892 * The value of @filename is used for fetching external entities
9893 * and error/warning reports.
9894 *
9895 * Returns the new parser context or NULL
9896 */
Daniel Veillard176d99f2002-07-06 19:22:28 +00009897
Owen Taylor3473f882001-02-23 17:55:21 +00009898xmlParserCtxtPtr
9899xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
9900 const char *chunk, int size, const char *filename) {
9901 xmlParserCtxtPtr ctxt;
9902 xmlParserInputPtr inputStream;
9903 xmlParserInputBufferPtr buf;
9904 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
9905
9906 /*
9907 * plug some encoding conversion routines
9908 */
9909 if ((chunk != NULL) && (size >= 4))
9910 enc = xmlDetectCharEncoding((const xmlChar *) chunk, size);
9911
9912 buf = xmlAllocParserInputBuffer(enc);
9913 if (buf == NULL) return(NULL);
9914
9915 ctxt = xmlNewParserCtxt();
9916 if (ctxt == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00009917 xmlErrMemory(NULL, "creating parser: out of memory\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009918 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00009919 return(NULL);
9920 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009921 ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * sizeof(xmlChar *));
9922 if (ctxt->pushTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009923 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00009924 xmlFreeParserInputBuffer(buf);
9925 xmlFreeParserCtxt(ctxt);
9926 return(NULL);
9927 }
Owen Taylor3473f882001-02-23 17:55:21 +00009928 if (sax != NULL) {
Daniel Veillard81273902003-09-30 00:43:48 +00009929#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00009930 if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
Daniel Veillard81273902003-09-30 00:43:48 +00009931#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00009932 xmlFree(ctxt->sax);
9933 ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler));
9934 if (ctxt->sax == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009935 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009936 xmlFreeParserInputBuffer(buf);
9937 xmlFreeParserCtxt(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00009938 return(NULL);
9939 }
9940 memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler));
9941 if (user_data != NULL)
9942 ctxt->userData = user_data;
9943 }
9944 if (filename == NULL) {
9945 ctxt->directory = NULL;
9946 } else {
9947 ctxt->directory = xmlParserGetDirectory(filename);
9948 }
9949
9950 inputStream = xmlNewInputStream(ctxt);
9951 if (inputStream == NULL) {
9952 xmlFreeParserCtxt(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009953 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00009954 return(NULL);
9955 }
9956
9957 if (filename == NULL)
9958 inputStream->filename = NULL;
William M. Bracka3215c72004-07-31 16:24:01 +00009959 else {
Daniel Veillardf4862f02002-09-10 11:13:43 +00009960 inputStream->filename = (char *)
Igor Zlatkovic5f9fada2003-02-19 14:51:00 +00009961 xmlCanonicPath((const xmlChar *) filename);
William M. Bracka3215c72004-07-31 16:24:01 +00009962 if (inputStream->filename == NULL) {
9963 xmlFreeParserCtxt(ctxt);
9964 xmlFreeParserInputBuffer(buf);
9965 return(NULL);
9966 }
9967 }
Owen Taylor3473f882001-02-23 17:55:21 +00009968 inputStream->buf = buf;
9969 inputStream->base = inputStream->buf->buffer->content;
9970 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00009971 inputStream->end =
9972 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00009973
9974 inputPush(ctxt, inputStream);
9975
9976 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
9977 (ctxt->input->buf != NULL)) {
Daniel Veillardaa39a0f2002-01-06 12:47:22 +00009978 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
9979 int cur = ctxt->input->cur - ctxt->input->base;
9980
Owen Taylor3473f882001-02-23 17:55:21 +00009981 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
Daniel Veillardaa39a0f2002-01-06 12:47:22 +00009982
9983 ctxt->input->base = ctxt->input->buf->buffer->content + base;
9984 ctxt->input->cur = ctxt->input->base + cur;
9985 ctxt->input->end =
9986 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00009987#ifdef DEBUG_PUSH
9988 xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
9989#endif
9990 }
9991
Daniel Veillard0e4cd172001-06-28 12:13:56 +00009992 if (enc != XML_CHAR_ENCODING_NONE) {
9993 xmlSwitchEncoding(ctxt, enc);
9994 }
9995
Owen Taylor3473f882001-02-23 17:55:21 +00009996 return(ctxt);
9997}
Daniel Veillard73b013f2003-09-30 12:36:01 +00009998#endif /* LIBXML_PUSH_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00009999
10000/**
10001 * xmlCreateIOParserCtxt:
10002 * @sax: a SAX handler
10003 * @user_data: The user data returned on SAX callbacks
10004 * @ioread: an I/O read function
10005 * @ioclose: an I/O close function
10006 * @ioctx: an I/O handler
10007 * @enc: the charset encoding if known
10008 *
10009 * Create a parser context for using the XML parser with an existing
10010 * I/O stream
10011 *
10012 * Returns the new parser context or NULL
10013 */
10014xmlParserCtxtPtr
10015xmlCreateIOParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
10016 xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
10017 void *ioctx, xmlCharEncoding enc) {
10018 xmlParserCtxtPtr ctxt;
10019 xmlParserInputPtr inputStream;
10020 xmlParserInputBufferPtr buf;
10021
10022 buf = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, enc);
10023 if (buf == NULL) return(NULL);
10024
10025 ctxt = xmlNewParserCtxt();
10026 if (ctxt == NULL) {
10027 xmlFree(buf);
10028 return(NULL);
10029 }
10030 if (sax != NULL) {
Daniel Veillard81273902003-09-30 00:43:48 +000010031#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +000010032 if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
Daniel Veillard81273902003-09-30 00:43:48 +000010033#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000010034 xmlFree(ctxt->sax);
10035 ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler));
10036 if (ctxt->sax == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010037 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010038 xmlFree(ctxt);
10039 return(NULL);
10040 }
10041 memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler));
10042 if (user_data != NULL)
10043 ctxt->userData = user_data;
10044 }
10045
10046 inputStream = xmlNewIOInputStream(ctxt, buf, enc);
10047 if (inputStream == NULL) {
10048 xmlFreeParserCtxt(ctxt);
10049 return(NULL);
10050 }
10051 inputPush(ctxt, inputStream);
10052
10053 return(ctxt);
10054}
10055
Daniel Veillard4432df22003-09-28 18:58:27 +000010056#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000010057/************************************************************************
10058 * *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010059 * Front ends when parsing a DTD *
Owen Taylor3473f882001-02-23 17:55:21 +000010060 * *
10061 ************************************************************************/
10062
10063/**
10064 * xmlIOParseDTD:
10065 * @sax: the SAX handler block or NULL
10066 * @input: an Input Buffer
10067 * @enc: the charset encoding if known
10068 *
10069 * Load and parse a DTD
10070 *
10071 * Returns the resulting xmlDtdPtr or NULL in case of error.
10072 * @input will be freed at parsing end.
10073 */
10074
10075xmlDtdPtr
10076xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input,
10077 xmlCharEncoding enc) {
10078 xmlDtdPtr ret = NULL;
10079 xmlParserCtxtPtr ctxt;
10080 xmlParserInputPtr pinput = NULL;
Daniel Veillard87a764e2001-06-20 17:41:10 +000010081 xmlChar start[4];
Owen Taylor3473f882001-02-23 17:55:21 +000010082
10083 if (input == NULL)
10084 return(NULL);
10085
10086 ctxt = xmlNewParserCtxt();
10087 if (ctxt == NULL) {
10088 return(NULL);
10089 }
10090
10091 /*
10092 * Set-up the SAX context
10093 */
10094 if (sax != NULL) {
10095 if (ctxt->sax != NULL)
10096 xmlFree(ctxt->sax);
10097 ctxt->sax = sax;
Daniel Veillard500a1de2004-03-22 15:22:58 +000010098 ctxt->userData = ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +000010099 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000010100 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010101
10102 /*
10103 * generate a parser input from the I/O handler
10104 */
10105
Daniel Veillard43caefb2003-12-07 19:32:22 +000010106 pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
Owen Taylor3473f882001-02-23 17:55:21 +000010107 if (pinput == NULL) {
10108 if (sax != NULL) ctxt->sax = NULL;
10109 xmlFreeParserCtxt(ctxt);
10110 return(NULL);
10111 }
10112
10113 /*
10114 * plug some encoding conversion routines here.
10115 */
10116 xmlPushInput(ctxt, pinput);
Daniel Veillard43caefb2003-12-07 19:32:22 +000010117 if (enc != XML_CHAR_ENCODING_NONE) {
10118 xmlSwitchEncoding(ctxt, enc);
10119 }
Owen Taylor3473f882001-02-23 17:55:21 +000010120
10121 pinput->filename = NULL;
10122 pinput->line = 1;
10123 pinput->col = 1;
10124 pinput->base = ctxt->input->cur;
10125 pinput->cur = ctxt->input->cur;
10126 pinput->free = NULL;
10127
10128 /*
10129 * let's parse that entity knowing it's an external subset.
10130 */
10131 ctxt->inSubset = 2;
10132 ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
10133 ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none",
10134 BAD_CAST "none", BAD_CAST "none");
Daniel Veillard87a764e2001-06-20 17:41:10 +000010135
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010136 if ((enc == XML_CHAR_ENCODING_NONE) &&
10137 ((ctxt->input->end - ctxt->input->cur) >= 4)) {
Daniel Veillard87a764e2001-06-20 17:41:10 +000010138 /*
10139 * Get the 4 first bytes and decode the charset
10140 * if enc != XML_CHAR_ENCODING_NONE
10141 * plug some encoding conversion routines.
10142 */
10143 start[0] = RAW;
10144 start[1] = NXT(1);
10145 start[2] = NXT(2);
10146 start[3] = NXT(3);
10147 enc = xmlDetectCharEncoding(start, 4);
10148 if (enc != XML_CHAR_ENCODING_NONE) {
10149 xmlSwitchEncoding(ctxt, enc);
10150 }
10151 }
10152
Owen Taylor3473f882001-02-23 17:55:21 +000010153 xmlParseExternalSubset(ctxt, BAD_CAST "none", BAD_CAST "none");
10154
10155 if (ctxt->myDoc != NULL) {
10156 if (ctxt->wellFormed) {
10157 ret = ctxt->myDoc->extSubset;
10158 ctxt->myDoc->extSubset = NULL;
Daniel Veillard329456a2003-04-26 21:21:00 +000010159 if (ret != NULL) {
10160 xmlNodePtr tmp;
10161
10162 ret->doc = NULL;
10163 tmp = ret->children;
10164 while (tmp != NULL) {
10165 tmp->doc = NULL;
10166 tmp = tmp->next;
10167 }
10168 }
Owen Taylor3473f882001-02-23 17:55:21 +000010169 } else {
10170 ret = NULL;
10171 }
10172 xmlFreeDoc(ctxt->myDoc);
10173 ctxt->myDoc = NULL;
10174 }
10175 if (sax != NULL) ctxt->sax = NULL;
10176 xmlFreeParserCtxt(ctxt);
10177
10178 return(ret);
10179}
10180
10181/**
10182 * xmlSAXParseDTD:
10183 * @sax: the SAX handler block
10184 * @ExternalID: a NAME* containing the External ID of the DTD
10185 * @SystemID: a NAME* containing the URL to the DTD
10186 *
10187 * Load and parse an external subset.
10188 *
10189 * Returns the resulting xmlDtdPtr or NULL in case of error.
10190 */
10191
10192xmlDtdPtr
10193xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID,
10194 const xmlChar *SystemID) {
10195 xmlDtdPtr ret = NULL;
10196 xmlParserCtxtPtr ctxt;
10197 xmlParserInputPtr input = NULL;
10198 xmlCharEncoding enc;
Igor Zlatkovic07d59762004-08-24 19:12:51 +000010199 xmlChar* systemIdCanonic;
Owen Taylor3473f882001-02-23 17:55:21 +000010200
10201 if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL);
10202
10203 ctxt = xmlNewParserCtxt();
10204 if (ctxt == NULL) {
10205 return(NULL);
10206 }
10207
10208 /*
10209 * Set-up the SAX context
10210 */
10211 if (sax != NULL) {
10212 if (ctxt->sax != NULL)
10213 xmlFree(ctxt->sax);
10214 ctxt->sax = sax;
Daniel Veillardbf1e3d82003-08-14 23:57:26 +000010215 ctxt->userData = ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +000010216 }
Igor Zlatkovic07d59762004-08-24 19:12:51 +000010217
10218 /*
10219 * Canonicalise the system ID
10220 */
10221 systemIdCanonic = xmlCanonicPath(SystemID);
Daniel Veillardc93a19f2004-10-04 11:53:20 +000010222 if ((SystemID != NULL) && (systemIdCanonic == NULL)) {
Igor Zlatkovic07d59762004-08-24 19:12:51 +000010223 xmlFreeParserCtxt(ctxt);
10224 return(NULL);
10225 }
Owen Taylor3473f882001-02-23 17:55:21 +000010226
10227 /*
10228 * Ask the Entity resolver to load the damn thing
10229 */
10230
10231 if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
Igor Zlatkovic07d59762004-08-24 19:12:51 +000010232 input = ctxt->sax->resolveEntity(ctxt, ExternalID, systemIdCanonic);
Owen Taylor3473f882001-02-23 17:55:21 +000010233 if (input == NULL) {
10234 if (sax != NULL) ctxt->sax = NULL;
10235 xmlFreeParserCtxt(ctxt);
Igor Zlatkovic07d59762004-08-24 19:12:51 +000010236 xmlFree(systemIdCanonic);
Owen Taylor3473f882001-02-23 17:55:21 +000010237 return(NULL);
10238 }
10239
10240 /*
10241 * plug some encoding conversion routines here.
10242 */
10243 xmlPushInput(ctxt, input);
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010244 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
10245 enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
10246 xmlSwitchEncoding(ctxt, enc);
10247 }
Owen Taylor3473f882001-02-23 17:55:21 +000010248
10249 if (input->filename == NULL)
Igor Zlatkovic07d59762004-08-24 19:12:51 +000010250 input->filename = (char *) systemIdCanonic;
10251 else
10252 xmlFree(systemIdCanonic);
Owen Taylor3473f882001-02-23 17:55:21 +000010253 input->line = 1;
10254 input->col = 1;
10255 input->base = ctxt->input->cur;
10256 input->cur = ctxt->input->cur;
10257 input->free = NULL;
10258
10259 /*
10260 * let's parse that entity knowing it's an external subset.
10261 */
10262 ctxt->inSubset = 2;
10263 ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
10264 ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none",
10265 ExternalID, SystemID);
10266 xmlParseExternalSubset(ctxt, ExternalID, SystemID);
10267
10268 if (ctxt->myDoc != NULL) {
10269 if (ctxt->wellFormed) {
10270 ret = ctxt->myDoc->extSubset;
10271 ctxt->myDoc->extSubset = NULL;
Daniel Veillardc5573462003-04-25 16:43:49 +000010272 if (ret != NULL) {
10273 xmlNodePtr tmp;
10274
10275 ret->doc = NULL;
10276 tmp = ret->children;
10277 while (tmp != NULL) {
10278 tmp->doc = NULL;
10279 tmp = tmp->next;
10280 }
10281 }
Owen Taylor3473f882001-02-23 17:55:21 +000010282 } else {
10283 ret = NULL;
10284 }
10285 xmlFreeDoc(ctxt->myDoc);
10286 ctxt->myDoc = NULL;
10287 }
10288 if (sax != NULL) ctxt->sax = NULL;
10289 xmlFreeParserCtxt(ctxt);
10290
10291 return(ret);
10292}
10293
Daniel Veillard4432df22003-09-28 18:58:27 +000010294
Owen Taylor3473f882001-02-23 17:55:21 +000010295/**
10296 * xmlParseDTD:
10297 * @ExternalID: a NAME* containing the External ID of the DTD
10298 * @SystemID: a NAME* containing the URL to the DTD
10299 *
10300 * Load and parse an external subset.
10301 *
10302 * Returns the resulting xmlDtdPtr or NULL in case of error.
10303 */
10304
10305xmlDtdPtr
10306xmlParseDTD(const xmlChar *ExternalID, const xmlChar *SystemID) {
10307 return(xmlSAXParseDTD(NULL, ExternalID, SystemID));
10308}
Daniel Veillard4432df22003-09-28 18:58:27 +000010309#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000010310
10311/************************************************************************
10312 * *
10313 * Front ends when parsing an Entity *
10314 * *
10315 ************************************************************************/
10316
10317/**
Owen Taylor3473f882001-02-23 17:55:21 +000010318 * xmlParseCtxtExternalEntity:
10319 * @ctx: the existing parsing context
10320 * @URL: the URL for the entity to load
10321 * @ID: the System ID for the entity to load
Daniel Veillardcda96922001-08-21 10:56:31 +000010322 * @lst: the return value for the set of parsed nodes
Owen Taylor3473f882001-02-23 17:55:21 +000010323 *
10324 * Parse an external general entity within an existing parsing context
10325 * An external general parsed entity is well-formed if it matches the
10326 * production labeled extParsedEnt.
10327 *
10328 * [78] extParsedEnt ::= TextDecl? content
10329 *
10330 * Returns 0 if the entity is well formed, -1 in case of args problem and
10331 * the parser error code otherwise
10332 */
10333
10334int
10335xmlParseCtxtExternalEntity(xmlParserCtxtPtr ctx, const xmlChar *URL,
Daniel Veillardcda96922001-08-21 10:56:31 +000010336 const xmlChar *ID, xmlNodePtr *lst) {
Owen Taylor3473f882001-02-23 17:55:21 +000010337 xmlParserCtxtPtr ctxt;
10338 xmlDocPtr newDoc;
10339 xmlSAXHandlerPtr oldsax = NULL;
10340 int ret = 0;
Daniel Veillard87a764e2001-06-20 17:41:10 +000010341 xmlChar start[4];
10342 xmlCharEncoding enc;
Owen Taylor3473f882001-02-23 17:55:21 +000010343
10344 if (ctx->depth > 40) {
10345 return(XML_ERR_ENTITY_LOOP);
10346 }
10347
Daniel Veillardcda96922001-08-21 10:56:31 +000010348 if (lst != NULL)
10349 *lst = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010350 if ((URL == NULL) && (ID == NULL))
10351 return(-1);
10352 if (ctx->myDoc == NULL) /* @@ relax but check for dereferences */
10353 return(-1);
10354
10355
10356 ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL);
10357 if (ctxt == NULL) return(-1);
10358 ctxt->userData = ctxt;
Daniel Veillarde2830f12003-01-08 17:47:49 +000010359 ctxt->_private = ctx->_private;
Owen Taylor3473f882001-02-23 17:55:21 +000010360 oldsax = ctxt->sax;
10361 ctxt->sax = ctx->sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000010362 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010363 newDoc = xmlNewDoc(BAD_CAST "1.0");
10364 if (newDoc == NULL) {
10365 xmlFreeParserCtxt(ctxt);
10366 return(-1);
10367 }
10368 if (ctx->myDoc != NULL) {
10369 newDoc->intSubset = ctx->myDoc->intSubset;
10370 newDoc->extSubset = ctx->myDoc->extSubset;
10371 }
10372 if (ctx->myDoc->URL != NULL) {
10373 newDoc->URL = xmlStrdup(ctx->myDoc->URL);
10374 }
10375 newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
10376 if (newDoc->children == NULL) {
10377 ctxt->sax = oldsax;
10378 xmlFreeParserCtxt(ctxt);
10379 newDoc->intSubset = NULL;
10380 newDoc->extSubset = NULL;
10381 xmlFreeDoc(newDoc);
10382 return(-1);
10383 }
10384 nodePush(ctxt, newDoc->children);
10385 if (ctx->myDoc == NULL) {
10386 ctxt->myDoc = newDoc;
10387 } else {
10388 ctxt->myDoc = ctx->myDoc;
10389 newDoc->children->doc = ctx->myDoc;
10390 }
10391
Daniel Veillard87a764e2001-06-20 17:41:10 +000010392 /*
10393 * Get the 4 first bytes and decode the charset
10394 * if enc != XML_CHAR_ENCODING_NONE
10395 * plug some encoding conversion routines.
10396 */
10397 GROW
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010398 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
10399 start[0] = RAW;
10400 start[1] = NXT(1);
10401 start[2] = NXT(2);
10402 start[3] = NXT(3);
10403 enc = xmlDetectCharEncoding(start, 4);
10404 if (enc != XML_CHAR_ENCODING_NONE) {
10405 xmlSwitchEncoding(ctxt, enc);
10406 }
Daniel Veillard87a764e2001-06-20 17:41:10 +000010407 }
10408
Owen Taylor3473f882001-02-23 17:55:21 +000010409 /*
10410 * Parse a possible text declaration first
10411 */
Daniel Veillarda07050d2003-10-19 14:46:32 +000010412 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +000010413 xmlParseTextDecl(ctxt);
10414 }
10415
10416 /*
10417 * Doing validity checking on chunk doesn't make sense
10418 */
10419 ctxt->instate = XML_PARSER_CONTENT;
10420 ctxt->validate = ctx->validate;
Daniel Veillard5f8d1a32003-03-23 21:02:00 +000010421 ctxt->valid = ctx->valid;
Owen Taylor3473f882001-02-23 17:55:21 +000010422 ctxt->loadsubset = ctx->loadsubset;
10423 ctxt->depth = ctx->depth + 1;
10424 ctxt->replaceEntities = ctx->replaceEntities;
10425 if (ctxt->validate) {
10426 ctxt->vctxt.error = ctx->vctxt.error;
10427 ctxt->vctxt.warning = ctx->vctxt.warning;
Owen Taylor3473f882001-02-23 17:55:21 +000010428 } else {
10429 ctxt->vctxt.error = NULL;
10430 ctxt->vctxt.warning = NULL;
10431 }
Daniel Veillarda9142e72001-06-19 11:07:54 +000010432 ctxt->vctxt.nodeTab = NULL;
10433 ctxt->vctxt.nodeNr = 0;
10434 ctxt->vctxt.nodeMax = 0;
10435 ctxt->vctxt.node = NULL;
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000010436 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
10437 ctxt->dict = ctx->dict;
Daniel Veillardfd343dc2003-10-31 10:55:22 +000010438 ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
10439 ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
10440 ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000010441 ctxt->dictNames = ctx->dictNames;
10442 ctxt->attsDefault = ctx->attsDefault;
10443 ctxt->attsSpecial = ctx->attsSpecial;
William M. Brack503b6102004-08-19 02:17:27 +000010444 ctxt->linenumbers = ctx->linenumbers;
Owen Taylor3473f882001-02-23 17:55:21 +000010445
10446 xmlParseContent(ctxt);
10447
Daniel Veillard5f8d1a32003-03-23 21:02:00 +000010448 ctx->validate = ctxt->validate;
10449 ctx->valid = ctxt->valid;
Owen Taylor3473f882001-02-23 17:55:21 +000010450 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010451 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010452 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010453 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010454 }
10455 if (ctxt->node != newDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010456 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010457 }
10458
10459 if (!ctxt->wellFormed) {
10460 if (ctxt->errNo == 0)
10461 ret = 1;
10462 else
10463 ret = ctxt->errNo;
10464 } else {
Daniel Veillardcda96922001-08-21 10:56:31 +000010465 if (lst != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +000010466 xmlNodePtr cur;
10467
10468 /*
10469 * Return the newly created nodeset after unlinking it from
10470 * they pseudo parent.
10471 */
10472 cur = newDoc->children->children;
Daniel Veillardcda96922001-08-21 10:56:31 +000010473 *lst = cur;
Owen Taylor3473f882001-02-23 17:55:21 +000010474 while (cur != NULL) {
10475 cur->parent = NULL;
10476 cur = cur->next;
10477 }
10478 newDoc->children->children = NULL;
10479 }
10480 ret = 0;
10481 }
10482 ctxt->sax = oldsax;
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000010483 ctxt->dict = NULL;
10484 ctxt->attsDefault = NULL;
10485 ctxt->attsSpecial = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010486 xmlFreeParserCtxt(ctxt);
10487 newDoc->intSubset = NULL;
10488 newDoc->extSubset = NULL;
10489 xmlFreeDoc(newDoc);
10490
10491 return(ret);
10492}
10493
10494/**
Daniel Veillard257d9102001-05-08 10:41:44 +000010495 * xmlParseExternalEntityPrivate:
Owen Taylor3473f882001-02-23 17:55:21 +000010496 * @doc: the document the chunk pertains to
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010497 * @oldctxt: the previous parser context if available
Owen Taylor3473f882001-02-23 17:55:21 +000010498 * @sax: the SAX handler bloc (possibly NULL)
10499 * @user_data: The user data returned on SAX callbacks (possibly NULL)
10500 * @depth: Used for loop detection, use 0
10501 * @URL: the URL for the entity to load
10502 * @ID: the System ID for the entity to load
10503 * @list: the return value for the set of parsed nodes
10504 *
Daniel Veillard257d9102001-05-08 10:41:44 +000010505 * Private version of xmlParseExternalEntity()
Owen Taylor3473f882001-02-23 17:55:21 +000010506 *
10507 * Returns 0 if the entity is well formed, -1 in case of args problem and
10508 * the parser error code otherwise
10509 */
10510
Daniel Veillard7d515752003-09-26 19:12:37 +000010511static xmlParserErrors
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010512xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt,
10513 xmlSAXHandlerPtr sax,
Daniel Veillard257d9102001-05-08 10:41:44 +000010514 void *user_data, int depth, const xmlChar *URL,
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010515 const xmlChar *ID, xmlNodePtr *list) {
Owen Taylor3473f882001-02-23 17:55:21 +000010516 xmlParserCtxtPtr ctxt;
10517 xmlDocPtr newDoc;
10518 xmlSAXHandlerPtr oldsax = NULL;
Daniel Veillard7d515752003-09-26 19:12:37 +000010519 xmlParserErrors ret = XML_ERR_OK;
Daniel Veillard87a764e2001-06-20 17:41:10 +000010520 xmlChar start[4];
10521 xmlCharEncoding enc;
Owen Taylor3473f882001-02-23 17:55:21 +000010522
10523 if (depth > 40) {
10524 return(XML_ERR_ENTITY_LOOP);
10525 }
10526
10527
10528
10529 if (list != NULL)
10530 *list = NULL;
10531 if ((URL == NULL) && (ID == NULL))
Daniel Veillard7d515752003-09-26 19:12:37 +000010532 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010533 if (doc == NULL) /* @@ relax but check for dereferences */
Daniel Veillard7d515752003-09-26 19:12:37 +000010534 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010535
10536
10537 ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL);
William M. Brackb670e2e2003-09-27 01:05:55 +000010538 if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY);
Owen Taylor3473f882001-02-23 17:55:21 +000010539 ctxt->userData = ctxt;
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010540 if (oldctxt != NULL) {
10541 ctxt->_private = oldctxt->_private;
10542 ctxt->loadsubset = oldctxt->loadsubset;
10543 ctxt->validate = oldctxt->validate;
10544 ctxt->external = oldctxt->external;
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010545 ctxt->record_info = oldctxt->record_info;
10546 ctxt->node_seq.maximum = oldctxt->node_seq.maximum;
10547 ctxt->node_seq.length = oldctxt->node_seq.length;
10548 ctxt->node_seq.buffer = oldctxt->node_seq.buffer;
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010549 } else {
10550 /*
10551 * Doing validity checking on chunk without context
10552 * doesn't make sense
10553 */
10554 ctxt->_private = NULL;
10555 ctxt->validate = 0;
10556 ctxt->external = 2;
10557 ctxt->loadsubset = 0;
10558 }
Owen Taylor3473f882001-02-23 17:55:21 +000010559 if (sax != NULL) {
10560 oldsax = ctxt->sax;
10561 ctxt->sax = sax;
10562 if (user_data != NULL)
10563 ctxt->userData = user_data;
10564 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000010565 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010566 newDoc = xmlNewDoc(BAD_CAST "1.0");
10567 if (newDoc == NULL) {
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010568 ctxt->node_seq.maximum = 0;
10569 ctxt->node_seq.length = 0;
10570 ctxt->node_seq.buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010571 xmlFreeParserCtxt(ctxt);
Daniel Veillard7d515752003-09-26 19:12:37 +000010572 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010573 }
10574 if (doc != NULL) {
10575 newDoc->intSubset = doc->intSubset;
10576 newDoc->extSubset = doc->extSubset;
10577 }
10578 if (doc->URL != NULL) {
10579 newDoc->URL = xmlStrdup(doc->URL);
10580 }
10581 newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
10582 if (newDoc->children == NULL) {
10583 if (sax != NULL)
10584 ctxt->sax = oldsax;
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010585 ctxt->node_seq.maximum = 0;
10586 ctxt->node_seq.length = 0;
10587 ctxt->node_seq.buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010588 xmlFreeParserCtxt(ctxt);
10589 newDoc->intSubset = NULL;
10590 newDoc->extSubset = NULL;
10591 xmlFreeDoc(newDoc);
Daniel Veillard7d515752003-09-26 19:12:37 +000010592 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010593 }
10594 nodePush(ctxt, newDoc->children);
10595 if (doc == NULL) {
10596 ctxt->myDoc = newDoc;
10597 } else {
10598 ctxt->myDoc = doc;
10599 newDoc->children->doc = doc;
10600 }
10601
Daniel Veillard87a764e2001-06-20 17:41:10 +000010602 /*
10603 * Get the 4 first bytes and decode the charset
10604 * if enc != XML_CHAR_ENCODING_NONE
10605 * plug some encoding conversion routines.
10606 */
10607 GROW;
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010608 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
10609 start[0] = RAW;
10610 start[1] = NXT(1);
10611 start[2] = NXT(2);
10612 start[3] = NXT(3);
10613 enc = xmlDetectCharEncoding(start, 4);
10614 if (enc != XML_CHAR_ENCODING_NONE) {
10615 xmlSwitchEncoding(ctxt, enc);
10616 }
Daniel Veillard87a764e2001-06-20 17:41:10 +000010617 }
10618
Owen Taylor3473f882001-02-23 17:55:21 +000010619 /*
10620 * Parse a possible text declaration first
10621 */
Daniel Veillarda07050d2003-10-19 14:46:32 +000010622 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +000010623 xmlParseTextDecl(ctxt);
10624 }
10625
Owen Taylor3473f882001-02-23 17:55:21 +000010626 ctxt->instate = XML_PARSER_CONTENT;
Owen Taylor3473f882001-02-23 17:55:21 +000010627 ctxt->depth = depth;
10628
10629 xmlParseContent(ctxt);
10630
Daniel Veillard561b7f82002-03-20 21:55:57 +000010631 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010632 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Daniel Veillard561b7f82002-03-20 21:55:57 +000010633 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010634 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010635 }
10636 if (ctxt->node != newDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010637 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010638 }
10639
10640 if (!ctxt->wellFormed) {
10641 if (ctxt->errNo == 0)
Daniel Veillard7d515752003-09-26 19:12:37 +000010642 ret = XML_ERR_INTERNAL_ERROR;
Owen Taylor3473f882001-02-23 17:55:21 +000010643 else
William M. Brack7b9154b2003-09-27 19:23:50 +000010644 ret = (xmlParserErrors)ctxt->errNo;
Owen Taylor3473f882001-02-23 17:55:21 +000010645 } else {
10646 if (list != NULL) {
10647 xmlNodePtr cur;
10648
10649 /*
10650 * Return the newly created nodeset after unlinking it from
10651 * they pseudo parent.
10652 */
10653 cur = newDoc->children->children;
10654 *list = cur;
10655 while (cur != NULL) {
10656 cur->parent = NULL;
10657 cur = cur->next;
10658 }
10659 newDoc->children->children = NULL;
10660 }
Daniel Veillard7d515752003-09-26 19:12:37 +000010661 ret = XML_ERR_OK;
Owen Taylor3473f882001-02-23 17:55:21 +000010662 }
10663 if (sax != NULL)
10664 ctxt->sax = oldsax;
Daniel Veillard0046c0f2003-02-23 13:52:30 +000010665 oldctxt->node_seq.maximum = ctxt->node_seq.maximum;
10666 oldctxt->node_seq.length = ctxt->node_seq.length;
10667 oldctxt->node_seq.buffer = ctxt->node_seq.buffer;
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010668 ctxt->node_seq.maximum = 0;
10669 ctxt->node_seq.length = 0;
10670 ctxt->node_seq.buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010671 xmlFreeParserCtxt(ctxt);
10672 newDoc->intSubset = NULL;
10673 newDoc->extSubset = NULL;
10674 xmlFreeDoc(newDoc);
10675
10676 return(ret);
10677}
10678
Daniel Veillard81273902003-09-30 00:43:48 +000010679#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000010680/**
Daniel Veillard257d9102001-05-08 10:41:44 +000010681 * xmlParseExternalEntity:
10682 * @doc: the document the chunk pertains to
10683 * @sax: the SAX handler bloc (possibly NULL)
10684 * @user_data: The user data returned on SAX callbacks (possibly NULL)
10685 * @depth: Used for loop detection, use 0
10686 * @URL: the URL for the entity to load
10687 * @ID: the System ID for the entity to load
Daniel Veillardcda96922001-08-21 10:56:31 +000010688 * @lst: the return value for the set of parsed nodes
Daniel Veillard257d9102001-05-08 10:41:44 +000010689 *
10690 * Parse an external general entity
10691 * An external general parsed entity is well-formed if it matches the
10692 * production labeled extParsedEnt.
10693 *
10694 * [78] extParsedEnt ::= TextDecl? content
10695 *
10696 * Returns 0 if the entity is well formed, -1 in case of args problem and
10697 * the parser error code otherwise
10698 */
10699
10700int
10701xmlParseExternalEntity(xmlDocPtr doc, xmlSAXHandlerPtr sax, void *user_data,
Daniel Veillardcda96922001-08-21 10:56:31 +000010702 int depth, const xmlChar *URL, const xmlChar *ID, xmlNodePtr *lst) {
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010703 return(xmlParseExternalEntityPrivate(doc, NULL, sax, user_data, depth, URL,
Daniel Veillardcda96922001-08-21 10:56:31 +000010704 ID, lst));
Daniel Veillard257d9102001-05-08 10:41:44 +000010705}
10706
10707/**
Daniel Veillarde020c3a2001-03-21 18:06:15 +000010708 * xmlParseBalancedChunkMemory:
Owen Taylor3473f882001-02-23 17:55:21 +000010709 * @doc: the document the chunk pertains to
10710 * @sax: the SAX handler bloc (possibly NULL)
10711 * @user_data: The user data returned on SAX callbacks (possibly NULL)
10712 * @depth: Used for loop detection, use 0
10713 * @string: the input string in UTF8 or ISO-Latin (zero terminated)
Daniel Veillardcda96922001-08-21 10:56:31 +000010714 * @lst: the return value for the set of parsed nodes
Owen Taylor3473f882001-02-23 17:55:21 +000010715 *
10716 * Parse a well-balanced chunk of an XML document
10717 * called by the parser
10718 * The allowed sequence for the Well Balanced Chunk is the one defined by
10719 * the content production in the XML grammar:
10720 *
10721 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
10722 *
10723 * Returns 0 if the chunk is well balanced, -1 in case of args problem and
10724 * the parser error code otherwise
10725 */
10726
10727int
10728xmlParseBalancedChunkMemory(xmlDocPtr doc, xmlSAXHandlerPtr sax,
Daniel Veillardcda96922001-08-21 10:56:31 +000010729 void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst) {
Daniel Veillard58e44c92002-08-02 22:19:49 +000010730 return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data,
10731 depth, string, lst, 0 );
10732}
Daniel Veillard81273902003-09-30 00:43:48 +000010733#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard58e44c92002-08-02 22:19:49 +000010734
10735/**
Daniel Veillard328f48c2002-11-15 15:24:34 +000010736 * xmlParseBalancedChunkMemoryInternal:
10737 * @oldctxt: the existing parsing context
10738 * @string: the input string in UTF8 or ISO-Latin (zero terminated)
10739 * @user_data: the user data field for the parser context
10740 * @lst: the return value for the set of parsed nodes
10741 *
10742 *
10743 * Parse a well-balanced chunk of an XML document
10744 * called by the parser
10745 * The allowed sequence for the Well Balanced Chunk is the one defined by
10746 * the content production in the XML grammar:
10747 *
10748 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
10749 *
Daniel Veillard7d515752003-09-26 19:12:37 +000010750 * Returns XML_ERR_OK if the chunk is well balanced, and the parser
10751 * error code otherwise
Daniel Veillard328f48c2002-11-15 15:24:34 +000010752 *
10753 * In case recover is set to 1, the nodelist will not be empty even if
10754 * the parsed chunk is not well balanced.
10755 */
Daniel Veillard7d515752003-09-26 19:12:37 +000010756static xmlParserErrors
Daniel Veillard328f48c2002-11-15 15:24:34 +000010757xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt,
10758 const xmlChar *string, void *user_data, xmlNodePtr *lst) {
10759 xmlParserCtxtPtr ctxt;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010760 xmlDocPtr newDoc = NULL;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010761 xmlSAXHandlerPtr oldsax = NULL;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010762 xmlNodePtr content = NULL;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010763 int size;
Daniel Veillard7d515752003-09-26 19:12:37 +000010764 xmlParserErrors ret = XML_ERR_OK;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010765
10766 if (oldctxt->depth > 40) {
10767 return(XML_ERR_ENTITY_LOOP);
10768 }
10769
10770
10771 if (lst != NULL)
10772 *lst = NULL;
10773 if (string == NULL)
William M. Brack7b9154b2003-09-27 19:23:50 +000010774 return(XML_ERR_INTERNAL_ERROR);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010775
10776 size = xmlStrlen(string);
10777
10778 ctxt = xmlCreateMemoryParserCtxt((char *) string, size);
William M. Brack7b9154b2003-09-27 19:23:50 +000010779 if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010780 if (user_data != NULL)
10781 ctxt->userData = user_data;
10782 else
10783 ctxt->userData = ctxt;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010784 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
10785 ctxt->dict = oldctxt->dict;
Daniel Veillardfd343dc2003-10-31 10:55:22 +000010786 ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
10787 ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
10788 ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010789
10790 oldsax = ctxt->sax;
10791 ctxt->sax = oldctxt->sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000010792 xmlDetectSAX2(ctxt);
Daniel Veillard87ab1c12003-12-21 13:01:56 +000010793 ctxt->replaceEntities = oldctxt->replaceEntities;
10794 ctxt->options = oldctxt->options;
Daniel Veillarde57ec792003-09-10 10:50:59 +000010795
Daniel Veillarde1ca5032002-12-09 14:13:43 +000010796 ctxt->_private = oldctxt->_private;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010797 if (oldctxt->myDoc == NULL) {
Daniel Veillard68e9e742002-11-16 15:35:11 +000010798 newDoc = xmlNewDoc(BAD_CAST "1.0");
10799 if (newDoc == NULL) {
10800 ctxt->sax = oldsax;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010801 ctxt->dict = NULL;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010802 xmlFreeParserCtxt(ctxt);
William M. Brack7b9154b2003-09-27 19:23:50 +000010803 return(XML_ERR_INTERNAL_ERROR);
Daniel Veillard68e9e742002-11-16 15:35:11 +000010804 }
Daniel Veillard328f48c2002-11-15 15:24:34 +000010805 ctxt->myDoc = newDoc;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010806 } else {
10807 ctxt->myDoc = oldctxt->myDoc;
10808 content = ctxt->myDoc->children;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010809 }
Daniel Veillard9bc53102002-11-25 13:20:04 +000010810 ctxt->myDoc->children = xmlNewDocNode(ctxt->myDoc, NULL,
Daniel Veillard68e9e742002-11-16 15:35:11 +000010811 BAD_CAST "pseudoroot", NULL);
10812 if (ctxt->myDoc->children == NULL) {
10813 ctxt->sax = oldsax;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010814 ctxt->dict = NULL;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010815 xmlFreeParserCtxt(ctxt);
10816 if (newDoc != NULL)
10817 xmlFreeDoc(newDoc);
William M. Brack7b9154b2003-09-27 19:23:50 +000010818 return(XML_ERR_INTERNAL_ERROR);
Daniel Veillard68e9e742002-11-16 15:35:11 +000010819 }
10820 nodePush(ctxt, ctxt->myDoc->children);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010821 ctxt->instate = XML_PARSER_CONTENT;
10822 ctxt->depth = oldctxt->depth + 1;
10823
Daniel Veillard328f48c2002-11-15 15:24:34 +000010824 ctxt->validate = 0;
10825 ctxt->loadsubset = oldctxt->loadsubset;
Daniel Veillardef8dd7b2003-03-23 12:02:56 +000010826 if ((oldctxt->validate) || (oldctxt->replaceEntities != 0)) {
10827 /*
10828 * ID/IDREF registration will be done in xmlValidateElement below
10829 */
10830 ctxt->loadsubset |= XML_SKIP_IDS;
10831 }
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010832 ctxt->dictNames = oldctxt->dictNames;
Daniel Veillard95d2d5b2003-10-27 14:54:49 +000010833 ctxt->attsDefault = oldctxt->attsDefault;
10834 ctxt->attsSpecial = oldctxt->attsSpecial;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010835
Daniel Veillard68e9e742002-11-16 15:35:11 +000010836 xmlParseContent(ctxt);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010837 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010838 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010839 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010840 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010841 }
Daniel Veillard68e9e742002-11-16 15:35:11 +000010842 if (ctxt->node != ctxt->myDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010843 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010844 }
10845
10846 if (!ctxt->wellFormed) {
10847 if (ctxt->errNo == 0)
Daniel Veillard7d515752003-09-26 19:12:37 +000010848 ret = XML_ERR_INTERNAL_ERROR;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010849 else
William M. Brack7b9154b2003-09-27 19:23:50 +000010850 ret = (xmlParserErrors)ctxt->errNo;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010851 } else {
William M. Brack7b9154b2003-09-27 19:23:50 +000010852 ret = XML_ERR_OK;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010853 }
10854
William M. Brack7b9154b2003-09-27 19:23:50 +000010855 if ((lst != NULL) && (ret == XML_ERR_OK)) {
Daniel Veillard328f48c2002-11-15 15:24:34 +000010856 xmlNodePtr cur;
10857
10858 /*
10859 * Return the newly created nodeset after unlinking it from
10860 * they pseudo parent.
10861 */
Daniel Veillard68e9e742002-11-16 15:35:11 +000010862 cur = ctxt->myDoc->children->children;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010863 *lst = cur;
10864 while (cur != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +000010865#ifdef LIBXML_VALID_ENABLED
Daniel Veillard8d589042003-02-04 15:07:21 +000010866 if (oldctxt->validate && oldctxt->wellFormed &&
10867 oldctxt->myDoc && oldctxt->myDoc->intSubset) {
10868 oldctxt->valid &= xmlValidateElement(&oldctxt->vctxt,
10869 oldctxt->myDoc, cur);
10870 }
Daniel Veillard4432df22003-09-28 18:58:27 +000010871#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard328f48c2002-11-15 15:24:34 +000010872 cur->parent = NULL;
10873 cur = cur->next;
10874 }
Daniel Veillard68e9e742002-11-16 15:35:11 +000010875 ctxt->myDoc->children->children = NULL;
10876 }
10877 if (ctxt->myDoc != NULL) {
10878 xmlFreeNode(ctxt->myDoc->children);
10879 ctxt->myDoc->children = content;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010880 }
10881
10882 ctxt->sax = oldsax;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010883 ctxt->dict = NULL;
Daniel Veillard95d2d5b2003-10-27 14:54:49 +000010884 ctxt->attsDefault = NULL;
10885 ctxt->attsSpecial = NULL;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010886 xmlFreeParserCtxt(ctxt);
Daniel Veillard68e9e742002-11-16 15:35:11 +000010887 if (newDoc != NULL)
10888 xmlFreeDoc(newDoc);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010889
10890 return(ret);
10891}
10892
Daniel Veillard29b17482004-08-16 00:39:03 +000010893/**
10894 * xmlParseInNodeContext:
10895 * @node: the context node
10896 * @data: the input string
10897 * @datalen: the input string length in bytes
10898 * @options: a combination of xmlParserOption
10899 * @lst: the return value for the set of parsed nodes
10900 *
10901 * Parse a well-balanced chunk of an XML document
10902 * within the context (DTD, namespaces, etc ...) of the given node.
10903 *
10904 * The allowed sequence for the data is a Well Balanced Chunk defined by
10905 * the content production in the XML grammar:
10906 *
10907 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
10908 *
10909 * Returns XML_ERR_OK if the chunk is well balanced, and the parser
10910 * error code otherwise
10911 */
10912xmlParserErrors
10913xmlParseInNodeContext(xmlNodePtr node, const char *data, int datalen,
10914 int options, xmlNodePtr *lst) {
10915#ifdef SAX2
10916 xmlParserCtxtPtr ctxt;
10917 xmlDocPtr doc = NULL;
10918 xmlNodePtr fake, cur;
10919 int nsnr = 0;
10920
10921 xmlParserErrors ret = XML_ERR_OK;
10922
10923 /*
10924 * check all input parameters, grab the document
10925 */
10926 if ((lst == NULL) || (node == NULL) || (data == NULL) || (datalen < 0))
10927 return(XML_ERR_INTERNAL_ERROR);
10928 switch (node->type) {
10929 case XML_ELEMENT_NODE:
10930 case XML_ATTRIBUTE_NODE:
10931 case XML_TEXT_NODE:
10932 case XML_CDATA_SECTION_NODE:
10933 case XML_ENTITY_REF_NODE:
10934 case XML_PI_NODE:
10935 case XML_COMMENT_NODE:
10936 case XML_DOCUMENT_NODE:
10937 case XML_HTML_DOCUMENT_NODE:
10938 break;
10939 default:
10940 return(XML_ERR_INTERNAL_ERROR);
10941
10942 }
10943 while ((node != NULL) && (node->type != XML_ELEMENT_NODE) &&
10944 (node->type != XML_DOCUMENT_NODE) &&
10945 (node->type != XML_HTML_DOCUMENT_NODE))
10946 node = node->parent;
10947 if (node == NULL)
10948 return(XML_ERR_INTERNAL_ERROR);
10949 if (node->type == XML_ELEMENT_NODE)
10950 doc = node->doc;
10951 else
10952 doc = (xmlDocPtr) node;
10953 if (doc == NULL)
10954 return(XML_ERR_INTERNAL_ERROR);
10955
10956 /*
10957 * allocate a context and set-up everything not related to the
10958 * node position in the tree
10959 */
10960 if (doc->type == XML_DOCUMENT_NODE)
10961 ctxt = xmlCreateMemoryParserCtxt((char *) data, datalen);
10962#ifdef LIBXML_HTML_ENABLED
10963 else if (doc->type == XML_HTML_DOCUMENT_NODE)
10964 ctxt = htmlCreateMemoryParserCtxt((char *) data, datalen);
10965#endif
10966 else
10967 return(XML_ERR_INTERNAL_ERROR);
10968
10969 if (ctxt == NULL)
10970 return(XML_ERR_NO_MEMORY);
10971 fake = xmlNewComment(NULL);
10972 if (fake == NULL) {
10973 xmlFreeParserCtxt(ctxt);
10974 return(XML_ERR_NO_MEMORY);
10975 }
10976 xmlAddChild(node, fake);
William M. Brackc3f81342004-10-03 01:22:44 +000010977
10978 /*
10979 * Use input doc's dict if present, else assure XML_PARSE_NODICT is set.
10980 * We need a dictionary for xmlDetectSAX2, so if there's no doc dict
10981 * we must wait until the last moment to free the original one.
10982 */
Daniel Veillard29b17482004-08-16 00:39:03 +000010983 if (doc->dict != NULL) {
William M. Brackc3f81342004-10-03 01:22:44 +000010984 if (ctxt->dict != NULL)
Daniel Veillard29b17482004-08-16 00:39:03 +000010985 xmlDictFree(ctxt->dict);
10986 ctxt->dict = doc->dict;
William M. Brackc3f81342004-10-03 01:22:44 +000010987 } else
10988 options |= XML_PARSE_NODICT;
10989
10990 xmlCtxtUseOptions(ctxt, options);
Daniel Veillard29b17482004-08-16 00:39:03 +000010991 xmlDetectSAX2(ctxt);
10992 ctxt->myDoc = doc;
10993
10994 if (node->type == XML_ELEMENT_NODE) {
10995 nodePush(ctxt, node);
10996 /*
10997 * initialize the SAX2 namespaces stack
10998 */
10999 cur = node;
11000 while ((cur != NULL) && (cur->type == XML_ELEMENT_NODE)) {
11001 xmlNsPtr ns = cur->nsDef;
11002 const xmlChar *iprefix, *ihref;
11003
11004 while (ns != NULL) {
11005 if (ctxt->dict) {
11006 iprefix = xmlDictLookup(ctxt->dict, ns->prefix, -1);
11007 ihref = xmlDictLookup(ctxt->dict, ns->href, -1);
11008 } else {
11009 iprefix = ns->prefix;
11010 ihref = ns->href;
11011 }
11012
11013 if (xmlGetNamespace(ctxt, iprefix) == NULL) {
11014 nsPush(ctxt, iprefix, ihref);
11015 nsnr++;
11016 }
11017 ns = ns->next;
11018 }
11019 cur = cur->parent;
11020 }
11021 ctxt->instate = XML_PARSER_CONTENT;
11022 }
11023
11024 if ((ctxt->validate) || (ctxt->replaceEntities != 0)) {
11025 /*
11026 * ID/IDREF registration will be done in xmlValidateElement below
11027 */
11028 ctxt->loadsubset |= XML_SKIP_IDS;
11029 }
11030
11031 xmlParseContent(ctxt);
11032 nsPop(ctxt, nsnr);
11033 if ((RAW == '<') && (NXT(1) == '/')) {
11034 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
11035 } else if (RAW != 0) {
11036 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
11037 }
11038 if ((ctxt->node != NULL) && (ctxt->node != node)) {
11039 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
11040 ctxt->wellFormed = 0;
11041 }
11042
11043 if (!ctxt->wellFormed) {
11044 if (ctxt->errNo == 0)
11045 ret = XML_ERR_INTERNAL_ERROR;
11046 else
11047 ret = (xmlParserErrors)ctxt->errNo;
11048 } else {
11049 ret = XML_ERR_OK;
11050 }
11051
11052 /*
11053 * Return the newly created nodeset after unlinking it from
11054 * the pseudo sibling.
11055 */
11056
11057 cur = fake->next;
11058 fake->next = NULL;
11059 node->last = fake;
11060
11061 if (cur != NULL) {
11062 cur->prev = NULL;
11063 }
11064
11065 *lst = cur;
11066
11067 while (cur != NULL) {
11068 cur->parent = NULL;
11069 cur = cur->next;
11070 }
11071
11072 xmlUnlinkNode(fake);
11073 xmlFreeNode(fake);
11074
11075
11076 if (ret != XML_ERR_OK) {
11077 xmlFreeNodeList(*lst);
11078 *lst = NULL;
11079 }
William M. Brackc3f81342004-10-03 01:22:44 +000011080
William M. Brackb7b54de2004-10-06 16:38:01 +000011081 if (doc->dict != NULL)
11082 ctxt->dict = NULL;
Daniel Veillard29b17482004-08-16 00:39:03 +000011083 xmlFreeParserCtxt(ctxt);
11084
11085 return(ret);
11086#else /* !SAX2 */
11087 return(XML_ERR_INTERNAL_ERROR);
11088#endif
11089}
11090
Daniel Veillard81273902003-09-30 00:43:48 +000011091#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard328f48c2002-11-15 15:24:34 +000011092/**
Daniel Veillard58e44c92002-08-02 22:19:49 +000011093 * xmlParseBalancedChunkMemoryRecover:
11094 * @doc: the document the chunk pertains to
11095 * @sax: the SAX handler bloc (possibly NULL)
11096 * @user_data: The user data returned on SAX callbacks (possibly NULL)
11097 * @depth: Used for loop detection, use 0
11098 * @string: the input string in UTF8 or ISO-Latin (zero terminated)
11099 * @lst: the return value for the set of parsed nodes
11100 * @recover: return nodes even if the data is broken (use 0)
11101 *
11102 *
11103 * Parse a well-balanced chunk of an XML document
11104 * called by the parser
11105 * The allowed sequence for the Well Balanced Chunk is the one defined by
11106 * the content production in the XML grammar:
11107 *
11108 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
11109 *
11110 * Returns 0 if the chunk is well balanced, -1 in case of args problem and
11111 * the parser error code otherwise
11112 *
11113 * In case recover is set to 1, the nodelist will not be empty even if
11114 * the parsed chunk is not well balanced.
11115 */
11116int
11117xmlParseBalancedChunkMemoryRecover(xmlDocPtr doc, xmlSAXHandlerPtr sax,
11118 void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst,
11119 int recover) {
Owen Taylor3473f882001-02-23 17:55:21 +000011120 xmlParserCtxtPtr ctxt;
11121 xmlDocPtr newDoc;
11122 xmlSAXHandlerPtr oldsax = NULL;
Daniel Veillard935494a2002-10-22 14:22:46 +000011123 xmlNodePtr content;
Owen Taylor3473f882001-02-23 17:55:21 +000011124 int size;
11125 int ret = 0;
11126
11127 if (depth > 40) {
11128 return(XML_ERR_ENTITY_LOOP);
11129 }
11130
11131
Daniel Veillardcda96922001-08-21 10:56:31 +000011132 if (lst != NULL)
11133 *lst = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000011134 if (string == NULL)
11135 return(-1);
11136
11137 size = xmlStrlen(string);
11138
11139 ctxt = xmlCreateMemoryParserCtxt((char *) string, size);
11140 if (ctxt == NULL) return(-1);
11141 ctxt->userData = ctxt;
11142 if (sax != NULL) {
11143 oldsax = ctxt->sax;
11144 ctxt->sax = sax;
11145 if (user_data != NULL)
11146 ctxt->userData = user_data;
11147 }
11148 newDoc = xmlNewDoc(BAD_CAST "1.0");
11149 if (newDoc == NULL) {
11150 xmlFreeParserCtxt(ctxt);
11151 return(-1);
11152 }
11153 if (doc != NULL) {
11154 newDoc->intSubset = doc->intSubset;
11155 newDoc->extSubset = doc->extSubset;
11156 }
11157 newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
11158 if (newDoc->children == NULL) {
11159 if (sax != NULL)
11160 ctxt->sax = oldsax;
11161 xmlFreeParserCtxt(ctxt);
11162 newDoc->intSubset = NULL;
11163 newDoc->extSubset = NULL;
11164 xmlFreeDoc(newDoc);
11165 return(-1);
11166 }
11167 nodePush(ctxt, newDoc->children);
11168 if (doc == NULL) {
11169 ctxt->myDoc = newDoc;
11170 } else {
Daniel Veillard42766c02002-08-22 20:52:17 +000011171 ctxt->myDoc = newDoc;
Owen Taylor3473f882001-02-23 17:55:21 +000011172 newDoc->children->doc = doc;
11173 }
11174 ctxt->instate = XML_PARSER_CONTENT;
11175 ctxt->depth = depth;
11176
11177 /*
11178 * Doing validity checking on chunk doesn't make sense
11179 */
11180 ctxt->validate = 0;
11181 ctxt->loadsubset = 0;
Daniel Veillarde57ec792003-09-10 10:50:59 +000011182 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011183
Daniel Veillardb39bc392002-10-26 19:29:51 +000011184 if ( doc != NULL ){
11185 content = doc->children;
11186 doc->children = NULL;
11187 xmlParseContent(ctxt);
11188 doc->children = content;
11189 }
11190 else {
11191 xmlParseContent(ctxt);
11192 }
Owen Taylor3473f882001-02-23 17:55:21 +000011193 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000011194 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000011195 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000011196 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000011197 }
11198 if (ctxt->node != newDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000011199 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000011200 }
11201
11202 if (!ctxt->wellFormed) {
11203 if (ctxt->errNo == 0)
11204 ret = 1;
11205 else
11206 ret = ctxt->errNo;
11207 } else {
Daniel Veillard58e44c92002-08-02 22:19:49 +000011208 ret = 0;
11209 }
11210
11211 if (lst != NULL && (ret == 0 || recover == 1)) {
11212 xmlNodePtr cur;
Owen Taylor3473f882001-02-23 17:55:21 +000011213
11214 /*
11215 * Return the newly created nodeset after unlinking it from
11216 * they pseudo parent.
11217 */
11218 cur = newDoc->children->children;
Daniel Veillardcda96922001-08-21 10:56:31 +000011219 *lst = cur;
Owen Taylor3473f882001-02-23 17:55:21 +000011220 while (cur != NULL) {
11221 cur->parent = NULL;
11222 cur = cur->next;
11223 }
11224 newDoc->children->children = NULL;
11225 }
Daniel Veillard58e44c92002-08-02 22:19:49 +000011226
Owen Taylor3473f882001-02-23 17:55:21 +000011227 if (sax != NULL)
11228 ctxt->sax = oldsax;
11229 xmlFreeParserCtxt(ctxt);
11230 newDoc->intSubset = NULL;
11231 newDoc->extSubset = NULL;
11232 xmlFreeDoc(newDoc);
11233
11234 return(ret);
11235}
11236
11237/**
11238 * xmlSAXParseEntity:
11239 * @sax: the SAX handler block
11240 * @filename: the filename
11241 *
11242 * parse an XML external entity out of context and build a tree.
11243 * It use the given SAX function block to handle the parsing callback.
11244 * If sax is NULL, fallback to the default DOM tree building routines.
11245 *
11246 * [78] extParsedEnt ::= TextDecl? content
11247 *
11248 * This correspond to a "Well Balanced" chunk
11249 *
11250 * Returns the resulting document tree
11251 */
11252
11253xmlDocPtr
11254xmlSAXParseEntity(xmlSAXHandlerPtr sax, const char *filename) {
11255 xmlDocPtr ret;
11256 xmlParserCtxtPtr ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +000011257
11258 ctxt = xmlCreateFileParserCtxt(filename);
11259 if (ctxt == NULL) {
11260 return(NULL);
11261 }
11262 if (sax != NULL) {
11263 if (ctxt->sax != NULL)
11264 xmlFree(ctxt->sax);
11265 ctxt->sax = sax;
11266 ctxt->userData = NULL;
11267 }
11268
Owen Taylor3473f882001-02-23 17:55:21 +000011269 xmlParseExtParsedEnt(ctxt);
11270
11271 if (ctxt->wellFormed)
11272 ret = ctxt->myDoc;
11273 else {
11274 ret = NULL;
11275 xmlFreeDoc(ctxt->myDoc);
11276 ctxt->myDoc = NULL;
11277 }
11278 if (sax != NULL)
11279 ctxt->sax = NULL;
11280 xmlFreeParserCtxt(ctxt);
11281
11282 return(ret);
11283}
11284
11285/**
11286 * xmlParseEntity:
11287 * @filename: the filename
11288 *
11289 * parse an XML external entity out of context and build a tree.
11290 *
11291 * [78] extParsedEnt ::= TextDecl? content
11292 *
11293 * This correspond to a "Well Balanced" chunk
11294 *
11295 * Returns the resulting document tree
11296 */
11297
11298xmlDocPtr
11299xmlParseEntity(const char *filename) {
11300 return(xmlSAXParseEntity(NULL, filename));
11301}
Daniel Veillard81273902003-09-30 00:43:48 +000011302#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011303
11304/**
11305 * xmlCreateEntityParserCtxt:
11306 * @URL: the entity URL
11307 * @ID: the entity PUBLIC ID
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011308 * @base: a possible base for the target URI
Owen Taylor3473f882001-02-23 17:55:21 +000011309 *
11310 * Create a parser context for an external entity
11311 * Automatic support for ZLIB/Compress compressed document is provided
11312 * by default if found at compile-time.
11313 *
11314 * Returns the new parser context or NULL
11315 */
11316xmlParserCtxtPtr
11317xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID,
11318 const xmlChar *base) {
11319 xmlParserCtxtPtr ctxt;
11320 xmlParserInputPtr inputStream;
11321 char *directory = NULL;
11322 xmlChar *uri;
11323
11324 ctxt = xmlNewParserCtxt();
11325 if (ctxt == NULL) {
11326 return(NULL);
11327 }
11328
11329 uri = xmlBuildURI(URL, base);
11330
11331 if (uri == NULL) {
11332 inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt);
11333 if (inputStream == NULL) {
11334 xmlFreeParserCtxt(ctxt);
11335 return(NULL);
11336 }
11337
11338 inputPush(ctxt, inputStream);
11339
11340 if ((ctxt->directory == NULL) && (directory == NULL))
11341 directory = xmlParserGetDirectory((char *)URL);
11342 if ((ctxt->directory == NULL) && (directory != NULL))
11343 ctxt->directory = directory;
11344 } else {
11345 inputStream = xmlLoadExternalEntity((char *)uri, (char *)ID, ctxt);
11346 if (inputStream == NULL) {
11347 xmlFree(uri);
11348 xmlFreeParserCtxt(ctxt);
11349 return(NULL);
11350 }
11351
11352 inputPush(ctxt, inputStream);
11353
11354 if ((ctxt->directory == NULL) && (directory == NULL))
11355 directory = xmlParserGetDirectory((char *)uri);
11356 if ((ctxt->directory == NULL) && (directory != NULL))
11357 ctxt->directory = directory;
11358 xmlFree(uri);
11359 }
Owen Taylor3473f882001-02-23 17:55:21 +000011360 return(ctxt);
11361}
11362
11363/************************************************************************
11364 * *
11365 * Front ends when parsing from a file *
11366 * *
11367 ************************************************************************/
11368
11369/**
Daniel Veillard61b93382003-11-03 14:28:31 +000011370 * xmlCreateURLParserCtxt:
11371 * @filename: the filename or URL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000011372 * @options: a combination of xmlParserOption
Owen Taylor3473f882001-02-23 17:55:21 +000011373 *
Daniel Veillard61b93382003-11-03 14:28:31 +000011374 * Create a parser context for a file or URL content.
Owen Taylor3473f882001-02-23 17:55:21 +000011375 * Automatic support for ZLIB/Compress compressed document is provided
Daniel Veillard61b93382003-11-03 14:28:31 +000011376 * by default if found at compile-time and for file accesses
Owen Taylor3473f882001-02-23 17:55:21 +000011377 *
11378 * Returns the new parser context or NULL
11379 */
11380xmlParserCtxtPtr
Daniel Veillard61b93382003-11-03 14:28:31 +000011381xmlCreateURLParserCtxt(const char *filename, int options)
Owen Taylor3473f882001-02-23 17:55:21 +000011382{
11383 xmlParserCtxtPtr ctxt;
11384 xmlParserInputPtr inputStream;
Owen Taylor3473f882001-02-23 17:55:21 +000011385 char *directory = NULL;
11386
Owen Taylor3473f882001-02-23 17:55:21 +000011387 ctxt = xmlNewParserCtxt();
11388 if (ctxt == NULL) {
Daniel Veillard81273902003-09-30 00:43:48 +000011389 xmlErrMemory(NULL, "cannot allocate parser context");
Owen Taylor3473f882001-02-23 17:55:21 +000011390 return(NULL);
11391 }
11392
Daniel Veillard61b93382003-11-03 14:28:31 +000011393 if (options != 0)
11394 xmlCtxtUseOptions(ctxt, options);
Igor Zlatkovicce076162003-02-23 13:39:39 +000011395
Daniel Veillard4e9b1bc2003-06-09 10:30:33 +000011396 inputStream = xmlLoadExternalEntity(filename, NULL, ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011397 if (inputStream == NULL) {
11398 xmlFreeParserCtxt(ctxt);
11399 return(NULL);
11400 }
11401
Owen Taylor3473f882001-02-23 17:55:21 +000011402 inputPush(ctxt, inputStream);
11403 if ((ctxt->directory == NULL) && (directory == NULL))
Igor Zlatkovic5f9fada2003-02-19 14:51:00 +000011404 directory = xmlParserGetDirectory(filename);
Owen Taylor3473f882001-02-23 17:55:21 +000011405 if ((ctxt->directory == NULL) && (directory != NULL))
11406 ctxt->directory = directory;
11407
11408 return(ctxt);
11409}
11410
Daniel Veillard61b93382003-11-03 14:28:31 +000011411/**
11412 * xmlCreateFileParserCtxt:
11413 * @filename: the filename
11414 *
11415 * Create a parser context for a file content.
11416 * Automatic support for ZLIB/Compress compressed document is provided
11417 * by default if found at compile-time.
11418 *
11419 * Returns the new parser context or NULL
11420 */
11421xmlParserCtxtPtr
11422xmlCreateFileParserCtxt(const char *filename)
11423{
11424 return(xmlCreateURLParserCtxt(filename, 0));
11425}
11426
Daniel Veillard81273902003-09-30 00:43:48 +000011427#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000011428/**
Daniel Veillarda293c322001-10-02 13:54:14 +000011429 * xmlSAXParseFileWithData:
Owen Taylor3473f882001-02-23 17:55:21 +000011430 * @sax: the SAX handler block
11431 * @filename: the filename
11432 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11433 * documents
Daniel Veillarda293c322001-10-02 13:54:14 +000011434 * @data: the userdata
Owen Taylor3473f882001-02-23 17:55:21 +000011435 *
11436 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11437 * compressed document is provided by default if found at compile-time.
11438 * It use the given SAX function block to handle the parsing callback.
11439 * If sax is NULL, fallback to the default DOM tree building routines.
11440 *
Daniel Veillarde19fc232002-04-22 16:01:24 +000011441 * User data (void *) is stored within the parser context in the
11442 * context's _private member, so it is available nearly everywhere in libxml
Daniel Veillarda293c322001-10-02 13:54:14 +000011443 *
Owen Taylor3473f882001-02-23 17:55:21 +000011444 * Returns the resulting document tree
11445 */
11446
11447xmlDocPtr
Daniel Veillarda293c322001-10-02 13:54:14 +000011448xmlSAXParseFileWithData(xmlSAXHandlerPtr sax, const char *filename,
11449 int recovery, void *data) {
Owen Taylor3473f882001-02-23 17:55:21 +000011450 xmlDocPtr ret;
11451 xmlParserCtxtPtr ctxt;
11452 char *directory = NULL;
11453
Daniel Veillard635ef722001-10-29 11:48:19 +000011454 xmlInitParser();
11455
Owen Taylor3473f882001-02-23 17:55:21 +000011456 ctxt = xmlCreateFileParserCtxt(filename);
11457 if (ctxt == NULL) {
11458 return(NULL);
11459 }
11460 if (sax != NULL) {
11461 if (ctxt->sax != NULL)
11462 xmlFree(ctxt->sax);
11463 ctxt->sax = sax;
Owen Taylor3473f882001-02-23 17:55:21 +000011464 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000011465 xmlDetectSAX2(ctxt);
Daniel Veillarda293c322001-10-02 13:54:14 +000011466 if (data!=NULL) {
Daniel Veillardc790bf42003-10-11 10:50:10 +000011467 ctxt->_private = data;
Daniel Veillarda293c322001-10-02 13:54:14 +000011468 }
Owen Taylor3473f882001-02-23 17:55:21 +000011469
11470 if ((ctxt->directory == NULL) && (directory == NULL))
11471 directory = xmlParserGetDirectory(filename);
11472 if ((ctxt->directory == NULL) && (directory != NULL))
11473 ctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
11474
Daniel Veillarddad3f682002-11-17 16:47:27 +000011475 ctxt->recovery = recovery;
11476
Owen Taylor3473f882001-02-23 17:55:21 +000011477 xmlParseDocument(ctxt);
11478
William M. Brackc07329e2003-09-08 01:57:30 +000011479 if ((ctxt->wellFormed) || recovery) {
11480 ret = ctxt->myDoc;
Daniel Veillardb65e12e2003-10-08 21:33:28 +000011481 if (ret != NULL) {
11482 if (ctxt->input->buf->compressed > 0)
11483 ret->compression = 9;
11484 else
11485 ret->compression = ctxt->input->buf->compressed;
11486 }
William M. Brackc07329e2003-09-08 01:57:30 +000011487 }
Owen Taylor3473f882001-02-23 17:55:21 +000011488 else {
11489 ret = NULL;
11490 xmlFreeDoc(ctxt->myDoc);
11491 ctxt->myDoc = NULL;
11492 }
11493 if (sax != NULL)
11494 ctxt->sax = NULL;
11495 xmlFreeParserCtxt(ctxt);
11496
11497 return(ret);
11498}
11499
11500/**
Daniel Veillarda293c322001-10-02 13:54:14 +000011501 * xmlSAXParseFile:
11502 * @sax: the SAX handler block
11503 * @filename: the filename
11504 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11505 * documents
11506 *
11507 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11508 * compressed document is provided by default if found at compile-time.
11509 * It use the given SAX function block to handle the parsing callback.
11510 * If sax is NULL, fallback to the default DOM tree building routines.
11511 *
11512 * Returns the resulting document tree
11513 */
11514
11515xmlDocPtr
11516xmlSAXParseFile(xmlSAXHandlerPtr sax, const char *filename,
11517 int recovery) {
11518 return(xmlSAXParseFileWithData(sax,filename,recovery,NULL));
11519}
11520
11521/**
Owen Taylor3473f882001-02-23 17:55:21 +000011522 * xmlRecoverDoc:
11523 * @cur: a pointer to an array of xmlChar
11524 *
11525 * parse an XML in-memory document and build a tree.
11526 * In the case the document is not Well Formed, a tree is built anyway
11527 *
11528 * Returns the resulting document tree
11529 */
11530
11531xmlDocPtr
11532xmlRecoverDoc(xmlChar *cur) {
11533 return(xmlSAXParseDoc(NULL, cur, 1));
11534}
11535
11536/**
11537 * xmlParseFile:
11538 * @filename: the filename
11539 *
11540 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11541 * compressed document is provided by default if found at compile-time.
11542 *
Daniel Veillard5d96fff2001-08-31 14:55:30 +000011543 * Returns the resulting document tree if the file was wellformed,
11544 * NULL otherwise.
Owen Taylor3473f882001-02-23 17:55:21 +000011545 */
11546
11547xmlDocPtr
11548xmlParseFile(const char *filename) {
11549 return(xmlSAXParseFile(NULL, filename, 0));
11550}
11551
11552/**
11553 * xmlRecoverFile:
11554 * @filename: the filename
11555 *
11556 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11557 * compressed document is provided by default if found at compile-time.
11558 * In the case the document is not Well Formed, a tree is built anyway
11559 *
11560 * Returns the resulting document tree
11561 */
11562
11563xmlDocPtr
11564xmlRecoverFile(const char *filename) {
11565 return(xmlSAXParseFile(NULL, filename, 1));
11566}
11567
11568
11569/**
11570 * xmlSetupParserForBuffer:
11571 * @ctxt: an XML parser context
11572 * @buffer: a xmlChar * buffer
11573 * @filename: a file name
11574 *
11575 * Setup the parser context to parse a new buffer; Clears any prior
11576 * contents from the parser context. The buffer parameter must not be
11577 * NULL, but the filename parameter can be
11578 */
11579void
11580xmlSetupParserForBuffer(xmlParserCtxtPtr ctxt, const xmlChar* buffer,
11581 const char* filename)
11582{
11583 xmlParserInputPtr input;
11584
11585 input = xmlNewInputStream(ctxt);
11586 if (input == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +000011587 xmlErrMemory(NULL, "parsing new buffer: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +000011588 xmlFree(ctxt);
11589 return;
11590 }
11591
11592 xmlClearParserCtxt(ctxt);
11593 if (filename != NULL)
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +000011594 input->filename = (char *) xmlCanonicPath((const xmlChar *)filename);
Owen Taylor3473f882001-02-23 17:55:21 +000011595 input->base = buffer;
11596 input->cur = buffer;
Daniel Veillard48b2f892001-02-25 16:11:03 +000011597 input->end = &buffer[xmlStrlen(buffer)];
Owen Taylor3473f882001-02-23 17:55:21 +000011598 inputPush(ctxt, input);
11599}
11600
11601/**
11602 * xmlSAXUserParseFile:
11603 * @sax: a SAX handler
11604 * @user_data: The user data returned on SAX callbacks
11605 * @filename: a file name
11606 *
11607 * parse an XML file and call the given SAX handler routines.
11608 * Automatic support for ZLIB/Compress compressed document is provided
11609 *
11610 * Returns 0 in case of success or a error number otherwise
11611 */
11612int
11613xmlSAXUserParseFile(xmlSAXHandlerPtr sax, void *user_data,
11614 const char *filename) {
11615 int ret = 0;
11616 xmlParserCtxtPtr ctxt;
11617
11618 ctxt = xmlCreateFileParserCtxt(filename);
11619 if (ctxt == NULL) return -1;
Daniel Veillard81273902003-09-30 00:43:48 +000011620#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +000011621 if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
Daniel Veillard81273902003-09-30 00:43:48 +000011622#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011623 xmlFree(ctxt->sax);
11624 ctxt->sax = sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000011625 xmlDetectSAX2(ctxt);
11626
Owen Taylor3473f882001-02-23 17:55:21 +000011627 if (user_data != NULL)
11628 ctxt->userData = user_data;
11629
11630 xmlParseDocument(ctxt);
11631
11632 if (ctxt->wellFormed)
11633 ret = 0;
11634 else {
11635 if (ctxt->errNo != 0)
11636 ret = ctxt->errNo;
11637 else
11638 ret = -1;
11639 }
11640 if (sax != NULL)
11641 ctxt->sax = NULL;
11642 xmlFreeParserCtxt(ctxt);
11643
11644 return ret;
11645}
Daniel Veillard81273902003-09-30 00:43:48 +000011646#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011647
11648/************************************************************************
11649 * *
11650 * Front ends when parsing from memory *
11651 * *
11652 ************************************************************************/
11653
11654/**
11655 * xmlCreateMemoryParserCtxt:
11656 * @buffer: a pointer to a char array
11657 * @size: the size of the array
11658 *
11659 * Create a parser context for an XML in-memory document.
11660 *
11661 * Returns the new parser context or NULL
11662 */
11663xmlParserCtxtPtr
Daniel Veillardfd7ddca2001-05-16 10:57:35 +000011664xmlCreateMemoryParserCtxt(const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000011665 xmlParserCtxtPtr ctxt;
11666 xmlParserInputPtr input;
11667 xmlParserInputBufferPtr buf;
11668
11669 if (buffer == NULL)
11670 return(NULL);
11671 if (size <= 0)
11672 return(NULL);
11673
11674 ctxt = xmlNewParserCtxt();
11675 if (ctxt == NULL)
11676 return(NULL);
11677
Daniel Veillard53350552003-09-18 13:35:51 +000011678 /* TODO: xmlParserInputBufferCreateStatic, requires some serious changes */
Owen Taylor3473f882001-02-23 17:55:21 +000011679 buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
Daniel Veillarda7e05b42002-11-19 08:11:14 +000011680 if (buf == NULL) {
11681 xmlFreeParserCtxt(ctxt);
11682 return(NULL);
11683 }
Owen Taylor3473f882001-02-23 17:55:21 +000011684
11685 input = xmlNewInputStream(ctxt);
11686 if (input == NULL) {
Daniel Veillarda7e05b42002-11-19 08:11:14 +000011687 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +000011688 xmlFreeParserCtxt(ctxt);
11689 return(NULL);
11690 }
11691
11692 input->filename = NULL;
11693 input->buf = buf;
11694 input->base = input->buf->buffer->content;
11695 input->cur = input->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +000011696 input->end = &input->buf->buffer->content[input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +000011697
11698 inputPush(ctxt, input);
11699 return(ctxt);
11700}
11701
Daniel Veillard81273902003-09-30 00:43:48 +000011702#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000011703/**
Daniel Veillard8606bbb2002-11-12 12:36:52 +000011704 * xmlSAXParseMemoryWithData:
11705 * @sax: the SAX handler block
11706 * @buffer: an pointer to a char array
11707 * @size: the size of the array
11708 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11709 * documents
11710 * @data: the userdata
11711 *
11712 * parse an XML in-memory block and use the given SAX function block
11713 * to handle the parsing callback. If sax is NULL, fallback to the default
11714 * DOM tree building routines.
11715 *
11716 * User data (void *) is stored within the parser context in the
11717 * context's _private member, so it is available nearly everywhere in libxml
11718 *
11719 * Returns the resulting document tree
11720 */
11721
11722xmlDocPtr
11723xmlSAXParseMemoryWithData(xmlSAXHandlerPtr sax, const char *buffer,
11724 int size, int recovery, void *data) {
11725 xmlDocPtr ret;
11726 xmlParserCtxtPtr ctxt;
11727
11728 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
11729 if (ctxt == NULL) return(NULL);
11730 if (sax != NULL) {
11731 if (ctxt->sax != NULL)
11732 xmlFree(ctxt->sax);
11733 ctxt->sax = sax;
11734 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000011735 xmlDetectSAX2(ctxt);
Daniel Veillard8606bbb2002-11-12 12:36:52 +000011736 if (data!=NULL) {
11737 ctxt->_private=data;
11738 }
11739
Daniel Veillardadba5f12003-04-04 16:09:01 +000011740 ctxt->recovery = recovery;
11741
Daniel Veillard8606bbb2002-11-12 12:36:52 +000011742 xmlParseDocument(ctxt);
11743
11744 if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc;
11745 else {
11746 ret = NULL;
11747 xmlFreeDoc(ctxt->myDoc);
11748 ctxt->myDoc = NULL;
11749 }
11750 if (sax != NULL)
11751 ctxt->sax = NULL;
11752 xmlFreeParserCtxt(ctxt);
11753
11754 return(ret);
11755}
11756
11757/**
Owen Taylor3473f882001-02-23 17:55:21 +000011758 * xmlSAXParseMemory:
11759 * @sax: the SAX handler block
11760 * @buffer: an pointer to a char array
11761 * @size: the size of the array
11762 * @recovery: work in recovery mode, i.e. tries to read not Well Formed
11763 * documents
11764 *
11765 * parse an XML in-memory block and use the given SAX function block
11766 * to handle the parsing callback. If sax is NULL, fallback to the default
11767 * DOM tree building routines.
11768 *
11769 * Returns the resulting document tree
11770 */
11771xmlDocPtr
Daniel Veillard50822cb2001-07-26 20:05:51 +000011772xmlSAXParseMemory(xmlSAXHandlerPtr sax, const char *buffer,
11773 int size, int recovery) {
Daniel Veillard8606bbb2002-11-12 12:36:52 +000011774 return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000011775}
11776
11777/**
11778 * xmlParseMemory:
11779 * @buffer: an pointer to a char array
11780 * @size: the size of the array
11781 *
11782 * parse an XML in-memory block and build a tree.
11783 *
11784 * Returns the resulting document tree
11785 */
11786
Daniel Veillard50822cb2001-07-26 20:05:51 +000011787xmlDocPtr xmlParseMemory(const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000011788 return(xmlSAXParseMemory(NULL, buffer, size, 0));
11789}
11790
11791/**
11792 * xmlRecoverMemory:
11793 * @buffer: an pointer to a char array
11794 * @size: the size of the array
11795 *
11796 * parse an XML in-memory block and build a tree.
11797 * In the case the document is not Well Formed, a tree is built anyway
11798 *
11799 * Returns the resulting document tree
11800 */
11801
Daniel Veillard50822cb2001-07-26 20:05:51 +000011802xmlDocPtr xmlRecoverMemory(const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000011803 return(xmlSAXParseMemory(NULL, buffer, size, 1));
11804}
11805
11806/**
11807 * xmlSAXUserParseMemory:
11808 * @sax: a SAX handler
11809 * @user_data: The user data returned on SAX callbacks
11810 * @buffer: an in-memory XML document input
11811 * @size: the length of the XML document in bytes
11812 *
11813 * A better SAX parsing routine.
11814 * parse an XML in-memory buffer and call the given SAX handler routines.
11815 *
11816 * Returns 0 in case of success or a error number otherwise
11817 */
11818int xmlSAXUserParseMemory(xmlSAXHandlerPtr sax, void *user_data,
Daniel Veillardfd7ddca2001-05-16 10:57:35 +000011819 const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000011820 int ret = 0;
11821 xmlParserCtxtPtr ctxt;
11822 xmlSAXHandlerPtr oldsax = NULL;
11823
Daniel Veillard9e923512002-08-14 08:48:52 +000011824 if (sax == NULL) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +000011825 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
11826 if (ctxt == NULL) return -1;
Daniel Veillard9e923512002-08-14 08:48:52 +000011827 oldsax = ctxt->sax;
11828 ctxt->sax = sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000011829 xmlDetectSAX2(ctxt);
Daniel Veillard30211a02001-04-26 09:33:18 +000011830 if (user_data != NULL)
11831 ctxt->userData = user_data;
Owen Taylor3473f882001-02-23 17:55:21 +000011832
11833 xmlParseDocument(ctxt);
11834
11835 if (ctxt->wellFormed)
11836 ret = 0;
11837 else {
11838 if (ctxt->errNo != 0)
11839 ret = ctxt->errNo;
11840 else
11841 ret = -1;
11842 }
Daniel Veillard9e923512002-08-14 08:48:52 +000011843 ctxt->sax = oldsax;
Owen Taylor3473f882001-02-23 17:55:21 +000011844 xmlFreeParserCtxt(ctxt);
11845
11846 return ret;
11847}
Daniel Veillard81273902003-09-30 00:43:48 +000011848#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011849
11850/**
11851 * xmlCreateDocParserCtxt:
11852 * @cur: a pointer to an array of xmlChar
11853 *
11854 * Creates a parser context for an XML in-memory document.
11855 *
11856 * Returns the new parser context or NULL
11857 */
11858xmlParserCtxtPtr
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011859xmlCreateDocParserCtxt(const xmlChar *cur) {
Owen Taylor3473f882001-02-23 17:55:21 +000011860 int len;
11861
11862 if (cur == NULL)
11863 return(NULL);
11864 len = xmlStrlen(cur);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011865 return(xmlCreateMemoryParserCtxt((const char *)cur, len));
Owen Taylor3473f882001-02-23 17:55:21 +000011866}
11867
Daniel Veillard81273902003-09-30 00:43:48 +000011868#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000011869/**
11870 * xmlSAXParseDoc:
11871 * @sax: the SAX handler block
11872 * @cur: a pointer to an array of xmlChar
11873 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11874 * documents
11875 *
11876 * parse an XML in-memory document and build a tree.
11877 * It use the given SAX function block to handle the parsing callback.
11878 * If sax is NULL, fallback to the default DOM tree building routines.
11879 *
11880 * Returns the resulting document tree
11881 */
11882
11883xmlDocPtr
11884xmlSAXParseDoc(xmlSAXHandlerPtr sax, xmlChar *cur, int recovery) {
11885 xmlDocPtr ret;
11886 xmlParserCtxtPtr ctxt;
11887
11888 if (cur == NULL) return(NULL);
11889
11890
11891 ctxt = xmlCreateDocParserCtxt(cur);
11892 if (ctxt == NULL) return(NULL);
11893 if (sax != NULL) {
11894 ctxt->sax = sax;
11895 ctxt->userData = NULL;
11896 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000011897 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011898
11899 xmlParseDocument(ctxt);
11900 if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc;
11901 else {
11902 ret = NULL;
11903 xmlFreeDoc(ctxt->myDoc);
11904 ctxt->myDoc = NULL;
11905 }
11906 if (sax != NULL)
11907 ctxt->sax = NULL;
11908 xmlFreeParserCtxt(ctxt);
11909
11910 return(ret);
11911}
11912
11913/**
11914 * xmlParseDoc:
11915 * @cur: a pointer to an array of xmlChar
11916 *
11917 * parse an XML in-memory document and build a tree.
11918 *
11919 * Returns the resulting document tree
11920 */
11921
11922xmlDocPtr
11923xmlParseDoc(xmlChar *cur) {
11924 return(xmlSAXParseDoc(NULL, cur, 0));
11925}
Daniel Veillard81273902003-09-30 00:43:48 +000011926#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011927
Daniel Veillard81273902003-09-30 00:43:48 +000011928#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +000011929/************************************************************************
11930 * *
11931 * Specific function to keep track of entities references *
11932 * and used by the XSLT debugger *
11933 * *
11934 ************************************************************************/
11935
11936static xmlEntityReferenceFunc xmlEntityRefFunc = NULL;
11937
11938/**
11939 * xmlAddEntityReference:
11940 * @ent : A valid entity
11941 * @firstNode : A valid first node for children of entity
11942 * @lastNode : A valid last node of children entity
11943 *
11944 * Notify of a reference to an entity of type XML_EXTERNAL_GENERAL_PARSED_ENTITY
11945 */
11946static void
11947xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode,
11948 xmlNodePtr lastNode)
11949{
11950 if (xmlEntityRefFunc != NULL) {
11951 (*xmlEntityRefFunc) (ent, firstNode, lastNode);
11952 }
11953}
11954
11955
11956/**
11957 * xmlSetEntityReferenceFunc:
Daniel Veillard01c13b52002-12-10 15:19:08 +000011958 * @func: A valid function
Daniel Veillard8107a222002-01-13 14:10:10 +000011959 *
11960 * Set the function to call call back when a xml reference has been made
11961 */
11962void
11963xmlSetEntityReferenceFunc(xmlEntityReferenceFunc func)
11964{
11965 xmlEntityRefFunc = func;
11966}
Daniel Veillard81273902003-09-30 00:43:48 +000011967#endif /* LIBXML_LEGACY_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011968
11969/************************************************************************
11970 * *
11971 * Miscellaneous *
11972 * *
11973 ************************************************************************/
11974
11975#ifdef LIBXML_XPATH_ENABLED
11976#include <libxml/xpath.h>
11977#endif
11978
Daniel Veillarddb5850a2002-01-18 11:49:26 +000011979extern void xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...);
Owen Taylor3473f882001-02-23 17:55:21 +000011980static int xmlParserInitialized = 0;
11981
11982/**
11983 * xmlInitParser:
11984 *
11985 * Initialization function for the XML parser.
11986 * This is not reentrant. Call once before processing in case of
11987 * use in multithreaded programs.
11988 */
11989
11990void
11991xmlInitParser(void) {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000011992 if (xmlParserInitialized != 0)
11993 return;
Owen Taylor3473f882001-02-23 17:55:21 +000011994
Daniel Veillarddb5850a2002-01-18 11:49:26 +000011995 if ((xmlGenericError == xmlGenericErrorDefaultFunc) ||
11996 (xmlGenericError == NULL))
11997 initGenericErrorDefaultFunc(NULL);
Daniel Veillard781ac8b2003-05-15 22:11:36 +000011998 xmlInitGlobals();
Daniel Veillardd0463562001-10-13 09:15:48 +000011999 xmlInitThreads();
Daniel Veillard6f350292001-10-14 09:56:15 +000012000 xmlInitMemory();
Owen Taylor3473f882001-02-23 17:55:21 +000012001 xmlInitCharEncodingHandlers();
Owen Taylor3473f882001-02-23 17:55:21 +000012002 xmlDefaultSAXHandlerInit();
12003 xmlRegisterDefaultInputCallbacks();
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000012004#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000012005 xmlRegisterDefaultOutputCallbacks();
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000012006#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000012007#ifdef LIBXML_HTML_ENABLED
12008 htmlInitAutoClose();
12009 htmlDefaultSAXHandlerInit();
12010#endif
12011#ifdef LIBXML_XPATH_ENABLED
12012 xmlXPathInit();
12013#endif
12014 xmlParserInitialized = 1;
12015}
12016
12017/**
12018 * xmlCleanupParser:
12019 *
Daniel Veillardd8cf9062003-11-11 21:12:36 +000012020 * Cleanup function for the XML library. It tries to reclaim all
12021 * parsing related global memory allocated for the library processing.
Owen Taylor3473f882001-02-23 17:55:21 +000012022 * It doesn't deallocate any document related memory. Calling this
Daniel Veillardd8cf9062003-11-11 21:12:36 +000012023 * function should not prevent reusing the library but one should
12024 * call xmlCleanupParser() only when the process has
Daniel Veillard7424eb62003-01-24 14:14:52 +000012025 * finished using the library or XML document built with it.
Owen Taylor3473f882001-02-23 17:55:21 +000012026 */
12027
12028void
12029xmlCleanupParser(void) {
Daniel Veillard7fb801f2003-08-17 21:07:26 +000012030 if (!xmlParserInitialized)
12031 return;
12032
Owen Taylor3473f882001-02-23 17:55:21 +000012033 xmlCleanupCharEncodingHandlers();
Daniel Veillarde2940dd2001-08-22 00:06:49 +000012034#ifdef LIBXML_CATALOG_ENABLED
12035 xmlCatalogCleanup();
12036#endif
Daniel Veillard04054be2003-10-15 10:48:54 +000012037 xmlCleanupInputCallbacks();
12038#ifdef LIBXML_OUTPUT_ENABLED
12039 xmlCleanupOutputCallbacks();
12040#endif
Daniel Veillard781ac8b2003-05-15 22:11:36 +000012041 xmlCleanupGlobals();
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000012042 xmlResetLastError();
Daniel Veillard74c0e592003-11-25 07:01:38 +000012043 xmlCleanupThreads(); /* must be last if called not from the main thread */
William M. Brack72ee48d2003-12-30 08:30:19 +000012044 xmlCleanupMemory();
Daniel Veillardd0463562001-10-13 09:15:48 +000012045 xmlParserInitialized = 0;
Owen Taylor3473f882001-02-23 17:55:21 +000012046}
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012047
12048/************************************************************************
12049 * *
12050 * New set (2.6.0) of simpler and more flexible APIs *
12051 * *
12052 ************************************************************************/
12053
12054/**
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012055 * DICT_FREE:
12056 * @str: a string
12057 *
12058 * Free a string if it is not owned by the "dict" dictionnary in the
12059 * current scope
12060 */
12061#define DICT_FREE(str) \
12062 if ((str) && ((!dict) || \
12063 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
12064 xmlFree((char *)(str));
12065
12066/**
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012067 * xmlCtxtReset:
12068 * @ctxt: an XML parser context
12069 *
12070 * Reset a parser context
12071 */
12072void
12073xmlCtxtReset(xmlParserCtxtPtr ctxt)
12074{
12075 xmlParserInputPtr input;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012076 xmlDictPtr dict = ctxt->dict;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012077
12078 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
12079 xmlFreeInputStream(input);
12080 }
12081 ctxt->inputNr = 0;
12082 ctxt->input = NULL;
12083
12084 ctxt->spaceNr = 0;
12085 ctxt->spaceTab[0] = -1;
12086 ctxt->space = &ctxt->spaceTab[0];
12087
12088
12089 ctxt->nodeNr = 0;
12090 ctxt->node = NULL;
12091
12092 ctxt->nameNr = 0;
12093 ctxt->name = NULL;
12094
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012095 DICT_FREE(ctxt->version);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012096 ctxt->version = NULL;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012097 DICT_FREE(ctxt->encoding);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012098 ctxt->encoding = NULL;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012099 DICT_FREE(ctxt->directory);
12100 ctxt->directory = NULL;
12101 DICT_FREE(ctxt->extSubURI);
12102 ctxt->extSubURI = NULL;
12103 DICT_FREE(ctxt->extSubSystem);
12104 ctxt->extSubSystem = NULL;
12105 if (ctxt->myDoc != NULL)
12106 xmlFreeDoc(ctxt->myDoc);
12107 ctxt->myDoc = NULL;
12108
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012109 ctxt->standalone = -1;
12110 ctxt->hasExternalSubset = 0;
12111 ctxt->hasPErefs = 0;
12112 ctxt->html = 0;
12113 ctxt->external = 0;
12114 ctxt->instate = XML_PARSER_START;
12115 ctxt->token = 0;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012116
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012117 ctxt->wellFormed = 1;
12118 ctxt->nsWellFormed = 1;
Daniel Veillardae289182004-01-21 16:00:43 +000012119 ctxt->disableSAX = 0;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012120 ctxt->valid = 1;
Daniel Veillard766c4f92004-03-26 10:48:29 +000012121#if 0
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012122 ctxt->vctxt.userData = ctxt;
12123 ctxt->vctxt.error = xmlParserValidityError;
12124 ctxt->vctxt.warning = xmlParserValidityWarning;
Daniel Veillard766c4f92004-03-26 10:48:29 +000012125#endif
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012126 ctxt->record_info = 0;
12127 ctxt->nbChars = 0;
12128 ctxt->checkIndex = 0;
12129 ctxt->inSubset = 0;
12130 ctxt->errNo = XML_ERR_OK;
12131 ctxt->depth = 0;
12132 ctxt->charset = XML_CHAR_ENCODING_UTF8;
12133 ctxt->catalogs = NULL;
12134 xmlInitNodeInfoSeq(&ctxt->node_seq);
12135
12136 if (ctxt->attsDefault != NULL) {
12137 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
12138 ctxt->attsDefault = NULL;
12139 }
12140 if (ctxt->attsSpecial != NULL) {
12141 xmlHashFree(ctxt->attsSpecial, NULL);
12142 ctxt->attsSpecial = NULL;
12143 }
12144
Daniel Veillard4432df22003-09-28 18:58:27 +000012145#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012146 if (ctxt->catalogs != NULL)
12147 xmlCatalogFreeLocal(ctxt->catalogs);
Daniel Veillard4432df22003-09-28 18:58:27 +000012148#endif
Daniel Veillardcc199e02003-10-24 21:11:48 +000012149 if (ctxt->lastError.code != XML_ERR_OK)
12150 xmlResetError(&ctxt->lastError);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012151}
12152
12153/**
Daniel Veillard9ba8e382003-10-28 21:31:45 +000012154 * xmlCtxtResetPush:
12155 * @ctxt: an XML parser context
12156 * @chunk: a pointer to an array of chars
12157 * @size: number of chars in the array
12158 * @filename: an optional file name or URI
12159 * @encoding: the document encoding, or NULL
12160 *
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000012161 * Reset a push parser context
12162 *
12163 * Returns 0 in case of success and 1 in case of error
Daniel Veillard9ba8e382003-10-28 21:31:45 +000012164 */
12165int
12166xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk,
12167 int size, const char *filename, const char *encoding)
12168{
12169 xmlParserInputPtr inputStream;
12170 xmlParserInputBufferPtr buf;
12171 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
12172
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000012173 if (ctxt == NULL)
12174 return(1);
12175
Daniel Veillard9ba8e382003-10-28 21:31:45 +000012176 if ((encoding == NULL) && (chunk != NULL) && (size >= 4))
12177 enc = xmlDetectCharEncoding((const xmlChar *) chunk, size);
12178
12179 buf = xmlAllocParserInputBuffer(enc);
12180 if (buf == NULL)
12181 return(1);
12182
12183 if (ctxt == NULL) {
12184 xmlFreeParserInputBuffer(buf);
12185 return(1);
12186 }
12187
12188 xmlCtxtReset(ctxt);
12189
12190 if (ctxt->pushTab == NULL) {
12191 ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 *
12192 sizeof(xmlChar *));
12193 if (ctxt->pushTab == NULL) {
12194 xmlErrMemory(ctxt, NULL);
12195 xmlFreeParserInputBuffer(buf);
12196 return(1);
12197 }
12198 }
12199
12200 if (filename == NULL) {
12201 ctxt->directory = NULL;
12202 } else {
12203 ctxt->directory = xmlParserGetDirectory(filename);
12204 }
12205
12206 inputStream = xmlNewInputStream(ctxt);
12207 if (inputStream == NULL) {
12208 xmlFreeParserInputBuffer(buf);
12209 return(1);
12210 }
12211
12212 if (filename == NULL)
12213 inputStream->filename = NULL;
12214 else
12215 inputStream->filename = (char *)
12216 xmlCanonicPath((const xmlChar *) filename);
12217 inputStream->buf = buf;
12218 inputStream->base = inputStream->buf->buffer->content;
12219 inputStream->cur = inputStream->buf->buffer->content;
12220 inputStream->end =
12221 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
12222
12223 inputPush(ctxt, inputStream);
12224
12225 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
12226 (ctxt->input->buf != NULL)) {
12227 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
12228 int cur = ctxt->input->cur - ctxt->input->base;
12229
12230 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
12231
12232 ctxt->input->base = ctxt->input->buf->buffer->content + base;
12233 ctxt->input->cur = ctxt->input->base + cur;
12234 ctxt->input->end =
12235 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->
12236 use];
12237#ifdef DEBUG_PUSH
12238 xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
12239#endif
12240 }
12241
12242 if (encoding != NULL) {
12243 xmlCharEncodingHandlerPtr hdlr;
12244
12245 hdlr = xmlFindCharEncodingHandler(encoding);
12246 if (hdlr != NULL) {
12247 xmlSwitchToEncoding(ctxt, hdlr);
12248 } else {
12249 xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
12250 "Unsupported encoding %s\n", BAD_CAST encoding);
12251 }
12252 } else if (enc != XML_CHAR_ENCODING_NONE) {
12253 xmlSwitchEncoding(ctxt, enc);
12254 }
12255
12256 return(0);
12257}
12258
12259/**
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012260 * xmlCtxtUseOptions:
12261 * @ctxt: an XML parser context
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012262 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012263 *
12264 * Applies the options to the parser context
12265 *
12266 * Returns 0 in case of success, the set of unknown or unimplemented options
12267 * in case of error.
12268 */
12269int
12270xmlCtxtUseOptions(xmlParserCtxtPtr ctxt, int options)
12271{
12272 if (options & XML_PARSE_RECOVER) {
12273 ctxt->recovery = 1;
12274 options -= XML_PARSE_RECOVER;
12275 } else
12276 ctxt->recovery = 0;
12277 if (options & XML_PARSE_DTDLOAD) {
12278 ctxt->loadsubset = XML_DETECT_IDS;
12279 options -= XML_PARSE_DTDLOAD;
12280 } else
12281 ctxt->loadsubset = 0;
12282 if (options & XML_PARSE_DTDATTR) {
12283 ctxt->loadsubset |= XML_COMPLETE_ATTRS;
12284 options -= XML_PARSE_DTDATTR;
12285 }
12286 if (options & XML_PARSE_NOENT) {
12287 ctxt->replaceEntities = 1;
12288 /* ctxt->loadsubset |= XML_DETECT_IDS; */
12289 options -= XML_PARSE_NOENT;
12290 } else
12291 ctxt->replaceEntities = 0;
12292 if (options & XML_PARSE_NOWARNING) {
12293 ctxt->sax->warning = NULL;
12294 options -= XML_PARSE_NOWARNING;
12295 }
12296 if (options & XML_PARSE_NOERROR) {
12297 ctxt->sax->error = NULL;
12298 ctxt->sax->fatalError = NULL;
12299 options -= XML_PARSE_NOERROR;
12300 }
12301 if (options & XML_PARSE_PEDANTIC) {
12302 ctxt->pedantic = 1;
12303 options -= XML_PARSE_PEDANTIC;
12304 } else
12305 ctxt->pedantic = 0;
12306 if (options & XML_PARSE_NOBLANKS) {
12307 ctxt->keepBlanks = 0;
12308 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
12309 options -= XML_PARSE_NOBLANKS;
12310 } else
12311 ctxt->keepBlanks = 1;
12312 if (options & XML_PARSE_DTDVALID) {
12313 ctxt->validate = 1;
12314 if (options & XML_PARSE_NOWARNING)
12315 ctxt->vctxt.warning = NULL;
12316 if (options & XML_PARSE_NOERROR)
12317 ctxt->vctxt.error = NULL;
12318 options -= XML_PARSE_DTDVALID;
12319 } else
12320 ctxt->validate = 0;
Daniel Veillard81273902003-09-30 00:43:48 +000012321#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012322 if (options & XML_PARSE_SAX1) {
12323 ctxt->sax->startElement = xmlSAX2StartElement;
12324 ctxt->sax->endElement = xmlSAX2EndElement;
12325 ctxt->sax->startElementNs = NULL;
12326 ctxt->sax->endElementNs = NULL;
12327 ctxt->sax->initialized = 1;
12328 options -= XML_PARSE_SAX1;
12329 }
Daniel Veillard81273902003-09-30 00:43:48 +000012330#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012331 if (options & XML_PARSE_NODICT) {
12332 ctxt->dictNames = 0;
12333 options -= XML_PARSE_NODICT;
12334 } else {
12335 ctxt->dictNames = 1;
12336 }
Daniel Veillarddca8cc72003-09-26 13:53:14 +000012337 if (options & XML_PARSE_NOCDATA) {
12338 ctxt->sax->cdataBlock = NULL;
12339 options -= XML_PARSE_NOCDATA;
12340 }
12341 if (options & XML_PARSE_NSCLEAN) {
12342 ctxt->options |= XML_PARSE_NSCLEAN;
12343 options -= XML_PARSE_NSCLEAN;
12344 }
Daniel Veillard61b93382003-11-03 14:28:31 +000012345 if (options & XML_PARSE_NONET) {
12346 ctxt->options |= XML_PARSE_NONET;
12347 options -= XML_PARSE_NONET;
12348 }
Daniel Veillard7ec29972003-10-31 14:36:36 +000012349 ctxt->linenumbers = 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012350 return (options);
12351}
12352
12353/**
12354 * xmlDoRead:
12355 * @ctxt: an XML parser context
Daniel Veillard60942de2003-09-25 21:05:58 +000012356 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012357 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012358 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012359 * @reuse: keep the context for reuse
12360 *
12361 * Common front-end for the xmlRead functions
12362 *
12363 * Returns the resulting document tree or NULL
12364 */
12365static xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012366xmlDoRead(xmlParserCtxtPtr ctxt, const char *URL, const char *encoding,
12367 int options, int reuse)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012368{
12369 xmlDocPtr ret;
12370
12371 xmlCtxtUseOptions(ctxt, options);
12372 if (encoding != NULL) {
12373 xmlCharEncodingHandlerPtr hdlr;
12374
12375 hdlr = xmlFindCharEncodingHandler(encoding);
12376 if (hdlr != NULL)
12377 xmlSwitchToEncoding(ctxt, hdlr);
12378 }
Daniel Veillard60942de2003-09-25 21:05:58 +000012379 if ((URL != NULL) && (ctxt->input != NULL) &&
12380 (ctxt->input->filename == NULL))
12381 ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012382 xmlParseDocument(ctxt);
12383 if ((ctxt->wellFormed) || ctxt->recovery)
12384 ret = ctxt->myDoc;
12385 else {
12386 ret = NULL;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012387 if (ctxt->myDoc != NULL) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012388 xmlFreeDoc(ctxt->myDoc);
12389 }
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012390 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012391 ctxt->myDoc = NULL;
12392 if (!reuse) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012393 xmlFreeParserCtxt(ctxt);
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012394 }
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012395
12396 return (ret);
12397}
12398
12399/**
12400 * xmlReadDoc:
12401 * @cur: a pointer to a zero terminated string
Daniel Veillard60942de2003-09-25 21:05:58 +000012402 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012403 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012404 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012405 *
12406 * parse an XML in-memory document and build a tree.
12407 *
12408 * Returns the resulting document tree
12409 */
12410xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012411xmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012412{
12413 xmlParserCtxtPtr ctxt;
12414
12415 if (cur == NULL)
12416 return (NULL);
12417
12418 ctxt = xmlCreateDocParserCtxt(cur);
12419 if (ctxt == NULL)
12420 return (NULL);
Daniel Veillard60942de2003-09-25 21:05:58 +000012421 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012422}
12423
12424/**
12425 * xmlReadFile:
12426 * @filename: a file or URL
12427 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012428 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012429 *
12430 * parse an XML file from the filesystem or the network.
12431 *
12432 * Returns the resulting document tree
12433 */
12434xmlDocPtr
12435xmlReadFile(const char *filename, const char *encoding, int options)
12436{
12437 xmlParserCtxtPtr ctxt;
12438
Daniel Veillard61b93382003-11-03 14:28:31 +000012439 ctxt = xmlCreateURLParserCtxt(filename, options);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012440 if (ctxt == NULL)
12441 return (NULL);
Daniel Veillard60942de2003-09-25 21:05:58 +000012442 return (xmlDoRead(ctxt, NULL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012443}
12444
12445/**
12446 * xmlReadMemory:
12447 * @buffer: a pointer to a char array
12448 * @size: the size of the array
Daniel Veillard60942de2003-09-25 21:05:58 +000012449 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012450 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012451 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012452 *
12453 * parse an XML in-memory document and build a tree.
12454 *
12455 * Returns the resulting document tree
12456 */
12457xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012458xmlReadMemory(const char *buffer, int size, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012459{
12460 xmlParserCtxtPtr ctxt;
12461
12462 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
12463 if (ctxt == NULL)
12464 return (NULL);
Daniel Veillard60942de2003-09-25 21:05:58 +000012465 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012466}
12467
12468/**
12469 * xmlReadFd:
12470 * @fd: an open file descriptor
Daniel Veillard60942de2003-09-25 21:05:58 +000012471 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012472 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012473 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012474 *
12475 * parse an XML from a file descriptor and build a tree.
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012476 * NOTE that the file descriptor will not be closed when the
12477 * reader is closed or reset.
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012478 *
12479 * Returns the resulting document tree
12480 */
12481xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012482xmlReadFd(int fd, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012483{
12484 xmlParserCtxtPtr ctxt;
12485 xmlParserInputBufferPtr input;
12486 xmlParserInputPtr stream;
12487
12488 if (fd < 0)
12489 return (NULL);
12490
12491 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
12492 if (input == NULL)
12493 return (NULL);
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012494 input->closecallback = NULL;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012495 ctxt = xmlNewParserCtxt();
12496 if (ctxt == NULL) {
12497 xmlFreeParserInputBuffer(input);
12498 return (NULL);
12499 }
12500 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12501 if (stream == NULL) {
12502 xmlFreeParserInputBuffer(input);
12503 xmlFreeParserCtxt(ctxt);
12504 return (NULL);
12505 }
12506 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012507 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012508}
12509
12510/**
12511 * xmlReadIO:
12512 * @ioread: an I/O read function
12513 * @ioclose: an I/O close function
12514 * @ioctx: an I/O handler
Daniel Veillard60942de2003-09-25 21:05:58 +000012515 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012516 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012517 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012518 *
12519 * parse an XML document from I/O functions and source and build a tree.
12520 *
12521 * Returns the resulting document tree
12522 */
12523xmlDocPtr
12524xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
Daniel Veillard60942de2003-09-25 21:05:58 +000012525 void *ioctx, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012526{
12527 xmlParserCtxtPtr ctxt;
12528 xmlParserInputBufferPtr input;
12529 xmlParserInputPtr stream;
12530
12531 if (ioread == NULL)
12532 return (NULL);
12533
12534 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
12535 XML_CHAR_ENCODING_NONE);
12536 if (input == NULL)
12537 return (NULL);
12538 ctxt = xmlNewParserCtxt();
12539 if (ctxt == NULL) {
12540 xmlFreeParserInputBuffer(input);
12541 return (NULL);
12542 }
12543 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12544 if (stream == NULL) {
12545 xmlFreeParserInputBuffer(input);
12546 xmlFreeParserCtxt(ctxt);
12547 return (NULL);
12548 }
12549 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012550 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012551}
12552
12553/**
12554 * xmlCtxtReadDoc:
12555 * @ctxt: an XML parser context
12556 * @cur: a pointer to a zero terminated string
Daniel Veillard60942de2003-09-25 21:05:58 +000012557 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012558 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012559 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012560 *
12561 * parse an XML in-memory document and build a tree.
12562 * This reuses the existing @ctxt parser context
12563 *
12564 * Returns the resulting document tree
12565 */
12566xmlDocPtr
12567xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur,
Daniel Veillard60942de2003-09-25 21:05:58 +000012568 const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012569{
12570 xmlParserInputPtr stream;
12571
12572 if (cur == NULL)
12573 return (NULL);
12574 if (ctxt == NULL)
12575 return (NULL);
12576
12577 xmlCtxtReset(ctxt);
12578
12579 stream = xmlNewStringInputStream(ctxt, cur);
12580 if (stream == NULL) {
12581 return (NULL);
12582 }
12583 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012584 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012585}
12586
12587/**
12588 * xmlCtxtReadFile:
12589 * @ctxt: an XML parser context
12590 * @filename: a file or URL
12591 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012592 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012593 *
12594 * parse an XML file from the filesystem or the network.
12595 * This reuses the existing @ctxt parser context
12596 *
12597 * Returns the resulting document tree
12598 */
12599xmlDocPtr
12600xmlCtxtReadFile(xmlParserCtxtPtr ctxt, const char *filename,
12601 const char *encoding, int options)
12602{
12603 xmlParserInputPtr stream;
12604
12605 if (filename == NULL)
12606 return (NULL);
12607 if (ctxt == NULL)
12608 return (NULL);
12609
12610 xmlCtxtReset(ctxt);
12611
12612 stream = xmlNewInputFromFile(ctxt, filename);
12613 if (stream == NULL) {
12614 return (NULL);
12615 }
12616 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012617 return (xmlDoRead(ctxt, NULL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012618}
12619
12620/**
12621 * xmlCtxtReadMemory:
12622 * @ctxt: an XML parser context
12623 * @buffer: a pointer to a char array
12624 * @size: the size of the array
Daniel Veillard60942de2003-09-25 21:05:58 +000012625 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012626 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012627 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012628 *
12629 * parse an XML in-memory document and build a tree.
12630 * This reuses the existing @ctxt parser context
12631 *
12632 * Returns the resulting document tree
12633 */
12634xmlDocPtr
12635xmlCtxtReadMemory(xmlParserCtxtPtr ctxt, const char *buffer, int size,
Daniel Veillard60942de2003-09-25 21:05:58 +000012636 const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012637{
12638 xmlParserInputBufferPtr input;
12639 xmlParserInputPtr stream;
12640
12641 if (ctxt == NULL)
12642 return (NULL);
12643 if (buffer == NULL)
12644 return (NULL);
12645
12646 xmlCtxtReset(ctxt);
12647
12648 input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
12649 if (input == NULL) {
12650 return(NULL);
12651 }
12652
12653 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12654 if (stream == NULL) {
12655 xmlFreeParserInputBuffer(input);
12656 return(NULL);
12657 }
12658
12659 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012660 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012661}
12662
12663/**
12664 * xmlCtxtReadFd:
12665 * @ctxt: an XML parser context
12666 * @fd: an open file descriptor
Daniel Veillard60942de2003-09-25 21:05:58 +000012667 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012668 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012669 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012670 *
12671 * parse an XML from a file descriptor and build a tree.
12672 * This reuses the existing @ctxt parser context
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012673 * NOTE that the file descriptor will not be closed when the
12674 * reader is closed or reset.
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012675 *
12676 * Returns the resulting document tree
12677 */
12678xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012679xmlCtxtReadFd(xmlParserCtxtPtr ctxt, int fd,
12680 const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012681{
12682 xmlParserInputBufferPtr input;
12683 xmlParserInputPtr stream;
12684
12685 if (fd < 0)
12686 return (NULL);
12687 if (ctxt == NULL)
12688 return (NULL);
12689
12690 xmlCtxtReset(ctxt);
12691
12692
12693 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
12694 if (input == NULL)
12695 return (NULL);
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012696 input->closecallback = NULL;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012697 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12698 if (stream == NULL) {
12699 xmlFreeParserInputBuffer(input);
12700 return (NULL);
12701 }
12702 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012703 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012704}
12705
12706/**
12707 * xmlCtxtReadIO:
12708 * @ctxt: an XML parser context
12709 * @ioread: an I/O read function
12710 * @ioclose: an I/O close function
12711 * @ioctx: an I/O handler
Daniel Veillard60942de2003-09-25 21:05:58 +000012712 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012713 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012714 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012715 *
12716 * parse an XML document from I/O functions and source and build a tree.
12717 * This reuses the existing @ctxt parser context
12718 *
12719 * Returns the resulting document tree
12720 */
12721xmlDocPtr
12722xmlCtxtReadIO(xmlParserCtxtPtr ctxt, xmlInputReadCallback ioread,
12723 xmlInputCloseCallback ioclose, void *ioctx,
Daniel Veillard60942de2003-09-25 21:05:58 +000012724 const char *URL,
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012725 const char *encoding, int options)
12726{
12727 xmlParserInputBufferPtr input;
12728 xmlParserInputPtr stream;
12729
12730 if (ioread == NULL)
12731 return (NULL);
12732 if (ctxt == NULL)
12733 return (NULL);
12734
12735 xmlCtxtReset(ctxt);
12736
12737 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
12738 XML_CHAR_ENCODING_NONE);
12739 if (input == NULL)
12740 return (NULL);
12741 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12742 if (stream == NULL) {
12743 xmlFreeParserInputBuffer(input);
12744 return (NULL);
12745 }
12746 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012747 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012748}