blob: 053fdc3766a135ec7a6a5806b879f830528b7d77 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * parser.c : an XML 1.0 parser, namespaces and validity support are mostly
3 * implemented on top of the SAX interfaces
4 *
5 * References:
6 * The XML specification:
7 * http://www.w3.org/TR/REC-xml
8 * Original 1.0 version:
9 * http://www.w3.org/TR/1998/REC-xml-19980210
10 * XML second edition working draft
11 * http://www.w3.org/TR/2000/WD-xml-2e-20000814
12 *
13 * Okay this is a big file, the parser core is around 7000 lines, then it
14 * is followed by the progressive parser top routines, then the various
Daniel Veillardcbaf3992001-12-31 16:16:02 +000015 * high level APIs to call the parser and a few miscellaneous functions.
Owen Taylor3473f882001-02-23 17:55:21 +000016 * A number of helper functions and deprecated ones have been moved to
17 * parserInternals.c to reduce this file size.
18 * As much as possible the functions are associated with their relative
19 * production in the XML specification. A few productions defining the
20 * different ranges of character are actually implanted either in
21 * parserInternals.h or parserInternals.c
22 * The DOM tree build is realized from the default SAX callbacks in
23 * the module SAX.c.
24 * The routines doing the validation checks are in valid.c and called either
Daniel Veillardcbaf3992001-12-31 16:16:02 +000025 * from the SAX callbacks or as standalone functions using a preparsed
Owen Taylor3473f882001-02-23 17:55:21 +000026 * document.
27 *
28 * See Copyright for the status of this software.
29 *
Daniel Veillardc5d64342001-06-24 12:13:24 +000030 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000031 */
32
Daniel Veillard34ce8be2002-03-18 19:37:11 +000033#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000034#include "libxml.h"
35
Daniel Veillard3c5ed912002-01-08 10:36:16 +000036#if defined(WIN32) && !defined (__CYGWIN__)
Owen Taylor3473f882001-02-23 17:55:21 +000037#define XML_DIR_SEP '\\'
38#else
Owen Taylor3473f882001-02-23 17:55:21 +000039#define XML_DIR_SEP '/'
40#endif
41
Owen Taylor3473f882001-02-23 17:55:21 +000042#include <stdlib.h>
43#include <string.h>
Aleksey Sanine7acf432003-10-02 20:05:27 +000044#include <stdarg.h>
Owen Taylor3473f882001-02-23 17:55:21 +000045#include <libxml/xmlmemory.h>
Daniel Veillardd0463562001-10-13 09:15:48 +000046#include <libxml/threads.h>
47#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000048#include <libxml/tree.h>
49#include <libxml/parser.h>
50#include <libxml/parserInternals.h>
51#include <libxml/valid.h>
52#include <libxml/entities.h>
53#include <libxml/xmlerror.h>
54#include <libxml/encoding.h>
55#include <libxml/xmlIO.h>
56#include <libxml/uri.h>
Daniel Veillard5d90b6c2001-08-22 14:29:45 +000057#ifdef LIBXML_CATALOG_ENABLED
58#include <libxml/catalog.h>
59#endif
Owen Taylor3473f882001-02-23 17:55:21 +000060
61#ifdef HAVE_CTYPE_H
62#include <ctype.h>
63#endif
64#ifdef HAVE_STDLIB_H
65#include <stdlib.h>
66#endif
67#ifdef HAVE_SYS_STAT_H
68#include <sys/stat.h>
69#endif
70#ifdef HAVE_FCNTL_H
71#include <fcntl.h>
72#endif
73#ifdef HAVE_UNISTD_H
74#include <unistd.h>
75#endif
76#ifdef HAVE_ZLIB_H
77#include <zlib.h>
78#endif
79
Daniel Veillard3b2e4e12003-02-03 08:52:58 +000080/**
Daniel Veillard4aede2e2003-10-17 12:43:59 +000081 * xmlParserMaxDepth:
Daniel Veillard3b2e4e12003-02-03 08:52:58 +000082 *
83 * arbitrary depth limit for the XML documents that we allow to
84 * process. This is not a limitation of the parser but a safety
85 * boundary feature.
86 */
Daniel Veillard4aede2e2003-10-17 12:43:59 +000087unsigned int xmlParserMaxDepth = 1024;
Owen Taylor3473f882001-02-23 17:55:21 +000088
Daniel Veillard0fb18932003-09-07 09:14:37 +000089#define SAX2 1
90
Daniel Veillard21a0f912001-02-25 19:54:14 +000091#define XML_PARSER_BIG_BUFFER_SIZE 300
Owen Taylor3473f882001-02-23 17:55:21 +000092#define XML_PARSER_BUFFER_SIZE 100
93
Daniel Veillard5997aca2002-03-18 18:36:20 +000094#define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document"
95
Owen Taylor3473f882001-02-23 17:55:21 +000096/*
Owen Taylor3473f882001-02-23 17:55:21 +000097 * List of XML prefixed PI allowed by W3C specs
98 */
99
Daniel Veillardb44025c2001-10-11 22:55:55 +0000100static const char *xmlW3CPIs[] = {
Owen Taylor3473f882001-02-23 17:55:21 +0000101 "xml-stylesheet",
102 NULL
103};
104
Daniel Veillarda07050d2003-10-19 14:46:32 +0000105
Owen Taylor3473f882001-02-23 17:55:21 +0000106/* DEPR void xmlParserHandleReference(xmlParserCtxtPtr ctxt); */
Owen Taylor3473f882001-02-23 17:55:21 +0000107xmlEntityPtr xmlParseStringPEReference(xmlParserCtxtPtr ctxt,
108 const xmlChar **str);
109
Daniel Veillard7d515752003-09-26 19:12:37 +0000110static xmlParserErrors
Daniel Veillarda97a19b2001-05-20 13:19:52 +0000111xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt,
112 xmlSAXHandlerPtr sax,
Daniel Veillard257d9102001-05-08 10:41:44 +0000113 void *user_data, int depth, const xmlChar *URL,
Daniel Veillarda97a19b2001-05-20 13:19:52 +0000114 const xmlChar *ID, xmlNodePtr *list);
Owen Taylor3473f882001-02-23 17:55:21 +0000115
Daniel Veillard81273902003-09-30 00:43:48 +0000116#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +0000117static void
118xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode,
119 xmlNodePtr lastNode);
Daniel Veillard81273902003-09-30 00:43:48 +0000120#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillard8107a222002-01-13 14:10:10 +0000121
Daniel Veillard7d515752003-09-26 19:12:37 +0000122static xmlParserErrors
Daniel Veillard328f48c2002-11-15 15:24:34 +0000123xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt,
124 const xmlChar *string, void *user_data, xmlNodePtr *lst);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000125
126/************************************************************************
127 * *
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000128 * Some factorized error routines *
129 * *
130 ************************************************************************/
131
132/**
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000133 * xmlErrAttributeDup:
134 * @ctxt: an XML parser context
135 * @prefix: the attribute prefix
136 * @localname: the attribute localname
137 *
138 * Handle a redefinition of attribute error
139 */
140static void
141xmlErrAttributeDup(xmlParserCtxtPtr ctxt, const xmlChar * prefix,
142 const xmlChar * localname)
143{
Daniel Veillard157fee02003-10-31 10:36:03 +0000144 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
145 (ctxt->instate == XML_PARSER_EOF))
146 return;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000147 ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000148 if (prefix == NULL)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000149 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000150 ctxt->errNo, XML_ERR_FATAL, NULL, 0,
151 (const char *) localname, NULL, NULL, 0, 0,
152 "Attribute %s redefined\n", localname);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000153 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000154 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000155 ctxt->errNo, XML_ERR_FATAL, NULL, 0,
156 (const char *) prefix, (const char *) localname,
157 NULL, 0, 0, "Attribute %s:%s redefined\n", prefix,
158 localname);
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000159 ctxt->wellFormed = 0;
160 if (ctxt->recovery == 0)
161 ctxt->disableSAX = 1;
162}
163
164/**
165 * xmlFatalErr:
166 * @ctxt: an XML parser context
167 * @error: the error number
168 * @extra: extra information string
169 *
170 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
171 */
172static void
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000173xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info)
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000174{
175 const char *errmsg;
176
Daniel Veillard157fee02003-10-31 10:36:03 +0000177 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
178 (ctxt->instate == XML_PARSER_EOF))
179 return;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000180 switch (error) {
181 case XML_ERR_INVALID_HEX_CHARREF:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000182 errmsg = "CharRef: invalid hexadecimal value\n";
183 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000184 case XML_ERR_INVALID_DEC_CHARREF:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000185 errmsg = "CharRef: invalid decimal value\n";
186 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000187 case XML_ERR_INVALID_CHARREF:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000188 errmsg = "CharRef: invalid value\n";
189 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000190 case XML_ERR_INTERNAL_ERROR:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000191 errmsg = "internal error";
192 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000193 case XML_ERR_PEREF_AT_EOF:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000194 errmsg = "PEReference at end of document\n";
195 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000196 case XML_ERR_PEREF_IN_PROLOG:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000197 errmsg = "PEReference in prolog\n";
198 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000199 case XML_ERR_PEREF_IN_EPILOG:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000200 errmsg = "PEReference in epilog\n";
201 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000202 case XML_ERR_PEREF_NO_NAME:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000203 errmsg = "PEReference: no name\n";
204 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000205 case XML_ERR_PEREF_SEMICOL_MISSING:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000206 errmsg = "PEReference: expecting ';'\n";
207 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000208 case XML_ERR_ENTITY_LOOP:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000209 errmsg = "Detected an entity reference loop\n";
210 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000211 case XML_ERR_ENTITY_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000212 errmsg = "EntityValue: \" or ' expected\n";
213 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000214 case XML_ERR_ENTITY_PE_INTERNAL:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000215 errmsg = "PEReferences forbidden in internal subset\n";
216 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000217 case XML_ERR_ENTITY_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000218 errmsg = "EntityValue: \" or ' expected\n";
219 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000220 case XML_ERR_ATTRIBUTE_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000221 errmsg = "AttValue: \" or ' expected\n";
222 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000223 case XML_ERR_LT_IN_ATTRIBUTE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000224 errmsg = "Unescaped '<' not allowed in attributes values\n";
225 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000226 case XML_ERR_LITERAL_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000227 errmsg = "SystemLiteral \" or ' expected\n";
228 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000229 case XML_ERR_LITERAL_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000230 errmsg = "Unfinished System or Public ID \" or ' expected\n";
231 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000232 case XML_ERR_MISPLACED_CDATA_END:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000233 errmsg = "Sequence ']]>' not allowed in content\n";
234 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000235 case XML_ERR_URI_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000236 errmsg = "SYSTEM or PUBLIC, the URI is missing\n";
237 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000238 case XML_ERR_PUBID_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000239 errmsg = "PUBLIC, the Public Identifier is missing\n";
240 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000241 case XML_ERR_HYPHEN_IN_COMMENT:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000242 errmsg = "Comment must not contain '--' (double-hyphen)\n";
243 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000244 case XML_ERR_PI_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000245 errmsg = "xmlParsePI : no target name\n";
246 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000247 case XML_ERR_RESERVED_XML_NAME:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000248 errmsg = "Invalid PI name\n";
249 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000250 case XML_ERR_NOTATION_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000251 errmsg = "NOTATION: Name expected here\n";
252 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000253 case XML_ERR_NOTATION_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000254 errmsg = "'>' required to close NOTATION declaration\n";
255 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000256 case XML_ERR_VALUE_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000257 errmsg = "Entity value required\n";
258 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000259 case XML_ERR_URI_FRAGMENT:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000260 errmsg = "Fragment not allowed";
261 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000262 case XML_ERR_ATTLIST_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000263 errmsg = "'(' required to start ATTLIST enumeration\n";
264 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000265 case XML_ERR_NMTOKEN_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000266 errmsg = "NmToken expected in ATTLIST enumeration\n";
267 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000268 case XML_ERR_ATTLIST_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000269 errmsg = "')' required to finish ATTLIST enumeration\n";
270 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000271 case XML_ERR_MIXED_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000272 errmsg = "MixedContentDecl : '|' or ')*' expected\n";
273 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000274 case XML_ERR_PCDATA_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000275 errmsg = "MixedContentDecl : '#PCDATA' expected\n";
276 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000277 case XML_ERR_ELEMCONTENT_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000278 errmsg = "ContentDecl : Name or '(' expected\n";
279 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000280 case XML_ERR_ELEMCONTENT_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000281 errmsg = "ContentDecl : ',' '|' or ')' expected\n";
282 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000283 case XML_ERR_PEREF_IN_INT_SUBSET:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000284 errmsg =
285 "PEReference: forbidden within markup decl in internal subset\n";
286 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000287 case XML_ERR_GT_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000288 errmsg = "expected '>'\n";
289 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000290 case XML_ERR_CONDSEC_INVALID:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000291 errmsg = "XML conditional section '[' expected\n";
292 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000293 case XML_ERR_EXT_SUBSET_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000294 errmsg = "Content error in the external subset\n";
295 break;
296 case XML_ERR_CONDSEC_INVALID_KEYWORD:
297 errmsg =
298 "conditional section INCLUDE or IGNORE keyword expected\n";
299 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000300 case XML_ERR_CONDSEC_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000301 errmsg = "XML conditional section not closed\n";
302 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000303 case XML_ERR_XMLDECL_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000304 errmsg = "Text declaration '<?xml' required\n";
305 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000306 case XML_ERR_XMLDECL_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000307 errmsg = "parsing XML declaration: '?>' expected\n";
308 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000309 case XML_ERR_EXT_ENTITY_STANDALONE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000310 errmsg = "external parsed entities cannot be standalone\n";
311 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000312 case XML_ERR_ENTITYREF_SEMICOL_MISSING:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000313 errmsg = "EntityRef: expecting ';'\n";
314 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000315 case XML_ERR_DOCTYPE_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000316 errmsg = "DOCTYPE improperly terminated\n";
317 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000318 case XML_ERR_LTSLASH_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000319 errmsg = "EndTag: '</' not found\n";
320 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000321 case XML_ERR_EQUAL_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000322 errmsg = "expected '='\n";
323 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000324 case XML_ERR_STRING_NOT_CLOSED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000325 errmsg = "String not closed expecting \" or '\n";
326 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000327 case XML_ERR_STRING_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000328 errmsg = "String not started expecting ' or \"\n";
329 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000330 case XML_ERR_ENCODING_NAME:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000331 errmsg = "Invalid XML encoding name\n";
332 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000333 case XML_ERR_STANDALONE_VALUE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000334 errmsg = "standalone accepts only 'yes' or 'no'\n";
335 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000336 case XML_ERR_DOCUMENT_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000337 errmsg = "Document is empty\n";
338 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000339 case XML_ERR_DOCUMENT_END:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000340 errmsg = "Extra content at the end of the document\n";
341 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000342 case XML_ERR_NOT_WELL_BALANCED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000343 errmsg = "chunk is not well balanced\n";
344 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000345 case XML_ERR_EXTRA_CONTENT:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000346 errmsg = "extra content at the end of well balanced chunk\n";
347 break;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000348 case XML_ERR_VERSION_MISSING:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000349 errmsg = "Malformed declaration expecting version\n";
350 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000351#if 0
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000352 case:
353 errmsg = "\n";
354 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000355#endif
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000356 default:
357 errmsg = "Unregistered error message\n";
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000358 }
359 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000360 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000361 XML_ERR_FATAL, NULL, 0, info, NULL, NULL, 0, 0, errmsg,
362 info);
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000363 ctxt->wellFormed = 0;
364 if (ctxt->recovery == 0)
365 ctxt->disableSAX = 1;
366}
367
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000368/**
369 * xmlFatalErrMsg:
370 * @ctxt: an XML parser context
371 * @error: the error number
372 * @msg: the error message
373 *
374 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
375 */
376static void
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000377xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
378 const char *msg)
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000379{
Daniel Veillard157fee02003-10-31 10:36:03 +0000380 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
381 (ctxt->instate == XML_PARSER_EOF))
382 return;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000383 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000384 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000385 XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, msg);
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000386 ctxt->wellFormed = 0;
387 if (ctxt->recovery == 0)
388 ctxt->disableSAX = 1;
389}
390
391/**
Daniel Veillard24eb9782003-10-04 21:08:09 +0000392 * xmlWarningMsg:
393 * @ctxt: an XML parser context
394 * @error: the error number
395 * @msg: the error message
396 * @str1: extra data
397 * @str2: extra data
398 *
399 * Handle a warning.
400 */
401static void
402xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
403 const char *msg, const xmlChar *str1, const xmlChar *str2)
404{
Daniel Veillard9bcc7c52003-10-11 10:57:05 +0000405 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardc790bf42003-10-11 10:50:10 +0000406
Daniel Veillard157fee02003-10-31 10:36:03 +0000407 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
408 (ctxt->instate == XML_PARSER_EOF))
409 return;
Daniel Veillard24eb9782003-10-04 21:08:09 +0000410 ctxt->errNo = error;
Daniel Veillardc790bf42003-10-11 10:50:10 +0000411 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
Daniel Veillard9bcc7c52003-10-11 10:57:05 +0000412 schannel = ctxt->sax->serror;
413 __xmlRaiseError(schannel,
Daniel Veillard659e71e2003-10-10 14:10:40 +0000414 (ctxt->sax) ? ctxt->sax->warning : NULL,
415 ctxt->userData,
Daniel Veillard24eb9782003-10-04 21:08:09 +0000416 ctxt, NULL, XML_FROM_PARSER, error,
417 XML_ERR_WARNING, NULL, 0,
418 (const char *) str1, (const char *) str2, NULL, 0, 0,
419 msg, (const char *) str1, (const char *) str2);
420}
421
422/**
423 * xmlValidityError:
424 * @ctxt: an XML parser context
425 * @error: the error number
426 * @msg: the error message
427 * @str1: extra data
428 *
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000429 * Handle a validity error.
Daniel Veillard24eb9782003-10-04 21:08:09 +0000430 */
431static void
432xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error,
433 const char *msg, const xmlChar *str1)
434{
Daniel Veillard9bcc7c52003-10-11 10:57:05 +0000435 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard157fee02003-10-31 10:36:03 +0000436
437 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
438 (ctxt->instate == XML_PARSER_EOF))
439 return;
Daniel Veillard24eb9782003-10-04 21:08:09 +0000440 ctxt->errNo = error;
Daniel Veillardc790bf42003-10-11 10:50:10 +0000441 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
Daniel Veillard9bcc7c52003-10-11 10:57:05 +0000442 schannel = ctxt->sax->serror;
Daniel Veillardc790bf42003-10-11 10:50:10 +0000443 __xmlRaiseError(schannel,
Daniel Veillard659e71e2003-10-10 14:10:40 +0000444 ctxt->vctxt.error, ctxt->vctxt.userData,
Daniel Veillard24eb9782003-10-04 21:08:09 +0000445 ctxt, NULL, XML_FROM_DTD, error,
446 XML_ERR_ERROR, NULL, 0, (const char *) str1,
447 NULL, NULL, 0, 0,
448 msg, (const char *) str1);
449 ctxt->valid = 0;
450}
451
452/**
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000453 * xmlFatalErrMsgInt:
454 * @ctxt: an XML parser context
455 * @error: the error number
456 * @msg: the error message
457 * @val: an integer value
458 *
459 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
460 */
461static void
462xmlFatalErrMsgInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000463 const char *msg, int val)
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000464{
Daniel Veillard157fee02003-10-31 10:36:03 +0000465 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
466 (ctxt->instate == XML_PARSER_EOF))
467 return;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000468 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000469 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000470 ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
471 NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000472 ctxt->wellFormed = 0;
473 if (ctxt->recovery == 0)
474 ctxt->disableSAX = 1;
475}
476
477/**
Daniel Veillardf403d292003-10-05 13:51:35 +0000478 * xmlFatalErrMsgStrIntStr:
479 * @ctxt: an XML parser context
480 * @error: the error number
481 * @msg: the error message
482 * @str1: an string info
483 * @val: an integer value
484 * @str2: an string info
485 *
486 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
487 */
488static void
489xmlFatalErrMsgStrIntStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
490 const char *msg, const xmlChar *str1, int val,
491 const xmlChar *str2)
492{
Daniel Veillard157fee02003-10-31 10:36:03 +0000493 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
494 (ctxt->instate == XML_PARSER_EOF))
495 return;
Daniel Veillardf403d292003-10-05 13:51:35 +0000496 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000497 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardf403d292003-10-05 13:51:35 +0000498 ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
499 NULL, 0, (const char *) str1, (const char *) str2,
500 NULL, val, 0, msg, str1, val, str2);
501 ctxt->wellFormed = 0;
502 if (ctxt->recovery == 0)
503 ctxt->disableSAX = 1;
504}
505
506/**
Daniel Veillardbc92eca2003-09-15 09:48:06 +0000507 * xmlFatalErrMsgStr:
508 * @ctxt: an XML parser context
509 * @error: the error number
510 * @msg: the error message
511 * @val: a string value
512 *
513 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
514 */
515static void
516xmlFatalErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000517 const char *msg, const xmlChar * val)
Daniel Veillardbc92eca2003-09-15 09:48:06 +0000518{
Daniel Veillard157fee02003-10-31 10:36:03 +0000519 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
520 (ctxt->instate == XML_PARSER_EOF))
521 return;
Daniel Veillardbc92eca2003-09-15 09:48:06 +0000522 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000523 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000524 XML_FROM_PARSER, error, XML_ERR_FATAL,
525 NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg,
526 val);
Daniel Veillardbc92eca2003-09-15 09:48:06 +0000527 ctxt->wellFormed = 0;
528 if (ctxt->recovery == 0)
529 ctxt->disableSAX = 1;
530}
531
532/**
Daniel Veillardf403d292003-10-05 13:51:35 +0000533 * xmlErrMsgStr:
534 * @ctxt: an XML parser context
535 * @error: the error number
536 * @msg: the error message
537 * @val: a string value
538 *
539 * Handle a non fatal parser error
540 */
541static void
542xmlErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
543 const char *msg, const xmlChar * val)
544{
Daniel Veillard157fee02003-10-31 10:36:03 +0000545 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
546 (ctxt->instate == XML_PARSER_EOF))
547 return;
Daniel Veillardf403d292003-10-05 13:51:35 +0000548 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000549 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL,
Daniel Veillardf403d292003-10-05 13:51:35 +0000550 XML_FROM_PARSER, error, XML_ERR_ERROR,
551 NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg,
552 val);
553}
554
555/**
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000556 * xmlNsErr:
557 * @ctxt: an XML parser context
558 * @error: the error number
559 * @msg: the message
560 * @info1: extra information string
561 * @info2: extra information string
562 *
563 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
564 */
565static void
566xmlNsErr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
567 const char *msg,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000568 const xmlChar * info1, const xmlChar * info2,
569 const xmlChar * info3)
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000570{
Daniel Veillard157fee02003-10-31 10:36:03 +0000571 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
572 (ctxt->instate == XML_PARSER_EOF))
573 return;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000574 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000575 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000576 XML_ERR_ERROR, NULL, 0, (const char *) info1,
577 (const char *) info2, (const char *) info3, 0, 0, msg,
578 info1, info2, info3);
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000579 ctxt->nsWellFormed = 0;
580}
581
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000582/************************************************************************
583 * *
Daniel Veillarde57ec792003-09-10 10:50:59 +0000584 * SAX2 defaulted attributes handling *
585 * *
586 ************************************************************************/
587
588/**
589 * xmlDetectSAX2:
590 * @ctxt: an XML parser context
591 *
592 * Do the SAX2 detection and specific intialization
593 */
594static void
595xmlDetectSAX2(xmlParserCtxtPtr ctxt) {
596 if (ctxt == NULL) return;
Daniel Veillard81273902003-09-30 00:43:48 +0000597#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +0000598 if ((ctxt->sax) && (ctxt->sax->initialized == XML_SAX2_MAGIC) &&
599 ((ctxt->sax->startElementNs != NULL) ||
600 (ctxt->sax->endElementNs != NULL))) ctxt->sax2 = 1;
Daniel Veillard81273902003-09-30 00:43:48 +0000601#else
602 ctxt->sax2 = 1;
603#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +0000604
605 ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
606 ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
607 ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
William M. Brack9f797ab2004-07-28 07:40:12 +0000608 if ((ctxt->str_xml==NULL) || (ctxt->str_xmlns==NULL) ||
609 (ctxt->str_xml_ns == NULL)) {
610 xmlErrMemory(ctxt, NULL);
611 }
Daniel Veillarde57ec792003-09-10 10:50:59 +0000612}
613
Daniel Veillarde57ec792003-09-10 10:50:59 +0000614typedef struct _xmlDefAttrs xmlDefAttrs;
615typedef xmlDefAttrs *xmlDefAttrsPtr;
616struct _xmlDefAttrs {
617 int nbAttrs; /* number of defaulted attributes on that element */
618 int maxAttrs; /* the size of the array */
619 const xmlChar *values[4]; /* array of localname/prefix/values */
620};
Daniel Veillarde57ec792003-09-10 10:50:59 +0000621
622/**
623 * xmlAddDefAttrs:
624 * @ctxt: an XML parser context
625 * @fullname: the element fullname
626 * @fullattr: the attribute fullname
627 * @value: the attribute value
628 *
629 * Add a defaulted attribute for an element
630 */
631static void
632xmlAddDefAttrs(xmlParserCtxtPtr ctxt,
633 const xmlChar *fullname,
634 const xmlChar *fullattr,
635 const xmlChar *value) {
636 xmlDefAttrsPtr defaults;
637 int len;
638 const xmlChar *name;
639 const xmlChar *prefix;
640
641 if (ctxt->attsDefault == NULL) {
642 ctxt->attsDefault = xmlHashCreate(10);
643 if (ctxt->attsDefault == NULL)
644 goto mem_error;
645 }
646
647 /*
Daniel Veillard079f6a72004-09-23 13:15:03 +0000648 * split the element name into prefix:localname , the string found
649 * are within the DTD and then not associated to namespace names.
Daniel Veillarde57ec792003-09-10 10:50:59 +0000650 */
651 name = xmlSplitQName3(fullname, &len);
652 if (name == NULL) {
653 name = xmlDictLookup(ctxt->dict, fullname, -1);
654 prefix = NULL;
655 } else {
656 name = xmlDictLookup(ctxt->dict, name, -1);
657 prefix = xmlDictLookup(ctxt->dict, fullname, len);
658 }
659
660 /*
661 * make sure there is some storage
662 */
663 defaults = xmlHashLookup2(ctxt->attsDefault, name, prefix);
664 if (defaults == NULL) {
665 defaults = (xmlDefAttrsPtr) xmlMalloc(sizeof(xmlDefAttrs) +
Daniel Veillard079f6a72004-09-23 13:15:03 +0000666 (4 * 4) * sizeof(const xmlChar *));
Daniel Veillarde57ec792003-09-10 10:50:59 +0000667 if (defaults == NULL)
668 goto mem_error;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000669 defaults->nbAttrs = 0;
Daniel Veillard079f6a72004-09-23 13:15:03 +0000670 defaults->maxAttrs = 4;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000671 xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, defaults, NULL);
672 } else if (defaults->nbAttrs >= defaults->maxAttrs) {
Daniel Veillard079f6a72004-09-23 13:15:03 +0000673 xmlDefAttrsPtr temp;
674
675 temp = (xmlDefAttrsPtr) xmlRealloc(defaults, sizeof(xmlDefAttrs) +
Daniel Veillarde57ec792003-09-10 10:50:59 +0000676 (2 * defaults->maxAttrs * 4) * sizeof(const xmlChar *));
Daniel Veillard079f6a72004-09-23 13:15:03 +0000677 if (temp == NULL)
Daniel Veillarde57ec792003-09-10 10:50:59 +0000678 goto mem_error;
Daniel Veillard079f6a72004-09-23 13:15:03 +0000679 defaults = temp;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000680 defaults->maxAttrs *= 2;
681 xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, defaults, NULL);
682 }
683
684 /*
685 * plit the element name into prefix:localname , the string found
686 * are within the DTD and hen not associated to namespace names.
687 */
688 name = xmlSplitQName3(fullattr, &len);
689 if (name == NULL) {
690 name = xmlDictLookup(ctxt->dict, fullattr, -1);
691 prefix = NULL;
692 } else {
693 name = xmlDictLookup(ctxt->dict, name, -1);
694 prefix = xmlDictLookup(ctxt->dict, fullattr, len);
695 }
696
697 defaults->values[4 * defaults->nbAttrs] = name;
698 defaults->values[4 * defaults->nbAttrs + 1] = prefix;
699 /* intern the string and precompute the end */
700 len = xmlStrlen(value);
701 value = xmlDictLookup(ctxt->dict, value, len);
702 defaults->values[4 * defaults->nbAttrs + 2] = value;
703 defaults->values[4 * defaults->nbAttrs + 3] = value + len;
704 defaults->nbAttrs++;
705
706 return;
707
708mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000709 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000710 return;
711}
712
Daniel Veillard8e36e6a2003-09-10 10:50:59 +0000713/**
714 * xmlAddSpecialAttr:
715 * @ctxt: an XML parser context
716 * @fullname: the element fullname
717 * @fullattr: the attribute fullname
718 * @type: the attribute type
719 *
720 * Register that this attribute is not CDATA
721 */
722static void
723xmlAddSpecialAttr(xmlParserCtxtPtr ctxt,
724 const xmlChar *fullname,
725 const xmlChar *fullattr,
726 int type)
727{
728 if (ctxt->attsSpecial == NULL) {
729 ctxt->attsSpecial = xmlHashCreate(10);
730 if (ctxt->attsSpecial == NULL)
731 goto mem_error;
732 }
733
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000734 xmlHashAddEntry2(ctxt->attsSpecial, fullname, fullattr,
735 (void *) (long) type);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +0000736 return;
737
738mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000739 xmlErrMemory(ctxt, NULL);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +0000740 return;
741}
742
Daniel Veillard4432df22003-09-28 18:58:27 +0000743/**
744 * xmlCheckLanguageID:
745 * @lang: pointer to the string value
746 *
747 * Checks that the value conforms to the LanguageID production:
748 *
749 * NOTE: this is somewhat deprecated, those productions were removed from
750 * the XML Second edition.
751 *
752 * [33] LanguageID ::= Langcode ('-' Subcode)*
753 * [34] Langcode ::= ISO639Code | IanaCode | UserCode
754 * [35] ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z])
755 * [36] IanaCode ::= ('i' | 'I') '-' ([a-z] | [A-Z])+
756 * [37] UserCode ::= ('x' | 'X') '-' ([a-z] | [A-Z])+
757 * [38] Subcode ::= ([a-z] | [A-Z])+
758 *
759 * Returns 1 if correct 0 otherwise
760 **/
761int
762xmlCheckLanguageID(const xmlChar * lang)
763{
764 const xmlChar *cur = lang;
765
766 if (cur == NULL)
767 return (0);
768 if (((cur[0] == 'i') && (cur[1] == '-')) ||
769 ((cur[0] == 'I') && (cur[1] == '-'))) {
770 /*
771 * IANA code
772 */
773 cur += 2;
774 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
775 ((cur[0] >= 'a') && (cur[0] <= 'z')))
776 cur++;
777 } else if (((cur[0] == 'x') && (cur[1] == '-')) ||
778 ((cur[0] == 'X') && (cur[1] == '-'))) {
779 /*
780 * User code
781 */
782 cur += 2;
783 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
784 ((cur[0] >= 'a') && (cur[0] <= 'z')))
785 cur++;
786 } else if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
787 ((cur[0] >= 'a') && (cur[0] <= 'z'))) {
788 /*
789 * ISO639
790 */
791 cur++;
792 if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
793 ((cur[0] >= 'a') && (cur[0] <= 'z')))
794 cur++;
795 else
796 return (0);
797 } else
798 return (0);
799 while (cur[0] != 0) { /* non input consuming */
800 if (cur[0] != '-')
801 return (0);
802 cur++;
803 if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
804 ((cur[0] >= 'a') && (cur[0] <= 'z')))
805 cur++;
806 else
807 return (0);
808 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
809 ((cur[0] >= 'a') && (cur[0] <= 'z')))
810 cur++;
811 }
812 return (1);
813}
814
Owen Taylor3473f882001-02-23 17:55:21 +0000815/************************************************************************
816 * *
817 * Parser stacks related functions and macros *
818 * *
819 ************************************************************************/
820
821xmlEntityPtr xmlParseStringEntityRef(xmlParserCtxtPtr ctxt,
822 const xmlChar ** str);
823
Daniel Veillard0fb18932003-09-07 09:14:37 +0000824#ifdef SAX2
825/**
826 * nsPush:
827 * @ctxt: an XML parser context
828 * @prefix: the namespace prefix or NULL
829 * @URL: the namespace name
830 *
831 * Pushes a new parser namespace on top of the ns stack
832 *
William M. Brack7b9154b2003-09-27 19:23:50 +0000833 * Returns -1 in case of error, -2 if the namespace should be discarded
834 * and the index in the stack otherwise.
Daniel Veillard0fb18932003-09-07 09:14:37 +0000835 */
836static int
837nsPush(xmlParserCtxtPtr ctxt, const xmlChar *prefix, const xmlChar *URL)
838{
Daniel Veillarddca8cc72003-09-26 13:53:14 +0000839 if (ctxt->options & XML_PARSE_NSCLEAN) {
840 int i;
841 for (i = 0;i < ctxt->nsNr;i += 2) {
842 if (ctxt->nsTab[i] == prefix) {
843 /* in scope */
844 if (ctxt->nsTab[i + 1] == URL)
845 return(-2);
846 /* out of scope keep it */
847 break;
848 }
849 }
850 }
Daniel Veillard0fb18932003-09-07 09:14:37 +0000851 if ((ctxt->nsMax == 0) || (ctxt->nsTab == NULL)) {
852 ctxt->nsMax = 10;
853 ctxt->nsNr = 0;
854 ctxt->nsTab = (const xmlChar **)
855 xmlMalloc(ctxt->nsMax * sizeof(xmlChar *));
856 if (ctxt->nsTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000857 xmlErrMemory(ctxt, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +0000858 ctxt->nsMax = 0;
859 return (-1);
860 }
861 } else if (ctxt->nsNr >= ctxt->nsMax) {
862 ctxt->nsMax *= 2;
863 ctxt->nsTab = (const xmlChar **)
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000864 xmlRealloc((char *) ctxt->nsTab,
Daniel Veillard0fb18932003-09-07 09:14:37 +0000865 ctxt->nsMax * sizeof(ctxt->nsTab[0]));
866 if (ctxt->nsTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000867 xmlErrMemory(ctxt, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +0000868 ctxt->nsMax /= 2;
869 return (-1);
870 }
871 }
872 ctxt->nsTab[ctxt->nsNr++] = prefix;
873 ctxt->nsTab[ctxt->nsNr++] = URL;
874 return (ctxt->nsNr);
875}
876/**
877 * nsPop:
878 * @ctxt: an XML parser context
879 * @nr: the number to pop
880 *
881 * Pops the top @nr parser prefix/namespace from the ns stack
882 *
883 * Returns the number of namespaces removed
884 */
885static int
886nsPop(xmlParserCtxtPtr ctxt, int nr)
887{
888 int i;
889
890 if (ctxt->nsTab == NULL) return(0);
891 if (ctxt->nsNr < nr) {
892 xmlGenericError(xmlGenericErrorContext, "Pbm popping %d NS\n", nr);
893 nr = ctxt->nsNr;
894 }
895 if (ctxt->nsNr <= 0)
896 return (0);
897
898 for (i = 0;i < nr;i++) {
899 ctxt->nsNr--;
900 ctxt->nsTab[ctxt->nsNr] = NULL;
901 }
902 return(nr);
903}
904#endif
905
906static int
907xmlCtxtGrowAttrs(xmlParserCtxtPtr ctxt, int nr) {
908 const xmlChar **atts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000909 int *attallocs;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000910 int maxatts;
911
912 if (ctxt->atts == NULL) {
Daniel Veillarde57ec792003-09-10 10:50:59 +0000913 maxatts = 55; /* allow for 10 attrs by default */
Daniel Veillard0fb18932003-09-07 09:14:37 +0000914 atts = (const xmlChar **)
915 xmlMalloc(maxatts * sizeof(xmlChar *));
Daniel Veillarde57ec792003-09-10 10:50:59 +0000916 if (atts == NULL) goto mem_error;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000917 ctxt->atts = atts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000918 attallocs = (int *) xmlMalloc((maxatts / 5) * sizeof(int));
919 if (attallocs == NULL) goto mem_error;
920 ctxt->attallocs = attallocs;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000921 ctxt->maxatts = maxatts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000922 } else if (nr + 5 > ctxt->maxatts) {
923 maxatts = (nr + 5) * 2;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000924 atts = (const xmlChar **) xmlRealloc((void *) ctxt->atts,
925 maxatts * sizeof(const xmlChar *));
Daniel Veillarde57ec792003-09-10 10:50:59 +0000926 if (atts == NULL) goto mem_error;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000927 ctxt->atts = atts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000928 attallocs = (int *) xmlRealloc((void *) ctxt->attallocs,
929 (maxatts / 5) * sizeof(int));
930 if (attallocs == NULL) goto mem_error;
931 ctxt->attallocs = attallocs;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000932 ctxt->maxatts = maxatts;
933 }
934 return(ctxt->maxatts);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000935mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000936 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000937 return(-1);
Daniel Veillard0fb18932003-09-07 09:14:37 +0000938}
939
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000940/**
941 * inputPush:
942 * @ctxt: an XML parser context
Daniel Veillard9d06d302002-01-22 18:15:52 +0000943 * @value: the parser input
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000944 *
945 * Pushes a new parser input on top of the input stack
Daniel Veillard9d06d302002-01-22 18:15:52 +0000946 *
947 * Returns 0 in case of error, the index in the stack otherwise
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000948 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000949int
Daniel Veillard1c732d22002-11-30 11:22:59 +0000950inputPush(xmlParserCtxtPtr ctxt, xmlParserInputPtr value)
951{
952 if (ctxt->inputNr >= ctxt->inputMax) {
953 ctxt->inputMax *= 2;
954 ctxt->inputTab =
955 (xmlParserInputPtr *) xmlRealloc(ctxt->inputTab,
956 ctxt->inputMax *
957 sizeof(ctxt->inputTab[0]));
958 if (ctxt->inputTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000959 xmlErrMemory(ctxt, NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +0000960 return (0);
961 }
962 }
963 ctxt->inputTab[ctxt->inputNr] = value;
964 ctxt->input = value;
965 return (ctxt->inputNr++);
966}
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000967/**
Daniel Veillard1c732d22002-11-30 11:22:59 +0000968 * inputPop:
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000969 * @ctxt: an XML parser context
970 *
Daniel Veillard1c732d22002-11-30 11:22:59 +0000971 * Pops the top parser input from the input stack
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000972 *
Daniel Veillard1c732d22002-11-30 11:22:59 +0000973 * Returns the input just removed
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000974 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +0000975xmlParserInputPtr
Daniel Veillard1c732d22002-11-30 11:22:59 +0000976inputPop(xmlParserCtxtPtr ctxt)
977{
978 xmlParserInputPtr ret;
979
980 if (ctxt->inputNr <= 0)
981 return (0);
982 ctxt->inputNr--;
983 if (ctxt->inputNr > 0)
984 ctxt->input = ctxt->inputTab[ctxt->inputNr - 1];
985 else
986 ctxt->input = NULL;
987 ret = ctxt->inputTab[ctxt->inputNr];
988 ctxt->inputTab[ctxt->inputNr] = 0;
989 return (ret);
990}
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000991/**
992 * nodePush:
993 * @ctxt: an XML parser context
Daniel Veillard9d06d302002-01-22 18:15:52 +0000994 * @value: the element node
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000995 *
996 * Pushes a new element node on top of the node stack
Daniel Veillard9d06d302002-01-22 18:15:52 +0000997 *
998 * Returns 0 in case of error, the index in the stack otherwise
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000999 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +00001000int
Daniel Veillard1c732d22002-11-30 11:22:59 +00001001nodePush(xmlParserCtxtPtr ctxt, xmlNodePtr value)
1002{
1003 if (ctxt->nodeNr >= ctxt->nodeMax) {
1004 ctxt->nodeMax *= 2;
1005 ctxt->nodeTab =
1006 (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
1007 ctxt->nodeMax *
1008 sizeof(ctxt->nodeTab[0]));
1009 if (ctxt->nodeTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001010 xmlErrMemory(ctxt, NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +00001011 return (0);
1012 }
1013 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00001014 if (((unsigned int) ctxt->nodeNr) > xmlParserMaxDepth) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001015 xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard4aede2e2003-10-17 12:43:59 +00001016 "Excessive depth in document: change xmlParserMaxDepth = %d\n",
1017 xmlParserMaxDepth);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00001018 ctxt->instate = XML_PARSER_EOF;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00001019 return(0);
1020 }
Daniel Veillard1c732d22002-11-30 11:22:59 +00001021 ctxt->nodeTab[ctxt->nodeNr] = value;
1022 ctxt->node = value;
1023 return (ctxt->nodeNr++);
1024}
1025/**
1026 * nodePop:
1027 * @ctxt: an XML parser context
1028 *
1029 * Pops the top element node from the node stack
1030 *
1031 * Returns the node just removed
Owen Taylor3473f882001-02-23 17:55:21 +00001032 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +00001033xmlNodePtr
Daniel Veillard1c732d22002-11-30 11:22:59 +00001034nodePop(xmlParserCtxtPtr ctxt)
1035{
1036 xmlNodePtr ret;
1037
1038 if (ctxt->nodeNr <= 0)
1039 return (0);
1040 ctxt->nodeNr--;
1041 if (ctxt->nodeNr > 0)
1042 ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
1043 else
1044 ctxt->node = NULL;
1045 ret = ctxt->nodeTab[ctxt->nodeNr];
1046 ctxt->nodeTab[ctxt->nodeNr] = 0;
1047 return (ret);
1048}
Daniel Veillarda2351322004-06-27 12:08:10 +00001049
1050#ifdef LIBXML_PUSH_ENABLED
Daniel Veillard1c732d22002-11-30 11:22:59 +00001051/**
Daniel Veillarde57ec792003-09-10 10:50:59 +00001052 * nameNsPush:
1053 * @ctxt: an XML parser context
1054 * @value: the element name
1055 * @prefix: the element prefix
1056 * @URI: the element namespace name
1057 *
1058 * Pushes a new element name/prefix/URL on top of the name stack
1059 *
1060 * Returns -1 in case of error, the index in the stack otherwise
1061 */
1062static int
1063nameNsPush(xmlParserCtxtPtr ctxt, const xmlChar * value,
1064 const xmlChar *prefix, const xmlChar *URI, int nsNr)
1065{
1066 if (ctxt->nameNr >= ctxt->nameMax) {
1067 const xmlChar * *tmp;
1068 void **tmp2;
1069 ctxt->nameMax *= 2;
1070 tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab,
1071 ctxt->nameMax *
1072 sizeof(ctxt->nameTab[0]));
1073 if (tmp == NULL) {
1074 ctxt->nameMax /= 2;
1075 goto mem_error;
1076 }
1077 ctxt->nameTab = tmp;
1078 tmp2 = (void **) xmlRealloc((void * *)ctxt->pushTab,
1079 ctxt->nameMax * 3 *
1080 sizeof(ctxt->pushTab[0]));
1081 if (tmp2 == NULL) {
1082 ctxt->nameMax /= 2;
1083 goto mem_error;
1084 }
1085 ctxt->pushTab = tmp2;
1086 }
1087 ctxt->nameTab[ctxt->nameNr] = value;
1088 ctxt->name = value;
1089 ctxt->pushTab[ctxt->nameNr * 3] = (void *) prefix;
1090 ctxt->pushTab[ctxt->nameNr * 3 + 1] = (void *) URI;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001091 ctxt->pushTab[ctxt->nameNr * 3 + 2] = (void *) (long) nsNr;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001092 return (ctxt->nameNr++);
1093mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001094 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001095 return (-1);
1096}
1097/**
1098 * nameNsPop:
1099 * @ctxt: an XML parser context
1100 *
1101 * Pops the top element/prefix/URI name from the name stack
1102 *
1103 * Returns the name just removed
1104 */
1105static const xmlChar *
1106nameNsPop(xmlParserCtxtPtr ctxt)
1107{
1108 const xmlChar *ret;
1109
1110 if (ctxt->nameNr <= 0)
1111 return (0);
1112 ctxt->nameNr--;
1113 if (ctxt->nameNr > 0)
1114 ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
1115 else
1116 ctxt->name = NULL;
1117 ret = ctxt->nameTab[ctxt->nameNr];
1118 ctxt->nameTab[ctxt->nameNr] = NULL;
1119 return (ret);
1120}
Daniel Veillarda2351322004-06-27 12:08:10 +00001121#endif /* LIBXML_PUSH_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001122
1123/**
Daniel Veillard1c732d22002-11-30 11:22:59 +00001124 * namePush:
1125 * @ctxt: an XML parser context
1126 * @value: the element name
1127 *
1128 * Pushes a new element name on top of the name stack
1129 *
Daniel Veillarde57ec792003-09-10 10:50:59 +00001130 * Returns -1 in case of error, the index in the stack otherwise
Daniel Veillard1c732d22002-11-30 11:22:59 +00001131 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +00001132int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001133namePush(xmlParserCtxtPtr ctxt, const xmlChar * value)
Daniel Veillard1c732d22002-11-30 11:22:59 +00001134{
1135 if (ctxt->nameNr >= ctxt->nameMax) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00001136 const xmlChar * *tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001137 ctxt->nameMax *= 2;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001138 tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab,
Daniel Veillard1c732d22002-11-30 11:22:59 +00001139 ctxt->nameMax *
1140 sizeof(ctxt->nameTab[0]));
Daniel Veillarde57ec792003-09-10 10:50:59 +00001141 if (tmp == NULL) {
1142 ctxt->nameMax /= 2;
1143 goto mem_error;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001144 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001145 ctxt->nameTab = tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001146 }
1147 ctxt->nameTab[ctxt->nameNr] = value;
1148 ctxt->name = value;
1149 return (ctxt->nameNr++);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001150mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001151 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001152 return (-1);
Daniel Veillard1c732d22002-11-30 11:22:59 +00001153}
1154/**
1155 * namePop:
1156 * @ctxt: an XML parser context
1157 *
1158 * Pops the top element name from the name stack
1159 *
1160 * Returns the name just removed
1161 */
Daniel Veillard7a5e0dd2004-09-17 08:45:25 +00001162const xmlChar *
Daniel Veillard1c732d22002-11-30 11:22:59 +00001163namePop(xmlParserCtxtPtr ctxt)
1164{
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001165 const xmlChar *ret;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001166
1167 if (ctxt->nameNr <= 0)
1168 return (0);
1169 ctxt->nameNr--;
1170 if (ctxt->nameNr > 0)
1171 ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
1172 else
1173 ctxt->name = NULL;
1174 ret = ctxt->nameTab[ctxt->nameNr];
1175 ctxt->nameTab[ctxt->nameNr] = 0;
1176 return (ret);
1177}
Owen Taylor3473f882001-02-23 17:55:21 +00001178
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001179static int spacePush(xmlParserCtxtPtr ctxt, int val) {
Owen Taylor3473f882001-02-23 17:55:21 +00001180 if (ctxt->spaceNr >= ctxt->spaceMax) {
1181 ctxt->spaceMax *= 2;
1182 ctxt->spaceTab = (int *) xmlRealloc(ctxt->spaceTab,
1183 ctxt->spaceMax * sizeof(ctxt->spaceTab[0]));
1184 if (ctxt->spaceTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001185 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001186 return(0);
1187 }
1188 }
1189 ctxt->spaceTab[ctxt->spaceNr] = val;
1190 ctxt->space = &ctxt->spaceTab[ctxt->spaceNr];
1191 return(ctxt->spaceNr++);
1192}
1193
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001194static int spacePop(xmlParserCtxtPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00001195 int ret;
1196 if (ctxt->spaceNr <= 0) return(0);
1197 ctxt->spaceNr--;
1198 if (ctxt->spaceNr > 0)
1199 ctxt->space = &ctxt->spaceTab[ctxt->spaceNr - 1];
1200 else
1201 ctxt->space = NULL;
1202 ret = ctxt->spaceTab[ctxt->spaceNr];
1203 ctxt->spaceTab[ctxt->spaceNr] = -1;
1204 return(ret);
1205}
1206
1207/*
1208 * Macros for accessing the content. Those should be used only by the parser,
1209 * and not exported.
1210 *
1211 * Dirty macros, i.e. one often need to make assumption on the context to
1212 * use them
1213 *
1214 * CUR_PTR return the current pointer to the xmlChar to be parsed.
1215 * To be used with extreme caution since operations consuming
1216 * characters may move the input buffer to a different location !
1217 * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled
1218 * This should be used internally by the parser
1219 * only to compare to ASCII values otherwise it would break when
1220 * running with UTF-8 encoding.
1221 * RAW same as CUR but in the input buffer, bypass any token
1222 * extraction that may have been done
1223 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
1224 * to compare on ASCII based substring.
1225 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
Daniel Veillard77a90a72003-03-22 00:04:05 +00001226 * strings without newlines within the parser.
1227 * NEXT1(l) Skip 1 xmlChar, and must also be used only to skip 1 non-newline ASCII
1228 * defined char within the parser.
Owen Taylor3473f882001-02-23 17:55:21 +00001229 * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
1230 *
1231 * NEXT Skip to the next character, this does the proper decoding
1232 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
Daniel Veillard77a90a72003-03-22 00:04:05 +00001233 * NEXTL(l) Skip the current unicode character of l xmlChars long.
Owen Taylor3473f882001-02-23 17:55:21 +00001234 * CUR_CHAR(l) returns the current unicode character (int), set l
1235 * to the number of xmlChars used for the encoding [0-5].
1236 * CUR_SCHAR same but operate on a string instead of the context
1237 * COPY_BUF copy the current unicode char to the target buffer, increment
1238 * the index
1239 * GROW, SHRINK handling of input buffers
1240 */
1241
Daniel Veillardfdc91562002-07-01 21:52:03 +00001242#define RAW (*ctxt->input->cur)
1243#define CUR (*ctxt->input->cur)
Owen Taylor3473f882001-02-23 17:55:21 +00001244#define NXT(val) ctxt->input->cur[(val)]
1245#define CUR_PTR ctxt->input->cur
1246
Daniel Veillarda07050d2003-10-19 14:46:32 +00001247#define CMP4( s, c1, c2, c3, c4 ) \
1248 ( ((unsigned char *) s)[ 0 ] == c1 && ((unsigned char *) s)[ 1 ] == c2 && \
1249 ((unsigned char *) s)[ 2 ] == c3 && ((unsigned char *) s)[ 3 ] == c4 )
1250#define CMP5( s, c1, c2, c3, c4, c5 ) \
1251 ( CMP4( s, c1, c2, c3, c4 ) && ((unsigned char *) s)[ 4 ] == c5 )
1252#define CMP6( s, c1, c2, c3, c4, c5, c6 ) \
1253 ( CMP5( s, c1, c2, c3, c4, c5 ) && ((unsigned char *) s)[ 5 ] == c6 )
1254#define CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) \
1255 ( CMP6( s, c1, c2, c3, c4, c5, c6 ) && ((unsigned char *) s)[ 6 ] == c7 )
1256#define CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) \
1257 ( CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) && ((unsigned char *) s)[ 7 ] == c8 )
1258#define CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) \
1259 ( CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) && \
1260 ((unsigned char *) s)[ 8 ] == c9 )
1261#define CMP10( s, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 ) \
1262 ( CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) && \
1263 ((unsigned char *) s)[ 9 ] == c10 )
1264
Owen Taylor3473f882001-02-23 17:55:21 +00001265#define SKIP(val) do { \
Daniel Veillard77a90a72003-03-22 00:04:05 +00001266 ctxt->nbChars += (val),ctxt->input->cur += (val),ctxt->input->col+=(val); \
Owen Taylor3473f882001-02-23 17:55:21 +00001267 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
Daniel Veillard561b7f82002-03-20 21:55:57 +00001268 if ((*ctxt->input->cur == 0) && \
Owen Taylor3473f882001-02-23 17:55:21 +00001269 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \
1270 xmlPopInput(ctxt); \
1271 } while (0)
1272
Daniel Veillard0b787f32004-03-26 17:29:53 +00001273#define SKIPL(val) do { \
1274 int skipl; \
1275 for(skipl=0; skipl<val; skipl++) { \
1276 if (*(ctxt->input->cur) == '\n') { \
1277 ctxt->input->line++; ctxt->input->col = 1; \
1278 } else ctxt->input->col++; \
1279 ctxt->nbChars++; \
1280 ctxt->input->cur++; \
1281 } \
1282 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
1283 if ((*ctxt->input->cur == 0) && \
1284 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \
1285 xmlPopInput(ctxt); \
1286 } while (0)
1287
Daniel Veillarda880b122003-04-21 21:36:41 +00001288#define SHRINK if ((ctxt->progressive == 0) && \
Daniel Veillard6155d8a2003-08-19 15:01:28 +00001289 (ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \
1290 (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \
Daniel Veillard46de64e2002-05-29 08:21:33 +00001291 xmlSHRINK (ctxt);
1292
1293static void xmlSHRINK (xmlParserCtxtPtr ctxt) {
1294 xmlParserInputShrink(ctxt->input);
1295 if ((*ctxt->input->cur == 0) &&
1296 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
1297 xmlPopInput(ctxt);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001298 }
Owen Taylor3473f882001-02-23 17:55:21 +00001299
Daniel Veillarda880b122003-04-21 21:36:41 +00001300#define GROW if ((ctxt->progressive == 0) && \
1301 (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \
Daniel Veillard46de64e2002-05-29 08:21:33 +00001302 xmlGROW (ctxt);
1303
1304static void xmlGROW (xmlParserCtxtPtr ctxt) {
1305 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1306 if ((*ctxt->input->cur == 0) &&
1307 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
1308 xmlPopInput(ctxt);
Daniel Veillarda880b122003-04-21 21:36:41 +00001309}
Owen Taylor3473f882001-02-23 17:55:21 +00001310
1311#define SKIP_BLANKS xmlSkipBlankChars(ctxt)
1312
1313#define NEXT xmlNextChar(ctxt)
1314
Daniel Veillard21a0f912001-02-25 19:54:14 +00001315#define NEXT1 { \
Daniel Veillard77a90a72003-03-22 00:04:05 +00001316 ctxt->input->col++; \
Daniel Veillard21a0f912001-02-25 19:54:14 +00001317 ctxt->input->cur++; \
1318 ctxt->nbChars++; \
Daniel Veillard561b7f82002-03-20 21:55:57 +00001319 if (*ctxt->input->cur == 0) \
Daniel Veillard21a0f912001-02-25 19:54:14 +00001320 xmlParserInputGrow(ctxt->input, INPUT_CHUNK); \
1321 }
1322
Owen Taylor3473f882001-02-23 17:55:21 +00001323#define NEXTL(l) do { \
1324 if (*(ctxt->input->cur) == '\n') { \
1325 ctxt->input->line++; ctxt->input->col = 1; \
1326 } else ctxt->input->col++; \
Daniel Veillardfdc91562002-07-01 21:52:03 +00001327 ctxt->input->cur += l; \
Owen Taylor3473f882001-02-23 17:55:21 +00001328 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
Owen Taylor3473f882001-02-23 17:55:21 +00001329 } while (0)
1330
1331#define CUR_CHAR(l) xmlCurrentChar(ctxt, &l)
1332#define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l)
1333
1334#define COPY_BUF(l,b,i,v) \
1335 if (l == 1) b[i++] = (xmlChar) v; \
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001336 else i += xmlCopyCharMultiByte(&b[i],v)
Owen Taylor3473f882001-02-23 17:55:21 +00001337
1338/**
1339 * xmlSkipBlankChars:
1340 * @ctxt: the XML parser context
1341 *
1342 * skip all blanks character found at that point in the input streams.
1343 * It pops up finished entities in the process if allowable at that point.
1344 *
1345 * Returns the number of space chars skipped
1346 */
1347
1348int
1349xmlSkipBlankChars(xmlParserCtxtPtr ctxt) {
Daniel Veillard02141ea2001-04-30 11:46:40 +00001350 int res = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001351
1352 /*
1353 * It's Okay to use CUR/NEXT here since all the blanks are on
1354 * the ASCII range.
1355 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00001356 if ((ctxt->inputNr == 1) && (ctxt->instate != XML_PARSER_DTD)) {
1357 const xmlChar *cur;
Owen Taylor3473f882001-02-23 17:55:21 +00001358 /*
Daniel Veillard02141ea2001-04-30 11:46:40 +00001359 * if we are in the document content, go really fast
Owen Taylor3473f882001-02-23 17:55:21 +00001360 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00001361 cur = ctxt->input->cur;
William M. Brack76e95df2003-10-18 16:20:14 +00001362 while (IS_BLANK_CH(*cur)) {
Daniel Veillard02141ea2001-04-30 11:46:40 +00001363 if (*cur == '\n') {
1364 ctxt->input->line++; ctxt->input->col = 1;
1365 }
1366 cur++;
1367 res++;
1368 if (*cur == 0) {
1369 ctxt->input->cur = cur;
1370 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1371 cur = ctxt->input->cur;
1372 }
1373 }
1374 ctxt->input->cur = cur;
1375 } else {
1376 int cur;
1377 do {
1378 cur = CUR;
1379 while (IS_BLANK(cur)) { /* CHECKED tstblanks.xml */
1380 NEXT;
1381 cur = CUR;
1382 res++;
1383 }
1384 while ((cur == 0) && (ctxt->inputNr > 1) &&
1385 (ctxt->instate != XML_PARSER_COMMENT)) {
1386 xmlPopInput(ctxt);
1387 cur = CUR;
1388 }
1389 /*
1390 * Need to handle support of entities branching here
1391 */
1392 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt);
1393 } while (IS_BLANK(cur)); /* CHECKED tstblanks.xml */
1394 }
Owen Taylor3473f882001-02-23 17:55:21 +00001395 return(res);
1396}
1397
1398/************************************************************************
1399 * *
1400 * Commodity functions to handle entities *
1401 * *
1402 ************************************************************************/
1403
1404/**
1405 * xmlPopInput:
1406 * @ctxt: an XML parser context
1407 *
1408 * xmlPopInput: the current input pointed by ctxt->input came to an end
1409 * pop it and return the next char.
1410 *
1411 * Returns the current xmlChar in the parser context
1412 */
1413xmlChar
1414xmlPopInput(xmlParserCtxtPtr ctxt) {
1415 if (ctxt->inputNr == 1) return(0); /* End of main Input */
1416 if (xmlParserDebugEntities)
1417 xmlGenericError(xmlGenericErrorContext,
1418 "Popping input %d\n", ctxt->inputNr);
1419 xmlFreeInputStream(inputPop(ctxt));
Daniel Veillard561b7f82002-03-20 21:55:57 +00001420 if ((*ctxt->input->cur == 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +00001421 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
1422 return(xmlPopInput(ctxt));
1423 return(CUR);
1424}
1425
1426/**
1427 * xmlPushInput:
1428 * @ctxt: an XML parser context
1429 * @input: an XML parser input fragment (entity, XML fragment ...).
1430 *
1431 * xmlPushInput: switch to a new input stream which is stacked on top
1432 * of the previous one(s).
1433 */
1434void
1435xmlPushInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr input) {
1436 if (input == NULL) return;
1437
1438 if (xmlParserDebugEntities) {
1439 if ((ctxt->input != NULL) && (ctxt->input->filename))
1440 xmlGenericError(xmlGenericErrorContext,
1441 "%s(%d): ", ctxt->input->filename,
1442 ctxt->input->line);
1443 xmlGenericError(xmlGenericErrorContext,
1444 "Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur);
1445 }
1446 inputPush(ctxt, input);
1447 GROW;
1448}
1449
1450/**
1451 * xmlParseCharRef:
1452 * @ctxt: an XML parser context
1453 *
1454 * parse Reference declarations
1455 *
1456 * [66] CharRef ::= '&#' [0-9]+ ';' |
1457 * '&#x' [0-9a-fA-F]+ ';'
1458 *
1459 * [ WFC: Legal Character ]
1460 * Characters referred to using character references must match the
1461 * production for Char.
1462 *
1463 * Returns the value parsed (as an int), 0 in case of error
1464 */
1465int
1466xmlParseCharRef(xmlParserCtxtPtr ctxt) {
Daniel Veillard50582112001-03-26 22:52:16 +00001467 unsigned int val = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001468 int count = 0;
Daniel Veillard37fd3072004-06-03 11:22:31 +00001469 unsigned int outofrange = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001470
Owen Taylor3473f882001-02-23 17:55:21 +00001471 /*
1472 * Using RAW/CUR/NEXT is okay since we are working on ASCII range here
1473 */
Daniel Veillard561b7f82002-03-20 21:55:57 +00001474 if ((RAW == '&') && (NXT(1) == '#') &&
Owen Taylor3473f882001-02-23 17:55:21 +00001475 (NXT(2) == 'x')) {
1476 SKIP(3);
1477 GROW;
1478 while (RAW != ';') { /* loop blocked by count */
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00001479 if (count++ > 20) {
1480 count = 0;
1481 GROW;
1482 }
1483 if ((RAW >= '0') && (RAW <= '9'))
Owen Taylor3473f882001-02-23 17:55:21 +00001484 val = val * 16 + (CUR - '0');
1485 else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20))
1486 val = val * 16 + (CUR - 'a') + 10;
1487 else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20))
1488 val = val * 16 + (CUR - 'A') + 10;
1489 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001490 xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001491 val = 0;
1492 break;
1493 }
Daniel Veillard37fd3072004-06-03 11:22:31 +00001494 if (val > 0x10FFFF)
1495 outofrange = val;
1496
Owen Taylor3473f882001-02-23 17:55:21 +00001497 NEXT;
1498 count++;
1499 }
1500 if (RAW == ';') {
1501 /* on purpose to avoid reentrancy problems with NEXT and SKIP */
Daniel Veillard77a90a72003-03-22 00:04:05 +00001502 ctxt->input->col++;
Owen Taylor3473f882001-02-23 17:55:21 +00001503 ctxt->nbChars ++;
1504 ctxt->input->cur++;
1505 }
Daniel Veillard561b7f82002-03-20 21:55:57 +00001506 } else if ((RAW == '&') && (NXT(1) == '#')) {
Owen Taylor3473f882001-02-23 17:55:21 +00001507 SKIP(2);
1508 GROW;
1509 while (RAW != ';') { /* loop blocked by count */
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00001510 if (count++ > 20) {
1511 count = 0;
1512 GROW;
1513 }
1514 if ((RAW >= '0') && (RAW <= '9'))
Owen Taylor3473f882001-02-23 17:55:21 +00001515 val = val * 10 + (CUR - '0');
1516 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001517 xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001518 val = 0;
1519 break;
1520 }
Daniel Veillard37fd3072004-06-03 11:22:31 +00001521 if (val > 0x10FFFF)
1522 outofrange = val;
1523
Owen Taylor3473f882001-02-23 17:55:21 +00001524 NEXT;
1525 count++;
1526 }
1527 if (RAW == ';') {
1528 /* on purpose to avoid reentrancy problems with NEXT and SKIP */
Daniel Veillard77a90a72003-03-22 00:04:05 +00001529 ctxt->input->col++;
Owen Taylor3473f882001-02-23 17:55:21 +00001530 ctxt->nbChars ++;
1531 ctxt->input->cur++;
1532 }
1533 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001534 xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001535 }
1536
1537 /*
1538 * [ WFC: Legal Character ]
1539 * Characters referred to using character references must match the
1540 * production for Char.
1541 */
Daniel Veillard37fd3072004-06-03 11:22:31 +00001542 if ((IS_CHAR(val) && (outofrange == 0))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001543 return(val);
1544 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001545 xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
1546 "xmlParseCharRef: invalid xmlChar value %d\n",
1547 val);
Owen Taylor3473f882001-02-23 17:55:21 +00001548 }
1549 return(0);
1550}
1551
1552/**
1553 * xmlParseStringCharRef:
1554 * @ctxt: an XML parser context
1555 * @str: a pointer to an index in the string
1556 *
1557 * parse Reference declarations, variant parsing from a string rather
1558 * than an an input flow.
1559 *
1560 * [66] CharRef ::= '&#' [0-9]+ ';' |
1561 * '&#x' [0-9a-fA-F]+ ';'
1562 *
1563 * [ WFC: Legal Character ]
1564 * Characters referred to using character references must match the
1565 * production for Char.
1566 *
1567 * Returns the value parsed (as an int), 0 in case of error, str will be
1568 * updated to the current value of the index
1569 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001570static int
Owen Taylor3473f882001-02-23 17:55:21 +00001571xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) {
1572 const xmlChar *ptr;
1573 xmlChar cur;
Daniel Veillard37fd3072004-06-03 11:22:31 +00001574 unsigned int val = 0;
1575 unsigned int outofrange = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001576
1577 if ((str == NULL) || (*str == NULL)) return(0);
1578 ptr = *str;
1579 cur = *ptr;
1580 if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) {
1581 ptr += 3;
1582 cur = *ptr;
1583 while (cur != ';') { /* Non input consuming loop */
1584 if ((cur >= '0') && (cur <= '9'))
1585 val = val * 16 + (cur - '0');
1586 else if ((cur >= 'a') && (cur <= 'f'))
1587 val = val * 16 + (cur - 'a') + 10;
1588 else if ((cur >= 'A') && (cur <= 'F'))
1589 val = val * 16 + (cur - 'A') + 10;
1590 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001591 xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001592 val = 0;
1593 break;
1594 }
Daniel Veillard37fd3072004-06-03 11:22:31 +00001595 if (val > 0x10FFFF)
1596 outofrange = val;
1597
Owen Taylor3473f882001-02-23 17:55:21 +00001598 ptr++;
1599 cur = *ptr;
1600 }
1601 if (cur == ';')
1602 ptr++;
1603 } else if ((cur == '&') && (ptr[1] == '#')){
1604 ptr += 2;
1605 cur = *ptr;
1606 while (cur != ';') { /* Non input consuming loops */
1607 if ((cur >= '0') && (cur <= '9'))
1608 val = val * 10 + (cur - '0');
1609 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001610 xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001611 val = 0;
1612 break;
1613 }
Daniel Veillard37fd3072004-06-03 11:22:31 +00001614 if (val > 0x10FFFF)
1615 outofrange = val;
1616
Owen Taylor3473f882001-02-23 17:55:21 +00001617 ptr++;
1618 cur = *ptr;
1619 }
1620 if (cur == ';')
1621 ptr++;
1622 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001623 xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001624 return(0);
1625 }
1626 *str = ptr;
1627
1628 /*
1629 * [ WFC: Legal Character ]
1630 * Characters referred to using character references must match the
1631 * production for Char.
1632 */
Daniel Veillard37fd3072004-06-03 11:22:31 +00001633 if ((IS_CHAR(val) && (outofrange == 0))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001634 return(val);
1635 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001636 xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
1637 "xmlParseStringCharRef: invalid xmlChar value %d\n",
1638 val);
Owen Taylor3473f882001-02-23 17:55:21 +00001639 }
1640 return(0);
1641}
1642
1643/**
Daniel Veillardf5582f12002-06-11 10:08:16 +00001644 * xmlNewBlanksWrapperInputStream:
1645 * @ctxt: an XML parser context
1646 * @entity: an Entity pointer
1647 *
1648 * Create a new input stream for wrapping
1649 * blanks around a PEReference
1650 *
1651 * Returns the new input stream or NULL
1652 */
1653
1654static void deallocblankswrapper (xmlChar *str) {xmlFree(str);}
1655
Daniel Veillardf4862f02002-09-10 11:13:43 +00001656static xmlParserInputPtr
Daniel Veillardf5582f12002-06-11 10:08:16 +00001657xmlNewBlanksWrapperInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
1658 xmlParserInputPtr input;
1659 xmlChar *buffer;
1660 size_t length;
1661 if (entity == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001662 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
1663 "xmlNewBlanksWrapperInputStream entity\n");
Daniel Veillardf5582f12002-06-11 10:08:16 +00001664 return(NULL);
1665 }
1666 if (xmlParserDebugEntities)
1667 xmlGenericError(xmlGenericErrorContext,
1668 "new blanks wrapper for entity: %s\n", entity->name);
1669 input = xmlNewInputStream(ctxt);
1670 if (input == NULL) {
1671 return(NULL);
1672 }
1673 length = xmlStrlen(entity->name) + 5;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001674 buffer = xmlMallocAtomic(length);
Daniel Veillardf5582f12002-06-11 10:08:16 +00001675 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001676 xmlErrMemory(ctxt, NULL);
Daniel Veillardf5582f12002-06-11 10:08:16 +00001677 return(NULL);
1678 }
1679 buffer [0] = ' ';
1680 buffer [1] = '%';
1681 buffer [length-3] = ';';
1682 buffer [length-2] = ' ';
1683 buffer [length-1] = 0;
1684 memcpy(buffer + 2, entity->name, length - 5);
1685 input->free = deallocblankswrapper;
1686 input->base = buffer;
1687 input->cur = buffer;
1688 input->length = length;
1689 input->end = &buffer[length];
1690 return(input);
1691}
1692
1693/**
Owen Taylor3473f882001-02-23 17:55:21 +00001694 * xmlParserHandlePEReference:
1695 * @ctxt: the parser context
1696 *
1697 * [69] PEReference ::= '%' Name ';'
1698 *
1699 * [ WFC: No Recursion ]
1700 * A parsed entity must not contain a recursive
1701 * reference to itself, either directly or indirectly.
1702 *
1703 * [ WFC: Entity Declared ]
1704 * In a document without any DTD, a document with only an internal DTD
1705 * subset which contains no parameter entity references, or a document
1706 * with "standalone='yes'", ... ... The declaration of a parameter
1707 * entity must precede any reference to it...
1708 *
1709 * [ VC: Entity Declared ]
1710 * In a document with an external subset or external parameter entities
1711 * with "standalone='no'", ... ... The declaration of a parameter entity
1712 * must precede any reference to it...
1713 *
1714 * [ WFC: In DTD ]
1715 * Parameter-entity references may only appear in the DTD.
1716 * NOTE: misleading but this is handled.
1717 *
1718 * A PEReference may have been detected in the current input stream
1719 * the handling is done accordingly to
1720 * http://www.w3.org/TR/REC-xml#entproc
1721 * i.e.
1722 * - Included in literal in entity values
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001723 * - Included as Parameter Entity reference within DTDs
Owen Taylor3473f882001-02-23 17:55:21 +00001724 */
1725void
1726xmlParserHandlePEReference(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001727 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00001728 xmlEntityPtr entity = NULL;
1729 xmlParserInputPtr input;
1730
Owen Taylor3473f882001-02-23 17:55:21 +00001731 if (RAW != '%') return;
1732 switch(ctxt->instate) {
1733 case XML_PARSER_CDATA_SECTION:
1734 return;
1735 case XML_PARSER_COMMENT:
1736 return;
1737 case XML_PARSER_START_TAG:
1738 return;
1739 case XML_PARSER_END_TAG:
1740 return;
1741 case XML_PARSER_EOF:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001742 xmlFatalErr(ctxt, XML_ERR_PEREF_AT_EOF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001743 return;
1744 case XML_PARSER_PROLOG:
1745 case XML_PARSER_START:
1746 case XML_PARSER_MISC:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001747 xmlFatalErr(ctxt, XML_ERR_PEREF_IN_PROLOG, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001748 return;
1749 case XML_PARSER_ENTITY_DECL:
1750 case XML_PARSER_CONTENT:
1751 case XML_PARSER_ATTRIBUTE_VALUE:
1752 case XML_PARSER_PI:
1753 case XML_PARSER_SYSTEM_LITERAL:
Daniel Veillard4a7ae502002-02-18 19:18:17 +00001754 case XML_PARSER_PUBLIC_LITERAL:
Owen Taylor3473f882001-02-23 17:55:21 +00001755 /* we just ignore it there */
1756 return;
1757 case XML_PARSER_EPILOG:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001758 xmlFatalErr(ctxt, XML_ERR_PEREF_IN_EPILOG, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001759 return;
1760 case XML_PARSER_ENTITY_VALUE:
1761 /*
1762 * NOTE: in the case of entity values, we don't do the
1763 * substitution here since we need the literal
1764 * entity value to be able to save the internal
1765 * subset of the document.
1766 * This will be handled by xmlStringDecodeEntities
1767 */
1768 return;
1769 case XML_PARSER_DTD:
1770 /*
1771 * [WFC: Well-Formedness Constraint: PEs in Internal Subset]
1772 * In the internal DTD subset, parameter-entity references
1773 * can occur only where markup declarations can occur, not
1774 * within markup declarations.
1775 * In that case this is handled in xmlParseMarkupDecl
1776 */
1777 if ((ctxt->external == 0) && (ctxt->inputNr == 1))
1778 return;
William M. Brack76e95df2003-10-18 16:20:14 +00001779 if (IS_BLANK_CH(NXT(1)) || NXT(1) == 0)
Daniel Veillardf5582f12002-06-11 10:08:16 +00001780 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001781 break;
1782 case XML_PARSER_IGNORE:
1783 return;
1784 }
1785
1786 NEXT;
1787 name = xmlParseName(ctxt);
1788 if (xmlParserDebugEntities)
1789 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001790 "PEReference: %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00001791 if (name == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001792 xmlFatalErr(ctxt, XML_ERR_PEREF_NO_NAME, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001793 } else {
1794 if (RAW == ';') {
1795 NEXT;
1796 if ((ctxt->sax != NULL) && (ctxt->sax->getParameterEntity != NULL))
1797 entity = ctxt->sax->getParameterEntity(ctxt->userData, name);
1798 if (entity == NULL) {
1799
1800 /*
1801 * [ WFC: Entity Declared ]
1802 * In a document without any DTD, a document with only an
1803 * internal DTD subset which contains no parameter entity
1804 * references, or a document with "standalone='yes'", ...
1805 * ... The declaration of a parameter entity must precede
1806 * any reference to it...
1807 */
1808 if ((ctxt->standalone == 1) ||
1809 ((ctxt->hasExternalSubset == 0) &&
1810 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00001811 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00001812 "PEReference: %%%s; not found\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00001813 } else {
1814 /*
1815 * [ VC: Entity Declared ]
1816 * In a document with an external subset or external
1817 * parameter entities with "standalone='no'", ...
1818 * ... The declaration of a parameter entity must precede
1819 * any reference to it...
1820 */
Daniel Veillard24eb9782003-10-04 21:08:09 +00001821 if ((ctxt->validate) && (ctxt->vctxt.error != NULL)) {
1822 xmlValidityError(ctxt, XML_WAR_UNDECLARED_ENTITY,
1823 "PEReference: %%%s; not found\n",
1824 name);
1825 } else
1826 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
1827 "PEReference: %%%s; not found\n",
1828 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001829 ctxt->valid = 0;
1830 }
Daniel Veillardf5582f12002-06-11 10:08:16 +00001831 } else if (ctxt->input->free != deallocblankswrapper) {
1832 input = xmlNewBlanksWrapperInputStream(ctxt, entity);
1833 xmlPushInput(ctxt, input);
Owen Taylor3473f882001-02-23 17:55:21 +00001834 } else {
1835 if ((entity->etype == XML_INTERNAL_PARAMETER_ENTITY) ||
1836 (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY)) {
Daniel Veillard87a764e2001-06-20 17:41:10 +00001837 xmlChar start[4];
1838 xmlCharEncoding enc;
1839
Owen Taylor3473f882001-02-23 17:55:21 +00001840 /*
1841 * handle the extra spaces added before and after
1842 * c.f. http://www.w3.org/TR/REC-xml#as-PE
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001843 * this is done independently.
Owen Taylor3473f882001-02-23 17:55:21 +00001844 */
1845 input = xmlNewEntityInputStream(ctxt, entity);
1846 xmlPushInput(ctxt, input);
Daniel Veillard87a764e2001-06-20 17:41:10 +00001847
1848 /*
1849 * Get the 4 first bytes and decode the charset
1850 * if enc != XML_CHAR_ENCODING_NONE
1851 * plug some encoding conversion routines.
William M. Bracka0c48ad2004-04-16 15:58:29 +00001852 * Note that, since we may have some non-UTF8
1853 * encoding (like UTF16, bug 135229), the 'length'
1854 * is not known, but we can calculate based upon
1855 * the amount of data in the buffer.
Daniel Veillard87a764e2001-06-20 17:41:10 +00001856 */
1857 GROW
William M. Bracka0c48ad2004-04-16 15:58:29 +00001858 if ((ctxt->input->end - ctxt->input->cur)>=4) {
Daniel Veillarde059b892002-06-13 15:32:10 +00001859 start[0] = RAW;
1860 start[1] = NXT(1);
1861 start[2] = NXT(2);
1862 start[3] = NXT(3);
1863 enc = xmlDetectCharEncoding(start, 4);
1864 if (enc != XML_CHAR_ENCODING_NONE) {
1865 xmlSwitchEncoding(ctxt, enc);
1866 }
Daniel Veillard87a764e2001-06-20 17:41:10 +00001867 }
1868
Owen Taylor3473f882001-02-23 17:55:21 +00001869 if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) &&
Daniel Veillarda07050d2003-10-19 14:46:32 +00001870 (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l' )) &&
1871 (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001872 xmlParseTextDecl(ctxt);
1873 }
Owen Taylor3473f882001-02-23 17:55:21 +00001874 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00001875 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER,
1876 "PEReference: %s is not a parameter entity\n",
1877 name);
Owen Taylor3473f882001-02-23 17:55:21 +00001878 }
1879 }
1880 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001881 xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001882 }
Owen Taylor3473f882001-02-23 17:55:21 +00001883 }
1884}
1885
1886/*
1887 * Macro used to grow the current buffer.
1888 */
1889#define growBuffer(buffer) { \
Daniel Veillardd3999c72004-03-10 16:27:03 +00001890 xmlChar *tmp; \
Owen Taylor3473f882001-02-23 17:55:21 +00001891 buffer##_size *= 2; \
Daniel Veillardd3999c72004-03-10 16:27:03 +00001892 tmp = (xmlChar *) \
Owen Taylor3473f882001-02-23 17:55:21 +00001893 xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \
Daniel Veillardd3999c72004-03-10 16:27:03 +00001894 if (tmp == NULL) goto mem_error; \
1895 buffer = tmp; \
Owen Taylor3473f882001-02-23 17:55:21 +00001896}
1897
1898/**
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00001899 * xmlStringLenDecodeEntities:
Owen Taylor3473f882001-02-23 17:55:21 +00001900 * @ctxt: the parser context
1901 * @str: the input string
Daniel Veillarde57ec792003-09-10 10:50:59 +00001902 * @len: the string length
Owen Taylor3473f882001-02-23 17:55:21 +00001903 * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
1904 * @end: an end marker xmlChar, 0 if none
1905 * @end2: an end marker xmlChar, 0 if none
1906 * @end3: an end marker xmlChar, 0 if none
1907 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001908 * Takes a entity string content and process to do the adequate substitutions.
Owen Taylor3473f882001-02-23 17:55:21 +00001909 *
1910 * [67] Reference ::= EntityRef | CharRef
1911 *
1912 * [69] PEReference ::= '%' Name ';'
1913 *
1914 * Returns A newly allocated string with the substitution done. The caller
1915 * must deallocate it !
1916 */
1917xmlChar *
Daniel Veillarde57ec792003-09-10 10:50:59 +00001918xmlStringLenDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int len,
1919 int what, xmlChar end, xmlChar end2, xmlChar end3) {
Owen Taylor3473f882001-02-23 17:55:21 +00001920 xmlChar *buffer = NULL;
1921 int buffer_size = 0;
1922
1923 xmlChar *current = NULL;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001924 const xmlChar *last;
Owen Taylor3473f882001-02-23 17:55:21 +00001925 xmlEntityPtr ent;
1926 int c,l;
1927 int nbchars = 0;
1928
Daniel Veillarde57ec792003-09-10 10:50:59 +00001929 if ((str == NULL) || (len < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00001930 return(NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001931 last = str + len;
Owen Taylor3473f882001-02-23 17:55:21 +00001932
1933 if (ctxt->depth > 40) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001934 xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001935 return(NULL);
1936 }
1937
1938 /*
1939 * allocate a translation buffer.
1940 */
1941 buffer_size = XML_PARSER_BIG_BUFFER_SIZE;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001942 buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar));
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00001943 if (buffer == NULL) goto mem_error;
Owen Taylor3473f882001-02-23 17:55:21 +00001944
1945 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001946 * OK loop until we reach one of the ending char or a size limit.
Owen Taylor3473f882001-02-23 17:55:21 +00001947 * we are operating on already parsed values.
1948 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001949 if (str < last)
1950 c = CUR_SCHAR(str, l);
1951 else
1952 c = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001953 while ((c != 0) && (c != end) && /* non input consuming loop */
1954 (c != end2) && (c != end3)) {
1955
1956 if (c == 0) break;
1957 if ((c == '&') && (str[1] == '#')) {
1958 int val = xmlParseStringCharRef(ctxt, &str);
1959 if (val != 0) {
1960 COPY_BUF(0,buffer,nbchars,val);
1961 }
1962 } else if ((c == '&') && (what & XML_SUBSTITUTE_REF)) {
1963 if (xmlParserDebugEntities)
1964 xmlGenericError(xmlGenericErrorContext,
1965 "String decoding Entity Reference: %.30s\n",
1966 str);
1967 ent = xmlParseStringEntityRef(ctxt, &str);
1968 if ((ent != NULL) &&
1969 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1970 if (ent->content != NULL) {
1971 COPY_BUF(0,buffer,nbchars,ent->content[0]);
1972 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00001973 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
1974 "predefined entity has no content\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001975 }
1976 } else if ((ent != NULL) && (ent->content != NULL)) {
1977 xmlChar *rep;
1978
1979 ctxt->depth++;
1980 rep = xmlStringDecodeEntities(ctxt, ent->content, what,
1981 0, 0, 0);
1982 ctxt->depth--;
1983 if (rep != NULL) {
1984 current = rep;
1985 while (*current != 0) { /* non input consuming loop */
1986 buffer[nbchars++] = *current++;
1987 if (nbchars >
1988 buffer_size - XML_PARSER_BUFFER_SIZE) {
1989 growBuffer(buffer);
1990 }
1991 }
1992 xmlFree(rep);
1993 }
1994 } else if (ent != NULL) {
1995 int i = xmlStrlen(ent->name);
1996 const xmlChar *cur = ent->name;
1997
1998 buffer[nbchars++] = '&';
1999 if (nbchars > buffer_size - i - XML_PARSER_BUFFER_SIZE) {
2000 growBuffer(buffer);
2001 }
2002 for (;i > 0;i--)
2003 buffer[nbchars++] = *cur++;
2004 buffer[nbchars++] = ';';
2005 }
2006 } else if (c == '%' && (what & XML_SUBSTITUTE_PEREF)) {
2007 if (xmlParserDebugEntities)
2008 xmlGenericError(xmlGenericErrorContext,
2009 "String decoding PE Reference: %.30s\n", str);
2010 ent = xmlParseStringPEReference(ctxt, &str);
2011 if (ent != NULL) {
2012 xmlChar *rep;
2013
2014 ctxt->depth++;
2015 rep = xmlStringDecodeEntities(ctxt, ent->content, what,
2016 0, 0, 0);
2017 ctxt->depth--;
2018 if (rep != NULL) {
2019 current = rep;
2020 while (*current != 0) { /* non input consuming loop */
2021 buffer[nbchars++] = *current++;
2022 if (nbchars >
2023 buffer_size - XML_PARSER_BUFFER_SIZE) {
2024 growBuffer(buffer);
2025 }
2026 }
2027 xmlFree(rep);
2028 }
2029 }
2030 } else {
2031 COPY_BUF(l,buffer,nbchars,c);
2032 str += l;
2033 if (nbchars > buffer_size - XML_PARSER_BUFFER_SIZE) {
2034 growBuffer(buffer);
2035 }
2036 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002037 if (str < last)
2038 c = CUR_SCHAR(str, l);
2039 else
2040 c = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002041 }
2042 buffer[nbchars++] = 0;
2043 return(buffer);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002044
2045mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002046 xmlErrMemory(ctxt, NULL);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002047 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002048}
2049
Daniel Veillarde57ec792003-09-10 10:50:59 +00002050/**
2051 * xmlStringDecodeEntities:
2052 * @ctxt: the parser context
2053 * @str: the input string
2054 * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
2055 * @end: an end marker xmlChar, 0 if none
2056 * @end2: an end marker xmlChar, 0 if none
2057 * @end3: an end marker xmlChar, 0 if none
2058 *
2059 * Takes a entity string content and process to do the adequate substitutions.
2060 *
2061 * [67] Reference ::= EntityRef | CharRef
2062 *
2063 * [69] PEReference ::= '%' Name ';'
2064 *
2065 * Returns A newly allocated string with the substitution done. The caller
2066 * must deallocate it !
2067 */
2068xmlChar *
2069xmlStringDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int what,
2070 xmlChar end, xmlChar end2, xmlChar end3) {
2071 return(xmlStringLenDecodeEntities(ctxt, str, xmlStrlen(str), what,
2072 end, end2, end3));
2073}
Owen Taylor3473f882001-02-23 17:55:21 +00002074
2075/************************************************************************
2076 * *
Owen Taylor3473f882001-02-23 17:55:21 +00002077 * Commodity functions, cleanup needed ? *
2078 * *
2079 ************************************************************************/
2080
2081/**
2082 * areBlanks:
2083 * @ctxt: an XML parser context
2084 * @str: a xmlChar *
2085 * @len: the size of @str
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002086 * @blank_chars: we know the chars are blanks
Owen Taylor3473f882001-02-23 17:55:21 +00002087 *
2088 * Is this a sequence of blank chars that one can ignore ?
2089 *
2090 * Returns 1 if ignorable 0 otherwise.
2091 */
2092
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002093static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len,
2094 int blank_chars) {
Owen Taylor3473f882001-02-23 17:55:21 +00002095 int i, ret;
2096 xmlNodePtr lastChild;
2097
Daniel Veillard05c13a22001-09-09 08:38:09 +00002098 /*
2099 * Don't spend time trying to differentiate them, the same callback is
2100 * used !
2101 */
2102 if (ctxt->sax->ignorableWhitespace == ctxt->sax->characters)
Daniel Veillard2f362242001-03-02 17:36:21 +00002103 return(0);
2104
Owen Taylor3473f882001-02-23 17:55:21 +00002105 /*
2106 * Check for xml:space value.
2107 */
2108 if (*(ctxt->space) == 1)
2109 return(0);
2110
2111 /*
2112 * Check that the string is made of blanks
2113 */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002114 if (blank_chars == 0) {
2115 for (i = 0;i < len;i++)
2116 if (!(IS_BLANK_CH(str[i]))) return(0);
2117 }
Owen Taylor3473f882001-02-23 17:55:21 +00002118
2119 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002120 * Look if the element is mixed content in the DTD if available
Owen Taylor3473f882001-02-23 17:55:21 +00002121 */
Daniel Veillard6dd398f2001-07-25 22:41:03 +00002122 if (ctxt->node == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002123 if (ctxt->myDoc != NULL) {
2124 ret = xmlIsMixedElement(ctxt->myDoc, ctxt->node->name);
2125 if (ret == 0) return(1);
2126 if (ret == 1) return(0);
2127 }
2128
2129 /*
2130 * Otherwise, heuristic :-\
2131 */
Daniel Veillard561b7f82002-03-20 21:55:57 +00002132 if (RAW != '<') return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002133 if ((ctxt->node->children == NULL) &&
2134 (RAW == '<') && (NXT(1) == '/')) return(0);
2135
2136 lastChild = xmlGetLastChild(ctxt->node);
2137 if (lastChild == NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002138 if ((ctxt->node->type != XML_ELEMENT_NODE) &&
2139 (ctxt->node->content != NULL)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002140 } else if (xmlNodeIsText(lastChild))
2141 return(0);
2142 else if ((ctxt->node->children != NULL) &&
2143 (xmlNodeIsText(ctxt->node->children)))
2144 return(0);
2145 return(1);
2146}
2147
Owen Taylor3473f882001-02-23 17:55:21 +00002148/************************************************************************
2149 * *
2150 * Extra stuff for namespace support *
2151 * Relates to http://www.w3.org/TR/WD-xml-names *
2152 * *
2153 ************************************************************************/
2154
2155/**
2156 * xmlSplitQName:
2157 * @ctxt: an XML parser context
2158 * @name: an XML parser context
2159 * @prefix: a xmlChar **
2160 *
2161 * parse an UTF8 encoded XML qualified name string
2162 *
2163 * [NS 5] QName ::= (Prefix ':')? LocalPart
2164 *
2165 * [NS 6] Prefix ::= NCName
2166 *
2167 * [NS 7] LocalPart ::= NCName
2168 *
2169 * Returns the local part, and prefix is updated
2170 * to get the Prefix if any.
2171 */
2172
2173xmlChar *
2174xmlSplitQName(xmlParserCtxtPtr ctxt, const xmlChar *name, xmlChar **prefix) {
2175 xmlChar buf[XML_MAX_NAMELEN + 5];
2176 xmlChar *buffer = NULL;
2177 int len = 0;
2178 int max = XML_MAX_NAMELEN;
2179 xmlChar *ret = NULL;
2180 const xmlChar *cur = name;
2181 int c;
2182
2183 *prefix = NULL;
2184
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002185 if (cur == NULL) return(NULL);
2186
Owen Taylor3473f882001-02-23 17:55:21 +00002187#ifndef XML_XML_NAMESPACE
2188 /* xml: prefix is not really a namespace */
2189 if ((cur[0] == 'x') && (cur[1] == 'm') &&
2190 (cur[2] == 'l') && (cur[3] == ':'))
2191 return(xmlStrdup(name));
2192#endif
2193
Daniel Veillard597bc482003-07-24 16:08:28 +00002194 /* nasty but well=formed */
Owen Taylor3473f882001-02-23 17:55:21 +00002195 if (cur[0] == ':')
2196 return(xmlStrdup(name));
2197
2198 c = *cur++;
2199 while ((c != 0) && (c != ':') && (len < max)) { /* tested bigname.xml */
2200 buf[len++] = c;
2201 c = *cur++;
2202 }
2203 if (len >= max) {
2204 /*
2205 * Okay someone managed to make a huge name, so he's ready to pay
2206 * for the processing speed.
2207 */
2208 max = len * 2;
2209
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002210 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002211 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002212 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002213 return(NULL);
2214 }
2215 memcpy(buffer, buf, len);
2216 while ((c != 0) && (c != ':')) { /* tested bigname.xml */
2217 if (len + 10 > max) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00002218 xmlChar *tmp;
2219
Owen Taylor3473f882001-02-23 17:55:21 +00002220 max *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00002221 tmp = (xmlChar *) xmlRealloc(buffer,
Owen Taylor3473f882001-02-23 17:55:21 +00002222 max * sizeof(xmlChar));
Daniel Veillard2248ff12004-09-22 23:05:14 +00002223 if (tmp == NULL) {
2224 xmlFree(tmp);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002225 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002226 return(NULL);
2227 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00002228 buffer = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002229 }
2230 buffer[len++] = c;
2231 c = *cur++;
2232 }
2233 buffer[len] = 0;
2234 }
2235
Daniel Veillard597bc482003-07-24 16:08:28 +00002236 /* nasty but well=formed
2237 if ((c == ':') && (*cur == 0)) {
2238 return(xmlStrdup(name));
2239 } */
2240
Owen Taylor3473f882001-02-23 17:55:21 +00002241 if (buffer == NULL)
2242 ret = xmlStrndup(buf, len);
2243 else {
2244 ret = buffer;
2245 buffer = NULL;
2246 max = XML_MAX_NAMELEN;
2247 }
2248
2249
2250 if (c == ':') {
Daniel Veillardbb284f42002-10-16 18:02:47 +00002251 c = *cur;
Owen Taylor3473f882001-02-23 17:55:21 +00002252 *prefix = ret;
Daniel Veillard597bc482003-07-24 16:08:28 +00002253 if (c == 0) {
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00002254 return(xmlStrndup(BAD_CAST "", 0));
Daniel Veillard597bc482003-07-24 16:08:28 +00002255 }
Owen Taylor3473f882001-02-23 17:55:21 +00002256 len = 0;
2257
Daniel Veillardbb284f42002-10-16 18:02:47 +00002258 /*
2259 * Check that the first character is proper to start
2260 * a new name
2261 */
2262 if (!(((c >= 0x61) && (c <= 0x7A)) ||
2263 ((c >= 0x41) && (c <= 0x5A)) ||
2264 (c == '_') || (c == ':'))) {
2265 int l;
2266 int first = CUR_SCHAR(cur, l);
2267
2268 if (!IS_LETTER(first) && (first != '_')) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00002269 xmlFatalErrMsgStr(ctxt, XML_NS_ERR_QNAME,
Daniel Veillardbb284f42002-10-16 18:02:47 +00002270 "Name %s is not XML Namespace compliant\n",
Daniel Veillardbc92eca2003-09-15 09:48:06 +00002271 name);
Daniel Veillardbb284f42002-10-16 18:02:47 +00002272 }
2273 }
2274 cur++;
2275
Owen Taylor3473f882001-02-23 17:55:21 +00002276 while ((c != 0) && (len < max)) { /* tested bigname2.xml */
2277 buf[len++] = c;
2278 c = *cur++;
2279 }
2280 if (len >= max) {
2281 /*
2282 * Okay someone managed to make a huge name, so he's ready to pay
2283 * for the processing speed.
2284 */
2285 max = len * 2;
2286
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002287 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002288 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002289 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002290 return(NULL);
2291 }
2292 memcpy(buffer, buf, len);
2293 while (c != 0) { /* tested bigname2.xml */
2294 if (len + 10 > max) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00002295 xmlChar *tmp;
2296
Owen Taylor3473f882001-02-23 17:55:21 +00002297 max *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00002298 tmp = (xmlChar *) xmlRealloc(buffer,
Owen Taylor3473f882001-02-23 17:55:21 +00002299 max * sizeof(xmlChar));
Daniel Veillard2248ff12004-09-22 23:05:14 +00002300 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002301 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00002302 xmlFree(buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00002303 return(NULL);
2304 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00002305 buffer = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002306 }
2307 buffer[len++] = c;
2308 c = *cur++;
2309 }
2310 buffer[len] = 0;
2311 }
2312
2313 if (buffer == NULL)
2314 ret = xmlStrndup(buf, len);
2315 else {
2316 ret = buffer;
2317 }
2318 }
2319
2320 return(ret);
2321}
2322
2323/************************************************************************
2324 * *
2325 * The parser itself *
2326 * Relates to http://www.w3.org/TR/REC-xml *
2327 * *
2328 ************************************************************************/
2329
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002330static const xmlChar * xmlParseNameComplex(xmlParserCtxtPtr ctxt);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002331static xmlChar * xmlParseAttValueInternal(xmlParserCtxtPtr ctxt,
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002332 int *len, int *alloc, int normalize);
Daniel Veillard0fb18932003-09-07 09:14:37 +00002333
Owen Taylor3473f882001-02-23 17:55:21 +00002334/**
2335 * xmlParseName:
2336 * @ctxt: an XML parser context
2337 *
2338 * parse an XML name.
2339 *
2340 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
2341 * CombiningChar | Extender
2342 *
2343 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
2344 *
Daniel Veillard807b4de2004-09-26 14:42:56 +00002345 * [6] Names ::= Name (#x20 Name)*
Owen Taylor3473f882001-02-23 17:55:21 +00002346 *
2347 * Returns the Name parsed or NULL
2348 */
2349
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002350const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002351xmlParseName(xmlParserCtxtPtr ctxt) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00002352 const xmlChar *in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002353 const xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00002354 int count = 0;
2355
2356 GROW;
Daniel Veillard48b2f892001-02-25 16:11:03 +00002357
2358 /*
2359 * Accelerator for simple ASCII names
2360 */
2361 in = ctxt->input->cur;
2362 if (((*in >= 0x61) && (*in <= 0x7A)) ||
2363 ((*in >= 0x41) && (*in <= 0x5A)) ||
2364 (*in == '_') || (*in == ':')) {
2365 in++;
2366 while (((*in >= 0x61) && (*in <= 0x7A)) ||
2367 ((*in >= 0x41) && (*in <= 0x5A)) ||
2368 ((*in >= 0x30) && (*in <= 0x39)) ||
Daniel Veillard76d66f42001-05-16 21:05:17 +00002369 (*in == '_') || (*in == '-') ||
2370 (*in == ':') || (*in == '.'))
Daniel Veillard48b2f892001-02-25 16:11:03 +00002371 in++;
Daniel Veillard76d66f42001-05-16 21:05:17 +00002372 if ((*in > 0) && (*in < 0x80)) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00002373 count = in - ctxt->input->cur;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002374 ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002375 ctxt->input->cur = in;
Daniel Veillard77a90a72003-03-22 00:04:05 +00002376 ctxt->nbChars += count;
2377 ctxt->input->col += count;
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002378 if (ret == NULL)
2379 xmlErrMemory(ctxt, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002380 return(ret);
2381 }
2382 }
Daniel Veillard2f362242001-03-02 17:36:21 +00002383 return(xmlParseNameComplex(ctxt));
Daniel Veillard21a0f912001-02-25 19:54:14 +00002384}
Daniel Veillard48b2f892001-02-25 16:11:03 +00002385
Daniel Veillard46de64e2002-05-29 08:21:33 +00002386/**
2387 * xmlParseNameAndCompare:
2388 * @ctxt: an XML parser context
2389 *
2390 * parse an XML name and compares for match
2391 * (specialized for endtag parsing)
2392 *
Daniel Veillard46de64e2002-05-29 08:21:33 +00002393 * Returns NULL for an illegal name, (xmlChar*) 1 for success
2394 * and the name for mismatch
2395 */
2396
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002397static const xmlChar *
Daniel Veillard46de64e2002-05-29 08:21:33 +00002398xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002399 register const xmlChar *cmp = other;
2400 register const xmlChar *in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002401 const xmlChar *ret;
Daniel Veillard46de64e2002-05-29 08:21:33 +00002402
2403 GROW;
2404
2405 in = ctxt->input->cur;
2406 while (*in != 0 && *in == *cmp) {
2407 ++in;
2408 ++cmp;
2409 }
William M. Brack76e95df2003-10-18 16:20:14 +00002410 if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
Daniel Veillard46de64e2002-05-29 08:21:33 +00002411 /* success */
2412 ctxt->input->cur = in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002413 return (const xmlChar*) 1;
Daniel Veillard46de64e2002-05-29 08:21:33 +00002414 }
2415 /* failure (or end of input buffer), check with full function */
2416 ret = xmlParseName (ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00002417 /* strings coming from the dictionnary direct compare possible */
2418 if (ret == other) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002419 return (const xmlChar*) 1;
Daniel Veillard46de64e2002-05-29 08:21:33 +00002420 }
2421 return ret;
2422}
2423
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002424static const xmlChar *
Daniel Veillard21a0f912001-02-25 19:54:14 +00002425xmlParseNameComplex(xmlParserCtxtPtr ctxt) {
Daniel Veillard21a0f912001-02-25 19:54:14 +00002426 int len = 0, l;
2427 int c;
2428 int count = 0;
2429
2430 /*
2431 * Handler for more complex cases
2432 */
2433 GROW;
Owen Taylor3473f882001-02-23 17:55:21 +00002434 c = CUR_CHAR(l);
2435 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
2436 (!IS_LETTER(c) && (c != '_') &&
2437 (c != ':'))) {
2438 return(NULL);
2439 }
2440
2441 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
William M. Brack871611b2003-10-18 04:53:14 +00002442 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
Owen Taylor3473f882001-02-23 17:55:21 +00002443 (c == '.') || (c == '-') ||
2444 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002445 (IS_COMBINING(c)) ||
2446 (IS_EXTENDER(c)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002447 if (count++ > 100) {
2448 count = 0;
2449 GROW;
2450 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002451 len += l;
Owen Taylor3473f882001-02-23 17:55:21 +00002452 NEXTL(l);
2453 c = CUR_CHAR(l);
Owen Taylor3473f882001-02-23 17:55:21 +00002454 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002455 return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len));
Owen Taylor3473f882001-02-23 17:55:21 +00002456}
2457
2458/**
2459 * xmlParseStringName:
2460 * @ctxt: an XML parser context
2461 * @str: a pointer to the string pointer (IN/OUT)
2462 *
2463 * parse an XML name.
2464 *
2465 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
2466 * CombiningChar | Extender
2467 *
2468 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
2469 *
Daniel Veillard807b4de2004-09-26 14:42:56 +00002470 * [6] Names ::= Name (#x20 Name)*
Owen Taylor3473f882001-02-23 17:55:21 +00002471 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002472 * Returns the Name parsed or NULL. The @str pointer
Owen Taylor3473f882001-02-23 17:55:21 +00002473 * is updated to the current location in the string.
2474 */
2475
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002476static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002477xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) {
2478 xmlChar buf[XML_MAX_NAMELEN + 5];
2479 const xmlChar *cur = *str;
2480 int len = 0, l;
2481 int c;
2482
2483 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +00002484 if (!IS_LETTER(c) && (c != '_') &&
Owen Taylor3473f882001-02-23 17:55:21 +00002485 (c != ':')) {
2486 return(NULL);
2487 }
2488
William M. Brack871611b2003-10-18 04:53:14 +00002489 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigentname.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002490 (c == '.') || (c == '-') ||
2491 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002492 (IS_COMBINING(c)) ||
2493 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002494 COPY_BUF(l,buf,len,c);
2495 cur += l;
2496 c = CUR_SCHAR(cur, l);
2497 if (len >= XML_MAX_NAMELEN) { /* test bigentname.xml */
2498 /*
2499 * Okay someone managed to make a huge name, so he's ready to pay
2500 * for the processing speed.
2501 */
2502 xmlChar *buffer;
2503 int max = len * 2;
2504
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002505 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002506 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002507 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002508 return(NULL);
2509 }
2510 memcpy(buffer, buf, len);
William M. Brack871611b2003-10-18 04:53:14 +00002511 while ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
Daniel Veillard73b013f2003-09-30 12:36:01 +00002512 /* test bigentname.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002513 (c == '.') || (c == '-') ||
2514 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002515 (IS_COMBINING(c)) ||
2516 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002517 if (len + 10 > max) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00002518 xmlChar *tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002519 max *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00002520 tmp = (xmlChar *) xmlRealloc(buffer,
Owen Taylor3473f882001-02-23 17:55:21 +00002521 max * sizeof(xmlChar));
Daniel Veillard2248ff12004-09-22 23:05:14 +00002522 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002523 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00002524 xmlFree(buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00002525 return(NULL);
2526 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00002527 buffer = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002528 }
2529 COPY_BUF(l,buffer,len,c);
2530 cur += l;
2531 c = CUR_SCHAR(cur, l);
2532 }
2533 buffer[len] = 0;
2534 *str = cur;
2535 return(buffer);
2536 }
2537 }
2538 *str = cur;
2539 return(xmlStrndup(buf, len));
2540}
2541
2542/**
2543 * xmlParseNmtoken:
2544 * @ctxt: an XML parser context
2545 *
2546 * parse an XML Nmtoken.
2547 *
2548 * [7] Nmtoken ::= (NameChar)+
2549 *
Daniel Veillard807b4de2004-09-26 14:42:56 +00002550 * [8] Nmtokens ::= Nmtoken (#x20 Nmtoken)*
Owen Taylor3473f882001-02-23 17:55:21 +00002551 *
2552 * Returns the Nmtoken parsed or NULL
2553 */
2554
2555xmlChar *
2556xmlParseNmtoken(xmlParserCtxtPtr ctxt) {
2557 xmlChar buf[XML_MAX_NAMELEN + 5];
2558 int len = 0, l;
2559 int c;
2560 int count = 0;
2561
2562 GROW;
2563 c = CUR_CHAR(l);
2564
William M. Brack871611b2003-10-18 04:53:14 +00002565 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigtoken.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002566 (c == '.') || (c == '-') ||
2567 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002568 (IS_COMBINING(c)) ||
2569 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002570 if (count++ > 100) {
2571 count = 0;
2572 GROW;
2573 }
2574 COPY_BUF(l,buf,len,c);
2575 NEXTL(l);
2576 c = CUR_CHAR(l);
2577 if (len >= XML_MAX_NAMELEN) {
2578 /*
2579 * Okay someone managed to make a huge token, so he's ready to pay
2580 * for the processing speed.
2581 */
2582 xmlChar *buffer;
2583 int max = len * 2;
2584
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002585 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002586 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002587 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002588 return(NULL);
2589 }
2590 memcpy(buffer, buf, len);
William M. Brack871611b2003-10-18 04:53:14 +00002591 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigtoken.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002592 (c == '.') || (c == '-') ||
2593 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002594 (IS_COMBINING(c)) ||
2595 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002596 if (count++ > 100) {
2597 count = 0;
2598 GROW;
2599 }
2600 if (len + 10 > max) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00002601 xmlChar *tmp;
2602
Owen Taylor3473f882001-02-23 17:55:21 +00002603 max *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00002604 tmp = (xmlChar *) xmlRealloc(buffer,
Owen Taylor3473f882001-02-23 17:55:21 +00002605 max * sizeof(xmlChar));
Daniel Veillard2248ff12004-09-22 23:05:14 +00002606 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002607 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00002608 xmlFree(buffer);
Owen Taylor3473f882001-02-23 17:55:21 +00002609 return(NULL);
2610 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00002611 buffer = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002612 }
2613 COPY_BUF(l,buffer,len,c);
2614 NEXTL(l);
2615 c = CUR_CHAR(l);
2616 }
2617 buffer[len] = 0;
2618 return(buffer);
2619 }
2620 }
2621 if (len == 0)
2622 return(NULL);
2623 return(xmlStrndup(buf, len));
2624}
2625
2626/**
2627 * xmlParseEntityValue:
2628 * @ctxt: an XML parser context
2629 * @orig: if non-NULL store a copy of the original entity value
2630 *
2631 * parse a value for ENTITY declarations
2632 *
2633 * [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' |
2634 * "'" ([^%&'] | PEReference | Reference)* "'"
2635 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002636 * Returns the EntityValue parsed with reference substituted or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002637 */
2638
2639xmlChar *
2640xmlParseEntityValue(xmlParserCtxtPtr ctxt, xmlChar **orig) {
2641 xmlChar *buf = NULL;
2642 int len = 0;
2643 int size = XML_PARSER_BUFFER_SIZE;
2644 int c, l;
2645 xmlChar stop;
2646 xmlChar *ret = NULL;
2647 const xmlChar *cur = NULL;
2648 xmlParserInputPtr input;
2649
2650 if (RAW == '"') stop = '"';
2651 else if (RAW == '\'') stop = '\'';
2652 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002653 xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002654 return(NULL);
2655 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002656 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002657 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002658 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002659 return(NULL);
2660 }
2661
2662 /*
2663 * The content of the entity definition is copied in a buffer.
2664 */
2665
2666 ctxt->instate = XML_PARSER_ENTITY_VALUE;
2667 input = ctxt->input;
2668 GROW;
2669 NEXT;
2670 c = CUR_CHAR(l);
2671 /*
2672 * NOTE: 4.4.5 Included in Literal
2673 * When a parameter entity reference appears in a literal entity
2674 * value, ... a single or double quote character in the replacement
2675 * text is always treated as a normal data character and will not
2676 * terminate the literal.
2677 * In practice it means we stop the loop only when back at parsing
2678 * the initial entity and the quote is found
2679 */
William M. Brack871611b2003-10-18 04:53:14 +00002680 while ((IS_CHAR(c)) && ((c != stop) || /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00002681 (ctxt->input != input))) {
2682 if (len + 5 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00002683 xmlChar *tmp;
2684
Owen Taylor3473f882001-02-23 17:55:21 +00002685 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00002686 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
2687 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002688 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00002689 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00002690 return(NULL);
2691 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00002692 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00002693 }
2694 COPY_BUF(l,buf,len,c);
2695 NEXTL(l);
2696 /*
2697 * Pop-up of finished entities.
2698 */
2699 while ((RAW == 0) && (ctxt->inputNr > 1)) /* non input consuming */
2700 xmlPopInput(ctxt);
2701
2702 GROW;
2703 c = CUR_CHAR(l);
2704 if (c == 0) {
2705 GROW;
2706 c = CUR_CHAR(l);
2707 }
2708 }
2709 buf[len] = 0;
2710
2711 /*
2712 * Raise problem w.r.t. '&' and '%' being used in non-entities
2713 * reference constructs. Note Charref will be handled in
2714 * xmlStringDecodeEntities()
2715 */
2716 cur = buf;
2717 while (*cur != 0) { /* non input consuming */
2718 if ((*cur == '%') || ((*cur == '&') && (cur[1] != '#'))) {
2719 xmlChar *name;
2720 xmlChar tmp = *cur;
2721
2722 cur++;
2723 name = xmlParseStringName(ctxt, &cur);
2724 if ((name == NULL) || (*cur != ';')) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00002725 xmlFatalErrMsgInt(ctxt, XML_ERR_ENTITY_CHAR_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00002726 "EntityValue: '%c' forbidden except for entities references\n",
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00002727 tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002728 }
Daniel Veillard5151c062001-10-23 13:10:19 +00002729 if ((tmp == '%') && (ctxt->inSubset == 1) &&
2730 (ctxt->inputNr == 1)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002731 xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002732 }
2733 if (name != NULL)
2734 xmlFree(name);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002735 if (*cur == 0)
2736 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002737 }
2738 cur++;
2739 }
2740
2741 /*
2742 * Then PEReference entities are substituted.
2743 */
2744 if (c != stop) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002745 xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002746 xmlFree(buf);
2747 } else {
2748 NEXT;
2749 /*
2750 * NOTE: 4.4.7 Bypassed
2751 * When a general entity reference appears in the EntityValue in
2752 * an entity declaration, it is bypassed and left as is.
2753 * so XML_SUBSTITUTE_REF is not set here.
2754 */
2755 ret = xmlStringDecodeEntities(ctxt, buf, XML_SUBSTITUTE_PEREF,
2756 0, 0, 0);
2757 if (orig != NULL)
2758 *orig = buf;
2759 else
2760 xmlFree(buf);
2761 }
2762
2763 return(ret);
2764}
2765
2766/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002767 * xmlParseAttValueComplex:
2768 * @ctxt: an XML parser context
Daniel Veillard0fb18932003-09-07 09:14:37 +00002769 * @len: the resulting attribute len
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002770 * @normalize: wether to apply the inner normalization
Daniel Veillard01c13b52002-12-10 15:19:08 +00002771 *
2772 * parse a value for an attribute, this is the fallback function
2773 * of xmlParseAttValue() when the attribute parsing requires handling
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002774 * of non-ASCII characters, or normalization compaction.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002775 *
2776 * Returns the AttValue parsed or NULL. The value has to be freed by the caller.
2777 */
Daniel Veillard0fb18932003-09-07 09:14:37 +00002778static xmlChar *
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002779xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) {
Daniel Veillarde72c7562002-05-31 09:47:30 +00002780 xmlChar limit = 0;
2781 xmlChar *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002782 int len = 0;
2783 int buf_size = 0;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002784 int c, l, in_space = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002785 xmlChar *current = NULL;
2786 xmlEntityPtr ent;
2787
Owen Taylor3473f882001-02-23 17:55:21 +00002788 if (NXT(0) == '"') {
2789 ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
2790 limit = '"';
2791 NEXT;
2792 } else if (NXT(0) == '\'') {
2793 limit = '\'';
2794 ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
2795 NEXT;
2796 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002797 xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002798 return(NULL);
2799 }
2800
2801 /*
2802 * allocate a translation buffer.
2803 */
2804 buf_size = XML_PARSER_BUFFER_SIZE;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002805 buf = (xmlChar *) xmlMallocAtomic(buf_size * sizeof(xmlChar));
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002806 if (buf == NULL) goto mem_error;
Owen Taylor3473f882001-02-23 17:55:21 +00002807
2808 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002809 * OK loop until we reach one of the ending char or a size limit.
Owen Taylor3473f882001-02-23 17:55:21 +00002810 */
2811 c = CUR_CHAR(l);
Daniel Veillardfdc91562002-07-01 21:52:03 +00002812 while ((NXT(0) != limit) && /* checked */
2813 (c != '<')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002814 if (c == 0) break;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002815 if (c == '&') {
Daniel Veillard62998c02003-09-15 12:56:36 +00002816 in_space = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002817 if (NXT(1) == '#') {
2818 int val = xmlParseCharRef(ctxt);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002819
Owen Taylor3473f882001-02-23 17:55:21 +00002820 if (val == '&') {
Daniel Veillard319a7422001-09-11 09:27:09 +00002821 if (ctxt->replaceEntities) {
2822 if (len > buf_size - 10) {
2823 growBuffer(buf);
2824 }
2825 buf[len++] = '&';
2826 } else {
2827 /*
2828 * The reparsing will be done in xmlStringGetNodeList()
2829 * called by the attribute() function in SAX.c
2830 */
Daniel Veillard319a7422001-09-11 09:27:09 +00002831 if (len > buf_size - 10) {
2832 growBuffer(buf);
2833 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002834 buf[len++] = '&';
2835 buf[len++] = '#';
2836 buf[len++] = '3';
2837 buf[len++] = '8';
2838 buf[len++] = ';';
Owen Taylor3473f882001-02-23 17:55:21 +00002839 }
2840 } else {
Daniel Veillard0b6b55b2001-03-20 11:27:34 +00002841 if (len > buf_size - 10) {
2842 growBuffer(buf);
2843 }
Owen Taylor3473f882001-02-23 17:55:21 +00002844 len += xmlCopyChar(0, &buf[len], val);
2845 }
2846 } else {
2847 ent = xmlParseEntityRef(ctxt);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002848 if ((ent != NULL) &&
2849 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
2850 if (len > buf_size - 10) {
2851 growBuffer(buf);
2852 }
2853 if ((ctxt->replaceEntities == 0) &&
2854 (ent->content[0] == '&')) {
2855 buf[len++] = '&';
2856 buf[len++] = '#';
2857 buf[len++] = '3';
2858 buf[len++] = '8';
2859 buf[len++] = ';';
2860 } else {
2861 buf[len++] = ent->content[0];
2862 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002863 } else if ((ent != NULL) &&
2864 (ctxt->replaceEntities != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002865 xmlChar *rep;
2866
2867 if (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) {
2868 rep = xmlStringDecodeEntities(ctxt, ent->content,
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002869 XML_SUBSTITUTE_REF,
2870 0, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002871 if (rep != NULL) {
2872 current = rep;
2873 while (*current != 0) { /* non input consuming */
2874 buf[len++] = *current++;
2875 if (len > buf_size - 10) {
2876 growBuffer(buf);
2877 }
2878 }
2879 xmlFree(rep);
2880 }
2881 } else {
Daniel Veillard0b6b55b2001-03-20 11:27:34 +00002882 if (len > buf_size - 10) {
2883 growBuffer(buf);
2884 }
Owen Taylor3473f882001-02-23 17:55:21 +00002885 if (ent->content != NULL)
2886 buf[len++] = ent->content[0];
2887 }
2888 } else if (ent != NULL) {
2889 int i = xmlStrlen(ent->name);
2890 const xmlChar *cur = ent->name;
2891
2892 /*
2893 * This may look absurd but is needed to detect
2894 * entities problems
2895 */
2896 if ((ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) &&
2897 (ent->content != NULL)) {
2898 xmlChar *rep;
2899 rep = xmlStringDecodeEntities(ctxt, ent->content,
2900 XML_SUBSTITUTE_REF, 0, 0, 0);
2901 if (rep != NULL)
2902 xmlFree(rep);
2903 }
2904
2905 /*
2906 * Just output the reference
2907 */
2908 buf[len++] = '&';
2909 if (len > buf_size - i - 10) {
2910 growBuffer(buf);
2911 }
2912 for (;i > 0;i--)
2913 buf[len++] = *cur++;
2914 buf[len++] = ';';
2915 }
2916 }
2917 } else {
2918 if ((c == 0x20) || (c == 0xD) || (c == 0xA) || (c == 0x9)) {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002919 if ((len != 0) || (!normalize)) {
2920 if ((!normalize) || (!in_space)) {
2921 COPY_BUF(l,buf,len,0x20);
2922 if (len > buf_size - 10) {
2923 growBuffer(buf);
2924 }
2925 }
2926 in_space = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002927 }
2928 } else {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002929 in_space = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002930 COPY_BUF(l,buf,len,c);
2931 if (len > buf_size - 10) {
2932 growBuffer(buf);
2933 }
2934 }
2935 NEXTL(l);
2936 }
2937 GROW;
2938 c = CUR_CHAR(l);
2939 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002940 if ((in_space) && (normalize)) {
2941 while (buf[len - 1] == 0x20) len--;
2942 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002943 buf[len] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002944 if (RAW == '<') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002945 xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002946 } else if (RAW != limit) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00002947 xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
2948 "AttValue: ' expected\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002949 } else
2950 NEXT;
Daniel Veillard0fb18932003-09-07 09:14:37 +00002951 if (attlen != NULL) *attlen = len;
Owen Taylor3473f882001-02-23 17:55:21 +00002952 return(buf);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002953
2954mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002955 xmlErrMemory(ctxt, NULL);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002956 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002957}
2958
2959/**
Daniel Veillard0fb18932003-09-07 09:14:37 +00002960 * xmlParseAttValue:
2961 * @ctxt: an XML parser context
2962 *
2963 * parse a value for an attribute
2964 * Note: the parser won't do substitution of entities here, this
2965 * will be handled later in xmlStringGetNodeList
2966 *
2967 * [10] AttValue ::= '"' ([^<&"] | Reference)* '"' |
2968 * "'" ([^<&'] | Reference)* "'"
2969 *
2970 * 3.3.3 Attribute-Value Normalization:
2971 * Before the value of an attribute is passed to the application or
2972 * checked for validity, the XML processor must normalize it as follows:
2973 * - a character reference is processed by appending the referenced
2974 * character to the attribute value
2975 * - an entity reference is processed by recursively processing the
2976 * replacement text of the entity
2977 * - a whitespace character (#x20, #xD, #xA, #x9) is processed by
2978 * appending #x20 to the normalized value, except that only a single
2979 * #x20 is appended for a "#xD#xA" sequence that is part of an external
2980 * parsed entity or the literal entity value of an internal parsed entity
2981 * - other characters are processed by appending them to the normalized value
2982 * If the declared value is not CDATA, then the XML processor must further
2983 * process the normalized attribute value by discarding any leading and
2984 * trailing space (#x20) characters, and by replacing sequences of space
2985 * (#x20) characters by a single space (#x20) character.
2986 * All attributes for which no declaration has been read should be treated
2987 * by a non-validating parser as if declared CDATA.
2988 *
2989 * Returns the AttValue parsed or NULL. The value has to be freed by the caller.
2990 */
2991
2992
2993xmlChar *
2994xmlParseAttValue(xmlParserCtxtPtr ctxt) {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002995 return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0));
Daniel Veillard0fb18932003-09-07 09:14:37 +00002996}
2997
2998/**
Owen Taylor3473f882001-02-23 17:55:21 +00002999 * xmlParseSystemLiteral:
3000 * @ctxt: an XML parser context
3001 *
3002 * parse an XML Literal
3003 *
3004 * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
3005 *
3006 * Returns the SystemLiteral parsed or NULL
3007 */
3008
3009xmlChar *
3010xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) {
3011 xmlChar *buf = NULL;
3012 int len = 0;
3013 int size = XML_PARSER_BUFFER_SIZE;
3014 int cur, l;
3015 xmlChar stop;
3016 int state = ctxt->instate;
3017 int count = 0;
3018
3019 SHRINK;
3020 if (RAW == '"') {
3021 NEXT;
3022 stop = '"';
3023 } else if (RAW == '\'') {
3024 NEXT;
3025 stop = '\'';
3026 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003027 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003028 return(NULL);
3029 }
3030
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003031 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003032 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003033 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003034 return(NULL);
3035 }
3036 ctxt->instate = XML_PARSER_SYSTEM_LITERAL;
3037 cur = CUR_CHAR(l);
William M. Brack871611b2003-10-18 04:53:14 +00003038 while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003039 if (len + 5 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00003040 xmlChar *tmp;
3041
Owen Taylor3473f882001-02-23 17:55:21 +00003042 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00003043 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3044 if (tmp == NULL) {
3045 xmlFree(buf);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003046 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003047 ctxt->instate = (xmlParserInputState) state;
3048 return(NULL);
3049 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00003050 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00003051 }
3052 count++;
3053 if (count > 50) {
3054 GROW;
3055 count = 0;
3056 }
3057 COPY_BUF(l,buf,len,cur);
3058 NEXTL(l);
3059 cur = CUR_CHAR(l);
3060 if (cur == 0) {
3061 GROW;
3062 SHRINK;
3063 cur = CUR_CHAR(l);
3064 }
3065 }
3066 buf[len] = 0;
3067 ctxt->instate = (xmlParserInputState) state;
William M. Brack871611b2003-10-18 04:53:14 +00003068 if (!IS_CHAR(cur)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003069 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003070 } else {
3071 NEXT;
3072 }
3073 return(buf);
3074}
3075
3076/**
3077 * xmlParsePubidLiteral:
3078 * @ctxt: an XML parser context
3079 *
3080 * parse an XML public literal
3081 *
3082 * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
3083 *
3084 * Returns the PubidLiteral parsed or NULL.
3085 */
3086
3087xmlChar *
3088xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) {
3089 xmlChar *buf = NULL;
3090 int len = 0;
3091 int size = XML_PARSER_BUFFER_SIZE;
3092 xmlChar cur;
3093 xmlChar stop;
3094 int count = 0;
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003095 xmlParserInputState oldstate = ctxt->instate;
Owen Taylor3473f882001-02-23 17:55:21 +00003096
3097 SHRINK;
3098 if (RAW == '"') {
3099 NEXT;
3100 stop = '"';
3101 } else if (RAW == '\'') {
3102 NEXT;
3103 stop = '\'';
3104 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003105 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003106 return(NULL);
3107 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003108 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003109 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003110 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003111 return(NULL);
3112 }
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003113 ctxt->instate = XML_PARSER_PUBLIC_LITERAL;
Owen Taylor3473f882001-02-23 17:55:21 +00003114 cur = CUR;
William M. Brack76e95df2003-10-18 16:20:14 +00003115 while ((IS_PUBIDCHAR_CH(cur)) && (cur != stop)) { /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003116 if (len + 1 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00003117 xmlChar *tmp;
3118
Owen Taylor3473f882001-02-23 17:55:21 +00003119 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00003120 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3121 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003122 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00003123 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003124 return(NULL);
3125 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00003126 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00003127 }
3128 buf[len++] = cur;
3129 count++;
3130 if (count > 50) {
3131 GROW;
3132 count = 0;
3133 }
3134 NEXT;
3135 cur = CUR;
3136 if (cur == 0) {
3137 GROW;
3138 SHRINK;
3139 cur = CUR;
3140 }
3141 }
3142 buf[len] = 0;
3143 if (cur != stop) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003144 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003145 } else {
3146 NEXT;
3147 }
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003148 ctxt->instate = oldstate;
Owen Taylor3473f882001-02-23 17:55:21 +00003149 return(buf);
3150}
3151
Daniel Veillard48b2f892001-02-25 16:11:03 +00003152void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata);
Owen Taylor3473f882001-02-23 17:55:21 +00003153/**
3154 * xmlParseCharData:
3155 * @ctxt: an XML parser context
3156 * @cdata: int indicating whether we are within a CDATA section
3157 *
3158 * parse a CharData section.
3159 * if we are within a CDATA section ']]>' marks an end of section.
3160 *
3161 * The right angle bracket (>) may be represented using the string "&gt;",
3162 * and must, for compatibility, be escaped using "&gt;" or a character
3163 * reference when it appears in the string "]]>" in content, when that
3164 * string is not marking the end of a CDATA section.
3165 *
3166 * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
3167 */
3168
3169void
3170xmlParseCharData(xmlParserCtxtPtr ctxt, int cdata) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00003171 const xmlChar *in;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003172 int nbchar = 0;
Daniel Veillard50582112001-03-26 22:52:16 +00003173 int line = ctxt->input->line;
3174 int col = ctxt->input->col;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003175
3176 SHRINK;
3177 GROW;
3178 /*
3179 * Accelerated common case where input don't need to be
3180 * modified before passing it to the handler.
3181 */
Daniel Veillardfdc91562002-07-01 21:52:03 +00003182 if (!cdata) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00003183 in = ctxt->input->cur;
3184 do {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003185get_more_space:
3186 while (*in == 0x20) in++;
3187 if (*in == 0xA) {
3188 ctxt->input->line++;
3189 in++;
3190 while (*in == 0xA) {
3191 ctxt->input->line++;
3192 in++;
3193 }
3194 goto get_more_space;
3195 }
3196 if (*in == '<') {
3197 nbchar = in - ctxt->input->cur;
3198 if (nbchar > 0) {
3199 const xmlChar *tmp = ctxt->input->cur;
3200 ctxt->input->cur = in;
3201
3202 if (ctxt->sax->ignorableWhitespace !=
3203 ctxt->sax->characters) {
3204 if (areBlanks(ctxt, tmp, nbchar, 1)) {
3205 ctxt->sax->ignorableWhitespace(ctxt->userData,
3206 tmp, nbchar);
3207 } else if (ctxt->sax->characters != NULL)
3208 ctxt->sax->characters(ctxt->userData,
3209 tmp, nbchar);
3210 } else if (ctxt->sax->characters != NULL) {
3211 ctxt->sax->characters(ctxt->userData,
3212 tmp, nbchar);
3213 }
3214 }
3215 return;
3216 }
Daniel Veillard3ed155f2001-04-29 19:56:59 +00003217get_more:
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003218 while (((*in > ']') && (*in <= 0x7F)) ||
3219 ((*in > '&') && (*in < '<')) ||
3220 ((*in > '<') && (*in < ']')) ||
3221 ((*in >= 0x20) && (*in < '&')) ||
3222 (*in == 0x09))
3223 in++;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003224 if (*in == 0xA) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00003225 ctxt->input->line++;
Daniel Veillard3ed155f2001-04-29 19:56:59 +00003226 in++;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003227 while (*in == 0xA) {
Daniel Veillard3ed155f2001-04-29 19:56:59 +00003228 ctxt->input->line++;
3229 in++;
3230 }
3231 goto get_more;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003232 }
3233 if (*in == ']') {
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003234 if ((in[1] == ']') && (in[2] == '>')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003235 xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003236 ctxt->input->cur = in;
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003237 return;
3238 }
3239 in++;
3240 goto get_more;
3241 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00003242 nbchar = in - ctxt->input->cur;
Daniel Veillard80f32572001-03-07 19:45:40 +00003243 if (nbchar > 0) {
Daniel Veillard40412cd2003-09-03 13:28:32 +00003244 if ((ctxt->sax->ignorableWhitespace !=
3245 ctxt->sax->characters) &&
William M. Brack76e95df2003-10-18 16:20:14 +00003246 (IS_BLANK_CH(*ctxt->input->cur))) {
Daniel Veillarda7374592001-05-10 14:17:55 +00003247 const xmlChar *tmp = ctxt->input->cur;
3248 ctxt->input->cur = in;
Daniel Veillard40412cd2003-09-03 13:28:32 +00003249
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003250 if (areBlanks(ctxt, tmp, nbchar, 0)) {
Daniel Veillard40412cd2003-09-03 13:28:32 +00003251 ctxt->sax->ignorableWhitespace(ctxt->userData,
3252 tmp, nbchar);
3253 } else if (ctxt->sax->characters != NULL)
3254 ctxt->sax->characters(ctxt->userData,
3255 tmp, nbchar);
Daniel Veillard3ed27bd2001-06-17 17:58:17 +00003256 line = ctxt->input->line;
3257 col = ctxt->input->col;
Daniel Veillard80f32572001-03-07 19:45:40 +00003258 } else {
3259 if (ctxt->sax->characters != NULL)
3260 ctxt->sax->characters(ctxt->userData,
3261 ctxt->input->cur, nbchar);
Daniel Veillard3ed27bd2001-06-17 17:58:17 +00003262 line = ctxt->input->line;
3263 col = ctxt->input->col;
Daniel Veillard80f32572001-03-07 19:45:40 +00003264 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00003265 }
3266 ctxt->input->cur = in;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003267 if (*in == 0xD) {
3268 in++;
3269 if (*in == 0xA) {
3270 ctxt->input->cur = in;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003271 in++;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003272 ctxt->input->line++;
3273 continue; /* while */
Daniel Veillard48b2f892001-02-25 16:11:03 +00003274 }
Daniel Veillard561b7f82002-03-20 21:55:57 +00003275 in--;
3276 }
3277 if (*in == '<') {
3278 return;
3279 }
3280 if (*in == '&') {
3281 return;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003282 }
3283 SHRINK;
3284 GROW;
3285 in = ctxt->input->cur;
William M. Brackc07329e2003-09-08 01:57:30 +00003286 } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09));
Daniel Veillard48b2f892001-02-25 16:11:03 +00003287 nbchar = 0;
3288 }
Daniel Veillard50582112001-03-26 22:52:16 +00003289 ctxt->input->line = line;
3290 ctxt->input->col = col;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003291 xmlParseCharDataComplex(ctxt, cdata);
3292}
3293
Daniel Veillard01c13b52002-12-10 15:19:08 +00003294/**
3295 * xmlParseCharDataComplex:
3296 * @ctxt: an XML parser context
3297 * @cdata: int indicating whether we are within a CDATA section
3298 *
3299 * parse a CharData section.this is the fallback function
3300 * of xmlParseCharData() when the parsing requires handling
3301 * of non-ASCII characters.
3302 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00003303void
3304xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata) {
Owen Taylor3473f882001-02-23 17:55:21 +00003305 xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5];
3306 int nbchar = 0;
3307 int cur, l;
3308 int count = 0;
3309
3310 SHRINK;
3311 GROW;
3312 cur = CUR_CHAR(l);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003313 while ((cur != '<') && /* checked */
3314 (cur != '&') &&
William M. Brack871611b2003-10-18 04:53:14 +00003315 (IS_CHAR(cur))) /* test also done in xmlCurrentChar() */ {
Owen Taylor3473f882001-02-23 17:55:21 +00003316 if ((cur == ']') && (NXT(1) == ']') &&
3317 (NXT(2) == '>')) {
3318 if (cdata) break;
3319 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003320 xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003321 }
3322 }
3323 COPY_BUF(l,buf,nbchar,cur);
3324 if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) {
Daniel Veillard092643b2003-09-25 14:29:29 +00003325 buf[nbchar] = 0;
3326
Owen Taylor3473f882001-02-23 17:55:21 +00003327 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003328 * OK the segment is to be consumed as chars.
Owen Taylor3473f882001-02-23 17:55:21 +00003329 */
3330 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003331 if (areBlanks(ctxt, buf, nbchar, 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003332 if (ctxt->sax->ignorableWhitespace != NULL)
3333 ctxt->sax->ignorableWhitespace(ctxt->userData,
3334 buf, nbchar);
3335 } else {
3336 if (ctxt->sax->characters != NULL)
3337 ctxt->sax->characters(ctxt->userData, buf, nbchar);
3338 }
3339 }
3340 nbchar = 0;
3341 }
3342 count++;
3343 if (count > 50) {
3344 GROW;
3345 count = 0;
3346 }
3347 NEXTL(l);
3348 cur = CUR_CHAR(l);
3349 }
3350 if (nbchar != 0) {
Daniel Veillard092643b2003-09-25 14:29:29 +00003351 buf[nbchar] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003352 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003353 * OK the segment is to be consumed as chars.
Owen Taylor3473f882001-02-23 17:55:21 +00003354 */
3355 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003356 if (areBlanks(ctxt, buf, nbchar, 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003357 if (ctxt->sax->ignorableWhitespace != NULL)
3358 ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar);
3359 } else {
3360 if (ctxt->sax->characters != NULL)
3361 ctxt->sax->characters(ctxt->userData, buf, nbchar);
3362 }
3363 }
3364 }
3365}
3366
3367/**
3368 * xmlParseExternalID:
3369 * @ctxt: an XML parser context
3370 * @publicID: a xmlChar** receiving PubidLiteral
3371 * @strict: indicate whether we should restrict parsing to only
3372 * production [75], see NOTE below
3373 *
3374 * Parse an External ID or a Public ID
3375 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003376 * NOTE: Productions [75] and [83] interact badly since [75] can generate
Owen Taylor3473f882001-02-23 17:55:21 +00003377 * 'PUBLIC' S PubidLiteral S SystemLiteral
3378 *
3379 * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
3380 * | 'PUBLIC' S PubidLiteral S SystemLiteral
3381 *
3382 * [83] PublicID ::= 'PUBLIC' S PubidLiteral
3383 *
3384 * Returns the function returns SystemLiteral and in the second
3385 * case publicID receives PubidLiteral, is strict is off
3386 * it is possible to return NULL and have publicID set.
3387 */
3388
3389xmlChar *
3390xmlParseExternalID(xmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) {
3391 xmlChar *URI = NULL;
3392
3393 SHRINK;
Daniel Veillard146c9122001-03-22 15:22:27 +00003394
3395 *publicID = NULL;
Daniel Veillarda07050d2003-10-19 14:46:32 +00003396 if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003397 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00003398 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003399 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3400 "Space required after 'SYSTEM'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003401 }
3402 SKIP_BLANKS;
3403 URI = xmlParseSystemLiteral(ctxt);
3404 if (URI == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003405 xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003406 }
Daniel Veillarda07050d2003-10-19 14:46:32 +00003407 } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003408 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00003409 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003410 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00003411 "Space required after 'PUBLIC'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003412 }
3413 SKIP_BLANKS;
3414 *publicID = xmlParsePubidLiteral(ctxt);
3415 if (*publicID == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003416 xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003417 }
3418 if (strict) {
3419 /*
3420 * We don't handle [83] so "S SystemLiteral" is required.
3421 */
William M. Brack76e95df2003-10-18 16:20:14 +00003422 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003423 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00003424 "Space required after the Public Identifier\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003425 }
3426 } else {
3427 /*
3428 * We handle [83] so we return immediately, if
3429 * "S SystemLiteral" is not detected. From a purely parsing
3430 * point of view that's a nice mess.
3431 */
3432 const xmlChar *ptr;
3433 GROW;
3434
3435 ptr = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00003436 if (!IS_BLANK_CH(*ptr)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003437
William M. Brack76e95df2003-10-18 16:20:14 +00003438 while (IS_BLANK_CH(*ptr)) ptr++; /* TODO: dangerous, fix ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003439 if ((*ptr != '\'') && (*ptr != '"')) return(NULL);
3440 }
3441 SKIP_BLANKS;
3442 URI = xmlParseSystemLiteral(ctxt);
3443 if (URI == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003444 xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003445 }
3446 }
3447 return(URI);
3448}
3449
3450/**
3451 * xmlParseComment:
3452 * @ctxt: an XML parser context
3453 *
3454 * Skip an XML (SGML) comment <!-- .... -->
3455 * The spec says that "For compatibility, the string "--" (double-hyphen)
3456 * must not occur within comments. "
3457 *
3458 * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
3459 */
3460void
3461xmlParseComment(xmlParserCtxtPtr ctxt) {
3462 xmlChar *buf = NULL;
3463 int len;
3464 int size = XML_PARSER_BUFFER_SIZE;
3465 int q, ql;
3466 int r, rl;
3467 int cur, l;
3468 xmlParserInputState state;
3469 xmlParserInputPtr input = ctxt->input;
3470 int count = 0;
3471
3472 /*
3473 * Check that there is a comment right here.
3474 */
3475 if ((RAW != '<') || (NXT(1) != '!') ||
3476 (NXT(2) != '-') || (NXT(3) != '-')) return;
3477
3478 state = ctxt->instate;
3479 ctxt->instate = XML_PARSER_COMMENT;
3480 SHRINK;
3481 SKIP(4);
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003482 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003483 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003484 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003485 ctxt->instate = state;
3486 return;
3487 }
3488 q = CUR_CHAR(ql);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003489 if (q == 0)
3490 goto not_terminated;
Owen Taylor3473f882001-02-23 17:55:21 +00003491 NEXTL(ql);
3492 r = CUR_CHAR(rl);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003493 if (r == 0)
3494 goto not_terminated;
Owen Taylor3473f882001-02-23 17:55:21 +00003495 NEXTL(rl);
3496 cur = CUR_CHAR(l);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003497 if (cur == 0)
3498 goto not_terminated;
Owen Taylor3473f882001-02-23 17:55:21 +00003499 len = 0;
William M. Brack871611b2003-10-18 04:53:14 +00003500 while (IS_CHAR(cur) && /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003501 ((cur != '>') ||
3502 (r != '-') || (q != '-'))) {
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003503 if ((r == '-') && (q == '-')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003504 xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003505 }
3506 if (len + 5 >= size) {
William M. Bracka3215c72004-07-31 16:24:01 +00003507 xmlChar *new_buf;
Owen Taylor3473f882001-02-23 17:55:21 +00003508 size *= 2;
William M. Bracka3215c72004-07-31 16:24:01 +00003509 new_buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3510 if (new_buf == NULL) {
3511 xmlFree (buf);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003512 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003513 ctxt->instate = state;
3514 return;
3515 }
William M. Bracka3215c72004-07-31 16:24:01 +00003516 buf = new_buf;
Owen Taylor3473f882001-02-23 17:55:21 +00003517 }
3518 COPY_BUF(ql,buf,len,q);
3519 q = r;
3520 ql = rl;
3521 r = cur;
3522 rl = l;
3523
3524 count++;
3525 if (count > 50) {
3526 GROW;
3527 count = 0;
3528 }
3529 NEXTL(l);
3530 cur = CUR_CHAR(l);
3531 if (cur == 0) {
3532 SHRINK;
3533 GROW;
3534 cur = CUR_CHAR(l);
3535 }
3536 }
3537 buf[len] = 0;
William M. Brack871611b2003-10-18 04:53:14 +00003538 if (!IS_CHAR(cur)) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00003539 xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
Owen Taylor3473f882001-02-23 17:55:21 +00003540 "Comment not terminated \n<!--%.50s\n", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003541 xmlFree(buf);
3542 } else {
3543 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003544 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
3545 "Comment doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003546 }
3547 NEXT;
3548 if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
3549 (!ctxt->disableSAX))
3550 ctxt->sax->comment(ctxt->userData, buf);
3551 xmlFree(buf);
3552 }
3553 ctxt->instate = state;
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003554 return;
3555not_terminated:
3556 xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
3557 "Comment not terminated\n", NULL);
3558 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003559}
3560
3561/**
3562 * xmlParsePITarget:
3563 * @ctxt: an XML parser context
3564 *
3565 * parse the name of a PI
3566 *
3567 * [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
3568 *
3569 * Returns the PITarget name or NULL
3570 */
3571
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003572const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00003573xmlParsePITarget(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003574 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003575
3576 name = xmlParseName(ctxt);
3577 if ((name != NULL) &&
3578 ((name[0] == 'x') || (name[0] == 'X')) &&
3579 ((name[1] == 'm') || (name[1] == 'M')) &&
3580 ((name[2] == 'l') || (name[2] == 'L'))) {
3581 int i;
3582 if ((name[0] == 'x') && (name[1] == 'm') &&
3583 (name[2] == 'l') && (name[3] == 0)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003584 xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
Owen Taylor3473f882001-02-23 17:55:21 +00003585 "XML declaration allowed only at the start of the document\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003586 return(name);
3587 } else if (name[3] == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003588 xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003589 return(name);
3590 }
3591 for (i = 0;;i++) {
3592 if (xmlW3CPIs[i] == NULL) break;
3593 if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i]))
3594 return(name);
3595 }
Daniel Veillard24eb9782003-10-04 21:08:09 +00003596 xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
3597 "xmlParsePITarget: invalid name prefix 'xml'\n",
3598 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003599 }
3600 return(name);
3601}
3602
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003603#ifdef LIBXML_CATALOG_ENABLED
3604/**
3605 * xmlParseCatalogPI:
3606 * @ctxt: an XML parser context
3607 * @catalog: the PI value string
3608 *
3609 * parse an XML Catalog Processing Instruction.
3610 *
3611 * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?>
3612 *
3613 * Occurs only if allowed by the user and if happening in the Misc
3614 * part of the document before any doctype informations
3615 * This will add the given catalog to the parsing context in order
3616 * to be used if there is a resolution need further down in the document
3617 */
3618
3619static void
3620xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) {
3621 xmlChar *URL = NULL;
3622 const xmlChar *tmp, *base;
3623 xmlChar marker;
3624
3625 tmp = catalog;
William M. Brack76e95df2003-10-18 16:20:14 +00003626 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003627 if (xmlStrncmp(tmp, BAD_CAST"catalog", 7))
3628 goto error;
3629 tmp += 7;
William M. Brack76e95df2003-10-18 16:20:14 +00003630 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003631 if (*tmp != '=') {
3632 return;
3633 }
3634 tmp++;
William M. Brack76e95df2003-10-18 16:20:14 +00003635 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003636 marker = *tmp;
3637 if ((marker != '\'') && (marker != '"'))
3638 goto error;
3639 tmp++;
3640 base = tmp;
3641 while ((*tmp != 0) && (*tmp != marker)) tmp++;
3642 if (*tmp == 0)
3643 goto error;
3644 URL = xmlStrndup(base, tmp - base);
3645 tmp++;
William M. Brack76e95df2003-10-18 16:20:14 +00003646 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003647 if (*tmp != 0)
3648 goto error;
3649
3650 if (URL != NULL) {
3651 ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
3652 xmlFree(URL);
3653 }
3654 return;
3655
3656error:
Daniel Veillard24eb9782003-10-04 21:08:09 +00003657 xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI,
3658 "Catalog PI syntax error: %s\n",
3659 catalog, NULL);
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003660 if (URL != NULL)
3661 xmlFree(URL);
3662}
3663#endif
3664
Owen Taylor3473f882001-02-23 17:55:21 +00003665/**
3666 * xmlParsePI:
3667 * @ctxt: an XML parser context
3668 *
3669 * parse an XML Processing Instruction.
3670 *
3671 * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
3672 *
3673 * The processing is transfered to SAX once parsed.
3674 */
3675
3676void
3677xmlParsePI(xmlParserCtxtPtr ctxt) {
3678 xmlChar *buf = NULL;
3679 int len = 0;
3680 int size = XML_PARSER_BUFFER_SIZE;
3681 int cur, l;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003682 const xmlChar *target;
Owen Taylor3473f882001-02-23 17:55:21 +00003683 xmlParserInputState state;
3684 int count = 0;
3685
3686 if ((RAW == '<') && (NXT(1) == '?')) {
3687 xmlParserInputPtr input = ctxt->input;
3688 state = ctxt->instate;
3689 ctxt->instate = XML_PARSER_PI;
3690 /*
3691 * this is a Processing Instruction.
3692 */
3693 SKIP(2);
3694 SHRINK;
3695
3696 /*
3697 * Parse the target name and check for special support like
3698 * namespace.
3699 */
3700 target = xmlParsePITarget(ctxt);
3701 if (target != NULL) {
3702 if ((RAW == '?') && (NXT(1) == '>')) {
3703 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003704 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
3705 "PI declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003706 }
3707 SKIP(2);
3708
3709 /*
3710 * SAX: PI detected.
3711 */
3712 if ((ctxt->sax) && (!ctxt->disableSAX) &&
3713 (ctxt->sax->processingInstruction != NULL))
3714 ctxt->sax->processingInstruction(ctxt->userData,
3715 target, NULL);
3716 ctxt->instate = state;
Owen Taylor3473f882001-02-23 17:55:21 +00003717 return;
3718 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003719 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003720 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003721 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003722 ctxt->instate = state;
3723 return;
3724 }
3725 cur = CUR;
3726 if (!IS_BLANK(cur)) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00003727 xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED,
3728 "ParsePI: PI %s space expected\n", target);
Owen Taylor3473f882001-02-23 17:55:21 +00003729 }
3730 SKIP_BLANKS;
3731 cur = CUR_CHAR(l);
William M. Brack871611b2003-10-18 04:53:14 +00003732 while (IS_CHAR(cur) && /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003733 ((cur != '?') || (NXT(1) != '>'))) {
3734 if (len + 5 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00003735 xmlChar *tmp;
3736
Owen Taylor3473f882001-02-23 17:55:21 +00003737 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00003738 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3739 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003740 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00003741 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003742 ctxt->instate = state;
3743 return;
3744 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00003745 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00003746 }
3747 count++;
3748 if (count > 50) {
3749 GROW;
3750 count = 0;
3751 }
3752 COPY_BUF(l,buf,len,cur);
3753 NEXTL(l);
3754 cur = CUR_CHAR(l);
3755 if (cur == 0) {
3756 SHRINK;
3757 GROW;
3758 cur = CUR_CHAR(l);
3759 }
3760 }
3761 buf[len] = 0;
3762 if (cur != '?') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00003763 xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED,
3764 "ParsePI: PI %s never end ...\n", target);
Owen Taylor3473f882001-02-23 17:55:21 +00003765 } else {
3766 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003767 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3768 "PI declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003769 }
3770 SKIP(2);
3771
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003772#ifdef LIBXML_CATALOG_ENABLED
3773 if (((state == XML_PARSER_MISC) ||
3774 (state == XML_PARSER_START)) &&
3775 (xmlStrEqual(target, XML_CATALOG_PI))) {
3776 xmlCatalogAllow allow = xmlCatalogGetDefaults();
3777 if ((allow == XML_CATA_ALLOW_DOCUMENT) ||
3778 (allow == XML_CATA_ALLOW_ALL))
3779 xmlParseCatalogPI(ctxt, buf);
3780 }
3781#endif
3782
3783
Owen Taylor3473f882001-02-23 17:55:21 +00003784 /*
3785 * SAX: PI detected.
3786 */
3787 if ((ctxt->sax) && (!ctxt->disableSAX) &&
3788 (ctxt->sax->processingInstruction != NULL))
3789 ctxt->sax->processingInstruction(ctxt->userData,
3790 target, buf);
3791 }
3792 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003793 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003794 xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003795 }
3796 ctxt->instate = state;
3797 }
3798}
3799
3800/**
3801 * xmlParseNotationDecl:
3802 * @ctxt: an XML parser context
3803 *
3804 * parse a notation declaration
3805 *
3806 * [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>'
3807 *
3808 * Hence there is actually 3 choices:
3809 * 'PUBLIC' S PubidLiteral
3810 * 'PUBLIC' S PubidLiteral S SystemLiteral
3811 * and 'SYSTEM' S SystemLiteral
3812 *
3813 * See the NOTE on xmlParseExternalID().
3814 */
3815
3816void
3817xmlParseNotationDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003818 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003819 xmlChar *Pubid;
3820 xmlChar *Systemid;
3821
Daniel Veillarda07050d2003-10-19 14:46:32 +00003822 if (CMP10(CUR_PTR, '<', '!', 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003823 xmlParserInputPtr input = ctxt->input;
3824 SHRINK;
3825 SKIP(10);
William M. Brack76e95df2003-10-18 16:20:14 +00003826 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003827 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3828 "Space required after '<!NOTATION'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003829 return;
3830 }
3831 SKIP_BLANKS;
3832
Daniel Veillard76d66f42001-05-16 21:05:17 +00003833 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003834 if (name == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003835 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003836 return;
3837 }
William M. Brack76e95df2003-10-18 16:20:14 +00003838 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003839 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00003840 "Space required after the NOTATION name'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003841 return;
3842 }
3843 SKIP_BLANKS;
3844
3845 /*
3846 * Parse the IDs.
3847 */
3848 Systemid = xmlParseExternalID(ctxt, &Pubid, 0);
3849 SKIP_BLANKS;
3850
3851 if (RAW == '>') {
3852 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003853 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3854 "Notation declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003855 }
3856 NEXT;
3857 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
3858 (ctxt->sax->notationDecl != NULL))
3859 ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid);
3860 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003861 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003862 }
Owen Taylor3473f882001-02-23 17:55:21 +00003863 if (Systemid != NULL) xmlFree(Systemid);
3864 if (Pubid != NULL) xmlFree(Pubid);
3865 }
3866}
3867
3868/**
3869 * xmlParseEntityDecl:
3870 * @ctxt: an XML parser context
3871 *
3872 * parse <!ENTITY declarations
3873 *
3874 * [70] EntityDecl ::= GEDecl | PEDecl
3875 *
3876 * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>'
3877 *
3878 * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>'
3879 *
3880 * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?)
3881 *
3882 * [74] PEDef ::= EntityValue | ExternalID
3883 *
3884 * [76] NDataDecl ::= S 'NDATA' S Name
3885 *
3886 * [ VC: Notation Declared ]
3887 * The Name must match the declared name of a notation.
3888 */
3889
3890void
3891xmlParseEntityDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003892 const xmlChar *name = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003893 xmlChar *value = NULL;
3894 xmlChar *URI = NULL, *literal = NULL;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003895 const xmlChar *ndata = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003896 int isParameter = 0;
3897 xmlChar *orig = NULL;
Daniel Veillardf5582f12002-06-11 10:08:16 +00003898 int skipped;
Owen Taylor3473f882001-02-23 17:55:21 +00003899
3900 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00003901 if (CMP8(CUR_PTR, '<', '!', 'E', 'N', 'T', 'I', 'T', 'Y')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003902 xmlParserInputPtr input = ctxt->input;
Owen Taylor3473f882001-02-23 17:55:21 +00003903 SHRINK;
3904 SKIP(8);
Daniel Veillardf5582f12002-06-11 10:08:16 +00003905 skipped = SKIP_BLANKS;
3906 if (skipped == 0) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003907 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3908 "Space required after '<!ENTITY'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003909 }
Owen Taylor3473f882001-02-23 17:55:21 +00003910
3911 if (RAW == '%') {
3912 NEXT;
Daniel Veillardf5582f12002-06-11 10:08:16 +00003913 skipped = SKIP_BLANKS;
3914 if (skipped == 0) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003915 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3916 "Space required after '%'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003917 }
Owen Taylor3473f882001-02-23 17:55:21 +00003918 isParameter = 1;
3919 }
3920
Daniel Veillard76d66f42001-05-16 21:05:17 +00003921 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003922 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003923 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
3924 "xmlParseEntityDecl: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003925 return;
3926 }
Daniel Veillardf5582f12002-06-11 10:08:16 +00003927 skipped = SKIP_BLANKS;
3928 if (skipped == 0) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003929 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3930 "Space required after the entity name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003931 }
Owen Taylor3473f882001-02-23 17:55:21 +00003932
Daniel Veillardf5582f12002-06-11 10:08:16 +00003933 ctxt->instate = XML_PARSER_ENTITY_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00003934 /*
3935 * handle the various case of definitions...
3936 */
3937 if (isParameter) {
3938 if ((RAW == '"') || (RAW == '\'')) {
3939 value = xmlParseEntityValue(ctxt, &orig);
3940 if (value) {
3941 if ((ctxt->sax != NULL) &&
3942 (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
3943 ctxt->sax->entityDecl(ctxt->userData, name,
3944 XML_INTERNAL_PARAMETER_ENTITY,
3945 NULL, NULL, value);
3946 }
3947 } else {
3948 URI = xmlParseExternalID(ctxt, &literal, 1);
3949 if ((URI == NULL) && (literal == NULL)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003950 xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003951 }
3952 if (URI) {
3953 xmlURIPtr uri;
3954
3955 uri = xmlParseURI((const char *) URI);
3956 if (uri == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003957 xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI,
3958 "Invalid URI: %s\n", URI);
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003959 /*
3960 * This really ought to be a well formedness error
3961 * but the XML Core WG decided otherwise c.f. issue
3962 * E26 of the XML erratas.
3963 */
Owen Taylor3473f882001-02-23 17:55:21 +00003964 } else {
3965 if (uri->fragment != NULL) {
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003966 /*
3967 * Okay this is foolish to block those but not
3968 * invalid URIs.
3969 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003970 xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003971 } else {
3972 if ((ctxt->sax != NULL) &&
3973 (!ctxt->disableSAX) &&
3974 (ctxt->sax->entityDecl != NULL))
3975 ctxt->sax->entityDecl(ctxt->userData, name,
3976 XML_EXTERNAL_PARAMETER_ENTITY,
3977 literal, URI, NULL);
3978 }
3979 xmlFreeURI(uri);
3980 }
3981 }
3982 }
3983 } else {
3984 if ((RAW == '"') || (RAW == '\'')) {
3985 value = xmlParseEntityValue(ctxt, &orig);
3986 if ((ctxt->sax != NULL) &&
3987 (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
3988 ctxt->sax->entityDecl(ctxt->userData, name,
3989 XML_INTERNAL_GENERAL_ENTITY,
3990 NULL, NULL, value);
Daniel Veillard5997aca2002-03-18 18:36:20 +00003991 /*
3992 * For expat compatibility in SAX mode.
3993 */
3994 if ((ctxt->myDoc == NULL) ||
3995 (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) {
3996 if (ctxt->myDoc == NULL) {
3997 ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
3998 }
3999 if (ctxt->myDoc->intSubset == NULL)
4000 ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
4001 BAD_CAST "fake", NULL, NULL);
4002
Daniel Veillard1af9a412003-08-20 22:54:39 +00004003 xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY,
4004 NULL, NULL, value);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004005 }
Owen Taylor3473f882001-02-23 17:55:21 +00004006 } else {
4007 URI = xmlParseExternalID(ctxt, &literal, 1);
4008 if ((URI == NULL) && (literal == NULL)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004009 xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004010 }
4011 if (URI) {
4012 xmlURIPtr uri;
4013
4014 uri = xmlParseURI((const char *)URI);
4015 if (uri == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004016 xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI,
4017 "Invalid URI: %s\n", URI);
Daniel Veillard4a7ae502002-02-18 19:18:17 +00004018 /*
4019 * This really ought to be a well formedness error
4020 * but the XML Core WG decided otherwise c.f. issue
4021 * E26 of the XML erratas.
4022 */
Owen Taylor3473f882001-02-23 17:55:21 +00004023 } else {
4024 if (uri->fragment != NULL) {
Daniel Veillard4a7ae502002-02-18 19:18:17 +00004025 /*
4026 * Okay this is foolish to block those but not
4027 * invalid URIs.
4028 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004029 xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004030 }
4031 xmlFreeURI(uri);
4032 }
4033 }
William M. Brack76e95df2003-10-18 16:20:14 +00004034 if ((RAW != '>') && (!IS_BLANK_CH(CUR))) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004035 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4036 "Space required before 'NDATA'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004037 }
4038 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004039 if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004040 SKIP(5);
William M. Brack76e95df2003-10-18 16:20:14 +00004041 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004042 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4043 "Space required after 'NDATA'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004044 }
4045 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004046 ndata = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004047 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4048 (ctxt->sax->unparsedEntityDecl != NULL))
4049 ctxt->sax->unparsedEntityDecl(ctxt->userData, name,
4050 literal, URI, ndata);
4051 } else {
4052 if ((ctxt->sax != NULL) &&
4053 (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
4054 ctxt->sax->entityDecl(ctxt->userData, name,
4055 XML_EXTERNAL_GENERAL_PARSED_ENTITY,
4056 literal, URI, NULL);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004057 /*
4058 * For expat compatibility in SAX mode.
4059 * assuming the entity repalcement was asked for
4060 */
4061 if ((ctxt->replaceEntities != 0) &&
4062 ((ctxt->myDoc == NULL) ||
4063 (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) {
4064 if (ctxt->myDoc == NULL) {
4065 ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
4066 }
4067
4068 if (ctxt->myDoc->intSubset == NULL)
4069 ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
4070 BAD_CAST "fake", NULL, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00004071 xmlSAX2EntityDecl(ctxt, name,
4072 XML_EXTERNAL_GENERAL_PARSED_ENTITY,
4073 literal, URI, NULL);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004074 }
Owen Taylor3473f882001-02-23 17:55:21 +00004075 }
4076 }
4077 }
4078 SKIP_BLANKS;
4079 if (RAW != '>') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00004080 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED,
Owen Taylor3473f882001-02-23 17:55:21 +00004081 "xmlParseEntityDecl: entity %s not terminated\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00004082 } else {
4083 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004084 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
4085 "Entity declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004086 }
4087 NEXT;
4088 }
4089 if (orig != NULL) {
4090 /*
4091 * Ugly mechanism to save the raw entity value.
4092 */
4093 xmlEntityPtr cur = NULL;
4094
4095 if (isParameter) {
4096 if ((ctxt->sax != NULL) &&
4097 (ctxt->sax->getParameterEntity != NULL))
4098 cur = ctxt->sax->getParameterEntity(ctxt->userData, name);
4099 } else {
4100 if ((ctxt->sax != NULL) &&
4101 (ctxt->sax->getEntity != NULL))
4102 cur = ctxt->sax->getEntity(ctxt->userData, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004103 if ((cur == NULL) && (ctxt->userData==ctxt)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00004104 cur = xmlSAX2GetEntity(ctxt, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004105 }
Owen Taylor3473f882001-02-23 17:55:21 +00004106 }
4107 if (cur != NULL) {
4108 if (cur->orig != NULL)
4109 xmlFree(orig);
4110 else
4111 cur->orig = orig;
4112 } else
4113 xmlFree(orig);
4114 }
Owen Taylor3473f882001-02-23 17:55:21 +00004115 if (value != NULL) xmlFree(value);
4116 if (URI != NULL) xmlFree(URI);
4117 if (literal != NULL) xmlFree(literal);
Owen Taylor3473f882001-02-23 17:55:21 +00004118 }
4119}
4120
4121/**
4122 * xmlParseDefaultDecl:
4123 * @ctxt: an XML parser context
4124 * @value: Receive a possible fixed default value for the attribute
4125 *
4126 * Parse an attribute default declaration
4127 *
4128 * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue)
4129 *
4130 * [ VC: Required Attribute ]
4131 * if the default declaration is the keyword #REQUIRED, then the
4132 * attribute must be specified for all elements of the type in the
4133 * attribute-list declaration.
4134 *
4135 * [ VC: Attribute Default Legal ]
4136 * The declared default value must meet the lexical constraints of
4137 * the declared attribute type c.f. xmlValidateAttributeDecl()
4138 *
4139 * [ VC: Fixed Attribute Default ]
4140 * if an attribute has a default value declared with the #FIXED
4141 * keyword, instances of that attribute must match the default value.
4142 *
4143 * [ WFC: No < in Attribute Values ]
4144 * handled in xmlParseAttValue()
4145 *
4146 * returns: XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED
4147 * or XML_ATTRIBUTE_FIXED.
4148 */
4149
4150int
4151xmlParseDefaultDecl(xmlParserCtxtPtr ctxt, xmlChar **value) {
4152 int val;
4153 xmlChar *ret;
4154
4155 *value = NULL;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004156 if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004157 SKIP(9);
4158 return(XML_ATTRIBUTE_REQUIRED);
4159 }
Daniel Veillarda07050d2003-10-19 14:46:32 +00004160 if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004161 SKIP(8);
4162 return(XML_ATTRIBUTE_IMPLIED);
4163 }
4164 val = XML_ATTRIBUTE_NONE;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004165 if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004166 SKIP(6);
4167 val = XML_ATTRIBUTE_FIXED;
William M. Brack76e95df2003-10-18 16:20:14 +00004168 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004169 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4170 "Space required after '#FIXED'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004171 }
4172 SKIP_BLANKS;
4173 }
4174 ret = xmlParseAttValue(ctxt);
4175 ctxt->instate = XML_PARSER_DTD;
4176 if (ret == NULL) {
William M. Brack7b9154b2003-09-27 19:23:50 +00004177 xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004178 "Attribute default value declaration error\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004179 } else
4180 *value = ret;
4181 return(val);
4182}
4183
4184/**
4185 * xmlParseNotationType:
4186 * @ctxt: an XML parser context
4187 *
4188 * parse an Notation attribute type.
4189 *
4190 * Note: the leading 'NOTATION' S part has already being parsed...
4191 *
4192 * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
4193 *
4194 * [ VC: Notation Attributes ]
4195 * Values of this type must match one of the notation names included
4196 * in the declaration; all notation names in the declaration must be declared.
4197 *
4198 * Returns: the notation attribute tree built while parsing
4199 */
4200
4201xmlEnumerationPtr
4202xmlParseNotationType(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004203 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00004204 xmlEnumerationPtr ret = NULL, last = NULL, cur;
4205
4206 if (RAW != '(') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004207 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004208 return(NULL);
4209 }
4210 SHRINK;
4211 do {
4212 NEXT;
4213 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004214 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004215 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004216 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4217 "Name expected in NOTATION declaration\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004218 return(ret);
4219 }
4220 cur = xmlCreateEnumeration(name);
Owen Taylor3473f882001-02-23 17:55:21 +00004221 if (cur == NULL) return(ret);
4222 if (last == NULL) ret = last = cur;
4223 else {
4224 last->next = cur;
4225 last = cur;
4226 }
4227 SKIP_BLANKS;
4228 } while (RAW == '|');
4229 if (RAW != ')') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004230 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004231 if ((last != NULL) && (last != ret))
4232 xmlFreeEnumeration(last);
4233 return(ret);
4234 }
4235 NEXT;
4236 return(ret);
4237}
4238
4239/**
4240 * xmlParseEnumerationType:
4241 * @ctxt: an XML parser context
4242 *
4243 * parse an Enumeration attribute type.
4244 *
4245 * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')'
4246 *
4247 * [ VC: Enumeration ]
4248 * Values of this type must match one of the Nmtoken tokens in
4249 * the declaration
4250 *
4251 * Returns: the enumeration attribute tree built while parsing
4252 */
4253
4254xmlEnumerationPtr
4255xmlParseEnumerationType(xmlParserCtxtPtr ctxt) {
4256 xmlChar *name;
4257 xmlEnumerationPtr ret = NULL, last = NULL, cur;
4258
4259 if (RAW != '(') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004260 xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004261 return(NULL);
4262 }
4263 SHRINK;
4264 do {
4265 NEXT;
4266 SKIP_BLANKS;
4267 name = xmlParseNmtoken(ctxt);
4268 if (name == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004269 xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004270 return(ret);
4271 }
4272 cur = xmlCreateEnumeration(name);
4273 xmlFree(name);
4274 if (cur == NULL) return(ret);
4275 if (last == NULL) ret = last = cur;
4276 else {
4277 last->next = cur;
4278 last = cur;
4279 }
4280 SKIP_BLANKS;
4281 } while (RAW == '|');
4282 if (RAW != ')') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004283 xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004284 return(ret);
4285 }
4286 NEXT;
4287 return(ret);
4288}
4289
4290/**
4291 * xmlParseEnumeratedType:
4292 * @ctxt: an XML parser context
4293 * @tree: the enumeration tree built while parsing
4294 *
4295 * parse an Enumerated attribute type.
4296 *
4297 * [57] EnumeratedType ::= NotationType | Enumeration
4298 *
4299 * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
4300 *
4301 *
4302 * Returns: XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION
4303 */
4304
4305int
4306xmlParseEnumeratedType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) {
Daniel Veillarda07050d2003-10-19 14:46:32 +00004307 if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004308 SKIP(8);
William M. Brack76e95df2003-10-18 16:20:14 +00004309 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004310 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4311 "Space required after 'NOTATION'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004312 return(0);
4313 }
4314 SKIP_BLANKS;
4315 *tree = xmlParseNotationType(ctxt);
4316 if (*tree == NULL) return(0);
4317 return(XML_ATTRIBUTE_NOTATION);
4318 }
4319 *tree = xmlParseEnumerationType(ctxt);
4320 if (*tree == NULL) return(0);
4321 return(XML_ATTRIBUTE_ENUMERATION);
4322}
4323
4324/**
4325 * xmlParseAttributeType:
4326 * @ctxt: an XML parser context
4327 * @tree: the enumeration tree built while parsing
4328 *
4329 * parse the Attribute list def for an element
4330 *
4331 * [54] AttType ::= StringType | TokenizedType | EnumeratedType
4332 *
4333 * [55] StringType ::= 'CDATA'
4334 *
4335 * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' |
4336 * 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS'
4337 *
4338 * Validity constraints for attribute values syntax are checked in
4339 * xmlValidateAttributeValue()
4340 *
4341 * [ VC: ID ]
4342 * Values of type ID must match the Name production. A name must not
4343 * appear more than once in an XML document as a value of this type;
4344 * i.e., ID values must uniquely identify the elements which bear them.
4345 *
4346 * [ VC: One ID per Element Type ]
4347 * No element type may have more than one ID attribute specified.
4348 *
4349 * [ VC: ID Attribute Default ]
4350 * An ID attribute must have a declared default of #IMPLIED or #REQUIRED.
4351 *
4352 * [ VC: IDREF ]
4353 * Values of type IDREF must match the Name production, and values
4354 * of type IDREFS must match Names; each IDREF Name must match the value
4355 * of an ID attribute on some element in the XML document; i.e. IDREF
4356 * values must match the value of some ID attribute.
4357 *
4358 * [ VC: Entity Name ]
4359 * Values of type ENTITY must match the Name production, values
4360 * of type ENTITIES must match Names; each Entity Name must match the
4361 * name of an unparsed entity declared in the DTD.
4362 *
4363 * [ VC: Name Token ]
4364 * Values of type NMTOKEN must match the Nmtoken production; values
4365 * of type NMTOKENS must match Nmtokens.
4366 *
4367 * Returns the attribute type
4368 */
4369int
4370xmlParseAttributeType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) {
4371 SHRINK;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004372 if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004373 SKIP(5);
4374 return(XML_ATTRIBUTE_CDATA);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004375 } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004376 SKIP(6);
4377 return(XML_ATTRIBUTE_IDREFS);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004378 } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004379 SKIP(5);
4380 return(XML_ATTRIBUTE_IDREF);
4381 } else if ((RAW == 'I') && (NXT(1) == 'D')) {
4382 SKIP(2);
4383 return(XML_ATTRIBUTE_ID);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004384 } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004385 SKIP(6);
4386 return(XML_ATTRIBUTE_ENTITY);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004387 } else if (CMP8(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'I', 'E', 'S')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004388 SKIP(8);
4389 return(XML_ATTRIBUTE_ENTITIES);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004390 } else if (CMP8(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N', 'S')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004391 SKIP(8);
4392 return(XML_ATTRIBUTE_NMTOKENS);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004393 } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004394 SKIP(7);
4395 return(XML_ATTRIBUTE_NMTOKEN);
4396 }
4397 return(xmlParseEnumeratedType(ctxt, tree));
4398}
4399
4400/**
4401 * xmlParseAttributeListDecl:
4402 * @ctxt: an XML parser context
4403 *
4404 * : parse the Attribute list def for an element
4405 *
4406 * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>'
4407 *
4408 * [53] AttDef ::= S Name S AttType S DefaultDecl
4409 *
4410 */
4411void
4412xmlParseAttributeListDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004413 const xmlChar *elemName;
4414 const xmlChar *attrName;
Owen Taylor3473f882001-02-23 17:55:21 +00004415 xmlEnumerationPtr tree;
4416
Daniel Veillarda07050d2003-10-19 14:46:32 +00004417 if (CMP9(CUR_PTR, '<', '!', 'A', 'T', 'T', 'L', 'I', 'S', 'T')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004418 xmlParserInputPtr input = ctxt->input;
4419
4420 SKIP(9);
William M. Brack76e95df2003-10-18 16:20:14 +00004421 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004422 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004423 "Space required after '<!ATTLIST'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004424 }
4425 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004426 elemName = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004427 if (elemName == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004428 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4429 "ATTLIST: no name for Element\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004430 return;
4431 }
4432 SKIP_BLANKS;
4433 GROW;
4434 while (RAW != '>') {
4435 const xmlChar *check = CUR_PTR;
4436 int type;
4437 int def;
4438 xmlChar *defaultValue = NULL;
4439
4440 GROW;
4441 tree = NULL;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004442 attrName = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004443 if (attrName == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004444 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4445 "ATTLIST: no name for Attribute\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004446 break;
4447 }
4448 GROW;
William M. Brack76e95df2003-10-18 16:20:14 +00004449 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004450 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004451 "Space required after the attribute name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004452 if (defaultValue != NULL)
4453 xmlFree(defaultValue);
4454 break;
4455 }
4456 SKIP_BLANKS;
4457
4458 type = xmlParseAttributeType(ctxt, &tree);
4459 if (type <= 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00004460 if (defaultValue != NULL)
4461 xmlFree(defaultValue);
4462 break;
4463 }
4464
4465 GROW;
William M. Brack76e95df2003-10-18 16:20:14 +00004466 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004467 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4468 "Space required after the attribute type\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004469 if (defaultValue != NULL)
4470 xmlFree(defaultValue);
4471 if (tree != NULL)
4472 xmlFreeEnumeration(tree);
4473 break;
4474 }
4475 SKIP_BLANKS;
4476
4477 def = xmlParseDefaultDecl(ctxt, &defaultValue);
4478 if (def <= 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00004479 if (defaultValue != NULL)
4480 xmlFree(defaultValue);
4481 if (tree != NULL)
4482 xmlFreeEnumeration(tree);
4483 break;
4484 }
4485
4486 GROW;
4487 if (RAW != '>') {
William M. Brack76e95df2003-10-18 16:20:14 +00004488 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004489 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004490 "Space required after the attribute default value\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004491 if (defaultValue != NULL)
4492 xmlFree(defaultValue);
4493 if (tree != NULL)
4494 xmlFreeEnumeration(tree);
4495 break;
4496 }
4497 SKIP_BLANKS;
4498 }
4499 if (check == CUR_PTR) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004500 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
4501 "in xmlParseAttributeListDecl\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004502 if (defaultValue != NULL)
4503 xmlFree(defaultValue);
4504 if (tree != NULL)
4505 xmlFreeEnumeration(tree);
4506 break;
4507 }
4508 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4509 (ctxt->sax->attributeDecl != NULL))
4510 ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName,
4511 type, def, defaultValue, tree);
Daniel Veillarde57ec792003-09-10 10:50:59 +00004512 else if (tree != NULL)
4513 xmlFreeEnumeration(tree);
4514
4515 if ((ctxt->sax2) && (defaultValue != NULL) &&
4516 (def != XML_ATTRIBUTE_IMPLIED) &&
4517 (def != XML_ATTRIBUTE_REQUIRED)) {
4518 xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue);
4519 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00004520 if ((ctxt->sax2) && (type != XML_ATTRIBUTE_CDATA)) {
4521 xmlAddSpecialAttr(ctxt, elemName, attrName, type);
4522 }
Owen Taylor3473f882001-02-23 17:55:21 +00004523 if (defaultValue != NULL)
4524 xmlFree(defaultValue);
4525 GROW;
4526 }
4527 if (RAW == '>') {
4528 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004529 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
4530 "Attribute list declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004531 }
4532 NEXT;
4533 }
Owen Taylor3473f882001-02-23 17:55:21 +00004534 }
4535}
4536
4537/**
4538 * xmlParseElementMixedContentDecl:
4539 * @ctxt: an XML parser context
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004540 * @inputchk: the input used for the current entity, needed for boundary checks
Owen Taylor3473f882001-02-23 17:55:21 +00004541 *
4542 * parse the declaration for a Mixed Element content
4543 * The leading '(' and spaces have been skipped in xmlParseElementContentDecl
4544 *
4545 * [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' |
4546 * '(' S? '#PCDATA' S? ')'
4547 *
4548 * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49])
4549 *
4550 * [ VC: No Duplicate Types ]
4551 * The same name must not appear more than once in a single
4552 * mixed-content declaration.
4553 *
4554 * returns: the list of the xmlElementContentPtr describing the element choices
4555 */
4556xmlElementContentPtr
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004557xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) {
Owen Taylor3473f882001-02-23 17:55:21 +00004558 xmlElementContentPtr ret = NULL, cur = NULL, n;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004559 const xmlChar *elem = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00004560
4561 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004562 if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004563 SKIP(7);
4564 SKIP_BLANKS;
4565 SHRINK;
4566 if (RAW == ')') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004567 if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00004568 xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
4569"Element content declaration doesn't start and stop in the same entity\n",
4570 NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004571 }
Owen Taylor3473f882001-02-23 17:55:21 +00004572 NEXT;
4573 ret = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_PCDATA);
4574 if (RAW == '*') {
4575 ret->ocur = XML_ELEMENT_CONTENT_MULT;
4576 NEXT;
4577 }
4578 return(ret);
4579 }
4580 if ((RAW == '(') || (RAW == '|')) {
4581 ret = cur = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_PCDATA);
4582 if (ret == NULL) return(NULL);
4583 }
4584 while (RAW == '|') {
4585 NEXT;
4586 if (elem == NULL) {
4587 ret = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR);
4588 if (ret == NULL) return(NULL);
4589 ret->c1 = cur;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004590 if (cur != NULL)
4591 cur->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +00004592 cur = ret;
4593 } else {
4594 n = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR);
4595 if (n == NULL) return(NULL);
4596 n->c1 = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004597 if (n->c1 != NULL)
4598 n->c1->parent = n;
Owen Taylor3473f882001-02-23 17:55:21 +00004599 cur->c2 = n;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004600 if (n != NULL)
4601 n->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004602 cur = n;
Owen Taylor3473f882001-02-23 17:55:21 +00004603 }
4604 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004605 elem = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004606 if (elem == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004607 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004608 "xmlParseElementMixedContentDecl : Name expected\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004609 xmlFreeElementContent(cur);
4610 return(NULL);
4611 }
4612 SKIP_BLANKS;
4613 GROW;
4614 }
4615 if ((RAW == ')') && (NXT(1) == '*')) {
4616 if (elem != NULL) {
4617 cur->c2 = xmlNewElementContent(elem,
4618 XML_ELEMENT_CONTENT_ELEMENT);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004619 if (cur->c2 != NULL)
4620 cur->c2->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004621 }
4622 ret->ocur = XML_ELEMENT_CONTENT_MULT;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004623 if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00004624 xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
4625"Element content declaration doesn't start and stop in the same entity\n",
4626 NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004627 }
Owen Taylor3473f882001-02-23 17:55:21 +00004628 SKIP(2);
4629 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00004630 xmlFreeElementContent(ret);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004631 xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004632 return(NULL);
4633 }
4634
4635 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004636 xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004637 }
4638 return(ret);
4639}
4640
4641/**
4642 * xmlParseElementChildrenContentDecl:
4643 * @ctxt: an XML parser context
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004644 * @inputchk: the input used for the current entity, needed for boundary checks
Owen Taylor3473f882001-02-23 17:55:21 +00004645 *
4646 * parse the declaration for a Mixed Element content
4647 * The leading '(' and spaces have been skipped in xmlParseElementContentDecl
4648 *
4649 *
4650 * [47] children ::= (choice | seq) ('?' | '*' | '+')?
4651 *
4652 * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')?
4653 *
4654 * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')'
4655 *
4656 * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')'
4657 *
4658 * [ VC: Proper Group/PE Nesting ] applies to [49] and [50]
4659 * TODO Parameter-entity replacement text must be properly nested
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004660 * with parenthesized groups. That is to say, if either of the
Owen Taylor3473f882001-02-23 17:55:21 +00004661 * opening or closing parentheses in a choice, seq, or Mixed
4662 * construct is contained in the replacement text for a parameter
4663 * entity, both must be contained in the same replacement text. For
4664 * interoperability, if a parameter-entity reference appears in a
4665 * choice, seq, or Mixed construct, its replacement text should not
4666 * be empty, and neither the first nor last non-blank character of
4667 * the replacement text should be a connector (| or ,).
4668 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00004669 * Returns the tree of xmlElementContentPtr describing the element
Owen Taylor3473f882001-02-23 17:55:21 +00004670 * hierarchy.
4671 */
4672xmlElementContentPtr
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004673xmlParseElementChildrenContentDecl (xmlParserCtxtPtr ctxt, int inputchk) {
Owen Taylor3473f882001-02-23 17:55:21 +00004674 xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004675 const xmlChar *elem;
Owen Taylor3473f882001-02-23 17:55:21 +00004676 xmlChar type = 0;
4677
4678 SKIP_BLANKS;
4679 GROW;
4680 if (RAW == '(') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004681 int inputid = ctxt->input->id;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004682
Owen Taylor3473f882001-02-23 17:55:21 +00004683 /* Recurse on first child */
4684 NEXT;
4685 SKIP_BLANKS;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004686 cur = ret = xmlParseElementChildrenContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004687 SKIP_BLANKS;
4688 GROW;
4689 } else {
Daniel Veillard76d66f42001-05-16 21:05:17 +00004690 elem = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004691 if (elem == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004692 xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004693 return(NULL);
4694 }
4695 cur = ret = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004696 if (cur == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004697 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004698 return(NULL);
4699 }
Owen Taylor3473f882001-02-23 17:55:21 +00004700 GROW;
4701 if (RAW == '?') {
4702 cur->ocur = XML_ELEMENT_CONTENT_OPT;
4703 NEXT;
4704 } else if (RAW == '*') {
4705 cur->ocur = XML_ELEMENT_CONTENT_MULT;
4706 NEXT;
4707 } else if (RAW == '+') {
4708 cur->ocur = XML_ELEMENT_CONTENT_PLUS;
4709 NEXT;
4710 } else {
4711 cur->ocur = XML_ELEMENT_CONTENT_ONCE;
4712 }
Owen Taylor3473f882001-02-23 17:55:21 +00004713 GROW;
4714 }
4715 SKIP_BLANKS;
4716 SHRINK;
4717 while (RAW != ')') {
4718 /*
4719 * Each loop we parse one separator and one element.
4720 */
4721 if (RAW == ',') {
4722 if (type == 0) type = CUR;
4723
4724 /*
4725 * Detect "Name | Name , Name" error
4726 */
4727 else if (type != CUR) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004728 xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004729 "xmlParseElementChildrenContentDecl : '%c' expected\n",
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004730 type);
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004731 if ((last != NULL) && (last != ret))
Owen Taylor3473f882001-02-23 17:55:21 +00004732 xmlFreeElementContent(last);
4733 if (ret != NULL)
4734 xmlFreeElementContent(ret);
4735 return(NULL);
4736 }
4737 NEXT;
4738
4739 op = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_SEQ);
4740 if (op == NULL) {
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004741 if ((last != NULL) && (last != ret))
4742 xmlFreeElementContent(last);
Owen Taylor3473f882001-02-23 17:55:21 +00004743 xmlFreeElementContent(ret);
4744 return(NULL);
4745 }
4746 if (last == NULL) {
4747 op->c1 = ret;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004748 if (ret != NULL)
4749 ret->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004750 ret = cur = op;
4751 } else {
4752 cur->c2 = op;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004753 if (op != NULL)
4754 op->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004755 op->c1 = last;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004756 if (last != NULL)
4757 last->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004758 cur =op;
4759 last = NULL;
4760 }
4761 } else if (RAW == '|') {
4762 if (type == 0) type = CUR;
4763
4764 /*
4765 * Detect "Name , Name | Name" error
4766 */
4767 else if (type != CUR) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004768 xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004769 "xmlParseElementChildrenContentDecl : '%c' expected\n",
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004770 type);
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004771 if ((last != NULL) && (last != ret))
Owen Taylor3473f882001-02-23 17:55:21 +00004772 xmlFreeElementContent(last);
4773 if (ret != NULL)
4774 xmlFreeElementContent(ret);
4775 return(NULL);
4776 }
4777 NEXT;
4778
4779 op = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR);
4780 if (op == NULL) {
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004781 if ((last != NULL) && (last != ret))
Owen Taylor3473f882001-02-23 17:55:21 +00004782 xmlFreeElementContent(last);
4783 if (ret != NULL)
4784 xmlFreeElementContent(ret);
4785 return(NULL);
4786 }
4787 if (last == NULL) {
4788 op->c1 = ret;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004789 if (ret != NULL)
4790 ret->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004791 ret = cur = op;
4792 } else {
4793 cur->c2 = op;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004794 if (op != NULL)
4795 op->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004796 op->c1 = last;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004797 if (last != NULL)
4798 last->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004799 cur =op;
4800 last = NULL;
4801 }
4802 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004803 xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004804 if (ret != NULL)
4805 xmlFreeElementContent(ret);
4806 return(NULL);
4807 }
4808 GROW;
4809 SKIP_BLANKS;
4810 GROW;
4811 if (RAW == '(') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004812 int inputid = ctxt->input->id;
Owen Taylor3473f882001-02-23 17:55:21 +00004813 /* Recurse on second child */
4814 NEXT;
4815 SKIP_BLANKS;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004816 last = xmlParseElementChildrenContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004817 SKIP_BLANKS;
4818 } else {
Daniel Veillard76d66f42001-05-16 21:05:17 +00004819 elem = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004820 if (elem == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004821 xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004822 if (ret != NULL)
4823 xmlFreeElementContent(ret);
4824 return(NULL);
4825 }
4826 last = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT);
Owen Taylor3473f882001-02-23 17:55:21 +00004827 if (RAW == '?') {
4828 last->ocur = XML_ELEMENT_CONTENT_OPT;
4829 NEXT;
4830 } else if (RAW == '*') {
4831 last->ocur = XML_ELEMENT_CONTENT_MULT;
4832 NEXT;
4833 } else if (RAW == '+') {
4834 last->ocur = XML_ELEMENT_CONTENT_PLUS;
4835 NEXT;
4836 } else {
4837 last->ocur = XML_ELEMENT_CONTENT_ONCE;
4838 }
4839 }
4840 SKIP_BLANKS;
4841 GROW;
4842 }
4843 if ((cur != NULL) && (last != NULL)) {
4844 cur->c2 = last;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004845 if (last != NULL)
4846 last->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004847 }
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004848 if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00004849 xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
4850"Element content declaration doesn't start and stop in the same entity\n",
4851 NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004852 }
Owen Taylor3473f882001-02-23 17:55:21 +00004853 NEXT;
4854 if (RAW == '?') {
William M. Brackf8f2e8f2004-05-14 04:37:41 +00004855 if (ret != NULL) {
4856 if ((ret->ocur == XML_ELEMENT_CONTENT_PLUS) ||
4857 (ret->ocur == XML_ELEMENT_CONTENT_MULT))
4858 ret->ocur = XML_ELEMENT_CONTENT_MULT;
4859 else
4860 ret->ocur = XML_ELEMENT_CONTENT_OPT;
4861 }
Owen Taylor3473f882001-02-23 17:55:21 +00004862 NEXT;
4863 } else if (RAW == '*') {
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004864 if (ret != NULL) {
Daniel Veillarde470df72001-04-18 21:41:07 +00004865 ret->ocur = XML_ELEMENT_CONTENT_MULT;
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004866 cur = ret;
4867 /*
4868 * Some normalization:
4869 * (a | b* | c?)* == (a | b | c)*
4870 */
4871 while (cur->type == XML_ELEMENT_CONTENT_OR) {
4872 if ((cur->c1 != NULL) &&
4873 ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4874 (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT)))
4875 cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
4876 if ((cur->c2 != NULL) &&
4877 ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
4878 (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT)))
4879 cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
4880 cur = cur->c2;
4881 }
4882 }
Owen Taylor3473f882001-02-23 17:55:21 +00004883 NEXT;
4884 } else if (RAW == '+') {
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004885 if (ret != NULL) {
4886 int found = 0;
4887
William M. Brackf8f2e8f2004-05-14 04:37:41 +00004888 if ((ret->ocur == XML_ELEMENT_CONTENT_OPT) ||
4889 (ret->ocur == XML_ELEMENT_CONTENT_MULT))
4890 ret->ocur = XML_ELEMENT_CONTENT_MULT;
William M. Brackeb8509c2004-05-14 03:48:02 +00004891 else
4892 ret->ocur = XML_ELEMENT_CONTENT_PLUS;
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004893 /*
4894 * Some normalization:
4895 * (a | b*)+ == (a | b)*
4896 * (a | b?)+ == (a | b)*
4897 */
4898 while (cur->type == XML_ELEMENT_CONTENT_OR) {
4899 if ((cur->c1 != NULL) &&
4900 ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4901 (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4902 cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
4903 found = 1;
4904 }
4905 if ((cur->c2 != NULL) &&
4906 ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
4907 (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) {
4908 cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
4909 found = 1;
4910 }
4911 cur = cur->c2;
4912 }
4913 if (found)
4914 ret->ocur = XML_ELEMENT_CONTENT_MULT;
4915 }
Owen Taylor3473f882001-02-23 17:55:21 +00004916 NEXT;
4917 }
4918 return(ret);
4919}
4920
4921/**
4922 * xmlParseElementContentDecl:
4923 * @ctxt: an XML parser context
4924 * @name: the name of the element being defined.
4925 * @result: the Element Content pointer will be stored here if any
4926 *
4927 * parse the declaration for an Element content either Mixed or Children,
4928 * the cases EMPTY and ANY are handled directly in xmlParseElementDecl
4929 *
4930 * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children
4931 *
4932 * returns: the type of element content XML_ELEMENT_TYPE_xxx
4933 */
4934
4935int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004936xmlParseElementContentDecl(xmlParserCtxtPtr ctxt, const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00004937 xmlElementContentPtr *result) {
4938
4939 xmlElementContentPtr tree = NULL;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004940 int inputid = ctxt->input->id;
Owen Taylor3473f882001-02-23 17:55:21 +00004941 int res;
4942
4943 *result = NULL;
4944
4945 if (RAW != '(') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00004946 xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004947 "xmlParseElementContentDecl : %s '(' expected\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00004948 return(-1);
4949 }
4950 NEXT;
4951 GROW;
4952 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004953 if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004954 tree = xmlParseElementMixedContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004955 res = XML_ELEMENT_TYPE_MIXED;
4956 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004957 tree = xmlParseElementChildrenContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004958 res = XML_ELEMENT_TYPE_ELEMENT;
4959 }
Owen Taylor3473f882001-02-23 17:55:21 +00004960 SKIP_BLANKS;
4961 *result = tree;
4962 return(res);
4963}
4964
4965/**
4966 * xmlParseElementDecl:
4967 * @ctxt: an XML parser context
4968 *
4969 * parse an Element declaration.
4970 *
4971 * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>'
4972 *
4973 * [ VC: Unique Element Type Declaration ]
4974 * No element type may be declared more than once
4975 *
4976 * Returns the type of the element, or -1 in case of error
4977 */
4978int
4979xmlParseElementDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004980 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00004981 int ret = -1;
4982 xmlElementContentPtr content = NULL;
4983
4984 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004985 if (CMP9(CUR_PTR, '<', '!', 'E', 'L', 'E', 'M', 'E', 'N', 'T')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004986 xmlParserInputPtr input = ctxt->input;
4987
4988 SKIP(9);
William M. Brack76e95df2003-10-18 16:20:14 +00004989 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004990 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4991 "Space required after 'ELEMENT'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004992 }
4993 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004994 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004995 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004996 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4997 "xmlParseElementDecl: no name for Element\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004998 return(-1);
4999 }
5000 while ((RAW == 0) && (ctxt->inputNr > 1))
5001 xmlPopInput(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00005002 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005003 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5004 "Space required after the element name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005005 }
5006 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00005007 if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005008 SKIP(5);
5009 /*
5010 * Element must always be empty.
5011 */
5012 ret = XML_ELEMENT_TYPE_EMPTY;
5013 } else if ((RAW == 'A') && (NXT(1) == 'N') &&
5014 (NXT(2) == 'Y')) {
5015 SKIP(3);
5016 /*
5017 * Element is a generic container.
5018 */
5019 ret = XML_ELEMENT_TYPE_ANY;
5020 } else if (RAW == '(') {
5021 ret = xmlParseElementContentDecl(ctxt, name, &content);
5022 } else {
5023 /*
5024 * [ WFC: PEs in Internal Subset ] error handling.
5025 */
5026 if ((RAW == '%') && (ctxt->external == 0) &&
5027 (ctxt->inputNr == 1)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005028 xmlFatalErrMsg(ctxt, XML_ERR_PEREF_IN_INT_SUBSET,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005029 "PEReference: forbidden within markup decl in internal subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005030 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00005031 xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
Owen Taylor3473f882001-02-23 17:55:21 +00005032 "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n");
5033 }
Owen Taylor3473f882001-02-23 17:55:21 +00005034 return(-1);
5035 }
5036
5037 SKIP_BLANKS;
5038 /*
5039 * Pop-up of finished entities.
5040 */
5041 while ((RAW == 0) && (ctxt->inputNr > 1))
5042 xmlPopInput(ctxt);
5043 SKIP_BLANKS;
5044
5045 if (RAW != '>') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005046 xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005047 } else {
5048 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005049 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
5050 "Element declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005051 }
5052
5053 NEXT;
5054 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
5055 (ctxt->sax->elementDecl != NULL))
5056 ctxt->sax->elementDecl(ctxt->userData, name, ret,
5057 content);
5058 }
5059 if (content != NULL) {
5060 xmlFreeElementContent(content);
5061 }
Owen Taylor3473f882001-02-23 17:55:21 +00005062 }
5063 return(ret);
5064}
5065
5066/**
Owen Taylor3473f882001-02-23 17:55:21 +00005067 * xmlParseConditionalSections
5068 * @ctxt: an XML parser context
5069 *
5070 * [61] conditionalSect ::= includeSect | ignoreSect
5071 * [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>'
5072 * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>'
5073 * [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)*
5074 * [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*)
5075 */
5076
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005077static void
Owen Taylor3473f882001-02-23 17:55:21 +00005078xmlParseConditionalSections(xmlParserCtxtPtr ctxt) {
5079 SKIP(3);
5080 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00005081 if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005082 SKIP(7);
5083 SKIP_BLANKS;
5084 if (RAW != '[') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005085 xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005086 } else {
5087 NEXT;
5088 }
5089 if (xmlParserDebugEntities) {
5090 if ((ctxt->input != NULL) && (ctxt->input->filename))
5091 xmlGenericError(xmlGenericErrorContext,
5092 "%s(%d): ", ctxt->input->filename,
5093 ctxt->input->line);
5094 xmlGenericError(xmlGenericErrorContext,
5095 "Entering INCLUDE Conditional Section\n");
5096 }
5097
5098 while ((RAW != 0) && ((RAW != ']') || (NXT(1) != ']') ||
5099 (NXT(2) != '>'))) {
5100 const xmlChar *check = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00005101 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00005102
5103 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5104 xmlParseConditionalSections(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00005105 } else if (IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005106 NEXT;
5107 } else if (RAW == '%') {
5108 xmlParsePEReference(ctxt);
5109 } else
5110 xmlParseMarkupDecl(ctxt);
5111
5112 /*
5113 * Pop-up of finished entities.
5114 */
5115 while ((RAW == 0) && (ctxt->inputNr > 1))
5116 xmlPopInput(ctxt);
5117
Daniel Veillardfdc91562002-07-01 21:52:03 +00005118 if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005119 xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005120 break;
5121 }
5122 }
5123 if (xmlParserDebugEntities) {
5124 if ((ctxt->input != NULL) && (ctxt->input->filename))
5125 xmlGenericError(xmlGenericErrorContext,
5126 "%s(%d): ", ctxt->input->filename,
5127 ctxt->input->line);
5128 xmlGenericError(xmlGenericErrorContext,
5129 "Leaving INCLUDE Conditional Section\n");
5130 }
5131
Daniel Veillarda07050d2003-10-19 14:46:32 +00005132 } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005133 int state;
William M. Brack78637da2003-07-31 14:47:38 +00005134 xmlParserInputState instate;
Owen Taylor3473f882001-02-23 17:55:21 +00005135 int depth = 0;
5136
5137 SKIP(6);
5138 SKIP_BLANKS;
5139 if (RAW != '[') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005140 xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005141 } else {
5142 NEXT;
5143 }
5144 if (xmlParserDebugEntities) {
5145 if ((ctxt->input != NULL) && (ctxt->input->filename))
5146 xmlGenericError(xmlGenericErrorContext,
5147 "%s(%d): ", ctxt->input->filename,
5148 ctxt->input->line);
5149 xmlGenericError(xmlGenericErrorContext,
5150 "Entering IGNORE Conditional Section\n");
5151 }
5152
5153 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005154 * Parse up to the end of the conditional section
Owen Taylor3473f882001-02-23 17:55:21 +00005155 * But disable SAX event generating DTD building in the meantime
5156 */
5157 state = ctxt->disableSAX;
5158 instate = ctxt->instate;
Daniel Veillarddad3f682002-11-17 16:47:27 +00005159 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00005160 ctxt->instate = XML_PARSER_IGNORE;
5161
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00005162 while ((depth >= 0) && (RAW != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005163 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5164 depth++;
5165 SKIP(3);
5166 continue;
5167 }
5168 if ((RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) {
5169 if (--depth >= 0) SKIP(3);
5170 continue;
5171 }
5172 NEXT;
5173 continue;
5174 }
5175
5176 ctxt->disableSAX = state;
5177 ctxt->instate = instate;
5178
5179 if (xmlParserDebugEntities) {
5180 if ((ctxt->input != NULL) && (ctxt->input->filename))
5181 xmlGenericError(xmlGenericErrorContext,
5182 "%s(%d): ", ctxt->input->filename,
5183 ctxt->input->line);
5184 xmlGenericError(xmlGenericErrorContext,
5185 "Leaving IGNORE Conditional Section\n");
5186 }
5187
5188 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005189 xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005190 }
5191
5192 if (RAW == 0)
5193 SHRINK;
5194
5195 if (RAW == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005196 xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005197 } else {
5198 SKIP(3);
5199 }
5200}
5201
5202/**
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005203 * xmlParseMarkupDecl:
5204 * @ctxt: an XML parser context
5205 *
5206 * parse Markup declarations
5207 *
5208 * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl |
5209 * NotationDecl | PI | Comment
5210 *
5211 * [ VC: Proper Declaration/PE Nesting ]
5212 * Parameter-entity replacement text must be properly nested with
5213 * markup declarations. That is to say, if either the first character
5214 * or the last character of a markup declaration (markupdecl above) is
5215 * contained in the replacement text for a parameter-entity reference,
5216 * both must be contained in the same replacement text.
5217 *
5218 * [ WFC: PEs in Internal Subset ]
5219 * In the internal DTD subset, parameter-entity references can occur
5220 * only where markup declarations can occur, not within markup declarations.
5221 * (This does not apply to references that occur in external parameter
5222 * entities or to the external subset.)
5223 */
5224void
5225xmlParseMarkupDecl(xmlParserCtxtPtr ctxt) {
5226 GROW;
5227 xmlParseElementDecl(ctxt);
5228 xmlParseAttributeListDecl(ctxt);
5229 xmlParseEntityDecl(ctxt);
5230 xmlParseNotationDecl(ctxt);
5231 xmlParsePI(ctxt);
5232 xmlParseComment(ctxt);
5233 /*
5234 * This is only for internal subset. On external entities,
5235 * the replacement is done before parsing stage
5236 */
5237 if ((ctxt->external == 0) && (ctxt->inputNr == 1))
5238 xmlParsePEReference(ctxt);
5239
5240 /*
5241 * Conditional sections are allowed from entities included
5242 * by PE References in the internal subset.
5243 */
5244 if ((ctxt->external == 0) && (ctxt->inputNr > 1)) {
5245 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5246 xmlParseConditionalSections(ctxt);
5247 }
5248 }
5249
5250 ctxt->instate = XML_PARSER_DTD;
5251}
5252
5253/**
5254 * xmlParseTextDecl:
5255 * @ctxt: an XML parser context
5256 *
5257 * parse an XML declaration header for external entities
5258 *
5259 * [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>'
5260 *
5261 * Question: Seems that EncodingDecl is mandatory ? Is that a typo ?
5262 */
5263
5264void
5265xmlParseTextDecl(xmlParserCtxtPtr ctxt) {
5266 xmlChar *version;
Daniel Veillardf5cb3cd2003-10-28 13:58:13 +00005267 const xmlChar *encoding;
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005268
5269 /*
5270 * We know that '<?xml' is here.
5271 */
Daniel Veillarda07050d2003-10-19 14:46:32 +00005272 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005273 SKIP(5);
5274 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005275 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005276 return;
5277 }
5278
William M. Brack76e95df2003-10-18 16:20:14 +00005279 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005280 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5281 "Space needed after '<?xml'\n");
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005282 }
5283 SKIP_BLANKS;
5284
5285 /*
5286 * We may have the VersionInfo here.
5287 */
5288 version = xmlParseVersionInfo(ctxt);
5289 if (version == NULL)
5290 version = xmlCharStrdup(XML_DEFAULT_VERSION);
Daniel Veillard401c2112002-01-07 16:54:10 +00005291 else {
William M. Brack76e95df2003-10-18 16:20:14 +00005292 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005293 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5294 "Space needed here\n");
Daniel Veillard401c2112002-01-07 16:54:10 +00005295 }
5296 }
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005297 ctxt->input->version = version;
5298
5299 /*
5300 * We must have the encoding declaration
5301 */
Daniel Veillardf5cb3cd2003-10-28 13:58:13 +00005302 encoding = xmlParseEncodingDecl(ctxt);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005303 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
5304 /*
5305 * The XML REC instructs us to stop parsing right here
5306 */
5307 return;
5308 }
Daniel Veillardf5cb3cd2003-10-28 13:58:13 +00005309 if ((encoding == NULL) && (ctxt->errNo == XML_ERR_OK)) {
5310 xmlFatalErrMsg(ctxt, XML_ERR_MISSING_ENCODING,
5311 "Missing encoding in text declaration\n");
5312 }
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005313
5314 SKIP_BLANKS;
5315 if ((RAW == '?') && (NXT(1) == '>')) {
5316 SKIP(2);
5317 } else if (RAW == '>') {
5318 /* Deprecated old WD ... */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005319 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005320 NEXT;
5321 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005322 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005323 MOVETO_ENDTAG(CUR_PTR);
5324 NEXT;
5325 }
5326}
5327
5328/**
Owen Taylor3473f882001-02-23 17:55:21 +00005329 * xmlParseExternalSubset:
5330 * @ctxt: an XML parser context
5331 * @ExternalID: the external identifier
5332 * @SystemID: the system identifier (or URL)
5333 *
5334 * parse Markup declarations from an external subset
5335 *
5336 * [30] extSubset ::= textDecl? extSubsetDecl
5337 *
5338 * [31] extSubsetDecl ::= (markupdecl | conditionalSect | PEReference | S) *
5339 */
5340void
5341xmlParseExternalSubset(xmlParserCtxtPtr ctxt, const xmlChar *ExternalID,
5342 const xmlChar *SystemID) {
Daniel Veillard309f81d2003-09-23 09:02:53 +00005343 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005344 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00005345 if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005346 xmlParseTextDecl(ctxt);
5347 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
5348 /*
5349 * The XML REC instructs us to stop parsing right here
5350 */
5351 ctxt->instate = XML_PARSER_EOF;
5352 return;
5353 }
5354 }
5355 if (ctxt->myDoc == NULL) {
5356 ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
5357 }
5358 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset == NULL))
5359 xmlCreateIntSubset(ctxt->myDoc, NULL, ExternalID, SystemID);
5360
5361 ctxt->instate = XML_PARSER_DTD;
5362 ctxt->external = 1;
5363 while (((RAW == '<') && (NXT(1) == '?')) ||
5364 ((RAW == '<') && (NXT(1) == '!')) ||
William M. Brack76e95df2003-10-18 16:20:14 +00005365 (RAW == '%') || IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005366 const xmlChar *check = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00005367 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00005368
5369 GROW;
5370 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5371 xmlParseConditionalSections(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00005372 } else if (IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005373 NEXT;
5374 } else if (RAW == '%') {
5375 xmlParsePEReference(ctxt);
5376 } else
5377 xmlParseMarkupDecl(ctxt);
5378
5379 /*
5380 * Pop-up of finished entities.
5381 */
5382 while ((RAW == 0) && (ctxt->inputNr > 1))
5383 xmlPopInput(ctxt);
5384
Daniel Veillardfdc91562002-07-01 21:52:03 +00005385 if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005386 xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005387 break;
5388 }
5389 }
5390
5391 if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005392 xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005393 }
5394
5395}
5396
5397/**
5398 * xmlParseReference:
5399 * @ctxt: an XML parser context
5400 *
5401 * parse and handle entity references in content, depending on the SAX
5402 * interface, this may end-up in a call to character() if this is a
5403 * CharRef, a predefined entity, if there is no reference() callback.
5404 * or if the parser was asked to switch to that mode.
5405 *
5406 * [67] Reference ::= EntityRef | CharRef
5407 */
5408void
5409xmlParseReference(xmlParserCtxtPtr ctxt) {
5410 xmlEntityPtr ent;
5411 xmlChar *val;
5412 if (RAW != '&') return;
5413
5414 if (NXT(1) == '#') {
5415 int i = 0;
5416 xmlChar out[10];
5417 int hex = NXT(2);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005418 int value = xmlParseCharRef(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005419
5420 if (ctxt->charset != XML_CHAR_ENCODING_UTF8) {
5421 /*
5422 * So we are using non-UTF-8 buffers
5423 * Check that the char fit on 8bits, if not
5424 * generate a CharRef.
5425 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005426 if (value <= 0xFF) {
5427 out[0] = value;
Owen Taylor3473f882001-02-23 17:55:21 +00005428 out[1] = 0;
5429 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
5430 (!ctxt->disableSAX))
5431 ctxt->sax->characters(ctxt->userData, out, 1);
5432 } else {
5433 if ((hex == 'x') || (hex == 'X'))
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005434 snprintf((char *)out, sizeof(out), "#x%X", value);
Owen Taylor3473f882001-02-23 17:55:21 +00005435 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005436 snprintf((char *)out, sizeof(out), "#%d", value);
Owen Taylor3473f882001-02-23 17:55:21 +00005437 if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) &&
5438 (!ctxt->disableSAX))
5439 ctxt->sax->reference(ctxt->userData, out);
5440 }
5441 } else {
5442 /*
5443 * Just encode the value in UTF-8
5444 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005445 COPY_BUF(0 ,out, i, value);
Owen Taylor3473f882001-02-23 17:55:21 +00005446 out[i] = 0;
5447 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
5448 (!ctxt->disableSAX))
5449 ctxt->sax->characters(ctxt->userData, out, i);
5450 }
5451 } else {
5452 ent = xmlParseEntityRef(ctxt);
5453 if (ent == NULL) return;
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00005454 if (!ctxt->wellFormed)
5455 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005456 if ((ent->name != NULL) &&
5457 (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY)) {
5458 xmlNodePtr list = NULL;
Daniel Veillard7d515752003-09-26 19:12:37 +00005459 xmlParserErrors ret = XML_ERR_OK;
Owen Taylor3473f882001-02-23 17:55:21 +00005460
5461
5462 /*
5463 * The first reference to the entity trigger a parsing phase
5464 * where the ent->children is filled with the result from
5465 * the parsing.
5466 */
5467 if (ent->children == NULL) {
5468 xmlChar *value;
5469 value = ent->content;
5470
5471 /*
5472 * Check that this entity is well formed
5473 */
Daniel Veillard4aede2e2003-10-17 12:43:59 +00005474 if ((value != NULL) && (value[0] != 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +00005475 (value[1] == 0) && (value[0] == '<') &&
5476 (xmlStrEqual(ent->name, BAD_CAST "lt"))) {
5477 /*
5478 * DONE: get definite answer on this !!!
5479 * Lots of entity decls are used to declare a single
5480 * char
5481 * <!ENTITY lt "<">
5482 * Which seems to be valid since
5483 * 2.4: The ampersand character (&) and the left angle
5484 * bracket (<) may appear in their literal form only
5485 * when used ... They are also legal within the literal
5486 * entity value of an internal entity declaration;i
5487 * see "4.3.2 Well-Formed Parsed Entities".
5488 * IMHO 2.4 and 4.3.2 are directly in contradiction.
5489 * Looking at the OASIS test suite and James Clark
5490 * tests, this is broken. However the XML REC uses
5491 * it. Is the XML REC not well-formed ????
5492 * This is a hack to avoid this problem
5493 *
5494 * ANSWER: since lt gt amp .. are already defined,
5495 * this is a redefinition and hence the fact that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005496 * content is not well balanced is not a Wf error, this
Owen Taylor3473f882001-02-23 17:55:21 +00005497 * is lousy but acceptable.
5498 */
5499 list = xmlNewDocText(ctxt->myDoc, value);
5500 if (list != NULL) {
5501 if ((ent->etype == XML_INTERNAL_GENERAL_ENTITY) &&
5502 (ent->children == NULL)) {
5503 ent->children = list;
5504 ent->last = list;
Daniel Veillard2d84a892002-12-30 00:01:08 +00005505 ent->owner = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00005506 list->parent = (xmlNodePtr) ent;
5507 } else {
5508 xmlFreeNodeList(list);
5509 }
5510 } else if (list != NULL) {
5511 xmlFreeNodeList(list);
5512 }
5513 } else {
5514 /*
5515 * 4.3.2: An internal general parsed entity is well-formed
5516 * if its replacement text matches the production labeled
5517 * content.
5518 */
Daniel Veillardb5a60ec2002-03-18 11:45:56 +00005519
5520 void *user_data;
5521 /*
5522 * This is a bit hackish but this seems the best
5523 * way to make sure both SAX and DOM entity support
5524 * behaves okay.
5525 */
5526 if (ctxt->userData == ctxt)
5527 user_data = NULL;
5528 else
5529 user_data = ctxt->userData;
5530
Owen Taylor3473f882001-02-23 17:55:21 +00005531 if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) {
5532 ctxt->depth++;
Daniel Veillard328f48c2002-11-15 15:24:34 +00005533 ret = xmlParseBalancedChunkMemoryInternal(ctxt,
5534 value, user_data, &list);
Owen Taylor3473f882001-02-23 17:55:21 +00005535 ctxt->depth--;
5536 } else if (ent->etype ==
5537 XML_EXTERNAL_GENERAL_PARSED_ENTITY) {
5538 ctxt->depth++;
Daniel Veillarda97a19b2001-05-20 13:19:52 +00005539 ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt,
Daniel Veillardb5a60ec2002-03-18 11:45:56 +00005540 ctxt->sax, user_data, ctxt->depth,
Daniel Veillarda97a19b2001-05-20 13:19:52 +00005541 ent->URI, ent->ExternalID, &list);
Owen Taylor3473f882001-02-23 17:55:21 +00005542 ctxt->depth--;
5543 } else {
Daniel Veillard7d515752003-09-26 19:12:37 +00005544 ret = XML_ERR_ENTITY_PE_INTERNAL;
Daniel Veillardf403d292003-10-05 13:51:35 +00005545 xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR,
5546 "invalid entity type found\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005547 }
5548 if (ret == XML_ERR_ENTITY_LOOP) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005549 xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00005550 return;
Daniel Veillard7d515752003-09-26 19:12:37 +00005551 } else if ((ret == XML_ERR_OK) && (list != NULL)) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00005552 if (((ent->etype == XML_INTERNAL_GENERAL_ENTITY) ||
5553 (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY))&&
Owen Taylor3473f882001-02-23 17:55:21 +00005554 (ent->children == NULL)) {
5555 ent->children = list;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005556 if (ctxt->replaceEntities) {
5557 /*
5558 * Prune it directly in the generated document
5559 * except for single text nodes.
5560 */
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005561 if (((list->type == XML_TEXT_NODE) &&
5562 (list->next == NULL)) ||
5563 (ctxt->parseMode == XML_PARSE_READER)) {
Daniel Veillard62f313b2001-07-04 19:49:14 +00005564 list->parent = (xmlNodePtr) ent;
5565 list = NULL;
Daniel Veillard2d84a892002-12-30 00:01:08 +00005566 ent->owner = 1;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005567 } else {
Daniel Veillard2d84a892002-12-30 00:01:08 +00005568 ent->owner = 0;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005569 while (list != NULL) {
5570 list->parent = (xmlNodePtr) ctxt->node;
Daniel Veillard68e9e742002-11-16 15:35:11 +00005571 list->doc = ctxt->myDoc;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005572 if (list->next == NULL)
5573 ent->last = list;
5574 list = list->next;
Daniel Veillard8107a222002-01-13 14:10:10 +00005575 }
Daniel Veillard62f313b2001-07-04 19:49:14 +00005576 list = ent->children;
Daniel Veillard81273902003-09-30 00:43:48 +00005577#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +00005578 if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
5579 xmlAddEntityReference(ent, list, NULL);
Daniel Veillard81273902003-09-30 00:43:48 +00005580#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillard62f313b2001-07-04 19:49:14 +00005581 }
5582 } else {
Daniel Veillard2d84a892002-12-30 00:01:08 +00005583 ent->owner = 1;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005584 while (list != NULL) {
5585 list->parent = (xmlNodePtr) ent;
5586 if (list->next == NULL)
5587 ent->last = list;
5588 list = list->next;
5589 }
Owen Taylor3473f882001-02-23 17:55:21 +00005590 }
5591 } else {
5592 xmlFreeNodeList(list);
Daniel Veillard62f313b2001-07-04 19:49:14 +00005593 list = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005594 }
William M. Brackb670e2e2003-09-27 01:05:55 +00005595 } else if ((ret != XML_ERR_OK) &&
5596 (ret != XML_WAR_UNDECLARED_ENTITY)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005597 xmlFatalErr(ctxt, ret, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005598 } else if (list != NULL) {
5599 xmlFreeNodeList(list);
Daniel Veillard62f313b2001-07-04 19:49:14 +00005600 list = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005601 }
5602 }
5603 }
5604 if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) &&
5605 (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) {
5606 /*
5607 * Create a node.
5608 */
5609 ctxt->sax->reference(ctxt->userData, ent->name);
5610 return;
5611 } else if (ctxt->replaceEntities) {
5612 if ((ctxt->node != NULL) && (ent->children != NULL)) {
5613 /*
5614 * Seems we are generating the DOM content, do
Daniel Veillard62f313b2001-07-04 19:49:14 +00005615 * a simple tree copy for all references except the first
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005616 * In the first occurrence list contains the replacement.
5617 * progressive == 2 means we are operating on the Reader
5618 * and since nodes are discarded we must copy all the time.
Owen Taylor3473f882001-02-23 17:55:21 +00005619 */
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005620 if (((list == NULL) && (ent->owner == 0)) ||
5621 (ctxt->parseMode == XML_PARSE_READER)) {
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005622 xmlNodePtr nw = NULL, cur, firstChild = NULL;
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005623
5624 /*
5625 * when operating on a reader, the entities definitions
5626 * are always owning the entities subtree.
5627 if (ctxt->parseMode == XML_PARSE_READER)
5628 ent->owner = 1;
5629 */
5630
Daniel Veillard62f313b2001-07-04 19:49:14 +00005631 cur = ent->children;
5632 while (cur != NULL) {
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005633 nw = xmlCopyNode(cur, 1);
5634 if (nw != NULL) {
5635 nw->_private = cur->_private;
Daniel Veillard8f872442003-01-09 23:19:02 +00005636 if (firstChild == NULL){
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005637 firstChild = nw;
Daniel Veillard8f872442003-01-09 23:19:02 +00005638 }
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005639 nw = xmlAddChild(ctxt->node, nw);
Daniel Veillard8107a222002-01-13 14:10:10 +00005640 }
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005641 if (cur == ent->last) {
5642 /*
5643 * needed to detect some strange empty
5644 * node cases in the reader tests
5645 */
5646 if ((ctxt->parseMode == XML_PARSE_READER) &&
5647 (nw->type == XML_ELEMENT_NODE) &&
5648 (nw->children == NULL))
5649 nw->extra = 1;
5650
Daniel Veillard62f313b2001-07-04 19:49:14 +00005651 break;
Daniel Veillard0df3bc32004-06-08 12:03:41 +00005652 }
Daniel Veillard62f313b2001-07-04 19:49:14 +00005653 cur = cur->next;
5654 }
Daniel Veillard81273902003-09-30 00:43:48 +00005655#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +00005656 if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005657 xmlAddEntityReference(ent, firstChild, nw);
Daniel Veillard81273902003-09-30 00:43:48 +00005658#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005659 } else if (list == NULL) {
5660 xmlNodePtr nw = NULL, cur, next, last,
5661 firstChild = NULL;
5662 /*
5663 * Copy the entity child list and make it the new
5664 * entity child list. The goal is to make sure any
5665 * ID or REF referenced will be the one from the
5666 * document content and not the entity copy.
5667 */
5668 cur = ent->children;
5669 ent->children = NULL;
5670 last = ent->last;
5671 ent->last = NULL;
5672 while (cur != NULL) {
5673 next = cur->next;
5674 cur->next = NULL;
5675 cur->parent = NULL;
5676 nw = xmlCopyNode(cur, 1);
5677 if (nw != NULL) {
5678 nw->_private = cur->_private;
5679 if (firstChild == NULL){
5680 firstChild = cur;
5681 }
5682 xmlAddChild((xmlNodePtr) ent, nw);
5683 xmlAddChild(ctxt->node, cur);
5684 }
5685 if (cur == last)
5686 break;
5687 cur = next;
5688 }
5689 ent->owner = 1;
Daniel Veillard81273902003-09-30 00:43:48 +00005690#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005691 if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
5692 xmlAddEntityReference(ent, firstChild, nw);
Daniel Veillard81273902003-09-30 00:43:48 +00005693#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillard62f313b2001-07-04 19:49:14 +00005694 } else {
Daniel Veillard370ba3d2004-10-25 16:23:56 +00005695 const xmlChar *nbktext;
5696
Daniel Veillard62f313b2001-07-04 19:49:14 +00005697 /*
5698 * the name change is to avoid coalescing of the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005699 * node with a possible previous text one which
5700 * would make ent->children a dangling pointer
Daniel Veillard62f313b2001-07-04 19:49:14 +00005701 */
Daniel Veillard370ba3d2004-10-25 16:23:56 +00005702 nbktext = xmlDictLookup(ctxt->dict, BAD_CAST "nbktext",
5703 -1);
Daniel Veillard62f313b2001-07-04 19:49:14 +00005704 if (ent->children->type == XML_TEXT_NODE)
Daniel Veillard370ba3d2004-10-25 16:23:56 +00005705 ent->children->name = nbktext;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005706 if ((ent->last != ent->children) &&
5707 (ent->last->type == XML_TEXT_NODE))
Daniel Veillard370ba3d2004-10-25 16:23:56 +00005708 ent->last->name = nbktext;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005709 xmlAddChildList(ctxt->node, ent->children);
5710 }
5711
Owen Taylor3473f882001-02-23 17:55:21 +00005712 /*
5713 * This is to avoid a nasty side effect, see
5714 * characters() in SAX.c
5715 */
5716 ctxt->nodemem = 0;
5717 ctxt->nodelen = 0;
5718 return;
5719 } else {
5720 /*
5721 * Probably running in SAX mode
5722 */
5723 xmlParserInputPtr input;
5724
5725 input = xmlNewEntityInputStream(ctxt, ent);
5726 xmlPushInput(ctxt, input);
5727 if ((ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) &&
Daniel Veillarda07050d2003-10-19 14:46:32 +00005728 (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) &&
5729 (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00005730 xmlParseTextDecl(ctxt);
5731 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
5732 /*
5733 * The XML REC instructs us to stop parsing right here
5734 */
5735 ctxt->instate = XML_PARSER_EOF;
5736 return;
5737 }
5738 if (input->standalone == 1) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005739 xmlFatalErr(ctxt, XML_ERR_EXT_ENTITY_STANDALONE,
5740 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005741 }
5742 }
5743 return;
5744 }
5745 }
5746 } else {
5747 val = ent->content;
5748 if (val == NULL) return;
5749 /*
5750 * inline the entity.
5751 */
5752 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
5753 (!ctxt->disableSAX))
5754 ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val));
5755 }
5756 }
5757}
5758
5759/**
5760 * xmlParseEntityRef:
5761 * @ctxt: an XML parser context
5762 *
5763 * parse ENTITY references declarations
5764 *
5765 * [68] EntityRef ::= '&' Name ';'
5766 *
5767 * [ WFC: Entity Declared ]
5768 * In a document without any DTD, a document with only an internal DTD
5769 * subset which contains no parameter entity references, or a document
5770 * with "standalone='yes'", the Name given in the entity reference
5771 * must match that in an entity declaration, except that well-formed
5772 * documents need not declare any of the following entities: amp, lt,
5773 * gt, apos, quot. The declaration of a parameter entity must precede
5774 * any reference to it. Similarly, the declaration of a general entity
5775 * must precede any reference to it which appears in a default value in an
5776 * attribute-list declaration. Note that if entities are declared in the
5777 * external subset or in external parameter entities, a non-validating
5778 * processor is not obligated to read and process their declarations;
5779 * for such documents, the rule that an entity must be declared is a
5780 * well-formedness constraint only if standalone='yes'.
5781 *
5782 * [ WFC: Parsed Entity ]
5783 * An entity reference must not contain the name of an unparsed entity
5784 *
5785 * Returns the xmlEntityPtr if found, or NULL otherwise.
5786 */
5787xmlEntityPtr
5788xmlParseEntityRef(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00005789 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00005790 xmlEntityPtr ent = NULL;
5791
5792 GROW;
5793
5794 if (RAW == '&') {
5795 NEXT;
5796 name = xmlParseName(ctxt);
5797 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005798 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
5799 "xmlParseEntityRef: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005800 } else {
5801 if (RAW == ';') {
5802 NEXT;
5803 /*
5804 * Ask first SAX for entity resolution, otherwise try the
5805 * predefined set.
5806 */
5807 if (ctxt->sax != NULL) {
5808 if (ctxt->sax->getEntity != NULL)
5809 ent = ctxt->sax->getEntity(ctxt->userData, name);
Daniel Veillard39eb88b2003-03-11 11:21:28 +00005810 if ((ctxt->wellFormed == 1 ) && (ent == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00005811 ent = xmlGetPredefinedEntity(name);
Daniel Veillard39eb88b2003-03-11 11:21:28 +00005812 if ((ctxt->wellFormed == 1 ) && (ent == NULL) &&
5813 (ctxt->userData==ctxt)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00005814 ent = xmlSAX2GetEntity(ctxt, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00005815 }
Owen Taylor3473f882001-02-23 17:55:21 +00005816 }
5817 /*
5818 * [ WFC: Entity Declared ]
5819 * In a document without any DTD, a document with only an
5820 * internal DTD subset which contains no parameter entity
5821 * references, or a document with "standalone='yes'", the
5822 * Name given in the entity reference must match that in an
5823 * entity declaration, except that well-formed documents
5824 * need not declare any of the following entities: amp, lt,
5825 * gt, apos, quot.
5826 * The declaration of a parameter entity must precede any
5827 * reference to it.
5828 * Similarly, the declaration of a general entity must
5829 * precede any reference to it which appears in a default
5830 * value in an attribute-list declaration. Note that if
5831 * entities are declared in the external subset or in
5832 * external parameter entities, a non-validating processor
5833 * is not obligated to read and process their declarations;
5834 * for such documents, the rule that an entity must be
5835 * declared is a well-formedness constraint only if
5836 * standalone='yes'.
5837 */
5838 if (ent == NULL) {
5839 if ((ctxt->standalone == 1) ||
5840 ((ctxt->hasExternalSubset == 0) &&
5841 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005842 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00005843 "Entity '%s' not defined\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005844 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00005845 xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00005846 "Entity '%s' not defined\n", name);
5847 }
Daniel Veillardf403d292003-10-05 13:51:35 +00005848 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005849 }
5850
5851 /*
5852 * [ WFC: Parsed Entity ]
5853 * An entity reference must not contain the name of an
5854 * unparsed entity
5855 */
5856 else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005857 xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00005858 "Entity reference to unparsed entity %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005859 }
5860
5861 /*
5862 * [ WFC: No External Entity References ]
5863 * Attribute values cannot contain direct or indirect
5864 * entity references to external entities.
5865 */
5866 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
5867 (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005868 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL,
5869 "Attribute references external entity '%s'\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005870 }
5871 /*
5872 * [ WFC: No < in Attribute Values ]
5873 * The replacement text of any entity referred to directly or
5874 * indirectly in an attribute value (other than "&lt;") must
5875 * not contain a <.
5876 */
5877 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
5878 (ent != NULL) &&
5879 (!xmlStrEqual(ent->name, BAD_CAST "lt")) &&
5880 (ent->content != NULL) &&
5881 (xmlStrchr(ent->content, '<'))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005882 xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
Owen Taylor3473f882001-02-23 17:55:21 +00005883 "'<' in entity '%s' is not allowed in attributes values\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005884 }
5885
5886 /*
5887 * Internal check, no parameter entities here ...
5888 */
5889 else {
5890 switch (ent->etype) {
5891 case XML_INTERNAL_PARAMETER_ENTITY:
5892 case XML_EXTERNAL_PARAMETER_ENTITY:
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005893 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER,
5894 "Attempt to reference the parameter entity '%s'\n",
5895 name);
Owen Taylor3473f882001-02-23 17:55:21 +00005896 break;
5897 default:
5898 break;
5899 }
5900 }
5901
5902 /*
5903 * [ WFC: No Recursion ]
5904 * A parsed entity must not contain a recursive reference
5905 * to itself, either directly or indirectly.
5906 * Done somewhere else
5907 */
5908
5909 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005910 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005911 }
Owen Taylor3473f882001-02-23 17:55:21 +00005912 }
5913 }
5914 return(ent);
5915}
5916
5917/**
5918 * xmlParseStringEntityRef:
5919 * @ctxt: an XML parser context
5920 * @str: a pointer to an index in the string
5921 *
5922 * parse ENTITY references declarations, but this version parses it from
5923 * a string value.
5924 *
5925 * [68] EntityRef ::= '&' Name ';'
5926 *
5927 * [ WFC: Entity Declared ]
5928 * In a document without any DTD, a document with only an internal DTD
5929 * subset which contains no parameter entity references, or a document
5930 * with "standalone='yes'", the Name given in the entity reference
5931 * must match that in an entity declaration, except that well-formed
5932 * documents need not declare any of the following entities: amp, lt,
5933 * gt, apos, quot. The declaration of a parameter entity must precede
5934 * any reference to it. Similarly, the declaration of a general entity
5935 * must precede any reference to it which appears in a default value in an
5936 * attribute-list declaration. Note that if entities are declared in the
5937 * external subset or in external parameter entities, a non-validating
5938 * processor is not obligated to read and process their declarations;
5939 * for such documents, the rule that an entity must be declared is a
5940 * well-formedness constraint only if standalone='yes'.
5941 *
5942 * [ WFC: Parsed Entity ]
5943 * An entity reference must not contain the name of an unparsed entity
5944 *
5945 * Returns the xmlEntityPtr if found, or NULL otherwise. The str pointer
5946 * is updated to the current location in the string.
5947 */
5948xmlEntityPtr
5949xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) {
5950 xmlChar *name;
5951 const xmlChar *ptr;
5952 xmlChar cur;
5953 xmlEntityPtr ent = NULL;
5954
5955 if ((str == NULL) || (*str == NULL))
5956 return(NULL);
5957 ptr = *str;
5958 cur = *ptr;
5959 if (cur == '&') {
5960 ptr++;
5961 cur = *ptr;
5962 name = xmlParseStringName(ctxt, &ptr);
5963 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005964 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
5965 "xmlParseStringEntityRef: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005966 } else {
5967 if (*ptr == ';') {
5968 ptr++;
5969 /*
5970 * Ask first SAX for entity resolution, otherwise try the
5971 * predefined set.
5972 */
5973 if (ctxt->sax != NULL) {
5974 if (ctxt->sax->getEntity != NULL)
5975 ent = ctxt->sax->getEntity(ctxt->userData, name);
5976 if (ent == NULL)
5977 ent = xmlGetPredefinedEntity(name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00005978 if ((ent == NULL) && (ctxt->userData==ctxt)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00005979 ent = xmlSAX2GetEntity(ctxt, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00005980 }
Owen Taylor3473f882001-02-23 17:55:21 +00005981 }
5982 /*
5983 * [ WFC: Entity Declared ]
5984 * In a document without any DTD, a document with only an
5985 * internal DTD subset which contains no parameter entity
5986 * references, or a document with "standalone='yes'", the
5987 * Name given in the entity reference must match that in an
5988 * entity declaration, except that well-formed documents
5989 * need not declare any of the following entities: amp, lt,
5990 * gt, apos, quot.
5991 * The declaration of a parameter entity must precede any
5992 * reference to it.
5993 * Similarly, the declaration of a general entity must
5994 * precede any reference to it which appears in a default
5995 * value in an attribute-list declaration. Note that if
5996 * entities are declared in the external subset or in
5997 * external parameter entities, a non-validating processor
5998 * is not obligated to read and process their declarations;
5999 * for such documents, the rule that an entity must be
6000 * declared is a well-formedness constraint only if
6001 * standalone='yes'.
6002 */
6003 if (ent == NULL) {
6004 if ((ctxt->standalone == 1) ||
6005 ((ctxt->hasExternalSubset == 0) &&
6006 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006007 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00006008 "Entity '%s' not defined\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006009 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00006010 xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY,
Daniel Veillard24eb9782003-10-04 21:08:09 +00006011 "Entity '%s' not defined\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00006012 name);
Owen Taylor3473f882001-02-23 17:55:21 +00006013 }
Daniel Veillard24eb9782003-10-04 21:08:09 +00006014 /* TODO ? check regressions ctxt->valid = 0; */
Owen Taylor3473f882001-02-23 17:55:21 +00006015 }
6016
6017 /*
6018 * [ WFC: Parsed Entity ]
6019 * An entity reference must not contain the name of an
6020 * unparsed entity
6021 */
6022 else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006023 xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00006024 "Entity reference to unparsed entity %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006025 }
6026
6027 /*
6028 * [ WFC: No External Entity References ]
6029 * Attribute values cannot contain direct or indirect
6030 * entity references to external entities.
6031 */
6032 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
6033 (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006034 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL,
Owen Taylor3473f882001-02-23 17:55:21 +00006035 "Attribute references external entity '%s'\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006036 }
6037 /*
6038 * [ WFC: No < in Attribute Values ]
6039 * The replacement text of any entity referred to directly or
6040 * indirectly in an attribute value (other than "&lt;") must
6041 * not contain a <.
6042 */
6043 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
6044 (ent != NULL) &&
6045 (!xmlStrEqual(ent->name, BAD_CAST "lt")) &&
6046 (ent->content != NULL) &&
6047 (xmlStrchr(ent->content, '<'))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006048 xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
6049 "'<' in entity '%s' is not allowed in attributes values\n",
6050 name);
Owen Taylor3473f882001-02-23 17:55:21 +00006051 }
6052
6053 /*
6054 * Internal check, no parameter entities here ...
6055 */
6056 else {
6057 switch (ent->etype) {
6058 case XML_INTERNAL_PARAMETER_ENTITY:
6059 case XML_EXTERNAL_PARAMETER_ENTITY:
Daniel Veillardf403d292003-10-05 13:51:35 +00006060 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER,
6061 "Attempt to reference the parameter entity '%s'\n",
6062 name);
Owen Taylor3473f882001-02-23 17:55:21 +00006063 break;
6064 default:
6065 break;
6066 }
6067 }
6068
6069 /*
6070 * [ WFC: No Recursion ]
6071 * A parsed entity must not contain a recursive reference
6072 * to itself, either directly or indirectly.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006073 * Done somewhere else
Owen Taylor3473f882001-02-23 17:55:21 +00006074 */
6075
6076 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006077 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006078 }
6079 xmlFree(name);
6080 }
6081 }
6082 *str = ptr;
6083 return(ent);
6084}
6085
6086/**
6087 * xmlParsePEReference:
6088 * @ctxt: an XML parser context
6089 *
6090 * parse PEReference declarations
6091 * The entity content is handled directly by pushing it's content as
6092 * a new input stream.
6093 *
6094 * [69] PEReference ::= '%' Name ';'
6095 *
6096 * [ WFC: No Recursion ]
6097 * A parsed entity must not contain a recursive
6098 * reference to itself, either directly or indirectly.
6099 *
6100 * [ WFC: Entity Declared ]
6101 * In a document without any DTD, a document with only an internal DTD
6102 * subset which contains no parameter entity references, or a document
6103 * with "standalone='yes'", ... ... The declaration of a parameter
6104 * entity must precede any reference to it...
6105 *
6106 * [ VC: Entity Declared ]
6107 * In a document with an external subset or external parameter entities
6108 * with "standalone='no'", ... ... The declaration of a parameter entity
6109 * must precede any reference to it...
6110 *
6111 * [ WFC: In DTD ]
6112 * Parameter-entity references may only appear in the DTD.
6113 * NOTE: misleading but this is handled.
6114 */
6115void
Daniel Veillard8f597c32003-10-06 08:19:27 +00006116xmlParsePEReference(xmlParserCtxtPtr ctxt)
6117{
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006118 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006119 xmlEntityPtr entity = NULL;
6120 xmlParserInputPtr input;
6121
6122 if (RAW == '%') {
6123 NEXT;
Daniel Veillard76d66f42001-05-16 21:05:17 +00006124 name = xmlParseName(ctxt);
Daniel Veillard8f597c32003-10-06 08:19:27 +00006125 if (name == NULL) {
6126 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6127 "xmlParsePEReference: no name\n");
6128 } else {
6129 if (RAW == ';') {
6130 NEXT;
6131 if ((ctxt->sax != NULL) &&
6132 (ctxt->sax->getParameterEntity != NULL))
6133 entity = ctxt->sax->getParameterEntity(ctxt->userData,
6134 name);
6135 if (entity == NULL) {
6136 /*
6137 * [ WFC: Entity Declared ]
6138 * In a document without any DTD, a document with only an
6139 * internal DTD subset which contains no parameter entity
6140 * references, or a document with "standalone='yes'", ...
6141 * ... The declaration of a parameter entity must precede
6142 * any reference to it...
6143 */
6144 if ((ctxt->standalone == 1) ||
6145 ((ctxt->hasExternalSubset == 0) &&
6146 (ctxt->hasPErefs == 0))) {
6147 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
6148 "PEReference: %%%s; not found\n",
6149 name);
6150 } else {
6151 /*
6152 * [ VC: Entity Declared ]
6153 * In a document with an external subset or external
6154 * parameter entities with "standalone='no'", ...
6155 * ... The declaration of a parameter entity must
6156 * precede any reference to it...
6157 */
6158 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6159 "PEReference: %%%s; not found\n",
6160 name, NULL);
6161 ctxt->valid = 0;
6162 }
6163 } else {
6164 /*
6165 * Internal checking in case the entity quest barfed
6166 */
6167 if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
6168 (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
6169 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6170 "Internal: %%%s; is not a parameter entity\n",
6171 name, NULL);
6172 } else if (ctxt->input->free != deallocblankswrapper) {
6173 input =
6174 xmlNewBlanksWrapperInputStream(ctxt, entity);
6175 xmlPushInput(ctxt, input);
6176 } else {
6177 /*
6178 * TODO !!!
6179 * handle the extra spaces added before and after
6180 * c.f. http://www.w3.org/TR/REC-xml#as-PE
6181 */
6182 input = xmlNewEntityInputStream(ctxt, entity);
6183 xmlPushInput(ctxt, input);
6184 if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) &&
Daniel Veillarda07050d2003-10-19 14:46:32 +00006185 (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) &&
William M. Brack76e95df2003-10-18 16:20:14 +00006186 (IS_BLANK_CH(NXT(5)))) {
Daniel Veillard8f597c32003-10-06 08:19:27 +00006187 xmlParseTextDecl(ctxt);
6188 if (ctxt->errNo ==
6189 XML_ERR_UNSUPPORTED_ENCODING) {
6190 /*
6191 * The XML REC instructs us to stop parsing
6192 * right here
6193 */
6194 ctxt->instate = XML_PARSER_EOF;
6195 return;
6196 }
6197 }
6198 }
6199 }
6200 ctxt->hasPErefs = 1;
6201 } else {
6202 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
6203 }
6204 }
Owen Taylor3473f882001-02-23 17:55:21 +00006205 }
6206}
6207
6208/**
6209 * xmlParseStringPEReference:
6210 * @ctxt: an XML parser context
6211 * @str: a pointer to an index in the string
6212 *
6213 * parse PEReference declarations
6214 *
6215 * [69] PEReference ::= '%' Name ';'
6216 *
6217 * [ WFC: No Recursion ]
6218 * A parsed entity must not contain a recursive
6219 * reference to itself, either directly or indirectly.
6220 *
6221 * [ WFC: Entity Declared ]
6222 * In a document without any DTD, a document with only an internal DTD
6223 * subset which contains no parameter entity references, or a document
6224 * with "standalone='yes'", ... ... The declaration of a parameter
6225 * entity must precede any reference to it...
6226 *
6227 * [ VC: Entity Declared ]
6228 * In a document with an external subset or external parameter entities
6229 * with "standalone='no'", ... ... The declaration of a parameter entity
6230 * must precede any reference to it...
6231 *
6232 * [ WFC: In DTD ]
6233 * Parameter-entity references may only appear in the DTD.
6234 * NOTE: misleading but this is handled.
6235 *
6236 * Returns the string of the entity content.
6237 * str is updated to the current value of the index
6238 */
6239xmlEntityPtr
6240xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) {
6241 const xmlChar *ptr;
6242 xmlChar cur;
6243 xmlChar *name;
6244 xmlEntityPtr entity = NULL;
6245
6246 if ((str == NULL) || (*str == NULL)) return(NULL);
6247 ptr = *str;
6248 cur = *ptr;
6249 if (cur == '%') {
6250 ptr++;
6251 cur = *ptr;
6252 name = xmlParseStringName(ctxt, &ptr);
6253 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006254 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6255 "xmlParseStringPEReference: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006256 } else {
6257 cur = *ptr;
6258 if (cur == ';') {
6259 ptr++;
6260 cur = *ptr;
6261 if ((ctxt->sax != NULL) &&
6262 (ctxt->sax->getParameterEntity != NULL))
6263 entity = ctxt->sax->getParameterEntity(ctxt->userData,
6264 name);
6265 if (entity == NULL) {
6266 /*
6267 * [ WFC: Entity Declared ]
6268 * In a document without any DTD, a document with only an
6269 * internal DTD subset which contains no parameter entity
6270 * references, or a document with "standalone='yes'", ...
6271 * ... The declaration of a parameter entity must precede
6272 * any reference to it...
6273 */
6274 if ((ctxt->standalone == 1) ||
6275 ((ctxt->hasExternalSubset == 0) &&
6276 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006277 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00006278 "PEReference: %%%s; not found\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006279 } else {
6280 /*
6281 * [ VC: Entity Declared ]
6282 * In a document with an external subset or external
6283 * parameter entities with "standalone='no'", ...
6284 * ... The declaration of a parameter entity must
6285 * precede any reference to it...
6286 */
Daniel Veillard24eb9782003-10-04 21:08:09 +00006287 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6288 "PEReference: %%%s; not found\n",
6289 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006290 ctxt->valid = 0;
6291 }
6292 } else {
6293 /*
6294 * Internal checking in case the entity quest barfed
6295 */
6296 if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
6297 (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00006298 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6299 "%%%s; is not a parameter entity\n",
6300 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006301 }
6302 }
6303 ctxt->hasPErefs = 1;
6304 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006305 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006306 }
6307 xmlFree(name);
6308 }
6309 }
6310 *str = ptr;
6311 return(entity);
6312}
6313
6314/**
6315 * xmlParseDocTypeDecl:
6316 * @ctxt: an XML parser context
6317 *
6318 * parse a DOCTYPE declaration
6319 *
6320 * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
6321 * ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
6322 *
6323 * [ VC: Root Element Type ]
6324 * The Name in the document type declaration must match the element
6325 * type of the root element.
6326 */
6327
6328void
6329xmlParseDocTypeDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006330 const xmlChar *name = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00006331 xmlChar *ExternalID = NULL;
6332 xmlChar *URI = NULL;
6333
6334 /*
6335 * We know that '<!DOCTYPE' has been detected.
6336 */
6337 SKIP(9);
6338
6339 SKIP_BLANKS;
6340
6341 /*
6342 * Parse the DOCTYPE name.
6343 */
6344 name = xmlParseName(ctxt);
6345 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006346 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6347 "xmlParseDocTypeDecl : no DOCTYPE name !\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006348 }
6349 ctxt->intSubName = name;
6350
6351 SKIP_BLANKS;
6352
6353 /*
6354 * Check for SystemID and ExternalID
6355 */
6356 URI = xmlParseExternalID(ctxt, &ExternalID, 1);
6357
6358 if ((URI != NULL) || (ExternalID != NULL)) {
6359 ctxt->hasExternalSubset = 1;
6360 }
6361 ctxt->extSubURI = URI;
6362 ctxt->extSubSystem = ExternalID;
6363
6364 SKIP_BLANKS;
6365
6366 /*
6367 * Create and update the internal subset.
6368 */
6369 if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
6370 (!ctxt->disableSAX))
6371 ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
6372
6373 /*
6374 * Is there any internal subset declarations ?
6375 * they are handled separately in xmlParseInternalSubset()
6376 */
6377 if (RAW == '[')
6378 return;
6379
6380 /*
6381 * We should be at the end of the DOCTYPE declaration.
6382 */
6383 if (RAW != '>') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006384 xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006385 }
6386 NEXT;
6387}
6388
6389/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006390 * xmlParseInternalSubset:
Owen Taylor3473f882001-02-23 17:55:21 +00006391 * @ctxt: an XML parser context
6392 *
6393 * parse the internal subset declaration
6394 *
6395 * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
6396 */
6397
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006398static void
Owen Taylor3473f882001-02-23 17:55:21 +00006399xmlParseInternalSubset(xmlParserCtxtPtr ctxt) {
6400 /*
6401 * Is there any DTD definition ?
6402 */
6403 if (RAW == '[') {
6404 ctxt->instate = XML_PARSER_DTD;
6405 NEXT;
6406 /*
6407 * Parse the succession of Markup declarations and
6408 * PEReferences.
6409 * Subsequence (markupdecl | PEReference | S)*
6410 */
6411 while (RAW != ']') {
6412 const xmlChar *check = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00006413 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00006414
6415 SKIP_BLANKS;
6416 xmlParseMarkupDecl(ctxt);
6417 xmlParsePEReference(ctxt);
6418
6419 /*
6420 * Pop-up of finished entities.
6421 */
6422 while ((RAW == 0) && (ctxt->inputNr > 1))
6423 xmlPopInput(ctxt);
6424
6425 if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006426 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006427 "xmlParseInternalSubset: error detected in Markup declaration\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006428 break;
6429 }
6430 }
6431 if (RAW == ']') {
6432 NEXT;
6433 SKIP_BLANKS;
6434 }
6435 }
6436
6437 /*
6438 * We should be at the end of the DOCTYPE declaration.
6439 */
6440 if (RAW != '>') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006441 xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006442 }
6443 NEXT;
6444}
6445
Daniel Veillard81273902003-09-30 00:43:48 +00006446#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00006447/**
6448 * xmlParseAttribute:
6449 * @ctxt: an XML parser context
6450 * @value: a xmlChar ** used to store the value of the attribute
6451 *
6452 * parse an attribute
6453 *
6454 * [41] Attribute ::= Name Eq AttValue
6455 *
6456 * [ WFC: No External Entity References ]
6457 * Attribute values cannot contain direct or indirect entity references
6458 * to external entities.
6459 *
6460 * [ WFC: No < in Attribute Values ]
6461 * The replacement text of any entity referred to directly or indirectly in
6462 * an attribute value (other than "&lt;") must not contain a <.
6463 *
6464 * [ VC: Attribute Value Type ]
6465 * The attribute must have been declared; the value must be of the type
6466 * declared for it.
6467 *
6468 * [25] Eq ::= S? '=' S?
6469 *
6470 * With namespace:
6471 *
6472 * [NS 11] Attribute ::= QName Eq AttValue
6473 *
6474 * Also the case QName == xmlns:??? is handled independently as a namespace
6475 * definition.
6476 *
6477 * Returns the attribute name, and the value in *value.
6478 */
6479
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006480const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006481xmlParseAttribute(xmlParserCtxtPtr ctxt, xmlChar **value) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006482 const xmlChar *name;
6483 xmlChar *val;
Owen Taylor3473f882001-02-23 17:55:21 +00006484
6485 *value = NULL;
Daniel Veillard878eab02002-02-19 13:46:09 +00006486 GROW;
Owen Taylor3473f882001-02-23 17:55:21 +00006487 name = xmlParseName(ctxt);
6488 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006489 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006490 "error parsing attribute name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006491 return(NULL);
6492 }
6493
6494 /*
6495 * read the value
6496 */
6497 SKIP_BLANKS;
6498 if (RAW == '=') {
6499 NEXT;
6500 SKIP_BLANKS;
6501 val = xmlParseAttValue(ctxt);
6502 ctxt->instate = XML_PARSER_CONTENT;
6503 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006504 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Owen Taylor3473f882001-02-23 17:55:21 +00006505 "Specification mandate value for attribute %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006506 return(NULL);
6507 }
6508
6509 /*
6510 * Check that xml:lang conforms to the specification
6511 * No more registered as an error, just generate a warning now
6512 * since this was deprecated in XML second edition
6513 */
6514 if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) {
6515 if (!xmlCheckLanguageID(val)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00006516 xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
6517 "Malformed value for xml:lang : %s\n",
6518 val, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006519 }
6520 }
6521
6522 /*
6523 * Check that xml:space conforms to the specification
6524 */
6525 if (xmlStrEqual(name, BAD_CAST "xml:space")) {
6526 if (xmlStrEqual(val, BAD_CAST "default"))
6527 *(ctxt->space) = 0;
6528 else if (xmlStrEqual(val, BAD_CAST "preserve"))
6529 *(ctxt->space) = 1;
6530 else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006531 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Daniel Veillard642104e2003-03-26 16:32:05 +00006532"Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
Owen Taylor3473f882001-02-23 17:55:21 +00006533 val);
Owen Taylor3473f882001-02-23 17:55:21 +00006534 }
6535 }
6536
6537 *value = val;
6538 return(name);
6539}
6540
6541/**
6542 * xmlParseStartTag:
6543 * @ctxt: an XML parser context
6544 *
6545 * parse a start of tag either for rule element or
6546 * EmptyElement. In both case we don't parse the tag closing chars.
6547 *
6548 * [40] STag ::= '<' Name (S Attribute)* S? '>'
6549 *
6550 * [ WFC: Unique Att Spec ]
6551 * No attribute name may appear more than once in the same start-tag or
6552 * empty-element tag.
6553 *
6554 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
6555 *
6556 * [ WFC: Unique Att Spec ]
6557 * No attribute name may appear more than once in the same start-tag or
6558 * empty-element tag.
6559 *
6560 * With namespace:
6561 *
6562 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
6563 *
6564 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
6565 *
6566 * Returns the element name parsed
6567 */
6568
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006569const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006570xmlParseStartTag(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006571 const xmlChar *name;
6572 const xmlChar *attname;
Owen Taylor3473f882001-02-23 17:55:21 +00006573 xmlChar *attvalue;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006574 const xmlChar **atts = ctxt->atts;
Owen Taylor3473f882001-02-23 17:55:21 +00006575 int nbatts = 0;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006576 int maxatts = ctxt->maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00006577 int i;
6578
6579 if (RAW != '<') return(NULL);
Daniel Veillard21a0f912001-02-25 19:54:14 +00006580 NEXT1;
Owen Taylor3473f882001-02-23 17:55:21 +00006581
6582 name = xmlParseName(ctxt);
6583 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006584 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00006585 "xmlParseStartTag: invalid element name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006586 return(NULL);
6587 }
6588
6589 /*
6590 * Now parse the attributes, it ends up with the ending
6591 *
6592 * (S Attribute)* S?
6593 */
6594 SKIP_BLANKS;
6595 GROW;
6596
Daniel Veillard21a0f912001-02-25 19:54:14 +00006597 while ((RAW != '>') &&
6598 ((RAW != '/') || (NXT(1) != '>')) &&
Daniel Veillard73b013f2003-09-30 12:36:01 +00006599 (IS_BYTE_CHAR(RAW))) {
Owen Taylor3473f882001-02-23 17:55:21 +00006600 const xmlChar *q = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00006601 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00006602
6603 attname = xmlParseAttribute(ctxt, &attvalue);
6604 if ((attname != NULL) && (attvalue != NULL)) {
6605 /*
6606 * [ WFC: Unique Att Spec ]
6607 * No attribute name may appear more than once in the same
6608 * start-tag or empty-element tag.
6609 */
6610 for (i = 0; i < nbatts;i += 2) {
6611 if (xmlStrEqual(atts[i], attname)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006612 xmlErrAttributeDup(ctxt, NULL, attname);
Owen Taylor3473f882001-02-23 17:55:21 +00006613 xmlFree(attvalue);
6614 goto failed;
6615 }
6616 }
Owen Taylor3473f882001-02-23 17:55:21 +00006617 /*
6618 * Add the pair to atts
6619 */
6620 if (atts == NULL) {
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006621 maxatts = 22; /* allow for 10 attrs by default */
6622 atts = (const xmlChar **)
6623 xmlMalloc(maxatts * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00006624 if (atts == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006625 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006626 if (attvalue != NULL)
6627 xmlFree(attvalue);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006628 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00006629 }
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006630 ctxt->atts = atts;
6631 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00006632 } else if (nbatts + 4 > maxatts) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006633 const xmlChar **n;
6634
Owen Taylor3473f882001-02-23 17:55:21 +00006635 maxatts *= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006636 n = (const xmlChar **) xmlRealloc((void *) atts,
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006637 maxatts * sizeof(const xmlChar *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006638 if (n == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006639 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006640 if (attvalue != NULL)
6641 xmlFree(attvalue);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006642 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00006643 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006644 atts = n;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006645 ctxt->atts = atts;
6646 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00006647 }
6648 atts[nbatts++] = attname;
6649 atts[nbatts++] = attvalue;
6650 atts[nbatts] = NULL;
6651 atts[nbatts + 1] = NULL;
6652 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00006653 if (attvalue != NULL)
6654 xmlFree(attvalue);
6655 }
6656
6657failed:
6658
Daniel Veillard3772de32002-12-17 10:31:45 +00006659 GROW
Owen Taylor3473f882001-02-23 17:55:21 +00006660 if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
6661 break;
William M. Brack76e95df2003-10-18 16:20:14 +00006662 if (!IS_BLANK_CH(RAW)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006663 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6664 "attributes construct error\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006665 }
6666 SKIP_BLANKS;
Daniel Veillard02111c12003-02-24 19:14:52 +00006667 if ((cons == ctxt->input->consumed) && (q == CUR_PTR) &&
6668 (attname == NULL) && (attvalue == NULL)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006669 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
6670 "xmlParseStartTag: problem parsing attributes\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006671 break;
6672 }
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006673 SHRINK;
Owen Taylor3473f882001-02-23 17:55:21 +00006674 GROW;
6675 }
6676
6677 /*
6678 * SAX: Start of Element !
6679 */
6680 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) &&
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006681 (!ctxt->disableSAX)) {
6682 if (nbatts > 0)
6683 ctxt->sax->startElement(ctxt->userData, name, atts);
6684 else
6685 ctxt->sax->startElement(ctxt->userData, name, NULL);
6686 }
Owen Taylor3473f882001-02-23 17:55:21 +00006687
6688 if (atts != NULL) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006689 /* Free only the content strings */
6690 for (i = 1;i < nbatts;i+=2)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006691 if (atts[i] != NULL)
6692 xmlFree((xmlChar *) atts[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00006693 }
6694 return(name);
6695}
6696
6697/**
Daniel Veillard0fb18932003-09-07 09:14:37 +00006698 * xmlParseEndTag1:
Owen Taylor3473f882001-02-23 17:55:21 +00006699 * @ctxt: an XML parser context
Daniel Veillard0fb18932003-09-07 09:14:37 +00006700 * @line: line of the start tag
6701 * @nsNr: number of namespaces on the start tag
Owen Taylor3473f882001-02-23 17:55:21 +00006702 *
6703 * parse an end of tag
6704 *
6705 * [42] ETag ::= '</' Name S? '>'
6706 *
6707 * With namespace
6708 *
6709 * [NS 9] ETag ::= '</' QName S? '>'
6710 */
6711
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00006712static void
Daniel Veillard0fb18932003-09-07 09:14:37 +00006713xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006714 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006715
6716 GROW;
6717 if ((RAW != '<') || (NXT(1) != '/')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006718 xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006719 "xmlParseEndTag: '</' not found\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006720 return;
6721 }
6722 SKIP(2);
6723
Daniel Veillard46de64e2002-05-29 08:21:33 +00006724 name = xmlParseNameAndCompare(ctxt,ctxt->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006725
6726 /*
6727 * We should definitely be at the ending "S? '>'" part
6728 */
6729 GROW;
6730 SKIP_BLANKS;
Daniel Veillard73b013f2003-09-30 12:36:01 +00006731 if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006732 xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006733 } else
Daniel Veillard21a0f912001-02-25 19:54:14 +00006734 NEXT1;
Owen Taylor3473f882001-02-23 17:55:21 +00006735
6736 /*
6737 * [ WFC: Element Type Match ]
6738 * The Name in an element's end-tag must match the element type in the
6739 * start-tag.
6740 *
6741 */
Daniel Veillard46de64e2002-05-29 08:21:33 +00006742 if (name != (xmlChar*)1) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006743 if (name == NULL) name = BAD_CAST "unparseable";
6744 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00006745 "Opening and ending tag mismatch: %s line %d and %s\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00006746 ctxt->name, line, name);
Owen Taylor3473f882001-02-23 17:55:21 +00006747 }
6748
6749 /*
6750 * SAX: End of Tag
6751 */
6752 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
6753 (!ctxt->disableSAX))
Daniel Veillard46de64e2002-05-29 08:21:33 +00006754 ctxt->sax->endElement(ctxt->userData, ctxt->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006755
Daniel Veillarde57ec792003-09-10 10:50:59 +00006756 namePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006757 spacePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006758 return;
6759}
6760
6761/**
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00006762 * xmlParseEndTag:
6763 * @ctxt: an XML parser context
6764 *
6765 * parse an end of tag
6766 *
6767 * [42] ETag ::= '</' Name S? '>'
6768 *
6769 * With namespace
6770 *
6771 * [NS 9] ETag ::= '</' QName S? '>'
6772 */
6773
6774void
6775xmlParseEndTag(xmlParserCtxtPtr ctxt) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00006776 xmlParseEndTag1(ctxt, 0);
6777}
Daniel Veillard81273902003-09-30 00:43:48 +00006778#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard0fb18932003-09-07 09:14:37 +00006779
6780/************************************************************************
6781 * *
6782 * SAX 2 specific operations *
6783 * *
6784 ************************************************************************/
6785
6786static const xmlChar *
6787xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) {
6788 int len = 0, l;
6789 int c;
6790 int count = 0;
6791
6792 /*
6793 * Handler for more complex cases
6794 */
6795 GROW;
6796 c = CUR_CHAR(l);
6797 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006798 (!IS_LETTER(c) && (c != '_'))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00006799 return(NULL);
6800 }
6801
6802 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
William M. Brack871611b2003-10-18 04:53:14 +00006803 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006804 (c == '.') || (c == '-') || (c == '_') ||
William M. Brack871611b2003-10-18 04:53:14 +00006805 (IS_COMBINING(c)) ||
6806 (IS_EXTENDER(c)))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00006807 if (count++ > 100) {
6808 count = 0;
6809 GROW;
6810 }
6811 len += l;
6812 NEXTL(l);
6813 c = CUR_CHAR(l);
6814 }
6815 return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len));
6816}
6817
6818/*
6819 * xmlGetNamespace:
6820 * @ctxt: an XML parser context
6821 * @prefix: the prefix to lookup
6822 *
6823 * Lookup the namespace name for the @prefix (which ca be NULL)
6824 * The prefix must come from the @ctxt->dict dictionnary
6825 *
6826 * Returns the namespace name or NULL if not bound
6827 */
6828static const xmlChar *
6829xmlGetNamespace(xmlParserCtxtPtr ctxt, const xmlChar *prefix) {
6830 int i;
6831
Daniel Veillarde57ec792003-09-10 10:50:59 +00006832 if (prefix == ctxt->str_xml) return(ctxt->str_xml_ns);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006833 for (i = ctxt->nsNr - 2;i >= 0;i-=2)
Daniel Veillarde57ec792003-09-10 10:50:59 +00006834 if (ctxt->nsTab[i] == prefix) {
6835 if ((prefix == NULL) && (*ctxt->nsTab[i + 1] == 0))
6836 return(NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006837 return(ctxt->nsTab[i + 1]);
Daniel Veillarde57ec792003-09-10 10:50:59 +00006838 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00006839 return(NULL);
6840}
6841
6842/**
6843 * xmlParseNCName:
6844 * @ctxt: an XML parser context
Daniel Veillardc82c57e2004-01-12 16:24:34 +00006845 * @len: lenght of the string parsed
Daniel Veillard0fb18932003-09-07 09:14:37 +00006846 *
6847 * parse an XML name.
6848 *
6849 * [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' |
6850 * CombiningChar | Extender
6851 *
6852 * [5NS] NCName ::= (Letter | '_') (NCNameChar)*
6853 *
6854 * Returns the Name parsed or NULL
6855 */
6856
6857static const xmlChar *
6858xmlParseNCName(xmlParserCtxtPtr ctxt) {
6859 const xmlChar *in;
6860 const xmlChar *ret;
6861 int count = 0;
6862
6863 /*
6864 * Accelerator for simple ASCII names
6865 */
6866 in = ctxt->input->cur;
6867 if (((*in >= 0x61) && (*in <= 0x7A)) ||
6868 ((*in >= 0x41) && (*in <= 0x5A)) ||
6869 (*in == '_')) {
6870 in++;
6871 while (((*in >= 0x61) && (*in <= 0x7A)) ||
6872 ((*in >= 0x41) && (*in <= 0x5A)) ||
6873 ((*in >= 0x30) && (*in <= 0x39)) ||
6874 (*in == '_') || (*in == '-') ||
6875 (*in == '.'))
6876 in++;
6877 if ((*in > 0) && (*in < 0x80)) {
6878 count = in - ctxt->input->cur;
6879 ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
6880 ctxt->input->cur = in;
6881 ctxt->nbChars += count;
6882 ctxt->input->col += count;
6883 if (ret == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006884 xmlErrMemory(ctxt, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006885 }
6886 return(ret);
6887 }
6888 }
6889 return(xmlParseNCNameComplex(ctxt));
6890}
6891
6892/**
6893 * xmlParseQName:
6894 * @ctxt: an XML parser context
6895 * @prefix: pointer to store the prefix part
6896 *
6897 * parse an XML Namespace QName
6898 *
6899 * [6] QName ::= (Prefix ':')? LocalPart
6900 * [7] Prefix ::= NCName
6901 * [8] LocalPart ::= NCName
6902 *
6903 * Returns the Name parsed or NULL
6904 */
6905
6906static const xmlChar *
6907xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) {
6908 const xmlChar *l, *p;
6909
6910 GROW;
6911
6912 l = xmlParseNCName(ctxt);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006913 if (l == NULL) {
6914 if (CUR == ':') {
6915 l = xmlParseName(ctxt);
6916 if (l != NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006917 xmlNsErr(ctxt, XML_NS_ERR_QNAME,
6918 "Failed to parse QName '%s'\n", l, NULL, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006919 *prefix = NULL;
6920 return(l);
6921 }
6922 }
6923 return(NULL);
6924 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00006925 if (CUR == ':') {
6926 NEXT;
6927 p = l;
6928 l = xmlParseNCName(ctxt);
6929 if (l == NULL) {
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006930 xmlChar *tmp;
6931
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006932 xmlNsErr(ctxt, XML_NS_ERR_QNAME,
6933 "Failed to parse QName '%s:'\n", p, NULL, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006934 tmp = xmlBuildQName(BAD_CAST "", p, NULL, 0);
6935 p = xmlDictLookup(ctxt->dict, tmp, -1);
6936 if (tmp != NULL) xmlFree(tmp);
6937 *prefix = NULL;
6938 return(p);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006939 }
6940 if (CUR == ':') {
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006941 xmlChar *tmp;
6942
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006943 xmlNsErr(ctxt, XML_NS_ERR_QNAME,
6944 "Failed to parse QName '%s:%s:'\n", p, l, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006945 NEXT;
6946 tmp = (xmlChar *) xmlParseName(ctxt);
6947 if (tmp != NULL) {
6948 tmp = xmlBuildQName(tmp, l, NULL, 0);
6949 l = xmlDictLookup(ctxt->dict, tmp, -1);
6950 if (tmp != NULL) xmlFree(tmp);
6951 *prefix = p;
6952 return(l);
6953 }
6954 tmp = xmlBuildQName(BAD_CAST "", l, NULL, 0);
6955 l = xmlDictLookup(ctxt->dict, tmp, -1);
6956 if (tmp != NULL) xmlFree(tmp);
6957 *prefix = p;
6958 return(l);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006959 }
6960 *prefix = p;
6961 } else
6962 *prefix = NULL;
6963 return(l);
6964}
6965
6966/**
6967 * xmlParseQNameAndCompare:
6968 * @ctxt: an XML parser context
6969 * @name: the localname
6970 * @prefix: the prefix, if any.
6971 *
6972 * parse an XML name and compares for match
6973 * (specialized for endtag parsing)
6974 *
6975 * Returns NULL for an illegal name, (xmlChar*) 1 for success
6976 * and the name for mismatch
6977 */
6978
6979static const xmlChar *
6980xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name,
6981 xmlChar const *prefix) {
6982 const xmlChar *cmp = name;
6983 const xmlChar *in;
6984 const xmlChar *ret;
6985 const xmlChar *prefix2;
6986
6987 if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name));
6988
6989 GROW;
6990 in = ctxt->input->cur;
6991
6992 cmp = prefix;
6993 while (*in != 0 && *in == *cmp) {
6994 ++in;
6995 ++cmp;
6996 }
6997 if ((*cmp == 0) && (*in == ':')) {
6998 in++;
6999 cmp = name;
7000 while (*in != 0 && *in == *cmp) {
7001 ++in;
7002 ++cmp;
7003 }
William M. Brack76e95df2003-10-18 16:20:14 +00007004 if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007005 /* success */
7006 ctxt->input->cur = in;
7007 return((const xmlChar*) 1);
7008 }
7009 }
7010 /*
7011 * all strings coms from the dictionary, equality can be done directly
7012 */
7013 ret = xmlParseQName (ctxt, &prefix2);
7014 if ((ret == name) && (prefix == prefix2))
7015 return((const xmlChar*) 1);
7016 return ret;
7017}
7018
7019/**
7020 * xmlParseAttValueInternal:
7021 * @ctxt: an XML parser context
7022 * @len: attribute len result
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007023 * @alloc: whether the attribute was reallocated as a new string
7024 * @normalize: if 1 then further non-CDATA normalization must be done
Daniel Veillard0fb18932003-09-07 09:14:37 +00007025 *
7026 * parse a value for an attribute.
7027 * NOTE: if no normalization is needed, the routine will return pointers
7028 * directly from the data buffer.
7029 *
7030 * 3.3.3 Attribute-Value Normalization:
7031 * Before the value of an attribute is passed to the application or
7032 * checked for validity, the XML processor must normalize it as follows:
7033 * - a character reference is processed by appending the referenced
7034 * character to the attribute value
7035 * - an entity reference is processed by recursively processing the
7036 * replacement text of the entity
7037 * - a whitespace character (#x20, #xD, #xA, #x9) is processed by
7038 * appending #x20 to the normalized value, except that only a single
7039 * #x20 is appended for a "#xD#xA" sequence that is part of an external
7040 * parsed entity or the literal entity value of an internal parsed entity
7041 * - other characters are processed by appending them to the normalized value
7042 * If the declared value is not CDATA, then the XML processor must further
7043 * process the normalized attribute value by discarding any leading and
7044 * trailing space (#x20) characters, and by replacing sequences of space
7045 * (#x20) characters by a single space (#x20) character.
7046 * All attributes for which no declaration has been read should be treated
7047 * by a non-validating parser as if declared CDATA.
7048 *
7049 * Returns the AttValue parsed or NULL. The value has to be freed by the
7050 * caller if it was copied, this can be detected by val[*len] == 0.
7051 */
7052
7053static xmlChar *
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007054xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc,
7055 int normalize)
Daniel Veillarde57ec792003-09-10 10:50:59 +00007056{
Daniel Veillard0fb18932003-09-07 09:14:37 +00007057 xmlChar limit = 0;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007058 const xmlChar *in = NULL, *start, *end, *last;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007059 xmlChar *ret = NULL;
7060
7061 GROW;
7062 in = (xmlChar *) CUR_PTR;
7063 if (*in != '"' && *in != '\'') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007064 xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007065 return (NULL);
7066 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007067 ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007068
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007069 /*
7070 * try to handle in this routine the most common case where no
7071 * allocation of a new string is required and where content is
7072 * pure ASCII.
7073 */
7074 limit = *in++;
7075 end = ctxt->input->end;
7076 start = in;
7077 if (in >= end) {
7078 const xmlChar *oldbase = ctxt->input->base;
7079 GROW;
7080 if (oldbase != ctxt->input->base) {
7081 long delta = ctxt->input->base - oldbase;
7082 start = start + delta;
7083 in = in + delta;
7084 }
7085 end = ctxt->input->end;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007086 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007087 if (normalize) {
7088 /*
7089 * Skip any leading spaces
7090 */
7091 while ((in < end) && (*in != limit) &&
7092 ((*in == 0x20) || (*in == 0x9) ||
7093 (*in == 0xA) || (*in == 0xD))) {
7094 in++;
7095 start = in;
7096 if (in >= end) {
7097 const xmlChar *oldbase = ctxt->input->base;
7098 GROW;
7099 if (oldbase != ctxt->input->base) {
7100 long delta = ctxt->input->base - oldbase;
7101 start = start + delta;
7102 in = in + delta;
7103 }
7104 end = ctxt->input->end;
7105 }
7106 }
7107 while ((in < end) && (*in != limit) && (*in >= 0x20) &&
7108 (*in <= 0x7f) && (*in != '&') && (*in != '<')) {
7109 if ((*in++ == 0x20) && (*in == 0x20)) break;
7110 if (in >= end) {
7111 const xmlChar *oldbase = ctxt->input->base;
7112 GROW;
7113 if (oldbase != ctxt->input->base) {
7114 long delta = ctxt->input->base - oldbase;
7115 start = start + delta;
7116 in = in + delta;
7117 }
7118 end = ctxt->input->end;
7119 }
7120 }
7121 last = in;
7122 /*
7123 * skip the trailing blanks
7124 */
Daniel Veillardc6e20e42003-09-11 16:30:26 +00007125 while ((last[-1] == 0x20) && (last > start)) last--;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007126 while ((in < end) && (*in != limit) &&
7127 ((*in == 0x20) || (*in == 0x9) ||
7128 (*in == 0xA) || (*in == 0xD))) {
7129 in++;
7130 if (in >= end) {
7131 const xmlChar *oldbase = ctxt->input->base;
7132 GROW;
7133 if (oldbase != ctxt->input->base) {
7134 long delta = ctxt->input->base - oldbase;
7135 start = start + delta;
7136 in = in + delta;
7137 last = last + delta;
7138 }
7139 end = ctxt->input->end;
7140 }
7141 }
7142 if (*in != limit) goto need_complex;
7143 } else {
7144 while ((in < end) && (*in != limit) && (*in >= 0x20) &&
7145 (*in <= 0x7f) && (*in != '&') && (*in != '<')) {
7146 in++;
7147 if (in >= end) {
7148 const xmlChar *oldbase = ctxt->input->base;
7149 GROW;
7150 if (oldbase != ctxt->input->base) {
7151 long delta = ctxt->input->base - oldbase;
7152 start = start + delta;
7153 in = in + delta;
7154 }
7155 end = ctxt->input->end;
7156 }
7157 }
7158 last = in;
7159 if (*in != limit) goto need_complex;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007160 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007161 in++;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007162 if (len != NULL) {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007163 *len = last - start;
7164 ret = (xmlChar *) start;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007165 } else {
Daniel Veillarde57ec792003-09-10 10:50:59 +00007166 if (alloc) *alloc = 1;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007167 ret = xmlStrndup(start, last - start);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007168 }
7169 CUR_PTR = in;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007170 if (alloc) *alloc = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007171 return ret;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007172need_complex:
7173 if (alloc) *alloc = 1;
7174 return xmlParseAttValueComplex(ctxt, len, normalize);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007175}
7176
7177/**
7178 * xmlParseAttribute2:
7179 * @ctxt: an XML parser context
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007180 * @pref: the element prefix
7181 * @elem: the element name
7182 * @prefix: a xmlChar ** used to store the value of the attribute prefix
Daniel Veillard0fb18932003-09-07 09:14:37 +00007183 * @value: a xmlChar ** used to store the value of the attribute
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007184 * @len: an int * to save the length of the attribute
7185 * @alloc: an int * to indicate if the attribute was allocated
Daniel Veillard0fb18932003-09-07 09:14:37 +00007186 *
7187 * parse an attribute in the new SAX2 framework.
7188 *
7189 * Returns the attribute name, and the value in *value, .
7190 */
7191
7192static const xmlChar *
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007193xmlParseAttribute2(xmlParserCtxtPtr ctxt,
7194 const xmlChar *pref, const xmlChar *elem,
7195 const xmlChar **prefix, xmlChar **value,
7196 int *len, int *alloc) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007197 const xmlChar *name;
7198 xmlChar *val;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007199 int normalize = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007200
7201 *value = NULL;
7202 GROW;
7203 name = xmlParseQName(ctxt, prefix);
7204 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007205 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7206 "error parsing attribute name\n");
Daniel Veillard0fb18932003-09-07 09:14:37 +00007207 return(NULL);
7208 }
7209
7210 /*
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007211 * get the type if needed
7212 */
7213 if (ctxt->attsSpecial != NULL) {
7214 int type;
7215
7216 type = (int) (long) xmlHashQLookup2(ctxt->attsSpecial,
7217 pref, elem, *prefix, name);
7218 if (type != 0) normalize = 1;
7219 }
7220
7221 /*
Daniel Veillard0fb18932003-09-07 09:14:37 +00007222 * read the value
7223 */
7224 SKIP_BLANKS;
7225 if (RAW == '=') {
7226 NEXT;
7227 SKIP_BLANKS;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007228 val = xmlParseAttValueInternal(ctxt, len, alloc, normalize);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007229 ctxt->instate = XML_PARSER_CONTENT;
7230 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00007231 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007232 "Specification mandate value for attribute %s\n", name);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007233 return(NULL);
7234 }
7235
7236 /*
7237 * Check that xml:lang conforms to the specification
7238 * No more registered as an error, just generate a warning now
7239 * since this was deprecated in XML second edition
7240 */
7241 if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) {
7242 if (!xmlCheckLanguageID(val)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007243 xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
7244 "Malformed value for xml:lang : %s\n",
7245 val, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007246 }
7247 }
7248
7249 /*
7250 * Check that xml:space conforms to the specification
7251 */
7252 if (xmlStrEqual(name, BAD_CAST "xml:space")) {
7253 if (xmlStrEqual(val, BAD_CAST "default"))
7254 *(ctxt->space) = 0;
7255 else if (xmlStrEqual(val, BAD_CAST "preserve"))
7256 *(ctxt->space) = 1;
7257 else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00007258 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007259"Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
7260 val);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007261 }
7262 }
7263
7264 *value = val;
7265 return(name);
7266}
7267
7268/**
7269 * xmlParseStartTag2:
7270 * @ctxt: an XML parser context
7271 *
7272 * parse a start of tag either for rule element or
7273 * EmptyElement. In both case we don't parse the tag closing chars.
7274 * This routine is called when running SAX2 parsing
7275 *
7276 * [40] STag ::= '<' Name (S Attribute)* S? '>'
7277 *
7278 * [ WFC: Unique Att Spec ]
7279 * No attribute name may appear more than once in the same start-tag or
7280 * empty-element tag.
7281 *
7282 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
7283 *
7284 * [ WFC: Unique Att Spec ]
7285 * No attribute name may appear more than once in the same start-tag or
7286 * empty-element tag.
7287 *
7288 * With namespace:
7289 *
7290 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
7291 *
7292 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
7293 *
7294 * Returns the element name parsed
7295 */
7296
7297static const xmlChar *
7298xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref,
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007299 const xmlChar **URI, int *tlen) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007300 const xmlChar *localname;
7301 const xmlChar *prefix;
7302 const xmlChar *attname;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007303 const xmlChar *aprefix;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007304 const xmlChar *nsname;
7305 xmlChar *attvalue;
7306 const xmlChar **atts = ctxt->atts;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007307 int maxatts = ctxt->maxatts;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007308 int nratts, nbatts, nbdef;
7309 int i, j, nbNs, attval;
7310 const xmlChar *base;
7311 unsigned long cur;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007312
7313 if (RAW != '<') return(NULL);
7314 NEXT1;
7315
7316 /*
7317 * NOTE: it is crucial with the SAX2 API to never call SHRINK beyond that
7318 * point since the attribute values may be stored as pointers to
7319 * the buffer and calling SHRINK would destroy them !
7320 * The Shrinking is only possible once the full set of attribute
7321 * callbacks have been done.
7322 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00007323reparse:
Daniel Veillard0fb18932003-09-07 09:14:37 +00007324 SHRINK;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007325 base = ctxt->input->base;
7326 cur = ctxt->input->cur - ctxt->input->base;
7327 nbatts = 0;
7328 nratts = 0;
7329 nbdef = 0;
7330 nbNs = 0;
7331 attval = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007332
7333 localname = xmlParseQName(ctxt, &prefix);
7334 if (localname == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007335 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7336 "StartTag: invalid element name\n");
Daniel Veillard0fb18932003-09-07 09:14:37 +00007337 return(NULL);
7338 }
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007339 *tlen = ctxt->input->cur - ctxt->input->base - cur;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007340
7341 /*
7342 * Now parse the attributes, it ends up with the ending
7343 *
7344 * (S Attribute)* S?
7345 */
7346 SKIP_BLANKS;
7347 GROW;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007348 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007349
7350 while ((RAW != '>') &&
7351 ((RAW != '/') || (NXT(1) != '>')) &&
Daniel Veillard73b013f2003-09-30 12:36:01 +00007352 (IS_BYTE_CHAR(RAW))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007353 const xmlChar *q = CUR_PTR;
7354 unsigned int cons = ctxt->input->consumed;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007355 int len = -1, alloc = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007356
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007357 attname = xmlParseAttribute2(ctxt, prefix, localname,
7358 &aprefix, &attvalue, &len, &alloc);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007359 if ((attname != NULL) && (attvalue != NULL)) {
7360 if (len < 0) len = xmlStrlen(attvalue);
7361 if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00007362 const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len);
7363 xmlURIPtr uri;
7364
7365 if (*URL != 0) {
7366 uri = xmlParseURI((const char *) URL);
7367 if (uri == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007368 xmlWarningMsg(ctxt, XML_WAR_NS_URI,
7369 "xmlns: %s not a valid URI\n",
7370 URL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007371 } else {
7372 if (uri->scheme == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007373 xmlWarningMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
7374 "xmlns: URI %s is not absolute\n",
7375 URL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007376 }
7377 xmlFreeURI(uri);
7378 }
7379 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007380 /*
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007381 * check that it's not a defined namespace
Daniel Veillard0fb18932003-09-07 09:14:37 +00007382 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007383 for (j = 1;j <= nbNs;j++)
7384 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL)
7385 break;
7386 if (j <= nbNs)
7387 xmlErrAttributeDup(ctxt, NULL, attname);
7388 else
7389 if (nsPush(ctxt, NULL, URL) > 0) nbNs++;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007390 if (alloc != 0) xmlFree(attvalue);
7391 SKIP_BLANKS;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007392 continue;
7393 }
7394 if (aprefix == ctxt->str_xmlns) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00007395 const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len);
7396 xmlURIPtr uri;
7397
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007398 if (attname == ctxt->str_xml) {
7399 if (URL != ctxt->str_xml_ns) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007400 xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
7401 "xml namespace prefix mapped to wrong URI\n",
7402 NULL, NULL, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007403 }
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007404 /*
7405 * Do not keep a namespace definition node
7406 */
7407 if (alloc != 0) xmlFree(attvalue);
7408 SKIP_BLANKS;
7409 continue;
7410 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00007411 uri = xmlParseURI((const char *) URL);
7412 if (uri == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007413 xmlWarningMsg(ctxt, XML_WAR_NS_URI,
7414 "xmlns:%s: '%s' is not a valid URI\n",
7415 attname, URL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007416 } else {
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007417 if ((ctxt->pedantic) && (uri->scheme == NULL)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007418 xmlWarningMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
7419 "xmlns:%s: URI %s is not absolute\n",
7420 attname, URL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007421 }
7422 xmlFreeURI(uri);
7423 }
7424
Daniel Veillard0fb18932003-09-07 09:14:37 +00007425 /*
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007426 * check that it's not a defined namespace
Daniel Veillard0fb18932003-09-07 09:14:37 +00007427 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007428 for (j = 1;j <= nbNs;j++)
7429 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname)
7430 break;
7431 if (j <= nbNs)
7432 xmlErrAttributeDup(ctxt, aprefix, attname);
7433 else
7434 if (nsPush(ctxt, attname, URL) > 0) nbNs++;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007435 if (alloc != 0) xmlFree(attvalue);
7436 SKIP_BLANKS;
Daniel Veillardd3999c72004-03-10 16:27:03 +00007437 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007438 continue;
7439 }
7440
7441 /*
7442 * Add the pair to atts
7443 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00007444 if ((atts == NULL) || (nbatts + 5 > maxatts)) {
7445 if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007446 if (attvalue[len] == 0)
7447 xmlFree(attvalue);
7448 goto failed;
7449 }
7450 maxatts = ctxt->maxatts;
7451 atts = ctxt->atts;
7452 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00007453 ctxt->attallocs[nratts++] = alloc;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007454 atts[nbatts++] = attname;
7455 atts[nbatts++] = aprefix;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007456 atts[nbatts++] = NULL; /* the URI will be fetched later */
Daniel Veillard0fb18932003-09-07 09:14:37 +00007457 atts[nbatts++] = attvalue;
7458 attvalue += len;
7459 atts[nbatts++] = attvalue;
7460 /*
7461 * tag if some deallocation is needed
7462 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00007463 if (alloc != 0) attval = 1;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007464 } else {
7465 if ((attvalue != NULL) && (attvalue[len] == 0))
7466 xmlFree(attvalue);
7467 }
7468
7469failed:
7470
7471 GROW
Daniel Veillarde57ec792003-09-10 10:50:59 +00007472 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007473 if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
7474 break;
William M. Brack76e95df2003-10-18 16:20:14 +00007475 if (!IS_BLANK_CH(RAW)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007476 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
7477 "attributes construct error\n");
William M. Brack13dfa872004-09-18 04:52:08 +00007478 break;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007479 }
7480 SKIP_BLANKS;
7481 if ((cons == ctxt->input->consumed) && (q == CUR_PTR) &&
7482 (attname == NULL) && (attvalue == NULL)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007483 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007484 "xmlParseStartTag: problem parsing attributes\n");
Daniel Veillard0fb18932003-09-07 09:14:37 +00007485 break;
7486 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007487 GROW;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007488 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007489 }
7490
Daniel Veillard0fb18932003-09-07 09:14:37 +00007491 /*
Daniel Veillarde57ec792003-09-10 10:50:59 +00007492 * The attributes defaulting
7493 */
7494 if (ctxt->attsDefault != NULL) {
7495 xmlDefAttrsPtr defaults;
7496
7497 defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix);
7498 if (defaults != NULL) {
7499 for (i = 0;i < defaults->nbAttrs;i++) {
7500 attname = defaults->values[4 * i];
7501 aprefix = defaults->values[4 * i + 1];
7502
7503 /*
7504 * special work for namespaces defaulted defs
7505 */
7506 if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
7507 /*
7508 * check that it's not a defined namespace
7509 */
7510 for (j = 1;j <= nbNs;j++)
7511 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL)
7512 break;
7513 if (j <= nbNs) continue;
7514
7515 nsname = xmlGetNamespace(ctxt, NULL);
7516 if (nsname != defaults->values[4 * i + 2]) {
7517 if (nsPush(ctxt, NULL,
7518 defaults->values[4 * i + 2]) > 0)
7519 nbNs++;
7520 }
7521 } else if (aprefix == ctxt->str_xmlns) {
7522 /*
7523 * check that it's not a defined namespace
7524 */
7525 for (j = 1;j <= nbNs;j++)
7526 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname)
7527 break;
7528 if (j <= nbNs) continue;
7529
7530 nsname = xmlGetNamespace(ctxt, attname);
7531 if (nsname != defaults->values[2]) {
7532 if (nsPush(ctxt, attname,
7533 defaults->values[4 * i + 2]) > 0)
7534 nbNs++;
7535 }
7536 } else {
7537 /*
7538 * check that it's not a defined attribute
7539 */
7540 for (j = 0;j < nbatts;j+=5) {
7541 if ((attname == atts[j]) && (aprefix == atts[j+1]))
7542 break;
7543 }
7544 if (j < nbatts) continue;
7545
7546 if ((atts == NULL) || (nbatts + 5 > maxatts)) {
7547 if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00007548 return(NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007549 }
7550 maxatts = ctxt->maxatts;
7551 atts = ctxt->atts;
7552 }
7553 atts[nbatts++] = attname;
7554 atts[nbatts++] = aprefix;
7555 if (aprefix == NULL)
7556 atts[nbatts++] = NULL;
7557 else
7558 atts[nbatts++] = xmlGetNamespace(ctxt, aprefix);
7559 atts[nbatts++] = defaults->values[4 * i + 2];
7560 atts[nbatts++] = defaults->values[4 * i + 3];
7561 nbdef++;
7562 }
7563 }
7564 }
7565 }
7566
Daniel Veillarde70c8772003-11-25 07:21:18 +00007567 /*
7568 * The attributes checkings
7569 */
7570 for (i = 0; i < nbatts;i += 5) {
7571 nsname = xmlGetNamespace(ctxt, atts[i + 1]);
7572 if ((atts[i + 1] != NULL) && (nsname == NULL)) {
7573 xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
7574 "Namespace prefix %s for %s on %s is not defined\n",
7575 atts[i + 1], atts[i], localname);
7576 }
7577 atts[i + 2] = nsname;
7578 /*
7579 * [ WFC: Unique Att Spec ]
7580 * No attribute name may appear more than once in the same
7581 * start-tag or empty-element tag.
7582 * As extended by the Namespace in XML REC.
7583 */
7584 for (j = 0; j < i;j += 5) {
7585 if (atts[i] == atts[j]) {
7586 if (atts[i+1] == atts[j+1]) {
7587 xmlErrAttributeDup(ctxt, atts[i+1], atts[i]);
7588 break;
7589 }
7590 if ((nsname != NULL) && (atts[j + 2] == nsname)) {
7591 xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED,
7592 "Namespaced Attribute %s in '%s' redefined\n",
7593 atts[i], nsname, NULL);
7594 break;
7595 }
7596 }
7597 }
7598 }
7599
Daniel Veillarde57ec792003-09-10 10:50:59 +00007600 nsname = xmlGetNamespace(ctxt, prefix);
7601 if ((prefix != NULL) && (nsname == NULL)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007602 xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
7603 "Namespace prefix %s on %s is not defined\n",
7604 prefix, localname, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007605 }
7606 *pref = prefix;
7607 *URI = nsname;
7608
7609 /*
7610 * SAX: Start of Element !
7611 */
7612 if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) &&
7613 (!ctxt->disableSAX)) {
7614 if (nbNs > 0)
7615 ctxt->sax->startElementNs(ctxt->userData, localname, prefix,
7616 nsname, nbNs, &ctxt->nsTab[ctxt->nsNr - 2 * nbNs],
7617 nbatts / 5, nbdef, atts);
7618 else
7619 ctxt->sax->startElementNs(ctxt->userData, localname, prefix,
7620 nsname, 0, NULL, nbatts / 5, nbdef, atts);
7621 }
7622
7623 /*
7624 * Free up attribute allocated strings if needed
7625 */
7626 if (attval != 0) {
7627 for (i = 3,j = 0; j < nratts;i += 5,j++)
7628 if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL))
7629 xmlFree((xmlChar *) atts[i]);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007630 }
7631
7632 return(localname);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007633
7634base_changed:
7635 /*
7636 * the attribute strings are valid iif the base didn't changed
7637 */
7638 if (attval != 0) {
7639 for (i = 3,j = 0; j < nratts;i += 5,j++)
7640 if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL))
7641 xmlFree((xmlChar *) atts[i]);
7642 }
7643 ctxt->input->cur = ctxt->input->base + cur;
7644 if (ctxt->wellFormed == 1) {
7645 goto reparse;
7646 }
7647 return(NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007648}
7649
7650/**
7651 * xmlParseEndTag2:
7652 * @ctxt: an XML parser context
7653 * @line: line of the start tag
7654 * @nsNr: number of namespaces on the start tag
7655 *
7656 * parse an end of tag
7657 *
7658 * [42] ETag ::= '</' Name S? '>'
7659 *
7660 * With namespace
7661 *
7662 * [NS 9] ETag ::= '</' QName S? '>'
7663 */
7664
7665static void
7666xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlChar *prefix,
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007667 const xmlChar *URI, int line, int nsNr, int tlen) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007668 const xmlChar *name;
7669
7670 GROW;
7671 if ((RAW != '<') || (NXT(1) != '/')) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007672 xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007673 return;
7674 }
7675 SKIP(2);
7676
William M. Brack13dfa872004-09-18 04:52:08 +00007677 if ((tlen > 0) && (xmlStrncmp(ctxt->input->cur, ctxt->name, tlen) == 0)) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007678 if (ctxt->input->cur[tlen] == '>') {
7679 ctxt->input->cur += tlen + 1;
7680 goto done;
7681 }
7682 ctxt->input->cur += tlen;
7683 name = (xmlChar*)1;
7684 } else {
7685 if (prefix == NULL)
7686 name = xmlParseNameAndCompare(ctxt, ctxt->name);
7687 else
7688 name = xmlParseQNameAndCompare(ctxt, ctxt->name, prefix);
7689 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007690
7691 /*
7692 * We should definitely be at the ending "S? '>'" part
7693 */
7694 GROW;
7695 SKIP_BLANKS;
Daniel Veillard73b013f2003-09-30 12:36:01 +00007696 if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007697 xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007698 } else
7699 NEXT1;
7700
7701 /*
7702 * [ WFC: Element Type Match ]
7703 * The Name in an element's end-tag must match the element type in the
7704 * start-tag.
7705 *
7706 */
7707 if (name != (xmlChar*)1) {
Daniel Veillardf403d292003-10-05 13:51:35 +00007708 if (name == NULL) name = BAD_CAST "unparseable";
7709 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007710 "Opening and ending tag mismatch: %s line %d and %s\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00007711 ctxt->name, line, name);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007712 }
7713
7714 /*
7715 * SAX: End of Tag
7716 */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007717done:
Daniel Veillard0fb18932003-09-07 09:14:37 +00007718 if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
7719 (!ctxt->disableSAX))
7720 ctxt->sax->endElementNs(ctxt->userData, ctxt->name, prefix, URI);
7721
Daniel Veillard0fb18932003-09-07 09:14:37 +00007722 spacePop(ctxt);
7723 if (nsNr != 0)
7724 nsPop(ctxt, nsNr);
7725 return;
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00007726}
7727
7728/**
Owen Taylor3473f882001-02-23 17:55:21 +00007729 * xmlParseCDSect:
7730 * @ctxt: an XML parser context
7731 *
7732 * Parse escaped pure raw content.
7733 *
7734 * [18] CDSect ::= CDStart CData CDEnd
7735 *
7736 * [19] CDStart ::= '<![CDATA['
7737 *
7738 * [20] Data ::= (Char* - (Char* ']]>' Char*))
7739 *
7740 * [21] CDEnd ::= ']]>'
7741 */
7742void
7743xmlParseCDSect(xmlParserCtxtPtr ctxt) {
7744 xmlChar *buf = NULL;
7745 int len = 0;
7746 int size = XML_PARSER_BUFFER_SIZE;
7747 int r, rl;
7748 int s, sl;
7749 int cur, l;
7750 int count = 0;
7751
Daniel Veillard8f597c32003-10-06 08:19:27 +00007752 /* Check 2.6.0 was NXT(0) not RAW */
Daniel Veillarda07050d2003-10-19 14:46:32 +00007753 if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007754 SKIP(9);
7755 } else
7756 return;
7757
7758 ctxt->instate = XML_PARSER_CDATA_SECTION;
7759 r = CUR_CHAR(rl);
William M. Brack871611b2003-10-18 04:53:14 +00007760 if (!IS_CHAR(r)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007761 xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007762 ctxt->instate = XML_PARSER_CONTENT;
7763 return;
7764 }
7765 NEXTL(rl);
7766 s = CUR_CHAR(sl);
William M. Brack871611b2003-10-18 04:53:14 +00007767 if (!IS_CHAR(s)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007768 xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007769 ctxt->instate = XML_PARSER_CONTENT;
7770 return;
7771 }
7772 NEXTL(sl);
7773 cur = CUR_CHAR(l);
Daniel Veillard3c908dc2003-04-19 00:07:51 +00007774 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00007775 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007776 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007777 return;
7778 }
William M. Brack871611b2003-10-18 04:53:14 +00007779 while (IS_CHAR(cur) &&
Owen Taylor3473f882001-02-23 17:55:21 +00007780 ((r != ']') || (s != ']') || (cur != '>'))) {
7781 if (len + 5 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00007782 xmlChar *tmp;
7783
Owen Taylor3473f882001-02-23 17:55:21 +00007784 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00007785 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
7786 if (tmp == NULL) {
7787 xmlFree(buf);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007788 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007789 return;
7790 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00007791 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00007792 }
7793 COPY_BUF(rl,buf,len,r);
7794 r = s;
7795 rl = sl;
7796 s = cur;
7797 sl = l;
7798 count++;
7799 if (count > 50) {
7800 GROW;
7801 count = 0;
7802 }
7803 NEXTL(l);
7804 cur = CUR_CHAR(l);
7805 }
7806 buf[len] = 0;
7807 ctxt->instate = XML_PARSER_CONTENT;
7808 if (cur != '>') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00007809 xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED,
Owen Taylor3473f882001-02-23 17:55:21 +00007810 "CData section not finished\n%.50s\n", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00007811 xmlFree(buf);
7812 return;
7813 }
7814 NEXTL(l);
7815
7816 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00007817 * OK the buffer is to be consumed as cdata.
Owen Taylor3473f882001-02-23 17:55:21 +00007818 */
7819 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
7820 if (ctxt->sax->cdataBlock != NULL)
7821 ctxt->sax->cdataBlock(ctxt->userData, buf, len);
Daniel Veillard7583a592001-07-08 13:15:55 +00007822 else if (ctxt->sax->characters != NULL)
7823 ctxt->sax->characters(ctxt->userData, buf, len);
Owen Taylor3473f882001-02-23 17:55:21 +00007824 }
7825 xmlFree(buf);
7826}
7827
7828/**
7829 * xmlParseContent:
7830 * @ctxt: an XML parser context
7831 *
7832 * Parse a content:
7833 *
7834 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
7835 */
7836
7837void
7838xmlParseContent(xmlParserCtxtPtr ctxt) {
7839 GROW;
Daniel Veillardfdc91562002-07-01 21:52:03 +00007840 while ((RAW != 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +00007841 ((RAW != '<') || (NXT(1) != '/'))) {
7842 const xmlChar *test = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00007843 unsigned int cons = ctxt->input->consumed;
Daniel Veillard21a0f912001-02-25 19:54:14 +00007844 const xmlChar *cur = ctxt->input->cur;
Owen Taylor3473f882001-02-23 17:55:21 +00007845
7846 /*
Owen Taylor3473f882001-02-23 17:55:21 +00007847 * First case : a Processing Instruction.
7848 */
Daniel Veillardfdc91562002-07-01 21:52:03 +00007849 if ((*cur == '<') && (cur[1] == '?')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007850 xmlParsePI(ctxt);
7851 }
7852
7853 /*
7854 * Second case : a CDSection
7855 */
Daniel Veillard8f597c32003-10-06 08:19:27 +00007856 /* 2.6.0 test was *cur not RAW */
Daniel Veillarda07050d2003-10-19 14:46:32 +00007857 else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007858 xmlParseCDSect(ctxt);
7859 }
7860
7861 /*
7862 * Third case : a comment
7863 */
Daniel Veillard21a0f912001-02-25 19:54:14 +00007864 else if ((*cur == '<') && (NXT(1) == '!') &&
Owen Taylor3473f882001-02-23 17:55:21 +00007865 (NXT(2) == '-') && (NXT(3) == '-')) {
7866 xmlParseComment(ctxt);
7867 ctxt->instate = XML_PARSER_CONTENT;
7868 }
7869
7870 /*
7871 * Fourth case : a sub-element.
7872 */
Daniel Veillard21a0f912001-02-25 19:54:14 +00007873 else if (*cur == '<') {
Owen Taylor3473f882001-02-23 17:55:21 +00007874 xmlParseElement(ctxt);
7875 }
7876
7877 /*
7878 * Fifth case : a reference. If if has not been resolved,
7879 * parsing returns it's Name, create the node
7880 */
7881
Daniel Veillard21a0f912001-02-25 19:54:14 +00007882 else if (*cur == '&') {
Owen Taylor3473f882001-02-23 17:55:21 +00007883 xmlParseReference(ctxt);
7884 }
7885
7886 /*
7887 * Last case, text. Note that References are handled directly.
7888 */
7889 else {
7890 xmlParseCharData(ctxt, 0);
7891 }
7892
7893 GROW;
7894 /*
7895 * Pop-up of finished entities.
7896 */
Daniel Veillard561b7f82002-03-20 21:55:57 +00007897 while ((RAW == 0) && (ctxt->inputNr > 1))
Owen Taylor3473f882001-02-23 17:55:21 +00007898 xmlPopInput(ctxt);
7899 SHRINK;
7900
Daniel Veillardfdc91562002-07-01 21:52:03 +00007901 if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007902 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
7903 "detected an error in element content\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007904 ctxt->instate = XML_PARSER_EOF;
7905 break;
7906 }
7907 }
7908}
7909
7910/**
7911 * xmlParseElement:
7912 * @ctxt: an XML parser context
7913 *
7914 * parse an XML element, this is highly recursive
7915 *
7916 * [39] element ::= EmptyElemTag | STag content ETag
7917 *
7918 * [ WFC: Element Type Match ]
7919 * The Name in an element's end-tag must match the element type in the
7920 * start-tag.
7921 *
Owen Taylor3473f882001-02-23 17:55:21 +00007922 */
7923
7924void
7925xmlParseElement(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00007926 const xmlChar *name;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007927 const xmlChar *prefix;
7928 const xmlChar *URI;
Owen Taylor3473f882001-02-23 17:55:21 +00007929 xmlParserNodeInfo node_info;
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007930 int line, tlen;
Owen Taylor3473f882001-02-23 17:55:21 +00007931 xmlNodePtr ret;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007932 int nsNr = ctxt->nsNr;
Owen Taylor3473f882001-02-23 17:55:21 +00007933
7934 /* Capture start position */
7935 if (ctxt->record_info) {
7936 node_info.begin_pos = ctxt->input->consumed +
7937 (CUR_PTR - ctxt->input->base);
7938 node_info.begin_line = ctxt->input->line;
7939 }
7940
7941 if (ctxt->spaceNr == 0)
7942 spacePush(ctxt, -1);
7943 else
7944 spacePush(ctxt, *ctxt->space);
7945
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00007946 line = ctxt->input->line;
Daniel Veillard81273902003-09-30 00:43:48 +00007947#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard0fb18932003-09-07 09:14:37 +00007948 if (ctxt->sax2)
Daniel Veillard81273902003-09-30 00:43:48 +00007949#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007950 name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen);
Daniel Veillard81273902003-09-30 00:43:48 +00007951#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard0fb18932003-09-07 09:14:37 +00007952 else
7953 name = xmlParseStartTag(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00007954#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00007955 if (name == NULL) {
7956 spacePop(ctxt);
7957 return;
7958 }
7959 namePush(ctxt, name);
7960 ret = ctxt->node;
7961
Daniel Veillard4432df22003-09-28 18:58:27 +00007962#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00007963 /*
7964 * [ VC: Root Element Type ]
7965 * The Name in the document type declaration must match the element
7966 * type of the root element.
7967 */
7968 if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
7969 ctxt->node && (ctxt->node == ctxt->myDoc->children))
7970 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillard4432df22003-09-28 18:58:27 +00007971#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00007972
7973 /*
7974 * Check for an Empty Element.
7975 */
7976 if ((RAW == '/') && (NXT(1) == '>')) {
7977 SKIP(2);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007978 if (ctxt->sax2) {
7979 if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
7980 (!ctxt->disableSAX))
7981 ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI);
Daniel Veillard81273902003-09-30 00:43:48 +00007982#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard0fb18932003-09-07 09:14:37 +00007983 } else {
7984 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
7985 (!ctxt->disableSAX))
7986 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard81273902003-09-30 00:43:48 +00007987#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00007988 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007989 namePop(ctxt);
7990 spacePop(ctxt);
7991 if (nsNr != ctxt->nsNr)
7992 nsPop(ctxt, ctxt->nsNr - nsNr);
Owen Taylor3473f882001-02-23 17:55:21 +00007993 if ( ret != NULL && ctxt->record_info ) {
7994 node_info.end_pos = ctxt->input->consumed +
7995 (CUR_PTR - ctxt->input->base);
7996 node_info.end_line = ctxt->input->line;
7997 node_info.node = ret;
7998 xmlParserAddNodeInfo(ctxt, &node_info);
7999 }
8000 return;
8001 }
8002 if (RAW == '>') {
Daniel Veillard21a0f912001-02-25 19:54:14 +00008003 NEXT1;
Owen Taylor3473f882001-02-23 17:55:21 +00008004 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00008005 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED,
8006 "Couldn't find end of Start Tag %s line %d\n",
8007 name, line, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008008
8009 /*
8010 * end of parsing of this node.
8011 */
8012 nodePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008013 namePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008014 spacePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008015 if (nsNr != ctxt->nsNr)
8016 nsPop(ctxt, ctxt->nsNr - nsNr);
Owen Taylor3473f882001-02-23 17:55:21 +00008017
8018 /*
8019 * Capture end position and add node
8020 */
8021 if ( ret != NULL && ctxt->record_info ) {
8022 node_info.end_pos = ctxt->input->consumed +
8023 (CUR_PTR - ctxt->input->base);
8024 node_info.end_line = ctxt->input->line;
8025 node_info.node = ret;
8026 xmlParserAddNodeInfo(ctxt, &node_info);
8027 }
8028 return;
8029 }
8030
8031 /*
8032 * Parse the content of the element:
8033 */
8034 xmlParseContent(ctxt);
Daniel Veillard73b013f2003-09-30 12:36:01 +00008035 if (!IS_BYTE_CHAR(RAW)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00008036 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED,
Daniel Veillard2b0f8792003-10-10 19:36:36 +00008037 "Premature end of data in tag %s line %d\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00008038 name, line, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008039
8040 /*
8041 * end of parsing of this node.
8042 */
8043 nodePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008044 namePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00008045 spacePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008046 if (nsNr != ctxt->nsNr)
8047 nsPop(ctxt, ctxt->nsNr - nsNr);
Owen Taylor3473f882001-02-23 17:55:21 +00008048 return;
8049 }
8050
8051 /*
8052 * parse the end of tag: '</' should be here.
8053 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00008054 if (ctxt->sax2) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00008055 xmlParseEndTag2(ctxt, prefix, URI, line, ctxt->nsNr - nsNr, tlen);
Daniel Veillarde57ec792003-09-10 10:50:59 +00008056 namePop(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00008057 }
8058#ifdef LIBXML_SAX1_ENABLED
8059 else
Daniel Veillard0fb18932003-09-07 09:14:37 +00008060 xmlParseEndTag1(ctxt, line);
Daniel Veillard81273902003-09-30 00:43:48 +00008061#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00008062
8063 /*
8064 * Capture end position and add node
8065 */
8066 if ( ret != NULL && ctxt->record_info ) {
8067 node_info.end_pos = ctxt->input->consumed +
8068 (CUR_PTR - ctxt->input->base);
8069 node_info.end_line = ctxt->input->line;
8070 node_info.node = ret;
8071 xmlParserAddNodeInfo(ctxt, &node_info);
8072 }
8073}
8074
8075/**
8076 * xmlParseVersionNum:
8077 * @ctxt: an XML parser context
8078 *
8079 * parse the XML version value.
8080 *
8081 * [26] VersionNum ::= ([a-zA-Z0-9_.:] | '-')+
8082 *
8083 * Returns the string giving the XML version number, or NULL
8084 */
8085xmlChar *
8086xmlParseVersionNum(xmlParserCtxtPtr ctxt) {
8087 xmlChar *buf = NULL;
8088 int len = 0;
8089 int size = 10;
8090 xmlChar cur;
8091
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008092 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00008093 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008094 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008095 return(NULL);
8096 }
8097 cur = CUR;
8098 while (((cur >= 'a') && (cur <= 'z')) ||
8099 ((cur >= 'A') && (cur <= 'Z')) ||
8100 ((cur >= '0') && (cur <= '9')) ||
8101 (cur == '_') || (cur == '.') ||
8102 (cur == ':') || (cur == '-')) {
8103 if (len + 1 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00008104 xmlChar *tmp;
8105
Owen Taylor3473f882001-02-23 17:55:21 +00008106 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00008107 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
8108 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008109 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008110 return(NULL);
8111 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00008112 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00008113 }
8114 buf[len++] = cur;
8115 NEXT;
8116 cur=CUR;
8117 }
8118 buf[len] = 0;
8119 return(buf);
8120}
8121
8122/**
8123 * xmlParseVersionInfo:
8124 * @ctxt: an XML parser context
8125 *
8126 * parse the XML version.
8127 *
8128 * [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ")
8129 *
8130 * [25] Eq ::= S? '=' S?
8131 *
8132 * Returns the version string, e.g. "1.0"
8133 */
8134
8135xmlChar *
8136xmlParseVersionInfo(xmlParserCtxtPtr ctxt) {
8137 xmlChar *version = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00008138
Daniel Veillarda07050d2003-10-19 14:46:32 +00008139 if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008140 SKIP(7);
8141 SKIP_BLANKS;
8142 if (RAW != '=') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008143 xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008144 return(NULL);
8145 }
8146 NEXT;
8147 SKIP_BLANKS;
8148 if (RAW == '"') {
8149 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008150 version = xmlParseVersionNum(ctxt);
8151 if (RAW != '"') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008152 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008153 } else
8154 NEXT;
8155 } else if (RAW == '\''){
8156 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008157 version = xmlParseVersionNum(ctxt);
8158 if (RAW != '\'') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008159 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008160 } else
8161 NEXT;
8162 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008163 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008164 }
8165 }
8166 return(version);
8167}
8168
8169/**
8170 * xmlParseEncName:
8171 * @ctxt: an XML parser context
8172 *
8173 * parse the XML encoding name
8174 *
8175 * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
8176 *
8177 * Returns the encoding name value or NULL
8178 */
8179xmlChar *
8180xmlParseEncName(xmlParserCtxtPtr ctxt) {
8181 xmlChar *buf = NULL;
8182 int len = 0;
8183 int size = 10;
8184 xmlChar cur;
8185
8186 cur = CUR;
8187 if (((cur >= 'a') && (cur <= 'z')) ||
8188 ((cur >= 'A') && (cur <= 'Z'))) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008189 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00008190 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008191 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008192 return(NULL);
8193 }
8194
8195 buf[len++] = cur;
8196 NEXT;
8197 cur = CUR;
8198 while (((cur >= 'a') && (cur <= 'z')) ||
8199 ((cur >= 'A') && (cur <= 'Z')) ||
8200 ((cur >= '0') && (cur <= '9')) ||
8201 (cur == '.') || (cur == '_') ||
8202 (cur == '-')) {
8203 if (len + 1 >= size) {
Daniel Veillard2248ff12004-09-22 23:05:14 +00008204 xmlChar *tmp;
8205
Owen Taylor3473f882001-02-23 17:55:21 +00008206 size *= 2;
Daniel Veillard2248ff12004-09-22 23:05:14 +00008207 tmp = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
8208 if (tmp == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008209 xmlErrMemory(ctxt, NULL);
Daniel Veillard2248ff12004-09-22 23:05:14 +00008210 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00008211 return(NULL);
8212 }
Daniel Veillard2248ff12004-09-22 23:05:14 +00008213 buf = tmp;
Owen Taylor3473f882001-02-23 17:55:21 +00008214 }
8215 buf[len++] = cur;
8216 NEXT;
8217 cur = CUR;
8218 if (cur == 0) {
8219 SHRINK;
8220 GROW;
8221 cur = CUR;
8222 }
8223 }
8224 buf[len] = 0;
8225 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008226 xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008227 }
8228 return(buf);
8229}
8230
8231/**
8232 * xmlParseEncodingDecl:
8233 * @ctxt: an XML parser context
8234 *
8235 * parse the XML encoding declaration
8236 *
8237 * [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'")
8238 *
8239 * this setups the conversion filters.
8240 *
8241 * Returns the encoding value or NULL
8242 */
8243
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008244const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00008245xmlParseEncodingDecl(xmlParserCtxtPtr ctxt) {
8246 xmlChar *encoding = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00008247
8248 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008249 if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008250 SKIP(8);
8251 SKIP_BLANKS;
8252 if (RAW != '=') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008253 xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008254 return(NULL);
8255 }
8256 NEXT;
8257 SKIP_BLANKS;
8258 if (RAW == '"') {
8259 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008260 encoding = xmlParseEncName(ctxt);
8261 if (RAW != '"') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008262 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008263 } else
8264 NEXT;
8265 } else if (RAW == '\''){
8266 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008267 encoding = xmlParseEncName(ctxt);
8268 if (RAW != '\'') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008269 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008270 } else
8271 NEXT;
Daniel Veillard82ac6b02002-02-17 23:18:55 +00008272 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008273 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008274 }
Daniel Veillard6b621b82003-08-11 15:03:34 +00008275 /*
8276 * UTF-16 encoding stwich has already taken place at this stage,
8277 * more over the little-endian/big-endian selection is already done
8278 */
8279 if ((encoding != NULL) &&
8280 ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-16")) ||
8281 (!xmlStrcasecmp(encoding, BAD_CAST "UTF16")))) {
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008282 if (ctxt->encoding != NULL)
8283 xmlFree((xmlChar *) ctxt->encoding);
8284 ctxt->encoding = encoding;
Daniel Veillardb19ba832003-08-14 00:33:46 +00008285 }
8286 /*
8287 * UTF-8 encoding is handled natively
8288 */
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008289 else if ((encoding != NULL) &&
Daniel Veillardb19ba832003-08-14 00:33:46 +00008290 ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-8")) ||
8291 (!xmlStrcasecmp(encoding, BAD_CAST "UTF8")))) {
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008292 if (ctxt->encoding != NULL)
8293 xmlFree((xmlChar *) ctxt->encoding);
8294 ctxt->encoding = encoding;
Daniel Veillard6b621b82003-08-11 15:03:34 +00008295 }
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008296 else if (encoding != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00008297 xmlCharEncodingHandlerPtr handler;
8298
8299 if (ctxt->input->encoding != NULL)
8300 xmlFree((xmlChar *) ctxt->input->encoding);
8301 ctxt->input->encoding = encoding;
8302
Daniel Veillarda6874ca2003-07-29 16:47:24 +00008303 handler = xmlFindCharEncodingHandler((const char *) encoding);
8304 if (handler != NULL) {
8305 xmlSwitchToEncoding(ctxt, handler);
Owen Taylor3473f882001-02-23 17:55:21 +00008306 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00008307 xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillarda6874ca2003-07-29 16:47:24 +00008308 "Unsupported encoding %s\n", encoding);
8309 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008310 }
8311 }
8312 }
8313 return(encoding);
8314}
8315
8316/**
8317 * xmlParseSDDecl:
8318 * @ctxt: an XML parser context
8319 *
8320 * parse the XML standalone declaration
8321 *
8322 * [32] SDDecl ::= S 'standalone' Eq
8323 * (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"'))
8324 *
8325 * [ VC: Standalone Document Declaration ]
8326 * TODO The standalone document declaration must have the value "no"
8327 * if any external markup declarations contain declarations of:
8328 * - attributes with default values, if elements to which these
8329 * attributes apply appear in the document without specifications
8330 * of values for these attributes, or
8331 * - entities (other than amp, lt, gt, apos, quot), if references
8332 * to those entities appear in the document, or
8333 * - attributes with values subject to normalization, where the
8334 * attribute appears in the document with a value which will change
8335 * as a result of normalization, or
8336 * - element types with element content, if white space occurs directly
8337 * within any instance of those types.
8338 *
8339 * Returns 1 if standalone, 0 otherwise
8340 */
8341
8342int
8343xmlParseSDDecl(xmlParserCtxtPtr ctxt) {
8344 int standalone = -1;
8345
8346 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008347 if (CMP10(CUR_PTR, 's', 't', 'a', 'n', 'd', 'a', 'l', 'o', 'n', 'e')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008348 SKIP(10);
8349 SKIP_BLANKS;
8350 if (RAW != '=') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008351 xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008352 return(standalone);
8353 }
8354 NEXT;
8355 SKIP_BLANKS;
8356 if (RAW == '\''){
8357 NEXT;
8358 if ((RAW == 'n') && (NXT(1) == 'o')) {
8359 standalone = 0;
8360 SKIP(2);
8361 } else if ((RAW == 'y') && (NXT(1) == 'e') &&
8362 (NXT(2) == 's')) {
8363 standalone = 1;
8364 SKIP(3);
8365 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008366 xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008367 }
8368 if (RAW != '\'') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008369 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008370 } else
8371 NEXT;
8372 } else if (RAW == '"'){
8373 NEXT;
8374 if ((RAW == 'n') && (NXT(1) == 'o')) {
8375 standalone = 0;
8376 SKIP(2);
8377 } else if ((RAW == 'y') && (NXT(1) == 'e') &&
8378 (NXT(2) == 's')) {
8379 standalone = 1;
8380 SKIP(3);
8381 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008382 xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008383 }
8384 if (RAW != '"') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008385 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008386 } else
8387 NEXT;
8388 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008389 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008390 }
8391 }
8392 return(standalone);
8393}
8394
8395/**
8396 * xmlParseXMLDecl:
8397 * @ctxt: an XML parser context
8398 *
8399 * parse an XML declaration header
8400 *
8401 * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
8402 */
8403
8404void
8405xmlParseXMLDecl(xmlParserCtxtPtr ctxt) {
8406 xmlChar *version;
8407
8408 /*
8409 * We know that '<?xml' is here.
8410 */
8411 SKIP(5);
8412
William M. Brack76e95df2003-10-18 16:20:14 +00008413 if (!IS_BLANK_CH(RAW)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008414 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
8415 "Blank needed after '<?xml'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008416 }
8417 SKIP_BLANKS;
8418
8419 /*
Daniel Veillard19840942001-11-29 16:11:38 +00008420 * We must have the VersionInfo here.
Owen Taylor3473f882001-02-23 17:55:21 +00008421 */
8422 version = xmlParseVersionInfo(ctxt);
Daniel Veillard19840942001-11-29 16:11:38 +00008423 if (version == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008424 xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL);
Daniel Veillard19840942001-11-29 16:11:38 +00008425 } else {
8426 if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) {
8427 /*
8428 * TODO: Blueberry should be detected here
8429 */
Daniel Veillard24eb9782003-10-04 21:08:09 +00008430 xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION,
8431 "Unsupported version '%s'\n",
8432 version, NULL);
Daniel Veillard19840942001-11-29 16:11:38 +00008433 }
8434 if (ctxt->version != NULL)
Daniel Veillardd3b08822001-12-05 12:03:33 +00008435 xmlFree((void *) ctxt->version);
Daniel Veillard19840942001-11-29 16:11:38 +00008436 ctxt->version = version;
Daniel Veillarda050d232001-09-05 15:51:05 +00008437 }
Owen Taylor3473f882001-02-23 17:55:21 +00008438
8439 /*
8440 * We may have the encoding declaration
8441 */
William M. Brack76e95df2003-10-18 16:20:14 +00008442 if (!IS_BLANK_CH(RAW)) {
Owen Taylor3473f882001-02-23 17:55:21 +00008443 if ((RAW == '?') && (NXT(1) == '>')) {
8444 SKIP(2);
8445 return;
8446 }
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008447 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008448 }
8449 xmlParseEncodingDecl(ctxt);
8450 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
8451 /*
8452 * The XML REC instructs us to stop parsing right here
8453 */
8454 return;
8455 }
8456
8457 /*
8458 * We may have the standalone status.
8459 */
William M. Brack76e95df2003-10-18 16:20:14 +00008460 if ((ctxt->input->encoding != NULL) && (!IS_BLANK_CH(RAW))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008461 if ((RAW == '?') && (NXT(1) == '>')) {
8462 SKIP(2);
8463 return;
8464 }
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008465 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008466 }
8467 SKIP_BLANKS;
8468 ctxt->input->standalone = xmlParseSDDecl(ctxt);
8469
8470 SKIP_BLANKS;
8471 if ((RAW == '?') && (NXT(1) == '>')) {
8472 SKIP(2);
8473 } else if (RAW == '>') {
8474 /* Deprecated old WD ... */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008475 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008476 NEXT;
8477 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008478 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008479 MOVETO_ENDTAG(CUR_PTR);
8480 NEXT;
8481 }
8482}
8483
8484/**
8485 * xmlParseMisc:
8486 * @ctxt: an XML parser context
8487 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00008488 * parse an XML Misc* optional field.
Owen Taylor3473f882001-02-23 17:55:21 +00008489 *
8490 * [27] Misc ::= Comment | PI | S
8491 */
8492
8493void
8494xmlParseMisc(xmlParserCtxtPtr ctxt) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00008495 while (((RAW == '<') && (NXT(1) == '?')) ||
Daniel Veillarda07050d2003-10-19 14:46:32 +00008496 (CMP4(CUR_PTR, '<', '!', '-', '-')) ||
William M. Brack76e95df2003-10-18 16:20:14 +00008497 IS_BLANK_CH(CUR)) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00008498 if ((RAW == '<') && (NXT(1) == '?')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008499 xmlParsePI(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00008500 } else if (IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00008501 NEXT;
8502 } else
8503 xmlParseComment(ctxt);
8504 }
8505}
8506
8507/**
8508 * xmlParseDocument:
8509 * @ctxt: an XML parser context
8510 *
8511 * parse an XML document (and build a tree if using the standard SAX
8512 * interface).
8513 *
8514 * [1] document ::= prolog element Misc*
8515 *
8516 * [22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)?
8517 *
8518 * Returns 0, -1 in case of error. the parser context is augmented
8519 * as a result of the parsing.
8520 */
8521
8522int
8523xmlParseDocument(xmlParserCtxtPtr ctxt) {
8524 xmlChar start[4];
8525 xmlCharEncoding enc;
8526
8527 xmlInitParser();
8528
8529 GROW;
8530
8531 /*
Daniel Veillard0fb18932003-09-07 09:14:37 +00008532 * SAX: detecting the level.
8533 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00008534 xmlDetectSAX2(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008535
8536 /*
Owen Taylor3473f882001-02-23 17:55:21 +00008537 * SAX: beginning of the document processing.
8538 */
8539 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
8540 ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
8541
Daniel Veillard4aede2e2003-10-17 12:43:59 +00008542 if ((ctxt->encoding == (const xmlChar *)XML_CHAR_ENCODING_NONE) &&
8543 ((ctxt->input->end - ctxt->input->cur) >= 4)) {
Daniel Veillard4aafa792001-07-28 17:21:12 +00008544 /*
8545 * Get the 4 first bytes and decode the charset
8546 * if enc != XML_CHAR_ENCODING_NONE
8547 * plug some encoding conversion routines.
8548 */
8549 start[0] = RAW;
8550 start[1] = NXT(1);
8551 start[2] = NXT(2);
8552 start[3] = NXT(3);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00008553 enc = xmlDetectCharEncoding(&start[0], 4);
Daniel Veillard4aafa792001-07-28 17:21:12 +00008554 if (enc != XML_CHAR_ENCODING_NONE) {
8555 xmlSwitchEncoding(ctxt, enc);
8556 }
Owen Taylor3473f882001-02-23 17:55:21 +00008557 }
8558
8559
8560 if (CUR == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008561 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008562 }
8563
8564 /*
8565 * Check for the XMLDecl in the Prolog.
8566 */
8567 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008568 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008569
8570 /*
8571 * Note that we will switch encoding on the fly.
8572 */
8573 xmlParseXMLDecl(ctxt);
8574 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
8575 /*
8576 * The XML REC instructs us to stop parsing right here
8577 */
8578 return(-1);
8579 }
8580 ctxt->standalone = ctxt->input->standalone;
8581 SKIP_BLANKS;
8582 } else {
8583 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
8584 }
8585 if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
8586 ctxt->sax->startDocument(ctxt->userData);
8587
8588 /*
8589 * The Misc part of the Prolog
8590 */
8591 GROW;
8592 xmlParseMisc(ctxt);
8593
8594 /*
8595 * Then possibly doc type declaration(s) and more Misc
8596 * (doctypedecl Misc*)?
8597 */
8598 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008599 if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008600
8601 ctxt->inSubset = 1;
8602 xmlParseDocTypeDecl(ctxt);
8603 if (RAW == '[') {
8604 ctxt->instate = XML_PARSER_DTD;
8605 xmlParseInternalSubset(ctxt);
8606 }
8607
8608 /*
8609 * Create and update the external subset.
8610 */
8611 ctxt->inSubset = 2;
8612 if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) &&
8613 (!ctxt->disableSAX))
8614 ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
8615 ctxt->extSubSystem, ctxt->extSubURI);
8616 ctxt->inSubset = 0;
8617
8618
8619 ctxt->instate = XML_PARSER_PROLOG;
8620 xmlParseMisc(ctxt);
8621 }
8622
8623 /*
8624 * Time to start parsing the tree itself
8625 */
8626 GROW;
8627 if (RAW != '<') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008628 xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY,
8629 "Start tag expected, '<' not found\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008630 } else {
8631 ctxt->instate = XML_PARSER_CONTENT;
8632 xmlParseElement(ctxt);
8633 ctxt->instate = XML_PARSER_EPILOG;
8634
8635
8636 /*
8637 * The Misc part at the end
8638 */
8639 xmlParseMisc(ctxt);
8640
Daniel Veillard561b7f82002-03-20 21:55:57 +00008641 if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008642 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008643 }
8644 ctxt->instate = XML_PARSER_EOF;
8645 }
8646
8647 /*
8648 * SAX: end of the document processing.
8649 */
Daniel Veillard8d24cc12002-03-05 15:41:29 +00008650 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00008651 ctxt->sax->endDocument(ctxt->userData);
8652
Daniel Veillard5997aca2002-03-18 18:36:20 +00008653 /*
8654 * Remove locally kept entity definitions if the tree was not built
8655 */
8656 if ((ctxt->myDoc != NULL) &&
8657 (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) {
8658 xmlFreeDoc(ctxt->myDoc);
8659 ctxt->myDoc = NULL;
8660 }
8661
Daniel Veillardc7612992002-02-17 22:47:37 +00008662 if (! ctxt->wellFormed) {
8663 ctxt->valid = 0;
8664 return(-1);
8665 }
Owen Taylor3473f882001-02-23 17:55:21 +00008666 return(0);
8667}
8668
8669/**
8670 * xmlParseExtParsedEnt:
8671 * @ctxt: an XML parser context
8672 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00008673 * parse a general parsed entity
Owen Taylor3473f882001-02-23 17:55:21 +00008674 * An external general parsed entity is well-formed if it matches the
8675 * production labeled extParsedEnt.
8676 *
8677 * [78] extParsedEnt ::= TextDecl? content
8678 *
8679 * Returns 0, -1 in case of error. the parser context is augmented
8680 * as a result of the parsing.
8681 */
8682
8683int
8684xmlParseExtParsedEnt(xmlParserCtxtPtr ctxt) {
8685 xmlChar start[4];
8686 xmlCharEncoding enc;
8687
8688 xmlDefaultSAXHandlerInit();
8689
Daniel Veillard309f81d2003-09-23 09:02:53 +00008690 xmlDetectSAX2(ctxt);
8691
Owen Taylor3473f882001-02-23 17:55:21 +00008692 GROW;
8693
8694 /*
8695 * SAX: beginning of the document processing.
8696 */
8697 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
8698 ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
8699
8700 /*
8701 * Get the 4 first bytes and decode the charset
8702 * if enc != XML_CHAR_ENCODING_NONE
8703 * plug some encoding conversion routines.
8704 */
Daniel Veillard4aede2e2003-10-17 12:43:59 +00008705 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
8706 start[0] = RAW;
8707 start[1] = NXT(1);
8708 start[2] = NXT(2);
8709 start[3] = NXT(3);
8710 enc = xmlDetectCharEncoding(start, 4);
8711 if (enc != XML_CHAR_ENCODING_NONE) {
8712 xmlSwitchEncoding(ctxt, enc);
8713 }
Owen Taylor3473f882001-02-23 17:55:21 +00008714 }
8715
8716
8717 if (CUR == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008718 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008719 }
8720
8721 /*
8722 * Check for the XMLDecl in the Prolog.
8723 */
8724 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008725 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008726
8727 /*
8728 * Note that we will switch encoding on the fly.
8729 */
8730 xmlParseXMLDecl(ctxt);
8731 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
8732 /*
8733 * The XML REC instructs us to stop parsing right here
8734 */
8735 return(-1);
8736 }
8737 SKIP_BLANKS;
8738 } else {
8739 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
8740 }
8741 if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
8742 ctxt->sax->startDocument(ctxt->userData);
8743
8744 /*
8745 * Doing validity checking on chunk doesn't make sense
8746 */
8747 ctxt->instate = XML_PARSER_CONTENT;
8748 ctxt->validate = 0;
8749 ctxt->loadsubset = 0;
8750 ctxt->depth = 0;
8751
8752 xmlParseContent(ctxt);
8753
8754 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008755 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008756 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008757 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008758 }
8759
8760 /*
8761 * SAX: end of the document processing.
8762 */
Daniel Veillard8d24cc12002-03-05 15:41:29 +00008763 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00008764 ctxt->sax->endDocument(ctxt->userData);
8765
8766 if (! ctxt->wellFormed) return(-1);
8767 return(0);
8768}
8769
Daniel Veillard73b013f2003-09-30 12:36:01 +00008770#ifdef LIBXML_PUSH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00008771/************************************************************************
8772 * *
8773 * Progressive parsing interfaces *
8774 * *
8775 ************************************************************************/
8776
8777/**
8778 * xmlParseLookupSequence:
8779 * @ctxt: an XML parser context
8780 * @first: the first char to lookup
8781 * @next: the next char to lookup or zero
8782 * @third: the next char to lookup or zero
8783 *
8784 * Try to find if a sequence (first, next, third) or just (first next) or
8785 * (first) is available in the input stream.
8786 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
8787 * to avoid rescanning sequences of bytes, it DOES change the state of the
8788 * parser, do not use liberally.
8789 *
8790 * Returns the index to the current parsing point if the full sequence
8791 * is available, -1 otherwise.
8792 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008793static int
Owen Taylor3473f882001-02-23 17:55:21 +00008794xmlParseLookupSequence(xmlParserCtxtPtr ctxt, xmlChar first,
8795 xmlChar next, xmlChar third) {
8796 int base, len;
8797 xmlParserInputPtr in;
8798 const xmlChar *buf;
8799
8800 in = ctxt->input;
8801 if (in == NULL) return(-1);
8802 base = in->cur - in->base;
8803 if (base < 0) return(-1);
8804 if (ctxt->checkIndex > base)
8805 base = ctxt->checkIndex;
8806 if (in->buf == NULL) {
8807 buf = in->base;
8808 len = in->length;
8809 } else {
8810 buf = in->buf->buffer->content;
8811 len = in->buf->buffer->use;
8812 }
8813 /* take into account the sequence length */
8814 if (third) len -= 2;
8815 else if (next) len --;
8816 for (;base < len;base++) {
8817 if (buf[base] == first) {
8818 if (third != 0) {
8819 if ((buf[base + 1] != next) ||
8820 (buf[base + 2] != third)) continue;
8821 } else if (next != 0) {
8822 if (buf[base + 1] != next) continue;
8823 }
8824 ctxt->checkIndex = 0;
8825#ifdef DEBUG_PUSH
8826 if (next == 0)
8827 xmlGenericError(xmlGenericErrorContext,
8828 "PP: lookup '%c' found at %d\n",
8829 first, base);
8830 else if (third == 0)
8831 xmlGenericError(xmlGenericErrorContext,
8832 "PP: lookup '%c%c' found at %d\n",
8833 first, next, base);
8834 else
8835 xmlGenericError(xmlGenericErrorContext,
8836 "PP: lookup '%c%c%c' found at %d\n",
8837 first, next, third, base);
8838#endif
8839 return(base - (in->cur - in->base));
8840 }
8841 }
8842 ctxt->checkIndex = base;
8843#ifdef DEBUG_PUSH
8844 if (next == 0)
8845 xmlGenericError(xmlGenericErrorContext,
8846 "PP: lookup '%c' failed\n", first);
8847 else if (third == 0)
8848 xmlGenericError(xmlGenericErrorContext,
8849 "PP: lookup '%c%c' failed\n", first, next);
8850 else
8851 xmlGenericError(xmlGenericErrorContext,
8852 "PP: lookup '%c%c%c' failed\n", first, next, third);
8853#endif
8854 return(-1);
8855}
8856
8857/**
Daniel Veillarda880b122003-04-21 21:36:41 +00008858 * xmlParseGetLasts:
8859 * @ctxt: an XML parser context
8860 * @lastlt: pointer to store the last '<' from the input
8861 * @lastgt: pointer to store the last '>' from the input
8862 *
8863 * Lookup the last < and > in the current chunk
8864 */
8865static void
8866xmlParseGetLasts(xmlParserCtxtPtr ctxt, const xmlChar **lastlt,
8867 const xmlChar **lastgt) {
8868 const xmlChar *tmp;
8869
8870 if ((ctxt == NULL) || (lastlt == NULL) || (lastgt == NULL)) {
8871 xmlGenericError(xmlGenericErrorContext,
8872 "Internal error: xmlParseGetLasts\n");
8873 return;
8874 }
Daniel Veillard0df3bc32004-06-08 12:03:41 +00008875 if ((ctxt->progressive != 0) && (ctxt->inputNr == 1)) {
Daniel Veillarda880b122003-04-21 21:36:41 +00008876 tmp = ctxt->input->end;
8877 tmp--;
Daniel Veillardeb70f932004-07-05 16:46:09 +00008878 while ((tmp >= ctxt->input->base) && (*tmp != '<')) tmp--;
Daniel Veillarda880b122003-04-21 21:36:41 +00008879 if (tmp < ctxt->input->base) {
8880 *lastlt = NULL;
8881 *lastgt = NULL;
Daniel Veillarda880b122003-04-21 21:36:41 +00008882 } else {
Daniel Veillardeb70f932004-07-05 16:46:09 +00008883 *lastlt = tmp;
8884 tmp++;
8885 while ((tmp < ctxt->input->end) && (*tmp != '>')) {
8886 if (*tmp == '\'') {
8887 tmp++;
8888 while ((tmp < ctxt->input->end) && (*tmp != '\'')) tmp++;
8889 if (tmp < ctxt->input->end) tmp++;
8890 } else if (*tmp == '"') {
8891 tmp++;
8892 while ((tmp < ctxt->input->end) && (*tmp != '"')) tmp++;
8893 if (tmp < ctxt->input->end) tmp++;
8894 } else
8895 tmp++;
8896 }
8897 if (tmp < ctxt->input->end)
8898 *lastgt = tmp;
8899 else {
8900 tmp = *lastlt;
8901 tmp--;
8902 while ((tmp >= ctxt->input->base) && (*tmp != '>')) tmp--;
8903 if (tmp >= ctxt->input->base)
8904 *lastgt = tmp;
8905 else
8906 *lastgt = NULL;
8907 }
Daniel Veillarda880b122003-04-21 21:36:41 +00008908 }
Daniel Veillarda880b122003-04-21 21:36:41 +00008909 } else {
8910 *lastlt = NULL;
8911 *lastgt = NULL;
8912 }
8913}
8914/**
Owen Taylor3473f882001-02-23 17:55:21 +00008915 * xmlParseTryOrFinish:
8916 * @ctxt: an XML parser context
8917 * @terminate: last chunk indicator
8918 *
8919 * Try to progress on parsing
8920 *
8921 * Returns zero if no parsing was possible
8922 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008923static int
Owen Taylor3473f882001-02-23 17:55:21 +00008924xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) {
8925 int ret = 0;
Daniel Veillardc82c57e2004-01-12 16:24:34 +00008926 int avail, tlen;
Owen Taylor3473f882001-02-23 17:55:21 +00008927 xmlChar cur, next;
Daniel Veillarda880b122003-04-21 21:36:41 +00008928 const xmlChar *lastlt, *lastgt;
Owen Taylor3473f882001-02-23 17:55:21 +00008929
8930#ifdef DEBUG_PUSH
8931 switch (ctxt->instate) {
8932 case XML_PARSER_EOF:
8933 xmlGenericError(xmlGenericErrorContext,
8934 "PP: try EOF\n"); break;
8935 case XML_PARSER_START:
8936 xmlGenericError(xmlGenericErrorContext,
8937 "PP: try START\n"); break;
8938 case XML_PARSER_MISC:
8939 xmlGenericError(xmlGenericErrorContext,
8940 "PP: try MISC\n");break;
8941 case XML_PARSER_COMMENT:
8942 xmlGenericError(xmlGenericErrorContext,
8943 "PP: try COMMENT\n");break;
8944 case XML_PARSER_PROLOG:
8945 xmlGenericError(xmlGenericErrorContext,
8946 "PP: try PROLOG\n");break;
8947 case XML_PARSER_START_TAG:
8948 xmlGenericError(xmlGenericErrorContext,
8949 "PP: try START_TAG\n");break;
8950 case XML_PARSER_CONTENT:
8951 xmlGenericError(xmlGenericErrorContext,
8952 "PP: try CONTENT\n");break;
8953 case XML_PARSER_CDATA_SECTION:
8954 xmlGenericError(xmlGenericErrorContext,
8955 "PP: try CDATA_SECTION\n");break;
8956 case XML_PARSER_END_TAG:
8957 xmlGenericError(xmlGenericErrorContext,
8958 "PP: try END_TAG\n");break;
8959 case XML_PARSER_ENTITY_DECL:
8960 xmlGenericError(xmlGenericErrorContext,
8961 "PP: try ENTITY_DECL\n");break;
8962 case XML_PARSER_ENTITY_VALUE:
8963 xmlGenericError(xmlGenericErrorContext,
8964 "PP: try ENTITY_VALUE\n");break;
8965 case XML_PARSER_ATTRIBUTE_VALUE:
8966 xmlGenericError(xmlGenericErrorContext,
8967 "PP: try ATTRIBUTE_VALUE\n");break;
8968 case XML_PARSER_DTD:
8969 xmlGenericError(xmlGenericErrorContext,
8970 "PP: try DTD\n");break;
8971 case XML_PARSER_EPILOG:
8972 xmlGenericError(xmlGenericErrorContext,
8973 "PP: try EPILOG\n");break;
8974 case XML_PARSER_PI:
8975 xmlGenericError(xmlGenericErrorContext,
8976 "PP: try PI\n");break;
8977 case XML_PARSER_IGNORE:
8978 xmlGenericError(xmlGenericErrorContext,
8979 "PP: try IGNORE\n");break;
8980 }
8981#endif
8982
Daniel Veillard198c1bf2003-10-20 17:07:41 +00008983 if ((ctxt->input != NULL) &&
8984 (ctxt->input->cur - ctxt->input->base > 4096)) {
Daniel Veillarda880b122003-04-21 21:36:41 +00008985 xmlSHRINK(ctxt);
8986 ctxt->checkIndex = 0;
8987 }
8988 xmlParseGetLasts(ctxt, &lastlt, &lastgt);
Aleksey Sanine48a3182002-05-09 18:20:01 +00008989
Daniel Veillarda880b122003-04-21 21:36:41 +00008990 while (1) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00008991 if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
8992 return(0);
8993
8994
Owen Taylor3473f882001-02-23 17:55:21 +00008995 /*
8996 * Pop-up of finished entities.
8997 */
8998 while ((RAW == 0) && (ctxt->inputNr > 1))
8999 xmlPopInput(ctxt);
9000
Daniel Veillard198c1bf2003-10-20 17:07:41 +00009001 if (ctxt->input == NULL) break;
Owen Taylor3473f882001-02-23 17:55:21 +00009002 if (ctxt->input->buf == NULL)
Daniel Veillarda880b122003-04-21 21:36:41 +00009003 avail = ctxt->input->length -
9004 (ctxt->input->cur - ctxt->input->base);
Daniel Veillard158a4d22002-02-20 22:17:58 +00009005 else {
9006 /*
9007 * If we are operating on converted input, try to flush
9008 * remainng chars to avoid them stalling in the non-converted
9009 * buffer.
9010 */
9011 if ((ctxt->input->buf->raw != NULL) &&
9012 (ctxt->input->buf->raw->use > 0)) {
9013 int base = ctxt->input->base -
9014 ctxt->input->buf->buffer->content;
9015 int current = ctxt->input->cur - ctxt->input->base;
9016
9017 xmlParserInputBufferPush(ctxt->input->buf, 0, "");
9018 ctxt->input->base = ctxt->input->buf->buffer->content + base;
9019 ctxt->input->cur = ctxt->input->base + current;
9020 ctxt->input->end =
9021 &ctxt->input->buf->buffer->content[
9022 ctxt->input->buf->buffer->use];
9023 }
9024 avail = ctxt->input->buf->buffer->use -
9025 (ctxt->input->cur - ctxt->input->base);
9026 }
Owen Taylor3473f882001-02-23 17:55:21 +00009027 if (avail < 1)
9028 goto done;
9029 switch (ctxt->instate) {
9030 case XML_PARSER_EOF:
9031 /*
9032 * Document parsing is done !
9033 */
9034 goto done;
9035 case XML_PARSER_START:
Daniel Veillard0e4cd172001-06-28 12:13:56 +00009036 if (ctxt->charset == XML_CHAR_ENCODING_NONE) {
9037 xmlChar start[4];
9038 xmlCharEncoding enc;
9039
9040 /*
9041 * Very first chars read from the document flow.
9042 */
9043 if (avail < 4)
9044 goto done;
9045
9046 /*
9047 * Get the 4 first bytes and decode the charset
9048 * if enc != XML_CHAR_ENCODING_NONE
9049 * plug some encoding conversion routines.
9050 */
9051 start[0] = RAW;
9052 start[1] = NXT(1);
9053 start[2] = NXT(2);
9054 start[3] = NXT(3);
9055 enc = xmlDetectCharEncoding(start, 4);
9056 if (enc != XML_CHAR_ENCODING_NONE) {
9057 xmlSwitchEncoding(ctxt, enc);
9058 }
9059 break;
9060 }
Owen Taylor3473f882001-02-23 17:55:21 +00009061
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00009062 if (avail < 2)
9063 goto done;
Owen Taylor3473f882001-02-23 17:55:21 +00009064 cur = ctxt->input->cur[0];
9065 next = ctxt->input->cur[1];
9066 if (cur == 0) {
9067 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
9068 ctxt->sax->setDocumentLocator(ctxt->userData,
9069 &xmlDefaultSAXLocator);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009070 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00009071 ctxt->instate = XML_PARSER_EOF;
9072#ifdef DEBUG_PUSH
9073 xmlGenericError(xmlGenericErrorContext,
9074 "PP: entering EOF\n");
9075#endif
9076 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
9077 ctxt->sax->endDocument(ctxt->userData);
9078 goto done;
9079 }
9080 if ((cur == '<') && (next == '?')) {
9081 /* PI or XML decl */
9082 if (avail < 5) return(ret);
9083 if ((!terminate) &&
9084 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9085 return(ret);
9086 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
9087 ctxt->sax->setDocumentLocator(ctxt->userData,
9088 &xmlDefaultSAXLocator);
9089 if ((ctxt->input->cur[2] == 'x') &&
9090 (ctxt->input->cur[3] == 'm') &&
9091 (ctxt->input->cur[4] == 'l') &&
William M. Brack76e95df2003-10-18 16:20:14 +00009092 (IS_BLANK_CH(ctxt->input->cur[5]))) {
Owen Taylor3473f882001-02-23 17:55:21 +00009093 ret += 5;
9094#ifdef DEBUG_PUSH
9095 xmlGenericError(xmlGenericErrorContext,
9096 "PP: Parsing XML Decl\n");
9097#endif
9098 xmlParseXMLDecl(ctxt);
9099 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
9100 /*
9101 * The XML REC instructs us to stop parsing right
9102 * here
9103 */
9104 ctxt->instate = XML_PARSER_EOF;
9105 return(0);
9106 }
9107 ctxt->standalone = ctxt->input->standalone;
9108 if ((ctxt->encoding == NULL) &&
9109 (ctxt->input->encoding != NULL))
9110 ctxt->encoding = xmlStrdup(ctxt->input->encoding);
9111 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
9112 (!ctxt->disableSAX))
9113 ctxt->sax->startDocument(ctxt->userData);
9114 ctxt->instate = XML_PARSER_MISC;
9115#ifdef DEBUG_PUSH
9116 xmlGenericError(xmlGenericErrorContext,
9117 "PP: entering MISC\n");
9118#endif
9119 } else {
9120 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
9121 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
9122 (!ctxt->disableSAX))
9123 ctxt->sax->startDocument(ctxt->userData);
9124 ctxt->instate = XML_PARSER_MISC;
9125#ifdef DEBUG_PUSH
9126 xmlGenericError(xmlGenericErrorContext,
9127 "PP: entering MISC\n");
9128#endif
9129 }
9130 } else {
9131 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
9132 ctxt->sax->setDocumentLocator(ctxt->userData,
9133 &xmlDefaultSAXLocator);
9134 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
William M. Bracka3215c72004-07-31 16:24:01 +00009135 if (ctxt->version == NULL) {
9136 xmlErrMemory(ctxt, NULL);
9137 break;
9138 }
Owen Taylor3473f882001-02-23 17:55:21 +00009139 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
9140 (!ctxt->disableSAX))
9141 ctxt->sax->startDocument(ctxt->userData);
9142 ctxt->instate = XML_PARSER_MISC;
9143#ifdef DEBUG_PUSH
9144 xmlGenericError(xmlGenericErrorContext,
9145 "PP: entering MISC\n");
9146#endif
9147 }
9148 break;
Daniel Veillarda880b122003-04-21 21:36:41 +00009149 case XML_PARSER_START_TAG: {
Daniel Veillarde57ec792003-09-10 10:50:59 +00009150 const xmlChar *name;
9151 const xmlChar *prefix;
9152 const xmlChar *URI;
9153 int nsNr = ctxt->nsNr;
Daniel Veillarda880b122003-04-21 21:36:41 +00009154
9155 if ((avail < 2) && (ctxt->inputNr == 1))
9156 goto done;
9157 cur = ctxt->input->cur[0];
9158 if (cur != '<') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009159 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Daniel Veillarda880b122003-04-21 21:36:41 +00009160 ctxt->instate = XML_PARSER_EOF;
Daniel Veillarda880b122003-04-21 21:36:41 +00009161 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
9162 ctxt->sax->endDocument(ctxt->userData);
9163 goto done;
9164 }
9165 if (!terminate) {
9166 if (ctxt->progressive) {
Daniel Veillardb3744002004-02-18 14:28:22 +00009167 /* > can be found unescaped in attribute values */
Daniel Veillardeb70f932004-07-05 16:46:09 +00009168 if ((lastgt == NULL) || (ctxt->input->cur >= lastgt))
Daniel Veillarda880b122003-04-21 21:36:41 +00009169 goto done;
9170 } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) {
9171 goto done;
9172 }
9173 }
9174 if (ctxt->spaceNr == 0)
9175 spacePush(ctxt, -1);
9176 else
9177 spacePush(ctxt, *ctxt->space);
Daniel Veillard81273902003-09-30 00:43:48 +00009178#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009179 if (ctxt->sax2)
Daniel Veillard81273902003-09-30 00:43:48 +00009180#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00009181 name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen);
Daniel Veillard81273902003-09-30 00:43:48 +00009182#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009183 else
9184 name = xmlParseStartTag(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00009185#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarda880b122003-04-21 21:36:41 +00009186 if (name == NULL) {
9187 spacePop(ctxt);
9188 ctxt->instate = XML_PARSER_EOF;
Daniel Veillarda880b122003-04-21 21:36:41 +00009189 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
9190 ctxt->sax->endDocument(ctxt->userData);
9191 goto done;
9192 }
Daniel Veillard4432df22003-09-28 18:58:27 +00009193#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda880b122003-04-21 21:36:41 +00009194 /*
9195 * [ VC: Root Element Type ]
9196 * The Name in the document type declaration must match
9197 * the element type of the root element.
9198 */
9199 if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
9200 ctxt->node && (ctxt->node == ctxt->myDoc->children))
9201 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillard4432df22003-09-28 18:58:27 +00009202#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda880b122003-04-21 21:36:41 +00009203
9204 /*
9205 * Check for an Empty Element.
9206 */
9207 if ((RAW == '/') && (NXT(1) == '>')) {
9208 SKIP(2);
Daniel Veillarde57ec792003-09-10 10:50:59 +00009209
9210 if (ctxt->sax2) {
9211 if ((ctxt->sax != NULL) &&
9212 (ctxt->sax->endElementNs != NULL) &&
9213 (!ctxt->disableSAX))
9214 ctxt->sax->endElementNs(ctxt->userData, name,
9215 prefix, URI);
Daniel Veillard81273902003-09-30 00:43:48 +00009216#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009217 } else {
9218 if ((ctxt->sax != NULL) &&
9219 (ctxt->sax->endElement != NULL) &&
9220 (!ctxt->disableSAX))
9221 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard81273902003-09-30 00:43:48 +00009222#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarda880b122003-04-21 21:36:41 +00009223 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009224 spacePop(ctxt);
9225 if (ctxt->nameNr == 0) {
Daniel Veillarda880b122003-04-21 21:36:41 +00009226 ctxt->instate = XML_PARSER_EPILOG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009227 } else {
9228 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillarda880b122003-04-21 21:36:41 +00009229 }
9230 break;
9231 }
9232 if (RAW == '>') {
9233 NEXT;
9234 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00009235 xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED,
Daniel Veillarda880b122003-04-21 21:36:41 +00009236 "Couldn't find end of Start Tag %s\n",
9237 name);
Daniel Veillarda880b122003-04-21 21:36:41 +00009238 nodePop(ctxt);
Daniel Veillarda880b122003-04-21 21:36:41 +00009239 spacePop(ctxt);
Daniel Veillarda880b122003-04-21 21:36:41 +00009240 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009241 if (ctxt->sax2)
9242 nameNsPush(ctxt, name, prefix, URI, ctxt->nsNr - nsNr);
Daniel Veillard81273902003-09-30 00:43:48 +00009243#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009244 else
9245 namePush(ctxt, name);
Daniel Veillard81273902003-09-30 00:43:48 +00009246#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00009247
Daniel Veillarda880b122003-04-21 21:36:41 +00009248 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillarda880b122003-04-21 21:36:41 +00009249 break;
9250 }
9251 case XML_PARSER_CONTENT: {
9252 const xmlChar *test;
9253 unsigned int cons;
9254 if ((avail < 2) && (ctxt->inputNr == 1))
9255 goto done;
9256 cur = ctxt->input->cur[0];
9257 next = ctxt->input->cur[1];
9258
9259 test = CUR_PTR;
9260 cons = ctxt->input->consumed;
9261 if ((cur == '<') && (next == '/')) {
9262 ctxt->instate = XML_PARSER_END_TAG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009263 break;
9264 } else if ((cur == '<') && (next == '?')) {
9265 if ((!terminate) &&
9266 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9267 goto done;
Daniel Veillarda880b122003-04-21 21:36:41 +00009268 xmlParsePI(ctxt);
9269 } else if ((cur == '<') && (next != '!')) {
9270 ctxt->instate = XML_PARSER_START_TAG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009271 break;
9272 } else if ((cur == '<') && (next == '!') &&
9273 (ctxt->input->cur[2] == '-') &&
9274 (ctxt->input->cur[3] == '-')) {
9275 if ((!terminate) &&
9276 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9277 goto done;
Daniel Veillarda880b122003-04-21 21:36:41 +00009278 xmlParseComment(ctxt);
9279 ctxt->instate = XML_PARSER_CONTENT;
9280 } else if ((cur == '<') && (ctxt->input->cur[1] == '!') &&
9281 (ctxt->input->cur[2] == '[') &&
9282 (ctxt->input->cur[3] == 'C') &&
9283 (ctxt->input->cur[4] == 'D') &&
9284 (ctxt->input->cur[5] == 'A') &&
9285 (ctxt->input->cur[6] == 'T') &&
9286 (ctxt->input->cur[7] == 'A') &&
9287 (ctxt->input->cur[8] == '[')) {
9288 SKIP(9);
9289 ctxt->instate = XML_PARSER_CDATA_SECTION;
Daniel Veillarda880b122003-04-21 21:36:41 +00009290 break;
9291 } else if ((cur == '<') && (next == '!') &&
9292 (avail < 9)) {
9293 goto done;
9294 } else if (cur == '&') {
9295 if ((!terminate) &&
9296 (xmlParseLookupSequence(ctxt, ';', 0, 0) < 0))
9297 goto done;
Daniel Veillarda880b122003-04-21 21:36:41 +00009298 xmlParseReference(ctxt);
9299 } else {
9300 /* TODO Avoid the extra copy, handle directly !!! */
9301 /*
9302 * Goal of the following test is:
9303 * - minimize calls to the SAX 'character' callback
9304 * when they are mergeable
9305 * - handle an problem for isBlank when we only parse
9306 * a sequence of blank chars and the next one is
9307 * not available to check against '<' presence.
9308 * - tries to homogenize the differences in SAX
9309 * callbacks between the push and pull versions
9310 * of the parser.
9311 */
9312 if ((ctxt->inputNr == 1) &&
9313 (avail < XML_PARSER_BIG_BUFFER_SIZE)) {
9314 if (!terminate) {
9315 if (ctxt->progressive) {
9316 if ((lastlt == NULL) ||
9317 (ctxt->input->cur > lastlt))
9318 goto done;
9319 } else if (xmlParseLookupSequence(ctxt,
9320 '<', 0, 0) < 0) {
9321 goto done;
9322 }
9323 }
9324 }
9325 ctxt->checkIndex = 0;
Daniel Veillarda880b122003-04-21 21:36:41 +00009326 xmlParseCharData(ctxt, 0);
9327 }
9328 /*
9329 * Pop-up of finished entities.
9330 */
9331 while ((RAW == 0) && (ctxt->inputNr > 1))
9332 xmlPopInput(ctxt);
9333 if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009334 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
9335 "detected an error in element content\n");
Daniel Veillarda880b122003-04-21 21:36:41 +00009336 ctxt->instate = XML_PARSER_EOF;
9337 break;
9338 }
9339 break;
9340 }
9341 case XML_PARSER_END_TAG:
9342 if (avail < 2)
9343 goto done;
9344 if (!terminate) {
9345 if (ctxt->progressive) {
Daniel Veillardeb70f932004-07-05 16:46:09 +00009346 /* > can be found unescaped in attribute values */
9347 if ((lastgt == NULL) || (ctxt->input->cur >= lastgt))
Daniel Veillarda880b122003-04-21 21:36:41 +00009348 goto done;
9349 } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) {
9350 goto done;
9351 }
9352 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009353 if (ctxt->sax2) {
9354 xmlParseEndTag2(ctxt,
9355 (void *) ctxt->pushTab[ctxt->nameNr * 3 - 3],
9356 (void *) ctxt->pushTab[ctxt->nameNr * 3 - 2], 0,
Daniel Veillardc82c57e2004-01-12 16:24:34 +00009357 (int) (long) ctxt->pushTab[ctxt->nameNr * 3 - 1], 0);
Daniel Veillarde57ec792003-09-10 10:50:59 +00009358 nameNsPop(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00009359 }
9360#ifdef LIBXML_SAX1_ENABLED
9361 else
Daniel Veillarde57ec792003-09-10 10:50:59 +00009362 xmlParseEndTag1(ctxt, 0);
Daniel Veillard81273902003-09-30 00:43:48 +00009363#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00009364 if (ctxt->nameNr == 0) {
Daniel Veillarda880b122003-04-21 21:36:41 +00009365 ctxt->instate = XML_PARSER_EPILOG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009366 } else {
9367 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillarda880b122003-04-21 21:36:41 +00009368 }
9369 break;
9370 case XML_PARSER_CDATA_SECTION: {
9371 /*
9372 * The Push mode need to have the SAX callback for
9373 * cdataBlock merge back contiguous callbacks.
9374 */
9375 int base;
9376
9377 base = xmlParseLookupSequence(ctxt, ']', ']', '>');
9378 if (base < 0) {
9379 if (avail >= XML_PARSER_BIG_BUFFER_SIZE + 2) {
9380 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
9381 if (ctxt->sax->cdataBlock != NULL)
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00009382 ctxt->sax->cdataBlock(ctxt->userData,
9383 ctxt->input->cur,
9384 XML_PARSER_BIG_BUFFER_SIZE);
9385 else if (ctxt->sax->characters != NULL)
9386 ctxt->sax->characters(ctxt->userData,
9387 ctxt->input->cur,
Daniel Veillarda880b122003-04-21 21:36:41 +00009388 XML_PARSER_BIG_BUFFER_SIZE);
9389 }
Daniel Veillard0b787f32004-03-26 17:29:53 +00009390 SKIPL(XML_PARSER_BIG_BUFFER_SIZE);
Daniel Veillarda880b122003-04-21 21:36:41 +00009391 ctxt->checkIndex = 0;
9392 }
9393 goto done;
9394 } else {
9395 if ((ctxt->sax != NULL) && (base > 0) &&
9396 (!ctxt->disableSAX)) {
9397 if (ctxt->sax->cdataBlock != NULL)
9398 ctxt->sax->cdataBlock(ctxt->userData,
9399 ctxt->input->cur, base);
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00009400 else if (ctxt->sax->characters != NULL)
9401 ctxt->sax->characters(ctxt->userData,
9402 ctxt->input->cur, base);
Daniel Veillarda880b122003-04-21 21:36:41 +00009403 }
Daniel Veillard0b787f32004-03-26 17:29:53 +00009404 SKIPL(base + 3);
Daniel Veillarda880b122003-04-21 21:36:41 +00009405 ctxt->checkIndex = 0;
9406 ctxt->instate = XML_PARSER_CONTENT;
9407#ifdef DEBUG_PUSH
9408 xmlGenericError(xmlGenericErrorContext,
9409 "PP: entering CONTENT\n");
9410#endif
9411 }
9412 break;
9413 }
Owen Taylor3473f882001-02-23 17:55:21 +00009414 case XML_PARSER_MISC:
9415 SKIP_BLANKS;
9416 if (ctxt->input->buf == NULL)
Daniel Veillarda880b122003-04-21 21:36:41 +00009417 avail = ctxt->input->length -
9418 (ctxt->input->cur - ctxt->input->base);
Owen Taylor3473f882001-02-23 17:55:21 +00009419 else
Daniel Veillarda880b122003-04-21 21:36:41 +00009420 avail = ctxt->input->buf->buffer->use -
9421 (ctxt->input->cur - ctxt->input->base);
Owen Taylor3473f882001-02-23 17:55:21 +00009422 if (avail < 2)
9423 goto done;
9424 cur = ctxt->input->cur[0];
9425 next = ctxt->input->cur[1];
9426 if ((cur == '<') && (next == '?')) {
9427 if ((!terminate) &&
9428 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9429 goto done;
9430#ifdef DEBUG_PUSH
9431 xmlGenericError(xmlGenericErrorContext,
9432 "PP: Parsing PI\n");
9433#endif
9434 xmlParsePI(ctxt);
9435 } else if ((cur == '<') && (next == '!') &&
Daniel Veillarda880b122003-04-21 21:36:41 +00009436 (ctxt->input->cur[2] == '-') &&
9437 (ctxt->input->cur[3] == '-')) {
Owen Taylor3473f882001-02-23 17:55:21 +00009438 if ((!terminate) &&
9439 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9440 goto done;
9441#ifdef DEBUG_PUSH
9442 xmlGenericError(xmlGenericErrorContext,
9443 "PP: Parsing Comment\n");
9444#endif
9445 xmlParseComment(ctxt);
9446 ctxt->instate = XML_PARSER_MISC;
9447 } else if ((cur == '<') && (next == '!') &&
Daniel Veillarda880b122003-04-21 21:36:41 +00009448 (ctxt->input->cur[2] == 'D') &&
9449 (ctxt->input->cur[3] == 'O') &&
9450 (ctxt->input->cur[4] == 'C') &&
9451 (ctxt->input->cur[5] == 'T') &&
9452 (ctxt->input->cur[6] == 'Y') &&
9453 (ctxt->input->cur[7] == 'P') &&
Owen Taylor3473f882001-02-23 17:55:21 +00009454 (ctxt->input->cur[8] == 'E')) {
9455 if ((!terminate) &&
9456 (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
9457 goto done;
9458#ifdef DEBUG_PUSH
9459 xmlGenericError(xmlGenericErrorContext,
9460 "PP: Parsing internal subset\n");
9461#endif
9462 ctxt->inSubset = 1;
9463 xmlParseDocTypeDecl(ctxt);
9464 if (RAW == '[') {
9465 ctxt->instate = XML_PARSER_DTD;
9466#ifdef DEBUG_PUSH
9467 xmlGenericError(xmlGenericErrorContext,
9468 "PP: entering DTD\n");
9469#endif
9470 } else {
9471 /*
9472 * Create and update the external subset.
9473 */
9474 ctxt->inSubset = 2;
9475 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
9476 (ctxt->sax->externalSubset != NULL))
9477 ctxt->sax->externalSubset(ctxt->userData,
9478 ctxt->intSubName, ctxt->extSubSystem,
9479 ctxt->extSubURI);
9480 ctxt->inSubset = 0;
9481 ctxt->instate = XML_PARSER_PROLOG;
9482#ifdef DEBUG_PUSH
9483 xmlGenericError(xmlGenericErrorContext,
9484 "PP: entering PROLOG\n");
9485#endif
9486 }
9487 } else if ((cur == '<') && (next == '!') &&
9488 (avail < 9)) {
9489 goto done;
9490 } else {
9491 ctxt->instate = XML_PARSER_START_TAG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009492 ctxt->progressive = 1;
9493 xmlParseGetLasts(ctxt, &lastlt, &lastgt);
Owen Taylor3473f882001-02-23 17:55:21 +00009494#ifdef DEBUG_PUSH
9495 xmlGenericError(xmlGenericErrorContext,
9496 "PP: entering START_TAG\n");
9497#endif
9498 }
9499 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009500 case XML_PARSER_PROLOG:
9501 SKIP_BLANKS;
9502 if (ctxt->input->buf == NULL)
9503 avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base);
9504 else
9505 avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base);
9506 if (avail < 2)
9507 goto done;
9508 cur = ctxt->input->cur[0];
9509 next = ctxt->input->cur[1];
9510 if ((cur == '<') && (next == '?')) {
9511 if ((!terminate) &&
9512 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9513 goto done;
9514#ifdef DEBUG_PUSH
9515 xmlGenericError(xmlGenericErrorContext,
9516 "PP: Parsing PI\n");
9517#endif
9518 xmlParsePI(ctxt);
9519 } else if ((cur == '<') && (next == '!') &&
9520 (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) {
9521 if ((!terminate) &&
9522 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9523 goto done;
9524#ifdef DEBUG_PUSH
9525 xmlGenericError(xmlGenericErrorContext,
9526 "PP: Parsing Comment\n");
9527#endif
9528 xmlParseComment(ctxt);
9529 ctxt->instate = XML_PARSER_PROLOG;
9530 } else if ((cur == '<') && (next == '!') &&
9531 (avail < 4)) {
9532 goto done;
9533 } else {
9534 ctxt->instate = XML_PARSER_START_TAG;
Daniel Veillard0df3bc32004-06-08 12:03:41 +00009535 if (ctxt->progressive == 0)
9536 ctxt->progressive = 1;
Daniel Veillarda880b122003-04-21 21:36:41 +00009537 xmlParseGetLasts(ctxt, &lastlt, &lastgt);
Owen Taylor3473f882001-02-23 17:55:21 +00009538#ifdef DEBUG_PUSH
9539 xmlGenericError(xmlGenericErrorContext,
9540 "PP: entering START_TAG\n");
9541#endif
9542 }
9543 break;
9544 case XML_PARSER_EPILOG:
9545 SKIP_BLANKS;
9546 if (ctxt->input->buf == NULL)
9547 avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base);
9548 else
9549 avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base);
9550 if (avail < 2)
9551 goto done;
9552 cur = ctxt->input->cur[0];
9553 next = ctxt->input->cur[1];
9554 if ((cur == '<') && (next == '?')) {
9555 if ((!terminate) &&
9556 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9557 goto done;
9558#ifdef DEBUG_PUSH
9559 xmlGenericError(xmlGenericErrorContext,
9560 "PP: Parsing PI\n");
9561#endif
9562 xmlParsePI(ctxt);
9563 ctxt->instate = XML_PARSER_EPILOG;
9564 } else if ((cur == '<') && (next == '!') &&
9565 (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) {
9566 if ((!terminate) &&
9567 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9568 goto done;
9569#ifdef DEBUG_PUSH
9570 xmlGenericError(xmlGenericErrorContext,
9571 "PP: Parsing Comment\n");
9572#endif
9573 xmlParseComment(ctxt);
9574 ctxt->instate = XML_PARSER_EPILOG;
9575 } else if ((cur == '<') && (next == '!') &&
9576 (avail < 4)) {
9577 goto done;
9578 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009579 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00009580 ctxt->instate = XML_PARSER_EOF;
9581#ifdef DEBUG_PUSH
9582 xmlGenericError(xmlGenericErrorContext,
9583 "PP: entering EOF\n");
9584#endif
Daniel Veillard8d24cc12002-03-05 15:41:29 +00009585 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00009586 ctxt->sax->endDocument(ctxt->userData);
9587 goto done;
9588 }
9589 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009590 case XML_PARSER_DTD: {
9591 /*
9592 * Sorry but progressive parsing of the internal subset
9593 * is not expected to be supported. We first check that
9594 * the full content of the internal subset is available and
9595 * the parsing is launched only at that point.
9596 * Internal subset ends up with "']' S? '>'" in an unescaped
9597 * section and not in a ']]>' sequence which are conditional
9598 * sections (whoever argued to keep that crap in XML deserve
9599 * a place in hell !).
9600 */
9601 int base, i;
9602 xmlChar *buf;
9603 xmlChar quote = 0;
9604
9605 base = ctxt->input->cur - ctxt->input->base;
9606 if (base < 0) return(0);
9607 if (ctxt->checkIndex > base)
9608 base = ctxt->checkIndex;
9609 buf = ctxt->input->buf->buffer->content;
9610 for (;(unsigned int) base < ctxt->input->buf->buffer->use;
9611 base++) {
9612 if (quote != 0) {
9613 if (buf[base] == quote)
9614 quote = 0;
9615 continue;
9616 }
Daniel Veillard036143b2004-02-12 11:57:52 +00009617 if ((quote == 0) && (buf[base] == '<')) {
9618 int found = 0;
9619 /* special handling of comments */
9620 if (((unsigned int) base + 4 <
9621 ctxt->input->buf->buffer->use) &&
9622 (buf[base + 1] == '!') &&
9623 (buf[base + 2] == '-') &&
9624 (buf[base + 3] == '-')) {
9625 for (;(unsigned int) base + 3 <
9626 ctxt->input->buf->buffer->use; base++) {
9627 if ((buf[base] == '-') &&
9628 (buf[base + 1] == '-') &&
9629 (buf[base + 2] == '>')) {
9630 found = 1;
9631 base += 2;
9632 break;
9633 }
9634 }
9635 if (!found)
9636 break;
9637 continue;
9638 }
9639 }
Owen Taylor3473f882001-02-23 17:55:21 +00009640 if (buf[base] == '"') {
9641 quote = '"';
9642 continue;
9643 }
9644 if (buf[base] == '\'') {
9645 quote = '\'';
9646 continue;
9647 }
9648 if (buf[base] == ']') {
9649 if ((unsigned int) base +1 >=
9650 ctxt->input->buf->buffer->use)
9651 break;
9652 if (buf[base + 1] == ']') {
9653 /* conditional crap, skip both ']' ! */
9654 base++;
9655 continue;
9656 }
9657 for (i = 0;
9658 (unsigned int) base + i < ctxt->input->buf->buffer->use;
9659 i++) {
9660 if (buf[base + i] == '>')
9661 goto found_end_int_subset;
9662 }
9663 break;
9664 }
9665 }
9666 /*
9667 * We didn't found the end of the Internal subset
9668 */
9669 if (quote == 0)
9670 ctxt->checkIndex = base;
9671#ifdef DEBUG_PUSH
9672 if (next == 0)
9673 xmlGenericError(xmlGenericErrorContext,
9674 "PP: lookup of int subset end filed\n");
9675#endif
9676 goto done;
9677
9678found_end_int_subset:
9679 xmlParseInternalSubset(ctxt);
9680 ctxt->inSubset = 2;
9681 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
9682 (ctxt->sax->externalSubset != NULL))
9683 ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
9684 ctxt->extSubSystem, ctxt->extSubURI);
9685 ctxt->inSubset = 0;
9686 ctxt->instate = XML_PARSER_PROLOG;
9687 ctxt->checkIndex = 0;
9688#ifdef DEBUG_PUSH
9689 xmlGenericError(xmlGenericErrorContext,
9690 "PP: entering PROLOG\n");
9691#endif
9692 break;
9693 }
9694 case XML_PARSER_COMMENT:
9695 xmlGenericError(xmlGenericErrorContext,
9696 "PP: internal error, state == COMMENT\n");
9697 ctxt->instate = XML_PARSER_CONTENT;
9698#ifdef DEBUG_PUSH
9699 xmlGenericError(xmlGenericErrorContext,
9700 "PP: entering CONTENT\n");
9701#endif
9702 break;
Daniel Veillarda880b122003-04-21 21:36:41 +00009703 case XML_PARSER_IGNORE:
9704 xmlGenericError(xmlGenericErrorContext,
9705 "PP: internal error, state == IGNORE");
9706 ctxt->instate = XML_PARSER_DTD;
9707#ifdef DEBUG_PUSH
9708 xmlGenericError(xmlGenericErrorContext,
9709 "PP: entering DTD\n");
9710#endif
9711 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009712 case XML_PARSER_PI:
9713 xmlGenericError(xmlGenericErrorContext,
9714 "PP: internal error, state == PI\n");
9715 ctxt->instate = XML_PARSER_CONTENT;
9716#ifdef DEBUG_PUSH
9717 xmlGenericError(xmlGenericErrorContext,
9718 "PP: entering CONTENT\n");
9719#endif
9720 break;
9721 case XML_PARSER_ENTITY_DECL:
9722 xmlGenericError(xmlGenericErrorContext,
9723 "PP: internal error, state == ENTITY_DECL\n");
9724 ctxt->instate = XML_PARSER_DTD;
9725#ifdef DEBUG_PUSH
9726 xmlGenericError(xmlGenericErrorContext,
9727 "PP: entering DTD\n");
9728#endif
9729 break;
9730 case XML_PARSER_ENTITY_VALUE:
9731 xmlGenericError(xmlGenericErrorContext,
9732 "PP: internal error, state == ENTITY_VALUE\n");
9733 ctxt->instate = XML_PARSER_CONTENT;
9734#ifdef DEBUG_PUSH
9735 xmlGenericError(xmlGenericErrorContext,
9736 "PP: entering DTD\n");
9737#endif
9738 break;
9739 case XML_PARSER_ATTRIBUTE_VALUE:
9740 xmlGenericError(xmlGenericErrorContext,
9741 "PP: internal error, state == ATTRIBUTE_VALUE\n");
9742 ctxt->instate = XML_PARSER_START_TAG;
9743#ifdef DEBUG_PUSH
9744 xmlGenericError(xmlGenericErrorContext,
9745 "PP: entering START_TAG\n");
9746#endif
9747 break;
9748 case XML_PARSER_SYSTEM_LITERAL:
9749 xmlGenericError(xmlGenericErrorContext,
9750 "PP: internal error, state == SYSTEM_LITERAL\n");
9751 ctxt->instate = XML_PARSER_START_TAG;
9752#ifdef DEBUG_PUSH
9753 xmlGenericError(xmlGenericErrorContext,
9754 "PP: entering START_TAG\n");
9755#endif
9756 break;
Daniel Veillard4a7ae502002-02-18 19:18:17 +00009757 case XML_PARSER_PUBLIC_LITERAL:
9758 xmlGenericError(xmlGenericErrorContext,
9759 "PP: internal error, state == PUBLIC_LITERAL\n");
9760 ctxt->instate = XML_PARSER_START_TAG;
9761#ifdef DEBUG_PUSH
9762 xmlGenericError(xmlGenericErrorContext,
9763 "PP: entering START_TAG\n");
9764#endif
9765 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009766 }
9767 }
9768done:
9769#ifdef DEBUG_PUSH
9770 xmlGenericError(xmlGenericErrorContext, "PP: done %d\n", ret);
9771#endif
9772 return(ret);
9773}
9774
9775/**
Owen Taylor3473f882001-02-23 17:55:21 +00009776 * xmlParseChunk:
9777 * @ctxt: an XML parser context
9778 * @chunk: an char array
9779 * @size: the size in byte of the chunk
9780 * @terminate: last chunk indicator
9781 *
9782 * Parse a Chunk of memory
9783 *
9784 * Returns zero if no error, the xmlParserErrors otherwise.
9785 */
9786int
9787xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size,
9788 int terminate) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009789 if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
9790 return(ctxt->errNo);
Daniel Veillard309f81d2003-09-23 09:02:53 +00009791 if (ctxt->instate == XML_PARSER_START)
9792 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00009793 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
9794 (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) {
9795 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
9796 int cur = ctxt->input->cur - ctxt->input->base;
William M. Bracka3215c72004-07-31 16:24:01 +00009797 int res;
Owen Taylor3473f882001-02-23 17:55:21 +00009798
William M. Bracka3215c72004-07-31 16:24:01 +00009799 res =xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
9800 if (res < 0) {
9801 ctxt->errNo = XML_PARSER_EOF;
9802 ctxt->disableSAX = 1;
9803 return (XML_PARSER_EOF);
9804 }
Owen Taylor3473f882001-02-23 17:55:21 +00009805 ctxt->input->base = ctxt->input->buf->buffer->content + base;
9806 ctxt->input->cur = ctxt->input->base + cur;
Daniel Veillard48b2f892001-02-25 16:11:03 +00009807 ctxt->input->end =
9808 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00009809#ifdef DEBUG_PUSH
9810 xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
9811#endif
9812
Owen Taylor3473f882001-02-23 17:55:21 +00009813 } else if (ctxt->instate != XML_PARSER_EOF) {
9814 if ((ctxt->input != NULL) && ctxt->input->buf != NULL) {
9815 xmlParserInputBufferPtr in = ctxt->input->buf;
9816 if ((in->encoder != NULL) && (in->buffer != NULL) &&
9817 (in->raw != NULL)) {
9818 int nbchars;
9819
9820 nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
9821 if (nbchars < 0) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00009822 /* TODO 2.6.0 */
Owen Taylor3473f882001-02-23 17:55:21 +00009823 xmlGenericError(xmlGenericErrorContext,
9824 "xmlParseChunk: encoder error\n");
9825 return(XML_ERR_INVALID_ENCODING);
9826 }
9827 }
9828 }
9829 }
9830 xmlParseTryOrFinish(ctxt, terminate);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009831 if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
9832 return(ctxt->errNo);
Owen Taylor3473f882001-02-23 17:55:21 +00009833 if (terminate) {
9834 /*
9835 * Check for termination
9836 */
Daniel Veillard819d5cb2002-10-14 11:15:18 +00009837 int avail = 0;
9838 if (ctxt->input->buf == NULL)
9839 avail = ctxt->input->length -
9840 (ctxt->input->cur - ctxt->input->base);
9841 else
9842 avail = ctxt->input->buf->buffer->use -
9843 (ctxt->input->cur - ctxt->input->base);
9844
Owen Taylor3473f882001-02-23 17:55:21 +00009845 if ((ctxt->instate != XML_PARSER_EOF) &&
9846 (ctxt->instate != XML_PARSER_EPILOG)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009847 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00009848 }
Daniel Veillard819d5cb2002-10-14 11:15:18 +00009849 if ((ctxt->instate == XML_PARSER_EPILOG) && (avail > 0)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009850 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Daniel Veillard819d5cb2002-10-14 11:15:18 +00009851 }
Owen Taylor3473f882001-02-23 17:55:21 +00009852 if (ctxt->instate != XML_PARSER_EOF) {
Daniel Veillard8d24cc12002-03-05 15:41:29 +00009853 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00009854 ctxt->sax->endDocument(ctxt->userData);
9855 }
9856 ctxt->instate = XML_PARSER_EOF;
9857 }
9858 return((xmlParserErrors) ctxt->errNo);
9859}
9860
9861/************************************************************************
9862 * *
9863 * I/O front end functions to the parser *
9864 * *
9865 ************************************************************************/
9866
9867/**
9868 * xmlStopParser:
9869 * @ctxt: an XML parser context
9870 *
9871 * Blocks further parser processing
9872 */
9873void
9874xmlStopParser(xmlParserCtxtPtr ctxt) {
Daniel Veillard157fee02003-10-31 10:36:03 +00009875 if (ctxt == NULL)
9876 return;
Owen Taylor3473f882001-02-23 17:55:21 +00009877 ctxt->instate = XML_PARSER_EOF;
Daniel Veillard157fee02003-10-31 10:36:03 +00009878 ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00009879 if (ctxt->input != NULL)
9880 ctxt->input->cur = BAD_CAST"";
9881}
9882
9883/**
9884 * xmlCreatePushParserCtxt:
9885 * @sax: a SAX handler
9886 * @user_data: The user data returned on SAX callbacks
9887 * @chunk: a pointer to an array of chars
9888 * @size: number of chars in the array
9889 * @filename: an optional file name or URI
9890 *
Daniel Veillard176d99f2002-07-06 19:22:28 +00009891 * Create a parser context for using the XML parser in push mode.
9892 * If @buffer and @size are non-NULL, the data is used to detect
9893 * the encoding. The remaining characters will be parsed so they
9894 * don't need to be fed in again through xmlParseChunk.
Owen Taylor3473f882001-02-23 17:55:21 +00009895 * To allow content encoding detection, @size should be >= 4
9896 * The value of @filename is used for fetching external entities
9897 * and error/warning reports.
9898 *
9899 * Returns the new parser context or NULL
9900 */
Daniel Veillard176d99f2002-07-06 19:22:28 +00009901
Owen Taylor3473f882001-02-23 17:55:21 +00009902xmlParserCtxtPtr
9903xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
9904 const char *chunk, int size, const char *filename) {
9905 xmlParserCtxtPtr ctxt;
9906 xmlParserInputPtr inputStream;
9907 xmlParserInputBufferPtr buf;
9908 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
9909
9910 /*
9911 * plug some encoding conversion routines
9912 */
9913 if ((chunk != NULL) && (size >= 4))
9914 enc = xmlDetectCharEncoding((const xmlChar *) chunk, size);
9915
9916 buf = xmlAllocParserInputBuffer(enc);
9917 if (buf == NULL) return(NULL);
9918
9919 ctxt = xmlNewParserCtxt();
9920 if (ctxt == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00009921 xmlErrMemory(NULL, "creating parser: out of memory\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009922 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00009923 return(NULL);
9924 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009925 ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * sizeof(xmlChar *));
9926 if (ctxt->pushTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009927 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00009928 xmlFreeParserInputBuffer(buf);
9929 xmlFreeParserCtxt(ctxt);
9930 return(NULL);
9931 }
Owen Taylor3473f882001-02-23 17:55:21 +00009932 if (sax != NULL) {
Daniel Veillard81273902003-09-30 00:43:48 +00009933#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00009934 if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
Daniel Veillard81273902003-09-30 00:43:48 +00009935#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00009936 xmlFree(ctxt->sax);
9937 ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler));
9938 if (ctxt->sax == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009939 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009940 xmlFreeParserInputBuffer(buf);
9941 xmlFreeParserCtxt(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00009942 return(NULL);
9943 }
9944 memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler));
9945 if (user_data != NULL)
9946 ctxt->userData = user_data;
9947 }
9948 if (filename == NULL) {
9949 ctxt->directory = NULL;
9950 } else {
9951 ctxt->directory = xmlParserGetDirectory(filename);
9952 }
9953
9954 inputStream = xmlNewInputStream(ctxt);
9955 if (inputStream == NULL) {
9956 xmlFreeParserCtxt(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009957 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00009958 return(NULL);
9959 }
9960
9961 if (filename == NULL)
9962 inputStream->filename = NULL;
William M. Bracka3215c72004-07-31 16:24:01 +00009963 else {
Daniel Veillardf4862f02002-09-10 11:13:43 +00009964 inputStream->filename = (char *)
Igor Zlatkovic5f9fada2003-02-19 14:51:00 +00009965 xmlCanonicPath((const xmlChar *) filename);
William M. Bracka3215c72004-07-31 16:24:01 +00009966 if (inputStream->filename == NULL) {
9967 xmlFreeParserCtxt(ctxt);
9968 xmlFreeParserInputBuffer(buf);
9969 return(NULL);
9970 }
9971 }
Owen Taylor3473f882001-02-23 17:55:21 +00009972 inputStream->buf = buf;
9973 inputStream->base = inputStream->buf->buffer->content;
9974 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00009975 inputStream->end =
9976 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00009977
9978 inputPush(ctxt, inputStream);
9979
9980 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
9981 (ctxt->input->buf != NULL)) {
Daniel Veillardaa39a0f2002-01-06 12:47:22 +00009982 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
9983 int cur = ctxt->input->cur - ctxt->input->base;
9984
Owen Taylor3473f882001-02-23 17:55:21 +00009985 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
Daniel Veillardaa39a0f2002-01-06 12:47:22 +00009986
9987 ctxt->input->base = ctxt->input->buf->buffer->content + base;
9988 ctxt->input->cur = ctxt->input->base + cur;
9989 ctxt->input->end =
9990 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00009991#ifdef DEBUG_PUSH
9992 xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
9993#endif
9994 }
9995
Daniel Veillard0e4cd172001-06-28 12:13:56 +00009996 if (enc != XML_CHAR_ENCODING_NONE) {
9997 xmlSwitchEncoding(ctxt, enc);
9998 }
9999
Owen Taylor3473f882001-02-23 17:55:21 +000010000 return(ctxt);
10001}
Daniel Veillard73b013f2003-09-30 12:36:01 +000010002#endif /* LIBXML_PUSH_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000010003
10004/**
10005 * xmlCreateIOParserCtxt:
10006 * @sax: a SAX handler
10007 * @user_data: The user data returned on SAX callbacks
10008 * @ioread: an I/O read function
10009 * @ioclose: an I/O close function
10010 * @ioctx: an I/O handler
10011 * @enc: the charset encoding if known
10012 *
10013 * Create a parser context for using the XML parser with an existing
10014 * I/O stream
10015 *
10016 * Returns the new parser context or NULL
10017 */
10018xmlParserCtxtPtr
10019xmlCreateIOParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
10020 xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
10021 void *ioctx, xmlCharEncoding enc) {
10022 xmlParserCtxtPtr ctxt;
10023 xmlParserInputPtr inputStream;
10024 xmlParserInputBufferPtr buf;
10025
10026 buf = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, enc);
10027 if (buf == NULL) return(NULL);
10028
10029 ctxt = xmlNewParserCtxt();
10030 if (ctxt == NULL) {
10031 xmlFree(buf);
10032 return(NULL);
10033 }
10034 if (sax != NULL) {
Daniel Veillard81273902003-09-30 00:43:48 +000010035#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +000010036 if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
Daniel Veillard81273902003-09-30 00:43:48 +000010037#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000010038 xmlFree(ctxt->sax);
10039 ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler));
10040 if (ctxt->sax == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010041 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010042 xmlFree(ctxt);
10043 return(NULL);
10044 }
10045 memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler));
10046 if (user_data != NULL)
10047 ctxt->userData = user_data;
10048 }
10049
10050 inputStream = xmlNewIOInputStream(ctxt, buf, enc);
10051 if (inputStream == NULL) {
10052 xmlFreeParserCtxt(ctxt);
10053 return(NULL);
10054 }
10055 inputPush(ctxt, inputStream);
10056
10057 return(ctxt);
10058}
10059
Daniel Veillard4432df22003-09-28 18:58:27 +000010060#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000010061/************************************************************************
10062 * *
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010063 * Front ends when parsing a DTD *
Owen Taylor3473f882001-02-23 17:55:21 +000010064 * *
10065 ************************************************************************/
10066
10067/**
10068 * xmlIOParseDTD:
10069 * @sax: the SAX handler block or NULL
10070 * @input: an Input Buffer
10071 * @enc: the charset encoding if known
10072 *
10073 * Load and parse a DTD
10074 *
10075 * Returns the resulting xmlDtdPtr or NULL in case of error.
10076 * @input will be freed at parsing end.
10077 */
10078
10079xmlDtdPtr
10080xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input,
10081 xmlCharEncoding enc) {
10082 xmlDtdPtr ret = NULL;
10083 xmlParserCtxtPtr ctxt;
10084 xmlParserInputPtr pinput = NULL;
Daniel Veillard87a764e2001-06-20 17:41:10 +000010085 xmlChar start[4];
Owen Taylor3473f882001-02-23 17:55:21 +000010086
10087 if (input == NULL)
10088 return(NULL);
10089
10090 ctxt = xmlNewParserCtxt();
10091 if (ctxt == NULL) {
10092 return(NULL);
10093 }
10094
10095 /*
10096 * Set-up the SAX context
10097 */
10098 if (sax != NULL) {
10099 if (ctxt->sax != NULL)
10100 xmlFree(ctxt->sax);
10101 ctxt->sax = sax;
Daniel Veillard500a1de2004-03-22 15:22:58 +000010102 ctxt->userData = ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +000010103 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000010104 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010105
10106 /*
10107 * generate a parser input from the I/O handler
10108 */
10109
Daniel Veillard43caefb2003-12-07 19:32:22 +000010110 pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
Owen Taylor3473f882001-02-23 17:55:21 +000010111 if (pinput == NULL) {
10112 if (sax != NULL) ctxt->sax = NULL;
10113 xmlFreeParserCtxt(ctxt);
10114 return(NULL);
10115 }
10116
10117 /*
10118 * plug some encoding conversion routines here.
10119 */
10120 xmlPushInput(ctxt, pinput);
Daniel Veillard43caefb2003-12-07 19:32:22 +000010121 if (enc != XML_CHAR_ENCODING_NONE) {
10122 xmlSwitchEncoding(ctxt, enc);
10123 }
Owen Taylor3473f882001-02-23 17:55:21 +000010124
10125 pinput->filename = NULL;
10126 pinput->line = 1;
10127 pinput->col = 1;
10128 pinput->base = ctxt->input->cur;
10129 pinput->cur = ctxt->input->cur;
10130 pinput->free = NULL;
10131
10132 /*
10133 * let's parse that entity knowing it's an external subset.
10134 */
10135 ctxt->inSubset = 2;
10136 ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
10137 ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none",
10138 BAD_CAST "none", BAD_CAST "none");
Daniel Veillard87a764e2001-06-20 17:41:10 +000010139
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010140 if ((enc == XML_CHAR_ENCODING_NONE) &&
10141 ((ctxt->input->end - ctxt->input->cur) >= 4)) {
Daniel Veillard87a764e2001-06-20 17:41:10 +000010142 /*
10143 * Get the 4 first bytes and decode the charset
10144 * if enc != XML_CHAR_ENCODING_NONE
10145 * plug some encoding conversion routines.
10146 */
10147 start[0] = RAW;
10148 start[1] = NXT(1);
10149 start[2] = NXT(2);
10150 start[3] = NXT(3);
10151 enc = xmlDetectCharEncoding(start, 4);
10152 if (enc != XML_CHAR_ENCODING_NONE) {
10153 xmlSwitchEncoding(ctxt, enc);
10154 }
10155 }
10156
Owen Taylor3473f882001-02-23 17:55:21 +000010157 xmlParseExternalSubset(ctxt, BAD_CAST "none", BAD_CAST "none");
10158
10159 if (ctxt->myDoc != NULL) {
10160 if (ctxt->wellFormed) {
10161 ret = ctxt->myDoc->extSubset;
10162 ctxt->myDoc->extSubset = NULL;
Daniel Veillard329456a2003-04-26 21:21:00 +000010163 if (ret != NULL) {
10164 xmlNodePtr tmp;
10165
10166 ret->doc = NULL;
10167 tmp = ret->children;
10168 while (tmp != NULL) {
10169 tmp->doc = NULL;
10170 tmp = tmp->next;
10171 }
10172 }
Owen Taylor3473f882001-02-23 17:55:21 +000010173 } else {
10174 ret = NULL;
10175 }
10176 xmlFreeDoc(ctxt->myDoc);
10177 ctxt->myDoc = NULL;
10178 }
10179 if (sax != NULL) ctxt->sax = NULL;
10180 xmlFreeParserCtxt(ctxt);
10181
10182 return(ret);
10183}
10184
10185/**
10186 * xmlSAXParseDTD:
10187 * @sax: the SAX handler block
10188 * @ExternalID: a NAME* containing the External ID of the DTD
10189 * @SystemID: a NAME* containing the URL to the DTD
10190 *
10191 * Load and parse an external subset.
10192 *
10193 * Returns the resulting xmlDtdPtr or NULL in case of error.
10194 */
10195
10196xmlDtdPtr
10197xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID,
10198 const xmlChar *SystemID) {
10199 xmlDtdPtr ret = NULL;
10200 xmlParserCtxtPtr ctxt;
10201 xmlParserInputPtr input = NULL;
10202 xmlCharEncoding enc;
Igor Zlatkovic07d59762004-08-24 19:12:51 +000010203 xmlChar* systemIdCanonic;
Owen Taylor3473f882001-02-23 17:55:21 +000010204
10205 if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL);
10206
10207 ctxt = xmlNewParserCtxt();
10208 if (ctxt == NULL) {
10209 return(NULL);
10210 }
10211
10212 /*
10213 * Set-up the SAX context
10214 */
10215 if (sax != NULL) {
10216 if (ctxt->sax != NULL)
10217 xmlFree(ctxt->sax);
10218 ctxt->sax = sax;
Daniel Veillardbf1e3d82003-08-14 23:57:26 +000010219 ctxt->userData = ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +000010220 }
Igor Zlatkovic07d59762004-08-24 19:12:51 +000010221
10222 /*
10223 * Canonicalise the system ID
10224 */
10225 systemIdCanonic = xmlCanonicPath(SystemID);
Daniel Veillardc93a19f2004-10-04 11:53:20 +000010226 if ((SystemID != NULL) && (systemIdCanonic == NULL)) {
Igor Zlatkovic07d59762004-08-24 19:12:51 +000010227 xmlFreeParserCtxt(ctxt);
10228 return(NULL);
10229 }
Owen Taylor3473f882001-02-23 17:55:21 +000010230
10231 /*
10232 * Ask the Entity resolver to load the damn thing
10233 */
10234
10235 if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
Igor Zlatkovic07d59762004-08-24 19:12:51 +000010236 input = ctxt->sax->resolveEntity(ctxt, ExternalID, systemIdCanonic);
Owen Taylor3473f882001-02-23 17:55:21 +000010237 if (input == NULL) {
10238 if (sax != NULL) ctxt->sax = NULL;
10239 xmlFreeParserCtxt(ctxt);
Igor Zlatkovic07d59762004-08-24 19:12:51 +000010240 xmlFree(systemIdCanonic);
Owen Taylor3473f882001-02-23 17:55:21 +000010241 return(NULL);
10242 }
10243
10244 /*
10245 * plug some encoding conversion routines here.
10246 */
10247 xmlPushInput(ctxt, input);
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010248 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
10249 enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
10250 xmlSwitchEncoding(ctxt, enc);
10251 }
Owen Taylor3473f882001-02-23 17:55:21 +000010252
10253 if (input->filename == NULL)
Igor Zlatkovic07d59762004-08-24 19:12:51 +000010254 input->filename = (char *) systemIdCanonic;
10255 else
10256 xmlFree(systemIdCanonic);
Owen Taylor3473f882001-02-23 17:55:21 +000010257 input->line = 1;
10258 input->col = 1;
10259 input->base = ctxt->input->cur;
10260 input->cur = ctxt->input->cur;
10261 input->free = NULL;
10262
10263 /*
10264 * let's parse that entity knowing it's an external subset.
10265 */
10266 ctxt->inSubset = 2;
10267 ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
10268 ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none",
10269 ExternalID, SystemID);
10270 xmlParseExternalSubset(ctxt, ExternalID, SystemID);
10271
10272 if (ctxt->myDoc != NULL) {
10273 if (ctxt->wellFormed) {
10274 ret = ctxt->myDoc->extSubset;
10275 ctxt->myDoc->extSubset = NULL;
Daniel Veillardc5573462003-04-25 16:43:49 +000010276 if (ret != NULL) {
10277 xmlNodePtr tmp;
10278
10279 ret->doc = NULL;
10280 tmp = ret->children;
10281 while (tmp != NULL) {
10282 tmp->doc = NULL;
10283 tmp = tmp->next;
10284 }
10285 }
Owen Taylor3473f882001-02-23 17:55:21 +000010286 } else {
10287 ret = NULL;
10288 }
10289 xmlFreeDoc(ctxt->myDoc);
10290 ctxt->myDoc = NULL;
10291 }
10292 if (sax != NULL) ctxt->sax = NULL;
10293 xmlFreeParserCtxt(ctxt);
10294
10295 return(ret);
10296}
10297
Daniel Veillard4432df22003-09-28 18:58:27 +000010298
Owen Taylor3473f882001-02-23 17:55:21 +000010299/**
10300 * xmlParseDTD:
10301 * @ExternalID: a NAME* containing the External ID of the DTD
10302 * @SystemID: a NAME* containing the URL to the DTD
10303 *
10304 * Load and parse an external subset.
10305 *
10306 * Returns the resulting xmlDtdPtr or NULL in case of error.
10307 */
10308
10309xmlDtdPtr
10310xmlParseDTD(const xmlChar *ExternalID, const xmlChar *SystemID) {
10311 return(xmlSAXParseDTD(NULL, ExternalID, SystemID));
10312}
Daniel Veillard4432df22003-09-28 18:58:27 +000010313#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000010314
10315/************************************************************************
10316 * *
10317 * Front ends when parsing an Entity *
10318 * *
10319 ************************************************************************/
10320
10321/**
Owen Taylor3473f882001-02-23 17:55:21 +000010322 * xmlParseCtxtExternalEntity:
10323 * @ctx: the existing parsing context
10324 * @URL: the URL for the entity to load
10325 * @ID: the System ID for the entity to load
Daniel Veillardcda96922001-08-21 10:56:31 +000010326 * @lst: the return value for the set of parsed nodes
Owen Taylor3473f882001-02-23 17:55:21 +000010327 *
10328 * Parse an external general entity within an existing parsing context
10329 * An external general parsed entity is well-formed if it matches the
10330 * production labeled extParsedEnt.
10331 *
10332 * [78] extParsedEnt ::= TextDecl? content
10333 *
10334 * Returns 0 if the entity is well formed, -1 in case of args problem and
10335 * the parser error code otherwise
10336 */
10337
10338int
10339xmlParseCtxtExternalEntity(xmlParserCtxtPtr ctx, const xmlChar *URL,
Daniel Veillardcda96922001-08-21 10:56:31 +000010340 const xmlChar *ID, xmlNodePtr *lst) {
Owen Taylor3473f882001-02-23 17:55:21 +000010341 xmlParserCtxtPtr ctxt;
10342 xmlDocPtr newDoc;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010343 xmlNodePtr newRoot;
Owen Taylor3473f882001-02-23 17:55:21 +000010344 xmlSAXHandlerPtr oldsax = NULL;
10345 int ret = 0;
Daniel Veillard87a764e2001-06-20 17:41:10 +000010346 xmlChar start[4];
10347 xmlCharEncoding enc;
Owen Taylor3473f882001-02-23 17:55:21 +000010348
10349 if (ctx->depth > 40) {
10350 return(XML_ERR_ENTITY_LOOP);
10351 }
10352
Daniel Veillardcda96922001-08-21 10:56:31 +000010353 if (lst != NULL)
10354 *lst = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010355 if ((URL == NULL) && (ID == NULL))
10356 return(-1);
10357 if (ctx->myDoc == NULL) /* @@ relax but check for dereferences */
10358 return(-1);
10359
10360
10361 ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL);
10362 if (ctxt == NULL) return(-1);
10363 ctxt->userData = ctxt;
Daniel Veillarde2830f12003-01-08 17:47:49 +000010364 ctxt->_private = ctx->_private;
Owen Taylor3473f882001-02-23 17:55:21 +000010365 oldsax = ctxt->sax;
10366 ctxt->sax = ctx->sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000010367 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010368 newDoc = xmlNewDoc(BAD_CAST "1.0");
10369 if (newDoc == NULL) {
10370 xmlFreeParserCtxt(ctxt);
10371 return(-1);
10372 }
10373 if (ctx->myDoc != NULL) {
10374 newDoc->intSubset = ctx->myDoc->intSubset;
10375 newDoc->extSubset = ctx->myDoc->extSubset;
10376 }
10377 if (ctx->myDoc->URL != NULL) {
10378 newDoc->URL = xmlStrdup(ctx->myDoc->URL);
10379 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010380 newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
10381 if (newRoot == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +000010382 ctxt->sax = oldsax;
10383 xmlFreeParserCtxt(ctxt);
10384 newDoc->intSubset = NULL;
10385 newDoc->extSubset = NULL;
10386 xmlFreeDoc(newDoc);
10387 return(-1);
10388 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010389 xmlAddChild((xmlNodePtr) newDoc, newRoot);
Owen Taylor3473f882001-02-23 17:55:21 +000010390 nodePush(ctxt, newDoc->children);
10391 if (ctx->myDoc == NULL) {
10392 ctxt->myDoc = newDoc;
10393 } else {
10394 ctxt->myDoc = ctx->myDoc;
10395 newDoc->children->doc = ctx->myDoc;
10396 }
10397
Daniel Veillard87a764e2001-06-20 17:41:10 +000010398 /*
10399 * Get the 4 first bytes and decode the charset
10400 * if enc != XML_CHAR_ENCODING_NONE
10401 * plug some encoding conversion routines.
10402 */
10403 GROW
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010404 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
10405 start[0] = RAW;
10406 start[1] = NXT(1);
10407 start[2] = NXT(2);
10408 start[3] = NXT(3);
10409 enc = xmlDetectCharEncoding(start, 4);
10410 if (enc != XML_CHAR_ENCODING_NONE) {
10411 xmlSwitchEncoding(ctxt, enc);
10412 }
Daniel Veillard87a764e2001-06-20 17:41:10 +000010413 }
10414
Owen Taylor3473f882001-02-23 17:55:21 +000010415 /*
10416 * Parse a possible text declaration first
10417 */
Daniel Veillarda07050d2003-10-19 14:46:32 +000010418 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +000010419 xmlParseTextDecl(ctxt);
10420 }
10421
10422 /*
10423 * Doing validity checking on chunk doesn't make sense
10424 */
10425 ctxt->instate = XML_PARSER_CONTENT;
10426 ctxt->validate = ctx->validate;
Daniel Veillard5f8d1a32003-03-23 21:02:00 +000010427 ctxt->valid = ctx->valid;
Owen Taylor3473f882001-02-23 17:55:21 +000010428 ctxt->loadsubset = ctx->loadsubset;
10429 ctxt->depth = ctx->depth + 1;
10430 ctxt->replaceEntities = ctx->replaceEntities;
10431 if (ctxt->validate) {
10432 ctxt->vctxt.error = ctx->vctxt.error;
10433 ctxt->vctxt.warning = ctx->vctxt.warning;
Owen Taylor3473f882001-02-23 17:55:21 +000010434 } else {
10435 ctxt->vctxt.error = NULL;
10436 ctxt->vctxt.warning = NULL;
10437 }
Daniel Veillarda9142e72001-06-19 11:07:54 +000010438 ctxt->vctxt.nodeTab = NULL;
10439 ctxt->vctxt.nodeNr = 0;
10440 ctxt->vctxt.nodeMax = 0;
10441 ctxt->vctxt.node = NULL;
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000010442 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
10443 ctxt->dict = ctx->dict;
Daniel Veillardfd343dc2003-10-31 10:55:22 +000010444 ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
10445 ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
10446 ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000010447 ctxt->dictNames = ctx->dictNames;
10448 ctxt->attsDefault = ctx->attsDefault;
10449 ctxt->attsSpecial = ctx->attsSpecial;
William M. Brack503b6102004-08-19 02:17:27 +000010450 ctxt->linenumbers = ctx->linenumbers;
Owen Taylor3473f882001-02-23 17:55:21 +000010451
10452 xmlParseContent(ctxt);
10453
Daniel Veillard5f8d1a32003-03-23 21:02:00 +000010454 ctx->validate = ctxt->validate;
10455 ctx->valid = ctxt->valid;
Owen Taylor3473f882001-02-23 17:55:21 +000010456 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010457 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010458 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010459 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010460 }
10461 if (ctxt->node != newDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010462 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010463 }
10464
10465 if (!ctxt->wellFormed) {
10466 if (ctxt->errNo == 0)
10467 ret = 1;
10468 else
10469 ret = ctxt->errNo;
10470 } else {
Daniel Veillardcda96922001-08-21 10:56:31 +000010471 if (lst != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +000010472 xmlNodePtr cur;
10473
10474 /*
10475 * Return the newly created nodeset after unlinking it from
10476 * they pseudo parent.
10477 */
10478 cur = newDoc->children->children;
Daniel Veillardcda96922001-08-21 10:56:31 +000010479 *lst = cur;
Owen Taylor3473f882001-02-23 17:55:21 +000010480 while (cur != NULL) {
10481 cur->parent = NULL;
10482 cur = cur->next;
10483 }
10484 newDoc->children->children = NULL;
10485 }
10486 ret = 0;
10487 }
10488 ctxt->sax = oldsax;
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000010489 ctxt->dict = NULL;
10490 ctxt->attsDefault = NULL;
10491 ctxt->attsSpecial = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010492 xmlFreeParserCtxt(ctxt);
10493 newDoc->intSubset = NULL;
10494 newDoc->extSubset = NULL;
10495 xmlFreeDoc(newDoc);
10496
10497 return(ret);
10498}
10499
10500/**
Daniel Veillard257d9102001-05-08 10:41:44 +000010501 * xmlParseExternalEntityPrivate:
Owen Taylor3473f882001-02-23 17:55:21 +000010502 * @doc: the document the chunk pertains to
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010503 * @oldctxt: the previous parser context if available
Owen Taylor3473f882001-02-23 17:55:21 +000010504 * @sax: the SAX handler bloc (possibly NULL)
10505 * @user_data: The user data returned on SAX callbacks (possibly NULL)
10506 * @depth: Used for loop detection, use 0
10507 * @URL: the URL for the entity to load
10508 * @ID: the System ID for the entity to load
10509 * @list: the return value for the set of parsed nodes
10510 *
Daniel Veillard257d9102001-05-08 10:41:44 +000010511 * Private version of xmlParseExternalEntity()
Owen Taylor3473f882001-02-23 17:55:21 +000010512 *
10513 * Returns 0 if the entity is well formed, -1 in case of args problem and
10514 * the parser error code otherwise
10515 */
10516
Daniel Veillard7d515752003-09-26 19:12:37 +000010517static xmlParserErrors
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010518xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt,
10519 xmlSAXHandlerPtr sax,
Daniel Veillard257d9102001-05-08 10:41:44 +000010520 void *user_data, int depth, const xmlChar *URL,
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010521 const xmlChar *ID, xmlNodePtr *list) {
Owen Taylor3473f882001-02-23 17:55:21 +000010522 xmlParserCtxtPtr ctxt;
10523 xmlDocPtr newDoc;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010524 xmlNodePtr newRoot;
Owen Taylor3473f882001-02-23 17:55:21 +000010525 xmlSAXHandlerPtr oldsax = NULL;
Daniel Veillard7d515752003-09-26 19:12:37 +000010526 xmlParserErrors ret = XML_ERR_OK;
Daniel Veillard87a764e2001-06-20 17:41:10 +000010527 xmlChar start[4];
10528 xmlCharEncoding enc;
Owen Taylor3473f882001-02-23 17:55:21 +000010529
10530 if (depth > 40) {
10531 return(XML_ERR_ENTITY_LOOP);
10532 }
10533
10534
10535
10536 if (list != NULL)
10537 *list = NULL;
10538 if ((URL == NULL) && (ID == NULL))
Daniel Veillard7d515752003-09-26 19:12:37 +000010539 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010540 if (doc == NULL) /* @@ relax but check for dereferences */
Daniel Veillard7d515752003-09-26 19:12:37 +000010541 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010542
10543
10544 ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL);
William M. Brackb670e2e2003-09-27 01:05:55 +000010545 if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY);
Owen Taylor3473f882001-02-23 17:55:21 +000010546 ctxt->userData = ctxt;
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010547 if (oldctxt != NULL) {
10548 ctxt->_private = oldctxt->_private;
10549 ctxt->loadsubset = oldctxt->loadsubset;
10550 ctxt->validate = oldctxt->validate;
10551 ctxt->external = oldctxt->external;
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010552 ctxt->record_info = oldctxt->record_info;
10553 ctxt->node_seq.maximum = oldctxt->node_seq.maximum;
10554 ctxt->node_seq.length = oldctxt->node_seq.length;
10555 ctxt->node_seq.buffer = oldctxt->node_seq.buffer;
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010556 } else {
10557 /*
10558 * Doing validity checking on chunk without context
10559 * doesn't make sense
10560 */
10561 ctxt->_private = NULL;
10562 ctxt->validate = 0;
10563 ctxt->external = 2;
10564 ctxt->loadsubset = 0;
10565 }
Owen Taylor3473f882001-02-23 17:55:21 +000010566 if (sax != NULL) {
10567 oldsax = ctxt->sax;
10568 ctxt->sax = sax;
10569 if (user_data != NULL)
10570 ctxt->userData = user_data;
10571 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000010572 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010573 newDoc = xmlNewDoc(BAD_CAST "1.0");
10574 if (newDoc == NULL) {
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010575 ctxt->node_seq.maximum = 0;
10576 ctxt->node_seq.length = 0;
10577 ctxt->node_seq.buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010578 xmlFreeParserCtxt(ctxt);
Daniel Veillard7d515752003-09-26 19:12:37 +000010579 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010580 }
10581 if (doc != NULL) {
10582 newDoc->intSubset = doc->intSubset;
10583 newDoc->extSubset = doc->extSubset;
10584 }
10585 if (doc->URL != NULL) {
10586 newDoc->URL = xmlStrdup(doc->URL);
10587 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010588 newRoot = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
10589 if (newRoot == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +000010590 if (sax != NULL)
10591 ctxt->sax = oldsax;
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010592 ctxt->node_seq.maximum = 0;
10593 ctxt->node_seq.length = 0;
10594 ctxt->node_seq.buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010595 xmlFreeParserCtxt(ctxt);
10596 newDoc->intSubset = NULL;
10597 newDoc->extSubset = NULL;
10598 xmlFreeDoc(newDoc);
Daniel Veillard7d515752003-09-26 19:12:37 +000010599 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010600 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010601 xmlAddChild((xmlNodePtr) newDoc, newRoot);
Owen Taylor3473f882001-02-23 17:55:21 +000010602 nodePush(ctxt, newDoc->children);
10603 if (doc == NULL) {
10604 ctxt->myDoc = newDoc;
10605 } else {
10606 ctxt->myDoc = doc;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010607 newRoot->doc = doc;
Owen Taylor3473f882001-02-23 17:55:21 +000010608 }
10609
Daniel Veillard87a764e2001-06-20 17:41:10 +000010610 /*
10611 * Get the 4 first bytes and decode the charset
10612 * if enc != XML_CHAR_ENCODING_NONE
10613 * plug some encoding conversion routines.
10614 */
10615 GROW;
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010616 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
10617 start[0] = RAW;
10618 start[1] = NXT(1);
10619 start[2] = NXT(2);
10620 start[3] = NXT(3);
10621 enc = xmlDetectCharEncoding(start, 4);
10622 if (enc != XML_CHAR_ENCODING_NONE) {
10623 xmlSwitchEncoding(ctxt, enc);
10624 }
Daniel Veillard87a764e2001-06-20 17:41:10 +000010625 }
10626
Owen Taylor3473f882001-02-23 17:55:21 +000010627 /*
10628 * Parse a possible text declaration first
10629 */
Daniel Veillarda07050d2003-10-19 14:46:32 +000010630 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +000010631 xmlParseTextDecl(ctxt);
10632 }
10633
Owen Taylor3473f882001-02-23 17:55:21 +000010634 ctxt->instate = XML_PARSER_CONTENT;
Owen Taylor3473f882001-02-23 17:55:21 +000010635 ctxt->depth = depth;
10636
10637 xmlParseContent(ctxt);
10638
Daniel Veillard561b7f82002-03-20 21:55:57 +000010639 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010640 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Daniel Veillard561b7f82002-03-20 21:55:57 +000010641 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010642 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010643 }
10644 if (ctxt->node != newDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010645 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010646 }
10647
10648 if (!ctxt->wellFormed) {
10649 if (ctxt->errNo == 0)
Daniel Veillard7d515752003-09-26 19:12:37 +000010650 ret = XML_ERR_INTERNAL_ERROR;
Owen Taylor3473f882001-02-23 17:55:21 +000010651 else
William M. Brack7b9154b2003-09-27 19:23:50 +000010652 ret = (xmlParserErrors)ctxt->errNo;
Owen Taylor3473f882001-02-23 17:55:21 +000010653 } else {
10654 if (list != NULL) {
10655 xmlNodePtr cur;
10656
10657 /*
10658 * Return the newly created nodeset after unlinking it from
10659 * they pseudo parent.
10660 */
10661 cur = newDoc->children->children;
10662 *list = cur;
10663 while (cur != NULL) {
10664 cur->parent = NULL;
10665 cur = cur->next;
10666 }
10667 newDoc->children->children = NULL;
10668 }
Daniel Veillard7d515752003-09-26 19:12:37 +000010669 ret = XML_ERR_OK;
Owen Taylor3473f882001-02-23 17:55:21 +000010670 }
10671 if (sax != NULL)
10672 ctxt->sax = oldsax;
Daniel Veillard0046c0f2003-02-23 13:52:30 +000010673 oldctxt->node_seq.maximum = ctxt->node_seq.maximum;
10674 oldctxt->node_seq.length = ctxt->node_seq.length;
10675 oldctxt->node_seq.buffer = ctxt->node_seq.buffer;
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010676 ctxt->node_seq.maximum = 0;
10677 ctxt->node_seq.length = 0;
10678 ctxt->node_seq.buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010679 xmlFreeParserCtxt(ctxt);
10680 newDoc->intSubset = NULL;
10681 newDoc->extSubset = NULL;
10682 xmlFreeDoc(newDoc);
10683
10684 return(ret);
10685}
10686
Daniel Veillard81273902003-09-30 00:43:48 +000010687#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000010688/**
Daniel Veillard257d9102001-05-08 10:41:44 +000010689 * xmlParseExternalEntity:
10690 * @doc: the document the chunk pertains to
10691 * @sax: the SAX handler bloc (possibly NULL)
10692 * @user_data: The user data returned on SAX callbacks (possibly NULL)
10693 * @depth: Used for loop detection, use 0
10694 * @URL: the URL for the entity to load
10695 * @ID: the System ID for the entity to load
Daniel Veillardcda96922001-08-21 10:56:31 +000010696 * @lst: the return value for the set of parsed nodes
Daniel Veillard257d9102001-05-08 10:41:44 +000010697 *
10698 * Parse an external general entity
10699 * An external general parsed entity is well-formed if it matches the
10700 * production labeled extParsedEnt.
10701 *
10702 * [78] extParsedEnt ::= TextDecl? content
10703 *
10704 * Returns 0 if the entity is well formed, -1 in case of args problem and
10705 * the parser error code otherwise
10706 */
10707
10708int
10709xmlParseExternalEntity(xmlDocPtr doc, xmlSAXHandlerPtr sax, void *user_data,
Daniel Veillardcda96922001-08-21 10:56:31 +000010710 int depth, const xmlChar *URL, const xmlChar *ID, xmlNodePtr *lst) {
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010711 return(xmlParseExternalEntityPrivate(doc, NULL, sax, user_data, depth, URL,
Daniel Veillardcda96922001-08-21 10:56:31 +000010712 ID, lst));
Daniel Veillard257d9102001-05-08 10:41:44 +000010713}
10714
10715/**
Daniel Veillarde020c3a2001-03-21 18:06:15 +000010716 * xmlParseBalancedChunkMemory:
Owen Taylor3473f882001-02-23 17:55:21 +000010717 * @doc: the document the chunk pertains to
10718 * @sax: the SAX handler bloc (possibly NULL)
10719 * @user_data: The user data returned on SAX callbacks (possibly NULL)
10720 * @depth: Used for loop detection, use 0
10721 * @string: the input string in UTF8 or ISO-Latin (zero terminated)
Daniel Veillardcda96922001-08-21 10:56:31 +000010722 * @lst: the return value for the set of parsed nodes
Owen Taylor3473f882001-02-23 17:55:21 +000010723 *
10724 * Parse a well-balanced chunk of an XML document
10725 * called by the parser
10726 * The allowed sequence for the Well Balanced Chunk is the one defined by
10727 * the content production in the XML grammar:
10728 *
10729 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
10730 *
10731 * Returns 0 if the chunk is well balanced, -1 in case of args problem and
10732 * the parser error code otherwise
10733 */
10734
10735int
10736xmlParseBalancedChunkMemory(xmlDocPtr doc, xmlSAXHandlerPtr sax,
Daniel Veillardcda96922001-08-21 10:56:31 +000010737 void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst) {
Daniel Veillard58e44c92002-08-02 22:19:49 +000010738 return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data,
10739 depth, string, lst, 0 );
10740}
Daniel Veillard81273902003-09-30 00:43:48 +000010741#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard58e44c92002-08-02 22:19:49 +000010742
10743/**
Daniel Veillard328f48c2002-11-15 15:24:34 +000010744 * xmlParseBalancedChunkMemoryInternal:
10745 * @oldctxt: the existing parsing context
10746 * @string: the input string in UTF8 or ISO-Latin (zero terminated)
10747 * @user_data: the user data field for the parser context
10748 * @lst: the return value for the set of parsed nodes
10749 *
10750 *
10751 * Parse a well-balanced chunk of an XML document
10752 * called by the parser
10753 * The allowed sequence for the Well Balanced Chunk is the one defined by
10754 * the content production in the XML grammar:
10755 *
10756 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
10757 *
Daniel Veillard7d515752003-09-26 19:12:37 +000010758 * Returns XML_ERR_OK if the chunk is well balanced, and the parser
10759 * error code otherwise
Daniel Veillard328f48c2002-11-15 15:24:34 +000010760 *
10761 * In case recover is set to 1, the nodelist will not be empty even if
10762 * the parsed chunk is not well balanced.
10763 */
Daniel Veillard7d515752003-09-26 19:12:37 +000010764static xmlParserErrors
Daniel Veillard328f48c2002-11-15 15:24:34 +000010765xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt,
10766 const xmlChar *string, void *user_data, xmlNodePtr *lst) {
10767 xmlParserCtxtPtr ctxt;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010768 xmlDocPtr newDoc = NULL;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010769 xmlNodePtr newRoot;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010770 xmlSAXHandlerPtr oldsax = NULL;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010771 xmlNodePtr content = NULL;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010772 xmlNodePtr last = NULL;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010773 int size;
Daniel Veillard7d515752003-09-26 19:12:37 +000010774 xmlParserErrors ret = XML_ERR_OK;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010775
10776 if (oldctxt->depth > 40) {
10777 return(XML_ERR_ENTITY_LOOP);
10778 }
10779
10780
10781 if (lst != NULL)
10782 *lst = NULL;
10783 if (string == NULL)
William M. Brack7b9154b2003-09-27 19:23:50 +000010784 return(XML_ERR_INTERNAL_ERROR);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010785
10786 size = xmlStrlen(string);
10787
10788 ctxt = xmlCreateMemoryParserCtxt((char *) string, size);
William M. Brack7b9154b2003-09-27 19:23:50 +000010789 if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010790 if (user_data != NULL)
10791 ctxt->userData = user_data;
10792 else
10793 ctxt->userData = ctxt;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010794 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
10795 ctxt->dict = oldctxt->dict;
Daniel Veillardfd343dc2003-10-31 10:55:22 +000010796 ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
10797 ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
10798 ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010799
10800 oldsax = ctxt->sax;
10801 ctxt->sax = oldctxt->sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000010802 xmlDetectSAX2(ctxt);
Daniel Veillard87ab1c12003-12-21 13:01:56 +000010803 ctxt->replaceEntities = oldctxt->replaceEntities;
10804 ctxt->options = oldctxt->options;
Daniel Veillarde57ec792003-09-10 10:50:59 +000010805
Daniel Veillarde1ca5032002-12-09 14:13:43 +000010806 ctxt->_private = oldctxt->_private;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010807 if (oldctxt->myDoc == NULL) {
Daniel Veillard68e9e742002-11-16 15:35:11 +000010808 newDoc = xmlNewDoc(BAD_CAST "1.0");
10809 if (newDoc == NULL) {
10810 ctxt->sax = oldsax;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010811 ctxt->dict = NULL;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010812 xmlFreeParserCtxt(ctxt);
William M. Brack7b9154b2003-09-27 19:23:50 +000010813 return(XML_ERR_INTERNAL_ERROR);
Daniel Veillard68e9e742002-11-16 15:35:11 +000010814 }
Daniel Veillard328f48c2002-11-15 15:24:34 +000010815 ctxt->myDoc = newDoc;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010816 } else {
10817 ctxt->myDoc = oldctxt->myDoc;
10818 content = ctxt->myDoc->children;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010819 last = ctxt->myDoc->last;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010820 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010821 newRoot = xmlNewDocNode(ctxt->myDoc, NULL, BAD_CAST "pseudoroot", NULL);
10822 if (newRoot == NULL) {
Daniel Veillard68e9e742002-11-16 15:35:11 +000010823 ctxt->sax = oldsax;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010824 ctxt->dict = NULL;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010825 xmlFreeParserCtxt(ctxt);
10826 if (newDoc != NULL)
10827 xmlFreeDoc(newDoc);
William M. Brack7b9154b2003-09-27 19:23:50 +000010828 return(XML_ERR_INTERNAL_ERROR);
Daniel Veillard68e9e742002-11-16 15:35:11 +000010829 }
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010830 ctxt->myDoc->children = NULL;
10831 ctxt->myDoc->last = NULL;
10832 xmlAddChild((xmlNodePtr) ctxt->myDoc, newRoot);
Daniel Veillard68e9e742002-11-16 15:35:11 +000010833 nodePush(ctxt, ctxt->myDoc->children);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010834 ctxt->instate = XML_PARSER_CONTENT;
10835 ctxt->depth = oldctxt->depth + 1;
10836
Daniel Veillard328f48c2002-11-15 15:24:34 +000010837 ctxt->validate = 0;
10838 ctxt->loadsubset = oldctxt->loadsubset;
Daniel Veillardef8dd7b2003-03-23 12:02:56 +000010839 if ((oldctxt->validate) || (oldctxt->replaceEntities != 0)) {
10840 /*
10841 * ID/IDREF registration will be done in xmlValidateElement below
10842 */
10843 ctxt->loadsubset |= XML_SKIP_IDS;
10844 }
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010845 ctxt->dictNames = oldctxt->dictNames;
Daniel Veillard95d2d5b2003-10-27 14:54:49 +000010846 ctxt->attsDefault = oldctxt->attsDefault;
10847 ctxt->attsSpecial = oldctxt->attsSpecial;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010848
Daniel Veillard68e9e742002-11-16 15:35:11 +000010849 xmlParseContent(ctxt);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010850 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010851 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010852 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010853 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010854 }
Daniel Veillard68e9e742002-11-16 15:35:11 +000010855 if (ctxt->node != ctxt->myDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010856 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010857 }
10858
10859 if (!ctxt->wellFormed) {
10860 if (ctxt->errNo == 0)
Daniel Veillard7d515752003-09-26 19:12:37 +000010861 ret = XML_ERR_INTERNAL_ERROR;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010862 else
William M. Brack7b9154b2003-09-27 19:23:50 +000010863 ret = (xmlParserErrors)ctxt->errNo;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010864 } else {
William M. Brack7b9154b2003-09-27 19:23:50 +000010865 ret = XML_ERR_OK;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010866 }
10867
William M. Brack7b9154b2003-09-27 19:23:50 +000010868 if ((lst != NULL) && (ret == XML_ERR_OK)) {
Daniel Veillard328f48c2002-11-15 15:24:34 +000010869 xmlNodePtr cur;
10870
10871 /*
10872 * Return the newly created nodeset after unlinking it from
10873 * they pseudo parent.
10874 */
Daniel Veillard68e9e742002-11-16 15:35:11 +000010875 cur = ctxt->myDoc->children->children;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010876 *lst = cur;
10877 while (cur != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +000010878#ifdef LIBXML_VALID_ENABLED
Daniel Veillard8d589042003-02-04 15:07:21 +000010879 if (oldctxt->validate && oldctxt->wellFormed &&
10880 oldctxt->myDoc && oldctxt->myDoc->intSubset) {
10881 oldctxt->valid &= xmlValidateElement(&oldctxt->vctxt,
10882 oldctxt->myDoc, cur);
10883 }
Daniel Veillard4432df22003-09-28 18:58:27 +000010884#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard328f48c2002-11-15 15:24:34 +000010885 cur->parent = NULL;
10886 cur = cur->next;
10887 }
Daniel Veillard68e9e742002-11-16 15:35:11 +000010888 ctxt->myDoc->children->children = NULL;
10889 }
10890 if (ctxt->myDoc != NULL) {
10891 xmlFreeNode(ctxt->myDoc->children);
10892 ctxt->myDoc->children = content;
Daniel Veillard8de5c0b2004-10-07 13:14:19 +000010893 ctxt->myDoc->last = last;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010894 }
10895
10896 ctxt->sax = oldsax;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010897 ctxt->dict = NULL;
Daniel Veillard95d2d5b2003-10-27 14:54:49 +000010898 ctxt->attsDefault = NULL;
10899 ctxt->attsSpecial = NULL;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010900 xmlFreeParserCtxt(ctxt);
Daniel Veillard68e9e742002-11-16 15:35:11 +000010901 if (newDoc != NULL)
10902 xmlFreeDoc(newDoc);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010903
10904 return(ret);
10905}
10906
Daniel Veillard29b17482004-08-16 00:39:03 +000010907/**
10908 * xmlParseInNodeContext:
10909 * @node: the context node
10910 * @data: the input string
10911 * @datalen: the input string length in bytes
10912 * @options: a combination of xmlParserOption
10913 * @lst: the return value for the set of parsed nodes
10914 *
10915 * Parse a well-balanced chunk of an XML document
10916 * within the context (DTD, namespaces, etc ...) of the given node.
10917 *
10918 * The allowed sequence for the data is a Well Balanced Chunk defined by
10919 * the content production in the XML grammar:
10920 *
10921 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
10922 *
10923 * Returns XML_ERR_OK if the chunk is well balanced, and the parser
10924 * error code otherwise
10925 */
10926xmlParserErrors
10927xmlParseInNodeContext(xmlNodePtr node, const char *data, int datalen,
10928 int options, xmlNodePtr *lst) {
10929#ifdef SAX2
10930 xmlParserCtxtPtr ctxt;
10931 xmlDocPtr doc = NULL;
10932 xmlNodePtr fake, cur;
10933 int nsnr = 0;
10934
10935 xmlParserErrors ret = XML_ERR_OK;
10936
10937 /*
10938 * check all input parameters, grab the document
10939 */
10940 if ((lst == NULL) || (node == NULL) || (data == NULL) || (datalen < 0))
10941 return(XML_ERR_INTERNAL_ERROR);
10942 switch (node->type) {
10943 case XML_ELEMENT_NODE:
10944 case XML_ATTRIBUTE_NODE:
10945 case XML_TEXT_NODE:
10946 case XML_CDATA_SECTION_NODE:
10947 case XML_ENTITY_REF_NODE:
10948 case XML_PI_NODE:
10949 case XML_COMMENT_NODE:
10950 case XML_DOCUMENT_NODE:
10951 case XML_HTML_DOCUMENT_NODE:
10952 break;
10953 default:
10954 return(XML_ERR_INTERNAL_ERROR);
10955
10956 }
10957 while ((node != NULL) && (node->type != XML_ELEMENT_NODE) &&
10958 (node->type != XML_DOCUMENT_NODE) &&
10959 (node->type != XML_HTML_DOCUMENT_NODE))
10960 node = node->parent;
10961 if (node == NULL)
10962 return(XML_ERR_INTERNAL_ERROR);
10963 if (node->type == XML_ELEMENT_NODE)
10964 doc = node->doc;
10965 else
10966 doc = (xmlDocPtr) node;
10967 if (doc == NULL)
10968 return(XML_ERR_INTERNAL_ERROR);
10969
10970 /*
10971 * allocate a context and set-up everything not related to the
10972 * node position in the tree
10973 */
10974 if (doc->type == XML_DOCUMENT_NODE)
10975 ctxt = xmlCreateMemoryParserCtxt((char *) data, datalen);
10976#ifdef LIBXML_HTML_ENABLED
10977 else if (doc->type == XML_HTML_DOCUMENT_NODE)
10978 ctxt = htmlCreateMemoryParserCtxt((char *) data, datalen);
10979#endif
10980 else
10981 return(XML_ERR_INTERNAL_ERROR);
10982
10983 if (ctxt == NULL)
10984 return(XML_ERR_NO_MEMORY);
10985 fake = xmlNewComment(NULL);
10986 if (fake == NULL) {
10987 xmlFreeParserCtxt(ctxt);
10988 return(XML_ERR_NO_MEMORY);
10989 }
10990 xmlAddChild(node, fake);
William M. Brackc3f81342004-10-03 01:22:44 +000010991
10992 /*
10993 * Use input doc's dict if present, else assure XML_PARSE_NODICT is set.
10994 * We need a dictionary for xmlDetectSAX2, so if there's no doc dict
10995 * we must wait until the last moment to free the original one.
10996 */
Daniel Veillard29b17482004-08-16 00:39:03 +000010997 if (doc->dict != NULL) {
William M. Brackc3f81342004-10-03 01:22:44 +000010998 if (ctxt->dict != NULL)
Daniel Veillard29b17482004-08-16 00:39:03 +000010999 xmlDictFree(ctxt->dict);
11000 ctxt->dict = doc->dict;
William M. Brackc3f81342004-10-03 01:22:44 +000011001 } else
11002 options |= XML_PARSE_NODICT;
11003
11004 xmlCtxtUseOptions(ctxt, options);
Daniel Veillard29b17482004-08-16 00:39:03 +000011005 xmlDetectSAX2(ctxt);
11006 ctxt->myDoc = doc;
11007
11008 if (node->type == XML_ELEMENT_NODE) {
11009 nodePush(ctxt, node);
11010 /*
11011 * initialize the SAX2 namespaces stack
11012 */
11013 cur = node;
11014 while ((cur != NULL) && (cur->type == XML_ELEMENT_NODE)) {
11015 xmlNsPtr ns = cur->nsDef;
11016 const xmlChar *iprefix, *ihref;
11017
11018 while (ns != NULL) {
11019 if (ctxt->dict) {
11020 iprefix = xmlDictLookup(ctxt->dict, ns->prefix, -1);
11021 ihref = xmlDictLookup(ctxt->dict, ns->href, -1);
11022 } else {
11023 iprefix = ns->prefix;
11024 ihref = ns->href;
11025 }
11026
11027 if (xmlGetNamespace(ctxt, iprefix) == NULL) {
11028 nsPush(ctxt, iprefix, ihref);
11029 nsnr++;
11030 }
11031 ns = ns->next;
11032 }
11033 cur = cur->parent;
11034 }
11035 ctxt->instate = XML_PARSER_CONTENT;
11036 }
11037
11038 if ((ctxt->validate) || (ctxt->replaceEntities != 0)) {
11039 /*
11040 * ID/IDREF registration will be done in xmlValidateElement below
11041 */
11042 ctxt->loadsubset |= XML_SKIP_IDS;
11043 }
11044
11045 xmlParseContent(ctxt);
11046 nsPop(ctxt, nsnr);
11047 if ((RAW == '<') && (NXT(1) == '/')) {
11048 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
11049 } else if (RAW != 0) {
11050 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
11051 }
11052 if ((ctxt->node != NULL) && (ctxt->node != node)) {
11053 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
11054 ctxt->wellFormed = 0;
11055 }
11056
11057 if (!ctxt->wellFormed) {
11058 if (ctxt->errNo == 0)
11059 ret = XML_ERR_INTERNAL_ERROR;
11060 else
11061 ret = (xmlParserErrors)ctxt->errNo;
11062 } else {
11063 ret = XML_ERR_OK;
11064 }
11065
11066 /*
11067 * Return the newly created nodeset after unlinking it from
11068 * the pseudo sibling.
11069 */
11070
11071 cur = fake->next;
11072 fake->next = NULL;
11073 node->last = fake;
11074
11075 if (cur != NULL) {
11076 cur->prev = NULL;
11077 }
11078
11079 *lst = cur;
11080
11081 while (cur != NULL) {
11082 cur->parent = NULL;
11083 cur = cur->next;
11084 }
11085
11086 xmlUnlinkNode(fake);
11087 xmlFreeNode(fake);
11088
11089
11090 if (ret != XML_ERR_OK) {
11091 xmlFreeNodeList(*lst);
11092 *lst = NULL;
11093 }
William M. Brackc3f81342004-10-03 01:22:44 +000011094
William M. Brackb7b54de2004-10-06 16:38:01 +000011095 if (doc->dict != NULL)
11096 ctxt->dict = NULL;
Daniel Veillard29b17482004-08-16 00:39:03 +000011097 xmlFreeParserCtxt(ctxt);
11098
11099 return(ret);
11100#else /* !SAX2 */
11101 return(XML_ERR_INTERNAL_ERROR);
11102#endif
11103}
11104
Daniel Veillard81273902003-09-30 00:43:48 +000011105#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard328f48c2002-11-15 15:24:34 +000011106/**
Daniel Veillard58e44c92002-08-02 22:19:49 +000011107 * xmlParseBalancedChunkMemoryRecover:
11108 * @doc: the document the chunk pertains to
11109 * @sax: the SAX handler bloc (possibly NULL)
11110 * @user_data: The user data returned on SAX callbacks (possibly NULL)
11111 * @depth: Used for loop detection, use 0
11112 * @string: the input string in UTF8 or ISO-Latin (zero terminated)
11113 * @lst: the return value for the set of parsed nodes
11114 * @recover: return nodes even if the data is broken (use 0)
11115 *
11116 *
11117 * Parse a well-balanced chunk of an XML document
11118 * called by the parser
11119 * The allowed sequence for the Well Balanced Chunk is the one defined by
11120 * the content production in the XML grammar:
11121 *
11122 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
11123 *
11124 * Returns 0 if the chunk is well balanced, -1 in case of args problem and
11125 * the parser error code otherwise
11126 *
11127 * In case recover is set to 1, the nodelist will not be empty even if
11128 * the parsed chunk is not well balanced.
11129 */
11130int
11131xmlParseBalancedChunkMemoryRecover(xmlDocPtr doc, xmlSAXHandlerPtr sax,
11132 void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst,
11133 int recover) {
Owen Taylor3473f882001-02-23 17:55:21 +000011134 xmlParserCtxtPtr ctxt;
11135 xmlDocPtr newDoc;
11136 xmlSAXHandlerPtr oldsax = NULL;
Daniel Veillard935494a2002-10-22 14:22:46 +000011137 xmlNodePtr content;
Owen Taylor3473f882001-02-23 17:55:21 +000011138 int size;
11139 int ret = 0;
11140
11141 if (depth > 40) {
11142 return(XML_ERR_ENTITY_LOOP);
11143 }
11144
11145
Daniel Veillardcda96922001-08-21 10:56:31 +000011146 if (lst != NULL)
11147 *lst = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000011148 if (string == NULL)
11149 return(-1);
11150
11151 size = xmlStrlen(string);
11152
11153 ctxt = xmlCreateMemoryParserCtxt((char *) string, size);
11154 if (ctxt == NULL) return(-1);
11155 ctxt->userData = ctxt;
11156 if (sax != NULL) {
11157 oldsax = ctxt->sax;
11158 ctxt->sax = sax;
11159 if (user_data != NULL)
11160 ctxt->userData = user_data;
11161 }
11162 newDoc = xmlNewDoc(BAD_CAST "1.0");
11163 if (newDoc == NULL) {
11164 xmlFreeParserCtxt(ctxt);
11165 return(-1);
11166 }
11167 if (doc != NULL) {
11168 newDoc->intSubset = doc->intSubset;
11169 newDoc->extSubset = doc->extSubset;
11170 }
11171 newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
11172 if (newDoc->children == NULL) {
11173 if (sax != NULL)
11174 ctxt->sax = oldsax;
11175 xmlFreeParserCtxt(ctxt);
11176 newDoc->intSubset = NULL;
11177 newDoc->extSubset = NULL;
11178 xmlFreeDoc(newDoc);
11179 return(-1);
11180 }
11181 nodePush(ctxt, newDoc->children);
11182 if (doc == NULL) {
11183 ctxt->myDoc = newDoc;
11184 } else {
Daniel Veillard42766c02002-08-22 20:52:17 +000011185 ctxt->myDoc = newDoc;
Owen Taylor3473f882001-02-23 17:55:21 +000011186 newDoc->children->doc = doc;
11187 }
11188 ctxt->instate = XML_PARSER_CONTENT;
11189 ctxt->depth = depth;
11190
11191 /*
11192 * Doing validity checking on chunk doesn't make sense
11193 */
11194 ctxt->validate = 0;
11195 ctxt->loadsubset = 0;
Daniel Veillarde57ec792003-09-10 10:50:59 +000011196 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011197
Daniel Veillardb39bc392002-10-26 19:29:51 +000011198 if ( doc != NULL ){
11199 content = doc->children;
11200 doc->children = NULL;
11201 xmlParseContent(ctxt);
11202 doc->children = content;
11203 }
11204 else {
11205 xmlParseContent(ctxt);
11206 }
Owen Taylor3473f882001-02-23 17:55:21 +000011207 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000011208 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000011209 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000011210 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000011211 }
11212 if (ctxt->node != newDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000011213 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000011214 }
11215
11216 if (!ctxt->wellFormed) {
11217 if (ctxt->errNo == 0)
11218 ret = 1;
11219 else
11220 ret = ctxt->errNo;
11221 } else {
Daniel Veillard58e44c92002-08-02 22:19:49 +000011222 ret = 0;
11223 }
11224
11225 if (lst != NULL && (ret == 0 || recover == 1)) {
11226 xmlNodePtr cur;
Owen Taylor3473f882001-02-23 17:55:21 +000011227
11228 /*
11229 * Return the newly created nodeset after unlinking it from
11230 * they pseudo parent.
11231 */
11232 cur = newDoc->children->children;
Daniel Veillardcda96922001-08-21 10:56:31 +000011233 *lst = cur;
Owen Taylor3473f882001-02-23 17:55:21 +000011234 while (cur != NULL) {
11235 cur->parent = NULL;
11236 cur = cur->next;
11237 }
11238 newDoc->children->children = NULL;
11239 }
Daniel Veillard58e44c92002-08-02 22:19:49 +000011240
Owen Taylor3473f882001-02-23 17:55:21 +000011241 if (sax != NULL)
11242 ctxt->sax = oldsax;
11243 xmlFreeParserCtxt(ctxt);
11244 newDoc->intSubset = NULL;
11245 newDoc->extSubset = NULL;
11246 xmlFreeDoc(newDoc);
11247
11248 return(ret);
11249}
11250
11251/**
11252 * xmlSAXParseEntity:
11253 * @sax: the SAX handler block
11254 * @filename: the filename
11255 *
11256 * parse an XML external entity out of context and build a tree.
11257 * It use the given SAX function block to handle the parsing callback.
11258 * If sax is NULL, fallback to the default DOM tree building routines.
11259 *
11260 * [78] extParsedEnt ::= TextDecl? content
11261 *
11262 * This correspond to a "Well Balanced" chunk
11263 *
11264 * Returns the resulting document tree
11265 */
11266
11267xmlDocPtr
11268xmlSAXParseEntity(xmlSAXHandlerPtr sax, const char *filename) {
11269 xmlDocPtr ret;
11270 xmlParserCtxtPtr ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +000011271
11272 ctxt = xmlCreateFileParserCtxt(filename);
11273 if (ctxt == NULL) {
11274 return(NULL);
11275 }
11276 if (sax != NULL) {
11277 if (ctxt->sax != NULL)
11278 xmlFree(ctxt->sax);
11279 ctxt->sax = sax;
11280 ctxt->userData = NULL;
11281 }
11282
Owen Taylor3473f882001-02-23 17:55:21 +000011283 xmlParseExtParsedEnt(ctxt);
11284
11285 if (ctxt->wellFormed)
11286 ret = ctxt->myDoc;
11287 else {
11288 ret = NULL;
11289 xmlFreeDoc(ctxt->myDoc);
11290 ctxt->myDoc = NULL;
11291 }
11292 if (sax != NULL)
11293 ctxt->sax = NULL;
11294 xmlFreeParserCtxt(ctxt);
11295
11296 return(ret);
11297}
11298
11299/**
11300 * xmlParseEntity:
11301 * @filename: the filename
11302 *
11303 * parse an XML external entity out of context and build a tree.
11304 *
11305 * [78] extParsedEnt ::= TextDecl? content
11306 *
11307 * This correspond to a "Well Balanced" chunk
11308 *
11309 * Returns the resulting document tree
11310 */
11311
11312xmlDocPtr
11313xmlParseEntity(const char *filename) {
11314 return(xmlSAXParseEntity(NULL, filename));
11315}
Daniel Veillard81273902003-09-30 00:43:48 +000011316#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011317
11318/**
11319 * xmlCreateEntityParserCtxt:
11320 * @URL: the entity URL
11321 * @ID: the entity PUBLIC ID
Daniel Veillardcbaf3992001-12-31 16:16:02 +000011322 * @base: a possible base for the target URI
Owen Taylor3473f882001-02-23 17:55:21 +000011323 *
11324 * Create a parser context for an external entity
11325 * Automatic support for ZLIB/Compress compressed document is provided
11326 * by default if found at compile-time.
11327 *
11328 * Returns the new parser context or NULL
11329 */
11330xmlParserCtxtPtr
11331xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID,
11332 const xmlChar *base) {
11333 xmlParserCtxtPtr ctxt;
11334 xmlParserInputPtr inputStream;
11335 char *directory = NULL;
11336 xmlChar *uri;
11337
11338 ctxt = xmlNewParserCtxt();
11339 if (ctxt == NULL) {
11340 return(NULL);
11341 }
11342
11343 uri = xmlBuildURI(URL, base);
11344
11345 if (uri == NULL) {
11346 inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt);
11347 if (inputStream == NULL) {
11348 xmlFreeParserCtxt(ctxt);
11349 return(NULL);
11350 }
11351
11352 inputPush(ctxt, inputStream);
11353
11354 if ((ctxt->directory == NULL) && (directory == NULL))
11355 directory = xmlParserGetDirectory((char *)URL);
11356 if ((ctxt->directory == NULL) && (directory != NULL))
11357 ctxt->directory = directory;
11358 } else {
11359 inputStream = xmlLoadExternalEntity((char *)uri, (char *)ID, ctxt);
11360 if (inputStream == NULL) {
11361 xmlFree(uri);
11362 xmlFreeParserCtxt(ctxt);
11363 return(NULL);
11364 }
11365
11366 inputPush(ctxt, inputStream);
11367
11368 if ((ctxt->directory == NULL) && (directory == NULL))
11369 directory = xmlParserGetDirectory((char *)uri);
11370 if ((ctxt->directory == NULL) && (directory != NULL))
11371 ctxt->directory = directory;
11372 xmlFree(uri);
11373 }
Owen Taylor3473f882001-02-23 17:55:21 +000011374 return(ctxt);
11375}
11376
11377/************************************************************************
11378 * *
11379 * Front ends when parsing from a file *
11380 * *
11381 ************************************************************************/
11382
11383/**
Daniel Veillard61b93382003-11-03 14:28:31 +000011384 * xmlCreateURLParserCtxt:
11385 * @filename: the filename or URL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000011386 * @options: a combination of xmlParserOption
Owen Taylor3473f882001-02-23 17:55:21 +000011387 *
Daniel Veillard61b93382003-11-03 14:28:31 +000011388 * Create a parser context for a file or URL content.
Owen Taylor3473f882001-02-23 17:55:21 +000011389 * Automatic support for ZLIB/Compress compressed document is provided
Daniel Veillard61b93382003-11-03 14:28:31 +000011390 * by default if found at compile-time and for file accesses
Owen Taylor3473f882001-02-23 17:55:21 +000011391 *
11392 * Returns the new parser context or NULL
11393 */
11394xmlParserCtxtPtr
Daniel Veillard61b93382003-11-03 14:28:31 +000011395xmlCreateURLParserCtxt(const char *filename, int options)
Owen Taylor3473f882001-02-23 17:55:21 +000011396{
11397 xmlParserCtxtPtr ctxt;
11398 xmlParserInputPtr inputStream;
Owen Taylor3473f882001-02-23 17:55:21 +000011399 char *directory = NULL;
11400
Owen Taylor3473f882001-02-23 17:55:21 +000011401 ctxt = xmlNewParserCtxt();
11402 if (ctxt == NULL) {
Daniel Veillard81273902003-09-30 00:43:48 +000011403 xmlErrMemory(NULL, "cannot allocate parser context");
Owen Taylor3473f882001-02-23 17:55:21 +000011404 return(NULL);
11405 }
11406
Daniel Veillard61b93382003-11-03 14:28:31 +000011407 if (options != 0)
11408 xmlCtxtUseOptions(ctxt, options);
Igor Zlatkovicce076162003-02-23 13:39:39 +000011409
Daniel Veillard4e9b1bc2003-06-09 10:30:33 +000011410 inputStream = xmlLoadExternalEntity(filename, NULL, ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011411 if (inputStream == NULL) {
11412 xmlFreeParserCtxt(ctxt);
11413 return(NULL);
11414 }
11415
Owen Taylor3473f882001-02-23 17:55:21 +000011416 inputPush(ctxt, inputStream);
11417 if ((ctxt->directory == NULL) && (directory == NULL))
Igor Zlatkovic5f9fada2003-02-19 14:51:00 +000011418 directory = xmlParserGetDirectory(filename);
Owen Taylor3473f882001-02-23 17:55:21 +000011419 if ((ctxt->directory == NULL) && (directory != NULL))
11420 ctxt->directory = directory;
11421
11422 return(ctxt);
11423}
11424
Daniel Veillard61b93382003-11-03 14:28:31 +000011425/**
11426 * xmlCreateFileParserCtxt:
11427 * @filename: the filename
11428 *
11429 * Create a parser context for a file content.
11430 * Automatic support for ZLIB/Compress compressed document is provided
11431 * by default if found at compile-time.
11432 *
11433 * Returns the new parser context or NULL
11434 */
11435xmlParserCtxtPtr
11436xmlCreateFileParserCtxt(const char *filename)
11437{
11438 return(xmlCreateURLParserCtxt(filename, 0));
11439}
11440
Daniel Veillard81273902003-09-30 00:43:48 +000011441#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000011442/**
Daniel Veillarda293c322001-10-02 13:54:14 +000011443 * xmlSAXParseFileWithData:
Owen Taylor3473f882001-02-23 17:55:21 +000011444 * @sax: the SAX handler block
11445 * @filename: the filename
11446 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11447 * documents
Daniel Veillarda293c322001-10-02 13:54:14 +000011448 * @data: the userdata
Owen Taylor3473f882001-02-23 17:55:21 +000011449 *
11450 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11451 * compressed document is provided by default if found at compile-time.
11452 * It use the given SAX function block to handle the parsing callback.
11453 * If sax is NULL, fallback to the default DOM tree building routines.
11454 *
Daniel Veillarde19fc232002-04-22 16:01:24 +000011455 * User data (void *) is stored within the parser context in the
11456 * context's _private member, so it is available nearly everywhere in libxml
Daniel Veillarda293c322001-10-02 13:54:14 +000011457 *
Owen Taylor3473f882001-02-23 17:55:21 +000011458 * Returns the resulting document tree
11459 */
11460
11461xmlDocPtr
Daniel Veillarda293c322001-10-02 13:54:14 +000011462xmlSAXParseFileWithData(xmlSAXHandlerPtr sax, const char *filename,
11463 int recovery, void *data) {
Owen Taylor3473f882001-02-23 17:55:21 +000011464 xmlDocPtr ret;
11465 xmlParserCtxtPtr ctxt;
11466 char *directory = NULL;
11467
Daniel Veillard635ef722001-10-29 11:48:19 +000011468 xmlInitParser();
11469
Owen Taylor3473f882001-02-23 17:55:21 +000011470 ctxt = xmlCreateFileParserCtxt(filename);
11471 if (ctxt == NULL) {
11472 return(NULL);
11473 }
11474 if (sax != NULL) {
11475 if (ctxt->sax != NULL)
11476 xmlFree(ctxt->sax);
11477 ctxt->sax = sax;
Owen Taylor3473f882001-02-23 17:55:21 +000011478 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000011479 xmlDetectSAX2(ctxt);
Daniel Veillarda293c322001-10-02 13:54:14 +000011480 if (data!=NULL) {
Daniel Veillardc790bf42003-10-11 10:50:10 +000011481 ctxt->_private = data;
Daniel Veillarda293c322001-10-02 13:54:14 +000011482 }
Owen Taylor3473f882001-02-23 17:55:21 +000011483
11484 if ((ctxt->directory == NULL) && (directory == NULL))
11485 directory = xmlParserGetDirectory(filename);
11486 if ((ctxt->directory == NULL) && (directory != NULL))
11487 ctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
11488
Daniel Veillarddad3f682002-11-17 16:47:27 +000011489 ctxt->recovery = recovery;
11490
Owen Taylor3473f882001-02-23 17:55:21 +000011491 xmlParseDocument(ctxt);
11492
William M. Brackc07329e2003-09-08 01:57:30 +000011493 if ((ctxt->wellFormed) || recovery) {
11494 ret = ctxt->myDoc;
Daniel Veillardb65e12e2003-10-08 21:33:28 +000011495 if (ret != NULL) {
11496 if (ctxt->input->buf->compressed > 0)
11497 ret->compression = 9;
11498 else
11499 ret->compression = ctxt->input->buf->compressed;
11500 }
William M. Brackc07329e2003-09-08 01:57:30 +000011501 }
Owen Taylor3473f882001-02-23 17:55:21 +000011502 else {
11503 ret = NULL;
11504 xmlFreeDoc(ctxt->myDoc);
11505 ctxt->myDoc = NULL;
11506 }
11507 if (sax != NULL)
11508 ctxt->sax = NULL;
11509 xmlFreeParserCtxt(ctxt);
11510
11511 return(ret);
11512}
11513
11514/**
Daniel Veillarda293c322001-10-02 13:54:14 +000011515 * xmlSAXParseFile:
11516 * @sax: the SAX handler block
11517 * @filename: the filename
11518 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11519 * documents
11520 *
11521 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11522 * compressed document is provided by default if found at compile-time.
11523 * It use the given SAX function block to handle the parsing callback.
11524 * If sax is NULL, fallback to the default DOM tree building routines.
11525 *
11526 * Returns the resulting document tree
11527 */
11528
11529xmlDocPtr
11530xmlSAXParseFile(xmlSAXHandlerPtr sax, const char *filename,
11531 int recovery) {
11532 return(xmlSAXParseFileWithData(sax,filename,recovery,NULL));
11533}
11534
11535/**
Owen Taylor3473f882001-02-23 17:55:21 +000011536 * xmlRecoverDoc:
11537 * @cur: a pointer to an array of xmlChar
11538 *
11539 * parse an XML in-memory document and build a tree.
11540 * In the case the document is not Well Formed, a tree is built anyway
11541 *
11542 * Returns the resulting document tree
11543 */
11544
11545xmlDocPtr
11546xmlRecoverDoc(xmlChar *cur) {
11547 return(xmlSAXParseDoc(NULL, cur, 1));
11548}
11549
11550/**
11551 * xmlParseFile:
11552 * @filename: the filename
11553 *
11554 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11555 * compressed document is provided by default if found at compile-time.
11556 *
Daniel Veillard5d96fff2001-08-31 14:55:30 +000011557 * Returns the resulting document tree if the file was wellformed,
11558 * NULL otherwise.
Owen Taylor3473f882001-02-23 17:55:21 +000011559 */
11560
11561xmlDocPtr
11562xmlParseFile(const char *filename) {
11563 return(xmlSAXParseFile(NULL, filename, 0));
11564}
11565
11566/**
11567 * xmlRecoverFile:
11568 * @filename: the filename
11569 *
11570 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11571 * compressed document is provided by default if found at compile-time.
11572 * In the case the document is not Well Formed, a tree is built anyway
11573 *
11574 * Returns the resulting document tree
11575 */
11576
11577xmlDocPtr
11578xmlRecoverFile(const char *filename) {
11579 return(xmlSAXParseFile(NULL, filename, 1));
11580}
11581
11582
11583/**
11584 * xmlSetupParserForBuffer:
11585 * @ctxt: an XML parser context
11586 * @buffer: a xmlChar * buffer
11587 * @filename: a file name
11588 *
11589 * Setup the parser context to parse a new buffer; Clears any prior
11590 * contents from the parser context. The buffer parameter must not be
11591 * NULL, but the filename parameter can be
11592 */
11593void
11594xmlSetupParserForBuffer(xmlParserCtxtPtr ctxt, const xmlChar* buffer,
11595 const char* filename)
11596{
11597 xmlParserInputPtr input;
11598
11599 input = xmlNewInputStream(ctxt);
11600 if (input == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +000011601 xmlErrMemory(NULL, "parsing new buffer: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +000011602 xmlFree(ctxt);
11603 return;
11604 }
11605
11606 xmlClearParserCtxt(ctxt);
11607 if (filename != NULL)
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +000011608 input->filename = (char *) xmlCanonicPath((const xmlChar *)filename);
Owen Taylor3473f882001-02-23 17:55:21 +000011609 input->base = buffer;
11610 input->cur = buffer;
Daniel Veillard48b2f892001-02-25 16:11:03 +000011611 input->end = &buffer[xmlStrlen(buffer)];
Owen Taylor3473f882001-02-23 17:55:21 +000011612 inputPush(ctxt, input);
11613}
11614
11615/**
11616 * xmlSAXUserParseFile:
11617 * @sax: a SAX handler
11618 * @user_data: The user data returned on SAX callbacks
11619 * @filename: a file name
11620 *
11621 * parse an XML file and call the given SAX handler routines.
11622 * Automatic support for ZLIB/Compress compressed document is provided
11623 *
11624 * Returns 0 in case of success or a error number otherwise
11625 */
11626int
11627xmlSAXUserParseFile(xmlSAXHandlerPtr sax, void *user_data,
11628 const char *filename) {
11629 int ret = 0;
11630 xmlParserCtxtPtr ctxt;
11631
11632 ctxt = xmlCreateFileParserCtxt(filename);
11633 if (ctxt == NULL) return -1;
Daniel Veillard81273902003-09-30 00:43:48 +000011634#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +000011635 if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
Daniel Veillard81273902003-09-30 00:43:48 +000011636#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011637 xmlFree(ctxt->sax);
11638 ctxt->sax = sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000011639 xmlDetectSAX2(ctxt);
11640
Owen Taylor3473f882001-02-23 17:55:21 +000011641 if (user_data != NULL)
11642 ctxt->userData = user_data;
11643
11644 xmlParseDocument(ctxt);
11645
11646 if (ctxt->wellFormed)
11647 ret = 0;
11648 else {
11649 if (ctxt->errNo != 0)
11650 ret = ctxt->errNo;
11651 else
11652 ret = -1;
11653 }
11654 if (sax != NULL)
11655 ctxt->sax = NULL;
11656 xmlFreeParserCtxt(ctxt);
11657
11658 return ret;
11659}
Daniel Veillard81273902003-09-30 00:43:48 +000011660#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011661
11662/************************************************************************
11663 * *
11664 * Front ends when parsing from memory *
11665 * *
11666 ************************************************************************/
11667
11668/**
11669 * xmlCreateMemoryParserCtxt:
11670 * @buffer: a pointer to a char array
11671 * @size: the size of the array
11672 *
11673 * Create a parser context for an XML in-memory document.
11674 *
11675 * Returns the new parser context or NULL
11676 */
11677xmlParserCtxtPtr
Daniel Veillardfd7ddca2001-05-16 10:57:35 +000011678xmlCreateMemoryParserCtxt(const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000011679 xmlParserCtxtPtr ctxt;
11680 xmlParserInputPtr input;
11681 xmlParserInputBufferPtr buf;
11682
11683 if (buffer == NULL)
11684 return(NULL);
11685 if (size <= 0)
11686 return(NULL);
11687
11688 ctxt = xmlNewParserCtxt();
11689 if (ctxt == NULL)
11690 return(NULL);
11691
Daniel Veillard53350552003-09-18 13:35:51 +000011692 /* TODO: xmlParserInputBufferCreateStatic, requires some serious changes */
Owen Taylor3473f882001-02-23 17:55:21 +000011693 buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
Daniel Veillarda7e05b42002-11-19 08:11:14 +000011694 if (buf == NULL) {
11695 xmlFreeParserCtxt(ctxt);
11696 return(NULL);
11697 }
Owen Taylor3473f882001-02-23 17:55:21 +000011698
11699 input = xmlNewInputStream(ctxt);
11700 if (input == NULL) {
Daniel Veillarda7e05b42002-11-19 08:11:14 +000011701 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +000011702 xmlFreeParserCtxt(ctxt);
11703 return(NULL);
11704 }
11705
11706 input->filename = NULL;
11707 input->buf = buf;
11708 input->base = input->buf->buffer->content;
11709 input->cur = input->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +000011710 input->end = &input->buf->buffer->content[input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +000011711
11712 inputPush(ctxt, input);
11713 return(ctxt);
11714}
11715
Daniel Veillard81273902003-09-30 00:43:48 +000011716#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000011717/**
Daniel Veillard8606bbb2002-11-12 12:36:52 +000011718 * xmlSAXParseMemoryWithData:
11719 * @sax: the SAX handler block
11720 * @buffer: an pointer to a char array
11721 * @size: the size of the array
11722 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11723 * documents
11724 * @data: the userdata
11725 *
11726 * parse an XML in-memory block and use the given SAX function block
11727 * to handle the parsing callback. If sax is NULL, fallback to the default
11728 * DOM tree building routines.
11729 *
11730 * User data (void *) is stored within the parser context in the
11731 * context's _private member, so it is available nearly everywhere in libxml
11732 *
11733 * Returns the resulting document tree
11734 */
11735
11736xmlDocPtr
11737xmlSAXParseMemoryWithData(xmlSAXHandlerPtr sax, const char *buffer,
11738 int size, int recovery, void *data) {
11739 xmlDocPtr ret;
11740 xmlParserCtxtPtr ctxt;
11741
11742 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
11743 if (ctxt == NULL) return(NULL);
11744 if (sax != NULL) {
11745 if (ctxt->sax != NULL)
11746 xmlFree(ctxt->sax);
11747 ctxt->sax = sax;
11748 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000011749 xmlDetectSAX2(ctxt);
Daniel Veillard8606bbb2002-11-12 12:36:52 +000011750 if (data!=NULL) {
11751 ctxt->_private=data;
11752 }
11753
Daniel Veillardadba5f12003-04-04 16:09:01 +000011754 ctxt->recovery = recovery;
11755
Daniel Veillard8606bbb2002-11-12 12:36:52 +000011756 xmlParseDocument(ctxt);
11757
11758 if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc;
11759 else {
11760 ret = NULL;
11761 xmlFreeDoc(ctxt->myDoc);
11762 ctxt->myDoc = NULL;
11763 }
11764 if (sax != NULL)
11765 ctxt->sax = NULL;
11766 xmlFreeParserCtxt(ctxt);
11767
11768 return(ret);
11769}
11770
11771/**
Owen Taylor3473f882001-02-23 17:55:21 +000011772 * xmlSAXParseMemory:
11773 * @sax: the SAX handler block
11774 * @buffer: an pointer to a char array
11775 * @size: the size of the array
11776 * @recovery: work in recovery mode, i.e. tries to read not Well Formed
11777 * documents
11778 *
11779 * parse an XML in-memory block and use the given SAX function block
11780 * to handle the parsing callback. If sax is NULL, fallback to the default
11781 * DOM tree building routines.
11782 *
11783 * Returns the resulting document tree
11784 */
11785xmlDocPtr
Daniel Veillard50822cb2001-07-26 20:05:51 +000011786xmlSAXParseMemory(xmlSAXHandlerPtr sax, const char *buffer,
11787 int size, int recovery) {
Daniel Veillard8606bbb2002-11-12 12:36:52 +000011788 return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000011789}
11790
11791/**
11792 * xmlParseMemory:
11793 * @buffer: an pointer to a char array
11794 * @size: the size of the array
11795 *
11796 * parse an XML in-memory block and build a tree.
11797 *
11798 * Returns the resulting document tree
11799 */
11800
Daniel Veillard50822cb2001-07-26 20:05:51 +000011801xmlDocPtr xmlParseMemory(const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000011802 return(xmlSAXParseMemory(NULL, buffer, size, 0));
11803}
11804
11805/**
11806 * xmlRecoverMemory:
11807 * @buffer: an pointer to a char array
11808 * @size: the size of the array
11809 *
11810 * parse an XML in-memory block and build a tree.
11811 * In the case the document is not Well Formed, a tree is built anyway
11812 *
11813 * Returns the resulting document tree
11814 */
11815
Daniel Veillard50822cb2001-07-26 20:05:51 +000011816xmlDocPtr xmlRecoverMemory(const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000011817 return(xmlSAXParseMemory(NULL, buffer, size, 1));
11818}
11819
11820/**
11821 * xmlSAXUserParseMemory:
11822 * @sax: a SAX handler
11823 * @user_data: The user data returned on SAX callbacks
11824 * @buffer: an in-memory XML document input
11825 * @size: the length of the XML document in bytes
11826 *
11827 * A better SAX parsing routine.
11828 * parse an XML in-memory buffer and call the given SAX handler routines.
11829 *
11830 * Returns 0 in case of success or a error number otherwise
11831 */
11832int xmlSAXUserParseMemory(xmlSAXHandlerPtr sax, void *user_data,
Daniel Veillardfd7ddca2001-05-16 10:57:35 +000011833 const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000011834 int ret = 0;
11835 xmlParserCtxtPtr ctxt;
11836 xmlSAXHandlerPtr oldsax = NULL;
11837
Daniel Veillard9e923512002-08-14 08:48:52 +000011838 if (sax == NULL) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +000011839 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
11840 if (ctxt == NULL) return -1;
Daniel Veillard9e923512002-08-14 08:48:52 +000011841 oldsax = ctxt->sax;
11842 ctxt->sax = sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000011843 xmlDetectSAX2(ctxt);
Daniel Veillard30211a02001-04-26 09:33:18 +000011844 if (user_data != NULL)
11845 ctxt->userData = user_data;
Owen Taylor3473f882001-02-23 17:55:21 +000011846
11847 xmlParseDocument(ctxt);
11848
11849 if (ctxt->wellFormed)
11850 ret = 0;
11851 else {
11852 if (ctxt->errNo != 0)
11853 ret = ctxt->errNo;
11854 else
11855 ret = -1;
11856 }
Daniel Veillard9e923512002-08-14 08:48:52 +000011857 ctxt->sax = oldsax;
Owen Taylor3473f882001-02-23 17:55:21 +000011858 xmlFreeParserCtxt(ctxt);
11859
11860 return ret;
11861}
Daniel Veillard81273902003-09-30 00:43:48 +000011862#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011863
11864/**
11865 * xmlCreateDocParserCtxt:
11866 * @cur: a pointer to an array of xmlChar
11867 *
11868 * Creates a parser context for an XML in-memory document.
11869 *
11870 * Returns the new parser context or NULL
11871 */
11872xmlParserCtxtPtr
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011873xmlCreateDocParserCtxt(const xmlChar *cur) {
Owen Taylor3473f882001-02-23 17:55:21 +000011874 int len;
11875
11876 if (cur == NULL)
11877 return(NULL);
11878 len = xmlStrlen(cur);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011879 return(xmlCreateMemoryParserCtxt((const char *)cur, len));
Owen Taylor3473f882001-02-23 17:55:21 +000011880}
11881
Daniel Veillard81273902003-09-30 00:43:48 +000011882#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000011883/**
11884 * xmlSAXParseDoc:
11885 * @sax: the SAX handler block
11886 * @cur: a pointer to an array of xmlChar
11887 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11888 * documents
11889 *
11890 * parse an XML in-memory document and build a tree.
11891 * It use the given SAX function block to handle the parsing callback.
11892 * If sax is NULL, fallback to the default DOM tree building routines.
11893 *
11894 * Returns the resulting document tree
11895 */
11896
11897xmlDocPtr
11898xmlSAXParseDoc(xmlSAXHandlerPtr sax, xmlChar *cur, int recovery) {
11899 xmlDocPtr ret;
11900 xmlParserCtxtPtr ctxt;
11901
11902 if (cur == NULL) return(NULL);
11903
11904
11905 ctxt = xmlCreateDocParserCtxt(cur);
11906 if (ctxt == NULL) return(NULL);
11907 if (sax != NULL) {
11908 ctxt->sax = sax;
11909 ctxt->userData = NULL;
11910 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000011911 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011912
11913 xmlParseDocument(ctxt);
11914 if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc;
11915 else {
11916 ret = NULL;
11917 xmlFreeDoc(ctxt->myDoc);
11918 ctxt->myDoc = NULL;
11919 }
11920 if (sax != NULL)
11921 ctxt->sax = NULL;
11922 xmlFreeParserCtxt(ctxt);
11923
11924 return(ret);
11925}
11926
11927/**
11928 * xmlParseDoc:
11929 * @cur: a pointer to an array of xmlChar
11930 *
11931 * parse an XML in-memory document and build a tree.
11932 *
11933 * Returns the resulting document tree
11934 */
11935
11936xmlDocPtr
11937xmlParseDoc(xmlChar *cur) {
11938 return(xmlSAXParseDoc(NULL, cur, 0));
11939}
Daniel Veillard81273902003-09-30 00:43:48 +000011940#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011941
Daniel Veillard81273902003-09-30 00:43:48 +000011942#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +000011943/************************************************************************
11944 * *
11945 * Specific function to keep track of entities references *
11946 * and used by the XSLT debugger *
11947 * *
11948 ************************************************************************/
11949
11950static xmlEntityReferenceFunc xmlEntityRefFunc = NULL;
11951
11952/**
11953 * xmlAddEntityReference:
11954 * @ent : A valid entity
11955 * @firstNode : A valid first node for children of entity
11956 * @lastNode : A valid last node of children entity
11957 *
11958 * Notify of a reference to an entity of type XML_EXTERNAL_GENERAL_PARSED_ENTITY
11959 */
11960static void
11961xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode,
11962 xmlNodePtr lastNode)
11963{
11964 if (xmlEntityRefFunc != NULL) {
11965 (*xmlEntityRefFunc) (ent, firstNode, lastNode);
11966 }
11967}
11968
11969
11970/**
11971 * xmlSetEntityReferenceFunc:
Daniel Veillard01c13b52002-12-10 15:19:08 +000011972 * @func: A valid function
Daniel Veillard8107a222002-01-13 14:10:10 +000011973 *
11974 * Set the function to call call back when a xml reference has been made
11975 */
11976void
11977xmlSetEntityReferenceFunc(xmlEntityReferenceFunc func)
11978{
11979 xmlEntityRefFunc = func;
11980}
Daniel Veillard81273902003-09-30 00:43:48 +000011981#endif /* LIBXML_LEGACY_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011982
11983/************************************************************************
11984 * *
11985 * Miscellaneous *
11986 * *
11987 ************************************************************************/
11988
11989#ifdef LIBXML_XPATH_ENABLED
11990#include <libxml/xpath.h>
11991#endif
11992
Daniel Veillarddb5850a2002-01-18 11:49:26 +000011993extern void xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...);
Owen Taylor3473f882001-02-23 17:55:21 +000011994static int xmlParserInitialized = 0;
11995
11996/**
11997 * xmlInitParser:
11998 *
11999 * Initialization function for the XML parser.
12000 * This is not reentrant. Call once before processing in case of
12001 * use in multithreaded programs.
12002 */
12003
12004void
12005xmlInitParser(void) {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000012006 if (xmlParserInitialized != 0)
12007 return;
Owen Taylor3473f882001-02-23 17:55:21 +000012008
Daniel Veillarddb5850a2002-01-18 11:49:26 +000012009 if ((xmlGenericError == xmlGenericErrorDefaultFunc) ||
12010 (xmlGenericError == NULL))
12011 initGenericErrorDefaultFunc(NULL);
Daniel Veillard781ac8b2003-05-15 22:11:36 +000012012 xmlInitGlobals();
Daniel Veillardd0463562001-10-13 09:15:48 +000012013 xmlInitThreads();
Daniel Veillard6f350292001-10-14 09:56:15 +000012014 xmlInitMemory();
Owen Taylor3473f882001-02-23 17:55:21 +000012015 xmlInitCharEncodingHandlers();
Owen Taylor3473f882001-02-23 17:55:21 +000012016 xmlDefaultSAXHandlerInit();
12017 xmlRegisterDefaultInputCallbacks();
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000012018#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000012019 xmlRegisterDefaultOutputCallbacks();
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000012020#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000012021#ifdef LIBXML_HTML_ENABLED
12022 htmlInitAutoClose();
12023 htmlDefaultSAXHandlerInit();
12024#endif
12025#ifdef LIBXML_XPATH_ENABLED
12026 xmlXPathInit();
12027#endif
12028 xmlParserInitialized = 1;
12029}
12030
12031/**
12032 * xmlCleanupParser:
12033 *
Daniel Veillardd8cf9062003-11-11 21:12:36 +000012034 * Cleanup function for the XML library. It tries to reclaim all
12035 * parsing related global memory allocated for the library processing.
Owen Taylor3473f882001-02-23 17:55:21 +000012036 * It doesn't deallocate any document related memory. Calling this
Daniel Veillardd8cf9062003-11-11 21:12:36 +000012037 * function should not prevent reusing the library but one should
12038 * call xmlCleanupParser() only when the process has
Daniel Veillard7424eb62003-01-24 14:14:52 +000012039 * finished using the library or XML document built with it.
Owen Taylor3473f882001-02-23 17:55:21 +000012040 */
12041
12042void
12043xmlCleanupParser(void) {
Daniel Veillard7fb801f2003-08-17 21:07:26 +000012044 if (!xmlParserInitialized)
12045 return;
12046
Owen Taylor3473f882001-02-23 17:55:21 +000012047 xmlCleanupCharEncodingHandlers();
Daniel Veillarde2940dd2001-08-22 00:06:49 +000012048#ifdef LIBXML_CATALOG_ENABLED
12049 xmlCatalogCleanup();
12050#endif
Daniel Veillard04054be2003-10-15 10:48:54 +000012051 xmlCleanupInputCallbacks();
12052#ifdef LIBXML_OUTPUT_ENABLED
12053 xmlCleanupOutputCallbacks();
12054#endif
Daniel Veillard781ac8b2003-05-15 22:11:36 +000012055 xmlCleanupGlobals();
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000012056 xmlResetLastError();
Daniel Veillard74c0e592003-11-25 07:01:38 +000012057 xmlCleanupThreads(); /* must be last if called not from the main thread */
William M. Brack72ee48d2003-12-30 08:30:19 +000012058 xmlCleanupMemory();
Daniel Veillardd0463562001-10-13 09:15:48 +000012059 xmlParserInitialized = 0;
Owen Taylor3473f882001-02-23 17:55:21 +000012060}
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012061
12062/************************************************************************
12063 * *
12064 * New set (2.6.0) of simpler and more flexible APIs *
12065 * *
12066 ************************************************************************/
12067
12068/**
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012069 * DICT_FREE:
12070 * @str: a string
12071 *
12072 * Free a string if it is not owned by the "dict" dictionnary in the
12073 * current scope
12074 */
12075#define DICT_FREE(str) \
12076 if ((str) && ((!dict) || \
12077 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
12078 xmlFree((char *)(str));
12079
12080/**
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012081 * xmlCtxtReset:
12082 * @ctxt: an XML parser context
12083 *
12084 * Reset a parser context
12085 */
12086void
12087xmlCtxtReset(xmlParserCtxtPtr ctxt)
12088{
12089 xmlParserInputPtr input;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012090 xmlDictPtr dict = ctxt->dict;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012091
12092 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
12093 xmlFreeInputStream(input);
12094 }
12095 ctxt->inputNr = 0;
12096 ctxt->input = NULL;
12097
12098 ctxt->spaceNr = 0;
12099 ctxt->spaceTab[0] = -1;
12100 ctxt->space = &ctxt->spaceTab[0];
12101
12102
12103 ctxt->nodeNr = 0;
12104 ctxt->node = NULL;
12105
12106 ctxt->nameNr = 0;
12107 ctxt->name = NULL;
12108
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012109 DICT_FREE(ctxt->version);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012110 ctxt->version = NULL;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012111 DICT_FREE(ctxt->encoding);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012112 ctxt->encoding = NULL;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012113 DICT_FREE(ctxt->directory);
12114 ctxt->directory = NULL;
12115 DICT_FREE(ctxt->extSubURI);
12116 ctxt->extSubURI = NULL;
12117 DICT_FREE(ctxt->extSubSystem);
12118 ctxt->extSubSystem = NULL;
12119 if (ctxt->myDoc != NULL)
12120 xmlFreeDoc(ctxt->myDoc);
12121 ctxt->myDoc = NULL;
12122
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012123 ctxt->standalone = -1;
12124 ctxt->hasExternalSubset = 0;
12125 ctxt->hasPErefs = 0;
12126 ctxt->html = 0;
12127 ctxt->external = 0;
12128 ctxt->instate = XML_PARSER_START;
12129 ctxt->token = 0;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012130
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012131 ctxt->wellFormed = 1;
12132 ctxt->nsWellFormed = 1;
Daniel Veillardae289182004-01-21 16:00:43 +000012133 ctxt->disableSAX = 0;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012134 ctxt->valid = 1;
Daniel Veillard766c4f92004-03-26 10:48:29 +000012135#if 0
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012136 ctxt->vctxt.userData = ctxt;
12137 ctxt->vctxt.error = xmlParserValidityError;
12138 ctxt->vctxt.warning = xmlParserValidityWarning;
Daniel Veillard766c4f92004-03-26 10:48:29 +000012139#endif
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012140 ctxt->record_info = 0;
12141 ctxt->nbChars = 0;
12142 ctxt->checkIndex = 0;
12143 ctxt->inSubset = 0;
12144 ctxt->errNo = XML_ERR_OK;
12145 ctxt->depth = 0;
12146 ctxt->charset = XML_CHAR_ENCODING_UTF8;
12147 ctxt->catalogs = NULL;
12148 xmlInitNodeInfoSeq(&ctxt->node_seq);
12149
12150 if (ctxt->attsDefault != NULL) {
12151 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
12152 ctxt->attsDefault = NULL;
12153 }
12154 if (ctxt->attsSpecial != NULL) {
12155 xmlHashFree(ctxt->attsSpecial, NULL);
12156 ctxt->attsSpecial = NULL;
12157 }
12158
Daniel Veillard4432df22003-09-28 18:58:27 +000012159#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012160 if (ctxt->catalogs != NULL)
12161 xmlCatalogFreeLocal(ctxt->catalogs);
Daniel Veillard4432df22003-09-28 18:58:27 +000012162#endif
Daniel Veillardcc199e02003-10-24 21:11:48 +000012163 if (ctxt->lastError.code != XML_ERR_OK)
12164 xmlResetError(&ctxt->lastError);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012165}
12166
12167/**
Daniel Veillard9ba8e382003-10-28 21:31:45 +000012168 * xmlCtxtResetPush:
12169 * @ctxt: an XML parser context
12170 * @chunk: a pointer to an array of chars
12171 * @size: number of chars in the array
12172 * @filename: an optional file name or URI
12173 * @encoding: the document encoding, or NULL
12174 *
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000012175 * Reset a push parser context
12176 *
12177 * Returns 0 in case of success and 1 in case of error
Daniel Veillard9ba8e382003-10-28 21:31:45 +000012178 */
12179int
12180xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk,
12181 int size, const char *filename, const char *encoding)
12182{
12183 xmlParserInputPtr inputStream;
12184 xmlParserInputBufferPtr buf;
12185 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
12186
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000012187 if (ctxt == NULL)
12188 return(1);
12189
Daniel Veillard9ba8e382003-10-28 21:31:45 +000012190 if ((encoding == NULL) && (chunk != NULL) && (size >= 4))
12191 enc = xmlDetectCharEncoding((const xmlChar *) chunk, size);
12192
12193 buf = xmlAllocParserInputBuffer(enc);
12194 if (buf == NULL)
12195 return(1);
12196
12197 if (ctxt == NULL) {
12198 xmlFreeParserInputBuffer(buf);
12199 return(1);
12200 }
12201
12202 xmlCtxtReset(ctxt);
12203
12204 if (ctxt->pushTab == NULL) {
12205 ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 *
12206 sizeof(xmlChar *));
12207 if (ctxt->pushTab == NULL) {
12208 xmlErrMemory(ctxt, NULL);
12209 xmlFreeParserInputBuffer(buf);
12210 return(1);
12211 }
12212 }
12213
12214 if (filename == NULL) {
12215 ctxt->directory = NULL;
12216 } else {
12217 ctxt->directory = xmlParserGetDirectory(filename);
12218 }
12219
12220 inputStream = xmlNewInputStream(ctxt);
12221 if (inputStream == NULL) {
12222 xmlFreeParserInputBuffer(buf);
12223 return(1);
12224 }
12225
12226 if (filename == NULL)
12227 inputStream->filename = NULL;
12228 else
12229 inputStream->filename = (char *)
12230 xmlCanonicPath((const xmlChar *) filename);
12231 inputStream->buf = buf;
12232 inputStream->base = inputStream->buf->buffer->content;
12233 inputStream->cur = inputStream->buf->buffer->content;
12234 inputStream->end =
12235 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
12236
12237 inputPush(ctxt, inputStream);
12238
12239 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
12240 (ctxt->input->buf != NULL)) {
12241 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
12242 int cur = ctxt->input->cur - ctxt->input->base;
12243
12244 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
12245
12246 ctxt->input->base = ctxt->input->buf->buffer->content + base;
12247 ctxt->input->cur = ctxt->input->base + cur;
12248 ctxt->input->end =
12249 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->
12250 use];
12251#ifdef DEBUG_PUSH
12252 xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
12253#endif
12254 }
12255
12256 if (encoding != NULL) {
12257 xmlCharEncodingHandlerPtr hdlr;
12258
12259 hdlr = xmlFindCharEncodingHandler(encoding);
12260 if (hdlr != NULL) {
12261 xmlSwitchToEncoding(ctxt, hdlr);
12262 } else {
12263 xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
12264 "Unsupported encoding %s\n", BAD_CAST encoding);
12265 }
12266 } else if (enc != XML_CHAR_ENCODING_NONE) {
12267 xmlSwitchEncoding(ctxt, enc);
12268 }
12269
12270 return(0);
12271}
12272
12273/**
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012274 * xmlCtxtUseOptions:
12275 * @ctxt: an XML parser context
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012276 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012277 *
12278 * Applies the options to the parser context
12279 *
12280 * Returns 0 in case of success, the set of unknown or unimplemented options
12281 * in case of error.
12282 */
12283int
12284xmlCtxtUseOptions(xmlParserCtxtPtr ctxt, int options)
12285{
12286 if (options & XML_PARSE_RECOVER) {
12287 ctxt->recovery = 1;
12288 options -= XML_PARSE_RECOVER;
12289 } else
12290 ctxt->recovery = 0;
12291 if (options & XML_PARSE_DTDLOAD) {
12292 ctxt->loadsubset = XML_DETECT_IDS;
12293 options -= XML_PARSE_DTDLOAD;
12294 } else
12295 ctxt->loadsubset = 0;
12296 if (options & XML_PARSE_DTDATTR) {
12297 ctxt->loadsubset |= XML_COMPLETE_ATTRS;
12298 options -= XML_PARSE_DTDATTR;
12299 }
12300 if (options & XML_PARSE_NOENT) {
12301 ctxt->replaceEntities = 1;
12302 /* ctxt->loadsubset |= XML_DETECT_IDS; */
12303 options -= XML_PARSE_NOENT;
12304 } else
12305 ctxt->replaceEntities = 0;
12306 if (options & XML_PARSE_NOWARNING) {
12307 ctxt->sax->warning = NULL;
12308 options -= XML_PARSE_NOWARNING;
12309 }
12310 if (options & XML_PARSE_NOERROR) {
12311 ctxt->sax->error = NULL;
12312 ctxt->sax->fatalError = NULL;
12313 options -= XML_PARSE_NOERROR;
12314 }
12315 if (options & XML_PARSE_PEDANTIC) {
12316 ctxt->pedantic = 1;
12317 options -= XML_PARSE_PEDANTIC;
12318 } else
12319 ctxt->pedantic = 0;
12320 if (options & XML_PARSE_NOBLANKS) {
12321 ctxt->keepBlanks = 0;
12322 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
12323 options -= XML_PARSE_NOBLANKS;
12324 } else
12325 ctxt->keepBlanks = 1;
12326 if (options & XML_PARSE_DTDVALID) {
12327 ctxt->validate = 1;
12328 if (options & XML_PARSE_NOWARNING)
12329 ctxt->vctxt.warning = NULL;
12330 if (options & XML_PARSE_NOERROR)
12331 ctxt->vctxt.error = NULL;
12332 options -= XML_PARSE_DTDVALID;
12333 } else
12334 ctxt->validate = 0;
Daniel Veillard81273902003-09-30 00:43:48 +000012335#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012336 if (options & XML_PARSE_SAX1) {
12337 ctxt->sax->startElement = xmlSAX2StartElement;
12338 ctxt->sax->endElement = xmlSAX2EndElement;
12339 ctxt->sax->startElementNs = NULL;
12340 ctxt->sax->endElementNs = NULL;
12341 ctxt->sax->initialized = 1;
12342 options -= XML_PARSE_SAX1;
12343 }
Daniel Veillard81273902003-09-30 00:43:48 +000012344#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012345 if (options & XML_PARSE_NODICT) {
12346 ctxt->dictNames = 0;
12347 options -= XML_PARSE_NODICT;
12348 } else {
12349 ctxt->dictNames = 1;
12350 }
Daniel Veillarddca8cc72003-09-26 13:53:14 +000012351 if (options & XML_PARSE_NOCDATA) {
12352 ctxt->sax->cdataBlock = NULL;
12353 options -= XML_PARSE_NOCDATA;
12354 }
12355 if (options & XML_PARSE_NSCLEAN) {
12356 ctxt->options |= XML_PARSE_NSCLEAN;
12357 options -= XML_PARSE_NSCLEAN;
12358 }
Daniel Veillard61b93382003-11-03 14:28:31 +000012359 if (options & XML_PARSE_NONET) {
12360 ctxt->options |= XML_PARSE_NONET;
12361 options -= XML_PARSE_NONET;
12362 }
Daniel Veillard7ec29972003-10-31 14:36:36 +000012363 ctxt->linenumbers = 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012364 return (options);
12365}
12366
12367/**
12368 * xmlDoRead:
12369 * @ctxt: an XML parser context
Daniel Veillard60942de2003-09-25 21:05:58 +000012370 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012371 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012372 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012373 * @reuse: keep the context for reuse
12374 *
12375 * Common front-end for the xmlRead functions
12376 *
12377 * Returns the resulting document tree or NULL
12378 */
12379static xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012380xmlDoRead(xmlParserCtxtPtr ctxt, const char *URL, const char *encoding,
12381 int options, int reuse)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012382{
12383 xmlDocPtr ret;
12384
12385 xmlCtxtUseOptions(ctxt, options);
12386 if (encoding != NULL) {
12387 xmlCharEncodingHandlerPtr hdlr;
12388
12389 hdlr = xmlFindCharEncodingHandler(encoding);
12390 if (hdlr != NULL)
12391 xmlSwitchToEncoding(ctxt, hdlr);
12392 }
Daniel Veillard60942de2003-09-25 21:05:58 +000012393 if ((URL != NULL) && (ctxt->input != NULL) &&
12394 (ctxt->input->filename == NULL))
12395 ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012396 xmlParseDocument(ctxt);
12397 if ((ctxt->wellFormed) || ctxt->recovery)
12398 ret = ctxt->myDoc;
12399 else {
12400 ret = NULL;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012401 if (ctxt->myDoc != NULL) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012402 xmlFreeDoc(ctxt->myDoc);
12403 }
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012404 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012405 ctxt->myDoc = NULL;
12406 if (!reuse) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012407 xmlFreeParserCtxt(ctxt);
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012408 }
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012409
12410 return (ret);
12411}
12412
12413/**
12414 * xmlReadDoc:
12415 * @cur: a pointer to a zero terminated string
Daniel Veillard60942de2003-09-25 21:05:58 +000012416 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012417 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012418 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012419 *
12420 * parse an XML in-memory document and build a tree.
12421 *
12422 * Returns the resulting document tree
12423 */
12424xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012425xmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012426{
12427 xmlParserCtxtPtr ctxt;
12428
12429 if (cur == NULL)
12430 return (NULL);
12431
12432 ctxt = xmlCreateDocParserCtxt(cur);
12433 if (ctxt == NULL)
12434 return (NULL);
Daniel Veillard60942de2003-09-25 21:05:58 +000012435 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012436}
12437
12438/**
12439 * xmlReadFile:
12440 * @filename: a file or URL
12441 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012442 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012443 *
12444 * parse an XML file from the filesystem or the network.
12445 *
12446 * Returns the resulting document tree
12447 */
12448xmlDocPtr
12449xmlReadFile(const char *filename, const char *encoding, int options)
12450{
12451 xmlParserCtxtPtr ctxt;
12452
Daniel Veillard61b93382003-11-03 14:28:31 +000012453 ctxt = xmlCreateURLParserCtxt(filename, options);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012454 if (ctxt == NULL)
12455 return (NULL);
Daniel Veillard60942de2003-09-25 21:05:58 +000012456 return (xmlDoRead(ctxt, NULL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012457}
12458
12459/**
12460 * xmlReadMemory:
12461 * @buffer: a pointer to a char array
12462 * @size: the size of the array
Daniel Veillard60942de2003-09-25 21:05:58 +000012463 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012464 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012465 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012466 *
12467 * parse an XML in-memory document and build a tree.
12468 *
12469 * Returns the resulting document tree
12470 */
12471xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012472xmlReadMemory(const char *buffer, int size, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012473{
12474 xmlParserCtxtPtr ctxt;
12475
12476 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
12477 if (ctxt == NULL)
12478 return (NULL);
Daniel Veillard60942de2003-09-25 21:05:58 +000012479 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012480}
12481
12482/**
12483 * xmlReadFd:
12484 * @fd: an open file descriptor
Daniel Veillard60942de2003-09-25 21:05:58 +000012485 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012486 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012487 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012488 *
12489 * parse an XML from a file descriptor and build a tree.
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012490 * NOTE that the file descriptor will not be closed when the
12491 * reader is closed or reset.
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012492 *
12493 * Returns the resulting document tree
12494 */
12495xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012496xmlReadFd(int fd, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012497{
12498 xmlParserCtxtPtr ctxt;
12499 xmlParserInputBufferPtr input;
12500 xmlParserInputPtr stream;
12501
12502 if (fd < 0)
12503 return (NULL);
12504
12505 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
12506 if (input == NULL)
12507 return (NULL);
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012508 input->closecallback = NULL;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012509 ctxt = xmlNewParserCtxt();
12510 if (ctxt == NULL) {
12511 xmlFreeParserInputBuffer(input);
12512 return (NULL);
12513 }
12514 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12515 if (stream == NULL) {
12516 xmlFreeParserInputBuffer(input);
12517 xmlFreeParserCtxt(ctxt);
12518 return (NULL);
12519 }
12520 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012521 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012522}
12523
12524/**
12525 * xmlReadIO:
12526 * @ioread: an I/O read function
12527 * @ioclose: an I/O close function
12528 * @ioctx: an I/O handler
Daniel Veillard60942de2003-09-25 21:05:58 +000012529 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012530 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012531 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012532 *
12533 * parse an XML document from I/O functions and source and build a tree.
12534 *
12535 * Returns the resulting document tree
12536 */
12537xmlDocPtr
12538xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
Daniel Veillard60942de2003-09-25 21:05:58 +000012539 void *ioctx, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012540{
12541 xmlParserCtxtPtr ctxt;
12542 xmlParserInputBufferPtr input;
12543 xmlParserInputPtr stream;
12544
12545 if (ioread == NULL)
12546 return (NULL);
12547
12548 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
12549 XML_CHAR_ENCODING_NONE);
12550 if (input == NULL)
12551 return (NULL);
12552 ctxt = xmlNewParserCtxt();
12553 if (ctxt == NULL) {
12554 xmlFreeParserInputBuffer(input);
12555 return (NULL);
12556 }
12557 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12558 if (stream == NULL) {
12559 xmlFreeParserInputBuffer(input);
12560 xmlFreeParserCtxt(ctxt);
12561 return (NULL);
12562 }
12563 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012564 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012565}
12566
12567/**
12568 * xmlCtxtReadDoc:
12569 * @ctxt: an XML parser context
12570 * @cur: a pointer to a zero terminated string
Daniel Veillard60942de2003-09-25 21:05:58 +000012571 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012572 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012573 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012574 *
12575 * parse an XML in-memory document and build a tree.
12576 * This reuses the existing @ctxt parser context
12577 *
12578 * Returns the resulting document tree
12579 */
12580xmlDocPtr
12581xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur,
Daniel Veillard60942de2003-09-25 21:05:58 +000012582 const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012583{
12584 xmlParserInputPtr stream;
12585
12586 if (cur == NULL)
12587 return (NULL);
12588 if (ctxt == NULL)
12589 return (NULL);
12590
12591 xmlCtxtReset(ctxt);
12592
12593 stream = xmlNewStringInputStream(ctxt, cur);
12594 if (stream == NULL) {
12595 return (NULL);
12596 }
12597 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012598 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012599}
12600
12601/**
12602 * xmlCtxtReadFile:
12603 * @ctxt: an XML parser context
12604 * @filename: a file or URL
12605 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012606 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012607 *
12608 * parse an XML file from the filesystem or the network.
12609 * This reuses the existing @ctxt parser context
12610 *
12611 * Returns the resulting document tree
12612 */
12613xmlDocPtr
12614xmlCtxtReadFile(xmlParserCtxtPtr ctxt, const char *filename,
12615 const char *encoding, int options)
12616{
12617 xmlParserInputPtr stream;
12618
12619 if (filename == NULL)
12620 return (NULL);
12621 if (ctxt == NULL)
12622 return (NULL);
12623
12624 xmlCtxtReset(ctxt);
12625
12626 stream = xmlNewInputFromFile(ctxt, filename);
12627 if (stream == NULL) {
12628 return (NULL);
12629 }
12630 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012631 return (xmlDoRead(ctxt, NULL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012632}
12633
12634/**
12635 * xmlCtxtReadMemory:
12636 * @ctxt: an XML parser context
12637 * @buffer: a pointer to a char array
12638 * @size: the size of the array
Daniel Veillard60942de2003-09-25 21:05:58 +000012639 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012640 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012641 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012642 *
12643 * parse an XML in-memory document and build a tree.
12644 * This reuses the existing @ctxt parser context
12645 *
12646 * Returns the resulting document tree
12647 */
12648xmlDocPtr
12649xmlCtxtReadMemory(xmlParserCtxtPtr ctxt, const char *buffer, int size,
Daniel Veillard60942de2003-09-25 21:05:58 +000012650 const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012651{
12652 xmlParserInputBufferPtr input;
12653 xmlParserInputPtr stream;
12654
12655 if (ctxt == NULL)
12656 return (NULL);
12657 if (buffer == NULL)
12658 return (NULL);
12659
12660 xmlCtxtReset(ctxt);
12661
12662 input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
12663 if (input == NULL) {
12664 return(NULL);
12665 }
12666
12667 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12668 if (stream == NULL) {
12669 xmlFreeParserInputBuffer(input);
12670 return(NULL);
12671 }
12672
12673 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012674 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012675}
12676
12677/**
12678 * xmlCtxtReadFd:
12679 * @ctxt: an XML parser context
12680 * @fd: an open file descriptor
Daniel Veillard60942de2003-09-25 21:05:58 +000012681 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012682 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012683 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012684 *
12685 * parse an XML from a file descriptor and build a tree.
12686 * This reuses the existing @ctxt parser context
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012687 * NOTE that the file descriptor will not be closed when the
12688 * reader is closed or reset.
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012689 *
12690 * Returns the resulting document tree
12691 */
12692xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012693xmlCtxtReadFd(xmlParserCtxtPtr ctxt, int fd,
12694 const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012695{
12696 xmlParserInputBufferPtr input;
12697 xmlParserInputPtr stream;
12698
12699 if (fd < 0)
12700 return (NULL);
12701 if (ctxt == NULL)
12702 return (NULL);
12703
12704 xmlCtxtReset(ctxt);
12705
12706
12707 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
12708 if (input == NULL)
12709 return (NULL);
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012710 input->closecallback = NULL;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012711 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12712 if (stream == NULL) {
12713 xmlFreeParserInputBuffer(input);
12714 return (NULL);
12715 }
12716 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012717 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012718}
12719
12720/**
12721 * xmlCtxtReadIO:
12722 * @ctxt: an XML parser context
12723 * @ioread: an I/O read function
12724 * @ioclose: an I/O close function
12725 * @ioctx: an I/O handler
Daniel Veillard60942de2003-09-25 21:05:58 +000012726 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012727 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012728 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012729 *
12730 * parse an XML document from I/O functions and source and build a tree.
12731 * This reuses the existing @ctxt parser context
12732 *
12733 * Returns the resulting document tree
12734 */
12735xmlDocPtr
12736xmlCtxtReadIO(xmlParserCtxtPtr ctxt, xmlInputReadCallback ioread,
12737 xmlInputCloseCallback ioclose, void *ioctx,
Daniel Veillard60942de2003-09-25 21:05:58 +000012738 const char *URL,
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012739 const char *encoding, int options)
12740{
12741 xmlParserInputBufferPtr input;
12742 xmlParserInputPtr stream;
12743
12744 if (ioread == NULL)
12745 return (NULL);
12746 if (ctxt == NULL)
12747 return (NULL);
12748
12749 xmlCtxtReset(ctxt);
12750
12751 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
12752 XML_CHAR_ENCODING_NONE);
12753 if (input == NULL)
12754 return (NULL);
12755 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12756 if (stream == NULL) {
12757 xmlFreeParserInputBuffer(input);
12758 return (NULL);
12759 }
12760 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012761 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012762}