blob: 4810e221c94a5528af17d03b3652cb79868a4f62 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * parser.c : an XML 1.0 parser, namespaces and validity support are mostly
3 * implemented on top of the SAX interfaces
4 *
5 * References:
6 * The XML specification:
7 * http://www.w3.org/TR/REC-xml
8 * Original 1.0 version:
9 * http://www.w3.org/TR/1998/REC-xml-19980210
10 * XML second edition working draft
11 * http://www.w3.org/TR/2000/WD-xml-2e-20000814
12 *
13 * Okay this is a big file, the parser core is around 7000 lines, then it
14 * is followed by the progressive parser top routines, then the various
Daniel Veillardcbaf3992001-12-31 16:16:02 +000015 * high level APIs to call the parser and a few miscellaneous functions.
Owen Taylor3473f882001-02-23 17:55:21 +000016 * A number of helper functions and deprecated ones have been moved to
17 * parserInternals.c to reduce this file size.
18 * As much as possible the functions are associated with their relative
19 * production in the XML specification. A few productions defining the
20 * different ranges of character are actually implanted either in
21 * parserInternals.h or parserInternals.c
22 * The DOM tree build is realized from the default SAX callbacks in
23 * the module SAX.c.
24 * The routines doing the validation checks are in valid.c and called either
Daniel Veillardcbaf3992001-12-31 16:16:02 +000025 * from the SAX callbacks or as standalone functions using a preparsed
Owen Taylor3473f882001-02-23 17:55:21 +000026 * document.
27 *
28 * See Copyright for the status of this software.
29 *
Daniel Veillardc5d64342001-06-24 12:13:24 +000030 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +000031 */
32
Daniel Veillard34ce8be2002-03-18 19:37:11 +000033#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000034#include "libxml.h"
35
Daniel Veillard3c5ed912002-01-08 10:36:16 +000036#if defined(WIN32) && !defined (__CYGWIN__)
Owen Taylor3473f882001-02-23 17:55:21 +000037#define XML_DIR_SEP '\\'
38#else
Owen Taylor3473f882001-02-23 17:55:21 +000039#define XML_DIR_SEP '/'
40#endif
41
Owen Taylor3473f882001-02-23 17:55:21 +000042#include <stdlib.h>
43#include <string.h>
Aleksey Sanine7acf432003-10-02 20:05:27 +000044#include <stdarg.h>
Owen Taylor3473f882001-02-23 17:55:21 +000045#include <libxml/xmlmemory.h>
Daniel Veillardd0463562001-10-13 09:15:48 +000046#include <libxml/threads.h>
47#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000048#include <libxml/tree.h>
49#include <libxml/parser.h>
50#include <libxml/parserInternals.h>
51#include <libxml/valid.h>
52#include <libxml/entities.h>
53#include <libxml/xmlerror.h>
54#include <libxml/encoding.h>
55#include <libxml/xmlIO.h>
56#include <libxml/uri.h>
Daniel Veillard5d90b6c2001-08-22 14:29:45 +000057#ifdef LIBXML_CATALOG_ENABLED
58#include <libxml/catalog.h>
59#endif
Owen Taylor3473f882001-02-23 17:55:21 +000060
61#ifdef HAVE_CTYPE_H
62#include <ctype.h>
63#endif
64#ifdef HAVE_STDLIB_H
65#include <stdlib.h>
66#endif
67#ifdef HAVE_SYS_STAT_H
68#include <sys/stat.h>
69#endif
70#ifdef HAVE_FCNTL_H
71#include <fcntl.h>
72#endif
73#ifdef HAVE_UNISTD_H
74#include <unistd.h>
75#endif
76#ifdef HAVE_ZLIB_H
77#include <zlib.h>
78#endif
79
Daniel Veillard3b2e4e12003-02-03 08:52:58 +000080/**
Daniel Veillard4aede2e2003-10-17 12:43:59 +000081 * xmlParserMaxDepth:
Daniel Veillard3b2e4e12003-02-03 08:52:58 +000082 *
83 * arbitrary depth limit for the XML documents that we allow to
84 * process. This is not a limitation of the parser but a safety
85 * boundary feature.
86 */
Daniel Veillard4aede2e2003-10-17 12:43:59 +000087unsigned int xmlParserMaxDepth = 1024;
Owen Taylor3473f882001-02-23 17:55:21 +000088
Daniel Veillard0fb18932003-09-07 09:14:37 +000089#define SAX2 1
90
Daniel Veillard21a0f912001-02-25 19:54:14 +000091#define XML_PARSER_BIG_BUFFER_SIZE 300
Owen Taylor3473f882001-02-23 17:55:21 +000092#define XML_PARSER_BUFFER_SIZE 100
93
Daniel Veillard5997aca2002-03-18 18:36:20 +000094#define SAX_COMPAT_MODE BAD_CAST "SAX compatibility mode document"
95
Owen Taylor3473f882001-02-23 17:55:21 +000096/*
Owen Taylor3473f882001-02-23 17:55:21 +000097 * List of XML prefixed PI allowed by W3C specs
98 */
99
Daniel Veillardb44025c2001-10-11 22:55:55 +0000100static const char *xmlW3CPIs[] = {
Owen Taylor3473f882001-02-23 17:55:21 +0000101 "xml-stylesheet",
102 NULL
103};
104
Daniel Veillarda07050d2003-10-19 14:46:32 +0000105
Owen Taylor3473f882001-02-23 17:55:21 +0000106/* DEPR void xmlParserHandleReference(xmlParserCtxtPtr ctxt); */
Owen Taylor3473f882001-02-23 17:55:21 +0000107xmlEntityPtr xmlParseStringPEReference(xmlParserCtxtPtr ctxt,
108 const xmlChar **str);
109
Daniel Veillard7d515752003-09-26 19:12:37 +0000110static xmlParserErrors
Daniel Veillarda97a19b2001-05-20 13:19:52 +0000111xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt,
112 xmlSAXHandlerPtr sax,
Daniel Veillard257d9102001-05-08 10:41:44 +0000113 void *user_data, int depth, const xmlChar *URL,
Daniel Veillarda97a19b2001-05-20 13:19:52 +0000114 const xmlChar *ID, xmlNodePtr *list);
Owen Taylor3473f882001-02-23 17:55:21 +0000115
Daniel Veillard81273902003-09-30 00:43:48 +0000116#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +0000117static void
118xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode,
119 xmlNodePtr lastNode);
Daniel Veillard81273902003-09-30 00:43:48 +0000120#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillard8107a222002-01-13 14:10:10 +0000121
Daniel Veillard7d515752003-09-26 19:12:37 +0000122static xmlParserErrors
Daniel Veillard328f48c2002-11-15 15:24:34 +0000123xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt,
124 const xmlChar *string, void *user_data, xmlNodePtr *lst);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000125
126/************************************************************************
127 * *
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000128 * Some factorized error routines *
129 * *
130 ************************************************************************/
131
132/**
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000133 * xmlErrAttributeDup:
134 * @ctxt: an XML parser context
135 * @prefix: the attribute prefix
136 * @localname: the attribute localname
137 *
138 * Handle a redefinition of attribute error
139 */
140static void
141xmlErrAttributeDup(xmlParserCtxtPtr ctxt, const xmlChar * prefix,
142 const xmlChar * localname)
143{
Daniel Veillard157fee02003-10-31 10:36:03 +0000144 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
145 (ctxt->instate == XML_PARSER_EOF))
146 return;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000147 ctxt->errNo = XML_ERR_ATTRIBUTE_REDEFINED;
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000148 if (prefix == NULL)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000149 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000150 ctxt->errNo, XML_ERR_FATAL, NULL, 0,
151 (const char *) localname, NULL, NULL, 0, 0,
152 "Attribute %s redefined\n", localname);
Daniel Veillard2b8c4a12003-10-02 22:28:19 +0000153 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000154 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000155 ctxt->errNo, XML_ERR_FATAL, NULL, 0,
156 (const char *) prefix, (const char *) localname,
157 NULL, 0, 0, "Attribute %s:%s redefined\n", prefix,
158 localname);
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000159 ctxt->wellFormed = 0;
160 if (ctxt->recovery == 0)
161 ctxt->disableSAX = 1;
162}
163
164/**
165 * xmlFatalErr:
166 * @ctxt: an XML parser context
167 * @error: the error number
168 * @extra: extra information string
169 *
170 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
171 */
172static void
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000173xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info)
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000174{
175 const char *errmsg;
176
Daniel Veillard157fee02003-10-31 10:36:03 +0000177 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
178 (ctxt->instate == XML_PARSER_EOF))
179 return;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000180 switch (error) {
181 case XML_ERR_INVALID_HEX_CHARREF:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000182 errmsg = "CharRef: invalid hexadecimal value\n";
183 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000184 case XML_ERR_INVALID_DEC_CHARREF:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000185 errmsg = "CharRef: invalid decimal value\n";
186 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000187 case XML_ERR_INVALID_CHARREF:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000188 errmsg = "CharRef: invalid value\n";
189 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000190 case XML_ERR_INTERNAL_ERROR:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000191 errmsg = "internal error";
192 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000193 case XML_ERR_PEREF_AT_EOF:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000194 errmsg = "PEReference at end of document\n";
195 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000196 case XML_ERR_PEREF_IN_PROLOG:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000197 errmsg = "PEReference in prolog\n";
198 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000199 case XML_ERR_PEREF_IN_EPILOG:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000200 errmsg = "PEReference in epilog\n";
201 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000202 case XML_ERR_PEREF_NO_NAME:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000203 errmsg = "PEReference: no name\n";
204 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000205 case XML_ERR_PEREF_SEMICOL_MISSING:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000206 errmsg = "PEReference: expecting ';'\n";
207 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000208 case XML_ERR_ENTITY_LOOP:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000209 errmsg = "Detected an entity reference loop\n";
210 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000211 case XML_ERR_ENTITY_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000212 errmsg = "EntityValue: \" or ' expected\n";
213 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000214 case XML_ERR_ENTITY_PE_INTERNAL:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000215 errmsg = "PEReferences forbidden in internal subset\n";
216 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000217 case XML_ERR_ENTITY_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000218 errmsg = "EntityValue: \" or ' expected\n";
219 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000220 case XML_ERR_ATTRIBUTE_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000221 errmsg = "AttValue: \" or ' expected\n";
222 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000223 case XML_ERR_LT_IN_ATTRIBUTE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000224 errmsg = "Unescaped '<' not allowed in attributes values\n";
225 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000226 case XML_ERR_LITERAL_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000227 errmsg = "SystemLiteral \" or ' expected\n";
228 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000229 case XML_ERR_LITERAL_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000230 errmsg = "Unfinished System or Public ID \" or ' expected\n";
231 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000232 case XML_ERR_MISPLACED_CDATA_END:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000233 errmsg = "Sequence ']]>' not allowed in content\n";
234 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000235 case XML_ERR_URI_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000236 errmsg = "SYSTEM or PUBLIC, the URI is missing\n";
237 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000238 case XML_ERR_PUBID_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000239 errmsg = "PUBLIC, the Public Identifier is missing\n";
240 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000241 case XML_ERR_HYPHEN_IN_COMMENT:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000242 errmsg = "Comment must not contain '--' (double-hyphen)\n";
243 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000244 case XML_ERR_PI_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000245 errmsg = "xmlParsePI : no target name\n";
246 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000247 case XML_ERR_RESERVED_XML_NAME:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000248 errmsg = "Invalid PI name\n";
249 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000250 case XML_ERR_NOTATION_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000251 errmsg = "NOTATION: Name expected here\n";
252 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000253 case XML_ERR_NOTATION_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000254 errmsg = "'>' required to close NOTATION declaration\n";
255 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000256 case XML_ERR_VALUE_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000257 errmsg = "Entity value required\n";
258 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000259 case XML_ERR_URI_FRAGMENT:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000260 errmsg = "Fragment not allowed";
261 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000262 case XML_ERR_ATTLIST_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000263 errmsg = "'(' required to start ATTLIST enumeration\n";
264 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000265 case XML_ERR_NMTOKEN_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000266 errmsg = "NmToken expected in ATTLIST enumeration\n";
267 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000268 case XML_ERR_ATTLIST_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000269 errmsg = "')' required to finish ATTLIST enumeration\n";
270 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000271 case XML_ERR_MIXED_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000272 errmsg = "MixedContentDecl : '|' or ')*' expected\n";
273 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000274 case XML_ERR_PCDATA_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000275 errmsg = "MixedContentDecl : '#PCDATA' expected\n";
276 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000277 case XML_ERR_ELEMCONTENT_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000278 errmsg = "ContentDecl : Name or '(' expected\n";
279 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000280 case XML_ERR_ELEMCONTENT_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000281 errmsg = "ContentDecl : ',' '|' or ')' expected\n";
282 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000283 case XML_ERR_PEREF_IN_INT_SUBSET:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000284 errmsg =
285 "PEReference: forbidden within markup decl in internal subset\n";
286 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000287 case XML_ERR_GT_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000288 errmsg = "expected '>'\n";
289 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000290 case XML_ERR_CONDSEC_INVALID:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000291 errmsg = "XML conditional section '[' expected\n";
292 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000293 case XML_ERR_EXT_SUBSET_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000294 errmsg = "Content error in the external subset\n";
295 break;
296 case XML_ERR_CONDSEC_INVALID_KEYWORD:
297 errmsg =
298 "conditional section INCLUDE or IGNORE keyword expected\n";
299 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000300 case XML_ERR_CONDSEC_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000301 errmsg = "XML conditional section not closed\n";
302 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000303 case XML_ERR_XMLDECL_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000304 errmsg = "Text declaration '<?xml' required\n";
305 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000306 case XML_ERR_XMLDECL_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000307 errmsg = "parsing XML declaration: '?>' expected\n";
308 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000309 case XML_ERR_EXT_ENTITY_STANDALONE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000310 errmsg = "external parsed entities cannot be standalone\n";
311 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000312 case XML_ERR_ENTITYREF_SEMICOL_MISSING:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000313 errmsg = "EntityRef: expecting ';'\n";
314 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000315 case XML_ERR_DOCTYPE_NOT_FINISHED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000316 errmsg = "DOCTYPE improperly terminated\n";
317 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000318 case XML_ERR_LTSLASH_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000319 errmsg = "EndTag: '</' not found\n";
320 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000321 case XML_ERR_EQUAL_REQUIRED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000322 errmsg = "expected '='\n";
323 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000324 case XML_ERR_STRING_NOT_CLOSED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000325 errmsg = "String not closed expecting \" or '\n";
326 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000327 case XML_ERR_STRING_NOT_STARTED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000328 errmsg = "String not started expecting ' or \"\n";
329 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000330 case XML_ERR_ENCODING_NAME:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000331 errmsg = "Invalid XML encoding name\n";
332 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000333 case XML_ERR_STANDALONE_VALUE:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000334 errmsg = "standalone accepts only 'yes' or 'no'\n";
335 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000336 case XML_ERR_DOCUMENT_EMPTY:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000337 errmsg = "Document is empty\n";
338 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000339 case XML_ERR_DOCUMENT_END:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000340 errmsg = "Extra content at the end of the document\n";
341 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000342 case XML_ERR_NOT_WELL_BALANCED:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000343 errmsg = "chunk is not well balanced\n";
344 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000345 case XML_ERR_EXTRA_CONTENT:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000346 errmsg = "extra content at the end of well balanced chunk\n";
347 break;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000348 case XML_ERR_VERSION_MISSING:
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000349 errmsg = "Malformed declaration expecting version\n";
350 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000351#if 0
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000352 case:
353 errmsg = "\n";
354 break;
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000355#endif
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000356 default:
357 errmsg = "Unregistered error message\n";
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000358 }
359 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000360 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000361 XML_ERR_FATAL, NULL, 0, info, NULL, NULL, 0, 0, errmsg,
362 info);
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000363 ctxt->wellFormed = 0;
364 if (ctxt->recovery == 0)
365 ctxt->disableSAX = 1;
366}
367
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000368/**
369 * xmlFatalErrMsg:
370 * @ctxt: an XML parser context
371 * @error: the error number
372 * @msg: the error message
373 *
374 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
375 */
376static void
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000377xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
378 const char *msg)
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000379{
Daniel Veillard157fee02003-10-31 10:36:03 +0000380 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
381 (ctxt->instate == XML_PARSER_EOF))
382 return;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000383 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000384 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000385 XML_ERR_FATAL, NULL, 0, NULL, NULL, NULL, 0, 0, msg);
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000386 ctxt->wellFormed = 0;
387 if (ctxt->recovery == 0)
388 ctxt->disableSAX = 1;
389}
390
391/**
Daniel Veillard24eb9782003-10-04 21:08:09 +0000392 * xmlWarningMsg:
393 * @ctxt: an XML parser context
394 * @error: the error number
395 * @msg: the error message
396 * @str1: extra data
397 * @str2: extra data
398 *
399 * Handle a warning.
400 */
401static void
402xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
403 const char *msg, const xmlChar *str1, const xmlChar *str2)
404{
Daniel Veillard9bcc7c52003-10-11 10:57:05 +0000405 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardc790bf42003-10-11 10:50:10 +0000406
Daniel Veillard157fee02003-10-31 10:36:03 +0000407 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
408 (ctxt->instate == XML_PARSER_EOF))
409 return;
Daniel Veillard24eb9782003-10-04 21:08:09 +0000410 ctxt->errNo = error;
Daniel Veillardc790bf42003-10-11 10:50:10 +0000411 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
Daniel Veillard9bcc7c52003-10-11 10:57:05 +0000412 schannel = ctxt->sax->serror;
413 __xmlRaiseError(schannel,
Daniel Veillard659e71e2003-10-10 14:10:40 +0000414 (ctxt->sax) ? ctxt->sax->warning : NULL,
415 ctxt->userData,
Daniel Veillard24eb9782003-10-04 21:08:09 +0000416 ctxt, NULL, XML_FROM_PARSER, error,
417 XML_ERR_WARNING, NULL, 0,
418 (const char *) str1, (const char *) str2, NULL, 0, 0,
419 msg, (const char *) str1, (const char *) str2);
420}
421
422/**
423 * xmlValidityError:
424 * @ctxt: an XML parser context
425 * @error: the error number
426 * @msg: the error message
427 * @str1: extra data
428 *
Daniel Veillardf88d8cf2003-12-08 10:25:02 +0000429 * Handle a validity error.
Daniel Veillard24eb9782003-10-04 21:08:09 +0000430 */
431static void
432xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error,
433 const char *msg, const xmlChar *str1)
434{
Daniel Veillard9bcc7c52003-10-11 10:57:05 +0000435 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard157fee02003-10-31 10:36:03 +0000436
437 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
438 (ctxt->instate == XML_PARSER_EOF))
439 return;
Daniel Veillard24eb9782003-10-04 21:08:09 +0000440 ctxt->errNo = error;
Daniel Veillardc790bf42003-10-11 10:50:10 +0000441 if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
Daniel Veillard9bcc7c52003-10-11 10:57:05 +0000442 schannel = ctxt->sax->serror;
Daniel Veillardc790bf42003-10-11 10:50:10 +0000443 __xmlRaiseError(schannel,
Daniel Veillard659e71e2003-10-10 14:10:40 +0000444 ctxt->vctxt.error, ctxt->vctxt.userData,
Daniel Veillard24eb9782003-10-04 21:08:09 +0000445 ctxt, NULL, XML_FROM_DTD, error,
446 XML_ERR_ERROR, NULL, 0, (const char *) str1,
447 NULL, NULL, 0, 0,
448 msg, (const char *) str1);
449 ctxt->valid = 0;
450}
451
452/**
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000453 * xmlFatalErrMsgInt:
454 * @ctxt: an XML parser context
455 * @error: the error number
456 * @msg: the error message
457 * @val: an integer value
458 *
459 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
460 */
461static void
462xmlFatalErrMsgInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000463 const char *msg, int val)
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000464{
Daniel Veillard157fee02003-10-31 10:36:03 +0000465 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
466 (ctxt->instate == XML_PARSER_EOF))
467 return;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000468 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000469 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000470 ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
471 NULL, 0, NULL, NULL, NULL, val, 0, msg, val);
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000472 ctxt->wellFormed = 0;
473 if (ctxt->recovery == 0)
474 ctxt->disableSAX = 1;
475}
476
477/**
Daniel Veillardf403d292003-10-05 13:51:35 +0000478 * xmlFatalErrMsgStrIntStr:
479 * @ctxt: an XML parser context
480 * @error: the error number
481 * @msg: the error message
482 * @str1: an string info
483 * @val: an integer value
484 * @str2: an string info
485 *
486 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
487 */
488static void
489xmlFatalErrMsgStrIntStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
490 const char *msg, const xmlChar *str1, int val,
491 const xmlChar *str2)
492{
Daniel Veillard157fee02003-10-31 10:36:03 +0000493 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
494 (ctxt->instate == XML_PARSER_EOF))
495 return;
Daniel Veillardf403d292003-10-05 13:51:35 +0000496 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000497 __xmlRaiseError(NULL, NULL, NULL,
Daniel Veillardf403d292003-10-05 13:51:35 +0000498 ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
499 NULL, 0, (const char *) str1, (const char *) str2,
500 NULL, val, 0, msg, str1, val, str2);
501 ctxt->wellFormed = 0;
502 if (ctxt->recovery == 0)
503 ctxt->disableSAX = 1;
504}
505
506/**
Daniel Veillardbc92eca2003-09-15 09:48:06 +0000507 * xmlFatalErrMsgStr:
508 * @ctxt: an XML parser context
509 * @error: the error number
510 * @msg: the error message
511 * @val: a string value
512 *
513 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
514 */
515static void
516xmlFatalErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000517 const char *msg, const xmlChar * val)
Daniel Veillardbc92eca2003-09-15 09:48:06 +0000518{
Daniel Veillard157fee02003-10-31 10:36:03 +0000519 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
520 (ctxt->instate == XML_PARSER_EOF))
521 return;
Daniel Veillardbc92eca2003-09-15 09:48:06 +0000522 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000523 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000524 XML_FROM_PARSER, error, XML_ERR_FATAL,
525 NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg,
526 val);
Daniel Veillardbc92eca2003-09-15 09:48:06 +0000527 ctxt->wellFormed = 0;
528 if (ctxt->recovery == 0)
529 ctxt->disableSAX = 1;
530}
531
532/**
Daniel Veillardf403d292003-10-05 13:51:35 +0000533 * xmlErrMsgStr:
534 * @ctxt: an XML parser context
535 * @error: the error number
536 * @msg: the error message
537 * @val: a string value
538 *
539 * Handle a non fatal parser error
540 */
541static void
542xmlErrMsgStr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
543 const char *msg, const xmlChar * val)
544{
Daniel Veillard157fee02003-10-31 10:36:03 +0000545 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
546 (ctxt->instate == XML_PARSER_EOF))
547 return;
Daniel Veillardf403d292003-10-05 13:51:35 +0000548 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000549 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL,
Daniel Veillardf403d292003-10-05 13:51:35 +0000550 XML_FROM_PARSER, error, XML_ERR_ERROR,
551 NULL, 0, (const char *) val, NULL, NULL, 0, 0, msg,
552 val);
553}
554
555/**
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000556 * xmlNsErr:
557 * @ctxt: an XML parser context
558 * @error: the error number
559 * @msg: the message
560 * @info1: extra information string
561 * @info2: extra information string
562 *
563 * Handle a fatal parser error, i.e. violating Well-Formedness constraints
564 */
565static void
566xmlNsErr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
567 const char *msg,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000568 const xmlChar * info1, const xmlChar * info2,
569 const xmlChar * info3)
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000570{
Daniel Veillard157fee02003-10-31 10:36:03 +0000571 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
572 (ctxt->instate == XML_PARSER_EOF))
573 return;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000574 ctxt->errNo = error;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000575 __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
Daniel Veillardbb5abab2003-10-03 22:21:51 +0000576 XML_ERR_ERROR, NULL, 0, (const char *) info1,
577 (const char *) info2, (const char *) info3, 0, 0, msg,
578 info1, info2, info3);
Daniel Veillardbdbe0d42003-09-14 19:56:14 +0000579 ctxt->nsWellFormed = 0;
580}
581
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000582/************************************************************************
583 * *
Daniel Veillarde57ec792003-09-10 10:50:59 +0000584 * SAX2 defaulted attributes handling *
585 * *
586 ************************************************************************/
587
588/**
589 * xmlDetectSAX2:
590 * @ctxt: an XML parser context
591 *
592 * Do the SAX2 detection and specific intialization
593 */
594static void
595xmlDetectSAX2(xmlParserCtxtPtr ctxt) {
596 if (ctxt == NULL) return;
Daniel Veillard81273902003-09-30 00:43:48 +0000597#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +0000598 if ((ctxt->sax) && (ctxt->sax->initialized == XML_SAX2_MAGIC) &&
599 ((ctxt->sax->startElementNs != NULL) ||
600 (ctxt->sax->endElementNs != NULL))) ctxt->sax2 = 1;
Daniel Veillard81273902003-09-30 00:43:48 +0000601#else
602 ctxt->sax2 = 1;
603#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +0000604
605 ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
606 ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
607 ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
608}
609
Daniel Veillarde57ec792003-09-10 10:50:59 +0000610typedef struct _xmlDefAttrs xmlDefAttrs;
611typedef xmlDefAttrs *xmlDefAttrsPtr;
612struct _xmlDefAttrs {
613 int nbAttrs; /* number of defaulted attributes on that element */
614 int maxAttrs; /* the size of the array */
615 const xmlChar *values[4]; /* array of localname/prefix/values */
616};
Daniel Veillarde57ec792003-09-10 10:50:59 +0000617
618/**
619 * xmlAddDefAttrs:
620 * @ctxt: an XML parser context
621 * @fullname: the element fullname
622 * @fullattr: the attribute fullname
623 * @value: the attribute value
624 *
625 * Add a defaulted attribute for an element
626 */
627static void
628xmlAddDefAttrs(xmlParserCtxtPtr ctxt,
629 const xmlChar *fullname,
630 const xmlChar *fullattr,
631 const xmlChar *value) {
632 xmlDefAttrsPtr defaults;
633 int len;
634 const xmlChar *name;
635 const xmlChar *prefix;
636
637 if (ctxt->attsDefault == NULL) {
638 ctxt->attsDefault = xmlHashCreate(10);
639 if (ctxt->attsDefault == NULL)
640 goto mem_error;
641 }
642
643 /*
644 * plit the element name into prefix:localname , the string found
645 * are within the DTD and hen not associated to namespace names.
646 */
647 name = xmlSplitQName3(fullname, &len);
648 if (name == NULL) {
649 name = xmlDictLookup(ctxt->dict, fullname, -1);
650 prefix = NULL;
651 } else {
652 name = xmlDictLookup(ctxt->dict, name, -1);
653 prefix = xmlDictLookup(ctxt->dict, fullname, len);
654 }
655
656 /*
657 * make sure there is some storage
658 */
659 defaults = xmlHashLookup2(ctxt->attsDefault, name, prefix);
660 if (defaults == NULL) {
661 defaults = (xmlDefAttrsPtr) xmlMalloc(sizeof(xmlDefAttrs) +
662 12 * sizeof(const xmlChar *));
663 if (defaults == NULL)
664 goto mem_error;
665 defaults->maxAttrs = 4;
666 defaults->nbAttrs = 0;
667 xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, defaults, NULL);
668 } else if (defaults->nbAttrs >= defaults->maxAttrs) {
669 defaults = (xmlDefAttrsPtr) xmlRealloc(defaults, sizeof(xmlDefAttrs) +
670 (2 * defaults->maxAttrs * 4) * sizeof(const xmlChar *));
671 if (defaults == NULL)
672 goto mem_error;
673 defaults->maxAttrs *= 2;
674 xmlHashUpdateEntry2(ctxt->attsDefault, name, prefix, defaults, NULL);
675 }
676
677 /*
678 * plit the element name into prefix:localname , the string found
679 * are within the DTD and hen not associated to namespace names.
680 */
681 name = xmlSplitQName3(fullattr, &len);
682 if (name == NULL) {
683 name = xmlDictLookup(ctxt->dict, fullattr, -1);
684 prefix = NULL;
685 } else {
686 name = xmlDictLookup(ctxt->dict, name, -1);
687 prefix = xmlDictLookup(ctxt->dict, fullattr, len);
688 }
689
690 defaults->values[4 * defaults->nbAttrs] = name;
691 defaults->values[4 * defaults->nbAttrs + 1] = prefix;
692 /* intern the string and precompute the end */
693 len = xmlStrlen(value);
694 value = xmlDictLookup(ctxt->dict, value, len);
695 defaults->values[4 * defaults->nbAttrs + 2] = value;
696 defaults->values[4 * defaults->nbAttrs + 3] = value + len;
697 defaults->nbAttrs++;
698
699 return;
700
701mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000702 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000703 return;
704}
705
Daniel Veillard8e36e6a2003-09-10 10:50:59 +0000706/**
707 * xmlAddSpecialAttr:
708 * @ctxt: an XML parser context
709 * @fullname: the element fullname
710 * @fullattr: the attribute fullname
711 * @type: the attribute type
712 *
713 * Register that this attribute is not CDATA
714 */
715static void
716xmlAddSpecialAttr(xmlParserCtxtPtr ctxt,
717 const xmlChar *fullname,
718 const xmlChar *fullattr,
719 int type)
720{
721 if (ctxt->attsSpecial == NULL) {
722 ctxt->attsSpecial = xmlHashCreate(10);
723 if (ctxt->attsSpecial == NULL)
724 goto mem_error;
725 }
726
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +0000727 xmlHashAddEntry2(ctxt->attsSpecial, fullname, fullattr,
728 (void *) (long) type);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +0000729 return;
730
731mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000732 xmlErrMemory(ctxt, NULL);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +0000733 return;
734}
735
Daniel Veillard4432df22003-09-28 18:58:27 +0000736/**
737 * xmlCheckLanguageID:
738 * @lang: pointer to the string value
739 *
740 * Checks that the value conforms to the LanguageID production:
741 *
742 * NOTE: this is somewhat deprecated, those productions were removed from
743 * the XML Second edition.
744 *
745 * [33] LanguageID ::= Langcode ('-' Subcode)*
746 * [34] Langcode ::= ISO639Code | IanaCode | UserCode
747 * [35] ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z])
748 * [36] IanaCode ::= ('i' | 'I') '-' ([a-z] | [A-Z])+
749 * [37] UserCode ::= ('x' | 'X') '-' ([a-z] | [A-Z])+
750 * [38] Subcode ::= ([a-z] | [A-Z])+
751 *
752 * Returns 1 if correct 0 otherwise
753 **/
754int
755xmlCheckLanguageID(const xmlChar * lang)
756{
757 const xmlChar *cur = lang;
758
759 if (cur == NULL)
760 return (0);
761 if (((cur[0] == 'i') && (cur[1] == '-')) ||
762 ((cur[0] == 'I') && (cur[1] == '-'))) {
763 /*
764 * IANA code
765 */
766 cur += 2;
767 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
768 ((cur[0] >= 'a') && (cur[0] <= 'z')))
769 cur++;
770 } else if (((cur[0] == 'x') && (cur[1] == '-')) ||
771 ((cur[0] == 'X') && (cur[1] == '-'))) {
772 /*
773 * User code
774 */
775 cur += 2;
776 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
777 ((cur[0] >= 'a') && (cur[0] <= 'z')))
778 cur++;
779 } else if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
780 ((cur[0] >= 'a') && (cur[0] <= 'z'))) {
781 /*
782 * ISO639
783 */
784 cur++;
785 if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
786 ((cur[0] >= 'a') && (cur[0] <= 'z')))
787 cur++;
788 else
789 return (0);
790 } else
791 return (0);
792 while (cur[0] != 0) { /* non input consuming */
793 if (cur[0] != '-')
794 return (0);
795 cur++;
796 if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
797 ((cur[0] >= 'a') && (cur[0] <= 'z')))
798 cur++;
799 else
800 return (0);
801 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
802 ((cur[0] >= 'a') && (cur[0] <= 'z')))
803 cur++;
804 }
805 return (1);
806}
807
Owen Taylor3473f882001-02-23 17:55:21 +0000808/************************************************************************
809 * *
810 * Parser stacks related functions and macros *
811 * *
812 ************************************************************************/
813
814xmlEntityPtr xmlParseStringEntityRef(xmlParserCtxtPtr ctxt,
815 const xmlChar ** str);
816
Daniel Veillard0fb18932003-09-07 09:14:37 +0000817#ifdef SAX2
818/**
819 * nsPush:
820 * @ctxt: an XML parser context
821 * @prefix: the namespace prefix or NULL
822 * @URL: the namespace name
823 *
824 * Pushes a new parser namespace on top of the ns stack
825 *
William M. Brack7b9154b2003-09-27 19:23:50 +0000826 * Returns -1 in case of error, -2 if the namespace should be discarded
827 * and the index in the stack otherwise.
Daniel Veillard0fb18932003-09-07 09:14:37 +0000828 */
829static int
830nsPush(xmlParserCtxtPtr ctxt, const xmlChar *prefix, const xmlChar *URL)
831{
Daniel Veillarddca8cc72003-09-26 13:53:14 +0000832 if (ctxt->options & XML_PARSE_NSCLEAN) {
833 int i;
834 for (i = 0;i < ctxt->nsNr;i += 2) {
835 if (ctxt->nsTab[i] == prefix) {
836 /* in scope */
837 if (ctxt->nsTab[i + 1] == URL)
838 return(-2);
839 /* out of scope keep it */
840 break;
841 }
842 }
843 }
Daniel Veillard0fb18932003-09-07 09:14:37 +0000844 if ((ctxt->nsMax == 0) || (ctxt->nsTab == NULL)) {
845 ctxt->nsMax = 10;
846 ctxt->nsNr = 0;
847 ctxt->nsTab = (const xmlChar **)
848 xmlMalloc(ctxt->nsMax * sizeof(xmlChar *));
849 if (ctxt->nsTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000850 xmlErrMemory(ctxt, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +0000851 ctxt->nsMax = 0;
852 return (-1);
853 }
854 } else if (ctxt->nsNr >= ctxt->nsMax) {
855 ctxt->nsMax *= 2;
856 ctxt->nsTab = (const xmlChar **)
Daniel Veillard5bb9ccd2004-02-09 12:39:02 +0000857 xmlRealloc((char *) ctxt->nsTab,
Daniel Veillard0fb18932003-09-07 09:14:37 +0000858 ctxt->nsMax * sizeof(ctxt->nsTab[0]));
859 if (ctxt->nsTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000860 xmlErrMemory(ctxt, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +0000861 ctxt->nsMax /= 2;
862 return (-1);
863 }
864 }
865 ctxt->nsTab[ctxt->nsNr++] = prefix;
866 ctxt->nsTab[ctxt->nsNr++] = URL;
867 return (ctxt->nsNr);
868}
869/**
870 * nsPop:
871 * @ctxt: an XML parser context
872 * @nr: the number to pop
873 *
874 * Pops the top @nr parser prefix/namespace from the ns stack
875 *
876 * Returns the number of namespaces removed
877 */
878static int
879nsPop(xmlParserCtxtPtr ctxt, int nr)
880{
881 int i;
882
883 if (ctxt->nsTab == NULL) return(0);
884 if (ctxt->nsNr < nr) {
885 xmlGenericError(xmlGenericErrorContext, "Pbm popping %d NS\n", nr);
886 nr = ctxt->nsNr;
887 }
888 if (ctxt->nsNr <= 0)
889 return (0);
890
891 for (i = 0;i < nr;i++) {
892 ctxt->nsNr--;
893 ctxt->nsTab[ctxt->nsNr] = NULL;
894 }
895 return(nr);
896}
897#endif
898
899static int
900xmlCtxtGrowAttrs(xmlParserCtxtPtr ctxt, int nr) {
901 const xmlChar **atts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000902 int *attallocs;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000903 int maxatts;
904
905 if (ctxt->atts == NULL) {
Daniel Veillarde57ec792003-09-10 10:50:59 +0000906 maxatts = 55; /* allow for 10 attrs by default */
Daniel Veillard0fb18932003-09-07 09:14:37 +0000907 atts = (const xmlChar **)
908 xmlMalloc(maxatts * sizeof(xmlChar *));
Daniel Veillarde57ec792003-09-10 10:50:59 +0000909 if (atts == NULL) goto mem_error;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000910 ctxt->atts = atts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000911 attallocs = (int *) xmlMalloc((maxatts / 5) * sizeof(int));
912 if (attallocs == NULL) goto mem_error;
913 ctxt->attallocs = attallocs;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000914 ctxt->maxatts = maxatts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000915 } else if (nr + 5 > ctxt->maxatts) {
916 maxatts = (nr + 5) * 2;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000917 atts = (const xmlChar **) xmlRealloc((void *) ctxt->atts,
918 maxatts * sizeof(const xmlChar *));
Daniel Veillarde57ec792003-09-10 10:50:59 +0000919 if (atts == NULL) goto mem_error;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000920 ctxt->atts = atts;
Daniel Veillarde57ec792003-09-10 10:50:59 +0000921 attallocs = (int *) xmlRealloc((void *) ctxt->attallocs,
922 (maxatts / 5) * sizeof(int));
923 if (attallocs == NULL) goto mem_error;
924 ctxt->attallocs = attallocs;
Daniel Veillard0fb18932003-09-07 09:14:37 +0000925 ctxt->maxatts = maxatts;
926 }
927 return(ctxt->maxatts);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000928mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000929 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +0000930 return(-1);
Daniel Veillard0fb18932003-09-07 09:14:37 +0000931}
932
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000933/**
934 * inputPush:
935 * @ctxt: an XML parser context
Daniel Veillard9d06d302002-01-22 18:15:52 +0000936 * @value: the parser input
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000937 *
938 * Pushes a new parser input on top of the input stack
Daniel Veillard9d06d302002-01-22 18:15:52 +0000939 *
940 * Returns 0 in case of error, the index in the stack otherwise
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000941 */
Daniel Veillard1c732d22002-11-30 11:22:59 +0000942extern int
943inputPush(xmlParserCtxtPtr ctxt, xmlParserInputPtr value)
944{
945 if (ctxt->inputNr >= ctxt->inputMax) {
946 ctxt->inputMax *= 2;
947 ctxt->inputTab =
948 (xmlParserInputPtr *) xmlRealloc(ctxt->inputTab,
949 ctxt->inputMax *
950 sizeof(ctxt->inputTab[0]));
951 if (ctxt->inputTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +0000952 xmlErrMemory(ctxt, NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +0000953 return (0);
954 }
955 }
956 ctxt->inputTab[ctxt->inputNr] = value;
957 ctxt->input = value;
958 return (ctxt->inputNr++);
959}
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000960/**
Daniel Veillard1c732d22002-11-30 11:22:59 +0000961 * inputPop:
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000962 * @ctxt: an XML parser context
963 *
Daniel Veillard1c732d22002-11-30 11:22:59 +0000964 * Pops the top parser input from the input stack
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000965 *
Daniel Veillard1c732d22002-11-30 11:22:59 +0000966 * Returns the input just removed
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000967 */
Daniel Veillard1c732d22002-11-30 11:22:59 +0000968extern xmlParserInputPtr
969inputPop(xmlParserCtxtPtr ctxt)
970{
971 xmlParserInputPtr ret;
972
973 if (ctxt->inputNr <= 0)
974 return (0);
975 ctxt->inputNr--;
976 if (ctxt->inputNr > 0)
977 ctxt->input = ctxt->inputTab[ctxt->inputNr - 1];
978 else
979 ctxt->input = NULL;
980 ret = ctxt->inputTab[ctxt->inputNr];
981 ctxt->inputTab[ctxt->inputNr] = 0;
982 return (ret);
983}
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000984/**
985 * nodePush:
986 * @ctxt: an XML parser context
Daniel Veillard9d06d302002-01-22 18:15:52 +0000987 * @value: the element node
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000988 *
989 * Pushes a new element node on top of the node stack
Daniel Veillard9d06d302002-01-22 18:15:52 +0000990 *
991 * Returns 0 in case of error, the index in the stack otherwise
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000992 */
Daniel Veillard1c732d22002-11-30 11:22:59 +0000993extern int
994nodePush(xmlParserCtxtPtr ctxt, xmlNodePtr value)
995{
996 if (ctxt->nodeNr >= ctxt->nodeMax) {
997 ctxt->nodeMax *= 2;
998 ctxt->nodeTab =
999 (xmlNodePtr *) xmlRealloc(ctxt->nodeTab,
1000 ctxt->nodeMax *
1001 sizeof(ctxt->nodeTab[0]));
1002 if (ctxt->nodeTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001003 xmlErrMemory(ctxt, NULL);
Daniel Veillard1c732d22002-11-30 11:22:59 +00001004 return (0);
1005 }
1006 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00001007 if (((unsigned int) ctxt->nodeNr) > xmlParserMaxDepth) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001008 xmlFatalErrMsgInt(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard4aede2e2003-10-17 12:43:59 +00001009 "Excessive depth in document: change xmlParserMaxDepth = %d\n",
1010 xmlParserMaxDepth);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00001011 ctxt->instate = XML_PARSER_EOF;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00001012 return(0);
1013 }
Daniel Veillard1c732d22002-11-30 11:22:59 +00001014 ctxt->nodeTab[ctxt->nodeNr] = value;
1015 ctxt->node = value;
1016 return (ctxt->nodeNr++);
1017}
1018/**
1019 * nodePop:
1020 * @ctxt: an XML parser context
1021 *
1022 * Pops the top element node from the node stack
1023 *
1024 * Returns the node just removed
Owen Taylor3473f882001-02-23 17:55:21 +00001025 */
Daniel Veillard1c732d22002-11-30 11:22:59 +00001026extern xmlNodePtr
1027nodePop(xmlParserCtxtPtr ctxt)
1028{
1029 xmlNodePtr ret;
1030
1031 if (ctxt->nodeNr <= 0)
1032 return (0);
1033 ctxt->nodeNr--;
1034 if (ctxt->nodeNr > 0)
1035 ctxt->node = ctxt->nodeTab[ctxt->nodeNr - 1];
1036 else
1037 ctxt->node = NULL;
1038 ret = ctxt->nodeTab[ctxt->nodeNr];
1039 ctxt->nodeTab[ctxt->nodeNr] = 0;
1040 return (ret);
1041}
1042/**
Daniel Veillarde57ec792003-09-10 10:50:59 +00001043 * nameNsPush:
1044 * @ctxt: an XML parser context
1045 * @value: the element name
1046 * @prefix: the element prefix
1047 * @URI: the element namespace name
1048 *
1049 * Pushes a new element name/prefix/URL on top of the name stack
1050 *
1051 * Returns -1 in case of error, the index in the stack otherwise
1052 */
1053static int
1054nameNsPush(xmlParserCtxtPtr ctxt, const xmlChar * value,
1055 const xmlChar *prefix, const xmlChar *URI, int nsNr)
1056{
1057 if (ctxt->nameNr >= ctxt->nameMax) {
1058 const xmlChar * *tmp;
1059 void **tmp2;
1060 ctxt->nameMax *= 2;
1061 tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab,
1062 ctxt->nameMax *
1063 sizeof(ctxt->nameTab[0]));
1064 if (tmp == NULL) {
1065 ctxt->nameMax /= 2;
1066 goto mem_error;
1067 }
1068 ctxt->nameTab = tmp;
1069 tmp2 = (void **) xmlRealloc((void * *)ctxt->pushTab,
1070 ctxt->nameMax * 3 *
1071 sizeof(ctxt->pushTab[0]));
1072 if (tmp2 == NULL) {
1073 ctxt->nameMax /= 2;
1074 goto mem_error;
1075 }
1076 ctxt->pushTab = tmp2;
1077 }
1078 ctxt->nameTab[ctxt->nameNr] = value;
1079 ctxt->name = value;
1080 ctxt->pushTab[ctxt->nameNr * 3] = (void *) prefix;
1081 ctxt->pushTab[ctxt->nameNr * 3 + 1] = (void *) URI;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00001082 ctxt->pushTab[ctxt->nameNr * 3 + 2] = (void *) (long) nsNr;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001083 return (ctxt->nameNr++);
1084mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001085 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001086 return (-1);
1087}
1088/**
1089 * nameNsPop:
1090 * @ctxt: an XML parser context
1091 *
1092 * Pops the top element/prefix/URI name from the name stack
1093 *
1094 * Returns the name just removed
1095 */
1096static const xmlChar *
1097nameNsPop(xmlParserCtxtPtr ctxt)
1098{
1099 const xmlChar *ret;
1100
1101 if (ctxt->nameNr <= 0)
1102 return (0);
1103 ctxt->nameNr--;
1104 if (ctxt->nameNr > 0)
1105 ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
1106 else
1107 ctxt->name = NULL;
1108 ret = ctxt->nameTab[ctxt->nameNr];
1109 ctxt->nameTab[ctxt->nameNr] = NULL;
1110 return (ret);
1111}
1112
1113/**
Daniel Veillard1c732d22002-11-30 11:22:59 +00001114 * namePush:
1115 * @ctxt: an XML parser context
1116 * @value: the element name
1117 *
1118 * Pushes a new element name on top of the name stack
1119 *
Daniel Veillarde57ec792003-09-10 10:50:59 +00001120 * Returns -1 in case of error, the index in the stack otherwise
Daniel Veillard1c732d22002-11-30 11:22:59 +00001121 */
1122extern int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001123namePush(xmlParserCtxtPtr ctxt, const xmlChar * value)
Daniel Veillard1c732d22002-11-30 11:22:59 +00001124{
1125 if (ctxt->nameNr >= ctxt->nameMax) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00001126 const xmlChar * *tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001127 ctxt->nameMax *= 2;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001128 tmp = (const xmlChar * *) xmlRealloc((xmlChar * *)ctxt->nameTab,
Daniel Veillard1c732d22002-11-30 11:22:59 +00001129 ctxt->nameMax *
1130 sizeof(ctxt->nameTab[0]));
Daniel Veillarde57ec792003-09-10 10:50:59 +00001131 if (tmp == NULL) {
1132 ctxt->nameMax /= 2;
1133 goto mem_error;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001134 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00001135 ctxt->nameTab = tmp;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001136 }
1137 ctxt->nameTab[ctxt->nameNr] = value;
1138 ctxt->name = value;
1139 return (ctxt->nameNr++);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001140mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001141 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001142 return (-1);
Daniel Veillard1c732d22002-11-30 11:22:59 +00001143}
1144/**
1145 * namePop:
1146 * @ctxt: an XML parser context
1147 *
1148 * Pops the top element name from the name stack
1149 *
1150 * Returns the name just removed
1151 */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001152extern const xmlChar *
Daniel Veillard1c732d22002-11-30 11:22:59 +00001153namePop(xmlParserCtxtPtr ctxt)
1154{
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001155 const xmlChar *ret;
Daniel Veillard1c732d22002-11-30 11:22:59 +00001156
1157 if (ctxt->nameNr <= 0)
1158 return (0);
1159 ctxt->nameNr--;
1160 if (ctxt->nameNr > 0)
1161 ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
1162 else
1163 ctxt->name = NULL;
1164 ret = ctxt->nameTab[ctxt->nameNr];
1165 ctxt->nameTab[ctxt->nameNr] = 0;
1166 return (ret);
1167}
Owen Taylor3473f882001-02-23 17:55:21 +00001168
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001169static int spacePush(xmlParserCtxtPtr ctxt, int val) {
Owen Taylor3473f882001-02-23 17:55:21 +00001170 if (ctxt->spaceNr >= ctxt->spaceMax) {
1171 ctxt->spaceMax *= 2;
1172 ctxt->spaceTab = (int *) xmlRealloc(ctxt->spaceTab,
1173 ctxt->spaceMax * sizeof(ctxt->spaceTab[0]));
1174 if (ctxt->spaceTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001175 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001176 return(0);
1177 }
1178 }
1179 ctxt->spaceTab[ctxt->spaceNr] = val;
1180 ctxt->space = &ctxt->spaceTab[ctxt->spaceNr];
1181 return(ctxt->spaceNr++);
1182}
1183
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001184static int spacePop(xmlParserCtxtPtr ctxt) {
Owen Taylor3473f882001-02-23 17:55:21 +00001185 int ret;
1186 if (ctxt->spaceNr <= 0) return(0);
1187 ctxt->spaceNr--;
1188 if (ctxt->spaceNr > 0)
1189 ctxt->space = &ctxt->spaceTab[ctxt->spaceNr - 1];
1190 else
1191 ctxt->space = NULL;
1192 ret = ctxt->spaceTab[ctxt->spaceNr];
1193 ctxt->spaceTab[ctxt->spaceNr] = -1;
1194 return(ret);
1195}
1196
1197/*
1198 * Macros for accessing the content. Those should be used only by the parser,
1199 * and not exported.
1200 *
1201 * Dirty macros, i.e. one often need to make assumption on the context to
1202 * use them
1203 *
1204 * CUR_PTR return the current pointer to the xmlChar to be parsed.
1205 * To be used with extreme caution since operations consuming
1206 * characters may move the input buffer to a different location !
1207 * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled
1208 * This should be used internally by the parser
1209 * only to compare to ASCII values otherwise it would break when
1210 * running with UTF-8 encoding.
1211 * RAW same as CUR but in the input buffer, bypass any token
1212 * extraction that may have been done
1213 * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
1214 * to compare on ASCII based substring.
1215 * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
Daniel Veillard77a90a72003-03-22 00:04:05 +00001216 * strings without newlines within the parser.
1217 * NEXT1(l) Skip 1 xmlChar, and must also be used only to skip 1 non-newline ASCII
1218 * defined char within the parser.
Owen Taylor3473f882001-02-23 17:55:21 +00001219 * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
1220 *
1221 * NEXT Skip to the next character, this does the proper decoding
1222 * in UTF-8 mode. It also pop-up unfinished entities on the fly.
Daniel Veillard77a90a72003-03-22 00:04:05 +00001223 * NEXTL(l) Skip the current unicode character of l xmlChars long.
Owen Taylor3473f882001-02-23 17:55:21 +00001224 * CUR_CHAR(l) returns the current unicode character (int), set l
1225 * to the number of xmlChars used for the encoding [0-5].
1226 * CUR_SCHAR same but operate on a string instead of the context
1227 * COPY_BUF copy the current unicode char to the target buffer, increment
1228 * the index
1229 * GROW, SHRINK handling of input buffers
1230 */
1231
Daniel Veillardfdc91562002-07-01 21:52:03 +00001232#define RAW (*ctxt->input->cur)
1233#define CUR (*ctxt->input->cur)
Owen Taylor3473f882001-02-23 17:55:21 +00001234#define NXT(val) ctxt->input->cur[(val)]
1235#define CUR_PTR ctxt->input->cur
1236
Daniel Veillarda07050d2003-10-19 14:46:32 +00001237#define CMP4( s, c1, c2, c3, c4 ) \
1238 ( ((unsigned char *) s)[ 0 ] == c1 && ((unsigned char *) s)[ 1 ] == c2 && \
1239 ((unsigned char *) s)[ 2 ] == c3 && ((unsigned char *) s)[ 3 ] == c4 )
1240#define CMP5( s, c1, c2, c3, c4, c5 ) \
1241 ( CMP4( s, c1, c2, c3, c4 ) && ((unsigned char *) s)[ 4 ] == c5 )
1242#define CMP6( s, c1, c2, c3, c4, c5, c6 ) \
1243 ( CMP5( s, c1, c2, c3, c4, c5 ) && ((unsigned char *) s)[ 5 ] == c6 )
1244#define CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) \
1245 ( CMP6( s, c1, c2, c3, c4, c5, c6 ) && ((unsigned char *) s)[ 6 ] == c7 )
1246#define CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) \
1247 ( CMP7( s, c1, c2, c3, c4, c5, c6, c7 ) && ((unsigned char *) s)[ 7 ] == c8 )
1248#define CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) \
1249 ( CMP8( s, c1, c2, c3, c4, c5, c6, c7, c8 ) && \
1250 ((unsigned char *) s)[ 8 ] == c9 )
1251#define CMP10( s, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 ) \
1252 ( CMP9( s, c1, c2, c3, c4, c5, c6, c7, c8, c9 ) && \
1253 ((unsigned char *) s)[ 9 ] == c10 )
1254
Owen Taylor3473f882001-02-23 17:55:21 +00001255#define SKIP(val) do { \
Daniel Veillard77a90a72003-03-22 00:04:05 +00001256 ctxt->nbChars += (val),ctxt->input->cur += (val),ctxt->input->col+=(val); \
Owen Taylor3473f882001-02-23 17:55:21 +00001257 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
Daniel Veillard561b7f82002-03-20 21:55:57 +00001258 if ((*ctxt->input->cur == 0) && \
Owen Taylor3473f882001-02-23 17:55:21 +00001259 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \
1260 xmlPopInput(ctxt); \
1261 } while (0)
1262
Daniel Veillard0b787f32004-03-26 17:29:53 +00001263#define SKIPL(val) do { \
1264 int skipl; \
1265 for(skipl=0; skipl<val; skipl++) { \
1266 if (*(ctxt->input->cur) == '\n') { \
1267 ctxt->input->line++; ctxt->input->col = 1; \
1268 } else ctxt->input->col++; \
1269 ctxt->nbChars++; \
1270 ctxt->input->cur++; \
1271 } \
1272 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
1273 if ((*ctxt->input->cur == 0) && \
1274 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) \
1275 xmlPopInput(ctxt); \
1276 } while (0)
1277
Daniel Veillarda880b122003-04-21 21:36:41 +00001278#define SHRINK if ((ctxt->progressive == 0) && \
Daniel Veillard6155d8a2003-08-19 15:01:28 +00001279 (ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \
1280 (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \
Daniel Veillard46de64e2002-05-29 08:21:33 +00001281 xmlSHRINK (ctxt);
1282
1283static void xmlSHRINK (xmlParserCtxtPtr ctxt) {
1284 xmlParserInputShrink(ctxt->input);
1285 if ((*ctxt->input->cur == 0) &&
1286 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
1287 xmlPopInput(ctxt);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001288 }
Owen Taylor3473f882001-02-23 17:55:21 +00001289
Daniel Veillarda880b122003-04-21 21:36:41 +00001290#define GROW if ((ctxt->progressive == 0) && \
1291 (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \
Daniel Veillard46de64e2002-05-29 08:21:33 +00001292 xmlGROW (ctxt);
1293
1294static void xmlGROW (xmlParserCtxtPtr ctxt) {
1295 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1296 if ((*ctxt->input->cur == 0) &&
1297 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
1298 xmlPopInput(ctxt);
Daniel Veillarda880b122003-04-21 21:36:41 +00001299}
Owen Taylor3473f882001-02-23 17:55:21 +00001300
1301#define SKIP_BLANKS xmlSkipBlankChars(ctxt)
1302
1303#define NEXT xmlNextChar(ctxt)
1304
Daniel Veillard21a0f912001-02-25 19:54:14 +00001305#define NEXT1 { \
Daniel Veillard77a90a72003-03-22 00:04:05 +00001306 ctxt->input->col++; \
Daniel Veillard21a0f912001-02-25 19:54:14 +00001307 ctxt->input->cur++; \
1308 ctxt->nbChars++; \
Daniel Veillard561b7f82002-03-20 21:55:57 +00001309 if (*ctxt->input->cur == 0) \
Daniel Veillard21a0f912001-02-25 19:54:14 +00001310 xmlParserInputGrow(ctxt->input, INPUT_CHUNK); \
1311 }
1312
Owen Taylor3473f882001-02-23 17:55:21 +00001313#define NEXTL(l) do { \
1314 if (*(ctxt->input->cur) == '\n') { \
1315 ctxt->input->line++; ctxt->input->col = 1; \
1316 } else ctxt->input->col++; \
Daniel Veillardfdc91562002-07-01 21:52:03 +00001317 ctxt->input->cur += l; \
Owen Taylor3473f882001-02-23 17:55:21 +00001318 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
Owen Taylor3473f882001-02-23 17:55:21 +00001319 } while (0)
1320
1321#define CUR_CHAR(l) xmlCurrentChar(ctxt, &l)
1322#define CUR_SCHAR(s, l) xmlStringCurrentChar(ctxt, s, &l)
1323
1324#define COPY_BUF(l,b,i,v) \
1325 if (l == 1) b[i++] = (xmlChar) v; \
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001326 else i += xmlCopyCharMultiByte(&b[i],v)
Owen Taylor3473f882001-02-23 17:55:21 +00001327
1328/**
1329 * xmlSkipBlankChars:
1330 * @ctxt: the XML parser context
1331 *
1332 * skip all blanks character found at that point in the input streams.
1333 * It pops up finished entities in the process if allowable at that point.
1334 *
1335 * Returns the number of space chars skipped
1336 */
1337
1338int
1339xmlSkipBlankChars(xmlParserCtxtPtr ctxt) {
Daniel Veillard02141ea2001-04-30 11:46:40 +00001340 int res = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001341
1342 /*
1343 * It's Okay to use CUR/NEXT here since all the blanks are on
1344 * the ASCII range.
1345 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00001346 if ((ctxt->inputNr == 1) && (ctxt->instate != XML_PARSER_DTD)) {
1347 const xmlChar *cur;
Owen Taylor3473f882001-02-23 17:55:21 +00001348 /*
Daniel Veillard02141ea2001-04-30 11:46:40 +00001349 * if we are in the document content, go really fast
Owen Taylor3473f882001-02-23 17:55:21 +00001350 */
Daniel Veillard02141ea2001-04-30 11:46:40 +00001351 cur = ctxt->input->cur;
William M. Brack76e95df2003-10-18 16:20:14 +00001352 while (IS_BLANK_CH(*cur)) {
Daniel Veillard02141ea2001-04-30 11:46:40 +00001353 if (*cur == '\n') {
1354 ctxt->input->line++; ctxt->input->col = 1;
1355 }
1356 cur++;
1357 res++;
1358 if (*cur == 0) {
1359 ctxt->input->cur = cur;
1360 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1361 cur = ctxt->input->cur;
1362 }
1363 }
1364 ctxt->input->cur = cur;
1365 } else {
1366 int cur;
1367 do {
1368 cur = CUR;
1369 while (IS_BLANK(cur)) { /* CHECKED tstblanks.xml */
1370 NEXT;
1371 cur = CUR;
1372 res++;
1373 }
1374 while ((cur == 0) && (ctxt->inputNr > 1) &&
1375 (ctxt->instate != XML_PARSER_COMMENT)) {
1376 xmlPopInput(ctxt);
1377 cur = CUR;
1378 }
1379 /*
1380 * Need to handle support of entities branching here
1381 */
1382 if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt);
1383 } while (IS_BLANK(cur)); /* CHECKED tstblanks.xml */
1384 }
Owen Taylor3473f882001-02-23 17:55:21 +00001385 return(res);
1386}
1387
1388/************************************************************************
1389 * *
1390 * Commodity functions to handle entities *
1391 * *
1392 ************************************************************************/
1393
1394/**
1395 * xmlPopInput:
1396 * @ctxt: an XML parser context
1397 *
1398 * xmlPopInput: the current input pointed by ctxt->input came to an end
1399 * pop it and return the next char.
1400 *
1401 * Returns the current xmlChar in the parser context
1402 */
1403xmlChar
1404xmlPopInput(xmlParserCtxtPtr ctxt) {
1405 if (ctxt->inputNr == 1) return(0); /* End of main Input */
1406 if (xmlParserDebugEntities)
1407 xmlGenericError(xmlGenericErrorContext,
1408 "Popping input %d\n", ctxt->inputNr);
1409 xmlFreeInputStream(inputPop(ctxt));
Daniel Veillard561b7f82002-03-20 21:55:57 +00001410 if ((*ctxt->input->cur == 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +00001411 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
1412 return(xmlPopInput(ctxt));
1413 return(CUR);
1414}
1415
1416/**
1417 * xmlPushInput:
1418 * @ctxt: an XML parser context
1419 * @input: an XML parser input fragment (entity, XML fragment ...).
1420 *
1421 * xmlPushInput: switch to a new input stream which is stacked on top
1422 * of the previous one(s).
1423 */
1424void
1425xmlPushInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr input) {
1426 if (input == NULL) return;
1427
1428 if (xmlParserDebugEntities) {
1429 if ((ctxt->input != NULL) && (ctxt->input->filename))
1430 xmlGenericError(xmlGenericErrorContext,
1431 "%s(%d): ", ctxt->input->filename,
1432 ctxt->input->line);
1433 xmlGenericError(xmlGenericErrorContext,
1434 "Pushing input %d : %.30s\n", ctxt->inputNr+1, input->cur);
1435 }
1436 inputPush(ctxt, input);
1437 GROW;
1438}
1439
1440/**
1441 * xmlParseCharRef:
1442 * @ctxt: an XML parser context
1443 *
1444 * parse Reference declarations
1445 *
1446 * [66] CharRef ::= '&#' [0-9]+ ';' |
1447 * '&#x' [0-9a-fA-F]+ ';'
1448 *
1449 * [ WFC: Legal Character ]
1450 * Characters referred to using character references must match the
1451 * production for Char.
1452 *
1453 * Returns the value parsed (as an int), 0 in case of error
1454 */
1455int
1456xmlParseCharRef(xmlParserCtxtPtr ctxt) {
Daniel Veillard50582112001-03-26 22:52:16 +00001457 unsigned int val = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001458 int count = 0;
1459
Owen Taylor3473f882001-02-23 17:55:21 +00001460 /*
1461 * Using RAW/CUR/NEXT is okay since we are working on ASCII range here
1462 */
Daniel Veillard561b7f82002-03-20 21:55:57 +00001463 if ((RAW == '&') && (NXT(1) == '#') &&
Owen Taylor3473f882001-02-23 17:55:21 +00001464 (NXT(2) == 'x')) {
1465 SKIP(3);
1466 GROW;
1467 while (RAW != ';') { /* loop blocked by count */
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00001468 if (count++ > 20) {
1469 count = 0;
1470 GROW;
1471 }
1472 if ((RAW >= '0') && (RAW <= '9'))
Owen Taylor3473f882001-02-23 17:55:21 +00001473 val = val * 16 + (CUR - '0');
1474 else if ((RAW >= 'a') && (RAW <= 'f') && (count < 20))
1475 val = val * 16 + (CUR - 'a') + 10;
1476 else if ((RAW >= 'A') && (RAW <= 'F') && (count < 20))
1477 val = val * 16 + (CUR - 'A') + 10;
1478 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001479 xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001480 val = 0;
1481 break;
1482 }
1483 NEXT;
1484 count++;
1485 }
1486 if (RAW == ';') {
1487 /* on purpose to avoid reentrancy problems with NEXT and SKIP */
Daniel Veillard77a90a72003-03-22 00:04:05 +00001488 ctxt->input->col++;
Owen Taylor3473f882001-02-23 17:55:21 +00001489 ctxt->nbChars ++;
1490 ctxt->input->cur++;
1491 }
Daniel Veillard561b7f82002-03-20 21:55:57 +00001492 } else if ((RAW == '&') && (NXT(1) == '#')) {
Owen Taylor3473f882001-02-23 17:55:21 +00001493 SKIP(2);
1494 GROW;
1495 while (RAW != ';') { /* loop blocked by count */
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00001496 if (count++ > 20) {
1497 count = 0;
1498 GROW;
1499 }
1500 if ((RAW >= '0') && (RAW <= '9'))
Owen Taylor3473f882001-02-23 17:55:21 +00001501 val = val * 10 + (CUR - '0');
1502 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001503 xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001504 val = 0;
1505 break;
1506 }
1507 NEXT;
1508 count++;
1509 }
1510 if (RAW == ';') {
1511 /* on purpose to avoid reentrancy problems with NEXT and SKIP */
Daniel Veillard77a90a72003-03-22 00:04:05 +00001512 ctxt->input->col++;
Owen Taylor3473f882001-02-23 17:55:21 +00001513 ctxt->nbChars ++;
1514 ctxt->input->cur++;
1515 }
1516 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001517 xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001518 }
1519
1520 /*
1521 * [ WFC: Legal Character ]
1522 * Characters referred to using character references must match the
1523 * production for Char.
1524 */
William M. Brack871611b2003-10-18 04:53:14 +00001525 if (IS_CHAR(val)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001526 return(val);
1527 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001528 xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
1529 "xmlParseCharRef: invalid xmlChar value %d\n",
1530 val);
Owen Taylor3473f882001-02-23 17:55:21 +00001531 }
1532 return(0);
1533}
1534
1535/**
1536 * xmlParseStringCharRef:
1537 * @ctxt: an XML parser context
1538 * @str: a pointer to an index in the string
1539 *
1540 * parse Reference declarations, variant parsing from a string rather
1541 * than an an input flow.
1542 *
1543 * [66] CharRef ::= '&#' [0-9]+ ';' |
1544 * '&#x' [0-9a-fA-F]+ ';'
1545 *
1546 * [ WFC: Legal Character ]
1547 * Characters referred to using character references must match the
1548 * production for Char.
1549 *
1550 * Returns the value parsed (as an int), 0 in case of error, str will be
1551 * updated to the current value of the index
1552 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001553static int
Owen Taylor3473f882001-02-23 17:55:21 +00001554xmlParseStringCharRef(xmlParserCtxtPtr ctxt, const xmlChar **str) {
1555 const xmlChar *ptr;
1556 xmlChar cur;
1557 int val = 0;
1558
1559 if ((str == NULL) || (*str == NULL)) return(0);
1560 ptr = *str;
1561 cur = *ptr;
1562 if ((cur == '&') && (ptr[1] == '#') && (ptr[2] == 'x')) {
1563 ptr += 3;
1564 cur = *ptr;
1565 while (cur != ';') { /* Non input consuming loop */
1566 if ((cur >= '0') && (cur <= '9'))
1567 val = val * 16 + (cur - '0');
1568 else if ((cur >= 'a') && (cur <= 'f'))
1569 val = val * 16 + (cur - 'a') + 10;
1570 else if ((cur >= 'A') && (cur <= 'F'))
1571 val = val * 16 + (cur - 'A') + 10;
1572 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001573 xmlFatalErr(ctxt, XML_ERR_INVALID_HEX_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001574 val = 0;
1575 break;
1576 }
1577 ptr++;
1578 cur = *ptr;
1579 }
1580 if (cur == ';')
1581 ptr++;
1582 } else if ((cur == '&') && (ptr[1] == '#')){
1583 ptr += 2;
1584 cur = *ptr;
1585 while (cur != ';') { /* Non input consuming loops */
1586 if ((cur >= '0') && (cur <= '9'))
1587 val = val * 10 + (cur - '0');
1588 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001589 xmlFatalErr(ctxt, XML_ERR_INVALID_DEC_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001590 val = 0;
1591 break;
1592 }
1593 ptr++;
1594 cur = *ptr;
1595 }
1596 if (cur == ';')
1597 ptr++;
1598 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001599 xmlFatalErr(ctxt, XML_ERR_INVALID_CHARREF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001600 return(0);
1601 }
1602 *str = ptr;
1603
1604 /*
1605 * [ WFC: Legal Character ]
1606 * Characters referred to using character references must match the
1607 * production for Char.
1608 */
William M. Brack871611b2003-10-18 04:53:14 +00001609 if (IS_CHAR(val)) {
Owen Taylor3473f882001-02-23 17:55:21 +00001610 return(val);
1611 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001612 xmlFatalErrMsgInt(ctxt, XML_ERR_INVALID_CHAR,
1613 "xmlParseStringCharRef: invalid xmlChar value %d\n",
1614 val);
Owen Taylor3473f882001-02-23 17:55:21 +00001615 }
1616 return(0);
1617}
1618
1619/**
Daniel Veillardf5582f12002-06-11 10:08:16 +00001620 * xmlNewBlanksWrapperInputStream:
1621 * @ctxt: an XML parser context
1622 * @entity: an Entity pointer
1623 *
1624 * Create a new input stream for wrapping
1625 * blanks around a PEReference
1626 *
1627 * Returns the new input stream or NULL
1628 */
1629
1630static void deallocblankswrapper (xmlChar *str) {xmlFree(str);}
1631
Daniel Veillardf4862f02002-09-10 11:13:43 +00001632static xmlParserInputPtr
Daniel Veillardf5582f12002-06-11 10:08:16 +00001633xmlNewBlanksWrapperInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
1634 xmlParserInputPtr input;
1635 xmlChar *buffer;
1636 size_t length;
1637 if (entity == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001638 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
1639 "xmlNewBlanksWrapperInputStream entity\n");
Daniel Veillardf5582f12002-06-11 10:08:16 +00001640 return(NULL);
1641 }
1642 if (xmlParserDebugEntities)
1643 xmlGenericError(xmlGenericErrorContext,
1644 "new blanks wrapper for entity: %s\n", entity->name);
1645 input = xmlNewInputStream(ctxt);
1646 if (input == NULL) {
1647 return(NULL);
1648 }
1649 length = xmlStrlen(entity->name) + 5;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001650 buffer = xmlMallocAtomic(length);
Daniel Veillardf5582f12002-06-11 10:08:16 +00001651 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001652 xmlErrMemory(ctxt, NULL);
Daniel Veillardf5582f12002-06-11 10:08:16 +00001653 return(NULL);
1654 }
1655 buffer [0] = ' ';
1656 buffer [1] = '%';
1657 buffer [length-3] = ';';
1658 buffer [length-2] = ' ';
1659 buffer [length-1] = 0;
1660 memcpy(buffer + 2, entity->name, length - 5);
1661 input->free = deallocblankswrapper;
1662 input->base = buffer;
1663 input->cur = buffer;
1664 input->length = length;
1665 input->end = &buffer[length];
1666 return(input);
1667}
1668
1669/**
Owen Taylor3473f882001-02-23 17:55:21 +00001670 * xmlParserHandlePEReference:
1671 * @ctxt: the parser context
1672 *
1673 * [69] PEReference ::= '%' Name ';'
1674 *
1675 * [ WFC: No Recursion ]
1676 * A parsed entity must not contain a recursive
1677 * reference to itself, either directly or indirectly.
1678 *
1679 * [ WFC: Entity Declared ]
1680 * In a document without any DTD, a document with only an internal DTD
1681 * subset which contains no parameter entity references, or a document
1682 * with "standalone='yes'", ... ... The declaration of a parameter
1683 * entity must precede any reference to it...
1684 *
1685 * [ VC: Entity Declared ]
1686 * In a document with an external subset or external parameter entities
1687 * with "standalone='no'", ... ... The declaration of a parameter entity
1688 * must precede any reference to it...
1689 *
1690 * [ WFC: In DTD ]
1691 * Parameter-entity references may only appear in the DTD.
1692 * NOTE: misleading but this is handled.
1693 *
1694 * A PEReference may have been detected in the current input stream
1695 * the handling is done accordingly to
1696 * http://www.w3.org/TR/REC-xml#entproc
1697 * i.e.
1698 * - Included in literal in entity values
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001699 * - Included as Parameter Entity reference within DTDs
Owen Taylor3473f882001-02-23 17:55:21 +00001700 */
1701void
1702xmlParserHandlePEReference(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001703 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00001704 xmlEntityPtr entity = NULL;
1705 xmlParserInputPtr input;
1706
Owen Taylor3473f882001-02-23 17:55:21 +00001707 if (RAW != '%') return;
1708 switch(ctxt->instate) {
1709 case XML_PARSER_CDATA_SECTION:
1710 return;
1711 case XML_PARSER_COMMENT:
1712 return;
1713 case XML_PARSER_START_TAG:
1714 return;
1715 case XML_PARSER_END_TAG:
1716 return;
1717 case XML_PARSER_EOF:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001718 xmlFatalErr(ctxt, XML_ERR_PEREF_AT_EOF, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001719 return;
1720 case XML_PARSER_PROLOG:
1721 case XML_PARSER_START:
1722 case XML_PARSER_MISC:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001723 xmlFatalErr(ctxt, XML_ERR_PEREF_IN_PROLOG, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001724 return;
1725 case XML_PARSER_ENTITY_DECL:
1726 case XML_PARSER_CONTENT:
1727 case XML_PARSER_ATTRIBUTE_VALUE:
1728 case XML_PARSER_PI:
1729 case XML_PARSER_SYSTEM_LITERAL:
Daniel Veillard4a7ae502002-02-18 19:18:17 +00001730 case XML_PARSER_PUBLIC_LITERAL:
Owen Taylor3473f882001-02-23 17:55:21 +00001731 /* we just ignore it there */
1732 return;
1733 case XML_PARSER_EPILOG:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001734 xmlFatalErr(ctxt, XML_ERR_PEREF_IN_EPILOG, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001735 return;
1736 case XML_PARSER_ENTITY_VALUE:
1737 /*
1738 * NOTE: in the case of entity values, we don't do the
1739 * substitution here since we need the literal
1740 * entity value to be able to save the internal
1741 * subset of the document.
1742 * This will be handled by xmlStringDecodeEntities
1743 */
1744 return;
1745 case XML_PARSER_DTD:
1746 /*
1747 * [WFC: Well-Formedness Constraint: PEs in Internal Subset]
1748 * In the internal DTD subset, parameter-entity references
1749 * can occur only where markup declarations can occur, not
1750 * within markup declarations.
1751 * In that case this is handled in xmlParseMarkupDecl
1752 */
1753 if ((ctxt->external == 0) && (ctxt->inputNr == 1))
1754 return;
William M. Brack76e95df2003-10-18 16:20:14 +00001755 if (IS_BLANK_CH(NXT(1)) || NXT(1) == 0)
Daniel Veillardf5582f12002-06-11 10:08:16 +00001756 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001757 break;
1758 case XML_PARSER_IGNORE:
1759 return;
1760 }
1761
1762 NEXT;
1763 name = xmlParseName(ctxt);
1764 if (xmlParserDebugEntities)
1765 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001766 "PEReference: %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00001767 if (name == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001768 xmlFatalErr(ctxt, XML_ERR_PEREF_NO_NAME, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001769 } else {
1770 if (RAW == ';') {
1771 NEXT;
1772 if ((ctxt->sax != NULL) && (ctxt->sax->getParameterEntity != NULL))
1773 entity = ctxt->sax->getParameterEntity(ctxt->userData, name);
1774 if (entity == NULL) {
1775
1776 /*
1777 * [ WFC: Entity Declared ]
1778 * In a document without any DTD, a document with only an
1779 * internal DTD subset which contains no parameter entity
1780 * references, or a document with "standalone='yes'", ...
1781 * ... The declaration of a parameter entity must precede
1782 * any reference to it...
1783 */
1784 if ((ctxt->standalone == 1) ||
1785 ((ctxt->hasExternalSubset == 0) &&
1786 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00001787 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00001788 "PEReference: %%%s; not found\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00001789 } else {
1790 /*
1791 * [ VC: Entity Declared ]
1792 * In a document with an external subset or external
1793 * parameter entities with "standalone='no'", ...
1794 * ... The declaration of a parameter entity must precede
1795 * any reference to it...
1796 */
Daniel Veillard24eb9782003-10-04 21:08:09 +00001797 if ((ctxt->validate) && (ctxt->vctxt.error != NULL)) {
1798 xmlValidityError(ctxt, XML_WAR_UNDECLARED_ENTITY,
1799 "PEReference: %%%s; not found\n",
1800 name);
1801 } else
1802 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
1803 "PEReference: %%%s; not found\n",
1804 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001805 ctxt->valid = 0;
1806 }
Daniel Veillardf5582f12002-06-11 10:08:16 +00001807 } else if (ctxt->input->free != deallocblankswrapper) {
1808 input = xmlNewBlanksWrapperInputStream(ctxt, entity);
1809 xmlPushInput(ctxt, input);
Owen Taylor3473f882001-02-23 17:55:21 +00001810 } else {
1811 if ((entity->etype == XML_INTERNAL_PARAMETER_ENTITY) ||
1812 (entity->etype == XML_EXTERNAL_PARAMETER_ENTITY)) {
Daniel Veillard87a764e2001-06-20 17:41:10 +00001813 xmlChar start[4];
1814 xmlCharEncoding enc;
1815
Owen Taylor3473f882001-02-23 17:55:21 +00001816 /*
1817 * handle the extra spaces added before and after
1818 * c.f. http://www.w3.org/TR/REC-xml#as-PE
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001819 * this is done independently.
Owen Taylor3473f882001-02-23 17:55:21 +00001820 */
1821 input = xmlNewEntityInputStream(ctxt, entity);
1822 xmlPushInput(ctxt, input);
Daniel Veillard87a764e2001-06-20 17:41:10 +00001823
1824 /*
1825 * Get the 4 first bytes and decode the charset
1826 * if enc != XML_CHAR_ENCODING_NONE
1827 * plug some encoding conversion routines.
William M. Bracka0c48ad2004-04-16 15:58:29 +00001828 * Note that, since we may have some non-UTF8
1829 * encoding (like UTF16, bug 135229), the 'length'
1830 * is not known, but we can calculate based upon
1831 * the amount of data in the buffer.
Daniel Veillard87a764e2001-06-20 17:41:10 +00001832 */
1833 GROW
William M. Bracka0c48ad2004-04-16 15:58:29 +00001834 if ((ctxt->input->end - ctxt->input->cur)>=4) {
Daniel Veillarde059b892002-06-13 15:32:10 +00001835 start[0] = RAW;
1836 start[1] = NXT(1);
1837 start[2] = NXT(2);
1838 start[3] = NXT(3);
1839 enc = xmlDetectCharEncoding(start, 4);
1840 if (enc != XML_CHAR_ENCODING_NONE) {
1841 xmlSwitchEncoding(ctxt, enc);
1842 }
Daniel Veillard87a764e2001-06-20 17:41:10 +00001843 }
1844
Owen Taylor3473f882001-02-23 17:55:21 +00001845 if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) &&
Daniel Veillarda07050d2003-10-19 14:46:32 +00001846 (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l' )) &&
1847 (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00001848 xmlParseTextDecl(ctxt);
1849 }
Owen Taylor3473f882001-02-23 17:55:21 +00001850 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00001851 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER,
1852 "PEReference: %s is not a parameter entity\n",
1853 name);
Owen Taylor3473f882001-02-23 17:55:21 +00001854 }
1855 }
1856 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001857 xmlFatalErr(ctxt, XML_ERR_PEREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001858 }
Owen Taylor3473f882001-02-23 17:55:21 +00001859 }
1860}
1861
1862/*
1863 * Macro used to grow the current buffer.
1864 */
1865#define growBuffer(buffer) { \
Daniel Veillardd3999c72004-03-10 16:27:03 +00001866 xmlChar *tmp; \
Owen Taylor3473f882001-02-23 17:55:21 +00001867 buffer##_size *= 2; \
Daniel Veillardd3999c72004-03-10 16:27:03 +00001868 tmp = (xmlChar *) \
Owen Taylor3473f882001-02-23 17:55:21 +00001869 xmlRealloc(buffer, buffer##_size * sizeof(xmlChar)); \
Daniel Veillardd3999c72004-03-10 16:27:03 +00001870 if (tmp == NULL) goto mem_error; \
1871 buffer = tmp; \
Owen Taylor3473f882001-02-23 17:55:21 +00001872}
1873
1874/**
Daniel Veillard7a02cfe2003-09-25 12:18:34 +00001875 * xmlStringLenDecodeEntities:
Owen Taylor3473f882001-02-23 17:55:21 +00001876 * @ctxt: the parser context
1877 * @str: the input string
Daniel Veillarde57ec792003-09-10 10:50:59 +00001878 * @len: the string length
Owen Taylor3473f882001-02-23 17:55:21 +00001879 * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
1880 * @end: an end marker xmlChar, 0 if none
1881 * @end2: an end marker xmlChar, 0 if none
1882 * @end3: an end marker xmlChar, 0 if none
1883 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001884 * Takes a entity string content and process to do the adequate substitutions.
Owen Taylor3473f882001-02-23 17:55:21 +00001885 *
1886 * [67] Reference ::= EntityRef | CharRef
1887 *
1888 * [69] PEReference ::= '%' Name ';'
1889 *
1890 * Returns A newly allocated string with the substitution done. The caller
1891 * must deallocate it !
1892 */
1893xmlChar *
Daniel Veillarde57ec792003-09-10 10:50:59 +00001894xmlStringLenDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int len,
1895 int what, xmlChar end, xmlChar end2, xmlChar end3) {
Owen Taylor3473f882001-02-23 17:55:21 +00001896 xmlChar *buffer = NULL;
1897 int buffer_size = 0;
1898
1899 xmlChar *current = NULL;
Daniel Veillarde57ec792003-09-10 10:50:59 +00001900 const xmlChar *last;
Owen Taylor3473f882001-02-23 17:55:21 +00001901 xmlEntityPtr ent;
1902 int c,l;
1903 int nbchars = 0;
1904
Daniel Veillarde57ec792003-09-10 10:50:59 +00001905 if ((str == NULL) || (len < 0))
Owen Taylor3473f882001-02-23 17:55:21 +00001906 return(NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00001907 last = str + len;
Owen Taylor3473f882001-02-23 17:55:21 +00001908
1909 if (ctxt->depth > 40) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00001910 xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001911 return(NULL);
1912 }
1913
1914 /*
1915 * allocate a translation buffer.
1916 */
1917 buffer_size = XML_PARSER_BIG_BUFFER_SIZE;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00001918 buffer = (xmlChar *) xmlMallocAtomic(buffer_size * sizeof(xmlChar));
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00001919 if (buffer == NULL) goto mem_error;
Owen Taylor3473f882001-02-23 17:55:21 +00001920
1921 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001922 * OK loop until we reach one of the ending char or a size limit.
Owen Taylor3473f882001-02-23 17:55:21 +00001923 * we are operating on already parsed values.
1924 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00001925 if (str < last)
1926 c = CUR_SCHAR(str, l);
1927 else
1928 c = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001929 while ((c != 0) && (c != end) && /* non input consuming loop */
1930 (c != end2) && (c != end3)) {
1931
1932 if (c == 0) break;
1933 if ((c == '&') && (str[1] == '#')) {
1934 int val = xmlParseStringCharRef(ctxt, &str);
1935 if (val != 0) {
1936 COPY_BUF(0,buffer,nbchars,val);
1937 }
1938 } else if ((c == '&') && (what & XML_SUBSTITUTE_REF)) {
1939 if (xmlParserDebugEntities)
1940 xmlGenericError(xmlGenericErrorContext,
1941 "String decoding Entity Reference: %.30s\n",
1942 str);
1943 ent = xmlParseStringEntityRef(ctxt, &str);
1944 if ((ent != NULL) &&
1945 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
1946 if (ent->content != NULL) {
1947 COPY_BUF(0,buffer,nbchars,ent->content[0]);
1948 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00001949 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
1950 "predefined entity has no content\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001951 }
1952 } else if ((ent != NULL) && (ent->content != NULL)) {
1953 xmlChar *rep;
1954
1955 ctxt->depth++;
1956 rep = xmlStringDecodeEntities(ctxt, ent->content, what,
1957 0, 0, 0);
1958 ctxt->depth--;
1959 if (rep != NULL) {
1960 current = rep;
1961 while (*current != 0) { /* non input consuming loop */
1962 buffer[nbchars++] = *current++;
1963 if (nbchars >
1964 buffer_size - XML_PARSER_BUFFER_SIZE) {
1965 growBuffer(buffer);
1966 }
1967 }
1968 xmlFree(rep);
1969 }
1970 } else if (ent != NULL) {
1971 int i = xmlStrlen(ent->name);
1972 const xmlChar *cur = ent->name;
1973
1974 buffer[nbchars++] = '&';
1975 if (nbchars > buffer_size - i - XML_PARSER_BUFFER_SIZE) {
1976 growBuffer(buffer);
1977 }
1978 for (;i > 0;i--)
1979 buffer[nbchars++] = *cur++;
1980 buffer[nbchars++] = ';';
1981 }
1982 } else if (c == '%' && (what & XML_SUBSTITUTE_PEREF)) {
1983 if (xmlParserDebugEntities)
1984 xmlGenericError(xmlGenericErrorContext,
1985 "String decoding PE Reference: %.30s\n", str);
1986 ent = xmlParseStringPEReference(ctxt, &str);
1987 if (ent != NULL) {
1988 xmlChar *rep;
1989
1990 ctxt->depth++;
1991 rep = xmlStringDecodeEntities(ctxt, ent->content, what,
1992 0, 0, 0);
1993 ctxt->depth--;
1994 if (rep != NULL) {
1995 current = rep;
1996 while (*current != 0) { /* non input consuming loop */
1997 buffer[nbchars++] = *current++;
1998 if (nbchars >
1999 buffer_size - XML_PARSER_BUFFER_SIZE) {
2000 growBuffer(buffer);
2001 }
2002 }
2003 xmlFree(rep);
2004 }
2005 }
2006 } else {
2007 COPY_BUF(l,buffer,nbchars,c);
2008 str += l;
2009 if (nbchars > buffer_size - XML_PARSER_BUFFER_SIZE) {
2010 growBuffer(buffer);
2011 }
2012 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002013 if (str < last)
2014 c = CUR_SCHAR(str, l);
2015 else
2016 c = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002017 }
2018 buffer[nbchars++] = 0;
2019 return(buffer);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002020
2021mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002022 xmlErrMemory(ctxt, NULL);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002023 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002024}
2025
Daniel Veillarde57ec792003-09-10 10:50:59 +00002026/**
2027 * xmlStringDecodeEntities:
2028 * @ctxt: the parser context
2029 * @str: the input string
2030 * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
2031 * @end: an end marker xmlChar, 0 if none
2032 * @end2: an end marker xmlChar, 0 if none
2033 * @end3: an end marker xmlChar, 0 if none
2034 *
2035 * Takes a entity string content and process to do the adequate substitutions.
2036 *
2037 * [67] Reference ::= EntityRef | CharRef
2038 *
2039 * [69] PEReference ::= '%' Name ';'
2040 *
2041 * Returns A newly allocated string with the substitution done. The caller
2042 * must deallocate it !
2043 */
2044xmlChar *
2045xmlStringDecodeEntities(xmlParserCtxtPtr ctxt, const xmlChar *str, int what,
2046 xmlChar end, xmlChar end2, xmlChar end3) {
2047 return(xmlStringLenDecodeEntities(ctxt, str, xmlStrlen(str), what,
2048 end, end2, end3));
2049}
Owen Taylor3473f882001-02-23 17:55:21 +00002050
2051/************************************************************************
2052 * *
Owen Taylor3473f882001-02-23 17:55:21 +00002053 * Commodity functions, cleanup needed ? *
2054 * *
2055 ************************************************************************/
2056
2057/**
2058 * areBlanks:
2059 * @ctxt: an XML parser context
2060 * @str: a xmlChar *
2061 * @len: the size of @str
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002062 * @blank_chars: we know the chars are blanks
Owen Taylor3473f882001-02-23 17:55:21 +00002063 *
2064 * Is this a sequence of blank chars that one can ignore ?
2065 *
2066 * Returns 1 if ignorable 0 otherwise.
2067 */
2068
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002069static int areBlanks(xmlParserCtxtPtr ctxt, const xmlChar *str, int len,
2070 int blank_chars) {
Owen Taylor3473f882001-02-23 17:55:21 +00002071 int i, ret;
2072 xmlNodePtr lastChild;
2073
Daniel Veillard05c13a22001-09-09 08:38:09 +00002074 /*
2075 * Don't spend time trying to differentiate them, the same callback is
2076 * used !
2077 */
2078 if (ctxt->sax->ignorableWhitespace == ctxt->sax->characters)
Daniel Veillard2f362242001-03-02 17:36:21 +00002079 return(0);
2080
Owen Taylor3473f882001-02-23 17:55:21 +00002081 /*
2082 * Check for xml:space value.
2083 */
2084 if (*(ctxt->space) == 1)
2085 return(0);
2086
2087 /*
2088 * Check that the string is made of blanks
2089 */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002090 if (blank_chars == 0) {
2091 for (i = 0;i < len;i++)
2092 if (!(IS_BLANK_CH(str[i]))) return(0);
2093 }
Owen Taylor3473f882001-02-23 17:55:21 +00002094
2095 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002096 * Look if the element is mixed content in the DTD if available
Owen Taylor3473f882001-02-23 17:55:21 +00002097 */
Daniel Veillard6dd398f2001-07-25 22:41:03 +00002098 if (ctxt->node == NULL) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002099 if (ctxt->myDoc != NULL) {
2100 ret = xmlIsMixedElement(ctxt->myDoc, ctxt->node->name);
2101 if (ret == 0) return(1);
2102 if (ret == 1) return(0);
2103 }
2104
2105 /*
2106 * Otherwise, heuristic :-\
2107 */
Daniel Veillard561b7f82002-03-20 21:55:57 +00002108 if (RAW != '<') return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002109 if ((ctxt->node->children == NULL) &&
2110 (RAW == '<') && (NXT(1) == '/')) return(0);
2111
2112 lastChild = xmlGetLastChild(ctxt->node);
2113 if (lastChild == NULL) {
Daniel Veillard7db37732001-07-12 01:20:08 +00002114 if ((ctxt->node->type != XML_ELEMENT_NODE) &&
2115 (ctxt->node->content != NULL)) return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002116 } else if (xmlNodeIsText(lastChild))
2117 return(0);
2118 else if ((ctxt->node->children != NULL) &&
2119 (xmlNodeIsText(ctxt->node->children)))
2120 return(0);
2121 return(1);
2122}
2123
Owen Taylor3473f882001-02-23 17:55:21 +00002124/************************************************************************
2125 * *
2126 * Extra stuff for namespace support *
2127 * Relates to http://www.w3.org/TR/WD-xml-names *
2128 * *
2129 ************************************************************************/
2130
2131/**
2132 * xmlSplitQName:
2133 * @ctxt: an XML parser context
2134 * @name: an XML parser context
2135 * @prefix: a xmlChar **
2136 *
2137 * parse an UTF8 encoded XML qualified name string
2138 *
2139 * [NS 5] QName ::= (Prefix ':')? LocalPart
2140 *
2141 * [NS 6] Prefix ::= NCName
2142 *
2143 * [NS 7] LocalPart ::= NCName
2144 *
2145 * Returns the local part, and prefix is updated
2146 * to get the Prefix if any.
2147 */
2148
2149xmlChar *
2150xmlSplitQName(xmlParserCtxtPtr ctxt, const xmlChar *name, xmlChar **prefix) {
2151 xmlChar buf[XML_MAX_NAMELEN + 5];
2152 xmlChar *buffer = NULL;
2153 int len = 0;
2154 int max = XML_MAX_NAMELEN;
2155 xmlChar *ret = NULL;
2156 const xmlChar *cur = name;
2157 int c;
2158
2159 *prefix = NULL;
2160
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002161 if (cur == NULL) return(NULL);
2162
Owen Taylor3473f882001-02-23 17:55:21 +00002163#ifndef XML_XML_NAMESPACE
2164 /* xml: prefix is not really a namespace */
2165 if ((cur[0] == 'x') && (cur[1] == 'm') &&
2166 (cur[2] == 'l') && (cur[3] == ':'))
2167 return(xmlStrdup(name));
2168#endif
2169
Daniel Veillard597bc482003-07-24 16:08:28 +00002170 /* nasty but well=formed */
Owen Taylor3473f882001-02-23 17:55:21 +00002171 if (cur[0] == ':')
2172 return(xmlStrdup(name));
2173
2174 c = *cur++;
2175 while ((c != 0) && (c != ':') && (len < max)) { /* tested bigname.xml */
2176 buf[len++] = c;
2177 c = *cur++;
2178 }
2179 if (len >= max) {
2180 /*
2181 * Okay someone managed to make a huge name, so he's ready to pay
2182 * for the processing speed.
2183 */
2184 max = len * 2;
2185
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002186 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002187 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002188 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002189 return(NULL);
2190 }
2191 memcpy(buffer, buf, len);
2192 while ((c != 0) && (c != ':')) { /* tested bigname.xml */
2193 if (len + 10 > max) {
2194 max *= 2;
2195 buffer = (xmlChar *) xmlRealloc(buffer,
2196 max * sizeof(xmlChar));
2197 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002198 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002199 return(NULL);
2200 }
2201 }
2202 buffer[len++] = c;
2203 c = *cur++;
2204 }
2205 buffer[len] = 0;
2206 }
2207
Daniel Veillard597bc482003-07-24 16:08:28 +00002208 /* nasty but well=formed
2209 if ((c == ':') && (*cur == 0)) {
2210 return(xmlStrdup(name));
2211 } */
2212
Owen Taylor3473f882001-02-23 17:55:21 +00002213 if (buffer == NULL)
2214 ret = xmlStrndup(buf, len);
2215 else {
2216 ret = buffer;
2217 buffer = NULL;
2218 max = XML_MAX_NAMELEN;
2219 }
2220
2221
2222 if (c == ':') {
Daniel Veillardbb284f42002-10-16 18:02:47 +00002223 c = *cur;
Owen Taylor3473f882001-02-23 17:55:21 +00002224 *prefix = ret;
Daniel Veillard597bc482003-07-24 16:08:28 +00002225 if (c == 0) {
Daniel Veillard8d73bcb2003-08-04 01:06:15 +00002226 return(xmlStrndup(BAD_CAST "", 0));
Daniel Veillard597bc482003-07-24 16:08:28 +00002227 }
Owen Taylor3473f882001-02-23 17:55:21 +00002228 len = 0;
2229
Daniel Veillardbb284f42002-10-16 18:02:47 +00002230 /*
2231 * Check that the first character is proper to start
2232 * a new name
2233 */
2234 if (!(((c >= 0x61) && (c <= 0x7A)) ||
2235 ((c >= 0x41) && (c <= 0x5A)) ||
2236 (c == '_') || (c == ':'))) {
2237 int l;
2238 int first = CUR_SCHAR(cur, l);
2239
2240 if (!IS_LETTER(first) && (first != '_')) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00002241 xmlFatalErrMsgStr(ctxt, XML_NS_ERR_QNAME,
Daniel Veillardbb284f42002-10-16 18:02:47 +00002242 "Name %s is not XML Namespace compliant\n",
Daniel Veillardbc92eca2003-09-15 09:48:06 +00002243 name);
Daniel Veillardbb284f42002-10-16 18:02:47 +00002244 }
2245 }
2246 cur++;
2247
Owen Taylor3473f882001-02-23 17:55:21 +00002248 while ((c != 0) && (len < max)) { /* tested bigname2.xml */
2249 buf[len++] = c;
2250 c = *cur++;
2251 }
2252 if (len >= max) {
2253 /*
2254 * Okay someone managed to make a huge name, so he's ready to pay
2255 * for the processing speed.
2256 */
2257 max = len * 2;
2258
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002259 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002260 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002261 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002262 return(NULL);
2263 }
2264 memcpy(buffer, buf, len);
2265 while (c != 0) { /* tested bigname2.xml */
2266 if (len + 10 > max) {
2267 max *= 2;
2268 buffer = (xmlChar *) xmlRealloc(buffer,
2269 max * sizeof(xmlChar));
2270 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002271 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002272 return(NULL);
2273 }
2274 }
2275 buffer[len++] = c;
2276 c = *cur++;
2277 }
2278 buffer[len] = 0;
2279 }
2280
2281 if (buffer == NULL)
2282 ret = xmlStrndup(buf, len);
2283 else {
2284 ret = buffer;
2285 }
2286 }
2287
2288 return(ret);
2289}
2290
2291/************************************************************************
2292 * *
2293 * The parser itself *
2294 * Relates to http://www.w3.org/TR/REC-xml *
2295 * *
2296 ************************************************************************/
2297
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002298static const xmlChar * xmlParseNameComplex(xmlParserCtxtPtr ctxt);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002299static xmlChar * xmlParseAttValueInternal(xmlParserCtxtPtr ctxt,
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002300 int *len, int *alloc, int normalize);
Daniel Veillard0fb18932003-09-07 09:14:37 +00002301
Owen Taylor3473f882001-02-23 17:55:21 +00002302/**
2303 * xmlParseName:
2304 * @ctxt: an XML parser context
2305 *
2306 * parse an XML name.
2307 *
2308 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
2309 * CombiningChar | Extender
2310 *
2311 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
2312 *
2313 * [6] Names ::= Name (S Name)*
2314 *
2315 * Returns the Name parsed or NULL
2316 */
2317
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002318const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002319xmlParseName(xmlParserCtxtPtr ctxt) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00002320 const xmlChar *in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002321 const xmlChar *ret;
Owen Taylor3473f882001-02-23 17:55:21 +00002322 int count = 0;
2323
2324 GROW;
Daniel Veillard48b2f892001-02-25 16:11:03 +00002325
2326 /*
2327 * Accelerator for simple ASCII names
2328 */
2329 in = ctxt->input->cur;
2330 if (((*in >= 0x61) && (*in <= 0x7A)) ||
2331 ((*in >= 0x41) && (*in <= 0x5A)) ||
2332 (*in == '_') || (*in == ':')) {
2333 in++;
2334 while (((*in >= 0x61) && (*in <= 0x7A)) ||
2335 ((*in >= 0x41) && (*in <= 0x5A)) ||
2336 ((*in >= 0x30) && (*in <= 0x39)) ||
Daniel Veillard76d66f42001-05-16 21:05:17 +00002337 (*in == '_') || (*in == '-') ||
2338 (*in == ':') || (*in == '.'))
Daniel Veillard48b2f892001-02-25 16:11:03 +00002339 in++;
Daniel Veillard76d66f42001-05-16 21:05:17 +00002340 if ((*in > 0) && (*in < 0x80)) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00002341 count = in - ctxt->input->cur;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002342 ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002343 ctxt->input->cur = in;
Daniel Veillard77a90a72003-03-22 00:04:05 +00002344 ctxt->nbChars += count;
2345 ctxt->input->col += count;
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002346 if (ret == NULL)
2347 xmlErrMemory(ctxt, NULL);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002348 return(ret);
2349 }
2350 }
Daniel Veillard2f362242001-03-02 17:36:21 +00002351 return(xmlParseNameComplex(ctxt));
Daniel Veillard21a0f912001-02-25 19:54:14 +00002352}
Daniel Veillard48b2f892001-02-25 16:11:03 +00002353
Daniel Veillard46de64e2002-05-29 08:21:33 +00002354/**
2355 * xmlParseNameAndCompare:
2356 * @ctxt: an XML parser context
2357 *
2358 * parse an XML name and compares for match
2359 * (specialized for endtag parsing)
2360 *
Daniel Veillard46de64e2002-05-29 08:21:33 +00002361 * Returns NULL for an illegal name, (xmlChar*) 1 for success
2362 * and the name for mismatch
2363 */
2364
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002365static const xmlChar *
Daniel Veillard46de64e2002-05-29 08:21:33 +00002366xmlParseNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *other) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00002367 register const xmlChar *cmp = other;
2368 register const xmlChar *in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002369 const xmlChar *ret;
Daniel Veillard46de64e2002-05-29 08:21:33 +00002370
2371 GROW;
2372
2373 in = ctxt->input->cur;
2374 while (*in != 0 && *in == *cmp) {
2375 ++in;
2376 ++cmp;
2377 }
William M. Brack76e95df2003-10-18 16:20:14 +00002378 if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
Daniel Veillard46de64e2002-05-29 08:21:33 +00002379 /* success */
2380 ctxt->input->cur = in;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002381 return (const xmlChar*) 1;
Daniel Veillard46de64e2002-05-29 08:21:33 +00002382 }
2383 /* failure (or end of input buffer), check with full function */
2384 ret = xmlParseName (ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00002385 /* strings coming from the dictionnary direct compare possible */
2386 if (ret == other) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002387 return (const xmlChar*) 1;
Daniel Veillard46de64e2002-05-29 08:21:33 +00002388 }
2389 return ret;
2390}
2391
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002392static const xmlChar *
Daniel Veillard21a0f912001-02-25 19:54:14 +00002393xmlParseNameComplex(xmlParserCtxtPtr ctxt) {
Daniel Veillard21a0f912001-02-25 19:54:14 +00002394 int len = 0, l;
2395 int c;
2396 int count = 0;
2397
2398 /*
2399 * Handler for more complex cases
2400 */
2401 GROW;
Owen Taylor3473f882001-02-23 17:55:21 +00002402 c = CUR_CHAR(l);
2403 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
2404 (!IS_LETTER(c) && (c != '_') &&
2405 (c != ':'))) {
2406 return(NULL);
2407 }
2408
2409 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
William M. Brack871611b2003-10-18 04:53:14 +00002410 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
Owen Taylor3473f882001-02-23 17:55:21 +00002411 (c == '.') || (c == '-') ||
2412 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002413 (IS_COMBINING(c)) ||
2414 (IS_EXTENDER(c)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002415 if (count++ > 100) {
2416 count = 0;
2417 GROW;
2418 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002419 len += l;
Owen Taylor3473f882001-02-23 17:55:21 +00002420 NEXTL(l);
2421 c = CUR_CHAR(l);
Owen Taylor3473f882001-02-23 17:55:21 +00002422 }
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002423 return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len));
Owen Taylor3473f882001-02-23 17:55:21 +00002424}
2425
2426/**
2427 * xmlParseStringName:
2428 * @ctxt: an XML parser context
2429 * @str: a pointer to the string pointer (IN/OUT)
2430 *
2431 * parse an XML name.
2432 *
2433 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
2434 * CombiningChar | Extender
2435 *
2436 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
2437 *
2438 * [6] Names ::= Name (S Name)*
2439 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002440 * Returns the Name parsed or NULL. The @str pointer
Owen Taylor3473f882001-02-23 17:55:21 +00002441 * is updated to the current location in the string.
2442 */
2443
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002444static xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00002445xmlParseStringName(xmlParserCtxtPtr ctxt, const xmlChar** str) {
2446 xmlChar buf[XML_MAX_NAMELEN + 5];
2447 const xmlChar *cur = *str;
2448 int len = 0, l;
2449 int c;
2450
2451 c = CUR_SCHAR(cur, l);
William M. Brack871611b2003-10-18 04:53:14 +00002452 if (!IS_LETTER(c) && (c != '_') &&
Owen Taylor3473f882001-02-23 17:55:21 +00002453 (c != ':')) {
2454 return(NULL);
2455 }
2456
William M. Brack871611b2003-10-18 04:53:14 +00002457 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigentname.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002458 (c == '.') || (c == '-') ||
2459 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002460 (IS_COMBINING(c)) ||
2461 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002462 COPY_BUF(l,buf,len,c);
2463 cur += l;
2464 c = CUR_SCHAR(cur, l);
2465 if (len >= XML_MAX_NAMELEN) { /* test bigentname.xml */
2466 /*
2467 * Okay someone managed to make a huge name, so he's ready to pay
2468 * for the processing speed.
2469 */
2470 xmlChar *buffer;
2471 int max = len * 2;
2472
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002473 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002474 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002475 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002476 return(NULL);
2477 }
2478 memcpy(buffer, buf, len);
William M. Brack871611b2003-10-18 04:53:14 +00002479 while ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
Daniel Veillard73b013f2003-09-30 12:36:01 +00002480 /* test bigentname.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002481 (c == '.') || (c == '-') ||
2482 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002483 (IS_COMBINING(c)) ||
2484 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002485 if (len + 10 > max) {
2486 max *= 2;
2487 buffer = (xmlChar *) xmlRealloc(buffer,
2488 max * sizeof(xmlChar));
2489 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002490 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002491 return(NULL);
2492 }
2493 }
2494 COPY_BUF(l,buffer,len,c);
2495 cur += l;
2496 c = CUR_SCHAR(cur, l);
2497 }
2498 buffer[len] = 0;
2499 *str = cur;
2500 return(buffer);
2501 }
2502 }
2503 *str = cur;
2504 return(xmlStrndup(buf, len));
2505}
2506
2507/**
2508 * xmlParseNmtoken:
2509 * @ctxt: an XML parser context
2510 *
2511 * parse an XML Nmtoken.
2512 *
2513 * [7] Nmtoken ::= (NameChar)+
2514 *
2515 * [8] Nmtokens ::= Nmtoken (S Nmtoken)*
2516 *
2517 * Returns the Nmtoken parsed or NULL
2518 */
2519
2520xmlChar *
2521xmlParseNmtoken(xmlParserCtxtPtr ctxt) {
2522 xmlChar buf[XML_MAX_NAMELEN + 5];
2523 int len = 0, l;
2524 int c;
2525 int count = 0;
2526
2527 GROW;
2528 c = CUR_CHAR(l);
2529
William M. Brack871611b2003-10-18 04:53:14 +00002530 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigtoken.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002531 (c == '.') || (c == '-') ||
2532 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002533 (IS_COMBINING(c)) ||
2534 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002535 if (count++ > 100) {
2536 count = 0;
2537 GROW;
2538 }
2539 COPY_BUF(l,buf,len,c);
2540 NEXTL(l);
2541 c = CUR_CHAR(l);
2542 if (len >= XML_MAX_NAMELEN) {
2543 /*
2544 * Okay someone managed to make a huge token, so he's ready to pay
2545 * for the processing speed.
2546 */
2547 xmlChar *buffer;
2548 int max = len * 2;
2549
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002550 buffer = (xmlChar *) xmlMallocAtomic(max * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002551 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002552 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002553 return(NULL);
2554 }
2555 memcpy(buffer, buf, len);
William M. Brack871611b2003-10-18 04:53:14 +00002556 while ((IS_LETTER(c)) || (IS_DIGIT(c)) || /* test bigtoken.xml */
Owen Taylor3473f882001-02-23 17:55:21 +00002557 (c == '.') || (c == '-') ||
2558 (c == '_') || (c == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00002559 (IS_COMBINING(c)) ||
2560 (IS_EXTENDER(c))) {
Owen Taylor3473f882001-02-23 17:55:21 +00002561 if (count++ > 100) {
2562 count = 0;
2563 GROW;
2564 }
2565 if (len + 10 > max) {
2566 max *= 2;
2567 buffer = (xmlChar *) xmlRealloc(buffer,
2568 max * sizeof(xmlChar));
2569 if (buffer == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002570 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002571 return(NULL);
2572 }
2573 }
2574 COPY_BUF(l,buffer,len,c);
2575 NEXTL(l);
2576 c = CUR_CHAR(l);
2577 }
2578 buffer[len] = 0;
2579 return(buffer);
2580 }
2581 }
2582 if (len == 0)
2583 return(NULL);
2584 return(xmlStrndup(buf, len));
2585}
2586
2587/**
2588 * xmlParseEntityValue:
2589 * @ctxt: an XML parser context
2590 * @orig: if non-NULL store a copy of the original entity value
2591 *
2592 * parse a value for ENTITY declarations
2593 *
2594 * [9] EntityValue ::= '"' ([^%&"] | PEReference | Reference)* '"' |
2595 * "'" ([^%&'] | PEReference | Reference)* "'"
2596 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002597 * Returns the EntityValue parsed with reference substituted or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002598 */
2599
2600xmlChar *
2601xmlParseEntityValue(xmlParserCtxtPtr ctxt, xmlChar **orig) {
2602 xmlChar *buf = NULL;
2603 int len = 0;
2604 int size = XML_PARSER_BUFFER_SIZE;
2605 int c, l;
2606 xmlChar stop;
2607 xmlChar *ret = NULL;
2608 const xmlChar *cur = NULL;
2609 xmlParserInputPtr input;
2610
2611 if (RAW == '"') stop = '"';
2612 else if (RAW == '\'') stop = '\'';
2613 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002614 xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002615 return(NULL);
2616 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002617 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002618 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002619 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002620 return(NULL);
2621 }
2622
2623 /*
2624 * The content of the entity definition is copied in a buffer.
2625 */
2626
2627 ctxt->instate = XML_PARSER_ENTITY_VALUE;
2628 input = ctxt->input;
2629 GROW;
2630 NEXT;
2631 c = CUR_CHAR(l);
2632 /*
2633 * NOTE: 4.4.5 Included in Literal
2634 * When a parameter entity reference appears in a literal entity
2635 * value, ... a single or double quote character in the replacement
2636 * text is always treated as a normal data character and will not
2637 * terminate the literal.
2638 * In practice it means we stop the loop only when back at parsing
2639 * the initial entity and the quote is found
2640 */
William M. Brack871611b2003-10-18 04:53:14 +00002641 while ((IS_CHAR(c)) && ((c != stop) || /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00002642 (ctxt->input != input))) {
2643 if (len + 5 >= size) {
2644 size *= 2;
2645 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
2646 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002647 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002648 return(NULL);
2649 }
2650 }
2651 COPY_BUF(l,buf,len,c);
2652 NEXTL(l);
2653 /*
2654 * Pop-up of finished entities.
2655 */
2656 while ((RAW == 0) && (ctxt->inputNr > 1)) /* non input consuming */
2657 xmlPopInput(ctxt);
2658
2659 GROW;
2660 c = CUR_CHAR(l);
2661 if (c == 0) {
2662 GROW;
2663 c = CUR_CHAR(l);
2664 }
2665 }
2666 buf[len] = 0;
2667
2668 /*
2669 * Raise problem w.r.t. '&' and '%' being used in non-entities
2670 * reference constructs. Note Charref will be handled in
2671 * xmlStringDecodeEntities()
2672 */
2673 cur = buf;
2674 while (*cur != 0) { /* non input consuming */
2675 if ((*cur == '%') || ((*cur == '&') && (cur[1] != '#'))) {
2676 xmlChar *name;
2677 xmlChar tmp = *cur;
2678
2679 cur++;
2680 name = xmlParseStringName(ctxt, &cur);
2681 if ((name == NULL) || (*cur != ';')) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00002682 xmlFatalErrMsgInt(ctxt, XML_ERR_ENTITY_CHAR_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00002683 "EntityValue: '%c' forbidden except for entities references\n",
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00002684 tmp);
Owen Taylor3473f882001-02-23 17:55:21 +00002685 }
Daniel Veillard5151c062001-10-23 13:10:19 +00002686 if ((tmp == '%') && (ctxt->inSubset == 1) &&
2687 (ctxt->inputNr == 1)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002688 xmlFatalErr(ctxt, XML_ERR_ENTITY_PE_INTERNAL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002689 }
2690 if (name != NULL)
2691 xmlFree(name);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00002692 if (*cur == 0)
2693 break;
Owen Taylor3473f882001-02-23 17:55:21 +00002694 }
2695 cur++;
2696 }
2697
2698 /*
2699 * Then PEReference entities are substituted.
2700 */
2701 if (c != stop) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002702 xmlFatalErr(ctxt, XML_ERR_ENTITY_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002703 xmlFree(buf);
2704 } else {
2705 NEXT;
2706 /*
2707 * NOTE: 4.4.7 Bypassed
2708 * When a general entity reference appears in the EntityValue in
2709 * an entity declaration, it is bypassed and left as is.
2710 * so XML_SUBSTITUTE_REF is not set here.
2711 */
2712 ret = xmlStringDecodeEntities(ctxt, buf, XML_SUBSTITUTE_PEREF,
2713 0, 0, 0);
2714 if (orig != NULL)
2715 *orig = buf;
2716 else
2717 xmlFree(buf);
2718 }
2719
2720 return(ret);
2721}
2722
2723/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002724 * xmlParseAttValueComplex:
2725 * @ctxt: an XML parser context
Daniel Veillard0fb18932003-09-07 09:14:37 +00002726 * @len: the resulting attribute len
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002727 * @normalize: wether to apply the inner normalization
Daniel Veillard01c13b52002-12-10 15:19:08 +00002728 *
2729 * parse a value for an attribute, this is the fallback function
2730 * of xmlParseAttValue() when the attribute parsing requires handling
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002731 * of non-ASCII characters, or normalization compaction.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002732 *
2733 * Returns the AttValue parsed or NULL. The value has to be freed by the caller.
2734 */
Daniel Veillard0fb18932003-09-07 09:14:37 +00002735static xmlChar *
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002736xmlParseAttValueComplex(xmlParserCtxtPtr ctxt, int *attlen, int normalize) {
Daniel Veillarde72c7562002-05-31 09:47:30 +00002737 xmlChar limit = 0;
2738 xmlChar *buf = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002739 int len = 0;
2740 int buf_size = 0;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002741 int c, l, in_space = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002742 xmlChar *current = NULL;
2743 xmlEntityPtr ent;
2744
Owen Taylor3473f882001-02-23 17:55:21 +00002745 if (NXT(0) == '"') {
2746 ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
2747 limit = '"';
2748 NEXT;
2749 } else if (NXT(0) == '\'') {
2750 limit = '\'';
2751 ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
2752 NEXT;
2753 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002754 xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002755 return(NULL);
2756 }
2757
2758 /*
2759 * allocate a translation buffer.
2760 */
2761 buf_size = XML_PARSER_BUFFER_SIZE;
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002762 buf = (xmlChar *) xmlMallocAtomic(buf_size * sizeof(xmlChar));
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002763 if (buf == NULL) goto mem_error;
Owen Taylor3473f882001-02-23 17:55:21 +00002764
2765 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002766 * OK loop until we reach one of the ending char or a size limit.
Owen Taylor3473f882001-02-23 17:55:21 +00002767 */
2768 c = CUR_CHAR(l);
Daniel Veillardfdc91562002-07-01 21:52:03 +00002769 while ((NXT(0) != limit) && /* checked */
2770 (c != '<')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002771 if (c == 0) break;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002772 if (c == '&') {
Daniel Veillard62998c02003-09-15 12:56:36 +00002773 in_space = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002774 if (NXT(1) == '#') {
2775 int val = xmlParseCharRef(ctxt);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002776
Owen Taylor3473f882001-02-23 17:55:21 +00002777 if (val == '&') {
Daniel Veillard319a7422001-09-11 09:27:09 +00002778 if (ctxt->replaceEntities) {
2779 if (len > buf_size - 10) {
2780 growBuffer(buf);
2781 }
2782 buf[len++] = '&';
2783 } else {
2784 /*
2785 * The reparsing will be done in xmlStringGetNodeList()
2786 * called by the attribute() function in SAX.c
2787 */
Daniel Veillard319a7422001-09-11 09:27:09 +00002788 if (len > buf_size - 10) {
2789 growBuffer(buf);
2790 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002791 buf[len++] = '&';
2792 buf[len++] = '#';
2793 buf[len++] = '3';
2794 buf[len++] = '8';
2795 buf[len++] = ';';
Owen Taylor3473f882001-02-23 17:55:21 +00002796 }
2797 } else {
Daniel Veillard0b6b55b2001-03-20 11:27:34 +00002798 if (len > buf_size - 10) {
2799 growBuffer(buf);
2800 }
Owen Taylor3473f882001-02-23 17:55:21 +00002801 len += xmlCopyChar(0, &buf[len], val);
2802 }
2803 } else {
2804 ent = xmlParseEntityRef(ctxt);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002805 if ((ent != NULL) &&
2806 (ent->etype == XML_INTERNAL_PREDEFINED_ENTITY)) {
2807 if (len > buf_size - 10) {
2808 growBuffer(buf);
2809 }
2810 if ((ctxt->replaceEntities == 0) &&
2811 (ent->content[0] == '&')) {
2812 buf[len++] = '&';
2813 buf[len++] = '#';
2814 buf[len++] = '3';
2815 buf[len++] = '8';
2816 buf[len++] = ';';
2817 } else {
2818 buf[len++] = ent->content[0];
2819 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002820 } else if ((ent != NULL) &&
2821 (ctxt->replaceEntities != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00002822 xmlChar *rep;
2823
2824 if (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) {
2825 rep = xmlStringDecodeEntities(ctxt, ent->content,
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002826 XML_SUBSTITUTE_REF,
2827 0, 0, 0);
Owen Taylor3473f882001-02-23 17:55:21 +00002828 if (rep != NULL) {
2829 current = rep;
2830 while (*current != 0) { /* non input consuming */
2831 buf[len++] = *current++;
2832 if (len > buf_size - 10) {
2833 growBuffer(buf);
2834 }
2835 }
2836 xmlFree(rep);
2837 }
2838 } else {
Daniel Veillard0b6b55b2001-03-20 11:27:34 +00002839 if (len > buf_size - 10) {
2840 growBuffer(buf);
2841 }
Owen Taylor3473f882001-02-23 17:55:21 +00002842 if (ent->content != NULL)
2843 buf[len++] = ent->content[0];
2844 }
2845 } else if (ent != NULL) {
2846 int i = xmlStrlen(ent->name);
2847 const xmlChar *cur = ent->name;
2848
2849 /*
2850 * This may look absurd but is needed to detect
2851 * entities problems
2852 */
2853 if ((ent->etype != XML_INTERNAL_PREDEFINED_ENTITY) &&
2854 (ent->content != NULL)) {
2855 xmlChar *rep;
2856 rep = xmlStringDecodeEntities(ctxt, ent->content,
2857 XML_SUBSTITUTE_REF, 0, 0, 0);
2858 if (rep != NULL)
2859 xmlFree(rep);
2860 }
2861
2862 /*
2863 * Just output the reference
2864 */
2865 buf[len++] = '&';
2866 if (len > buf_size - i - 10) {
2867 growBuffer(buf);
2868 }
2869 for (;i > 0;i--)
2870 buf[len++] = *cur++;
2871 buf[len++] = ';';
2872 }
2873 }
2874 } else {
2875 if ((c == 0x20) || (c == 0xD) || (c == 0xA) || (c == 0x9)) {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002876 if ((len != 0) || (!normalize)) {
2877 if ((!normalize) || (!in_space)) {
2878 COPY_BUF(l,buf,len,0x20);
2879 if (len > buf_size - 10) {
2880 growBuffer(buf);
2881 }
2882 }
2883 in_space = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002884 }
2885 } else {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002886 in_space = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002887 COPY_BUF(l,buf,len,c);
2888 if (len > buf_size - 10) {
2889 growBuffer(buf);
2890 }
2891 }
2892 NEXTL(l);
2893 }
2894 GROW;
2895 c = CUR_CHAR(l);
2896 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002897 if ((in_space) && (normalize)) {
2898 while (buf[len - 1] == 0x20) len--;
2899 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00002900 buf[len] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002901 if (RAW == '<') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002902 xmlFatalErr(ctxt, XML_ERR_LT_IN_ATTRIBUTE, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002903 } else if (RAW != limit) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00002904 xmlFatalErrMsg(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
2905 "AttValue: ' expected\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002906 } else
2907 NEXT;
Daniel Veillard0fb18932003-09-07 09:14:37 +00002908 if (attlen != NULL) *attlen = len;
Owen Taylor3473f882001-02-23 17:55:21 +00002909 return(buf);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002910
2911mem_error:
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002912 xmlErrMemory(ctxt, NULL);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002913 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002914}
2915
2916/**
Daniel Veillard0fb18932003-09-07 09:14:37 +00002917 * xmlParseAttValue:
2918 * @ctxt: an XML parser context
2919 *
2920 * parse a value for an attribute
2921 * Note: the parser won't do substitution of entities here, this
2922 * will be handled later in xmlStringGetNodeList
2923 *
2924 * [10] AttValue ::= '"' ([^<&"] | Reference)* '"' |
2925 * "'" ([^<&'] | Reference)* "'"
2926 *
2927 * 3.3.3 Attribute-Value Normalization:
2928 * Before the value of an attribute is passed to the application or
2929 * checked for validity, the XML processor must normalize it as follows:
2930 * - a character reference is processed by appending the referenced
2931 * character to the attribute value
2932 * - an entity reference is processed by recursively processing the
2933 * replacement text of the entity
2934 * - a whitespace character (#x20, #xD, #xA, #x9) is processed by
2935 * appending #x20 to the normalized value, except that only a single
2936 * #x20 is appended for a "#xD#xA" sequence that is part of an external
2937 * parsed entity or the literal entity value of an internal parsed entity
2938 * - other characters are processed by appending them to the normalized value
2939 * If the declared value is not CDATA, then the XML processor must further
2940 * process the normalized attribute value by discarding any leading and
2941 * trailing space (#x20) characters, and by replacing sequences of space
2942 * (#x20) characters by a single space (#x20) character.
2943 * All attributes for which no declaration has been read should be treated
2944 * by a non-validating parser as if declared CDATA.
2945 *
2946 * Returns the AttValue parsed or NULL. The value has to be freed by the caller.
2947 */
2948
2949
2950xmlChar *
2951xmlParseAttValue(xmlParserCtxtPtr ctxt) {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002952 return(xmlParseAttValueInternal(ctxt, NULL, NULL, 0));
Daniel Veillard0fb18932003-09-07 09:14:37 +00002953}
2954
2955/**
Owen Taylor3473f882001-02-23 17:55:21 +00002956 * xmlParseSystemLiteral:
2957 * @ctxt: an XML parser context
2958 *
2959 * parse an XML Literal
2960 *
2961 * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
2962 *
2963 * Returns the SystemLiteral parsed or NULL
2964 */
2965
2966xmlChar *
2967xmlParseSystemLiteral(xmlParserCtxtPtr ctxt) {
2968 xmlChar *buf = NULL;
2969 int len = 0;
2970 int size = XML_PARSER_BUFFER_SIZE;
2971 int cur, l;
2972 xmlChar stop;
2973 int state = ctxt->instate;
2974 int count = 0;
2975
2976 SHRINK;
2977 if (RAW == '"') {
2978 NEXT;
2979 stop = '"';
2980 } else if (RAW == '\'') {
2981 NEXT;
2982 stop = '\'';
2983 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002984 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002985 return(NULL);
2986 }
2987
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002988 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00002989 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00002990 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00002991 return(NULL);
2992 }
2993 ctxt->instate = XML_PARSER_SYSTEM_LITERAL;
2994 cur = CUR_CHAR(l);
William M. Brack871611b2003-10-18 04:53:14 +00002995 while ((IS_CHAR(cur)) && (cur != stop)) { /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00002996 if (len + 5 >= size) {
2997 size *= 2;
2998 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
2999 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003000 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003001 ctxt->instate = (xmlParserInputState) state;
3002 return(NULL);
3003 }
3004 }
3005 count++;
3006 if (count > 50) {
3007 GROW;
3008 count = 0;
3009 }
3010 COPY_BUF(l,buf,len,cur);
3011 NEXTL(l);
3012 cur = CUR_CHAR(l);
3013 if (cur == 0) {
3014 GROW;
3015 SHRINK;
3016 cur = CUR_CHAR(l);
3017 }
3018 }
3019 buf[len] = 0;
3020 ctxt->instate = (xmlParserInputState) state;
William M. Brack871611b2003-10-18 04:53:14 +00003021 if (!IS_CHAR(cur)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003022 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003023 } else {
3024 NEXT;
3025 }
3026 return(buf);
3027}
3028
3029/**
3030 * xmlParsePubidLiteral:
3031 * @ctxt: an XML parser context
3032 *
3033 * parse an XML public literal
3034 *
3035 * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
3036 *
3037 * Returns the PubidLiteral parsed or NULL.
3038 */
3039
3040xmlChar *
3041xmlParsePubidLiteral(xmlParserCtxtPtr ctxt) {
3042 xmlChar *buf = NULL;
3043 int len = 0;
3044 int size = XML_PARSER_BUFFER_SIZE;
3045 xmlChar cur;
3046 xmlChar stop;
3047 int count = 0;
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003048 xmlParserInputState oldstate = ctxt->instate;
Owen Taylor3473f882001-02-23 17:55:21 +00003049
3050 SHRINK;
3051 if (RAW == '"') {
3052 NEXT;
3053 stop = '"';
3054 } else if (RAW == '\'') {
3055 NEXT;
3056 stop = '\'';
3057 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003058 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003059 return(NULL);
3060 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003061 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003062 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003063 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003064 return(NULL);
3065 }
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003066 ctxt->instate = XML_PARSER_PUBLIC_LITERAL;
Owen Taylor3473f882001-02-23 17:55:21 +00003067 cur = CUR;
William M. Brack76e95df2003-10-18 16:20:14 +00003068 while ((IS_PUBIDCHAR_CH(cur)) && (cur != stop)) { /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003069 if (len + 1 >= size) {
3070 size *= 2;
3071 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3072 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003073 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003074 return(NULL);
3075 }
3076 }
3077 buf[len++] = cur;
3078 count++;
3079 if (count > 50) {
3080 GROW;
3081 count = 0;
3082 }
3083 NEXT;
3084 cur = CUR;
3085 if (cur == 0) {
3086 GROW;
3087 SHRINK;
3088 cur = CUR;
3089 }
3090 }
3091 buf[len] = 0;
3092 if (cur != stop) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003093 xmlFatalErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003094 } else {
3095 NEXT;
3096 }
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003097 ctxt->instate = oldstate;
Owen Taylor3473f882001-02-23 17:55:21 +00003098 return(buf);
3099}
3100
Daniel Veillard48b2f892001-02-25 16:11:03 +00003101void xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata);
Owen Taylor3473f882001-02-23 17:55:21 +00003102/**
3103 * xmlParseCharData:
3104 * @ctxt: an XML parser context
3105 * @cdata: int indicating whether we are within a CDATA section
3106 *
3107 * parse a CharData section.
3108 * if we are within a CDATA section ']]>' marks an end of section.
3109 *
3110 * The right angle bracket (>) may be represented using the string "&gt;",
3111 * and must, for compatibility, be escaped using "&gt;" or a character
3112 * reference when it appears in the string "]]>" in content, when that
3113 * string is not marking the end of a CDATA section.
3114 *
3115 * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
3116 */
3117
3118void
3119xmlParseCharData(xmlParserCtxtPtr ctxt, int cdata) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00003120 const xmlChar *in;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003121 int nbchar = 0;
Daniel Veillard50582112001-03-26 22:52:16 +00003122 int line = ctxt->input->line;
3123 int col = ctxt->input->col;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003124
3125 SHRINK;
3126 GROW;
3127 /*
3128 * Accelerated common case where input don't need to be
3129 * modified before passing it to the handler.
3130 */
Daniel Veillardfdc91562002-07-01 21:52:03 +00003131 if (!cdata) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00003132 in = ctxt->input->cur;
3133 do {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003134get_more_space:
3135 while (*in == 0x20) in++;
3136 if (*in == 0xA) {
3137 ctxt->input->line++;
3138 in++;
3139 while (*in == 0xA) {
3140 ctxt->input->line++;
3141 in++;
3142 }
3143 goto get_more_space;
3144 }
3145 if (*in == '<') {
3146 nbchar = in - ctxt->input->cur;
3147 if (nbchar > 0) {
3148 const xmlChar *tmp = ctxt->input->cur;
3149 ctxt->input->cur = in;
3150
3151 if (ctxt->sax->ignorableWhitespace !=
3152 ctxt->sax->characters) {
3153 if (areBlanks(ctxt, tmp, nbchar, 1)) {
3154 ctxt->sax->ignorableWhitespace(ctxt->userData,
3155 tmp, nbchar);
3156 } else if (ctxt->sax->characters != NULL)
3157 ctxt->sax->characters(ctxt->userData,
3158 tmp, nbchar);
3159 } else if (ctxt->sax->characters != NULL) {
3160 ctxt->sax->characters(ctxt->userData,
3161 tmp, nbchar);
3162 }
3163 }
3164 return;
3165 }
Daniel Veillard3ed155f2001-04-29 19:56:59 +00003166get_more:
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003167 while (((*in > ']') && (*in <= 0x7F)) ||
3168 ((*in > '&') && (*in < '<')) ||
3169 ((*in > '<') && (*in < ']')) ||
3170 ((*in >= 0x20) && (*in < '&')) ||
3171 (*in == 0x09))
3172 in++;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003173 if (*in == 0xA) {
Daniel Veillard48b2f892001-02-25 16:11:03 +00003174 ctxt->input->line++;
Daniel Veillard3ed155f2001-04-29 19:56:59 +00003175 in++;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003176 while (*in == 0xA) {
Daniel Veillard3ed155f2001-04-29 19:56:59 +00003177 ctxt->input->line++;
3178 in++;
3179 }
3180 goto get_more;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003181 }
3182 if (*in == ']') {
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003183 if ((in[1] == ']') && (in[2] == '>')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003184 xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003185 ctxt->input->cur = in;
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003186 return;
3187 }
3188 in++;
3189 goto get_more;
3190 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00003191 nbchar = in - ctxt->input->cur;
Daniel Veillard80f32572001-03-07 19:45:40 +00003192 if (nbchar > 0) {
Daniel Veillard40412cd2003-09-03 13:28:32 +00003193 if ((ctxt->sax->ignorableWhitespace !=
3194 ctxt->sax->characters) &&
William M. Brack76e95df2003-10-18 16:20:14 +00003195 (IS_BLANK_CH(*ctxt->input->cur))) {
Daniel Veillarda7374592001-05-10 14:17:55 +00003196 const xmlChar *tmp = ctxt->input->cur;
3197 ctxt->input->cur = in;
Daniel Veillard40412cd2003-09-03 13:28:32 +00003198
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003199 if (areBlanks(ctxt, tmp, nbchar, 0)) {
Daniel Veillard40412cd2003-09-03 13:28:32 +00003200 ctxt->sax->ignorableWhitespace(ctxt->userData,
3201 tmp, nbchar);
3202 } else if (ctxt->sax->characters != NULL)
3203 ctxt->sax->characters(ctxt->userData,
3204 tmp, nbchar);
Daniel Veillard3ed27bd2001-06-17 17:58:17 +00003205 line = ctxt->input->line;
3206 col = ctxt->input->col;
Daniel Veillard80f32572001-03-07 19:45:40 +00003207 } else {
3208 if (ctxt->sax->characters != NULL)
3209 ctxt->sax->characters(ctxt->userData,
3210 ctxt->input->cur, nbchar);
Daniel Veillard3ed27bd2001-06-17 17:58:17 +00003211 line = ctxt->input->line;
3212 col = ctxt->input->col;
Daniel Veillard80f32572001-03-07 19:45:40 +00003213 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00003214 }
3215 ctxt->input->cur = in;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003216 if (*in == 0xD) {
3217 in++;
3218 if (*in == 0xA) {
3219 ctxt->input->cur = in;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003220 in++;
Daniel Veillard561b7f82002-03-20 21:55:57 +00003221 ctxt->input->line++;
3222 continue; /* while */
Daniel Veillard48b2f892001-02-25 16:11:03 +00003223 }
Daniel Veillard561b7f82002-03-20 21:55:57 +00003224 in--;
3225 }
3226 if (*in == '<') {
3227 return;
3228 }
3229 if (*in == '&') {
3230 return;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003231 }
3232 SHRINK;
3233 GROW;
3234 in = ctxt->input->cur;
William M. Brackc07329e2003-09-08 01:57:30 +00003235 } while (((*in >= 0x20) && (*in <= 0x7F)) || (*in == 0x09));
Daniel Veillard48b2f892001-02-25 16:11:03 +00003236 nbchar = 0;
3237 }
Daniel Veillard50582112001-03-26 22:52:16 +00003238 ctxt->input->line = line;
3239 ctxt->input->col = col;
Daniel Veillard48b2f892001-02-25 16:11:03 +00003240 xmlParseCharDataComplex(ctxt, cdata);
3241}
3242
Daniel Veillard01c13b52002-12-10 15:19:08 +00003243/**
3244 * xmlParseCharDataComplex:
3245 * @ctxt: an XML parser context
3246 * @cdata: int indicating whether we are within a CDATA section
3247 *
3248 * parse a CharData section.this is the fallback function
3249 * of xmlParseCharData() when the parsing requires handling
3250 * of non-ASCII characters.
3251 */
Daniel Veillard48b2f892001-02-25 16:11:03 +00003252void
3253xmlParseCharDataComplex(xmlParserCtxtPtr ctxt, int cdata) {
Owen Taylor3473f882001-02-23 17:55:21 +00003254 xmlChar buf[XML_PARSER_BIG_BUFFER_SIZE + 5];
3255 int nbchar = 0;
3256 int cur, l;
3257 int count = 0;
3258
3259 SHRINK;
3260 GROW;
3261 cur = CUR_CHAR(l);
Daniel Veillardfdc91562002-07-01 21:52:03 +00003262 while ((cur != '<') && /* checked */
3263 (cur != '&') &&
William M. Brack871611b2003-10-18 04:53:14 +00003264 (IS_CHAR(cur))) /* test also done in xmlCurrentChar() */ {
Owen Taylor3473f882001-02-23 17:55:21 +00003265 if ((cur == ']') && (NXT(1) == ']') &&
3266 (NXT(2) == '>')) {
3267 if (cdata) break;
3268 else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003269 xmlFatalErr(ctxt, XML_ERR_MISPLACED_CDATA_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003270 }
3271 }
3272 COPY_BUF(l,buf,nbchar,cur);
3273 if (nbchar >= XML_PARSER_BIG_BUFFER_SIZE) {
Daniel Veillard092643b2003-09-25 14:29:29 +00003274 buf[nbchar] = 0;
3275
Owen Taylor3473f882001-02-23 17:55:21 +00003276 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003277 * OK the segment is to be consumed as chars.
Owen Taylor3473f882001-02-23 17:55:21 +00003278 */
3279 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003280 if (areBlanks(ctxt, buf, nbchar, 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003281 if (ctxt->sax->ignorableWhitespace != NULL)
3282 ctxt->sax->ignorableWhitespace(ctxt->userData,
3283 buf, nbchar);
3284 } else {
3285 if (ctxt->sax->characters != NULL)
3286 ctxt->sax->characters(ctxt->userData, buf, nbchar);
3287 }
3288 }
3289 nbchar = 0;
3290 }
3291 count++;
3292 if (count > 50) {
3293 GROW;
3294 count = 0;
3295 }
3296 NEXTL(l);
3297 cur = CUR_CHAR(l);
3298 }
3299 if (nbchar != 0) {
Daniel Veillard092643b2003-09-25 14:29:29 +00003300 buf[nbchar] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003301 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003302 * OK the segment is to be consumed as chars.
Owen Taylor3473f882001-02-23 17:55:21 +00003303 */
3304 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00003305 if (areBlanks(ctxt, buf, nbchar, 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00003306 if (ctxt->sax->ignorableWhitespace != NULL)
3307 ctxt->sax->ignorableWhitespace(ctxt->userData, buf, nbchar);
3308 } else {
3309 if (ctxt->sax->characters != NULL)
3310 ctxt->sax->characters(ctxt->userData, buf, nbchar);
3311 }
3312 }
3313 }
3314}
3315
3316/**
3317 * xmlParseExternalID:
3318 * @ctxt: an XML parser context
3319 * @publicID: a xmlChar** receiving PubidLiteral
3320 * @strict: indicate whether we should restrict parsing to only
3321 * production [75], see NOTE below
3322 *
3323 * Parse an External ID or a Public ID
3324 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003325 * NOTE: Productions [75] and [83] interact badly since [75] can generate
Owen Taylor3473f882001-02-23 17:55:21 +00003326 * 'PUBLIC' S PubidLiteral S SystemLiteral
3327 *
3328 * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
3329 * | 'PUBLIC' S PubidLiteral S SystemLiteral
3330 *
3331 * [83] PublicID ::= 'PUBLIC' S PubidLiteral
3332 *
3333 * Returns the function returns SystemLiteral and in the second
3334 * case publicID receives PubidLiteral, is strict is off
3335 * it is possible to return NULL and have publicID set.
3336 */
3337
3338xmlChar *
3339xmlParseExternalID(xmlParserCtxtPtr ctxt, xmlChar **publicID, int strict) {
3340 xmlChar *URI = NULL;
3341
3342 SHRINK;
Daniel Veillard146c9122001-03-22 15:22:27 +00003343
3344 *publicID = NULL;
Daniel Veillarda07050d2003-10-19 14:46:32 +00003345 if (CMP6(CUR_PTR, 'S', 'Y', 'S', 'T', 'E', 'M')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003346 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00003347 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003348 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3349 "Space required after 'SYSTEM'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003350 }
3351 SKIP_BLANKS;
3352 URI = xmlParseSystemLiteral(ctxt);
3353 if (URI == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003354 xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003355 }
Daniel Veillarda07050d2003-10-19 14:46:32 +00003356 } else if (CMP6(CUR_PTR, 'P', 'U', 'B', 'L', 'I', 'C')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003357 SKIP(6);
William M. Brack76e95df2003-10-18 16:20:14 +00003358 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003359 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00003360 "Space required after 'PUBLIC'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003361 }
3362 SKIP_BLANKS;
3363 *publicID = xmlParsePubidLiteral(ctxt);
3364 if (*publicID == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003365 xmlFatalErr(ctxt, XML_ERR_PUBID_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003366 }
3367 if (strict) {
3368 /*
3369 * We don't handle [83] so "S SystemLiteral" is required.
3370 */
William M. Brack76e95df2003-10-18 16:20:14 +00003371 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003372 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00003373 "Space required after the Public Identifier\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003374 }
3375 } else {
3376 /*
3377 * We handle [83] so we return immediately, if
3378 * "S SystemLiteral" is not detected. From a purely parsing
3379 * point of view that's a nice mess.
3380 */
3381 const xmlChar *ptr;
3382 GROW;
3383
3384 ptr = CUR_PTR;
William M. Brack76e95df2003-10-18 16:20:14 +00003385 if (!IS_BLANK_CH(*ptr)) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003386
William M. Brack76e95df2003-10-18 16:20:14 +00003387 while (IS_BLANK_CH(*ptr)) ptr++; /* TODO: dangerous, fix ! */
Owen Taylor3473f882001-02-23 17:55:21 +00003388 if ((*ptr != '\'') && (*ptr != '"')) return(NULL);
3389 }
3390 SKIP_BLANKS;
3391 URI = xmlParseSystemLiteral(ctxt);
3392 if (URI == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003393 xmlFatalErr(ctxt, XML_ERR_URI_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003394 }
3395 }
3396 return(URI);
3397}
3398
3399/**
3400 * xmlParseComment:
3401 * @ctxt: an XML parser context
3402 *
3403 * Skip an XML (SGML) comment <!-- .... -->
3404 * The spec says that "For compatibility, the string "--" (double-hyphen)
3405 * must not occur within comments. "
3406 *
3407 * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
3408 */
3409void
3410xmlParseComment(xmlParserCtxtPtr ctxt) {
3411 xmlChar *buf = NULL;
3412 int len;
3413 int size = XML_PARSER_BUFFER_SIZE;
3414 int q, ql;
3415 int r, rl;
3416 int cur, l;
3417 xmlParserInputState state;
3418 xmlParserInputPtr input = ctxt->input;
3419 int count = 0;
3420
3421 /*
3422 * Check that there is a comment right here.
3423 */
3424 if ((RAW != '<') || (NXT(1) != '!') ||
3425 (NXT(2) != '-') || (NXT(3) != '-')) return;
3426
3427 state = ctxt->instate;
3428 ctxt->instate = XML_PARSER_COMMENT;
3429 SHRINK;
3430 SKIP(4);
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003431 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003432 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003433 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003434 ctxt->instate = state;
3435 return;
3436 }
3437 q = CUR_CHAR(ql);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003438 if (q == 0)
3439 goto not_terminated;
Owen Taylor3473f882001-02-23 17:55:21 +00003440 NEXTL(ql);
3441 r = CUR_CHAR(rl);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003442 if (r == 0)
3443 goto not_terminated;
Owen Taylor3473f882001-02-23 17:55:21 +00003444 NEXTL(rl);
3445 cur = CUR_CHAR(l);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003446 if (cur == 0)
3447 goto not_terminated;
Owen Taylor3473f882001-02-23 17:55:21 +00003448 len = 0;
William M. Brack871611b2003-10-18 04:53:14 +00003449 while (IS_CHAR(cur) && /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003450 ((cur != '>') ||
3451 (r != '-') || (q != '-'))) {
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00003452 if ((r == '-') && (q == '-')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003453 xmlFatalErr(ctxt, XML_ERR_HYPHEN_IN_COMMENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003454 }
3455 if (len + 5 >= size) {
3456 size *= 2;
3457 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3458 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003459 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003460 ctxt->instate = state;
3461 return;
3462 }
3463 }
3464 COPY_BUF(ql,buf,len,q);
3465 q = r;
3466 ql = rl;
3467 r = cur;
3468 rl = l;
3469
3470 count++;
3471 if (count > 50) {
3472 GROW;
3473 count = 0;
3474 }
3475 NEXTL(l);
3476 cur = CUR_CHAR(l);
3477 if (cur == 0) {
3478 SHRINK;
3479 GROW;
3480 cur = CUR_CHAR(l);
3481 }
3482 }
3483 buf[len] = 0;
William M. Brack871611b2003-10-18 04:53:14 +00003484 if (!IS_CHAR(cur)) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00003485 xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
Owen Taylor3473f882001-02-23 17:55:21 +00003486 "Comment not terminated \n<!--%.50s\n", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003487 xmlFree(buf);
3488 } else {
3489 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003490 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
3491 "Comment doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003492 }
3493 NEXT;
3494 if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
3495 (!ctxt->disableSAX))
3496 ctxt->sax->comment(ctxt->userData, buf);
3497 xmlFree(buf);
3498 }
3499 ctxt->instate = state;
Daniel Veillard4aede2e2003-10-17 12:43:59 +00003500 return;
3501not_terminated:
3502 xmlFatalErrMsgStr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
3503 "Comment not terminated\n", NULL);
3504 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003505}
3506
3507/**
3508 * xmlParsePITarget:
3509 * @ctxt: an XML parser context
3510 *
3511 * parse the name of a PI
3512 *
3513 * [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
3514 *
3515 * Returns the PITarget name or NULL
3516 */
3517
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003518const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00003519xmlParsePITarget(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003520 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003521
3522 name = xmlParseName(ctxt);
3523 if ((name != NULL) &&
3524 ((name[0] == 'x') || (name[0] == 'X')) &&
3525 ((name[1] == 'm') || (name[1] == 'M')) &&
3526 ((name[2] == 'l') || (name[2] == 'L'))) {
3527 int i;
3528 if ((name[0] == 'x') && (name[1] == 'm') &&
3529 (name[2] == 'l') && (name[3] == 0)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003530 xmlFatalErrMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
Owen Taylor3473f882001-02-23 17:55:21 +00003531 "XML declaration allowed only at the start of the document\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003532 return(name);
3533 } else if (name[3] == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003534 xmlFatalErr(ctxt, XML_ERR_RESERVED_XML_NAME, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003535 return(name);
3536 }
3537 for (i = 0;;i++) {
3538 if (xmlW3CPIs[i] == NULL) break;
3539 if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i]))
3540 return(name);
3541 }
Daniel Veillard24eb9782003-10-04 21:08:09 +00003542 xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
3543 "xmlParsePITarget: invalid name prefix 'xml'\n",
3544 NULL, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003545 }
3546 return(name);
3547}
3548
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003549#ifdef LIBXML_CATALOG_ENABLED
3550/**
3551 * xmlParseCatalogPI:
3552 * @ctxt: an XML parser context
3553 * @catalog: the PI value string
3554 *
3555 * parse an XML Catalog Processing Instruction.
3556 *
3557 * <?oasis-xml-catalog catalog="http://example.com/catalog.xml"?>
3558 *
3559 * Occurs only if allowed by the user and if happening in the Misc
3560 * part of the document before any doctype informations
3561 * This will add the given catalog to the parsing context in order
3562 * to be used if there is a resolution need further down in the document
3563 */
3564
3565static void
3566xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) {
3567 xmlChar *URL = NULL;
3568 const xmlChar *tmp, *base;
3569 xmlChar marker;
3570
3571 tmp = catalog;
William M. Brack76e95df2003-10-18 16:20:14 +00003572 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003573 if (xmlStrncmp(tmp, BAD_CAST"catalog", 7))
3574 goto error;
3575 tmp += 7;
William M. Brack76e95df2003-10-18 16:20:14 +00003576 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003577 if (*tmp != '=') {
3578 return;
3579 }
3580 tmp++;
William M. Brack76e95df2003-10-18 16:20:14 +00003581 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003582 marker = *tmp;
3583 if ((marker != '\'') && (marker != '"'))
3584 goto error;
3585 tmp++;
3586 base = tmp;
3587 while ((*tmp != 0) && (*tmp != marker)) tmp++;
3588 if (*tmp == 0)
3589 goto error;
3590 URL = xmlStrndup(base, tmp - base);
3591 tmp++;
William M. Brack76e95df2003-10-18 16:20:14 +00003592 while (IS_BLANK_CH(*tmp)) tmp++;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003593 if (*tmp != 0)
3594 goto error;
3595
3596 if (URL != NULL) {
3597 ctxt->catalogs = xmlCatalogAddLocal(ctxt->catalogs, URL);
3598 xmlFree(URL);
3599 }
3600 return;
3601
3602error:
Daniel Veillard24eb9782003-10-04 21:08:09 +00003603 xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI,
3604 "Catalog PI syntax error: %s\n",
3605 catalog, NULL);
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003606 if (URL != NULL)
3607 xmlFree(URL);
3608}
3609#endif
3610
Owen Taylor3473f882001-02-23 17:55:21 +00003611/**
3612 * xmlParsePI:
3613 * @ctxt: an XML parser context
3614 *
3615 * parse an XML Processing Instruction.
3616 *
3617 * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
3618 *
3619 * The processing is transfered to SAX once parsed.
3620 */
3621
3622void
3623xmlParsePI(xmlParserCtxtPtr ctxt) {
3624 xmlChar *buf = NULL;
3625 int len = 0;
3626 int size = XML_PARSER_BUFFER_SIZE;
3627 int cur, l;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003628 const xmlChar *target;
Owen Taylor3473f882001-02-23 17:55:21 +00003629 xmlParserInputState state;
3630 int count = 0;
3631
3632 if ((RAW == '<') && (NXT(1) == '?')) {
3633 xmlParserInputPtr input = ctxt->input;
3634 state = ctxt->instate;
3635 ctxt->instate = XML_PARSER_PI;
3636 /*
3637 * this is a Processing Instruction.
3638 */
3639 SKIP(2);
3640 SHRINK;
3641
3642 /*
3643 * Parse the target name and check for special support like
3644 * namespace.
3645 */
3646 target = xmlParsePITarget(ctxt);
3647 if (target != NULL) {
3648 if ((RAW == '?') && (NXT(1) == '>')) {
3649 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003650 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
3651 "PI declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003652 }
3653 SKIP(2);
3654
3655 /*
3656 * SAX: PI detected.
3657 */
3658 if ((ctxt->sax) && (!ctxt->disableSAX) &&
3659 (ctxt->sax->processingInstruction != NULL))
3660 ctxt->sax->processingInstruction(ctxt->userData,
3661 target, NULL);
3662 ctxt->instate = state;
Owen Taylor3473f882001-02-23 17:55:21 +00003663 return;
3664 }
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003665 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00003666 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003667 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003668 ctxt->instate = state;
3669 return;
3670 }
3671 cur = CUR;
3672 if (!IS_BLANK(cur)) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00003673 xmlFatalErrMsgStr(ctxt, XML_ERR_SPACE_REQUIRED,
3674 "ParsePI: PI %s space expected\n", target);
Owen Taylor3473f882001-02-23 17:55:21 +00003675 }
3676 SKIP_BLANKS;
3677 cur = CUR_CHAR(l);
William M. Brack871611b2003-10-18 04:53:14 +00003678 while (IS_CHAR(cur) && /* checked */
Owen Taylor3473f882001-02-23 17:55:21 +00003679 ((cur != '?') || (NXT(1) != '>'))) {
3680 if (len + 5 >= size) {
3681 size *= 2;
3682 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3683 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003684 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003685 ctxt->instate = state;
3686 return;
3687 }
3688 }
3689 count++;
3690 if (count > 50) {
3691 GROW;
3692 count = 0;
3693 }
3694 COPY_BUF(l,buf,len,cur);
3695 NEXTL(l);
3696 cur = CUR_CHAR(l);
3697 if (cur == 0) {
3698 SHRINK;
3699 GROW;
3700 cur = CUR_CHAR(l);
3701 }
3702 }
3703 buf[len] = 0;
3704 if (cur != '?') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00003705 xmlFatalErrMsgStr(ctxt, XML_ERR_PI_NOT_FINISHED,
3706 "ParsePI: PI %s never end ...\n", target);
Owen Taylor3473f882001-02-23 17:55:21 +00003707 } else {
3708 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003709 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3710 "PI declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003711 }
3712 SKIP(2);
3713
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00003714#ifdef LIBXML_CATALOG_ENABLED
3715 if (((state == XML_PARSER_MISC) ||
3716 (state == XML_PARSER_START)) &&
3717 (xmlStrEqual(target, XML_CATALOG_PI))) {
3718 xmlCatalogAllow allow = xmlCatalogGetDefaults();
3719 if ((allow == XML_CATA_ALLOW_DOCUMENT) ||
3720 (allow == XML_CATA_ALLOW_ALL))
3721 xmlParseCatalogPI(ctxt, buf);
3722 }
3723#endif
3724
3725
Owen Taylor3473f882001-02-23 17:55:21 +00003726 /*
3727 * SAX: PI detected.
3728 */
3729 if ((ctxt->sax) && (!ctxt->disableSAX) &&
3730 (ctxt->sax->processingInstruction != NULL))
3731 ctxt->sax->processingInstruction(ctxt->userData,
3732 target, buf);
3733 }
3734 xmlFree(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00003735 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003736 xmlFatalErr(ctxt, XML_ERR_PI_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003737 }
3738 ctxt->instate = state;
3739 }
3740}
3741
3742/**
3743 * xmlParseNotationDecl:
3744 * @ctxt: an XML parser context
3745 *
3746 * parse a notation declaration
3747 *
3748 * [82] NotationDecl ::= '<!NOTATION' S Name S (ExternalID | PublicID) S? '>'
3749 *
3750 * Hence there is actually 3 choices:
3751 * 'PUBLIC' S PubidLiteral
3752 * 'PUBLIC' S PubidLiteral S SystemLiteral
3753 * and 'SYSTEM' S SystemLiteral
3754 *
3755 * See the NOTE on xmlParseExternalID().
3756 */
3757
3758void
3759xmlParseNotationDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003760 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00003761 xmlChar *Pubid;
3762 xmlChar *Systemid;
3763
Daniel Veillarda07050d2003-10-19 14:46:32 +00003764 if (CMP10(CUR_PTR, '<', '!', 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003765 xmlParserInputPtr input = ctxt->input;
3766 SHRINK;
3767 SKIP(10);
William M. Brack76e95df2003-10-18 16:20:14 +00003768 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003769 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3770 "Space required after '<!NOTATION'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003771 return;
3772 }
3773 SKIP_BLANKS;
3774
Daniel Veillard76d66f42001-05-16 21:05:17 +00003775 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003776 if (name == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003777 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003778 return;
3779 }
William M. Brack76e95df2003-10-18 16:20:14 +00003780 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003781 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00003782 "Space required after the NOTATION name'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003783 return;
3784 }
3785 SKIP_BLANKS;
3786
3787 /*
3788 * Parse the IDs.
3789 */
3790 Systemid = xmlParseExternalID(ctxt, &Pubid, 0);
3791 SKIP_BLANKS;
3792
3793 if (RAW == '>') {
3794 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003795 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3796 "Notation declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003797 }
3798 NEXT;
3799 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
3800 (ctxt->sax->notationDecl != NULL))
3801 ctxt->sax->notationDecl(ctxt->userData, name, Pubid, Systemid);
3802 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003803 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003804 }
Owen Taylor3473f882001-02-23 17:55:21 +00003805 if (Systemid != NULL) xmlFree(Systemid);
3806 if (Pubid != NULL) xmlFree(Pubid);
3807 }
3808}
3809
3810/**
3811 * xmlParseEntityDecl:
3812 * @ctxt: an XML parser context
3813 *
3814 * parse <!ENTITY declarations
3815 *
3816 * [70] EntityDecl ::= GEDecl | PEDecl
3817 *
3818 * [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>'
3819 *
3820 * [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>'
3821 *
3822 * [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?)
3823 *
3824 * [74] PEDef ::= EntityValue | ExternalID
3825 *
3826 * [76] NDataDecl ::= S 'NDATA' S Name
3827 *
3828 * [ VC: Notation Declared ]
3829 * The Name must match the declared name of a notation.
3830 */
3831
3832void
3833xmlParseEntityDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003834 const xmlChar *name = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003835 xmlChar *value = NULL;
3836 xmlChar *URI = NULL, *literal = NULL;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00003837 const xmlChar *ndata = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00003838 int isParameter = 0;
3839 xmlChar *orig = NULL;
Daniel Veillardf5582f12002-06-11 10:08:16 +00003840 int skipped;
Owen Taylor3473f882001-02-23 17:55:21 +00003841
3842 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00003843 if (CMP8(CUR_PTR, '<', '!', 'E', 'N', 'T', 'I', 'T', 'Y')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003844 xmlParserInputPtr input = ctxt->input;
Owen Taylor3473f882001-02-23 17:55:21 +00003845 SHRINK;
3846 SKIP(8);
Daniel Veillardf5582f12002-06-11 10:08:16 +00003847 skipped = SKIP_BLANKS;
3848 if (skipped == 0) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003849 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3850 "Space required after '<!ENTITY'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003851 }
Owen Taylor3473f882001-02-23 17:55:21 +00003852
3853 if (RAW == '%') {
3854 NEXT;
Daniel Veillardf5582f12002-06-11 10:08:16 +00003855 skipped = SKIP_BLANKS;
3856 if (skipped == 0) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003857 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3858 "Space required after '%'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003859 }
Owen Taylor3473f882001-02-23 17:55:21 +00003860 isParameter = 1;
3861 }
3862
Daniel Veillard76d66f42001-05-16 21:05:17 +00003863 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003864 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003865 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
3866 "xmlParseEntityDecl: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003867 return;
3868 }
Daniel Veillardf5582f12002-06-11 10:08:16 +00003869 skipped = SKIP_BLANKS;
3870 if (skipped == 0) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003871 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3872 "Space required after the entity name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003873 }
Owen Taylor3473f882001-02-23 17:55:21 +00003874
Daniel Veillardf5582f12002-06-11 10:08:16 +00003875 ctxt->instate = XML_PARSER_ENTITY_DECL;
Owen Taylor3473f882001-02-23 17:55:21 +00003876 /*
3877 * handle the various case of definitions...
3878 */
3879 if (isParameter) {
3880 if ((RAW == '"') || (RAW == '\'')) {
3881 value = xmlParseEntityValue(ctxt, &orig);
3882 if (value) {
3883 if ((ctxt->sax != NULL) &&
3884 (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
3885 ctxt->sax->entityDecl(ctxt->userData, name,
3886 XML_INTERNAL_PARAMETER_ENTITY,
3887 NULL, NULL, value);
3888 }
3889 } else {
3890 URI = xmlParseExternalID(ctxt, &literal, 1);
3891 if ((URI == NULL) && (literal == NULL)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003892 xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003893 }
3894 if (URI) {
3895 xmlURIPtr uri;
3896
3897 uri = xmlParseURI((const char *) URI);
3898 if (uri == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003899 xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI,
3900 "Invalid URI: %s\n", URI);
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003901 /*
3902 * This really ought to be a well formedness error
3903 * but the XML Core WG decided otherwise c.f. issue
3904 * E26 of the XML erratas.
3905 */
Owen Taylor3473f882001-02-23 17:55:21 +00003906 } else {
3907 if (uri->fragment != NULL) {
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003908 /*
3909 * Okay this is foolish to block those but not
3910 * invalid URIs.
3911 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003912 xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003913 } else {
3914 if ((ctxt->sax != NULL) &&
3915 (!ctxt->disableSAX) &&
3916 (ctxt->sax->entityDecl != NULL))
3917 ctxt->sax->entityDecl(ctxt->userData, name,
3918 XML_EXTERNAL_PARAMETER_ENTITY,
3919 literal, URI, NULL);
3920 }
3921 xmlFreeURI(uri);
3922 }
3923 }
3924 }
3925 } else {
3926 if ((RAW == '"') || (RAW == '\'')) {
3927 value = xmlParseEntityValue(ctxt, &orig);
3928 if ((ctxt->sax != NULL) &&
3929 (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
3930 ctxt->sax->entityDecl(ctxt->userData, name,
3931 XML_INTERNAL_GENERAL_ENTITY,
3932 NULL, NULL, value);
Daniel Veillard5997aca2002-03-18 18:36:20 +00003933 /*
3934 * For expat compatibility in SAX mode.
3935 */
3936 if ((ctxt->myDoc == NULL) ||
3937 (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) {
3938 if (ctxt->myDoc == NULL) {
3939 ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
3940 }
3941 if (ctxt->myDoc->intSubset == NULL)
3942 ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
3943 BAD_CAST "fake", NULL, NULL);
3944
Daniel Veillard1af9a412003-08-20 22:54:39 +00003945 xmlSAX2EntityDecl(ctxt, name, XML_INTERNAL_GENERAL_ENTITY,
3946 NULL, NULL, value);
Daniel Veillard5997aca2002-03-18 18:36:20 +00003947 }
Owen Taylor3473f882001-02-23 17:55:21 +00003948 } else {
3949 URI = xmlParseExternalID(ctxt, &literal, 1);
3950 if ((URI == NULL) && (literal == NULL)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003951 xmlFatalErr(ctxt, XML_ERR_VALUE_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003952 }
3953 if (URI) {
3954 xmlURIPtr uri;
3955
3956 uri = xmlParseURI((const char *)URI);
3957 if (uri == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00003958 xmlErrMsgStr(ctxt, XML_ERR_INVALID_URI,
3959 "Invalid URI: %s\n", URI);
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003960 /*
3961 * This really ought to be a well formedness error
3962 * but the XML Core WG decided otherwise c.f. issue
3963 * E26 of the XML erratas.
3964 */
Owen Taylor3473f882001-02-23 17:55:21 +00003965 } else {
3966 if (uri->fragment != NULL) {
Daniel Veillard4a7ae502002-02-18 19:18:17 +00003967 /*
3968 * Okay this is foolish to block those but not
3969 * invalid URIs.
3970 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00003971 xmlFatalErr(ctxt, XML_ERR_URI_FRAGMENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003972 }
3973 xmlFreeURI(uri);
3974 }
3975 }
William M. Brack76e95df2003-10-18 16:20:14 +00003976 if ((RAW != '>') && (!IS_BLANK_CH(CUR))) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003977 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3978 "Space required before 'NDATA'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003979 }
3980 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00003981 if (CMP5(CUR_PTR, 'N', 'D', 'A', 'T', 'A')) {
Owen Taylor3473f882001-02-23 17:55:21 +00003982 SKIP(5);
William M. Brack76e95df2003-10-18 16:20:14 +00003983 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00003984 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
3985 "Space required after 'NDATA'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003986 }
3987 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00003988 ndata = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00003989 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
3990 (ctxt->sax->unparsedEntityDecl != NULL))
3991 ctxt->sax->unparsedEntityDecl(ctxt->userData, name,
3992 literal, URI, ndata);
3993 } else {
3994 if ((ctxt->sax != NULL) &&
3995 (!ctxt->disableSAX) && (ctxt->sax->entityDecl != NULL))
3996 ctxt->sax->entityDecl(ctxt->userData, name,
3997 XML_EXTERNAL_GENERAL_PARSED_ENTITY,
3998 literal, URI, NULL);
Daniel Veillard5997aca2002-03-18 18:36:20 +00003999 /*
4000 * For expat compatibility in SAX mode.
4001 * assuming the entity repalcement was asked for
4002 */
4003 if ((ctxt->replaceEntities != 0) &&
4004 ((ctxt->myDoc == NULL) ||
4005 (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE)))) {
4006 if (ctxt->myDoc == NULL) {
4007 ctxt->myDoc = xmlNewDoc(SAX_COMPAT_MODE);
4008 }
4009
4010 if (ctxt->myDoc->intSubset == NULL)
4011 ctxt->myDoc->intSubset = xmlNewDtd(ctxt->myDoc,
4012 BAD_CAST "fake", NULL, NULL);
Daniel Veillard1af9a412003-08-20 22:54:39 +00004013 xmlSAX2EntityDecl(ctxt, name,
4014 XML_EXTERNAL_GENERAL_PARSED_ENTITY,
4015 literal, URI, NULL);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004016 }
Owen Taylor3473f882001-02-23 17:55:21 +00004017 }
4018 }
4019 }
4020 SKIP_BLANKS;
4021 if (RAW != '>') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00004022 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_NOT_FINISHED,
Owen Taylor3473f882001-02-23 17:55:21 +00004023 "xmlParseEntityDecl: entity %s not terminated\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00004024 } else {
4025 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004026 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
4027 "Entity declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004028 }
4029 NEXT;
4030 }
4031 if (orig != NULL) {
4032 /*
4033 * Ugly mechanism to save the raw entity value.
4034 */
4035 xmlEntityPtr cur = NULL;
4036
4037 if (isParameter) {
4038 if ((ctxt->sax != NULL) &&
4039 (ctxt->sax->getParameterEntity != NULL))
4040 cur = ctxt->sax->getParameterEntity(ctxt->userData, name);
4041 } else {
4042 if ((ctxt->sax != NULL) &&
4043 (ctxt->sax->getEntity != NULL))
4044 cur = ctxt->sax->getEntity(ctxt->userData, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004045 if ((cur == NULL) && (ctxt->userData==ctxt)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00004046 cur = xmlSAX2GetEntity(ctxt, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00004047 }
Owen Taylor3473f882001-02-23 17:55:21 +00004048 }
4049 if (cur != NULL) {
4050 if (cur->orig != NULL)
4051 xmlFree(orig);
4052 else
4053 cur->orig = orig;
4054 } else
4055 xmlFree(orig);
4056 }
Owen Taylor3473f882001-02-23 17:55:21 +00004057 if (value != NULL) xmlFree(value);
4058 if (URI != NULL) xmlFree(URI);
4059 if (literal != NULL) xmlFree(literal);
Owen Taylor3473f882001-02-23 17:55:21 +00004060 }
4061}
4062
4063/**
4064 * xmlParseDefaultDecl:
4065 * @ctxt: an XML parser context
4066 * @value: Receive a possible fixed default value for the attribute
4067 *
4068 * Parse an attribute default declaration
4069 *
4070 * [60] DefaultDecl ::= '#REQUIRED' | '#IMPLIED' | (('#FIXED' S)? AttValue)
4071 *
4072 * [ VC: Required Attribute ]
4073 * if the default declaration is the keyword #REQUIRED, then the
4074 * attribute must be specified for all elements of the type in the
4075 * attribute-list declaration.
4076 *
4077 * [ VC: Attribute Default Legal ]
4078 * The declared default value must meet the lexical constraints of
4079 * the declared attribute type c.f. xmlValidateAttributeDecl()
4080 *
4081 * [ VC: Fixed Attribute Default ]
4082 * if an attribute has a default value declared with the #FIXED
4083 * keyword, instances of that attribute must match the default value.
4084 *
4085 * [ WFC: No < in Attribute Values ]
4086 * handled in xmlParseAttValue()
4087 *
4088 * returns: XML_ATTRIBUTE_NONE, XML_ATTRIBUTE_REQUIRED, XML_ATTRIBUTE_IMPLIED
4089 * or XML_ATTRIBUTE_FIXED.
4090 */
4091
4092int
4093xmlParseDefaultDecl(xmlParserCtxtPtr ctxt, xmlChar **value) {
4094 int val;
4095 xmlChar *ret;
4096
4097 *value = NULL;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004098 if (CMP9(CUR_PTR, '#', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004099 SKIP(9);
4100 return(XML_ATTRIBUTE_REQUIRED);
4101 }
Daniel Veillarda07050d2003-10-19 14:46:32 +00004102 if (CMP8(CUR_PTR, '#', 'I', 'M', 'P', 'L', 'I', 'E', 'D')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004103 SKIP(8);
4104 return(XML_ATTRIBUTE_IMPLIED);
4105 }
4106 val = XML_ATTRIBUTE_NONE;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004107 if (CMP6(CUR_PTR, '#', 'F', 'I', 'X', 'E', 'D')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004108 SKIP(6);
4109 val = XML_ATTRIBUTE_FIXED;
William M. Brack76e95df2003-10-18 16:20:14 +00004110 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004111 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4112 "Space required after '#FIXED'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004113 }
4114 SKIP_BLANKS;
4115 }
4116 ret = xmlParseAttValue(ctxt);
4117 ctxt->instate = XML_PARSER_DTD;
4118 if (ret == NULL) {
William M. Brack7b9154b2003-09-27 19:23:50 +00004119 xmlFatalErrMsg(ctxt, (xmlParserErrors)ctxt->errNo,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004120 "Attribute default value declaration error\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004121 } else
4122 *value = ret;
4123 return(val);
4124}
4125
4126/**
4127 * xmlParseNotationType:
4128 * @ctxt: an XML parser context
4129 *
4130 * parse an Notation attribute type.
4131 *
4132 * Note: the leading 'NOTATION' S part has already being parsed...
4133 *
4134 * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
4135 *
4136 * [ VC: Notation Attributes ]
4137 * Values of this type must match one of the notation names included
4138 * in the declaration; all notation names in the declaration must be declared.
4139 *
4140 * Returns: the notation attribute tree built while parsing
4141 */
4142
4143xmlEnumerationPtr
4144xmlParseNotationType(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004145 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00004146 xmlEnumerationPtr ret = NULL, last = NULL, cur;
4147
4148 if (RAW != '(') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004149 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004150 return(NULL);
4151 }
4152 SHRINK;
4153 do {
4154 NEXT;
4155 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004156 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004157 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004158 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4159 "Name expected in NOTATION declaration\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004160 return(ret);
4161 }
4162 cur = xmlCreateEnumeration(name);
Owen Taylor3473f882001-02-23 17:55:21 +00004163 if (cur == NULL) return(ret);
4164 if (last == NULL) ret = last = cur;
4165 else {
4166 last->next = cur;
4167 last = cur;
4168 }
4169 SKIP_BLANKS;
4170 } while (RAW == '|');
4171 if (RAW != ')') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004172 xmlFatalErr(ctxt, XML_ERR_NOTATION_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004173 if ((last != NULL) && (last != ret))
4174 xmlFreeEnumeration(last);
4175 return(ret);
4176 }
4177 NEXT;
4178 return(ret);
4179}
4180
4181/**
4182 * xmlParseEnumerationType:
4183 * @ctxt: an XML parser context
4184 *
4185 * parse an Enumeration attribute type.
4186 *
4187 * [59] Enumeration ::= '(' S? Nmtoken (S? '|' S? Nmtoken)* S? ')'
4188 *
4189 * [ VC: Enumeration ]
4190 * Values of this type must match one of the Nmtoken tokens in
4191 * the declaration
4192 *
4193 * Returns: the enumeration attribute tree built while parsing
4194 */
4195
4196xmlEnumerationPtr
4197xmlParseEnumerationType(xmlParserCtxtPtr ctxt) {
4198 xmlChar *name;
4199 xmlEnumerationPtr ret = NULL, last = NULL, cur;
4200
4201 if (RAW != '(') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004202 xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004203 return(NULL);
4204 }
4205 SHRINK;
4206 do {
4207 NEXT;
4208 SKIP_BLANKS;
4209 name = xmlParseNmtoken(ctxt);
4210 if (name == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004211 xmlFatalErr(ctxt, XML_ERR_NMTOKEN_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004212 return(ret);
4213 }
4214 cur = xmlCreateEnumeration(name);
4215 xmlFree(name);
4216 if (cur == NULL) return(ret);
4217 if (last == NULL) ret = last = cur;
4218 else {
4219 last->next = cur;
4220 last = cur;
4221 }
4222 SKIP_BLANKS;
4223 } while (RAW == '|');
4224 if (RAW != ')') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004225 xmlFatalErr(ctxt, XML_ERR_ATTLIST_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004226 return(ret);
4227 }
4228 NEXT;
4229 return(ret);
4230}
4231
4232/**
4233 * xmlParseEnumeratedType:
4234 * @ctxt: an XML parser context
4235 * @tree: the enumeration tree built while parsing
4236 *
4237 * parse an Enumerated attribute type.
4238 *
4239 * [57] EnumeratedType ::= NotationType | Enumeration
4240 *
4241 * [58] NotationType ::= 'NOTATION' S '(' S? Name (S? '|' S? Name)* S? ')'
4242 *
4243 *
4244 * Returns: XML_ATTRIBUTE_ENUMERATION or XML_ATTRIBUTE_NOTATION
4245 */
4246
4247int
4248xmlParseEnumeratedType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) {
Daniel Veillarda07050d2003-10-19 14:46:32 +00004249 if (CMP8(CUR_PTR, 'N', 'O', 'T', 'A', 'T', 'I', 'O', 'N')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004250 SKIP(8);
William M. Brack76e95df2003-10-18 16:20:14 +00004251 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004252 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4253 "Space required after 'NOTATION'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004254 return(0);
4255 }
4256 SKIP_BLANKS;
4257 *tree = xmlParseNotationType(ctxt);
4258 if (*tree == NULL) return(0);
4259 return(XML_ATTRIBUTE_NOTATION);
4260 }
4261 *tree = xmlParseEnumerationType(ctxt);
4262 if (*tree == NULL) return(0);
4263 return(XML_ATTRIBUTE_ENUMERATION);
4264}
4265
4266/**
4267 * xmlParseAttributeType:
4268 * @ctxt: an XML parser context
4269 * @tree: the enumeration tree built while parsing
4270 *
4271 * parse the Attribute list def for an element
4272 *
4273 * [54] AttType ::= StringType | TokenizedType | EnumeratedType
4274 *
4275 * [55] StringType ::= 'CDATA'
4276 *
4277 * [56] TokenizedType ::= 'ID' | 'IDREF' | 'IDREFS' | 'ENTITY' |
4278 * 'ENTITIES' | 'NMTOKEN' | 'NMTOKENS'
4279 *
4280 * Validity constraints for attribute values syntax are checked in
4281 * xmlValidateAttributeValue()
4282 *
4283 * [ VC: ID ]
4284 * Values of type ID must match the Name production. A name must not
4285 * appear more than once in an XML document as a value of this type;
4286 * i.e., ID values must uniquely identify the elements which bear them.
4287 *
4288 * [ VC: One ID per Element Type ]
4289 * No element type may have more than one ID attribute specified.
4290 *
4291 * [ VC: ID Attribute Default ]
4292 * An ID attribute must have a declared default of #IMPLIED or #REQUIRED.
4293 *
4294 * [ VC: IDREF ]
4295 * Values of type IDREF must match the Name production, and values
4296 * of type IDREFS must match Names; each IDREF Name must match the value
4297 * of an ID attribute on some element in the XML document; i.e. IDREF
4298 * values must match the value of some ID attribute.
4299 *
4300 * [ VC: Entity Name ]
4301 * Values of type ENTITY must match the Name production, values
4302 * of type ENTITIES must match Names; each Entity Name must match the
4303 * name of an unparsed entity declared in the DTD.
4304 *
4305 * [ VC: Name Token ]
4306 * Values of type NMTOKEN must match the Nmtoken production; values
4307 * of type NMTOKENS must match Nmtokens.
4308 *
4309 * Returns the attribute type
4310 */
4311int
4312xmlParseAttributeType(xmlParserCtxtPtr ctxt, xmlEnumerationPtr *tree) {
4313 SHRINK;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004314 if (CMP5(CUR_PTR, 'C', 'D', 'A', 'T', 'A')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004315 SKIP(5);
4316 return(XML_ATTRIBUTE_CDATA);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004317 } else if (CMP6(CUR_PTR, 'I', 'D', 'R', 'E', 'F', 'S')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004318 SKIP(6);
4319 return(XML_ATTRIBUTE_IDREFS);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004320 } else if (CMP5(CUR_PTR, 'I', 'D', 'R', 'E', 'F')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004321 SKIP(5);
4322 return(XML_ATTRIBUTE_IDREF);
4323 } else if ((RAW == 'I') && (NXT(1) == 'D')) {
4324 SKIP(2);
4325 return(XML_ATTRIBUTE_ID);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004326 } else if (CMP6(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'Y')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004327 SKIP(6);
4328 return(XML_ATTRIBUTE_ENTITY);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004329 } else if (CMP8(CUR_PTR, 'E', 'N', 'T', 'I', 'T', 'I', 'E', 'S')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004330 SKIP(8);
4331 return(XML_ATTRIBUTE_ENTITIES);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004332 } else if (CMP8(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N', 'S')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004333 SKIP(8);
4334 return(XML_ATTRIBUTE_NMTOKENS);
Daniel Veillarda07050d2003-10-19 14:46:32 +00004335 } else if (CMP7(CUR_PTR, 'N', 'M', 'T', 'O', 'K', 'E', 'N')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004336 SKIP(7);
4337 return(XML_ATTRIBUTE_NMTOKEN);
4338 }
4339 return(xmlParseEnumeratedType(ctxt, tree));
4340}
4341
4342/**
4343 * xmlParseAttributeListDecl:
4344 * @ctxt: an XML parser context
4345 *
4346 * : parse the Attribute list def for an element
4347 *
4348 * [52] AttlistDecl ::= '<!ATTLIST' S Name AttDef* S? '>'
4349 *
4350 * [53] AttDef ::= S Name S AttType S DefaultDecl
4351 *
4352 */
4353void
4354xmlParseAttributeListDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004355 const xmlChar *elemName;
4356 const xmlChar *attrName;
Owen Taylor3473f882001-02-23 17:55:21 +00004357 xmlEnumerationPtr tree;
4358
Daniel Veillarda07050d2003-10-19 14:46:32 +00004359 if (CMP9(CUR_PTR, '<', '!', 'A', 'T', 'T', 'L', 'I', 'S', 'T')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004360 xmlParserInputPtr input = ctxt->input;
4361
4362 SKIP(9);
William M. Brack76e95df2003-10-18 16:20:14 +00004363 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004364 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004365 "Space required after '<!ATTLIST'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004366 }
4367 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004368 elemName = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004369 if (elemName == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004370 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4371 "ATTLIST: no name for Element\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004372 return;
4373 }
4374 SKIP_BLANKS;
4375 GROW;
4376 while (RAW != '>') {
4377 const xmlChar *check = CUR_PTR;
4378 int type;
4379 int def;
4380 xmlChar *defaultValue = NULL;
4381
4382 GROW;
4383 tree = NULL;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004384 attrName = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004385 if (attrName == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004386 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4387 "ATTLIST: no name for Attribute\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004388 break;
4389 }
4390 GROW;
William M. Brack76e95df2003-10-18 16:20:14 +00004391 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004392 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004393 "Space required after the attribute name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004394 if (defaultValue != NULL)
4395 xmlFree(defaultValue);
4396 break;
4397 }
4398 SKIP_BLANKS;
4399
4400 type = xmlParseAttributeType(ctxt, &tree);
4401 if (type <= 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00004402 if (defaultValue != NULL)
4403 xmlFree(defaultValue);
4404 break;
4405 }
4406
4407 GROW;
William M. Brack76e95df2003-10-18 16:20:14 +00004408 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004409 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4410 "Space required after the attribute type\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004411 if (defaultValue != NULL)
4412 xmlFree(defaultValue);
4413 if (tree != NULL)
4414 xmlFreeEnumeration(tree);
4415 break;
4416 }
4417 SKIP_BLANKS;
4418
4419 def = xmlParseDefaultDecl(ctxt, &defaultValue);
4420 if (def <= 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00004421 if (defaultValue != NULL)
4422 xmlFree(defaultValue);
4423 if (tree != NULL)
4424 xmlFreeEnumeration(tree);
4425 break;
4426 }
4427
4428 GROW;
4429 if (RAW != '>') {
William M. Brack76e95df2003-10-18 16:20:14 +00004430 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004431 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004432 "Space required after the attribute default value\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004433 if (defaultValue != NULL)
4434 xmlFree(defaultValue);
4435 if (tree != NULL)
4436 xmlFreeEnumeration(tree);
4437 break;
4438 }
4439 SKIP_BLANKS;
4440 }
4441 if (check == CUR_PTR) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004442 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
4443 "in xmlParseAttributeListDecl\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004444 if (defaultValue != NULL)
4445 xmlFree(defaultValue);
4446 if (tree != NULL)
4447 xmlFreeEnumeration(tree);
4448 break;
4449 }
4450 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4451 (ctxt->sax->attributeDecl != NULL))
4452 ctxt->sax->attributeDecl(ctxt->userData, elemName, attrName,
4453 type, def, defaultValue, tree);
Daniel Veillarde57ec792003-09-10 10:50:59 +00004454 else if (tree != NULL)
4455 xmlFreeEnumeration(tree);
4456
4457 if ((ctxt->sax2) && (defaultValue != NULL) &&
4458 (def != XML_ATTRIBUTE_IMPLIED) &&
4459 (def != XML_ATTRIBUTE_REQUIRED)) {
4460 xmlAddDefAttrs(ctxt, elemName, attrName, defaultValue);
4461 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00004462 if ((ctxt->sax2) && (type != XML_ATTRIBUTE_CDATA)) {
4463 xmlAddSpecialAttr(ctxt, elemName, attrName, type);
4464 }
Owen Taylor3473f882001-02-23 17:55:21 +00004465 if (defaultValue != NULL)
4466 xmlFree(defaultValue);
4467 GROW;
4468 }
4469 if (RAW == '>') {
4470 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004471 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
4472 "Attribute list declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004473 }
4474 NEXT;
4475 }
Owen Taylor3473f882001-02-23 17:55:21 +00004476 }
4477}
4478
4479/**
4480 * xmlParseElementMixedContentDecl:
4481 * @ctxt: an XML parser context
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004482 * @inputchk: the input used for the current entity, needed for boundary checks
Owen Taylor3473f882001-02-23 17:55:21 +00004483 *
4484 * parse the declaration for a Mixed Element content
4485 * The leading '(' and spaces have been skipped in xmlParseElementContentDecl
4486 *
4487 * [51] Mixed ::= '(' S? '#PCDATA' (S? '|' S? Name)* S? ')*' |
4488 * '(' S? '#PCDATA' S? ')'
4489 *
4490 * [ VC: Proper Group/PE Nesting ] applies to [51] too (see [49])
4491 *
4492 * [ VC: No Duplicate Types ]
4493 * The same name must not appear more than once in a single
4494 * mixed-content declaration.
4495 *
4496 * returns: the list of the xmlElementContentPtr describing the element choices
4497 */
4498xmlElementContentPtr
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004499xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) {
Owen Taylor3473f882001-02-23 17:55:21 +00004500 xmlElementContentPtr ret = NULL, cur = NULL, n;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004501 const xmlChar *elem = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00004502
4503 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004504 if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004505 SKIP(7);
4506 SKIP_BLANKS;
4507 SHRINK;
4508 if (RAW == ')') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004509 if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00004510 xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
4511"Element content declaration doesn't start and stop in the same entity\n",
4512 NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004513 }
Owen Taylor3473f882001-02-23 17:55:21 +00004514 NEXT;
4515 ret = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_PCDATA);
4516 if (RAW == '*') {
4517 ret->ocur = XML_ELEMENT_CONTENT_MULT;
4518 NEXT;
4519 }
4520 return(ret);
4521 }
4522 if ((RAW == '(') || (RAW == '|')) {
4523 ret = cur = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_PCDATA);
4524 if (ret == NULL) return(NULL);
4525 }
4526 while (RAW == '|') {
4527 NEXT;
4528 if (elem == NULL) {
4529 ret = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR);
4530 if (ret == NULL) return(NULL);
4531 ret->c1 = cur;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004532 if (cur != NULL)
4533 cur->parent = ret;
Owen Taylor3473f882001-02-23 17:55:21 +00004534 cur = ret;
4535 } else {
4536 n = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR);
4537 if (n == NULL) return(NULL);
4538 n->c1 = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004539 if (n->c1 != NULL)
4540 n->c1->parent = n;
Owen Taylor3473f882001-02-23 17:55:21 +00004541 cur->c2 = n;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004542 if (n != NULL)
4543 n->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004544 cur = n;
Owen Taylor3473f882001-02-23 17:55:21 +00004545 }
4546 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004547 elem = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004548 if (elem == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004549 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004550 "xmlParseElementMixedContentDecl : Name expected\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004551 xmlFreeElementContent(cur);
4552 return(NULL);
4553 }
4554 SKIP_BLANKS;
4555 GROW;
4556 }
4557 if ((RAW == ')') && (NXT(1) == '*')) {
4558 if (elem != NULL) {
4559 cur->c2 = xmlNewElementContent(elem,
4560 XML_ELEMENT_CONTENT_ELEMENT);
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004561 if (cur->c2 != NULL)
4562 cur->c2->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004563 }
4564 ret->ocur = XML_ELEMENT_CONTENT_MULT;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004565 if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00004566 xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
4567"Element content declaration doesn't start and stop in the same entity\n",
4568 NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004569 }
Owen Taylor3473f882001-02-23 17:55:21 +00004570 SKIP(2);
4571 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00004572 xmlFreeElementContent(ret);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004573 xmlFatalErr(ctxt, XML_ERR_MIXED_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004574 return(NULL);
4575 }
4576
4577 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004578 xmlFatalErr(ctxt, XML_ERR_PCDATA_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004579 }
4580 return(ret);
4581}
4582
4583/**
4584 * xmlParseElementChildrenContentDecl:
4585 * @ctxt: an XML parser context
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004586 * @inputchk: the input used for the current entity, needed for boundary checks
Owen Taylor3473f882001-02-23 17:55:21 +00004587 *
4588 * parse the declaration for a Mixed Element content
4589 * The leading '(' and spaces have been skipped in xmlParseElementContentDecl
4590 *
4591 *
4592 * [47] children ::= (choice | seq) ('?' | '*' | '+')?
4593 *
4594 * [48] cp ::= (Name | choice | seq) ('?' | '*' | '+')?
4595 *
4596 * [49] choice ::= '(' S? cp ( S? '|' S? cp )* S? ')'
4597 *
4598 * [50] seq ::= '(' S? cp ( S? ',' S? cp )* S? ')'
4599 *
4600 * [ VC: Proper Group/PE Nesting ] applies to [49] and [50]
4601 * TODO Parameter-entity replacement text must be properly nested
Daniel Veillardcbaf3992001-12-31 16:16:02 +00004602 * with parenthesized groups. That is to say, if either of the
Owen Taylor3473f882001-02-23 17:55:21 +00004603 * opening or closing parentheses in a choice, seq, or Mixed
4604 * construct is contained in the replacement text for a parameter
4605 * entity, both must be contained in the same replacement text. For
4606 * interoperability, if a parameter-entity reference appears in a
4607 * choice, seq, or Mixed construct, its replacement text should not
4608 * be empty, and neither the first nor last non-blank character of
4609 * the replacement text should be a connector (| or ,).
4610 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00004611 * Returns the tree of xmlElementContentPtr describing the element
Owen Taylor3473f882001-02-23 17:55:21 +00004612 * hierarchy.
4613 */
4614xmlElementContentPtr
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004615xmlParseElementChildrenContentDecl (xmlParserCtxtPtr ctxt, int inputchk) {
Owen Taylor3473f882001-02-23 17:55:21 +00004616 xmlElementContentPtr ret = NULL, cur = NULL, last = NULL, op = NULL;
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004617 const xmlChar *elem;
Owen Taylor3473f882001-02-23 17:55:21 +00004618 xmlChar type = 0;
4619
4620 SKIP_BLANKS;
4621 GROW;
4622 if (RAW == '(') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004623 int inputid = ctxt->input->id;
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004624
Owen Taylor3473f882001-02-23 17:55:21 +00004625 /* Recurse on first child */
4626 NEXT;
4627 SKIP_BLANKS;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004628 cur = ret = xmlParseElementChildrenContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004629 SKIP_BLANKS;
4630 GROW;
4631 } else {
Daniel Veillard76d66f42001-05-16 21:05:17 +00004632 elem = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004633 if (elem == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004634 xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004635 return(NULL);
4636 }
4637 cur = ret = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004638 if (cur == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004639 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004640 return(NULL);
4641 }
Owen Taylor3473f882001-02-23 17:55:21 +00004642 GROW;
4643 if (RAW == '?') {
4644 cur->ocur = XML_ELEMENT_CONTENT_OPT;
4645 NEXT;
4646 } else if (RAW == '*') {
4647 cur->ocur = XML_ELEMENT_CONTENT_MULT;
4648 NEXT;
4649 } else if (RAW == '+') {
4650 cur->ocur = XML_ELEMENT_CONTENT_PLUS;
4651 NEXT;
4652 } else {
4653 cur->ocur = XML_ELEMENT_CONTENT_ONCE;
4654 }
Owen Taylor3473f882001-02-23 17:55:21 +00004655 GROW;
4656 }
4657 SKIP_BLANKS;
4658 SHRINK;
4659 while (RAW != ')') {
4660 /*
4661 * Each loop we parse one separator and one element.
4662 */
4663 if (RAW == ',') {
4664 if (type == 0) type = CUR;
4665
4666 /*
4667 * Detect "Name | Name , Name" error
4668 */
4669 else if (type != CUR) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004670 xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004671 "xmlParseElementChildrenContentDecl : '%c' expected\n",
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004672 type);
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004673 if ((last != NULL) && (last != ret))
Owen Taylor3473f882001-02-23 17:55:21 +00004674 xmlFreeElementContent(last);
4675 if (ret != NULL)
4676 xmlFreeElementContent(ret);
4677 return(NULL);
4678 }
4679 NEXT;
4680
4681 op = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_SEQ);
4682 if (op == NULL) {
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004683 if ((last != NULL) && (last != ret))
4684 xmlFreeElementContent(last);
Owen Taylor3473f882001-02-23 17:55:21 +00004685 xmlFreeElementContent(ret);
4686 return(NULL);
4687 }
4688 if (last == NULL) {
4689 op->c1 = ret;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004690 if (ret != NULL)
4691 ret->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004692 ret = cur = op;
4693 } else {
4694 cur->c2 = op;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004695 if (op != NULL)
4696 op->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004697 op->c1 = last;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004698 if (last != NULL)
4699 last->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004700 cur =op;
4701 last = NULL;
4702 }
4703 } else if (RAW == '|') {
4704 if (type == 0) type = CUR;
4705
4706 /*
4707 * Detect "Name , Name | Name" error
4708 */
4709 else if (type != CUR) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004710 xmlFatalErrMsgInt(ctxt, XML_ERR_SEPARATOR_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00004711 "xmlParseElementChildrenContentDecl : '%c' expected\n",
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004712 type);
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004713 if ((last != NULL) && (last != ret))
Owen Taylor3473f882001-02-23 17:55:21 +00004714 xmlFreeElementContent(last);
4715 if (ret != NULL)
4716 xmlFreeElementContent(ret);
4717 return(NULL);
4718 }
4719 NEXT;
4720
4721 op = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_OR);
4722 if (op == NULL) {
Daniel Veillardd54fa3e2002-02-20 16:48:52 +00004723 if ((last != NULL) && (last != ret))
Owen Taylor3473f882001-02-23 17:55:21 +00004724 xmlFreeElementContent(last);
4725 if (ret != NULL)
4726 xmlFreeElementContent(ret);
4727 return(NULL);
4728 }
4729 if (last == NULL) {
4730 op->c1 = ret;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004731 if (ret != NULL)
4732 ret->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004733 ret = cur = op;
4734 } else {
4735 cur->c2 = op;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004736 if (op != NULL)
4737 op->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004738 op->c1 = last;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004739 if (last != NULL)
4740 last->parent = op;
Owen Taylor3473f882001-02-23 17:55:21 +00004741 cur =op;
4742 last = NULL;
4743 }
4744 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004745 xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004746 if (ret != NULL)
4747 xmlFreeElementContent(ret);
4748 return(NULL);
4749 }
4750 GROW;
4751 SKIP_BLANKS;
4752 GROW;
4753 if (RAW == '(') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004754 int inputid = ctxt->input->id;
Owen Taylor3473f882001-02-23 17:55:21 +00004755 /* Recurse on second child */
4756 NEXT;
4757 SKIP_BLANKS;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004758 last = xmlParseElementChildrenContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004759 SKIP_BLANKS;
4760 } else {
Daniel Veillard76d66f42001-05-16 21:05:17 +00004761 elem = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004762 if (elem == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004763 xmlFatalErr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004764 if (ret != NULL)
4765 xmlFreeElementContent(ret);
4766 return(NULL);
4767 }
4768 last = xmlNewElementContent(elem, XML_ELEMENT_CONTENT_ELEMENT);
Owen Taylor3473f882001-02-23 17:55:21 +00004769 if (RAW == '?') {
4770 last->ocur = XML_ELEMENT_CONTENT_OPT;
4771 NEXT;
4772 } else if (RAW == '*') {
4773 last->ocur = XML_ELEMENT_CONTENT_MULT;
4774 NEXT;
4775 } else if (RAW == '+') {
4776 last->ocur = XML_ELEMENT_CONTENT_PLUS;
4777 NEXT;
4778 } else {
4779 last->ocur = XML_ELEMENT_CONTENT_ONCE;
4780 }
4781 }
4782 SKIP_BLANKS;
4783 GROW;
4784 }
4785 if ((cur != NULL) && (last != NULL)) {
4786 cur->c2 = last;
Daniel Veillarddab4cb32001-04-20 13:03:48 +00004787 if (last != NULL)
4788 last->parent = cur;
Owen Taylor3473f882001-02-23 17:55:21 +00004789 }
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004790 if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00004791 xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
4792"Element content declaration doesn't start and stop in the same entity\n",
4793 NULL);
Daniel Veillard8dc16a62002-02-19 21:08:48 +00004794 }
Owen Taylor3473f882001-02-23 17:55:21 +00004795 NEXT;
4796 if (RAW == '?') {
Daniel Veillarde470df72001-04-18 21:41:07 +00004797 if (ret != NULL)
4798 ret->ocur = XML_ELEMENT_CONTENT_OPT;
Owen Taylor3473f882001-02-23 17:55:21 +00004799 NEXT;
4800 } else if (RAW == '*') {
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004801 if (ret != NULL) {
Daniel Veillarde470df72001-04-18 21:41:07 +00004802 ret->ocur = XML_ELEMENT_CONTENT_MULT;
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004803 cur = ret;
4804 /*
4805 * Some normalization:
4806 * (a | b* | c?)* == (a | b | c)*
4807 */
4808 while (cur->type == XML_ELEMENT_CONTENT_OR) {
4809 if ((cur->c1 != NULL) &&
4810 ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4811 (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT)))
4812 cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
4813 if ((cur->c2 != NULL) &&
4814 ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
4815 (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT)))
4816 cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
4817 cur = cur->c2;
4818 }
4819 }
Owen Taylor3473f882001-02-23 17:55:21 +00004820 NEXT;
4821 } else if (RAW == '+') {
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004822 if (ret != NULL) {
4823 int found = 0;
4824
William M. Brackeb8509c2004-05-14 03:48:02 +00004825 if (ret->ocur == XML_ELEMENT_CONTENT_OPT)
4826 ret->ocur == XML_ELEMENT_CONTENT_MULT;
4827 else
4828 ret->ocur = XML_ELEMENT_CONTENT_PLUS;
Daniel Veillardce2c2f02001-10-18 14:57:24 +00004829 /*
4830 * Some normalization:
4831 * (a | b*)+ == (a | b)*
4832 * (a | b?)+ == (a | b)*
4833 */
4834 while (cur->type == XML_ELEMENT_CONTENT_OR) {
4835 if ((cur->c1 != NULL) &&
4836 ((cur->c1->ocur == XML_ELEMENT_CONTENT_OPT) ||
4837 (cur->c1->ocur == XML_ELEMENT_CONTENT_MULT))) {
4838 cur->c1->ocur = XML_ELEMENT_CONTENT_ONCE;
4839 found = 1;
4840 }
4841 if ((cur->c2 != NULL) &&
4842 ((cur->c2->ocur == XML_ELEMENT_CONTENT_OPT) ||
4843 (cur->c2->ocur == XML_ELEMENT_CONTENT_MULT))) {
4844 cur->c2->ocur = XML_ELEMENT_CONTENT_ONCE;
4845 found = 1;
4846 }
4847 cur = cur->c2;
4848 }
4849 if (found)
4850 ret->ocur = XML_ELEMENT_CONTENT_MULT;
4851 }
Owen Taylor3473f882001-02-23 17:55:21 +00004852 NEXT;
4853 }
4854 return(ret);
4855}
4856
4857/**
4858 * xmlParseElementContentDecl:
4859 * @ctxt: an XML parser context
4860 * @name: the name of the element being defined.
4861 * @result: the Element Content pointer will be stored here if any
4862 *
4863 * parse the declaration for an Element content either Mixed or Children,
4864 * the cases EMPTY and ANY are handled directly in xmlParseElementDecl
4865 *
4866 * [46] contentspec ::= 'EMPTY' | 'ANY' | Mixed | children
4867 *
4868 * returns: the type of element content XML_ELEMENT_TYPE_xxx
4869 */
4870
4871int
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004872xmlParseElementContentDecl(xmlParserCtxtPtr ctxt, const xmlChar *name,
Owen Taylor3473f882001-02-23 17:55:21 +00004873 xmlElementContentPtr *result) {
4874
4875 xmlElementContentPtr tree = NULL;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004876 int inputid = ctxt->input->id;
Owen Taylor3473f882001-02-23 17:55:21 +00004877 int res;
4878
4879 *result = NULL;
4880
4881 if (RAW != '(') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00004882 xmlFatalErrMsgStr(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
Daniel Veillard56a4cb82001-03-24 17:00:36 +00004883 "xmlParseElementContentDecl : %s '(' expected\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00004884 return(-1);
4885 }
4886 NEXT;
4887 GROW;
4888 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004889 if (CMP7(CUR_PTR, '#', 'P', 'C', 'D', 'A', 'T', 'A')) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004890 tree = xmlParseElementMixedContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004891 res = XML_ELEMENT_TYPE_MIXED;
4892 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004893 tree = xmlParseElementChildrenContentDecl(ctxt, inputid);
Owen Taylor3473f882001-02-23 17:55:21 +00004894 res = XML_ELEMENT_TYPE_ELEMENT;
4895 }
Owen Taylor3473f882001-02-23 17:55:21 +00004896 SKIP_BLANKS;
4897 *result = tree;
4898 return(res);
4899}
4900
4901/**
4902 * xmlParseElementDecl:
4903 * @ctxt: an XML parser context
4904 *
4905 * parse an Element declaration.
4906 *
4907 * [45] elementdecl ::= '<!ELEMENT' S Name S contentspec S? '>'
4908 *
4909 * [ VC: Unique Element Type Declaration ]
4910 * No element type may be declared more than once
4911 *
4912 * Returns the type of the element, or -1 in case of error
4913 */
4914int
4915xmlParseElementDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00004916 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00004917 int ret = -1;
4918 xmlElementContentPtr content = NULL;
4919
4920 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004921 if (CMP9(CUR_PTR, '<', '!', 'E', 'L', 'E', 'M', 'E', 'N', 'T')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004922 xmlParserInputPtr input = ctxt->input;
4923
4924 SKIP(9);
William M. Brack76e95df2003-10-18 16:20:14 +00004925 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004926 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4927 "Space required after 'ELEMENT'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004928 }
4929 SKIP_BLANKS;
Daniel Veillard76d66f42001-05-16 21:05:17 +00004930 name = xmlParseName(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00004931 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004932 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
4933 "xmlParseElementDecl: no name for Element\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004934 return(-1);
4935 }
4936 while ((RAW == 0) && (ctxt->inputNr > 1))
4937 xmlPopInput(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00004938 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004939 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
4940 "Space required after the element name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004941 }
4942 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00004943 if (CMP5(CUR_PTR, 'E', 'M', 'P', 'T', 'Y')) {
Owen Taylor3473f882001-02-23 17:55:21 +00004944 SKIP(5);
4945 /*
4946 * Element must always be empty.
4947 */
4948 ret = XML_ELEMENT_TYPE_EMPTY;
4949 } else if ((RAW == 'A') && (NXT(1) == 'N') &&
4950 (NXT(2) == 'Y')) {
4951 SKIP(3);
4952 /*
4953 * Element is a generic container.
4954 */
4955 ret = XML_ELEMENT_TYPE_ANY;
4956 } else if (RAW == '(') {
4957 ret = xmlParseElementContentDecl(ctxt, name, &content);
4958 } else {
4959 /*
4960 * [ WFC: PEs in Internal Subset ] error handling.
4961 */
4962 if ((RAW == '%') && (ctxt->external == 0) &&
4963 (ctxt->inputNr == 1)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00004964 xmlFatalErrMsg(ctxt, XML_ERR_PEREF_IN_INT_SUBSET,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004965 "PEReference: forbidden within markup decl in internal subset\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004966 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00004967 xmlFatalErrMsg(ctxt, XML_ERR_ELEMCONTENT_NOT_STARTED,
Owen Taylor3473f882001-02-23 17:55:21 +00004968 "xmlParseElementDecl: 'EMPTY', 'ANY' or '(' expected\n");
4969 }
Owen Taylor3473f882001-02-23 17:55:21 +00004970 return(-1);
4971 }
4972
4973 SKIP_BLANKS;
4974 /*
4975 * Pop-up of finished entities.
4976 */
4977 while ((RAW == 0) && (ctxt->inputNr > 1))
4978 xmlPopInput(ctxt);
4979 SKIP_BLANKS;
4980
4981 if (RAW != '>') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00004982 xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00004983 } else {
4984 if (input != ctxt->input) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00004985 xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_BOUNDARY,
4986 "Element declaration doesn't start and stop in the same entity\n");
Owen Taylor3473f882001-02-23 17:55:21 +00004987 }
4988
4989 NEXT;
4990 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
4991 (ctxt->sax->elementDecl != NULL))
4992 ctxt->sax->elementDecl(ctxt->userData, name, ret,
4993 content);
4994 }
4995 if (content != NULL) {
4996 xmlFreeElementContent(content);
4997 }
Owen Taylor3473f882001-02-23 17:55:21 +00004998 }
4999 return(ret);
5000}
5001
5002/**
Owen Taylor3473f882001-02-23 17:55:21 +00005003 * xmlParseConditionalSections
5004 * @ctxt: an XML parser context
5005 *
5006 * [61] conditionalSect ::= includeSect | ignoreSect
5007 * [62] includeSect ::= '<![' S? 'INCLUDE' S? '[' extSubsetDecl ']]>'
5008 * [63] ignoreSect ::= '<![' S? 'IGNORE' S? '[' ignoreSectContents* ']]>'
5009 * [64] ignoreSectContents ::= Ignore ('<![' ignoreSectContents ']]>' Ignore)*
5010 * [65] Ignore ::= Char* - (Char* ('<![' | ']]>') Char*)
5011 */
5012
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005013static void
Owen Taylor3473f882001-02-23 17:55:21 +00005014xmlParseConditionalSections(xmlParserCtxtPtr ctxt) {
5015 SKIP(3);
5016 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00005017 if (CMP7(CUR_PTR, 'I', 'N', 'C', 'L', 'U', 'D', 'E')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005018 SKIP(7);
5019 SKIP_BLANKS;
5020 if (RAW != '[') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005021 xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005022 } else {
5023 NEXT;
5024 }
5025 if (xmlParserDebugEntities) {
5026 if ((ctxt->input != NULL) && (ctxt->input->filename))
5027 xmlGenericError(xmlGenericErrorContext,
5028 "%s(%d): ", ctxt->input->filename,
5029 ctxt->input->line);
5030 xmlGenericError(xmlGenericErrorContext,
5031 "Entering INCLUDE Conditional Section\n");
5032 }
5033
5034 while ((RAW != 0) && ((RAW != ']') || (NXT(1) != ']') ||
5035 (NXT(2) != '>'))) {
5036 const xmlChar *check = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00005037 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00005038
5039 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5040 xmlParseConditionalSections(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00005041 } else if (IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005042 NEXT;
5043 } else if (RAW == '%') {
5044 xmlParsePEReference(ctxt);
5045 } else
5046 xmlParseMarkupDecl(ctxt);
5047
5048 /*
5049 * Pop-up of finished entities.
5050 */
5051 while ((RAW == 0) && (ctxt->inputNr > 1))
5052 xmlPopInput(ctxt);
5053
Daniel Veillardfdc91562002-07-01 21:52:03 +00005054 if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005055 xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005056 break;
5057 }
5058 }
5059 if (xmlParserDebugEntities) {
5060 if ((ctxt->input != NULL) && (ctxt->input->filename))
5061 xmlGenericError(xmlGenericErrorContext,
5062 "%s(%d): ", ctxt->input->filename,
5063 ctxt->input->line);
5064 xmlGenericError(xmlGenericErrorContext,
5065 "Leaving INCLUDE Conditional Section\n");
5066 }
5067
Daniel Veillarda07050d2003-10-19 14:46:32 +00005068 } else if (CMP6(CUR_PTR, 'I', 'G', 'N', 'O', 'R', 'E')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005069 int state;
William M. Brack78637da2003-07-31 14:47:38 +00005070 xmlParserInputState instate;
Owen Taylor3473f882001-02-23 17:55:21 +00005071 int depth = 0;
5072
5073 SKIP(6);
5074 SKIP_BLANKS;
5075 if (RAW != '[') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005076 xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005077 } else {
5078 NEXT;
5079 }
5080 if (xmlParserDebugEntities) {
5081 if ((ctxt->input != NULL) && (ctxt->input->filename))
5082 xmlGenericError(xmlGenericErrorContext,
5083 "%s(%d): ", ctxt->input->filename,
5084 ctxt->input->line);
5085 xmlGenericError(xmlGenericErrorContext,
5086 "Entering IGNORE Conditional Section\n");
5087 }
5088
5089 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005090 * Parse up to the end of the conditional section
Owen Taylor3473f882001-02-23 17:55:21 +00005091 * But disable SAX event generating DTD building in the meantime
5092 */
5093 state = ctxt->disableSAX;
5094 instate = ctxt->instate;
Daniel Veillarddad3f682002-11-17 16:47:27 +00005095 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00005096 ctxt->instate = XML_PARSER_IGNORE;
5097
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00005098 while ((depth >= 0) && (RAW != 0)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005099 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5100 depth++;
5101 SKIP(3);
5102 continue;
5103 }
5104 if ((RAW == ']') && (NXT(1) == ']') && (NXT(2) == '>')) {
5105 if (--depth >= 0) SKIP(3);
5106 continue;
5107 }
5108 NEXT;
5109 continue;
5110 }
5111
5112 ctxt->disableSAX = state;
5113 ctxt->instate = instate;
5114
5115 if (xmlParserDebugEntities) {
5116 if ((ctxt->input != NULL) && (ctxt->input->filename))
5117 xmlGenericError(xmlGenericErrorContext,
5118 "%s(%d): ", ctxt->input->filename,
5119 ctxt->input->line);
5120 xmlGenericError(xmlGenericErrorContext,
5121 "Leaving IGNORE Conditional Section\n");
5122 }
5123
5124 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005125 xmlFatalErr(ctxt, XML_ERR_CONDSEC_INVALID_KEYWORD, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005126 }
5127
5128 if (RAW == 0)
5129 SHRINK;
5130
5131 if (RAW == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005132 xmlFatalErr(ctxt, XML_ERR_CONDSEC_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005133 } else {
5134 SKIP(3);
5135 }
5136}
5137
5138/**
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005139 * xmlParseMarkupDecl:
5140 * @ctxt: an XML parser context
5141 *
5142 * parse Markup declarations
5143 *
5144 * [29] markupdecl ::= elementdecl | AttlistDecl | EntityDecl |
5145 * NotationDecl | PI | Comment
5146 *
5147 * [ VC: Proper Declaration/PE Nesting ]
5148 * Parameter-entity replacement text must be properly nested with
5149 * markup declarations. That is to say, if either the first character
5150 * or the last character of a markup declaration (markupdecl above) is
5151 * contained in the replacement text for a parameter-entity reference,
5152 * both must be contained in the same replacement text.
5153 *
5154 * [ WFC: PEs in Internal Subset ]
5155 * In the internal DTD subset, parameter-entity references can occur
5156 * only where markup declarations can occur, not within markup declarations.
5157 * (This does not apply to references that occur in external parameter
5158 * entities or to the external subset.)
5159 */
5160void
5161xmlParseMarkupDecl(xmlParserCtxtPtr ctxt) {
5162 GROW;
5163 xmlParseElementDecl(ctxt);
5164 xmlParseAttributeListDecl(ctxt);
5165 xmlParseEntityDecl(ctxt);
5166 xmlParseNotationDecl(ctxt);
5167 xmlParsePI(ctxt);
5168 xmlParseComment(ctxt);
5169 /*
5170 * This is only for internal subset. On external entities,
5171 * the replacement is done before parsing stage
5172 */
5173 if ((ctxt->external == 0) && (ctxt->inputNr == 1))
5174 xmlParsePEReference(ctxt);
5175
5176 /*
5177 * Conditional sections are allowed from entities included
5178 * by PE References in the internal subset.
5179 */
5180 if ((ctxt->external == 0) && (ctxt->inputNr > 1)) {
5181 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5182 xmlParseConditionalSections(ctxt);
5183 }
5184 }
5185
5186 ctxt->instate = XML_PARSER_DTD;
5187}
5188
5189/**
5190 * xmlParseTextDecl:
5191 * @ctxt: an XML parser context
5192 *
5193 * parse an XML declaration header for external entities
5194 *
5195 * [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>'
5196 *
5197 * Question: Seems that EncodingDecl is mandatory ? Is that a typo ?
5198 */
5199
5200void
5201xmlParseTextDecl(xmlParserCtxtPtr ctxt) {
5202 xmlChar *version;
Daniel Veillardf5cb3cd2003-10-28 13:58:13 +00005203 const xmlChar *encoding;
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005204
5205 /*
5206 * We know that '<?xml' is here.
5207 */
Daniel Veillarda07050d2003-10-19 14:46:32 +00005208 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005209 SKIP(5);
5210 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005211 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_STARTED, NULL);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005212 return;
5213 }
5214
William M. Brack76e95df2003-10-18 16:20:14 +00005215 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005216 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5217 "Space needed after '<?xml'\n");
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005218 }
5219 SKIP_BLANKS;
5220
5221 /*
5222 * We may have the VersionInfo here.
5223 */
5224 version = xmlParseVersionInfo(ctxt);
5225 if (version == NULL)
5226 version = xmlCharStrdup(XML_DEFAULT_VERSION);
Daniel Veillard401c2112002-01-07 16:54:10 +00005227 else {
William M. Brack76e95df2003-10-18 16:20:14 +00005228 if (!IS_BLANK_CH(CUR)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005229 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
5230 "Space needed here\n");
Daniel Veillard401c2112002-01-07 16:54:10 +00005231 }
5232 }
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005233 ctxt->input->version = version;
5234
5235 /*
5236 * We must have the encoding declaration
5237 */
Daniel Veillardf5cb3cd2003-10-28 13:58:13 +00005238 encoding = xmlParseEncodingDecl(ctxt);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005239 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
5240 /*
5241 * The XML REC instructs us to stop parsing right here
5242 */
5243 return;
5244 }
Daniel Veillardf5cb3cd2003-10-28 13:58:13 +00005245 if ((encoding == NULL) && (ctxt->errNo == XML_ERR_OK)) {
5246 xmlFatalErrMsg(ctxt, XML_ERR_MISSING_ENCODING,
5247 "Missing encoding in text declaration\n");
5248 }
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005249
5250 SKIP_BLANKS;
5251 if ((RAW == '?') && (NXT(1) == '>')) {
5252 SKIP(2);
5253 } else if (RAW == '>') {
5254 /* Deprecated old WD ... */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005255 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005256 NEXT;
5257 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005258 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Daniel Veillard5e3eecb2001-07-31 15:10:53 +00005259 MOVETO_ENDTAG(CUR_PTR);
5260 NEXT;
5261 }
5262}
5263
5264/**
Owen Taylor3473f882001-02-23 17:55:21 +00005265 * xmlParseExternalSubset:
5266 * @ctxt: an XML parser context
5267 * @ExternalID: the external identifier
5268 * @SystemID: the system identifier (or URL)
5269 *
5270 * parse Markup declarations from an external subset
5271 *
5272 * [30] extSubset ::= textDecl? extSubsetDecl
5273 *
5274 * [31] extSubsetDecl ::= (markupdecl | conditionalSect | PEReference | S) *
5275 */
5276void
5277xmlParseExternalSubset(xmlParserCtxtPtr ctxt, const xmlChar *ExternalID,
5278 const xmlChar *SystemID) {
Daniel Veillard309f81d2003-09-23 09:02:53 +00005279 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005280 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00005281 if (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) {
Owen Taylor3473f882001-02-23 17:55:21 +00005282 xmlParseTextDecl(ctxt);
5283 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
5284 /*
5285 * The XML REC instructs us to stop parsing right here
5286 */
5287 ctxt->instate = XML_PARSER_EOF;
5288 return;
5289 }
5290 }
5291 if (ctxt->myDoc == NULL) {
5292 ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
5293 }
5294 if ((ctxt->myDoc != NULL) && (ctxt->myDoc->intSubset == NULL))
5295 xmlCreateIntSubset(ctxt->myDoc, NULL, ExternalID, SystemID);
5296
5297 ctxt->instate = XML_PARSER_DTD;
5298 ctxt->external = 1;
5299 while (((RAW == '<') && (NXT(1) == '?')) ||
5300 ((RAW == '<') && (NXT(1) == '!')) ||
William M. Brack76e95df2003-10-18 16:20:14 +00005301 (RAW == '%') || IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005302 const xmlChar *check = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00005303 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00005304
5305 GROW;
5306 if ((RAW == '<') && (NXT(1) == '!') && (NXT(2) == '[')) {
5307 xmlParseConditionalSections(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00005308 } else if (IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00005309 NEXT;
5310 } else if (RAW == '%') {
5311 xmlParsePEReference(ctxt);
5312 } else
5313 xmlParseMarkupDecl(ctxt);
5314
5315 /*
5316 * Pop-up of finished entities.
5317 */
5318 while ((RAW == 0) && (ctxt->inputNr > 1))
5319 xmlPopInput(ctxt);
5320
Daniel Veillardfdc91562002-07-01 21:52:03 +00005321 if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005322 xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005323 break;
5324 }
5325 }
5326
5327 if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005328 xmlFatalErr(ctxt, XML_ERR_EXT_SUBSET_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005329 }
5330
5331}
5332
5333/**
5334 * xmlParseReference:
5335 * @ctxt: an XML parser context
5336 *
5337 * parse and handle entity references in content, depending on the SAX
5338 * interface, this may end-up in a call to character() if this is a
5339 * CharRef, a predefined entity, if there is no reference() callback.
5340 * or if the parser was asked to switch to that mode.
5341 *
5342 * [67] Reference ::= EntityRef | CharRef
5343 */
5344void
5345xmlParseReference(xmlParserCtxtPtr ctxt) {
5346 xmlEntityPtr ent;
5347 xmlChar *val;
5348 if (RAW != '&') return;
5349
5350 if (NXT(1) == '#') {
5351 int i = 0;
5352 xmlChar out[10];
5353 int hex = NXT(2);
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005354 int value = xmlParseCharRef(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00005355
5356 if (ctxt->charset != XML_CHAR_ENCODING_UTF8) {
5357 /*
5358 * So we are using non-UTF-8 buffers
5359 * Check that the char fit on 8bits, if not
5360 * generate a CharRef.
5361 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005362 if (value <= 0xFF) {
5363 out[0] = value;
Owen Taylor3473f882001-02-23 17:55:21 +00005364 out[1] = 0;
5365 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
5366 (!ctxt->disableSAX))
5367 ctxt->sax->characters(ctxt->userData, out, 1);
5368 } else {
5369 if ((hex == 'x') || (hex == 'X'))
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005370 snprintf((char *)out, sizeof(out), "#x%X", value);
Owen Taylor3473f882001-02-23 17:55:21 +00005371 else
Aleksey Sanin49cc9752002-06-14 17:07:10 +00005372 snprintf((char *)out, sizeof(out), "#%d", value);
Owen Taylor3473f882001-02-23 17:55:21 +00005373 if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) &&
5374 (!ctxt->disableSAX))
5375 ctxt->sax->reference(ctxt->userData, out);
5376 }
5377 } else {
5378 /*
5379 * Just encode the value in UTF-8
5380 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00005381 COPY_BUF(0 ,out, i, value);
Owen Taylor3473f882001-02-23 17:55:21 +00005382 out[i] = 0;
5383 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
5384 (!ctxt->disableSAX))
5385 ctxt->sax->characters(ctxt->userData, out, i);
5386 }
5387 } else {
5388 ent = xmlParseEntityRef(ctxt);
5389 if (ent == NULL) return;
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00005390 if (!ctxt->wellFormed)
5391 return;
Owen Taylor3473f882001-02-23 17:55:21 +00005392 if ((ent->name != NULL) &&
5393 (ent->etype != XML_INTERNAL_PREDEFINED_ENTITY)) {
5394 xmlNodePtr list = NULL;
Daniel Veillard7d515752003-09-26 19:12:37 +00005395 xmlParserErrors ret = XML_ERR_OK;
Owen Taylor3473f882001-02-23 17:55:21 +00005396
5397
5398 /*
5399 * The first reference to the entity trigger a parsing phase
5400 * where the ent->children is filled with the result from
5401 * the parsing.
5402 */
5403 if (ent->children == NULL) {
5404 xmlChar *value;
5405 value = ent->content;
5406
5407 /*
5408 * Check that this entity is well formed
5409 */
Daniel Veillard4aede2e2003-10-17 12:43:59 +00005410 if ((value != NULL) && (value[0] != 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +00005411 (value[1] == 0) && (value[0] == '<') &&
5412 (xmlStrEqual(ent->name, BAD_CAST "lt"))) {
5413 /*
5414 * DONE: get definite answer on this !!!
5415 * Lots of entity decls are used to declare a single
5416 * char
5417 * <!ENTITY lt "<">
5418 * Which seems to be valid since
5419 * 2.4: The ampersand character (&) and the left angle
5420 * bracket (<) may appear in their literal form only
5421 * when used ... They are also legal within the literal
5422 * entity value of an internal entity declaration;i
5423 * see "4.3.2 Well-Formed Parsed Entities".
5424 * IMHO 2.4 and 4.3.2 are directly in contradiction.
5425 * Looking at the OASIS test suite and James Clark
5426 * tests, this is broken. However the XML REC uses
5427 * it. Is the XML REC not well-formed ????
5428 * This is a hack to avoid this problem
5429 *
5430 * ANSWER: since lt gt amp .. are already defined,
5431 * this is a redefinition and hence the fact that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005432 * content is not well balanced is not a Wf error, this
Owen Taylor3473f882001-02-23 17:55:21 +00005433 * is lousy but acceptable.
5434 */
5435 list = xmlNewDocText(ctxt->myDoc, value);
5436 if (list != NULL) {
5437 if ((ent->etype == XML_INTERNAL_GENERAL_ENTITY) &&
5438 (ent->children == NULL)) {
5439 ent->children = list;
5440 ent->last = list;
Daniel Veillard2d84a892002-12-30 00:01:08 +00005441 ent->owner = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00005442 list->parent = (xmlNodePtr) ent;
5443 } else {
5444 xmlFreeNodeList(list);
5445 }
5446 } else if (list != NULL) {
5447 xmlFreeNodeList(list);
5448 }
5449 } else {
5450 /*
5451 * 4.3.2: An internal general parsed entity is well-formed
5452 * if its replacement text matches the production labeled
5453 * content.
5454 */
Daniel Veillardb5a60ec2002-03-18 11:45:56 +00005455
5456 void *user_data;
5457 /*
5458 * This is a bit hackish but this seems the best
5459 * way to make sure both SAX and DOM entity support
5460 * behaves okay.
5461 */
5462 if (ctxt->userData == ctxt)
5463 user_data = NULL;
5464 else
5465 user_data = ctxt->userData;
5466
Owen Taylor3473f882001-02-23 17:55:21 +00005467 if (ent->etype == XML_INTERNAL_GENERAL_ENTITY) {
5468 ctxt->depth++;
Daniel Veillard328f48c2002-11-15 15:24:34 +00005469 ret = xmlParseBalancedChunkMemoryInternal(ctxt,
5470 value, user_data, &list);
Owen Taylor3473f882001-02-23 17:55:21 +00005471 ctxt->depth--;
5472 } else if (ent->etype ==
5473 XML_EXTERNAL_GENERAL_PARSED_ENTITY) {
5474 ctxt->depth++;
Daniel Veillarda97a19b2001-05-20 13:19:52 +00005475 ret = xmlParseExternalEntityPrivate(ctxt->myDoc, ctxt,
Daniel Veillardb5a60ec2002-03-18 11:45:56 +00005476 ctxt->sax, user_data, ctxt->depth,
Daniel Veillarda97a19b2001-05-20 13:19:52 +00005477 ent->URI, ent->ExternalID, &list);
Owen Taylor3473f882001-02-23 17:55:21 +00005478 ctxt->depth--;
5479 } else {
Daniel Veillard7d515752003-09-26 19:12:37 +00005480 ret = XML_ERR_ENTITY_PE_INTERNAL;
Daniel Veillardf403d292003-10-05 13:51:35 +00005481 xmlErrMsgStr(ctxt, XML_ERR_INTERNAL_ERROR,
5482 "invalid entity type found\n", NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005483 }
5484 if (ret == XML_ERR_ENTITY_LOOP) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005485 xmlFatalErr(ctxt, XML_ERR_ENTITY_LOOP, NULL);
Daniel Veillardbb7ddb32002-02-17 21:26:33 +00005486 return;
Daniel Veillard7d515752003-09-26 19:12:37 +00005487 } else if ((ret == XML_ERR_OK) && (list != NULL)) {
Daniel Veillard76d66f42001-05-16 21:05:17 +00005488 if (((ent->etype == XML_INTERNAL_GENERAL_ENTITY) ||
5489 (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY))&&
Owen Taylor3473f882001-02-23 17:55:21 +00005490 (ent->children == NULL)) {
5491 ent->children = list;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005492 if (ctxt->replaceEntities) {
5493 /*
5494 * Prune it directly in the generated document
5495 * except for single text nodes.
5496 */
5497 if ((list->type == XML_TEXT_NODE) &&
5498 (list->next == NULL)) {
5499 list->parent = (xmlNodePtr) ent;
5500 list = NULL;
Daniel Veillard2d84a892002-12-30 00:01:08 +00005501 ent->owner = 1;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005502 } else {
Daniel Veillard2d84a892002-12-30 00:01:08 +00005503 ent->owner = 0;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005504 while (list != NULL) {
5505 list->parent = (xmlNodePtr) ctxt->node;
Daniel Veillard68e9e742002-11-16 15:35:11 +00005506 list->doc = ctxt->myDoc;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005507 if (list->next == NULL)
5508 ent->last = list;
5509 list = list->next;
Daniel Veillard8107a222002-01-13 14:10:10 +00005510 }
Daniel Veillard62f313b2001-07-04 19:49:14 +00005511 list = ent->children;
Daniel Veillard81273902003-09-30 00:43:48 +00005512#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +00005513 if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
5514 xmlAddEntityReference(ent, list, NULL);
Daniel Veillard81273902003-09-30 00:43:48 +00005515#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillard62f313b2001-07-04 19:49:14 +00005516 }
5517 } else {
Daniel Veillard2d84a892002-12-30 00:01:08 +00005518 ent->owner = 1;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005519 while (list != NULL) {
5520 list->parent = (xmlNodePtr) ent;
5521 if (list->next == NULL)
5522 ent->last = list;
5523 list = list->next;
5524 }
Owen Taylor3473f882001-02-23 17:55:21 +00005525 }
5526 } else {
5527 xmlFreeNodeList(list);
Daniel Veillard62f313b2001-07-04 19:49:14 +00005528 list = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005529 }
William M. Brackb670e2e2003-09-27 01:05:55 +00005530 } else if ((ret != XML_ERR_OK) &&
5531 (ret != XML_WAR_UNDECLARED_ENTITY)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00005532 xmlFatalErr(ctxt, ret, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005533 } else if (list != NULL) {
5534 xmlFreeNodeList(list);
Daniel Veillard62f313b2001-07-04 19:49:14 +00005535 list = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00005536 }
5537 }
5538 }
5539 if ((ctxt->sax != NULL) && (ctxt->sax->reference != NULL) &&
5540 (ctxt->replaceEntities == 0) && (!ctxt->disableSAX)) {
5541 /*
5542 * Create a node.
5543 */
5544 ctxt->sax->reference(ctxt->userData, ent->name);
5545 return;
5546 } else if (ctxt->replaceEntities) {
5547 if ((ctxt->node != NULL) && (ent->children != NULL)) {
5548 /*
5549 * Seems we are generating the DOM content, do
Daniel Veillard62f313b2001-07-04 19:49:14 +00005550 * a simple tree copy for all references except the first
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005551 * In the first occurrence list contains the replacement
Owen Taylor3473f882001-02-23 17:55:21 +00005552 */
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005553 if ((list == NULL) && (ent->owner == 0)) {
5554 xmlNodePtr nw = NULL, cur, firstChild = NULL;
Daniel Veillard62f313b2001-07-04 19:49:14 +00005555 cur = ent->children;
5556 while (cur != NULL) {
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005557 nw = xmlCopyNode(cur, 1);
5558 if (nw != NULL) {
5559 nw->_private = cur->_private;
Daniel Veillard8f872442003-01-09 23:19:02 +00005560 if (firstChild == NULL){
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005561 firstChild = nw;
Daniel Veillard8f872442003-01-09 23:19:02 +00005562 }
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005563 xmlAddChild(ctxt->node, nw);
Daniel Veillard8107a222002-01-13 14:10:10 +00005564 }
Daniel Veillard62f313b2001-07-04 19:49:14 +00005565 if (cur == ent->last)
5566 break;
5567 cur = cur->next;
5568 }
Daniel Veillard81273902003-09-30 00:43:48 +00005569#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +00005570 if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005571 xmlAddEntityReference(ent, firstChild, nw);
Daniel Veillard81273902003-09-30 00:43:48 +00005572#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005573 } else if (list == NULL) {
5574 xmlNodePtr nw = NULL, cur, next, last,
5575 firstChild = NULL;
5576 /*
5577 * Copy the entity child list and make it the new
5578 * entity child list. The goal is to make sure any
5579 * ID or REF referenced will be the one from the
5580 * document content and not the entity copy.
5581 */
5582 cur = ent->children;
5583 ent->children = NULL;
5584 last = ent->last;
5585 ent->last = NULL;
5586 while (cur != NULL) {
5587 next = cur->next;
5588 cur->next = NULL;
5589 cur->parent = NULL;
5590 nw = xmlCopyNode(cur, 1);
5591 if (nw != NULL) {
5592 nw->_private = cur->_private;
5593 if (firstChild == NULL){
5594 firstChild = cur;
5595 }
5596 xmlAddChild((xmlNodePtr) ent, nw);
5597 xmlAddChild(ctxt->node, cur);
5598 }
5599 if (cur == last)
5600 break;
5601 cur = next;
5602 }
5603 ent->owner = 1;
Daniel Veillard81273902003-09-30 00:43:48 +00005604#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillardef8dd7b2003-03-23 12:02:56 +00005605 if (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)
5606 xmlAddEntityReference(ent, firstChild, nw);
Daniel Veillard81273902003-09-30 00:43:48 +00005607#endif /* LIBXML_LEGACY_ENABLED */
Daniel Veillard62f313b2001-07-04 19:49:14 +00005608 } else {
5609 /*
5610 * the name change is to avoid coalescing of the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005611 * node with a possible previous text one which
5612 * would make ent->children a dangling pointer
Daniel Veillard62f313b2001-07-04 19:49:14 +00005613 */
5614 if (ent->children->type == XML_TEXT_NODE)
5615 ent->children->name = xmlStrdup(BAD_CAST "nbktext");
5616 if ((ent->last != ent->children) &&
5617 (ent->last->type == XML_TEXT_NODE))
5618 ent->last->name = xmlStrdup(BAD_CAST "nbktext");
5619 xmlAddChildList(ctxt->node, ent->children);
5620 }
5621
Owen Taylor3473f882001-02-23 17:55:21 +00005622 /*
5623 * This is to avoid a nasty side effect, see
5624 * characters() in SAX.c
5625 */
5626 ctxt->nodemem = 0;
5627 ctxt->nodelen = 0;
5628 return;
5629 } else {
5630 /*
5631 * Probably running in SAX mode
5632 */
5633 xmlParserInputPtr input;
5634
5635 input = xmlNewEntityInputStream(ctxt, ent);
5636 xmlPushInput(ctxt, input);
5637 if ((ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY) &&
Daniel Veillarda07050d2003-10-19 14:46:32 +00005638 (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) &&
5639 (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00005640 xmlParseTextDecl(ctxt);
5641 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
5642 /*
5643 * The XML REC instructs us to stop parsing right here
5644 */
5645 ctxt->instate = XML_PARSER_EOF;
5646 return;
5647 }
5648 if (input->standalone == 1) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005649 xmlFatalErr(ctxt, XML_ERR_EXT_ENTITY_STANDALONE,
5650 NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005651 }
5652 }
5653 return;
5654 }
5655 }
5656 } else {
5657 val = ent->content;
5658 if (val == NULL) return;
5659 /*
5660 * inline the entity.
5661 */
5662 if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL) &&
5663 (!ctxt->disableSAX))
5664 ctxt->sax->characters(ctxt->userData, val, xmlStrlen(val));
5665 }
5666 }
5667}
5668
5669/**
5670 * xmlParseEntityRef:
5671 * @ctxt: an XML parser context
5672 *
5673 * parse ENTITY references declarations
5674 *
5675 * [68] EntityRef ::= '&' Name ';'
5676 *
5677 * [ WFC: Entity Declared ]
5678 * In a document without any DTD, a document with only an internal DTD
5679 * subset which contains no parameter entity references, or a document
5680 * with "standalone='yes'", the Name given in the entity reference
5681 * must match that in an entity declaration, except that well-formed
5682 * documents need not declare any of the following entities: amp, lt,
5683 * gt, apos, quot. The declaration of a parameter entity must precede
5684 * any reference to it. Similarly, the declaration of a general entity
5685 * must precede any reference to it which appears in a default value in an
5686 * attribute-list declaration. Note that if entities are declared in the
5687 * external subset or in external parameter entities, a non-validating
5688 * processor is not obligated to read and process their declarations;
5689 * for such documents, the rule that an entity must be declared is a
5690 * well-formedness constraint only if standalone='yes'.
5691 *
5692 * [ WFC: Parsed Entity ]
5693 * An entity reference must not contain the name of an unparsed entity
5694 *
5695 * Returns the xmlEntityPtr if found, or NULL otherwise.
5696 */
5697xmlEntityPtr
5698xmlParseEntityRef(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00005699 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00005700 xmlEntityPtr ent = NULL;
5701
5702 GROW;
5703
5704 if (RAW == '&') {
5705 NEXT;
5706 name = xmlParseName(ctxt);
5707 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005708 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
5709 "xmlParseEntityRef: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005710 } else {
5711 if (RAW == ';') {
5712 NEXT;
5713 /*
5714 * Ask first SAX for entity resolution, otherwise try the
5715 * predefined set.
5716 */
5717 if (ctxt->sax != NULL) {
5718 if (ctxt->sax->getEntity != NULL)
5719 ent = ctxt->sax->getEntity(ctxt->userData, name);
Daniel Veillard39eb88b2003-03-11 11:21:28 +00005720 if ((ctxt->wellFormed == 1 ) && (ent == NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00005721 ent = xmlGetPredefinedEntity(name);
Daniel Veillard39eb88b2003-03-11 11:21:28 +00005722 if ((ctxt->wellFormed == 1 ) && (ent == NULL) &&
5723 (ctxt->userData==ctxt)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00005724 ent = xmlSAX2GetEntity(ctxt, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00005725 }
Owen Taylor3473f882001-02-23 17:55:21 +00005726 }
5727 /*
5728 * [ WFC: Entity Declared ]
5729 * In a document without any DTD, a document with only an
5730 * internal DTD subset which contains no parameter entity
5731 * references, or a document with "standalone='yes'", the
5732 * Name given in the entity reference must match that in an
5733 * entity declaration, except that well-formed documents
5734 * need not declare any of the following entities: amp, lt,
5735 * gt, apos, quot.
5736 * The declaration of a parameter entity must precede any
5737 * reference to it.
5738 * Similarly, the declaration of a general entity must
5739 * precede any reference to it which appears in a default
5740 * value in an attribute-list declaration. Note that if
5741 * entities are declared in the external subset or in
5742 * external parameter entities, a non-validating processor
5743 * is not obligated to read and process their declarations;
5744 * for such documents, the rule that an entity must be
5745 * declared is a well-formedness constraint only if
5746 * standalone='yes'.
5747 */
5748 if (ent == NULL) {
5749 if ((ctxt->standalone == 1) ||
5750 ((ctxt->hasExternalSubset == 0) &&
5751 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005752 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00005753 "Entity '%s' not defined\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005754 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00005755 xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00005756 "Entity '%s' not defined\n", name);
5757 }
Daniel Veillardf403d292003-10-05 13:51:35 +00005758 ctxt->valid = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00005759 }
5760
5761 /*
5762 * [ WFC: Parsed Entity ]
5763 * An entity reference must not contain the name of an
5764 * unparsed entity
5765 */
5766 else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005767 xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00005768 "Entity reference to unparsed entity %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005769 }
5770
5771 /*
5772 * [ WFC: No External Entity References ]
5773 * Attribute values cannot contain direct or indirect
5774 * entity references to external entities.
5775 */
5776 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
5777 (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005778 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL,
5779 "Attribute references external entity '%s'\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005780 }
5781 /*
5782 * [ WFC: No < in Attribute Values ]
5783 * The replacement text of any entity referred to directly or
5784 * indirectly in an attribute value (other than "&lt;") must
5785 * not contain a <.
5786 */
5787 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
5788 (ent != NULL) &&
5789 (!xmlStrEqual(ent->name, BAD_CAST "lt")) &&
5790 (ent->content != NULL) &&
5791 (xmlStrchr(ent->content, '<'))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005792 xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
Owen Taylor3473f882001-02-23 17:55:21 +00005793 "'<' in entity '%s' is not allowed in attributes values\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005794 }
5795
5796 /*
5797 * Internal check, no parameter entities here ...
5798 */
5799 else {
5800 switch (ent->etype) {
5801 case XML_INTERNAL_PARAMETER_ENTITY:
5802 case XML_EXTERNAL_PARAMETER_ENTITY:
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005803 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER,
5804 "Attempt to reference the parameter entity '%s'\n",
5805 name);
Owen Taylor3473f882001-02-23 17:55:21 +00005806 break;
5807 default:
5808 break;
5809 }
5810 }
5811
5812 /*
5813 * [ WFC: No Recursion ]
5814 * A parsed entity must not contain a recursive reference
5815 * to itself, either directly or indirectly.
5816 * Done somewhere else
5817 */
5818
5819 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005820 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005821 }
Owen Taylor3473f882001-02-23 17:55:21 +00005822 }
5823 }
5824 return(ent);
5825}
5826
5827/**
5828 * xmlParseStringEntityRef:
5829 * @ctxt: an XML parser context
5830 * @str: a pointer to an index in the string
5831 *
5832 * parse ENTITY references declarations, but this version parses it from
5833 * a string value.
5834 *
5835 * [68] EntityRef ::= '&' Name ';'
5836 *
5837 * [ WFC: Entity Declared ]
5838 * In a document without any DTD, a document with only an internal DTD
5839 * subset which contains no parameter entity references, or a document
5840 * with "standalone='yes'", the Name given in the entity reference
5841 * must match that in an entity declaration, except that well-formed
5842 * documents need not declare any of the following entities: amp, lt,
5843 * gt, apos, quot. The declaration of a parameter entity must precede
5844 * any reference to it. Similarly, the declaration of a general entity
5845 * must precede any reference to it which appears in a default value in an
5846 * attribute-list declaration. Note that if entities are declared in the
5847 * external subset or in external parameter entities, a non-validating
5848 * processor is not obligated to read and process their declarations;
5849 * for such documents, the rule that an entity must be declared is a
5850 * well-formedness constraint only if standalone='yes'.
5851 *
5852 * [ WFC: Parsed Entity ]
5853 * An entity reference must not contain the name of an unparsed entity
5854 *
5855 * Returns the xmlEntityPtr if found, or NULL otherwise. The str pointer
5856 * is updated to the current location in the string.
5857 */
5858xmlEntityPtr
5859xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) {
5860 xmlChar *name;
5861 const xmlChar *ptr;
5862 xmlChar cur;
5863 xmlEntityPtr ent = NULL;
5864
5865 if ((str == NULL) || (*str == NULL))
5866 return(NULL);
5867 ptr = *str;
5868 cur = *ptr;
5869 if (cur == '&') {
5870 ptr++;
5871 cur = *ptr;
5872 name = xmlParseStringName(ctxt, &ptr);
5873 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005874 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
5875 "xmlParseStringEntityRef: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00005876 } else {
5877 if (*ptr == ';') {
5878 ptr++;
5879 /*
5880 * Ask first SAX for entity resolution, otherwise try the
5881 * predefined set.
5882 */
5883 if (ctxt->sax != NULL) {
5884 if (ctxt->sax->getEntity != NULL)
5885 ent = ctxt->sax->getEntity(ctxt->userData, name);
5886 if (ent == NULL)
5887 ent = xmlGetPredefinedEntity(name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00005888 if ((ent == NULL) && (ctxt->userData==ctxt)) {
Daniel Veillard1af9a412003-08-20 22:54:39 +00005889 ent = xmlSAX2GetEntity(ctxt, name);
Daniel Veillard5997aca2002-03-18 18:36:20 +00005890 }
Owen Taylor3473f882001-02-23 17:55:21 +00005891 }
5892 /*
5893 * [ WFC: Entity Declared ]
5894 * In a document without any DTD, a document with only an
5895 * internal DTD subset which contains no parameter entity
5896 * references, or a document with "standalone='yes'", the
5897 * Name given in the entity reference must match that in an
5898 * entity declaration, except that well-formed documents
5899 * need not declare any of the following entities: amp, lt,
5900 * gt, apos, quot.
5901 * The declaration of a parameter entity must precede any
5902 * reference to it.
5903 * Similarly, the declaration of a general entity must
5904 * precede any reference to it which appears in a default
5905 * value in an attribute-list declaration. Note that if
5906 * entities are declared in the external subset or in
5907 * external parameter entities, a non-validating processor
5908 * is not obligated to read and process their declarations;
5909 * for such documents, the rule that an entity must be
5910 * declared is a well-formedness constraint only if
5911 * standalone='yes'.
5912 */
5913 if (ent == NULL) {
5914 if ((ctxt->standalone == 1) ||
5915 ((ctxt->hasExternalSubset == 0) &&
5916 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00005917 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00005918 "Entity '%s' not defined\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005919 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00005920 xmlErrMsgStr(ctxt, XML_WAR_UNDECLARED_ENTITY,
Daniel Veillard24eb9782003-10-04 21:08:09 +00005921 "Entity '%s' not defined\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00005922 name);
Owen Taylor3473f882001-02-23 17:55:21 +00005923 }
Daniel Veillard24eb9782003-10-04 21:08:09 +00005924 /* TODO ? check regressions ctxt->valid = 0; */
Owen Taylor3473f882001-02-23 17:55:21 +00005925 }
5926
5927 /*
5928 * [ WFC: Parsed Entity ]
5929 * An entity reference must not contain the name of an
5930 * unparsed entity
5931 */
5932 else if (ent->etype == XML_EXTERNAL_GENERAL_UNPARSED_ENTITY) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005933 xmlFatalErrMsgStr(ctxt, XML_ERR_UNPARSED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00005934 "Entity reference to unparsed entity %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005935 }
5936
5937 /*
5938 * [ WFC: No External Entity References ]
5939 * Attribute values cannot contain direct or indirect
5940 * entity references to external entities.
5941 */
5942 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
5943 (ent->etype == XML_EXTERNAL_GENERAL_PARSED_ENTITY)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005944 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_EXTERNAL,
Owen Taylor3473f882001-02-23 17:55:21 +00005945 "Attribute references external entity '%s'\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00005946 }
5947 /*
5948 * [ WFC: No < in Attribute Values ]
5949 * The replacement text of any entity referred to directly or
5950 * indirectly in an attribute value (other than "&lt;") must
5951 * not contain a <.
5952 */
5953 else if ((ctxt->instate == XML_PARSER_ATTRIBUTE_VALUE) &&
5954 (ent != NULL) &&
5955 (!xmlStrEqual(ent->name, BAD_CAST "lt")) &&
5956 (ent->content != NULL) &&
5957 (xmlStrchr(ent->content, '<'))) {
Daniel Veillardf403d292003-10-05 13:51:35 +00005958 xmlFatalErrMsgStr(ctxt, XML_ERR_LT_IN_ATTRIBUTE,
5959 "'<' in entity '%s' is not allowed in attributes values\n",
5960 name);
Owen Taylor3473f882001-02-23 17:55:21 +00005961 }
5962
5963 /*
5964 * Internal check, no parameter entities here ...
5965 */
5966 else {
5967 switch (ent->etype) {
5968 case XML_INTERNAL_PARAMETER_ENTITY:
5969 case XML_EXTERNAL_PARAMETER_ENTITY:
Daniel Veillardf403d292003-10-05 13:51:35 +00005970 xmlFatalErrMsgStr(ctxt, XML_ERR_ENTITY_IS_PARAMETER,
5971 "Attempt to reference the parameter entity '%s'\n",
5972 name);
Owen Taylor3473f882001-02-23 17:55:21 +00005973 break;
5974 default:
5975 break;
5976 }
5977 }
5978
5979 /*
5980 * [ WFC: No Recursion ]
5981 * A parsed entity must not contain a recursive reference
5982 * to itself, either directly or indirectly.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00005983 * Done somewhere else
Owen Taylor3473f882001-02-23 17:55:21 +00005984 */
5985
5986 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00005987 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00005988 }
5989 xmlFree(name);
5990 }
5991 }
5992 *str = ptr;
5993 return(ent);
5994}
5995
5996/**
5997 * xmlParsePEReference:
5998 * @ctxt: an XML parser context
5999 *
6000 * parse PEReference declarations
6001 * The entity content is handled directly by pushing it's content as
6002 * a new input stream.
6003 *
6004 * [69] PEReference ::= '%' Name ';'
6005 *
6006 * [ WFC: No Recursion ]
6007 * A parsed entity must not contain a recursive
6008 * reference to itself, either directly or indirectly.
6009 *
6010 * [ WFC: Entity Declared ]
6011 * In a document without any DTD, a document with only an internal DTD
6012 * subset which contains no parameter entity references, or a document
6013 * with "standalone='yes'", ... ... The declaration of a parameter
6014 * entity must precede any reference to it...
6015 *
6016 * [ VC: Entity Declared ]
6017 * In a document with an external subset or external parameter entities
6018 * with "standalone='no'", ... ... The declaration of a parameter entity
6019 * must precede any reference to it...
6020 *
6021 * [ WFC: In DTD ]
6022 * Parameter-entity references may only appear in the DTD.
6023 * NOTE: misleading but this is handled.
6024 */
6025void
Daniel Veillard8f597c32003-10-06 08:19:27 +00006026xmlParsePEReference(xmlParserCtxtPtr ctxt)
6027{
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006028 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006029 xmlEntityPtr entity = NULL;
6030 xmlParserInputPtr input;
6031
6032 if (RAW == '%') {
6033 NEXT;
Daniel Veillard76d66f42001-05-16 21:05:17 +00006034 name = xmlParseName(ctxt);
Daniel Veillard8f597c32003-10-06 08:19:27 +00006035 if (name == NULL) {
6036 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6037 "xmlParsePEReference: no name\n");
6038 } else {
6039 if (RAW == ';') {
6040 NEXT;
6041 if ((ctxt->sax != NULL) &&
6042 (ctxt->sax->getParameterEntity != NULL))
6043 entity = ctxt->sax->getParameterEntity(ctxt->userData,
6044 name);
6045 if (entity == NULL) {
6046 /*
6047 * [ WFC: Entity Declared ]
6048 * In a document without any DTD, a document with only an
6049 * internal DTD subset which contains no parameter entity
6050 * references, or a document with "standalone='yes'", ...
6051 * ... The declaration of a parameter entity must precede
6052 * any reference to it...
6053 */
6054 if ((ctxt->standalone == 1) ||
6055 ((ctxt->hasExternalSubset == 0) &&
6056 (ctxt->hasPErefs == 0))) {
6057 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
6058 "PEReference: %%%s; not found\n",
6059 name);
6060 } else {
6061 /*
6062 * [ VC: Entity Declared ]
6063 * In a document with an external subset or external
6064 * parameter entities with "standalone='no'", ...
6065 * ... The declaration of a parameter entity must
6066 * precede any reference to it...
6067 */
6068 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6069 "PEReference: %%%s; not found\n",
6070 name, NULL);
6071 ctxt->valid = 0;
6072 }
6073 } else {
6074 /*
6075 * Internal checking in case the entity quest barfed
6076 */
6077 if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
6078 (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
6079 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6080 "Internal: %%%s; is not a parameter entity\n",
6081 name, NULL);
6082 } else if (ctxt->input->free != deallocblankswrapper) {
6083 input =
6084 xmlNewBlanksWrapperInputStream(ctxt, entity);
6085 xmlPushInput(ctxt, input);
6086 } else {
6087 /*
6088 * TODO !!!
6089 * handle the extra spaces added before and after
6090 * c.f. http://www.w3.org/TR/REC-xml#as-PE
6091 */
6092 input = xmlNewEntityInputStream(ctxt, entity);
6093 xmlPushInput(ctxt, input);
6094 if ((entity->etype == XML_EXTERNAL_PARAMETER_ENTITY) &&
Daniel Veillarda07050d2003-10-19 14:46:32 +00006095 (CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) &&
William M. Brack76e95df2003-10-18 16:20:14 +00006096 (IS_BLANK_CH(NXT(5)))) {
Daniel Veillard8f597c32003-10-06 08:19:27 +00006097 xmlParseTextDecl(ctxt);
6098 if (ctxt->errNo ==
6099 XML_ERR_UNSUPPORTED_ENCODING) {
6100 /*
6101 * The XML REC instructs us to stop parsing
6102 * right here
6103 */
6104 ctxt->instate = XML_PARSER_EOF;
6105 return;
6106 }
6107 }
6108 }
6109 }
6110 ctxt->hasPErefs = 1;
6111 } else {
6112 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
6113 }
6114 }
Owen Taylor3473f882001-02-23 17:55:21 +00006115 }
6116}
6117
6118/**
6119 * xmlParseStringPEReference:
6120 * @ctxt: an XML parser context
6121 * @str: a pointer to an index in the string
6122 *
6123 * parse PEReference declarations
6124 *
6125 * [69] PEReference ::= '%' Name ';'
6126 *
6127 * [ WFC: No Recursion ]
6128 * A parsed entity must not contain a recursive
6129 * reference to itself, either directly or indirectly.
6130 *
6131 * [ WFC: Entity Declared ]
6132 * In a document without any DTD, a document with only an internal DTD
6133 * subset which contains no parameter entity references, or a document
6134 * with "standalone='yes'", ... ... The declaration of a parameter
6135 * entity must precede any reference to it...
6136 *
6137 * [ VC: Entity Declared ]
6138 * In a document with an external subset or external parameter entities
6139 * with "standalone='no'", ... ... The declaration of a parameter entity
6140 * must precede any reference to it...
6141 *
6142 * [ WFC: In DTD ]
6143 * Parameter-entity references may only appear in the DTD.
6144 * NOTE: misleading but this is handled.
6145 *
6146 * Returns the string of the entity content.
6147 * str is updated to the current value of the index
6148 */
6149xmlEntityPtr
6150xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) {
6151 const xmlChar *ptr;
6152 xmlChar cur;
6153 xmlChar *name;
6154 xmlEntityPtr entity = NULL;
6155
6156 if ((str == NULL) || (*str == NULL)) return(NULL);
6157 ptr = *str;
6158 cur = *ptr;
6159 if (cur == '%') {
6160 ptr++;
6161 cur = *ptr;
6162 name = xmlParseStringName(ctxt, &ptr);
6163 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006164 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6165 "xmlParseStringPEReference: no name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006166 } else {
6167 cur = *ptr;
6168 if (cur == ';') {
6169 ptr++;
6170 cur = *ptr;
6171 if ((ctxt->sax != NULL) &&
6172 (ctxt->sax->getParameterEntity != NULL))
6173 entity = ctxt->sax->getParameterEntity(ctxt->userData,
6174 name);
6175 if (entity == NULL) {
6176 /*
6177 * [ WFC: Entity Declared ]
6178 * In a document without any DTD, a document with only an
6179 * internal DTD subset which contains no parameter entity
6180 * references, or a document with "standalone='yes'", ...
6181 * ... The declaration of a parameter entity must precede
6182 * any reference to it...
6183 */
6184 if ((ctxt->standalone == 1) ||
6185 ((ctxt->hasExternalSubset == 0) &&
6186 (ctxt->hasPErefs == 0))) {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006187 xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
Owen Taylor3473f882001-02-23 17:55:21 +00006188 "PEReference: %%%s; not found\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006189 } else {
6190 /*
6191 * [ VC: Entity Declared ]
6192 * In a document with an external subset or external
6193 * parameter entities with "standalone='no'", ...
6194 * ... The declaration of a parameter entity must
6195 * precede any reference to it...
6196 */
Daniel Veillard24eb9782003-10-04 21:08:09 +00006197 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6198 "PEReference: %%%s; not found\n",
6199 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006200 ctxt->valid = 0;
6201 }
6202 } else {
6203 /*
6204 * Internal checking in case the entity quest barfed
6205 */
6206 if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
6207 (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00006208 xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
6209 "%%%s; is not a parameter entity\n",
6210 name, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006211 }
6212 }
6213 ctxt->hasPErefs = 1;
6214 } else {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006215 xmlFatalErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006216 }
6217 xmlFree(name);
6218 }
6219 }
6220 *str = ptr;
6221 return(entity);
6222}
6223
6224/**
6225 * xmlParseDocTypeDecl:
6226 * @ctxt: an XML parser context
6227 *
6228 * parse a DOCTYPE declaration
6229 *
6230 * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
6231 * ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
6232 *
6233 * [ VC: Root Element Type ]
6234 * The Name in the document type declaration must match the element
6235 * type of the root element.
6236 */
6237
6238void
6239xmlParseDocTypeDecl(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006240 const xmlChar *name = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00006241 xmlChar *ExternalID = NULL;
6242 xmlChar *URI = NULL;
6243
6244 /*
6245 * We know that '<!DOCTYPE' has been detected.
6246 */
6247 SKIP(9);
6248
6249 SKIP_BLANKS;
6250
6251 /*
6252 * Parse the DOCTYPE name.
6253 */
6254 name = xmlParseName(ctxt);
6255 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006256 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
6257 "xmlParseDocTypeDecl : no DOCTYPE name !\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006258 }
6259 ctxt->intSubName = name;
6260
6261 SKIP_BLANKS;
6262
6263 /*
6264 * Check for SystemID and ExternalID
6265 */
6266 URI = xmlParseExternalID(ctxt, &ExternalID, 1);
6267
6268 if ((URI != NULL) || (ExternalID != NULL)) {
6269 ctxt->hasExternalSubset = 1;
6270 }
6271 ctxt->extSubURI = URI;
6272 ctxt->extSubSystem = ExternalID;
6273
6274 SKIP_BLANKS;
6275
6276 /*
6277 * Create and update the internal subset.
6278 */
6279 if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
6280 (!ctxt->disableSAX))
6281 ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
6282
6283 /*
6284 * Is there any internal subset declarations ?
6285 * they are handled separately in xmlParseInternalSubset()
6286 */
6287 if (RAW == '[')
6288 return;
6289
6290 /*
6291 * We should be at the end of the DOCTYPE declaration.
6292 */
6293 if (RAW != '>') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006294 xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006295 }
6296 NEXT;
6297}
6298
6299/**
Daniel Veillardcbaf3992001-12-31 16:16:02 +00006300 * xmlParseInternalSubset:
Owen Taylor3473f882001-02-23 17:55:21 +00006301 * @ctxt: an XML parser context
6302 *
6303 * parse the internal subset declaration
6304 *
6305 * [28 end] ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
6306 */
6307
Daniel Veillard56a4cb82001-03-24 17:00:36 +00006308static void
Owen Taylor3473f882001-02-23 17:55:21 +00006309xmlParseInternalSubset(xmlParserCtxtPtr ctxt) {
6310 /*
6311 * Is there any DTD definition ?
6312 */
6313 if (RAW == '[') {
6314 ctxt->instate = XML_PARSER_DTD;
6315 NEXT;
6316 /*
6317 * Parse the succession of Markup declarations and
6318 * PEReferences.
6319 * Subsequence (markupdecl | PEReference | S)*
6320 */
6321 while (RAW != ']') {
6322 const xmlChar *check = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00006323 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00006324
6325 SKIP_BLANKS;
6326 xmlParseMarkupDecl(ctxt);
6327 xmlParsePEReference(ctxt);
6328
6329 /*
6330 * Pop-up of finished entities.
6331 */
6332 while ((RAW == 0) && (ctxt->inputNr > 1))
6333 xmlPopInput(ctxt);
6334
6335 if ((CUR_PTR == check) && (cons == ctxt->input->consumed)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006336 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
Owen Taylor3473f882001-02-23 17:55:21 +00006337 "xmlParseInternalSubset: error detected in Markup declaration\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006338 break;
6339 }
6340 }
6341 if (RAW == ']') {
6342 NEXT;
6343 SKIP_BLANKS;
6344 }
6345 }
6346
6347 /*
6348 * We should be at the end of the DOCTYPE declaration.
6349 */
6350 if (RAW != '>') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006351 xmlFatalErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006352 }
6353 NEXT;
6354}
6355
Daniel Veillard81273902003-09-30 00:43:48 +00006356#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00006357/**
6358 * xmlParseAttribute:
6359 * @ctxt: an XML parser context
6360 * @value: a xmlChar ** used to store the value of the attribute
6361 *
6362 * parse an attribute
6363 *
6364 * [41] Attribute ::= Name Eq AttValue
6365 *
6366 * [ WFC: No External Entity References ]
6367 * Attribute values cannot contain direct or indirect entity references
6368 * to external entities.
6369 *
6370 * [ WFC: No < in Attribute Values ]
6371 * The replacement text of any entity referred to directly or indirectly in
6372 * an attribute value (other than "&lt;") must not contain a <.
6373 *
6374 * [ VC: Attribute Value Type ]
6375 * The attribute must have been declared; the value must be of the type
6376 * declared for it.
6377 *
6378 * [25] Eq ::= S? '=' S?
6379 *
6380 * With namespace:
6381 *
6382 * [NS 11] Attribute ::= QName Eq AttValue
6383 *
6384 * Also the case QName == xmlns:??? is handled independently as a namespace
6385 * definition.
6386 *
6387 * Returns the attribute name, and the value in *value.
6388 */
6389
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006390const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006391xmlParseAttribute(xmlParserCtxtPtr ctxt, xmlChar **value) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006392 const xmlChar *name;
6393 xmlChar *val;
Owen Taylor3473f882001-02-23 17:55:21 +00006394
6395 *value = NULL;
Daniel Veillard878eab02002-02-19 13:46:09 +00006396 GROW;
Owen Taylor3473f882001-02-23 17:55:21 +00006397 name = xmlParseName(ctxt);
6398 if (name == NULL) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006399 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006400 "error parsing attribute name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006401 return(NULL);
6402 }
6403
6404 /*
6405 * read the value
6406 */
6407 SKIP_BLANKS;
6408 if (RAW == '=') {
6409 NEXT;
6410 SKIP_BLANKS;
6411 val = xmlParseAttValue(ctxt);
6412 ctxt->instate = XML_PARSER_CONTENT;
6413 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006414 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Owen Taylor3473f882001-02-23 17:55:21 +00006415 "Specification mandate value for attribute %s\n", name);
Owen Taylor3473f882001-02-23 17:55:21 +00006416 return(NULL);
6417 }
6418
6419 /*
6420 * Check that xml:lang conforms to the specification
6421 * No more registered as an error, just generate a warning now
6422 * since this was deprecated in XML second edition
6423 */
6424 if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) {
6425 if (!xmlCheckLanguageID(val)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00006426 xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
6427 "Malformed value for xml:lang : %s\n",
6428 val, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006429 }
6430 }
6431
6432 /*
6433 * Check that xml:space conforms to the specification
6434 */
6435 if (xmlStrEqual(name, BAD_CAST "xml:space")) {
6436 if (xmlStrEqual(val, BAD_CAST "default"))
6437 *(ctxt->space) = 0;
6438 else if (xmlStrEqual(val, BAD_CAST "preserve"))
6439 *(ctxt->space) = 1;
6440 else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00006441 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Daniel Veillard642104e2003-03-26 16:32:05 +00006442"Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
Owen Taylor3473f882001-02-23 17:55:21 +00006443 val);
Owen Taylor3473f882001-02-23 17:55:21 +00006444 }
6445 }
6446
6447 *value = val;
6448 return(name);
6449}
6450
6451/**
6452 * xmlParseStartTag:
6453 * @ctxt: an XML parser context
6454 *
6455 * parse a start of tag either for rule element or
6456 * EmptyElement. In both case we don't parse the tag closing chars.
6457 *
6458 * [40] STag ::= '<' Name (S Attribute)* S? '>'
6459 *
6460 * [ WFC: Unique Att Spec ]
6461 * No attribute name may appear more than once in the same start-tag or
6462 * empty-element tag.
6463 *
6464 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
6465 *
6466 * [ WFC: Unique Att Spec ]
6467 * No attribute name may appear more than once in the same start-tag or
6468 * empty-element tag.
6469 *
6470 * With namespace:
6471 *
6472 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
6473 *
6474 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
6475 *
6476 * Returns the element name parsed
6477 */
6478
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006479const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00006480xmlParseStartTag(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006481 const xmlChar *name;
6482 const xmlChar *attname;
Owen Taylor3473f882001-02-23 17:55:21 +00006483 xmlChar *attvalue;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006484 const xmlChar **atts = ctxt->atts;
Owen Taylor3473f882001-02-23 17:55:21 +00006485 int nbatts = 0;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006486 int maxatts = ctxt->maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00006487 int i;
6488
6489 if (RAW != '<') return(NULL);
Daniel Veillard21a0f912001-02-25 19:54:14 +00006490 NEXT1;
Owen Taylor3473f882001-02-23 17:55:21 +00006491
6492 name = xmlParseName(ctxt);
6493 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006494 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
Owen Taylor3473f882001-02-23 17:55:21 +00006495 "xmlParseStartTag: invalid element name\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006496 return(NULL);
6497 }
6498
6499 /*
6500 * Now parse the attributes, it ends up with the ending
6501 *
6502 * (S Attribute)* S?
6503 */
6504 SKIP_BLANKS;
6505 GROW;
6506
Daniel Veillard21a0f912001-02-25 19:54:14 +00006507 while ((RAW != '>') &&
6508 ((RAW != '/') || (NXT(1) != '>')) &&
Daniel Veillard73b013f2003-09-30 12:36:01 +00006509 (IS_BYTE_CHAR(RAW))) {
Owen Taylor3473f882001-02-23 17:55:21 +00006510 const xmlChar *q = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00006511 unsigned int cons = ctxt->input->consumed;
Owen Taylor3473f882001-02-23 17:55:21 +00006512
6513 attname = xmlParseAttribute(ctxt, &attvalue);
6514 if ((attname != NULL) && (attvalue != NULL)) {
6515 /*
6516 * [ WFC: Unique Att Spec ]
6517 * No attribute name may appear more than once in the same
6518 * start-tag or empty-element tag.
6519 */
6520 for (i = 0; i < nbatts;i += 2) {
6521 if (xmlStrEqual(atts[i], attname)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006522 xmlErrAttributeDup(ctxt, NULL, attname);
Owen Taylor3473f882001-02-23 17:55:21 +00006523 xmlFree(attvalue);
6524 goto failed;
6525 }
6526 }
Owen Taylor3473f882001-02-23 17:55:21 +00006527 /*
6528 * Add the pair to atts
6529 */
6530 if (atts == NULL) {
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006531 maxatts = 22; /* allow for 10 attrs by default */
6532 atts = (const xmlChar **)
6533 xmlMalloc(maxatts * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00006534 if (atts == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006535 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006536 if (attvalue != NULL)
6537 xmlFree(attvalue);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006538 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00006539 }
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006540 ctxt->atts = atts;
6541 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00006542 } else if (nbatts + 4 > maxatts) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006543 const xmlChar **n;
6544
Owen Taylor3473f882001-02-23 17:55:21 +00006545 maxatts *= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006546 n = (const xmlChar **) xmlRealloc((void *) atts,
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006547 maxatts * sizeof(const xmlChar *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006548 if (n == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006549 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006550 if (attvalue != NULL)
6551 xmlFree(attvalue);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006552 goto failed;
Owen Taylor3473f882001-02-23 17:55:21 +00006553 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006554 atts = n;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006555 ctxt->atts = atts;
6556 ctxt->maxatts = maxatts;
Owen Taylor3473f882001-02-23 17:55:21 +00006557 }
6558 atts[nbatts++] = attname;
6559 atts[nbatts++] = attvalue;
6560 atts[nbatts] = NULL;
6561 atts[nbatts + 1] = NULL;
6562 } else {
Owen Taylor3473f882001-02-23 17:55:21 +00006563 if (attvalue != NULL)
6564 xmlFree(attvalue);
6565 }
6566
6567failed:
6568
Daniel Veillard3772de32002-12-17 10:31:45 +00006569 GROW
Owen Taylor3473f882001-02-23 17:55:21 +00006570 if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
6571 break;
William M. Brack76e95df2003-10-18 16:20:14 +00006572 if (!IS_BLANK_CH(RAW)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006573 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
6574 "attributes construct error\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006575 }
6576 SKIP_BLANKS;
Daniel Veillard02111c12003-02-24 19:14:52 +00006577 if ((cons == ctxt->input->consumed) && (q == CUR_PTR) &&
6578 (attname == NULL) && (attvalue == NULL)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006579 xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
6580 "xmlParseStartTag: problem parsing attributes\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006581 break;
6582 }
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006583 SHRINK;
Owen Taylor3473f882001-02-23 17:55:21 +00006584 GROW;
6585 }
6586
6587 /*
6588 * SAX: Start of Element !
6589 */
6590 if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL) &&
Daniel Veillard6155d8a2003-08-19 15:01:28 +00006591 (!ctxt->disableSAX)) {
6592 if (nbatts > 0)
6593 ctxt->sax->startElement(ctxt->userData, name, atts);
6594 else
6595 ctxt->sax->startElement(ctxt->userData, name, NULL);
6596 }
Owen Taylor3473f882001-02-23 17:55:21 +00006597
6598 if (atts != NULL) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006599 /* Free only the content strings */
6600 for (i = 1;i < nbatts;i+=2)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00006601 if (atts[i] != NULL)
6602 xmlFree((xmlChar *) atts[i]);
Owen Taylor3473f882001-02-23 17:55:21 +00006603 }
6604 return(name);
6605}
6606
6607/**
Daniel Veillard0fb18932003-09-07 09:14:37 +00006608 * xmlParseEndTag1:
Owen Taylor3473f882001-02-23 17:55:21 +00006609 * @ctxt: an XML parser context
Daniel Veillard0fb18932003-09-07 09:14:37 +00006610 * @line: line of the start tag
6611 * @nsNr: number of namespaces on the start tag
Owen Taylor3473f882001-02-23 17:55:21 +00006612 *
6613 * parse an end of tag
6614 *
6615 * [42] ETag ::= '</' Name S? '>'
6616 *
6617 * With namespace
6618 *
6619 * [NS 9] ETag ::= '</' QName S? '>'
6620 */
6621
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00006622static void
Daniel Veillard0fb18932003-09-07 09:14:37 +00006623xmlParseEndTag1(xmlParserCtxtPtr ctxt, int line) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00006624 const xmlChar *name;
Owen Taylor3473f882001-02-23 17:55:21 +00006625
6626 GROW;
6627 if ((RAW != '<') || (NXT(1) != '/')) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006628 xmlFatalErrMsg(ctxt, XML_ERR_LTSLASH_REQUIRED,
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006629 "xmlParseEndTag: '</' not found\n");
Owen Taylor3473f882001-02-23 17:55:21 +00006630 return;
6631 }
6632 SKIP(2);
6633
Daniel Veillard46de64e2002-05-29 08:21:33 +00006634 name = xmlParseNameAndCompare(ctxt,ctxt->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006635
6636 /*
6637 * We should definitely be at the ending "S? '>'" part
6638 */
6639 GROW;
6640 SKIP_BLANKS;
Daniel Veillard73b013f2003-09-30 12:36:01 +00006641 if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006642 xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00006643 } else
Daniel Veillard21a0f912001-02-25 19:54:14 +00006644 NEXT1;
Owen Taylor3473f882001-02-23 17:55:21 +00006645
6646 /*
6647 * [ WFC: Element Type Match ]
6648 * The Name in an element's end-tag must match the element type in the
6649 * start-tag.
6650 *
6651 */
Daniel Veillard46de64e2002-05-29 08:21:33 +00006652 if (name != (xmlChar*)1) {
Daniel Veillardf403d292003-10-05 13:51:35 +00006653 if (name == NULL) name = BAD_CAST "unparseable";
6654 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00006655 "Opening and ending tag mismatch: %s line %d and %s\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00006656 ctxt->name, line, name);
Owen Taylor3473f882001-02-23 17:55:21 +00006657 }
6658
6659 /*
6660 * SAX: End of Tag
6661 */
6662 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
6663 (!ctxt->disableSAX))
Daniel Veillard46de64e2002-05-29 08:21:33 +00006664 ctxt->sax->endElement(ctxt->userData, ctxt->name);
Owen Taylor3473f882001-02-23 17:55:21 +00006665
Daniel Veillarde57ec792003-09-10 10:50:59 +00006666 namePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006667 spacePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00006668 return;
6669}
6670
6671/**
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00006672 * xmlParseEndTag:
6673 * @ctxt: an XML parser context
6674 *
6675 * parse an end of tag
6676 *
6677 * [42] ETag ::= '</' Name S? '>'
6678 *
6679 * With namespace
6680 *
6681 * [NS 9] ETag ::= '</' QName S? '>'
6682 */
6683
6684void
6685xmlParseEndTag(xmlParserCtxtPtr ctxt) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00006686 xmlParseEndTag1(ctxt, 0);
6687}
Daniel Veillard81273902003-09-30 00:43:48 +00006688#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard0fb18932003-09-07 09:14:37 +00006689
6690/************************************************************************
6691 * *
6692 * SAX 2 specific operations *
6693 * *
6694 ************************************************************************/
6695
6696static const xmlChar *
6697xmlParseNCNameComplex(xmlParserCtxtPtr ctxt) {
6698 int len = 0, l;
6699 int c;
6700 int count = 0;
6701
6702 /*
6703 * Handler for more complex cases
6704 */
6705 GROW;
6706 c = CUR_CHAR(l);
6707 if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006708 (!IS_LETTER(c) && (c != '_'))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00006709 return(NULL);
6710 }
6711
6712 while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
William M. Brack871611b2003-10-18 04:53:14 +00006713 ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006714 (c == '.') || (c == '-') || (c == '_') ||
William M. Brack871611b2003-10-18 04:53:14 +00006715 (IS_COMBINING(c)) ||
6716 (IS_EXTENDER(c)))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00006717 if (count++ > 100) {
6718 count = 0;
6719 GROW;
6720 }
6721 len += l;
6722 NEXTL(l);
6723 c = CUR_CHAR(l);
6724 }
6725 return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len));
6726}
6727
6728/*
6729 * xmlGetNamespace:
6730 * @ctxt: an XML parser context
6731 * @prefix: the prefix to lookup
6732 *
6733 * Lookup the namespace name for the @prefix (which ca be NULL)
6734 * The prefix must come from the @ctxt->dict dictionnary
6735 *
6736 * Returns the namespace name or NULL if not bound
6737 */
6738static const xmlChar *
6739xmlGetNamespace(xmlParserCtxtPtr ctxt, const xmlChar *prefix) {
6740 int i;
6741
Daniel Veillarde57ec792003-09-10 10:50:59 +00006742 if (prefix == ctxt->str_xml) return(ctxt->str_xml_ns);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006743 for (i = ctxt->nsNr - 2;i >= 0;i-=2)
Daniel Veillarde57ec792003-09-10 10:50:59 +00006744 if (ctxt->nsTab[i] == prefix) {
6745 if ((prefix == NULL) && (*ctxt->nsTab[i + 1] == 0))
6746 return(NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006747 return(ctxt->nsTab[i + 1]);
Daniel Veillarde57ec792003-09-10 10:50:59 +00006748 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00006749 return(NULL);
6750}
6751
6752/**
6753 * xmlParseNCName:
6754 * @ctxt: an XML parser context
Daniel Veillardc82c57e2004-01-12 16:24:34 +00006755 * @len: lenght of the string parsed
Daniel Veillard0fb18932003-09-07 09:14:37 +00006756 *
6757 * parse an XML name.
6758 *
6759 * [4NS] NCNameChar ::= Letter | Digit | '.' | '-' | '_' |
6760 * CombiningChar | Extender
6761 *
6762 * [5NS] NCName ::= (Letter | '_') (NCNameChar)*
6763 *
6764 * Returns the Name parsed or NULL
6765 */
6766
6767static const xmlChar *
6768xmlParseNCName(xmlParserCtxtPtr ctxt) {
6769 const xmlChar *in;
6770 const xmlChar *ret;
6771 int count = 0;
6772
6773 /*
6774 * Accelerator for simple ASCII names
6775 */
6776 in = ctxt->input->cur;
6777 if (((*in >= 0x61) && (*in <= 0x7A)) ||
6778 ((*in >= 0x41) && (*in <= 0x5A)) ||
6779 (*in == '_')) {
6780 in++;
6781 while (((*in >= 0x61) && (*in <= 0x7A)) ||
6782 ((*in >= 0x41) && (*in <= 0x5A)) ||
6783 ((*in >= 0x30) && (*in <= 0x39)) ||
6784 (*in == '_') || (*in == '-') ||
6785 (*in == '.'))
6786 in++;
6787 if ((*in > 0) && (*in < 0x80)) {
6788 count = in - ctxt->input->cur;
6789 ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
6790 ctxt->input->cur = in;
6791 ctxt->nbChars += count;
6792 ctxt->input->col += count;
6793 if (ret == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00006794 xmlErrMemory(ctxt, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006795 }
6796 return(ret);
6797 }
6798 }
6799 return(xmlParseNCNameComplex(ctxt));
6800}
6801
6802/**
6803 * xmlParseQName:
6804 * @ctxt: an XML parser context
6805 * @prefix: pointer to store the prefix part
6806 *
6807 * parse an XML Namespace QName
6808 *
6809 * [6] QName ::= (Prefix ':')? LocalPart
6810 * [7] Prefix ::= NCName
6811 * [8] LocalPart ::= NCName
6812 *
6813 * Returns the Name parsed or NULL
6814 */
6815
6816static const xmlChar *
6817xmlParseQName(xmlParserCtxtPtr ctxt, const xmlChar **prefix) {
6818 const xmlChar *l, *p;
6819
6820 GROW;
6821
6822 l = xmlParseNCName(ctxt);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006823 if (l == NULL) {
6824 if (CUR == ':') {
6825 l = xmlParseName(ctxt);
6826 if (l != NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006827 xmlNsErr(ctxt, XML_NS_ERR_QNAME,
6828 "Failed to parse QName '%s'\n", l, NULL, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006829 *prefix = NULL;
6830 return(l);
6831 }
6832 }
6833 return(NULL);
6834 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00006835 if (CUR == ':') {
6836 NEXT;
6837 p = l;
6838 l = xmlParseNCName(ctxt);
6839 if (l == NULL) {
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006840 xmlChar *tmp;
6841
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006842 xmlNsErr(ctxt, XML_NS_ERR_QNAME,
6843 "Failed to parse QName '%s:'\n", p, NULL, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006844 tmp = xmlBuildQName(BAD_CAST "", p, NULL, 0);
6845 p = xmlDictLookup(ctxt->dict, tmp, -1);
6846 if (tmp != NULL) xmlFree(tmp);
6847 *prefix = NULL;
6848 return(p);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006849 }
6850 if (CUR == ':') {
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006851 xmlChar *tmp;
6852
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006853 xmlNsErr(ctxt, XML_NS_ERR_QNAME,
6854 "Failed to parse QName '%s:%s:'\n", p, l, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00006855 NEXT;
6856 tmp = (xmlChar *) xmlParseName(ctxt);
6857 if (tmp != NULL) {
6858 tmp = xmlBuildQName(tmp, l, NULL, 0);
6859 l = xmlDictLookup(ctxt->dict, tmp, -1);
6860 if (tmp != NULL) xmlFree(tmp);
6861 *prefix = p;
6862 return(l);
6863 }
6864 tmp = xmlBuildQName(BAD_CAST "", l, NULL, 0);
6865 l = xmlDictLookup(ctxt->dict, tmp, -1);
6866 if (tmp != NULL) xmlFree(tmp);
6867 *prefix = p;
6868 return(l);
Daniel Veillard0fb18932003-09-07 09:14:37 +00006869 }
6870 *prefix = p;
6871 } else
6872 *prefix = NULL;
6873 return(l);
6874}
6875
6876/**
6877 * xmlParseQNameAndCompare:
6878 * @ctxt: an XML parser context
6879 * @name: the localname
6880 * @prefix: the prefix, if any.
6881 *
6882 * parse an XML name and compares for match
6883 * (specialized for endtag parsing)
6884 *
6885 * Returns NULL for an illegal name, (xmlChar*) 1 for success
6886 * and the name for mismatch
6887 */
6888
6889static const xmlChar *
6890xmlParseQNameAndCompare(xmlParserCtxtPtr ctxt, xmlChar const *name,
6891 xmlChar const *prefix) {
6892 const xmlChar *cmp = name;
6893 const xmlChar *in;
6894 const xmlChar *ret;
6895 const xmlChar *prefix2;
6896
6897 if (prefix == NULL) return(xmlParseNameAndCompare(ctxt, name));
6898
6899 GROW;
6900 in = ctxt->input->cur;
6901
6902 cmp = prefix;
6903 while (*in != 0 && *in == *cmp) {
6904 ++in;
6905 ++cmp;
6906 }
6907 if ((*cmp == 0) && (*in == ':')) {
6908 in++;
6909 cmp = name;
6910 while (*in != 0 && *in == *cmp) {
6911 ++in;
6912 ++cmp;
6913 }
William M. Brack76e95df2003-10-18 16:20:14 +00006914 if (*cmp == 0 && (*in == '>' || IS_BLANK_CH (*in))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00006915 /* success */
6916 ctxt->input->cur = in;
6917 return((const xmlChar*) 1);
6918 }
6919 }
6920 /*
6921 * all strings coms from the dictionary, equality can be done directly
6922 */
6923 ret = xmlParseQName (ctxt, &prefix2);
6924 if ((ret == name) && (prefix == prefix2))
6925 return((const xmlChar*) 1);
6926 return ret;
6927}
6928
6929/**
6930 * xmlParseAttValueInternal:
6931 * @ctxt: an XML parser context
6932 * @len: attribute len result
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00006933 * @alloc: whether the attribute was reallocated as a new string
6934 * @normalize: if 1 then further non-CDATA normalization must be done
Daniel Veillard0fb18932003-09-07 09:14:37 +00006935 *
6936 * parse a value for an attribute.
6937 * NOTE: if no normalization is needed, the routine will return pointers
6938 * directly from the data buffer.
6939 *
6940 * 3.3.3 Attribute-Value Normalization:
6941 * Before the value of an attribute is passed to the application or
6942 * checked for validity, the XML processor must normalize it as follows:
6943 * - a character reference is processed by appending the referenced
6944 * character to the attribute value
6945 * - an entity reference is processed by recursively processing the
6946 * replacement text of the entity
6947 * - a whitespace character (#x20, #xD, #xA, #x9) is processed by
6948 * appending #x20 to the normalized value, except that only a single
6949 * #x20 is appended for a "#xD#xA" sequence that is part of an external
6950 * parsed entity or the literal entity value of an internal parsed entity
6951 * - other characters are processed by appending them to the normalized value
6952 * If the declared value is not CDATA, then the XML processor must further
6953 * process the normalized attribute value by discarding any leading and
6954 * trailing space (#x20) characters, and by replacing sequences of space
6955 * (#x20) characters by a single space (#x20) character.
6956 * All attributes for which no declaration has been read should be treated
6957 * by a non-validating parser as if declared CDATA.
6958 *
6959 * Returns the AttValue parsed or NULL. The value has to be freed by the
6960 * caller if it was copied, this can be detected by val[*len] == 0.
6961 */
6962
6963static xmlChar *
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00006964xmlParseAttValueInternal(xmlParserCtxtPtr ctxt, int *len, int *alloc,
6965 int normalize)
Daniel Veillarde57ec792003-09-10 10:50:59 +00006966{
Daniel Veillard0fb18932003-09-07 09:14:37 +00006967 xmlChar limit = 0;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00006968 const xmlChar *in = NULL, *start, *end, *last;
Daniel Veillard0fb18932003-09-07 09:14:37 +00006969 xmlChar *ret = NULL;
6970
6971 GROW;
6972 in = (xmlChar *) CUR_PTR;
6973 if (*in != '"' && *in != '\'') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00006974 xmlFatalErr(ctxt, XML_ERR_ATTRIBUTE_NOT_STARTED, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00006975 return (NULL);
6976 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00006977 ctxt->instate = XML_PARSER_ATTRIBUTE_VALUE;
Daniel Veillarde57ec792003-09-10 10:50:59 +00006978
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00006979 /*
6980 * try to handle in this routine the most common case where no
6981 * allocation of a new string is required and where content is
6982 * pure ASCII.
6983 */
6984 limit = *in++;
6985 end = ctxt->input->end;
6986 start = in;
6987 if (in >= end) {
6988 const xmlChar *oldbase = ctxt->input->base;
6989 GROW;
6990 if (oldbase != ctxt->input->base) {
6991 long delta = ctxt->input->base - oldbase;
6992 start = start + delta;
6993 in = in + delta;
6994 }
6995 end = ctxt->input->end;
Daniel Veillard0fb18932003-09-07 09:14:37 +00006996 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00006997 if (normalize) {
6998 /*
6999 * Skip any leading spaces
7000 */
7001 while ((in < end) && (*in != limit) &&
7002 ((*in == 0x20) || (*in == 0x9) ||
7003 (*in == 0xA) || (*in == 0xD))) {
7004 in++;
7005 start = in;
7006 if (in >= end) {
7007 const xmlChar *oldbase = ctxt->input->base;
7008 GROW;
7009 if (oldbase != ctxt->input->base) {
7010 long delta = ctxt->input->base - oldbase;
7011 start = start + delta;
7012 in = in + delta;
7013 }
7014 end = ctxt->input->end;
7015 }
7016 }
7017 while ((in < end) && (*in != limit) && (*in >= 0x20) &&
7018 (*in <= 0x7f) && (*in != '&') && (*in != '<')) {
7019 if ((*in++ == 0x20) && (*in == 0x20)) break;
7020 if (in >= end) {
7021 const xmlChar *oldbase = ctxt->input->base;
7022 GROW;
7023 if (oldbase != ctxt->input->base) {
7024 long delta = ctxt->input->base - oldbase;
7025 start = start + delta;
7026 in = in + delta;
7027 }
7028 end = ctxt->input->end;
7029 }
7030 }
7031 last = in;
7032 /*
7033 * skip the trailing blanks
7034 */
Daniel Veillardc6e20e42003-09-11 16:30:26 +00007035 while ((last[-1] == 0x20) && (last > start)) last--;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007036 while ((in < end) && (*in != limit) &&
7037 ((*in == 0x20) || (*in == 0x9) ||
7038 (*in == 0xA) || (*in == 0xD))) {
7039 in++;
7040 if (in >= end) {
7041 const xmlChar *oldbase = ctxt->input->base;
7042 GROW;
7043 if (oldbase != ctxt->input->base) {
7044 long delta = ctxt->input->base - oldbase;
7045 start = start + delta;
7046 in = in + delta;
7047 last = last + delta;
7048 }
7049 end = ctxt->input->end;
7050 }
7051 }
7052 if (*in != limit) goto need_complex;
7053 } else {
7054 while ((in < end) && (*in != limit) && (*in >= 0x20) &&
7055 (*in <= 0x7f) && (*in != '&') && (*in != '<')) {
7056 in++;
7057 if (in >= end) {
7058 const xmlChar *oldbase = ctxt->input->base;
7059 GROW;
7060 if (oldbase != ctxt->input->base) {
7061 long delta = ctxt->input->base - oldbase;
7062 start = start + delta;
7063 in = in + delta;
7064 }
7065 end = ctxt->input->end;
7066 }
7067 }
7068 last = in;
7069 if (*in != limit) goto need_complex;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007070 }
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007071 in++;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007072 if (len != NULL) {
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007073 *len = last - start;
7074 ret = (xmlChar *) start;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007075 } else {
Daniel Veillarde57ec792003-09-10 10:50:59 +00007076 if (alloc) *alloc = 1;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007077 ret = xmlStrndup(start, last - start);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007078 }
7079 CUR_PTR = in;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007080 if (alloc) *alloc = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007081 return ret;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007082need_complex:
7083 if (alloc) *alloc = 1;
7084 return xmlParseAttValueComplex(ctxt, len, normalize);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007085}
7086
7087/**
7088 * xmlParseAttribute2:
7089 * @ctxt: an XML parser context
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007090 * @pref: the element prefix
7091 * @elem: the element name
7092 * @prefix: a xmlChar ** used to store the value of the attribute prefix
Daniel Veillard0fb18932003-09-07 09:14:37 +00007093 * @value: a xmlChar ** used to store the value of the attribute
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007094 * @len: an int * to save the length of the attribute
7095 * @alloc: an int * to indicate if the attribute was allocated
Daniel Veillard0fb18932003-09-07 09:14:37 +00007096 *
7097 * parse an attribute in the new SAX2 framework.
7098 *
7099 * Returns the attribute name, and the value in *value, .
7100 */
7101
7102static const xmlChar *
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007103xmlParseAttribute2(xmlParserCtxtPtr ctxt,
7104 const xmlChar *pref, const xmlChar *elem,
7105 const xmlChar **prefix, xmlChar **value,
7106 int *len, int *alloc) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007107 const xmlChar *name;
7108 xmlChar *val;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007109 int normalize = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007110
7111 *value = NULL;
7112 GROW;
7113 name = xmlParseQName(ctxt, prefix);
7114 if (name == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007115 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7116 "error parsing attribute name\n");
Daniel Veillard0fb18932003-09-07 09:14:37 +00007117 return(NULL);
7118 }
7119
7120 /*
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007121 * get the type if needed
7122 */
7123 if (ctxt->attsSpecial != NULL) {
7124 int type;
7125
7126 type = (int) (long) xmlHashQLookup2(ctxt->attsSpecial,
7127 pref, elem, *prefix, name);
7128 if (type != 0) normalize = 1;
7129 }
7130
7131 /*
Daniel Veillard0fb18932003-09-07 09:14:37 +00007132 * read the value
7133 */
7134 SKIP_BLANKS;
7135 if (RAW == '=') {
7136 NEXT;
7137 SKIP_BLANKS;
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007138 val = xmlParseAttValueInternal(ctxt, len, alloc, normalize);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007139 ctxt->instate = XML_PARSER_CONTENT;
7140 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00007141 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007142 "Specification mandate value for attribute %s\n", name);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007143 return(NULL);
7144 }
7145
7146 /*
7147 * Check that xml:lang conforms to the specification
7148 * No more registered as an error, just generate a warning now
7149 * since this was deprecated in XML second edition
7150 */
7151 if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) {
7152 if (!xmlCheckLanguageID(val)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007153 xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
7154 "Malformed value for xml:lang : %s\n",
7155 val, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007156 }
7157 }
7158
7159 /*
7160 * Check that xml:space conforms to the specification
7161 */
7162 if (xmlStrEqual(name, BAD_CAST "xml:space")) {
7163 if (xmlStrEqual(val, BAD_CAST "default"))
7164 *(ctxt->space) = 0;
7165 else if (xmlStrEqual(val, BAD_CAST "preserve"))
7166 *(ctxt->space) = 1;
7167 else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00007168 xmlFatalErrMsgStr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007169"Invalid value \"%s\" for xml:space : \"default\" or \"preserve\" expected\n",
7170 val);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007171 }
7172 }
7173
7174 *value = val;
7175 return(name);
7176}
7177
7178/**
7179 * xmlParseStartTag2:
7180 * @ctxt: an XML parser context
7181 *
7182 * parse a start of tag either for rule element or
7183 * EmptyElement. In both case we don't parse the tag closing chars.
7184 * This routine is called when running SAX2 parsing
7185 *
7186 * [40] STag ::= '<' Name (S Attribute)* S? '>'
7187 *
7188 * [ WFC: Unique Att Spec ]
7189 * No attribute name may appear more than once in the same start-tag or
7190 * empty-element tag.
7191 *
7192 * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
7193 *
7194 * [ WFC: Unique Att Spec ]
7195 * No attribute name may appear more than once in the same start-tag or
7196 * empty-element tag.
7197 *
7198 * With namespace:
7199 *
7200 * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
7201 *
7202 * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
7203 *
7204 * Returns the element name parsed
7205 */
7206
7207static const xmlChar *
7208xmlParseStartTag2(xmlParserCtxtPtr ctxt, const xmlChar **pref,
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007209 const xmlChar **URI, int *tlen) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007210 const xmlChar *localname;
7211 const xmlChar *prefix;
7212 const xmlChar *attname;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007213 const xmlChar *aprefix;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007214 const xmlChar *nsname;
7215 xmlChar *attvalue;
7216 const xmlChar **atts = ctxt->atts;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007217 int maxatts = ctxt->maxatts;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007218 int nratts, nbatts, nbdef;
7219 int i, j, nbNs, attval;
7220 const xmlChar *base;
7221 unsigned long cur;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007222
7223 if (RAW != '<') return(NULL);
7224 NEXT1;
7225
7226 /*
7227 * NOTE: it is crucial with the SAX2 API to never call SHRINK beyond that
7228 * point since the attribute values may be stored as pointers to
7229 * the buffer and calling SHRINK would destroy them !
7230 * The Shrinking is only possible once the full set of attribute
7231 * callbacks have been done.
7232 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00007233reparse:
Daniel Veillard0fb18932003-09-07 09:14:37 +00007234 SHRINK;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007235 base = ctxt->input->base;
7236 cur = ctxt->input->cur - ctxt->input->base;
7237 nbatts = 0;
7238 nratts = 0;
7239 nbdef = 0;
7240 nbNs = 0;
7241 attval = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007242
7243 localname = xmlParseQName(ctxt, &prefix);
7244 if (localname == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007245 xmlFatalErrMsg(ctxt, XML_ERR_NAME_REQUIRED,
7246 "StartTag: invalid element name\n");
Daniel Veillard0fb18932003-09-07 09:14:37 +00007247 return(NULL);
7248 }
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007249 *tlen = ctxt->input->cur - ctxt->input->base - cur;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007250
7251 /*
7252 * Now parse the attributes, it ends up with the ending
7253 *
7254 * (S Attribute)* S?
7255 */
7256 SKIP_BLANKS;
7257 GROW;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007258 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007259
7260 while ((RAW != '>') &&
7261 ((RAW != '/') || (NXT(1) != '>')) &&
Daniel Veillard73b013f2003-09-30 12:36:01 +00007262 (IS_BYTE_CHAR(RAW))) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007263 const xmlChar *q = CUR_PTR;
7264 unsigned int cons = ctxt->input->consumed;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007265 int len = -1, alloc = 0;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007266
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00007267 attname = xmlParseAttribute2(ctxt, prefix, localname,
7268 &aprefix, &attvalue, &len, &alloc);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007269 if ((attname != NULL) && (attvalue != NULL)) {
7270 if (len < 0) len = xmlStrlen(attvalue);
7271 if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00007272 const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len);
7273 xmlURIPtr uri;
7274
7275 if (*URL != 0) {
7276 uri = xmlParseURI((const char *) URL);
7277 if (uri == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007278 xmlWarningMsg(ctxt, XML_WAR_NS_URI,
7279 "xmlns: %s not a valid URI\n",
7280 URL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007281 } else {
7282 if (uri->scheme == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007283 xmlWarningMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
7284 "xmlns: URI %s is not absolute\n",
7285 URL, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007286 }
7287 xmlFreeURI(uri);
7288 }
7289 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007290 /*
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007291 * check that it's not a defined namespace
Daniel Veillard0fb18932003-09-07 09:14:37 +00007292 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007293 for (j = 1;j <= nbNs;j++)
7294 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL)
7295 break;
7296 if (j <= nbNs)
7297 xmlErrAttributeDup(ctxt, NULL, attname);
7298 else
7299 if (nsPush(ctxt, NULL, URL) > 0) nbNs++;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007300 if (alloc != 0) xmlFree(attvalue);
7301 SKIP_BLANKS;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007302 continue;
7303 }
7304 if (aprefix == ctxt->str_xmlns) {
Daniel Veillarde57ec792003-09-10 10:50:59 +00007305 const xmlChar *URL = xmlDictLookup(ctxt->dict, attvalue, len);
7306 xmlURIPtr uri;
7307
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007308 if (attname == ctxt->str_xml) {
7309 if (URL != ctxt->str_xml_ns) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007310 xmlNsErr(ctxt, XML_NS_ERR_XML_NAMESPACE,
7311 "xml namespace prefix mapped to wrong URI\n",
7312 NULL, NULL, NULL);
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007313 }
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007314 /*
7315 * Do not keep a namespace definition node
7316 */
7317 if (alloc != 0) xmlFree(attvalue);
7318 SKIP_BLANKS;
7319 continue;
7320 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00007321 uri = xmlParseURI((const char *) URL);
7322 if (uri == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007323 xmlWarningMsg(ctxt, XML_WAR_NS_URI,
7324 "xmlns:%s: '%s' is not a valid URI\n",
7325 attname, URL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007326 } else {
Daniel Veillard3b7840c2003-09-11 23:42:01 +00007327 if ((ctxt->pedantic) && (uri->scheme == NULL)) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00007328 xmlWarningMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
7329 "xmlns:%s: URI %s is not absolute\n",
7330 attname, URL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007331 }
7332 xmlFreeURI(uri);
7333 }
7334
Daniel Veillard0fb18932003-09-07 09:14:37 +00007335 /*
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007336 * check that it's not a defined namespace
Daniel Veillard0fb18932003-09-07 09:14:37 +00007337 */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007338 for (j = 1;j <= nbNs;j++)
7339 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname)
7340 break;
7341 if (j <= nbNs)
7342 xmlErrAttributeDup(ctxt, aprefix, attname);
7343 else
7344 if (nsPush(ctxt, attname, URL) > 0) nbNs++;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007345 if (alloc != 0) xmlFree(attvalue);
7346 SKIP_BLANKS;
Daniel Veillardd3999c72004-03-10 16:27:03 +00007347 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007348 continue;
7349 }
7350
7351 /*
7352 * Add the pair to atts
7353 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00007354 if ((atts == NULL) || (nbatts + 5 > maxatts)) {
7355 if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007356 if (attvalue[len] == 0)
7357 xmlFree(attvalue);
7358 goto failed;
7359 }
7360 maxatts = ctxt->maxatts;
7361 atts = ctxt->atts;
7362 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00007363 ctxt->attallocs[nratts++] = alloc;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007364 atts[nbatts++] = attname;
7365 atts[nbatts++] = aprefix;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007366 atts[nbatts++] = NULL; /* the URI will be fetched later */
Daniel Veillard0fb18932003-09-07 09:14:37 +00007367 atts[nbatts++] = attvalue;
7368 attvalue += len;
7369 atts[nbatts++] = attvalue;
7370 /*
7371 * tag if some deallocation is needed
7372 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00007373 if (alloc != 0) attval = 1;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007374 } else {
7375 if ((attvalue != NULL) && (attvalue[len] == 0))
7376 xmlFree(attvalue);
7377 }
7378
7379failed:
7380
7381 GROW
Daniel Veillarde57ec792003-09-10 10:50:59 +00007382 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007383 if ((RAW == '>') || (((RAW == '/') && (NXT(1) == '>'))))
7384 break;
William M. Brack76e95df2003-10-18 16:20:14 +00007385 if (!IS_BLANK_CH(RAW)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007386 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
7387 "attributes construct error\n");
Daniel Veillard0fb18932003-09-07 09:14:37 +00007388 }
7389 SKIP_BLANKS;
7390 if ((cons == ctxt->input->consumed) && (q == CUR_PTR) &&
7391 (attname == NULL) && (attvalue == NULL)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007392 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007393 "xmlParseStartTag: problem parsing attributes\n");
Daniel Veillard0fb18932003-09-07 09:14:37 +00007394 break;
7395 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007396 GROW;
Daniel Veillarde57ec792003-09-10 10:50:59 +00007397 if (ctxt->input->base != base) goto base_changed;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007398 }
7399
Daniel Veillard0fb18932003-09-07 09:14:37 +00007400 /*
Daniel Veillarde57ec792003-09-10 10:50:59 +00007401 * The attributes defaulting
7402 */
7403 if (ctxt->attsDefault != NULL) {
7404 xmlDefAttrsPtr defaults;
7405
7406 defaults = xmlHashLookup2(ctxt->attsDefault, localname, prefix);
7407 if (defaults != NULL) {
7408 for (i = 0;i < defaults->nbAttrs;i++) {
7409 attname = defaults->values[4 * i];
7410 aprefix = defaults->values[4 * i + 1];
7411
7412 /*
7413 * special work for namespaces defaulted defs
7414 */
7415 if ((attname == ctxt->str_xmlns) && (aprefix == NULL)) {
7416 /*
7417 * check that it's not a defined namespace
7418 */
7419 for (j = 1;j <= nbNs;j++)
7420 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == NULL)
7421 break;
7422 if (j <= nbNs) continue;
7423
7424 nsname = xmlGetNamespace(ctxt, NULL);
7425 if (nsname != defaults->values[4 * i + 2]) {
7426 if (nsPush(ctxt, NULL,
7427 defaults->values[4 * i + 2]) > 0)
7428 nbNs++;
7429 }
7430 } else if (aprefix == ctxt->str_xmlns) {
7431 /*
7432 * check that it's not a defined namespace
7433 */
7434 for (j = 1;j <= nbNs;j++)
7435 if (ctxt->nsTab[ctxt->nsNr - 2 * j] == attname)
7436 break;
7437 if (j <= nbNs) continue;
7438
7439 nsname = xmlGetNamespace(ctxt, attname);
7440 if (nsname != defaults->values[2]) {
7441 if (nsPush(ctxt, attname,
7442 defaults->values[4 * i + 2]) > 0)
7443 nbNs++;
7444 }
7445 } else {
7446 /*
7447 * check that it's not a defined attribute
7448 */
7449 for (j = 0;j < nbatts;j+=5) {
7450 if ((attname == atts[j]) && (aprefix == atts[j+1]))
7451 break;
7452 }
7453 if (j < nbatts) continue;
7454
7455 if ((atts == NULL) || (nbatts + 5 > maxatts)) {
7456 if (xmlCtxtGrowAttrs(ctxt, nbatts + 5) < 0) {
Daniel Veillard9ee35f32003-09-28 00:19:54 +00007457 return(NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007458 }
7459 maxatts = ctxt->maxatts;
7460 atts = ctxt->atts;
7461 }
7462 atts[nbatts++] = attname;
7463 atts[nbatts++] = aprefix;
7464 if (aprefix == NULL)
7465 atts[nbatts++] = NULL;
7466 else
7467 atts[nbatts++] = xmlGetNamespace(ctxt, aprefix);
7468 atts[nbatts++] = defaults->values[4 * i + 2];
7469 atts[nbatts++] = defaults->values[4 * i + 3];
7470 nbdef++;
7471 }
7472 }
7473 }
7474 }
7475
Daniel Veillarde70c8772003-11-25 07:21:18 +00007476 /*
7477 * The attributes checkings
7478 */
7479 for (i = 0; i < nbatts;i += 5) {
7480 nsname = xmlGetNamespace(ctxt, atts[i + 1]);
7481 if ((atts[i + 1] != NULL) && (nsname == NULL)) {
7482 xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
7483 "Namespace prefix %s for %s on %s is not defined\n",
7484 atts[i + 1], atts[i], localname);
7485 }
7486 atts[i + 2] = nsname;
7487 /*
7488 * [ WFC: Unique Att Spec ]
7489 * No attribute name may appear more than once in the same
7490 * start-tag or empty-element tag.
7491 * As extended by the Namespace in XML REC.
7492 */
7493 for (j = 0; j < i;j += 5) {
7494 if (atts[i] == atts[j]) {
7495 if (atts[i+1] == atts[j+1]) {
7496 xmlErrAttributeDup(ctxt, atts[i+1], atts[i]);
7497 break;
7498 }
7499 if ((nsname != NULL) && (atts[j + 2] == nsname)) {
7500 xmlNsErr(ctxt, XML_NS_ERR_ATTRIBUTE_REDEFINED,
7501 "Namespaced Attribute %s in '%s' redefined\n",
7502 atts[i], nsname, NULL);
7503 break;
7504 }
7505 }
7506 }
7507 }
7508
Daniel Veillarde57ec792003-09-10 10:50:59 +00007509 nsname = xmlGetNamespace(ctxt, prefix);
7510 if ((prefix != NULL) && (nsname == NULL)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007511 xmlNsErr(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
7512 "Namespace prefix %s on %s is not defined\n",
7513 prefix, localname, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007514 }
7515 *pref = prefix;
7516 *URI = nsname;
7517
7518 /*
7519 * SAX: Start of Element !
7520 */
7521 if ((ctxt->sax != NULL) && (ctxt->sax->startElementNs != NULL) &&
7522 (!ctxt->disableSAX)) {
7523 if (nbNs > 0)
7524 ctxt->sax->startElementNs(ctxt->userData, localname, prefix,
7525 nsname, nbNs, &ctxt->nsTab[ctxt->nsNr - 2 * nbNs],
7526 nbatts / 5, nbdef, atts);
7527 else
7528 ctxt->sax->startElementNs(ctxt->userData, localname, prefix,
7529 nsname, 0, NULL, nbatts / 5, nbdef, atts);
7530 }
7531
7532 /*
7533 * Free up attribute allocated strings if needed
7534 */
7535 if (attval != 0) {
7536 for (i = 3,j = 0; j < nratts;i += 5,j++)
7537 if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL))
7538 xmlFree((xmlChar *) atts[i]);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007539 }
7540
7541 return(localname);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007542
7543base_changed:
7544 /*
7545 * the attribute strings are valid iif the base didn't changed
7546 */
7547 if (attval != 0) {
7548 for (i = 3,j = 0; j < nratts;i += 5,j++)
7549 if ((ctxt->attallocs[j] != 0) && (atts[i] != NULL))
7550 xmlFree((xmlChar *) atts[i]);
7551 }
7552 ctxt->input->cur = ctxt->input->base + cur;
7553 if (ctxt->wellFormed == 1) {
7554 goto reparse;
7555 }
7556 return(NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007557}
7558
7559/**
7560 * xmlParseEndTag2:
7561 * @ctxt: an XML parser context
7562 * @line: line of the start tag
7563 * @nsNr: number of namespaces on the start tag
7564 *
7565 * parse an end of tag
7566 *
7567 * [42] ETag ::= '</' Name S? '>'
7568 *
7569 * With namespace
7570 *
7571 * [NS 9] ETag ::= '</' QName S? '>'
7572 */
7573
7574static void
7575xmlParseEndTag2(xmlParserCtxtPtr ctxt, const xmlChar *prefix,
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007576 const xmlChar *URI, int line, int nsNr, int tlen) {
Daniel Veillard0fb18932003-09-07 09:14:37 +00007577 const xmlChar *name;
7578
7579 GROW;
7580 if ((RAW != '<') || (NXT(1) != '/')) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00007581 xmlFatalErr(ctxt, XML_ERR_LTSLASH_REQUIRED, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007582 return;
7583 }
7584 SKIP(2);
7585
Daniel Veillard453e71b2004-04-20 17:44:46 +00007586 if ((tlen > 0) && (strncmp(ctxt->input->cur, ctxt->name, tlen) == 0)) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007587 if (ctxt->input->cur[tlen] == '>') {
7588 ctxt->input->cur += tlen + 1;
7589 goto done;
7590 }
7591 ctxt->input->cur += tlen;
7592 name = (xmlChar*)1;
7593 } else {
7594 if (prefix == NULL)
7595 name = xmlParseNameAndCompare(ctxt, ctxt->name);
7596 else
7597 name = xmlParseQNameAndCompare(ctxt, ctxt->name, prefix);
7598 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007599
7600 /*
7601 * We should definitely be at the ending "S? '>'" part
7602 */
7603 GROW;
7604 SKIP_BLANKS;
Daniel Veillard73b013f2003-09-30 12:36:01 +00007605 if ((!IS_BYTE_CHAR(RAW)) || (RAW != '>')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007606 xmlFatalErr(ctxt, XML_ERR_GT_REQUIRED, NULL);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007607 } else
7608 NEXT1;
7609
7610 /*
7611 * [ WFC: Element Type Match ]
7612 * The Name in an element's end-tag must match the element type in the
7613 * start-tag.
7614 *
7615 */
7616 if (name != (xmlChar*)1) {
Daniel Veillardf403d292003-10-05 13:51:35 +00007617 if (name == NULL) name = BAD_CAST "unparseable";
7618 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
Daniel Veillard0fb18932003-09-07 09:14:37 +00007619 "Opening and ending tag mismatch: %s line %d and %s\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00007620 ctxt->name, line, name);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007621 }
7622
7623 /*
7624 * SAX: End of Tag
7625 */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007626done:
Daniel Veillard0fb18932003-09-07 09:14:37 +00007627 if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
7628 (!ctxt->disableSAX))
7629 ctxt->sax->endElementNs(ctxt->userData, ctxt->name, prefix, URI);
7630
Daniel Veillard0fb18932003-09-07 09:14:37 +00007631 spacePop(ctxt);
7632 if (nsNr != 0)
7633 nsPop(ctxt, nsNr);
7634 return;
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00007635}
7636
7637/**
Owen Taylor3473f882001-02-23 17:55:21 +00007638 * xmlParseCDSect:
7639 * @ctxt: an XML parser context
7640 *
7641 * Parse escaped pure raw content.
7642 *
7643 * [18] CDSect ::= CDStart CData CDEnd
7644 *
7645 * [19] CDStart ::= '<![CDATA['
7646 *
7647 * [20] Data ::= (Char* - (Char* ']]>' Char*))
7648 *
7649 * [21] CDEnd ::= ']]>'
7650 */
7651void
7652xmlParseCDSect(xmlParserCtxtPtr ctxt) {
7653 xmlChar *buf = NULL;
7654 int len = 0;
7655 int size = XML_PARSER_BUFFER_SIZE;
7656 int r, rl;
7657 int s, sl;
7658 int cur, l;
7659 int count = 0;
7660
Daniel Veillard8f597c32003-10-06 08:19:27 +00007661 /* Check 2.6.0 was NXT(0) not RAW */
Daniel Veillarda07050d2003-10-19 14:46:32 +00007662 if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007663 SKIP(9);
7664 } else
7665 return;
7666
7667 ctxt->instate = XML_PARSER_CDATA_SECTION;
7668 r = CUR_CHAR(rl);
William M. Brack871611b2003-10-18 04:53:14 +00007669 if (!IS_CHAR(r)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007670 xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007671 ctxt->instate = XML_PARSER_CONTENT;
7672 return;
7673 }
7674 NEXTL(rl);
7675 s = CUR_CHAR(sl);
William M. Brack871611b2003-10-18 04:53:14 +00007676 if (!IS_CHAR(s)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007677 xmlFatalErr(ctxt, XML_ERR_CDATA_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007678 ctxt->instate = XML_PARSER_CONTENT;
7679 return;
7680 }
7681 NEXTL(sl);
7682 cur = CUR_CHAR(l);
Daniel Veillard3c908dc2003-04-19 00:07:51 +00007683 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00007684 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007685 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007686 return;
7687 }
William M. Brack871611b2003-10-18 04:53:14 +00007688 while (IS_CHAR(cur) &&
Owen Taylor3473f882001-02-23 17:55:21 +00007689 ((r != ']') || (s != ']') || (cur != '>'))) {
7690 if (len + 5 >= size) {
7691 size *= 2;
7692 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
7693 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007694 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007695 return;
7696 }
7697 }
7698 COPY_BUF(rl,buf,len,r);
7699 r = s;
7700 rl = sl;
7701 s = cur;
7702 sl = l;
7703 count++;
7704 if (count > 50) {
7705 GROW;
7706 count = 0;
7707 }
7708 NEXTL(l);
7709 cur = CUR_CHAR(l);
7710 }
7711 buf[len] = 0;
7712 ctxt->instate = XML_PARSER_CONTENT;
7713 if (cur != '>') {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00007714 xmlFatalErrMsgStr(ctxt, XML_ERR_CDATA_NOT_FINISHED,
Owen Taylor3473f882001-02-23 17:55:21 +00007715 "CData section not finished\n%.50s\n", buf);
Owen Taylor3473f882001-02-23 17:55:21 +00007716 xmlFree(buf);
7717 return;
7718 }
7719 NEXTL(l);
7720
7721 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00007722 * OK the buffer is to be consumed as cdata.
Owen Taylor3473f882001-02-23 17:55:21 +00007723 */
7724 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
7725 if (ctxt->sax->cdataBlock != NULL)
7726 ctxt->sax->cdataBlock(ctxt->userData, buf, len);
Daniel Veillard7583a592001-07-08 13:15:55 +00007727 else if (ctxt->sax->characters != NULL)
7728 ctxt->sax->characters(ctxt->userData, buf, len);
Owen Taylor3473f882001-02-23 17:55:21 +00007729 }
7730 xmlFree(buf);
7731}
7732
7733/**
7734 * xmlParseContent:
7735 * @ctxt: an XML parser context
7736 *
7737 * Parse a content:
7738 *
7739 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
7740 */
7741
7742void
7743xmlParseContent(xmlParserCtxtPtr ctxt) {
7744 GROW;
Daniel Veillardfdc91562002-07-01 21:52:03 +00007745 while ((RAW != 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +00007746 ((RAW != '<') || (NXT(1) != '/'))) {
7747 const xmlChar *test = CUR_PTR;
Daniel Veillard3e59fc52003-04-18 12:34:58 +00007748 unsigned int cons = ctxt->input->consumed;
Daniel Veillard21a0f912001-02-25 19:54:14 +00007749 const xmlChar *cur = ctxt->input->cur;
Owen Taylor3473f882001-02-23 17:55:21 +00007750
7751 /*
Owen Taylor3473f882001-02-23 17:55:21 +00007752 * First case : a Processing Instruction.
7753 */
Daniel Veillardfdc91562002-07-01 21:52:03 +00007754 if ((*cur == '<') && (cur[1] == '?')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007755 xmlParsePI(ctxt);
7756 }
7757
7758 /*
7759 * Second case : a CDSection
7760 */
Daniel Veillard8f597c32003-10-06 08:19:27 +00007761 /* 2.6.0 test was *cur not RAW */
Daniel Veillarda07050d2003-10-19 14:46:32 +00007762 else if (CMP9(CUR_PTR, '<', '!', '[', 'C', 'D', 'A', 'T', 'A', '[')) {
Owen Taylor3473f882001-02-23 17:55:21 +00007763 xmlParseCDSect(ctxt);
7764 }
7765
7766 /*
7767 * Third case : a comment
7768 */
Daniel Veillard21a0f912001-02-25 19:54:14 +00007769 else if ((*cur == '<') && (NXT(1) == '!') &&
Owen Taylor3473f882001-02-23 17:55:21 +00007770 (NXT(2) == '-') && (NXT(3) == '-')) {
7771 xmlParseComment(ctxt);
7772 ctxt->instate = XML_PARSER_CONTENT;
7773 }
7774
7775 /*
7776 * Fourth case : a sub-element.
7777 */
Daniel Veillard21a0f912001-02-25 19:54:14 +00007778 else if (*cur == '<') {
Owen Taylor3473f882001-02-23 17:55:21 +00007779 xmlParseElement(ctxt);
7780 }
7781
7782 /*
7783 * Fifth case : a reference. If if has not been resolved,
7784 * parsing returns it's Name, create the node
7785 */
7786
Daniel Veillard21a0f912001-02-25 19:54:14 +00007787 else if (*cur == '&') {
Owen Taylor3473f882001-02-23 17:55:21 +00007788 xmlParseReference(ctxt);
7789 }
7790
7791 /*
7792 * Last case, text. Note that References are handled directly.
7793 */
7794 else {
7795 xmlParseCharData(ctxt, 0);
7796 }
7797
7798 GROW;
7799 /*
7800 * Pop-up of finished entities.
7801 */
Daniel Veillard561b7f82002-03-20 21:55:57 +00007802 while ((RAW == 0) && (ctxt->inputNr > 1))
Owen Taylor3473f882001-02-23 17:55:21 +00007803 xmlPopInput(ctxt);
7804 SHRINK;
7805
Daniel Veillardfdc91562002-07-01 21:52:03 +00007806 if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007807 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
7808 "detected an error in element content\n");
Owen Taylor3473f882001-02-23 17:55:21 +00007809 ctxt->instate = XML_PARSER_EOF;
7810 break;
7811 }
7812 }
7813}
7814
7815/**
7816 * xmlParseElement:
7817 * @ctxt: an XML parser context
7818 *
7819 * parse an XML element, this is highly recursive
7820 *
7821 * [39] element ::= EmptyElemTag | STag content ETag
7822 *
7823 * [ WFC: Element Type Match ]
7824 * The Name in an element's end-tag must match the element type in the
7825 * start-tag.
7826 *
Owen Taylor3473f882001-02-23 17:55:21 +00007827 */
7828
7829void
7830xmlParseElement(xmlParserCtxtPtr ctxt) {
Daniel Veillard2fdbd322003-08-18 12:15:38 +00007831 const xmlChar *name;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007832 const xmlChar *prefix;
7833 const xmlChar *URI;
Owen Taylor3473f882001-02-23 17:55:21 +00007834 xmlParserNodeInfo node_info;
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007835 int line, tlen;
Owen Taylor3473f882001-02-23 17:55:21 +00007836 xmlNodePtr ret;
Daniel Veillard0fb18932003-09-07 09:14:37 +00007837 int nsNr = ctxt->nsNr;
Owen Taylor3473f882001-02-23 17:55:21 +00007838
7839 /* Capture start position */
7840 if (ctxt->record_info) {
7841 node_info.begin_pos = ctxt->input->consumed +
7842 (CUR_PTR - ctxt->input->base);
7843 node_info.begin_line = ctxt->input->line;
7844 }
7845
7846 if (ctxt->spaceNr == 0)
7847 spacePush(ctxt, -1);
7848 else
7849 spacePush(ctxt, *ctxt->space);
7850
Daniel Veillard6c5b2d32003-03-27 14:55:52 +00007851 line = ctxt->input->line;
Daniel Veillard81273902003-09-30 00:43:48 +00007852#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard0fb18932003-09-07 09:14:37 +00007853 if (ctxt->sax2)
Daniel Veillard81273902003-09-30 00:43:48 +00007854#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007855 name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen);
Daniel Veillard81273902003-09-30 00:43:48 +00007856#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard0fb18932003-09-07 09:14:37 +00007857 else
7858 name = xmlParseStartTag(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00007859#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00007860 if (name == NULL) {
7861 spacePop(ctxt);
7862 return;
7863 }
7864 namePush(ctxt, name);
7865 ret = ctxt->node;
7866
Daniel Veillard4432df22003-09-28 18:58:27 +00007867#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00007868 /*
7869 * [ VC: Root Element Type ]
7870 * The Name in the document type declaration must match the element
7871 * type of the root element.
7872 */
7873 if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
7874 ctxt->node && (ctxt->node == ctxt->myDoc->children))
7875 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillard4432df22003-09-28 18:58:27 +00007876#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00007877
7878 /*
7879 * Check for an Empty Element.
7880 */
7881 if ((RAW == '/') && (NXT(1) == '>')) {
7882 SKIP(2);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007883 if (ctxt->sax2) {
7884 if ((ctxt->sax != NULL) && (ctxt->sax->endElementNs != NULL) &&
7885 (!ctxt->disableSAX))
7886 ctxt->sax->endElementNs(ctxt->userData, name, prefix, URI);
Daniel Veillard81273902003-09-30 00:43:48 +00007887#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard0fb18932003-09-07 09:14:37 +00007888 } else {
7889 if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL) &&
7890 (!ctxt->disableSAX))
7891 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard81273902003-09-30 00:43:48 +00007892#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00007893 }
Daniel Veillard0fb18932003-09-07 09:14:37 +00007894 namePop(ctxt);
7895 spacePop(ctxt);
7896 if (nsNr != ctxt->nsNr)
7897 nsPop(ctxt, ctxt->nsNr - nsNr);
Owen Taylor3473f882001-02-23 17:55:21 +00007898 if ( ret != NULL && ctxt->record_info ) {
7899 node_info.end_pos = ctxt->input->consumed +
7900 (CUR_PTR - ctxt->input->base);
7901 node_info.end_line = ctxt->input->line;
7902 node_info.node = ret;
7903 xmlParserAddNodeInfo(ctxt, &node_info);
7904 }
7905 return;
7906 }
7907 if (RAW == '>') {
Daniel Veillard21a0f912001-02-25 19:54:14 +00007908 NEXT1;
Owen Taylor3473f882001-02-23 17:55:21 +00007909 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00007910 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED,
7911 "Couldn't find end of Start Tag %s line %d\n",
7912 name, line, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007913
7914 /*
7915 * end of parsing of this node.
7916 */
7917 nodePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007918 namePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007919 spacePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007920 if (nsNr != ctxt->nsNr)
7921 nsPop(ctxt, ctxt->nsNr - nsNr);
Owen Taylor3473f882001-02-23 17:55:21 +00007922
7923 /*
7924 * Capture end position and add node
7925 */
7926 if ( ret != NULL && ctxt->record_info ) {
7927 node_info.end_pos = ctxt->input->consumed +
7928 (CUR_PTR - ctxt->input->base);
7929 node_info.end_line = ctxt->input->line;
7930 node_info.node = ret;
7931 xmlParserAddNodeInfo(ctxt, &node_info);
7932 }
7933 return;
7934 }
7935
7936 /*
7937 * Parse the content of the element:
7938 */
7939 xmlParseContent(ctxt);
Daniel Veillard73b013f2003-09-30 12:36:01 +00007940 if (!IS_BYTE_CHAR(RAW)) {
Daniel Veillardf403d292003-10-05 13:51:35 +00007941 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED,
Daniel Veillard2b0f8792003-10-10 19:36:36 +00007942 "Premature end of data in tag %s line %d\n",
Daniel Veillardf403d292003-10-05 13:51:35 +00007943 name, line, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00007944
7945 /*
7946 * end of parsing of this node.
7947 */
7948 nodePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007949 namePop(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00007950 spacePop(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00007951 if (nsNr != ctxt->nsNr)
7952 nsPop(ctxt, ctxt->nsNr - nsNr);
Owen Taylor3473f882001-02-23 17:55:21 +00007953 return;
7954 }
7955
7956 /*
7957 * parse the end of tag: '</' should be here.
7958 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00007959 if (ctxt->sax2) {
Daniel Veillardc82c57e2004-01-12 16:24:34 +00007960 xmlParseEndTag2(ctxt, prefix, URI, line, ctxt->nsNr - nsNr, tlen);
Daniel Veillarde57ec792003-09-10 10:50:59 +00007961 namePop(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00007962 }
7963#ifdef LIBXML_SAX1_ENABLED
7964 else
Daniel Veillard0fb18932003-09-07 09:14:37 +00007965 xmlParseEndTag1(ctxt, line);
Daniel Veillard81273902003-09-30 00:43:48 +00007966#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00007967
7968 /*
7969 * Capture end position and add node
7970 */
7971 if ( ret != NULL && ctxt->record_info ) {
7972 node_info.end_pos = ctxt->input->consumed +
7973 (CUR_PTR - ctxt->input->base);
7974 node_info.end_line = ctxt->input->line;
7975 node_info.node = ret;
7976 xmlParserAddNodeInfo(ctxt, &node_info);
7977 }
7978}
7979
7980/**
7981 * xmlParseVersionNum:
7982 * @ctxt: an XML parser context
7983 *
7984 * parse the XML version value.
7985 *
7986 * [26] VersionNum ::= ([a-zA-Z0-9_.:] | '-')+
7987 *
7988 * Returns the string giving the XML version number, or NULL
7989 */
7990xmlChar *
7991xmlParseVersionNum(xmlParserCtxtPtr ctxt) {
7992 xmlChar *buf = NULL;
7993 int len = 0;
7994 int size = 10;
7995 xmlChar cur;
7996
Daniel Veillard3c908dc2003-04-19 00:07:51 +00007997 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00007998 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00007999 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008000 return(NULL);
8001 }
8002 cur = CUR;
8003 while (((cur >= 'a') && (cur <= 'z')) ||
8004 ((cur >= 'A') && (cur <= 'Z')) ||
8005 ((cur >= '0') && (cur <= '9')) ||
8006 (cur == '_') || (cur == '.') ||
8007 (cur == ':') || (cur == '-')) {
8008 if (len + 1 >= size) {
8009 size *= 2;
8010 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
8011 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008012 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008013 return(NULL);
8014 }
8015 }
8016 buf[len++] = cur;
8017 NEXT;
8018 cur=CUR;
8019 }
8020 buf[len] = 0;
8021 return(buf);
8022}
8023
8024/**
8025 * xmlParseVersionInfo:
8026 * @ctxt: an XML parser context
8027 *
8028 * parse the XML version.
8029 *
8030 * [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ")
8031 *
8032 * [25] Eq ::= S? '=' S?
8033 *
8034 * Returns the version string, e.g. "1.0"
8035 */
8036
8037xmlChar *
8038xmlParseVersionInfo(xmlParserCtxtPtr ctxt) {
8039 xmlChar *version = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00008040
Daniel Veillarda07050d2003-10-19 14:46:32 +00008041 if (CMP7(CUR_PTR, 'v', 'e', 'r', 's', 'i', 'o', 'n')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008042 SKIP(7);
8043 SKIP_BLANKS;
8044 if (RAW != '=') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008045 xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008046 return(NULL);
8047 }
8048 NEXT;
8049 SKIP_BLANKS;
8050 if (RAW == '"') {
8051 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008052 version = xmlParseVersionNum(ctxt);
8053 if (RAW != '"') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008054 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008055 } else
8056 NEXT;
8057 } else if (RAW == '\''){
8058 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008059 version = xmlParseVersionNum(ctxt);
8060 if (RAW != '\'') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008061 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008062 } else
8063 NEXT;
8064 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008065 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008066 }
8067 }
8068 return(version);
8069}
8070
8071/**
8072 * xmlParseEncName:
8073 * @ctxt: an XML parser context
8074 *
8075 * parse the XML encoding name
8076 *
8077 * [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
8078 *
8079 * Returns the encoding name value or NULL
8080 */
8081xmlChar *
8082xmlParseEncName(xmlParserCtxtPtr ctxt) {
8083 xmlChar *buf = NULL;
8084 int len = 0;
8085 int size = 10;
8086 xmlChar cur;
8087
8088 cur = CUR;
8089 if (((cur >= 'a') && (cur <= 'z')) ||
8090 ((cur >= 'A') && (cur <= 'Z'))) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008091 buf = (xmlChar *) xmlMallocAtomic(size * sizeof(xmlChar));
Owen Taylor3473f882001-02-23 17:55:21 +00008092 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008093 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008094 return(NULL);
8095 }
8096
8097 buf[len++] = cur;
8098 NEXT;
8099 cur = CUR;
8100 while (((cur >= 'a') && (cur <= 'z')) ||
8101 ((cur >= 'A') && (cur <= 'Z')) ||
8102 ((cur >= '0') && (cur <= '9')) ||
8103 (cur == '.') || (cur == '_') ||
8104 (cur == '-')) {
8105 if (len + 1 >= size) {
8106 size *= 2;
8107 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
8108 if (buf == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008109 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008110 return(NULL);
8111 }
8112 }
8113 buf[len++] = cur;
8114 NEXT;
8115 cur = CUR;
8116 if (cur == 0) {
8117 SHRINK;
8118 GROW;
8119 cur = CUR;
8120 }
8121 }
8122 buf[len] = 0;
8123 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008124 xmlFatalErr(ctxt, XML_ERR_ENCODING_NAME, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008125 }
8126 return(buf);
8127}
8128
8129/**
8130 * xmlParseEncodingDecl:
8131 * @ctxt: an XML parser context
8132 *
8133 * parse the XML encoding declaration
8134 *
8135 * [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'")
8136 *
8137 * this setups the conversion filters.
8138 *
8139 * Returns the encoding value or NULL
8140 */
8141
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008142const xmlChar *
Owen Taylor3473f882001-02-23 17:55:21 +00008143xmlParseEncodingDecl(xmlParserCtxtPtr ctxt) {
8144 xmlChar *encoding = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00008145
8146 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008147 if (CMP8(CUR_PTR, 'e', 'n', 'c', 'o', 'd', 'i', 'n', 'g')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008148 SKIP(8);
8149 SKIP_BLANKS;
8150 if (RAW != '=') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008151 xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008152 return(NULL);
8153 }
8154 NEXT;
8155 SKIP_BLANKS;
8156 if (RAW == '"') {
8157 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008158 encoding = xmlParseEncName(ctxt);
8159 if (RAW != '"') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008160 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008161 } else
8162 NEXT;
8163 } else if (RAW == '\''){
8164 NEXT;
Owen Taylor3473f882001-02-23 17:55:21 +00008165 encoding = xmlParseEncName(ctxt);
8166 if (RAW != '\'') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008167 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008168 } else
8169 NEXT;
Daniel Veillard82ac6b02002-02-17 23:18:55 +00008170 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008171 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008172 }
Daniel Veillard6b621b82003-08-11 15:03:34 +00008173 /*
8174 * UTF-16 encoding stwich has already taken place at this stage,
8175 * more over the little-endian/big-endian selection is already done
8176 */
8177 if ((encoding != NULL) &&
8178 ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-16")) ||
8179 (!xmlStrcasecmp(encoding, BAD_CAST "UTF16")))) {
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008180 if (ctxt->encoding != NULL)
8181 xmlFree((xmlChar *) ctxt->encoding);
8182 ctxt->encoding = encoding;
Daniel Veillardb19ba832003-08-14 00:33:46 +00008183 }
8184 /*
8185 * UTF-8 encoding is handled natively
8186 */
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008187 else if ((encoding != NULL) &&
Daniel Veillardb19ba832003-08-14 00:33:46 +00008188 ((!xmlStrcasecmp(encoding, BAD_CAST "UTF-8")) ||
8189 (!xmlStrcasecmp(encoding, BAD_CAST "UTF8")))) {
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008190 if (ctxt->encoding != NULL)
8191 xmlFree((xmlChar *) ctxt->encoding);
8192 ctxt->encoding = encoding;
Daniel Veillard6b621b82003-08-11 15:03:34 +00008193 }
Daniel Veillardab1ae3a2003-08-14 12:19:54 +00008194 else if (encoding != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00008195 xmlCharEncodingHandlerPtr handler;
8196
8197 if (ctxt->input->encoding != NULL)
8198 xmlFree((xmlChar *) ctxt->input->encoding);
8199 ctxt->input->encoding = encoding;
8200
Daniel Veillarda6874ca2003-07-29 16:47:24 +00008201 handler = xmlFindCharEncodingHandler((const char *) encoding);
8202 if (handler != NULL) {
8203 xmlSwitchToEncoding(ctxt, handler);
Owen Taylor3473f882001-02-23 17:55:21 +00008204 } else {
Daniel Veillardf403d292003-10-05 13:51:35 +00008205 xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
Daniel Veillarda6874ca2003-07-29 16:47:24 +00008206 "Unsupported encoding %s\n", encoding);
8207 return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008208 }
8209 }
8210 }
8211 return(encoding);
8212}
8213
8214/**
8215 * xmlParseSDDecl:
8216 * @ctxt: an XML parser context
8217 *
8218 * parse the XML standalone declaration
8219 *
8220 * [32] SDDecl ::= S 'standalone' Eq
8221 * (("'" ('yes' | 'no') "'") | ('"' ('yes' | 'no')'"'))
8222 *
8223 * [ VC: Standalone Document Declaration ]
8224 * TODO The standalone document declaration must have the value "no"
8225 * if any external markup declarations contain declarations of:
8226 * - attributes with default values, if elements to which these
8227 * attributes apply appear in the document without specifications
8228 * of values for these attributes, or
8229 * - entities (other than amp, lt, gt, apos, quot), if references
8230 * to those entities appear in the document, or
8231 * - attributes with values subject to normalization, where the
8232 * attribute appears in the document with a value which will change
8233 * as a result of normalization, or
8234 * - element types with element content, if white space occurs directly
8235 * within any instance of those types.
8236 *
8237 * Returns 1 if standalone, 0 otherwise
8238 */
8239
8240int
8241xmlParseSDDecl(xmlParserCtxtPtr ctxt) {
8242 int standalone = -1;
8243
8244 SKIP_BLANKS;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008245 if (CMP10(CUR_PTR, 's', 't', 'a', 'n', 'd', 'a', 'l', 'o', 'n', 'e')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008246 SKIP(10);
8247 SKIP_BLANKS;
8248 if (RAW != '=') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008249 xmlFatalErr(ctxt, XML_ERR_EQUAL_REQUIRED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008250 return(standalone);
8251 }
8252 NEXT;
8253 SKIP_BLANKS;
8254 if (RAW == '\''){
8255 NEXT;
8256 if ((RAW == 'n') && (NXT(1) == 'o')) {
8257 standalone = 0;
8258 SKIP(2);
8259 } else if ((RAW == 'y') && (NXT(1) == 'e') &&
8260 (NXT(2) == 's')) {
8261 standalone = 1;
8262 SKIP(3);
8263 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008264 xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008265 }
8266 if (RAW != '\'') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008267 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008268 } else
8269 NEXT;
8270 } else if (RAW == '"'){
8271 NEXT;
8272 if ((RAW == 'n') && (NXT(1) == 'o')) {
8273 standalone = 0;
8274 SKIP(2);
8275 } else if ((RAW == 'y') && (NXT(1) == 'e') &&
8276 (NXT(2) == 's')) {
8277 standalone = 1;
8278 SKIP(3);
8279 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008280 xmlFatalErr(ctxt, XML_ERR_STANDALONE_VALUE, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008281 }
8282 if (RAW != '"') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008283 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_CLOSED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008284 } else
8285 NEXT;
8286 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008287 xmlFatalErr(ctxt, XML_ERR_STRING_NOT_STARTED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008288 }
8289 }
8290 return(standalone);
8291}
8292
8293/**
8294 * xmlParseXMLDecl:
8295 * @ctxt: an XML parser context
8296 *
8297 * parse an XML declaration header
8298 *
8299 * [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
8300 */
8301
8302void
8303xmlParseXMLDecl(xmlParserCtxtPtr ctxt) {
8304 xmlChar *version;
8305
8306 /*
8307 * We know that '<?xml' is here.
8308 */
8309 SKIP(5);
8310
William M. Brack76e95df2003-10-18 16:20:14 +00008311 if (!IS_BLANK_CH(RAW)) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008312 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED,
8313 "Blank needed after '<?xml'\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008314 }
8315 SKIP_BLANKS;
8316
8317 /*
Daniel Veillard19840942001-11-29 16:11:38 +00008318 * We must have the VersionInfo here.
Owen Taylor3473f882001-02-23 17:55:21 +00008319 */
8320 version = xmlParseVersionInfo(ctxt);
Daniel Veillard19840942001-11-29 16:11:38 +00008321 if (version == NULL) {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008322 xmlFatalErr(ctxt, XML_ERR_VERSION_MISSING, NULL);
Daniel Veillard19840942001-11-29 16:11:38 +00008323 } else {
8324 if (!xmlStrEqual(version, (const xmlChar *) XML_DEFAULT_VERSION)) {
8325 /*
8326 * TODO: Blueberry should be detected here
8327 */
Daniel Veillard24eb9782003-10-04 21:08:09 +00008328 xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION,
8329 "Unsupported version '%s'\n",
8330 version, NULL);
Daniel Veillard19840942001-11-29 16:11:38 +00008331 }
8332 if (ctxt->version != NULL)
Daniel Veillardd3b08822001-12-05 12:03:33 +00008333 xmlFree((void *) ctxt->version);
Daniel Veillard19840942001-11-29 16:11:38 +00008334 ctxt->version = version;
Daniel Veillarda050d232001-09-05 15:51:05 +00008335 }
Owen Taylor3473f882001-02-23 17:55:21 +00008336
8337 /*
8338 * We may have the encoding declaration
8339 */
William M. Brack76e95df2003-10-18 16:20:14 +00008340 if (!IS_BLANK_CH(RAW)) {
Owen Taylor3473f882001-02-23 17:55:21 +00008341 if ((RAW == '?') && (NXT(1) == '>')) {
8342 SKIP(2);
8343 return;
8344 }
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008345 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008346 }
8347 xmlParseEncodingDecl(ctxt);
8348 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
8349 /*
8350 * The XML REC instructs us to stop parsing right here
8351 */
8352 return;
8353 }
8354
8355 /*
8356 * We may have the standalone status.
8357 */
William M. Brack76e95df2003-10-18 16:20:14 +00008358 if ((ctxt->input->encoding != NULL) && (!IS_BLANK_CH(RAW))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008359 if ((RAW == '?') && (NXT(1) == '>')) {
8360 SKIP(2);
8361 return;
8362 }
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008363 xmlFatalErrMsg(ctxt, XML_ERR_SPACE_REQUIRED, "Blank needed here\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008364 }
8365 SKIP_BLANKS;
8366 ctxt->input->standalone = xmlParseSDDecl(ctxt);
8367
8368 SKIP_BLANKS;
8369 if ((RAW == '?') && (NXT(1) == '>')) {
8370 SKIP(2);
8371 } else if (RAW == '>') {
8372 /* Deprecated old WD ... */
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008373 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008374 NEXT;
8375 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008376 xmlFatalErr(ctxt, XML_ERR_XMLDECL_NOT_FINISHED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008377 MOVETO_ENDTAG(CUR_PTR);
8378 NEXT;
8379 }
8380}
8381
8382/**
8383 * xmlParseMisc:
8384 * @ctxt: an XML parser context
8385 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00008386 * parse an XML Misc* optional field.
Owen Taylor3473f882001-02-23 17:55:21 +00008387 *
8388 * [27] Misc ::= Comment | PI | S
8389 */
8390
8391void
8392xmlParseMisc(xmlParserCtxtPtr ctxt) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00008393 while (((RAW == '<') && (NXT(1) == '?')) ||
Daniel Veillarda07050d2003-10-19 14:46:32 +00008394 (CMP4(CUR_PTR, '<', '!', '-', '-')) ||
William M. Brack76e95df2003-10-18 16:20:14 +00008395 IS_BLANK_CH(CUR)) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00008396 if ((RAW == '<') && (NXT(1) == '?')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008397 xmlParsePI(ctxt);
William M. Brack76e95df2003-10-18 16:20:14 +00008398 } else if (IS_BLANK_CH(CUR)) {
Owen Taylor3473f882001-02-23 17:55:21 +00008399 NEXT;
8400 } else
8401 xmlParseComment(ctxt);
8402 }
8403}
8404
8405/**
8406 * xmlParseDocument:
8407 * @ctxt: an XML parser context
8408 *
8409 * parse an XML document (and build a tree if using the standard SAX
8410 * interface).
8411 *
8412 * [1] document ::= prolog element Misc*
8413 *
8414 * [22] prolog ::= XMLDecl? Misc* (doctypedecl Misc*)?
8415 *
8416 * Returns 0, -1 in case of error. the parser context is augmented
8417 * as a result of the parsing.
8418 */
8419
8420int
8421xmlParseDocument(xmlParserCtxtPtr ctxt) {
8422 xmlChar start[4];
8423 xmlCharEncoding enc;
8424
8425 xmlInitParser();
8426
8427 GROW;
8428
8429 /*
Daniel Veillard0fb18932003-09-07 09:14:37 +00008430 * SAX: detecting the level.
8431 */
Daniel Veillarde57ec792003-09-10 10:50:59 +00008432 xmlDetectSAX2(ctxt);
Daniel Veillard0fb18932003-09-07 09:14:37 +00008433
8434 /*
Owen Taylor3473f882001-02-23 17:55:21 +00008435 * SAX: beginning of the document processing.
8436 */
8437 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
8438 ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
8439
Daniel Veillard4aede2e2003-10-17 12:43:59 +00008440 if ((ctxt->encoding == (const xmlChar *)XML_CHAR_ENCODING_NONE) &&
8441 ((ctxt->input->end - ctxt->input->cur) >= 4)) {
Daniel Veillard4aafa792001-07-28 17:21:12 +00008442 /*
8443 * Get the 4 first bytes and decode the charset
8444 * if enc != XML_CHAR_ENCODING_NONE
8445 * plug some encoding conversion routines.
8446 */
8447 start[0] = RAW;
8448 start[1] = NXT(1);
8449 start[2] = NXT(2);
8450 start[3] = NXT(3);
Daniel Veillard4aede2e2003-10-17 12:43:59 +00008451 enc = xmlDetectCharEncoding(&start[0], 4);
Daniel Veillard4aafa792001-07-28 17:21:12 +00008452 if (enc != XML_CHAR_ENCODING_NONE) {
8453 xmlSwitchEncoding(ctxt, enc);
8454 }
Owen Taylor3473f882001-02-23 17:55:21 +00008455 }
8456
8457
8458 if (CUR == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008459 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008460 }
8461
8462 /*
8463 * Check for the XMLDecl in the Prolog.
8464 */
8465 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008466 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008467
8468 /*
8469 * Note that we will switch encoding on the fly.
8470 */
8471 xmlParseXMLDecl(ctxt);
8472 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
8473 /*
8474 * The XML REC instructs us to stop parsing right here
8475 */
8476 return(-1);
8477 }
8478 ctxt->standalone = ctxt->input->standalone;
8479 SKIP_BLANKS;
8480 } else {
8481 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
8482 }
8483 if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
8484 ctxt->sax->startDocument(ctxt->userData);
8485
8486 /*
8487 * The Misc part of the Prolog
8488 */
8489 GROW;
8490 xmlParseMisc(ctxt);
8491
8492 /*
8493 * Then possibly doc type declaration(s) and more Misc
8494 * (doctypedecl Misc*)?
8495 */
8496 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008497 if (CMP9(CUR_PTR, '<', '!', 'D', 'O', 'C', 'T', 'Y', 'P', 'E')) {
Owen Taylor3473f882001-02-23 17:55:21 +00008498
8499 ctxt->inSubset = 1;
8500 xmlParseDocTypeDecl(ctxt);
8501 if (RAW == '[') {
8502 ctxt->instate = XML_PARSER_DTD;
8503 xmlParseInternalSubset(ctxt);
8504 }
8505
8506 /*
8507 * Create and update the external subset.
8508 */
8509 ctxt->inSubset = 2;
8510 if ((ctxt->sax != NULL) && (ctxt->sax->externalSubset != NULL) &&
8511 (!ctxt->disableSAX))
8512 ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
8513 ctxt->extSubSystem, ctxt->extSubURI);
8514 ctxt->inSubset = 0;
8515
8516
8517 ctxt->instate = XML_PARSER_PROLOG;
8518 xmlParseMisc(ctxt);
8519 }
8520
8521 /*
8522 * Time to start parsing the tree itself
8523 */
8524 GROW;
8525 if (RAW != '<') {
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00008526 xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY,
8527 "Start tag expected, '<' not found\n");
Owen Taylor3473f882001-02-23 17:55:21 +00008528 } else {
8529 ctxt->instate = XML_PARSER_CONTENT;
8530 xmlParseElement(ctxt);
8531 ctxt->instate = XML_PARSER_EPILOG;
8532
8533
8534 /*
8535 * The Misc part at the end
8536 */
8537 xmlParseMisc(ctxt);
8538
Daniel Veillard561b7f82002-03-20 21:55:57 +00008539 if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008540 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008541 }
8542 ctxt->instate = XML_PARSER_EOF;
8543 }
8544
8545 /*
8546 * SAX: end of the document processing.
8547 */
Daniel Veillard8d24cc12002-03-05 15:41:29 +00008548 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00008549 ctxt->sax->endDocument(ctxt->userData);
8550
Daniel Veillard5997aca2002-03-18 18:36:20 +00008551 /*
8552 * Remove locally kept entity definitions if the tree was not built
8553 */
8554 if ((ctxt->myDoc != NULL) &&
8555 (xmlStrEqual(ctxt->myDoc->version, SAX_COMPAT_MODE))) {
8556 xmlFreeDoc(ctxt->myDoc);
8557 ctxt->myDoc = NULL;
8558 }
8559
Daniel Veillardc7612992002-02-17 22:47:37 +00008560 if (! ctxt->wellFormed) {
8561 ctxt->valid = 0;
8562 return(-1);
8563 }
Owen Taylor3473f882001-02-23 17:55:21 +00008564 return(0);
8565}
8566
8567/**
8568 * xmlParseExtParsedEnt:
8569 * @ctxt: an XML parser context
8570 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00008571 * parse a general parsed entity
Owen Taylor3473f882001-02-23 17:55:21 +00008572 * An external general parsed entity is well-formed if it matches the
8573 * production labeled extParsedEnt.
8574 *
8575 * [78] extParsedEnt ::= TextDecl? content
8576 *
8577 * Returns 0, -1 in case of error. the parser context is augmented
8578 * as a result of the parsing.
8579 */
8580
8581int
8582xmlParseExtParsedEnt(xmlParserCtxtPtr ctxt) {
8583 xmlChar start[4];
8584 xmlCharEncoding enc;
8585
8586 xmlDefaultSAXHandlerInit();
8587
Daniel Veillard309f81d2003-09-23 09:02:53 +00008588 xmlDetectSAX2(ctxt);
8589
Owen Taylor3473f882001-02-23 17:55:21 +00008590 GROW;
8591
8592 /*
8593 * SAX: beginning of the document processing.
8594 */
8595 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
8596 ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
8597
8598 /*
8599 * Get the 4 first bytes and decode the charset
8600 * if enc != XML_CHAR_ENCODING_NONE
8601 * plug some encoding conversion routines.
8602 */
Daniel Veillard4aede2e2003-10-17 12:43:59 +00008603 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
8604 start[0] = RAW;
8605 start[1] = NXT(1);
8606 start[2] = NXT(2);
8607 start[3] = NXT(3);
8608 enc = xmlDetectCharEncoding(start, 4);
8609 if (enc != XML_CHAR_ENCODING_NONE) {
8610 xmlSwitchEncoding(ctxt, enc);
8611 }
Owen Taylor3473f882001-02-23 17:55:21 +00008612 }
8613
8614
8615 if (CUR == 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008616 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008617 }
8618
8619 /*
8620 * Check for the XMLDecl in the Prolog.
8621 */
8622 GROW;
Daniel Veillarda07050d2003-10-19 14:46:32 +00008623 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008624
8625 /*
8626 * Note that we will switch encoding on the fly.
8627 */
8628 xmlParseXMLDecl(ctxt);
8629 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
8630 /*
8631 * The XML REC instructs us to stop parsing right here
8632 */
8633 return(-1);
8634 }
8635 SKIP_BLANKS;
8636 } else {
8637 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
8638 }
8639 if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
8640 ctxt->sax->startDocument(ctxt->userData);
8641
8642 /*
8643 * Doing validity checking on chunk doesn't make sense
8644 */
8645 ctxt->instate = XML_PARSER_CONTENT;
8646 ctxt->validate = 0;
8647 ctxt->loadsubset = 0;
8648 ctxt->depth = 0;
8649
8650 xmlParseContent(ctxt);
8651
8652 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008653 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008654 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008655 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008656 }
8657
8658 /*
8659 * SAX: end of the document processing.
8660 */
Daniel Veillard8d24cc12002-03-05 15:41:29 +00008661 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00008662 ctxt->sax->endDocument(ctxt->userData);
8663
8664 if (! ctxt->wellFormed) return(-1);
8665 return(0);
8666}
8667
Daniel Veillard73b013f2003-09-30 12:36:01 +00008668#ifdef LIBXML_PUSH_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00008669/************************************************************************
8670 * *
8671 * Progressive parsing interfaces *
8672 * *
8673 ************************************************************************/
8674
8675/**
8676 * xmlParseLookupSequence:
8677 * @ctxt: an XML parser context
8678 * @first: the first char to lookup
8679 * @next: the next char to lookup or zero
8680 * @third: the next char to lookup or zero
8681 *
8682 * Try to find if a sequence (first, next, third) or just (first next) or
8683 * (first) is available in the input stream.
8684 * This function has a side effect of (possibly) incrementing ctxt->checkIndex
8685 * to avoid rescanning sequences of bytes, it DOES change the state of the
8686 * parser, do not use liberally.
8687 *
8688 * Returns the index to the current parsing point if the full sequence
8689 * is available, -1 otherwise.
8690 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008691static int
Owen Taylor3473f882001-02-23 17:55:21 +00008692xmlParseLookupSequence(xmlParserCtxtPtr ctxt, xmlChar first,
8693 xmlChar next, xmlChar third) {
8694 int base, len;
8695 xmlParserInputPtr in;
8696 const xmlChar *buf;
8697
8698 in = ctxt->input;
8699 if (in == NULL) return(-1);
8700 base = in->cur - in->base;
8701 if (base < 0) return(-1);
8702 if (ctxt->checkIndex > base)
8703 base = ctxt->checkIndex;
8704 if (in->buf == NULL) {
8705 buf = in->base;
8706 len = in->length;
8707 } else {
8708 buf = in->buf->buffer->content;
8709 len = in->buf->buffer->use;
8710 }
8711 /* take into account the sequence length */
8712 if (third) len -= 2;
8713 else if (next) len --;
8714 for (;base < len;base++) {
8715 if (buf[base] == first) {
8716 if (third != 0) {
8717 if ((buf[base + 1] != next) ||
8718 (buf[base + 2] != third)) continue;
8719 } else if (next != 0) {
8720 if (buf[base + 1] != next) continue;
8721 }
8722 ctxt->checkIndex = 0;
8723#ifdef DEBUG_PUSH
8724 if (next == 0)
8725 xmlGenericError(xmlGenericErrorContext,
8726 "PP: lookup '%c' found at %d\n",
8727 first, base);
8728 else if (third == 0)
8729 xmlGenericError(xmlGenericErrorContext,
8730 "PP: lookup '%c%c' found at %d\n",
8731 first, next, base);
8732 else
8733 xmlGenericError(xmlGenericErrorContext,
8734 "PP: lookup '%c%c%c' found at %d\n",
8735 first, next, third, base);
8736#endif
8737 return(base - (in->cur - in->base));
8738 }
8739 }
8740 ctxt->checkIndex = base;
8741#ifdef DEBUG_PUSH
8742 if (next == 0)
8743 xmlGenericError(xmlGenericErrorContext,
8744 "PP: lookup '%c' failed\n", first);
8745 else if (third == 0)
8746 xmlGenericError(xmlGenericErrorContext,
8747 "PP: lookup '%c%c' failed\n", first, next);
8748 else
8749 xmlGenericError(xmlGenericErrorContext,
8750 "PP: lookup '%c%c%c' failed\n", first, next, third);
8751#endif
8752 return(-1);
8753}
8754
8755/**
Daniel Veillarda880b122003-04-21 21:36:41 +00008756 * xmlParseGetLasts:
8757 * @ctxt: an XML parser context
8758 * @lastlt: pointer to store the last '<' from the input
8759 * @lastgt: pointer to store the last '>' from the input
8760 *
8761 * Lookup the last < and > in the current chunk
8762 */
8763static void
8764xmlParseGetLasts(xmlParserCtxtPtr ctxt, const xmlChar **lastlt,
8765 const xmlChar **lastgt) {
8766 const xmlChar *tmp;
8767
8768 if ((ctxt == NULL) || (lastlt == NULL) || (lastgt == NULL)) {
8769 xmlGenericError(xmlGenericErrorContext,
8770 "Internal error: xmlParseGetLasts\n");
8771 return;
8772 }
8773 if ((ctxt->progressive == 1) && (ctxt->inputNr == 1)) {
8774 tmp = ctxt->input->end;
8775 tmp--;
8776 while ((tmp >= ctxt->input->base) && (*tmp != '<') &&
8777 (*tmp != '>')) tmp--;
8778 if (tmp < ctxt->input->base) {
8779 *lastlt = NULL;
8780 *lastgt = NULL;
8781 } else if (*tmp == '<') {
8782 *lastlt = tmp;
8783 tmp--;
8784 while ((tmp >= ctxt->input->base) && (*tmp != '>')) tmp--;
8785 if (tmp < ctxt->input->base)
8786 *lastgt = NULL;
8787 else
8788 *lastgt = tmp;
8789 } else {
8790 *lastgt = tmp;
8791 tmp--;
8792 while ((tmp >= ctxt->input->base) && (*tmp != '<')) tmp--;
8793 if (tmp < ctxt->input->base)
8794 *lastlt = NULL;
8795 else
8796 *lastlt = tmp;
8797 }
8798
8799 } else {
8800 *lastlt = NULL;
8801 *lastgt = NULL;
8802 }
8803}
8804/**
Owen Taylor3473f882001-02-23 17:55:21 +00008805 * xmlParseTryOrFinish:
8806 * @ctxt: an XML parser context
8807 * @terminate: last chunk indicator
8808 *
8809 * Try to progress on parsing
8810 *
8811 * Returns zero if no parsing was possible
8812 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00008813static int
Owen Taylor3473f882001-02-23 17:55:21 +00008814xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) {
8815 int ret = 0;
Daniel Veillardc82c57e2004-01-12 16:24:34 +00008816 int avail, tlen;
Owen Taylor3473f882001-02-23 17:55:21 +00008817 xmlChar cur, next;
Daniel Veillarda880b122003-04-21 21:36:41 +00008818 const xmlChar *lastlt, *lastgt;
Owen Taylor3473f882001-02-23 17:55:21 +00008819
8820#ifdef DEBUG_PUSH
8821 switch (ctxt->instate) {
8822 case XML_PARSER_EOF:
8823 xmlGenericError(xmlGenericErrorContext,
8824 "PP: try EOF\n"); break;
8825 case XML_PARSER_START:
8826 xmlGenericError(xmlGenericErrorContext,
8827 "PP: try START\n"); break;
8828 case XML_PARSER_MISC:
8829 xmlGenericError(xmlGenericErrorContext,
8830 "PP: try MISC\n");break;
8831 case XML_PARSER_COMMENT:
8832 xmlGenericError(xmlGenericErrorContext,
8833 "PP: try COMMENT\n");break;
8834 case XML_PARSER_PROLOG:
8835 xmlGenericError(xmlGenericErrorContext,
8836 "PP: try PROLOG\n");break;
8837 case XML_PARSER_START_TAG:
8838 xmlGenericError(xmlGenericErrorContext,
8839 "PP: try START_TAG\n");break;
8840 case XML_PARSER_CONTENT:
8841 xmlGenericError(xmlGenericErrorContext,
8842 "PP: try CONTENT\n");break;
8843 case XML_PARSER_CDATA_SECTION:
8844 xmlGenericError(xmlGenericErrorContext,
8845 "PP: try CDATA_SECTION\n");break;
8846 case XML_PARSER_END_TAG:
8847 xmlGenericError(xmlGenericErrorContext,
8848 "PP: try END_TAG\n");break;
8849 case XML_PARSER_ENTITY_DECL:
8850 xmlGenericError(xmlGenericErrorContext,
8851 "PP: try ENTITY_DECL\n");break;
8852 case XML_PARSER_ENTITY_VALUE:
8853 xmlGenericError(xmlGenericErrorContext,
8854 "PP: try ENTITY_VALUE\n");break;
8855 case XML_PARSER_ATTRIBUTE_VALUE:
8856 xmlGenericError(xmlGenericErrorContext,
8857 "PP: try ATTRIBUTE_VALUE\n");break;
8858 case XML_PARSER_DTD:
8859 xmlGenericError(xmlGenericErrorContext,
8860 "PP: try DTD\n");break;
8861 case XML_PARSER_EPILOG:
8862 xmlGenericError(xmlGenericErrorContext,
8863 "PP: try EPILOG\n");break;
8864 case XML_PARSER_PI:
8865 xmlGenericError(xmlGenericErrorContext,
8866 "PP: try PI\n");break;
8867 case XML_PARSER_IGNORE:
8868 xmlGenericError(xmlGenericErrorContext,
8869 "PP: try IGNORE\n");break;
8870 }
8871#endif
8872
Daniel Veillard198c1bf2003-10-20 17:07:41 +00008873 if ((ctxt->input != NULL) &&
8874 (ctxt->input->cur - ctxt->input->base > 4096)) {
Daniel Veillarda880b122003-04-21 21:36:41 +00008875 xmlSHRINK(ctxt);
8876 ctxt->checkIndex = 0;
8877 }
8878 xmlParseGetLasts(ctxt, &lastlt, &lastgt);
Aleksey Sanine48a3182002-05-09 18:20:01 +00008879
Daniel Veillarda880b122003-04-21 21:36:41 +00008880 while (1) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00008881 if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
8882 return(0);
8883
8884
Owen Taylor3473f882001-02-23 17:55:21 +00008885 /*
8886 * Pop-up of finished entities.
8887 */
8888 while ((RAW == 0) && (ctxt->inputNr > 1))
8889 xmlPopInput(ctxt);
8890
Daniel Veillard198c1bf2003-10-20 17:07:41 +00008891 if (ctxt->input == NULL) break;
Owen Taylor3473f882001-02-23 17:55:21 +00008892 if (ctxt->input->buf == NULL)
Daniel Veillarda880b122003-04-21 21:36:41 +00008893 avail = ctxt->input->length -
8894 (ctxt->input->cur - ctxt->input->base);
Daniel Veillard158a4d22002-02-20 22:17:58 +00008895 else {
8896 /*
8897 * If we are operating on converted input, try to flush
8898 * remainng chars to avoid them stalling in the non-converted
8899 * buffer.
8900 */
8901 if ((ctxt->input->buf->raw != NULL) &&
8902 (ctxt->input->buf->raw->use > 0)) {
8903 int base = ctxt->input->base -
8904 ctxt->input->buf->buffer->content;
8905 int current = ctxt->input->cur - ctxt->input->base;
8906
8907 xmlParserInputBufferPush(ctxt->input->buf, 0, "");
8908 ctxt->input->base = ctxt->input->buf->buffer->content + base;
8909 ctxt->input->cur = ctxt->input->base + current;
8910 ctxt->input->end =
8911 &ctxt->input->buf->buffer->content[
8912 ctxt->input->buf->buffer->use];
8913 }
8914 avail = ctxt->input->buf->buffer->use -
8915 (ctxt->input->cur - ctxt->input->base);
8916 }
Owen Taylor3473f882001-02-23 17:55:21 +00008917 if (avail < 1)
8918 goto done;
8919 switch (ctxt->instate) {
8920 case XML_PARSER_EOF:
8921 /*
8922 * Document parsing is done !
8923 */
8924 goto done;
8925 case XML_PARSER_START:
Daniel Veillard0e4cd172001-06-28 12:13:56 +00008926 if (ctxt->charset == XML_CHAR_ENCODING_NONE) {
8927 xmlChar start[4];
8928 xmlCharEncoding enc;
8929
8930 /*
8931 * Very first chars read from the document flow.
8932 */
8933 if (avail < 4)
8934 goto done;
8935
8936 /*
8937 * Get the 4 first bytes and decode the charset
8938 * if enc != XML_CHAR_ENCODING_NONE
8939 * plug some encoding conversion routines.
8940 */
8941 start[0] = RAW;
8942 start[1] = NXT(1);
8943 start[2] = NXT(2);
8944 start[3] = NXT(3);
8945 enc = xmlDetectCharEncoding(start, 4);
8946 if (enc != XML_CHAR_ENCODING_NONE) {
8947 xmlSwitchEncoding(ctxt, enc);
8948 }
8949 break;
8950 }
Owen Taylor3473f882001-02-23 17:55:21 +00008951
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00008952 if (avail < 2)
8953 goto done;
Owen Taylor3473f882001-02-23 17:55:21 +00008954 cur = ctxt->input->cur[0];
8955 next = ctxt->input->cur[1];
8956 if (cur == 0) {
8957 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
8958 ctxt->sax->setDocumentLocator(ctxt->userData,
8959 &xmlDefaultSAXLocator);
Daniel Veillard1afc9f32003-09-13 12:44:05 +00008960 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00008961 ctxt->instate = XML_PARSER_EOF;
8962#ifdef DEBUG_PUSH
8963 xmlGenericError(xmlGenericErrorContext,
8964 "PP: entering EOF\n");
8965#endif
8966 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
8967 ctxt->sax->endDocument(ctxt->userData);
8968 goto done;
8969 }
8970 if ((cur == '<') && (next == '?')) {
8971 /* PI or XML decl */
8972 if (avail < 5) return(ret);
8973 if ((!terminate) &&
8974 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
8975 return(ret);
8976 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
8977 ctxt->sax->setDocumentLocator(ctxt->userData,
8978 &xmlDefaultSAXLocator);
8979 if ((ctxt->input->cur[2] == 'x') &&
8980 (ctxt->input->cur[3] == 'm') &&
8981 (ctxt->input->cur[4] == 'l') &&
William M. Brack76e95df2003-10-18 16:20:14 +00008982 (IS_BLANK_CH(ctxt->input->cur[5]))) {
Owen Taylor3473f882001-02-23 17:55:21 +00008983 ret += 5;
8984#ifdef DEBUG_PUSH
8985 xmlGenericError(xmlGenericErrorContext,
8986 "PP: Parsing XML Decl\n");
8987#endif
8988 xmlParseXMLDecl(ctxt);
8989 if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
8990 /*
8991 * The XML REC instructs us to stop parsing right
8992 * here
8993 */
8994 ctxt->instate = XML_PARSER_EOF;
8995 return(0);
8996 }
8997 ctxt->standalone = ctxt->input->standalone;
8998 if ((ctxt->encoding == NULL) &&
8999 (ctxt->input->encoding != NULL))
9000 ctxt->encoding = xmlStrdup(ctxt->input->encoding);
9001 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
9002 (!ctxt->disableSAX))
9003 ctxt->sax->startDocument(ctxt->userData);
9004 ctxt->instate = XML_PARSER_MISC;
9005#ifdef DEBUG_PUSH
9006 xmlGenericError(xmlGenericErrorContext,
9007 "PP: entering MISC\n");
9008#endif
9009 } else {
9010 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
9011 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
9012 (!ctxt->disableSAX))
9013 ctxt->sax->startDocument(ctxt->userData);
9014 ctxt->instate = XML_PARSER_MISC;
9015#ifdef DEBUG_PUSH
9016 xmlGenericError(xmlGenericErrorContext,
9017 "PP: entering MISC\n");
9018#endif
9019 }
9020 } else {
9021 if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
9022 ctxt->sax->setDocumentLocator(ctxt->userData,
9023 &xmlDefaultSAXLocator);
9024 ctxt->version = xmlCharStrdup(XML_DEFAULT_VERSION);
9025 if ((ctxt->sax) && (ctxt->sax->startDocument) &&
9026 (!ctxt->disableSAX))
9027 ctxt->sax->startDocument(ctxt->userData);
9028 ctxt->instate = XML_PARSER_MISC;
9029#ifdef DEBUG_PUSH
9030 xmlGenericError(xmlGenericErrorContext,
9031 "PP: entering MISC\n");
9032#endif
9033 }
9034 break;
Daniel Veillarda880b122003-04-21 21:36:41 +00009035 case XML_PARSER_START_TAG: {
Daniel Veillarde57ec792003-09-10 10:50:59 +00009036 const xmlChar *name;
9037 const xmlChar *prefix;
9038 const xmlChar *URI;
9039 int nsNr = ctxt->nsNr;
Daniel Veillarda880b122003-04-21 21:36:41 +00009040
9041 if ((avail < 2) && (ctxt->inputNr == 1))
9042 goto done;
9043 cur = ctxt->input->cur[0];
9044 if (cur != '<') {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009045 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL);
Daniel Veillarda880b122003-04-21 21:36:41 +00009046 ctxt->instate = XML_PARSER_EOF;
Daniel Veillarda880b122003-04-21 21:36:41 +00009047 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
9048 ctxt->sax->endDocument(ctxt->userData);
9049 goto done;
9050 }
9051 if (!terminate) {
9052 if (ctxt->progressive) {
Daniel Veillardb3744002004-02-18 14:28:22 +00009053 /* > can be found unescaped in attribute values */
9054 if ((lastlt == NULL) || (ctxt->input->cur >= lastlt))
Daniel Veillarda880b122003-04-21 21:36:41 +00009055 goto done;
9056 } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) {
9057 goto done;
9058 }
9059 }
9060 if (ctxt->spaceNr == 0)
9061 spacePush(ctxt, -1);
9062 else
9063 spacePush(ctxt, *ctxt->space);
Daniel Veillard81273902003-09-30 00:43:48 +00009064#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009065 if (ctxt->sax2)
Daniel Veillard81273902003-09-30 00:43:48 +00009066#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillardc82c57e2004-01-12 16:24:34 +00009067 name = xmlParseStartTag2(ctxt, &prefix, &URI, &tlen);
Daniel Veillard81273902003-09-30 00:43:48 +00009068#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009069 else
9070 name = xmlParseStartTag(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00009071#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarda880b122003-04-21 21:36:41 +00009072 if (name == NULL) {
9073 spacePop(ctxt);
9074 ctxt->instate = XML_PARSER_EOF;
Daniel Veillarda880b122003-04-21 21:36:41 +00009075 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
9076 ctxt->sax->endDocument(ctxt->userData);
9077 goto done;
9078 }
Daniel Veillard4432df22003-09-28 18:58:27 +00009079#ifdef LIBXML_VALID_ENABLED
Daniel Veillarda880b122003-04-21 21:36:41 +00009080 /*
9081 * [ VC: Root Element Type ]
9082 * The Name in the document type declaration must match
9083 * the element type of the root element.
9084 */
9085 if (ctxt->validate && ctxt->wellFormed && ctxt->myDoc &&
9086 ctxt->node && (ctxt->node == ctxt->myDoc->children))
9087 ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
Daniel Veillard4432df22003-09-28 18:58:27 +00009088#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda880b122003-04-21 21:36:41 +00009089
9090 /*
9091 * Check for an Empty Element.
9092 */
9093 if ((RAW == '/') && (NXT(1) == '>')) {
9094 SKIP(2);
Daniel Veillarde57ec792003-09-10 10:50:59 +00009095
9096 if (ctxt->sax2) {
9097 if ((ctxt->sax != NULL) &&
9098 (ctxt->sax->endElementNs != NULL) &&
9099 (!ctxt->disableSAX))
9100 ctxt->sax->endElementNs(ctxt->userData, name,
9101 prefix, URI);
Daniel Veillard81273902003-09-30 00:43:48 +00009102#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009103 } else {
9104 if ((ctxt->sax != NULL) &&
9105 (ctxt->sax->endElement != NULL) &&
9106 (!ctxt->disableSAX))
9107 ctxt->sax->endElement(ctxt->userData, name);
Daniel Veillard81273902003-09-30 00:43:48 +00009108#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarda880b122003-04-21 21:36:41 +00009109 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009110 spacePop(ctxt);
9111 if (ctxt->nameNr == 0) {
Daniel Veillarda880b122003-04-21 21:36:41 +00009112 ctxt->instate = XML_PARSER_EPILOG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009113 } else {
9114 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillarda880b122003-04-21 21:36:41 +00009115 }
9116 break;
9117 }
9118 if (RAW == '>') {
9119 NEXT;
9120 } else {
Daniel Veillardbc92eca2003-09-15 09:48:06 +00009121 xmlFatalErrMsgStr(ctxt, XML_ERR_GT_REQUIRED,
Daniel Veillarda880b122003-04-21 21:36:41 +00009122 "Couldn't find end of Start Tag %s\n",
9123 name);
Daniel Veillarda880b122003-04-21 21:36:41 +00009124 nodePop(ctxt);
Daniel Veillarda880b122003-04-21 21:36:41 +00009125 spacePop(ctxt);
Daniel Veillarda880b122003-04-21 21:36:41 +00009126 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009127 if (ctxt->sax2)
9128 nameNsPush(ctxt, name, prefix, URI, ctxt->nsNr - nsNr);
Daniel Veillard81273902003-09-30 00:43:48 +00009129#ifdef LIBXML_SAX1_ENABLED
Daniel Veillarde57ec792003-09-10 10:50:59 +00009130 else
9131 namePush(ctxt, name);
Daniel Veillard81273902003-09-30 00:43:48 +00009132#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00009133
Daniel Veillarda880b122003-04-21 21:36:41 +00009134 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillarda880b122003-04-21 21:36:41 +00009135 break;
9136 }
9137 case XML_PARSER_CONTENT: {
9138 const xmlChar *test;
9139 unsigned int cons;
9140 if ((avail < 2) && (ctxt->inputNr == 1))
9141 goto done;
9142 cur = ctxt->input->cur[0];
9143 next = ctxt->input->cur[1];
9144
9145 test = CUR_PTR;
9146 cons = ctxt->input->consumed;
9147 if ((cur == '<') && (next == '/')) {
9148 ctxt->instate = XML_PARSER_END_TAG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009149 break;
9150 } else if ((cur == '<') && (next == '?')) {
9151 if ((!terminate) &&
9152 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9153 goto done;
Daniel Veillarda880b122003-04-21 21:36:41 +00009154 xmlParsePI(ctxt);
9155 } else if ((cur == '<') && (next != '!')) {
9156 ctxt->instate = XML_PARSER_START_TAG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009157 break;
9158 } else if ((cur == '<') && (next == '!') &&
9159 (ctxt->input->cur[2] == '-') &&
9160 (ctxt->input->cur[3] == '-')) {
9161 if ((!terminate) &&
9162 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9163 goto done;
Daniel Veillarda880b122003-04-21 21:36:41 +00009164 xmlParseComment(ctxt);
9165 ctxt->instate = XML_PARSER_CONTENT;
9166 } else if ((cur == '<') && (ctxt->input->cur[1] == '!') &&
9167 (ctxt->input->cur[2] == '[') &&
9168 (ctxt->input->cur[3] == 'C') &&
9169 (ctxt->input->cur[4] == 'D') &&
9170 (ctxt->input->cur[5] == 'A') &&
9171 (ctxt->input->cur[6] == 'T') &&
9172 (ctxt->input->cur[7] == 'A') &&
9173 (ctxt->input->cur[8] == '[')) {
9174 SKIP(9);
9175 ctxt->instate = XML_PARSER_CDATA_SECTION;
Daniel Veillarda880b122003-04-21 21:36:41 +00009176 break;
9177 } else if ((cur == '<') && (next == '!') &&
9178 (avail < 9)) {
9179 goto done;
9180 } else if (cur == '&') {
9181 if ((!terminate) &&
9182 (xmlParseLookupSequence(ctxt, ';', 0, 0) < 0))
9183 goto done;
Daniel Veillarda880b122003-04-21 21:36:41 +00009184 xmlParseReference(ctxt);
9185 } else {
9186 /* TODO Avoid the extra copy, handle directly !!! */
9187 /*
9188 * Goal of the following test is:
9189 * - minimize calls to the SAX 'character' callback
9190 * when they are mergeable
9191 * - handle an problem for isBlank when we only parse
9192 * a sequence of blank chars and the next one is
9193 * not available to check against '<' presence.
9194 * - tries to homogenize the differences in SAX
9195 * callbacks between the push and pull versions
9196 * of the parser.
9197 */
9198 if ((ctxt->inputNr == 1) &&
9199 (avail < XML_PARSER_BIG_BUFFER_SIZE)) {
9200 if (!terminate) {
9201 if (ctxt->progressive) {
9202 if ((lastlt == NULL) ||
9203 (ctxt->input->cur > lastlt))
9204 goto done;
9205 } else if (xmlParseLookupSequence(ctxt,
9206 '<', 0, 0) < 0) {
9207 goto done;
9208 }
9209 }
9210 }
9211 ctxt->checkIndex = 0;
Daniel Veillarda880b122003-04-21 21:36:41 +00009212 xmlParseCharData(ctxt, 0);
9213 }
9214 /*
9215 * Pop-up of finished entities.
9216 */
9217 while ((RAW == 0) && (ctxt->inputNr > 1))
9218 xmlPopInput(ctxt);
9219 if ((cons == ctxt->input->consumed) && (test == CUR_PTR)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009220 xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR,
9221 "detected an error in element content\n");
Daniel Veillarda880b122003-04-21 21:36:41 +00009222 ctxt->instate = XML_PARSER_EOF;
9223 break;
9224 }
9225 break;
9226 }
9227 case XML_PARSER_END_TAG:
9228 if (avail < 2)
9229 goto done;
9230 if (!terminate) {
9231 if (ctxt->progressive) {
9232 if ((lastgt == NULL) || (ctxt->input->cur > lastgt))
9233 goto done;
9234 } else if (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0) {
9235 goto done;
9236 }
9237 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009238 if (ctxt->sax2) {
9239 xmlParseEndTag2(ctxt,
9240 (void *) ctxt->pushTab[ctxt->nameNr * 3 - 3],
9241 (void *) ctxt->pushTab[ctxt->nameNr * 3 - 2], 0,
Daniel Veillardc82c57e2004-01-12 16:24:34 +00009242 (int) (long) ctxt->pushTab[ctxt->nameNr * 3 - 1], 0);
Daniel Veillarde57ec792003-09-10 10:50:59 +00009243 nameNsPop(ctxt);
Daniel Veillard81273902003-09-30 00:43:48 +00009244 }
9245#ifdef LIBXML_SAX1_ENABLED
9246 else
Daniel Veillarde57ec792003-09-10 10:50:59 +00009247 xmlParseEndTag1(ctxt, 0);
Daniel Veillard81273902003-09-30 00:43:48 +00009248#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde57ec792003-09-10 10:50:59 +00009249 if (ctxt->nameNr == 0) {
Daniel Veillarda880b122003-04-21 21:36:41 +00009250 ctxt->instate = XML_PARSER_EPILOG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009251 } else {
9252 ctxt->instate = XML_PARSER_CONTENT;
Daniel Veillarda880b122003-04-21 21:36:41 +00009253 }
9254 break;
9255 case XML_PARSER_CDATA_SECTION: {
9256 /*
9257 * The Push mode need to have the SAX callback for
9258 * cdataBlock merge back contiguous callbacks.
9259 */
9260 int base;
9261
9262 base = xmlParseLookupSequence(ctxt, ']', ']', '>');
9263 if (base < 0) {
9264 if (avail >= XML_PARSER_BIG_BUFFER_SIZE + 2) {
9265 if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
9266 if (ctxt->sax->cdataBlock != NULL)
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00009267 ctxt->sax->cdataBlock(ctxt->userData,
9268 ctxt->input->cur,
9269 XML_PARSER_BIG_BUFFER_SIZE);
9270 else if (ctxt->sax->characters != NULL)
9271 ctxt->sax->characters(ctxt->userData,
9272 ctxt->input->cur,
Daniel Veillarda880b122003-04-21 21:36:41 +00009273 XML_PARSER_BIG_BUFFER_SIZE);
9274 }
Daniel Veillard0b787f32004-03-26 17:29:53 +00009275 SKIPL(XML_PARSER_BIG_BUFFER_SIZE);
Daniel Veillarda880b122003-04-21 21:36:41 +00009276 ctxt->checkIndex = 0;
9277 }
9278 goto done;
9279 } else {
9280 if ((ctxt->sax != NULL) && (base > 0) &&
9281 (!ctxt->disableSAX)) {
9282 if (ctxt->sax->cdataBlock != NULL)
9283 ctxt->sax->cdataBlock(ctxt->userData,
9284 ctxt->input->cur, base);
Daniel Veillardd9d32ae2003-07-05 20:32:43 +00009285 else if (ctxt->sax->characters != NULL)
9286 ctxt->sax->characters(ctxt->userData,
9287 ctxt->input->cur, base);
Daniel Veillarda880b122003-04-21 21:36:41 +00009288 }
Daniel Veillard0b787f32004-03-26 17:29:53 +00009289 SKIPL(base + 3);
Daniel Veillarda880b122003-04-21 21:36:41 +00009290 ctxt->checkIndex = 0;
9291 ctxt->instate = XML_PARSER_CONTENT;
9292#ifdef DEBUG_PUSH
9293 xmlGenericError(xmlGenericErrorContext,
9294 "PP: entering CONTENT\n");
9295#endif
9296 }
9297 break;
9298 }
Owen Taylor3473f882001-02-23 17:55:21 +00009299 case XML_PARSER_MISC:
9300 SKIP_BLANKS;
9301 if (ctxt->input->buf == NULL)
Daniel Veillarda880b122003-04-21 21:36:41 +00009302 avail = ctxt->input->length -
9303 (ctxt->input->cur - ctxt->input->base);
Owen Taylor3473f882001-02-23 17:55:21 +00009304 else
Daniel Veillarda880b122003-04-21 21:36:41 +00009305 avail = ctxt->input->buf->buffer->use -
9306 (ctxt->input->cur - ctxt->input->base);
Owen Taylor3473f882001-02-23 17:55:21 +00009307 if (avail < 2)
9308 goto done;
9309 cur = ctxt->input->cur[0];
9310 next = ctxt->input->cur[1];
9311 if ((cur == '<') && (next == '?')) {
9312 if ((!terminate) &&
9313 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9314 goto done;
9315#ifdef DEBUG_PUSH
9316 xmlGenericError(xmlGenericErrorContext,
9317 "PP: Parsing PI\n");
9318#endif
9319 xmlParsePI(ctxt);
9320 } else if ((cur == '<') && (next == '!') &&
Daniel Veillarda880b122003-04-21 21:36:41 +00009321 (ctxt->input->cur[2] == '-') &&
9322 (ctxt->input->cur[3] == '-')) {
Owen Taylor3473f882001-02-23 17:55:21 +00009323 if ((!terminate) &&
9324 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9325 goto done;
9326#ifdef DEBUG_PUSH
9327 xmlGenericError(xmlGenericErrorContext,
9328 "PP: Parsing Comment\n");
9329#endif
9330 xmlParseComment(ctxt);
9331 ctxt->instate = XML_PARSER_MISC;
9332 } else if ((cur == '<') && (next == '!') &&
Daniel Veillarda880b122003-04-21 21:36:41 +00009333 (ctxt->input->cur[2] == 'D') &&
9334 (ctxt->input->cur[3] == 'O') &&
9335 (ctxt->input->cur[4] == 'C') &&
9336 (ctxt->input->cur[5] == 'T') &&
9337 (ctxt->input->cur[6] == 'Y') &&
9338 (ctxt->input->cur[7] == 'P') &&
Owen Taylor3473f882001-02-23 17:55:21 +00009339 (ctxt->input->cur[8] == 'E')) {
9340 if ((!terminate) &&
9341 (xmlParseLookupSequence(ctxt, '>', 0, 0) < 0))
9342 goto done;
9343#ifdef DEBUG_PUSH
9344 xmlGenericError(xmlGenericErrorContext,
9345 "PP: Parsing internal subset\n");
9346#endif
9347 ctxt->inSubset = 1;
9348 xmlParseDocTypeDecl(ctxt);
9349 if (RAW == '[') {
9350 ctxt->instate = XML_PARSER_DTD;
9351#ifdef DEBUG_PUSH
9352 xmlGenericError(xmlGenericErrorContext,
9353 "PP: entering DTD\n");
9354#endif
9355 } else {
9356 /*
9357 * Create and update the external subset.
9358 */
9359 ctxt->inSubset = 2;
9360 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
9361 (ctxt->sax->externalSubset != NULL))
9362 ctxt->sax->externalSubset(ctxt->userData,
9363 ctxt->intSubName, ctxt->extSubSystem,
9364 ctxt->extSubURI);
9365 ctxt->inSubset = 0;
9366 ctxt->instate = XML_PARSER_PROLOG;
9367#ifdef DEBUG_PUSH
9368 xmlGenericError(xmlGenericErrorContext,
9369 "PP: entering PROLOG\n");
9370#endif
9371 }
9372 } else if ((cur == '<') && (next == '!') &&
9373 (avail < 9)) {
9374 goto done;
9375 } else {
9376 ctxt->instate = XML_PARSER_START_TAG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009377 ctxt->progressive = 1;
9378 xmlParseGetLasts(ctxt, &lastlt, &lastgt);
Owen Taylor3473f882001-02-23 17:55:21 +00009379#ifdef DEBUG_PUSH
9380 xmlGenericError(xmlGenericErrorContext,
9381 "PP: entering START_TAG\n");
9382#endif
9383 }
9384 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009385 case XML_PARSER_PROLOG:
9386 SKIP_BLANKS;
9387 if (ctxt->input->buf == NULL)
9388 avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base);
9389 else
9390 avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base);
9391 if (avail < 2)
9392 goto done;
9393 cur = ctxt->input->cur[0];
9394 next = ctxt->input->cur[1];
9395 if ((cur == '<') && (next == '?')) {
9396 if ((!terminate) &&
9397 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9398 goto done;
9399#ifdef DEBUG_PUSH
9400 xmlGenericError(xmlGenericErrorContext,
9401 "PP: Parsing PI\n");
9402#endif
9403 xmlParsePI(ctxt);
9404 } else if ((cur == '<') && (next == '!') &&
9405 (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) {
9406 if ((!terminate) &&
9407 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9408 goto done;
9409#ifdef DEBUG_PUSH
9410 xmlGenericError(xmlGenericErrorContext,
9411 "PP: Parsing Comment\n");
9412#endif
9413 xmlParseComment(ctxt);
9414 ctxt->instate = XML_PARSER_PROLOG;
9415 } else if ((cur == '<') && (next == '!') &&
9416 (avail < 4)) {
9417 goto done;
9418 } else {
9419 ctxt->instate = XML_PARSER_START_TAG;
Daniel Veillarda880b122003-04-21 21:36:41 +00009420 ctxt->progressive = 1;
9421 xmlParseGetLasts(ctxt, &lastlt, &lastgt);
Owen Taylor3473f882001-02-23 17:55:21 +00009422#ifdef DEBUG_PUSH
9423 xmlGenericError(xmlGenericErrorContext,
9424 "PP: entering START_TAG\n");
9425#endif
9426 }
9427 break;
9428 case XML_PARSER_EPILOG:
9429 SKIP_BLANKS;
9430 if (ctxt->input->buf == NULL)
9431 avail = ctxt->input->length - (ctxt->input->cur - ctxt->input->base);
9432 else
9433 avail = ctxt->input->buf->buffer->use - (ctxt->input->cur - ctxt->input->base);
9434 if (avail < 2)
9435 goto done;
9436 cur = ctxt->input->cur[0];
9437 next = ctxt->input->cur[1];
9438 if ((cur == '<') && (next == '?')) {
9439 if ((!terminate) &&
9440 (xmlParseLookupSequence(ctxt, '?', '>', 0) < 0))
9441 goto done;
9442#ifdef DEBUG_PUSH
9443 xmlGenericError(xmlGenericErrorContext,
9444 "PP: Parsing PI\n");
9445#endif
9446 xmlParsePI(ctxt);
9447 ctxt->instate = XML_PARSER_EPILOG;
9448 } else if ((cur == '<') && (next == '!') &&
9449 (ctxt->input->cur[2] == '-') && (ctxt->input->cur[3] == '-')) {
9450 if ((!terminate) &&
9451 (xmlParseLookupSequence(ctxt, '-', '-', '>') < 0))
9452 goto done;
9453#ifdef DEBUG_PUSH
9454 xmlGenericError(xmlGenericErrorContext,
9455 "PP: Parsing Comment\n");
9456#endif
9457 xmlParseComment(ctxt);
9458 ctxt->instate = XML_PARSER_EPILOG;
9459 } else if ((cur == '<') && (next == '!') &&
9460 (avail < 4)) {
9461 goto done;
9462 } else {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009463 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00009464 ctxt->instate = XML_PARSER_EOF;
9465#ifdef DEBUG_PUSH
9466 xmlGenericError(xmlGenericErrorContext,
9467 "PP: entering EOF\n");
9468#endif
Daniel Veillard8d24cc12002-03-05 15:41:29 +00009469 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00009470 ctxt->sax->endDocument(ctxt->userData);
9471 goto done;
9472 }
9473 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009474 case XML_PARSER_DTD: {
9475 /*
9476 * Sorry but progressive parsing of the internal subset
9477 * is not expected to be supported. We first check that
9478 * the full content of the internal subset is available and
9479 * the parsing is launched only at that point.
9480 * Internal subset ends up with "']' S? '>'" in an unescaped
9481 * section and not in a ']]>' sequence which are conditional
9482 * sections (whoever argued to keep that crap in XML deserve
9483 * a place in hell !).
9484 */
9485 int base, i;
9486 xmlChar *buf;
9487 xmlChar quote = 0;
9488
9489 base = ctxt->input->cur - ctxt->input->base;
9490 if (base < 0) return(0);
9491 if (ctxt->checkIndex > base)
9492 base = ctxt->checkIndex;
9493 buf = ctxt->input->buf->buffer->content;
9494 for (;(unsigned int) base < ctxt->input->buf->buffer->use;
9495 base++) {
9496 if (quote != 0) {
9497 if (buf[base] == quote)
9498 quote = 0;
9499 continue;
9500 }
Daniel Veillard036143b2004-02-12 11:57:52 +00009501 if ((quote == 0) && (buf[base] == '<')) {
9502 int found = 0;
9503 /* special handling of comments */
9504 if (((unsigned int) base + 4 <
9505 ctxt->input->buf->buffer->use) &&
9506 (buf[base + 1] == '!') &&
9507 (buf[base + 2] == '-') &&
9508 (buf[base + 3] == '-')) {
9509 for (;(unsigned int) base + 3 <
9510 ctxt->input->buf->buffer->use; base++) {
9511 if ((buf[base] == '-') &&
9512 (buf[base + 1] == '-') &&
9513 (buf[base + 2] == '>')) {
9514 found = 1;
9515 base += 2;
9516 break;
9517 }
9518 }
9519 if (!found)
9520 break;
9521 continue;
9522 }
9523 }
Owen Taylor3473f882001-02-23 17:55:21 +00009524 if (buf[base] == '"') {
9525 quote = '"';
9526 continue;
9527 }
9528 if (buf[base] == '\'') {
9529 quote = '\'';
9530 continue;
9531 }
9532 if (buf[base] == ']') {
9533 if ((unsigned int) base +1 >=
9534 ctxt->input->buf->buffer->use)
9535 break;
9536 if (buf[base + 1] == ']') {
9537 /* conditional crap, skip both ']' ! */
9538 base++;
9539 continue;
9540 }
9541 for (i = 0;
9542 (unsigned int) base + i < ctxt->input->buf->buffer->use;
9543 i++) {
9544 if (buf[base + i] == '>')
9545 goto found_end_int_subset;
9546 }
9547 break;
9548 }
9549 }
9550 /*
9551 * We didn't found the end of the Internal subset
9552 */
9553 if (quote == 0)
9554 ctxt->checkIndex = base;
9555#ifdef DEBUG_PUSH
9556 if (next == 0)
9557 xmlGenericError(xmlGenericErrorContext,
9558 "PP: lookup of int subset end filed\n");
9559#endif
9560 goto done;
9561
9562found_end_int_subset:
9563 xmlParseInternalSubset(ctxt);
9564 ctxt->inSubset = 2;
9565 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
9566 (ctxt->sax->externalSubset != NULL))
9567 ctxt->sax->externalSubset(ctxt->userData, ctxt->intSubName,
9568 ctxt->extSubSystem, ctxt->extSubURI);
9569 ctxt->inSubset = 0;
9570 ctxt->instate = XML_PARSER_PROLOG;
9571 ctxt->checkIndex = 0;
9572#ifdef DEBUG_PUSH
9573 xmlGenericError(xmlGenericErrorContext,
9574 "PP: entering PROLOG\n");
9575#endif
9576 break;
9577 }
9578 case XML_PARSER_COMMENT:
9579 xmlGenericError(xmlGenericErrorContext,
9580 "PP: internal error, state == COMMENT\n");
9581 ctxt->instate = XML_PARSER_CONTENT;
9582#ifdef DEBUG_PUSH
9583 xmlGenericError(xmlGenericErrorContext,
9584 "PP: entering CONTENT\n");
9585#endif
9586 break;
Daniel Veillarda880b122003-04-21 21:36:41 +00009587 case XML_PARSER_IGNORE:
9588 xmlGenericError(xmlGenericErrorContext,
9589 "PP: internal error, state == IGNORE");
9590 ctxt->instate = XML_PARSER_DTD;
9591#ifdef DEBUG_PUSH
9592 xmlGenericError(xmlGenericErrorContext,
9593 "PP: entering DTD\n");
9594#endif
9595 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009596 case XML_PARSER_PI:
9597 xmlGenericError(xmlGenericErrorContext,
9598 "PP: internal error, state == PI\n");
9599 ctxt->instate = XML_PARSER_CONTENT;
9600#ifdef DEBUG_PUSH
9601 xmlGenericError(xmlGenericErrorContext,
9602 "PP: entering CONTENT\n");
9603#endif
9604 break;
9605 case XML_PARSER_ENTITY_DECL:
9606 xmlGenericError(xmlGenericErrorContext,
9607 "PP: internal error, state == ENTITY_DECL\n");
9608 ctxt->instate = XML_PARSER_DTD;
9609#ifdef DEBUG_PUSH
9610 xmlGenericError(xmlGenericErrorContext,
9611 "PP: entering DTD\n");
9612#endif
9613 break;
9614 case XML_PARSER_ENTITY_VALUE:
9615 xmlGenericError(xmlGenericErrorContext,
9616 "PP: internal error, state == ENTITY_VALUE\n");
9617 ctxt->instate = XML_PARSER_CONTENT;
9618#ifdef DEBUG_PUSH
9619 xmlGenericError(xmlGenericErrorContext,
9620 "PP: entering DTD\n");
9621#endif
9622 break;
9623 case XML_PARSER_ATTRIBUTE_VALUE:
9624 xmlGenericError(xmlGenericErrorContext,
9625 "PP: internal error, state == ATTRIBUTE_VALUE\n");
9626 ctxt->instate = XML_PARSER_START_TAG;
9627#ifdef DEBUG_PUSH
9628 xmlGenericError(xmlGenericErrorContext,
9629 "PP: entering START_TAG\n");
9630#endif
9631 break;
9632 case XML_PARSER_SYSTEM_LITERAL:
9633 xmlGenericError(xmlGenericErrorContext,
9634 "PP: internal error, state == SYSTEM_LITERAL\n");
9635 ctxt->instate = XML_PARSER_START_TAG;
9636#ifdef DEBUG_PUSH
9637 xmlGenericError(xmlGenericErrorContext,
9638 "PP: entering START_TAG\n");
9639#endif
9640 break;
Daniel Veillard4a7ae502002-02-18 19:18:17 +00009641 case XML_PARSER_PUBLIC_LITERAL:
9642 xmlGenericError(xmlGenericErrorContext,
9643 "PP: internal error, state == PUBLIC_LITERAL\n");
9644 ctxt->instate = XML_PARSER_START_TAG;
9645#ifdef DEBUG_PUSH
9646 xmlGenericError(xmlGenericErrorContext,
9647 "PP: entering START_TAG\n");
9648#endif
9649 break;
Owen Taylor3473f882001-02-23 17:55:21 +00009650 }
9651 }
9652done:
9653#ifdef DEBUG_PUSH
9654 xmlGenericError(xmlGenericErrorContext, "PP: done %d\n", ret);
9655#endif
9656 return(ret);
9657}
9658
9659/**
Owen Taylor3473f882001-02-23 17:55:21 +00009660 * xmlParseChunk:
9661 * @ctxt: an XML parser context
9662 * @chunk: an char array
9663 * @size: the size in byte of the chunk
9664 * @terminate: last chunk indicator
9665 *
9666 * Parse a Chunk of memory
9667 *
9668 * Returns zero if no error, the xmlParserErrors otherwise.
9669 */
9670int
9671xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size,
9672 int terminate) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009673 if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
9674 return(ctxt->errNo);
Daniel Veillard309f81d2003-09-23 09:02:53 +00009675 if (ctxt->instate == XML_PARSER_START)
9676 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00009677 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
9678 (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) {
9679 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
9680 int cur = ctxt->input->cur - ctxt->input->base;
9681
9682 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
9683 ctxt->input->base = ctxt->input->buf->buffer->content + base;
9684 ctxt->input->cur = ctxt->input->base + cur;
Daniel Veillard48b2f892001-02-25 16:11:03 +00009685 ctxt->input->end =
9686 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00009687#ifdef DEBUG_PUSH
9688 xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
9689#endif
9690
Owen Taylor3473f882001-02-23 17:55:21 +00009691 } else if (ctxt->instate != XML_PARSER_EOF) {
9692 if ((ctxt->input != NULL) && ctxt->input->buf != NULL) {
9693 xmlParserInputBufferPtr in = ctxt->input->buf;
9694 if ((in->encoder != NULL) && (in->buffer != NULL) &&
9695 (in->raw != NULL)) {
9696 int nbchars;
9697
9698 nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
9699 if (nbchars < 0) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00009700 /* TODO 2.6.0 */
Owen Taylor3473f882001-02-23 17:55:21 +00009701 xmlGenericError(xmlGenericErrorContext,
9702 "xmlParseChunk: encoder error\n");
9703 return(XML_ERR_INVALID_ENCODING);
9704 }
9705 }
9706 }
9707 }
9708 xmlParseTryOrFinish(ctxt, terminate);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009709 if ((ctxt->errNo != XML_ERR_OK) && (ctxt->disableSAX == 1))
9710 return(ctxt->errNo);
Owen Taylor3473f882001-02-23 17:55:21 +00009711 if (terminate) {
9712 /*
9713 * Check for termination
9714 */
Daniel Veillard819d5cb2002-10-14 11:15:18 +00009715 int avail = 0;
9716 if (ctxt->input->buf == NULL)
9717 avail = ctxt->input->length -
9718 (ctxt->input->cur - ctxt->input->base);
9719 else
9720 avail = ctxt->input->buf->buffer->use -
9721 (ctxt->input->cur - ctxt->input->base);
9722
Owen Taylor3473f882001-02-23 17:55:21 +00009723 if ((ctxt->instate != XML_PARSER_EOF) &&
9724 (ctxt->instate != XML_PARSER_EPILOG)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009725 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00009726 }
Daniel Veillard819d5cb2002-10-14 11:15:18 +00009727 if ((ctxt->instate == XML_PARSER_EPILOG) && (avail > 0)) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009728 xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL);
Daniel Veillard819d5cb2002-10-14 11:15:18 +00009729 }
Owen Taylor3473f882001-02-23 17:55:21 +00009730 if (ctxt->instate != XML_PARSER_EOF) {
Daniel Veillard8d24cc12002-03-05 15:41:29 +00009731 if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00009732 ctxt->sax->endDocument(ctxt->userData);
9733 }
9734 ctxt->instate = XML_PARSER_EOF;
9735 }
9736 return((xmlParserErrors) ctxt->errNo);
9737}
9738
9739/************************************************************************
9740 * *
9741 * I/O front end functions to the parser *
9742 * *
9743 ************************************************************************/
9744
9745/**
9746 * xmlStopParser:
9747 * @ctxt: an XML parser context
9748 *
9749 * Blocks further parser processing
9750 */
9751void
9752xmlStopParser(xmlParserCtxtPtr ctxt) {
Daniel Veillard157fee02003-10-31 10:36:03 +00009753 if (ctxt == NULL)
9754 return;
Owen Taylor3473f882001-02-23 17:55:21 +00009755 ctxt->instate = XML_PARSER_EOF;
Daniel Veillard157fee02003-10-31 10:36:03 +00009756 ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00009757 if (ctxt->input != NULL)
9758 ctxt->input->cur = BAD_CAST"";
9759}
9760
9761/**
9762 * xmlCreatePushParserCtxt:
9763 * @sax: a SAX handler
9764 * @user_data: The user data returned on SAX callbacks
9765 * @chunk: a pointer to an array of chars
9766 * @size: number of chars in the array
9767 * @filename: an optional file name or URI
9768 *
Daniel Veillard176d99f2002-07-06 19:22:28 +00009769 * Create a parser context for using the XML parser in push mode.
9770 * If @buffer and @size are non-NULL, the data is used to detect
9771 * the encoding. The remaining characters will be parsed so they
9772 * don't need to be fed in again through xmlParseChunk.
Owen Taylor3473f882001-02-23 17:55:21 +00009773 * To allow content encoding detection, @size should be >= 4
9774 * The value of @filename is used for fetching external entities
9775 * and error/warning reports.
9776 *
9777 * Returns the new parser context or NULL
9778 */
Daniel Veillard176d99f2002-07-06 19:22:28 +00009779
Owen Taylor3473f882001-02-23 17:55:21 +00009780xmlParserCtxtPtr
9781xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
9782 const char *chunk, int size, const char *filename) {
9783 xmlParserCtxtPtr ctxt;
9784 xmlParserInputPtr inputStream;
9785 xmlParserInputBufferPtr buf;
9786 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
9787
9788 /*
9789 * plug some encoding conversion routines
9790 */
9791 if ((chunk != NULL) && (size >= 4))
9792 enc = xmlDetectCharEncoding((const xmlChar *) chunk, size);
9793
9794 buf = xmlAllocParserInputBuffer(enc);
9795 if (buf == NULL) return(NULL);
9796
9797 ctxt = xmlNewParserCtxt();
9798 if (ctxt == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +00009799 xmlErrMemory(NULL, "creating parser: out of memory\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009800 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00009801 return(NULL);
9802 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009803 ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 * sizeof(xmlChar *));
9804 if (ctxt->pushTab == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009805 xmlErrMemory(ctxt, NULL);
Daniel Veillarde57ec792003-09-10 10:50:59 +00009806 xmlFreeParserInputBuffer(buf);
9807 xmlFreeParserCtxt(ctxt);
9808 return(NULL);
9809 }
Owen Taylor3473f882001-02-23 17:55:21 +00009810 if (sax != NULL) {
Daniel Veillard81273902003-09-30 00:43:48 +00009811#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00009812 if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
Daniel Veillard81273902003-09-30 00:43:48 +00009813#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00009814 xmlFree(ctxt->sax);
9815 ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler));
9816 if (ctxt->sax == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009817 xmlErrMemory(ctxt, NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009818 xmlFreeParserInputBuffer(buf);
9819 xmlFreeParserCtxt(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00009820 return(NULL);
9821 }
9822 memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler));
9823 if (user_data != NULL)
9824 ctxt->userData = user_data;
9825 }
9826 if (filename == NULL) {
9827 ctxt->directory = NULL;
9828 } else {
9829 ctxt->directory = xmlParserGetDirectory(filename);
9830 }
9831
9832 inputStream = xmlNewInputStream(ctxt);
9833 if (inputStream == NULL) {
9834 xmlFreeParserCtxt(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00009835 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +00009836 return(NULL);
9837 }
9838
9839 if (filename == NULL)
9840 inputStream->filename = NULL;
9841 else
Daniel Veillardf4862f02002-09-10 11:13:43 +00009842 inputStream->filename = (char *)
Igor Zlatkovic5f9fada2003-02-19 14:51:00 +00009843 xmlCanonicPath((const xmlChar *) filename);
Owen Taylor3473f882001-02-23 17:55:21 +00009844 inputStream->buf = buf;
9845 inputStream->base = inputStream->buf->buffer->content;
9846 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00009847 inputStream->end =
9848 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00009849
9850 inputPush(ctxt, inputStream);
9851
9852 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
9853 (ctxt->input->buf != NULL)) {
Daniel Veillardaa39a0f2002-01-06 12:47:22 +00009854 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
9855 int cur = ctxt->input->cur - ctxt->input->base;
9856
Owen Taylor3473f882001-02-23 17:55:21 +00009857 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
Daniel Veillardaa39a0f2002-01-06 12:47:22 +00009858
9859 ctxt->input->base = ctxt->input->buf->buffer->content + base;
9860 ctxt->input->cur = ctxt->input->base + cur;
9861 ctxt->input->end =
9862 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00009863#ifdef DEBUG_PUSH
9864 xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
9865#endif
9866 }
9867
Daniel Veillard0e4cd172001-06-28 12:13:56 +00009868 if (enc != XML_CHAR_ENCODING_NONE) {
9869 xmlSwitchEncoding(ctxt, enc);
9870 }
9871
Owen Taylor3473f882001-02-23 17:55:21 +00009872 return(ctxt);
9873}
Daniel Veillard73b013f2003-09-30 12:36:01 +00009874#endif /* LIBXML_PUSH_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00009875
9876/**
9877 * xmlCreateIOParserCtxt:
9878 * @sax: a SAX handler
9879 * @user_data: The user data returned on SAX callbacks
9880 * @ioread: an I/O read function
9881 * @ioclose: an I/O close function
9882 * @ioctx: an I/O handler
9883 * @enc: the charset encoding if known
9884 *
9885 * Create a parser context for using the XML parser with an existing
9886 * I/O stream
9887 *
9888 * Returns the new parser context or NULL
9889 */
9890xmlParserCtxtPtr
9891xmlCreateIOParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
9892 xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
9893 void *ioctx, xmlCharEncoding enc) {
9894 xmlParserCtxtPtr ctxt;
9895 xmlParserInputPtr inputStream;
9896 xmlParserInputBufferPtr buf;
9897
9898 buf = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx, enc);
9899 if (buf == NULL) return(NULL);
9900
9901 ctxt = xmlNewParserCtxt();
9902 if (ctxt == NULL) {
9903 xmlFree(buf);
9904 return(NULL);
9905 }
9906 if (sax != NULL) {
Daniel Veillard81273902003-09-30 00:43:48 +00009907#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00009908 if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
Daniel Veillard81273902003-09-30 00:43:48 +00009909#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00009910 xmlFree(ctxt->sax);
9911 ctxt->sax = (xmlSAXHandlerPtr) xmlMalloc(sizeof(xmlSAXHandler));
9912 if (ctxt->sax == NULL) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +00009913 xmlErrMemory(ctxt, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00009914 xmlFree(ctxt);
9915 return(NULL);
9916 }
9917 memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler));
9918 if (user_data != NULL)
9919 ctxt->userData = user_data;
9920 }
9921
9922 inputStream = xmlNewIOInputStream(ctxt, buf, enc);
9923 if (inputStream == NULL) {
9924 xmlFreeParserCtxt(ctxt);
9925 return(NULL);
9926 }
9927 inputPush(ctxt, inputStream);
9928
9929 return(ctxt);
9930}
9931
Daniel Veillard4432df22003-09-28 18:58:27 +00009932#ifdef LIBXML_VALID_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00009933/************************************************************************
9934 * *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00009935 * Front ends when parsing a DTD *
Owen Taylor3473f882001-02-23 17:55:21 +00009936 * *
9937 ************************************************************************/
9938
9939/**
9940 * xmlIOParseDTD:
9941 * @sax: the SAX handler block or NULL
9942 * @input: an Input Buffer
9943 * @enc: the charset encoding if known
9944 *
9945 * Load and parse a DTD
9946 *
9947 * Returns the resulting xmlDtdPtr or NULL in case of error.
9948 * @input will be freed at parsing end.
9949 */
9950
9951xmlDtdPtr
9952xmlIOParseDTD(xmlSAXHandlerPtr sax, xmlParserInputBufferPtr input,
9953 xmlCharEncoding enc) {
9954 xmlDtdPtr ret = NULL;
9955 xmlParserCtxtPtr ctxt;
9956 xmlParserInputPtr pinput = NULL;
Daniel Veillard87a764e2001-06-20 17:41:10 +00009957 xmlChar start[4];
Owen Taylor3473f882001-02-23 17:55:21 +00009958
9959 if (input == NULL)
9960 return(NULL);
9961
9962 ctxt = xmlNewParserCtxt();
9963 if (ctxt == NULL) {
9964 return(NULL);
9965 }
9966
9967 /*
9968 * Set-up the SAX context
9969 */
9970 if (sax != NULL) {
9971 if (ctxt->sax != NULL)
9972 xmlFree(ctxt->sax);
9973 ctxt->sax = sax;
Daniel Veillard500a1de2004-03-22 15:22:58 +00009974 ctxt->userData = ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +00009975 }
Daniel Veillarde57ec792003-09-10 10:50:59 +00009976 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00009977
9978 /*
9979 * generate a parser input from the I/O handler
9980 */
9981
Daniel Veillard43caefb2003-12-07 19:32:22 +00009982 pinput = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
Owen Taylor3473f882001-02-23 17:55:21 +00009983 if (pinput == NULL) {
9984 if (sax != NULL) ctxt->sax = NULL;
9985 xmlFreeParserCtxt(ctxt);
9986 return(NULL);
9987 }
9988
9989 /*
9990 * plug some encoding conversion routines here.
9991 */
9992 xmlPushInput(ctxt, pinput);
Daniel Veillard43caefb2003-12-07 19:32:22 +00009993 if (enc != XML_CHAR_ENCODING_NONE) {
9994 xmlSwitchEncoding(ctxt, enc);
9995 }
Owen Taylor3473f882001-02-23 17:55:21 +00009996
9997 pinput->filename = NULL;
9998 pinput->line = 1;
9999 pinput->col = 1;
10000 pinput->base = ctxt->input->cur;
10001 pinput->cur = ctxt->input->cur;
10002 pinput->free = NULL;
10003
10004 /*
10005 * let's parse that entity knowing it's an external subset.
10006 */
10007 ctxt->inSubset = 2;
10008 ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
10009 ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none",
10010 BAD_CAST "none", BAD_CAST "none");
Daniel Veillard87a764e2001-06-20 17:41:10 +000010011
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010012 if ((enc == XML_CHAR_ENCODING_NONE) &&
10013 ((ctxt->input->end - ctxt->input->cur) >= 4)) {
Daniel Veillard87a764e2001-06-20 17:41:10 +000010014 /*
10015 * Get the 4 first bytes and decode the charset
10016 * if enc != XML_CHAR_ENCODING_NONE
10017 * plug some encoding conversion routines.
10018 */
10019 start[0] = RAW;
10020 start[1] = NXT(1);
10021 start[2] = NXT(2);
10022 start[3] = NXT(3);
10023 enc = xmlDetectCharEncoding(start, 4);
10024 if (enc != XML_CHAR_ENCODING_NONE) {
10025 xmlSwitchEncoding(ctxt, enc);
10026 }
10027 }
10028
Owen Taylor3473f882001-02-23 17:55:21 +000010029 xmlParseExternalSubset(ctxt, BAD_CAST "none", BAD_CAST "none");
10030
10031 if (ctxt->myDoc != NULL) {
10032 if (ctxt->wellFormed) {
10033 ret = ctxt->myDoc->extSubset;
10034 ctxt->myDoc->extSubset = NULL;
Daniel Veillard329456a2003-04-26 21:21:00 +000010035 if (ret != NULL) {
10036 xmlNodePtr tmp;
10037
10038 ret->doc = NULL;
10039 tmp = ret->children;
10040 while (tmp != NULL) {
10041 tmp->doc = NULL;
10042 tmp = tmp->next;
10043 }
10044 }
Owen Taylor3473f882001-02-23 17:55:21 +000010045 } else {
10046 ret = NULL;
10047 }
10048 xmlFreeDoc(ctxt->myDoc);
10049 ctxt->myDoc = NULL;
10050 }
10051 if (sax != NULL) ctxt->sax = NULL;
10052 xmlFreeParserCtxt(ctxt);
10053
10054 return(ret);
10055}
10056
10057/**
10058 * xmlSAXParseDTD:
10059 * @sax: the SAX handler block
10060 * @ExternalID: a NAME* containing the External ID of the DTD
10061 * @SystemID: a NAME* containing the URL to the DTD
10062 *
10063 * Load and parse an external subset.
10064 *
10065 * Returns the resulting xmlDtdPtr or NULL in case of error.
10066 */
10067
10068xmlDtdPtr
10069xmlSAXParseDTD(xmlSAXHandlerPtr sax, const xmlChar *ExternalID,
10070 const xmlChar *SystemID) {
10071 xmlDtdPtr ret = NULL;
10072 xmlParserCtxtPtr ctxt;
10073 xmlParserInputPtr input = NULL;
10074 xmlCharEncoding enc;
10075
10076 if ((ExternalID == NULL) && (SystemID == NULL)) return(NULL);
10077
10078 ctxt = xmlNewParserCtxt();
10079 if (ctxt == NULL) {
10080 return(NULL);
10081 }
10082
10083 /*
10084 * Set-up the SAX context
10085 */
10086 if (sax != NULL) {
10087 if (ctxt->sax != NULL)
10088 xmlFree(ctxt->sax);
10089 ctxt->sax = sax;
Daniel Veillardbf1e3d82003-08-14 23:57:26 +000010090 ctxt->userData = ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +000010091 }
10092
10093 /*
10094 * Ask the Entity resolver to load the damn thing
10095 */
10096
10097 if ((ctxt->sax != NULL) && (ctxt->sax->resolveEntity != NULL))
Daniel Veillardc6abc3d2003-04-26 13:27:30 +000010098 input = ctxt->sax->resolveEntity(ctxt, ExternalID, SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +000010099 if (input == NULL) {
10100 if (sax != NULL) ctxt->sax = NULL;
10101 xmlFreeParserCtxt(ctxt);
10102 return(NULL);
10103 }
10104
10105 /*
10106 * plug some encoding conversion routines here.
10107 */
10108 xmlPushInput(ctxt, input);
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010109 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
10110 enc = xmlDetectCharEncoding(ctxt->input->cur, 4);
10111 xmlSwitchEncoding(ctxt, enc);
10112 }
Owen Taylor3473f882001-02-23 17:55:21 +000010113
10114 if (input->filename == NULL)
Daniel Veillard85095e22003-04-23 13:56:44 +000010115 input->filename = (char *) xmlCanonicPath(SystemID);
Owen Taylor3473f882001-02-23 17:55:21 +000010116 input->line = 1;
10117 input->col = 1;
10118 input->base = ctxt->input->cur;
10119 input->cur = ctxt->input->cur;
10120 input->free = NULL;
10121
10122 /*
10123 * let's parse that entity knowing it's an external subset.
10124 */
10125 ctxt->inSubset = 2;
10126 ctxt->myDoc = xmlNewDoc(BAD_CAST "1.0");
10127 ctxt->myDoc->extSubset = xmlNewDtd(ctxt->myDoc, BAD_CAST "none",
10128 ExternalID, SystemID);
10129 xmlParseExternalSubset(ctxt, ExternalID, SystemID);
10130
10131 if (ctxt->myDoc != NULL) {
10132 if (ctxt->wellFormed) {
10133 ret = ctxt->myDoc->extSubset;
10134 ctxt->myDoc->extSubset = NULL;
Daniel Veillardc5573462003-04-25 16:43:49 +000010135 if (ret != NULL) {
10136 xmlNodePtr tmp;
10137
10138 ret->doc = NULL;
10139 tmp = ret->children;
10140 while (tmp != NULL) {
10141 tmp->doc = NULL;
10142 tmp = tmp->next;
10143 }
10144 }
Owen Taylor3473f882001-02-23 17:55:21 +000010145 } else {
10146 ret = NULL;
10147 }
10148 xmlFreeDoc(ctxt->myDoc);
10149 ctxt->myDoc = NULL;
10150 }
10151 if (sax != NULL) ctxt->sax = NULL;
10152 xmlFreeParserCtxt(ctxt);
10153
10154 return(ret);
10155}
10156
Daniel Veillard4432df22003-09-28 18:58:27 +000010157
Owen Taylor3473f882001-02-23 17:55:21 +000010158/**
10159 * xmlParseDTD:
10160 * @ExternalID: a NAME* containing the External ID of the DTD
10161 * @SystemID: a NAME* containing the URL to the DTD
10162 *
10163 * Load and parse an external subset.
10164 *
10165 * Returns the resulting xmlDtdPtr or NULL in case of error.
10166 */
10167
10168xmlDtdPtr
10169xmlParseDTD(const xmlChar *ExternalID, const xmlChar *SystemID) {
10170 return(xmlSAXParseDTD(NULL, ExternalID, SystemID));
10171}
Daniel Veillard4432df22003-09-28 18:58:27 +000010172#endif /* LIBXML_VALID_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000010173
10174/************************************************************************
10175 * *
10176 * Front ends when parsing an Entity *
10177 * *
10178 ************************************************************************/
10179
10180/**
Owen Taylor3473f882001-02-23 17:55:21 +000010181 * xmlParseCtxtExternalEntity:
10182 * @ctx: the existing parsing context
10183 * @URL: the URL for the entity to load
10184 * @ID: the System ID for the entity to load
Daniel Veillardcda96922001-08-21 10:56:31 +000010185 * @lst: the return value for the set of parsed nodes
Owen Taylor3473f882001-02-23 17:55:21 +000010186 *
10187 * Parse an external general entity within an existing parsing context
10188 * An external general parsed entity is well-formed if it matches the
10189 * production labeled extParsedEnt.
10190 *
10191 * [78] extParsedEnt ::= TextDecl? content
10192 *
10193 * Returns 0 if the entity is well formed, -1 in case of args problem and
10194 * the parser error code otherwise
10195 */
10196
10197int
10198xmlParseCtxtExternalEntity(xmlParserCtxtPtr ctx, const xmlChar *URL,
Daniel Veillardcda96922001-08-21 10:56:31 +000010199 const xmlChar *ID, xmlNodePtr *lst) {
Owen Taylor3473f882001-02-23 17:55:21 +000010200 xmlParserCtxtPtr ctxt;
10201 xmlDocPtr newDoc;
10202 xmlSAXHandlerPtr oldsax = NULL;
10203 int ret = 0;
Daniel Veillard87a764e2001-06-20 17:41:10 +000010204 xmlChar start[4];
10205 xmlCharEncoding enc;
Owen Taylor3473f882001-02-23 17:55:21 +000010206
10207 if (ctx->depth > 40) {
10208 return(XML_ERR_ENTITY_LOOP);
10209 }
10210
Daniel Veillardcda96922001-08-21 10:56:31 +000010211 if (lst != NULL)
10212 *lst = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010213 if ((URL == NULL) && (ID == NULL))
10214 return(-1);
10215 if (ctx->myDoc == NULL) /* @@ relax but check for dereferences */
10216 return(-1);
10217
10218
10219 ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL);
10220 if (ctxt == NULL) return(-1);
10221 ctxt->userData = ctxt;
Daniel Veillarde2830f12003-01-08 17:47:49 +000010222 ctxt->_private = ctx->_private;
Owen Taylor3473f882001-02-23 17:55:21 +000010223 oldsax = ctxt->sax;
10224 ctxt->sax = ctx->sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000010225 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010226 newDoc = xmlNewDoc(BAD_CAST "1.0");
10227 if (newDoc == NULL) {
10228 xmlFreeParserCtxt(ctxt);
10229 return(-1);
10230 }
10231 if (ctx->myDoc != NULL) {
10232 newDoc->intSubset = ctx->myDoc->intSubset;
10233 newDoc->extSubset = ctx->myDoc->extSubset;
10234 }
10235 if (ctx->myDoc->URL != NULL) {
10236 newDoc->URL = xmlStrdup(ctx->myDoc->URL);
10237 }
10238 newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
10239 if (newDoc->children == NULL) {
10240 ctxt->sax = oldsax;
10241 xmlFreeParserCtxt(ctxt);
10242 newDoc->intSubset = NULL;
10243 newDoc->extSubset = NULL;
10244 xmlFreeDoc(newDoc);
10245 return(-1);
10246 }
10247 nodePush(ctxt, newDoc->children);
10248 if (ctx->myDoc == NULL) {
10249 ctxt->myDoc = newDoc;
10250 } else {
10251 ctxt->myDoc = ctx->myDoc;
10252 newDoc->children->doc = ctx->myDoc;
10253 }
10254
Daniel Veillard87a764e2001-06-20 17:41:10 +000010255 /*
10256 * Get the 4 first bytes and decode the charset
10257 * if enc != XML_CHAR_ENCODING_NONE
10258 * plug some encoding conversion routines.
10259 */
10260 GROW
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010261 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
10262 start[0] = RAW;
10263 start[1] = NXT(1);
10264 start[2] = NXT(2);
10265 start[3] = NXT(3);
10266 enc = xmlDetectCharEncoding(start, 4);
10267 if (enc != XML_CHAR_ENCODING_NONE) {
10268 xmlSwitchEncoding(ctxt, enc);
10269 }
Daniel Veillard87a764e2001-06-20 17:41:10 +000010270 }
10271
Owen Taylor3473f882001-02-23 17:55:21 +000010272 /*
10273 * Parse a possible text declaration first
10274 */
Daniel Veillarda07050d2003-10-19 14:46:32 +000010275 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +000010276 xmlParseTextDecl(ctxt);
10277 }
10278
10279 /*
10280 * Doing validity checking on chunk doesn't make sense
10281 */
10282 ctxt->instate = XML_PARSER_CONTENT;
10283 ctxt->validate = ctx->validate;
Daniel Veillard5f8d1a32003-03-23 21:02:00 +000010284 ctxt->valid = ctx->valid;
Owen Taylor3473f882001-02-23 17:55:21 +000010285 ctxt->loadsubset = ctx->loadsubset;
10286 ctxt->depth = ctx->depth + 1;
10287 ctxt->replaceEntities = ctx->replaceEntities;
10288 if (ctxt->validate) {
10289 ctxt->vctxt.error = ctx->vctxt.error;
10290 ctxt->vctxt.warning = ctx->vctxt.warning;
Owen Taylor3473f882001-02-23 17:55:21 +000010291 } else {
10292 ctxt->vctxt.error = NULL;
10293 ctxt->vctxt.warning = NULL;
10294 }
Daniel Veillarda9142e72001-06-19 11:07:54 +000010295 ctxt->vctxt.nodeTab = NULL;
10296 ctxt->vctxt.nodeNr = 0;
10297 ctxt->vctxt.nodeMax = 0;
10298 ctxt->vctxt.node = NULL;
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000010299 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
10300 ctxt->dict = ctx->dict;
Daniel Veillardfd343dc2003-10-31 10:55:22 +000010301 ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
10302 ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
10303 ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000010304 ctxt->dictNames = ctx->dictNames;
10305 ctxt->attsDefault = ctx->attsDefault;
10306 ctxt->attsSpecial = ctx->attsSpecial;
Owen Taylor3473f882001-02-23 17:55:21 +000010307
10308 xmlParseContent(ctxt);
10309
Daniel Veillard5f8d1a32003-03-23 21:02:00 +000010310 ctx->validate = ctxt->validate;
10311 ctx->valid = ctxt->valid;
Owen Taylor3473f882001-02-23 17:55:21 +000010312 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010313 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010314 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010315 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010316 }
10317 if (ctxt->node != newDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010318 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010319 }
10320
10321 if (!ctxt->wellFormed) {
10322 if (ctxt->errNo == 0)
10323 ret = 1;
10324 else
10325 ret = ctxt->errNo;
10326 } else {
Daniel Veillardcda96922001-08-21 10:56:31 +000010327 if (lst != NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +000010328 xmlNodePtr cur;
10329
10330 /*
10331 * Return the newly created nodeset after unlinking it from
10332 * they pseudo parent.
10333 */
10334 cur = newDoc->children->children;
Daniel Veillardcda96922001-08-21 10:56:31 +000010335 *lst = cur;
Owen Taylor3473f882001-02-23 17:55:21 +000010336 while (cur != NULL) {
10337 cur->parent = NULL;
10338 cur = cur->next;
10339 }
10340 newDoc->children->children = NULL;
10341 }
10342 ret = 0;
10343 }
10344 ctxt->sax = oldsax;
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000010345 ctxt->dict = NULL;
10346 ctxt->attsDefault = NULL;
10347 ctxt->attsSpecial = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010348 xmlFreeParserCtxt(ctxt);
10349 newDoc->intSubset = NULL;
10350 newDoc->extSubset = NULL;
10351 xmlFreeDoc(newDoc);
10352
10353 return(ret);
10354}
10355
10356/**
Daniel Veillard257d9102001-05-08 10:41:44 +000010357 * xmlParseExternalEntityPrivate:
Owen Taylor3473f882001-02-23 17:55:21 +000010358 * @doc: the document the chunk pertains to
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010359 * @oldctxt: the previous parser context if available
Owen Taylor3473f882001-02-23 17:55:21 +000010360 * @sax: the SAX handler bloc (possibly NULL)
10361 * @user_data: The user data returned on SAX callbacks (possibly NULL)
10362 * @depth: Used for loop detection, use 0
10363 * @URL: the URL for the entity to load
10364 * @ID: the System ID for the entity to load
10365 * @list: the return value for the set of parsed nodes
10366 *
Daniel Veillard257d9102001-05-08 10:41:44 +000010367 * Private version of xmlParseExternalEntity()
Owen Taylor3473f882001-02-23 17:55:21 +000010368 *
10369 * Returns 0 if the entity is well formed, -1 in case of args problem and
10370 * the parser error code otherwise
10371 */
10372
Daniel Veillard7d515752003-09-26 19:12:37 +000010373static xmlParserErrors
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010374xmlParseExternalEntityPrivate(xmlDocPtr doc, xmlParserCtxtPtr oldctxt,
10375 xmlSAXHandlerPtr sax,
Daniel Veillard257d9102001-05-08 10:41:44 +000010376 void *user_data, int depth, const xmlChar *URL,
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010377 const xmlChar *ID, xmlNodePtr *list) {
Owen Taylor3473f882001-02-23 17:55:21 +000010378 xmlParserCtxtPtr ctxt;
10379 xmlDocPtr newDoc;
10380 xmlSAXHandlerPtr oldsax = NULL;
Daniel Veillard7d515752003-09-26 19:12:37 +000010381 xmlParserErrors ret = XML_ERR_OK;
Daniel Veillard87a764e2001-06-20 17:41:10 +000010382 xmlChar start[4];
10383 xmlCharEncoding enc;
Owen Taylor3473f882001-02-23 17:55:21 +000010384
10385 if (depth > 40) {
10386 return(XML_ERR_ENTITY_LOOP);
10387 }
10388
10389
10390
10391 if (list != NULL)
10392 *list = NULL;
10393 if ((URL == NULL) && (ID == NULL))
Daniel Veillard7d515752003-09-26 19:12:37 +000010394 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010395 if (doc == NULL) /* @@ relax but check for dereferences */
Daniel Veillard7d515752003-09-26 19:12:37 +000010396 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010397
10398
10399 ctxt = xmlCreateEntityParserCtxt(URL, ID, NULL);
William M. Brackb670e2e2003-09-27 01:05:55 +000010400 if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY);
Owen Taylor3473f882001-02-23 17:55:21 +000010401 ctxt->userData = ctxt;
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010402 if (oldctxt != NULL) {
10403 ctxt->_private = oldctxt->_private;
10404 ctxt->loadsubset = oldctxt->loadsubset;
10405 ctxt->validate = oldctxt->validate;
10406 ctxt->external = oldctxt->external;
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010407 ctxt->record_info = oldctxt->record_info;
10408 ctxt->node_seq.maximum = oldctxt->node_seq.maximum;
10409 ctxt->node_seq.length = oldctxt->node_seq.length;
10410 ctxt->node_seq.buffer = oldctxt->node_seq.buffer;
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010411 } else {
10412 /*
10413 * Doing validity checking on chunk without context
10414 * doesn't make sense
10415 */
10416 ctxt->_private = NULL;
10417 ctxt->validate = 0;
10418 ctxt->external = 2;
10419 ctxt->loadsubset = 0;
10420 }
Owen Taylor3473f882001-02-23 17:55:21 +000010421 if (sax != NULL) {
10422 oldsax = ctxt->sax;
10423 ctxt->sax = sax;
10424 if (user_data != NULL)
10425 ctxt->userData = user_data;
10426 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000010427 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010428 newDoc = xmlNewDoc(BAD_CAST "1.0");
10429 if (newDoc == NULL) {
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010430 ctxt->node_seq.maximum = 0;
10431 ctxt->node_seq.length = 0;
10432 ctxt->node_seq.buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010433 xmlFreeParserCtxt(ctxt);
Daniel Veillard7d515752003-09-26 19:12:37 +000010434 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010435 }
10436 if (doc != NULL) {
10437 newDoc->intSubset = doc->intSubset;
10438 newDoc->extSubset = doc->extSubset;
10439 }
10440 if (doc->URL != NULL) {
10441 newDoc->URL = xmlStrdup(doc->URL);
10442 }
10443 newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
10444 if (newDoc->children == NULL) {
10445 if (sax != NULL)
10446 ctxt->sax = oldsax;
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010447 ctxt->node_seq.maximum = 0;
10448 ctxt->node_seq.length = 0;
10449 ctxt->node_seq.buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010450 xmlFreeParserCtxt(ctxt);
10451 newDoc->intSubset = NULL;
10452 newDoc->extSubset = NULL;
10453 xmlFreeDoc(newDoc);
Daniel Veillard7d515752003-09-26 19:12:37 +000010454 return(XML_ERR_INTERNAL_ERROR);
Owen Taylor3473f882001-02-23 17:55:21 +000010455 }
10456 nodePush(ctxt, newDoc->children);
10457 if (doc == NULL) {
10458 ctxt->myDoc = newDoc;
10459 } else {
10460 ctxt->myDoc = doc;
10461 newDoc->children->doc = doc;
10462 }
10463
Daniel Veillard87a764e2001-06-20 17:41:10 +000010464 /*
10465 * Get the 4 first bytes and decode the charset
10466 * if enc != XML_CHAR_ENCODING_NONE
10467 * plug some encoding conversion routines.
10468 */
10469 GROW;
Daniel Veillard4aede2e2003-10-17 12:43:59 +000010470 if ((ctxt->input->end - ctxt->input->cur) >= 4) {
10471 start[0] = RAW;
10472 start[1] = NXT(1);
10473 start[2] = NXT(2);
10474 start[3] = NXT(3);
10475 enc = xmlDetectCharEncoding(start, 4);
10476 if (enc != XML_CHAR_ENCODING_NONE) {
10477 xmlSwitchEncoding(ctxt, enc);
10478 }
Daniel Veillard87a764e2001-06-20 17:41:10 +000010479 }
10480
Owen Taylor3473f882001-02-23 17:55:21 +000010481 /*
10482 * Parse a possible text declaration first
10483 */
Daniel Veillarda07050d2003-10-19 14:46:32 +000010484 if ((CMP5(CUR_PTR, '<', '?', 'x', 'm', 'l')) && (IS_BLANK_CH(NXT(5)))) {
Owen Taylor3473f882001-02-23 17:55:21 +000010485 xmlParseTextDecl(ctxt);
10486 }
10487
Owen Taylor3473f882001-02-23 17:55:21 +000010488 ctxt->instate = XML_PARSER_CONTENT;
Owen Taylor3473f882001-02-23 17:55:21 +000010489 ctxt->depth = depth;
10490
10491 xmlParseContent(ctxt);
10492
Daniel Veillard561b7f82002-03-20 21:55:57 +000010493 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010494 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Daniel Veillard561b7f82002-03-20 21:55:57 +000010495 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010496 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010497 }
10498 if (ctxt->node != newDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010499 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010500 }
10501
10502 if (!ctxt->wellFormed) {
10503 if (ctxt->errNo == 0)
Daniel Veillard7d515752003-09-26 19:12:37 +000010504 ret = XML_ERR_INTERNAL_ERROR;
Owen Taylor3473f882001-02-23 17:55:21 +000010505 else
William M. Brack7b9154b2003-09-27 19:23:50 +000010506 ret = (xmlParserErrors)ctxt->errNo;
Owen Taylor3473f882001-02-23 17:55:21 +000010507 } else {
10508 if (list != NULL) {
10509 xmlNodePtr cur;
10510
10511 /*
10512 * Return the newly created nodeset after unlinking it from
10513 * they pseudo parent.
10514 */
10515 cur = newDoc->children->children;
10516 *list = cur;
10517 while (cur != NULL) {
10518 cur->parent = NULL;
10519 cur = cur->next;
10520 }
10521 newDoc->children->children = NULL;
10522 }
Daniel Veillard7d515752003-09-26 19:12:37 +000010523 ret = XML_ERR_OK;
Owen Taylor3473f882001-02-23 17:55:21 +000010524 }
10525 if (sax != NULL)
10526 ctxt->sax = oldsax;
Daniel Veillard0046c0f2003-02-23 13:52:30 +000010527 oldctxt->node_seq.maximum = ctxt->node_seq.maximum;
10528 oldctxt->node_seq.length = ctxt->node_seq.length;
10529 oldctxt->node_seq.buffer = ctxt->node_seq.buffer;
Daniel Veillard44e1dd02003-02-21 23:23:28 +000010530 ctxt->node_seq.maximum = 0;
10531 ctxt->node_seq.length = 0;
10532 ctxt->node_seq.buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010533 xmlFreeParserCtxt(ctxt);
10534 newDoc->intSubset = NULL;
10535 newDoc->extSubset = NULL;
10536 xmlFreeDoc(newDoc);
10537
10538 return(ret);
10539}
10540
Daniel Veillard81273902003-09-30 00:43:48 +000010541#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000010542/**
Daniel Veillard257d9102001-05-08 10:41:44 +000010543 * xmlParseExternalEntity:
10544 * @doc: the document the chunk pertains to
10545 * @sax: the SAX handler bloc (possibly NULL)
10546 * @user_data: The user data returned on SAX callbacks (possibly NULL)
10547 * @depth: Used for loop detection, use 0
10548 * @URL: the URL for the entity to load
10549 * @ID: the System ID for the entity to load
Daniel Veillardcda96922001-08-21 10:56:31 +000010550 * @lst: the return value for the set of parsed nodes
Daniel Veillard257d9102001-05-08 10:41:44 +000010551 *
10552 * Parse an external general entity
10553 * An external general parsed entity is well-formed if it matches the
10554 * production labeled extParsedEnt.
10555 *
10556 * [78] extParsedEnt ::= TextDecl? content
10557 *
10558 * Returns 0 if the entity is well formed, -1 in case of args problem and
10559 * the parser error code otherwise
10560 */
10561
10562int
10563xmlParseExternalEntity(xmlDocPtr doc, xmlSAXHandlerPtr sax, void *user_data,
Daniel Veillardcda96922001-08-21 10:56:31 +000010564 int depth, const xmlChar *URL, const xmlChar *ID, xmlNodePtr *lst) {
Daniel Veillarda97a19b2001-05-20 13:19:52 +000010565 return(xmlParseExternalEntityPrivate(doc, NULL, sax, user_data, depth, URL,
Daniel Veillardcda96922001-08-21 10:56:31 +000010566 ID, lst));
Daniel Veillard257d9102001-05-08 10:41:44 +000010567}
10568
10569/**
Daniel Veillarde020c3a2001-03-21 18:06:15 +000010570 * xmlParseBalancedChunkMemory:
Owen Taylor3473f882001-02-23 17:55:21 +000010571 * @doc: the document the chunk pertains to
10572 * @sax: the SAX handler bloc (possibly NULL)
10573 * @user_data: The user data returned on SAX callbacks (possibly NULL)
10574 * @depth: Used for loop detection, use 0
10575 * @string: the input string in UTF8 or ISO-Latin (zero terminated)
Daniel Veillardcda96922001-08-21 10:56:31 +000010576 * @lst: the return value for the set of parsed nodes
Owen Taylor3473f882001-02-23 17:55:21 +000010577 *
10578 * Parse a well-balanced chunk of an XML document
10579 * called by the parser
10580 * The allowed sequence for the Well Balanced Chunk is the one defined by
10581 * the content production in the XML grammar:
10582 *
10583 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
10584 *
10585 * Returns 0 if the chunk is well balanced, -1 in case of args problem and
10586 * the parser error code otherwise
10587 */
10588
10589int
10590xmlParseBalancedChunkMemory(xmlDocPtr doc, xmlSAXHandlerPtr sax,
Daniel Veillardcda96922001-08-21 10:56:31 +000010591 void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst) {
Daniel Veillard58e44c92002-08-02 22:19:49 +000010592 return xmlParseBalancedChunkMemoryRecover( doc, sax, user_data,
10593 depth, string, lst, 0 );
10594}
Daniel Veillard81273902003-09-30 00:43:48 +000010595#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillard58e44c92002-08-02 22:19:49 +000010596
10597/**
Daniel Veillard328f48c2002-11-15 15:24:34 +000010598 * xmlParseBalancedChunkMemoryInternal:
10599 * @oldctxt: the existing parsing context
10600 * @string: the input string in UTF8 or ISO-Latin (zero terminated)
10601 * @user_data: the user data field for the parser context
10602 * @lst: the return value for the set of parsed nodes
10603 *
10604 *
10605 * Parse a well-balanced chunk of an XML document
10606 * called by the parser
10607 * The allowed sequence for the Well Balanced Chunk is the one defined by
10608 * the content production in the XML grammar:
10609 *
10610 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
10611 *
Daniel Veillard7d515752003-09-26 19:12:37 +000010612 * Returns XML_ERR_OK if the chunk is well balanced, and the parser
10613 * error code otherwise
Daniel Veillard328f48c2002-11-15 15:24:34 +000010614 *
10615 * In case recover is set to 1, the nodelist will not be empty even if
10616 * the parsed chunk is not well balanced.
10617 */
Daniel Veillard7d515752003-09-26 19:12:37 +000010618static xmlParserErrors
Daniel Veillard328f48c2002-11-15 15:24:34 +000010619xmlParseBalancedChunkMemoryInternal(xmlParserCtxtPtr oldctxt,
10620 const xmlChar *string, void *user_data, xmlNodePtr *lst) {
10621 xmlParserCtxtPtr ctxt;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010622 xmlDocPtr newDoc = NULL;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010623 xmlSAXHandlerPtr oldsax = NULL;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010624 xmlNodePtr content = NULL;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010625 int size;
Daniel Veillard7d515752003-09-26 19:12:37 +000010626 xmlParserErrors ret = XML_ERR_OK;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010627
10628 if (oldctxt->depth > 40) {
10629 return(XML_ERR_ENTITY_LOOP);
10630 }
10631
10632
10633 if (lst != NULL)
10634 *lst = NULL;
10635 if (string == NULL)
William M. Brack7b9154b2003-09-27 19:23:50 +000010636 return(XML_ERR_INTERNAL_ERROR);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010637
10638 size = xmlStrlen(string);
10639
10640 ctxt = xmlCreateMemoryParserCtxt((char *) string, size);
William M. Brack7b9154b2003-09-27 19:23:50 +000010641 if (ctxt == NULL) return(XML_WAR_UNDECLARED_ENTITY);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010642 if (user_data != NULL)
10643 ctxt->userData = user_data;
10644 else
10645 ctxt->userData = ctxt;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010646 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
10647 ctxt->dict = oldctxt->dict;
Daniel Veillardfd343dc2003-10-31 10:55:22 +000010648 ctxt->str_xml = xmlDictLookup(ctxt->dict, BAD_CAST "xml", 3);
10649 ctxt->str_xmlns = xmlDictLookup(ctxt->dict, BAD_CAST "xmlns", 5);
10650 ctxt->str_xml_ns = xmlDictLookup(ctxt->dict, XML_XML_NAMESPACE, 36);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010651
10652 oldsax = ctxt->sax;
10653 ctxt->sax = oldctxt->sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000010654 xmlDetectSAX2(ctxt);
Daniel Veillard87ab1c12003-12-21 13:01:56 +000010655 ctxt->replaceEntities = oldctxt->replaceEntities;
10656 ctxt->options = oldctxt->options;
Daniel Veillarde57ec792003-09-10 10:50:59 +000010657
Daniel Veillarde1ca5032002-12-09 14:13:43 +000010658 ctxt->_private = oldctxt->_private;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010659 if (oldctxt->myDoc == NULL) {
Daniel Veillard68e9e742002-11-16 15:35:11 +000010660 newDoc = xmlNewDoc(BAD_CAST "1.0");
10661 if (newDoc == NULL) {
10662 ctxt->sax = oldsax;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010663 ctxt->dict = NULL;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010664 xmlFreeParserCtxt(ctxt);
William M. Brack7b9154b2003-09-27 19:23:50 +000010665 return(XML_ERR_INTERNAL_ERROR);
Daniel Veillard68e9e742002-11-16 15:35:11 +000010666 }
Daniel Veillard328f48c2002-11-15 15:24:34 +000010667 ctxt->myDoc = newDoc;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010668 } else {
10669 ctxt->myDoc = oldctxt->myDoc;
10670 content = ctxt->myDoc->children;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010671 }
Daniel Veillard9bc53102002-11-25 13:20:04 +000010672 ctxt->myDoc->children = xmlNewDocNode(ctxt->myDoc, NULL,
Daniel Veillard68e9e742002-11-16 15:35:11 +000010673 BAD_CAST "pseudoroot", NULL);
10674 if (ctxt->myDoc->children == NULL) {
10675 ctxt->sax = oldsax;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010676 ctxt->dict = NULL;
Daniel Veillard68e9e742002-11-16 15:35:11 +000010677 xmlFreeParserCtxt(ctxt);
10678 if (newDoc != NULL)
10679 xmlFreeDoc(newDoc);
William M. Brack7b9154b2003-09-27 19:23:50 +000010680 return(XML_ERR_INTERNAL_ERROR);
Daniel Veillard68e9e742002-11-16 15:35:11 +000010681 }
10682 nodePush(ctxt, ctxt->myDoc->children);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010683 ctxt->instate = XML_PARSER_CONTENT;
10684 ctxt->depth = oldctxt->depth + 1;
10685
Daniel Veillard328f48c2002-11-15 15:24:34 +000010686 ctxt->validate = 0;
10687 ctxt->loadsubset = oldctxt->loadsubset;
Daniel Veillardef8dd7b2003-03-23 12:02:56 +000010688 if ((oldctxt->validate) || (oldctxt->replaceEntities != 0)) {
10689 /*
10690 * ID/IDREF registration will be done in xmlValidateElement below
10691 */
10692 ctxt->loadsubset |= XML_SKIP_IDS;
10693 }
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010694 ctxt->dictNames = oldctxt->dictNames;
Daniel Veillard95d2d5b2003-10-27 14:54:49 +000010695 ctxt->attsDefault = oldctxt->attsDefault;
10696 ctxt->attsSpecial = oldctxt->attsSpecial;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010697
Daniel Veillard68e9e742002-11-16 15:35:11 +000010698 xmlParseContent(ctxt);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010699 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010700 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010701 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010702 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010703 }
Daniel Veillard68e9e742002-11-16 15:35:11 +000010704 if (ctxt->node != ctxt->myDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010705 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010706 }
10707
10708 if (!ctxt->wellFormed) {
10709 if (ctxt->errNo == 0)
Daniel Veillard7d515752003-09-26 19:12:37 +000010710 ret = XML_ERR_INTERNAL_ERROR;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010711 else
William M. Brack7b9154b2003-09-27 19:23:50 +000010712 ret = (xmlParserErrors)ctxt->errNo;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010713 } else {
William M. Brack7b9154b2003-09-27 19:23:50 +000010714 ret = XML_ERR_OK;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010715 }
10716
William M. Brack7b9154b2003-09-27 19:23:50 +000010717 if ((lst != NULL) && (ret == XML_ERR_OK)) {
Daniel Veillard328f48c2002-11-15 15:24:34 +000010718 xmlNodePtr cur;
10719
10720 /*
10721 * Return the newly created nodeset after unlinking it from
10722 * they pseudo parent.
10723 */
Daniel Veillard68e9e742002-11-16 15:35:11 +000010724 cur = ctxt->myDoc->children->children;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010725 *lst = cur;
10726 while (cur != NULL) {
Daniel Veillard4432df22003-09-28 18:58:27 +000010727#ifdef LIBXML_VALID_ENABLED
Daniel Veillard8d589042003-02-04 15:07:21 +000010728 if (oldctxt->validate && oldctxt->wellFormed &&
10729 oldctxt->myDoc && oldctxt->myDoc->intSubset) {
10730 oldctxt->valid &= xmlValidateElement(&oldctxt->vctxt,
10731 oldctxt->myDoc, cur);
10732 }
Daniel Veillard4432df22003-09-28 18:58:27 +000010733#endif /* LIBXML_VALID_ENABLED */
Daniel Veillard328f48c2002-11-15 15:24:34 +000010734 cur->parent = NULL;
10735 cur = cur->next;
10736 }
Daniel Veillard68e9e742002-11-16 15:35:11 +000010737 ctxt->myDoc->children->children = NULL;
10738 }
10739 if (ctxt->myDoc != NULL) {
10740 xmlFreeNode(ctxt->myDoc->children);
10741 ctxt->myDoc->children = content;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010742 }
10743
10744 ctxt->sax = oldsax;
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +000010745 ctxt->dict = NULL;
Daniel Veillard95d2d5b2003-10-27 14:54:49 +000010746 ctxt->attsDefault = NULL;
10747 ctxt->attsSpecial = NULL;
Daniel Veillard328f48c2002-11-15 15:24:34 +000010748 xmlFreeParserCtxt(ctxt);
Daniel Veillard68e9e742002-11-16 15:35:11 +000010749 if (newDoc != NULL)
10750 xmlFreeDoc(newDoc);
Daniel Veillard328f48c2002-11-15 15:24:34 +000010751
10752 return(ret);
10753}
10754
Daniel Veillard81273902003-09-30 00:43:48 +000010755#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard328f48c2002-11-15 15:24:34 +000010756/**
Daniel Veillard58e44c92002-08-02 22:19:49 +000010757 * xmlParseBalancedChunkMemoryRecover:
10758 * @doc: the document the chunk pertains to
10759 * @sax: the SAX handler bloc (possibly NULL)
10760 * @user_data: The user data returned on SAX callbacks (possibly NULL)
10761 * @depth: Used for loop detection, use 0
10762 * @string: the input string in UTF8 or ISO-Latin (zero terminated)
10763 * @lst: the return value for the set of parsed nodes
10764 * @recover: return nodes even if the data is broken (use 0)
10765 *
10766 *
10767 * Parse a well-balanced chunk of an XML document
10768 * called by the parser
10769 * The allowed sequence for the Well Balanced Chunk is the one defined by
10770 * the content production in the XML grammar:
10771 *
10772 * [43] content ::= (element | CharData | Reference | CDSect | PI | Comment)*
10773 *
10774 * Returns 0 if the chunk is well balanced, -1 in case of args problem and
10775 * the parser error code otherwise
10776 *
10777 * In case recover is set to 1, the nodelist will not be empty even if
10778 * the parsed chunk is not well balanced.
10779 */
10780int
10781xmlParseBalancedChunkMemoryRecover(xmlDocPtr doc, xmlSAXHandlerPtr sax,
10782 void *user_data, int depth, const xmlChar *string, xmlNodePtr *lst,
10783 int recover) {
Owen Taylor3473f882001-02-23 17:55:21 +000010784 xmlParserCtxtPtr ctxt;
10785 xmlDocPtr newDoc;
10786 xmlSAXHandlerPtr oldsax = NULL;
Daniel Veillard935494a2002-10-22 14:22:46 +000010787 xmlNodePtr content;
Owen Taylor3473f882001-02-23 17:55:21 +000010788 int size;
10789 int ret = 0;
10790
10791 if (depth > 40) {
10792 return(XML_ERR_ENTITY_LOOP);
10793 }
10794
10795
Daniel Veillardcda96922001-08-21 10:56:31 +000010796 if (lst != NULL)
10797 *lst = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000010798 if (string == NULL)
10799 return(-1);
10800
10801 size = xmlStrlen(string);
10802
10803 ctxt = xmlCreateMemoryParserCtxt((char *) string, size);
10804 if (ctxt == NULL) return(-1);
10805 ctxt->userData = ctxt;
10806 if (sax != NULL) {
10807 oldsax = ctxt->sax;
10808 ctxt->sax = sax;
10809 if (user_data != NULL)
10810 ctxt->userData = user_data;
10811 }
10812 newDoc = xmlNewDoc(BAD_CAST "1.0");
10813 if (newDoc == NULL) {
10814 xmlFreeParserCtxt(ctxt);
10815 return(-1);
10816 }
10817 if (doc != NULL) {
10818 newDoc->intSubset = doc->intSubset;
10819 newDoc->extSubset = doc->extSubset;
10820 }
10821 newDoc->children = xmlNewDocNode(newDoc, NULL, BAD_CAST "pseudoroot", NULL);
10822 if (newDoc->children == NULL) {
10823 if (sax != NULL)
10824 ctxt->sax = oldsax;
10825 xmlFreeParserCtxt(ctxt);
10826 newDoc->intSubset = NULL;
10827 newDoc->extSubset = NULL;
10828 xmlFreeDoc(newDoc);
10829 return(-1);
10830 }
10831 nodePush(ctxt, newDoc->children);
10832 if (doc == NULL) {
10833 ctxt->myDoc = newDoc;
10834 } else {
Daniel Veillard42766c02002-08-22 20:52:17 +000010835 ctxt->myDoc = newDoc;
Owen Taylor3473f882001-02-23 17:55:21 +000010836 newDoc->children->doc = doc;
10837 }
10838 ctxt->instate = XML_PARSER_CONTENT;
10839 ctxt->depth = depth;
10840
10841 /*
10842 * Doing validity checking on chunk doesn't make sense
10843 */
10844 ctxt->validate = 0;
10845 ctxt->loadsubset = 0;
Daniel Veillarde57ec792003-09-10 10:50:59 +000010846 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000010847
Daniel Veillardb39bc392002-10-26 19:29:51 +000010848 if ( doc != NULL ){
10849 content = doc->children;
10850 doc->children = NULL;
10851 xmlParseContent(ctxt);
10852 doc->children = content;
10853 }
10854 else {
10855 xmlParseContent(ctxt);
10856 }
Owen Taylor3473f882001-02-23 17:55:21 +000010857 if ((RAW == '<') && (NXT(1) == '/')) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010858 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010859 } else if (RAW != 0) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010860 xmlFatalErr(ctxt, XML_ERR_EXTRA_CONTENT, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010861 }
10862 if (ctxt->node != newDoc->children) {
Daniel Veillard1afc9f32003-09-13 12:44:05 +000010863 xmlFatalErr(ctxt, XML_ERR_NOT_WELL_BALANCED, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000010864 }
10865
10866 if (!ctxt->wellFormed) {
10867 if (ctxt->errNo == 0)
10868 ret = 1;
10869 else
10870 ret = ctxt->errNo;
10871 } else {
Daniel Veillard58e44c92002-08-02 22:19:49 +000010872 ret = 0;
10873 }
10874
10875 if (lst != NULL && (ret == 0 || recover == 1)) {
10876 xmlNodePtr cur;
Owen Taylor3473f882001-02-23 17:55:21 +000010877
10878 /*
10879 * Return the newly created nodeset after unlinking it from
10880 * they pseudo parent.
10881 */
10882 cur = newDoc->children->children;
Daniel Veillardcda96922001-08-21 10:56:31 +000010883 *lst = cur;
Owen Taylor3473f882001-02-23 17:55:21 +000010884 while (cur != NULL) {
10885 cur->parent = NULL;
10886 cur = cur->next;
10887 }
10888 newDoc->children->children = NULL;
10889 }
Daniel Veillard58e44c92002-08-02 22:19:49 +000010890
Owen Taylor3473f882001-02-23 17:55:21 +000010891 if (sax != NULL)
10892 ctxt->sax = oldsax;
10893 xmlFreeParserCtxt(ctxt);
10894 newDoc->intSubset = NULL;
10895 newDoc->extSubset = NULL;
10896 xmlFreeDoc(newDoc);
10897
10898 return(ret);
10899}
10900
10901/**
10902 * xmlSAXParseEntity:
10903 * @sax: the SAX handler block
10904 * @filename: the filename
10905 *
10906 * parse an XML external entity out of context and build a tree.
10907 * It use the given SAX function block to handle the parsing callback.
10908 * If sax is NULL, fallback to the default DOM tree building routines.
10909 *
10910 * [78] extParsedEnt ::= TextDecl? content
10911 *
10912 * This correspond to a "Well Balanced" chunk
10913 *
10914 * Returns the resulting document tree
10915 */
10916
10917xmlDocPtr
10918xmlSAXParseEntity(xmlSAXHandlerPtr sax, const char *filename) {
10919 xmlDocPtr ret;
10920 xmlParserCtxtPtr ctxt;
Owen Taylor3473f882001-02-23 17:55:21 +000010921
10922 ctxt = xmlCreateFileParserCtxt(filename);
10923 if (ctxt == NULL) {
10924 return(NULL);
10925 }
10926 if (sax != NULL) {
10927 if (ctxt->sax != NULL)
10928 xmlFree(ctxt->sax);
10929 ctxt->sax = sax;
10930 ctxt->userData = NULL;
10931 }
10932
Owen Taylor3473f882001-02-23 17:55:21 +000010933 xmlParseExtParsedEnt(ctxt);
10934
10935 if (ctxt->wellFormed)
10936 ret = ctxt->myDoc;
10937 else {
10938 ret = NULL;
10939 xmlFreeDoc(ctxt->myDoc);
10940 ctxt->myDoc = NULL;
10941 }
10942 if (sax != NULL)
10943 ctxt->sax = NULL;
10944 xmlFreeParserCtxt(ctxt);
10945
10946 return(ret);
10947}
10948
10949/**
10950 * xmlParseEntity:
10951 * @filename: the filename
10952 *
10953 * parse an XML external entity out of context and build a tree.
10954 *
10955 * [78] extParsedEnt ::= TextDecl? content
10956 *
10957 * This correspond to a "Well Balanced" chunk
10958 *
10959 * Returns the resulting document tree
10960 */
10961
10962xmlDocPtr
10963xmlParseEntity(const char *filename) {
10964 return(xmlSAXParseEntity(NULL, filename));
10965}
Daniel Veillard81273902003-09-30 00:43:48 +000010966#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000010967
10968/**
10969 * xmlCreateEntityParserCtxt:
10970 * @URL: the entity URL
10971 * @ID: the entity PUBLIC ID
Daniel Veillardcbaf3992001-12-31 16:16:02 +000010972 * @base: a possible base for the target URI
Owen Taylor3473f882001-02-23 17:55:21 +000010973 *
10974 * Create a parser context for an external entity
10975 * Automatic support for ZLIB/Compress compressed document is provided
10976 * by default if found at compile-time.
10977 *
10978 * Returns the new parser context or NULL
10979 */
10980xmlParserCtxtPtr
10981xmlCreateEntityParserCtxt(const xmlChar *URL, const xmlChar *ID,
10982 const xmlChar *base) {
10983 xmlParserCtxtPtr ctxt;
10984 xmlParserInputPtr inputStream;
10985 char *directory = NULL;
10986 xmlChar *uri;
10987
10988 ctxt = xmlNewParserCtxt();
10989 if (ctxt == NULL) {
10990 return(NULL);
10991 }
10992
10993 uri = xmlBuildURI(URL, base);
10994
10995 if (uri == NULL) {
10996 inputStream = xmlLoadExternalEntity((char *)URL, (char *)ID, ctxt);
10997 if (inputStream == NULL) {
10998 xmlFreeParserCtxt(ctxt);
10999 return(NULL);
11000 }
11001
11002 inputPush(ctxt, inputStream);
11003
11004 if ((ctxt->directory == NULL) && (directory == NULL))
11005 directory = xmlParserGetDirectory((char *)URL);
11006 if ((ctxt->directory == NULL) && (directory != NULL))
11007 ctxt->directory = directory;
11008 } else {
11009 inputStream = xmlLoadExternalEntity((char *)uri, (char *)ID, ctxt);
11010 if (inputStream == NULL) {
11011 xmlFree(uri);
11012 xmlFreeParserCtxt(ctxt);
11013 return(NULL);
11014 }
11015
11016 inputPush(ctxt, inputStream);
11017
11018 if ((ctxt->directory == NULL) && (directory == NULL))
11019 directory = xmlParserGetDirectory((char *)uri);
11020 if ((ctxt->directory == NULL) && (directory != NULL))
11021 ctxt->directory = directory;
11022 xmlFree(uri);
11023 }
Owen Taylor3473f882001-02-23 17:55:21 +000011024 return(ctxt);
11025}
11026
11027/************************************************************************
11028 * *
11029 * Front ends when parsing from a file *
11030 * *
11031 ************************************************************************/
11032
11033/**
Daniel Veillard61b93382003-11-03 14:28:31 +000011034 * xmlCreateURLParserCtxt:
11035 * @filename: the filename or URL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000011036 * @options: a combination of xmlParserOption
Owen Taylor3473f882001-02-23 17:55:21 +000011037 *
Daniel Veillard61b93382003-11-03 14:28:31 +000011038 * Create a parser context for a file or URL content.
Owen Taylor3473f882001-02-23 17:55:21 +000011039 * Automatic support for ZLIB/Compress compressed document is provided
Daniel Veillard61b93382003-11-03 14:28:31 +000011040 * by default if found at compile-time and for file accesses
Owen Taylor3473f882001-02-23 17:55:21 +000011041 *
11042 * Returns the new parser context or NULL
11043 */
11044xmlParserCtxtPtr
Daniel Veillard61b93382003-11-03 14:28:31 +000011045xmlCreateURLParserCtxt(const char *filename, int options)
Owen Taylor3473f882001-02-23 17:55:21 +000011046{
11047 xmlParserCtxtPtr ctxt;
11048 xmlParserInputPtr inputStream;
Owen Taylor3473f882001-02-23 17:55:21 +000011049 char *directory = NULL;
11050
Owen Taylor3473f882001-02-23 17:55:21 +000011051 ctxt = xmlNewParserCtxt();
11052 if (ctxt == NULL) {
Daniel Veillard81273902003-09-30 00:43:48 +000011053 xmlErrMemory(NULL, "cannot allocate parser context");
Owen Taylor3473f882001-02-23 17:55:21 +000011054 return(NULL);
11055 }
11056
Daniel Veillard61b93382003-11-03 14:28:31 +000011057 if (options != 0)
11058 xmlCtxtUseOptions(ctxt, options);
Igor Zlatkovicce076162003-02-23 13:39:39 +000011059
Daniel Veillard4e9b1bc2003-06-09 10:30:33 +000011060 inputStream = xmlLoadExternalEntity(filename, NULL, ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011061 if (inputStream == NULL) {
11062 xmlFreeParserCtxt(ctxt);
11063 return(NULL);
11064 }
11065
Owen Taylor3473f882001-02-23 17:55:21 +000011066 inputPush(ctxt, inputStream);
11067 if ((ctxt->directory == NULL) && (directory == NULL))
Igor Zlatkovic5f9fada2003-02-19 14:51:00 +000011068 directory = xmlParserGetDirectory(filename);
Owen Taylor3473f882001-02-23 17:55:21 +000011069 if ((ctxt->directory == NULL) && (directory != NULL))
11070 ctxt->directory = directory;
11071
11072 return(ctxt);
11073}
11074
Daniel Veillard61b93382003-11-03 14:28:31 +000011075/**
11076 * xmlCreateFileParserCtxt:
11077 * @filename: the filename
11078 *
11079 * Create a parser context for a file content.
11080 * Automatic support for ZLIB/Compress compressed document is provided
11081 * by default if found at compile-time.
11082 *
11083 * Returns the new parser context or NULL
11084 */
11085xmlParserCtxtPtr
11086xmlCreateFileParserCtxt(const char *filename)
11087{
11088 return(xmlCreateURLParserCtxt(filename, 0));
11089}
11090
Daniel Veillard81273902003-09-30 00:43:48 +000011091#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000011092/**
Daniel Veillarda293c322001-10-02 13:54:14 +000011093 * xmlSAXParseFileWithData:
Owen Taylor3473f882001-02-23 17:55:21 +000011094 * @sax: the SAX handler block
11095 * @filename: the filename
11096 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11097 * documents
Daniel Veillarda293c322001-10-02 13:54:14 +000011098 * @data: the userdata
Owen Taylor3473f882001-02-23 17:55:21 +000011099 *
11100 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11101 * compressed document is provided by default if found at compile-time.
11102 * It use the given SAX function block to handle the parsing callback.
11103 * If sax is NULL, fallback to the default DOM tree building routines.
11104 *
Daniel Veillarde19fc232002-04-22 16:01:24 +000011105 * User data (void *) is stored within the parser context in the
11106 * context's _private member, so it is available nearly everywhere in libxml
Daniel Veillarda293c322001-10-02 13:54:14 +000011107 *
Owen Taylor3473f882001-02-23 17:55:21 +000011108 * Returns the resulting document tree
11109 */
11110
11111xmlDocPtr
Daniel Veillarda293c322001-10-02 13:54:14 +000011112xmlSAXParseFileWithData(xmlSAXHandlerPtr sax, const char *filename,
11113 int recovery, void *data) {
Owen Taylor3473f882001-02-23 17:55:21 +000011114 xmlDocPtr ret;
11115 xmlParserCtxtPtr ctxt;
11116 char *directory = NULL;
11117
Daniel Veillard635ef722001-10-29 11:48:19 +000011118 xmlInitParser();
11119
Owen Taylor3473f882001-02-23 17:55:21 +000011120 ctxt = xmlCreateFileParserCtxt(filename);
11121 if (ctxt == NULL) {
11122 return(NULL);
11123 }
11124 if (sax != NULL) {
11125 if (ctxt->sax != NULL)
11126 xmlFree(ctxt->sax);
11127 ctxt->sax = sax;
Owen Taylor3473f882001-02-23 17:55:21 +000011128 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000011129 xmlDetectSAX2(ctxt);
Daniel Veillarda293c322001-10-02 13:54:14 +000011130 if (data!=NULL) {
Daniel Veillardc790bf42003-10-11 10:50:10 +000011131 ctxt->_private = data;
Daniel Veillarda293c322001-10-02 13:54:14 +000011132 }
Owen Taylor3473f882001-02-23 17:55:21 +000011133
11134 if ((ctxt->directory == NULL) && (directory == NULL))
11135 directory = xmlParserGetDirectory(filename);
11136 if ((ctxt->directory == NULL) && (directory != NULL))
11137 ctxt->directory = (char *) xmlStrdup((xmlChar *) directory);
11138
Daniel Veillarddad3f682002-11-17 16:47:27 +000011139 ctxt->recovery = recovery;
11140
Owen Taylor3473f882001-02-23 17:55:21 +000011141 xmlParseDocument(ctxt);
11142
William M. Brackc07329e2003-09-08 01:57:30 +000011143 if ((ctxt->wellFormed) || recovery) {
11144 ret = ctxt->myDoc;
Daniel Veillardb65e12e2003-10-08 21:33:28 +000011145 if (ret != NULL) {
11146 if (ctxt->input->buf->compressed > 0)
11147 ret->compression = 9;
11148 else
11149 ret->compression = ctxt->input->buf->compressed;
11150 }
William M. Brackc07329e2003-09-08 01:57:30 +000011151 }
Owen Taylor3473f882001-02-23 17:55:21 +000011152 else {
11153 ret = NULL;
11154 xmlFreeDoc(ctxt->myDoc);
11155 ctxt->myDoc = NULL;
11156 }
11157 if (sax != NULL)
11158 ctxt->sax = NULL;
11159 xmlFreeParserCtxt(ctxt);
11160
11161 return(ret);
11162}
11163
11164/**
Daniel Veillarda293c322001-10-02 13:54:14 +000011165 * xmlSAXParseFile:
11166 * @sax: the SAX handler block
11167 * @filename: the filename
11168 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11169 * documents
11170 *
11171 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11172 * compressed document is provided by default if found at compile-time.
11173 * It use the given SAX function block to handle the parsing callback.
11174 * If sax is NULL, fallback to the default DOM tree building routines.
11175 *
11176 * Returns the resulting document tree
11177 */
11178
11179xmlDocPtr
11180xmlSAXParseFile(xmlSAXHandlerPtr sax, const char *filename,
11181 int recovery) {
11182 return(xmlSAXParseFileWithData(sax,filename,recovery,NULL));
11183}
11184
11185/**
Owen Taylor3473f882001-02-23 17:55:21 +000011186 * xmlRecoverDoc:
11187 * @cur: a pointer to an array of xmlChar
11188 *
11189 * parse an XML in-memory document and build a tree.
11190 * In the case the document is not Well Formed, a tree is built anyway
11191 *
11192 * Returns the resulting document tree
11193 */
11194
11195xmlDocPtr
11196xmlRecoverDoc(xmlChar *cur) {
11197 return(xmlSAXParseDoc(NULL, cur, 1));
11198}
11199
11200/**
11201 * xmlParseFile:
11202 * @filename: the filename
11203 *
11204 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11205 * compressed document is provided by default if found at compile-time.
11206 *
Daniel Veillard5d96fff2001-08-31 14:55:30 +000011207 * Returns the resulting document tree if the file was wellformed,
11208 * NULL otherwise.
Owen Taylor3473f882001-02-23 17:55:21 +000011209 */
11210
11211xmlDocPtr
11212xmlParseFile(const char *filename) {
11213 return(xmlSAXParseFile(NULL, filename, 0));
11214}
11215
11216/**
11217 * xmlRecoverFile:
11218 * @filename: the filename
11219 *
11220 * parse an XML file and build a tree. Automatic support for ZLIB/Compress
11221 * compressed document is provided by default if found at compile-time.
11222 * In the case the document is not Well Formed, a tree is built anyway
11223 *
11224 * Returns the resulting document tree
11225 */
11226
11227xmlDocPtr
11228xmlRecoverFile(const char *filename) {
11229 return(xmlSAXParseFile(NULL, filename, 1));
11230}
11231
11232
11233/**
11234 * xmlSetupParserForBuffer:
11235 * @ctxt: an XML parser context
11236 * @buffer: a xmlChar * buffer
11237 * @filename: a file name
11238 *
11239 * Setup the parser context to parse a new buffer; Clears any prior
11240 * contents from the parser context. The buffer parameter must not be
11241 * NULL, but the filename parameter can be
11242 */
11243void
11244xmlSetupParserForBuffer(xmlParserCtxtPtr ctxt, const xmlChar* buffer,
11245 const char* filename)
11246{
11247 xmlParserInputPtr input;
11248
11249 input = xmlNewInputStream(ctxt);
11250 if (input == NULL) {
Daniel Veillard24eb9782003-10-04 21:08:09 +000011251 xmlErrMemory(NULL, "parsing new buffer: out of memory\n");
Owen Taylor3473f882001-02-23 17:55:21 +000011252 xmlFree(ctxt);
11253 return;
11254 }
11255
11256 xmlClearParserCtxt(ctxt);
11257 if (filename != NULL)
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +000011258 input->filename = (char *) xmlCanonicPath((const xmlChar *)filename);
Owen Taylor3473f882001-02-23 17:55:21 +000011259 input->base = buffer;
11260 input->cur = buffer;
Daniel Veillard48b2f892001-02-25 16:11:03 +000011261 input->end = &buffer[xmlStrlen(buffer)];
Owen Taylor3473f882001-02-23 17:55:21 +000011262 inputPush(ctxt, input);
11263}
11264
11265/**
11266 * xmlSAXUserParseFile:
11267 * @sax: a SAX handler
11268 * @user_data: The user data returned on SAX callbacks
11269 * @filename: a file name
11270 *
11271 * parse an XML file and call the given SAX handler routines.
11272 * Automatic support for ZLIB/Compress compressed document is provided
11273 *
11274 * Returns 0 in case of success or a error number otherwise
11275 */
11276int
11277xmlSAXUserParseFile(xmlSAXHandlerPtr sax, void *user_data,
11278 const char *filename) {
11279 int ret = 0;
11280 xmlParserCtxtPtr ctxt;
11281
11282 ctxt = xmlCreateFileParserCtxt(filename);
11283 if (ctxt == NULL) return -1;
Daniel Veillard81273902003-09-30 00:43:48 +000011284#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +000011285 if (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
Daniel Veillard81273902003-09-30 00:43:48 +000011286#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011287 xmlFree(ctxt->sax);
11288 ctxt->sax = sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000011289 xmlDetectSAX2(ctxt);
11290
Owen Taylor3473f882001-02-23 17:55:21 +000011291 if (user_data != NULL)
11292 ctxt->userData = user_data;
11293
11294 xmlParseDocument(ctxt);
11295
11296 if (ctxt->wellFormed)
11297 ret = 0;
11298 else {
11299 if (ctxt->errNo != 0)
11300 ret = ctxt->errNo;
11301 else
11302 ret = -1;
11303 }
11304 if (sax != NULL)
11305 ctxt->sax = NULL;
11306 xmlFreeParserCtxt(ctxt);
11307
11308 return ret;
11309}
Daniel Veillard81273902003-09-30 00:43:48 +000011310#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011311
11312/************************************************************************
11313 * *
11314 * Front ends when parsing from memory *
11315 * *
11316 ************************************************************************/
11317
11318/**
11319 * xmlCreateMemoryParserCtxt:
11320 * @buffer: a pointer to a char array
11321 * @size: the size of the array
11322 *
11323 * Create a parser context for an XML in-memory document.
11324 *
11325 * Returns the new parser context or NULL
11326 */
11327xmlParserCtxtPtr
Daniel Veillardfd7ddca2001-05-16 10:57:35 +000011328xmlCreateMemoryParserCtxt(const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000011329 xmlParserCtxtPtr ctxt;
11330 xmlParserInputPtr input;
11331 xmlParserInputBufferPtr buf;
11332
11333 if (buffer == NULL)
11334 return(NULL);
11335 if (size <= 0)
11336 return(NULL);
11337
11338 ctxt = xmlNewParserCtxt();
11339 if (ctxt == NULL)
11340 return(NULL);
11341
Daniel Veillard53350552003-09-18 13:35:51 +000011342 /* TODO: xmlParserInputBufferCreateStatic, requires some serious changes */
Owen Taylor3473f882001-02-23 17:55:21 +000011343 buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
Daniel Veillarda7e05b42002-11-19 08:11:14 +000011344 if (buf == NULL) {
11345 xmlFreeParserCtxt(ctxt);
11346 return(NULL);
11347 }
Owen Taylor3473f882001-02-23 17:55:21 +000011348
11349 input = xmlNewInputStream(ctxt);
11350 if (input == NULL) {
Daniel Veillarda7e05b42002-11-19 08:11:14 +000011351 xmlFreeParserInputBuffer(buf);
Owen Taylor3473f882001-02-23 17:55:21 +000011352 xmlFreeParserCtxt(ctxt);
11353 return(NULL);
11354 }
11355
11356 input->filename = NULL;
11357 input->buf = buf;
11358 input->base = input->buf->buffer->content;
11359 input->cur = input->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +000011360 input->end = &input->buf->buffer->content[input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +000011361
11362 inputPush(ctxt, input);
11363 return(ctxt);
11364}
11365
Daniel Veillard81273902003-09-30 00:43:48 +000011366#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000011367/**
Daniel Veillard8606bbb2002-11-12 12:36:52 +000011368 * xmlSAXParseMemoryWithData:
11369 * @sax: the SAX handler block
11370 * @buffer: an pointer to a char array
11371 * @size: the size of the array
11372 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11373 * documents
11374 * @data: the userdata
11375 *
11376 * parse an XML in-memory block and use the given SAX function block
11377 * to handle the parsing callback. If sax is NULL, fallback to the default
11378 * DOM tree building routines.
11379 *
11380 * User data (void *) is stored within the parser context in the
11381 * context's _private member, so it is available nearly everywhere in libxml
11382 *
11383 * Returns the resulting document tree
11384 */
11385
11386xmlDocPtr
11387xmlSAXParseMemoryWithData(xmlSAXHandlerPtr sax, const char *buffer,
11388 int size, int recovery, void *data) {
11389 xmlDocPtr ret;
11390 xmlParserCtxtPtr ctxt;
11391
11392 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
11393 if (ctxt == NULL) return(NULL);
11394 if (sax != NULL) {
11395 if (ctxt->sax != NULL)
11396 xmlFree(ctxt->sax);
11397 ctxt->sax = sax;
11398 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000011399 xmlDetectSAX2(ctxt);
Daniel Veillard8606bbb2002-11-12 12:36:52 +000011400 if (data!=NULL) {
11401 ctxt->_private=data;
11402 }
11403
Daniel Veillardadba5f12003-04-04 16:09:01 +000011404 ctxt->recovery = recovery;
11405
Daniel Veillard8606bbb2002-11-12 12:36:52 +000011406 xmlParseDocument(ctxt);
11407
11408 if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc;
11409 else {
11410 ret = NULL;
11411 xmlFreeDoc(ctxt->myDoc);
11412 ctxt->myDoc = NULL;
11413 }
11414 if (sax != NULL)
11415 ctxt->sax = NULL;
11416 xmlFreeParserCtxt(ctxt);
11417
11418 return(ret);
11419}
11420
11421/**
Owen Taylor3473f882001-02-23 17:55:21 +000011422 * xmlSAXParseMemory:
11423 * @sax: the SAX handler block
11424 * @buffer: an pointer to a char array
11425 * @size: the size of the array
11426 * @recovery: work in recovery mode, i.e. tries to read not Well Formed
11427 * documents
11428 *
11429 * parse an XML in-memory block and use the given SAX function block
11430 * to handle the parsing callback. If sax is NULL, fallback to the default
11431 * DOM tree building routines.
11432 *
11433 * Returns the resulting document tree
11434 */
11435xmlDocPtr
Daniel Veillard50822cb2001-07-26 20:05:51 +000011436xmlSAXParseMemory(xmlSAXHandlerPtr sax, const char *buffer,
11437 int size, int recovery) {
Daniel Veillard8606bbb2002-11-12 12:36:52 +000011438 return xmlSAXParseMemoryWithData(sax, buffer, size, recovery, NULL);
Owen Taylor3473f882001-02-23 17:55:21 +000011439}
11440
11441/**
11442 * xmlParseMemory:
11443 * @buffer: an pointer to a char array
11444 * @size: the size of the array
11445 *
11446 * parse an XML in-memory block and build a tree.
11447 *
11448 * Returns the resulting document tree
11449 */
11450
Daniel Veillard50822cb2001-07-26 20:05:51 +000011451xmlDocPtr xmlParseMemory(const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000011452 return(xmlSAXParseMemory(NULL, buffer, size, 0));
11453}
11454
11455/**
11456 * xmlRecoverMemory:
11457 * @buffer: an pointer to a char array
11458 * @size: the size of the array
11459 *
11460 * parse an XML in-memory block and build a tree.
11461 * In the case the document is not Well Formed, a tree is built anyway
11462 *
11463 * Returns the resulting document tree
11464 */
11465
Daniel Veillard50822cb2001-07-26 20:05:51 +000011466xmlDocPtr xmlRecoverMemory(const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000011467 return(xmlSAXParseMemory(NULL, buffer, size, 1));
11468}
11469
11470/**
11471 * xmlSAXUserParseMemory:
11472 * @sax: a SAX handler
11473 * @user_data: The user data returned on SAX callbacks
11474 * @buffer: an in-memory XML document input
11475 * @size: the length of the XML document in bytes
11476 *
11477 * A better SAX parsing routine.
11478 * parse an XML in-memory buffer and call the given SAX handler routines.
11479 *
11480 * Returns 0 in case of success or a error number otherwise
11481 */
11482int xmlSAXUserParseMemory(xmlSAXHandlerPtr sax, void *user_data,
Daniel Veillardfd7ddca2001-05-16 10:57:35 +000011483 const char *buffer, int size) {
Owen Taylor3473f882001-02-23 17:55:21 +000011484 int ret = 0;
11485 xmlParserCtxtPtr ctxt;
11486 xmlSAXHandlerPtr oldsax = NULL;
11487
Daniel Veillard9e923512002-08-14 08:48:52 +000011488 if (sax == NULL) return -1;
Owen Taylor3473f882001-02-23 17:55:21 +000011489 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
11490 if (ctxt == NULL) return -1;
Daniel Veillard9e923512002-08-14 08:48:52 +000011491 oldsax = ctxt->sax;
11492 ctxt->sax = sax;
Daniel Veillarde57ec792003-09-10 10:50:59 +000011493 xmlDetectSAX2(ctxt);
Daniel Veillard30211a02001-04-26 09:33:18 +000011494 if (user_data != NULL)
11495 ctxt->userData = user_data;
Owen Taylor3473f882001-02-23 17:55:21 +000011496
11497 xmlParseDocument(ctxt);
11498
11499 if (ctxt->wellFormed)
11500 ret = 0;
11501 else {
11502 if (ctxt->errNo != 0)
11503 ret = ctxt->errNo;
11504 else
11505 ret = -1;
11506 }
Daniel Veillard9e923512002-08-14 08:48:52 +000011507 ctxt->sax = oldsax;
Owen Taylor3473f882001-02-23 17:55:21 +000011508 xmlFreeParserCtxt(ctxt);
11509
11510 return ret;
11511}
Daniel Veillard81273902003-09-30 00:43:48 +000011512#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011513
11514/**
11515 * xmlCreateDocParserCtxt:
11516 * @cur: a pointer to an array of xmlChar
11517 *
11518 * Creates a parser context for an XML in-memory document.
11519 *
11520 * Returns the new parser context or NULL
11521 */
11522xmlParserCtxtPtr
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011523xmlCreateDocParserCtxt(const xmlChar *cur) {
Owen Taylor3473f882001-02-23 17:55:21 +000011524 int len;
11525
11526 if (cur == NULL)
11527 return(NULL);
11528 len = xmlStrlen(cur);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011529 return(xmlCreateMemoryParserCtxt((const char *)cur, len));
Owen Taylor3473f882001-02-23 17:55:21 +000011530}
11531
Daniel Veillard81273902003-09-30 00:43:48 +000011532#ifdef LIBXML_SAX1_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000011533/**
11534 * xmlSAXParseDoc:
11535 * @sax: the SAX handler block
11536 * @cur: a pointer to an array of xmlChar
11537 * @recovery: work in recovery mode, i.e. tries to read no Well Formed
11538 * documents
11539 *
11540 * parse an XML in-memory document and build a tree.
11541 * It use the given SAX function block to handle the parsing callback.
11542 * If sax is NULL, fallback to the default DOM tree building routines.
11543 *
11544 * Returns the resulting document tree
11545 */
11546
11547xmlDocPtr
11548xmlSAXParseDoc(xmlSAXHandlerPtr sax, xmlChar *cur, int recovery) {
11549 xmlDocPtr ret;
11550 xmlParserCtxtPtr ctxt;
11551
11552 if (cur == NULL) return(NULL);
11553
11554
11555 ctxt = xmlCreateDocParserCtxt(cur);
11556 if (ctxt == NULL) return(NULL);
11557 if (sax != NULL) {
11558 ctxt->sax = sax;
11559 ctxt->userData = NULL;
11560 }
Daniel Veillarde57ec792003-09-10 10:50:59 +000011561 xmlDetectSAX2(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +000011562
11563 xmlParseDocument(ctxt);
11564 if ((ctxt->wellFormed) || recovery) ret = ctxt->myDoc;
11565 else {
11566 ret = NULL;
11567 xmlFreeDoc(ctxt->myDoc);
11568 ctxt->myDoc = NULL;
11569 }
11570 if (sax != NULL)
11571 ctxt->sax = NULL;
11572 xmlFreeParserCtxt(ctxt);
11573
11574 return(ret);
11575}
11576
11577/**
11578 * xmlParseDoc:
11579 * @cur: a pointer to an array of xmlChar
11580 *
11581 * parse an XML in-memory document and build a tree.
11582 *
11583 * Returns the resulting document tree
11584 */
11585
11586xmlDocPtr
11587xmlParseDoc(xmlChar *cur) {
11588 return(xmlSAXParseDoc(NULL, cur, 0));
11589}
Daniel Veillard81273902003-09-30 00:43:48 +000011590#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011591
Daniel Veillard81273902003-09-30 00:43:48 +000011592#ifdef LIBXML_LEGACY_ENABLED
Daniel Veillard8107a222002-01-13 14:10:10 +000011593/************************************************************************
11594 * *
11595 * Specific function to keep track of entities references *
11596 * and used by the XSLT debugger *
11597 * *
11598 ************************************************************************/
11599
11600static xmlEntityReferenceFunc xmlEntityRefFunc = NULL;
11601
11602/**
11603 * xmlAddEntityReference:
11604 * @ent : A valid entity
11605 * @firstNode : A valid first node for children of entity
11606 * @lastNode : A valid last node of children entity
11607 *
11608 * Notify of a reference to an entity of type XML_EXTERNAL_GENERAL_PARSED_ENTITY
11609 */
11610static void
11611xmlAddEntityReference(xmlEntityPtr ent, xmlNodePtr firstNode,
11612 xmlNodePtr lastNode)
11613{
11614 if (xmlEntityRefFunc != NULL) {
11615 (*xmlEntityRefFunc) (ent, firstNode, lastNode);
11616 }
11617}
11618
11619
11620/**
11621 * xmlSetEntityReferenceFunc:
Daniel Veillard01c13b52002-12-10 15:19:08 +000011622 * @func: A valid function
Daniel Veillard8107a222002-01-13 14:10:10 +000011623 *
11624 * Set the function to call call back when a xml reference has been made
11625 */
11626void
11627xmlSetEntityReferenceFunc(xmlEntityReferenceFunc func)
11628{
11629 xmlEntityRefFunc = func;
11630}
Daniel Veillard81273902003-09-30 00:43:48 +000011631#endif /* LIBXML_LEGACY_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011632
11633/************************************************************************
11634 * *
11635 * Miscellaneous *
11636 * *
11637 ************************************************************************/
11638
11639#ifdef LIBXML_XPATH_ENABLED
11640#include <libxml/xpath.h>
11641#endif
11642
Daniel Veillarddb5850a2002-01-18 11:49:26 +000011643extern void xmlGenericErrorDefaultFunc(void *ctx, const char *msg, ...);
Owen Taylor3473f882001-02-23 17:55:21 +000011644static int xmlParserInitialized = 0;
11645
11646/**
11647 * xmlInitParser:
11648 *
11649 * Initialization function for the XML parser.
11650 * This is not reentrant. Call once before processing in case of
11651 * use in multithreaded programs.
11652 */
11653
11654void
11655xmlInitParser(void) {
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000011656 if (xmlParserInitialized != 0)
11657 return;
Owen Taylor3473f882001-02-23 17:55:21 +000011658
Daniel Veillarddb5850a2002-01-18 11:49:26 +000011659 if ((xmlGenericError == xmlGenericErrorDefaultFunc) ||
11660 (xmlGenericError == NULL))
11661 initGenericErrorDefaultFunc(NULL);
Daniel Veillard781ac8b2003-05-15 22:11:36 +000011662 xmlInitGlobals();
Daniel Veillardd0463562001-10-13 09:15:48 +000011663 xmlInitThreads();
Daniel Veillard6f350292001-10-14 09:56:15 +000011664 xmlInitMemory();
Owen Taylor3473f882001-02-23 17:55:21 +000011665 xmlInitCharEncodingHandlers();
Owen Taylor3473f882001-02-23 17:55:21 +000011666 xmlDefaultSAXHandlerInit();
11667 xmlRegisterDefaultInputCallbacks();
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000011668#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +000011669 xmlRegisterDefaultOutputCallbacks();
Daniel Veillarda9cce9c2003-09-29 13:20:24 +000011670#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +000011671#ifdef LIBXML_HTML_ENABLED
11672 htmlInitAutoClose();
11673 htmlDefaultSAXHandlerInit();
11674#endif
11675#ifdef LIBXML_XPATH_ENABLED
11676 xmlXPathInit();
11677#endif
11678 xmlParserInitialized = 1;
11679}
11680
11681/**
11682 * xmlCleanupParser:
11683 *
Daniel Veillardd8cf9062003-11-11 21:12:36 +000011684 * Cleanup function for the XML library. It tries to reclaim all
11685 * parsing related global memory allocated for the library processing.
Owen Taylor3473f882001-02-23 17:55:21 +000011686 * It doesn't deallocate any document related memory. Calling this
Daniel Veillardd8cf9062003-11-11 21:12:36 +000011687 * function should not prevent reusing the library but one should
11688 * call xmlCleanupParser() only when the process has
Daniel Veillard7424eb62003-01-24 14:14:52 +000011689 * finished using the library or XML document built with it.
Owen Taylor3473f882001-02-23 17:55:21 +000011690 */
11691
11692void
11693xmlCleanupParser(void) {
Daniel Veillard7fb801f2003-08-17 21:07:26 +000011694 if (!xmlParserInitialized)
11695 return;
11696
Owen Taylor3473f882001-02-23 17:55:21 +000011697 xmlCleanupCharEncodingHandlers();
Daniel Veillarde2940dd2001-08-22 00:06:49 +000011698#ifdef LIBXML_CATALOG_ENABLED
11699 xmlCatalogCleanup();
11700#endif
Daniel Veillard04054be2003-10-15 10:48:54 +000011701 xmlCleanupInputCallbacks();
11702#ifdef LIBXML_OUTPUT_ENABLED
11703 xmlCleanupOutputCallbacks();
11704#endif
Daniel Veillard781ac8b2003-05-15 22:11:36 +000011705 xmlCleanupGlobals();
Daniel Veillard2b8c4a12003-10-02 22:28:19 +000011706 xmlResetLastError();
Daniel Veillard74c0e592003-11-25 07:01:38 +000011707 xmlCleanupThreads(); /* must be last if called not from the main thread */
William M. Brack72ee48d2003-12-30 08:30:19 +000011708 xmlCleanupMemory();
Daniel Veillardd0463562001-10-13 09:15:48 +000011709 xmlParserInitialized = 0;
Owen Taylor3473f882001-02-23 17:55:21 +000011710}
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011711
11712/************************************************************************
11713 * *
11714 * New set (2.6.0) of simpler and more flexible APIs *
11715 * *
11716 ************************************************************************/
11717
11718/**
Daniel Veillarde96a2a42003-09-24 21:23:56 +000011719 * DICT_FREE:
11720 * @str: a string
11721 *
11722 * Free a string if it is not owned by the "dict" dictionnary in the
11723 * current scope
11724 */
11725#define DICT_FREE(str) \
11726 if ((str) && ((!dict) || \
11727 (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
11728 xmlFree((char *)(str));
11729
11730/**
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011731 * xmlCtxtReset:
11732 * @ctxt: an XML parser context
11733 *
11734 * Reset a parser context
11735 */
11736void
11737xmlCtxtReset(xmlParserCtxtPtr ctxt)
11738{
11739 xmlParserInputPtr input;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000011740 xmlDictPtr dict = ctxt->dict;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011741
11742 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
11743 xmlFreeInputStream(input);
11744 }
11745 ctxt->inputNr = 0;
11746 ctxt->input = NULL;
11747
11748 ctxt->spaceNr = 0;
11749 ctxt->spaceTab[0] = -1;
11750 ctxt->space = &ctxt->spaceTab[0];
11751
11752
11753 ctxt->nodeNr = 0;
11754 ctxt->node = NULL;
11755
11756 ctxt->nameNr = 0;
11757 ctxt->name = NULL;
11758
Daniel Veillarde96a2a42003-09-24 21:23:56 +000011759 DICT_FREE(ctxt->version);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011760 ctxt->version = NULL;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000011761 DICT_FREE(ctxt->encoding);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011762 ctxt->encoding = NULL;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000011763 DICT_FREE(ctxt->directory);
11764 ctxt->directory = NULL;
11765 DICT_FREE(ctxt->extSubURI);
11766 ctxt->extSubURI = NULL;
11767 DICT_FREE(ctxt->extSubSystem);
11768 ctxt->extSubSystem = NULL;
11769 if (ctxt->myDoc != NULL)
11770 xmlFreeDoc(ctxt->myDoc);
11771 ctxt->myDoc = NULL;
11772
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011773 ctxt->standalone = -1;
11774 ctxt->hasExternalSubset = 0;
11775 ctxt->hasPErefs = 0;
11776 ctxt->html = 0;
11777 ctxt->external = 0;
11778 ctxt->instate = XML_PARSER_START;
11779 ctxt->token = 0;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011780
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011781 ctxt->wellFormed = 1;
11782 ctxt->nsWellFormed = 1;
Daniel Veillardae289182004-01-21 16:00:43 +000011783 ctxt->disableSAX = 0;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011784 ctxt->valid = 1;
Daniel Veillard766c4f92004-03-26 10:48:29 +000011785#if 0
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011786 ctxt->vctxt.userData = ctxt;
11787 ctxt->vctxt.error = xmlParserValidityError;
11788 ctxt->vctxt.warning = xmlParserValidityWarning;
Daniel Veillard766c4f92004-03-26 10:48:29 +000011789#endif
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011790 ctxt->record_info = 0;
11791 ctxt->nbChars = 0;
11792 ctxt->checkIndex = 0;
11793 ctxt->inSubset = 0;
11794 ctxt->errNo = XML_ERR_OK;
11795 ctxt->depth = 0;
11796 ctxt->charset = XML_CHAR_ENCODING_UTF8;
11797 ctxt->catalogs = NULL;
11798 xmlInitNodeInfoSeq(&ctxt->node_seq);
11799
11800 if (ctxt->attsDefault != NULL) {
11801 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
11802 ctxt->attsDefault = NULL;
11803 }
11804 if (ctxt->attsSpecial != NULL) {
11805 xmlHashFree(ctxt->attsSpecial, NULL);
11806 ctxt->attsSpecial = NULL;
11807 }
11808
Daniel Veillard4432df22003-09-28 18:58:27 +000011809#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011810 if (ctxt->catalogs != NULL)
11811 xmlCatalogFreeLocal(ctxt->catalogs);
Daniel Veillard4432df22003-09-28 18:58:27 +000011812#endif
Daniel Veillardcc199e02003-10-24 21:11:48 +000011813 if (ctxt->lastError.code != XML_ERR_OK)
11814 xmlResetError(&ctxt->lastError);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011815}
11816
11817/**
Daniel Veillard9ba8e382003-10-28 21:31:45 +000011818 * xmlCtxtResetPush:
11819 * @ctxt: an XML parser context
11820 * @chunk: a pointer to an array of chars
11821 * @size: number of chars in the array
11822 * @filename: an optional file name or URI
11823 * @encoding: the document encoding, or NULL
11824 *
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000011825 * Reset a push parser context
11826 *
11827 * Returns 0 in case of success and 1 in case of error
Daniel Veillard9ba8e382003-10-28 21:31:45 +000011828 */
11829int
11830xmlCtxtResetPush(xmlParserCtxtPtr ctxt, const char *chunk,
11831 int size, const char *filename, const char *encoding)
11832{
11833 xmlParserInputPtr inputStream;
11834 xmlParserInputBufferPtr buf;
11835 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
11836
Daniel Veillarde4e3f5d2003-10-28 23:06:32 +000011837 if (ctxt == NULL)
11838 return(1);
11839
Daniel Veillard9ba8e382003-10-28 21:31:45 +000011840 if ((encoding == NULL) && (chunk != NULL) && (size >= 4))
11841 enc = xmlDetectCharEncoding((const xmlChar *) chunk, size);
11842
11843 buf = xmlAllocParserInputBuffer(enc);
11844 if (buf == NULL)
11845 return(1);
11846
11847 if (ctxt == NULL) {
11848 xmlFreeParserInputBuffer(buf);
11849 return(1);
11850 }
11851
11852 xmlCtxtReset(ctxt);
11853
11854 if (ctxt->pushTab == NULL) {
11855 ctxt->pushTab = (void **) xmlMalloc(ctxt->nameMax * 3 *
11856 sizeof(xmlChar *));
11857 if (ctxt->pushTab == NULL) {
11858 xmlErrMemory(ctxt, NULL);
11859 xmlFreeParserInputBuffer(buf);
11860 return(1);
11861 }
11862 }
11863
11864 if (filename == NULL) {
11865 ctxt->directory = NULL;
11866 } else {
11867 ctxt->directory = xmlParserGetDirectory(filename);
11868 }
11869
11870 inputStream = xmlNewInputStream(ctxt);
11871 if (inputStream == NULL) {
11872 xmlFreeParserInputBuffer(buf);
11873 return(1);
11874 }
11875
11876 if (filename == NULL)
11877 inputStream->filename = NULL;
11878 else
11879 inputStream->filename = (char *)
11880 xmlCanonicPath((const xmlChar *) filename);
11881 inputStream->buf = buf;
11882 inputStream->base = inputStream->buf->buffer->content;
11883 inputStream->cur = inputStream->buf->buffer->content;
11884 inputStream->end =
11885 &inputStream->buf->buffer->content[inputStream->buf->buffer->use];
11886
11887 inputPush(ctxt, inputStream);
11888
11889 if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
11890 (ctxt->input->buf != NULL)) {
11891 int base = ctxt->input->base - ctxt->input->buf->buffer->content;
11892 int cur = ctxt->input->cur - ctxt->input->base;
11893
11894 xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
11895
11896 ctxt->input->base = ctxt->input->buf->buffer->content + base;
11897 ctxt->input->cur = ctxt->input->base + cur;
11898 ctxt->input->end =
11899 &ctxt->input->buf->buffer->content[ctxt->input->buf->buffer->
11900 use];
11901#ifdef DEBUG_PUSH
11902 xmlGenericError(xmlGenericErrorContext, "PP: pushed %d\n", size);
11903#endif
11904 }
11905
11906 if (encoding != NULL) {
11907 xmlCharEncodingHandlerPtr hdlr;
11908
11909 hdlr = xmlFindCharEncodingHandler(encoding);
11910 if (hdlr != NULL) {
11911 xmlSwitchToEncoding(ctxt, hdlr);
11912 } else {
11913 xmlFatalErrMsgStr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
11914 "Unsupported encoding %s\n", BAD_CAST encoding);
11915 }
11916 } else if (enc != XML_CHAR_ENCODING_NONE) {
11917 xmlSwitchEncoding(ctxt, enc);
11918 }
11919
11920 return(0);
11921}
11922
11923/**
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011924 * xmlCtxtUseOptions:
11925 * @ctxt: an XML parser context
Daniel Veillard87ab1c12003-12-21 13:01:56 +000011926 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011927 *
11928 * Applies the options to the parser context
11929 *
11930 * Returns 0 in case of success, the set of unknown or unimplemented options
11931 * in case of error.
11932 */
11933int
11934xmlCtxtUseOptions(xmlParserCtxtPtr ctxt, int options)
11935{
11936 if (options & XML_PARSE_RECOVER) {
11937 ctxt->recovery = 1;
11938 options -= XML_PARSE_RECOVER;
11939 } else
11940 ctxt->recovery = 0;
11941 if (options & XML_PARSE_DTDLOAD) {
11942 ctxt->loadsubset = XML_DETECT_IDS;
11943 options -= XML_PARSE_DTDLOAD;
11944 } else
11945 ctxt->loadsubset = 0;
11946 if (options & XML_PARSE_DTDATTR) {
11947 ctxt->loadsubset |= XML_COMPLETE_ATTRS;
11948 options -= XML_PARSE_DTDATTR;
11949 }
11950 if (options & XML_PARSE_NOENT) {
11951 ctxt->replaceEntities = 1;
11952 /* ctxt->loadsubset |= XML_DETECT_IDS; */
11953 options -= XML_PARSE_NOENT;
11954 } else
11955 ctxt->replaceEntities = 0;
11956 if (options & XML_PARSE_NOWARNING) {
11957 ctxt->sax->warning = NULL;
11958 options -= XML_PARSE_NOWARNING;
11959 }
11960 if (options & XML_PARSE_NOERROR) {
11961 ctxt->sax->error = NULL;
11962 ctxt->sax->fatalError = NULL;
11963 options -= XML_PARSE_NOERROR;
11964 }
11965 if (options & XML_PARSE_PEDANTIC) {
11966 ctxt->pedantic = 1;
11967 options -= XML_PARSE_PEDANTIC;
11968 } else
11969 ctxt->pedantic = 0;
11970 if (options & XML_PARSE_NOBLANKS) {
11971 ctxt->keepBlanks = 0;
11972 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
11973 options -= XML_PARSE_NOBLANKS;
11974 } else
11975 ctxt->keepBlanks = 1;
11976 if (options & XML_PARSE_DTDVALID) {
11977 ctxt->validate = 1;
11978 if (options & XML_PARSE_NOWARNING)
11979 ctxt->vctxt.warning = NULL;
11980 if (options & XML_PARSE_NOERROR)
11981 ctxt->vctxt.error = NULL;
11982 options -= XML_PARSE_DTDVALID;
11983 } else
11984 ctxt->validate = 0;
Daniel Veillard81273902003-09-30 00:43:48 +000011985#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard16fa96c2003-09-23 21:50:54 +000011986 if (options & XML_PARSE_SAX1) {
11987 ctxt->sax->startElement = xmlSAX2StartElement;
11988 ctxt->sax->endElement = xmlSAX2EndElement;
11989 ctxt->sax->startElementNs = NULL;
11990 ctxt->sax->endElementNs = NULL;
11991 ctxt->sax->initialized = 1;
11992 options -= XML_PARSE_SAX1;
11993 }
Daniel Veillard81273902003-09-30 00:43:48 +000011994#endif /* LIBXML_SAX1_ENABLED */
Daniel Veillarde96a2a42003-09-24 21:23:56 +000011995 if (options & XML_PARSE_NODICT) {
11996 ctxt->dictNames = 0;
11997 options -= XML_PARSE_NODICT;
11998 } else {
11999 ctxt->dictNames = 1;
12000 }
Daniel Veillarddca8cc72003-09-26 13:53:14 +000012001 if (options & XML_PARSE_NOCDATA) {
12002 ctxt->sax->cdataBlock = NULL;
12003 options -= XML_PARSE_NOCDATA;
12004 }
12005 if (options & XML_PARSE_NSCLEAN) {
12006 ctxt->options |= XML_PARSE_NSCLEAN;
12007 options -= XML_PARSE_NSCLEAN;
12008 }
Daniel Veillard61b93382003-11-03 14:28:31 +000012009 if (options & XML_PARSE_NONET) {
12010 ctxt->options |= XML_PARSE_NONET;
12011 options -= XML_PARSE_NONET;
12012 }
Daniel Veillard7ec29972003-10-31 14:36:36 +000012013 ctxt->linenumbers = 1;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012014 return (options);
12015}
12016
12017/**
12018 * xmlDoRead:
12019 * @ctxt: an XML parser context
Daniel Veillard60942de2003-09-25 21:05:58 +000012020 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012021 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012022 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012023 * @reuse: keep the context for reuse
12024 *
12025 * Common front-end for the xmlRead functions
12026 *
12027 * Returns the resulting document tree or NULL
12028 */
12029static xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012030xmlDoRead(xmlParserCtxtPtr ctxt, const char *URL, const char *encoding,
12031 int options, int reuse)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012032{
12033 xmlDocPtr ret;
12034
12035 xmlCtxtUseOptions(ctxt, options);
12036 if (encoding != NULL) {
12037 xmlCharEncodingHandlerPtr hdlr;
12038
12039 hdlr = xmlFindCharEncodingHandler(encoding);
12040 if (hdlr != NULL)
12041 xmlSwitchToEncoding(ctxt, hdlr);
12042 }
Daniel Veillard60942de2003-09-25 21:05:58 +000012043 if ((URL != NULL) && (ctxt->input != NULL) &&
12044 (ctxt->input->filename == NULL))
12045 ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012046 xmlParseDocument(ctxt);
12047 if ((ctxt->wellFormed) || ctxt->recovery)
12048 ret = ctxt->myDoc;
12049 else {
12050 ret = NULL;
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012051 if (ctxt->myDoc != NULL) {
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012052 xmlFreeDoc(ctxt->myDoc);
12053 }
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012054 }
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012055 ctxt->myDoc = NULL;
12056 if (!reuse) {
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012057 xmlFreeParserCtxt(ctxt);
Daniel Veillarde96a2a42003-09-24 21:23:56 +000012058 }
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012059
12060 return (ret);
12061}
12062
12063/**
12064 * xmlReadDoc:
12065 * @cur: a pointer to a zero terminated string
Daniel Veillard60942de2003-09-25 21:05:58 +000012066 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012067 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012068 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012069 *
12070 * parse an XML in-memory document and build a tree.
12071 *
12072 * Returns the resulting document tree
12073 */
12074xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012075xmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012076{
12077 xmlParserCtxtPtr ctxt;
12078
12079 if (cur == NULL)
12080 return (NULL);
12081
12082 ctxt = xmlCreateDocParserCtxt(cur);
12083 if (ctxt == NULL)
12084 return (NULL);
Daniel Veillard60942de2003-09-25 21:05:58 +000012085 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012086}
12087
12088/**
12089 * xmlReadFile:
12090 * @filename: a file or URL
12091 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012092 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012093 *
12094 * parse an XML file from the filesystem or the network.
12095 *
12096 * Returns the resulting document tree
12097 */
12098xmlDocPtr
12099xmlReadFile(const char *filename, const char *encoding, int options)
12100{
12101 xmlParserCtxtPtr ctxt;
12102
Daniel Veillard61b93382003-11-03 14:28:31 +000012103 ctxt = xmlCreateURLParserCtxt(filename, options);
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012104 if (ctxt == NULL)
12105 return (NULL);
Daniel Veillard60942de2003-09-25 21:05:58 +000012106 return (xmlDoRead(ctxt, NULL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012107}
12108
12109/**
12110 * xmlReadMemory:
12111 * @buffer: a pointer to a char array
12112 * @size: the size of the array
Daniel Veillard60942de2003-09-25 21:05:58 +000012113 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012114 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012115 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012116 *
12117 * parse an XML in-memory document and build a tree.
12118 *
12119 * Returns the resulting document tree
12120 */
12121xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012122xmlReadMemory(const char *buffer, int size, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012123{
12124 xmlParserCtxtPtr ctxt;
12125
12126 ctxt = xmlCreateMemoryParserCtxt(buffer, size);
12127 if (ctxt == NULL)
12128 return (NULL);
Daniel Veillard60942de2003-09-25 21:05:58 +000012129 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012130}
12131
12132/**
12133 * xmlReadFd:
12134 * @fd: an open file descriptor
Daniel Veillard60942de2003-09-25 21:05:58 +000012135 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012136 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012137 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012138 *
12139 * parse an XML from a file descriptor and build a tree.
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012140 * NOTE that the file descriptor will not be closed when the
12141 * reader is closed or reset.
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012142 *
12143 * Returns the resulting document tree
12144 */
12145xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012146xmlReadFd(int fd, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012147{
12148 xmlParserCtxtPtr ctxt;
12149 xmlParserInputBufferPtr input;
12150 xmlParserInputPtr stream;
12151
12152 if (fd < 0)
12153 return (NULL);
12154
12155 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
12156 if (input == NULL)
12157 return (NULL);
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012158 input->closecallback = NULL;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012159 ctxt = xmlNewParserCtxt();
12160 if (ctxt == NULL) {
12161 xmlFreeParserInputBuffer(input);
12162 return (NULL);
12163 }
12164 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12165 if (stream == NULL) {
12166 xmlFreeParserInputBuffer(input);
12167 xmlFreeParserCtxt(ctxt);
12168 return (NULL);
12169 }
12170 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012171 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012172}
12173
12174/**
12175 * xmlReadIO:
12176 * @ioread: an I/O read function
12177 * @ioclose: an I/O close function
12178 * @ioctx: an I/O handler
Daniel Veillard60942de2003-09-25 21:05:58 +000012179 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012180 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012181 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012182 *
12183 * parse an XML document from I/O functions and source and build a tree.
12184 *
12185 * Returns the resulting document tree
12186 */
12187xmlDocPtr
12188xmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
Daniel Veillard60942de2003-09-25 21:05:58 +000012189 void *ioctx, const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012190{
12191 xmlParserCtxtPtr ctxt;
12192 xmlParserInputBufferPtr input;
12193 xmlParserInputPtr stream;
12194
12195 if (ioread == NULL)
12196 return (NULL);
12197
12198 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
12199 XML_CHAR_ENCODING_NONE);
12200 if (input == NULL)
12201 return (NULL);
12202 ctxt = xmlNewParserCtxt();
12203 if (ctxt == NULL) {
12204 xmlFreeParserInputBuffer(input);
12205 return (NULL);
12206 }
12207 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12208 if (stream == NULL) {
12209 xmlFreeParserInputBuffer(input);
12210 xmlFreeParserCtxt(ctxt);
12211 return (NULL);
12212 }
12213 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012214 return (xmlDoRead(ctxt, URL, encoding, options, 0));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012215}
12216
12217/**
12218 * xmlCtxtReadDoc:
12219 * @ctxt: an XML parser context
12220 * @cur: a pointer to a zero terminated string
Daniel Veillard60942de2003-09-25 21:05:58 +000012221 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012222 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012223 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012224 *
12225 * parse an XML in-memory document and build a tree.
12226 * This reuses the existing @ctxt parser context
12227 *
12228 * Returns the resulting document tree
12229 */
12230xmlDocPtr
12231xmlCtxtReadDoc(xmlParserCtxtPtr ctxt, const xmlChar * cur,
Daniel Veillard60942de2003-09-25 21:05:58 +000012232 const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012233{
12234 xmlParserInputPtr stream;
12235
12236 if (cur == NULL)
12237 return (NULL);
12238 if (ctxt == NULL)
12239 return (NULL);
12240
12241 xmlCtxtReset(ctxt);
12242
12243 stream = xmlNewStringInputStream(ctxt, cur);
12244 if (stream == NULL) {
12245 return (NULL);
12246 }
12247 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012248 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012249}
12250
12251/**
12252 * xmlCtxtReadFile:
12253 * @ctxt: an XML parser context
12254 * @filename: a file or URL
12255 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012256 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012257 *
12258 * parse an XML file from the filesystem or the network.
12259 * This reuses the existing @ctxt parser context
12260 *
12261 * Returns the resulting document tree
12262 */
12263xmlDocPtr
12264xmlCtxtReadFile(xmlParserCtxtPtr ctxt, const char *filename,
12265 const char *encoding, int options)
12266{
12267 xmlParserInputPtr stream;
12268
12269 if (filename == NULL)
12270 return (NULL);
12271 if (ctxt == NULL)
12272 return (NULL);
12273
12274 xmlCtxtReset(ctxt);
12275
12276 stream = xmlNewInputFromFile(ctxt, filename);
12277 if (stream == NULL) {
12278 return (NULL);
12279 }
12280 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012281 return (xmlDoRead(ctxt, NULL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012282}
12283
12284/**
12285 * xmlCtxtReadMemory:
12286 * @ctxt: an XML parser context
12287 * @buffer: a pointer to a char array
12288 * @size: the size of the array
Daniel Veillard60942de2003-09-25 21:05:58 +000012289 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012290 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012291 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012292 *
12293 * parse an XML in-memory document and build a tree.
12294 * This reuses the existing @ctxt parser context
12295 *
12296 * Returns the resulting document tree
12297 */
12298xmlDocPtr
12299xmlCtxtReadMemory(xmlParserCtxtPtr ctxt, const char *buffer, int size,
Daniel Veillard60942de2003-09-25 21:05:58 +000012300 const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012301{
12302 xmlParserInputBufferPtr input;
12303 xmlParserInputPtr stream;
12304
12305 if (ctxt == NULL)
12306 return (NULL);
12307 if (buffer == NULL)
12308 return (NULL);
12309
12310 xmlCtxtReset(ctxt);
12311
12312 input = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
12313 if (input == NULL) {
12314 return(NULL);
12315 }
12316
12317 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12318 if (stream == NULL) {
12319 xmlFreeParserInputBuffer(input);
12320 return(NULL);
12321 }
12322
12323 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012324 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012325}
12326
12327/**
12328 * xmlCtxtReadFd:
12329 * @ctxt: an XML parser context
12330 * @fd: an open file descriptor
Daniel Veillard60942de2003-09-25 21:05:58 +000012331 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012332 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012333 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012334 *
12335 * parse an XML from a file descriptor and build a tree.
12336 * This reuses the existing @ctxt parser context
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012337 * NOTE that the file descriptor will not be closed when the
12338 * reader is closed or reset.
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012339 *
12340 * Returns the resulting document tree
12341 */
12342xmlDocPtr
Daniel Veillard60942de2003-09-25 21:05:58 +000012343xmlCtxtReadFd(xmlParserCtxtPtr ctxt, int fd,
12344 const char *URL, const char *encoding, int options)
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012345{
12346 xmlParserInputBufferPtr input;
12347 xmlParserInputPtr stream;
12348
12349 if (fd < 0)
12350 return (NULL);
12351 if (ctxt == NULL)
12352 return (NULL);
12353
12354 xmlCtxtReset(ctxt);
12355
12356
12357 input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
12358 if (input == NULL)
12359 return (NULL);
Daniel Veillard4bc5f432003-12-22 18:13:12 +000012360 input->closecallback = NULL;
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012361 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12362 if (stream == NULL) {
12363 xmlFreeParserInputBuffer(input);
12364 return (NULL);
12365 }
12366 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012367 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012368}
12369
12370/**
12371 * xmlCtxtReadIO:
12372 * @ctxt: an XML parser context
12373 * @ioread: an I/O read function
12374 * @ioclose: an I/O close function
12375 * @ioctx: an I/O handler
Daniel Veillard60942de2003-09-25 21:05:58 +000012376 * @URL: the base URL to use for the document
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012377 * @encoding: the document encoding, or NULL
Daniel Veillard87ab1c12003-12-21 13:01:56 +000012378 * @options: a combination of xmlParserOption
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012379 *
12380 * parse an XML document from I/O functions and source and build a tree.
12381 * This reuses the existing @ctxt parser context
12382 *
12383 * Returns the resulting document tree
12384 */
12385xmlDocPtr
12386xmlCtxtReadIO(xmlParserCtxtPtr ctxt, xmlInputReadCallback ioread,
12387 xmlInputCloseCallback ioclose, void *ioctx,
Daniel Veillard60942de2003-09-25 21:05:58 +000012388 const char *URL,
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012389 const char *encoding, int options)
12390{
12391 xmlParserInputBufferPtr input;
12392 xmlParserInputPtr stream;
12393
12394 if (ioread == NULL)
12395 return (NULL);
12396 if (ctxt == NULL)
12397 return (NULL);
12398
12399 xmlCtxtReset(ctxt);
12400
12401 input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
12402 XML_CHAR_ENCODING_NONE);
12403 if (input == NULL)
12404 return (NULL);
12405 stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
12406 if (stream == NULL) {
12407 xmlFreeParserInputBuffer(input);
12408 return (NULL);
12409 }
12410 inputPush(ctxt, stream);
Daniel Veillard60942de2003-09-25 21:05:58 +000012411 return (xmlDoRead(ctxt, URL, encoding, options, 1));
Daniel Veillard16fa96c2003-09-23 21:50:54 +000012412}