blob: b0d9f6882dbdf8018786dddb3a17a20b6e66396c [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
William M. Brack1d8c9b22004-12-25 10:14:57 +000060#ifdef LIBXML_SCHEMAS_ENABLED
61#include <libxml/xmlschemastypes.h>
62#include <libxml/relaxng.h>
63#endif
Owen Taylor3473f882001-02-23 17:55:21 +000064#ifdef HAVE_CTYPE_H
65#include <ctype.h>
66#endif
67#ifdef HAVE_STDLIB_H
68#include <stdlib.h>
69#endif
70#ifdef HAVE_SYS_STAT_H
71#include <sys/stat.h>
72#endif
73#ifdef HAVE_FCNTL_H
74#include <fcntl.h>
75#endif
76#ifdef HAVE_UNISTD_H
77#include <unistd.h>
78#endif
79#ifdef HAVE_ZLIB_H
80#include <zlib.h>
81#endif
82
Daniel Veillard3b2e4e12003-02-03 08:52:58 +000083/**
Daniel Veillard4aede2e2003-10-17 12:43:59 +000084 * xmlParserMaxDepth:
Daniel Veillard3b2e4e12003-02-03 08:52:58 +000085 *
86 * arbitrary depth limit for the XML documents that we allow to
87 * process. This is not a limitation of the parser but a safety
88 * boundary feature.
89 */
Daniel Veillard4aede2e2003-10-17 12:43:59 +000090unsigned int xmlParserMaxDepth = 1024;
Owen Taylor3473f882001-02-23 17:55:21 +000091
Daniel Veillard0fb18932003-09-07 09:14:37 +000092#define SAX2 1
93
Daniel Veillard21a0f912001-02-25 19:54:14 +000094#define XML_PARSER_BIG_BUFFER_SIZE 300
Owen Taylor3473f882001-02-23 17:55:21 +000095#define XML_PARSER_BUFFER_SIZE 100
96
Daniel Veillard5997aca2002-03-18 18:36:20 +000097#define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document"
98
Owen Taylor3473f882001-02-23 17:55:21 +000099/*
Owen Taylor3473f882001-02-23 17:55:21 +0000100 * List of XML prefixed PI allowed by W3C specs
101 */
102
Daniel Veillardb44025c2001-10-11 22:55:55 +0000103static const char *xmlW3CPIs[] = {
Owen Taylor3473f882001-02-23 17:55:21 +0000104 "xml-stylesheet",
105 NULL
106};
107
Daniel Veillarda07050d2003-10-19 14:46:32 +0000108
Owen Taylor3473f882001-02-23 17:55:21 +0000109/* DEPR void xmlParserHandleReference(xmlParserCtxtPtr ctxt); */
Owen Taylor3473f882001-02-23 17:55:21 +0000110xmlEntityPtr xmlParseStringPEReference(xmlParserCtxtPtr ctxt,
111 const xmlChar **str);
112
Daniel Veillard7d515752003-09-26 19:12:37 +0000113static xmlParserErrors
Daniel Veillarda97a19b2001-05-20 13:19:52 +0000114xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt,
115 xmlSAXHandlerPtr sax,
Daniel Veillard257d9102001-05-08 10:41:44 +0000116 void *user_data, int depth, const xmlChar *URL,
Daniel Veillarda97a19b2001-05-20 13:19:52 +0000117 const xmlChar *ID, xmlNodePtr *list);
Owen Taylor3473f882001-02-23 17:55:21 +0000118
Daniel Veillard81273902003-09-30 00:43:48 +0000119#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +0000120static void
121xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode,
122 xmlNodePtr lastNode);
Daniel Veillard81273902003-09-30 00:43:48 +0000123#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillard8107a222002-01-13 14:10:10 +0000124
Daniel Veillard7d515752003-09-26 19:12:37 +0000125static xmlParserErrors
Daniel Veillard328f48c2002-11-15 15:24:34 +0000126xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt,
127 const xmlChar *string, void *user_data, xmlNodePtr *lst);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000128
129/************************************************************************
130 * *
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000131 * Some factorized error routines *
132 * *
133 ************************************************************************/
134
135/**
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000136 * xmlErrAttributeDup:
137 * @ctxt: an XML parser context
138 * @prefix: the attribute prefix
139 * @localname: the attribute localname
140 *
141 * Handle a redefinition of attribute error
142 */
143static void
144xmlErrAttributeDup(xmlParserCtxtPtr ctxt, const xmlChar * prefix,
145 const xmlChar * localname)
146{
Daniel Veillard157fee02003-10-31 10:36:03 +0000147 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
148 (ctxt->instate == XML_PARSER_EOF))
149 return;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000150 ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000151 if (prefix == NULL)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000152 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000153 ctxt->errNo, XML_ERR_FATAL, NULL, 0,
154 (const char *) localname, NULL, NULL, 0, 0,
155 "Attribute %s redefined\n", localname);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000156 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000157 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000158 ctxt->errNo, XML_ERR_FATAL, NULL, 0,
159 (const char *) prefix, (const char *) localname,
160 NULL, 0, 0, "Attribute %s:%s redefined\n", prefix,
161 localname);
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000162 ctxt->wellFormed = 0;
163 if (ctxt->recovery == 0)
164 ctxt->disableSAX = 1;
165}
166
167/**
168 * xmlFatalErr:
169 * @ctxt: an XML parser context
170 * @error: the error number
171 * @extra: extra information string
172 *
173 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
174 */
175static void
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000176xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info)
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000177{
178 const char *errmsg;
179
Daniel Veillard157fee02003-10-31 10:36:03 +0000180 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
181 (ctxt->instate == XML_PARSER_EOF))
182 return;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000183 switch (error) {
184 case XML_ERR_INVALID_HEX_CHARREF:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000185 errmsg = "CharRef: invalid hexadecimal value\n";
186 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000187 case XML_ERR_INVALID_DEC_CHARREF:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000188 errmsg = "CharRef: invalid decimal value\n";
189 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000190 case XML_ERR_INVALID_CHARREF:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000191 errmsg = "CharRef: invalid value\n";
192 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000193 case XML_ERR_INTERNAL_ERROR:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000194 errmsg = "internal error";
195 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000196 case XML_ERR_PEREF_AT_EOF:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000197 errmsg = "PEReference at end of document\n";
198 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000199 case XML_ERR_PEREF_IN_PROLOG:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000200 errmsg = "PEReference in prolog\n";
201 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000202 case XML_ERR_PEREF_IN_EPILOG:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000203 errmsg = "PEReference in epilog\n";
204 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000205 case XML_ERR_PEREF_NO_NAME:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000206 errmsg = "PEReference: no name\n";
207 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000208 case XML_ERR_PEREF_SEMICOL_MISSING:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000209 errmsg = "PEReference: expecting ';'\n";
210 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000211 case XML_ERR_ENTITY_LOOP:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000212 errmsg = "Detected an entity reference loop\n";
213 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000214 case XML_ERR_ENTITY_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000215 errmsg = "EntityValue: \" or ' expected\n";
216 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000217 case XML_ERR_ENTITY_PE_INTERNAL:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000218 errmsg = "PEReferences forbidden in internal subset\n";
219 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000220 case XML_ERR_ENTITY_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000221 errmsg = "EntityValue: \" or ' expected\n";
222 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000223 case XML_ERR_ATTRIBUTE_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000224 errmsg = "AttValue: \" or ' expected\n";
225 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000226 case XML_ERR_LT_IN_ATTRIBUTE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000227 errmsg = "Unescaped '<' not allowed in attributes values\n";
228 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000229 case XML_ERR_LITERAL_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000230 errmsg = "SystemLiteral \" or ' expected\n";
231 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000232 case XML_ERR_LITERAL_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000233 errmsg = "Unfinished System or Public ID \" or ' expected\n";
234 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000235 case XML_ERR_MISPLACED_CDATA_END:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000236 errmsg = "Sequence ']]>' not allowed in content\n";
237 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000238 case XML_ERR_URI_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000239 errmsg = "SYSTEM or PUBLIC, the URI is missing\n";
240 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000241 case XML_ERR_PUBID_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000242 errmsg = "PUBLIC, the Public Identifier is missing\n";
243 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000244 case XML_ERR_HYPHEN_IN_COMMENT:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000245 errmsg = "Comment must not contain '--' (double-hyphen)\n";
246 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000247 case XML_ERR_PI_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000248 errmsg = "xmlParsePI : no target name\n";
249 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000250 case XML_ERR_RESERVED_XML_NAME:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000251 errmsg = "Invalid PI name\n";
252 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000253 case XML_ERR_NOTATION_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000254 errmsg = "NOTATION: Name expected here\n";
255 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000256 case XML_ERR_NOTATION_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000257 errmsg = "'>' required to close NOTATION declaration\n";
258 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000259 case XML_ERR_VALUE_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000260 errmsg = "Entity value required\n";
261 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000262 case XML_ERR_URI_FRAGMENT:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000263 errmsg = "Fragment not allowed";
264 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000265 case XML_ERR_ATTLIST_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000266 errmsg = "'(' required to start ATTLIST enumeration\n";
267 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000268 case XML_ERR_NMTOKEN_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000269 errmsg = "NmToken expected in ATTLIST enumeration\n";
270 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000271 case XML_ERR_ATTLIST_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000272 errmsg = "')' required to finish ATTLIST enumeration\n";
273 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000274 case XML_ERR_MIXED_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000275 errmsg = "MixedContentDecl : '|' or ')*' expected\n";
276 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000277 case XML_ERR_PCDATA_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000278 errmsg = "MixedContentDecl : '#PCDATA' expected\n";
279 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000280 case XML_ERR_ELEMCONTENT_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000281 errmsg = "ContentDecl : Name or '(' expected\n";
282 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000283 case XML_ERR_ELEMCONTENT_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000284 errmsg = "ContentDecl : ',' '|' or ')' expected\n";
285 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000286 case XML_ERR_PEREF_IN_INT_SUBSET:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000287 errmsg =
288 "PEReference: forbidden within markup decl in internal subset\n";
289 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000290 case XML_ERR_GT_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000291 errmsg = "expected '>'\n";
292 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000293 case XML_ERR_CONDSEC_INVALID:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000294 errmsg = "XML conditional section '[' expected\n";
295 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000296 case XML_ERR_EXT_SUBSET_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000297 errmsg = "Content error in the external subset\n";
298 break;
299 case XML_ERR_CONDSEC_INVALID_KEYWORD:
300 errmsg =
301 "conditional section INCLUDE or IGNORE keyword expected\n";
302 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000303 case XML_ERR_CONDSEC_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000304 errmsg = "XML conditional section not closed\n";
305 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000306 case XML_ERR_XMLDECL_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000307 errmsg = "Text declaration '<?xml' required\n";
308 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000309 case XML_ERR_XMLDECL_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000310 errmsg = "parsing XML declaration: '?>' expected\n";
311 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000312 case XML_ERR_EXT_ENTITY_STANDALONE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000313 errmsg = "external parsed entities cannot be standalone\n";
314 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000315 case XML_ERR_ENTITYREF_SEMICOL_MISSING:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000316 errmsg = "EntityRef: expecting ';'\n";
317 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000318 case XML_ERR_DOCTYPE_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000319 errmsg = "DOCTYPE improperly terminated\n";
320 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000321 case XML_ERR_LTSLASH_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000322 errmsg = "EndTag: '</' not found\n";
323 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000324 case XML_ERR_EQUAL_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000325 errmsg = "expected '='\n";
326 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000327 case XML_ERR_STRING_NOT_CLOSED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000328 errmsg = "String not closed expecting \" or '\n";
329 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000330 case XML_ERR_STRING_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000331 errmsg = "String not started expecting ' or \"\n";
332 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000333 case XML_ERR_ENCODING_NAME:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000334 errmsg = "Invalid XML encoding name\n";
335 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000336 case XML_ERR_STANDALONE_VALUE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000337 errmsg = "standalone accepts only 'yes' or 'no'\n";
338 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000339 case XML_ERR_DOCUMENT_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000340 errmsg = "Document is empty\n";
341 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000342 case XML_ERR_DOCUMENT_END:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000343 errmsg = "Extra content at the end of the document\n";
344 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000345 case XML_ERR_NOT_WELL_BALANCED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000346 errmsg = "chunk is not well balanced\n";
347 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000348 case XML_ERR_EXTRA_CONTENT:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000349 errmsg = "extra content at the end of well balanced chunk\n";
350 break;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000351 case XML_ERR_VERSION_MISSING:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000352 errmsg = "Malformed declaration expecting version\n";
353 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000354#if 0
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000355 case:
356 errmsg = "\n";
357 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000358#endif
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000359 default:
360 errmsg = "Unregistered error message\n";
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000361 }
362 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000363 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000364 XML_ERR_FATAL, NULL, 0, info, NULL, NULL, 0, 0, errmsg,
365 info);
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000366 ctxt->wellFormed = 0;
367 if (ctxt->recovery == 0)
368 ctxt->disableSAX = 1;
369}
370
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000371/**
372 * xmlFatalErrMsg:
373 * @ctxt: an XML parser context
374 * @error: the error number
375 * @msg: the error message
376 *
377 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
378 */
379static void
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000380xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
381 const char *msg)
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000382{
Daniel Veillard157fee02003-10-31 10:36:03 +0000383 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
384 (ctxt->instate == XML_PARSER_EOF))
385 return;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000386 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000387 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000388 XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, msg);
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000389 ctxt->wellFormed = 0;
390 if (ctxt->recovery == 0)
391 ctxt->disableSAX = 1;
392}
393
394/**
Daniel Veillard24eb9782003-10-04 21:08:09 +0000395 * xmlWarningMsg:
396 * @ctxt: an XML parser context
397 * @error: the error number
398 * @msg: the error message
399 * @str1: extra data
400 * @str2: extra data
401 *
402 * Handle a warning.
403 */
404static void
405xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
406 const char *msg, const xmlChar *str1, const xmlChar *str2)
407{
Daniel Veillard9bcc7c52003-10-11 10:57:05 +0000408 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardc790bf42003-10-11 10:50:10 +0000409
Daniel Veillard157fee02003-10-31 10:36:03 +0000410 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
411 (ctxt->instate == XML_PARSER_EOF))
412 return;
Daniel Veillardc790bf42003-10-11 10:50:10 +0000413 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
Daniel Veillard9bcc7c52003-10-11 10:57:05 +0000414 schannel = ctxt->sax->serror;
415 __xmlRaiseError(schannel,
Daniel Veillard659e71e2003-10-10 14:10:40 +0000416 (ctxt->sax) ? ctxt->sax->warning : NULL,
417 ctxt->userData,
Daniel Veillard24eb9782003-10-04 21:08:09 +0000418 ctxt, NULL, XML_FROM_PARSER, error,
419 XML_ERR_WARNING, NULL, 0,
420 (const char *) str1, (const char *) str2, NULL, 0, 0,
421 msg, (const char *) str1, (const char *) str2);
422}
423
424/**
425 * xmlValidityError:
426 * @ctxt: an XML parser context
427 * @error: the error number
428 * @msg: the error message
429 * @str1: extra data
430 *
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000431 * Handle a validity error.
Daniel Veillard24eb9782003-10-04 21:08:09 +0000432 */
433static void
434xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error,
435 const char *msg, const xmlChar *str1)
436{
Daniel Veillard9bcc7c52003-10-11 10:57:05 +0000437 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard157fee02003-10-31 10:36:03 +0000438
439 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
440 (ctxt->instate == XML_PARSER_EOF))
441 return;
Daniel Veillard24eb9782003-10-04 21:08:09 +0000442 ctxt->errNo = error;
Daniel Veillardc790bf42003-10-11 10:50:10 +0000443 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
Daniel Veillard9bcc7c52003-10-11 10:57:05 +0000444 schannel = ctxt->sax->serror;
Daniel Veillardc790bf42003-10-11 10:50:10 +0000445 __xmlRaiseError(schannel,
Daniel Veillard659e71e2003-10-10 14:10:40 +0000446 ctxt->vctxt.error, ctxt->vctxt.userData,
Daniel Veillard24eb9782003-10-04 21:08:09 +0000447 ctxt, NULL, XML_FROM_DTD, error,
448 XML_ERR_ERROR, NULL, 0, (const char *) str1,
449 NULL, NULL, 0, 0,
450 msg, (const char *) str1);
451 ctxt->valid = 0;
452}
453
454/**
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000455 * xmlFatalErrMsgInt:
456 * @ctxt: an XML parser context
457 * @error: the error number
458 * @msg: the error message
459 * @val: an integer value
460 *
461 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
462 */
463static void
464xmlFatalErrMsgInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000465 const char *msg, int val)
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000466{
Daniel Veillard157fee02003-10-31 10:36:03 +0000467 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
468 (ctxt->instate == XML_PARSER_EOF))
469 return;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000470 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000471 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000472 ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
473 NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000474 ctxt->wellFormed = 0;
475 if (ctxt->recovery == 0)
476 ctxt->disableSAX = 1;
477}
478
479/**
Daniel Veillardf403d292003-10-05 13:51:35 +0000480 * xmlFatalErrMsgStrIntStr:
481 * @ctxt: an XML parser context
482 * @error: the error number
483 * @msg: the error message
484 * @str1: an string info
485 * @val: an integer value
486 * @str2: an string info
487 *
488 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
489 */
490static void
491xmlFatalErrMsgStrIntStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
492 const char *msg, const xmlChar *str1, int val,
493 const xmlChar *str2)
494{
Daniel Veillard157fee02003-10-31 10:36:03 +0000495 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
496 (ctxt->instate == XML_PARSER_EOF))
497 return;
Daniel Veillardf403d292003-10-05 13:51:35 +0000498 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000499 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardf403d292003-10-05 13:51:35 +0000500 ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
501 NULL, 0, (const char *) str1, (const char *) str2,
502 NULL, val, 0, msg, str1, val, str2);
503 ctxt->wellFormed = 0;
504 if (ctxt->recovery == 0)
505 ctxt->disableSAX = 1;
506}
507
508/**
Daniel Veillardbc92eca2003-09-15 09:48:06 +0000509 * xmlFatalErrMsgStr:
510 * @ctxt: an XML parser context
511 * @error: the error number
512 * @msg: the error message
513 * @val: a string value
514 *
515 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
516 */
517static void
518xmlFatalErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000519 const char *msg, const xmlChar * val)
Daniel Veillardbc92eca2003-09-15 09:48:06 +0000520{
Daniel Veillard157fee02003-10-31 10:36:03 +0000521 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
522 (ctxt->instate == XML_PARSER_EOF))
523 return;
Daniel Veillardbc92eca2003-09-15 09:48:06 +0000524 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000525 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000526 XML_FROM_PARSER, error, XML_ERR_FATAL,
527 NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg,
528 val);
Daniel Veillardbc92eca2003-09-15 09:48:06 +0000529 ctxt->wellFormed = 0;
530 if (ctxt->recovery == 0)
531 ctxt->disableSAX = 1;
532}
533
534/**
Daniel Veillardf403d292003-10-05 13:51:35 +0000535 * xmlErrMsgStr:
536 * @ctxt: an XML parser context
537 * @error: the error number
538 * @msg: the error message
539 * @val: a string value
540 *
541 * Handle a non fatal parser error
542 */
543static void
544xmlErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
545 const char *msg, const xmlChar * val)
546{
Daniel Veillard157fee02003-10-31 10:36:03 +0000547 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
548 (ctxt->instate == XML_PARSER_EOF))
549 return;
Daniel Veillardf403d292003-10-05 13:51:35 +0000550 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000551 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL,
Daniel Veillardf403d292003-10-05 13:51:35 +0000552 XML_FROM_PARSER, error, XML_ERR_ERROR,
553 NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg,
554 val);
555}
556
557/**
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000558 * xmlNsErr:
559 * @ctxt: an XML parser context
560 * @error: the error number
561 * @msg: the message
562 * @info1: extra information string
563 * @info2: extra information string
564 *
565 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
566 */
567static void
568xmlNsErr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
569 const char *msg,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000570 const xmlChar * info1, const xmlChar * info2,
571 const xmlChar * info3)
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000572{
Daniel Veillard157fee02003-10-31 10:36:03 +0000573 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
574 (ctxt->instate == XML_PARSER_EOF))
575 return;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000576 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000577 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000578 XML_ERR_ERROR, NULL, 0, (const char *) info1,
579 (const char *) info2, (const char *) info3, 0, 0, msg,
580 info1, info2, info3);
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000581 ctxt->nsWellFormed = 0;
582}
583
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000584/************************************************************************
585 * *
Daniel Veillarde57ec792003-09-10 10:50:59 +0000586 * SAX2 defaulted attributes handling *
587 * *
588 ************************************************************************/
589
590/**
591 * xmlDetectSAX2:
592 * @ctxt: an XML parser context
593 *
594 * Do the SAX2 detection and specific intialization
595 */
596static void
597xmlDetectSAX2(xmlParserCtxtPtr ctxt) {
598 if (ctxt == NULL) return;
Daniel Veillard81273902003-09-30 00:43:48 +0000599#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +0000600 if ((ctxt->sax) && (ctxt->sax->initialized == XML_SAX2_MAGIC) &&
601 ((ctxt->sax->startElementNs != NULL) ||
602 (ctxt->sax->endElementNs != NULL))) ctxt->sax2 = 1;
Daniel Veillard81273902003-09-30 00:43:48 +0000603#else
604 ctxt->sax2 = 1;
605#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +0000606
607 ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
608 ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
609 ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
William M. Brack9f797ab2004-07-28 07:40:12 +0000610 if ((ctxt->str_xml==NULL) || (ctxt->str_xmlns==NULL) ||
611 (ctxt->str_xml_ns == NULL)) {
612 xmlErrMemory(ctxt, NULL);
613 }
Daniel Veillarde57ec792003-09-10 10:50:59 +0000614}
615
Daniel Veillarde57ec792003-09-10 10:50:59 +0000616typedef struct _xmlDefAttrs xmlDefAttrs;
617typedef xmlDefAttrs *xmlDefAttrsPtr;
618struct _xmlDefAttrs {
619 int nbAttrs; /* number of defaulted attributes on that element */
620 int maxAttrs; /* the size of the array */
621 const xmlChar *values[4]; /* array of localname/prefix/values */
622};
Daniel Veillarde57ec792003-09-10 10:50:59 +0000623
624/**
625 * xmlAddDefAttrs:
626 * @ctxt: an XML parser context
627 * @fullname: the element fullname
628 * @fullattr: the attribute fullname
629 * @value: the attribute value
630 *
631 * Add a defaulted attribute for an element
632 */
633static void
634xmlAddDefAttrs(xmlParserCtxtPtr ctxt,
635 const xmlChar *fullname,
636 const xmlChar *fullattr,
637 const xmlChar *value) {
638 xmlDefAttrsPtr defaults;
639 int len;
640 const xmlChar *name;
641 const xmlChar *prefix;
642
643 if (ctxt->attsDefault == NULL) {
Daniel Veillard316a5c32005-01-23 22:56:39 +0000644 ctxt->attsDefault = xmlHashCreateDict(10, ctxt->dict);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000645 if (ctxt->attsDefault == NULL)
646 goto mem_error;
647 }
648
649 /*
Daniel Veillard079f6a72004-09-23 13:15:03 +0000650 * split the element name into prefix:localname , the string found
651 * are within the DTD and then not associated to namespace names.
Daniel Veillarde57ec792003-09-10 10:50:59 +0000652 */
653 name = xmlSplitQName3(fullname, &len);
654 if (name == NULL) {
655 name = xmlDictLookup(ctxt->dict, fullname, -1);
656 prefix = NULL;
657 } else {
658 name = xmlDictLookup(ctxt->dict, name, -1);
659 prefix = xmlDictLookup(ctxt->dict, fullname, len);
660 }
661
662 /*
663 * make sure there is some storage
664 */
665 defaults = xmlHashLookup2(ctxt->attsDefault, name, prefix);
666 if (defaults == NULL) {
667 defaults = (xmlDefAttrsPtr) xmlMalloc(sizeof(xmlDefAttrs) +
Daniel Veillard079f6a72004-09-23 13:15:03 +0000668 (4 * 4) * sizeof(const xmlChar *));
Daniel Veillarde57ec792003-09-10 10:50:59 +0000669 if (defaults == NULL)
670 goto mem_error;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000671 defaults->nbAttrs = 0;
Daniel Veillard079f6a72004-09-23 13:15:03 +0000672 defaults->maxAttrs = 4;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000673 xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, defaults, NULL);
674 } else if (defaults->nbAttrs >= defaults->maxAttrs) {
Daniel Veillard079f6a72004-09-23 13:15:03 +0000675 xmlDefAttrsPtr temp;
676
677 temp = (xmlDefAttrsPtr) xmlRealloc(defaults, sizeof(xmlDefAttrs) +
Daniel Veillarde57ec792003-09-10 10:50:59 +0000678 (2 * defaults->maxAttrs * 4) * sizeof(const xmlChar *));
Daniel Veillard079f6a72004-09-23 13:15:03 +0000679 if (temp == NULL)
Daniel Veillarde57ec792003-09-10 10:50:59 +0000680 goto mem_error;
Daniel Veillard079f6a72004-09-23 13:15:03 +0000681 defaults = temp;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000682 defaults->maxAttrs *= 2;
683 xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, defaults, NULL);
684 }
685
686 /*
687 * plit the element name into prefix:localname , the string found
688 * are within the DTD and hen not associated to namespace names.
689 */
690 name = xmlSplitQName3(fullattr, &len);
691 if (name == NULL) {
692 name = xmlDictLookup(ctxt->dict, fullattr, -1);
693 prefix = NULL;
694 } else {
695 name = xmlDictLookup(ctxt->dict, name, -1);
696 prefix = xmlDictLookup(ctxt->dict, fullattr, len);
697 }
698
699 defaults->values[4 * defaults->nbAttrs] = name;
700 defaults->values[4 * defaults->nbAttrs + 1] = prefix;
701 /* intern the string and precompute the end */
702 len = xmlStrlen(value);
703 value = xmlDictLookup(ctxt->dict, value, len);
704 defaults->values[4 * defaults->nbAttrs + 2] = value;
705 defaults->values[4 * defaults->nbAttrs + 3] = value + len;
706 defaults->nbAttrs++;
707
708 return;
709
710mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000711 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000712 return;
713}
714
Daniel Veillard8e36e6a2003-09-10 10:50:59 +0000715/**
716 * xmlAddSpecialAttr:
717 * @ctxt: an XML parser context
718 * @fullname: the element fullname
719 * @fullattr: the attribute fullname
720 * @type: the attribute type
721 *
722 * Register that this attribute is not CDATA
723 */
724static void
725xmlAddSpecialAttr(xmlParserCtxtPtr ctxt,
726 const xmlChar *fullname,
727 const xmlChar *fullattr,
728 int type)
729{
730 if (ctxt->attsSpecial == NULL) {
Daniel Veillard316a5c32005-01-23 22:56:39 +0000731 ctxt->attsSpecial = xmlHashCreateDict(10, ctxt->dict);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +0000732 if (ctxt->attsSpecial == NULL)
733 goto mem_error;
734 }
735
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000736 xmlHashAddEntry2(ctxt->attsSpecial, fullname, fullattr,
737 (void *) (long) type);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +0000738 return;
739
740mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000741 xmlErrMemory(ctxt, NULL);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +0000742 return;
743}
744
Daniel Veillard4432df22003-09-28 18:58:27 +0000745/**
746 * xmlCheckLanguageID:
747 * @lang: pointer to the string value
748 *
749 * Checks that the value conforms to the LanguageID production:
750 *
751 * NOTE: this is somewhat deprecated, those productions were removed from
752 * the XML Second edition.
753 *
754 * [33] LanguageID ::= Langcode ('-' Subcode)*
755 * [34] Langcode ::= ISO639Code | IanaCode | UserCode
756 * [35] ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z])
757 * [36] IanaCode ::= ('i' | 'I') '-' ([a-z] | [A-Z])+
758 * [37] UserCode ::= ('x' | 'X') '-' ([a-z] | [A-Z])+
759 * [38] Subcode ::= ([a-z] | [A-Z])+
760 *
761 * Returns 1 if correct 0 otherwise
762 **/
763int
764xmlCheckLanguageID(const xmlChar * lang)
765{
766 const xmlChar *cur = lang;
767
768 if (cur == NULL)
769 return (0);
770 if (((cur[0] == 'i') && (cur[1] == '-')) ||
771 ((cur[0] == 'I') && (cur[1] == '-'))) {
772 /*
773 * IANA code
774 */
775 cur += 2;
776 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
777 ((cur[0] >= 'a') && (cur[0] <= 'z')))
778 cur++;
779 } else if (((cur[0] == 'x') && (cur[1] == '-')) ||
780 ((cur[0] == 'X') && (cur[1] == '-'))) {
781 /*
782 * User code
783 */
784 cur += 2;
785 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
786 ((cur[0] >= 'a') && (cur[0] <= 'z')))
787 cur++;
788 } else if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
789 ((cur[0] >= 'a') && (cur[0] <= 'z'))) {
790 /*
791 * ISO639
792 */
793 cur++;
794 if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
795 ((cur[0] >= 'a') && (cur[0] <= 'z')))
796 cur++;
797 else
798 return (0);
799 } else
800 return (0);
801 while (cur[0] != 0) { /* non input consuming */
802 if (cur[0] != '-')
803 return (0);
804 cur++;
805 if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
806 ((cur[0] >= 'a') && (cur[0] <= 'z')))
807 cur++;
808 else
809 return (0);
810 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
811 ((cur[0] >= 'a') && (cur[0] <= 'z')))
812 cur++;
813 }
814 return (1);
815}
816
Owen Taylor3473f882001-02-23 17:55:21 +0000817/************************************************************************
818 * *
819 * Parser stacks related functions and macros *
820 * *
821 ************************************************************************/
822
823xmlEntityPtr xmlParseStringEntityRef(xmlParserCtxtPtr ctxt,
824 const xmlChar ** str);
825
Daniel Veillard0fb18932003-09-07 09:14:37 +0000826#ifdef SAX2
827/**
828 * nsPush:
829 * @ctxt: an XML parser context
830 * @prefix: the namespace prefix or NULL
831 * @URL: the namespace name
832 *
833 * Pushes a new parser namespace on top of the ns stack
834 *
William M. Brack7b9154b2003-09-27 19:23:50 +0000835 * Returns -1 in case of error, -2 if the namespace should be discarded
836 * and the index in the stack otherwise.
Daniel Veillard0fb18932003-09-07 09:14:37 +0000837 */
838static int
839nsPush(xmlParserCtxtPtr ctxt, const xmlChar *prefix, const xmlChar *URL)
840{
Daniel Veillarddca8cc72003-09-26 13:53:14 +0000841 if (ctxt->options & XML_PARSE_NSCLEAN) {
842 int i;
843 for (i = 0;i < ctxt->nsNr;i += 2) {
844 if (ctxt->nsTab[i] == prefix) {
845 /* in scope */
846 if (ctxt->nsTab[i + 1] == URL)
847 return(-2);
848 /* out of scope keep it */
849 break;
850 }
851 }
852 }
Daniel Veillard0fb18932003-09-07 09:14:37 +0000853 if ((ctxt->nsMax == 0) || (ctxt->nsTab == NULL)) {
854 ctxt->nsMax = 10;
855 ctxt->nsNr = 0;
856 ctxt->nsTab = (const xmlChar **)
857 xmlMalloc(ctxt->nsMax * sizeof(xmlChar *));
858 if (ctxt->nsTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000859 xmlErrMemory(ctxt, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +0000860 ctxt->nsMax = 0;
861 return (-1);
862 }
863 } else if (ctxt->nsNr >= ctxt->nsMax) {
864 ctxt->nsMax *= 2;
865 ctxt->nsTab = (const xmlChar **)
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000866 xmlRealloc((char *) ctxt->nsTab,
Daniel Veillard0fb18932003-09-07 09:14:37 +0000867 ctxt->nsMax * sizeof(ctxt->nsTab[0]));
868 if (ctxt->nsTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000869 xmlErrMemory(ctxt, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +0000870 ctxt->nsMax /= 2;
871 return (-1);
872 }
873 }
874 ctxt->nsTab[ctxt->nsNr++] = prefix;
875 ctxt->nsTab[ctxt->nsNr++] = URL;
876 return (ctxt->nsNr);
877}
878/**
879 * nsPop:
880 * @ctxt: an XML parser context
881 * @nr: the number to pop
882 *
883 * Pops the top @nr parser prefix/namespace from the ns stack
884 *
885 * Returns the number of namespaces removed
886 */
887static int
888nsPop(xmlParserCtxtPtr ctxt, int nr)
889{
890 int i;
891
892 if (ctxt->nsTab == NULL) return(0);
893 if (ctxt->nsNr < nr) {
894 xmlGenericError(xmlGenericErrorContext, "Pbm popping %d NS\n", nr);
895 nr = ctxt->nsNr;
896 }
897 if (ctxt->nsNr <= 0)
898 return (0);
899
900 for (i = 0;i < nr;i++) {
901 ctxt->nsNr--;
902 ctxt->nsTab[ctxt->nsNr] = NULL;
903 }
904 return(nr);
905}
906#endif
907
908static int
909xmlCtxtGrowAttrs(xmlParserCtxtPtr ctxt, int nr) {
910 const xmlChar **atts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000911 int *attallocs;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000912 int maxatts;
913
914 if (ctxt->atts == NULL) {
Daniel Veillarde57ec792003-09-10 10:50:59 +0000915 maxatts = 55; /* allow for 10 attrs by default */
Daniel Veillard0fb18932003-09-07 09:14:37 +0000916 atts = (const xmlChar **)
917 xmlMalloc(maxatts * sizeof(xmlChar *));
Daniel Veillarde57ec792003-09-10 10:50:59 +0000918 if (atts == NULL) goto mem_error;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000919 ctxt->atts = atts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000920 attallocs = (int *) xmlMalloc((maxatts / 5) * sizeof(int));
921 if (attallocs == NULL) goto mem_error;
922 ctxt->attallocs = attallocs;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000923 ctxt->maxatts = maxatts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000924 } else if (nr + 5 > ctxt->maxatts) {
925 maxatts = (nr + 5) * 2;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000926 atts = (const xmlChar **) xmlRealloc((void *) ctxt->atts,
927 maxatts * sizeof(const xmlChar *));
Daniel Veillarde57ec792003-09-10 10:50:59 +0000928 if (atts == NULL) goto mem_error;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000929 ctxt->atts = atts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000930 attallocs = (int *) xmlRealloc((void *) ctxt->attallocs,
931 (maxatts / 5) * sizeof(int));
932 if (attallocs == NULL) goto mem_error;
933 ctxt->attallocs = attallocs;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000934 ctxt->maxatts = maxatts;
935 }
936 return(ctxt->maxatts);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000937mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000938 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000939 return(-1);
Daniel Veillard0fb18932003-09-07 09:14:37 +0000940}
941
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000942/**
943 * inputPush:
944 * @ctxt: an XML parser context
Daniel Veillard9d06d302002-01-22 18:15:52 +0000945 * @value: the parser input
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000946 *
947 * Pushes a new parser input on top of the input stack
Daniel Veillard9d06d302002-01-22 18:15:52 +0000948 *
949 * Returns 0 in case of error, the index in the stack otherwise
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000950 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000951int
Daniel Veillard1c732d22002-11-30 11:22:59 +0000952inputPush(xmlParserCtxtPtr ctxt, xmlParserInputPtr value)
953{
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000954 if ((ctxt == NULL) || (value == NULL))
955 return(0);
Daniel Veillard1c732d22002-11-30 11:22:59 +0000956 if (ctxt->inputNr >= ctxt->inputMax) {
957 ctxt->inputMax *= 2;
958 ctxt->inputTab =
959 (xmlParserInputPtr *) xmlRealloc(ctxt->inputTab,
960 ctxt->inputMax *
961 sizeof(ctxt->inputTab[0]));
962 if (ctxt->inputTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000963 xmlErrMemory(ctxt, NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +0000964 return (0);
965 }
966 }
967 ctxt->inputTab[ctxt->inputNr] = value;
968 ctxt->input = value;
969 return (ctxt->inputNr++);
970}
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000971/**
Daniel Veillard1c732d22002-11-30 11:22:59 +0000972 * inputPop:
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000973 * @ctxt: an XML parser context
974 *
Daniel Veillard1c732d22002-11-30 11:22:59 +0000975 * Pops the top parser input from the input stack
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000976 *
Daniel Veillard1c732d22002-11-30 11:22:59 +0000977 * Returns the input just removed
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000978 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000979xmlParserInputPtr
Daniel Veillard1c732d22002-11-30 11:22:59 +0000980inputPop(xmlParserCtxtPtr ctxt)
981{
982 xmlParserInputPtr ret;
983
Daniel Veillard36e5cd52004-11-02 14:52:23 +0000984 if (ctxt == NULL)
985 return(NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +0000986 if (ctxt->inputNr <= 0)
987 return (0);
988 ctxt->inputNr--;
989 if (ctxt->inputNr > 0)
990 ctxt->input = ctxt->inputTab[ctxt->inputNr - 1];
991 else
992 ctxt->input = NULL;
993 ret = ctxt->inputTab[ctxt->inputNr];
994 ctxt->inputTab[ctxt->inputNr] = 0;
995 return (ret);
996}
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000997/**
998 * nodePush:
999 * @ctxt: an XML parser context
Daniel Veillard9d06d302002-01-22 18:15:52 +00001000 * @value: the element node
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001001 *
1002 * Pushes a new element node on top of the node stack
Daniel Veillard9d06d302002-01-22 18:15:52 +00001003 *
1004 * Returns 0 in case of error, the index in the stack otherwise
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001005 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +00001006int
Daniel Veillard1c732d22002-11-30 11:22:59 +00001007nodePush(xmlParserCtxtPtr ctxt, xmlNodePtr value)
1008{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001009 if (ctxt == NULL) return(0);
Daniel Veillard1c732d22002-11-30 11:22:59 +00001010 if (ctxt->nodeNr >= ctxt->nodeMax) {
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00001011 xmlNodePtr *tmp;
1012
1013 tmp = (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
1014 ctxt->nodeMax * 2 *
Daniel Veillard1c732d22002-11-30 11:22:59 +00001015 sizeof(ctxt->nodeTab[0]));
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00001016 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001017 xmlErrMemory(ctxt, NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +00001018 return (0);
1019 }
Daniel Veillardd0cf7f62004-11-09 16:17:02 +00001020 ctxt->nodeTab = tmp;
1021 ctxt->nodeMax *= 2;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001022 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00001023 if (((unsigned int) ctxt->nodeNr) > xmlParserMaxDepth) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001024 xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard4aede2e2003-10-17 12:43:59 +00001025 "Excessive depth in document: change xmlParserMaxDepth = %d\n",
1026 xmlParserMaxDepth);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00001027 ctxt->instate = XML_PARSER_EOF;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00001028 return(0);
1029 }
Daniel Veillard1c732d22002-11-30 11:22:59 +00001030 ctxt->nodeTab[ctxt->nodeNr] = value;
1031 ctxt->node = value;
1032 return (ctxt->nodeNr++);
1033}
1034/**
1035 * nodePop:
1036 * @ctxt: an XML parser context
1037 *
1038 * Pops the top element node from the node stack
1039 *
1040 * Returns the node just removed
Owen Taylor3473f882001-02-23 17:55:21 +00001041 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +00001042xmlNodePtr
Daniel Veillard1c732d22002-11-30 11:22:59 +00001043nodePop(xmlParserCtxtPtr ctxt)
1044{
1045 xmlNodePtr ret;
1046
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001047 if (ctxt == NULL) return(NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +00001048 if (ctxt->nodeNr <= 0)
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001049 return (NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +00001050 ctxt->nodeNr--;
1051 if (ctxt->nodeNr > 0)
1052 ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
1053 else
1054 ctxt->node = NULL;
1055 ret = ctxt->nodeTab[ctxt->nodeNr];
1056 ctxt->nodeTab[ctxt->nodeNr] = 0;
1057 return (ret);
1058}
Daniel Veillarda2351322004-06-27 12:08:10 +00001059
1060#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard1c732d22002-11-30 11:22:59 +00001061/**
Daniel Veillarde57ec792003-09-10 10:50:59 +00001062 * nameNsPush:
1063 * @ctxt: an XML parser context
1064 * @value: the element name
1065 * @prefix: the element prefix
1066 * @URI: the element namespace name
1067 *
1068 * Pushes a new element name/prefix/URL on top of the name stack
1069 *
1070 * Returns -1 in case of error, the index in the stack otherwise
1071 */
1072static int
1073nameNsPush(xmlParserCtxtPtr ctxt, const xmlChar * value,
1074 const xmlChar *prefix, const xmlChar *URI, int nsNr)
1075{
1076 if (ctxt->nameNr >= ctxt->nameMax) {
1077 const xmlChar * *tmp;
1078 void **tmp2;
1079 ctxt->nameMax *= 2;
1080 tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab,
1081 ctxt->nameMax *
1082 sizeof(ctxt->nameTab[0]));
1083 if (tmp == NULL) {
1084 ctxt->nameMax /= 2;
1085 goto mem_error;
1086 }
1087 ctxt->nameTab = tmp;
1088 tmp2 = (void **) xmlRealloc((void * *)ctxt->pushTab,
1089 ctxt->nameMax * 3 *
1090 sizeof(ctxt->pushTab[0]));
1091 if (tmp2 == NULL) {
1092 ctxt->nameMax /= 2;
1093 goto mem_error;
1094 }
1095 ctxt->pushTab = tmp2;
1096 }
1097 ctxt->nameTab[ctxt->nameNr] = value;
1098 ctxt->name = value;
1099 ctxt->pushTab[ctxt->nameNr * 3] = (void *) prefix;
1100 ctxt->pushTab[ctxt->nameNr * 3 + 1] = (void *) URI;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001101 ctxt->pushTab[ctxt->nameNr * 3 + 2] = (void *) (long) nsNr;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001102 return (ctxt->nameNr++);
1103mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001104 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001105 return (-1);
1106}
1107/**
1108 * nameNsPop:
1109 * @ctxt: an XML parser context
1110 *
1111 * Pops the top element/prefix/URI name from the name stack
1112 *
1113 * Returns the name just removed
1114 */
1115static const xmlChar *
1116nameNsPop(xmlParserCtxtPtr ctxt)
1117{
1118 const xmlChar *ret;
1119
1120 if (ctxt->nameNr <= 0)
1121 return (0);
1122 ctxt->nameNr--;
1123 if (ctxt->nameNr > 0)
1124 ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
1125 else
1126 ctxt->name = NULL;
1127 ret = ctxt->nameTab[ctxt->nameNr];
1128 ctxt->nameTab[ctxt->nameNr] = NULL;
1129 return (ret);
1130}
Daniel Veillarda2351322004-06-27 12:08:10 +00001131#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001132
1133/**
Daniel Veillard1c732d22002-11-30 11:22:59 +00001134 * namePush:
1135 * @ctxt: an XML parser context
1136 * @value: the element name
1137 *
1138 * Pushes a new element name on top of the name stack
1139 *
Daniel Veillarde57ec792003-09-10 10:50:59 +00001140 * Returns -1 in case of error, the index in the stack otherwise
Daniel Veillard1c732d22002-11-30 11:22:59 +00001141 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +00001142int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001143namePush(xmlParserCtxtPtr ctxt, const xmlChar * value)
Daniel Veillard1c732d22002-11-30 11:22:59 +00001144{
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001145 if (ctxt == NULL) return (-1);
1146
Daniel Veillard1c732d22002-11-30 11:22:59 +00001147 if (ctxt->nameNr >= ctxt->nameMax) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00001148 const xmlChar * *tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001149 ctxt->nameMax *= 2;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001150 tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab,
Daniel Veillard1c732d22002-11-30 11:22:59 +00001151 ctxt->nameMax *
1152 sizeof(ctxt->nameTab[0]));
Daniel Veillarde57ec792003-09-10 10:50:59 +00001153 if (tmp == NULL) {
1154 ctxt->nameMax /= 2;
1155 goto mem_error;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001156 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001157 ctxt->nameTab = tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001158 }
1159 ctxt->nameTab[ctxt->nameNr] = value;
1160 ctxt->name = value;
1161 return (ctxt->nameNr++);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001162mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001163 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001164 return (-1);
Daniel Veillard1c732d22002-11-30 11:22:59 +00001165}
1166/**
1167 * namePop:
1168 * @ctxt: an XML parser context
1169 *
1170 * Pops the top element name from the name stack
1171 *
1172 * Returns the name just removed
1173 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +00001174const xmlChar *
Daniel Veillard1c732d22002-11-30 11:22:59 +00001175namePop(xmlParserCtxtPtr ctxt)
1176{
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001177 const xmlChar *ret;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001178
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00001179 if ((ctxt == NULL) || (ctxt->nameNr <= 0))
1180 return (NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +00001181 ctxt->nameNr--;
1182 if (ctxt->nameNr > 0)
1183 ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
1184 else
1185 ctxt->name = NULL;
1186 ret = ctxt->nameTab[ctxt->nameNr];
1187 ctxt->nameTab[ctxt->nameNr] = 0;
1188 return (ret);
1189}
Owen Taylor3473f882001-02-23 17:55:21 +00001190
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001191static int spacePush(xmlParserCtxtPtr ctxt, int val) {
Owen Taylor3473f882001-02-23 17:55:21 +00001192 if (ctxt->spaceNr >= ctxt->spaceMax) {
1193 ctxt->spaceMax *= 2;
1194 ctxt->spaceTab = (int *) xmlRealloc(ctxt->spaceTab,
1195 ctxt->spaceMax * sizeof(ctxt->spaceTab[0]));
1196 if (ctxt->spaceTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001197 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001198 return(0);
1199 }
1200 }
1201 ctxt->spaceTab[ctxt->spaceNr] = val;
1202 ctxt->space = &ctxt->spaceTab[ctxt->spaceNr];
1203 return(ctxt->spaceNr++);
1204}
1205
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001206static int spacePop(xmlParserCtxtPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00001207 int ret;
1208 if (ctxt->spaceNr <= 0) return(0);
1209 ctxt->spaceNr--;
1210 if (ctxt->spaceNr > 0)
1211 ctxt->space = &ctxt->spaceTab[ctxt->spaceNr - 1];
1212 else
1213 ctxt->space = NULL;
1214 ret = ctxt->spaceTab[ctxt->spaceNr];
1215 ctxt->spaceTab[ctxt->spaceNr] = -1;
1216 return(ret);
1217}
1218
1219/*
1220 * Macros for accessing the content. Those should be used only by the parser,
1221 * and not exported.
1222 *
1223 * Dirty macros, i.e. one often need to make assumption on the context to
1224 * use them
1225 *
1226 * CUR_PTR return the current pointer to the xmlChar to be parsed.
1227 * To be used with extreme caution since operations consuming
1228 * characters may move the input buffer to a different location !
1229 * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled
1230 * This should be used internally by the parser
1231 * only to compare to ASCII values otherwise it would break when
1232 * running with UTF-8 encoding.
1233 * RAW same as CUR but in the input buffer, bypass any token
1234 * extraction that may have been done
1235 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
1236 * to compare on ASCII based substring.
1237 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
Daniel Veillard77a90a72003-03-22 00:04:05 +00001238 * strings without newlines within the parser.
1239 * NEXT1(l) Skip 1 xmlChar, and must also be used only to skip 1 non-newline ASCII
1240 * defined char within the parser.
Owen Taylor3473f882001-02-23 17:55:21 +00001241 * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
1242 *
1243 * NEXT Skip to the next character, this does the proper decoding
1244 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
Daniel Veillard77a90a72003-03-22 00:04:05 +00001245 * NEXTL(l) Skip the current unicode character of l xmlChars long.
Owen Taylor3473f882001-02-23 17:55:21 +00001246 * CUR_CHAR(l) returns the current unicode character (int), set l
1247 * to the number of xmlChars used for the encoding [0-5].
1248 * CUR_SCHAR same but operate on a string instead of the context
1249 * COPY_BUF copy the current unicode char to the target buffer, increment
1250 * the index
1251 * GROW, SHRINK handling of input buffers
1252 */
1253
Daniel Veillardfdc91562002-07-01 21:52:03 +00001254#define RAW (*ctxt->input->cur)
1255#define CUR (*ctxt->input->cur)
Owen Taylor3473f882001-02-23 17:55:21 +00001256#define NXT(val) ctxt->input->cur[(val)]
1257#define CUR_PTR ctxt->input->cur
1258
Daniel Veillarda07050d2003-10-19 14:46:32 +00001259#define CMP4( s, c1, c2, c3, c4 ) \
1260 ( ((unsigned char *) s)[ 0 ] == c1 && ((unsigned char *) s)[ 1 ] == c2 && \
1261 ((unsigned char *) s)[ 2 ] == c3 && ((unsigned char *) s)[ 3 ] == c4 )
1262#define CMP5( s, c1, c2, c3, c4, c5 ) \
1263 ( CMP4( s, c1, c2, c3, c4 ) && ((unsigned char *) s)[ 4 ] == c5 )
1264#define CMP6( s, c1, c2, c3, c4, c5, c6 ) \
1265 ( CMP5( s, c1, c2, c3, c4, c5 ) && ((unsigned char *) s)[ 5 ] == c6 )
1266#define CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) \
1267 ( CMP6( s, c1, c2, c3, c4, c5, c6 ) && ((unsigned char *) s)[ 6 ] == c7 )
1268#define CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) \
1269 ( CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) && ((unsigned char *) s)[ 7 ] == c8 )
1270#define CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) \
1271 ( CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) && \
1272 ((unsigned char *) s)[ 8 ] == c9 )
1273#define CMP10( s, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 ) \
1274 ( CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) && \
1275 ((unsigned char *) s)[ 9 ] == c10 )
1276
Owen Taylor3473f882001-02-23 17:55:21 +00001277#define SKIP(val) do { \
Daniel Veillard77a90a72003-03-22 00:04:05 +00001278 ctxt->nbChars += (val),ctxt->input->cur += (val),ctxt->input->col+=(val); \
Owen Taylor3473f882001-02-23 17:55:21 +00001279 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
Daniel Veillard561b7f82002-03-20 21:55:57 +00001280 if ((*ctxt->input->cur == 0) && \
Owen Taylor3473f882001-02-23 17:55:21 +00001281 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \
1282 xmlPopInput(ctxt); \
1283 } while (0)
1284
Daniel Veillard0b787f32004-03-26 17:29:53 +00001285#define SKIPL(val) do { \
1286 int skipl; \
1287 for(skipl=0; skipl<val; skipl++) { \
1288 if (*(ctxt->input->cur) == '\n') { \
1289 ctxt->input->line++; ctxt->input->col = 1; \
1290 } else ctxt->input->col++; \
1291 ctxt->nbChars++; \
1292 ctxt->input->cur++; \
1293 } \
1294 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
1295 if ((*ctxt->input->cur == 0) && \
1296 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \
1297 xmlPopInput(ctxt); \
1298 } while (0)
1299
Daniel Veillarda880b122003-04-21 21:36:41 +00001300#define SHRINK if ((ctxt->progressive == 0) && \
Daniel Veillard6155d8a2003-08-19 15:01:28 +00001301 (ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \
1302 (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \
Daniel Veillard46de64e2002-05-29 08:21:33 +00001303 xmlSHRINK (ctxt);
1304
1305static void xmlSHRINK (xmlParserCtxtPtr ctxt) {
1306 xmlParserInputShrink(ctxt->input);
1307 if ((*ctxt->input->cur == 0) &&
1308 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
1309 xmlPopInput(ctxt);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001310 }
Owen Taylor3473f882001-02-23 17:55:21 +00001311
Daniel Veillarda880b122003-04-21 21:36:41 +00001312#define GROW if ((ctxt->progressive == 0) && \
1313 (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \
Daniel Veillard46de64e2002-05-29 08:21:33 +00001314 xmlGROW (ctxt);
1315
1316static void xmlGROW (xmlParserCtxtPtr ctxt) {
1317 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1318 if ((*ctxt->input->cur == 0) &&
1319 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
1320 xmlPopInput(ctxt);
Daniel Veillarda880b122003-04-21 21:36:41 +00001321}
Owen Taylor3473f882001-02-23 17:55:21 +00001322
1323#define SKIP_BLANKS xmlSkipBlankChars(ctxt)
1324
1325#define NEXT xmlNextChar(ctxt)
1326
Daniel Veillard21a0f912001-02-25 19:54:14 +00001327#define NEXT1 { \
Daniel Veillard77a90a72003-03-22 00:04:05 +00001328 ctxt->input->col++; \
Daniel Veillard21a0f912001-02-25 19:54:14 +00001329 ctxt->input->cur++; \
1330 ctxt->nbChars++; \
Daniel Veillard561b7f82002-03-20 21:55:57 +00001331 if (*ctxt->input->cur == 0) \
Daniel Veillard21a0f912001-02-25 19:54:14 +00001332 xmlParserInputGrow(ctxt->input, INPUT_CHUNK); \
1333 }
1334
Owen Taylor3473f882001-02-23 17:55:21 +00001335#define NEXTL(l) do { \
1336 if (*(ctxt->input->cur) == '\n') { \
1337 ctxt->input->line++; ctxt->input->col = 1; \
1338 } else ctxt->input->col++; \
Daniel Veillardfdc91562002-07-01 21:52:03 +00001339 ctxt->input->cur += l; \
Owen Taylor3473f882001-02-23 17:55:21 +00001340 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
Owen Taylor3473f882001-02-23 17:55:21 +00001341 } while (0)
1342
1343#define CUR_CHAR(l) xmlCurrentChar(ctxt, &l)
1344#define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l)
1345
1346#define COPY_BUF(l,b,i,v) \
1347 if (l == 1) b[i++] = (xmlChar) v; \
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001348 else i += xmlCopyCharMultiByte(&b[i],v)
Owen Taylor3473f882001-02-23 17:55:21 +00001349
1350/**
1351 * xmlSkipBlankChars:
1352 * @ctxt: the XML parser context
1353 *
1354 * skip all blanks character found at that point in the input streams.
1355 * It pops up finished entities in the process if allowable at that point.
1356 *
1357 * Returns the number of space chars skipped
1358 */
1359
1360int
1361xmlSkipBlankChars(xmlParserCtxtPtr ctxt) {
Daniel Veillard02141ea2001-04-30 11:46:40 +00001362 int res = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001363
1364 /*
1365 * It's Okay to use CUR/NEXT here since all the blanks are on
1366 * the ASCII range.
1367 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00001368 if ((ctxt->inputNr == 1) && (ctxt->instate != XML_PARSER_DTD)) {
1369 const xmlChar *cur;
Owen Taylor3473f882001-02-23 17:55:21 +00001370 /*
Daniel Veillard02141ea2001-04-30 11:46:40 +00001371 * if we are in the document content, go really fast
Owen Taylor3473f882001-02-23 17:55:21 +00001372 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00001373 cur = ctxt->input->cur;
William M. Brack76e95df2003-10-18 16:20:14 +00001374 while (IS_BLANK_CH(*cur)) {
Daniel Veillard02141ea2001-04-30 11:46:40 +00001375 if (*cur == '\n') {
1376 ctxt->input->line++; ctxt->input->col = 1;
1377 }
1378 cur++;
1379 res++;
1380 if (*cur == 0) {
1381 ctxt->input->cur = cur;
1382 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1383 cur = ctxt->input->cur;
1384 }
1385 }
1386 ctxt->input->cur = cur;
1387 } else {
1388 int cur;
1389 do {
1390 cur = CUR;
Daniel Veillard7da92702005-01-23 20:15:53 +00001391 while (IS_BLANK_CH(cur)) { /* CHECKED tstblanks.xml */
Daniel Veillard02141ea2001-04-30 11:46:40 +00001392 NEXT;
1393 cur = CUR;
1394 res++;
1395 }
1396 while ((cur == 0) && (ctxt->inputNr > 1) &&
1397 (ctxt->instate != XML_PARSER_COMMENT)) {
1398 xmlPopInput(ctxt);
1399 cur = CUR;
1400 }
1401 /*
1402 * Need to handle support of entities branching here
1403 */
1404 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt);
1405 } while (IS_BLANK(cur)); /* CHECKED tstblanks.xml */
1406 }
Owen Taylor3473f882001-02-23 17:55:21 +00001407 return(res);
1408}
1409
1410/************************************************************************
1411 * *
1412 * Commodity functions to handle entities *
1413 * *
1414 ************************************************************************/
1415
1416/**
1417 * xmlPopInput:
1418 * @ctxt: an XML parser context
1419 *
1420 * xmlPopInput: the current input pointed by ctxt->input came to an end
1421 * pop it and return the next char.
1422 *
1423 * Returns the current xmlChar in the parser context
1424 */
1425xmlChar
1426xmlPopInput(xmlParserCtxtPtr ctxt) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00001427 if ((ctxt == NULL) || (ctxt->inputNr <= 1)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00001428 if (xmlParserDebugEntities)
1429 xmlGenericError(xmlGenericErrorContext,
1430 "Popping input %d\n", ctxt->inputNr);
1431 xmlFreeInputStream(inputPop(ctxt));
Daniel Veillard561b7f82002-03-20 21:55:57 +00001432 if ((*ctxt->input->cur == 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +00001433 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
1434 return(xmlPopInput(ctxt));
1435 return(CUR);
1436}
1437
1438/**
1439 * xmlPushInput:
1440 * @ctxt: an XML parser context
1441 * @input: an XML parser input fragment (entity, XML fragment ...).
1442 *
1443 * xmlPushInput: switch to a new input stream which is stacked on top
1444 * of the previous one(s).
1445 */
1446void
1447xmlPushInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr input) {
1448 if (input == NULL) return;
1449
1450 if (xmlParserDebugEntities) {
1451 if ((ctxt->input != NULL) && (ctxt->input->filename))
1452 xmlGenericError(xmlGenericErrorContext,
1453 "%s(%d): ", ctxt->input->filename,
1454 ctxt->input->line);
1455 xmlGenericError(xmlGenericErrorContext,
1456 "Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur);
1457 }
1458 inputPush(ctxt, input);
1459 GROW;
1460}
1461
1462/**
1463 * xmlParseCharRef:
1464 * @ctxt: an XML parser context
1465 *
1466 * parse Reference declarations
1467 *
1468 * [66] CharRef ::= '&#' [0-9]+ ';' |
1469 * '&#x' [0-9a-fA-F]+ ';'
1470 *
1471 * [ WFC: Legal Character ]
1472 * Characters referred to using character references must match the
1473 * production for Char.
1474 *
1475 * Returns the value parsed (as an int), 0 in case of error
1476 */
1477int
1478xmlParseCharRef(xmlParserCtxtPtr ctxt) {
Daniel Veillard50582112001-03-26 22:52:16 +00001479 unsigned int val = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001480 int count = 0;
Daniel Veillard37fd3072004-06-03 11:22:31 +00001481 unsigned int outofrange = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001482
Owen Taylor3473f882001-02-23 17:55:21 +00001483 /*
1484 * Using RAW/CUR/NEXT is okay since we are working on ASCII range here
1485 */
Daniel Veillard561b7f82002-03-20 21:55:57 +00001486 if ((RAW == '&') && (NXT(1) == '#') &&
Owen Taylor3473f882001-02-23 17:55:21 +00001487 (NXT(2) == 'x')) {
1488 SKIP(3);
1489 GROW;
1490 while (RAW != ';') { /* loop blocked by count */
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00001491 if (count++ > 20) {
1492 count = 0;
1493 GROW;
1494 }
1495 if ((RAW >= '0') && (RAW <= '9'))
Owen Taylor3473f882001-02-23 17:55:21 +00001496 val = val * 16 + (CUR - '0');
1497 else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20))
1498 val = val * 16 + (CUR - 'a') + 10;
1499 else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20))
1500 val = val * 16 + (CUR - 'A') + 10;
1501 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001502 xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001503 val = 0;
1504 break;
1505 }
Daniel Veillard37fd3072004-06-03 11:22:31 +00001506 if (val > 0x10FFFF)
1507 outofrange = val;
1508
Owen Taylor3473f882001-02-23 17:55:21 +00001509 NEXT;
1510 count++;
1511 }
1512 if (RAW == ';') {
1513 /* on purpose to avoid reentrancy problems with NEXT and SKIP */
Daniel Veillard77a90a72003-03-22 00:04:05 +00001514 ctxt->input->col++;
Owen Taylor3473f882001-02-23 17:55:21 +00001515 ctxt->nbChars ++;
1516 ctxt->input->cur++;
1517 }
Daniel Veillard561b7f82002-03-20 21:55:57 +00001518 } else if ((RAW == '&') && (NXT(1) == '#')) {
Owen Taylor3473f882001-02-23 17:55:21 +00001519 SKIP(2);
1520 GROW;
1521 while (RAW != ';') { /* loop blocked by count */
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00001522 if (count++ > 20) {
1523 count = 0;
1524 GROW;
1525 }
1526 if ((RAW >= '0') && (RAW <= '9'))
Owen Taylor3473f882001-02-23 17:55:21 +00001527 val = val * 10 + (CUR - '0');
1528 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001529 xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001530 val = 0;
1531 break;
1532 }
Daniel Veillard37fd3072004-06-03 11:22:31 +00001533 if (val > 0x10FFFF)
1534 outofrange = val;
1535
Owen Taylor3473f882001-02-23 17:55:21 +00001536 NEXT;
1537 count++;
1538 }
1539 if (RAW == ';') {
1540 /* on purpose to avoid reentrancy problems with NEXT and SKIP */
Daniel Veillard77a90a72003-03-22 00:04:05 +00001541 ctxt->input->col++;
Owen Taylor3473f882001-02-23 17:55:21 +00001542 ctxt->nbChars ++;
1543 ctxt->input->cur++;
1544 }
1545 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001546 xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001547 }
1548
1549 /*
1550 * [ WFC: Legal Character ]
1551 * Characters referred to using character references must match the
1552 * production for Char.
1553 */
Daniel Veillard37fd3072004-06-03 11:22:31 +00001554 if ((IS_CHAR(val) && (outofrange == 0))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001555 return(val);
1556 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001557 xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
1558 "xmlParseCharRef: invalid xmlChar value %d\n",
1559 val);
Owen Taylor3473f882001-02-23 17:55:21 +00001560 }
1561 return(0);
1562}
1563
1564/**
1565 * xmlParseStringCharRef:
1566 * @ctxt: an XML parser context
1567 * @str: a pointer to an index in the string
1568 *
1569 * parse Reference declarations, variant parsing from a string rather
1570 * than an an input flow.
1571 *
1572 * [66] CharRef ::= '&#' [0-9]+ ';' |
1573 * '&#x' [0-9a-fA-F]+ ';'
1574 *
1575 * [ WFC: Legal Character ]
1576 * Characters referred to using character references must match the
1577 * production for Char.
1578 *
1579 * Returns the value parsed (as an int), 0 in case of error, str will be
1580 * updated to the current value of the index
1581 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001582static int
Owen Taylor3473f882001-02-23 17:55:21 +00001583xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) {
1584 const xmlChar *ptr;
1585 xmlChar cur;
Daniel Veillard37fd3072004-06-03 11:22:31 +00001586 unsigned int val = 0;
1587 unsigned int outofrange = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001588
1589 if ((str == NULL) || (*str == NULL)) return(0);
1590 ptr = *str;
1591 cur = *ptr;
1592 if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) {
1593 ptr += 3;
1594 cur = *ptr;
1595 while (cur != ';') { /* Non input consuming loop */
1596 if ((cur >= '0') && (cur <= '9'))
1597 val = val * 16 + (cur - '0');
1598 else if ((cur >= 'a') && (cur <= 'f'))
1599 val = val * 16 + (cur - 'a') + 10;
1600 else if ((cur >= 'A') && (cur <= 'F'))
1601 val = val * 16 + (cur - 'A') + 10;
1602 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001603 xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001604 val = 0;
1605 break;
1606 }
Daniel Veillard37fd3072004-06-03 11:22:31 +00001607 if (val > 0x10FFFF)
1608 outofrange = val;
1609
Owen Taylor3473f882001-02-23 17:55:21 +00001610 ptr++;
1611 cur = *ptr;
1612 }
1613 if (cur == ';')
1614 ptr++;
1615 } else if ((cur == '&') && (ptr[1] == '#')){
1616 ptr += 2;
1617 cur = *ptr;
1618 while (cur != ';') { /* Non input consuming loops */
1619 if ((cur >= '0') && (cur <= '9'))
1620 val = val * 10 + (cur - '0');
1621 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001622 xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001623 val = 0;
1624 break;
1625 }
Daniel Veillard37fd3072004-06-03 11:22:31 +00001626 if (val > 0x10FFFF)
1627 outofrange = val;
1628
Owen Taylor3473f882001-02-23 17:55:21 +00001629 ptr++;
1630 cur = *ptr;
1631 }
1632 if (cur == ';')
1633 ptr++;
1634 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001635 xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001636 return(0);
1637 }
1638 *str = ptr;
1639
1640 /*
1641 * [ WFC: Legal Character ]
1642 * Characters referred to using character references must match the
1643 * production for Char.
1644 */
Daniel Veillard37fd3072004-06-03 11:22:31 +00001645 if ((IS_CHAR(val) && (outofrange == 0))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001646 return(val);
1647 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001648 xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
1649 "xmlParseStringCharRef: invalid xmlChar value %d\n",
1650 val);
Owen Taylor3473f882001-02-23 17:55:21 +00001651 }
1652 return(0);
1653}
1654
1655/**
Daniel Veillardf5582f12002-06-11 10:08:16 +00001656 * xmlNewBlanksWrapperInputStream:
1657 * @ctxt: an XML parser context
1658 * @entity: an Entity pointer
1659 *
1660 * Create a new input stream for wrapping
1661 * blanks around a PEReference
1662 *
1663 * Returns the new input stream or NULL
1664 */
1665
1666static void deallocblankswrapper (xmlChar *str) {xmlFree(str);}
1667
Daniel Veillardf4862f02002-09-10 11:13:43 +00001668static xmlParserInputPtr
Daniel Veillardf5582f12002-06-11 10:08:16 +00001669xmlNewBlanksWrapperInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
1670 xmlParserInputPtr input;
1671 xmlChar *buffer;
1672 size_t length;
1673 if (entity == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001674 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
1675 "xmlNewBlanksWrapperInputStream entity\n");
Daniel Veillardf5582f12002-06-11 10:08:16 +00001676 return(NULL);
1677 }
1678 if (xmlParserDebugEntities)
1679 xmlGenericError(xmlGenericErrorContext,
1680 "new blanks wrapper for entity: %s\n", entity->name);
1681 input = xmlNewInputStream(ctxt);
1682 if (input == NULL) {
1683 return(NULL);
1684 }
1685 length = xmlStrlen(entity->name) + 5;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001686 buffer = xmlMallocAtomic(length);
Daniel Veillardf5582f12002-06-11 10:08:16 +00001687 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001688 xmlErrMemory(ctxt, NULL);
Daniel Veillardf5582f12002-06-11 10:08:16 +00001689 return(NULL);
1690 }
1691 buffer [0] = ' ';
1692 buffer [1] = '%';
1693 buffer [length-3] = ';';
1694 buffer [length-2] = ' ';
1695 buffer [length-1] = 0;
1696 memcpy(buffer + 2, entity->name, length - 5);
1697 input->free = deallocblankswrapper;
1698 input->base = buffer;
1699 input->cur = buffer;
1700 input->length = length;
1701 input->end = &buffer[length];
1702 return(input);
1703}
1704
1705/**
Owen Taylor3473f882001-02-23 17:55:21 +00001706 * xmlParserHandlePEReference:
1707 * @ctxt: the parser context
1708 *
1709 * [69] PEReference ::= '%' Name ';'
1710 *
1711 * [ WFC: No Recursion ]
1712 * A parsed entity must not contain a recursive
1713 * reference to itself, either directly or indirectly.
1714 *
1715 * [ WFC: Entity Declared ]
1716 * In a document without any DTD, a document with only an internal DTD
1717 * subset which contains no parameter entity references, or a document
1718 * with "standalone='yes'", ... ... The declaration of a parameter
1719 * entity must precede any reference to it...
1720 *
1721 * [ VC: Entity Declared ]
1722 * In a document with an external subset or external parameter entities
1723 * with "standalone='no'", ... ... The declaration of a parameter entity
1724 * must precede any reference to it...
1725 *
1726 * [ WFC: In DTD ]
1727 * Parameter-entity references may only appear in the DTD.
1728 * NOTE: misleading but this is handled.
1729 *
1730 * A PEReference may have been detected in the current input stream
1731 * the handling is done accordingly to
1732 * http://www.w3.org/TR/REC-xml#entproc
1733 * i.e.
1734 * - Included in literal in entity values
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001735 * - Included as Parameter Entity reference within DTDs
Owen Taylor3473f882001-02-23 17:55:21 +00001736 */
1737void
1738xmlParserHandlePEReference(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001739 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00001740 xmlEntityPtr entity = NULL;
1741 xmlParserInputPtr input;
1742
Owen Taylor3473f882001-02-23 17:55:21 +00001743 if (RAW != '%') return;
1744 switch(ctxt->instate) {
1745 case XML_PARSER_CDATA_SECTION:
1746 return;
1747 case XML_PARSER_COMMENT:
1748 return;
1749 case XML_PARSER_START_TAG:
1750 return;
1751 case XML_PARSER_END_TAG:
1752 return;
1753 case XML_PARSER_EOF:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001754 xmlFatalErr(ctxt, XML_ERR_PEREF_AT_EOF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001755 return;
1756 case XML_PARSER_PROLOG:
1757 case XML_PARSER_START:
1758 case XML_PARSER_MISC:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001759 xmlFatalErr(ctxt, XML_ERR_PEREF_IN_PROLOG, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001760 return;
1761 case XML_PARSER_ENTITY_DECL:
1762 case XML_PARSER_CONTENT:
1763 case XML_PARSER_ATTRIBUTE_VALUE:
1764 case XML_PARSER_PI:
1765 case XML_PARSER_SYSTEM_LITERAL:
Daniel Veillard4a7ae502002-02-18 19:18:17 +00001766 case XML_PARSER_PUBLIC_LITERAL:
Owen Taylor3473f882001-02-23 17:55:21 +00001767 /* we just ignore it there */
1768 return;
1769 case XML_PARSER_EPILOG:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001770 xmlFatalErr(ctxt, XML_ERR_PEREF_IN_EPILOG, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001771 return;
1772 case XML_PARSER_ENTITY_VALUE:
1773 /*
1774 * NOTE: in the case of entity values, we don't do the
1775 * substitution here since we need the literal
1776 * entity value to be able to save the internal
1777 * subset of the document.
1778 * This will be handled by xmlStringDecodeEntities
1779 */
1780 return;
1781 case XML_PARSER_DTD:
1782 /*
1783 * [WFC: Well-Formedness Constraint: PEs in Internal Subset]
1784 * In the internal DTD subset, parameter-entity references
1785 * can occur only where markup declarations can occur, not
1786 * within markup declarations.
1787 * In that case this is handled in xmlParseMarkupDecl
1788 */
1789 if ((ctxt->external == 0) && (ctxt->inputNr == 1))
1790 return;
William M. Brack76e95df2003-10-18 16:20:14 +00001791 if (IS_BLANK_CH(NXT(1)) || NXT(1) == 0)
Daniel Veillardf5582f12002-06-11 10:08:16 +00001792 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001793 break;
1794 case XML_PARSER_IGNORE:
1795 return;
1796 }
1797
1798 NEXT;
1799 name = xmlParseName(ctxt);
1800 if (xmlParserDebugEntities)
1801 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001802 "PEReference: %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00001803 if (name == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001804 xmlFatalErr(ctxt, XML_ERR_PEREF_NO_NAME, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001805 } else {
1806 if (RAW == ';') {
1807 NEXT;
1808 if ((ctxt->sax != NULL) && (ctxt->sax->getParameterEntity != NULL))
1809 entity = ctxt->sax->getParameterEntity(ctxt->userData, name);
1810 if (entity == NULL) {
1811
1812 /*
1813 * [ WFC: Entity Declared ]
1814 * In a document without any DTD, a document with only an
1815 * internal DTD subset which contains no parameter entity
1816 * references, or a document with "standalone='yes'", ...
1817 * ... The declaration of a parameter entity must precede
1818 * any reference to it...
1819 */
1820 if ((ctxt->standalone == 1) ||
1821 ((ctxt->hasExternalSubset == 0) &&
1822 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00001823 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00001824 "PEReference: %%%s; not found\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00001825 } else {
1826 /*
1827 * [ VC: Entity Declared ]
1828 * In a document with an external subset or external
1829 * parameter entities with "standalone='no'", ...
1830 * ... The declaration of a parameter entity must precede
1831 * any reference to it...
1832 */
Daniel Veillard24eb9782003-10-04 21:08:09 +00001833 if ((ctxt->validate) && (ctxt->vctxt.error != NULL)) {
1834 xmlValidityError(ctxt, XML_WAR_UNDECLARED_ENTITY,
1835 "PEReference: %%%s; not found\n",
1836 name);
1837 } else
1838 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
1839 "PEReference: %%%s; not found\n",
1840 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001841 ctxt->valid = 0;
1842 }
Daniel Veillardf5582f12002-06-11 10:08:16 +00001843 } else if (ctxt->input->free != deallocblankswrapper) {
1844 input = xmlNewBlanksWrapperInputStream(ctxt, entity);
1845 xmlPushInput(ctxt, input);
Owen Taylor3473f882001-02-23 17:55:21 +00001846 } else {
1847 if ((entity->etype == XML_INTERNAL_PARAMETER_ENTITY) ||
1848 (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY)) {
Daniel Veillard87a764e2001-06-20 17:41:10 +00001849 xmlChar start[4];
1850 xmlCharEncoding enc;
1851
Owen Taylor3473f882001-02-23 17:55:21 +00001852 /*
1853 * handle the extra spaces added before and after
1854 * c.f. http://www.w3.org/TR/REC-xml#as-PE
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001855 * this is done independently.
Owen Taylor3473f882001-02-23 17:55:21 +00001856 */
1857 input = xmlNewEntityInputStream(ctxt, entity);
1858 xmlPushInput(ctxt, input);
Daniel Veillard87a764e2001-06-20 17:41:10 +00001859
1860 /*
1861 * Get the 4 first bytes and decode the charset
1862 * if enc != XML_CHAR_ENCODING_NONE
1863 * plug some encoding conversion routines.
William M. Bracka0c48ad2004-04-16 15:58:29 +00001864 * Note that, since we may have some non-UTF8
1865 * encoding (like UTF16, bug 135229), the 'length'
1866 * is not known, but we can calculate based upon
1867 * the amount of data in the buffer.
Daniel Veillard87a764e2001-06-20 17:41:10 +00001868 */
1869 GROW
William M. Bracka0c48ad2004-04-16 15:58:29 +00001870 if ((ctxt->input->end - ctxt->input->cur)>=4) {
Daniel Veillarde059b892002-06-13 15:32:10 +00001871 start[0] = RAW;
1872 start[1] = NXT(1);
1873 start[2] = NXT(2);
1874 start[3] = NXT(3);
1875 enc = xmlDetectCharEncoding(start, 4);
1876 if (enc != XML_CHAR_ENCODING_NONE) {
1877 xmlSwitchEncoding(ctxt, enc);
1878 }
Daniel Veillard87a764e2001-06-20 17:41:10 +00001879 }
1880
Owen Taylor3473f882001-02-23 17:55:21 +00001881 if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) &&
Daniel Veillarda07050d2003-10-19 14:46:32 +00001882 (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l' )) &&
1883 (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001884 xmlParseTextDecl(ctxt);
1885 }
Owen Taylor3473f882001-02-23 17:55:21 +00001886 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00001887 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER,
1888 "PEReference: %s is not a parameter entity\n",
1889 name);
Owen Taylor3473f882001-02-23 17:55:21 +00001890 }
1891 }
1892 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001893 xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001894 }
Owen Taylor3473f882001-02-23 17:55:21 +00001895 }
1896}
1897
1898/*
1899 * Macro used to grow the current buffer.
1900 */
1901#define growBuffer(buffer) { \
Daniel Veillardd3999c72004-03-10 16:27:03 +00001902 xmlChar *tmp; \
Owen Taylor3473f882001-02-23 17:55:21 +00001903 buffer##_size *= 2; \
Daniel Veillardd3999c72004-03-10 16:27:03 +00001904 tmp = (xmlChar *) \
Owen Taylor3473f882001-02-23 17:55:21 +00001905 xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \
Daniel Veillardd3999c72004-03-10 16:27:03 +00001906 if (tmp == NULL) goto mem_error; \
1907 buffer = tmp; \
Owen Taylor3473f882001-02-23 17:55:21 +00001908}
1909
1910/**
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00001911 * xmlStringLenDecodeEntities:
Owen Taylor3473f882001-02-23 17:55:21 +00001912 * @ctxt: the parser context
1913 * @str: the input string
Daniel Veillarde57ec792003-09-10 10:50:59 +00001914 * @len: the string length
Owen Taylor3473f882001-02-23 17:55:21 +00001915 * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
1916 * @end: an end marker xmlChar, 0 if none
1917 * @end2: an end marker xmlChar, 0 if none
1918 * @end3: an end marker xmlChar, 0 if none
1919 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001920 * Takes a entity string content and process to do the adequate substitutions.
Owen Taylor3473f882001-02-23 17:55:21 +00001921 *
1922 * [67] Reference ::= EntityRef | CharRef
1923 *
1924 * [69] PEReference ::= '%' Name ';'
1925 *
1926 * Returns A newly allocated string with the substitution done. The caller
1927 * must deallocate it !
1928 */
1929xmlChar *
Daniel Veillarde57ec792003-09-10 10:50:59 +00001930xmlStringLenDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int len,
1931 int what, xmlChar end, xmlChar end2, xmlChar end3) {
Owen Taylor3473f882001-02-23 17:55:21 +00001932 xmlChar *buffer = NULL;
1933 int buffer_size = 0;
1934
1935 xmlChar *current = NULL;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001936 const xmlChar *last;
Owen Taylor3473f882001-02-23 17:55:21 +00001937 xmlEntityPtr ent;
1938 int c,l;
1939 int nbchars = 0;
1940
Daniel Veillarda82b1822004-11-08 16:24:57 +00001941 if ((ctxt == NULL) || (str == NULL) || (len < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00001942 return(NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001943 last = str + len;
Owen Taylor3473f882001-02-23 17:55:21 +00001944
1945 if (ctxt->depth > 40) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001946 xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001947 return(NULL);
1948 }
1949
1950 /*
1951 * allocate a translation buffer.
1952 */
1953 buffer_size = XML_PARSER_BIG_BUFFER_SIZE;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001954 buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar));
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00001955 if (buffer == NULL) goto mem_error;
Owen Taylor3473f882001-02-23 17:55:21 +00001956
1957 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001958 * OK loop until we reach one of the ending char or a size limit.
Owen Taylor3473f882001-02-23 17:55:21 +00001959 * we are operating on already parsed values.
1960 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001961 if (str < last)
1962 c = CUR_SCHAR(str, l);
1963 else
1964 c = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001965 while ((c != 0) && (c != end) && /* non input consuming loop */
1966 (c != end2) && (c != end3)) {
1967
1968 if (c == 0) break;
1969 if ((c == '&') && (str[1] == '#')) {
1970 int val = xmlParseStringCharRef(ctxt, &str);
1971 if (val != 0) {
1972 COPY_BUF(0,buffer,nbchars,val);
1973 }
1974 } else if ((c == '&') && (what & XML_SUBSTITUTE_REF)) {
1975 if (xmlParserDebugEntities)
1976 xmlGenericError(xmlGenericErrorContext,
1977 "String decoding Entity Reference: %.30s\n",
1978 str);
1979 ent = xmlParseStringEntityRef(ctxt, &str);
1980 if ((ent != NULL) &&
1981 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1982 if (ent->content != NULL) {
1983 COPY_BUF(0,buffer,nbchars,ent->content[0]);
1984 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00001985 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
1986 "predefined entity has no content\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001987 }
1988 } else if ((ent != NULL) && (ent->content != NULL)) {
1989 xmlChar *rep;
1990
1991 ctxt->depth++;
1992 rep = xmlStringDecodeEntities(ctxt, ent->content, what,
1993 0, 0, 0);
1994 ctxt->depth--;
1995 if (rep != NULL) {
1996 current = rep;
1997 while (*current != 0) { /* non input consuming loop */
1998 buffer[nbchars++] = *current++;
1999 if (nbchars >
2000 buffer_size - XML_PARSER_BUFFER_SIZE) {
2001 growBuffer(buffer);
2002 }
2003 }
2004 xmlFree(rep);
2005 }
2006 } else if (ent != NULL) {
2007 int i = xmlStrlen(ent->name);
2008 const xmlChar *cur = ent->name;
2009
2010 buffer[nbchars++] = '&';
2011 if (nbchars > buffer_size - i - XML_PARSER_BUFFER_SIZE) {
2012 growBuffer(buffer);
2013 }
2014 for (;i > 0;i--)
2015 buffer[nbchars++] = *cur++;
2016 buffer[nbchars++] = ';';
2017 }
2018 } else if (c == '%' && (what & XML_SUBSTITUTE_PEREF)) {
2019 if (xmlParserDebugEntities)
2020 xmlGenericError(xmlGenericErrorContext,
2021 "String decoding PE Reference: %.30s\n", str);
2022 ent = xmlParseStringPEReference(ctxt, &str);
2023 if (ent != NULL) {
2024 xmlChar *rep;
2025
2026 ctxt->depth++;
2027 rep = xmlStringDecodeEntities(ctxt, ent->content, what,
2028 0, 0, 0);
2029 ctxt->depth--;
2030 if (rep != NULL) {
2031 current = rep;
2032 while (*current != 0) { /* non input consuming loop */
2033 buffer[nbchars++] = *current++;
2034 if (nbchars >
2035 buffer_size - XML_PARSER_BUFFER_SIZE) {
2036 growBuffer(buffer);
2037 }
2038 }
2039 xmlFree(rep);
2040 }
2041 }
2042 } else {
2043 COPY_BUF(l,buffer,nbchars,c);
2044 str += l;
2045 if (nbchars > buffer_size - XML_PARSER_BUFFER_SIZE) {
2046 growBuffer(buffer);
2047 }
2048 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002049 if (str < last)
2050 c = CUR_SCHAR(str, l);
2051 else
2052 c = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002053 }
2054 buffer[nbchars++] = 0;
2055 return(buffer);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002056
2057mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002058 xmlErrMemory(ctxt, NULL);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002059 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002060}
2061
Daniel Veillarde57ec792003-09-10 10:50:59 +00002062/**
2063 * xmlStringDecodeEntities:
2064 * @ctxt: the parser context
2065 * @str: the input string
2066 * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
2067 * @end: an end marker xmlChar, 0 if none
2068 * @end2: an end marker xmlChar, 0 if none
2069 * @end3: an end marker xmlChar, 0 if none
2070 *
2071 * Takes a entity string content and process to do the adequate substitutions.
2072 *
2073 * [67] Reference ::= EntityRef | CharRef
2074 *
2075 * [69] PEReference ::= '%' Name ';'
2076 *
2077 * Returns A newly allocated string with the substitution done. The caller
2078 * must deallocate it !
2079 */
2080xmlChar *
2081xmlStringDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int what,
2082 xmlChar end, xmlChar end2, xmlChar end3) {
Daniel Veillarda82b1822004-11-08 16:24:57 +00002083 if ((ctxt == NULL) || (str == NULL)) return(NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002084 return(xmlStringLenDecodeEntities(ctxt, str, xmlStrlen(str), what,
2085 end, end2, end3));
2086}
Owen Taylor3473f882001-02-23 17:55:21 +00002087
2088/************************************************************************
2089 * *
Owen Taylor3473f882001-02-23 17:55:21 +00002090 * Commodity functions, cleanup needed ? *
2091 * *
2092 ************************************************************************/
2093
2094/**
2095 * areBlanks:
2096 * @ctxt: an XML parser context
2097 * @str: a xmlChar *
2098 * @len: the size of @str
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002099 * @blank_chars: we know the chars are blanks
Owen Taylor3473f882001-02-23 17:55:21 +00002100 *
2101 * Is this a sequence of blank chars that one can ignore ?
2102 *
2103 * Returns 1 if ignorable 0 otherwise.
2104 */
2105
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002106static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len,
2107 int blank_chars) {
Owen Taylor3473f882001-02-23 17:55:21 +00002108 int i, ret;
2109 xmlNodePtr lastChild;
2110
Daniel Veillard05c13a22001-09-09 08:38:09 +00002111 /*
2112 * Don't spend time trying to differentiate them, the same callback is
2113 * used !
2114 */
2115 if (ctxt->sax->ignorableWhitespace == ctxt->sax->characters)
Daniel Veillard2f362242001-03-02 17:36:21 +00002116 return(0);
2117
Owen Taylor3473f882001-02-23 17:55:21 +00002118 /*
2119 * Check for xml:space value.
2120 */
2121 if (*(ctxt->space) == 1)
2122 return(0);
2123
2124 /*
2125 * Check that the string is made of blanks
2126 */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002127 if (blank_chars == 0) {
2128 for (i = 0;i < len;i++)
2129 if (!(IS_BLANK_CH(str[i]))) return(0);
2130 }
Owen Taylor3473f882001-02-23 17:55:21 +00002131
2132 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002133 * Look if the element is mixed content in the DTD if available
Owen Taylor3473f882001-02-23 17:55:21 +00002134 */
Daniel Veillard6dd398f2001-07-25 22:41:03 +00002135 if (ctxt->node == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002136 if (ctxt->myDoc != NULL) {
2137 ret = xmlIsMixedElement(ctxt->myDoc, ctxt->node->name);
2138 if (ret == 0) return(1);
2139 if (ret == 1) return(0);
2140 }
2141
2142 /*
2143 * Otherwise, heuristic :-\
2144 */
Daniel Veillard561b7f82002-03-20 21:55:57 +00002145 if (RAW != '<') return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002146 if ((ctxt->node->children == NULL) &&
2147 (RAW == '<') && (NXT(1) == '/')) return(0);
2148
2149 lastChild = xmlGetLastChild(ctxt->node);
2150 if (lastChild == NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002151 if ((ctxt->node->type != XML_ELEMENT_NODE) &&
2152 (ctxt->node->content != NULL)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002153 } else if (xmlNodeIsText(lastChild))
2154 return(0);
2155 else if ((ctxt->node->children != NULL) &&
2156 (xmlNodeIsText(ctxt->node->children)))
2157 return(0);
2158 return(1);
2159}
2160
Owen Taylor3473f882001-02-23 17:55:21 +00002161/************************************************************************
2162 * *
2163 * Extra stuff for namespace support *
2164 * Relates to http://www.w3.org/TR/WD-xml-names *
2165 * *
2166 ************************************************************************/
2167
2168/**
2169 * xmlSplitQName:
2170 * @ctxt: an XML parser context
2171 * @name: an XML parser context
2172 * @prefix: a xmlChar **
2173 *
2174 * parse an UTF8 encoded XML qualified name string
2175 *
2176 * [NS 5] QName ::= (Prefix ':')? LocalPart
2177 *
2178 * [NS 6] Prefix ::= NCName
2179 *
2180 * [NS 7] LocalPart ::= NCName
2181 *
2182 * Returns the local part, and prefix is updated
2183 * to get the Prefix if any.
2184 */
2185
2186xmlChar *
2187xmlSplitQName(xmlParserCtxtPtr ctxt, const xmlChar *name, xmlChar **prefix) {
2188 xmlChar buf[XML_MAX_NAMELEN + 5];
2189 xmlChar *buffer = NULL;
2190 int len = 0;
2191 int max = XML_MAX_NAMELEN;
2192 xmlChar *ret = NULL;
2193 const xmlChar *cur = name;
2194 int c;
2195
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00002196 if (prefix == NULL) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002197 *prefix = NULL;
2198
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002199 if (cur == NULL) return(NULL);
2200
Owen Taylor3473f882001-02-23 17:55:21 +00002201#ifndef XML_XML_NAMESPACE
2202 /* xml: prefix is not really a namespace */
2203 if ((cur[0] == 'x') && (cur[1] == 'm') &&
2204 (cur[2] == 'l') && (cur[3] == ':'))
2205 return(xmlStrdup(name));
2206#endif
2207
Daniel Veillard597bc482003-07-24 16:08:28 +00002208 /* nasty but well=formed */
Owen Taylor3473f882001-02-23 17:55:21 +00002209 if (cur[0] == ':')
2210 return(xmlStrdup(name));
2211
2212 c = *cur++;
2213 while ((c != 0) && (c != ':') && (len < max)) { /* tested bigname.xml */
2214 buf[len++] = c;
2215 c = *cur++;
2216 }
2217 if (len >= max) {
2218 /*
2219 * Okay someone managed to make a huge name, so he's ready to pay
2220 * for the processing speed.
2221 */
2222 max = len * 2;
2223
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002224 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002225 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002226 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002227 return(NULL);
2228 }
2229 memcpy(buffer, buf, len);
2230 while ((c != 0) && (c != ':')) { /* tested bigname.xml */
2231 if (len + 10 > max) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00002232 xmlChar *tmp;
2233
Owen Taylor3473f882001-02-23 17:55:21 +00002234 max *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00002235 tmp = (xmlChar *) xmlRealloc(buffer,
Owen Taylor3473f882001-02-23 17:55:21 +00002236 max * sizeof(xmlChar));
Daniel Veillard2248ff12004-09-22 23:05:14 +00002237 if (tmp == NULL) {
2238 xmlFree(tmp);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002239 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002240 return(NULL);
2241 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00002242 buffer = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002243 }
2244 buffer[len++] = c;
2245 c = *cur++;
2246 }
2247 buffer[len] = 0;
2248 }
2249
Daniel Veillard597bc482003-07-24 16:08:28 +00002250 /* nasty but well=formed
2251 if ((c == ':') && (*cur == 0)) {
2252 return(xmlStrdup(name));
2253 } */
2254
Owen Taylor3473f882001-02-23 17:55:21 +00002255 if (buffer == NULL)
2256 ret = xmlStrndup(buf, len);
2257 else {
2258 ret = buffer;
2259 buffer = NULL;
2260 max = XML_MAX_NAMELEN;
2261 }
2262
2263
2264 if (c == ':') {
Daniel Veillardbb284f42002-10-16 18:02:47 +00002265 c = *cur;
Owen Taylor3473f882001-02-23 17:55:21 +00002266 *prefix = ret;
Daniel Veillard597bc482003-07-24 16:08:28 +00002267 if (c == 0) {
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00002268 return(xmlStrndup(BAD_CAST "", 0));
Daniel Veillard597bc482003-07-24 16:08:28 +00002269 }
Owen Taylor3473f882001-02-23 17:55:21 +00002270 len = 0;
2271
Daniel Veillardbb284f42002-10-16 18:02:47 +00002272 /*
2273 * Check that the first character is proper to start
2274 * a new name
2275 */
2276 if (!(((c >= 0x61) && (c <= 0x7A)) ||
2277 ((c >= 0x41) && (c <= 0x5A)) ||
2278 (c == '_') || (c == ':'))) {
2279 int l;
2280 int first = CUR_SCHAR(cur, l);
2281
2282 if (!IS_LETTER(first) && (first != '_')) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00002283 xmlFatalErrMsgStr(ctxt, XML_NS_ERR_QNAME,
Daniel Veillardbb284f42002-10-16 18:02:47 +00002284 "Name %s is not XML Namespace compliant\n",
Daniel Veillardbc92eca2003-09-15 09:48:06 +00002285 name);
Daniel Veillardbb284f42002-10-16 18:02:47 +00002286 }
2287 }
2288 cur++;
2289
Owen Taylor3473f882001-02-23 17:55:21 +00002290 while ((c != 0) && (len < max)) { /* tested bigname2.xml */
2291 buf[len++] = c;
2292 c = *cur++;
2293 }
2294 if (len >= max) {
2295 /*
2296 * Okay someone managed to make a huge name, so he's ready to pay
2297 * for the processing speed.
2298 */
2299 max = len * 2;
2300
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002301 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002302 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002303 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002304 return(NULL);
2305 }
2306 memcpy(buffer, buf, len);
2307 while (c != 0) { /* tested bigname2.xml */
2308 if (len + 10 > max) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00002309 xmlChar *tmp;
2310
Owen Taylor3473f882001-02-23 17:55:21 +00002311 max *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00002312 tmp = (xmlChar *) xmlRealloc(buffer,
Owen Taylor3473f882001-02-23 17:55:21 +00002313 max * sizeof(xmlChar));
Daniel Veillard2248ff12004-09-22 23:05:14 +00002314 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002315 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00002316 xmlFree(buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00002317 return(NULL);
2318 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00002319 buffer = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002320 }
2321 buffer[len++] = c;
2322 c = *cur++;
2323 }
2324 buffer[len] = 0;
2325 }
2326
2327 if (buffer == NULL)
2328 ret = xmlStrndup(buf, len);
2329 else {
2330 ret = buffer;
2331 }
2332 }
2333
2334 return(ret);
2335}
2336
2337/************************************************************************
2338 * *
2339 * The parser itself *
2340 * Relates to http://www.w3.org/TR/REC-xml *
2341 * *
2342 ************************************************************************/
2343
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002344static const xmlChar * xmlParseNameComplex(xmlParserCtxtPtr ctxt);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002345static xmlChar * xmlParseAttValueInternal(xmlParserCtxtPtr ctxt,
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002346 int *len, int *alloc, int normalize);
Daniel Veillard0fb18932003-09-07 09:14:37 +00002347
Owen Taylor3473f882001-02-23 17:55:21 +00002348/**
2349 * xmlParseName:
2350 * @ctxt: an XML parser context
2351 *
2352 * parse an XML name.
2353 *
2354 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
2355 * CombiningChar | Extender
2356 *
2357 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
2358 *
Daniel Veillard807b4de2004-09-26 14:42:56 +00002359 * [6] Names ::= Name (#x20 Name)*
Owen Taylor3473f882001-02-23 17:55:21 +00002360 *
2361 * Returns the Name parsed or NULL
2362 */
2363
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002364const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002365xmlParseName(xmlParserCtxtPtr ctxt) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00002366 const xmlChar *in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002367 const xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00002368 int count = 0;
2369
2370 GROW;
Daniel Veillard48b2f892001-02-25 16:11:03 +00002371
2372 /*
2373 * Accelerator for simple ASCII names
2374 */
2375 in = ctxt->input->cur;
2376 if (((*in >= 0x61) && (*in <= 0x7A)) ||
2377 ((*in >= 0x41) && (*in <= 0x5A)) ||
2378 (*in == '_') || (*in == ':')) {
2379 in++;
2380 while (((*in >= 0x61) && (*in <= 0x7A)) ||
2381 ((*in >= 0x41) && (*in <= 0x5A)) ||
2382 ((*in >= 0x30) && (*in <= 0x39)) ||
Daniel Veillard76d66f42001-05-16 21:05:17 +00002383 (*in == '_') || (*in == '-') ||
2384 (*in == ':') || (*in == '.'))
Daniel Veillard48b2f892001-02-25 16:11:03 +00002385 in++;
Daniel Veillard76d66f42001-05-16 21:05:17 +00002386 if ((*in > 0) && (*in < 0x80)) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00002387 count = in - ctxt->input->cur;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002388 ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002389 ctxt->input->cur = in;
Daniel Veillard77a90a72003-03-22 00:04:05 +00002390 ctxt->nbChars += count;
2391 ctxt->input->col += count;
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002392 if (ret == NULL)
2393 xmlErrMemory(ctxt, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002394 return(ret);
2395 }
2396 }
Daniel Veillard2f362242001-03-02 17:36:21 +00002397 return(xmlParseNameComplex(ctxt));
Daniel Veillard21a0f912001-02-25 19:54:14 +00002398}
Daniel Veillard48b2f892001-02-25 16:11:03 +00002399
Daniel Veillard46de64e2002-05-29 08:21:33 +00002400/**
2401 * xmlParseNameAndCompare:
2402 * @ctxt: an XML parser context
2403 *
2404 * parse an XML name and compares for match
2405 * (specialized for endtag parsing)
2406 *
Daniel Veillard46de64e2002-05-29 08:21:33 +00002407 * Returns NULL for an illegal name, (xmlChar*) 1 for success
2408 * and the name for mismatch
2409 */
2410
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002411static const xmlChar *
Daniel Veillard46de64e2002-05-29 08:21:33 +00002412xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002413 register const xmlChar *cmp = other;
2414 register const xmlChar *in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002415 const xmlChar *ret;
Daniel Veillard46de64e2002-05-29 08:21:33 +00002416
2417 GROW;
2418
2419 in = ctxt->input->cur;
2420 while (*in != 0 && *in == *cmp) {
2421 ++in;
2422 ++cmp;
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +00002423 ctxt->input->col++;
Daniel Veillard46de64e2002-05-29 08:21:33 +00002424 }
William M. Brack76e95df2003-10-18 16:20:14 +00002425 if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
Daniel Veillard46de64e2002-05-29 08:21:33 +00002426 /* success */
2427 ctxt->input->cur = in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002428 return (const xmlChar*) 1;
Daniel Veillard46de64e2002-05-29 08:21:33 +00002429 }
2430 /* failure (or end of input buffer), check with full function */
2431 ret = xmlParseName (ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00002432 /* strings coming from the dictionnary direct compare possible */
2433 if (ret == other) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002434 return (const xmlChar*) 1;
Daniel Veillard46de64e2002-05-29 08:21:33 +00002435 }
2436 return ret;
2437}
2438
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002439static const xmlChar *
Daniel Veillard21a0f912001-02-25 19:54:14 +00002440xmlParseNameComplex(xmlParserCtxtPtr ctxt) {
Daniel Veillard21a0f912001-02-25 19:54:14 +00002441 int len = 0, l;
2442 int c;
2443 int count = 0;
2444
2445 /*
2446 * Handler for more complex cases
2447 */
2448 GROW;
Owen Taylor3473f882001-02-23 17:55:21 +00002449 c = CUR_CHAR(l);
2450 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
2451 (!IS_LETTER(c) && (c != '_') &&
2452 (c != ':'))) {
2453 return(NULL);
2454 }
2455
2456 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
William M. Brack871611b2003-10-18 04:53:14 +00002457 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
Owen Taylor3473f882001-02-23 17:55:21 +00002458 (c == '.') || (c == '-') ||
2459 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002460 (IS_COMBINING(c)) ||
2461 (IS_EXTENDER(c)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002462 if (count++ > 100) {
2463 count = 0;
2464 GROW;
2465 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002466 len += l;
Owen Taylor3473f882001-02-23 17:55:21 +00002467 NEXTL(l);
2468 c = CUR_CHAR(l);
Owen Taylor3473f882001-02-23 17:55:21 +00002469 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002470 return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len));
Owen Taylor3473f882001-02-23 17:55:21 +00002471}
2472
2473/**
2474 * xmlParseStringName:
2475 * @ctxt: an XML parser context
2476 * @str: a pointer to the string pointer (IN/OUT)
2477 *
2478 * parse an XML name.
2479 *
2480 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
2481 * CombiningChar | Extender
2482 *
2483 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
2484 *
Daniel Veillard807b4de2004-09-26 14:42:56 +00002485 * [6] Names ::= Name (#x20 Name)*
Owen Taylor3473f882001-02-23 17:55:21 +00002486 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002487 * Returns the Name parsed or NULL. The @str pointer
Owen Taylor3473f882001-02-23 17:55:21 +00002488 * is updated to the current location in the string.
2489 */
2490
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002491static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002492xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) {
2493 xmlChar buf[XML_MAX_NAMELEN + 5];
2494 const xmlChar *cur = *str;
2495 int len = 0, l;
2496 int c;
2497
2498 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +00002499 if (!IS_LETTER(c) && (c != '_') &&
Owen Taylor3473f882001-02-23 17:55:21 +00002500 (c != ':')) {
2501 return(NULL);
2502 }
2503
William M. Brack871611b2003-10-18 04:53:14 +00002504 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigentname.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002505 (c == '.') || (c == '-') ||
2506 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002507 (IS_COMBINING(c)) ||
2508 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002509 COPY_BUF(l,buf,len,c);
2510 cur += l;
2511 c = CUR_SCHAR(cur, l);
2512 if (len >= XML_MAX_NAMELEN) { /* test bigentname.xml */
2513 /*
2514 * Okay someone managed to make a huge name, so he's ready to pay
2515 * for the processing speed.
2516 */
2517 xmlChar *buffer;
2518 int max = len * 2;
2519
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002520 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002521 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002522 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002523 return(NULL);
2524 }
2525 memcpy(buffer, buf, len);
William M. Brack871611b2003-10-18 04:53:14 +00002526 while ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
Daniel Veillard73b013f2003-09-30 12:36:01 +00002527 /* test bigentname.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002528 (c == '.') || (c == '-') ||
2529 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002530 (IS_COMBINING(c)) ||
2531 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002532 if (len + 10 > max) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00002533 xmlChar *tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002534 max *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00002535 tmp = (xmlChar *) xmlRealloc(buffer,
Owen Taylor3473f882001-02-23 17:55:21 +00002536 max * sizeof(xmlChar));
Daniel Veillard2248ff12004-09-22 23:05:14 +00002537 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002538 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00002539 xmlFree(buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00002540 return(NULL);
2541 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00002542 buffer = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002543 }
2544 COPY_BUF(l,buffer,len,c);
2545 cur += l;
2546 c = CUR_SCHAR(cur, l);
2547 }
2548 buffer[len] = 0;
2549 *str = cur;
2550 return(buffer);
2551 }
2552 }
2553 *str = cur;
2554 return(xmlStrndup(buf, len));
2555}
2556
2557/**
2558 * xmlParseNmtoken:
2559 * @ctxt: an XML parser context
2560 *
2561 * parse an XML Nmtoken.
2562 *
2563 * [7] Nmtoken ::= (NameChar)+
2564 *
Daniel Veillard807b4de2004-09-26 14:42:56 +00002565 * [8] Nmtokens ::= Nmtoken (#x20 Nmtoken)*
Owen Taylor3473f882001-02-23 17:55:21 +00002566 *
2567 * Returns the Nmtoken parsed or NULL
2568 */
2569
2570xmlChar *
2571xmlParseNmtoken(xmlParserCtxtPtr ctxt) {
2572 xmlChar buf[XML_MAX_NAMELEN + 5];
2573 int len = 0, l;
2574 int c;
2575 int count = 0;
2576
2577 GROW;
2578 c = CUR_CHAR(l);
2579
William M. Brack871611b2003-10-18 04:53:14 +00002580 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigtoken.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002581 (c == '.') || (c == '-') ||
2582 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002583 (IS_COMBINING(c)) ||
2584 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002585 if (count++ > 100) {
2586 count = 0;
2587 GROW;
2588 }
2589 COPY_BUF(l,buf,len,c);
2590 NEXTL(l);
2591 c = CUR_CHAR(l);
2592 if (len >= XML_MAX_NAMELEN) {
2593 /*
2594 * Okay someone managed to make a huge token, so he's ready to pay
2595 * for the processing speed.
2596 */
2597 xmlChar *buffer;
2598 int max = len * 2;
2599
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002600 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002601 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002602 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002603 return(NULL);
2604 }
2605 memcpy(buffer, buf, len);
William M. Brack871611b2003-10-18 04:53:14 +00002606 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigtoken.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002607 (c == '.') || (c == '-') ||
2608 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002609 (IS_COMBINING(c)) ||
2610 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002611 if (count++ > 100) {
2612 count = 0;
2613 GROW;
2614 }
2615 if (len + 10 > max) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00002616 xmlChar *tmp;
2617
Owen Taylor3473f882001-02-23 17:55:21 +00002618 max *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00002619 tmp = (xmlChar *) xmlRealloc(buffer,
Owen Taylor3473f882001-02-23 17:55:21 +00002620 max * sizeof(xmlChar));
Daniel Veillard2248ff12004-09-22 23:05:14 +00002621 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002622 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00002623 xmlFree(buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00002624 return(NULL);
2625 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00002626 buffer = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002627 }
2628 COPY_BUF(l,buffer,len,c);
2629 NEXTL(l);
2630 c = CUR_CHAR(l);
2631 }
2632 buffer[len] = 0;
2633 return(buffer);
2634 }
2635 }
2636 if (len == 0)
2637 return(NULL);
2638 return(xmlStrndup(buf, len));
2639}
2640
2641/**
2642 * xmlParseEntityValue:
2643 * @ctxt: an XML parser context
2644 * @orig: if non-NULL store a copy of the original entity value
2645 *
2646 * parse a value for ENTITY declarations
2647 *
2648 * [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' |
2649 * "'" ([^%&'] | PEReference | Reference)* "'"
2650 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002651 * Returns the EntityValue parsed with reference substituted or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002652 */
2653
2654xmlChar *
2655xmlParseEntityValue(xmlParserCtxtPtr ctxt, xmlChar **orig) {
2656 xmlChar *buf = NULL;
2657 int len = 0;
2658 int size = XML_PARSER_BUFFER_SIZE;
2659 int c, l;
2660 xmlChar stop;
2661 xmlChar *ret = NULL;
2662 const xmlChar *cur = NULL;
2663 xmlParserInputPtr input;
2664
2665 if (RAW == '"') stop = '"';
2666 else if (RAW == '\'') stop = '\'';
2667 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002668 xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002669 return(NULL);
2670 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002671 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002672 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002673 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002674 return(NULL);
2675 }
2676
2677 /*
2678 * The content of the entity definition is copied in a buffer.
2679 */
2680
2681 ctxt->instate = XML_PARSER_ENTITY_VALUE;
2682 input = ctxt->input;
2683 GROW;
2684 NEXT;
2685 c = CUR_CHAR(l);
2686 /*
2687 * NOTE: 4.4.5 Included in Literal
2688 * When a parameter entity reference appears in a literal entity
2689 * value, ... a single or double quote character in the replacement
2690 * text is always treated as a normal data character and will not
2691 * terminate the literal.
2692 * In practice it means we stop the loop only when back at parsing
2693 * the initial entity and the quote is found
2694 */
William M. Brack871611b2003-10-18 04:53:14 +00002695 while ((IS_CHAR(c)) && ((c != stop) || /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00002696 (ctxt->input != input))) {
2697 if (len + 5 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00002698 xmlChar *tmp;
2699
Owen Taylor3473f882001-02-23 17:55:21 +00002700 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00002701 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
2702 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002703 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00002704 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00002705 return(NULL);
2706 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00002707 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002708 }
2709 COPY_BUF(l,buf,len,c);
2710 NEXTL(l);
2711 /*
2712 * Pop-up of finished entities.
2713 */
2714 while ((RAW == 0) && (ctxt->inputNr > 1)) /* non input consuming */
2715 xmlPopInput(ctxt);
2716
2717 GROW;
2718 c = CUR_CHAR(l);
2719 if (c == 0) {
2720 GROW;
2721 c = CUR_CHAR(l);
2722 }
2723 }
2724 buf[len] = 0;
2725
2726 /*
2727 * Raise problem w.r.t. '&' and '%' being used in non-entities
2728 * reference constructs. Note Charref will be handled in
2729 * xmlStringDecodeEntities()
2730 */
2731 cur = buf;
2732 while (*cur != 0) { /* non input consuming */
2733 if ((*cur == '%') || ((*cur == '&') && (cur[1] != '#'))) {
2734 xmlChar *name;
2735 xmlChar tmp = *cur;
2736
2737 cur++;
2738 name = xmlParseStringName(ctxt, &cur);
2739 if ((name == NULL) || (*cur != ';')) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00002740 xmlFatalErrMsgInt(ctxt, XML_ERR_ENTITY_CHAR_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00002741 "EntityValue: '%c' forbidden except for entities references\n",
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00002742 tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002743 }
Daniel Veillard5151c062001-10-23 13:10:19 +00002744 if ((tmp == '%') && (ctxt->inSubset == 1) &&
2745 (ctxt->inputNr == 1)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002746 xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002747 }
2748 if (name != NULL)
2749 xmlFree(name);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002750 if (*cur == 0)
2751 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002752 }
2753 cur++;
2754 }
2755
2756 /*
2757 * Then PEReference entities are substituted.
2758 */
2759 if (c != stop) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002760 xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002761 xmlFree(buf);
2762 } else {
2763 NEXT;
2764 /*
2765 * NOTE: 4.4.7 Bypassed
2766 * When a general entity reference appears in the EntityValue in
2767 * an entity declaration, it is bypassed and left as is.
2768 * so XML_SUBSTITUTE_REF is not set here.
2769 */
2770 ret = xmlStringDecodeEntities(ctxt, buf, XML_SUBSTITUTE_PEREF,
2771 0, 0, 0);
2772 if (orig != NULL)
2773 *orig = buf;
2774 else
2775 xmlFree(buf);
2776 }
2777
2778 return(ret);
2779}
2780
2781/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002782 * xmlParseAttValueComplex:
2783 * @ctxt: an XML parser context
Daniel Veillard0fb18932003-09-07 09:14:37 +00002784 * @len: the resulting attribute len
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002785 * @normalize: wether to apply the inner normalization
Daniel Veillard01c13b52002-12-10 15:19:08 +00002786 *
2787 * parse a value for an attribute, this is the fallback function
2788 * of xmlParseAttValue() when the attribute parsing requires handling
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002789 * of non-ASCII characters, or normalization compaction.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002790 *
2791 * Returns the AttValue parsed or NULL. The value has to be freed by the caller.
2792 */
Daniel Veillard0fb18932003-09-07 09:14:37 +00002793static xmlChar *
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002794xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) {
Daniel Veillarde72c7562002-05-31 09:47:30 +00002795 xmlChar limit = 0;
2796 xmlChar *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002797 int len = 0;
2798 int buf_size = 0;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002799 int c, l, in_space = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002800 xmlChar *current = NULL;
2801 xmlEntityPtr ent;
2802
Owen Taylor3473f882001-02-23 17:55:21 +00002803 if (NXT(0) == '"') {
2804 ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
2805 limit = '"';
2806 NEXT;
2807 } else if (NXT(0) == '\'') {
2808 limit = '\'';
2809 ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
2810 NEXT;
2811 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002812 xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002813 return(NULL);
2814 }
2815
2816 /*
2817 * allocate a translation buffer.
2818 */
2819 buf_size = XML_PARSER_BUFFER_SIZE;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002820 buf = (xmlChar *) xmlMallocAtomic(buf_size * sizeof(xmlChar));
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002821 if (buf == NULL) goto mem_error;
Owen Taylor3473f882001-02-23 17:55:21 +00002822
2823 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002824 * OK loop until we reach one of the ending char or a size limit.
Owen Taylor3473f882001-02-23 17:55:21 +00002825 */
2826 c = CUR_CHAR(l);
Daniel Veillardfdc91562002-07-01 21:52:03 +00002827 while ((NXT(0) != limit) && /* checked */
2828 (c != '<')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002829 if (c == 0) break;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002830 if (c == '&') {
Daniel Veillard62998c02003-09-15 12:56:36 +00002831 in_space = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002832 if (NXT(1) == '#') {
2833 int val = xmlParseCharRef(ctxt);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002834
Owen Taylor3473f882001-02-23 17:55:21 +00002835 if (val == '&') {
Daniel Veillard319a7422001-09-11 09:27:09 +00002836 if (ctxt->replaceEntities) {
2837 if (len > buf_size - 10) {
2838 growBuffer(buf);
2839 }
2840 buf[len++] = '&';
2841 } else {
2842 /*
2843 * The reparsing will be done in xmlStringGetNodeList()
2844 * called by the attribute() function in SAX.c
2845 */
Daniel Veillard319a7422001-09-11 09:27:09 +00002846 if (len > buf_size - 10) {
2847 growBuffer(buf);
2848 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002849 buf[len++] = '&';
2850 buf[len++] = '#';
2851 buf[len++] = '3';
2852 buf[len++] = '8';
2853 buf[len++] = ';';
Owen Taylor3473f882001-02-23 17:55:21 +00002854 }
2855 } else {
Daniel Veillard0b6b55b2001-03-20 11:27:34 +00002856 if (len > buf_size - 10) {
2857 growBuffer(buf);
2858 }
Owen Taylor3473f882001-02-23 17:55:21 +00002859 len += xmlCopyChar(0, &buf[len], val);
2860 }
2861 } else {
2862 ent = xmlParseEntityRef(ctxt);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002863 if ((ent != NULL) &&
2864 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
2865 if (len > buf_size - 10) {
2866 growBuffer(buf);
2867 }
2868 if ((ctxt->replaceEntities == 0) &&
2869 (ent->content[0] == '&')) {
2870 buf[len++] = '&';
2871 buf[len++] = '#';
2872 buf[len++] = '3';
2873 buf[len++] = '8';
2874 buf[len++] = ';';
2875 } else {
2876 buf[len++] = ent->content[0];
2877 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002878 } else if ((ent != NULL) &&
2879 (ctxt->replaceEntities != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002880 xmlChar *rep;
2881
2882 if (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) {
2883 rep = xmlStringDecodeEntities(ctxt, ent->content,
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002884 XML_SUBSTITUTE_REF,
2885 0, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002886 if (rep != NULL) {
2887 current = rep;
2888 while (*current != 0) { /* non input consuming */
2889 buf[len++] = *current++;
2890 if (len > buf_size - 10) {
2891 growBuffer(buf);
2892 }
2893 }
2894 xmlFree(rep);
2895 }
2896 } else {
Daniel Veillard0b6b55b2001-03-20 11:27:34 +00002897 if (len > buf_size - 10) {
2898 growBuffer(buf);
2899 }
Owen Taylor3473f882001-02-23 17:55:21 +00002900 if (ent->content != NULL)
2901 buf[len++] = ent->content[0];
2902 }
2903 } else if (ent != NULL) {
2904 int i = xmlStrlen(ent->name);
2905 const xmlChar *cur = ent->name;
2906
2907 /*
2908 * This may look absurd but is needed to detect
2909 * entities problems
2910 */
2911 if ((ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) &&
2912 (ent->content != NULL)) {
2913 xmlChar *rep;
2914 rep = xmlStringDecodeEntities(ctxt, ent->content,
2915 XML_SUBSTITUTE_REF, 0, 0, 0);
2916 if (rep != NULL)
2917 xmlFree(rep);
2918 }
2919
2920 /*
2921 * Just output the reference
2922 */
2923 buf[len++] = '&';
2924 if (len > buf_size - i - 10) {
2925 growBuffer(buf);
2926 }
2927 for (;i > 0;i--)
2928 buf[len++] = *cur++;
2929 buf[len++] = ';';
2930 }
2931 }
2932 } else {
2933 if ((c == 0x20) || (c == 0xD) || (c == 0xA) || (c == 0x9)) {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002934 if ((len != 0) || (!normalize)) {
2935 if ((!normalize) || (!in_space)) {
2936 COPY_BUF(l,buf,len,0x20);
2937 if (len > buf_size - 10) {
2938 growBuffer(buf);
2939 }
2940 }
2941 in_space = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002942 }
2943 } else {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002944 in_space = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002945 COPY_BUF(l,buf,len,c);
2946 if (len > buf_size - 10) {
2947 growBuffer(buf);
2948 }
2949 }
2950 NEXTL(l);
2951 }
2952 GROW;
2953 c = CUR_CHAR(l);
2954 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002955 if ((in_space) && (normalize)) {
2956 while (buf[len - 1] == 0x20) len--;
2957 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002958 buf[len] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002959 if (RAW == '<') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002960 xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002961 } else if (RAW != limit) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00002962 xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
2963 "AttValue: ' expected\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002964 } else
2965 NEXT;
Daniel Veillard0fb18932003-09-07 09:14:37 +00002966 if (attlen != NULL) *attlen = len;
Owen Taylor3473f882001-02-23 17:55:21 +00002967 return(buf);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002968
2969mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002970 xmlErrMemory(ctxt, NULL);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002971 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002972}
2973
2974/**
Daniel Veillard0fb18932003-09-07 09:14:37 +00002975 * xmlParseAttValue:
2976 * @ctxt: an XML parser context
2977 *
2978 * parse a value for an attribute
2979 * Note: the parser won't do substitution of entities here, this
2980 * will be handled later in xmlStringGetNodeList
2981 *
2982 * [10] AttValue ::= '"' ([^<&"] | Reference)* '"' |
2983 * "'" ([^<&'] | Reference)* "'"
2984 *
2985 * 3.3.3 Attribute-Value Normalization:
2986 * Before the value of an attribute is passed to the application or
2987 * checked for validity, the XML processor must normalize it as follows:
2988 * - a character reference is processed by appending the referenced
2989 * character to the attribute value
2990 * - an entity reference is processed by recursively processing the
2991 * replacement text of the entity
2992 * - a whitespace character (#x20, #xD, #xA, #x9) is processed by
2993 * appending #x20 to the normalized value, except that only a single
2994 * #x20 is appended for a "#xD#xA" sequence that is part of an external
2995 * parsed entity or the literal entity value of an internal parsed entity
2996 * - other characters are processed by appending them to the normalized value
2997 * If the declared value is not CDATA, then the XML processor must further
2998 * process the normalized attribute value by discarding any leading and
2999 * trailing space (#x20) characters, and by replacing sequences of space
3000 * (#x20) characters by a single space (#x20) character.
3001 * All attributes for which no declaration has been read should be treated
3002 * by a non-validating parser as if declared CDATA.
3003 *
3004 * Returns the AttValue parsed or NULL. The value has to be freed by the caller.
3005 */
3006
3007
3008xmlChar *
3009xmlParseAttValue(xmlParserCtxtPtr ctxt) {
Daniel Veillard2a4fb5a2004-11-08 14:02:18 +00003010 if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00003011 return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0));
Daniel Veillard0fb18932003-09-07 09:14:37 +00003012}
3013
3014/**
Owen Taylor3473f882001-02-23 17:55:21 +00003015 * xmlParseSystemLiteral:
3016 * @ctxt: an XML parser context
3017 *
3018 * parse an XML Literal
3019 *
3020 * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
3021 *
3022 * Returns the SystemLiteral parsed or NULL
3023 */
3024
3025xmlChar *
3026xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) {
3027 xmlChar *buf = NULL;
3028 int len = 0;
3029 int size = XML_PARSER_BUFFER_SIZE;
3030 int cur, l;
3031 xmlChar stop;
3032 int state = ctxt->instate;
3033 int count = 0;
3034
3035 SHRINK;
3036 if (RAW == '"') {
3037 NEXT;
3038 stop = '"';
3039 } else if (RAW == '\'') {
3040 NEXT;
3041 stop = '\'';
3042 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003043 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003044 return(NULL);
3045 }
3046
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003047 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003048 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003049 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003050 return(NULL);
3051 }
3052 ctxt->instate = XML_PARSER_SYSTEM_LITERAL;
3053 cur = CUR_CHAR(l);
William M. Brack871611b2003-10-18 04:53:14 +00003054 while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003055 if (len + 5 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00003056 xmlChar *tmp;
3057
Owen Taylor3473f882001-02-23 17:55:21 +00003058 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00003059 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3060 if (tmp == NULL) {
3061 xmlFree(buf);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003062 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003063 ctxt->instate = (xmlParserInputState) state;
3064 return(NULL);
3065 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00003066 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00003067 }
3068 count++;
3069 if (count > 50) {
3070 GROW;
3071 count = 0;
3072 }
3073 COPY_BUF(l,buf,len,cur);
3074 NEXTL(l);
3075 cur = CUR_CHAR(l);
3076 if (cur == 0) {
3077 GROW;
3078 SHRINK;
3079 cur = CUR_CHAR(l);
3080 }
3081 }
3082 buf[len] = 0;
3083 ctxt->instate = (xmlParserInputState) state;
William M. Brack871611b2003-10-18 04:53:14 +00003084 if (!IS_CHAR(cur)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003085 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003086 } else {
3087 NEXT;
3088 }
3089 return(buf);
3090}
3091
3092/**
3093 * xmlParsePubidLiteral:
3094 * @ctxt: an XML parser context
3095 *
3096 * parse an XML public literal
3097 *
3098 * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
3099 *
3100 * Returns the PubidLiteral parsed or NULL.
3101 */
3102
3103xmlChar *
3104xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) {
3105 xmlChar *buf = NULL;
3106 int len = 0;
3107 int size = XML_PARSER_BUFFER_SIZE;
3108 xmlChar cur;
3109 xmlChar stop;
3110 int count = 0;
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003111 xmlParserInputState oldstate = ctxt->instate;
Owen Taylor3473f882001-02-23 17:55:21 +00003112
3113 SHRINK;
3114 if (RAW == '"') {
3115 NEXT;
3116 stop = '"';
3117 } else if (RAW == '\'') {
3118 NEXT;
3119 stop = '\'';
3120 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003121 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003122 return(NULL);
3123 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003124 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003125 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003126 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003127 return(NULL);
3128 }
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003129 ctxt->instate = XML_PARSER_PUBLIC_LITERAL;
Owen Taylor3473f882001-02-23 17:55:21 +00003130 cur = CUR;
William M. Brack76e95df2003-10-18 16:20:14 +00003131 while ((IS_PUBIDCHAR_CH(cur)) && (cur != stop)) { /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003132 if (len + 1 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00003133 xmlChar *tmp;
3134
Owen Taylor3473f882001-02-23 17:55:21 +00003135 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00003136 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3137 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003138 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00003139 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003140 return(NULL);
3141 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00003142 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00003143 }
3144 buf[len++] = cur;
3145 count++;
3146 if (count > 50) {
3147 GROW;
3148 count = 0;
3149 }
3150 NEXT;
3151 cur = CUR;
3152 if (cur == 0) {
3153 GROW;
3154 SHRINK;
3155 cur = CUR;
3156 }
3157 }
3158 buf[len] = 0;
3159 if (cur != stop) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003160 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003161 } else {
3162 NEXT;
3163 }
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003164 ctxt->instate = oldstate;
Owen Taylor3473f882001-02-23 17:55:21 +00003165 return(buf);
3166}
3167
Daniel Veillard48b2f892001-02-25 16:11:03 +00003168void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata);
Owen Taylor3473f882001-02-23 17:55:21 +00003169/**
3170 * xmlParseCharData:
3171 * @ctxt: an XML parser context
3172 * @cdata: int indicating whether we are within a CDATA section
3173 *
3174 * parse a CharData section.
3175 * if we are within a CDATA section ']]>' marks an end of section.
3176 *
3177 * The right angle bracket (>) may be represented using the string "&gt;",
3178 * and must, for compatibility, be escaped using "&gt;" or a character
3179 * reference when it appears in the string "]]>" in content, when that
3180 * string is not marking the end of a CDATA section.
3181 *
3182 * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
3183 */
3184
3185void
3186xmlParseCharData(xmlParserCtxtPtr ctxt, int cdata) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00003187 const xmlChar *in;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003188 int nbchar = 0;
Daniel Veillard50582112001-03-26 22:52:16 +00003189 int line = ctxt->input->line;
3190 int col = ctxt->input->col;
Daniel Veillard0714c5b2005-01-23 00:01:01 +00003191 int ccol;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003192
3193 SHRINK;
3194 GROW;
3195 /*
3196 * Accelerated common case where input don't need to be
3197 * modified before passing it to the handler.
3198 */
Daniel Veillardfdc91562002-07-01 21:52:03 +00003199 if (!cdata) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00003200 in = ctxt->input->cur;
3201 do {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003202get_more_space:
3203 while (*in == 0x20) in++;
3204 if (*in == 0xA) {
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +00003205 ctxt->input->line++; ctxt->input->col = 1;
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003206 in++;
3207 while (*in == 0xA) {
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +00003208 ctxt->input->line++; ctxt->input->col = 1;
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003209 in++;
3210 }
3211 goto get_more_space;
3212 }
3213 if (*in == '<') {
3214 nbchar = in - ctxt->input->cur;
3215 if (nbchar > 0) {
3216 const xmlChar *tmp = ctxt->input->cur;
3217 ctxt->input->cur = in;
3218
Daniel Veillard34099b42004-11-04 17:34:35 +00003219 if ((ctxt->sax != NULL) &&
3220 (ctxt->sax->ignorableWhitespace !=
3221 ctxt->sax->characters)) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003222 if (areBlanks(ctxt, tmp, nbchar, 1)) {
Daniel Veillard32acf0c2005-03-31 14:12:37 +00003223 if (ctxt->sax->ignorableWhitespace != NULL)
3224 ctxt->sax->ignorableWhitespace(ctxt->userData,
3225 tmp, nbchar);
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003226 } else if (ctxt->sax->characters != NULL)
3227 ctxt->sax->characters(ctxt->userData,
3228 tmp, nbchar);
Daniel Veillard34099b42004-11-04 17:34:35 +00003229 } else if ((ctxt->sax != NULL) &&
3230 (ctxt->sax->characters != NULL)) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003231 ctxt->sax->characters(ctxt->userData,
3232 tmp, nbchar);
3233 }
3234 }
3235 return;
3236 }
Daniel Veillard0714c5b2005-01-23 00:01:01 +00003237
Daniel Veillard3ed155f2001-04-29 19:56:59 +00003238get_more:
Daniel Veillard0714c5b2005-01-23 00:01:01 +00003239 ccol = ctxt->input->col;
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003240 while (((*in > ']') && (*in <= 0x7F)) ||
3241 ((*in > '&') && (*in < '<')) ||
3242 ((*in > '<') && (*in < ']')) ||
3243 ((*in >= 0x20) && (*in < '&')) ||
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +00003244 (*in == 0x09)) {
3245 in++;
Daniel Veillard0714c5b2005-01-23 00:01:01 +00003246 ccol++;
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +00003247 }
Daniel Veillard0714c5b2005-01-23 00:01:01 +00003248 ctxt->input->col = ccol;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003249 if (*in == 0xA) {
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +00003250 ctxt->input->line++; ctxt->input->col = 1;
Daniel Veillard3ed155f2001-04-29 19:56:59 +00003251 in++;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003252 while (*in == 0xA) {
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +00003253 ctxt->input->line++; ctxt->input->col = 1;
Daniel Veillard3ed155f2001-04-29 19:56:59 +00003254 in++;
3255 }
3256 goto get_more;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003257 }
3258 if (*in == ']') {
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003259 if ((in[1] == ']') && (in[2] == '>')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003260 xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003261 ctxt->input->cur = in;
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003262 return;
3263 }
3264 in++;
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +00003265 ctxt->input->col++;
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003266 goto get_more;
3267 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00003268 nbchar = in - ctxt->input->cur;
Daniel Veillard80f32572001-03-07 19:45:40 +00003269 if (nbchar > 0) {
Daniel Veillard34099b42004-11-04 17:34:35 +00003270 if ((ctxt->sax != NULL) &&
3271 (ctxt->sax->ignorableWhitespace !=
Daniel Veillard40412cd2003-09-03 13:28:32 +00003272 ctxt->sax->characters) &&
William M. Brack76e95df2003-10-18 16:20:14 +00003273 (IS_BLANK_CH(*ctxt->input->cur))) {
Daniel Veillarda7374592001-05-10 14:17:55 +00003274 const xmlChar *tmp = ctxt->input->cur;
3275 ctxt->input->cur = in;
Daniel Veillard40412cd2003-09-03 13:28:32 +00003276
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003277 if (areBlanks(ctxt, tmp, nbchar, 0)) {
Daniel Veillard32acf0c2005-03-31 14:12:37 +00003278 if (ctxt->sax->ignorableWhitespace != NULL)
3279 ctxt->sax->ignorableWhitespace(ctxt->userData,
3280 tmp, nbchar);
Daniel Veillard40412cd2003-09-03 13:28:32 +00003281 } else if (ctxt->sax->characters != NULL)
3282 ctxt->sax->characters(ctxt->userData,
3283 tmp, nbchar);
Daniel Veillard3ed27bd2001-06-17 17:58:17 +00003284 line = ctxt->input->line;
3285 col = ctxt->input->col;
Daniel Veillard34099b42004-11-04 17:34:35 +00003286 } else if (ctxt->sax != NULL) {
Daniel Veillard80f32572001-03-07 19:45:40 +00003287 if (ctxt->sax->characters != NULL)
3288 ctxt->sax->characters(ctxt->userData,
3289 ctxt->input->cur, nbchar);
Daniel Veillard3ed27bd2001-06-17 17:58:17 +00003290 line = ctxt->input->line;
3291 col = ctxt->input->col;
Daniel Veillard80f32572001-03-07 19:45:40 +00003292 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00003293 }
3294 ctxt->input->cur = in;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003295 if (*in == 0xD) {
3296 in++;
3297 if (*in == 0xA) {
3298 ctxt->input->cur = in;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003299 in++;
Aleksey Sanin8fdc32a2005-01-05 15:37:55 +00003300 ctxt->input->line++; ctxt->input->col = 1;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003301 continue; /* while */
Daniel Veillard48b2f892001-02-25 16:11:03 +00003302 }
Daniel Veillard561b7f82002-03-20 21:55:57 +00003303 in--;
3304 }
3305 if (*in == '<') {
3306 return;
3307 }
3308 if (*in == '&') {
3309 return;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003310 }
3311 SHRINK;
3312 GROW;
3313 in = ctxt->input->cur;
William M. Brackc07329e2003-09-08 01:57:30 +00003314 } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09));
Daniel Veillard48b2f892001-02-25 16:11:03 +00003315 nbchar = 0;
3316 }
Daniel Veillard50582112001-03-26 22:52:16 +00003317 ctxt->input->line = line;
3318 ctxt->input->col = col;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003319 xmlParseCharDataComplex(ctxt, cdata);
3320}
3321
Daniel Veillard01c13b52002-12-10 15:19:08 +00003322/**
3323 * xmlParseCharDataComplex:
3324 * @ctxt: an XML parser context
3325 * @cdata: int indicating whether we are within a CDATA section
3326 *
3327 * parse a CharData section.this is the fallback function
3328 * of xmlParseCharData() when the parsing requires handling
3329 * of non-ASCII characters.
3330 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00003331void
3332xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata) {
Owen Taylor3473f882001-02-23 17:55:21 +00003333 xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5];
3334 int nbchar = 0;
3335 int cur, l;
3336 int count = 0;
3337
3338 SHRINK;
3339 GROW;
3340 cur = CUR_CHAR(l);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003341 while ((cur != '<') && /* checked */
3342 (cur != '&') &&
William M. Brack871611b2003-10-18 04:53:14 +00003343 (IS_CHAR(cur))) /* test also done in xmlCurrentChar() */ {
Owen Taylor3473f882001-02-23 17:55:21 +00003344 if ((cur == ']') && (NXT(1) == ']') &&
3345 (NXT(2) == '>')) {
3346 if (cdata) break;
3347 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003348 xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003349 }
3350 }
3351 COPY_BUF(l,buf,nbchar,cur);
3352 if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) {
Daniel Veillard092643b2003-09-25 14:29:29 +00003353 buf[nbchar] = 0;
3354
Owen Taylor3473f882001-02-23 17:55:21 +00003355 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003356 * OK the segment is to be consumed as chars.
Owen Taylor3473f882001-02-23 17:55:21 +00003357 */
3358 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003359 if (areBlanks(ctxt, buf, nbchar, 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003360 if (ctxt->sax->ignorableWhitespace != NULL)
3361 ctxt->sax->ignorableWhitespace(ctxt->userData,
3362 buf, nbchar);
3363 } else {
3364 if (ctxt->sax->characters != NULL)
3365 ctxt->sax->characters(ctxt->userData, buf, nbchar);
3366 }
3367 }
3368 nbchar = 0;
3369 }
3370 count++;
3371 if (count > 50) {
3372 GROW;
3373 count = 0;
3374 }
3375 NEXTL(l);
3376 cur = CUR_CHAR(l);
3377 }
3378 if (nbchar != 0) {
Daniel Veillard092643b2003-09-25 14:29:29 +00003379 buf[nbchar] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003380 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003381 * OK the segment is to be consumed as chars.
Owen Taylor3473f882001-02-23 17:55:21 +00003382 */
3383 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003384 if (areBlanks(ctxt, buf, nbchar, 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003385 if (ctxt->sax->ignorableWhitespace != NULL)
3386 ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar);
3387 } else {
3388 if (ctxt->sax->characters != NULL)
3389 ctxt->sax->characters(ctxt->userData, buf, nbchar);
3390 }
3391 }
3392 }
3393}
3394
3395/**
3396 * xmlParseExternalID:
3397 * @ctxt: an XML parser context
3398 * @publicID: a xmlChar** receiving PubidLiteral
3399 * @strict: indicate whether we should restrict parsing to only
3400 * production [75], see NOTE below
3401 *
3402 * Parse an External ID or a Public ID
3403 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003404 * NOTE: Productions [75] and [83] interact badly since [75] can generate
Owen Taylor3473f882001-02-23 17:55:21 +00003405 * 'PUBLIC' S PubidLiteral S SystemLiteral
3406 *
3407 * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
3408 * | 'PUBLIC' S PubidLiteral S SystemLiteral
3409 *
3410 * [83] PublicID ::= 'PUBLIC' S PubidLiteral
3411 *
3412 * Returns the function returns SystemLiteral and in the second
3413 * case publicID receives PubidLiteral, is strict is off
3414 * it is possible to return NULL and have publicID set.
3415 */
3416
3417xmlChar *
3418xmlParseExternalID(xmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) {
3419 xmlChar *URI = NULL;
3420
3421 SHRINK;
Daniel Veillard146c9122001-03-22 15:22:27 +00003422
3423 *publicID = NULL;
Daniel Veillarda07050d2003-10-19 14:46:32 +00003424 if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003425 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00003426 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003427 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3428 "Space required after 'SYSTEM'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003429 }
3430 SKIP_BLANKS;
3431 URI = xmlParseSystemLiteral(ctxt);
3432 if (URI == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003433 xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003434 }
Daniel Veillarda07050d2003-10-19 14:46:32 +00003435 } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003436 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00003437 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003438 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00003439 "Space required after 'PUBLIC'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003440 }
3441 SKIP_BLANKS;
3442 *publicID = xmlParsePubidLiteral(ctxt);
3443 if (*publicID == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003444 xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003445 }
3446 if (strict) {
3447 /*
3448 * We don't handle [83] so "S SystemLiteral" is required.
3449 */
William M. Brack76e95df2003-10-18 16:20:14 +00003450 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003451 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00003452 "Space required after the Public Identifier\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003453 }
3454 } else {
3455 /*
3456 * We handle [83] so we return immediately, if
3457 * "S SystemLiteral" is not detected. From a purely parsing
3458 * point of view that's a nice mess.
3459 */
3460 const xmlChar *ptr;
3461 GROW;
3462
3463 ptr = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00003464 if (!IS_BLANK_CH(*ptr)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003465
William M. Brack76e95df2003-10-18 16:20:14 +00003466 while (IS_BLANK_CH(*ptr)) ptr++; /* TODO: dangerous, fix ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003467 if ((*ptr != '\'') && (*ptr != '"')) return(NULL);
3468 }
3469 SKIP_BLANKS;
3470 URI = xmlParseSystemLiteral(ctxt);
3471 if (URI == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003472 xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003473 }
3474 }
3475 return(URI);
3476}
3477
3478/**
Daniel Veillard4c778d82005-01-23 17:37:44 +00003479 * xmlParseCommentComplex:
Owen Taylor3473f882001-02-23 17:55:21 +00003480 * @ctxt: an XML parser context
Daniel Veillard4c778d82005-01-23 17:37:44 +00003481 * @buf: the already parsed part of the buffer
3482 * @len: number of bytes filles in the buffer
3483 * @size: allocated size of the buffer
Owen Taylor3473f882001-02-23 17:55:21 +00003484 *
3485 * Skip an XML (SGML) comment <!-- .... -->
3486 * The spec says that "For compatibility, the string "--" (double-hyphen)
3487 * must not occur within comments. "
Daniel Veillard4c778d82005-01-23 17:37:44 +00003488 * This is the slow routine in case the accelerator for ascii didn't work
Owen Taylor3473f882001-02-23 17:55:21 +00003489 *
3490 * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
3491 */
Daniel Veillard4c778d82005-01-23 17:37:44 +00003492static void
3493xmlParseCommentComplex(xmlParserCtxtPtr ctxt, xmlChar *buf, int len, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +00003494 int q, ql;
3495 int r, rl;
3496 int cur, l;
Owen Taylor3473f882001-02-23 17:55:21 +00003497 xmlParserInputPtr input = ctxt->input;
3498 int count = 0;
3499
Owen Taylor3473f882001-02-23 17:55:21 +00003500 if (buf == NULL) {
Daniel Veillard4c778d82005-01-23 17:37:44 +00003501 len = 0;
3502 size = XML_PARSER_BUFFER_SIZE;
3503 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
3504 if (buf == NULL) {
3505 xmlErrMemory(ctxt, NULL);
3506 return;
3507 }
Owen Taylor3473f882001-02-23 17:55:21 +00003508 }
3509 q = CUR_CHAR(ql);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003510 if (q == 0)
3511 goto not_terminated;
Owen Taylor3473f882001-02-23 17:55:21 +00003512 NEXTL(ql);
3513 r = CUR_CHAR(rl);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003514 if (r == 0)
3515 goto not_terminated;
Owen Taylor3473f882001-02-23 17:55:21 +00003516 NEXTL(rl);
3517 cur = CUR_CHAR(l);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003518 if (cur == 0)
3519 goto not_terminated;
William M. Brack871611b2003-10-18 04:53:14 +00003520 while (IS_CHAR(cur) && /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003521 ((cur != '>') ||
3522 (r != '-') || (q != '-'))) {
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003523 if ((r == '-') && (q == '-')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003524 xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003525 }
3526 if (len + 5 >= size) {
William M. Bracka3215c72004-07-31 16:24:01 +00003527 xmlChar *new_buf;
Owen Taylor3473f882001-02-23 17:55:21 +00003528 size *= 2;
William M. Bracka3215c72004-07-31 16:24:01 +00003529 new_buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3530 if (new_buf == NULL) {
3531 xmlFree (buf);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003532 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003533 return;
3534 }
William M. Bracka3215c72004-07-31 16:24:01 +00003535 buf = new_buf;
Owen Taylor3473f882001-02-23 17:55:21 +00003536 }
3537 COPY_BUF(ql,buf,len,q);
3538 q = r;
3539 ql = rl;
3540 r = cur;
3541 rl = l;
3542
3543 count++;
3544 if (count > 50) {
3545 GROW;
3546 count = 0;
3547 }
3548 NEXTL(l);
3549 cur = CUR_CHAR(l);
3550 if (cur == 0) {
3551 SHRINK;
3552 GROW;
3553 cur = CUR_CHAR(l);
3554 }
3555 }
3556 buf[len] = 0;
William M. Brack871611b2003-10-18 04:53:14 +00003557 if (!IS_CHAR(cur)) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00003558 xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
Owen Taylor3473f882001-02-23 17:55:21 +00003559 "Comment not terminated \n<!--%.50s\n", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003560 xmlFree(buf);
3561 } else {
3562 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003563 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
3564 "Comment doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003565 }
3566 NEXT;
3567 if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
3568 (!ctxt->disableSAX))
3569 ctxt->sax->comment(ctxt->userData, buf);
3570 xmlFree(buf);
3571 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003572 return;
3573not_terminated:
3574 xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
3575 "Comment not terminated\n", NULL);
3576 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003577}
Daniel Veillard4c778d82005-01-23 17:37:44 +00003578/**
3579 * xmlParseComment:
3580 * @ctxt: an XML parser context
3581 *
3582 * Skip an XML (SGML) comment <!-- .... -->
3583 * The spec says that "For compatibility, the string "--" (double-hyphen)
3584 * must not occur within comments. "
3585 *
3586 * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
3587 */
3588void
3589xmlParseComment(xmlParserCtxtPtr ctxt) {
3590 xmlChar *buf = NULL;
3591 int size = XML_PARSER_BUFFER_SIZE;
Daniel Veillard316a5c32005-01-23 22:56:39 +00003592 int len = 0;
Daniel Veillard4c778d82005-01-23 17:37:44 +00003593 xmlParserInputState state;
3594 const xmlChar *in;
3595 int nbchar = 0, ccol;
3596
3597 /*
3598 * Check that there is a comment right here.
3599 */
3600 if ((RAW != '<') || (NXT(1) != '!') ||
3601 (NXT(2) != '-') || (NXT(3) != '-')) return;
3602
3603 state = ctxt->instate;
3604 ctxt->instate = XML_PARSER_COMMENT;
3605 SKIP(4);
3606 SHRINK;
3607 GROW;
3608
3609 /*
3610 * Accelerated common case where input don't need to be
3611 * modified before passing it to the handler.
3612 */
3613 in = ctxt->input->cur;
3614 do {
3615 if (*in == 0xA) {
3616 ctxt->input->line++; ctxt->input->col = 1;
3617 in++;
3618 while (*in == 0xA) {
3619 ctxt->input->line++; ctxt->input->col = 1;
3620 in++;
3621 }
3622 }
3623get_more:
3624 ccol = ctxt->input->col;
3625 while (((*in > '-') && (*in <= 0x7F)) ||
3626 ((*in >= 0x20) && (*in < '-')) ||
3627 (*in == 0x09)) {
3628 in++;
3629 ccol++;
3630 }
3631 ctxt->input->col = ccol;
3632 if (*in == 0xA) {
3633 ctxt->input->line++; ctxt->input->col = 1;
3634 in++;
3635 while (*in == 0xA) {
3636 ctxt->input->line++; ctxt->input->col = 1;
3637 in++;
3638 }
3639 goto get_more;
3640 }
3641 nbchar = in - ctxt->input->cur;
3642 /*
3643 * save current set of data
3644 */
3645 if (nbchar > 0) {
3646 if ((ctxt->sax != NULL) &&
3647 (ctxt->sax->comment != NULL)) {
3648 if (buf == NULL) {
3649 if ((*in == '-') && (in[1] == '-'))
3650 size = nbchar + 1;
3651 else
3652 size = XML_PARSER_BUFFER_SIZE + nbchar;
3653 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
3654 if (buf == NULL) {
3655 xmlErrMemory(ctxt, NULL);
3656 ctxt->instate = state;
3657 return;
3658 }
3659 len = 0;
3660 } else if (len + nbchar + 1 >= size) {
3661 xmlChar *new_buf;
3662 size += len + nbchar + XML_PARSER_BUFFER_SIZE;
3663 new_buf = (xmlChar *) xmlRealloc(buf,
3664 size * sizeof(xmlChar));
3665 if (new_buf == NULL) {
3666 xmlFree (buf);
3667 xmlErrMemory(ctxt, NULL);
3668 ctxt->instate = state;
3669 return;
3670 }
3671 buf = new_buf;
3672 }
3673 memcpy(&buf[len], ctxt->input->cur, nbchar);
3674 len += nbchar;
3675 buf[len] = 0;
3676 }
3677 }
3678 ctxt->input->cur = in;
3679 if (*in == 0xA)
3680
3681 if (*in == 0xD) {
3682 in++;
3683 if (*in == 0xA) {
3684 ctxt->input->cur = in;
3685 in++;
3686 ctxt->input->line++; ctxt->input->col = 1;
3687 continue; /* while */
3688 }
3689 in--;
3690 }
3691 SHRINK;
3692 GROW;
3693 in = ctxt->input->cur;
3694 if (*in == '-') {
3695 if (in[1] == '-') {
3696 if (in[2] == '>') {
3697 SKIP(3);
3698 if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
3699 (!ctxt->disableSAX)) {
3700 if (buf != NULL)
3701 ctxt->sax->comment(ctxt->userData, buf);
3702 else
3703 ctxt->sax->comment(ctxt->userData, BAD_CAST "");
3704 }
3705 if (buf != NULL)
3706 xmlFree(buf);
3707 ctxt->instate = state;
3708 return;
3709 }
3710 if (buf != NULL)
3711 xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
3712 "Comment not terminated \n<!--%.50s\n",
3713 buf);
3714 else
3715 xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
3716 "Comment not terminated \n", NULL);
3717 in++;
3718 ctxt->input->col++;
3719 }
3720 in++;
3721 ctxt->input->col++;
3722 goto get_more;
3723 }
3724 } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09));
3725 xmlParseCommentComplex(ctxt, buf, len, size);
3726 ctxt->instate = state;
3727 return;
3728}
3729
Owen Taylor3473f882001-02-23 17:55:21 +00003730
3731/**
3732 * xmlParsePITarget:
3733 * @ctxt: an XML parser context
3734 *
3735 * parse the name of a PI
3736 *
3737 * [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
3738 *
3739 * Returns the PITarget name or NULL
3740 */
3741
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003742const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00003743xmlParsePITarget(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003744 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003745
3746 name = xmlParseName(ctxt);
3747 if ((name != NULL) &&
3748 ((name[0] == 'x') || (name[0] == 'X')) &&
3749 ((name[1] == 'm') || (name[1] == 'M')) &&
3750 ((name[2] == 'l') || (name[2] == 'L'))) {
3751 int i;
3752 if ((name[0] == 'x') && (name[1] == 'm') &&
3753 (name[2] == 'l') && (name[3] == 0)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003754 xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
Owen Taylor3473f882001-02-23 17:55:21 +00003755 "XML declaration allowed only at the start of the document\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003756 return(name);
3757 } else if (name[3] == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003758 xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003759 return(name);
3760 }
3761 for (i = 0;;i++) {
3762 if (xmlW3CPIs[i] == NULL) break;
3763 if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i]))
3764 return(name);
3765 }
Daniel Veillard24eb9782003-10-04 21:08:09 +00003766 xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
3767 "xmlParsePITarget: invalid name prefix 'xml'\n",
3768 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003769 }
3770 return(name);
3771}
3772
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003773#ifdef LIBXML_CATALOG_ENABLED
3774/**
3775 * xmlParseCatalogPI:
3776 * @ctxt: an XML parser context
3777 * @catalog: the PI value string
3778 *
3779 * parse an XML Catalog Processing Instruction.
3780 *
3781 * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?>
3782 *
3783 * Occurs only if allowed by the user and if happening in the Misc
3784 * part of the document before any doctype informations
3785 * This will add the given catalog to the parsing context in order
3786 * to be used if there is a resolution need further down in the document
3787 */
3788
3789static void
3790xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) {
3791 xmlChar *URL = NULL;
3792 const xmlChar *tmp, *base;
3793 xmlChar marker;
3794
3795 tmp = catalog;
William M. Brack76e95df2003-10-18 16:20:14 +00003796 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003797 if (xmlStrncmp(tmp, BAD_CAST"catalog", 7))
3798 goto error;
3799 tmp += 7;
William M. Brack76e95df2003-10-18 16:20:14 +00003800 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003801 if (*tmp != '=') {
3802 return;
3803 }
3804 tmp++;
William M. Brack76e95df2003-10-18 16:20:14 +00003805 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003806 marker = *tmp;
3807 if ((marker != '\'') && (marker != '"'))
3808 goto error;
3809 tmp++;
3810 base = tmp;
3811 while ((*tmp != 0) && (*tmp != marker)) tmp++;
3812 if (*tmp == 0)
3813 goto error;
3814 URL = xmlStrndup(base, tmp - base);
3815 tmp++;
William M. Brack76e95df2003-10-18 16:20:14 +00003816 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003817 if (*tmp != 0)
3818 goto error;
3819
3820 if (URL != NULL) {
3821 ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
3822 xmlFree(URL);
3823 }
3824 return;
3825
3826error:
Daniel Veillard24eb9782003-10-04 21:08:09 +00003827 xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI,
3828 "Catalog PI syntax error: %s\n",
3829 catalog, NULL);
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003830 if (URL != NULL)
3831 xmlFree(URL);
3832}
3833#endif
3834
Owen Taylor3473f882001-02-23 17:55:21 +00003835/**
3836 * xmlParsePI:
3837 * @ctxt: an XML parser context
3838 *
3839 * parse an XML Processing Instruction.
3840 *
3841 * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
3842 *
3843 * The processing is transfered to SAX once parsed.
3844 */
3845
3846void
3847xmlParsePI(xmlParserCtxtPtr ctxt) {
3848 xmlChar *buf = NULL;
3849 int len = 0;
3850 int size = XML_PARSER_BUFFER_SIZE;
3851 int cur, l;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003852 const xmlChar *target;
Owen Taylor3473f882001-02-23 17:55:21 +00003853 xmlParserInputState state;
3854 int count = 0;
3855
3856 if ((RAW == '<') && (NXT(1) == '?')) {
3857 xmlParserInputPtr input = ctxt->input;
3858 state = ctxt->instate;
3859 ctxt->instate = XML_PARSER_PI;
3860 /*
3861 * this is a Processing Instruction.
3862 */
3863 SKIP(2);
3864 SHRINK;
3865
3866 /*
3867 * Parse the target name and check for special support like
3868 * namespace.
3869 */
3870 target = xmlParsePITarget(ctxt);
3871 if (target != NULL) {
3872 if ((RAW == '?') && (NXT(1) == '>')) {
3873 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003874 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
3875 "PI declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003876 }
3877 SKIP(2);
3878
3879 /*
3880 * SAX: PI detected.
3881 */
3882 if ((ctxt->sax) && (!ctxt->disableSAX) &&
3883 (ctxt->sax->processingInstruction != NULL))
3884 ctxt->sax->processingInstruction(ctxt->userData,
3885 target, NULL);
3886 ctxt->instate = state;
Owen Taylor3473f882001-02-23 17:55:21 +00003887 return;
3888 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003889 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003890 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003891 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003892 ctxt->instate = state;
3893 return;
3894 }
3895 cur = CUR;
3896 if (!IS_BLANK(cur)) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00003897 xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED,
3898 "ParsePI: PI %s space expected\n", target);
Owen Taylor3473f882001-02-23 17:55:21 +00003899 }
3900 SKIP_BLANKS;
3901 cur = CUR_CHAR(l);
William M. Brack871611b2003-10-18 04:53:14 +00003902 while (IS_CHAR(cur) && /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003903 ((cur != '?') || (NXT(1) != '>'))) {
3904 if (len + 5 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00003905 xmlChar *tmp;
3906
Owen Taylor3473f882001-02-23 17:55:21 +00003907 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00003908 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3909 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003910 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00003911 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003912 ctxt->instate = state;
3913 return;
3914 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00003915 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00003916 }
3917 count++;
3918 if (count > 50) {
3919 GROW;
3920 count = 0;
3921 }
3922 COPY_BUF(l,buf,len,cur);
3923 NEXTL(l);
3924 cur = CUR_CHAR(l);
3925 if (cur == 0) {
3926 SHRINK;
3927 GROW;
3928 cur = CUR_CHAR(l);
3929 }
3930 }
3931 buf[len] = 0;
3932 if (cur != '?') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00003933 xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED,
3934 "ParsePI: PI %s never end ...\n", target);
Owen Taylor3473f882001-02-23 17:55:21 +00003935 } else {
3936 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003937 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3938 "PI declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003939 }
3940 SKIP(2);
3941
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003942#ifdef LIBXML_CATALOG_ENABLED
3943 if (((state == XML_PARSER_MISC) ||
3944 (state == XML_PARSER_START)) &&
3945 (xmlStrEqual(target, XML_CATALOG_PI))) {
3946 xmlCatalogAllow allow = xmlCatalogGetDefaults();
3947 if ((allow == XML_CATA_ALLOW_DOCUMENT) ||
3948 (allow == XML_CATA_ALLOW_ALL))
3949 xmlParseCatalogPI(ctxt, buf);
3950 }
3951#endif
3952
3953
Owen Taylor3473f882001-02-23 17:55:21 +00003954 /*
3955 * SAX: PI detected.
3956 */
3957 if ((ctxt->sax) && (!ctxt->disableSAX) &&
3958 (ctxt->sax->processingInstruction != NULL))
3959 ctxt->sax->processingInstruction(ctxt->userData,
3960 target, buf);
3961 }
3962 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003963 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003964 xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003965 }
3966 ctxt->instate = state;
3967 }
3968}
3969
3970/**
3971 * xmlParseNotationDecl:
3972 * @ctxt: an XML parser context
3973 *
3974 * parse a notation declaration
3975 *
3976 * [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>'
3977 *
3978 * Hence there is actually 3 choices:
3979 * 'PUBLIC' S PubidLiteral
3980 * 'PUBLIC' S PubidLiteral S SystemLiteral
3981 * and 'SYSTEM' S SystemLiteral
3982 *
3983 * See the NOTE on xmlParseExternalID().
3984 */
3985
3986void
3987xmlParseNotationDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003988 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003989 xmlChar *Pubid;
3990 xmlChar *Systemid;
3991
Daniel Veillarda07050d2003-10-19 14:46:32 +00003992 if (CMP10(CUR_PTR, '<', '!', 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003993 xmlParserInputPtr input = ctxt->input;
3994 SHRINK;
3995 SKIP(10);
William M. Brack76e95df2003-10-18 16:20:14 +00003996 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003997 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3998 "Space required after '<!NOTATION'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003999 return;
4000 }
4001 SKIP_BLANKS;
4002
Daniel Veillard76d66f42001-05-16 21:05:17 +00004003 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004004 if (name == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004005 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004006 return;
4007 }
William M. Brack76e95df2003-10-18 16:20:14 +00004008 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004009 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004010 "Space required after the NOTATION name'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004011 return;
4012 }
4013 SKIP_BLANKS;
4014
4015 /*
4016 * Parse the IDs.
4017 */
4018 Systemid = xmlParseExternalID(ctxt, &Pubid, 0);
4019 SKIP_BLANKS;
4020
4021 if (RAW == '>') {
4022 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004023 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4024 "Notation declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004025 }
4026 NEXT;
4027 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4028 (ctxt->sax->notationDecl != NULL))
4029 ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid);
4030 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004031 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004032 }
Owen Taylor3473f882001-02-23 17:55:21 +00004033 if (Systemid != NULL) xmlFree(Systemid);
4034 if (Pubid != NULL) xmlFree(Pubid);
4035 }
4036}
4037
4038/**
4039 * xmlParseEntityDecl:
4040 * @ctxt: an XML parser context
4041 *
4042 * parse <!ENTITY declarations
4043 *
4044 * [70] EntityDecl ::= GEDecl | PEDecl
4045 *
4046 * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>'
4047 *
4048 * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>'
4049 *
4050 * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?)
4051 *
4052 * [74] PEDef ::= EntityValue | ExternalID
4053 *
4054 * [76] NDataDecl ::= S 'NDATA' S Name
4055 *
4056 * [ VC: Notation Declared ]
4057 * The Name must match the declared name of a notation.
4058 */
4059
4060void
4061xmlParseEntityDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004062 const xmlChar *name = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00004063 xmlChar *value = NULL;
4064 xmlChar *URI = NULL, *literal = NULL;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004065 const xmlChar *ndata = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00004066 int isParameter = 0;
4067 xmlChar *orig = NULL;
Daniel Veillardf5582f12002-06-11 10:08:16 +00004068 int skipped;
Owen Taylor3473f882001-02-23 17:55:21 +00004069
Daniel Veillard4c778d82005-01-23 17:37:44 +00004070 /* GROW; done in the caller */
Daniel Veillarda07050d2003-10-19 14:46:32 +00004071 if (CMP8(CUR_PTR, '<', '!', 'E', 'N', 'T', 'I', 'T', 'Y')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004072 xmlParserInputPtr input = ctxt->input;
Owen Taylor3473f882001-02-23 17:55:21 +00004073 SHRINK;
4074 SKIP(8);
Daniel Veillardf5582f12002-06-11 10:08:16 +00004075 skipped = SKIP_BLANKS;
4076 if (skipped == 0) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004077 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4078 "Space required after '<!ENTITY'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004079 }
Owen Taylor3473f882001-02-23 17:55:21 +00004080
4081 if (RAW == '%') {
4082 NEXT;
Daniel Veillardf5582f12002-06-11 10:08:16 +00004083 skipped = SKIP_BLANKS;
4084 if (skipped == 0) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004085 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4086 "Space required after '%'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004087 }
Owen Taylor3473f882001-02-23 17:55:21 +00004088 isParameter = 1;
4089 }
4090
Daniel Veillard76d66f42001-05-16 21:05:17 +00004091 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004092 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004093 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4094 "xmlParseEntityDecl: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004095 return;
4096 }
Daniel Veillardf5582f12002-06-11 10:08:16 +00004097 skipped = SKIP_BLANKS;
4098 if (skipped == 0) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004099 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4100 "Space required after the entity name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004101 }
Owen Taylor3473f882001-02-23 17:55:21 +00004102
Daniel Veillardf5582f12002-06-11 10:08:16 +00004103 ctxt->instate = XML_PARSER_ENTITY_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00004104 /*
4105 * handle the various case of definitions...
4106 */
4107 if (isParameter) {
4108 if ((RAW == '"') || (RAW == '\'')) {
4109 value = xmlParseEntityValue(ctxt, &orig);
4110 if (value) {
4111 if ((ctxt->sax != NULL) &&
4112 (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
4113 ctxt->sax->entityDecl(ctxt->userData, name,
4114 XML_INTERNAL_PARAMETER_ENTITY,
4115 NULL, NULL, value);
4116 }
4117 } else {
4118 URI = xmlParseExternalID(ctxt, &literal, 1);
4119 if ((URI == NULL) && (literal == NULL)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004120 xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004121 }
4122 if (URI) {
4123 xmlURIPtr uri;
4124
4125 uri = xmlParseURI((const char *) URI);
4126 if (uri == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004127 xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI,
4128 "Invalid URI: %s\n", URI);
Daniel Veillard4a7ae502002-02-18 19:18:17 +00004129 /*
4130 * This really ought to be a well formedness error
4131 * but the XML Core WG decided otherwise c.f. issue
4132 * E26 of the XML erratas.
4133 */
Owen Taylor3473f882001-02-23 17:55:21 +00004134 } else {
4135 if (uri->fragment != NULL) {
Daniel Veillard4a7ae502002-02-18 19:18:17 +00004136 /*
4137 * Okay this is foolish to block those but not
4138 * invalid URIs.
4139 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004140 xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004141 } else {
4142 if ((ctxt->sax != NULL) &&
4143 (!ctxt->disableSAX) &&
4144 (ctxt->sax->entityDecl != NULL))
4145 ctxt->sax->entityDecl(ctxt->userData, name,
4146 XML_EXTERNAL_PARAMETER_ENTITY,
4147 literal, URI, NULL);
4148 }
4149 xmlFreeURI(uri);
4150 }
4151 }
4152 }
4153 } else {
4154 if ((RAW == '"') || (RAW == '\'')) {
4155 value = xmlParseEntityValue(ctxt, &orig);
4156 if ((ctxt->sax != NULL) &&
4157 (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
4158 ctxt->sax->entityDecl(ctxt->userData, name,
4159 XML_INTERNAL_GENERAL_ENTITY,
4160 NULL, NULL, value);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004161 /*
4162 * For expat compatibility in SAX mode.
4163 */
4164 if ((ctxt->myDoc == NULL) ||
4165 (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) {
4166 if (ctxt->myDoc == NULL) {
4167 ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
4168 }
4169 if (ctxt->myDoc->intSubset == NULL)
4170 ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
4171 BAD_CAST "fake", NULL, NULL);
4172
Daniel Veillard1af9a412003-08-20 22:54:39 +00004173 xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY,
4174 NULL, NULL, value);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004175 }
Owen Taylor3473f882001-02-23 17:55:21 +00004176 } else {
4177 URI = xmlParseExternalID(ctxt, &literal, 1);
4178 if ((URI == NULL) && (literal == NULL)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004179 xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004180 }
4181 if (URI) {
4182 xmlURIPtr uri;
4183
4184 uri = xmlParseURI((const char *)URI);
4185 if (uri == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004186 xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI,
4187 "Invalid URI: %s\n", URI);
Daniel Veillard4a7ae502002-02-18 19:18:17 +00004188 /*
4189 * This really ought to be a well formedness error
4190 * but the XML Core WG decided otherwise c.f. issue
4191 * E26 of the XML erratas.
4192 */
Owen Taylor3473f882001-02-23 17:55:21 +00004193 } else {
4194 if (uri->fragment != NULL) {
Daniel Veillard4a7ae502002-02-18 19:18:17 +00004195 /*
4196 * Okay this is foolish to block those but not
4197 * invalid URIs.
4198 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004199 xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004200 }
4201 xmlFreeURI(uri);
4202 }
4203 }
William M. Brack76e95df2003-10-18 16:20:14 +00004204 if ((RAW != '>') && (!IS_BLANK_CH(CUR))) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004205 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4206 "Space required before 'NDATA'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004207 }
4208 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004209 if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004210 SKIP(5);
William M. Brack76e95df2003-10-18 16:20:14 +00004211 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004212 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4213 "Space required after 'NDATA'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004214 }
4215 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004216 ndata = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004217 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4218 (ctxt->sax->unparsedEntityDecl != NULL))
4219 ctxt->sax->unparsedEntityDecl(ctxt->userData, name,
4220 literal, URI, ndata);
4221 } else {
4222 if ((ctxt->sax != NULL) &&
4223 (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
4224 ctxt->sax->entityDecl(ctxt->userData, name,
4225 XML_EXTERNAL_GENERAL_PARSED_ENTITY,
4226 literal, URI, NULL);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004227 /*
4228 * For expat compatibility in SAX mode.
4229 * assuming the entity repalcement was asked for
4230 */
4231 if ((ctxt->replaceEntities != 0) &&
4232 ((ctxt->myDoc == NULL) ||
4233 (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) {
4234 if (ctxt->myDoc == NULL) {
4235 ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
4236 }
4237
4238 if (ctxt->myDoc->intSubset == NULL)
4239 ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
4240 BAD_CAST "fake", NULL, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00004241 xmlSAX2EntityDecl(ctxt, name,
4242 XML_EXTERNAL_GENERAL_PARSED_ENTITY,
4243 literal, URI, NULL);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004244 }
Owen Taylor3473f882001-02-23 17:55:21 +00004245 }
4246 }
4247 }
4248 SKIP_BLANKS;
4249 if (RAW != '>') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00004250 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED,
Owen Taylor3473f882001-02-23 17:55:21 +00004251 "xmlParseEntityDecl: entity %s not terminated\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00004252 } else {
4253 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004254 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
4255 "Entity declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004256 }
4257 NEXT;
4258 }
4259 if (orig != NULL) {
4260 /*
4261 * Ugly mechanism to save the raw entity value.
4262 */
4263 xmlEntityPtr cur = NULL;
4264
4265 if (isParameter) {
4266 if ((ctxt->sax != NULL) &&
4267 (ctxt->sax->getParameterEntity != NULL))
4268 cur = ctxt->sax->getParameterEntity(ctxt->userData, name);
4269 } else {
4270 if ((ctxt->sax != NULL) &&
4271 (ctxt->sax->getEntity != NULL))
4272 cur = ctxt->sax->getEntity(ctxt->userData, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004273 if ((cur == NULL) && (ctxt->userData==ctxt)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00004274 cur = xmlSAX2GetEntity(ctxt, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004275 }
Owen Taylor3473f882001-02-23 17:55:21 +00004276 }
4277 if (cur != NULL) {
4278 if (cur->orig != NULL)
4279 xmlFree(orig);
4280 else
4281 cur->orig = orig;
4282 } else
4283 xmlFree(orig);
4284 }
Owen Taylor3473f882001-02-23 17:55:21 +00004285 if (value != NULL) xmlFree(value);
4286 if (URI != NULL) xmlFree(URI);
4287 if (literal != NULL) xmlFree(literal);
Owen Taylor3473f882001-02-23 17:55:21 +00004288 }
4289}
4290
4291/**
4292 * xmlParseDefaultDecl:
4293 * @ctxt: an XML parser context
4294 * @value: Receive a possible fixed default value for the attribute
4295 *
4296 * Parse an attribute default declaration
4297 *
4298 * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue)
4299 *
4300 * [ VC: Required Attribute ]
4301 * if the default declaration is the keyword #REQUIRED, then the
4302 * attribute must be specified for all elements of the type in the
4303 * attribute-list declaration.
4304 *
4305 * [ VC: Attribute Default Legal ]
4306 * The declared default value must meet the lexical constraints of
4307 * the declared attribute type c.f. xmlValidateAttributeDecl()
4308 *
4309 * [ VC: Fixed Attribute Default ]
4310 * if an attribute has a default value declared with the #FIXED
4311 * keyword, instances of that attribute must match the default value.
4312 *
4313 * [ WFC: No < in Attribute Values ]
4314 * handled in xmlParseAttValue()
4315 *
4316 * returns: XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED
4317 * or XML_ATTRIBUTE_FIXED.
4318 */
4319
4320int
4321xmlParseDefaultDecl(xmlParserCtxtPtr ctxt, xmlChar **value) {
4322 int val;
4323 xmlChar *ret;
4324
4325 *value = NULL;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004326 if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004327 SKIP(9);
4328 return(XML_ATTRIBUTE_REQUIRED);
4329 }
Daniel Veillarda07050d2003-10-19 14:46:32 +00004330 if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004331 SKIP(8);
4332 return(XML_ATTRIBUTE_IMPLIED);
4333 }
4334 val = XML_ATTRIBUTE_NONE;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004335 if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004336 SKIP(6);
4337 val = XML_ATTRIBUTE_FIXED;
William M. Brack76e95df2003-10-18 16:20:14 +00004338 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004339 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4340 "Space required after '#FIXED'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004341 }
4342 SKIP_BLANKS;
4343 }
4344 ret = xmlParseAttValue(ctxt);
4345 ctxt->instate = XML_PARSER_DTD;
4346 if (ret == NULL) {
William M. Brack7b9154b2003-09-27 19:23:50 +00004347 xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004348 "Attribute default value declaration error\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004349 } else
4350 *value = ret;
4351 return(val);
4352}
4353
4354/**
4355 * xmlParseNotationType:
4356 * @ctxt: an XML parser context
4357 *
4358 * parse an Notation attribute type.
4359 *
4360 * Note: the leading 'NOTATION' S part has already being parsed...
4361 *
4362 * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
4363 *
4364 * [ VC: Notation Attributes ]
4365 * Values of this type must match one of the notation names included
4366 * in the declaration; all notation names in the declaration must be declared.
4367 *
4368 * Returns: the notation attribute tree built while parsing
4369 */
4370
4371xmlEnumerationPtr
4372xmlParseNotationType(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004373 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00004374 xmlEnumerationPtr ret = NULL, last = NULL, cur;
4375
4376 if (RAW != '(') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004377 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004378 return(NULL);
4379 }
4380 SHRINK;
4381 do {
4382 NEXT;
4383 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004384 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004385 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004386 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4387 "Name expected in NOTATION declaration\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004388 return(ret);
4389 }
4390 cur = xmlCreateEnumeration(name);
Owen Taylor3473f882001-02-23 17:55:21 +00004391 if (cur == NULL) return(ret);
4392 if (last == NULL) ret = last = cur;
4393 else {
4394 last->next = cur;
4395 last = cur;
4396 }
4397 SKIP_BLANKS;
4398 } while (RAW == '|');
4399 if (RAW != ')') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004400 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004401 if ((last != NULL) && (last != ret))
4402 xmlFreeEnumeration(last);
4403 return(ret);
4404 }
4405 NEXT;
4406 return(ret);
4407}
4408
4409/**
4410 * xmlParseEnumerationType:
4411 * @ctxt: an XML parser context
4412 *
4413 * parse an Enumeration attribute type.
4414 *
4415 * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')'
4416 *
4417 * [ VC: Enumeration ]
4418 * Values of this type must match one of the Nmtoken tokens in
4419 * the declaration
4420 *
4421 * Returns: the enumeration attribute tree built while parsing
4422 */
4423
4424xmlEnumerationPtr
4425xmlParseEnumerationType(xmlParserCtxtPtr ctxt) {
4426 xmlChar *name;
4427 xmlEnumerationPtr ret = NULL, last = NULL, cur;
4428
4429 if (RAW != '(') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004430 xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004431 return(NULL);
4432 }
4433 SHRINK;
4434 do {
4435 NEXT;
4436 SKIP_BLANKS;
4437 name = xmlParseNmtoken(ctxt);
4438 if (name == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004439 xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004440 return(ret);
4441 }
4442 cur = xmlCreateEnumeration(name);
4443 xmlFree(name);
4444 if (cur == NULL) return(ret);
4445 if (last == NULL) ret = last = cur;
4446 else {
4447 last->next = cur;
4448 last = cur;
4449 }
4450 SKIP_BLANKS;
4451 } while (RAW == '|');
4452 if (RAW != ')') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004453 xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004454 return(ret);
4455 }
4456 NEXT;
4457 return(ret);
4458}
4459
4460/**
4461 * xmlParseEnumeratedType:
4462 * @ctxt: an XML parser context
4463 * @tree: the enumeration tree built while parsing
4464 *
4465 * parse an Enumerated attribute type.
4466 *
4467 * [57] EnumeratedType ::= NotationType | Enumeration
4468 *
4469 * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
4470 *
4471 *
4472 * Returns: XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION
4473 */
4474
4475int
4476xmlParseEnumeratedType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) {
Daniel Veillarda07050d2003-10-19 14:46:32 +00004477 if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004478 SKIP(8);
William M. Brack76e95df2003-10-18 16:20:14 +00004479 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004480 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4481 "Space required after 'NOTATION'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004482 return(0);
4483 }
4484 SKIP_BLANKS;
4485 *tree = xmlParseNotationType(ctxt);
4486 if (*tree == NULL) return(0);
4487 return(XML_ATTRIBUTE_NOTATION);
4488 }
4489 *tree = xmlParseEnumerationType(ctxt);
4490 if (*tree == NULL) return(0);
4491 return(XML_ATTRIBUTE_ENUMERATION);
4492}
4493
4494/**
4495 * xmlParseAttributeType:
4496 * @ctxt: an XML parser context
4497 * @tree: the enumeration tree built while parsing
4498 *
4499 * parse the Attribute list def for an element
4500 *
4501 * [54] AttType ::= StringType | TokenizedType | EnumeratedType
4502 *
4503 * [55] StringType ::= 'CDATA'
4504 *
4505 * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' |
4506 * 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS'
4507 *
4508 * Validity constraints for attribute values syntax are checked in
4509 * xmlValidateAttributeValue()
4510 *
4511 * [ VC: ID ]
4512 * Values of type ID must match the Name production. A name must not
4513 * appear more than once in an XML document as a value of this type;
4514 * i.e., ID values must uniquely identify the elements which bear them.
4515 *
4516 * [ VC: One ID per Element Type ]
4517 * No element type may have more than one ID attribute specified.
4518 *
4519 * [ VC: ID Attribute Default ]
4520 * An ID attribute must have a declared default of #IMPLIED or #REQUIRED.
4521 *
4522 * [ VC: IDREF ]
4523 * Values of type IDREF must match the Name production, and values
4524 * of type IDREFS must match Names; each IDREF Name must match the value
4525 * of an ID attribute on some element in the XML document; i.e. IDREF
4526 * values must match the value of some ID attribute.
4527 *
4528 * [ VC: Entity Name ]
4529 * Values of type ENTITY must match the Name production, values
4530 * of type ENTITIES must match Names; each Entity Name must match the
4531 * name of an unparsed entity declared in the DTD.
4532 *
4533 * [ VC: Name Token ]
4534 * Values of type NMTOKEN must match the Nmtoken production; values
4535 * of type NMTOKENS must match Nmtokens.
4536 *
4537 * Returns the attribute type
4538 */
4539int
4540xmlParseAttributeType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) {
4541 SHRINK;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004542 if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004543 SKIP(5);
4544 return(XML_ATTRIBUTE_CDATA);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004545 } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004546 SKIP(6);
4547 return(XML_ATTRIBUTE_IDREFS);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004548 } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004549 SKIP(5);
4550 return(XML_ATTRIBUTE_IDREF);
4551 } else if ((RAW == 'I') && (NXT(1) == 'D')) {
4552 SKIP(2);
4553 return(XML_ATTRIBUTE_ID);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004554 } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004555 SKIP(6);
4556 return(XML_ATTRIBUTE_ENTITY);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004557 } else if (CMP8(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'I', 'E', 'S')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004558 SKIP(8);
4559 return(XML_ATTRIBUTE_ENTITIES);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004560 } else if (CMP8(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N', 'S')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004561 SKIP(8);
4562 return(XML_ATTRIBUTE_NMTOKENS);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004563 } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004564 SKIP(7);
4565 return(XML_ATTRIBUTE_NMTOKEN);
4566 }
4567 return(xmlParseEnumeratedType(ctxt, tree));
4568}
4569
4570/**
4571 * xmlParseAttributeListDecl:
4572 * @ctxt: an XML parser context
4573 *
4574 * : parse the Attribute list def for an element
4575 *
4576 * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>'
4577 *
4578 * [53] AttDef ::= S Name S AttType S DefaultDecl
4579 *
4580 */
4581void
4582xmlParseAttributeListDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004583 const xmlChar *elemName;
4584 const xmlChar *attrName;
Owen Taylor3473f882001-02-23 17:55:21 +00004585 xmlEnumerationPtr tree;
4586
Daniel Veillarda07050d2003-10-19 14:46:32 +00004587 if (CMP9(CUR_PTR, '<', '!', 'A', 'T', 'T', 'L', 'I', 'S', 'T')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004588 xmlParserInputPtr input = ctxt->input;
4589
4590 SKIP(9);
William M. Brack76e95df2003-10-18 16:20:14 +00004591 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004592 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004593 "Space required after '<!ATTLIST'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004594 }
4595 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004596 elemName = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004597 if (elemName == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004598 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4599 "ATTLIST: no name for Element\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004600 return;
4601 }
4602 SKIP_BLANKS;
4603 GROW;
4604 while (RAW != '>') {
4605 const xmlChar *check = CUR_PTR;
4606 int type;
4607 int def;
4608 xmlChar *defaultValue = NULL;
4609
4610 GROW;
4611 tree = NULL;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004612 attrName = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004613 if (attrName == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004614 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4615 "ATTLIST: no name for Attribute\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004616 break;
4617 }
4618 GROW;
William M. Brack76e95df2003-10-18 16:20:14 +00004619 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004620 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004621 "Space required after the attribute name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004622 if (defaultValue != NULL)
4623 xmlFree(defaultValue);
4624 break;
4625 }
4626 SKIP_BLANKS;
4627
4628 type = xmlParseAttributeType(ctxt, &tree);
4629 if (type <= 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00004630 if (defaultValue != NULL)
4631 xmlFree(defaultValue);
4632 break;
4633 }
4634
4635 GROW;
William M. Brack76e95df2003-10-18 16:20:14 +00004636 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004637 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4638 "Space required after the attribute type\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004639 if (defaultValue != NULL)
4640 xmlFree(defaultValue);
4641 if (tree != NULL)
4642 xmlFreeEnumeration(tree);
4643 break;
4644 }
4645 SKIP_BLANKS;
4646
4647 def = xmlParseDefaultDecl(ctxt, &defaultValue);
4648 if (def <= 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00004649 if (defaultValue != NULL)
4650 xmlFree(defaultValue);
4651 if (tree != NULL)
4652 xmlFreeEnumeration(tree);
4653 break;
4654 }
4655
4656 GROW;
4657 if (RAW != '>') {
William M. Brack76e95df2003-10-18 16:20:14 +00004658 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004659 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004660 "Space required after the attribute default value\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004661 if (defaultValue != NULL)
4662 xmlFree(defaultValue);
4663 if (tree != NULL)
4664 xmlFreeEnumeration(tree);
4665 break;
4666 }
4667 SKIP_BLANKS;
4668 }
4669 if (check == CUR_PTR) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004670 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
4671 "in xmlParseAttributeListDecl\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004672 if (defaultValue != NULL)
4673 xmlFree(defaultValue);
4674 if (tree != NULL)
4675 xmlFreeEnumeration(tree);
4676 break;
4677 }
4678 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4679 (ctxt->sax->attributeDecl != NULL))
4680 ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName,
4681 type, def, defaultValue, tree);
Daniel Veillarde57ec792003-09-10 10:50:59 +00004682 else if (tree != NULL)
4683 xmlFreeEnumeration(tree);
4684
4685 if ((ctxt->sax2) && (defaultValue != NULL) &&
4686 (def != XML_ATTRIBUTE_IMPLIED) &&
4687 (def != XML_ATTRIBUTE_REQUIRED)) {
4688 xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue);
4689 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00004690 if ((ctxt->sax2) && (type != XML_ATTRIBUTE_CDATA)) {
4691 xmlAddSpecialAttr(ctxt, elemName, attrName, type);
4692 }
Owen Taylor3473f882001-02-23 17:55:21 +00004693 if (defaultValue != NULL)
4694 xmlFree(defaultValue);
4695 GROW;
4696 }
4697 if (RAW == '>') {
4698 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004699 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
4700 "Attribute list declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004701 }
4702 NEXT;
4703 }
Owen Taylor3473f882001-02-23 17:55:21 +00004704 }
4705}
4706
4707/**
4708 * xmlParseElementMixedContentDecl:
4709 * @ctxt: an XML parser context
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004710 * @inputchk: the input used for the current entity, needed for boundary checks
Owen Taylor3473f882001-02-23 17:55:21 +00004711 *
4712 * parse the declaration for a Mixed Element content
4713 * The leading '(' and spaces have been skipped in xmlParseElementContentDecl
4714 *
4715 * [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' |
4716 * '(' S? '#PCDATA' S? ')'
4717 *
4718 * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49])
4719 *
4720 * [ VC: No Duplicate Types ]
4721 * The same name must not appear more than once in a single
4722 * mixed-content declaration.
4723 *
4724 * returns: the list of the xmlElementContentPtr describing the element choices
4725 */
4726xmlElementContentPtr
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004727xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) {
Owen Taylor3473f882001-02-23 17:55:21 +00004728 xmlElementContentPtr ret = NULL, cur = NULL, n;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004729 const xmlChar *elem = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00004730
4731 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004732 if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004733 SKIP(7);
4734 SKIP_BLANKS;
4735 SHRINK;
4736 if (RAW == ')') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004737 if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00004738 xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
4739"Element content declaration doesn't start and stop in the same entity\n",
4740 NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004741 }
Owen Taylor3473f882001-02-23 17:55:21 +00004742 NEXT;
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004743 ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA);
Owen Taylor3473f882001-02-23 17:55:21 +00004744 if (RAW == '*') {
4745 ret->ocur = XML_ELEMENT_CONTENT_MULT;
4746 NEXT;
4747 }
4748 return(ret);
4749 }
4750 if ((RAW == '(') || (RAW == '|')) {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004751 ret = cur = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_PCDATA);
Owen Taylor3473f882001-02-23 17:55:21 +00004752 if (ret == NULL) return(NULL);
4753 }
4754 while (RAW == '|') {
4755 NEXT;
4756 if (elem == NULL) {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004757 ret = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR);
Owen Taylor3473f882001-02-23 17:55:21 +00004758 if (ret == NULL) return(NULL);
4759 ret->c1 = cur;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004760 if (cur != NULL)
4761 cur->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +00004762 cur = ret;
4763 } else {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004764 n = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR);
Owen Taylor3473f882001-02-23 17:55:21 +00004765 if (n == NULL) return(NULL);
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004766 n->c1 = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004767 if (n->c1 != NULL)
4768 n->c1->parent = n;
Owen Taylor3473f882001-02-23 17:55:21 +00004769 cur->c2 = n;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004770 if (n != NULL)
4771 n->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004772 cur = n;
Owen Taylor3473f882001-02-23 17:55:21 +00004773 }
4774 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004775 elem = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004776 if (elem == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004777 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004778 "xmlParseElementMixedContentDecl : Name expected\n");
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004779 xmlFreeDocElementContent(ctxt->myDoc, cur);
Owen Taylor3473f882001-02-23 17:55:21 +00004780 return(NULL);
4781 }
4782 SKIP_BLANKS;
4783 GROW;
4784 }
4785 if ((RAW == ')') && (NXT(1) == '*')) {
4786 if (elem != NULL) {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004787 cur->c2 = xmlNewDocElementContent(ctxt->myDoc, elem,
Owen Taylor3473f882001-02-23 17:55:21 +00004788 XML_ELEMENT_CONTENT_ELEMENT);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004789 if (cur->c2 != NULL)
4790 cur->c2->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004791 }
4792 ret->ocur = XML_ELEMENT_CONTENT_MULT;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004793 if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00004794 xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
4795"Element content declaration doesn't start and stop in the same entity\n",
4796 NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004797 }
Owen Taylor3473f882001-02-23 17:55:21 +00004798 SKIP(2);
4799 } else {
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004800 xmlFreeDocElementContent(ctxt->myDoc, ret);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004801 xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004802 return(NULL);
4803 }
4804
4805 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004806 xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004807 }
4808 return(ret);
4809}
4810
4811/**
4812 * xmlParseElementChildrenContentDecl:
4813 * @ctxt: an XML parser context
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004814 * @inputchk: the input used for the current entity, needed for boundary checks
Owen Taylor3473f882001-02-23 17:55:21 +00004815 *
4816 * parse the declaration for a Mixed Element content
4817 * The leading '(' and spaces have been skipped in xmlParseElementContentDecl
4818 *
4819 *
4820 * [47] children ::= (choice | seq) ('?' | '*' | '+')?
4821 *
4822 * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')?
4823 *
4824 * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')'
4825 *
4826 * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')'
4827 *
4828 * [ VC: Proper Group/PE Nesting ] applies to [49] and [50]
4829 * TODO Parameter-entity replacement text must be properly nested
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004830 * with parenthesized groups. That is to say, if either of the
Owen Taylor3473f882001-02-23 17:55:21 +00004831 * opening or closing parentheses in a choice, seq, or Mixed
4832 * construct is contained in the replacement text for a parameter
4833 * entity, both must be contained in the same replacement text. For
4834 * interoperability, if a parameter-entity reference appears in a
4835 * choice, seq, or Mixed construct, its replacement text should not
4836 * be empty, and neither the first nor last non-blank character of
4837 * the replacement text should be a connector (| or ,).
4838 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00004839 * Returns the tree of xmlElementContentPtr describing the element
Owen Taylor3473f882001-02-23 17:55:21 +00004840 * hierarchy.
4841 */
4842xmlElementContentPtr
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004843xmlParseElementChildrenContentDecl (xmlParserCtxtPtr ctxt, int inputchk) {
Owen Taylor3473f882001-02-23 17:55:21 +00004844 xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004845 const xmlChar *elem;
Owen Taylor3473f882001-02-23 17:55:21 +00004846 xmlChar type = 0;
4847
4848 SKIP_BLANKS;
4849 GROW;
4850 if (RAW == '(') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004851 int inputid = ctxt->input->id;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004852
Owen Taylor3473f882001-02-23 17:55:21 +00004853 /* Recurse on first child */
4854 NEXT;
4855 SKIP_BLANKS;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004856 cur = ret = xmlParseElementChildrenContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004857 SKIP_BLANKS;
4858 GROW;
4859 } else {
Daniel Veillard76d66f42001-05-16 21:05:17 +00004860 elem = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004861 if (elem == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004862 xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004863 return(NULL);
4864 }
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004865 cur = ret = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004866 if (cur == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004867 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004868 return(NULL);
4869 }
Owen Taylor3473f882001-02-23 17:55:21 +00004870 GROW;
4871 if (RAW == '?') {
4872 cur->ocur = XML_ELEMENT_CONTENT_OPT;
4873 NEXT;
4874 } else if (RAW == '*') {
4875 cur->ocur = XML_ELEMENT_CONTENT_MULT;
4876 NEXT;
4877 } else if (RAW == '+') {
4878 cur->ocur = XML_ELEMENT_CONTENT_PLUS;
4879 NEXT;
4880 } else {
4881 cur->ocur = XML_ELEMENT_CONTENT_ONCE;
4882 }
Owen Taylor3473f882001-02-23 17:55:21 +00004883 GROW;
4884 }
4885 SKIP_BLANKS;
4886 SHRINK;
4887 while (RAW != ')') {
4888 /*
4889 * Each loop we parse one separator and one element.
4890 */
4891 if (RAW == ',') {
4892 if (type == 0) type = CUR;
4893
4894 /*
4895 * Detect "Name | Name , Name" error
4896 */
4897 else if (type != CUR) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004898 xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004899 "xmlParseElementChildrenContentDecl : '%c' expected\n",
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004900 type);
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004901 if ((last != NULL) && (last != ret))
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004902 xmlFreeDocElementContent(ctxt->myDoc, last);
Owen Taylor3473f882001-02-23 17:55:21 +00004903 if (ret != NULL)
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004904 xmlFreeDocElementContent(ctxt->myDoc, ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004905 return(NULL);
4906 }
4907 NEXT;
4908
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004909 op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_SEQ);
Owen Taylor3473f882001-02-23 17:55:21 +00004910 if (op == NULL) {
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004911 if ((last != NULL) && (last != ret))
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004912 xmlFreeDocElementContent(ctxt->myDoc, last);
4913 xmlFreeDocElementContent(ctxt->myDoc, ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004914 return(NULL);
4915 }
4916 if (last == NULL) {
4917 op->c1 = ret;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004918 if (ret != NULL)
4919 ret->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004920 ret = cur = op;
4921 } else {
4922 cur->c2 = op;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004923 if (op != NULL)
4924 op->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004925 op->c1 = last;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004926 if (last != NULL)
4927 last->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004928 cur =op;
4929 last = NULL;
4930 }
4931 } else if (RAW == '|') {
4932 if (type == 0) type = CUR;
4933
4934 /*
4935 * Detect "Name , Name | Name" error
4936 */
4937 else if (type != CUR) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004938 xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004939 "xmlParseElementChildrenContentDecl : '%c' expected\n",
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004940 type);
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004941 if ((last != NULL) && (last != ret))
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004942 xmlFreeDocElementContent(ctxt->myDoc, last);
Owen Taylor3473f882001-02-23 17:55:21 +00004943 if (ret != NULL)
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004944 xmlFreeDocElementContent(ctxt->myDoc, ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004945 return(NULL);
4946 }
4947 NEXT;
4948
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004949 op = xmlNewDocElementContent(ctxt->myDoc, NULL, XML_ELEMENT_CONTENT_OR);
Owen Taylor3473f882001-02-23 17:55:21 +00004950 if (op == NULL) {
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004951 if ((last != NULL) && (last != ret))
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004952 xmlFreeDocElementContent(ctxt->myDoc, last);
Owen Taylor3473f882001-02-23 17:55:21 +00004953 if (ret != NULL)
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004954 xmlFreeDocElementContent(ctxt->myDoc, ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004955 return(NULL);
4956 }
4957 if (last == NULL) {
4958 op->c1 = ret;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004959 if (ret != NULL)
4960 ret->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004961 ret = cur = op;
4962 } else {
4963 cur->c2 = op;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004964 if (op != NULL)
4965 op->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004966 op->c1 = last;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004967 if (last != NULL)
4968 last->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004969 cur =op;
4970 last = NULL;
4971 }
4972 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004973 xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004974 if (ret != NULL)
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004975 xmlFreeDocElementContent(ctxt->myDoc, ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004976 return(NULL);
4977 }
4978 GROW;
4979 SKIP_BLANKS;
4980 GROW;
4981 if (RAW == '(') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004982 int inputid = ctxt->input->id;
Owen Taylor3473f882001-02-23 17:55:21 +00004983 /* Recurse on second child */
4984 NEXT;
4985 SKIP_BLANKS;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004986 last = xmlParseElementChildrenContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004987 SKIP_BLANKS;
4988 } else {
Daniel Veillard76d66f42001-05-16 21:05:17 +00004989 elem = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004990 if (elem == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004991 xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004992 if (ret != NULL)
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004993 xmlFreeDocElementContent(ctxt->myDoc, ret);
Owen Taylor3473f882001-02-23 17:55:21 +00004994 return(NULL);
4995 }
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00004996 last = xmlNewDocElementContent(ctxt->myDoc, elem, XML_ELEMENT_CONTENT_ELEMENT);
Owen Taylor3473f882001-02-23 17:55:21 +00004997 if (RAW == '?') {
4998 last->ocur = XML_ELEMENT_CONTENT_OPT;
4999 NEXT;
5000 } else if (RAW == '*') {
5001 last->ocur = XML_ELEMENT_CONTENT_MULT;
5002 NEXT;
5003 } else if (RAW == '+') {
5004 last->ocur = XML_ELEMENT_CONTENT_PLUS;
5005 NEXT;
5006 } else {
5007 last->ocur = XML_ELEMENT_CONTENT_ONCE;
5008 }
5009 }
5010 SKIP_BLANKS;
5011 GROW;
5012 }
5013 if ((cur != NULL) && (last != NULL)) {
5014 cur->c2 = last;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00005015 if (last != NULL)
5016 last->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00005017 }
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005018 if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00005019 xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
5020"Element content declaration doesn't start and stop in the same entity\n",
5021 NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00005022 }
Owen Taylor3473f882001-02-23 17:55:21 +00005023 NEXT;
5024 if (RAW == '?') {
William M. Brackf8f2e8f2004-05-14 04:37:41 +00005025 if (ret != NULL) {
5026 if ((ret->ocur == XML_ELEMENT_CONTENT_PLUS) ||
5027 (ret->ocur == XML_ELEMENT_CONTENT_MULT))
5028 ret->ocur = XML_ELEMENT_CONTENT_MULT;
5029 else
5030 ret->ocur = XML_ELEMENT_CONTENT_OPT;
5031 }
Owen Taylor3473f882001-02-23 17:55:21 +00005032 NEXT;
5033 } else if (RAW == '*') {
Daniel Veillardce2c2f02001-10-18 14:57:24 +00005034 if (ret != NULL) {
Daniel Veillarde470df72001-04-18 21:41:07 +00005035 ret->ocur = XML_ELEMENT_CONTENT_MULT;
Daniel Veillardce2c2f02001-10-18 14:57:24 +00005036 cur = ret;
5037 /*
5038 * Some normalization:
5039 * (a | b* | c?)* == (a | b | c)*
5040 */
5041 while (cur->type == XML_ELEMENT_CONTENT_OR) {
5042 if ((cur->c1 != NULL) &&
5043 ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
5044 (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT)))
5045 cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
5046 if ((cur->c2 != NULL) &&
5047 ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
5048 (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT)))
5049 cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
5050 cur = cur->c2;
5051 }
5052 }
Owen Taylor3473f882001-02-23 17:55:21 +00005053 NEXT;
5054 } else if (RAW == '+') {
Daniel Veillardce2c2f02001-10-18 14:57:24 +00005055 if (ret != NULL) {
5056 int found = 0;
5057
William M. Brackf8f2e8f2004-05-14 04:37:41 +00005058 if ((ret->ocur == XML_ELEMENT_CONTENT_OPT) ||
5059 (ret->ocur == XML_ELEMENT_CONTENT_MULT))
5060 ret->ocur = XML_ELEMENT_CONTENT_MULT;
William M. Brackeb8509c2004-05-14 03:48:02 +00005061 else
5062 ret->ocur = XML_ELEMENT_CONTENT_PLUS;
Daniel Veillardce2c2f02001-10-18 14:57:24 +00005063 /*
5064 * Some normalization:
5065 * (a | b*)+ == (a | b)*
5066 * (a | b?)+ == (a | b)*
5067 */
5068 while (cur->type == XML_ELEMENT_CONTENT_OR) {
5069 if ((cur->c1 != NULL) &&
5070 ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
5071 (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
5072 cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
5073 found = 1;
5074 }
5075 if ((cur->c2 != NULL) &&
5076 ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
5077 (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) {
5078 cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
5079 found = 1;
5080 }
5081 cur = cur->c2;
5082 }
5083 if (found)
5084 ret->ocur = XML_ELEMENT_CONTENT_MULT;
5085 }
Owen Taylor3473f882001-02-23 17:55:21 +00005086 NEXT;
5087 }
5088 return(ret);
5089}
5090
5091/**
5092 * xmlParseElementContentDecl:
5093 * @ctxt: an XML parser context
5094 * @name: the name of the element being defined.
5095 * @result: the Element Content pointer will be stored here if any
5096 *
5097 * parse the declaration for an Element content either Mixed or Children,
5098 * the cases EMPTY and ANY are handled directly in xmlParseElementDecl
5099 *
5100 * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children
5101 *
5102 * returns: the type of element content XML_ELEMENT_TYPE_xxx
5103 */
5104
5105int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00005106xmlParseElementContentDecl(xmlParserCtxtPtr ctxt, const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00005107 xmlElementContentPtr *result) {
5108
5109 xmlElementContentPtr tree = NULL;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005110 int inputid = ctxt->input->id;
Owen Taylor3473f882001-02-23 17:55:21 +00005111 int res;
5112
5113 *result = NULL;
5114
5115 if (RAW != '(') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005116 xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005117 "xmlParseElementContentDecl : %s '(' expected\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005118 return(-1);
5119 }
5120 NEXT;
5121 GROW;
5122 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00005123 if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005124 tree = xmlParseElementMixedContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00005125 res = XML_ELEMENT_TYPE_MIXED;
5126 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005127 tree = xmlParseElementChildrenContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00005128 res = XML_ELEMENT_TYPE_ELEMENT;
5129 }
Owen Taylor3473f882001-02-23 17:55:21 +00005130 SKIP_BLANKS;
5131 *result = tree;
5132 return(res);
5133}
5134
5135/**
5136 * xmlParseElementDecl:
5137 * @ctxt: an XML parser context
5138 *
5139 * parse an Element declaration.
5140 *
5141 * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>'
5142 *
5143 * [ VC: Unique Element Type Declaration ]
5144 * No element type may be declared more than once
5145 *
5146 * Returns the type of the element, or -1 in case of error
5147 */
5148int
5149xmlParseElementDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00005150 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00005151 int ret = -1;
5152 xmlElementContentPtr content = NULL;
5153
Daniel Veillard4c778d82005-01-23 17:37:44 +00005154 /* GROW; done in the caller */
Daniel Veillarda07050d2003-10-19 14:46:32 +00005155 if (CMP9(CUR_PTR, '<', '!', 'E', 'L', 'E', 'M', 'E', 'N', 'T')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005156 xmlParserInputPtr input = ctxt->input;
5157
5158 SKIP(9);
William M. Brack76e95df2003-10-18 16:20:14 +00005159 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005160 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5161 "Space required after 'ELEMENT'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005162 }
5163 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00005164 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005165 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005166 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
5167 "xmlParseElementDecl: no name for Element\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005168 return(-1);
5169 }
5170 while ((RAW == 0) && (ctxt->inputNr > 1))
5171 xmlPopInput(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00005172 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005173 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5174 "Space required after the element name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005175 }
5176 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00005177 if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005178 SKIP(5);
5179 /*
5180 * Element must always be empty.
5181 */
5182 ret = XML_ELEMENT_TYPE_EMPTY;
5183 } else if ((RAW == 'A') && (NXT(1) == 'N') &&
5184 (NXT(2) == 'Y')) {
5185 SKIP(3);
5186 /*
5187 * Element is a generic container.
5188 */
5189 ret = XML_ELEMENT_TYPE_ANY;
5190 } else if (RAW == '(') {
5191 ret = xmlParseElementContentDecl(ctxt, name, &content);
5192 } else {
5193 /*
5194 * [ WFC: PEs in Internal Subset ] error handling.
5195 */
5196 if ((RAW == '%') && (ctxt->external == 0) &&
5197 (ctxt->inputNr == 1)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005198 xmlFatalErrMsg(ctxt, XML_ERR_PEREF_IN_INT_SUBSET,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005199 "PEReference: forbidden within markup decl in internal subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005200 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00005201 xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
Owen Taylor3473f882001-02-23 17:55:21 +00005202 "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n");
5203 }
Owen Taylor3473f882001-02-23 17:55:21 +00005204 return(-1);
5205 }
5206
5207 SKIP_BLANKS;
5208 /*
5209 * Pop-up of finished entities.
5210 */
5211 while ((RAW == 0) && (ctxt->inputNr > 1))
5212 xmlPopInput(ctxt);
5213 SKIP_BLANKS;
5214
5215 if (RAW != '>') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005216 xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00005217 if (content != NULL) {
5218 xmlFreeDocElementContent(ctxt->myDoc, content);
5219 }
Owen Taylor3473f882001-02-23 17:55:21 +00005220 } else {
5221 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005222 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
5223 "Element declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005224 }
5225
5226 NEXT;
5227 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00005228 (ctxt->sax->elementDecl != NULL)) {
5229 if (content != NULL)
5230 content->parent = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005231 ctxt->sax->elementDecl(ctxt->userData, name, ret,
5232 content);
Daniel Veillardcee2b3a2005-01-25 00:22:52 +00005233 if ((content != NULL) && (content->parent == NULL)) {
5234 /*
5235 * this is a trick: if xmlAddElementDecl is called,
5236 * instead of copying the full tree it is plugged directly
5237 * if called from the parser. Avoid duplicating the
5238 * interfaces or change the API/ABI
5239 */
5240 xmlFreeDocElementContent(ctxt->myDoc, content);
5241 }
5242 } else if (content != NULL) {
5243 xmlFreeDocElementContent(ctxt->myDoc, content);
5244 }
Owen Taylor3473f882001-02-23 17:55:21 +00005245 }
Owen Taylor3473f882001-02-23 17:55:21 +00005246 }
5247 return(ret);
5248}
5249
5250/**
Owen Taylor3473f882001-02-23 17:55:21 +00005251 * xmlParseConditionalSections
5252 * @ctxt: an XML parser context
5253 *
5254 * [61] conditionalSect ::= includeSect | ignoreSect
5255 * [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>'
5256 * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>'
5257 * [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)*
5258 * [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*)
5259 */
5260
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005261static void
Owen Taylor3473f882001-02-23 17:55:21 +00005262xmlParseConditionalSections(xmlParserCtxtPtr ctxt) {
5263 SKIP(3);
5264 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00005265 if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005266 SKIP(7);
5267 SKIP_BLANKS;
5268 if (RAW != '[') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005269 xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005270 } else {
5271 NEXT;
5272 }
5273 if (xmlParserDebugEntities) {
5274 if ((ctxt->input != NULL) && (ctxt->input->filename))
5275 xmlGenericError(xmlGenericErrorContext,
5276 "%s(%d): ", ctxt->input->filename,
5277 ctxt->input->line);
5278 xmlGenericError(xmlGenericErrorContext,
5279 "Entering INCLUDE Conditional Section\n");
5280 }
5281
5282 while ((RAW != 0) && ((RAW != ']') || (NXT(1) != ']') ||
5283 (NXT(2) != '>'))) {
5284 const xmlChar *check = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00005285 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00005286
5287 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5288 xmlParseConditionalSections(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00005289 } else if (IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005290 NEXT;
5291 } else if (RAW == '%') {
5292 xmlParsePEReference(ctxt);
5293 } else
5294 xmlParseMarkupDecl(ctxt);
5295
5296 /*
5297 * Pop-up of finished entities.
5298 */
5299 while ((RAW == 0) && (ctxt->inputNr > 1))
5300 xmlPopInput(ctxt);
5301
Daniel Veillardfdc91562002-07-01 21:52:03 +00005302 if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005303 xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005304 break;
5305 }
5306 }
5307 if (xmlParserDebugEntities) {
5308 if ((ctxt->input != NULL) && (ctxt->input->filename))
5309 xmlGenericError(xmlGenericErrorContext,
5310 "%s(%d): ", ctxt->input->filename,
5311 ctxt->input->line);
5312 xmlGenericError(xmlGenericErrorContext,
5313 "Leaving INCLUDE Conditional Section\n");
5314 }
5315
Daniel Veillarda07050d2003-10-19 14:46:32 +00005316 } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005317 int state;
William M. Brack78637da2003-07-31 14:47:38 +00005318 xmlParserInputState instate;
Owen Taylor3473f882001-02-23 17:55:21 +00005319 int depth = 0;
5320
5321 SKIP(6);
5322 SKIP_BLANKS;
5323 if (RAW != '[') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005324 xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005325 } else {
5326 NEXT;
5327 }
5328 if (xmlParserDebugEntities) {
5329 if ((ctxt->input != NULL) && (ctxt->input->filename))
5330 xmlGenericError(xmlGenericErrorContext,
5331 "%s(%d): ", ctxt->input->filename,
5332 ctxt->input->line);
5333 xmlGenericError(xmlGenericErrorContext,
5334 "Entering IGNORE Conditional Section\n");
5335 }
5336
5337 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005338 * Parse up to the end of the conditional section
Owen Taylor3473f882001-02-23 17:55:21 +00005339 * But disable SAX event generating DTD building in the meantime
5340 */
5341 state = ctxt->disableSAX;
5342 instate = ctxt->instate;
Daniel Veillarddad3f682002-11-17 16:47:27 +00005343 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00005344 ctxt->instate = XML_PARSER_IGNORE;
5345
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00005346 while ((depth >= 0) && (RAW != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005347 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5348 depth++;
5349 SKIP(3);
5350 continue;
5351 }
5352 if ((RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) {
5353 if (--depth >= 0) SKIP(3);
5354 continue;
5355 }
5356 NEXT;
5357 continue;
5358 }
5359
5360 ctxt->disableSAX = state;
5361 ctxt->instate = instate;
5362
5363 if (xmlParserDebugEntities) {
5364 if ((ctxt->input != NULL) && (ctxt->input->filename))
5365 xmlGenericError(xmlGenericErrorContext,
5366 "%s(%d): ", ctxt->input->filename,
5367 ctxt->input->line);
5368 xmlGenericError(xmlGenericErrorContext,
5369 "Leaving IGNORE Conditional Section\n");
5370 }
5371
5372 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005373 xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005374 }
5375
5376 if (RAW == 0)
5377 SHRINK;
5378
5379 if (RAW == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005380 xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005381 } else {
5382 SKIP(3);
5383 }
5384}
5385
5386/**
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005387 * xmlParseMarkupDecl:
5388 * @ctxt: an XML parser context
5389 *
5390 * parse Markup declarations
5391 *
5392 * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl |
5393 * NotationDecl | PI | Comment
5394 *
5395 * [ VC: Proper Declaration/PE Nesting ]
5396 * Parameter-entity replacement text must be properly nested with
5397 * markup declarations. That is to say, if either the first character
5398 * or the last character of a markup declaration (markupdecl above) is
5399 * contained in the replacement text for a parameter-entity reference,
5400 * both must be contained in the same replacement text.
5401 *
5402 * [ WFC: PEs in Internal Subset ]
5403 * In the internal DTD subset, parameter-entity references can occur
5404 * only where markup declarations can occur, not within markup declarations.
5405 * (This does not apply to references that occur in external parameter
5406 * entities or to the external subset.)
5407 */
5408void
5409xmlParseMarkupDecl(xmlParserCtxtPtr ctxt) {
5410 GROW;
Daniel Veillard4c778d82005-01-23 17:37:44 +00005411 if (CUR == '<') {
5412 if (NXT(1) == '!') {
5413 switch (NXT(2)) {
5414 case 'E':
5415 if (NXT(3) == 'L')
5416 xmlParseElementDecl(ctxt);
5417 else if (NXT(3) == 'N')
5418 xmlParseEntityDecl(ctxt);
5419 break;
5420 case 'A':
5421 xmlParseAttributeListDecl(ctxt);
5422 break;
5423 case 'N':
5424 xmlParseNotationDecl(ctxt);
5425 break;
5426 case '-':
5427 xmlParseComment(ctxt);
5428 break;
5429 default:
5430 /* there is an error but it will be detected later */
5431 break;
5432 }
5433 } else if (NXT(1) == '?') {
5434 xmlParsePI(ctxt);
5435 }
5436 }
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005437 /*
5438 * This is only for internal subset. On external entities,
5439 * the replacement is done before parsing stage
5440 */
5441 if ((ctxt->external == 0) && (ctxt->inputNr == 1))
5442 xmlParsePEReference(ctxt);
5443
5444 /*
5445 * Conditional sections are allowed from entities included
5446 * by PE References in the internal subset.
5447 */
5448 if ((ctxt->external == 0) && (ctxt->inputNr > 1)) {
5449 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5450 xmlParseConditionalSections(ctxt);
5451 }
5452 }
5453
5454 ctxt->instate = XML_PARSER_DTD;
5455}
5456
5457/**
5458 * xmlParseTextDecl:
5459 * @ctxt: an XML parser context
5460 *
5461 * parse an XML declaration header for external entities
5462 *
5463 * [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>'
5464 *
5465 * Question: Seems that EncodingDecl is mandatory ? Is that a typo ?
5466 */
5467
5468void
5469xmlParseTextDecl(xmlParserCtxtPtr ctxt) {
5470 xmlChar *version;
Daniel Veillardf5cb3cd2003-10-28 13:58:13 +00005471 const xmlChar *encoding;
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005472
5473 /*
5474 * We know that '<?xml' is here.
5475 */
Daniel Veillarda07050d2003-10-19 14:46:32 +00005476 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005477 SKIP(5);
5478 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005479 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005480 return;
5481 }
5482
William M. Brack76e95df2003-10-18 16:20:14 +00005483 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005484 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5485 "Space needed after '<?xml'\n");
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005486 }
5487 SKIP_BLANKS;
5488
5489 /*
5490 * We may have the VersionInfo here.
5491 */
5492 version = xmlParseVersionInfo(ctxt);
5493 if (version == NULL)
5494 version = xmlCharStrdup(XML_DEFAULT_VERSION);
Daniel Veillard401c2112002-01-07 16:54:10 +00005495 else {
William M. Brack76e95df2003-10-18 16:20:14 +00005496 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005497 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5498 "Space needed here\n");
Daniel Veillard401c2112002-01-07 16:54:10 +00005499 }
5500 }
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005501 ctxt->input->version = version;
5502
5503 /*
5504 * We must have the encoding declaration
5505 */
Daniel Veillardf5cb3cd2003-10-28 13:58:13 +00005506 encoding = xmlParseEncodingDecl(ctxt);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005507 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
5508 /*
5509 * The XML REC instructs us to stop parsing right here
5510 */
5511 return;
5512 }
Daniel Veillardf5cb3cd2003-10-28 13:58:13 +00005513 if ((encoding == NULL) && (ctxt->errNo == XML_ERR_OK)) {
5514 xmlFatalErrMsg(ctxt, XML_ERR_MISSING_ENCODING,
5515 "Missing encoding in text declaration\n");
5516 }
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005517
5518 SKIP_BLANKS;
5519 if ((RAW == '?') && (NXT(1) == '>')) {
5520 SKIP(2);
5521 } else if (RAW == '>') {
5522 /* Deprecated old WD ... */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005523 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005524 NEXT;
5525 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005526 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005527 MOVETO_ENDTAG(CUR_PTR);
5528 NEXT;
5529 }
5530}
5531
5532/**
Owen Taylor3473f882001-02-23 17:55:21 +00005533 * xmlParseExternalSubset:
5534 * @ctxt: an XML parser context
5535 * @ExternalID: the external identifier
5536 * @SystemID: the system identifier (or URL)
5537 *
5538 * parse Markup declarations from an external subset
5539 *
5540 * [30] extSubset ::= textDecl? extSubsetDecl
5541 *
5542 * [31] extSubsetDecl ::= (markupdecl | conditionalSect | PEReference | S) *
5543 */
5544void
5545xmlParseExternalSubset(xmlParserCtxtPtr ctxt, const xmlChar *ExternalID,
5546 const xmlChar *SystemID) {
Daniel Veillard309f81d2003-09-23 09:02:53 +00005547 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005548 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00005549 if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005550 xmlParseTextDecl(ctxt);
5551 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
5552 /*
5553 * The XML REC instructs us to stop parsing right here
5554 */
5555 ctxt->instate = XML_PARSER_EOF;
5556 return;
5557 }
5558 }
5559 if (ctxt->myDoc == NULL) {
5560 ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
5561 }
5562 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset == NULL))
5563 xmlCreateIntSubset(ctxt->myDoc, NULL, ExternalID, SystemID);
5564
5565 ctxt->instate = XML_PARSER_DTD;
5566 ctxt->external = 1;
5567 while (((RAW == '<') && (NXT(1) == '?')) ||
5568 ((RAW == '<') && (NXT(1) == '!')) ||
William M. Brack76e95df2003-10-18 16:20:14 +00005569 (RAW == '%') || IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005570 const xmlChar *check = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00005571 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00005572
5573 GROW;
5574 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5575 xmlParseConditionalSections(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00005576 } else if (IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005577 NEXT;
5578 } else if (RAW == '%') {
5579 xmlParsePEReference(ctxt);
5580 } else
5581 xmlParseMarkupDecl(ctxt);
5582
5583 /*
5584 * Pop-up of finished entities.
5585 */
5586 while ((RAW == 0) && (ctxt->inputNr > 1))
5587 xmlPopInput(ctxt);
5588
Daniel Veillardfdc91562002-07-01 21:52:03 +00005589 if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005590 xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005591 break;
5592 }
5593 }
5594
5595 if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005596 xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005597 }
5598
5599}
5600
5601/**
5602 * xmlParseReference:
5603 * @ctxt: an XML parser context
5604 *
5605 * parse and handle entity references in content, depending on the SAX
5606 * interface, this may end-up in a call to character() if this is a
5607 * CharRef, a predefined entity, if there is no reference() callback.
5608 * or if the parser was asked to switch to that mode.
5609 *
5610 * [67] Reference ::= EntityRef | CharRef
5611 */
5612void
5613xmlParseReference(xmlParserCtxtPtr ctxt) {
5614 xmlEntityPtr ent;
5615 xmlChar *val;
5616 if (RAW != '&') return;
5617
5618 if (NXT(1) == '#') {
5619 int i = 0;
5620 xmlChar out[10];
5621 int hex = NXT(2);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005622 int value = xmlParseCharRef(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005623
5624 if (ctxt->charset != XML_CHAR_ENCODING_UTF8) {
5625 /*
5626 * So we are using non-UTF-8 buffers
5627 * Check that the char fit on 8bits, if not
5628 * generate a CharRef.
5629 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005630 if (value <= 0xFF) {
5631 out[0] = value;
Owen Taylor3473f882001-02-23 17:55:21 +00005632 out[1] = 0;
5633 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
5634 (!ctxt->disableSAX))
5635 ctxt->sax->characters(ctxt->userData, out, 1);
5636 } else {
5637 if ((hex == 'x') || (hex == 'X'))
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005638 snprintf((char *)out, sizeof(out), "#x%X", value);
Owen Taylor3473f882001-02-23 17:55:21 +00005639 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005640 snprintf((char *)out, sizeof(out), "#%d", value);
Owen Taylor3473f882001-02-23 17:55:21 +00005641 if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) &&
5642 (!ctxt->disableSAX))
5643 ctxt->sax->reference(ctxt->userData, out);
5644 }
5645 } else {
5646 /*
5647 * Just encode the value in UTF-8
5648 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005649 COPY_BUF(0 ,out, i, value);
Owen Taylor3473f882001-02-23 17:55:21 +00005650 out[i] = 0;
5651 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
5652 (!ctxt->disableSAX))
5653 ctxt->sax->characters(ctxt->userData, out, i);
5654 }
5655 } else {
5656 ent = xmlParseEntityRef(ctxt);
5657 if (ent == NULL) return;
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00005658 if (!ctxt->wellFormed)
5659 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005660 if ((ent->name != NULL) &&
5661 (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY)) {
5662 xmlNodePtr list = NULL;
Daniel Veillard7d515752003-09-26 19:12:37 +00005663 xmlParserErrors ret = XML_ERR_OK;
Owen Taylor3473f882001-02-23 17:55:21 +00005664
5665
5666 /*
5667 * The first reference to the entity trigger a parsing phase
5668 * where the ent->children is filled with the result from
5669 * the parsing.
5670 */
5671 if (ent->children == NULL) {
5672 xmlChar *value;
5673 value = ent->content;
5674
5675 /*
5676 * Check that this entity is well formed
5677 */
Daniel Veillard4aede2e2003-10-17 12:43:59 +00005678 if ((value != NULL) && (value[0] != 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +00005679 (value[1] == 0) && (value[0] == '<') &&
5680 (xmlStrEqual(ent->name, BAD_CAST "lt"))) {
5681 /*
5682 * DONE: get definite answer on this !!!
5683 * Lots of entity decls are used to declare a single
5684 * char
5685 * <!ENTITY lt "<">
5686 * Which seems to be valid since
5687 * 2.4: The ampersand character (&) and the left angle
5688 * bracket (<) may appear in their literal form only
5689 * when used ... They are also legal within the literal
5690 * entity value of an internal entity declaration;i
5691 * see "4.3.2 Well-Formed Parsed Entities".
5692 * IMHO 2.4 and 4.3.2 are directly in contradiction.
5693 * Looking at the OASIS test suite and James Clark
5694 * tests, this is broken. However the XML REC uses
5695 * it. Is the XML REC not well-formed ????
5696 * This is a hack to avoid this problem
5697 *
5698 * ANSWER: since lt gt amp .. are already defined,
5699 * this is a redefinition and hence the fact that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005700 * content is not well balanced is not a Wf error, this
Owen Taylor3473f882001-02-23 17:55:21 +00005701 * is lousy but acceptable.
5702 */
5703 list = xmlNewDocText(ctxt->myDoc, value);
5704 if (list != NULL) {
5705 if ((ent->etype == XML_INTERNAL_GENERAL_ENTITY) &&
5706 (ent->children == NULL)) {
5707 ent->children = list;
5708 ent->last = list;
Daniel Veillard2d84a892002-12-30 00:01:08 +00005709 ent->owner = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00005710 list->parent = (xmlNodePtr) ent;
5711 } else {
5712 xmlFreeNodeList(list);
5713 }
5714 } else if (list != NULL) {
5715 xmlFreeNodeList(list);
5716 }
5717 } else {
5718 /*
5719 * 4.3.2: An internal general parsed entity is well-formed
5720 * if its replacement text matches the production labeled
5721 * content.
5722 */
Daniel Veillardb5a60ec2002-03-18 11:45:56 +00005723
5724 void *user_data;
5725 /*
5726 * This is a bit hackish but this seems the best
5727 * way to make sure both SAX and DOM entity support
5728 * behaves okay.
5729 */
5730 if (ctxt->userData == ctxt)
5731 user_data = NULL;
5732 else
5733 user_data = ctxt->userData;
5734
Owen Taylor3473f882001-02-23 17:55:21 +00005735 if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) {
5736 ctxt->depth++;
Daniel Veillard328f48c2002-11-15 15:24:34 +00005737 ret = xmlParseBalancedChunkMemoryInternal(ctxt,
5738 value, user_data, &list);
Owen Taylor3473f882001-02-23 17:55:21 +00005739 ctxt->depth--;
5740 } else if (ent->etype ==
5741 XML_EXTERNAL_GENERAL_PARSED_ENTITY) {
5742 ctxt->depth++;
Daniel Veillarda97a19b2001-05-20 13:19:52 +00005743 ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt,
Daniel Veillardb5a60ec2002-03-18 11:45:56 +00005744 ctxt->sax, user_data, ctxt->depth,
Daniel Veillarda97a19b2001-05-20 13:19:52 +00005745 ent->URI, ent->ExternalID, &list);
Owen Taylor3473f882001-02-23 17:55:21 +00005746 ctxt->depth--;
5747 } else {
Daniel Veillard7d515752003-09-26 19:12:37 +00005748 ret = XML_ERR_ENTITY_PE_INTERNAL;
Daniel Veillardf403d292003-10-05 13:51:35 +00005749 xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR,
5750 "invalid entity type found\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005751 }
5752 if (ret == XML_ERR_ENTITY_LOOP) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005753 xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00005754 return;
Daniel Veillard7d515752003-09-26 19:12:37 +00005755 } else if ((ret == XML_ERR_OK) && (list != NULL)) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00005756 if (((ent->etype == XML_INTERNAL_GENERAL_ENTITY) ||
5757 (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY))&&
Owen Taylor3473f882001-02-23 17:55:21 +00005758 (ent->children == NULL)) {
5759 ent->children = list;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005760 if (ctxt->replaceEntities) {
5761 /*
5762 * Prune it directly in the generated document
5763 * except for single text nodes.
5764 */
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005765 if (((list->type == XML_TEXT_NODE) &&
5766 (list->next == NULL)) ||
5767 (ctxt->parseMode == XML_PARSE_READER)) {
Daniel Veillard62f313b2001-07-04 19:49:14 +00005768 list->parent = (xmlNodePtr) ent;
5769 list = NULL;
Daniel Veillard2d84a892002-12-30 00:01:08 +00005770 ent->owner = 1;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005771 } else {
Daniel Veillard2d84a892002-12-30 00:01:08 +00005772 ent->owner = 0;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005773 while (list != NULL) {
5774 list->parent = (xmlNodePtr) ctxt->node;
Daniel Veillard68e9e742002-11-16 15:35:11 +00005775 list->doc = ctxt->myDoc;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005776 if (list->next == NULL)
5777 ent->last = list;
5778 list = list->next;
Daniel Veillard8107a222002-01-13 14:10:10 +00005779 }
Daniel Veillard62f313b2001-07-04 19:49:14 +00005780 list = ent->children;
Daniel Veillard81273902003-09-30 00:43:48 +00005781#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +00005782 if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
5783 xmlAddEntityReference(ent, list, NULL);
Daniel Veillard81273902003-09-30 00:43:48 +00005784#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillard62f313b2001-07-04 19:49:14 +00005785 }
5786 } else {
Daniel Veillard2d84a892002-12-30 00:01:08 +00005787 ent->owner = 1;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005788 while (list != NULL) {
5789 list->parent = (xmlNodePtr) ent;
5790 if (list->next == NULL)
5791 ent->last = list;
5792 list = list->next;
5793 }
Owen Taylor3473f882001-02-23 17:55:21 +00005794 }
5795 } else {
5796 xmlFreeNodeList(list);
Daniel Veillard62f313b2001-07-04 19:49:14 +00005797 list = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005798 }
William M. Brackb670e2e2003-09-27 01:05:55 +00005799 } else if ((ret != XML_ERR_OK) &&
5800 (ret != XML_WAR_UNDECLARED_ENTITY)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005801 xmlFatalErr(ctxt, ret, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005802 } else if (list != NULL) {
5803 xmlFreeNodeList(list);
Daniel Veillard62f313b2001-07-04 19:49:14 +00005804 list = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005805 }
5806 }
5807 }
5808 if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) &&
5809 (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) {
5810 /*
5811 * Create a node.
5812 */
5813 ctxt->sax->reference(ctxt->userData, ent->name);
5814 return;
5815 } else if (ctxt->replaceEntities) {
William M. Brack1227fb32004-10-25 23:17:53 +00005816 /*
5817 * There is a problem on the handling of _private for entities
5818 * (bug 155816): Should we copy the content of the field from
5819 * the entity (possibly overwriting some value set by the user
5820 * when a copy is created), should we leave it alone, or should
5821 * we try to take care of different situations? The problem
5822 * is exacerbated by the usage of this field by the xmlReader.
5823 * To fix this bug, we look at _private on the created node
5824 * and, if it's NULL, we copy in whatever was in the entity.
5825 * If it's not NULL we leave it alone. This is somewhat of a
5826 * hack - maybe we should have further tests to determine
5827 * what to do.
5828 */
Owen Taylor3473f882001-02-23 17:55:21 +00005829 if ((ctxt->node != NULL) && (ent->children != NULL)) {
5830 /*
5831 * Seems we are generating the DOM content, do
Daniel Veillard62f313b2001-07-04 19:49:14 +00005832 * a simple tree copy for all references except the first
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005833 * In the first occurrence list contains the replacement.
5834 * progressive == 2 means we are operating on the Reader
5835 * and since nodes are discarded we must copy all the time.
Owen Taylor3473f882001-02-23 17:55:21 +00005836 */
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005837 if (((list == NULL) && (ent->owner == 0)) ||
5838 (ctxt->parseMode == XML_PARSE_READER)) {
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005839 xmlNodePtr nw = NULL, cur, firstChild = NULL;
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005840
5841 /*
5842 * when operating on a reader, the entities definitions
5843 * are always owning the entities subtree.
5844 if (ctxt->parseMode == XML_PARSE_READER)
5845 ent->owner = 1;
5846 */
5847
Daniel Veillard62f313b2001-07-04 19:49:14 +00005848 cur = ent->children;
5849 while (cur != NULL) {
Daniel Veillard03a53c32004-10-26 16:06:51 +00005850 nw = xmlDocCopyNode(cur, ctxt->myDoc, 1);
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005851 if (nw != NULL) {
William M. Brack1227fb32004-10-25 23:17:53 +00005852 if (nw->_private == NULL)
5853 nw->_private = cur->_private;
Daniel Veillard8f872442003-01-09 23:19:02 +00005854 if (firstChild == NULL){
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005855 firstChild = nw;
Daniel Veillard8f872442003-01-09 23:19:02 +00005856 }
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005857 nw = xmlAddChild(ctxt->node, nw);
Daniel Veillard8107a222002-01-13 14:10:10 +00005858 }
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005859 if (cur == ent->last) {
5860 /*
5861 * needed to detect some strange empty
5862 * node cases in the reader tests
5863 */
5864 if ((ctxt->parseMode == XML_PARSE_READER) &&
5865 (nw->type == XML_ELEMENT_NODE) &&
5866 (nw->children == NULL))
5867 nw->extra = 1;
5868
Daniel Veillard62f313b2001-07-04 19:49:14 +00005869 break;
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005870 }
Daniel Veillard62f313b2001-07-04 19:49:14 +00005871 cur = cur->next;
5872 }
Daniel Veillard81273902003-09-30 00:43:48 +00005873#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +00005874 if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005875 xmlAddEntityReference(ent, firstChild, nw);
Daniel Veillard81273902003-09-30 00:43:48 +00005876#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005877 } else if (list == NULL) {
5878 xmlNodePtr nw = NULL, cur, next, last,
5879 firstChild = NULL;
5880 /*
5881 * Copy the entity child list and make it the new
5882 * entity child list. The goal is to make sure any
5883 * ID or REF referenced will be the one from the
5884 * document content and not the entity copy.
5885 */
5886 cur = ent->children;
5887 ent->children = NULL;
5888 last = ent->last;
5889 ent->last = NULL;
5890 while (cur != NULL) {
5891 next = cur->next;
5892 cur->next = NULL;
5893 cur->parent = NULL;
Daniel Veillard03a53c32004-10-26 16:06:51 +00005894 nw = xmlDocCopyNode(cur, ctxt->myDoc, 1);
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005895 if (nw != NULL) {
William M. Brack1227fb32004-10-25 23:17:53 +00005896 if (nw->_private == NULL)
5897 nw->_private = cur->_private;
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005898 if (firstChild == NULL){
5899 firstChild = cur;
5900 }
5901 xmlAddChild((xmlNodePtr) ent, nw);
5902 xmlAddChild(ctxt->node, cur);
5903 }
5904 if (cur == last)
5905 break;
5906 cur = next;
5907 }
5908 ent->owner = 1;
Daniel Veillard81273902003-09-30 00:43:48 +00005909#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005910 if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
5911 xmlAddEntityReference(ent, firstChild, nw);
Daniel Veillard81273902003-09-30 00:43:48 +00005912#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillard62f313b2001-07-04 19:49:14 +00005913 } else {
Daniel Veillard370ba3d2004-10-25 16:23:56 +00005914 const xmlChar *nbktext;
5915
Daniel Veillard62f313b2001-07-04 19:49:14 +00005916 /*
5917 * the name change is to avoid coalescing of the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005918 * node with a possible previous text one which
5919 * would make ent->children a dangling pointer
Daniel Veillard62f313b2001-07-04 19:49:14 +00005920 */
Daniel Veillard370ba3d2004-10-25 16:23:56 +00005921 nbktext = xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
5922 -1);
Daniel Veillard62f313b2001-07-04 19:49:14 +00005923 if (ent->children->type == XML_TEXT_NODE)
Daniel Veillard370ba3d2004-10-25 16:23:56 +00005924 ent->children->name = nbktext;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005925 if ((ent->last != ent->children) &&
5926 (ent->last->type == XML_TEXT_NODE))
Daniel Veillard370ba3d2004-10-25 16:23:56 +00005927 ent->last->name = nbktext;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005928 xmlAddChildList(ctxt->node, ent->children);
5929 }
5930
Owen Taylor3473f882001-02-23 17:55:21 +00005931 /*
5932 * This is to avoid a nasty side effect, see
5933 * characters() in SAX.c
5934 */
5935 ctxt->nodemem = 0;
5936 ctxt->nodelen = 0;
5937 return;
5938 } else {
5939 /*
5940 * Probably running in SAX mode
5941 */
5942 xmlParserInputPtr input;
5943
5944 input = xmlNewEntityInputStream(ctxt, ent);
5945 xmlPushInput(ctxt, input);
5946 if ((ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) &&
Daniel Veillarda07050d2003-10-19 14:46:32 +00005947 (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) &&
5948 (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00005949 xmlParseTextDecl(ctxt);
5950 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
5951 /*
5952 * The XML REC instructs us to stop parsing right here
5953 */
5954 ctxt->instate = XML_PARSER_EOF;
5955 return;
5956 }
5957 if (input->standalone == 1) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005958 xmlFatalErr(ctxt, XML_ERR_EXT_ENTITY_STANDALONE,
5959 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005960 }
5961 }
5962 return;
5963 }
5964 }
5965 } else {
5966 val = ent->content;
5967 if (val == NULL) return;
5968 /*
5969 * inline the entity.
5970 */
5971 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
5972 (!ctxt->disableSAX))
5973 ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val));
5974 }
5975 }
5976}
5977
5978/**
5979 * xmlParseEntityRef:
5980 * @ctxt: an XML parser context
5981 *
5982 * parse ENTITY references declarations
5983 *
5984 * [68] EntityRef ::= '&' Name ';'
5985 *
5986 * [ WFC: Entity Declared ]
5987 * In a document without any DTD, a document with only an internal DTD
5988 * subset which contains no parameter entity references, or a document
5989 * with "standalone='yes'", the Name given in the entity reference
5990 * must match that in an entity declaration, except that well-formed
5991 * documents need not declare any of the following entities: amp, lt,
5992 * gt, apos, quot. The declaration of a parameter entity must precede
5993 * any reference to it. Similarly, the declaration of a general entity
5994 * must precede any reference to it which appears in a default value in an
5995 * attribute-list declaration. Note that if entities are declared in the
5996 * external subset or in external parameter entities, a non-validating
5997 * processor is not obligated to read and process their declarations;
5998 * for such documents, the rule that an entity must be declared is a
5999 * well-formedness constraint only if standalone='yes'.
6000 *
6001 * [ WFC: Parsed Entity ]
6002 * An entity reference must not contain the name of an unparsed entity
6003 *
6004 * Returns the xmlEntityPtr if found, or NULL otherwise.
6005 */
6006xmlEntityPtr
6007xmlParseEntityRef(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006008 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006009 xmlEntityPtr ent = NULL;
6010
6011 GROW;
6012
6013 if (RAW == '&') {
6014 NEXT;
6015 name = xmlParseName(ctxt);
6016 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006017 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6018 "xmlParseEntityRef: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006019 } else {
6020 if (RAW == ';') {
6021 NEXT;
6022 /*
6023 * Ask first SAX for entity resolution, otherwise try the
6024 * predefined set.
6025 */
6026 if (ctxt->sax != NULL) {
6027 if (ctxt->sax->getEntity != NULL)
6028 ent = ctxt->sax->getEntity(ctxt->userData, name);
Daniel Veillard39eb88b2003-03-11 11:21:28 +00006029 if ((ctxt->wellFormed == 1 ) && (ent == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00006030 ent = xmlGetPredefinedEntity(name);
Daniel Veillard39eb88b2003-03-11 11:21:28 +00006031 if ((ctxt->wellFormed == 1 ) && (ent == NULL) &&
6032 (ctxt->userData==ctxt)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00006033 ent = xmlSAX2GetEntity(ctxt, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00006034 }
Owen Taylor3473f882001-02-23 17:55:21 +00006035 }
6036 /*
6037 * [ WFC: Entity Declared ]
6038 * In a document without any DTD, a document with only an
6039 * internal DTD subset which contains no parameter entity
6040 * references, or a document with "standalone='yes'", the
6041 * Name given in the entity reference must match that in an
6042 * entity declaration, except that well-formed documents
6043 * need not declare any of the following entities: amp, lt,
6044 * gt, apos, quot.
6045 * The declaration of a parameter entity must precede any
6046 * reference to it.
6047 * Similarly, the declaration of a general entity must
6048 * precede any reference to it which appears in a default
6049 * value in an attribute-list declaration. Note that if
6050 * entities are declared in the external subset or in
6051 * external parameter entities, a non-validating processor
6052 * is not obligated to read and process their declarations;
6053 * for such documents, the rule that an entity must be
6054 * declared is a well-formedness constraint only if
6055 * standalone='yes'.
6056 */
6057 if (ent == NULL) {
6058 if ((ctxt->standalone == 1) ||
6059 ((ctxt->hasExternalSubset == 0) &&
6060 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006061 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00006062 "Entity '%s' not defined\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006063 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00006064 xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00006065 "Entity '%s' not defined\n", name);
6066 }
Daniel Veillardf403d292003-10-05 13:51:35 +00006067 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00006068 }
6069
6070 /*
6071 * [ WFC: Parsed Entity ]
6072 * An entity reference must not contain the name of an
6073 * unparsed entity
6074 */
6075 else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006076 xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00006077 "Entity reference to unparsed entity %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006078 }
6079
6080 /*
6081 * [ WFC: No External Entity References ]
6082 * Attribute values cannot contain direct or indirect
6083 * entity references to external entities.
6084 */
6085 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
6086 (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006087 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL,
6088 "Attribute references external entity '%s'\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006089 }
6090 /*
6091 * [ WFC: No < in Attribute Values ]
6092 * The replacement text of any entity referred to directly or
6093 * indirectly in an attribute value (other than "&lt;") must
6094 * not contain a <.
6095 */
6096 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
6097 (ent != NULL) &&
6098 (!xmlStrEqual(ent->name, BAD_CAST "lt")) &&
6099 (ent->content != NULL) &&
6100 (xmlStrchr(ent->content, '<'))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006101 xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
Owen Taylor3473f882001-02-23 17:55:21 +00006102 "'<' in entity '%s' is not allowed in attributes values\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006103 }
6104
6105 /*
6106 * Internal check, no parameter entities here ...
6107 */
6108 else {
6109 switch (ent->etype) {
6110 case XML_INTERNAL_PARAMETER_ENTITY:
6111 case XML_EXTERNAL_PARAMETER_ENTITY:
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006112 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER,
6113 "Attempt to reference the parameter entity '%s'\n",
6114 name);
Owen Taylor3473f882001-02-23 17:55:21 +00006115 break;
6116 default:
6117 break;
6118 }
6119 }
6120
6121 /*
6122 * [ WFC: No Recursion ]
6123 * A parsed entity must not contain a recursive reference
6124 * to itself, either directly or indirectly.
6125 * Done somewhere else
6126 */
6127
6128 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006129 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006130 }
Owen Taylor3473f882001-02-23 17:55:21 +00006131 }
6132 }
6133 return(ent);
6134}
6135
6136/**
6137 * xmlParseStringEntityRef:
6138 * @ctxt: an XML parser context
6139 * @str: a pointer to an index in the string
6140 *
6141 * parse ENTITY references declarations, but this version parses it from
6142 * a string value.
6143 *
6144 * [68] EntityRef ::= '&' Name ';'
6145 *
6146 * [ WFC: Entity Declared ]
6147 * In a document without any DTD, a document with only an internal DTD
6148 * subset which contains no parameter entity references, or a document
6149 * with "standalone='yes'", the Name given in the entity reference
6150 * must match that in an entity declaration, except that well-formed
6151 * documents need not declare any of the following entities: amp, lt,
6152 * gt, apos, quot. The declaration of a parameter entity must precede
6153 * any reference to it. Similarly, the declaration of a general entity
6154 * must precede any reference to it which appears in a default value in an
6155 * attribute-list declaration. Note that if entities are declared in the
6156 * external subset or in external parameter entities, a non-validating
6157 * processor is not obligated to read and process their declarations;
6158 * for such documents, the rule that an entity must be declared is a
6159 * well-formedness constraint only if standalone='yes'.
6160 *
6161 * [ WFC: Parsed Entity ]
6162 * An entity reference must not contain the name of an unparsed entity
6163 *
6164 * Returns the xmlEntityPtr if found, or NULL otherwise. The str pointer
6165 * is updated to the current location in the string.
6166 */
6167xmlEntityPtr
6168xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) {
6169 xmlChar *name;
6170 const xmlChar *ptr;
6171 xmlChar cur;
6172 xmlEntityPtr ent = NULL;
6173
6174 if ((str == NULL) || (*str == NULL))
6175 return(NULL);
6176 ptr = *str;
6177 cur = *ptr;
6178 if (cur == '&') {
6179 ptr++;
6180 cur = *ptr;
6181 name = xmlParseStringName(ctxt, &ptr);
6182 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006183 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6184 "xmlParseStringEntityRef: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006185 } else {
6186 if (*ptr == ';') {
6187 ptr++;
6188 /*
6189 * Ask first SAX for entity resolution, otherwise try the
6190 * predefined set.
6191 */
6192 if (ctxt->sax != NULL) {
6193 if (ctxt->sax->getEntity != NULL)
6194 ent = ctxt->sax->getEntity(ctxt->userData, name);
6195 if (ent == NULL)
6196 ent = xmlGetPredefinedEntity(name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00006197 if ((ent == NULL) && (ctxt->userData==ctxt)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00006198 ent = xmlSAX2GetEntity(ctxt, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00006199 }
Owen Taylor3473f882001-02-23 17:55:21 +00006200 }
6201 /*
6202 * [ WFC: Entity Declared ]
6203 * In a document without any DTD, a document with only an
6204 * internal DTD subset which contains no parameter entity
6205 * references, or a document with "standalone='yes'", the
6206 * Name given in the entity reference must match that in an
6207 * entity declaration, except that well-formed documents
6208 * need not declare any of the following entities: amp, lt,
6209 * gt, apos, quot.
6210 * The declaration of a parameter entity must precede any
6211 * reference to it.
6212 * Similarly, the declaration of a general entity must
6213 * precede any reference to it which appears in a default
6214 * value in an attribute-list declaration. Note that if
6215 * entities are declared in the external subset or in
6216 * external parameter entities, a non-validating processor
6217 * is not obligated to read and process their declarations;
6218 * for such documents, the rule that an entity must be
6219 * declared is a well-formedness constraint only if
6220 * standalone='yes'.
6221 */
6222 if (ent == NULL) {
6223 if ((ctxt->standalone == 1) ||
6224 ((ctxt->hasExternalSubset == 0) &&
6225 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006226 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00006227 "Entity '%s' not defined\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006228 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00006229 xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY,
Daniel Veillard24eb9782003-10-04 21:08:09 +00006230 "Entity '%s' not defined\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00006231 name);
Owen Taylor3473f882001-02-23 17:55:21 +00006232 }
Daniel Veillard24eb9782003-10-04 21:08:09 +00006233 /* TODO ? check regressions ctxt->valid = 0; */
Owen Taylor3473f882001-02-23 17:55:21 +00006234 }
6235
6236 /*
6237 * [ WFC: Parsed Entity ]
6238 * An entity reference must not contain the name of an
6239 * unparsed entity
6240 */
6241 else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006242 xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00006243 "Entity reference to unparsed entity %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006244 }
6245
6246 /*
6247 * [ WFC: No External Entity References ]
6248 * Attribute values cannot contain direct or indirect
6249 * entity references to external entities.
6250 */
6251 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
6252 (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006253 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL,
Owen Taylor3473f882001-02-23 17:55:21 +00006254 "Attribute references external entity '%s'\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006255 }
6256 /*
6257 * [ WFC: No < in Attribute Values ]
6258 * The replacement text of any entity referred to directly or
6259 * indirectly in an attribute value (other than "&lt;") must
6260 * not contain a <.
6261 */
6262 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
6263 (ent != NULL) &&
6264 (!xmlStrEqual(ent->name, BAD_CAST "lt")) &&
6265 (ent->content != NULL) &&
6266 (xmlStrchr(ent->content, '<'))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006267 xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
6268 "'<' in entity '%s' is not allowed in attributes values\n",
6269 name);
Owen Taylor3473f882001-02-23 17:55:21 +00006270 }
6271
6272 /*
6273 * Internal check, no parameter entities here ...
6274 */
6275 else {
6276 switch (ent->etype) {
6277 case XML_INTERNAL_PARAMETER_ENTITY:
6278 case XML_EXTERNAL_PARAMETER_ENTITY:
Daniel Veillardf403d292003-10-05 13:51:35 +00006279 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER,
6280 "Attempt to reference the parameter entity '%s'\n",
6281 name);
Owen Taylor3473f882001-02-23 17:55:21 +00006282 break;
6283 default:
6284 break;
6285 }
6286 }
6287
6288 /*
6289 * [ WFC: No Recursion ]
6290 * A parsed entity must not contain a recursive reference
6291 * to itself, either directly or indirectly.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006292 * Done somewhere else
Owen Taylor3473f882001-02-23 17:55:21 +00006293 */
6294
6295 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006296 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006297 }
6298 xmlFree(name);
6299 }
6300 }
6301 *str = ptr;
6302 return(ent);
6303}
6304
6305/**
6306 * xmlParsePEReference:
6307 * @ctxt: an XML parser context
6308 *
6309 * parse PEReference declarations
6310 * The entity content is handled directly by pushing it's content as
6311 * a new input stream.
6312 *
6313 * [69] PEReference ::= '%' Name ';'
6314 *
6315 * [ WFC: No Recursion ]
6316 * A parsed entity must not contain a recursive
6317 * reference to itself, either directly or indirectly.
6318 *
6319 * [ WFC: Entity Declared ]
6320 * In a document without any DTD, a document with only an internal DTD
6321 * subset which contains no parameter entity references, or a document
6322 * with "standalone='yes'", ... ... The declaration of a parameter
6323 * entity must precede any reference to it...
6324 *
6325 * [ VC: Entity Declared ]
6326 * In a document with an external subset or external parameter entities
6327 * with "standalone='no'", ... ... The declaration of a parameter entity
6328 * must precede any reference to it...
6329 *
6330 * [ WFC: In DTD ]
6331 * Parameter-entity references may only appear in the DTD.
6332 * NOTE: misleading but this is handled.
6333 */
6334void
Daniel Veillard8f597c32003-10-06 08:19:27 +00006335xmlParsePEReference(xmlParserCtxtPtr ctxt)
6336{
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006337 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006338 xmlEntityPtr entity = NULL;
6339 xmlParserInputPtr input;
6340
6341 if (RAW == '%') {
6342 NEXT;
Daniel Veillard76d66f42001-05-16 21:05:17 +00006343 name = xmlParseName(ctxt);
Daniel Veillard8f597c32003-10-06 08:19:27 +00006344 if (name == NULL) {
6345 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6346 "xmlParsePEReference: no name\n");
6347 } else {
6348 if (RAW == ';') {
6349 NEXT;
6350 if ((ctxt->sax != NULL) &&
6351 (ctxt->sax->getParameterEntity != NULL))
6352 entity = ctxt->sax->getParameterEntity(ctxt->userData,
6353 name);
6354 if (entity == NULL) {
6355 /*
6356 * [ WFC: Entity Declared ]
6357 * In a document without any DTD, a document with only an
6358 * internal DTD subset which contains no parameter entity
6359 * references, or a document with "standalone='yes'", ...
6360 * ... The declaration of a parameter entity must precede
6361 * any reference to it...
6362 */
6363 if ((ctxt->standalone == 1) ||
6364 ((ctxt->hasExternalSubset == 0) &&
6365 (ctxt->hasPErefs == 0))) {
6366 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
6367 "PEReference: %%%s; not found\n",
6368 name);
6369 } else {
6370 /*
6371 * [ VC: Entity Declared ]
6372 * In a document with an external subset or external
6373 * parameter entities with "standalone='no'", ...
6374 * ... The declaration of a parameter entity must
6375 * precede any reference to it...
6376 */
6377 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6378 "PEReference: %%%s; not found\n",
6379 name, NULL);
6380 ctxt->valid = 0;
6381 }
6382 } else {
6383 /*
6384 * Internal checking in case the entity quest barfed
6385 */
6386 if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
6387 (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
6388 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6389 "Internal: %%%s; is not a parameter entity\n",
6390 name, NULL);
6391 } else if (ctxt->input->free != deallocblankswrapper) {
6392 input =
6393 xmlNewBlanksWrapperInputStream(ctxt, entity);
6394 xmlPushInput(ctxt, input);
6395 } else {
6396 /*
6397 * TODO !!!
6398 * handle the extra spaces added before and after
6399 * c.f. http://www.w3.org/TR/REC-xml#as-PE
6400 */
6401 input = xmlNewEntityInputStream(ctxt, entity);
6402 xmlPushInput(ctxt, input);
6403 if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) &&
Daniel Veillarda07050d2003-10-19 14:46:32 +00006404 (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) &&
William M. Brack76e95df2003-10-18 16:20:14 +00006405 (IS_BLANK_CH(NXT(5)))) {
Daniel Veillard8f597c32003-10-06 08:19:27 +00006406 xmlParseTextDecl(ctxt);
6407 if (ctxt->errNo ==
6408 XML_ERR_UNSUPPORTED_ENCODING) {
6409 /*
6410 * The XML REC instructs us to stop parsing
6411 * right here
6412 */
6413 ctxt->instate = XML_PARSER_EOF;
6414 return;
6415 }
6416 }
6417 }
6418 }
6419 ctxt->hasPErefs = 1;
6420 } else {
6421 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
6422 }
6423 }
Owen Taylor3473f882001-02-23 17:55:21 +00006424 }
6425}
6426
6427/**
6428 * xmlParseStringPEReference:
6429 * @ctxt: an XML parser context
6430 * @str: a pointer to an index in the string
6431 *
6432 * parse PEReference declarations
6433 *
6434 * [69] PEReference ::= '%' Name ';'
6435 *
6436 * [ WFC: No Recursion ]
6437 * A parsed entity must not contain a recursive
6438 * reference to itself, either directly or indirectly.
6439 *
6440 * [ WFC: Entity Declared ]
6441 * In a document without any DTD, a document with only an internal DTD
6442 * subset which contains no parameter entity references, or a document
6443 * with "standalone='yes'", ... ... The declaration of a parameter
6444 * entity must precede any reference to it...
6445 *
6446 * [ VC: Entity Declared ]
6447 * In a document with an external subset or external parameter entities
6448 * with "standalone='no'", ... ... The declaration of a parameter entity
6449 * must precede any reference to it...
6450 *
6451 * [ WFC: In DTD ]
6452 * Parameter-entity references may only appear in the DTD.
6453 * NOTE: misleading but this is handled.
6454 *
6455 * Returns the string of the entity content.
6456 * str is updated to the current value of the index
6457 */
6458xmlEntityPtr
6459xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) {
6460 const xmlChar *ptr;
6461 xmlChar cur;
6462 xmlChar *name;
6463 xmlEntityPtr entity = NULL;
6464
6465 if ((str == NULL) || (*str == NULL)) return(NULL);
6466 ptr = *str;
6467 cur = *ptr;
6468 if (cur == '%') {
6469 ptr++;
6470 cur = *ptr;
6471 name = xmlParseStringName(ctxt, &ptr);
6472 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006473 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6474 "xmlParseStringPEReference: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006475 } else {
6476 cur = *ptr;
6477 if (cur == ';') {
6478 ptr++;
6479 cur = *ptr;
6480 if ((ctxt->sax != NULL) &&
6481 (ctxt->sax->getParameterEntity != NULL))
6482 entity = ctxt->sax->getParameterEntity(ctxt->userData,
6483 name);
6484 if (entity == NULL) {
6485 /*
6486 * [ WFC: Entity Declared ]
6487 * In a document without any DTD, a document with only an
6488 * internal DTD subset which contains no parameter entity
6489 * references, or a document with "standalone='yes'", ...
6490 * ... The declaration of a parameter entity must precede
6491 * any reference to it...
6492 */
6493 if ((ctxt->standalone == 1) ||
6494 ((ctxt->hasExternalSubset == 0) &&
6495 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006496 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00006497 "PEReference: %%%s; not found\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006498 } else {
6499 /*
6500 * [ VC: Entity Declared ]
6501 * In a document with an external subset or external
6502 * parameter entities with "standalone='no'", ...
6503 * ... The declaration of a parameter entity must
6504 * precede any reference to it...
6505 */
Daniel Veillard24eb9782003-10-04 21:08:09 +00006506 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6507 "PEReference: %%%s; not found\n",
6508 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006509 ctxt->valid = 0;
6510 }
6511 } else {
6512 /*
6513 * Internal checking in case the entity quest barfed
6514 */
6515 if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
6516 (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00006517 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6518 "%%%s; is not a parameter entity\n",
6519 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006520 }
6521 }
6522 ctxt->hasPErefs = 1;
6523 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006524 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006525 }
6526 xmlFree(name);
6527 }
6528 }
6529 *str = ptr;
6530 return(entity);
6531}
6532
6533/**
6534 * xmlParseDocTypeDecl:
6535 * @ctxt: an XML parser context
6536 *
6537 * parse a DOCTYPE declaration
6538 *
6539 * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
6540 * ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
6541 *
6542 * [ VC: Root Element Type ]
6543 * The Name in the document type declaration must match the element
6544 * type of the root element.
6545 */
6546
6547void
6548xmlParseDocTypeDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006549 const xmlChar *name = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00006550 xmlChar *ExternalID = NULL;
6551 xmlChar *URI = NULL;
6552
6553 /*
6554 * We know that '<!DOCTYPE' has been detected.
6555 */
6556 SKIP(9);
6557
6558 SKIP_BLANKS;
6559
6560 /*
6561 * Parse the DOCTYPE name.
6562 */
6563 name = xmlParseName(ctxt);
6564 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006565 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6566 "xmlParseDocTypeDecl : no DOCTYPE name !\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006567 }
6568 ctxt->intSubName = name;
6569
6570 SKIP_BLANKS;
6571
6572 /*
6573 * Check for SystemID and ExternalID
6574 */
6575 URI = xmlParseExternalID(ctxt, &ExternalID, 1);
6576
6577 if ((URI != NULL) || (ExternalID != NULL)) {
6578 ctxt->hasExternalSubset = 1;
6579 }
6580 ctxt->extSubURI = URI;
6581 ctxt->extSubSystem = ExternalID;
6582
6583 SKIP_BLANKS;
6584
6585 /*
6586 * Create and update the internal subset.
6587 */
6588 if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
6589 (!ctxt->disableSAX))
6590 ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
6591
6592 /*
6593 * Is there any internal subset declarations ?
6594 * they are handled separately in xmlParseInternalSubset()
6595 */
6596 if (RAW == '[')
6597 return;
6598
6599 /*
6600 * We should be at the end of the DOCTYPE declaration.
6601 */
6602 if (RAW != '>') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006603 xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006604 }
6605 NEXT;
6606}
6607
6608/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006609 * xmlParseInternalSubset:
Owen Taylor3473f882001-02-23 17:55:21 +00006610 * @ctxt: an XML parser context
6611 *
6612 * parse the internal subset declaration
6613 *
6614 * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
6615 */
6616
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006617static void
Owen Taylor3473f882001-02-23 17:55:21 +00006618xmlParseInternalSubset(xmlParserCtxtPtr ctxt) {
6619 /*
6620 * Is there any DTD definition ?
6621 */
6622 if (RAW == '[') {
6623 ctxt->instate = XML_PARSER_DTD;
6624 NEXT;
6625 /*
6626 * Parse the succession of Markup declarations and
6627 * PEReferences.
6628 * Subsequence (markupdecl | PEReference | S)*
6629 */
6630 while (RAW != ']') {
6631 const xmlChar *check = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00006632 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00006633
6634 SKIP_BLANKS;
6635 xmlParseMarkupDecl(ctxt);
6636 xmlParsePEReference(ctxt);
6637
6638 /*
6639 * Pop-up of finished entities.
6640 */
6641 while ((RAW == 0) && (ctxt->inputNr > 1))
6642 xmlPopInput(ctxt);
6643
6644 if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006645 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006646 "xmlParseInternalSubset: error detected in Markup declaration\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006647 break;
6648 }
6649 }
6650 if (RAW == ']') {
6651 NEXT;
6652 SKIP_BLANKS;
6653 }
6654 }
6655
6656 /*
6657 * We should be at the end of the DOCTYPE declaration.
6658 */
6659 if (RAW != '>') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006660 xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006661 }
6662 NEXT;
6663}
6664
Daniel Veillard81273902003-09-30 00:43:48 +00006665#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00006666/**
6667 * xmlParseAttribute:
6668 * @ctxt: an XML parser context
6669 * @value: a xmlChar ** used to store the value of the attribute
6670 *
6671 * parse an attribute
6672 *
6673 * [41] Attribute ::= Name Eq AttValue
6674 *
6675 * [ WFC: No External Entity References ]
6676 * Attribute values cannot contain direct or indirect entity references
6677 * to external entities.
6678 *
6679 * [ WFC: No < in Attribute Values ]
6680 * The replacement text of any entity referred to directly or indirectly in
6681 * an attribute value (other than "&lt;") must not contain a <.
6682 *
6683 * [ VC: Attribute Value Type ]
6684 * The attribute must have been declared; the value must be of the type
6685 * declared for it.
6686 *
6687 * [25] Eq ::= S? '=' S?
6688 *
6689 * With namespace:
6690 *
6691 * [NS 11] Attribute ::= QName Eq AttValue
6692 *
6693 * Also the case QName == xmlns:??? is handled independently as a namespace
6694 * definition.
6695 *
6696 * Returns the attribute name, and the value in *value.
6697 */
6698
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006699const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006700xmlParseAttribute(xmlParserCtxtPtr ctxt, xmlChar **value) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006701 const xmlChar *name;
6702 xmlChar *val;
Owen Taylor3473f882001-02-23 17:55:21 +00006703
6704 *value = NULL;
Daniel Veillard878eab02002-02-19 13:46:09 +00006705 GROW;
Owen Taylor3473f882001-02-23 17:55:21 +00006706 name = xmlParseName(ctxt);
6707 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006708 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006709 "error parsing attribute name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006710 return(NULL);
6711 }
6712
6713 /*
6714 * read the value
6715 */
6716 SKIP_BLANKS;
6717 if (RAW == '=') {
6718 NEXT;
6719 SKIP_BLANKS;
6720 val = xmlParseAttValue(ctxt);
6721 ctxt->instate = XML_PARSER_CONTENT;
6722 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006723 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Owen Taylor3473f882001-02-23 17:55:21 +00006724 "Specification mandate value for attribute %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006725 return(NULL);
6726 }
6727
6728 /*
6729 * Check that xml:lang conforms to the specification
6730 * No more registered as an error, just generate a warning now
6731 * since this was deprecated in XML second edition
6732 */
6733 if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) {
6734 if (!xmlCheckLanguageID(val)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00006735 xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
6736 "Malformed value for xml:lang : %s\n",
6737 val, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006738 }
6739 }
6740
6741 /*
6742 * Check that xml:space conforms to the specification
6743 */
6744 if (xmlStrEqual(name, BAD_CAST "xml:space")) {
6745 if (xmlStrEqual(val, BAD_CAST "default"))
6746 *(ctxt->space) = 0;
6747 else if (xmlStrEqual(val, BAD_CAST "preserve"))
6748 *(ctxt->space) = 1;
6749 else {
Daniel Veillardd8925572005-06-08 22:34:55 +00006750 xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE,
Daniel Veillard642104e2003-03-26 16:32:05 +00006751"Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
Daniel Veillardd8925572005-06-08 22:34:55 +00006752 val, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006753 }
6754 }
6755
6756 *value = val;
6757 return(name);
6758}
6759
6760/**
6761 * xmlParseStartTag:
6762 * @ctxt: an XML parser context
6763 *
6764 * parse a start of tag either for rule element or
6765 * EmptyElement. In both case we don't parse the tag closing chars.
6766 *
6767 * [40] STag ::= '<' Name (S Attribute)* S? '>'
6768 *
6769 * [ WFC: Unique Att Spec ]
6770 * No attribute name may appear more than once in the same start-tag or
6771 * empty-element tag.
6772 *
6773 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
6774 *
6775 * [ WFC: Unique Att Spec ]
6776 * No attribute name may appear more than once in the same start-tag or
6777 * empty-element tag.
6778 *
6779 * With namespace:
6780 *
6781 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
6782 *
6783 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
6784 *
6785 * Returns the element name parsed
6786 */
6787
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006788const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006789xmlParseStartTag(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006790 const xmlChar *name;
6791 const xmlChar *attname;
Owen Taylor3473f882001-02-23 17:55:21 +00006792 xmlChar *attvalue;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006793 const xmlChar **atts = ctxt->atts;
Owen Taylor3473f882001-02-23 17:55:21 +00006794 int nbatts = 0;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006795 int maxatts = ctxt->maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00006796 int i;
6797
6798 if (RAW != '<') return(NULL);
Daniel Veillard21a0f912001-02-25 19:54:14 +00006799 NEXT1;
Owen Taylor3473f882001-02-23 17:55:21 +00006800
6801 name = xmlParseName(ctxt);
6802 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006803 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00006804 "xmlParseStartTag: invalid element name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006805 return(NULL);
6806 }
6807
6808 /*
6809 * Now parse the attributes, it ends up with the ending
6810 *
6811 * (S Attribute)* S?
6812 */
6813 SKIP_BLANKS;
6814 GROW;
6815
Daniel Veillard21a0f912001-02-25 19:54:14 +00006816 while ((RAW != '>') &&
6817 ((RAW != '/') || (NXT(1) != '>')) &&
Daniel Veillard73b013f2003-09-30 12:36:01 +00006818 (IS_BYTE_CHAR(RAW))) {
Owen Taylor3473f882001-02-23 17:55:21 +00006819 const xmlChar *q = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00006820 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00006821
6822 attname = xmlParseAttribute(ctxt, &attvalue);
6823 if ((attname != NULL) && (attvalue != NULL)) {
6824 /*
6825 * [ WFC: Unique Att Spec ]
6826 * No attribute name may appear more than once in the same
6827 * start-tag or empty-element tag.
6828 */
6829 for (i = 0; i < nbatts;i += 2) {
6830 if (xmlStrEqual(atts[i], attname)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006831 xmlErrAttributeDup(ctxt, NULL, attname);
Owen Taylor3473f882001-02-23 17:55:21 +00006832 xmlFree(attvalue);
6833 goto failed;
6834 }
6835 }
Owen Taylor3473f882001-02-23 17:55:21 +00006836 /*
6837 * Add the pair to atts
6838 */
6839 if (atts == NULL) {
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006840 maxatts = 22; /* allow for 10 attrs by default */
6841 atts = (const xmlChar **)
6842 xmlMalloc(maxatts * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00006843 if (atts == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006844 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006845 if (attvalue != NULL)
6846 xmlFree(attvalue);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006847 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00006848 }
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006849 ctxt->atts = atts;
6850 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00006851 } else if (nbatts + 4 > maxatts) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006852 const xmlChar **n;
6853
Owen Taylor3473f882001-02-23 17:55:21 +00006854 maxatts *= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006855 n = (const xmlChar **) xmlRealloc((void *) atts,
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006856 maxatts * sizeof(const xmlChar *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006857 if (n == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006858 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006859 if (attvalue != NULL)
6860 xmlFree(attvalue);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006861 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00006862 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006863 atts = n;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006864 ctxt->atts = atts;
6865 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00006866 }
6867 atts[nbatts++] = attname;
6868 atts[nbatts++] = attvalue;
6869 atts[nbatts] = NULL;
6870 atts[nbatts + 1] = NULL;
6871 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00006872 if (attvalue != NULL)
6873 xmlFree(attvalue);
6874 }
6875
6876failed:
6877
Daniel Veillard3772de32002-12-17 10:31:45 +00006878 GROW
Owen Taylor3473f882001-02-23 17:55:21 +00006879 if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
6880 break;
William M. Brack76e95df2003-10-18 16:20:14 +00006881 if (!IS_BLANK_CH(RAW)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006882 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6883 "attributes construct error\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006884 }
6885 SKIP_BLANKS;
Daniel Veillard02111c12003-02-24 19:14:52 +00006886 if ((cons == ctxt->input->consumed) && (q == CUR_PTR) &&
6887 (attname == NULL) && (attvalue == NULL)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006888 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
6889 "xmlParseStartTag: problem parsing attributes\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006890 break;
6891 }
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006892 SHRINK;
Owen Taylor3473f882001-02-23 17:55:21 +00006893 GROW;
6894 }
6895
6896 /*
6897 * SAX: Start of Element !
6898 */
6899 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) &&
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006900 (!ctxt->disableSAX)) {
6901 if (nbatts > 0)
6902 ctxt->sax->startElement(ctxt->userData, name, atts);
6903 else
6904 ctxt->sax->startElement(ctxt->userData, name, NULL);
6905 }
Owen Taylor3473f882001-02-23 17:55:21 +00006906
6907 if (atts != NULL) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006908 /* Free only the content strings */
6909 for (i = 1;i < nbatts;i+=2)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006910 if (atts[i] != NULL)
6911 xmlFree((xmlChar *) atts[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00006912 }
6913 return(name);
6914}
6915
6916/**
Daniel Veillard0fb18932003-09-07 09:14:37 +00006917 * xmlParseEndTag1:
Owen Taylor3473f882001-02-23 17:55:21 +00006918 * @ctxt: an XML parser context
Daniel Veillard0fb18932003-09-07 09:14:37 +00006919 * @line: line of the start tag
6920 * @nsNr: number of namespaces on the start tag
Owen Taylor3473f882001-02-23 17:55:21 +00006921 *
6922 * parse an end of tag
6923 *
6924 * [42] ETag ::= '</' Name S? '>'
6925 *
6926 * With namespace
6927 *
6928 * [NS 9] ETag ::= '</' QName S? '>'
6929 */
6930
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00006931static void
Daniel Veillard0fb18932003-09-07 09:14:37 +00006932xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006933 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006934
6935 GROW;
6936 if ((RAW != '<') || (NXT(1) != '/')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006937 xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006938 "xmlParseEndTag: '</' not found\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006939 return;
6940 }
6941 SKIP(2);
6942
Daniel Veillard46de64e2002-05-29 08:21:33 +00006943 name = xmlParseNameAndCompare(ctxt,ctxt->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006944
6945 /*
6946 * We should definitely be at the ending "S? '>'" part
6947 */
6948 GROW;
6949 SKIP_BLANKS;
Daniel Veillard73b013f2003-09-30 12:36:01 +00006950 if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006951 xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006952 } else
Daniel Veillard21a0f912001-02-25 19:54:14 +00006953 NEXT1;
Owen Taylor3473f882001-02-23 17:55:21 +00006954
6955 /*
6956 * [ WFC: Element Type Match ]
6957 * The Name in an element's end-tag must match the element type in the
6958 * start-tag.
6959 *
6960 */
Daniel Veillard46de64e2002-05-29 08:21:33 +00006961 if (name != (xmlChar*)1) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006962 if (name == NULL) name = BAD_CAST "unparseable";
6963 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00006964 "Opening and ending tag mismatch: %s line %d and %s\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00006965 ctxt->name, line, name);
Owen Taylor3473f882001-02-23 17:55:21 +00006966 }
6967
6968 /*
6969 * SAX: End of Tag
6970 */
6971 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
6972 (!ctxt->disableSAX))
Daniel Veillard46de64e2002-05-29 08:21:33 +00006973 ctxt->sax->endElement(ctxt->userData, ctxt->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006974
Daniel Veillarde57ec792003-09-10 10:50:59 +00006975 namePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006976 spacePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006977 return;
6978}
6979
6980/**
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00006981 * xmlParseEndTag:
6982 * @ctxt: an XML parser context
6983 *
6984 * parse an end of tag
6985 *
6986 * [42] ETag ::= '</' Name S? '>'
6987 *
6988 * With namespace
6989 *
6990 * [NS 9] ETag ::= '</' QName S? '>'
6991 */
6992
6993void
6994xmlParseEndTag(xmlParserCtxtPtr ctxt) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00006995 xmlParseEndTag1(ctxt, 0);
6996}
Daniel Veillard81273902003-09-30 00:43:48 +00006997#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard0fb18932003-09-07 09:14:37 +00006998
6999/************************************************************************
7000 * *
7001 * SAX 2 specific operations *
7002 * *
7003 ************************************************************************/
7004
7005static const xmlChar *
7006xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) {
7007 int len = 0, l;
7008 int c;
7009 int count = 0;
7010
7011 /*
7012 * Handler for more complex cases
7013 */
7014 GROW;
7015 c = CUR_CHAR(l);
7016 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007017 (!IS_LETTER(c) && (c != '_'))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007018 return(NULL);
7019 }
7020
7021 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
William M. Brack871611b2003-10-18 04:53:14 +00007022 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007023 (c == '.') || (c == '-') || (c == '_') ||
William M. Brack871611b2003-10-18 04:53:14 +00007024 (IS_COMBINING(c)) ||
7025 (IS_EXTENDER(c)))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007026 if (count++ > 100) {
7027 count = 0;
7028 GROW;
7029 }
7030 len += l;
7031 NEXTL(l);
7032 c = CUR_CHAR(l);
7033 }
7034 return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len));
7035}
7036
7037/*
7038 * xmlGetNamespace:
7039 * @ctxt: an XML parser context
7040 * @prefix: the prefix to lookup
7041 *
7042 * Lookup the namespace name for the @prefix (which ca be NULL)
7043 * The prefix must come from the @ctxt->dict dictionnary
7044 *
7045 * Returns the namespace name or NULL if not bound
7046 */
7047static const xmlChar *
7048xmlGetNamespace(xmlParserCtxtPtr ctxt, const xmlChar *prefix) {
7049 int i;
7050
Daniel Veillarde57ec792003-09-10 10:50:59 +00007051 if (prefix == ctxt->str_xml) return(ctxt->str_xml_ns);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007052 for (i = ctxt->nsNr - 2;i >= 0;i-=2)
Daniel Veillarde57ec792003-09-10 10:50:59 +00007053 if (ctxt->nsTab[i] == prefix) {
7054 if ((prefix == NULL) && (*ctxt->nsTab[i + 1] == 0))
7055 return(NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007056 return(ctxt->nsTab[i + 1]);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007057 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007058 return(NULL);
7059}
7060
7061/**
7062 * xmlParseNCName:
7063 * @ctxt: an XML parser context
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007064 * @len: lenght of the string parsed
Daniel Veillard0fb18932003-09-07 09:14:37 +00007065 *
7066 * parse an XML name.
7067 *
7068 * [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' |
7069 * CombiningChar | Extender
7070 *
7071 * [5NS] NCName ::= (Letter | '_') (NCNameChar)*
7072 *
7073 * Returns the Name parsed or NULL
7074 */
7075
7076static const xmlChar *
7077xmlParseNCName(xmlParserCtxtPtr ctxt) {
7078 const xmlChar *in;
7079 const xmlChar *ret;
7080 int count = 0;
7081
7082 /*
7083 * Accelerator for simple ASCII names
7084 */
7085 in = ctxt->input->cur;
7086 if (((*in >= 0x61) && (*in <= 0x7A)) ||
7087 ((*in >= 0x41) && (*in <= 0x5A)) ||
7088 (*in == '_')) {
7089 in++;
7090 while (((*in >= 0x61) && (*in <= 0x7A)) ||
7091 ((*in >= 0x41) && (*in <= 0x5A)) ||
7092 ((*in >= 0x30) && (*in <= 0x39)) ||
7093 (*in == '_') || (*in == '-') ||
7094 (*in == '.'))
7095 in++;
7096 if ((*in > 0) && (*in < 0x80)) {
7097 count = in - ctxt->input->cur;
7098 ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
7099 ctxt->input->cur = in;
7100 ctxt->nbChars += count;
7101 ctxt->input->col += count;
7102 if (ret == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007103 xmlErrMemory(ctxt, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007104 }
7105 return(ret);
7106 }
7107 }
7108 return(xmlParseNCNameComplex(ctxt));
7109}
7110
7111/**
7112 * xmlParseQName:
7113 * @ctxt: an XML parser context
7114 * @prefix: pointer to store the prefix part
7115 *
7116 * parse an XML Namespace QName
7117 *
7118 * [6] QName ::= (Prefix ':')? LocalPart
7119 * [7] Prefix ::= NCName
7120 * [8] LocalPart ::= NCName
7121 *
7122 * Returns the Name parsed or NULL
7123 */
7124
7125static const xmlChar *
7126xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) {
7127 const xmlChar *l, *p;
7128
7129 GROW;
7130
7131 l = xmlParseNCName(ctxt);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007132 if (l == NULL) {
7133 if (CUR == ':') {
7134 l = xmlParseName(ctxt);
7135 if (l != NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007136 xmlNsErr(ctxt, XML_NS_ERR_QNAME,
7137 "Failed to parse QName '%s'\n", l, NULL, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007138 *prefix = NULL;
7139 return(l);
7140 }
7141 }
7142 return(NULL);
7143 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007144 if (CUR == ':') {
7145 NEXT;
7146 p = l;
7147 l = xmlParseNCName(ctxt);
7148 if (l == NULL) {
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007149 xmlChar *tmp;
7150
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007151 xmlNsErr(ctxt, XML_NS_ERR_QNAME,
7152 "Failed to parse QName '%s:'\n", p, NULL, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007153 tmp = xmlBuildQName(BAD_CAST "", p, NULL, 0);
7154 p = xmlDictLookup(ctxt->dict, tmp, -1);
7155 if (tmp != NULL) xmlFree(tmp);
7156 *prefix = NULL;
7157 return(p);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007158 }
7159 if (CUR == ':') {
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007160 xmlChar *tmp;
7161
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007162 xmlNsErr(ctxt, XML_NS_ERR_QNAME,
7163 "Failed to parse QName '%s:%s:'\n", p, l, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007164 NEXT;
7165 tmp = (xmlChar *) xmlParseName(ctxt);
7166 if (tmp != NULL) {
7167 tmp = xmlBuildQName(tmp, l, NULL, 0);
7168 l = xmlDictLookup(ctxt->dict, tmp, -1);
7169 if (tmp != NULL) xmlFree(tmp);
7170 *prefix = p;
7171 return(l);
7172 }
7173 tmp = xmlBuildQName(BAD_CAST "", l, NULL, 0);
7174 l = xmlDictLookup(ctxt->dict, tmp, -1);
7175 if (tmp != NULL) xmlFree(tmp);
7176 *prefix = p;
7177 return(l);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007178 }
7179 *prefix = p;
7180 } else
7181 *prefix = NULL;
7182 return(l);
7183}
7184
7185/**
7186 * xmlParseQNameAndCompare:
7187 * @ctxt: an XML parser context
7188 * @name: the localname
7189 * @prefix: the prefix, if any.
7190 *
7191 * parse an XML name and compares for match
7192 * (specialized for endtag parsing)
7193 *
7194 * Returns NULL for an illegal name, (xmlChar*) 1 for success
7195 * and the name for mismatch
7196 */
7197
7198static const xmlChar *
7199xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name,
7200 xmlChar const *prefix) {
7201 const xmlChar *cmp = name;
7202 const xmlChar *in;
7203 const xmlChar *ret;
7204 const xmlChar *prefix2;
7205
7206 if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name));
7207
7208 GROW;
7209 in = ctxt->input->cur;
7210
7211 cmp = prefix;
7212 while (*in != 0 && *in == *cmp) {
7213 ++in;
7214 ++cmp;
7215 }
7216 if ((*cmp == 0) && (*in == ':')) {
7217 in++;
7218 cmp = name;
7219 while (*in != 0 && *in == *cmp) {
7220 ++in;
7221 ++cmp;
7222 }
William M. Brack76e95df2003-10-18 16:20:14 +00007223 if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007224 /* success */
7225 ctxt->input->cur = in;
7226 return((const xmlChar*) 1);
7227 }
7228 }
7229 /*
7230 * all strings coms from the dictionary, equality can be done directly
7231 */
7232 ret = xmlParseQName (ctxt, &prefix2);
7233 if ((ret == name) && (prefix == prefix2))
7234 return((const xmlChar*) 1);
7235 return ret;
7236}
7237
7238/**
7239 * xmlParseAttValueInternal:
7240 * @ctxt: an XML parser context
7241 * @len: attribute len result
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007242 * @alloc: whether the attribute was reallocated as a new string
7243 * @normalize: if 1 then further non-CDATA normalization must be done
Daniel Veillard0fb18932003-09-07 09:14:37 +00007244 *
7245 * parse a value for an attribute.
7246 * NOTE: if no normalization is needed, the routine will return pointers
7247 * directly from the data buffer.
7248 *
7249 * 3.3.3 Attribute-Value Normalization:
7250 * Before the value of an attribute is passed to the application or
7251 * checked for validity, the XML processor must normalize it as follows:
7252 * - a character reference is processed by appending the referenced
7253 * character to the attribute value
7254 * - an entity reference is processed by recursively processing the
7255 * replacement text of the entity
7256 * - a whitespace character (#x20, #xD, #xA, #x9) is processed by
7257 * appending #x20 to the normalized value, except that only a single
7258 * #x20 is appended for a "#xD#xA" sequence that is part of an external
7259 * parsed entity or the literal entity value of an internal parsed entity
7260 * - other characters are processed by appending them to the normalized value
7261 * If the declared value is not CDATA, then the XML processor must further
7262 * process the normalized attribute value by discarding any leading and
7263 * trailing space (#x20) characters, and by replacing sequences of space
7264 * (#x20) characters by a single space (#x20) character.
7265 * All attributes for which no declaration has been read should be treated
7266 * by a non-validating parser as if declared CDATA.
7267 *
7268 * Returns the AttValue parsed or NULL. The value has to be freed by the
7269 * caller if it was copied, this can be detected by val[*len] == 0.
7270 */
7271
7272static xmlChar *
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007273xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc,
7274 int normalize)
Daniel Veillarde57ec792003-09-10 10:50:59 +00007275{
Daniel Veillard0fb18932003-09-07 09:14:37 +00007276 xmlChar limit = 0;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007277 const xmlChar *in = NULL, *start, *end, *last;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007278 xmlChar *ret = NULL;
7279
7280 GROW;
7281 in = (xmlChar *) CUR_PTR;
7282 if (*in != '"' && *in != '\'') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007283 xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007284 return (NULL);
7285 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007286 ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007287
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007288 /*
7289 * try to handle in this routine the most common case where no
7290 * allocation of a new string is required and where content is
7291 * pure ASCII.
7292 */
7293 limit = *in++;
7294 end = ctxt->input->end;
7295 start = in;
7296 if (in >= end) {
7297 const xmlChar *oldbase = ctxt->input->base;
7298 GROW;
7299 if (oldbase != ctxt->input->base) {
7300 long delta = ctxt->input->base - oldbase;
7301 start = start + delta;
7302 in = in + delta;
7303 }
7304 end = ctxt->input->end;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007305 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007306 if (normalize) {
7307 /*
7308 * Skip any leading spaces
7309 */
7310 while ((in < end) && (*in != limit) &&
7311 ((*in == 0x20) || (*in == 0x9) ||
7312 (*in == 0xA) || (*in == 0xD))) {
7313 in++;
7314 start = in;
7315 if (in >= end) {
7316 const xmlChar *oldbase = ctxt->input->base;
7317 GROW;
7318 if (oldbase != ctxt->input->base) {
7319 long delta = ctxt->input->base - oldbase;
7320 start = start + delta;
7321 in = in + delta;
7322 }
7323 end = ctxt->input->end;
7324 }
7325 }
7326 while ((in < end) && (*in != limit) && (*in >= 0x20) &&
7327 (*in <= 0x7f) && (*in != '&') && (*in != '<')) {
7328 if ((*in++ == 0x20) && (*in == 0x20)) break;
7329 if (in >= end) {
7330 const xmlChar *oldbase = ctxt->input->base;
7331 GROW;
7332 if (oldbase != ctxt->input->base) {
7333 long delta = ctxt->input->base - oldbase;
7334 start = start + delta;
7335 in = in + delta;
7336 }
7337 end = ctxt->input->end;
7338 }
7339 }
7340 last = in;
7341 /*
7342 * skip the trailing blanks
7343 */
Daniel Veillardc6e20e42003-09-11 16:30:26 +00007344 while ((last[-1] == 0x20) && (last > start)) last--;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007345 while ((in < end) && (*in != limit) &&
7346 ((*in == 0x20) || (*in == 0x9) ||
7347 (*in == 0xA) || (*in == 0xD))) {
7348 in++;
7349 if (in >= end) {
7350 const xmlChar *oldbase = ctxt->input->base;
7351 GROW;
7352 if (oldbase != ctxt->input->base) {
7353 long delta = ctxt->input->base - oldbase;
7354 start = start + delta;
7355 in = in + delta;
7356 last = last + delta;
7357 }
7358 end = ctxt->input->end;
7359 }
7360 }
7361 if (*in != limit) goto need_complex;
7362 } else {
7363 while ((in < end) && (*in != limit) && (*in >= 0x20) &&
7364 (*in <= 0x7f) && (*in != '&') && (*in != '<')) {
7365 in++;
7366 if (in >= end) {
7367 const xmlChar *oldbase = ctxt->input->base;
7368 GROW;
7369 if (oldbase != ctxt->input->base) {
7370 long delta = ctxt->input->base - oldbase;
7371 start = start + delta;
7372 in = in + delta;
7373 }
7374 end = ctxt->input->end;
7375 }
7376 }
7377 last = in;
7378 if (*in != limit) goto need_complex;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007379 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007380 in++;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007381 if (len != NULL) {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007382 *len = last - start;
7383 ret = (xmlChar *) start;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007384 } else {
Daniel Veillarde57ec792003-09-10 10:50:59 +00007385 if (alloc) *alloc = 1;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007386 ret = xmlStrndup(start, last - start);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007387 }
7388 CUR_PTR = in;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007389 if (alloc) *alloc = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007390 return ret;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007391need_complex:
7392 if (alloc) *alloc = 1;
7393 return xmlParseAttValueComplex(ctxt, len, normalize);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007394}
7395
7396/**
7397 * xmlParseAttribute2:
7398 * @ctxt: an XML parser context
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007399 * @pref: the element prefix
7400 * @elem: the element name
7401 * @prefix: a xmlChar ** used to store the value of the attribute prefix
Daniel Veillard0fb18932003-09-07 09:14:37 +00007402 * @value: a xmlChar ** used to store the value of the attribute
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007403 * @len: an int * to save the length of the attribute
7404 * @alloc: an int * to indicate if the attribute was allocated
Daniel Veillard0fb18932003-09-07 09:14:37 +00007405 *
7406 * parse an attribute in the new SAX2 framework.
7407 *
7408 * Returns the attribute name, and the value in *value, .
7409 */
7410
7411static const xmlChar *
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007412xmlParseAttribute2(xmlParserCtxtPtr ctxt,
7413 const xmlChar *pref, const xmlChar *elem,
7414 const xmlChar **prefix, xmlChar **value,
7415 int *len, int *alloc) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007416 const xmlChar *name;
Daniel Veillardd8925572005-06-08 22:34:55 +00007417 xmlChar *val, *internal_val = NULL;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007418 int normalize = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007419
7420 *value = NULL;
7421 GROW;
7422 name = xmlParseQName(ctxt, prefix);
7423 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007424 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7425 "error parsing attribute name\n");
Daniel Veillard0fb18932003-09-07 09:14:37 +00007426 return(NULL);
7427 }
7428
7429 /*
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007430 * get the type if needed
7431 */
7432 if (ctxt->attsSpecial != NULL) {
7433 int type;
7434
7435 type = (int) (long) xmlHashQLookup2(ctxt->attsSpecial,
7436 pref, elem, *prefix, name);
7437 if (type != 0) normalize = 1;
7438 }
7439
7440 /*
Daniel Veillard0fb18932003-09-07 09:14:37 +00007441 * read the value
7442 */
7443 SKIP_BLANKS;
7444 if (RAW == '=') {
7445 NEXT;
7446 SKIP_BLANKS;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007447 val = xmlParseAttValueInternal(ctxt, len, alloc, normalize);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007448 ctxt->instate = XML_PARSER_CONTENT;
7449 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00007450 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007451 "Specification mandate value for attribute %s\n", name);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007452 return(NULL);
7453 }
7454
Daniel Veillardd8925572005-06-08 22:34:55 +00007455 if (*prefix == ctxt->str_xml) {
7456 /*
7457 * Check that xml:lang conforms to the specification
7458 * No more registered as an error, just generate a warning now
7459 * since this was deprecated in XML second edition
7460 */
7461 if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "lang"))) {
7462 internal_val = xmlStrndup(val, *len);
7463 if (!xmlCheckLanguageID(internal_val)) {
7464 xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
7465 "Malformed value for xml:lang : %s\n",
7466 internal_val, NULL);
7467 }
7468 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007469
Daniel Veillardd8925572005-06-08 22:34:55 +00007470 /*
7471 * Check that xml:space conforms to the specification
7472 */
7473 if (xmlStrEqual(name, BAD_CAST "space")) {
7474 internal_val = xmlStrndup(val, *len);
7475 if (xmlStrEqual(internal_val, BAD_CAST "default"))
7476 *(ctxt->space) = 0;
7477 else if (xmlStrEqual(internal_val, BAD_CAST "preserve"))
7478 *(ctxt->space) = 1;
7479 else {
7480 xmlWarningMsg(ctxt, XML_WAR_SPACE_VALUE,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007481"Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
Daniel Veillardd8925572005-06-08 22:34:55 +00007482 internal_val, NULL);
7483 }
7484 }
7485 if (internal_val) {
7486 xmlFree(internal_val);
7487 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007488 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007489
7490 *value = val;
7491 return(name);
7492}
7493
7494/**
7495 * xmlParseStartTag2:
7496 * @ctxt: an XML parser context
7497 *
7498 * parse a start of tag either for rule element or
7499 * EmptyElement. In both case we don't parse the tag closing chars.
7500 * This routine is called when running SAX2 parsing
7501 *
7502 * [40] STag ::= '<' Name (S Attribute)* S? '>'
7503 *
7504 * [ WFC: Unique Att Spec ]
7505 * No attribute name may appear more than once in the same start-tag or
7506 * empty-element tag.
7507 *
7508 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
7509 *
7510 * [ WFC: Unique Att Spec ]
7511 * No attribute name may appear more than once in the same start-tag or
7512 * empty-element tag.
7513 *
7514 * With namespace:
7515 *
7516 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
7517 *
7518 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
7519 *
7520 * Returns the element name parsed
7521 */
7522
7523static const xmlChar *
7524xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref,
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007525 const xmlChar **URI, int *tlen) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007526 const xmlChar *localname;
7527 const xmlChar *prefix;
7528 const xmlChar *attname;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007529 const xmlChar *aprefix;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007530 const xmlChar *nsname;
7531 xmlChar *attvalue;
7532 const xmlChar **atts = ctxt->atts;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007533 int maxatts = ctxt->maxatts;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007534 int nratts, nbatts, nbdef;
7535 int i, j, nbNs, attval;
7536 const xmlChar *base;
7537 unsigned long cur;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007538
7539 if (RAW != '<') return(NULL);
7540 NEXT1;
7541
7542 /*
7543 * NOTE: it is crucial with the SAX2 API to never call SHRINK beyond that
7544 * point since the attribute values may be stored as pointers to
7545 * the buffer and calling SHRINK would destroy them !
7546 * The Shrinking is only possible once the full set of attribute
7547 * callbacks have been done.
7548 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00007549reparse:
Daniel Veillard0fb18932003-09-07 09:14:37 +00007550 SHRINK;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007551 base = ctxt->input->base;
7552 cur = ctxt->input->cur - ctxt->input->base;
7553 nbatts = 0;
7554 nratts = 0;
7555 nbdef = 0;
7556 nbNs = 0;
7557 attval = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007558
7559 localname = xmlParseQName(ctxt, &prefix);
7560 if (localname == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007561 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7562 "StartTag: invalid element name\n");
Daniel Veillard0fb18932003-09-07 09:14:37 +00007563 return(NULL);
7564 }
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007565 *tlen = ctxt->input->cur - ctxt->input->base - cur;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007566
7567 /*
7568 * Now parse the attributes, it ends up with the ending
7569 *
7570 * (S Attribute)* S?
7571 */
7572 SKIP_BLANKS;
7573 GROW;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007574 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007575
7576 while ((RAW != '>') &&
7577 ((RAW != '/') || (NXT(1) != '>')) &&
Daniel Veillard73b013f2003-09-30 12:36:01 +00007578 (IS_BYTE_CHAR(RAW))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007579 const xmlChar *q = CUR_PTR;
7580 unsigned int cons = ctxt->input->consumed;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007581 int len = -1, alloc = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007582
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007583 attname = xmlParseAttribute2(ctxt, prefix, localname,
7584 &aprefix, &attvalue, &len, &alloc);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007585 if ((attname != NULL) && (attvalue != NULL)) {
7586 if (len < 0) len = xmlStrlen(attvalue);
7587 if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00007588 const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len);
7589 xmlURIPtr uri;
7590
7591 if (*URL != 0) {
7592 uri = xmlParseURI((const char *) URL);
7593 if (uri == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007594 xmlWarningMsg(ctxt, XML_WAR_NS_URI,
7595 "xmlns: %s not a valid URI\n",
7596 URL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007597 } else {
7598 if (uri->scheme == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007599 xmlWarningMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
7600 "xmlns: URI %s is not absolute\n",
7601 URL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007602 }
7603 xmlFreeURI(uri);
7604 }
7605 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007606 /*
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007607 * check that it's not a defined namespace
Daniel Veillard0fb18932003-09-07 09:14:37 +00007608 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007609 for (j = 1;j <= nbNs;j++)
7610 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL)
7611 break;
7612 if (j <= nbNs)
7613 xmlErrAttributeDup(ctxt, NULL, attname);
7614 else
7615 if (nsPush(ctxt, NULL, URL) > 0) nbNs++;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007616 if (alloc != 0) xmlFree(attvalue);
7617 SKIP_BLANKS;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007618 continue;
7619 }
7620 if (aprefix == ctxt->str_xmlns) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00007621 const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len);
7622 xmlURIPtr uri;
7623
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007624 if (attname == ctxt->str_xml) {
7625 if (URL != ctxt->str_xml_ns) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007626 xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
7627 "xml namespace prefix mapped to wrong URI\n",
7628 NULL, NULL, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007629 }
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007630 /*
7631 * Do not keep a namespace definition node
7632 */
7633 if (alloc != 0) xmlFree(attvalue);
7634 SKIP_BLANKS;
7635 continue;
7636 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00007637 uri = xmlParseURI((const char *) URL);
7638 if (uri == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007639 xmlWarningMsg(ctxt, XML_WAR_NS_URI,
7640 "xmlns:%s: '%s' is not a valid URI\n",
7641 attname, URL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007642 } else {
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007643 if ((ctxt->pedantic) && (uri->scheme == NULL)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007644 xmlWarningMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
7645 "xmlns:%s: URI %s is not absolute\n",
7646 attname, URL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007647 }
7648 xmlFreeURI(uri);
7649 }
7650
Daniel Veillard0fb18932003-09-07 09:14:37 +00007651 /*
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007652 * check that it's not a defined namespace
Daniel Veillard0fb18932003-09-07 09:14:37 +00007653 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007654 for (j = 1;j <= nbNs;j++)
7655 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname)
7656 break;
7657 if (j <= nbNs)
7658 xmlErrAttributeDup(ctxt, aprefix, attname);
7659 else
7660 if (nsPush(ctxt, attname, URL) > 0) nbNs++;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007661 if (alloc != 0) xmlFree(attvalue);
7662 SKIP_BLANKS;
Daniel Veillardd3999c72004-03-10 16:27:03 +00007663 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007664 continue;
7665 }
7666
7667 /*
7668 * Add the pair to atts
7669 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00007670 if ((atts == NULL) || (nbatts + 5 > maxatts)) {
7671 if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007672 if (attvalue[len] == 0)
7673 xmlFree(attvalue);
7674 goto failed;
7675 }
7676 maxatts = ctxt->maxatts;
7677 atts = ctxt->atts;
7678 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00007679 ctxt->attallocs[nratts++] = alloc;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007680 atts[nbatts++] = attname;
7681 atts[nbatts++] = aprefix;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007682 atts[nbatts++] = NULL; /* the URI will be fetched later */
Daniel Veillard0fb18932003-09-07 09:14:37 +00007683 atts[nbatts++] = attvalue;
7684 attvalue += len;
7685 atts[nbatts++] = attvalue;
7686 /*
7687 * tag if some deallocation is needed
7688 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00007689 if (alloc != 0) attval = 1;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007690 } else {
7691 if ((attvalue != NULL) && (attvalue[len] == 0))
7692 xmlFree(attvalue);
7693 }
7694
7695failed:
7696
7697 GROW
Daniel Veillarde57ec792003-09-10 10:50:59 +00007698 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007699 if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
7700 break;
William M. Brack76e95df2003-10-18 16:20:14 +00007701 if (!IS_BLANK_CH(RAW)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007702 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
7703 "attributes construct error\n");
William M. Brack13dfa872004-09-18 04:52:08 +00007704 break;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007705 }
7706 SKIP_BLANKS;
7707 if ((cons == ctxt->input->consumed) && (q == CUR_PTR) &&
7708 (attname == NULL) && (attvalue == NULL)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007709 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007710 "xmlParseStartTag: problem parsing attributes\n");
Daniel Veillard0fb18932003-09-07 09:14:37 +00007711 break;
7712 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007713 GROW;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007714 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007715 }
7716
Daniel Veillard0fb18932003-09-07 09:14:37 +00007717 /*
Daniel Veillarde57ec792003-09-10 10:50:59 +00007718 * The attributes defaulting
7719 */
7720 if (ctxt->attsDefault != NULL) {
7721 xmlDefAttrsPtr defaults;
7722
7723 defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix);
7724 if (defaults != NULL) {
7725 for (i = 0;i < defaults->nbAttrs;i++) {
7726 attname = defaults->values[4 * i];
7727 aprefix = defaults->values[4 * i + 1];
7728
7729 /*
7730 * special work for namespaces defaulted defs
7731 */
7732 if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
7733 /*
7734 * check that it's not a defined namespace
7735 */
7736 for (j = 1;j <= nbNs;j++)
7737 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL)
7738 break;
7739 if (j <= nbNs) continue;
7740
7741 nsname = xmlGetNamespace(ctxt, NULL);
7742 if (nsname != defaults->values[4 * i + 2]) {
7743 if (nsPush(ctxt, NULL,
7744 defaults->values[4 * i + 2]) > 0)
7745 nbNs++;
7746 }
7747 } else if (aprefix == ctxt->str_xmlns) {
7748 /*
7749 * check that it's not a defined namespace
7750 */
7751 for (j = 1;j <= nbNs;j++)
7752 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname)
7753 break;
7754 if (j <= nbNs) continue;
7755
7756 nsname = xmlGetNamespace(ctxt, attname);
7757 if (nsname != defaults->values[2]) {
7758 if (nsPush(ctxt, attname,
7759 defaults->values[4 * i + 2]) > 0)
7760 nbNs++;
7761 }
7762 } else {
7763 /*
7764 * check that it's not a defined attribute
7765 */
7766 for (j = 0;j < nbatts;j+=5) {
7767 if ((attname == atts[j]) && (aprefix == atts[j+1]))
7768 break;
7769 }
7770 if (j < nbatts) continue;
7771
7772 if ((atts == NULL) || (nbatts + 5 > maxatts)) {
7773 if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00007774 return(NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007775 }
7776 maxatts = ctxt->maxatts;
7777 atts = ctxt->atts;
7778 }
7779 atts[nbatts++] = attname;
7780 atts[nbatts++] = aprefix;
7781 if (aprefix == NULL)
7782 atts[nbatts++] = NULL;
7783 else
7784 atts[nbatts++] = xmlGetNamespace(ctxt, aprefix);
7785 atts[nbatts++] = defaults->values[4 * i + 2];
7786 atts[nbatts++] = defaults->values[4 * i + 3];
7787 nbdef++;
7788 }
7789 }
7790 }
7791 }
7792
Daniel Veillarde70c8772003-11-25 07:21:18 +00007793 /*
7794 * The attributes checkings
7795 */
7796 for (i = 0; i < nbatts;i += 5) {
Kasimier T. Buchcik455472f2005-04-29 10:04:43 +00007797 /*
7798 * The default namespace does not apply to attribute names.
7799 */
7800 if (atts[i + 1] != NULL) {
7801 nsname = xmlGetNamespace(ctxt, atts[i + 1]);
7802 if (nsname == NULL) {
7803 xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
7804 "Namespace prefix %s for %s on %s is not defined\n",
7805 atts[i + 1], atts[i], localname);
7806 }
7807 atts[i + 2] = nsname;
7808 } else
7809 nsname = NULL;
Daniel Veillarde70c8772003-11-25 07:21:18 +00007810 /*
7811 * [ WFC: Unique Att Spec ]
7812 * No attribute name may appear more than once in the same
7813 * start-tag or empty-element tag.
7814 * As extended by the Namespace in XML REC.
7815 */
7816 for (j = 0; j < i;j += 5) {
7817 if (atts[i] == atts[j]) {
7818 if (atts[i+1] == atts[j+1]) {
7819 xmlErrAttributeDup(ctxt, atts[i+1], atts[i]);
7820 break;
7821 }
7822 if ((nsname != NULL) && (atts[j + 2] == nsname)) {
7823 xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED,
7824 "Namespaced Attribute %s in '%s' redefined\n",
7825 atts[i], nsname, NULL);
7826 break;
7827 }
7828 }
7829 }
7830 }
7831
Daniel Veillarde57ec792003-09-10 10:50:59 +00007832 nsname = xmlGetNamespace(ctxt, prefix);
7833 if ((prefix != NULL) && (nsname == NULL)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007834 xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
7835 "Namespace prefix %s on %s is not defined\n",
7836 prefix, localname, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007837 }
7838 *pref = prefix;
7839 *URI = nsname;
7840
7841 /*
7842 * SAX: Start of Element !
7843 */
7844 if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) &&
7845 (!ctxt->disableSAX)) {
7846 if (nbNs > 0)
7847 ctxt->sax->startElementNs(ctxt->userData, localname, prefix,
7848 nsname, nbNs, &ctxt->nsTab[ctxt->nsNr - 2 * nbNs],
7849 nbatts / 5, nbdef, atts);
7850 else
7851 ctxt->sax->startElementNs(ctxt->userData, localname, prefix,
7852 nsname, 0, NULL, nbatts / 5, nbdef, atts);
7853 }
7854
7855 /*
7856 * Free up attribute allocated strings if needed
7857 */
7858 if (attval != 0) {
7859 for (i = 3,j = 0; j < nratts;i += 5,j++)
7860 if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL))
7861 xmlFree((xmlChar *) atts[i]);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007862 }
7863
7864 return(localname);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007865
7866base_changed:
7867 /*
7868 * the attribute strings are valid iif the base didn't changed
7869 */
7870 if (attval != 0) {
7871 for (i = 3,j = 0; j < nratts;i += 5,j++)
7872 if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL))
7873 xmlFree((xmlChar *) atts[i]);
7874 }
7875 ctxt->input->cur = ctxt->input->base + cur;
7876 if (ctxt->wellFormed == 1) {
7877 goto reparse;
7878 }
7879 return(NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007880}
7881
7882/**
7883 * xmlParseEndTag2:
7884 * @ctxt: an XML parser context
7885 * @line: line of the start tag
7886 * @nsNr: number of namespaces on the start tag
7887 *
7888 * parse an end of tag
7889 *
7890 * [42] ETag ::= '</' Name S? '>'
7891 *
7892 * With namespace
7893 *
7894 * [NS 9] ETag ::= '</' QName S? '>'
7895 */
7896
7897static void
7898xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlChar *prefix,
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007899 const xmlChar *URI, int line, int nsNr, int tlen) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007900 const xmlChar *name;
7901
7902 GROW;
7903 if ((RAW != '<') || (NXT(1) != '/')) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007904 xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007905 return;
7906 }
7907 SKIP(2);
7908
William M. Brack13dfa872004-09-18 04:52:08 +00007909 if ((tlen > 0) && (xmlStrncmp(ctxt->input->cur, ctxt->name, tlen) == 0)) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007910 if (ctxt->input->cur[tlen] == '>') {
7911 ctxt->input->cur += tlen + 1;
7912 goto done;
7913 }
7914 ctxt->input->cur += tlen;
7915 name = (xmlChar*)1;
7916 } else {
7917 if (prefix == NULL)
7918 name = xmlParseNameAndCompare(ctxt, ctxt->name);
7919 else
7920 name = xmlParseQNameAndCompare(ctxt, ctxt->name, prefix);
7921 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007922
7923 /*
7924 * We should definitely be at the ending "S? '>'" part
7925 */
7926 GROW;
7927 SKIP_BLANKS;
Daniel Veillard73b013f2003-09-30 12:36:01 +00007928 if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007929 xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007930 } else
7931 NEXT1;
7932
7933 /*
7934 * [ WFC: Element Type Match ]
7935 * The Name in an element's end-tag must match the element type in the
7936 * start-tag.
7937 *
7938 */
7939 if (name != (xmlChar*)1) {
Daniel Veillardf403d292003-10-05 13:51:35 +00007940 if (name == NULL) name = BAD_CAST "unparseable";
7941 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007942 "Opening and ending tag mismatch: %s line %d and %s\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00007943 ctxt->name, line, name);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007944 }
7945
7946 /*
7947 * SAX: End of Tag
7948 */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007949done:
Daniel Veillard0fb18932003-09-07 09:14:37 +00007950 if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
7951 (!ctxt->disableSAX))
7952 ctxt->sax->endElementNs(ctxt->userData, ctxt->name, prefix, URI);
7953
Daniel Veillard0fb18932003-09-07 09:14:37 +00007954 spacePop(ctxt);
7955 if (nsNr != 0)
7956 nsPop(ctxt, nsNr);
7957 return;
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00007958}
7959
7960/**
Owen Taylor3473f882001-02-23 17:55:21 +00007961 * xmlParseCDSect:
7962 * @ctxt: an XML parser context
7963 *
7964 * Parse escaped pure raw content.
7965 *
7966 * [18] CDSect ::= CDStart CData CDEnd
7967 *
7968 * [19] CDStart ::= '<![CDATA['
7969 *
7970 * [20] Data ::= (Char* - (Char* ']]>' Char*))
7971 *
7972 * [21] CDEnd ::= ']]>'
7973 */
7974void
7975xmlParseCDSect(xmlParserCtxtPtr ctxt) {
7976 xmlChar *buf = NULL;
7977 int len = 0;
7978 int size = XML_PARSER_BUFFER_SIZE;
7979 int r, rl;
7980 int s, sl;
7981 int cur, l;
7982 int count = 0;
7983
Daniel Veillard8f597c32003-10-06 08:19:27 +00007984 /* Check 2.6.0 was NXT(0) not RAW */
Daniel Veillarda07050d2003-10-19 14:46:32 +00007985 if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007986 SKIP(9);
7987 } else
7988 return;
7989
7990 ctxt->instate = XML_PARSER_CDATA_SECTION;
7991 r = CUR_CHAR(rl);
William M. Brack871611b2003-10-18 04:53:14 +00007992 if (!IS_CHAR(r)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007993 xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007994 ctxt->instate = XML_PARSER_CONTENT;
7995 return;
7996 }
7997 NEXTL(rl);
7998 s = CUR_CHAR(sl);
William M. Brack871611b2003-10-18 04:53:14 +00007999 if (!IS_CHAR(s)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008000 xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008001 ctxt->instate = XML_PARSER_CONTENT;
8002 return;
8003 }
8004 NEXTL(sl);
8005 cur = CUR_CHAR(l);
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008006 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00008007 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008008 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008009 return;
8010 }
William M. Brack871611b2003-10-18 04:53:14 +00008011 while (IS_CHAR(cur) &&
Owen Taylor3473f882001-02-23 17:55:21 +00008012 ((r != ']') || (s != ']') || (cur != '>'))) {
8013 if (len + 5 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00008014 xmlChar *tmp;
8015
Owen Taylor3473f882001-02-23 17:55:21 +00008016 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00008017 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
8018 if (tmp == NULL) {
8019 xmlFree(buf);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008020 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008021 return;
8022 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00008023 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00008024 }
8025 COPY_BUF(rl,buf,len,r);
8026 r = s;
8027 rl = sl;
8028 s = cur;
8029 sl = l;
8030 count++;
8031 if (count > 50) {
8032 GROW;
8033 count = 0;
8034 }
8035 NEXTL(l);
8036 cur = CUR_CHAR(l);
8037 }
8038 buf[len] = 0;
8039 ctxt->instate = XML_PARSER_CONTENT;
8040 if (cur != '>') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00008041 xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED,
Owen Taylor3473f882001-02-23 17:55:21 +00008042 "CData section not finished\n%.50s\n", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00008043 xmlFree(buf);
8044 return;
8045 }
8046 NEXTL(l);
8047
8048 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00008049 * OK the buffer is to be consumed as cdata.
Owen Taylor3473f882001-02-23 17:55:21 +00008050 */
8051 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
8052 if (ctxt->sax->cdataBlock != NULL)
8053 ctxt->sax->cdataBlock(ctxt->userData, buf, len);
Daniel Veillard7583a592001-07-08 13:15:55 +00008054 else if (ctxt->sax->characters != NULL)
8055 ctxt->sax->characters(ctxt->userData, buf, len);
Owen Taylor3473f882001-02-23 17:55:21 +00008056 }
8057 xmlFree(buf);
8058}
8059
8060/**
8061 * xmlParseContent:
8062 * @ctxt: an XML parser context
8063 *
8064 * Parse a content:
8065 *
8066 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
8067 */
8068
8069void
8070xmlParseContent(xmlParserCtxtPtr ctxt) {
8071 GROW;
Daniel Veillardfdc91562002-07-01 21:52:03 +00008072 while ((RAW != 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +00008073 ((RAW != '<') || (NXT(1) != '/'))) {
8074 const xmlChar *test = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00008075 unsigned int cons = ctxt->input->consumed;
Daniel Veillard21a0f912001-02-25 19:54:14 +00008076 const xmlChar *cur = ctxt->input->cur;
Owen Taylor3473f882001-02-23 17:55:21 +00008077
8078 /*
Owen Taylor3473f882001-02-23 17:55:21 +00008079 * First case : a Processing Instruction.
8080 */
Daniel Veillardfdc91562002-07-01 21:52:03 +00008081 if ((*cur == '<') && (cur[1] == '?')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008082 xmlParsePI(ctxt);
8083 }
8084
8085 /*
8086 * Second case : a CDSection
8087 */
Daniel Veillard8f597c32003-10-06 08:19:27 +00008088 /* 2.6.0 test was *cur not RAW */
Daniel Veillarda07050d2003-10-19 14:46:32 +00008089 else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008090 xmlParseCDSect(ctxt);
8091 }
8092
8093 /*
8094 * Third case : a comment
8095 */
Daniel Veillard21a0f912001-02-25 19:54:14 +00008096 else if ((*cur == '<') && (NXT(1) == '!') &&
Owen Taylor3473f882001-02-23 17:55:21 +00008097 (NXT(2) == '-') && (NXT(3) == '-')) {
8098 xmlParseComment(ctxt);
8099 ctxt->instate = XML_PARSER_CONTENT;
8100 }
8101
8102 /*
8103 * Fourth case : a sub-element.
8104 */
Daniel Veillard21a0f912001-02-25 19:54:14 +00008105 else if (*cur == '<') {
Owen Taylor3473f882001-02-23 17:55:21 +00008106 xmlParseElement(ctxt);
8107 }
8108
8109 /*
8110 * Fifth case : a reference. If if has not been resolved,
8111 * parsing returns it's Name, create the node
8112 */
8113
Daniel Veillard21a0f912001-02-25 19:54:14 +00008114 else if (*cur == '&') {
Owen Taylor3473f882001-02-23 17:55:21 +00008115 xmlParseReference(ctxt);
8116 }
8117
8118 /*
8119 * Last case, text. Note that References are handled directly.
8120 */
8121 else {
8122 xmlParseCharData(ctxt, 0);
8123 }
8124
8125 GROW;
8126 /*
8127 * Pop-up of finished entities.
8128 */
Daniel Veillard561b7f82002-03-20 21:55:57 +00008129 while ((RAW == 0) && (ctxt->inputNr > 1))
Owen Taylor3473f882001-02-23 17:55:21 +00008130 xmlPopInput(ctxt);
8131 SHRINK;
8132
Daniel Veillardfdc91562002-07-01 21:52:03 +00008133 if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008134 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
8135 "detected an error in element content\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008136 ctxt->instate = XML_PARSER_EOF;
8137 break;
8138 }
8139 }
8140}
8141
8142/**
8143 * xmlParseElement:
8144 * @ctxt: an XML parser context
8145 *
8146 * parse an XML element, this is highly recursive
8147 *
8148 * [39] element ::= EmptyElemTag | STag content ETag
8149 *
8150 * [ WFC: Element Type Match ]
8151 * The Name in an element's end-tag must match the element type in the
8152 * start-tag.
8153 *
Owen Taylor3473f882001-02-23 17:55:21 +00008154 */
8155
8156void
8157xmlParseElement(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00008158 const xmlChar *name;
Daniel Veillard0fb18932003-09-07 09:14:37 +00008159 const xmlChar *prefix;
8160 const xmlChar *URI;
Owen Taylor3473f882001-02-23 17:55:21 +00008161 xmlParserNodeInfo node_info;
Daniel Veillardc82c57e2004-01-12 16:24:34 +00008162 int line, tlen;
Owen Taylor3473f882001-02-23 17:55:21 +00008163 xmlNodePtr ret;
Daniel Veillard0fb18932003-09-07 09:14:37 +00008164 int nsNr = ctxt->nsNr;
Owen Taylor3473f882001-02-23 17:55:21 +00008165
8166 /* Capture start position */
8167 if (ctxt->record_info) {
8168 node_info.begin_pos = ctxt->input->consumed +
8169 (CUR_PTR - ctxt->input->base);
8170 node_info.begin_line = ctxt->input->line;
8171 }
8172
8173 if (ctxt->spaceNr == 0)
8174 spacePush(ctxt, -1);
8175 else
8176 spacePush(ctxt, *ctxt->space);
8177
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00008178 line = ctxt->input->line;
Daniel Veillard81273902003-09-30 00:43:48 +00008179#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard0fb18932003-09-07 09:14:37 +00008180 if (ctxt->sax2)
Daniel Veillard81273902003-09-30 00:43:48 +00008181#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00008182 name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen);
Daniel Veillard81273902003-09-30 00:43:48 +00008183#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard0fb18932003-09-07 09:14:37 +00008184 else
8185 name = xmlParseStartTag(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00008186#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00008187 if (name == NULL) {
8188 spacePop(ctxt);
8189 return;
8190 }
8191 namePush(ctxt, name);
8192 ret = ctxt->node;
8193
Daniel Veillard4432df22003-09-28 18:58:27 +00008194#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00008195 /*
8196 * [ VC: Root Element Type ]
8197 * The Name in the document type declaration must match the element
8198 * type of the root element.
8199 */
8200 if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
8201 ctxt->node && (ctxt->node == ctxt->myDoc->children))
8202 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillard4432df22003-09-28 18:58:27 +00008203#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00008204
8205 /*
8206 * Check for an Empty Element.
8207 */
8208 if ((RAW == '/') && (NXT(1) == '>')) {
8209 SKIP(2);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008210 if (ctxt->sax2) {
8211 if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
8212 (!ctxt->disableSAX))
8213 ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI);
Daniel Veillard81273902003-09-30 00:43:48 +00008214#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard0fb18932003-09-07 09:14:37 +00008215 } else {
8216 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
8217 (!ctxt->disableSAX))
8218 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard81273902003-09-30 00:43:48 +00008219#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00008220 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00008221 namePop(ctxt);
8222 spacePop(ctxt);
8223 if (nsNr != ctxt->nsNr)
8224 nsPop(ctxt, ctxt->nsNr - nsNr);
Owen Taylor3473f882001-02-23 17:55:21 +00008225 if ( ret != NULL && ctxt->record_info ) {
8226 node_info.end_pos = ctxt->input->consumed +
8227 (CUR_PTR - ctxt->input->base);
8228 node_info.end_line = ctxt->input->line;
8229 node_info.node = ret;
8230 xmlParserAddNodeInfo(ctxt, &node_info);
8231 }
8232 return;
8233 }
8234 if (RAW == '>') {
Daniel Veillard21a0f912001-02-25 19:54:14 +00008235 NEXT1;
Owen Taylor3473f882001-02-23 17:55:21 +00008236 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00008237 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED,
8238 "Couldn't find end of Start Tag %s line %d\n",
8239 name, line, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008240
8241 /*
8242 * end of parsing of this node.
8243 */
8244 nodePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008245 namePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008246 spacePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008247 if (nsNr != ctxt->nsNr)
8248 nsPop(ctxt, ctxt->nsNr - nsNr);
Owen Taylor3473f882001-02-23 17:55:21 +00008249
8250 /*
8251 * Capture end position and add node
8252 */
8253 if ( ret != NULL && ctxt->record_info ) {
8254 node_info.end_pos = ctxt->input->consumed +
8255 (CUR_PTR - ctxt->input->base);
8256 node_info.end_line = ctxt->input->line;
8257 node_info.node = ret;
8258 xmlParserAddNodeInfo(ctxt, &node_info);
8259 }
8260 return;
8261 }
8262
8263 /*
8264 * Parse the content of the element:
8265 */
8266 xmlParseContent(ctxt);
Daniel Veillard73b013f2003-09-30 12:36:01 +00008267 if (!IS_BYTE_CHAR(RAW)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00008268 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED,
Daniel Veillard2b0f8792003-10-10 19:36:36 +00008269 "Premature end of data in tag %s line %d\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00008270 name, line, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008271
8272 /*
8273 * end of parsing of this node.
8274 */
8275 nodePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008276 namePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008277 spacePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008278 if (nsNr != ctxt->nsNr)
8279 nsPop(ctxt, ctxt->nsNr - nsNr);
Owen Taylor3473f882001-02-23 17:55:21 +00008280 return;
8281 }
8282
8283 /*
8284 * parse the end of tag: '</' should be here.
8285 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00008286 if (ctxt->sax2) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00008287 xmlParseEndTag2(ctxt, prefix, URI, line, ctxt->nsNr - nsNr, tlen);
Daniel Veillarde57ec792003-09-10 10:50:59 +00008288 namePop(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00008289 }
8290#ifdef LIBXML_SAX1_ENABLED
8291 else
Daniel Veillard0fb18932003-09-07 09:14:37 +00008292 xmlParseEndTag1(ctxt, line);
Daniel Veillard81273902003-09-30 00:43:48 +00008293#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00008294
8295 /*
8296 * Capture end position and add node
8297 */
8298 if ( ret != NULL && ctxt->record_info ) {
8299 node_info.end_pos = ctxt->input->consumed +
8300 (CUR_PTR - ctxt->input->base);
8301 node_info.end_line = ctxt->input->line;
8302 node_info.node = ret;
8303 xmlParserAddNodeInfo(ctxt, &node_info);
8304 }
8305}
8306
8307/**
8308 * xmlParseVersionNum:
8309 * @ctxt: an XML parser context
8310 *
8311 * parse the XML version value.
8312 *
8313 * [26] VersionNum ::= ([a-zA-Z0-9_.:] | '-')+
8314 *
8315 * Returns the string giving the XML version number, or NULL
8316 */
8317xmlChar *
8318xmlParseVersionNum(xmlParserCtxtPtr ctxt) {
8319 xmlChar *buf = NULL;
8320 int len = 0;
8321 int size = 10;
8322 xmlChar cur;
8323
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008324 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00008325 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008326 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008327 return(NULL);
8328 }
8329 cur = CUR;
8330 while (((cur >= 'a') && (cur <= 'z')) ||
8331 ((cur >= 'A') && (cur <= 'Z')) ||
8332 ((cur >= '0') && (cur <= '9')) ||
8333 (cur == '_') || (cur == '.') ||
8334 (cur == ':') || (cur == '-')) {
8335 if (len + 1 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00008336 xmlChar *tmp;
8337
Owen Taylor3473f882001-02-23 17:55:21 +00008338 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00008339 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
8340 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008341 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008342 return(NULL);
8343 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00008344 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00008345 }
8346 buf[len++] = cur;
8347 NEXT;
8348 cur=CUR;
8349 }
8350 buf[len] = 0;
8351 return(buf);
8352}
8353
8354/**
8355 * xmlParseVersionInfo:
8356 * @ctxt: an XML parser context
8357 *
8358 * parse the XML version.
8359 *
8360 * [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ")
8361 *
8362 * [25] Eq ::= S? '=' S?
8363 *
8364 * Returns the version string, e.g. "1.0"
8365 */
8366
8367xmlChar *
8368xmlParseVersionInfo(xmlParserCtxtPtr ctxt) {
8369 xmlChar *version = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00008370
Daniel Veillarda07050d2003-10-19 14:46:32 +00008371 if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008372 SKIP(7);
8373 SKIP_BLANKS;
8374 if (RAW != '=') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008375 xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008376 return(NULL);
8377 }
8378 NEXT;
8379 SKIP_BLANKS;
8380 if (RAW == '"') {
8381 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008382 version = xmlParseVersionNum(ctxt);
8383 if (RAW != '"') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008384 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008385 } else
8386 NEXT;
8387 } else if (RAW == '\''){
8388 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008389 version = xmlParseVersionNum(ctxt);
8390 if (RAW != '\'') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008391 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008392 } else
8393 NEXT;
8394 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008395 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008396 }
8397 }
8398 return(version);
8399}
8400
8401/**
8402 * xmlParseEncName:
8403 * @ctxt: an XML parser context
8404 *
8405 * parse the XML encoding name
8406 *
8407 * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
8408 *
8409 * Returns the encoding name value or NULL
8410 */
8411xmlChar *
8412xmlParseEncName(xmlParserCtxtPtr ctxt) {
8413 xmlChar *buf = NULL;
8414 int len = 0;
8415 int size = 10;
8416 xmlChar cur;
8417
8418 cur = CUR;
8419 if (((cur >= 'a') && (cur <= 'z')) ||
8420 ((cur >= 'A') && (cur <= 'Z'))) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008421 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00008422 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008423 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008424 return(NULL);
8425 }
8426
8427 buf[len++] = cur;
8428 NEXT;
8429 cur = CUR;
8430 while (((cur >= 'a') && (cur <= 'z')) ||
8431 ((cur >= 'A') && (cur <= 'Z')) ||
8432 ((cur >= '0') && (cur <= '9')) ||
8433 (cur == '.') || (cur == '_') ||
8434 (cur == '-')) {
8435 if (len + 1 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00008436 xmlChar *tmp;
8437
Owen Taylor3473f882001-02-23 17:55:21 +00008438 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00008439 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
8440 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008441 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00008442 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00008443 return(NULL);
8444 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00008445 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00008446 }
8447 buf[len++] = cur;
8448 NEXT;
8449 cur = CUR;
8450 if (cur == 0) {
8451 SHRINK;
8452 GROW;
8453 cur = CUR;
8454 }
8455 }
8456 buf[len] = 0;
8457 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008458 xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008459 }
8460 return(buf);
8461}
8462
8463/**
8464 * xmlParseEncodingDecl:
8465 * @ctxt: an XML parser context
8466 *
8467 * parse the XML encoding declaration
8468 *
8469 * [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'")
8470 *
8471 * this setups the conversion filters.
8472 *
8473 * Returns the encoding value or NULL
8474 */
8475
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008476const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00008477xmlParseEncodingDecl(xmlParserCtxtPtr ctxt) {
8478 xmlChar *encoding = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00008479
8480 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008481 if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008482 SKIP(8);
8483 SKIP_BLANKS;
8484 if (RAW != '=') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008485 xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008486 return(NULL);
8487 }
8488 NEXT;
8489 SKIP_BLANKS;
8490 if (RAW == '"') {
8491 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008492 encoding = xmlParseEncName(ctxt);
8493 if (RAW != '"') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008494 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008495 } else
8496 NEXT;
8497 } else if (RAW == '\''){
8498 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008499 encoding = xmlParseEncName(ctxt);
8500 if (RAW != '\'') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008501 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008502 } else
8503 NEXT;
Daniel Veillard82ac6b02002-02-17 23:18:55 +00008504 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008505 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008506 }
Daniel Veillard6b621b82003-08-11 15:03:34 +00008507 /*
8508 * UTF-16 encoding stwich has already taken place at this stage,
8509 * more over the little-endian/big-endian selection is already done
8510 */
8511 if ((encoding != NULL) &&
8512 ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-16")) ||
8513 (!xmlStrcasecmp(encoding, BAD_CAST "UTF16")))) {
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008514 if (ctxt->encoding != NULL)
8515 xmlFree((xmlChar *) ctxt->encoding);
8516 ctxt->encoding = encoding;
Daniel Veillardb19ba832003-08-14 00:33:46 +00008517 }
8518 /*
8519 * UTF-8 encoding is handled natively
8520 */
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008521 else if ((encoding != NULL) &&
Daniel Veillardb19ba832003-08-14 00:33:46 +00008522 ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-8")) ||
8523 (!xmlStrcasecmp(encoding, BAD_CAST "UTF8")))) {
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008524 if (ctxt->encoding != NULL)
8525 xmlFree((xmlChar *) ctxt->encoding);
8526 ctxt->encoding = encoding;
Daniel Veillard6b621b82003-08-11 15:03:34 +00008527 }
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008528 else if (encoding != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00008529 xmlCharEncodingHandlerPtr handler;
8530
8531 if (ctxt->input->encoding != NULL)
8532 xmlFree((xmlChar *) ctxt->input->encoding);
8533 ctxt->input->encoding = encoding;
8534
Daniel Veillarda6874ca2003-07-29 16:47:24 +00008535 handler = xmlFindCharEncodingHandler((const char *) encoding);
8536 if (handler != NULL) {
8537 xmlSwitchToEncoding(ctxt, handler);
Owen Taylor3473f882001-02-23 17:55:21 +00008538 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00008539 xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillarda6874ca2003-07-29 16:47:24 +00008540 "Unsupported encoding %s\n", encoding);
8541 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008542 }
8543 }
8544 }
8545 return(encoding);
8546}
8547
8548/**
8549 * xmlParseSDDecl:
8550 * @ctxt: an XML parser context
8551 *
8552 * parse the XML standalone declaration
8553 *
8554 * [32] SDDecl ::= S 'standalone' Eq
8555 * (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"'))
8556 *
8557 * [ VC: Standalone Document Declaration ]
8558 * TODO The standalone document declaration must have the value "no"
8559 * if any external markup declarations contain declarations of:
8560 * - attributes with default values, if elements to which these
8561 * attributes apply appear in the document without specifications
8562 * of values for these attributes, or
8563 * - entities (other than amp, lt, gt, apos, quot), if references
8564 * to those entities appear in the document, or
8565 * - attributes with values subject to normalization, where the
8566 * attribute appears in the document with a value which will change
8567 * as a result of normalization, or
8568 * - element types with element content, if white space occurs directly
8569 * within any instance of those types.
8570 *
8571 * Returns 1 if standalone, 0 otherwise
8572 */
8573
8574int
8575xmlParseSDDecl(xmlParserCtxtPtr ctxt) {
8576 int standalone = -1;
8577
8578 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008579 if (CMP10(CUR_PTR, 's', 't', 'a', 'n', 'd', 'a', 'l', 'o', 'n', 'e')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008580 SKIP(10);
8581 SKIP_BLANKS;
8582 if (RAW != '=') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008583 xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008584 return(standalone);
8585 }
8586 NEXT;
8587 SKIP_BLANKS;
8588 if (RAW == '\''){
8589 NEXT;
8590 if ((RAW == 'n') && (NXT(1) == 'o')) {
8591 standalone = 0;
8592 SKIP(2);
8593 } else if ((RAW == 'y') && (NXT(1) == 'e') &&
8594 (NXT(2) == 's')) {
8595 standalone = 1;
8596 SKIP(3);
8597 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008598 xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008599 }
8600 if (RAW != '\'') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008601 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008602 } else
8603 NEXT;
8604 } else if (RAW == '"'){
8605 NEXT;
8606 if ((RAW == 'n') && (NXT(1) == 'o')) {
8607 standalone = 0;
8608 SKIP(2);
8609 } else if ((RAW == 'y') && (NXT(1) == 'e') &&
8610 (NXT(2) == 's')) {
8611 standalone = 1;
8612 SKIP(3);
8613 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008614 xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008615 }
8616 if (RAW != '"') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008617 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008618 } else
8619 NEXT;
8620 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008621 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008622 }
8623 }
8624 return(standalone);
8625}
8626
8627/**
8628 * xmlParseXMLDecl:
8629 * @ctxt: an XML parser context
8630 *
8631 * parse an XML declaration header
8632 *
8633 * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
8634 */
8635
8636void
8637xmlParseXMLDecl(xmlParserCtxtPtr ctxt) {
8638 xmlChar *version;
8639
8640 /*
8641 * We know that '<?xml' is here.
8642 */
8643 SKIP(5);
8644
William M. Brack76e95df2003-10-18 16:20:14 +00008645 if (!IS_BLANK_CH(RAW)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008646 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
8647 "Blank needed after '<?xml'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008648 }
8649 SKIP_BLANKS;
8650
8651 /*
Daniel Veillard19840942001-11-29 16:11:38 +00008652 * We must have the VersionInfo here.
Owen Taylor3473f882001-02-23 17:55:21 +00008653 */
8654 version = xmlParseVersionInfo(ctxt);
Daniel Veillard19840942001-11-29 16:11:38 +00008655 if (version == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008656 xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL);
Daniel Veillard19840942001-11-29 16:11:38 +00008657 } else {
8658 if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) {
8659 /*
8660 * TODO: Blueberry should be detected here
8661 */
Daniel Veillard24eb9782003-10-04 21:08:09 +00008662 xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION,
8663 "Unsupported version '%s'\n",
8664 version, NULL);
Daniel Veillard19840942001-11-29 16:11:38 +00008665 }
8666 if (ctxt->version != NULL)
Daniel Veillardd3b08822001-12-05 12:03:33 +00008667 xmlFree((void *) ctxt->version);
Daniel Veillard19840942001-11-29 16:11:38 +00008668 ctxt->version = version;
Daniel Veillarda050d232001-09-05 15:51:05 +00008669 }
Owen Taylor3473f882001-02-23 17:55:21 +00008670
8671 /*
8672 * We may have the encoding declaration
8673 */
William M. Brack76e95df2003-10-18 16:20:14 +00008674 if (!IS_BLANK_CH(RAW)) {
Owen Taylor3473f882001-02-23 17:55:21 +00008675 if ((RAW == '?') && (NXT(1) == '>')) {
8676 SKIP(2);
8677 return;
8678 }
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008679 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008680 }
8681 xmlParseEncodingDecl(ctxt);
8682 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
8683 /*
8684 * The XML REC instructs us to stop parsing right here
8685 */
8686 return;
8687 }
8688
8689 /*
8690 * We may have the standalone status.
8691 */
William M. Brack76e95df2003-10-18 16:20:14 +00008692 if ((ctxt->input->encoding != NULL) && (!IS_BLANK_CH(RAW))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008693 if ((RAW == '?') && (NXT(1) == '>')) {
8694 SKIP(2);
8695 return;
8696 }
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008697 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008698 }
8699 SKIP_BLANKS;
8700 ctxt->input->standalone = xmlParseSDDecl(ctxt);
8701
8702 SKIP_BLANKS;
8703 if ((RAW == '?') && (NXT(1) == '>')) {
8704 SKIP(2);
8705 } else if (RAW == '>') {
8706 /* Deprecated old WD ... */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008707 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008708 NEXT;
8709 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008710 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008711 MOVETO_ENDTAG(CUR_PTR);
8712 NEXT;
8713 }
8714}
8715
8716/**
8717 * xmlParseMisc:
8718 * @ctxt: an XML parser context
8719 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00008720 * parse an XML Misc* optional field.
Owen Taylor3473f882001-02-23 17:55:21 +00008721 *
8722 * [27] Misc ::= Comment | PI | S
8723 */
8724
8725void
8726xmlParseMisc(xmlParserCtxtPtr ctxt) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00008727 while (((RAW == '<') && (NXT(1) == '?')) ||
Daniel Veillarda07050d2003-10-19 14:46:32 +00008728 (CMP4(CUR_PTR, '<', '!', '-', '-')) ||
William M. Brack76e95df2003-10-18 16:20:14 +00008729 IS_BLANK_CH(CUR)) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00008730 if ((RAW == '<') && (NXT(1) == '?')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008731 xmlParsePI(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00008732 } else if (IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00008733 NEXT;
8734 } else
8735 xmlParseComment(ctxt);
8736 }
8737}
8738
8739/**
8740 * xmlParseDocument:
8741 * @ctxt: an XML parser context
8742 *
8743 * parse an XML document (and build a tree if using the standard SAX
8744 * interface).
8745 *
8746 * [1] document ::= prolog element Misc*
8747 *
8748 * [22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)?
8749 *
8750 * Returns 0, -1 in case of error. the parser context is augmented
8751 * as a result of the parsing.
8752 */
8753
8754int
8755xmlParseDocument(xmlParserCtxtPtr ctxt) {
8756 xmlChar start[4];
8757 xmlCharEncoding enc;
8758
8759 xmlInitParser();
8760
Daniel Veillard36e5cd52004-11-02 14:52:23 +00008761 if ((ctxt == NULL) || (ctxt->input == NULL))
8762 return(-1);
8763
Owen Taylor3473f882001-02-23 17:55:21 +00008764 GROW;
8765
8766 /*
Daniel Veillard0fb18932003-09-07 09:14:37 +00008767 * SAX: detecting the level.
8768 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00008769 xmlDetectSAX2(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008770
8771 /*
Owen Taylor3473f882001-02-23 17:55:21 +00008772 * SAX: beginning of the document processing.
8773 */
8774 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
8775 ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
8776
Daniel Veillard4aede2e2003-10-17 12:43:59 +00008777 if ((ctxt->encoding == (const xmlChar *)XML_CHAR_ENCODING_NONE) &&
8778 ((ctxt->input->end - ctxt->input->cur) >= 4)) {
Daniel Veillard4aafa792001-07-28 17:21:12 +00008779 /*
8780 * Get the 4 first bytes and decode the charset
8781 * if enc != XML_CHAR_ENCODING_NONE
8782 * plug some encoding conversion routines.
8783 */
8784 start[0] = RAW;
8785 start[1] = NXT(1);
8786 start[2] = NXT(2);
8787 start[3] = NXT(3);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00008788 enc = xmlDetectCharEncoding(&start[0], 4);
Daniel Veillard4aafa792001-07-28 17:21:12 +00008789 if (enc != XML_CHAR_ENCODING_NONE) {
8790 xmlSwitchEncoding(ctxt, enc);
8791 }
Owen Taylor3473f882001-02-23 17:55:21 +00008792 }
8793
8794
8795 if (CUR == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008796 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008797 }
8798
8799 /*
8800 * Check for the XMLDecl in the Prolog.
8801 */
8802 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008803 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008804
8805 /*
8806 * Note that we will switch encoding on the fly.
8807 */
8808 xmlParseXMLDecl(ctxt);
8809 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
8810 /*
8811 * The XML REC instructs us to stop parsing right here
8812 */
8813 return(-1);
8814 }
8815 ctxt->standalone = ctxt->input->standalone;
8816 SKIP_BLANKS;
8817 } else {
8818 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
8819 }
8820 if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
8821 ctxt->sax->startDocument(ctxt->userData);
8822
8823 /*
8824 * The Misc part of the Prolog
8825 */
8826 GROW;
8827 xmlParseMisc(ctxt);
8828
8829 /*
8830 * Then possibly doc type declaration(s) and more Misc
8831 * (doctypedecl Misc*)?
8832 */
8833 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008834 if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008835
8836 ctxt->inSubset = 1;
8837 xmlParseDocTypeDecl(ctxt);
8838 if (RAW == '[') {
8839 ctxt->instate = XML_PARSER_DTD;
8840 xmlParseInternalSubset(ctxt);
8841 }
8842
8843 /*
8844 * Create and update the external subset.
8845 */
8846 ctxt->inSubset = 2;
8847 if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) &&
8848 (!ctxt->disableSAX))
8849 ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
8850 ctxt->extSubSystem, ctxt->extSubURI);
8851 ctxt->inSubset = 0;
8852
8853
8854 ctxt->instate = XML_PARSER_PROLOG;
8855 xmlParseMisc(ctxt);
8856 }
8857
8858 /*
8859 * Time to start parsing the tree itself
8860 */
8861 GROW;
8862 if (RAW != '<') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008863 xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY,
8864 "Start tag expected, '<' not found\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008865 } else {
8866 ctxt->instate = XML_PARSER_CONTENT;
8867 xmlParseElement(ctxt);
8868 ctxt->instate = XML_PARSER_EPILOG;
8869
8870
8871 /*
8872 * The Misc part at the end
8873 */
8874 xmlParseMisc(ctxt);
8875
Daniel Veillard561b7f82002-03-20 21:55:57 +00008876 if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008877 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008878 }
8879 ctxt->instate = XML_PARSER_EOF;
8880 }
8881
8882 /*
8883 * SAX: end of the document processing.
8884 */
Daniel Veillard8d24cc12002-03-05 15:41:29 +00008885 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00008886 ctxt->sax->endDocument(ctxt->userData);
8887
Daniel Veillard5997aca2002-03-18 18:36:20 +00008888 /*
8889 * Remove locally kept entity definitions if the tree was not built
8890 */
8891 if ((ctxt->myDoc != NULL) &&
8892 (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) {
8893 xmlFreeDoc(ctxt->myDoc);
8894 ctxt->myDoc = NULL;
8895 }
8896
Daniel Veillardc7612992002-02-17 22:47:37 +00008897 if (! ctxt->wellFormed) {
8898 ctxt->valid = 0;
8899 return(-1);
8900 }
Owen Taylor3473f882001-02-23 17:55:21 +00008901 return(0);
8902}
8903
8904/**
8905 * xmlParseExtParsedEnt:
8906 * @ctxt: an XML parser context
8907 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00008908 * parse a general parsed entity
Owen Taylor3473f882001-02-23 17:55:21 +00008909 * An external general parsed entity is well-formed if it matches the
8910 * production labeled extParsedEnt.
8911 *
8912 * [78] extParsedEnt ::= TextDecl? content
8913 *
8914 * Returns 0, -1 in case of error. the parser context is augmented
8915 * as a result of the parsing.
8916 */
8917
8918int
8919xmlParseExtParsedEnt(xmlParserCtxtPtr ctxt) {
8920 xmlChar start[4];
8921 xmlCharEncoding enc;
8922
Daniel Veillard36e5cd52004-11-02 14:52:23 +00008923 if ((ctxt == NULL) || (ctxt->input == NULL))
8924 return(-1);
8925
Owen Taylor3473f882001-02-23 17:55:21 +00008926 xmlDefaultSAXHandlerInit();
8927
Daniel Veillard309f81d2003-09-23 09:02:53 +00008928 xmlDetectSAX2(ctxt);
8929
Owen Taylor3473f882001-02-23 17:55:21 +00008930 GROW;
8931
8932 /*
8933 * SAX: beginning of the document processing.
8934 */
8935 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
8936 ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
8937
8938 /*
8939 * Get the 4 first bytes and decode the charset
8940 * if enc != XML_CHAR_ENCODING_NONE
8941 * plug some encoding conversion routines.
8942 */
Daniel Veillard4aede2e2003-10-17 12:43:59 +00008943 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
8944 start[0] = RAW;
8945 start[1] = NXT(1);
8946 start[2] = NXT(2);
8947 start[3] = NXT(3);
8948 enc = xmlDetectCharEncoding(start, 4);
8949 if (enc != XML_CHAR_ENCODING_NONE) {
8950 xmlSwitchEncoding(ctxt, enc);
8951 }
Owen Taylor3473f882001-02-23 17:55:21 +00008952 }
8953
8954
8955 if (CUR == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008956 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008957 }
8958
8959 /*
8960 * Check for the XMLDecl in the Prolog.
8961 */
8962 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008963 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008964
8965 /*
8966 * Note that we will switch encoding on the fly.
8967 */
8968 xmlParseXMLDecl(ctxt);
8969 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
8970 /*
8971 * The XML REC instructs us to stop parsing right here
8972 */
8973 return(-1);
8974 }
8975 SKIP_BLANKS;
8976 } else {
8977 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
8978 }
8979 if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
8980 ctxt->sax->startDocument(ctxt->userData);
8981
8982 /*
8983 * Doing validity checking on chunk doesn't make sense
8984 */
8985 ctxt->instate = XML_PARSER_CONTENT;
8986 ctxt->validate = 0;
8987 ctxt->loadsubset = 0;
8988 ctxt->depth = 0;
8989
8990 xmlParseContent(ctxt);
8991
8992 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008993 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008994 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008995 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008996 }
8997
8998 /*
8999 * SAX: end of the document processing.
9000 */
Daniel Veillard8d24cc12002-03-05 15:41:29 +00009001 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00009002 ctxt->sax->endDocument(ctxt->userData);
9003
9004 if (! ctxt->wellFormed) return(-1);
9005 return(0);
9006}
9007
Daniel Veillard73b013f2003-09-30 12:36:01 +00009008#ifdef LIBXML_PUSH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00009009/************************************************************************
9010 * *
9011 * Progressive parsing interfaces *
9012 * *
9013 ************************************************************************/
9014
9015/**
9016 * xmlParseLookupSequence:
9017 * @ctxt: an XML parser context
9018 * @first: the first char to lookup
9019 * @next: the next char to lookup or zero
9020 * @third: the next char to lookup or zero
9021 *
9022 * Try to find if a sequence (first, next, third) or just (first next) or
9023 * (first) is available in the input stream.
9024 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
9025 * to avoid rescanning sequences of bytes, it DOES change the state of the
9026 * parser, do not use liberally.
9027 *
9028 * Returns the index to the current parsing point if the full sequence
9029 * is available, -1 otherwise.
9030 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00009031static int
Owen Taylor3473f882001-02-23 17:55:21 +00009032xmlParseLookupSequence(xmlParserCtxtPtr ctxt, xmlChar first,
9033 xmlChar next, xmlChar third) {
9034 int base, len;
9035 xmlParserInputPtr in;
9036 const xmlChar *buf;
9037
9038 in = ctxt->input;
9039 if (in == NULL) return(-1);
9040 base = in->cur - in->base;
9041 if (base < 0) return(-1);
9042 if (ctxt->checkIndex > base)
9043 base = ctxt->checkIndex;
9044 if (in->buf == NULL) {
9045 buf = in->base;
9046 len = in->length;
9047 } else {
9048 buf = in->buf->buffer->content;
9049 len = in->buf->buffer->use;
9050 }
9051 /* take into account the sequence length */
9052 if (third) len -= 2;
9053 else if (next) len --;
9054 for (;base < len;base++) {
9055 if (buf[base] == first) {
9056 if (third != 0) {
9057 if ((buf[base + 1] != next) ||
9058 (buf[base + 2] != third)) continue;
9059 } else if (next != 0) {
9060 if (buf[base + 1] != next) continue;
9061 }
9062 ctxt->checkIndex = 0;
9063#ifdef DEBUG_PUSH
9064 if (next == 0)
9065 xmlGenericError(xmlGenericErrorContext,
9066 "PP: lookup '%c' found at %d\n",
9067 first, base);
9068 else if (third == 0)
9069 xmlGenericError(xmlGenericErrorContext,
9070 "PP: lookup '%c%c' found at %d\n",
9071 first, next, base);
9072 else
9073 xmlGenericError(xmlGenericErrorContext,
9074 "PP: lookup '%c%c%c' found at %d\n",
9075 first, next, third, base);
9076#endif
9077 return(base - (in->cur - in->base));
9078 }
9079 }
9080 ctxt->checkIndex = base;
9081#ifdef DEBUG_PUSH
9082 if (next == 0)
9083 xmlGenericError(xmlGenericErrorContext,
9084 "PP: lookup '%c' failed\n", first);
9085 else if (third == 0)
9086 xmlGenericError(xmlGenericErrorContext,
9087 "PP: lookup '%c%c' failed\n", first, next);
9088 else
9089 xmlGenericError(xmlGenericErrorContext,
9090 "PP: lookup '%c%c%c' failed\n", first, next, third);
9091#endif
9092 return(-1);
9093}
9094
9095/**
Daniel Veillarda880b122003-04-21 21:36:41 +00009096 * xmlParseGetLasts:
9097 * @ctxt: an XML parser context
9098 * @lastlt: pointer to store the last '<' from the input
9099 * @lastgt: pointer to store the last '>' from the input
9100 *
9101 * Lookup the last < and > in the current chunk
9102 */
9103static void
9104xmlParseGetLasts(xmlParserCtxtPtr ctxt, const xmlChar **lastlt,
9105 const xmlChar **lastgt) {
9106 const xmlChar *tmp;
9107
9108 if ((ctxt == NULL) || (lastlt == NULL) || (lastgt == NULL)) {
9109 xmlGenericError(xmlGenericErrorContext,
9110 "Internal error: xmlParseGetLasts\n");
9111 return;
9112 }
Daniel Veillard0df3bc32004-06-08 12:03:41 +00009113 if ((ctxt->progressive != 0) && (ctxt->inputNr == 1)) {
Daniel Veillarda880b122003-04-21 21:36:41 +00009114 tmp = ctxt->input->end;
9115 tmp--;
Daniel Veillardeb70f932004-07-05 16:46:09 +00009116 while ((tmp >= ctxt->input->base) && (*tmp != '<')) tmp--;
Daniel Veillarda880b122003-04-21 21:36:41 +00009117 if (tmp < ctxt->input->base) {
9118 *lastlt = NULL;
9119 *lastgt = NULL;
Daniel Veillarda880b122003-04-21 21:36:41 +00009120 } else {
Daniel Veillardeb70f932004-07-05 16:46:09 +00009121 *lastlt = tmp;
9122 tmp++;
9123 while ((tmp < ctxt->input->end) && (*tmp != '>')) {
9124 if (*tmp == '\'') {
9125 tmp++;
9126 while ((tmp < ctxt->input->end) && (*tmp != '\'')) tmp++;
9127 if (tmp < ctxt->input->end) tmp++;
9128 } else if (*tmp == '"') {
9129 tmp++;
9130 while ((tmp < ctxt->input->end) && (*tmp != '"')) tmp++;
9131 if (tmp < ctxt->input->end) tmp++;
9132 } else
9133 tmp++;
9134 }
9135 if (tmp < ctxt->input->end)
9136 *lastgt = tmp;
9137 else {
9138 tmp = *lastlt;
9139 tmp--;
9140 while ((tmp >= ctxt->input->base) && (*tmp != '>')) tmp--;
9141 if (tmp >= ctxt->input->base)
9142 *lastgt = tmp;
9143 else
9144 *lastgt = NULL;
9145 }
Daniel Veillarda880b122003-04-21 21:36:41 +00009146 }
Daniel Veillarda880b122003-04-21 21:36:41 +00009147 } else {
9148 *lastlt = NULL;
9149 *lastgt = NULL;
9150 }
9151}
9152/**
Owen Taylor3473f882001-02-23 17:55:21 +00009153 * xmlParseTryOrFinish:
9154 * @ctxt: an XML parser context
9155 * @terminate: last chunk indicator
9156 *
9157 * Try to progress on parsing
9158 *
9159 * Returns zero if no parsing was possible
9160 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00009161static int
Owen Taylor3473f882001-02-23 17:55:21 +00009162xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) {
9163 int ret = 0;
Daniel Veillardc82c57e2004-01-12 16:24:34 +00009164 int avail, tlen;
Owen Taylor3473f882001-02-23 17:55:21 +00009165 xmlChar cur, next;
Daniel Veillarda880b122003-04-21 21:36:41 +00009166 const xmlChar *lastlt, *lastgt;
Owen Taylor3473f882001-02-23 17:55:21 +00009167
Daniel Veillard36e5cd52004-11-02 14:52:23 +00009168 if (ctxt->input == NULL)
9169 return(0);
9170
Owen Taylor3473f882001-02-23 17:55:21 +00009171#ifdef DEBUG_PUSH
9172 switch (ctxt->instate) {
9173 case XML_PARSER_EOF:
9174 xmlGenericError(xmlGenericErrorContext,
9175 "PP: try EOF\n"); break;
9176 case XML_PARSER_START:
9177 xmlGenericError(xmlGenericErrorContext,
9178 "PP: try START\n"); break;
9179 case XML_PARSER_MISC:
9180 xmlGenericError(xmlGenericErrorContext,
9181 "PP: try MISC\n");break;
9182 case XML_PARSER_COMMENT:
9183 xmlGenericError(xmlGenericErrorContext,
9184 "PP: try COMMENT\n");break;
9185 case XML_PARSER_PROLOG:
9186 xmlGenericError(xmlGenericErrorContext,
9187 "PP: try PROLOG\n");break;
9188 case XML_PARSER_START_TAG:
9189 xmlGenericError(xmlGenericErrorContext,
9190 "PP: try START_TAG\n");break;
9191 case XML_PARSER_CONTENT:
9192 xmlGenericError(xmlGenericErrorContext,
9193 "PP: try CONTENT\n");break;
9194 case XML_PARSER_CDATA_SECTION:
9195 xmlGenericError(xmlGenericErrorContext,
9196 "PP: try CDATA_SECTION\n");break;
9197 case XML_PARSER_END_TAG:
9198 xmlGenericError(xmlGenericErrorContext,
9199 "PP: try END_TAG\n");break;
9200 case XML_PARSER_ENTITY_DECL:
9201 xmlGenericError(xmlGenericErrorContext,
9202 "PP: try ENTITY_DECL\n");break;
9203 case XML_PARSER_ENTITY_VALUE:
9204 xmlGenericError(xmlGenericErrorContext,
9205 "PP: try ENTITY_VALUE\n");break;
9206 case XML_PARSER_ATTRIBUTE_VALUE:
9207 xmlGenericError(xmlGenericErrorContext,
9208 "PP: try ATTRIBUTE_VALUE\n");break;
9209 case XML_PARSER_DTD:
9210 xmlGenericError(xmlGenericErrorContext,
9211 "PP: try DTD\n");break;
9212 case XML_PARSER_EPILOG:
9213 xmlGenericError(xmlGenericErrorContext,
9214 "PP: try EPILOG\n");break;
9215 case XML_PARSER_PI:
9216 xmlGenericError(xmlGenericErrorContext,
9217 "PP: try PI\n");break;
9218 case XML_PARSER_IGNORE:
9219 xmlGenericError(xmlGenericErrorContext,
9220 "PP: try IGNORE\n");break;
9221 }
9222#endif
9223
Daniel Veillard198c1bf2003-10-20 17:07:41 +00009224 if ((ctxt->input != NULL) &&
9225 (ctxt->input->cur - ctxt->input->base > 4096)) {
Daniel Veillarda880b122003-04-21 21:36:41 +00009226 xmlSHRINK(ctxt);
9227 ctxt->checkIndex = 0;
9228 }
9229 xmlParseGetLasts(ctxt, &lastlt, &lastgt);
Aleksey Sanine48a3182002-05-09 18:20:01 +00009230
Daniel Veillarda880b122003-04-21 21:36:41 +00009231 while (1) {
Daniel Veillard14412512005-01-21 23:53:26 +00009232 if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009233 return(0);
9234
9235
Owen Taylor3473f882001-02-23 17:55:21 +00009236 /*
9237 * Pop-up of finished entities.
9238 */
9239 while ((RAW == 0) && (ctxt->inputNr > 1))
9240 xmlPopInput(ctxt);
9241
Daniel Veillard198c1bf2003-10-20 17:07:41 +00009242 if (ctxt->input == NULL) break;
Owen Taylor3473f882001-02-23 17:55:21 +00009243 if (ctxt->input->buf == NULL)
Daniel Veillarda880b122003-04-21 21:36:41 +00009244 avail = ctxt->input->length -
9245 (ctxt->input->cur - ctxt->input->base);
Daniel Veillard158a4d22002-02-20 22:17:58 +00009246 else {
9247 /*
9248 * If we are operating on converted input, try to flush
9249 * remainng chars to avoid them stalling in the non-converted
9250 * buffer.
9251 */
9252 if ((ctxt->input->buf->raw != NULL) &&
9253 (ctxt->input->buf->raw->use > 0)) {
9254 int base = ctxt->input->base -
9255 ctxt->input->buf->buffer->content;
9256 int current = ctxt->input->cur - ctxt->input->base;
9257
9258 xmlParserInputBufferPush(ctxt->input->buf, 0, "");
9259 ctxt->input->base = ctxt->input->buf->buffer->content + base;
9260 ctxt->input->cur = ctxt->input->base + current;
9261 ctxt->input->end =
9262 &ctxt->input->buf->buffer->content[
9263 ctxt->input->buf->buffer->use];
9264 }
9265 avail = ctxt->input->buf->buffer->use -
9266 (ctxt->input->cur - ctxt->input->base);
9267 }
Owen Taylor3473f882001-02-23 17:55:21 +00009268 if (avail < 1)
9269 goto done;
9270 switch (ctxt->instate) {
9271 case XML_PARSER_EOF:
9272 /*
9273 * Document parsing is done !
9274 */
9275 goto done;
9276 case XML_PARSER_START:
Daniel Veillard0e4cd172001-06-28 12:13:56 +00009277 if (ctxt->charset == XML_CHAR_ENCODING_NONE) {
9278 xmlChar start[4];
9279 xmlCharEncoding enc;
9280
9281 /*
9282 * Very first chars read from the document flow.
9283 */
9284 if (avail < 4)
9285 goto done;
9286
9287 /*
9288 * Get the 4 first bytes and decode the charset
9289 * if enc != XML_CHAR_ENCODING_NONE
William M. Brack3a1cd212005-02-11 14:35:54 +00009290 * plug some encoding conversion routines,
9291 * else xmlSwitchEncoding will set to (default)
9292 * UTF8.
Daniel Veillard0e4cd172001-06-28 12:13:56 +00009293 */
9294 start[0] = RAW;
9295 start[1] = NXT(1);
9296 start[2] = NXT(2);
9297 start[3] = NXT(3);
9298 enc = xmlDetectCharEncoding(start, 4);
William M. Brack3a1cd212005-02-11 14:35:54 +00009299 xmlSwitchEncoding(ctxt, enc);
Daniel Veillard0e4cd172001-06-28 12:13:56 +00009300 break;
9301 }
Owen Taylor3473f882001-02-23 17:55:21 +00009302
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00009303 if (avail < 2)
9304 goto done;
Owen Taylor3473f882001-02-23 17:55:21 +00009305 cur = ctxt->input->cur[0];
9306 next = ctxt->input->cur[1];
9307 if (cur == 0) {
9308 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
9309 ctxt->sax->setDocumentLocator(ctxt->userData,
9310 &xmlDefaultSAXLocator);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009311 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00009312 ctxt->instate = XML_PARSER_EOF;
9313#ifdef DEBUG_PUSH
9314 xmlGenericError(xmlGenericErrorContext,
9315 "PP: entering EOF\n");
9316#endif
9317 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
9318 ctxt->sax->endDocument(ctxt->userData);
9319 goto done;
9320 }
9321 if ((cur == '<') && (next == '?')) {
9322 /* PI or XML decl */
9323 if (avail < 5) return(ret);
9324 if ((!terminate) &&
9325 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9326 return(ret);
9327 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
9328 ctxt->sax->setDocumentLocator(ctxt->userData,
9329 &xmlDefaultSAXLocator);
9330 if ((ctxt->input->cur[2] == 'x') &&
9331 (ctxt->input->cur[3] == 'm') &&
9332 (ctxt->input->cur[4] == 'l') &&
William M. Brack76e95df2003-10-18 16:20:14 +00009333 (IS_BLANK_CH(ctxt->input->cur[5]))) {
Owen Taylor3473f882001-02-23 17:55:21 +00009334 ret += 5;
9335#ifdef DEBUG_PUSH
9336 xmlGenericError(xmlGenericErrorContext,
9337 "PP: Parsing XML Decl\n");
9338#endif
9339 xmlParseXMLDecl(ctxt);
9340 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
9341 /*
9342 * The XML REC instructs us to stop parsing right
9343 * here
9344 */
9345 ctxt->instate = XML_PARSER_EOF;
9346 return(0);
9347 }
9348 ctxt->standalone = ctxt->input->standalone;
9349 if ((ctxt->encoding == NULL) &&
9350 (ctxt->input->encoding != NULL))
9351 ctxt->encoding = xmlStrdup(ctxt->input->encoding);
9352 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
9353 (!ctxt->disableSAX))
9354 ctxt->sax->startDocument(ctxt->userData);
9355 ctxt->instate = XML_PARSER_MISC;
9356#ifdef DEBUG_PUSH
9357 xmlGenericError(xmlGenericErrorContext,
9358 "PP: entering MISC\n");
9359#endif
9360 } else {
9361 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
9362 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
9363 (!ctxt->disableSAX))
9364 ctxt->sax->startDocument(ctxt->userData);
9365 ctxt->instate = XML_PARSER_MISC;
9366#ifdef DEBUG_PUSH
9367 xmlGenericError(xmlGenericErrorContext,
9368 "PP: entering MISC\n");
9369#endif
9370 }
9371 } else {
9372 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
9373 ctxt->sax->setDocumentLocator(ctxt->userData,
9374 &xmlDefaultSAXLocator);
9375 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
William M. Bracka3215c72004-07-31 16:24:01 +00009376 if (ctxt->version == NULL) {
9377 xmlErrMemory(ctxt, NULL);
9378 break;
9379 }
Owen Taylor3473f882001-02-23 17:55:21 +00009380 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
9381 (!ctxt->disableSAX))
9382 ctxt->sax->startDocument(ctxt->userData);
9383 ctxt->instate = XML_PARSER_MISC;
9384#ifdef DEBUG_PUSH
9385 xmlGenericError(xmlGenericErrorContext,
9386 "PP: entering MISC\n");
9387#endif
9388 }
9389 break;
Daniel Veillarda880b122003-04-21 21:36:41 +00009390 case XML_PARSER_START_TAG: {
Daniel Veillarde57ec792003-09-10 10:50:59 +00009391 const xmlChar *name;
9392 const xmlChar *prefix;
9393 const xmlChar *URI;
9394 int nsNr = ctxt->nsNr;
Daniel Veillarda880b122003-04-21 21:36:41 +00009395
9396 if ((avail < 2) && (ctxt->inputNr == 1))
9397 goto done;
9398 cur = ctxt->input->cur[0];
9399 if (cur != '<') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009400 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Daniel Veillarda880b122003-04-21 21:36:41 +00009401 ctxt->instate = XML_PARSER_EOF;
Daniel Veillarda880b122003-04-21 21:36:41 +00009402 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
9403 ctxt->sax->endDocument(ctxt->userData);
9404 goto done;
9405 }
9406 if (!terminate) {
9407 if (ctxt->progressive) {
Daniel Veillardb3744002004-02-18 14:28:22 +00009408 /* > can be found unescaped in attribute values */
Daniel Veillardeb70f932004-07-05 16:46:09 +00009409 if ((lastgt == NULL) || (ctxt->input->cur >= lastgt))
Daniel Veillarda880b122003-04-21 21:36:41 +00009410 goto done;
9411 } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) {
9412 goto done;
9413 }
9414 }
9415 if (ctxt->spaceNr == 0)
9416 spacePush(ctxt, -1);
9417 else
9418 spacePush(ctxt, *ctxt->space);
Daniel Veillard81273902003-09-30 00:43:48 +00009419#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009420 if (ctxt->sax2)
Daniel Veillard81273902003-09-30 00:43:48 +00009421#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00009422 name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen);
Daniel Veillard81273902003-09-30 00:43:48 +00009423#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009424 else
9425 name = xmlParseStartTag(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00009426#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarda880b122003-04-21 21:36:41 +00009427 if (name == NULL) {
9428 spacePop(ctxt);
9429 ctxt->instate = XML_PARSER_EOF;
Daniel Veillarda880b122003-04-21 21:36:41 +00009430 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
9431 ctxt->sax->endDocument(ctxt->userData);
9432 goto done;
9433 }
Daniel Veillard4432df22003-09-28 18:58:27 +00009434#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda880b122003-04-21 21:36:41 +00009435 /*
9436 * [ VC: Root Element Type ]
9437 * The Name in the document type declaration must match
9438 * the element type of the root element.
9439 */
9440 if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
9441 ctxt->node && (ctxt->node == ctxt->myDoc->children))
9442 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillard4432df22003-09-28 18:58:27 +00009443#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda880b122003-04-21 21:36:41 +00009444
9445 /*
9446 * Check for an Empty Element.
9447 */
9448 if ((RAW == '/') && (NXT(1) == '>')) {
9449 SKIP(2);
Daniel Veillarde57ec792003-09-10 10:50:59 +00009450
9451 if (ctxt->sax2) {
9452 if ((ctxt->sax != NULL) &&
9453 (ctxt->sax->endElementNs != NULL) &&
9454 (!ctxt->disableSAX))
9455 ctxt->sax->endElementNs(ctxt->userData, name,
9456 prefix, URI);
Daniel Veillard48df9612005-01-04 21:50:05 +00009457 if (ctxt->nsNr - nsNr > 0)
9458 nsPop(ctxt, ctxt->nsNr - nsNr);
Daniel Veillard81273902003-09-30 00:43:48 +00009459#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009460 } else {
9461 if ((ctxt->sax != NULL) &&
9462 (ctxt->sax->endElement != NULL) &&
9463 (!ctxt->disableSAX))
9464 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard81273902003-09-30 00:43:48 +00009465#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarda880b122003-04-21 21:36:41 +00009466 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009467 spacePop(ctxt);
9468 if (ctxt->nameNr == 0) {
Daniel Veillarda880b122003-04-21 21:36:41 +00009469 ctxt->instate = XML_PARSER_EPILOG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009470 } else {
9471 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillarda880b122003-04-21 21:36:41 +00009472 }
9473 break;
9474 }
9475 if (RAW == '>') {
9476 NEXT;
9477 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00009478 xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED,
Daniel Veillarda880b122003-04-21 21:36:41 +00009479 "Couldn't find end of Start Tag %s\n",
9480 name);
Daniel Veillarda880b122003-04-21 21:36:41 +00009481 nodePop(ctxt);
Daniel Veillarda880b122003-04-21 21:36:41 +00009482 spacePop(ctxt);
Daniel Veillarda880b122003-04-21 21:36:41 +00009483 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009484 if (ctxt->sax2)
9485 nameNsPush(ctxt, name, prefix, URI, ctxt->nsNr - nsNr);
Daniel Veillard81273902003-09-30 00:43:48 +00009486#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009487 else
9488 namePush(ctxt, name);
Daniel Veillard81273902003-09-30 00:43:48 +00009489#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00009490
Daniel Veillarda880b122003-04-21 21:36:41 +00009491 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillarda880b122003-04-21 21:36:41 +00009492 break;
9493 }
9494 case XML_PARSER_CONTENT: {
9495 const xmlChar *test;
9496 unsigned int cons;
9497 if ((avail < 2) && (ctxt->inputNr == 1))
9498 goto done;
9499 cur = ctxt->input->cur[0];
9500 next = ctxt->input->cur[1];
9501
9502 test = CUR_PTR;
9503 cons = ctxt->input->consumed;
9504 if ((cur == '<') && (next == '/')) {
9505 ctxt->instate = XML_PARSER_END_TAG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009506 break;
9507 } else if ((cur == '<') && (next == '?')) {
9508 if ((!terminate) &&
9509 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9510 goto done;
Daniel Veillarda880b122003-04-21 21:36:41 +00009511 xmlParsePI(ctxt);
9512 } else if ((cur == '<') && (next != '!')) {
9513 ctxt->instate = XML_PARSER_START_TAG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009514 break;
9515 } else if ((cur == '<') && (next == '!') &&
9516 (ctxt->input->cur[2] == '-') &&
9517 (ctxt->input->cur[3] == '-')) {
9518 if ((!terminate) &&
9519 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9520 goto done;
Daniel Veillarda880b122003-04-21 21:36:41 +00009521 xmlParseComment(ctxt);
9522 ctxt->instate = XML_PARSER_CONTENT;
9523 } else if ((cur == '<') && (ctxt->input->cur[1] == '!') &&
9524 (ctxt->input->cur[2] == '[') &&
9525 (ctxt->input->cur[3] == 'C') &&
9526 (ctxt->input->cur[4] == 'D') &&
9527 (ctxt->input->cur[5] == 'A') &&
9528 (ctxt->input->cur[6] == 'T') &&
9529 (ctxt->input->cur[7] == 'A') &&
9530 (ctxt->input->cur[8] == '[')) {
9531 SKIP(9);
9532 ctxt->instate = XML_PARSER_CDATA_SECTION;
Daniel Veillarda880b122003-04-21 21:36:41 +00009533 break;
9534 } else if ((cur == '<') && (next == '!') &&
9535 (avail < 9)) {
9536 goto done;
9537 } else if (cur == '&') {
9538 if ((!terminate) &&
9539 (xmlParseLookupSequence(ctxt, ';', 0, 0) < 0))
9540 goto done;
Daniel Veillarda880b122003-04-21 21:36:41 +00009541 xmlParseReference(ctxt);
9542 } else {
9543 /* TODO Avoid the extra copy, handle directly !!! */
9544 /*
9545 * Goal of the following test is:
9546 * - minimize calls to the SAX 'character' callback
9547 * when they are mergeable
9548 * - handle an problem for isBlank when we only parse
9549 * a sequence of blank chars and the next one is
9550 * not available to check against '<' presence.
9551 * - tries to homogenize the differences in SAX
9552 * callbacks between the push and pull versions
9553 * of the parser.
9554 */
9555 if ((ctxt->inputNr == 1) &&
9556 (avail < XML_PARSER_BIG_BUFFER_SIZE)) {
9557 if (!terminate) {
9558 if (ctxt->progressive) {
9559 if ((lastlt == NULL) ||
9560 (ctxt->input->cur > lastlt))
9561 goto done;
9562 } else if (xmlParseLookupSequence(ctxt,
9563 '<', 0, 0) < 0) {
9564 goto done;
9565 }
9566 }
9567 }
9568 ctxt->checkIndex = 0;
Daniel Veillarda880b122003-04-21 21:36:41 +00009569 xmlParseCharData(ctxt, 0);
9570 }
9571 /*
9572 * Pop-up of finished entities.
9573 */
9574 while ((RAW == 0) && (ctxt->inputNr > 1))
9575 xmlPopInput(ctxt);
9576 if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009577 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
9578 "detected an error in element content\n");
Daniel Veillarda880b122003-04-21 21:36:41 +00009579 ctxt->instate = XML_PARSER_EOF;
9580 break;
9581 }
9582 break;
9583 }
9584 case XML_PARSER_END_TAG:
9585 if (avail < 2)
9586 goto done;
9587 if (!terminate) {
9588 if (ctxt->progressive) {
Daniel Veillardeb70f932004-07-05 16:46:09 +00009589 /* > can be found unescaped in attribute values */
9590 if ((lastgt == NULL) || (ctxt->input->cur >= lastgt))
Daniel Veillarda880b122003-04-21 21:36:41 +00009591 goto done;
9592 } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) {
9593 goto done;
9594 }
9595 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009596 if (ctxt->sax2) {
9597 xmlParseEndTag2(ctxt,
9598 (void *) ctxt->pushTab[ctxt->nameNr * 3 - 3],
9599 (void *) ctxt->pushTab[ctxt->nameNr * 3 - 2], 0,
Daniel Veillardc82c57e2004-01-12 16:24:34 +00009600 (int) (long) ctxt->pushTab[ctxt->nameNr * 3 - 1], 0);
Daniel Veillarde57ec792003-09-10 10:50:59 +00009601 nameNsPop(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00009602 }
9603#ifdef LIBXML_SAX1_ENABLED
9604 else
Daniel Veillarde57ec792003-09-10 10:50:59 +00009605 xmlParseEndTag1(ctxt, 0);
Daniel Veillard81273902003-09-30 00:43:48 +00009606#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00009607 if (ctxt->nameNr == 0) {
Daniel Veillarda880b122003-04-21 21:36:41 +00009608 ctxt->instate = XML_PARSER_EPILOG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009609 } else {
9610 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillarda880b122003-04-21 21:36:41 +00009611 }
9612 break;
9613 case XML_PARSER_CDATA_SECTION: {
9614 /*
9615 * The Push mode need to have the SAX callback for
9616 * cdataBlock merge back contiguous callbacks.
9617 */
9618 int base;
9619
9620 base = xmlParseLookupSequence(ctxt, ']', ']', '>');
9621 if (base < 0) {
9622 if (avail >= XML_PARSER_BIG_BUFFER_SIZE + 2) {
9623 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
9624 if (ctxt->sax->cdataBlock != NULL)
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00009625 ctxt->sax->cdataBlock(ctxt->userData,
9626 ctxt->input->cur,
9627 XML_PARSER_BIG_BUFFER_SIZE);
9628 else if (ctxt->sax->characters != NULL)
9629 ctxt->sax->characters(ctxt->userData,
9630 ctxt->input->cur,
Daniel Veillarda880b122003-04-21 21:36:41 +00009631 XML_PARSER_BIG_BUFFER_SIZE);
9632 }
Daniel Veillard0b787f32004-03-26 17:29:53 +00009633 SKIPL(XML_PARSER_BIG_BUFFER_SIZE);
Daniel Veillarda880b122003-04-21 21:36:41 +00009634 ctxt->checkIndex = 0;
9635 }
9636 goto done;
9637 } else {
9638 if ((ctxt->sax != NULL) && (base > 0) &&
9639 (!ctxt->disableSAX)) {
9640 if (ctxt->sax->cdataBlock != NULL)
9641 ctxt->sax->cdataBlock(ctxt->userData,
9642 ctxt->input->cur, base);
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00009643 else if (ctxt->sax->characters != NULL)
9644 ctxt->sax->characters(ctxt->userData,
9645 ctxt->input->cur, base);
Daniel Veillarda880b122003-04-21 21:36:41 +00009646 }
Daniel Veillard0b787f32004-03-26 17:29:53 +00009647 SKIPL(base + 3);
Daniel Veillarda880b122003-04-21 21:36:41 +00009648 ctxt->checkIndex = 0;
9649 ctxt->instate = XML_PARSER_CONTENT;
9650#ifdef DEBUG_PUSH
9651 xmlGenericError(xmlGenericErrorContext,
9652 "PP: entering CONTENT\n");
9653#endif
9654 }
9655 break;
9656 }
Owen Taylor3473f882001-02-23 17:55:21 +00009657 case XML_PARSER_MISC:
9658 SKIP_BLANKS;
9659 if (ctxt->input->buf == NULL)
Daniel Veillarda880b122003-04-21 21:36:41 +00009660 avail = ctxt->input->length -
9661 (ctxt->input->cur - ctxt->input->base);
Owen Taylor3473f882001-02-23 17:55:21 +00009662 else
Daniel Veillarda880b122003-04-21 21:36:41 +00009663 avail = ctxt->input->buf->buffer->use -
9664 (ctxt->input->cur - ctxt->input->base);
Owen Taylor3473f882001-02-23 17:55:21 +00009665 if (avail < 2)
9666 goto done;
9667 cur = ctxt->input->cur[0];
9668 next = ctxt->input->cur[1];
9669 if ((cur == '<') && (next == '?')) {
9670 if ((!terminate) &&
9671 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9672 goto done;
9673#ifdef DEBUG_PUSH
9674 xmlGenericError(xmlGenericErrorContext,
9675 "PP: Parsing PI\n");
9676#endif
9677 xmlParsePI(ctxt);
9678 } else if ((cur == '<') && (next == '!') &&
Daniel Veillarda880b122003-04-21 21:36:41 +00009679 (ctxt->input->cur[2] == '-') &&
9680 (ctxt->input->cur[3] == '-')) {
Owen Taylor3473f882001-02-23 17:55:21 +00009681 if ((!terminate) &&
9682 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9683 goto done;
9684#ifdef DEBUG_PUSH
9685 xmlGenericError(xmlGenericErrorContext,
9686 "PP: Parsing Comment\n");
9687#endif
9688 xmlParseComment(ctxt);
9689 ctxt->instate = XML_PARSER_MISC;
9690 } else if ((cur == '<') && (next == '!') &&
Daniel Veillarda880b122003-04-21 21:36:41 +00009691 (ctxt->input->cur[2] == 'D') &&
9692 (ctxt->input->cur[3] == 'O') &&
9693 (ctxt->input->cur[4] == 'C') &&
9694 (ctxt->input->cur[5] == 'T') &&
9695 (ctxt->input->cur[6] == 'Y') &&
9696 (ctxt->input->cur[7] == 'P') &&
Owen Taylor3473f882001-02-23 17:55:21 +00009697 (ctxt->input->cur[8] == 'E')) {
9698 if ((!terminate) &&
9699 (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
9700 goto done;
9701#ifdef DEBUG_PUSH
9702 xmlGenericError(xmlGenericErrorContext,
9703 "PP: Parsing internal subset\n");
9704#endif
9705 ctxt->inSubset = 1;
9706 xmlParseDocTypeDecl(ctxt);
9707 if (RAW == '[') {
9708 ctxt->instate = XML_PARSER_DTD;
9709#ifdef DEBUG_PUSH
9710 xmlGenericError(xmlGenericErrorContext,
9711 "PP: entering DTD\n");
9712#endif
9713 } else {
9714 /*
9715 * Create and update the external subset.
9716 */
9717 ctxt->inSubset = 2;
9718 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
9719 (ctxt->sax->externalSubset != NULL))
9720 ctxt->sax->externalSubset(ctxt->userData,
9721 ctxt->intSubName, ctxt->extSubSystem,
9722 ctxt->extSubURI);
9723 ctxt->inSubset = 0;
9724 ctxt->instate = XML_PARSER_PROLOG;
9725#ifdef DEBUG_PUSH
9726 xmlGenericError(xmlGenericErrorContext,
9727 "PP: entering PROLOG\n");
9728#endif
9729 }
9730 } else if ((cur == '<') && (next == '!') &&
9731 (avail < 9)) {
9732 goto done;
9733 } else {
9734 ctxt->instate = XML_PARSER_START_TAG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009735 ctxt->progressive = 1;
9736 xmlParseGetLasts(ctxt, &lastlt, &lastgt);
Owen Taylor3473f882001-02-23 17:55:21 +00009737#ifdef DEBUG_PUSH
9738 xmlGenericError(xmlGenericErrorContext,
9739 "PP: entering START_TAG\n");
9740#endif
9741 }
9742 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009743 case XML_PARSER_PROLOG:
9744 SKIP_BLANKS;
9745 if (ctxt->input->buf == NULL)
9746 avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base);
9747 else
9748 avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base);
9749 if (avail < 2)
9750 goto done;
9751 cur = ctxt->input->cur[0];
9752 next = ctxt->input->cur[1];
9753 if ((cur == '<') && (next == '?')) {
9754 if ((!terminate) &&
9755 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9756 goto done;
9757#ifdef DEBUG_PUSH
9758 xmlGenericError(xmlGenericErrorContext,
9759 "PP: Parsing PI\n");
9760#endif
9761 xmlParsePI(ctxt);
9762 } else if ((cur == '<') && (next == '!') &&
9763 (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) {
9764 if ((!terminate) &&
9765 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9766 goto done;
9767#ifdef DEBUG_PUSH
9768 xmlGenericError(xmlGenericErrorContext,
9769 "PP: Parsing Comment\n");
9770#endif
9771 xmlParseComment(ctxt);
9772 ctxt->instate = XML_PARSER_PROLOG;
9773 } else if ((cur == '<') && (next == '!') &&
9774 (avail < 4)) {
9775 goto done;
9776 } else {
9777 ctxt->instate = XML_PARSER_START_TAG;
Daniel Veillard0df3bc32004-06-08 12:03:41 +00009778 if (ctxt->progressive == 0)
9779 ctxt->progressive = 1;
Daniel Veillarda880b122003-04-21 21:36:41 +00009780 xmlParseGetLasts(ctxt, &lastlt, &lastgt);
Owen Taylor3473f882001-02-23 17:55:21 +00009781#ifdef DEBUG_PUSH
9782 xmlGenericError(xmlGenericErrorContext,
9783 "PP: entering START_TAG\n");
9784#endif
9785 }
9786 break;
9787 case XML_PARSER_EPILOG:
9788 SKIP_BLANKS;
9789 if (ctxt->input->buf == NULL)
9790 avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base);
9791 else
9792 avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base);
9793 if (avail < 2)
9794 goto done;
9795 cur = ctxt->input->cur[0];
9796 next = ctxt->input->cur[1];
9797 if ((cur == '<') && (next == '?')) {
9798 if ((!terminate) &&
9799 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9800 goto done;
9801#ifdef DEBUG_PUSH
9802 xmlGenericError(xmlGenericErrorContext,
9803 "PP: Parsing PI\n");
9804#endif
9805 xmlParsePI(ctxt);
9806 ctxt->instate = XML_PARSER_EPILOG;
9807 } else if ((cur == '<') && (next == '!') &&
9808 (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) {
9809 if ((!terminate) &&
9810 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9811 goto done;
9812#ifdef DEBUG_PUSH
9813 xmlGenericError(xmlGenericErrorContext,
9814 "PP: Parsing Comment\n");
9815#endif
9816 xmlParseComment(ctxt);
9817 ctxt->instate = XML_PARSER_EPILOG;
9818 } else if ((cur == '<') && (next == '!') &&
9819 (avail < 4)) {
9820 goto done;
9821 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009822 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00009823 ctxt->instate = XML_PARSER_EOF;
9824#ifdef DEBUG_PUSH
9825 xmlGenericError(xmlGenericErrorContext,
9826 "PP: entering EOF\n");
9827#endif
Daniel Veillard8d24cc12002-03-05 15:41:29 +00009828 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00009829 ctxt->sax->endDocument(ctxt->userData);
9830 goto done;
9831 }
9832 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009833 case XML_PARSER_DTD: {
9834 /*
9835 * Sorry but progressive parsing of the internal subset
9836 * is not expected to be supported. We first check that
9837 * the full content of the internal subset is available and
9838 * the parsing is launched only at that point.
9839 * Internal subset ends up with "']' S? '>'" in an unescaped
9840 * section and not in a ']]>' sequence which are conditional
9841 * sections (whoever argued to keep that crap in XML deserve
9842 * a place in hell !).
9843 */
9844 int base, i;
9845 xmlChar *buf;
9846 xmlChar quote = 0;
9847
9848 base = ctxt->input->cur - ctxt->input->base;
9849 if (base < 0) return(0);
9850 if (ctxt->checkIndex > base)
9851 base = ctxt->checkIndex;
9852 buf = ctxt->input->buf->buffer->content;
9853 for (;(unsigned int) base < ctxt->input->buf->buffer->use;
9854 base++) {
9855 if (quote != 0) {
9856 if (buf[base] == quote)
9857 quote = 0;
9858 continue;
9859 }
Daniel Veillard036143b2004-02-12 11:57:52 +00009860 if ((quote == 0) && (buf[base] == '<')) {
9861 int found = 0;
9862 /* special handling of comments */
9863 if (((unsigned int) base + 4 <
9864 ctxt->input->buf->buffer->use) &&
9865 (buf[base + 1] == '!') &&
9866 (buf[base + 2] == '-') &&
9867 (buf[base + 3] == '-')) {
9868 for (;(unsigned int) base + 3 <
9869 ctxt->input->buf->buffer->use; base++) {
9870 if ((buf[base] == '-') &&
9871 (buf[base + 1] == '-') &&
9872 (buf[base + 2] == '>')) {
9873 found = 1;
9874 base += 2;
9875 break;
9876 }
9877 }
Daniel Veillard8f8a9dd2005-01-25 21:41:42 +00009878 if (!found) {
9879#if 0
9880 fprintf(stderr, "unfinished comment\n");
9881#endif
9882 break; /* for */
9883 }
Daniel Veillard036143b2004-02-12 11:57:52 +00009884 continue;
9885 }
9886 }
Owen Taylor3473f882001-02-23 17:55:21 +00009887 if (buf[base] == '"') {
9888 quote = '"';
9889 continue;
9890 }
9891 if (buf[base] == '\'') {
9892 quote = '\'';
9893 continue;
9894 }
9895 if (buf[base] == ']') {
Daniel Veillard8f8a9dd2005-01-25 21:41:42 +00009896#if 0
9897 fprintf(stderr, "%c%c%c%c: ", buf[base],
9898 buf[base + 1], buf[base + 2], buf[base + 3]);
9899#endif
Owen Taylor3473f882001-02-23 17:55:21 +00009900 if ((unsigned int) base +1 >=
9901 ctxt->input->buf->buffer->use)
9902 break;
9903 if (buf[base + 1] == ']') {
9904 /* conditional crap, skip both ']' ! */
9905 base++;
9906 continue;
9907 }
Daniel Veillard8f8a9dd2005-01-25 21:41:42 +00009908 for (i = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00009909 (unsigned int) base + i < ctxt->input->buf->buffer->use;
9910 i++) {
Daniel Veillard8f8a9dd2005-01-25 21:41:42 +00009911 if (buf[base + i] == '>') {
9912#if 0
9913 fprintf(stderr, "found\n");
9914#endif
Owen Taylor3473f882001-02-23 17:55:21 +00009915 goto found_end_int_subset;
Daniel Veillard8f8a9dd2005-01-25 21:41:42 +00009916 }
9917 if (!IS_BLANK_CH(buf[base + i])) {
9918#if 0
9919 fprintf(stderr, "not found\n");
9920#endif
9921 goto not_end_of_int_subset;
9922 }
Owen Taylor3473f882001-02-23 17:55:21 +00009923 }
Daniel Veillard8f8a9dd2005-01-25 21:41:42 +00009924#if 0
9925 fprintf(stderr, "end of stream\n");
9926#endif
Owen Taylor3473f882001-02-23 17:55:21 +00009927 break;
Daniel Veillard8f8a9dd2005-01-25 21:41:42 +00009928
Owen Taylor3473f882001-02-23 17:55:21 +00009929 }
Daniel Veillard8f8a9dd2005-01-25 21:41:42 +00009930not_end_of_int_subset:
9931 continue; /* for */
Owen Taylor3473f882001-02-23 17:55:21 +00009932 }
9933 /*
9934 * We didn't found the end of the Internal subset
9935 */
Owen Taylor3473f882001-02-23 17:55:21 +00009936#ifdef DEBUG_PUSH
9937 if (next == 0)
9938 xmlGenericError(xmlGenericErrorContext,
9939 "PP: lookup of int subset end filed\n");
9940#endif
9941 goto done;
9942
9943found_end_int_subset:
9944 xmlParseInternalSubset(ctxt);
9945 ctxt->inSubset = 2;
9946 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
9947 (ctxt->sax->externalSubset != NULL))
9948 ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
9949 ctxt->extSubSystem, ctxt->extSubURI);
9950 ctxt->inSubset = 0;
9951 ctxt->instate = XML_PARSER_PROLOG;
9952 ctxt->checkIndex = 0;
9953#ifdef DEBUG_PUSH
9954 xmlGenericError(xmlGenericErrorContext,
9955 "PP: entering PROLOG\n");
9956#endif
9957 break;
9958 }
9959 case XML_PARSER_COMMENT:
9960 xmlGenericError(xmlGenericErrorContext,
9961 "PP: internal error, state == COMMENT\n");
9962 ctxt->instate = XML_PARSER_CONTENT;
9963#ifdef DEBUG_PUSH
9964 xmlGenericError(xmlGenericErrorContext,
9965 "PP: entering CONTENT\n");
9966#endif
9967 break;
Daniel Veillarda880b122003-04-21 21:36:41 +00009968 case XML_PARSER_IGNORE:
9969 xmlGenericError(xmlGenericErrorContext,
9970 "PP: internal error, state == IGNORE");
9971 ctxt->instate = XML_PARSER_DTD;
9972#ifdef DEBUG_PUSH
9973 xmlGenericError(xmlGenericErrorContext,
9974 "PP: entering DTD\n");
9975#endif
9976 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009977 case XML_PARSER_PI:
9978 xmlGenericError(xmlGenericErrorContext,
9979 "PP: internal error, state == PI\n");
9980 ctxt->instate = XML_PARSER_CONTENT;
9981#ifdef DEBUG_PUSH
9982 xmlGenericError(xmlGenericErrorContext,
9983 "PP: entering CONTENT\n");
9984#endif
9985 break;
9986 case XML_PARSER_ENTITY_DECL:
9987 xmlGenericError(xmlGenericErrorContext,
9988 "PP: internal error, state == ENTITY_DECL\n");
9989 ctxt->instate = XML_PARSER_DTD;
9990#ifdef DEBUG_PUSH
9991 xmlGenericError(xmlGenericErrorContext,
9992 "PP: entering DTD\n");
9993#endif
9994 break;
9995 case XML_PARSER_ENTITY_VALUE:
9996 xmlGenericError(xmlGenericErrorContext,
9997 "PP: internal error, state == ENTITY_VALUE\n");
9998 ctxt->instate = XML_PARSER_CONTENT;
9999#ifdef DEBUG_PUSH
10000 xmlGenericError(xmlGenericErrorContext,
10001 "PP: entering DTD\n");
10002#endif
10003 break;
10004 case XML_PARSER_ATTRIBUTE_VALUE:
10005 xmlGenericError(xmlGenericErrorContext,
10006 "PP: internal error, state == ATTRIBUTE_VALUE\n");
10007 ctxt->instate = XML_PARSER_START_TAG;
10008#ifdef DEBUG_PUSH
10009 xmlGenericError(xmlGenericErrorContext,
10010 "PP: entering START_TAG\n");
10011#endif
10012 break;
10013 case XML_PARSER_SYSTEM_LITERAL:
10014 xmlGenericError(xmlGenericErrorContext,
10015 "PP: internal error, state == SYSTEM_LITERAL\n");
10016 ctxt->instate = XML_PARSER_START_TAG;
10017#ifdef DEBUG_PUSH
10018 xmlGenericError(xmlGenericErrorContext,
10019 "PP: entering START_TAG\n");
10020#endif
10021 break;
Daniel Veillard4a7ae502002-02-18 19:18:17 +000010022 case XML_PARSER_PUBLIC_LITERAL:
10023 xmlGenericError(xmlGenericErrorContext,
10024 "PP: internal error, state == PUBLIC_LITERAL\n");
10025 ctxt->instate = XML_PARSER_START_TAG;
10026#ifdef DEBUG_PUSH
10027 xmlGenericError(xmlGenericErrorContext,
10028 "PP: entering START_TAG\n");
10029#endif
10030 break;
Owen Taylor3473f882001-02-23 17:55:21 +000010031 }
10032 }
10033done:
10034#ifdef DEBUG_PUSH
10035 xmlGenericError(xmlGenericErrorContext, "PP: done %d\n", ret);
10036#endif
10037 return(ret);
10038}
10039
10040/**
Owen Taylor3473f882001-02-23 17:55:21 +000010041 * xmlParseChunk:
10042 * @ctxt: an XML parser context
10043 * @chunk: an char array
10044 * @size: the size in byte of the chunk
10045 * @terminate: last chunk indicator
10046 *
10047 * Parse a Chunk of memory
10048 *
10049 * Returns zero if no error, the xmlParserErrors otherwise.
10050 */
10051int
10052xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size,
10053 int terminate) {
Daniel Veillard36e5cd52004-11-02 14:52:23 +000010054 if (ctxt == NULL)
10055 return(XML_ERR_INTERNAL_ERROR);
Daniel Veillard14412512005-01-21 23:53:26 +000010056 if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000010057 return(ctxt->errNo);
Daniel Veillard309f81d2003-09-23 09:02:53 +000010058 if (ctxt->instate == XML_PARSER_START)
10059 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010060 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
10061 (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) {
10062 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
10063 int cur = ctxt->input->cur - ctxt->input->base;
William M. Bracka3215c72004-07-31 16:24:01 +000010064 int res;
Owen Taylor3473f882001-02-23 17:55:21 +000010065
William M. Bracka3215c72004-07-31 16:24:01 +000010066 res =xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
10067 if (res < 0) {
10068 ctxt->errNo = XML_PARSER_EOF;
10069 ctxt->disableSAX = 1;
10070 return (XML_PARSER_EOF);
10071 }
Owen Taylor3473f882001-02-23 17:55:21 +000010072 ctxt->input->base = ctxt->input->buf->buffer->content + base;
10073 ctxt->input->cur = ctxt->input->base + cur;
Daniel Veillard48b2f892001-02-25 16:11:03 +000010074 ctxt->input->end =
10075 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +000010076#ifdef DEBUG_PUSH
10077 xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
10078#endif
10079
Owen Taylor3473f882001-02-23 17:55:21 +000010080 } else if (ctxt->instate != XML_PARSER_EOF) {
10081 if ((ctxt->input != NULL) && ctxt->input->buf != NULL) {
10082 xmlParserInputBufferPtr in = ctxt->input->buf;
10083 if ((in->encoder != NULL) && (in->buffer != NULL) &&
10084 (in->raw != NULL)) {
10085 int nbchars;
10086
10087 nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
10088 if (nbchars < 0) {
Daniel Veillard24eb9782003-10-04 21:08:09 +000010089 /* TODO 2.6.0 */
Owen Taylor3473f882001-02-23 17:55:21 +000010090 xmlGenericError(xmlGenericErrorContext,
10091 "xmlParseChunk: encoder error\n");
10092 return(XML_ERR_INVALID_ENCODING);
10093 }
10094 }
10095 }
10096 }
10097 xmlParseTryOrFinish(ctxt, terminate);
Daniel Veillard14412512005-01-21 23:53:26 +000010098 if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000010099 return(ctxt->errNo);
Owen Taylor3473f882001-02-23 17:55:21 +000010100 if (terminate) {
10101 /*
10102 * Check for termination
10103 */
Daniel Veillard36e5cd52004-11-02 14:52:23 +000010104 int avail = 0;
10105
10106 if (ctxt->input != NULL) {
Daniel Veillard819d5cb2002-10-14 11:15:18 +000010107 if (ctxt->input->buf == NULL)
Daniel Veillard36e5cd52004-11-02 14:52:23 +000010108 avail = ctxt->input->length -
10109 (ctxt->input->cur - ctxt->input->base);
10110 else
10111 avail = ctxt->input->buf->buffer->use -
10112 (ctxt->input->cur - ctxt->input->base);
10113 }
Daniel Veillard819d5cb2002-10-14 11:15:18 +000010114
Owen Taylor3473f882001-02-23 17:55:21 +000010115 if ((ctxt->instate != XML_PARSER_EOF) &&
10116 (ctxt->instate != XML_PARSER_EPILOG)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010117 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010118 }
Daniel Veillard819d5cb2002-10-14 11:15:18 +000010119 if ((ctxt->instate == XML_PARSER_EPILOG) && (avail > 0)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010120 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Daniel Veillard819d5cb2002-10-14 11:15:18 +000010121 }
Owen Taylor3473f882001-02-23 17:55:21 +000010122 if (ctxt->instate != XML_PARSER_EOF) {
Daniel Veillard8d24cc12002-03-05 15:41:29 +000010123 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +000010124 ctxt->sax->endDocument(ctxt->userData);
10125 }
10126 ctxt->instate = XML_PARSER_EOF;
10127 }
10128 return((xmlParserErrors) ctxt->errNo);
10129}
10130
10131/************************************************************************
10132 * *
10133 * I/O front end functions to the parser *
10134 * *
10135 ************************************************************************/
10136
10137/**
10138 * xmlStopParser:
10139 * @ctxt: an XML parser context
10140 *
10141 * Blocks further parser processing
10142 */
10143void
10144xmlStopParser(xmlParserCtxtPtr ctxt) {
Daniel Veillard157fee02003-10-31 10:36:03 +000010145 if (ctxt == NULL)
10146 return;
Owen Taylor3473f882001-02-23 17:55:21 +000010147 ctxt->instate = XML_PARSER_EOF;
Daniel Veillard157fee02003-10-31 10:36:03 +000010148 ctxt->disableSAX = 1;
William M. Brack230c5502004-12-20 16:18:49 +000010149 if (ctxt->input != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +000010150 ctxt->input->cur = BAD_CAST"";
William M. Brack230c5502004-12-20 16:18:49 +000010151 ctxt->input->base = ctxt->input->cur;
10152 }
Owen Taylor3473f882001-02-23 17:55:21 +000010153}
10154
10155/**
10156 * xmlCreatePushParserCtxt:
10157 * @sax: a SAX handler
10158 * @user_data: The user data returned on SAX callbacks
10159 * @chunk: a pointer to an array of chars
10160 * @size: number of chars in the array
10161 * @filename: an optional file name or URI
10162 *
Daniel Veillard176d99f2002-07-06 19:22:28 +000010163 * Create a parser context for using the XML parser in push mode.
10164 * If @buffer and @size are non-NULL, the data is used to detect
10165 * the encoding. The remaining characters will be parsed so they
10166 * don't need to be fed in again through xmlParseChunk.
Owen Taylor3473f882001-02-23 17:55:21 +000010167 * To allow content encoding detection, @size should be >= 4
10168 * The value of @filename is used for fetching external entities
10169 * and error/warning reports.
10170 *
10171 * Returns the new parser context or NULL
10172 */
Daniel Veillard176d99f2002-07-06 19:22:28 +000010173
Owen Taylor3473f882001-02-23 17:55:21 +000010174xmlParserCtxtPtr
10175xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
10176 const char *chunk, int size, const char *filename) {
10177 xmlParserCtxtPtr ctxt;
10178 xmlParserInputPtr inputStream;
10179 xmlParserInputBufferPtr buf;
10180 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
10181
10182 /*
10183 * plug some encoding conversion routines
10184 */
10185 if ((chunk != NULL) && (size >= 4))
10186 enc = xmlDetectCharEncoding((const xmlChar *) chunk, size);
10187
10188 buf = xmlAllocParserInputBuffer(enc);
10189 if (buf == NULL) return(NULL);
10190
10191 ctxt = xmlNewParserCtxt();
10192 if (ctxt == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +000010193 xmlErrMemory(NULL, "creating parser: out of memory\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000010194 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +000010195 return(NULL);
10196 }
Daniel Veillard03a53c32004-10-26 16:06:51 +000010197 ctxt->dictNames = 1;
Daniel Veillarde57ec792003-09-10 10:50:59 +000010198 ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * sizeof(xmlChar *));
10199 if (ctxt->pushTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010200 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +000010201 xmlFreeParserInputBuffer(buf);
10202 xmlFreeParserCtxt(ctxt);
10203 return(NULL);
10204 }
Owen Taylor3473f882001-02-23 17:55:21 +000010205 if (sax != NULL) {
Daniel Veillard81273902003-09-30 00:43:48 +000010206#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +000010207 if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
Daniel Veillard81273902003-09-30 00:43:48 +000010208#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000010209 xmlFree(ctxt->sax);
10210 ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler));
10211 if (ctxt->sax == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010212 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000010213 xmlFreeParserInputBuffer(buf);
10214 xmlFreeParserCtxt(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010215 return(NULL);
10216 }
Daniel Veillard5ea30d72004-11-08 11:54:28 +000010217 memset(ctxt->sax, 0, sizeof(xmlSAXHandler));
10218 if (sax->initialized == XML_SAX2_MAGIC)
10219 memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler));
10220 else
10221 memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1));
Owen Taylor3473f882001-02-23 17:55:21 +000010222 if (user_data != NULL)
10223 ctxt->userData = user_data;
10224 }
10225 if (filename == NULL) {
10226 ctxt->directory = NULL;
10227 } else {
10228 ctxt->directory = xmlParserGetDirectory(filename);
10229 }
10230
10231 inputStream = xmlNewInputStream(ctxt);
10232 if (inputStream == NULL) {
10233 xmlFreeParserCtxt(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +000010234 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +000010235 return(NULL);
10236 }
10237
10238 if (filename == NULL)
10239 inputStream->filename = NULL;
William M. Bracka3215c72004-07-31 16:24:01 +000010240 else {
Daniel Veillardf4862f02002-09-10 11:13:43 +000010241 inputStream->filename = (char *)
Igor Zlatkovic5f9fada2003-02-19 14:51:00 +000010242 xmlCanonicPath((const xmlChar *) filename);
William M. Bracka3215c72004-07-31 16:24:01 +000010243 if (inputStream->filename == NULL) {
10244 xmlFreeParserCtxt(ctxt);
10245 xmlFreeParserInputBuffer(buf);
10246 return(NULL);
10247 }
10248 }
Owen Taylor3473f882001-02-23 17:55:21 +000010249 inputStream->buf = buf;
10250 inputStream->base = inputStream->buf->buffer->content;
10251 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +000010252 inputStream->end =
10253 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +000010254
10255 inputPush(ctxt, inputStream);
10256
William M. Brack3a1cd212005-02-11 14:35:54 +000010257 /*
10258 * If the caller didn't provide an initial 'chunk' for determining
10259 * the encoding, we set the context to XML_CHAR_ENCODING_NONE so
10260 * that it can be automatically determined later
10261 */
10262 if ((size == 0) || (chunk == NULL)) {
10263 ctxt->charset = XML_CHAR_ENCODING_NONE;
10264 } else if ((ctxt->input != NULL) && (ctxt->input->buf != NULL)) {
Daniel Veillardaa39a0f2002-01-06 12:47:22 +000010265 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
10266 int cur = ctxt->input->cur - ctxt->input->base;
10267
Owen Taylor3473f882001-02-23 17:55:21 +000010268 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
Daniel Veillardaa39a0f2002-01-06 12:47:22 +000010269
10270 ctxt->input->base = ctxt->input->buf->buffer->content + base;
10271 ctxt->input->cur = ctxt->input->base + cur;
10272 ctxt->input->end =
10273 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +000010274#ifdef DEBUG_PUSH
10275 xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
10276#endif
10277 }
10278
Daniel Veillard0e4cd172001-06-28 12:13:56 +000010279 if (enc != XML_CHAR_ENCODING_NONE) {
10280 xmlSwitchEncoding(ctxt, enc);
10281 }
10282
Owen Taylor3473f882001-02-23 17:55:21 +000010283 return(ctxt);
10284}
Daniel Veillard73b013f2003-09-30 12:36:01 +000010285#endif /* LIBXML_PUSH_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000010286
10287/**
10288 * xmlCreateIOParserCtxt:
10289 * @sax: a SAX handler
10290 * @user_data: The user data returned on SAX callbacks
10291 * @ioread: an I/O read function
10292 * @ioclose: an I/O close function
10293 * @ioctx: an I/O handler
10294 * @enc: the charset encoding if known
10295 *
10296 * Create a parser context for using the XML parser with an existing
10297 * I/O stream
10298 *
10299 * Returns the new parser context or NULL
10300 */
10301xmlParserCtxtPtr
10302xmlCreateIOParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
10303 xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
10304 void *ioctx, xmlCharEncoding enc) {
10305 xmlParserCtxtPtr ctxt;
10306 xmlParserInputPtr inputStream;
10307 xmlParserInputBufferPtr buf;
Daniel Veillard42595322004-11-08 10:52:06 +000010308
10309 if (ioread == NULL) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010310
10311 buf = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, enc);
10312 if (buf == NULL) return(NULL);
10313
10314 ctxt = xmlNewParserCtxt();
10315 if (ctxt == NULL) {
10316 xmlFree(buf);
10317 return(NULL);
10318 }
10319 if (sax != NULL) {
Daniel Veillard81273902003-09-30 00:43:48 +000010320#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +000010321 if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
Daniel Veillard81273902003-09-30 00:43:48 +000010322#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000010323 xmlFree(ctxt->sax);
10324 ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler));
10325 if (ctxt->sax == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010326 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010327 xmlFree(ctxt);
10328 return(NULL);
10329 }
Daniel Veillard5ea30d72004-11-08 11:54:28 +000010330 memset(ctxt->sax, 0, sizeof(xmlSAXHandler));
10331 if (sax->initialized == XML_SAX2_MAGIC)
10332 memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler));
10333 else
10334 memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1));
Owen Taylor3473f882001-02-23 17:55:21 +000010335 if (user_data != NULL)
10336 ctxt->userData = user_data;
10337 }
10338
10339 inputStream = xmlNewIOInputStream(ctxt, buf, enc);
10340 if (inputStream == NULL) {
10341 xmlFreeParserCtxt(ctxt);
10342 return(NULL);
10343 }
10344 inputPush(ctxt, inputStream);
10345
10346 return(ctxt);
10347}
10348
Daniel Veillard4432df22003-09-28 18:58:27 +000010349#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000010350/************************************************************************
10351 * *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010352 * Front ends when parsing a DTD *
Owen Taylor3473f882001-02-23 17:55:21 +000010353 * *
10354 ************************************************************************/
10355
10356/**
10357 * xmlIOParseDTD:
10358 * @sax: the SAX handler block or NULL
10359 * @input: an Input Buffer
10360 * @enc: the charset encoding if known
10361 *
10362 * Load and parse a DTD
10363 *
10364 * Returns the resulting xmlDtdPtr or NULL in case of error.
10365 * @input will be freed at parsing end.
10366 */
10367
10368xmlDtdPtr
10369xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input,
10370 xmlCharEncoding enc) {
10371 xmlDtdPtr ret = NULL;
10372 xmlParserCtxtPtr ctxt;
10373 xmlParserInputPtr pinput = NULL;
Daniel Veillard87a764e2001-06-20 17:41:10 +000010374 xmlChar start[4];
Owen Taylor3473f882001-02-23 17:55:21 +000010375
10376 if (input == NULL)
10377 return(NULL);
10378
10379 ctxt = xmlNewParserCtxt();
10380 if (ctxt == NULL) {
10381 return(NULL);
10382 }
10383
10384 /*
10385 * Set-up the SAX context
10386 */
10387 if (sax != NULL) {
10388 if (ctxt->sax != NULL)
10389 xmlFree(ctxt->sax);
10390 ctxt->sax = sax;
Daniel Veillard500a1de2004-03-22 15:22:58 +000010391 ctxt->userData = ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +000010392 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000010393 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010394
10395 /*
10396 * generate a parser input from the I/O handler
10397 */
10398
Daniel Veillard43caefb2003-12-07 19:32:22 +000010399 pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
Owen Taylor3473f882001-02-23 17:55:21 +000010400 if (pinput == NULL) {
10401 if (sax != NULL) ctxt->sax = NULL;
10402 xmlFreeParserCtxt(ctxt);
10403 return(NULL);
10404 }
10405
10406 /*
10407 * plug some encoding conversion routines here.
10408 */
10409 xmlPushInput(ctxt, pinput);
Daniel Veillard43caefb2003-12-07 19:32:22 +000010410 if (enc != XML_CHAR_ENCODING_NONE) {
10411 xmlSwitchEncoding(ctxt, enc);
10412 }
Owen Taylor3473f882001-02-23 17:55:21 +000010413
10414 pinput->filename = NULL;
10415 pinput->line = 1;
10416 pinput->col = 1;
10417 pinput->base = ctxt->input->cur;
10418 pinput->cur = ctxt->input->cur;
10419 pinput->free = NULL;
10420
10421 /*
10422 * let's parse that entity knowing it's an external subset.
10423 */
10424 ctxt->inSubset = 2;
10425 ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
10426 ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none",
10427 BAD_CAST "none", BAD_CAST "none");
Daniel Veillard87a764e2001-06-20 17:41:10 +000010428
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010429 if ((enc == XML_CHAR_ENCODING_NONE) &&
10430 ((ctxt->input->end - ctxt->input->cur) >= 4)) {
Daniel Veillard87a764e2001-06-20 17:41:10 +000010431 /*
10432 * Get the 4 first bytes and decode the charset
10433 * if enc != XML_CHAR_ENCODING_NONE
10434 * plug some encoding conversion routines.
10435 */
10436 start[0] = RAW;
10437 start[1] = NXT(1);
10438 start[2] = NXT(2);
10439 start[3] = NXT(3);
10440 enc = xmlDetectCharEncoding(start, 4);
10441 if (enc != XML_CHAR_ENCODING_NONE) {
10442 xmlSwitchEncoding(ctxt, enc);
10443 }
10444 }
10445
Owen Taylor3473f882001-02-23 17:55:21 +000010446 xmlParseExternalSubset(ctxt, BAD_CAST "none", BAD_CAST "none");
10447
10448 if (ctxt->myDoc != NULL) {
10449 if (ctxt->wellFormed) {
10450 ret = ctxt->myDoc->extSubset;
10451 ctxt->myDoc->extSubset = NULL;
Daniel Veillard329456a2003-04-26 21:21:00 +000010452 if (ret != NULL) {
10453 xmlNodePtr tmp;
10454
10455 ret->doc = NULL;
10456 tmp = ret->children;
10457 while (tmp != NULL) {
10458 tmp->doc = NULL;
10459 tmp = tmp->next;
10460 }
10461 }
Owen Taylor3473f882001-02-23 17:55:21 +000010462 } else {
10463 ret = NULL;
10464 }
10465 xmlFreeDoc(ctxt->myDoc);
10466 ctxt->myDoc = NULL;
10467 }
10468 if (sax != NULL) ctxt->sax = NULL;
10469 xmlFreeParserCtxt(ctxt);
10470
10471 return(ret);
10472}
10473
10474/**
10475 * xmlSAXParseDTD:
10476 * @sax: the SAX handler block
10477 * @ExternalID: a NAME* containing the External ID of the DTD
10478 * @SystemID: a NAME* containing the URL to the DTD
10479 *
10480 * Load and parse an external subset.
10481 *
10482 * Returns the resulting xmlDtdPtr or NULL in case of error.
10483 */
10484
10485xmlDtdPtr
10486xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID,
10487 const xmlChar *SystemID) {
10488 xmlDtdPtr ret = NULL;
10489 xmlParserCtxtPtr ctxt;
10490 xmlParserInputPtr input = NULL;
10491 xmlCharEncoding enc;
Igor Zlatkovic07d59762004-08-24 19:12:51 +000010492 xmlChar* systemIdCanonic;
Owen Taylor3473f882001-02-23 17:55:21 +000010493
10494 if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL);
10495
10496 ctxt = xmlNewParserCtxt();
10497 if (ctxt == NULL) {
10498 return(NULL);
10499 }
10500
10501 /*
10502 * Set-up the SAX context
10503 */
10504 if (sax != NULL) {
10505 if (ctxt->sax != NULL)
10506 xmlFree(ctxt->sax);
10507 ctxt->sax = sax;
Daniel Veillardbf1e3d82003-08-14 23:57:26 +000010508 ctxt->userData = ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +000010509 }
Igor Zlatkovic07d59762004-08-24 19:12:51 +000010510
10511 /*
10512 * Canonicalise the system ID
10513 */
10514 systemIdCanonic = xmlCanonicPath(SystemID);
Daniel Veillardc93a19f2004-10-04 11:53:20 +000010515 if ((SystemID != NULL) && (systemIdCanonic == NULL)) {
Igor Zlatkovic07d59762004-08-24 19:12:51 +000010516 xmlFreeParserCtxt(ctxt);
10517 return(NULL);
10518 }
Owen Taylor3473f882001-02-23 17:55:21 +000010519
10520 /*
10521 * Ask the Entity resolver to load the damn thing
10522 */
10523
10524 if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
Igor Zlatkovic07d59762004-08-24 19:12:51 +000010525 input = ctxt->sax->resolveEntity(ctxt, ExternalID, systemIdCanonic);
Owen Taylor3473f882001-02-23 17:55:21 +000010526 if (input == NULL) {
10527 if (sax != NULL) ctxt->sax = NULL;
10528 xmlFreeParserCtxt(ctxt);
Daniel Veillard34099b42004-11-04 17:34:35 +000010529 if (systemIdCanonic != NULL)
10530 xmlFree(systemIdCanonic);
Owen Taylor3473f882001-02-23 17:55:21 +000010531 return(NULL);
10532 }
10533
10534 /*
10535 * plug some encoding conversion routines here.
10536 */
10537 xmlPushInput(ctxt, input);
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010538 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
10539 enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
10540 xmlSwitchEncoding(ctxt, enc);
10541 }
Owen Taylor3473f882001-02-23 17:55:21 +000010542
10543 if (input->filename == NULL)
Igor Zlatkovic07d59762004-08-24 19:12:51 +000010544 input->filename = (char *) systemIdCanonic;
10545 else
10546 xmlFree(systemIdCanonic);
Owen Taylor3473f882001-02-23 17:55:21 +000010547 input->line = 1;
10548 input->col = 1;
10549 input->base = ctxt->input->cur;
10550 input->cur = ctxt->input->cur;
10551 input->free = NULL;
10552
10553 /*
10554 * let's parse that entity knowing it's an external subset.
10555 */
10556 ctxt->inSubset = 2;
10557 ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
10558 ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none",
10559 ExternalID, SystemID);
10560 xmlParseExternalSubset(ctxt, ExternalID, SystemID);
10561
10562 if (ctxt->myDoc != NULL) {
10563 if (ctxt->wellFormed) {
10564 ret = ctxt->myDoc->extSubset;
10565 ctxt->myDoc->extSubset = NULL;
Daniel Veillardc5573462003-04-25 16:43:49 +000010566 if (ret != NULL) {
10567 xmlNodePtr tmp;
10568
10569 ret->doc = NULL;
10570 tmp = ret->children;
10571 while (tmp != NULL) {
10572 tmp->doc = NULL;
10573 tmp = tmp->next;
10574 }
10575 }
Owen Taylor3473f882001-02-23 17:55:21 +000010576 } else {
10577 ret = NULL;
10578 }
10579 xmlFreeDoc(ctxt->myDoc);
10580 ctxt->myDoc = NULL;
10581 }
10582 if (sax != NULL) ctxt->sax = NULL;
10583 xmlFreeParserCtxt(ctxt);
10584
10585 return(ret);
10586}
10587
Daniel Veillard4432df22003-09-28 18:58:27 +000010588
Owen Taylor3473f882001-02-23 17:55:21 +000010589/**
10590 * xmlParseDTD:
10591 * @ExternalID: a NAME* containing the External ID of the DTD
10592 * @SystemID: a NAME* containing the URL to the DTD
10593 *
10594 * Load and parse an external subset.
10595 *
10596 * Returns the resulting xmlDtdPtr or NULL in case of error.
10597 */
10598
10599xmlDtdPtr
10600xmlParseDTD(const xmlChar *ExternalID, const xmlChar *SystemID) {
10601 return(xmlSAXParseDTD(NULL, ExternalID, SystemID));
10602}
Daniel Veillard4432df22003-09-28 18:58:27 +000010603#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000010604
10605/************************************************************************
10606 * *
10607 * Front ends when parsing an Entity *
10608 * *
10609 ************************************************************************/
10610
10611/**
Owen Taylor3473f882001-02-23 17:55:21 +000010612 * xmlParseCtxtExternalEntity:
10613 * @ctx: the existing parsing context
10614 * @URL: the URL for the entity to load
10615 * @ID: the System ID for the entity to load
Daniel Veillardcda96922001-08-21 10:56:31 +000010616 * @lst: the return value for the set of parsed nodes
Owen Taylor3473f882001-02-23 17:55:21 +000010617 *
10618 * Parse an external general entity within an existing parsing context
10619 * An external general parsed entity is well-formed if it matches the
10620 * production labeled extParsedEnt.
10621 *
10622 * [78] extParsedEnt ::= TextDecl? content
10623 *
10624 * Returns 0 if the entity is well formed, -1 in case of args problem and
10625 * the parser error code otherwise
10626 */
10627
10628int
10629xmlParseCtxtExternalEntity(xmlParserCtxtPtr ctx, const xmlChar *URL,
Daniel Veillardcda96922001-08-21 10:56:31 +000010630 const xmlChar *ID, xmlNodePtr *lst) {
Owen Taylor3473f882001-02-23 17:55:21 +000010631 xmlParserCtxtPtr ctxt;
10632 xmlDocPtr newDoc;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010633 xmlNodePtr newRoot;
Owen Taylor3473f882001-02-23 17:55:21 +000010634 xmlSAXHandlerPtr oldsax = NULL;
10635 int ret = 0;
Daniel Veillard87a764e2001-06-20 17:41:10 +000010636 xmlChar start[4];
10637 xmlCharEncoding enc;
Owen Taylor3473f882001-02-23 17:55:21 +000010638
Daniel Veillardce682bc2004-11-05 17:22:25 +000010639 if (ctx == NULL) return(-1);
10640
Owen Taylor3473f882001-02-23 17:55:21 +000010641 if (ctx->depth > 40) {
10642 return(XML_ERR_ENTITY_LOOP);
10643 }
10644
Daniel Veillardcda96922001-08-21 10:56:31 +000010645 if (lst != NULL)
10646 *lst = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010647 if ((URL == NULL) && (ID == NULL))
10648 return(-1);
10649 if (ctx->myDoc == NULL) /* @@ relax but check for dereferences */
10650 return(-1);
10651
10652
10653 ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL);
10654 if (ctxt == NULL) return(-1);
10655 ctxt->userData = ctxt;
Daniel Veillarde2830f12003-01-08 17:47:49 +000010656 ctxt->_private = ctx->_private;
Owen Taylor3473f882001-02-23 17:55:21 +000010657 oldsax = ctxt->sax;
10658 ctxt->sax = ctx->sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000010659 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010660 newDoc = xmlNewDoc(BAD_CAST "1.0");
10661 if (newDoc == NULL) {
10662 xmlFreeParserCtxt(ctxt);
10663 return(-1);
10664 }
Daniel Veillard03a53c32004-10-26 16:06:51 +000010665 if (ctx->myDoc->dict) {
10666 newDoc->dict = ctx->myDoc->dict;
10667 xmlDictReference(newDoc->dict);
10668 }
Owen Taylor3473f882001-02-23 17:55:21 +000010669 if (ctx->myDoc != NULL) {
10670 newDoc->intSubset = ctx->myDoc->intSubset;
10671 newDoc->extSubset = ctx->myDoc->extSubset;
10672 }
10673 if (ctx->myDoc->URL != NULL) {
10674 newDoc->URL = xmlStrdup(ctx->myDoc->URL);
10675 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010676 newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
10677 if (newRoot == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +000010678 ctxt->sax = oldsax;
10679 xmlFreeParserCtxt(ctxt);
10680 newDoc->intSubset = NULL;
10681 newDoc->extSubset = NULL;
10682 xmlFreeDoc(newDoc);
10683 return(-1);
10684 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010685 xmlAddChild((xmlNodePtr) newDoc, newRoot);
Owen Taylor3473f882001-02-23 17:55:21 +000010686 nodePush(ctxt, newDoc->children);
10687 if (ctx->myDoc == NULL) {
10688 ctxt->myDoc = newDoc;
10689 } else {
10690 ctxt->myDoc = ctx->myDoc;
10691 newDoc->children->doc = ctx->myDoc;
10692 }
10693
Daniel Veillard87a764e2001-06-20 17:41:10 +000010694 /*
10695 * Get the 4 first bytes and decode the charset
10696 * if enc != XML_CHAR_ENCODING_NONE
10697 * plug some encoding conversion routines.
10698 */
10699 GROW
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010700 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
10701 start[0] = RAW;
10702 start[1] = NXT(1);
10703 start[2] = NXT(2);
10704 start[3] = NXT(3);
10705 enc = xmlDetectCharEncoding(start, 4);
10706 if (enc != XML_CHAR_ENCODING_NONE) {
10707 xmlSwitchEncoding(ctxt, enc);
10708 }
Daniel Veillard87a764e2001-06-20 17:41:10 +000010709 }
10710
Owen Taylor3473f882001-02-23 17:55:21 +000010711 /*
10712 * Parse a possible text declaration first
10713 */
Daniel Veillarda07050d2003-10-19 14:46:32 +000010714 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +000010715 xmlParseTextDecl(ctxt);
10716 }
10717
10718 /*
10719 * Doing validity checking on chunk doesn't make sense
10720 */
10721 ctxt->instate = XML_PARSER_CONTENT;
10722 ctxt->validate = ctx->validate;
Daniel Veillard5f8d1a32003-03-23 21:02:00 +000010723 ctxt->valid = ctx->valid;
Owen Taylor3473f882001-02-23 17:55:21 +000010724 ctxt->loadsubset = ctx->loadsubset;
10725 ctxt->depth = ctx->depth + 1;
10726 ctxt->replaceEntities = ctx->replaceEntities;
10727 if (ctxt->validate) {
10728 ctxt->vctxt.error = ctx->vctxt.error;
10729 ctxt->vctxt.warning = ctx->vctxt.warning;
Owen Taylor3473f882001-02-23 17:55:21 +000010730 } else {
10731 ctxt->vctxt.error = NULL;
10732 ctxt->vctxt.warning = NULL;
10733 }
Daniel Veillarda9142e72001-06-19 11:07:54 +000010734 ctxt->vctxt.nodeTab = NULL;
10735 ctxt->vctxt.nodeNr = 0;
10736 ctxt->vctxt.nodeMax = 0;
10737 ctxt->vctxt.node = NULL;
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000010738 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
10739 ctxt->dict = ctx->dict;
Daniel Veillardfd343dc2003-10-31 10:55:22 +000010740 ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
10741 ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
10742 ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000010743 ctxt->dictNames = ctx->dictNames;
10744 ctxt->attsDefault = ctx->attsDefault;
10745 ctxt->attsSpecial = ctx->attsSpecial;
William M. Brack503b6102004-08-19 02:17:27 +000010746 ctxt->linenumbers = ctx->linenumbers;
Owen Taylor3473f882001-02-23 17:55:21 +000010747
10748 xmlParseContent(ctxt);
10749
Daniel Veillard5f8d1a32003-03-23 21:02:00 +000010750 ctx->validate = ctxt->validate;
10751 ctx->valid = ctxt->valid;
Owen Taylor3473f882001-02-23 17:55:21 +000010752 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010753 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010754 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010755 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010756 }
10757 if (ctxt->node != newDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010758 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010759 }
10760
10761 if (!ctxt->wellFormed) {
10762 if (ctxt->errNo == 0)
10763 ret = 1;
10764 else
10765 ret = ctxt->errNo;
10766 } else {
Daniel Veillardcda96922001-08-21 10:56:31 +000010767 if (lst != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +000010768 xmlNodePtr cur;
10769
10770 /*
10771 * Return the newly created nodeset after unlinking it from
10772 * they pseudo parent.
10773 */
10774 cur = newDoc->children->children;
Daniel Veillardcda96922001-08-21 10:56:31 +000010775 *lst = cur;
Owen Taylor3473f882001-02-23 17:55:21 +000010776 while (cur != NULL) {
10777 cur->parent = NULL;
10778 cur = cur->next;
10779 }
10780 newDoc->children->children = NULL;
10781 }
10782 ret = 0;
10783 }
10784 ctxt->sax = oldsax;
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000010785 ctxt->dict = NULL;
10786 ctxt->attsDefault = NULL;
10787 ctxt->attsSpecial = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010788 xmlFreeParserCtxt(ctxt);
10789 newDoc->intSubset = NULL;
10790 newDoc->extSubset = NULL;
10791 xmlFreeDoc(newDoc);
10792
10793 return(ret);
10794}
10795
10796/**
Daniel Veillard257d9102001-05-08 10:41:44 +000010797 * xmlParseExternalEntityPrivate:
Owen Taylor3473f882001-02-23 17:55:21 +000010798 * @doc: the document the chunk pertains to
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010799 * @oldctxt: the previous parser context if available
Owen Taylor3473f882001-02-23 17:55:21 +000010800 * @sax: the SAX handler bloc (possibly NULL)
10801 * @user_data: The user data returned on SAX callbacks (possibly NULL)
10802 * @depth: Used for loop detection, use 0
10803 * @URL: the URL for the entity to load
10804 * @ID: the System ID for the entity to load
10805 * @list: the return value for the set of parsed nodes
10806 *
Daniel Veillard257d9102001-05-08 10:41:44 +000010807 * Private version of xmlParseExternalEntity()
Owen Taylor3473f882001-02-23 17:55:21 +000010808 *
10809 * Returns 0 if the entity is well formed, -1 in case of args problem and
10810 * the parser error code otherwise
10811 */
10812
Daniel Veillard7d515752003-09-26 19:12:37 +000010813static xmlParserErrors
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010814xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt,
10815 xmlSAXHandlerPtr sax,
Daniel Veillard257d9102001-05-08 10:41:44 +000010816 void *user_data, int depth, const xmlChar *URL,
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010817 const xmlChar *ID, xmlNodePtr *list) {
Owen Taylor3473f882001-02-23 17:55:21 +000010818 xmlParserCtxtPtr ctxt;
10819 xmlDocPtr newDoc;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010820 xmlNodePtr newRoot;
Owen Taylor3473f882001-02-23 17:55:21 +000010821 xmlSAXHandlerPtr oldsax = NULL;
Daniel Veillard7d515752003-09-26 19:12:37 +000010822 xmlParserErrors ret = XML_ERR_OK;
Daniel Veillard87a764e2001-06-20 17:41:10 +000010823 xmlChar start[4];
10824 xmlCharEncoding enc;
Owen Taylor3473f882001-02-23 17:55:21 +000010825
10826 if (depth > 40) {
10827 return(XML_ERR_ENTITY_LOOP);
10828 }
10829
10830
10831
10832 if (list != NULL)
10833 *list = NULL;
10834 if ((URL == NULL) && (ID == NULL))
Daniel Veillard7d515752003-09-26 19:12:37 +000010835 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010836 if (doc == NULL) /* @@ relax but check for dereferences */
Daniel Veillard7d515752003-09-26 19:12:37 +000010837 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010838
10839
10840 ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL);
William M. Brackb670e2e2003-09-27 01:05:55 +000010841 if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY);
Owen Taylor3473f882001-02-23 17:55:21 +000010842 ctxt->userData = ctxt;
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010843 if (oldctxt != NULL) {
10844 ctxt->_private = oldctxt->_private;
10845 ctxt->loadsubset = oldctxt->loadsubset;
10846 ctxt->validate = oldctxt->validate;
10847 ctxt->external = oldctxt->external;
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010848 ctxt->record_info = oldctxt->record_info;
10849 ctxt->node_seq.maximum = oldctxt->node_seq.maximum;
10850 ctxt->node_seq.length = oldctxt->node_seq.length;
10851 ctxt->node_seq.buffer = oldctxt->node_seq.buffer;
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010852 } else {
10853 /*
10854 * Doing validity checking on chunk without context
10855 * doesn't make sense
10856 */
10857 ctxt->_private = NULL;
10858 ctxt->validate = 0;
10859 ctxt->external = 2;
10860 ctxt->loadsubset = 0;
10861 }
Owen Taylor3473f882001-02-23 17:55:21 +000010862 if (sax != NULL) {
10863 oldsax = ctxt->sax;
10864 ctxt->sax = sax;
10865 if (user_data != NULL)
10866 ctxt->userData = user_data;
10867 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000010868 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010869 newDoc = xmlNewDoc(BAD_CAST "1.0");
10870 if (newDoc == NULL) {
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010871 ctxt->node_seq.maximum = 0;
10872 ctxt->node_seq.length = 0;
10873 ctxt->node_seq.buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010874 xmlFreeParserCtxt(ctxt);
Daniel Veillard7d515752003-09-26 19:12:37 +000010875 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010876 }
10877 if (doc != NULL) {
10878 newDoc->intSubset = doc->intSubset;
10879 newDoc->extSubset = doc->extSubset;
Daniel Veillard03a53c32004-10-26 16:06:51 +000010880 newDoc->dict = doc->dict;
10881 } else if (oldctxt != NULL) {
10882 newDoc->dict = oldctxt->dict;
Owen Taylor3473f882001-02-23 17:55:21 +000010883 }
Daniel Veillard03a53c32004-10-26 16:06:51 +000010884 xmlDictReference(newDoc->dict);
10885
Owen Taylor3473f882001-02-23 17:55:21 +000010886 if (doc->URL != NULL) {
10887 newDoc->URL = xmlStrdup(doc->URL);
10888 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010889 newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
10890 if (newRoot == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +000010891 if (sax != NULL)
10892 ctxt->sax = oldsax;
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010893 ctxt->node_seq.maximum = 0;
10894 ctxt->node_seq.length = 0;
10895 ctxt->node_seq.buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010896 xmlFreeParserCtxt(ctxt);
10897 newDoc->intSubset = NULL;
10898 newDoc->extSubset = NULL;
10899 xmlFreeDoc(newDoc);
Daniel Veillard7d515752003-09-26 19:12:37 +000010900 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010901 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010902 xmlAddChild((xmlNodePtr) newDoc, newRoot);
Owen Taylor3473f882001-02-23 17:55:21 +000010903 nodePush(ctxt, newDoc->children);
10904 if (doc == NULL) {
10905 ctxt->myDoc = newDoc;
10906 } else {
10907 ctxt->myDoc = doc;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010908 newRoot->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +000010909 }
10910
Daniel Veillard87a764e2001-06-20 17:41:10 +000010911 /*
10912 * Get the 4 first bytes and decode the charset
10913 * if enc != XML_CHAR_ENCODING_NONE
10914 * plug some encoding conversion routines.
10915 */
10916 GROW;
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010917 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
10918 start[0] = RAW;
10919 start[1] = NXT(1);
10920 start[2] = NXT(2);
10921 start[3] = NXT(3);
10922 enc = xmlDetectCharEncoding(start, 4);
10923 if (enc != XML_CHAR_ENCODING_NONE) {
10924 xmlSwitchEncoding(ctxt, enc);
10925 }
Daniel Veillard87a764e2001-06-20 17:41:10 +000010926 }
10927
Owen Taylor3473f882001-02-23 17:55:21 +000010928 /*
10929 * Parse a possible text declaration first
10930 */
Daniel Veillarda07050d2003-10-19 14:46:32 +000010931 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +000010932 xmlParseTextDecl(ctxt);
10933 }
10934
Owen Taylor3473f882001-02-23 17:55:21 +000010935 ctxt->instate = XML_PARSER_CONTENT;
Owen Taylor3473f882001-02-23 17:55:21 +000010936 ctxt->depth = depth;
10937
10938 xmlParseContent(ctxt);
10939
Daniel Veillard561b7f82002-03-20 21:55:57 +000010940 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010941 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Daniel Veillard561b7f82002-03-20 21:55:57 +000010942 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010943 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010944 }
10945 if (ctxt->node != newDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010946 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010947 }
10948
10949 if (!ctxt->wellFormed) {
10950 if (ctxt->errNo == 0)
Daniel Veillard7d515752003-09-26 19:12:37 +000010951 ret = XML_ERR_INTERNAL_ERROR;
Owen Taylor3473f882001-02-23 17:55:21 +000010952 else
William M. Brack7b9154b2003-09-27 19:23:50 +000010953 ret = (xmlParserErrors)ctxt->errNo;
Owen Taylor3473f882001-02-23 17:55:21 +000010954 } else {
10955 if (list != NULL) {
10956 xmlNodePtr cur;
10957
10958 /*
10959 * Return the newly created nodeset after unlinking it from
10960 * they pseudo parent.
10961 */
10962 cur = newDoc->children->children;
10963 *list = cur;
10964 while (cur != NULL) {
10965 cur->parent = NULL;
10966 cur = cur->next;
10967 }
10968 newDoc->children->children = NULL;
10969 }
Daniel Veillard7d515752003-09-26 19:12:37 +000010970 ret = XML_ERR_OK;
Owen Taylor3473f882001-02-23 17:55:21 +000010971 }
10972 if (sax != NULL)
10973 ctxt->sax = oldsax;
Daniel Veillard0046c0f2003-02-23 13:52:30 +000010974 oldctxt->node_seq.maximum = ctxt->node_seq.maximum;
10975 oldctxt->node_seq.length = ctxt->node_seq.length;
10976 oldctxt->node_seq.buffer = ctxt->node_seq.buffer;
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010977 ctxt->node_seq.maximum = 0;
10978 ctxt->node_seq.length = 0;
10979 ctxt->node_seq.buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010980 xmlFreeParserCtxt(ctxt);
10981 newDoc->intSubset = NULL;
10982 newDoc->extSubset = NULL;
10983 xmlFreeDoc(newDoc);
10984
10985 return(ret);
10986}
10987
Daniel Veillard81273902003-09-30 00:43:48 +000010988#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000010989/**
Daniel Veillard257d9102001-05-08 10:41:44 +000010990 * xmlParseExternalEntity:
10991 * @doc: the document the chunk pertains to
10992 * @sax: the SAX handler bloc (possibly NULL)
10993 * @user_data: The user data returned on SAX callbacks (possibly NULL)
10994 * @depth: Used for loop detection, use 0
10995 * @URL: the URL for the entity to load
10996 * @ID: the System ID for the entity to load
Daniel Veillardcda96922001-08-21 10:56:31 +000010997 * @lst: the return value for the set of parsed nodes
Daniel Veillard257d9102001-05-08 10:41:44 +000010998 *
10999 * Parse an external general entity
11000 * An external general parsed entity is well-formed if it matches the
11001 * production labeled extParsedEnt.
11002 *
11003 * [78] extParsedEnt ::= TextDecl? content
11004 *
11005 * Returns 0 if the entity is well formed, -1 in case of args problem and
11006 * the parser error code otherwise
11007 */
11008
11009int
11010xmlParseExternalEntity(xmlDocPtr doc, xmlSAXHandlerPtr sax, void *user_data,
Daniel Veillardcda96922001-08-21 10:56:31 +000011011 int depth, const xmlChar *URL, const xmlChar *ID, xmlNodePtr *lst) {
Daniel Veillarda97a19b2001-05-20 13:19:52 +000011012 return(xmlParseExternalEntityPrivate(doc, NULL, sax, user_data, depth, URL,
Daniel Veillardcda96922001-08-21 10:56:31 +000011013 ID, lst));
Daniel Veillard257d9102001-05-08 10:41:44 +000011014}
11015
11016/**
Daniel Veillarde020c3a2001-03-21 18:06:15 +000011017 * xmlParseBalancedChunkMemory:
Owen Taylor3473f882001-02-23 17:55:21 +000011018 * @doc: the document the chunk pertains to
11019 * @sax: the SAX handler bloc (possibly NULL)
11020 * @user_data: The user data returned on SAX callbacks (possibly NULL)
11021 * @depth: Used for loop detection, use 0
11022 * @string: the input string in UTF8 or ISO-Latin (zero terminated)
Daniel Veillardcda96922001-08-21 10:56:31 +000011023 * @lst: the return value for the set of parsed nodes
Owen Taylor3473f882001-02-23 17:55:21 +000011024 *
11025 * Parse a well-balanced chunk of an XML document
11026 * called by the parser
11027 * The allowed sequence for the Well Balanced Chunk is the one defined by
11028 * the content production in the XML grammar:
11029 *
11030 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
11031 *
11032 * Returns 0 if the chunk is well balanced, -1 in case of args problem and
11033 * the parser error code otherwise
11034 */
11035
11036int
11037xmlParseBalancedChunkMemory(xmlDocPtr doc, xmlSAXHandlerPtr sax,
Daniel Veillardcda96922001-08-21 10:56:31 +000011038 void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst) {
Daniel Veillard58e44c92002-08-02 22:19:49 +000011039 return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data,
11040 depth, string, lst, 0 );
11041}
Daniel Veillard81273902003-09-30 00:43:48 +000011042#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard58e44c92002-08-02 22:19:49 +000011043
11044/**
Daniel Veillard328f48c2002-11-15 15:24:34 +000011045 * xmlParseBalancedChunkMemoryInternal:
11046 * @oldctxt: the existing parsing context
11047 * @string: the input string in UTF8 or ISO-Latin (zero terminated)
11048 * @user_data: the user data field for the parser context
11049 * @lst: the return value for the set of parsed nodes
11050 *
11051 *
11052 * Parse a well-balanced chunk of an XML document
11053 * called by the parser
11054 * The allowed sequence for the Well Balanced Chunk is the one defined by
11055 * the content production in the XML grammar:
11056 *
11057 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
11058 *
Daniel Veillard7d515752003-09-26 19:12:37 +000011059 * Returns XML_ERR_OK if the chunk is well balanced, and the parser
11060 * error code otherwise
Daniel Veillard328f48c2002-11-15 15:24:34 +000011061 *
11062 * In case recover is set to 1, the nodelist will not be empty even if
11063 * the parsed chunk is not well balanced.
11064 */
Daniel Veillard7d515752003-09-26 19:12:37 +000011065static xmlParserErrors
Daniel Veillard328f48c2002-11-15 15:24:34 +000011066xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt,
11067 const xmlChar *string, void *user_data, xmlNodePtr *lst) {
11068 xmlParserCtxtPtr ctxt;
Daniel Veillard68e9e742002-11-16 15:35:11 +000011069 xmlDocPtr newDoc = NULL;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000011070 xmlNodePtr newRoot;
Daniel Veillard328f48c2002-11-15 15:24:34 +000011071 xmlSAXHandlerPtr oldsax = NULL;
Daniel Veillard68e9e742002-11-16 15:35:11 +000011072 xmlNodePtr content = NULL;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000011073 xmlNodePtr last = NULL;
Daniel Veillard328f48c2002-11-15 15:24:34 +000011074 int size;
Daniel Veillard7d515752003-09-26 19:12:37 +000011075 xmlParserErrors ret = XML_ERR_OK;
Daniel Veillard328f48c2002-11-15 15:24:34 +000011076
11077 if (oldctxt->depth > 40) {
11078 return(XML_ERR_ENTITY_LOOP);
11079 }
11080
11081
11082 if (lst != NULL)
11083 *lst = NULL;
11084 if (string == NULL)
William M. Brack7b9154b2003-09-27 19:23:50 +000011085 return(XML_ERR_INTERNAL_ERROR);
Daniel Veillard328f48c2002-11-15 15:24:34 +000011086
11087 size = xmlStrlen(string);
11088
11089 ctxt = xmlCreateMemoryParserCtxt((char *) string, size);
William M. Brack7b9154b2003-09-27 19:23:50 +000011090 if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY);
Daniel Veillard328f48c2002-11-15 15:24:34 +000011091 if (user_data != NULL)
11092 ctxt->userData = user_data;
11093 else
11094 ctxt->userData = ctxt;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000011095 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
11096 ctxt->dict = oldctxt->dict;
Daniel Veillardfd343dc2003-10-31 10:55:22 +000011097 ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
11098 ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
11099 ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
Daniel Veillard328f48c2002-11-15 15:24:34 +000011100
11101 oldsax = ctxt->sax;
11102 ctxt->sax = oldctxt->sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000011103 xmlDetectSAX2(ctxt);
Daniel Veillard87ab1c12003-12-21 13:01:56 +000011104 ctxt->replaceEntities = oldctxt->replaceEntities;
11105 ctxt->options = oldctxt->options;
Daniel Veillarde57ec792003-09-10 10:50:59 +000011106
Daniel Veillarde1ca5032002-12-09 14:13:43 +000011107 ctxt->_private = oldctxt->_private;
Daniel Veillard328f48c2002-11-15 15:24:34 +000011108 if (oldctxt->myDoc == NULL) {
Daniel Veillard68e9e742002-11-16 15:35:11 +000011109 newDoc = xmlNewDoc(BAD_CAST "1.0");
11110 if (newDoc == NULL) {
11111 ctxt->sax = oldsax;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000011112 ctxt->dict = NULL;
Daniel Veillard68e9e742002-11-16 15:35:11 +000011113 xmlFreeParserCtxt(ctxt);
William M. Brack7b9154b2003-09-27 19:23:50 +000011114 return(XML_ERR_INTERNAL_ERROR);
Daniel Veillard68e9e742002-11-16 15:35:11 +000011115 }
Daniel Veillard03a53c32004-10-26 16:06:51 +000011116 newDoc->dict = ctxt->dict;
11117 xmlDictReference(newDoc->dict);
Daniel Veillard328f48c2002-11-15 15:24:34 +000011118 ctxt->myDoc = newDoc;
Daniel Veillard68e9e742002-11-16 15:35:11 +000011119 } else {
11120 ctxt->myDoc = oldctxt->myDoc;
11121 content = ctxt->myDoc->children;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000011122 last = ctxt->myDoc->last;
Daniel Veillard328f48c2002-11-15 15:24:34 +000011123 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000011124 newRoot = xmlNewDocNode(ctxt->myDoc, NULL, BAD_CAST "pseudoroot", NULL);
11125 if (newRoot == NULL) {
Daniel Veillard68e9e742002-11-16 15:35:11 +000011126 ctxt->sax = oldsax;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000011127 ctxt->dict = NULL;
Daniel Veillard68e9e742002-11-16 15:35:11 +000011128 xmlFreeParserCtxt(ctxt);
Daniel Veillard03a53c32004-10-26 16:06:51 +000011129 if (newDoc != NULL) {
Daniel Veillard68e9e742002-11-16 15:35:11 +000011130 xmlFreeDoc(newDoc);
Daniel Veillard03a53c32004-10-26 16:06:51 +000011131 }
William M. Brack7b9154b2003-09-27 19:23:50 +000011132 return(XML_ERR_INTERNAL_ERROR);
Daniel Veillard68e9e742002-11-16 15:35:11 +000011133 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000011134 ctxt->myDoc->children = NULL;
11135 ctxt->myDoc->last = NULL;
11136 xmlAddChild((xmlNodePtr) ctxt->myDoc, newRoot);
Daniel Veillard68e9e742002-11-16 15:35:11 +000011137 nodePush(ctxt, ctxt->myDoc->children);
Daniel Veillard328f48c2002-11-15 15:24:34 +000011138 ctxt->instate = XML_PARSER_CONTENT;
11139 ctxt->depth = oldctxt->depth + 1;
11140
Daniel Veillard328f48c2002-11-15 15:24:34 +000011141 ctxt->validate = 0;
11142 ctxt->loadsubset = oldctxt->loadsubset;
Daniel Veillardef8dd7b2003-03-23 12:02:56 +000011143 if ((oldctxt->validate) || (oldctxt->replaceEntities != 0)) {
11144 /*
11145 * ID/IDREF registration will be done in xmlValidateElement below
11146 */
11147 ctxt->loadsubset |= XML_SKIP_IDS;
11148 }
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000011149 ctxt->dictNames = oldctxt->dictNames;
Daniel Veillard95d2d5b2003-10-27 14:54:49 +000011150 ctxt->attsDefault = oldctxt->attsDefault;
11151 ctxt->attsSpecial = oldctxt->attsSpecial;
Daniel Veillard328f48c2002-11-15 15:24:34 +000011152
Daniel Veillard68e9e742002-11-16 15:35:11 +000011153 xmlParseContent(ctxt);
Daniel Veillard328f48c2002-11-15 15:24:34 +000011154 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000011155 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Daniel Veillard328f48c2002-11-15 15:24:34 +000011156 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000011157 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Daniel Veillard328f48c2002-11-15 15:24:34 +000011158 }
Daniel Veillard68e9e742002-11-16 15:35:11 +000011159 if (ctxt->node != ctxt->myDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000011160 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Daniel Veillard328f48c2002-11-15 15:24:34 +000011161 }
11162
11163 if (!ctxt->wellFormed) {
11164 if (ctxt->errNo == 0)
Daniel Veillard7d515752003-09-26 19:12:37 +000011165 ret = XML_ERR_INTERNAL_ERROR;
Daniel Veillard328f48c2002-11-15 15:24:34 +000011166 else
William M. Brack7b9154b2003-09-27 19:23:50 +000011167 ret = (xmlParserErrors)ctxt->errNo;
Daniel Veillard328f48c2002-11-15 15:24:34 +000011168 } else {
William M. Brack7b9154b2003-09-27 19:23:50 +000011169 ret = XML_ERR_OK;
Daniel Veillard328f48c2002-11-15 15:24:34 +000011170 }
11171
William M. Brack7b9154b2003-09-27 19:23:50 +000011172 if ((lst != NULL) && (ret == XML_ERR_OK)) {
Daniel Veillard328f48c2002-11-15 15:24:34 +000011173 xmlNodePtr cur;
11174
11175 /*
11176 * Return the newly created nodeset after unlinking it from
11177 * they pseudo parent.
11178 */
Daniel Veillard68e9e742002-11-16 15:35:11 +000011179 cur = ctxt->myDoc->children->children;
Daniel Veillard328f48c2002-11-15 15:24:34 +000011180 *lst = cur;
11181 while (cur != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +000011182#ifdef LIBXML_VALID_ENABLED
Daniel Veillard8d589042003-02-04 15:07:21 +000011183 if (oldctxt->validate && oldctxt->wellFormed &&
11184 oldctxt->myDoc && oldctxt->myDoc->intSubset) {
11185 oldctxt->valid &= xmlValidateElement(&oldctxt->vctxt,
11186 oldctxt->myDoc, cur);
11187 }
Daniel Veillard4432df22003-09-28 18:58:27 +000011188#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard328f48c2002-11-15 15:24:34 +000011189 cur->parent = NULL;
11190 cur = cur->next;
11191 }
Daniel Veillard68e9e742002-11-16 15:35:11 +000011192 ctxt->myDoc->children->children = NULL;
11193 }
11194 if (ctxt->myDoc != NULL) {
11195 xmlFreeNode(ctxt->myDoc->children);
11196 ctxt->myDoc->children = content;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000011197 ctxt->myDoc->last = last;
Daniel Veillard328f48c2002-11-15 15:24:34 +000011198 }
11199
11200 ctxt->sax = oldsax;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000011201 ctxt->dict = NULL;
Daniel Veillard95d2d5b2003-10-27 14:54:49 +000011202 ctxt->attsDefault = NULL;
11203 ctxt->attsSpecial = NULL;
Daniel Veillard328f48c2002-11-15 15:24:34 +000011204 xmlFreeParserCtxt(ctxt);
Daniel Veillard03a53c32004-10-26 16:06:51 +000011205 if (newDoc != NULL) {
Daniel Veillard68e9e742002-11-16 15:35:11 +000011206 xmlFreeDoc(newDoc);
Daniel Veillard03a53c32004-10-26 16:06:51 +000011207 }
Daniel Veillard328f48c2002-11-15 15:24:34 +000011208
11209 return(ret);
11210}
11211
Daniel Veillard29b17482004-08-16 00:39:03 +000011212/**
11213 * xmlParseInNodeContext:
11214 * @node: the context node
11215 * @data: the input string
11216 * @datalen: the input string length in bytes
11217 * @options: a combination of xmlParserOption
11218 * @lst: the return value for the set of parsed nodes
11219 *
11220 * Parse a well-balanced chunk of an XML document
11221 * within the context (DTD, namespaces, etc ...) of the given node.
11222 *
11223 * The allowed sequence for the data is a Well Balanced Chunk defined by
11224 * the content production in the XML grammar:
11225 *
11226 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
11227 *
11228 * Returns XML_ERR_OK if the chunk is well balanced, and the parser
11229 * error code otherwise
11230 */
11231xmlParserErrors
11232xmlParseInNodeContext(xmlNodePtr node, const char *data, int datalen,
11233 int options, xmlNodePtr *lst) {
11234#ifdef SAX2
11235 xmlParserCtxtPtr ctxt;
11236 xmlDocPtr doc = NULL;
11237 xmlNodePtr fake, cur;
11238 int nsnr = 0;
11239
11240 xmlParserErrors ret = XML_ERR_OK;
11241
11242 /*
11243 * check all input parameters, grab the document
11244 */
11245 if ((lst == NULL) || (node == NULL) || (data == NULL) || (datalen < 0))
11246 return(XML_ERR_INTERNAL_ERROR);
11247 switch (node->type) {
11248 case XML_ELEMENT_NODE:
11249 case XML_ATTRIBUTE_NODE:
11250 case XML_TEXT_NODE:
11251 case XML_CDATA_SECTION_NODE:
11252 case XML_ENTITY_REF_NODE:
11253 case XML_PI_NODE:
11254 case XML_COMMENT_NODE:
11255 case XML_DOCUMENT_NODE:
11256 case XML_HTML_DOCUMENT_NODE:
11257 break;
11258 default:
11259 return(XML_ERR_INTERNAL_ERROR);
11260
11261 }
11262 while ((node != NULL) && (node->type != XML_ELEMENT_NODE) &&
11263 (node->type != XML_DOCUMENT_NODE) &&
11264 (node->type != XML_HTML_DOCUMENT_NODE))
11265 node = node->parent;
11266 if (node == NULL)
11267 return(XML_ERR_INTERNAL_ERROR);
11268 if (node->type == XML_ELEMENT_NODE)
11269 doc = node->doc;
11270 else
11271 doc = (xmlDocPtr) node;
11272 if (doc == NULL)
11273 return(XML_ERR_INTERNAL_ERROR);
11274
11275 /*
11276 * allocate a context and set-up everything not related to the
11277 * node position in the tree
11278 */
11279 if (doc->type == XML_DOCUMENT_NODE)
11280 ctxt = xmlCreateMemoryParserCtxt((char *) data, datalen);
11281#ifdef LIBXML_HTML_ENABLED
11282 else if (doc->type == XML_HTML_DOCUMENT_NODE)
11283 ctxt = htmlCreateMemoryParserCtxt((char *) data, datalen);
11284#endif
11285 else
11286 return(XML_ERR_INTERNAL_ERROR);
11287
11288 if (ctxt == NULL)
11289 return(XML_ERR_NO_MEMORY);
11290 fake = xmlNewComment(NULL);
11291 if (fake == NULL) {
11292 xmlFreeParserCtxt(ctxt);
11293 return(XML_ERR_NO_MEMORY);
11294 }
11295 xmlAddChild(node, fake);
William M. Brackc3f81342004-10-03 01:22:44 +000011296
11297 /*
11298 * Use input doc's dict if present, else assure XML_PARSE_NODICT is set.
11299 * We need a dictionary for xmlDetectSAX2, so if there's no doc dict
11300 * we must wait until the last moment to free the original one.
11301 */
Daniel Veillard29b17482004-08-16 00:39:03 +000011302 if (doc->dict != NULL) {
William M. Brackc3f81342004-10-03 01:22:44 +000011303 if (ctxt->dict != NULL)
Daniel Veillard29b17482004-08-16 00:39:03 +000011304 xmlDictFree(ctxt->dict);
11305 ctxt->dict = doc->dict;
William M. Brackc3f81342004-10-03 01:22:44 +000011306 } else
11307 options |= XML_PARSE_NODICT;
11308
11309 xmlCtxtUseOptions(ctxt, options);
Daniel Veillard29b17482004-08-16 00:39:03 +000011310 xmlDetectSAX2(ctxt);
11311 ctxt->myDoc = doc;
11312
11313 if (node->type == XML_ELEMENT_NODE) {
11314 nodePush(ctxt, node);
11315 /*
11316 * initialize the SAX2 namespaces stack
11317 */
11318 cur = node;
11319 while ((cur != NULL) && (cur->type == XML_ELEMENT_NODE)) {
11320 xmlNsPtr ns = cur->nsDef;
11321 const xmlChar *iprefix, *ihref;
11322
11323 while (ns != NULL) {
11324 if (ctxt->dict) {
11325 iprefix = xmlDictLookup(ctxt->dict, ns->prefix, -1);
11326 ihref = xmlDictLookup(ctxt->dict, ns->href, -1);
11327 } else {
11328 iprefix = ns->prefix;
11329 ihref = ns->href;
11330 }
11331
11332 if (xmlGetNamespace(ctxt, iprefix) == NULL) {
11333 nsPush(ctxt, iprefix, ihref);
11334 nsnr++;
11335 }
11336 ns = ns->next;
11337 }
11338 cur = cur->parent;
11339 }
11340 ctxt->instate = XML_PARSER_CONTENT;
11341 }
11342
11343 if ((ctxt->validate) || (ctxt->replaceEntities != 0)) {
11344 /*
11345 * ID/IDREF registration will be done in xmlValidateElement below
11346 */
11347 ctxt->loadsubset |= XML_SKIP_IDS;
11348 }
11349
11350 xmlParseContent(ctxt);
11351 nsPop(ctxt, nsnr);
11352 if ((RAW == '<') && (NXT(1) == '/')) {
11353 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
11354 } else if (RAW != 0) {
11355 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
11356 }
11357 if ((ctxt->node != NULL) && (ctxt->node != node)) {
11358 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
11359 ctxt->wellFormed = 0;
11360 }
11361
11362 if (!ctxt->wellFormed) {
11363 if (ctxt->errNo == 0)
11364 ret = XML_ERR_INTERNAL_ERROR;
11365 else
11366 ret = (xmlParserErrors)ctxt->errNo;
11367 } else {
11368 ret = XML_ERR_OK;
11369 }
11370
11371 /*
11372 * Return the newly created nodeset after unlinking it from
11373 * the pseudo sibling.
11374 */
11375
11376 cur = fake->next;
11377 fake->next = NULL;
11378 node->last = fake;
11379
11380 if (cur != NULL) {
11381 cur->prev = NULL;
11382 }
11383
11384 *lst = cur;
11385
11386 while (cur != NULL) {
11387 cur->parent = NULL;
11388 cur = cur->next;
11389 }
11390
11391 xmlUnlinkNode(fake);
11392 xmlFreeNode(fake);
11393
11394
11395 if (ret != XML_ERR_OK) {
11396 xmlFreeNodeList(*lst);
11397 *lst = NULL;
11398 }
William M. Brackc3f81342004-10-03 01:22:44 +000011399
William M. Brackb7b54de2004-10-06 16:38:01 +000011400 if (doc->dict != NULL)
11401 ctxt->dict = NULL;
Daniel Veillard29b17482004-08-16 00:39:03 +000011402 xmlFreeParserCtxt(ctxt);
11403
11404 return(ret);
11405#else /* !SAX2 */
11406 return(XML_ERR_INTERNAL_ERROR);
11407#endif
11408}
11409
Daniel Veillard81273902003-09-30 00:43:48 +000011410#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard328f48c2002-11-15 15:24:34 +000011411/**
Daniel Veillard58e44c92002-08-02 22:19:49 +000011412 * xmlParseBalancedChunkMemoryRecover:
11413 * @doc: the document the chunk pertains to
11414 * @sax: the SAX handler bloc (possibly NULL)
11415 * @user_data: The user data returned on SAX callbacks (possibly NULL)
11416 * @depth: Used for loop detection, use 0
11417 * @string: the input string in UTF8 or ISO-Latin (zero terminated)
11418 * @lst: the return value for the set of parsed nodes
11419 * @recover: return nodes even if the data is broken (use 0)
11420 *
11421 *
11422 * Parse a well-balanced chunk of an XML document
11423 * called by the parser
11424 * The allowed sequence for the Well Balanced Chunk is the one defined by
11425 * the content production in the XML grammar:
11426 *
11427 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
11428 *
11429 * Returns 0 if the chunk is well balanced, -1 in case of args problem and
11430 * the parser error code otherwise
11431 *
11432 * In case recover is set to 1, the nodelist will not be empty even if
11433 * the parsed chunk is not well balanced.
11434 */
11435int
11436xmlParseBalancedChunkMemoryRecover(xmlDocPtr doc, xmlSAXHandlerPtr sax,
11437 void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst,
11438 int recover) {
Owen Taylor3473f882001-02-23 17:55:21 +000011439 xmlParserCtxtPtr ctxt;
11440 xmlDocPtr newDoc;
11441 xmlSAXHandlerPtr oldsax = NULL;
Daniel Veillardacbe6cf2004-10-31 21:04:50 +000011442 xmlNodePtr content, newRoot;
Owen Taylor3473f882001-02-23 17:55:21 +000011443 int size;
11444 int ret = 0;
11445
11446 if (depth > 40) {
11447 return(XML_ERR_ENTITY_LOOP);
11448 }
11449
11450
Daniel Veillardcda96922001-08-21 10:56:31 +000011451 if (lst != NULL)
11452 *lst = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000011453 if (string == NULL)
11454 return(-1);
11455
11456 size = xmlStrlen(string);
11457
11458 ctxt = xmlCreateMemoryParserCtxt((char *) string, size);
11459 if (ctxt == NULL) return(-1);
11460 ctxt->userData = ctxt;
11461 if (sax != NULL) {
11462 oldsax = ctxt->sax;
11463 ctxt->sax = sax;
11464 if (user_data != NULL)
11465 ctxt->userData = user_data;
11466 }
11467 newDoc = xmlNewDoc(BAD_CAST "1.0");
11468 if (newDoc == NULL) {
11469 xmlFreeParserCtxt(ctxt);
11470 return(-1);
11471 }
Daniel Veillardacbe6cf2004-10-31 21:04:50 +000011472 if ((doc != NULL) && (doc->dict != NULL)) {
11473 xmlDictFree(ctxt->dict);
11474 ctxt->dict = doc->dict;
11475 xmlDictReference(ctxt->dict);
11476 ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
11477 ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
11478 ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
11479 ctxt->dictNames = 1;
11480 } else {
11481 xmlCtxtUseOptions(ctxt, XML_PARSE_NODICT);
11482 }
Owen Taylor3473f882001-02-23 17:55:21 +000011483 if (doc != NULL) {
11484 newDoc->intSubset = doc->intSubset;
11485 newDoc->extSubset = doc->extSubset;
11486 }
Daniel Veillardacbe6cf2004-10-31 21:04:50 +000011487 newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
11488 if (newRoot == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +000011489 if (sax != NULL)
11490 ctxt->sax = oldsax;
11491 xmlFreeParserCtxt(ctxt);
11492 newDoc->intSubset = NULL;
11493 newDoc->extSubset = NULL;
11494 xmlFreeDoc(newDoc);
11495 return(-1);
11496 }
Daniel Veillardacbe6cf2004-10-31 21:04:50 +000011497 xmlAddChild((xmlNodePtr) newDoc, newRoot);
11498 nodePush(ctxt, newRoot);
Owen Taylor3473f882001-02-23 17:55:21 +000011499 if (doc == NULL) {
11500 ctxt->myDoc = newDoc;
11501 } else {
Daniel Veillard42766c02002-08-22 20:52:17 +000011502 ctxt->myDoc = newDoc;
Owen Taylor3473f882001-02-23 17:55:21 +000011503 newDoc->children->doc = doc;
11504 }
11505 ctxt->instate = XML_PARSER_CONTENT;
11506 ctxt->depth = depth;
11507
11508 /*
11509 * Doing validity checking on chunk doesn't make sense
11510 */
11511 ctxt->validate = 0;
11512 ctxt->loadsubset = 0;
Daniel Veillarde57ec792003-09-10 10:50:59 +000011513 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011514
Daniel Veillardb39bc392002-10-26 19:29:51 +000011515 if ( doc != NULL ){
11516 content = doc->children;
11517 doc->children = NULL;
11518 xmlParseContent(ctxt);
11519 doc->children = content;
11520 }
11521 else {
11522 xmlParseContent(ctxt);
11523 }
Owen Taylor3473f882001-02-23 17:55:21 +000011524 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000011525 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000011526 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000011527 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000011528 }
11529 if (ctxt->node != newDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000011530 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000011531 }
11532
11533 if (!ctxt->wellFormed) {
11534 if (ctxt->errNo == 0)
11535 ret = 1;
11536 else
11537 ret = ctxt->errNo;
11538 } else {
Daniel Veillard58e44c92002-08-02 22:19:49 +000011539 ret = 0;
11540 }
11541
Daniel Veillardacbe6cf2004-10-31 21:04:50 +000011542 if ((lst != NULL) && ((ret == 0) || (recover == 1))) {
11543 xmlNodePtr cur;
Owen Taylor3473f882001-02-23 17:55:21 +000011544
Daniel Veillardacbe6cf2004-10-31 21:04:50 +000011545 /*
11546 * Return the newly created nodeset after unlinking it from
11547 * they pseudo parent.
11548 */
11549 cur = newDoc->children->children;
11550 *lst = cur;
11551 while (cur != NULL) {
11552 xmlSetTreeDoc(cur, doc);
11553 cur->parent = NULL;
11554 cur = cur->next;
Owen Taylor3473f882001-02-23 17:55:21 +000011555 }
Daniel Veillardacbe6cf2004-10-31 21:04:50 +000011556 newDoc->children->children = NULL;
11557 }
Daniel Veillard58e44c92002-08-02 22:19:49 +000011558
Owen Taylor3473f882001-02-23 17:55:21 +000011559 if (sax != NULL)
11560 ctxt->sax = oldsax;
11561 xmlFreeParserCtxt(ctxt);
11562 newDoc->intSubset = NULL;
11563 newDoc->extSubset = NULL;
11564 xmlFreeDoc(newDoc);
11565
11566 return(ret);
11567}
11568
11569/**
11570 * xmlSAXParseEntity:
11571 * @sax: the SAX handler block
11572 * @filename: the filename
11573 *
11574 * parse an XML external entity out of context and build a tree.
11575 * It use the given SAX function block to handle the parsing callback.
11576 * If sax is NULL, fallback to the default DOM tree building routines.
11577 *
11578 * [78] extParsedEnt ::= TextDecl? content
11579 *
11580 * This correspond to a "Well Balanced" chunk
11581 *
11582 * Returns the resulting document tree
11583 */
11584
11585xmlDocPtr
11586xmlSAXParseEntity(xmlSAXHandlerPtr sax, const char *filename) {
11587 xmlDocPtr ret;
11588 xmlParserCtxtPtr ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +000011589
11590 ctxt = xmlCreateFileParserCtxt(filename);
11591 if (ctxt == NULL) {
11592 return(NULL);
11593 }
11594 if (sax != NULL) {
11595 if (ctxt->sax != NULL)
11596 xmlFree(ctxt->sax);
11597 ctxt->sax = sax;
11598 ctxt->userData = NULL;
11599 }
11600
Owen Taylor3473f882001-02-23 17:55:21 +000011601 xmlParseExtParsedEnt(ctxt);
11602
11603 if (ctxt->wellFormed)
11604 ret = ctxt->myDoc;
11605 else {
11606 ret = NULL;
11607 xmlFreeDoc(ctxt->myDoc);
11608 ctxt->myDoc = NULL;
11609 }
11610 if (sax != NULL)
11611 ctxt->sax = NULL;
11612 xmlFreeParserCtxt(ctxt);
11613
11614 return(ret);
11615}
11616
11617/**
11618 * xmlParseEntity:
11619 * @filename: the filename
11620 *
11621 * parse an XML external entity out of context and build a tree.
11622 *
11623 * [78] extParsedEnt ::= TextDecl? content
11624 *
11625 * This correspond to a "Well Balanced" chunk
11626 *
11627 * Returns the resulting document tree
11628 */
11629
11630xmlDocPtr
11631xmlParseEntity(const char *filename) {
11632 return(xmlSAXParseEntity(NULL, filename));
11633}
Daniel Veillard81273902003-09-30 00:43:48 +000011634#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011635
11636/**
11637 * xmlCreateEntityParserCtxt:
11638 * @URL: the entity URL
11639 * @ID: the entity PUBLIC ID
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011640 * @base: a possible base for the target URI
Owen Taylor3473f882001-02-23 17:55:21 +000011641 *
11642 * Create a parser context for an external entity
11643 * Automatic support for ZLIB/Compress compressed document is provided
11644 * by default if found at compile-time.
11645 *
11646 * Returns the new parser context or NULL
11647 */
11648xmlParserCtxtPtr
11649xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID,
11650 const xmlChar *base) {
11651 xmlParserCtxtPtr ctxt;
11652 xmlParserInputPtr inputStream;
11653 char *directory = NULL;
11654 xmlChar *uri;
11655
11656 ctxt = xmlNewParserCtxt();
11657 if (ctxt == NULL) {
11658 return(NULL);
11659 }
11660
11661 uri = xmlBuildURI(URL, base);
11662
11663 if (uri == NULL) {
11664 inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt);
11665 if (inputStream == NULL) {
11666 xmlFreeParserCtxt(ctxt);
11667 return(NULL);
11668 }
11669
11670 inputPush(ctxt, inputStream);
11671
11672 if ((ctxt->directory == NULL) && (directory == NULL))
11673 directory = xmlParserGetDirectory((char *)URL);
11674 if ((ctxt->directory == NULL) && (directory != NULL))
11675 ctxt->directory = directory;
11676 } else {
11677 inputStream = xmlLoadExternalEntity((char *)uri, (char *)ID, ctxt);
11678 if (inputStream == NULL) {
11679 xmlFree(uri);
11680 xmlFreeParserCtxt(ctxt);
11681 return(NULL);
11682 }
11683
11684 inputPush(ctxt, inputStream);
11685
11686 if ((ctxt->directory == NULL) && (directory == NULL))
11687 directory = xmlParserGetDirectory((char *)uri);
11688 if ((ctxt->directory == NULL) && (directory != NULL))
11689 ctxt->directory = directory;
11690 xmlFree(uri);
11691 }
Owen Taylor3473f882001-02-23 17:55:21 +000011692 return(ctxt);
11693}
11694
11695/************************************************************************
11696 * *
11697 * Front ends when parsing from a file *
11698 * *
11699 ************************************************************************/
11700
11701/**
Daniel Veillard61b93382003-11-03 14:28:31 +000011702 * xmlCreateURLParserCtxt:
11703 * @filename: the filename or URL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000011704 * @options: a combination of xmlParserOption
Owen Taylor3473f882001-02-23 17:55:21 +000011705 *
Daniel Veillard61b93382003-11-03 14:28:31 +000011706 * Create a parser context for a file or URL content.
Owen Taylor3473f882001-02-23 17:55:21 +000011707 * Automatic support for ZLIB/Compress compressed document is provided
Daniel Veillard61b93382003-11-03 14:28:31 +000011708 * by default if found at compile-time and for file accesses
Owen Taylor3473f882001-02-23 17:55:21 +000011709 *
11710 * Returns the new parser context or NULL
11711 */
11712xmlParserCtxtPtr
Daniel Veillard61b93382003-11-03 14:28:31 +000011713xmlCreateURLParserCtxt(const char *filename, int options)
Owen Taylor3473f882001-02-23 17:55:21 +000011714{
11715 xmlParserCtxtPtr ctxt;
11716 xmlParserInputPtr inputStream;
Owen Taylor3473f882001-02-23 17:55:21 +000011717 char *directory = NULL;
11718
Owen Taylor3473f882001-02-23 17:55:21 +000011719 ctxt = xmlNewParserCtxt();
11720 if (ctxt == NULL) {
Daniel Veillard81273902003-09-30 00:43:48 +000011721 xmlErrMemory(NULL, "cannot allocate parser context");
Owen Taylor3473f882001-02-23 17:55:21 +000011722 return(NULL);
11723 }
11724
Daniel Veillarddf292f72005-01-16 19:00:15 +000011725 if (options)
11726 xmlCtxtUseOptions(ctxt, options);
11727 ctxt->linenumbers = 1;
Igor Zlatkovicce076162003-02-23 13:39:39 +000011728
Daniel Veillard4e9b1bc2003-06-09 10:30:33 +000011729 inputStream = xmlLoadExternalEntity(filename, NULL, ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011730 if (inputStream == NULL) {
11731 xmlFreeParserCtxt(ctxt);
11732 return(NULL);
11733 }
11734
Owen Taylor3473f882001-02-23 17:55:21 +000011735 inputPush(ctxt, inputStream);
11736 if ((ctxt->directory == NULL) && (directory == NULL))
Igor Zlatkovic5f9fada2003-02-19 14:51:00 +000011737 directory = xmlParserGetDirectory(filename);
Owen Taylor3473f882001-02-23 17:55:21 +000011738 if ((ctxt->directory == NULL) && (directory != NULL))
11739 ctxt->directory = directory;
11740
11741 return(ctxt);
11742}
11743
Daniel Veillard61b93382003-11-03 14:28:31 +000011744/**
11745 * xmlCreateFileParserCtxt:
11746 * @filename: the filename
11747 *
11748 * Create a parser context for a file content.
11749 * Automatic support for ZLIB/Compress compressed document is provided
11750 * by default if found at compile-time.
11751 *
11752 * Returns the new parser context or NULL
11753 */
11754xmlParserCtxtPtr
11755xmlCreateFileParserCtxt(const char *filename)
11756{
11757 return(xmlCreateURLParserCtxt(filename, 0));
11758}
11759
Daniel Veillard81273902003-09-30 00:43:48 +000011760#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000011761/**
Daniel Veillarda293c322001-10-02 13:54:14 +000011762 * xmlSAXParseFileWithData:
Owen Taylor3473f882001-02-23 17:55:21 +000011763 * @sax: the SAX handler block
11764 * @filename: the filename
11765 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11766 * documents
Daniel Veillarda293c322001-10-02 13:54:14 +000011767 * @data: the userdata
Owen Taylor3473f882001-02-23 17:55:21 +000011768 *
11769 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11770 * compressed document is provided by default if found at compile-time.
11771 * It use the given SAX function block to handle the parsing callback.
11772 * If sax is NULL, fallback to the default DOM tree building routines.
11773 *
Daniel Veillarde19fc232002-04-22 16:01:24 +000011774 * User data (void *) is stored within the parser context in the
11775 * context's _private member, so it is available nearly everywhere in libxml
Daniel Veillarda293c322001-10-02 13:54:14 +000011776 *
Owen Taylor3473f882001-02-23 17:55:21 +000011777 * Returns the resulting document tree
11778 */
11779
11780xmlDocPtr
Daniel Veillarda293c322001-10-02 13:54:14 +000011781xmlSAXParseFileWithData(xmlSAXHandlerPtr sax, const char *filename,
11782 int recovery, void *data) {
Owen Taylor3473f882001-02-23 17:55:21 +000011783 xmlDocPtr ret;
11784 xmlParserCtxtPtr ctxt;
11785 char *directory = NULL;
11786
Daniel Veillard635ef722001-10-29 11:48:19 +000011787 xmlInitParser();
11788
Owen Taylor3473f882001-02-23 17:55:21 +000011789 ctxt = xmlCreateFileParserCtxt(filename);
11790 if (ctxt == NULL) {
11791 return(NULL);
11792 }
11793 if (sax != NULL) {
11794 if (ctxt->sax != NULL)
11795 xmlFree(ctxt->sax);
11796 ctxt->sax = sax;
Owen Taylor3473f882001-02-23 17:55:21 +000011797 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000011798 xmlDetectSAX2(ctxt);
Daniel Veillarda293c322001-10-02 13:54:14 +000011799 if (data!=NULL) {
Daniel Veillardc790bf42003-10-11 10:50:10 +000011800 ctxt->_private = data;
Daniel Veillarda293c322001-10-02 13:54:14 +000011801 }
Owen Taylor3473f882001-02-23 17:55:21 +000011802
11803 if ((ctxt->directory == NULL) && (directory == NULL))
11804 directory = xmlParserGetDirectory(filename);
11805 if ((ctxt->directory == NULL) && (directory != NULL))
11806 ctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
11807
Daniel Veillarddad3f682002-11-17 16:47:27 +000011808 ctxt->recovery = recovery;
11809
Owen Taylor3473f882001-02-23 17:55:21 +000011810 xmlParseDocument(ctxt);
11811
William M. Brackc07329e2003-09-08 01:57:30 +000011812 if ((ctxt->wellFormed) || recovery) {
11813 ret = ctxt->myDoc;
Daniel Veillardb65e12e2003-10-08 21:33:28 +000011814 if (ret != NULL) {
11815 if (ctxt->input->buf->compressed > 0)
11816 ret->compression = 9;
11817 else
11818 ret->compression = ctxt->input->buf->compressed;
11819 }
William M. Brackc07329e2003-09-08 01:57:30 +000011820 }
Owen Taylor3473f882001-02-23 17:55:21 +000011821 else {
11822 ret = NULL;
11823 xmlFreeDoc(ctxt->myDoc);
11824 ctxt->myDoc = NULL;
11825 }
11826 if (sax != NULL)
11827 ctxt->sax = NULL;
11828 xmlFreeParserCtxt(ctxt);
11829
11830 return(ret);
11831}
11832
11833/**
Daniel Veillarda293c322001-10-02 13:54:14 +000011834 * xmlSAXParseFile:
11835 * @sax: the SAX handler block
11836 * @filename: the filename
11837 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11838 * documents
11839 *
11840 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11841 * compressed document is provided by default if found at compile-time.
11842 * It use the given SAX function block to handle the parsing callback.
11843 * If sax is NULL, fallback to the default DOM tree building routines.
11844 *
11845 * Returns the resulting document tree
11846 */
11847
11848xmlDocPtr
11849xmlSAXParseFile(xmlSAXHandlerPtr sax, const char *filename,
11850 int recovery) {
11851 return(xmlSAXParseFileWithData(sax,filename,recovery,NULL));
11852}
11853
11854/**
Owen Taylor3473f882001-02-23 17:55:21 +000011855 * xmlRecoverDoc:
11856 * @cur: a pointer to an array of xmlChar
11857 *
11858 * parse an XML in-memory document and build a tree.
11859 * In the case the document is not Well Formed, a tree is built anyway
11860 *
11861 * Returns the resulting document tree
11862 */
11863
11864xmlDocPtr
11865xmlRecoverDoc(xmlChar *cur) {
11866 return(xmlSAXParseDoc(NULL, cur, 1));
11867}
11868
11869/**
11870 * xmlParseFile:
11871 * @filename: the filename
11872 *
11873 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11874 * compressed document is provided by default if found at compile-time.
11875 *
Daniel Veillard5d96fff2001-08-31 14:55:30 +000011876 * Returns the resulting document tree if the file was wellformed,
11877 * NULL otherwise.
Owen Taylor3473f882001-02-23 17:55:21 +000011878 */
11879
11880xmlDocPtr
11881xmlParseFile(const char *filename) {
11882 return(xmlSAXParseFile(NULL, filename, 0));
11883}
11884
11885/**
11886 * xmlRecoverFile:
11887 * @filename: the filename
11888 *
11889 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11890 * compressed document is provided by default if found at compile-time.
11891 * In the case the document is not Well Formed, a tree is built anyway
11892 *
11893 * Returns the resulting document tree
11894 */
11895
11896xmlDocPtr
11897xmlRecoverFile(const char *filename) {
11898 return(xmlSAXParseFile(NULL, filename, 1));
11899}
11900
11901
11902/**
11903 * xmlSetupParserForBuffer:
11904 * @ctxt: an XML parser context
11905 * @buffer: a xmlChar * buffer
11906 * @filename: a file name
11907 *
11908 * Setup the parser context to parse a new buffer; Clears any prior
11909 * contents from the parser context. The buffer parameter must not be
11910 * NULL, but the filename parameter can be
11911 */
11912void
11913xmlSetupParserForBuffer(xmlParserCtxtPtr ctxt, const xmlChar* buffer,
11914 const char* filename)
11915{
11916 xmlParserInputPtr input;
11917
Daniel Veillard36e5cd52004-11-02 14:52:23 +000011918 if ((ctxt == NULL) || (buffer == NULL))
11919 return;
11920
Owen Taylor3473f882001-02-23 17:55:21 +000011921 input = xmlNewInputStream(ctxt);
11922 if (input == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +000011923 xmlErrMemory(NULL, "parsing new buffer: out of memory\n");
Daniel Veillard36e5cd52004-11-02 14:52:23 +000011924 xmlClearParserCtxt(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011925 return;
11926 }
11927
11928 xmlClearParserCtxt(ctxt);
11929 if (filename != NULL)
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +000011930 input->filename = (char *) xmlCanonicPath((const xmlChar *)filename);
Owen Taylor3473f882001-02-23 17:55:21 +000011931 input->base = buffer;
11932 input->cur = buffer;
Daniel Veillard48b2f892001-02-25 16:11:03 +000011933 input->end = &buffer[xmlStrlen(buffer)];
Owen Taylor3473f882001-02-23 17:55:21 +000011934 inputPush(ctxt, input);
11935}
11936
11937/**
11938 * xmlSAXUserParseFile:
11939 * @sax: a SAX handler
11940 * @user_data: The user data returned on SAX callbacks
11941 * @filename: a file name
11942 *
11943 * parse an XML file and call the given SAX handler routines.
11944 * Automatic support for ZLIB/Compress compressed document is provided
11945 *
11946 * Returns 0 in case of success or a error number otherwise
11947 */
11948int
11949xmlSAXUserParseFile(xmlSAXHandlerPtr sax, void *user_data,
11950 const char *filename) {
11951 int ret = 0;
11952 xmlParserCtxtPtr ctxt;
11953
11954 ctxt = xmlCreateFileParserCtxt(filename);
11955 if (ctxt == NULL) return -1;
Daniel Veillard81273902003-09-30 00:43:48 +000011956#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +000011957 if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
Daniel Veillard81273902003-09-30 00:43:48 +000011958#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011959 xmlFree(ctxt->sax);
11960 ctxt->sax = sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000011961 xmlDetectSAX2(ctxt);
11962
Owen Taylor3473f882001-02-23 17:55:21 +000011963 if (user_data != NULL)
11964 ctxt->userData = user_data;
11965
11966 xmlParseDocument(ctxt);
11967
11968 if (ctxt->wellFormed)
11969 ret = 0;
11970 else {
11971 if (ctxt->errNo != 0)
11972 ret = ctxt->errNo;
11973 else
11974 ret = -1;
11975 }
11976 if (sax != NULL)
11977 ctxt->sax = NULL;
Daniel Veillard34099b42004-11-04 17:34:35 +000011978 if (ctxt->myDoc != NULL) {
11979 xmlFreeDoc(ctxt->myDoc);
11980 ctxt->myDoc = NULL;
11981 }
Owen Taylor3473f882001-02-23 17:55:21 +000011982 xmlFreeParserCtxt(ctxt);
11983
11984 return ret;
11985}
Daniel Veillard81273902003-09-30 00:43:48 +000011986#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011987
11988/************************************************************************
11989 * *
11990 * Front ends when parsing from memory *
11991 * *
11992 ************************************************************************/
11993
11994/**
11995 * xmlCreateMemoryParserCtxt:
11996 * @buffer: a pointer to a char array
11997 * @size: the size of the array
11998 *
11999 * Create a parser context for an XML in-memory document.
12000 *
12001 * Returns the new parser context or NULL
12002 */
12003xmlParserCtxtPtr
Daniel Veillardfd7ddca2001-05-16 10:57:35 +000012004xmlCreateMemoryParserCtxt(const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000012005 xmlParserCtxtPtr ctxt;
12006 xmlParserInputPtr input;
12007 xmlParserInputBufferPtr buf;
12008
12009 if (buffer == NULL)
12010 return(NULL);
12011 if (size <= 0)
12012 return(NULL);
12013
12014 ctxt = xmlNewParserCtxt();
12015 if (ctxt == NULL)
12016 return(NULL);
12017
Daniel Veillard53350552003-09-18 13:35:51 +000012018 /* TODO: xmlParserInputBufferCreateStatic, requires some serious changes */
Owen Taylor3473f882001-02-23 17:55:21 +000012019 buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
Daniel Veillarda7e05b42002-11-19 08:11:14 +000012020 if (buf == NULL) {
12021 xmlFreeParserCtxt(ctxt);
12022 return(NULL);
12023 }
Owen Taylor3473f882001-02-23 17:55:21 +000012024
12025 input = xmlNewInputStream(ctxt);
12026 if (input == NULL) {
Daniel Veillarda7e05b42002-11-19 08:11:14 +000012027 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +000012028 xmlFreeParserCtxt(ctxt);
12029 return(NULL);
12030 }
12031
12032 input->filename = NULL;
12033 input->buf = buf;
12034 input->base = input->buf->buffer->content;
12035 input->cur = input->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +000012036 input->end = &input->buf->buffer->content[input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +000012037
12038 inputPush(ctxt, input);
12039 return(ctxt);
12040}
12041
Daniel Veillard81273902003-09-30 00:43:48 +000012042#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000012043/**
Daniel Veillard8606bbb2002-11-12 12:36:52 +000012044 * xmlSAXParseMemoryWithData:
12045 * @sax: the SAX handler block
12046 * @buffer: an pointer to a char array
12047 * @size: the size of the array
12048 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
12049 * documents
12050 * @data: the userdata
12051 *
12052 * parse an XML in-memory block and use the given SAX function block
12053 * to handle the parsing callback. If sax is NULL, fallback to the default
12054 * DOM tree building routines.
12055 *
12056 * User data (void *) is stored within the parser context in the
12057 * context's _private member, so it is available nearly everywhere in libxml
12058 *
12059 * Returns the resulting document tree
12060 */
12061
12062xmlDocPtr
12063xmlSAXParseMemoryWithData(xmlSAXHandlerPtr sax, const char *buffer,
12064 int size, int recovery, void *data) {
12065 xmlDocPtr ret;
12066 xmlParserCtxtPtr ctxt;
12067
12068 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
12069 if (ctxt == NULL) return(NULL);
12070 if (sax != NULL) {
12071 if (ctxt->sax != NULL)
12072 xmlFree(ctxt->sax);
12073 ctxt->sax = sax;
12074 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000012075 xmlDetectSAX2(ctxt);
Daniel Veillard8606bbb2002-11-12 12:36:52 +000012076 if (data!=NULL) {
12077 ctxt->_private=data;
12078 }
12079
Daniel Veillardadba5f12003-04-04 16:09:01 +000012080 ctxt->recovery = recovery;
12081
Daniel Veillard8606bbb2002-11-12 12:36:52 +000012082 xmlParseDocument(ctxt);
12083
12084 if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc;
12085 else {
12086 ret = NULL;
12087 xmlFreeDoc(ctxt->myDoc);
12088 ctxt->myDoc = NULL;
12089 }
12090 if (sax != NULL)
12091 ctxt->sax = NULL;
12092 xmlFreeParserCtxt(ctxt);
12093
12094 return(ret);
12095}
12096
12097/**
Owen Taylor3473f882001-02-23 17:55:21 +000012098 * xmlSAXParseMemory:
12099 * @sax: the SAX handler block
12100 * @buffer: an pointer to a char array
12101 * @size: the size of the array
12102 * @recovery: work in recovery mode, i.e. tries to read not Well Formed
12103 * documents
12104 *
12105 * parse an XML in-memory block and use the given SAX function block
12106 * to handle the parsing callback. If sax is NULL, fallback to the default
12107 * DOM tree building routines.
12108 *
12109 * Returns the resulting document tree
12110 */
12111xmlDocPtr
Daniel Veillard50822cb2001-07-26 20:05:51 +000012112xmlSAXParseMemory(xmlSAXHandlerPtr sax, const char *buffer,
12113 int size, int recovery) {
Daniel Veillard8606bbb2002-11-12 12:36:52 +000012114 return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000012115}
12116
12117/**
12118 * xmlParseMemory:
12119 * @buffer: an pointer to a char array
12120 * @size: the size of the array
12121 *
12122 * parse an XML in-memory block and build a tree.
12123 *
12124 * Returns the resulting document tree
12125 */
12126
Daniel Veillard50822cb2001-07-26 20:05:51 +000012127xmlDocPtr xmlParseMemory(const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000012128 return(xmlSAXParseMemory(NULL, buffer, size, 0));
12129}
12130
12131/**
12132 * xmlRecoverMemory:
12133 * @buffer: an pointer to a char array
12134 * @size: the size of the array
12135 *
12136 * parse an XML in-memory block and build a tree.
12137 * In the case the document is not Well Formed, a tree is built anyway
12138 *
12139 * Returns the resulting document tree
12140 */
12141
Daniel Veillard50822cb2001-07-26 20:05:51 +000012142xmlDocPtr xmlRecoverMemory(const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000012143 return(xmlSAXParseMemory(NULL, buffer, size, 1));
12144}
12145
12146/**
12147 * xmlSAXUserParseMemory:
12148 * @sax: a SAX handler
12149 * @user_data: The user data returned on SAX callbacks
12150 * @buffer: an in-memory XML document input
12151 * @size: the length of the XML document in bytes
12152 *
12153 * A better SAX parsing routine.
12154 * parse an XML in-memory buffer and call the given SAX handler routines.
12155 *
12156 * Returns 0 in case of success or a error number otherwise
12157 */
12158int xmlSAXUserParseMemory(xmlSAXHandlerPtr sax, void *user_data,
Daniel Veillardfd7ddca2001-05-16 10:57:35 +000012159 const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000012160 int ret = 0;
12161 xmlParserCtxtPtr ctxt;
12162 xmlSAXHandlerPtr oldsax = NULL;
12163
Daniel Veillard9e923512002-08-14 08:48:52 +000012164 if (sax == NULL) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +000012165 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
12166 if (ctxt == NULL) return -1;
Daniel Veillard9e923512002-08-14 08:48:52 +000012167 oldsax = ctxt->sax;
12168 ctxt->sax = sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000012169 xmlDetectSAX2(ctxt);
Daniel Veillard30211a02001-04-26 09:33:18 +000012170 if (user_data != NULL)
12171 ctxt->userData = user_data;
Owen Taylor3473f882001-02-23 17:55:21 +000012172
12173 xmlParseDocument(ctxt);
12174
12175 if (ctxt->wellFormed)
12176 ret = 0;
12177 else {
12178 if (ctxt->errNo != 0)
12179 ret = ctxt->errNo;
12180 else
12181 ret = -1;
12182 }
Daniel Veillard9e923512002-08-14 08:48:52 +000012183 ctxt->sax = oldsax;
Daniel Veillard34099b42004-11-04 17:34:35 +000012184 if (ctxt->myDoc != NULL) {
12185 xmlFreeDoc(ctxt->myDoc);
12186 ctxt->myDoc = NULL;
12187 }
Owen Taylor3473f882001-02-23 17:55:21 +000012188 xmlFreeParserCtxt(ctxt);
12189
12190 return ret;
12191}
Daniel Veillard81273902003-09-30 00:43:48 +000012192#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000012193
12194/**
12195 * xmlCreateDocParserCtxt:
12196 * @cur: a pointer to an array of xmlChar
12197 *
12198 * Creates a parser context for an XML in-memory document.
12199 *
12200 * Returns the new parser context or NULL
12201 */
12202xmlParserCtxtPtr
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012203xmlCreateDocParserCtxt(const xmlChar *cur) {
Owen Taylor3473f882001-02-23 17:55:21 +000012204 int len;
12205
12206 if (cur == NULL)
12207 return(NULL);
12208 len = xmlStrlen(cur);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012209 return(xmlCreateMemoryParserCtxt((const char *)cur, len));
Owen Taylor3473f882001-02-23 17:55:21 +000012210}
12211
Daniel Veillard81273902003-09-30 00:43:48 +000012212#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000012213/**
12214 * xmlSAXParseDoc:
12215 * @sax: the SAX handler block
12216 * @cur: a pointer to an array of xmlChar
12217 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
12218 * documents
12219 *
12220 * parse an XML in-memory document and build a tree.
12221 * It use the given SAX function block to handle the parsing callback.
12222 * If sax is NULL, fallback to the default DOM tree building routines.
12223 *
12224 * Returns the resulting document tree
12225 */
12226
12227xmlDocPtr
Daniel Veillard7331e5c2005-03-31 14:59:00 +000012228xmlSAXParseDoc(xmlSAXHandlerPtr sax, const xmlChar *cur, int recovery) {
Owen Taylor3473f882001-02-23 17:55:21 +000012229 xmlDocPtr ret;
12230 xmlParserCtxtPtr ctxt;
Daniel Veillard38936062004-11-04 17:45:11 +000012231 xmlSAXHandlerPtr oldsax = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000012232
Daniel Veillard38936062004-11-04 17:45:11 +000012233 if (cur == NULL) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000012234
12235
12236 ctxt = xmlCreateDocParserCtxt(cur);
12237 if (ctxt == NULL) return(NULL);
12238 if (sax != NULL) {
Daniel Veillard38936062004-11-04 17:45:11 +000012239 oldsax = ctxt->sax;
Owen Taylor3473f882001-02-23 17:55:21 +000012240 ctxt->sax = sax;
12241 ctxt->userData = NULL;
12242 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000012243 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000012244
12245 xmlParseDocument(ctxt);
12246 if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc;
12247 else {
12248 ret = NULL;
12249 xmlFreeDoc(ctxt->myDoc);
12250 ctxt->myDoc = NULL;
12251 }
Daniel Veillard34099b42004-11-04 17:34:35 +000012252 if (sax != NULL)
Daniel Veillard38936062004-11-04 17:45:11 +000012253 ctxt->sax = oldsax;
Owen Taylor3473f882001-02-23 17:55:21 +000012254 xmlFreeParserCtxt(ctxt);
12255
12256 return(ret);
12257}
12258
12259/**
12260 * xmlParseDoc:
12261 * @cur: a pointer to an array of xmlChar
12262 *
12263 * parse an XML in-memory document and build a tree.
12264 *
12265 * Returns the resulting document tree
12266 */
12267
12268xmlDocPtr
Daniel Veillard7331e5c2005-03-31 14:59:00 +000012269xmlParseDoc(const xmlChar *cur) {
Owen Taylor3473f882001-02-23 17:55:21 +000012270 return(xmlSAXParseDoc(NULL, cur, 0));
12271}
Daniel Veillard81273902003-09-30 00:43:48 +000012272#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000012273
Daniel Veillard81273902003-09-30 00:43:48 +000012274#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +000012275/************************************************************************
12276 * *
12277 * Specific function to keep track of entities references *
12278 * and used by the XSLT debugger *
12279 * *
12280 ************************************************************************/
12281
12282static xmlEntityReferenceFunc xmlEntityRefFunc = NULL;
12283
12284/**
12285 * xmlAddEntityReference:
12286 * @ent : A valid entity
12287 * @firstNode : A valid first node for children of entity
12288 * @lastNode : A valid last node of children entity
12289 *
12290 * Notify of a reference to an entity of type XML_EXTERNAL_GENERAL_PARSED_ENTITY
12291 */
12292static void
12293xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode,
12294 xmlNodePtr lastNode)
12295{
12296 if (xmlEntityRefFunc != NULL) {
12297 (*xmlEntityRefFunc) (ent, firstNode, lastNode);
12298 }
12299}
12300
12301
12302/**
12303 * xmlSetEntityReferenceFunc:
Daniel Veillard01c13b52002-12-10 15:19:08 +000012304 * @func: A valid function
Daniel Veillard8107a222002-01-13 14:10:10 +000012305 *
12306 * Set the function to call call back when a xml reference has been made
12307 */
12308void
12309xmlSetEntityReferenceFunc(xmlEntityReferenceFunc func)
12310{
12311 xmlEntityRefFunc = func;
12312}
Daniel Veillard81273902003-09-30 00:43:48 +000012313#endif /* LIBXML_LEGACY_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000012314
12315/************************************************************************
12316 * *
12317 * Miscellaneous *
12318 * *
12319 ************************************************************************/
12320
12321#ifdef LIBXML_XPATH_ENABLED
12322#include <libxml/xpath.h>
12323#endif
12324
Daniel Veillarddb5850a2002-01-18 11:49:26 +000012325extern void xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...);
Owen Taylor3473f882001-02-23 17:55:21 +000012326static int xmlParserInitialized = 0;
12327
12328/**
12329 * xmlInitParser:
12330 *
12331 * Initialization function for the XML parser.
12332 * This is not reentrant. Call once before processing in case of
12333 * use in multithreaded programs.
12334 */
12335
12336void
12337xmlInitParser(void) {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000012338 if (xmlParserInitialized != 0)
12339 return;
Owen Taylor3473f882001-02-23 17:55:21 +000012340
Daniel Veillarddb5850a2002-01-18 11:49:26 +000012341 if ((xmlGenericError == xmlGenericErrorDefaultFunc) ||
12342 (xmlGenericError == NULL))
12343 initGenericErrorDefaultFunc(NULL);
Daniel Veillard781ac8b2003-05-15 22:11:36 +000012344 xmlInitGlobals();
Daniel Veillardd0463562001-10-13 09:15:48 +000012345 xmlInitThreads();
Daniel Veillard6f350292001-10-14 09:56:15 +000012346 xmlInitMemory();
Owen Taylor3473f882001-02-23 17:55:21 +000012347 xmlInitCharEncodingHandlers();
Owen Taylor3473f882001-02-23 17:55:21 +000012348 xmlDefaultSAXHandlerInit();
12349 xmlRegisterDefaultInputCallbacks();
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000012350#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000012351 xmlRegisterDefaultOutputCallbacks();
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000012352#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000012353#ifdef LIBXML_HTML_ENABLED
12354 htmlInitAutoClose();
12355 htmlDefaultSAXHandlerInit();
12356#endif
12357#ifdef LIBXML_XPATH_ENABLED
12358 xmlXPathInit();
12359#endif
12360 xmlParserInitialized = 1;
12361}
12362
12363/**
12364 * xmlCleanupParser:
12365 *
Daniel Veillardd8cf9062003-11-11 21:12:36 +000012366 * Cleanup function for the XML library. It tries to reclaim all
12367 * parsing related global memory allocated for the library processing.
Owen Taylor3473f882001-02-23 17:55:21 +000012368 * It doesn't deallocate any document related memory. Calling this
Daniel Veillardd8cf9062003-11-11 21:12:36 +000012369 * function should not prevent reusing the library but one should
12370 * call xmlCleanupParser() only when the process has
Daniel Veillard7424eb62003-01-24 14:14:52 +000012371 * finished using the library or XML document built with it.
Owen Taylor3473f882001-02-23 17:55:21 +000012372 */
12373
12374void
12375xmlCleanupParser(void) {
Daniel Veillard7fb801f2003-08-17 21:07:26 +000012376 if (!xmlParserInitialized)
12377 return;
12378
Owen Taylor3473f882001-02-23 17:55:21 +000012379 xmlCleanupCharEncodingHandlers();
Daniel Veillarde2940dd2001-08-22 00:06:49 +000012380#ifdef LIBXML_CATALOG_ENABLED
12381 xmlCatalogCleanup();
12382#endif
Daniel Veillard14412512005-01-21 23:53:26 +000012383 xmlDictCleanup();
Daniel Veillard04054be2003-10-15 10:48:54 +000012384 xmlCleanupInputCallbacks();
12385#ifdef LIBXML_OUTPUT_ENABLED
12386 xmlCleanupOutputCallbacks();
12387#endif
Daniel Veillard36e5cd52004-11-02 14:52:23 +000012388#ifdef LIBXML_SCHEMAS_ENABLED
12389 xmlSchemaCleanupTypes();
Daniel Veillarddd6d3002004-11-03 14:20:29 +000012390 xmlRelaxNGCleanupTypes();
Daniel Veillard36e5cd52004-11-02 14:52:23 +000012391#endif
Daniel Veillard781ac8b2003-05-15 22:11:36 +000012392 xmlCleanupGlobals();
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000012393 xmlResetLastError();
Daniel Veillard74c0e592003-11-25 07:01:38 +000012394 xmlCleanupThreads(); /* must be last if called not from the main thread */
William M. Brack72ee48d2003-12-30 08:30:19 +000012395 xmlCleanupMemory();
Daniel Veillardd0463562001-10-13 09:15:48 +000012396 xmlParserInitialized = 0;
Owen Taylor3473f882001-02-23 17:55:21 +000012397}
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012398
12399/************************************************************************
12400 * *
12401 * New set (2.6.0) of simpler and more flexible APIs *
12402 * *
12403 ************************************************************************/
12404
12405/**
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012406 * DICT_FREE:
12407 * @str: a string
12408 *
12409 * Free a string if it is not owned by the "dict" dictionnary in the
12410 * current scope
12411 */
12412#define DICT_FREE(str) \
12413 if ((str) && ((!dict) || \
12414 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
12415 xmlFree((char *)(str));
12416
12417/**
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012418 * xmlCtxtReset:
12419 * @ctxt: an XML parser context
12420 *
12421 * Reset a parser context
12422 */
12423void
12424xmlCtxtReset(xmlParserCtxtPtr ctxt)
12425{
12426 xmlParserInputPtr input;
Daniel Veillard36e5cd52004-11-02 14:52:23 +000012427 xmlDictPtr dict;
12428
12429 if (ctxt == NULL)
12430 return;
12431
12432 dict = ctxt->dict;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012433
12434 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
12435 xmlFreeInputStream(input);
12436 }
12437 ctxt->inputNr = 0;
12438 ctxt->input = NULL;
12439
12440 ctxt->spaceNr = 0;
12441 ctxt->spaceTab[0] = -1;
12442 ctxt->space = &ctxt->spaceTab[0];
12443
12444
12445 ctxt->nodeNr = 0;
12446 ctxt->node = NULL;
12447
12448 ctxt->nameNr = 0;
12449 ctxt->name = NULL;
12450
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012451 DICT_FREE(ctxt->version);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012452 ctxt->version = NULL;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012453 DICT_FREE(ctxt->encoding);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012454 ctxt->encoding = NULL;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012455 DICT_FREE(ctxt->directory);
12456 ctxt->directory = NULL;
12457 DICT_FREE(ctxt->extSubURI);
12458 ctxt->extSubURI = NULL;
12459 DICT_FREE(ctxt->extSubSystem);
12460 ctxt->extSubSystem = NULL;
12461 if (ctxt->myDoc != NULL)
12462 xmlFreeDoc(ctxt->myDoc);
12463 ctxt->myDoc = NULL;
12464
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012465 ctxt->standalone = -1;
12466 ctxt->hasExternalSubset = 0;
12467 ctxt->hasPErefs = 0;
12468 ctxt->html = 0;
12469 ctxt->external = 0;
12470 ctxt->instate = XML_PARSER_START;
12471 ctxt->token = 0;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012472
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012473 ctxt->wellFormed = 1;
12474 ctxt->nsWellFormed = 1;
Daniel Veillardae289182004-01-21 16:00:43 +000012475 ctxt->disableSAX = 0;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012476 ctxt->valid = 1;
Daniel Veillard766c4f92004-03-26 10:48:29 +000012477#if 0
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012478 ctxt->vctxt.userData = ctxt;
12479 ctxt->vctxt.error = xmlParserValidityError;
12480 ctxt->vctxt.warning = xmlParserValidityWarning;
Daniel Veillard766c4f92004-03-26 10:48:29 +000012481#endif
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012482 ctxt->record_info = 0;
12483 ctxt->nbChars = 0;
12484 ctxt->checkIndex = 0;
12485 ctxt->inSubset = 0;
12486 ctxt->errNo = XML_ERR_OK;
12487 ctxt->depth = 0;
12488 ctxt->charset = XML_CHAR_ENCODING_UTF8;
12489 ctxt->catalogs = NULL;
12490 xmlInitNodeInfoSeq(&ctxt->node_seq);
12491
12492 if (ctxt->attsDefault != NULL) {
12493 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
12494 ctxt->attsDefault = NULL;
12495 }
12496 if (ctxt->attsSpecial != NULL) {
12497 xmlHashFree(ctxt->attsSpecial, NULL);
12498 ctxt->attsSpecial = NULL;
12499 }
12500
Daniel Veillard4432df22003-09-28 18:58:27 +000012501#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012502 if (ctxt->catalogs != NULL)
12503 xmlCatalogFreeLocal(ctxt->catalogs);
Daniel Veillard4432df22003-09-28 18:58:27 +000012504#endif
Daniel Veillardcc199e02003-10-24 21:11:48 +000012505 if (ctxt->lastError.code != XML_ERR_OK)
12506 xmlResetError(&ctxt->lastError);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012507}
12508
12509/**
Daniel Veillard9ba8e382003-10-28 21:31:45 +000012510 * xmlCtxtResetPush:
12511 * @ctxt: an XML parser context
12512 * @chunk: a pointer to an array of chars
12513 * @size: number of chars in the array
12514 * @filename: an optional file name or URI
12515 * @encoding: the document encoding, or NULL
12516 *
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000012517 * Reset a push parser context
12518 *
12519 * Returns 0 in case of success and 1 in case of error
Daniel Veillard9ba8e382003-10-28 21:31:45 +000012520 */
12521int
12522xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk,
12523 int size, const char *filename, const char *encoding)
12524{
12525 xmlParserInputPtr inputStream;
12526 xmlParserInputBufferPtr buf;
12527 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
12528
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000012529 if (ctxt == NULL)
12530 return(1);
12531
Daniel Veillard9ba8e382003-10-28 21:31:45 +000012532 if ((encoding == NULL) && (chunk != NULL) && (size >= 4))
12533 enc = xmlDetectCharEncoding((const xmlChar *) chunk, size);
12534
12535 buf = xmlAllocParserInputBuffer(enc);
12536 if (buf == NULL)
12537 return(1);
12538
12539 if (ctxt == NULL) {
12540 xmlFreeParserInputBuffer(buf);
12541 return(1);
12542 }
12543
12544 xmlCtxtReset(ctxt);
12545
12546 if (ctxt->pushTab == NULL) {
12547 ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 *
12548 sizeof(xmlChar *));
12549 if (ctxt->pushTab == NULL) {
12550 xmlErrMemory(ctxt, NULL);
12551 xmlFreeParserInputBuffer(buf);
12552 return(1);
12553 }
12554 }
12555
12556 if (filename == NULL) {
12557 ctxt->directory = NULL;
12558 } else {
12559 ctxt->directory = xmlParserGetDirectory(filename);
12560 }
12561
12562 inputStream = xmlNewInputStream(ctxt);
12563 if (inputStream == NULL) {
12564 xmlFreeParserInputBuffer(buf);
12565 return(1);
12566 }
12567
12568 if (filename == NULL)
12569 inputStream->filename = NULL;
12570 else
12571 inputStream->filename = (char *)
12572 xmlCanonicPath((const xmlChar *) filename);
12573 inputStream->buf = buf;
12574 inputStream->base = inputStream->buf->buffer->content;
12575 inputStream->cur = inputStream->buf->buffer->content;
12576 inputStream->end =
12577 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
12578
12579 inputPush(ctxt, inputStream);
12580
12581 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
12582 (ctxt->input->buf != NULL)) {
12583 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
12584 int cur = ctxt->input->cur - ctxt->input->base;
12585
12586 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
12587
12588 ctxt->input->base = ctxt->input->buf->buffer->content + base;
12589 ctxt->input->cur = ctxt->input->base + cur;
12590 ctxt->input->end =
12591 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->
12592 use];
12593#ifdef DEBUG_PUSH
12594 xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
12595#endif
12596 }
12597
12598 if (encoding != NULL) {
12599 xmlCharEncodingHandlerPtr hdlr;
12600
12601 hdlr = xmlFindCharEncodingHandler(encoding);
12602 if (hdlr != NULL) {
12603 xmlSwitchToEncoding(ctxt, hdlr);
12604 } else {
12605 xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
12606 "Unsupported encoding %s\n", BAD_CAST encoding);
12607 }
12608 } else if (enc != XML_CHAR_ENCODING_NONE) {
12609 xmlSwitchEncoding(ctxt, enc);
12610 }
12611
12612 return(0);
12613}
12614
12615/**
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012616 * xmlCtxtUseOptions:
12617 * @ctxt: an XML parser context
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012618 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012619 *
12620 * Applies the options to the parser context
12621 *
12622 * Returns 0 in case of success, the set of unknown or unimplemented options
12623 * in case of error.
12624 */
12625int
12626xmlCtxtUseOptions(xmlParserCtxtPtr ctxt, int options)
12627{
Daniel Veillard36e5cd52004-11-02 14:52:23 +000012628 if (ctxt == NULL)
12629 return(-1);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012630 if (options & XML_PARSE_RECOVER) {
12631 ctxt->recovery = 1;
12632 options -= XML_PARSE_RECOVER;
12633 } else
12634 ctxt->recovery = 0;
12635 if (options & XML_PARSE_DTDLOAD) {
12636 ctxt->loadsubset = XML_DETECT_IDS;
12637 options -= XML_PARSE_DTDLOAD;
12638 } else
12639 ctxt->loadsubset = 0;
12640 if (options & XML_PARSE_DTDATTR) {
12641 ctxt->loadsubset |= XML_COMPLETE_ATTRS;
12642 options -= XML_PARSE_DTDATTR;
12643 }
12644 if (options & XML_PARSE_NOENT) {
12645 ctxt->replaceEntities = 1;
12646 /* ctxt->loadsubset |= XML_DETECT_IDS; */
12647 options -= XML_PARSE_NOENT;
12648 } else
12649 ctxt->replaceEntities = 0;
12650 if (options & XML_PARSE_NOWARNING) {
12651 ctxt->sax->warning = NULL;
12652 options -= XML_PARSE_NOWARNING;
12653 }
12654 if (options & XML_PARSE_NOERROR) {
12655 ctxt->sax->error = NULL;
12656 ctxt->sax->fatalError = NULL;
12657 options -= XML_PARSE_NOERROR;
12658 }
12659 if (options & XML_PARSE_PEDANTIC) {
12660 ctxt->pedantic = 1;
12661 options -= XML_PARSE_PEDANTIC;
12662 } else
12663 ctxt->pedantic = 0;
12664 if (options & XML_PARSE_NOBLANKS) {
12665 ctxt->keepBlanks = 0;
12666 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
12667 options -= XML_PARSE_NOBLANKS;
12668 } else
12669 ctxt->keepBlanks = 1;
12670 if (options & XML_PARSE_DTDVALID) {
12671 ctxt->validate = 1;
12672 if (options & XML_PARSE_NOWARNING)
12673 ctxt->vctxt.warning = NULL;
12674 if (options & XML_PARSE_NOERROR)
12675 ctxt->vctxt.error = NULL;
12676 options -= XML_PARSE_DTDVALID;
12677 } else
12678 ctxt->validate = 0;
Daniel Veillard81273902003-09-30 00:43:48 +000012679#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012680 if (options & XML_PARSE_SAX1) {
12681 ctxt->sax->startElement = xmlSAX2StartElement;
12682 ctxt->sax->endElement = xmlSAX2EndElement;
12683 ctxt->sax->startElementNs = NULL;
12684 ctxt->sax->endElementNs = NULL;
12685 ctxt->sax->initialized = 1;
12686 options -= XML_PARSE_SAX1;
12687 }
Daniel Veillard81273902003-09-30 00:43:48 +000012688#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012689 if (options & XML_PARSE_NODICT) {
12690 ctxt->dictNames = 0;
12691 options -= XML_PARSE_NODICT;
12692 } else {
12693 ctxt->dictNames = 1;
12694 }
Daniel Veillarddca8cc72003-09-26 13:53:14 +000012695 if (options & XML_PARSE_NOCDATA) {
12696 ctxt->sax->cdataBlock = NULL;
12697 options -= XML_PARSE_NOCDATA;
12698 }
12699 if (options & XML_PARSE_NSCLEAN) {
12700 ctxt->options |= XML_PARSE_NSCLEAN;
12701 options -= XML_PARSE_NSCLEAN;
12702 }
Daniel Veillard61b93382003-11-03 14:28:31 +000012703 if (options & XML_PARSE_NONET) {
12704 ctxt->options |= XML_PARSE_NONET;
12705 options -= XML_PARSE_NONET;
12706 }
Daniel Veillard7ec29972003-10-31 14:36:36 +000012707 ctxt->linenumbers = 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012708 return (options);
12709}
12710
12711/**
12712 * xmlDoRead:
12713 * @ctxt: an XML parser context
Daniel Veillard60942de2003-09-25 21:05:58 +000012714 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012715 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012716 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012717 * @reuse: keep the context for reuse
12718 *
12719 * Common front-end for the xmlRead functions
12720 *
12721 * Returns the resulting document tree or NULL
12722 */
12723static xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012724xmlDoRead(xmlParserCtxtPtr ctxt, const char *URL, const char *encoding,
12725 int options, int reuse)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012726{
12727 xmlDocPtr ret;
12728
12729 xmlCtxtUseOptions(ctxt, options);
12730 if (encoding != NULL) {
12731 xmlCharEncodingHandlerPtr hdlr;
12732
12733 hdlr = xmlFindCharEncodingHandler(encoding);
12734 if (hdlr != NULL)
12735 xmlSwitchToEncoding(ctxt, hdlr);
12736 }
Daniel Veillard60942de2003-09-25 21:05:58 +000012737 if ((URL != NULL) && (ctxt->input != NULL) &&
12738 (ctxt->input->filename == NULL))
12739 ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012740 xmlParseDocument(ctxt);
12741 if ((ctxt->wellFormed) || ctxt->recovery)
12742 ret = ctxt->myDoc;
12743 else {
12744 ret = NULL;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012745 if (ctxt->myDoc != NULL) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012746 xmlFreeDoc(ctxt->myDoc);
12747 }
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012748 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012749 ctxt->myDoc = NULL;
12750 if (!reuse) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012751 xmlFreeParserCtxt(ctxt);
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012752 }
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012753
12754 return (ret);
12755}
12756
12757/**
12758 * xmlReadDoc:
12759 * @cur: a pointer to a zero terminated string
Daniel Veillard60942de2003-09-25 21:05:58 +000012760 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012761 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012762 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012763 *
12764 * parse an XML in-memory document and build a tree.
12765 *
12766 * Returns the resulting document tree
12767 */
12768xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012769xmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012770{
12771 xmlParserCtxtPtr ctxt;
12772
12773 if (cur == NULL)
12774 return (NULL);
12775
12776 ctxt = xmlCreateDocParserCtxt(cur);
12777 if (ctxt == NULL)
12778 return (NULL);
Daniel Veillard60942de2003-09-25 21:05:58 +000012779 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012780}
12781
12782/**
12783 * xmlReadFile:
12784 * @filename: a file or URL
12785 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012786 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012787 *
12788 * parse an XML file from the filesystem or the network.
12789 *
12790 * Returns the resulting document tree
12791 */
12792xmlDocPtr
12793xmlReadFile(const char *filename, const char *encoding, int options)
12794{
12795 xmlParserCtxtPtr ctxt;
12796
Daniel Veillard61b93382003-11-03 14:28:31 +000012797 ctxt = xmlCreateURLParserCtxt(filename, options);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012798 if (ctxt == NULL)
12799 return (NULL);
Daniel Veillard60942de2003-09-25 21:05:58 +000012800 return (xmlDoRead(ctxt, NULL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012801}
12802
12803/**
12804 * xmlReadMemory:
12805 * @buffer: a pointer to a char array
12806 * @size: the size of the array
Daniel Veillard60942de2003-09-25 21:05:58 +000012807 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012808 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012809 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012810 *
12811 * parse an XML in-memory document and build a tree.
12812 *
12813 * Returns the resulting document tree
12814 */
12815xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012816xmlReadMemory(const char *buffer, int size, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012817{
12818 xmlParserCtxtPtr ctxt;
12819
12820 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
12821 if (ctxt == NULL)
12822 return (NULL);
Daniel Veillard60942de2003-09-25 21:05:58 +000012823 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012824}
12825
12826/**
12827 * xmlReadFd:
12828 * @fd: an open file descriptor
Daniel Veillard60942de2003-09-25 21:05:58 +000012829 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012830 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012831 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012832 *
12833 * parse an XML from a file descriptor and build a tree.
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012834 * NOTE that the file descriptor will not be closed when the
12835 * reader is closed or reset.
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012836 *
12837 * Returns the resulting document tree
12838 */
12839xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012840xmlReadFd(int fd, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012841{
12842 xmlParserCtxtPtr ctxt;
12843 xmlParserInputBufferPtr input;
12844 xmlParserInputPtr stream;
12845
12846 if (fd < 0)
12847 return (NULL);
12848
12849 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
12850 if (input == NULL)
12851 return (NULL);
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012852 input->closecallback = NULL;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012853 ctxt = xmlNewParserCtxt();
12854 if (ctxt == NULL) {
12855 xmlFreeParserInputBuffer(input);
12856 return (NULL);
12857 }
12858 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12859 if (stream == NULL) {
12860 xmlFreeParserInputBuffer(input);
12861 xmlFreeParserCtxt(ctxt);
12862 return (NULL);
12863 }
12864 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012865 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012866}
12867
12868/**
12869 * xmlReadIO:
12870 * @ioread: an I/O read function
12871 * @ioclose: an I/O close function
12872 * @ioctx: an I/O handler
Daniel Veillard60942de2003-09-25 21:05:58 +000012873 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012874 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012875 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012876 *
12877 * parse an XML document from I/O functions and source and build a tree.
12878 *
12879 * Returns the resulting document tree
12880 */
12881xmlDocPtr
12882xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
Daniel Veillard60942de2003-09-25 21:05:58 +000012883 void *ioctx, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012884{
12885 xmlParserCtxtPtr ctxt;
12886 xmlParserInputBufferPtr input;
12887 xmlParserInputPtr stream;
12888
12889 if (ioread == NULL)
12890 return (NULL);
12891
12892 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
12893 XML_CHAR_ENCODING_NONE);
12894 if (input == NULL)
12895 return (NULL);
12896 ctxt = xmlNewParserCtxt();
12897 if (ctxt == NULL) {
12898 xmlFreeParserInputBuffer(input);
12899 return (NULL);
12900 }
12901 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12902 if (stream == NULL) {
12903 xmlFreeParserInputBuffer(input);
12904 xmlFreeParserCtxt(ctxt);
12905 return (NULL);
12906 }
12907 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012908 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012909}
12910
12911/**
12912 * xmlCtxtReadDoc:
12913 * @ctxt: an XML parser context
12914 * @cur: a pointer to a zero terminated string
Daniel Veillard60942de2003-09-25 21:05:58 +000012915 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012916 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012917 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012918 *
12919 * parse an XML in-memory document and build a tree.
12920 * This reuses the existing @ctxt parser context
12921 *
12922 * Returns the resulting document tree
12923 */
12924xmlDocPtr
12925xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur,
Daniel Veillard60942de2003-09-25 21:05:58 +000012926 const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012927{
12928 xmlParserInputPtr stream;
12929
12930 if (cur == NULL)
12931 return (NULL);
12932 if (ctxt == NULL)
12933 return (NULL);
12934
12935 xmlCtxtReset(ctxt);
12936
12937 stream = xmlNewStringInputStream(ctxt, cur);
12938 if (stream == NULL) {
12939 return (NULL);
12940 }
12941 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012942 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012943}
12944
12945/**
12946 * xmlCtxtReadFile:
12947 * @ctxt: an XML parser context
12948 * @filename: a file or URL
12949 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012950 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012951 *
12952 * parse an XML file from the filesystem or the network.
12953 * This reuses the existing @ctxt parser context
12954 *
12955 * Returns the resulting document tree
12956 */
12957xmlDocPtr
12958xmlCtxtReadFile(xmlParserCtxtPtr ctxt, const char *filename,
12959 const char *encoding, int options)
12960{
12961 xmlParserInputPtr stream;
12962
12963 if (filename == NULL)
12964 return (NULL);
12965 if (ctxt == NULL)
12966 return (NULL);
12967
12968 xmlCtxtReset(ctxt);
12969
Daniel Veillard29614c72004-11-26 10:47:26 +000012970 stream = xmlLoadExternalEntity(filename, NULL, ctxt);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012971 if (stream == NULL) {
12972 return (NULL);
12973 }
12974 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012975 return (xmlDoRead(ctxt, NULL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012976}
12977
12978/**
12979 * xmlCtxtReadMemory:
12980 * @ctxt: an XML parser context
12981 * @buffer: a pointer to a char array
12982 * @size: the size of the array
Daniel Veillard60942de2003-09-25 21:05:58 +000012983 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012984 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012985 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012986 *
12987 * parse an XML in-memory document and build a tree.
12988 * This reuses the existing @ctxt parser context
12989 *
12990 * Returns the resulting document tree
12991 */
12992xmlDocPtr
12993xmlCtxtReadMemory(xmlParserCtxtPtr ctxt, const char *buffer, int size,
Daniel Veillard60942de2003-09-25 21:05:58 +000012994 const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012995{
12996 xmlParserInputBufferPtr input;
12997 xmlParserInputPtr stream;
12998
12999 if (ctxt == NULL)
13000 return (NULL);
13001 if (buffer == NULL)
13002 return (NULL);
13003
13004 xmlCtxtReset(ctxt);
13005
13006 input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
13007 if (input == NULL) {
13008 return(NULL);
13009 }
13010
13011 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
13012 if (stream == NULL) {
13013 xmlFreeParserInputBuffer(input);
13014 return(NULL);
13015 }
13016
13017 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000013018 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000013019}
13020
13021/**
13022 * xmlCtxtReadFd:
13023 * @ctxt: an XML parser context
13024 * @fd: an open file descriptor
Daniel Veillard60942de2003-09-25 21:05:58 +000013025 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000013026 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000013027 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000013028 *
13029 * parse an XML from a file descriptor and build a tree.
13030 * This reuses the existing @ctxt parser context
Daniel Veillard4bc5f432003-12-22 18:13:12 +000013031 * NOTE that the file descriptor will not be closed when the
13032 * reader is closed or reset.
Daniel Veillard16fa96c2003-09-23 21:50:54 +000013033 *
13034 * Returns the resulting document tree
13035 */
13036xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000013037xmlCtxtReadFd(xmlParserCtxtPtr ctxt, int fd,
13038 const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000013039{
13040 xmlParserInputBufferPtr input;
13041 xmlParserInputPtr stream;
13042
13043 if (fd < 0)
13044 return (NULL);
13045 if (ctxt == NULL)
13046 return (NULL);
13047
13048 xmlCtxtReset(ctxt);
13049
13050
13051 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
13052 if (input == NULL)
13053 return (NULL);
Daniel Veillard4bc5f432003-12-22 18:13:12 +000013054 input->closecallback = NULL;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000013055 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
13056 if (stream == NULL) {
13057 xmlFreeParserInputBuffer(input);
13058 return (NULL);
13059 }
13060 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000013061 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000013062}
13063
13064/**
13065 * xmlCtxtReadIO:
13066 * @ctxt: an XML parser context
13067 * @ioread: an I/O read function
13068 * @ioclose: an I/O close function
13069 * @ioctx: an I/O handler
Daniel Veillard60942de2003-09-25 21:05:58 +000013070 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000013071 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000013072 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000013073 *
13074 * parse an XML document from I/O functions and source and build a tree.
13075 * This reuses the existing @ctxt parser context
13076 *
13077 * Returns the resulting document tree
13078 */
13079xmlDocPtr
13080xmlCtxtReadIO(xmlParserCtxtPtr ctxt, xmlInputReadCallback ioread,
13081 xmlInputCloseCallback ioclose, void *ioctx,
Daniel Veillard60942de2003-09-25 21:05:58 +000013082 const char *URL,
Daniel Veillard16fa96c2003-09-23 21:50:54 +000013083 const char *encoding, int options)
13084{
13085 xmlParserInputBufferPtr input;
13086 xmlParserInputPtr stream;
13087
13088 if (ioread == NULL)
13089 return (NULL);
13090 if (ctxt == NULL)
13091 return (NULL);
13092
13093 xmlCtxtReset(ctxt);
13094
13095 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
13096 XML_CHAR_ENCODING_NONE);
13097 if (input == NULL)
13098 return (NULL);
13099 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
13100 if (stream == NULL) {
13101 xmlFreeParserInputBuffer(input);
13102 return (NULL);
13103 }
13104 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000013105 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000013106}
Daniel Veillard5d4644e2005-04-01 13:11:58 +000013107
13108#define bottom_parser
13109#include "elfgcchack.h"