blob: 5c85cbc4e8c3c373d9b17a60eb4d782752fffefc [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002 * parserInternals.c : Internal routines (and obsolete ones) needed for the
3 * XML and HTML parsers.
Owen Taylor3473f882001-02-23 17:55:21 +00004 *
5 * See Copyright for the status of this software.
6 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00007 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +00008 */
9
Daniel Veillard34ce8be2002-03-18 19:37:11 +000010#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000011#include "libxml.h"
12
Daniel Veillard3c5ed912002-01-08 10:36:16 +000013#if defined(WIN32) && !defined (__CYGWIN__)
Owen Taylor3473f882001-02-23 17:55:21 +000014#define XML_DIR_SEP '\\'
15#else
Owen Taylor3473f882001-02-23 17:55:21 +000016#define XML_DIR_SEP '/'
17#endif
18
Owen Taylor3473f882001-02-23 17:55:21 +000019#include <string.h>
20#ifdef HAVE_CTYPE_H
21#include <ctype.h>
22#endif
23#ifdef HAVE_STDLIB_H
24#include <stdlib.h>
25#endif
26#ifdef HAVE_SYS_STAT_H
27#include <sys/stat.h>
28#endif
29#ifdef HAVE_FCNTL_H
30#include <fcntl.h>
31#endif
32#ifdef HAVE_UNISTD_H
33#include <unistd.h>
34#endif
35#ifdef HAVE_ZLIB_H
36#include <zlib.h>
37#endif
38
39#include <libxml/xmlmemory.h>
40#include <libxml/tree.h>
41#include <libxml/parser.h>
42#include <libxml/parserInternals.h>
43#include <libxml/valid.h>
44#include <libxml/entities.h>
45#include <libxml/xmlerror.h>
46#include <libxml/encoding.h>
47#include <libxml/valid.h>
48#include <libxml/xmlIO.h>
49#include <libxml/uri.h>
Daniel Veillard2fdbd322003-08-18 12:15:38 +000050#include <libxml/dict.h>
Daniel Veillard16698282001-09-14 10:29:27 +000051#include <libxml/SAX.h>
Daniel Veillard5d90b6c2001-08-22 14:29:45 +000052#ifdef LIBXML_CATALOG_ENABLED
53#include <libxml/catalog.h>
54#endif
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000055#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000056
Daniel Veillard56a4cb82001-03-24 17:00:36 +000057void xmlUpgradeOldNs(xmlDocPtr doc);
Owen Taylor3473f882001-02-23 17:55:21 +000058
Daniel Veillarda53c6882001-07-25 17:18:57 +000059/*
60 * Various global defaults for parsing
61 */
Owen Taylor3473f882001-02-23 17:55:21 +000062
Daniel Veillard5e2dace2001-07-18 19:30:27 +000063/**
Owen Taylor3473f882001-02-23 17:55:21 +000064 * xmlCheckVersion:
65 * @version: the include version number
66 *
67 * check the compiled lib version against the include one.
68 * This can warn or immediately kill the application
69 */
70void
71xmlCheckVersion(int version) {
72 int myversion = (int) LIBXML_VERSION;
73
Daniel Veillard6f350292001-10-14 09:56:15 +000074 xmlInitParser();
Daniel Veillard4de4d3b2001-05-07 20:50:47 +000075
Owen Taylor3473f882001-02-23 17:55:21 +000076 if ((myversion / 10000) != (version / 10000)) {
77 xmlGenericError(xmlGenericErrorContext,
78 "Fatal: program compiled against libxml %d using libxml %d\n",
79 (version / 10000), (myversion / 10000));
Daniel Veillardc69e0b12001-11-20 08:35:07 +000080 fprintf(stderr,
81 "Fatal: program compiled against libxml %d using libxml %d\n",
82 (version / 10000), (myversion / 10000));
Owen Taylor3473f882001-02-23 17:55:21 +000083 }
84 if ((myversion / 100) < (version / 100)) {
85 xmlGenericError(xmlGenericErrorContext,
86 "Warning: program compiled against libxml %d using older %d\n",
87 (version / 100), (myversion / 100));
88 }
89}
90
91
Daniel Veillard22090732001-07-16 00:06:07 +000092static const char *xmlFeaturesList[] = {
Owen Taylor3473f882001-02-23 17:55:21 +000093 "validate",
94 "load subset",
95 "keep blanks",
96 "disable SAX",
97 "fetch external entities",
98 "substitute entities",
99 "gather line info",
100 "user data",
101 "is html",
102 "is standalone",
103 "stop parser",
104 "document",
105 "is well formed",
106 "is valid",
107 "SAX block",
108 "SAX function internalSubset",
109 "SAX function isStandalone",
110 "SAX function hasInternalSubset",
111 "SAX function hasExternalSubset",
112 "SAX function resolveEntity",
113 "SAX function getEntity",
114 "SAX function entityDecl",
115 "SAX function notationDecl",
116 "SAX function attributeDecl",
117 "SAX function elementDecl",
118 "SAX function unparsedEntityDecl",
119 "SAX function setDocumentLocator",
120 "SAX function startDocument",
121 "SAX function endDocument",
122 "SAX function startElement",
123 "SAX function endElement",
124 "SAX function reference",
125 "SAX function characters",
126 "SAX function ignorableWhitespace",
127 "SAX function processingInstruction",
128 "SAX function comment",
129 "SAX function warning",
130 "SAX function error",
131 "SAX function fatalError",
132 "SAX function getParameterEntity",
133 "SAX function cdataBlock",
134 "SAX function externalSubset",
135};
136
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000137/**
Owen Taylor3473f882001-02-23 17:55:21 +0000138 * xmlGetFeaturesList:
139 * @len: the length of the features name array (input/output)
140 * @result: an array of string to be filled with the features name.
141 *
142 * Copy at most *@len feature names into the @result array
143 *
144 * Returns -1 in case or error, or the total number of features,
145 * len is updated with the number of strings copied,
146 * strings must not be deallocated
147 */
148int
149xmlGetFeaturesList(int *len, const char **result) {
150 int ret, i;
151
152 ret = sizeof(xmlFeaturesList)/sizeof(xmlFeaturesList[0]);
153 if ((len == NULL) || (result == NULL))
154 return(ret);
155 if ((*len < 0) || (*len >= 1000))
156 return(-1);
157 if (*len > ret)
158 *len = ret;
159 for (i = 0;i < *len;i++)
160 result[i] = xmlFeaturesList[i];
161 return(ret);
162}
163
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000164/**
Owen Taylor3473f882001-02-23 17:55:21 +0000165 * xmlGetFeature:
166 * @ctxt: an XML/HTML parser context
167 * @name: the feature name
168 * @result: location to store the result
169 *
170 * Read the current value of one feature of this parser instance
171 *
172 * Returns -1 in case or error, 0 otherwise
173 */
174int
175xmlGetFeature(xmlParserCtxtPtr ctxt, const char *name, void *result) {
176 if ((ctxt == NULL) || (name == NULL) || (result == NULL))
177 return(-1);
178
179 if (!strcmp(name, "validate")) {
180 *((int *) result) = ctxt->validate;
181 } else if (!strcmp(name, "keep blanks")) {
182 *((int *) result) = ctxt->keepBlanks;
183 } else if (!strcmp(name, "disable SAX")) {
184 *((int *) result) = ctxt->disableSAX;
185 } else if (!strcmp(name, "fetch external entities")) {
186 *((int *) result) = ctxt->loadsubset;
187 } else if (!strcmp(name, "substitute entities")) {
188 *((int *) result) = ctxt->replaceEntities;
189 } else if (!strcmp(name, "gather line info")) {
190 *((int *) result) = ctxt->record_info;
191 } else if (!strcmp(name, "user data")) {
192 *((void **)result) = ctxt->userData;
193 } else if (!strcmp(name, "is html")) {
194 *((int *) result) = ctxt->html;
195 } else if (!strcmp(name, "is standalone")) {
196 *((int *) result) = ctxt->standalone;
197 } else if (!strcmp(name, "document")) {
198 *((xmlDocPtr *) result) = ctxt->myDoc;
199 } else if (!strcmp(name, "is well formed")) {
200 *((int *) result) = ctxt->wellFormed;
201 } else if (!strcmp(name, "is valid")) {
202 *((int *) result) = ctxt->valid;
203 } else if (!strcmp(name, "SAX block")) {
204 *((xmlSAXHandlerPtr *) result) = ctxt->sax;
205 } else if (!strcmp(name, "SAX function internalSubset")) {
206 *((internalSubsetSAXFunc *) result) = ctxt->sax->internalSubset;
207 } else if (!strcmp(name, "SAX function isStandalone")) {
208 *((isStandaloneSAXFunc *) result) = ctxt->sax->isStandalone;
209 } else if (!strcmp(name, "SAX function hasInternalSubset")) {
210 *((hasInternalSubsetSAXFunc *) result) = ctxt->sax->hasInternalSubset;
211 } else if (!strcmp(name, "SAX function hasExternalSubset")) {
212 *((hasExternalSubsetSAXFunc *) result) = ctxt->sax->hasExternalSubset;
213 } else if (!strcmp(name, "SAX function resolveEntity")) {
214 *((resolveEntitySAXFunc *) result) = ctxt->sax->resolveEntity;
215 } else if (!strcmp(name, "SAX function getEntity")) {
216 *((getEntitySAXFunc *) result) = ctxt->sax->getEntity;
217 } else if (!strcmp(name, "SAX function entityDecl")) {
218 *((entityDeclSAXFunc *) result) = ctxt->sax->entityDecl;
219 } else if (!strcmp(name, "SAX function notationDecl")) {
220 *((notationDeclSAXFunc *) result) = ctxt->sax->notationDecl;
221 } else if (!strcmp(name, "SAX function attributeDecl")) {
222 *((attributeDeclSAXFunc *) result) = ctxt->sax->attributeDecl;
223 } else if (!strcmp(name, "SAX function elementDecl")) {
224 *((elementDeclSAXFunc *) result) = ctxt->sax->elementDecl;
225 } else if (!strcmp(name, "SAX function unparsedEntityDecl")) {
226 *((unparsedEntityDeclSAXFunc *) result) = ctxt->sax->unparsedEntityDecl;
227 } else if (!strcmp(name, "SAX function setDocumentLocator")) {
228 *((setDocumentLocatorSAXFunc *) result) = ctxt->sax->setDocumentLocator;
229 } else if (!strcmp(name, "SAX function startDocument")) {
230 *((startDocumentSAXFunc *) result) = ctxt->sax->startDocument;
231 } else if (!strcmp(name, "SAX function endDocument")) {
232 *((endDocumentSAXFunc *) result) = ctxt->sax->endDocument;
233 } else if (!strcmp(name, "SAX function startElement")) {
234 *((startElementSAXFunc *) result) = ctxt->sax->startElement;
235 } else if (!strcmp(name, "SAX function endElement")) {
236 *((endElementSAXFunc *) result) = ctxt->sax->endElement;
237 } else if (!strcmp(name, "SAX function reference")) {
238 *((referenceSAXFunc *) result) = ctxt->sax->reference;
239 } else if (!strcmp(name, "SAX function characters")) {
240 *((charactersSAXFunc *) result) = ctxt->sax->characters;
241 } else if (!strcmp(name, "SAX function ignorableWhitespace")) {
242 *((ignorableWhitespaceSAXFunc *) result) = ctxt->sax->ignorableWhitespace;
243 } else if (!strcmp(name, "SAX function processingInstruction")) {
244 *((processingInstructionSAXFunc *) result) = ctxt->sax->processingInstruction;
245 } else if (!strcmp(name, "SAX function comment")) {
246 *((commentSAXFunc *) result) = ctxt->sax->comment;
247 } else if (!strcmp(name, "SAX function warning")) {
248 *((warningSAXFunc *) result) = ctxt->sax->warning;
249 } else if (!strcmp(name, "SAX function error")) {
250 *((errorSAXFunc *) result) = ctxt->sax->error;
251 } else if (!strcmp(name, "SAX function fatalError")) {
252 *((fatalErrorSAXFunc *) result) = ctxt->sax->fatalError;
253 } else if (!strcmp(name, "SAX function getParameterEntity")) {
254 *((getParameterEntitySAXFunc *) result) = ctxt->sax->getParameterEntity;
255 } else if (!strcmp(name, "SAX function cdataBlock")) {
256 *((cdataBlockSAXFunc *) result) = ctxt->sax->cdataBlock;
257 } else if (!strcmp(name, "SAX function externalSubset")) {
258 *((externalSubsetSAXFunc *) result) = ctxt->sax->externalSubset;
259 } else {
260 return(-1);
261 }
262 return(0);
263}
264
Daniel Veillard5e2dace2001-07-18 19:30:27 +0000265/**
Owen Taylor3473f882001-02-23 17:55:21 +0000266 * xmlSetFeature:
267 * @ctxt: an XML/HTML parser context
268 * @name: the feature name
269 * @value: pointer to the location of the new value
270 *
271 * Change the current value of one feature of this parser instance
272 *
273 * Returns -1 in case or error, 0 otherwise
274 */
275int
276xmlSetFeature(xmlParserCtxtPtr ctxt, const char *name, void *value) {
277 if ((ctxt == NULL) || (name == NULL) || (value == NULL))
278 return(-1);
279
280 if (!strcmp(name, "validate")) {
281 int newvalidate = *((int *) value);
282 if ((!ctxt->validate) && (newvalidate != 0)) {
283 if (ctxt->vctxt.warning == NULL)
284 ctxt->vctxt.warning = xmlParserValidityWarning;
285 if (ctxt->vctxt.error == NULL)
286 ctxt->vctxt.error = xmlParserValidityError;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +0000287 ctxt->vctxt.nodeMax = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000288 }
289 ctxt->validate = newvalidate;
290 } else if (!strcmp(name, "keep blanks")) {
291 ctxt->keepBlanks = *((int *) value);
292 } else if (!strcmp(name, "disable SAX")) {
293 ctxt->disableSAX = *((int *) value);
294 } else if (!strcmp(name, "fetch external entities")) {
295 ctxt->loadsubset = *((int *) value);
296 } else if (!strcmp(name, "substitute entities")) {
297 ctxt->replaceEntities = *((int *) value);
298 } else if (!strcmp(name, "gather line info")) {
299 ctxt->record_info = *((int *) value);
300 } else if (!strcmp(name, "user data")) {
301 ctxt->userData = *((void **)value);
302 } else if (!strcmp(name, "is html")) {
303 ctxt->html = *((int *) value);
304 } else if (!strcmp(name, "is standalone")) {
305 ctxt->standalone = *((int *) value);
306 } else if (!strcmp(name, "document")) {
307 ctxt->myDoc = *((xmlDocPtr *) value);
308 } else if (!strcmp(name, "is well formed")) {
309 ctxt->wellFormed = *((int *) value);
310 } else if (!strcmp(name, "is valid")) {
311 ctxt->valid = *((int *) value);
312 } else if (!strcmp(name, "SAX block")) {
313 ctxt->sax = *((xmlSAXHandlerPtr *) value);
314 } else if (!strcmp(name, "SAX function internalSubset")) {
315 ctxt->sax->internalSubset = *((internalSubsetSAXFunc *) value);
316 } else if (!strcmp(name, "SAX function isStandalone")) {
317 ctxt->sax->isStandalone = *((isStandaloneSAXFunc *) value);
318 } else if (!strcmp(name, "SAX function hasInternalSubset")) {
319 ctxt->sax->hasInternalSubset = *((hasInternalSubsetSAXFunc *) value);
320 } else if (!strcmp(name, "SAX function hasExternalSubset")) {
321 ctxt->sax->hasExternalSubset = *((hasExternalSubsetSAXFunc *) value);
322 } else if (!strcmp(name, "SAX function resolveEntity")) {
323 ctxt->sax->resolveEntity = *((resolveEntitySAXFunc *) value);
324 } else if (!strcmp(name, "SAX function getEntity")) {
325 ctxt->sax->getEntity = *((getEntitySAXFunc *) value);
326 } else if (!strcmp(name, "SAX function entityDecl")) {
327 ctxt->sax->entityDecl = *((entityDeclSAXFunc *) value);
328 } else if (!strcmp(name, "SAX function notationDecl")) {
329 ctxt->sax->notationDecl = *((notationDeclSAXFunc *) value);
330 } else if (!strcmp(name, "SAX function attributeDecl")) {
331 ctxt->sax->attributeDecl = *((attributeDeclSAXFunc *) value);
332 } else if (!strcmp(name, "SAX function elementDecl")) {
333 ctxt->sax->elementDecl = *((elementDeclSAXFunc *) value);
334 } else if (!strcmp(name, "SAX function unparsedEntityDecl")) {
335 ctxt->sax->unparsedEntityDecl = *((unparsedEntityDeclSAXFunc *) value);
336 } else if (!strcmp(name, "SAX function setDocumentLocator")) {
337 ctxt->sax->setDocumentLocator = *((setDocumentLocatorSAXFunc *) value);
338 } else if (!strcmp(name, "SAX function startDocument")) {
339 ctxt->sax->startDocument = *((startDocumentSAXFunc *) value);
340 } else if (!strcmp(name, "SAX function endDocument")) {
341 ctxt->sax->endDocument = *((endDocumentSAXFunc *) value);
342 } else if (!strcmp(name, "SAX function startElement")) {
343 ctxt->sax->startElement = *((startElementSAXFunc *) value);
344 } else if (!strcmp(name, "SAX function endElement")) {
345 ctxt->sax->endElement = *((endElementSAXFunc *) value);
346 } else if (!strcmp(name, "SAX function reference")) {
347 ctxt->sax->reference = *((referenceSAXFunc *) value);
348 } else if (!strcmp(name, "SAX function characters")) {
349 ctxt->sax->characters = *((charactersSAXFunc *) value);
350 } else if (!strcmp(name, "SAX function ignorableWhitespace")) {
351 ctxt->sax->ignorableWhitespace = *((ignorableWhitespaceSAXFunc *) value);
352 } else if (!strcmp(name, "SAX function processingInstruction")) {
353 ctxt->sax->processingInstruction = *((processingInstructionSAXFunc *) value);
354 } else if (!strcmp(name, "SAX function comment")) {
355 ctxt->sax->comment = *((commentSAXFunc *) value);
356 } else if (!strcmp(name, "SAX function warning")) {
357 ctxt->sax->warning = *((warningSAXFunc *) value);
358 } else if (!strcmp(name, "SAX function error")) {
359 ctxt->sax->error = *((errorSAXFunc *) value);
360 } else if (!strcmp(name, "SAX function fatalError")) {
361 ctxt->sax->fatalError = *((fatalErrorSAXFunc *) value);
362 } else if (!strcmp(name, "SAX function getParameterEntity")) {
363 ctxt->sax->getParameterEntity = *((getParameterEntitySAXFunc *) value);
364 } else if (!strcmp(name, "SAX function cdataBlock")) {
365 ctxt->sax->cdataBlock = *((cdataBlockSAXFunc *) value);
366 } else if (!strcmp(name, "SAX function externalSubset")) {
367 ctxt->sax->externalSubset = *((externalSubsetSAXFunc *) value);
368 } else {
369 return(-1);
370 }
371 return(0);
372}
373
374/************************************************************************
375 * *
376 * Some functions to avoid too large macros *
377 * *
378 ************************************************************************/
379
380/**
381 * xmlIsChar:
382 * @c: an unicode character (int)
383 *
384 * Check whether the character is allowed by the production
385 * [2] Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD]
386 * | [#x10000-#x10FFFF]
387 * any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
388 * Also available as a macro IS_CHAR()
389 *
390 * Returns 0 if not, non-zero otherwise
391 */
392int
393xmlIsChar(int c) {
394 return(
395 ((c) == 0x09) || ((c) == 0x0A) || ((c) == 0x0D) ||
396 (((c) >= 0x20) && ((c) <= 0xD7FF)) ||
397 (((c) >= 0xE000) && ((c) <= 0xFFFD)) ||
398 (((c) >= 0x10000) && ((c) <= 0x10FFFF)));
399}
400
401/**
402 * xmlIsBlank:
403 * @c: an unicode character (int)
404 *
405 * Check whether the character is allowed by the production
406 * [3] S ::= (#x20 | #x9 | #xD | #xA)+
407 * Also available as a macro IS_BLANK()
408 *
409 * Returns 0 if not, non-zero otherwise
410 */
411int
412xmlIsBlank(int c) {
413 return(((c) == 0x20) || ((c) == 0x09) || ((c) == 0xA) || ((c) == 0x0D));
414}
415
Owen Taylor3473f882001-02-23 17:55:21 +0000416static int xmlBaseArray[] = {
417 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0000 - 0x000F */
418 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0010 - 0x001F */
419 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0020 - 0x002F */
420 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0030 - 0x003F */
421 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x0040 - 0x004F */
422 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x0050 - 0x005F */
423 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x0060 - 0x006F */
424 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x0070 - 0x007F */
425 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0080 - 0x008F */
426 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0090 - 0x009F */
427 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00A0 - 0x00AF */
428 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00B0 - 0x00BF */
429 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x00C0 - 0x00CF */
430 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x00D0 - 0x00DF */
431 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x00E0 - 0x00EF */
432 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x00F0 - 0x00FF */
433};
434
Daniel Veillard01c13b52002-12-10 15:19:08 +0000435/**
436 * xmlIsBaseChar:
437 * @c: an unicode character (int)
438 *
439 * Check whether the character is allowed by the production
440 * [85] BaseChar ::= ... long list see REC ...
441 *
442 * VI is your friend !
443 * :1,$ s/\[#x\([0-9A-Z]*\)-#x\([0-9A-Z]*\)\]/ (((c) >= 0x\1) \&\& ((c) <= 0x\2)) ||/
444 * and
445 * :1,$ s/#x\([0-9A-Z]*\)/ ((c) == 0x\1) ||/
446 *
447 * Returns 0 if not, non-zero otherwise
448 */
Owen Taylor3473f882001-02-23 17:55:21 +0000449int
450xmlIsBaseChar(int c) {
451 return(
452 (((c) < 0x0100) ? xmlBaseArray[c] :
453 ( /* accelerator */
454 (((c) >= 0x0100) && ((c) <= 0x0131)) ||
455 (((c) >= 0x0134) && ((c) <= 0x013E)) ||
456 (((c) >= 0x0141) && ((c) <= 0x0148)) ||
457 (((c) >= 0x014A) && ((c) <= 0x017E)) ||
458 (((c) >= 0x0180) && ((c) <= 0x01C3)) ||
459 (((c) >= 0x01CD) && ((c) <= 0x01F0)) ||
460 (((c) >= 0x01F4) && ((c) <= 0x01F5)) ||
461 (((c) >= 0x01FA) && ((c) <= 0x0217)) ||
462 (((c) >= 0x0250) && ((c) <= 0x02A8)) ||
463 (((c) >= 0x02BB) && ((c) <= 0x02C1)) ||
464 ((c) == 0x0386) ||
465 (((c) >= 0x0388) && ((c) <= 0x038A)) ||
466 ((c) == 0x038C) ||
467 (((c) >= 0x038E) && ((c) <= 0x03A1)) ||
468 (((c) >= 0x03A3) && ((c) <= 0x03CE)) ||
469 (((c) >= 0x03D0) && ((c) <= 0x03D6)) ||
470 ((c) == 0x03DA) ||
471 ((c) == 0x03DC) ||
472 ((c) == 0x03DE) ||
473 ((c) == 0x03E0) ||
474 (((c) >= 0x03E2) && ((c) <= 0x03F3)) ||
475 (((c) >= 0x0401) && ((c) <= 0x040C)) ||
476 (((c) >= 0x040E) && ((c) <= 0x044F)) ||
477 (((c) >= 0x0451) && ((c) <= 0x045C)) ||
478 (((c) >= 0x045E) && ((c) <= 0x0481)) ||
479 (((c) >= 0x0490) && ((c) <= 0x04C4)) ||
480 (((c) >= 0x04C7) && ((c) <= 0x04C8)) ||
481 (((c) >= 0x04CB) && ((c) <= 0x04CC)) ||
482 (((c) >= 0x04D0) && ((c) <= 0x04EB)) ||
483 (((c) >= 0x04EE) && ((c) <= 0x04F5)) ||
484 (((c) >= 0x04F8) && ((c) <= 0x04F9)) ||
485 (((c) >= 0x0531) && ((c) <= 0x0556)) ||
486 ((c) == 0x0559) ||
487 (((c) >= 0x0561) && ((c) <= 0x0586)) ||
488 (((c) >= 0x05D0) && ((c) <= 0x05EA)) ||
489 (((c) >= 0x05F0) && ((c) <= 0x05F2)) ||
490 (((c) >= 0x0621) && ((c) <= 0x063A)) ||
491 (((c) >= 0x0641) && ((c) <= 0x064A)) ||
492 (((c) >= 0x0671) && ((c) <= 0x06B7)) ||
493 (((c) >= 0x06BA) && ((c) <= 0x06BE)) ||
494 (((c) >= 0x06C0) && ((c) <= 0x06CE)) ||
495 (((c) >= 0x06D0) && ((c) <= 0x06D3)) ||
496 ((c) == 0x06D5) ||
497 (((c) >= 0x06E5) && ((c) <= 0x06E6)) ||
498 (((c) >= 0x905) && ( /* accelerator */
499 (((c) >= 0x0905) && ((c) <= 0x0939)) ||
500 ((c) == 0x093D) ||
501 (((c) >= 0x0958) && ((c) <= 0x0961)) ||
502 (((c) >= 0x0985) && ((c) <= 0x098C)) ||
503 (((c) >= 0x098F) && ((c) <= 0x0990)) ||
504 (((c) >= 0x0993) && ((c) <= 0x09A8)) ||
505 (((c) >= 0x09AA) && ((c) <= 0x09B0)) ||
506 ((c) == 0x09B2) ||
507 (((c) >= 0x09B6) && ((c) <= 0x09B9)) ||
508 (((c) >= 0x09DC) && ((c) <= 0x09DD)) ||
509 (((c) >= 0x09DF) && ((c) <= 0x09E1)) ||
510 (((c) >= 0x09F0) && ((c) <= 0x09F1)) ||
511 (((c) >= 0x0A05) && ((c) <= 0x0A0A)) ||
512 (((c) >= 0x0A0F) && ((c) <= 0x0A10)) ||
513 (((c) >= 0x0A13) && ((c) <= 0x0A28)) ||
514 (((c) >= 0x0A2A) && ((c) <= 0x0A30)) ||
515 (((c) >= 0x0A32) && ((c) <= 0x0A33)) ||
516 (((c) >= 0x0A35) && ((c) <= 0x0A36)) ||
517 (((c) >= 0x0A38) && ((c) <= 0x0A39)) ||
518 (((c) >= 0x0A59) && ((c) <= 0x0A5C)) ||
519 ((c) == 0x0A5E) ||
520 (((c) >= 0x0A72) && ((c) <= 0x0A74)) ||
521 (((c) >= 0x0A85) && ((c) <= 0x0A8B)) ||
522 ((c) == 0x0A8D) ||
523 (((c) >= 0x0A8F) && ((c) <= 0x0A91)) ||
524 (((c) >= 0x0A93) && ((c) <= 0x0AA8)) ||
525 (((c) >= 0x0AAA) && ((c) <= 0x0AB0)) ||
526 (((c) >= 0x0AB2) && ((c) <= 0x0AB3)) ||
527 (((c) >= 0x0AB5) && ((c) <= 0x0AB9)) ||
528 ((c) == 0x0ABD) ||
529 ((c) == 0x0AE0) ||
530 (((c) >= 0x0B05) && ((c) <= 0x0B0C)) ||
531 (((c) >= 0x0B0F) && ((c) <= 0x0B10)) ||
532 (((c) >= 0x0B13) && ((c) <= 0x0B28)) ||
533 (((c) >= 0x0B2A) && ((c) <= 0x0B30)) ||
534 (((c) >= 0x0B32) && ((c) <= 0x0B33)) ||
535 (((c) >= 0x0B36) && ((c) <= 0x0B39)) ||
536 ((c) == 0x0B3D) ||
537 (((c) >= 0x0B5C) && ((c) <= 0x0B5D)) ||
538 (((c) >= 0x0B5F) && ((c) <= 0x0B61)) ||
539 (((c) >= 0x0B85) && ((c) <= 0x0B8A)) ||
540 (((c) >= 0x0B8E) && ((c) <= 0x0B90)) ||
541 (((c) >= 0x0B92) && ((c) <= 0x0B95)) ||
542 (((c) >= 0x0B99) && ((c) <= 0x0B9A)) ||
543 ((c) == 0x0B9C) ||
544 (((c) >= 0x0B9E) && ((c) <= 0x0B9F)) ||
545 (((c) >= 0x0BA3) && ((c) <= 0x0BA4)) ||
546 (((c) >= 0x0BA8) && ((c) <= 0x0BAA)) ||
547 (((c) >= 0x0BAE) && ((c) <= 0x0BB5)) ||
548 (((c) >= 0x0BB7) && ((c) <= 0x0BB9)) ||
549 (((c) >= 0x0C05) && ((c) <= 0x0C0C)) ||
550 (((c) >= 0x0C0E) && ((c) <= 0x0C10)) ||
551 (((c) >= 0x0C12) && ((c) <= 0x0C28)) ||
552 (((c) >= 0x0C2A) && ((c) <= 0x0C33)) ||
553 (((c) >= 0x0C35) && ((c) <= 0x0C39)) ||
554 (((c) >= 0x0C60) && ((c) <= 0x0C61)) ||
555 (((c) >= 0x0C85) && ((c) <= 0x0C8C)) ||
556 (((c) >= 0x0C8E) && ((c) <= 0x0C90)) ||
557 (((c) >= 0x0C92) && ((c) <= 0x0CA8)) ||
558 (((c) >= 0x0CAA) && ((c) <= 0x0CB3)) ||
559 (((c) >= 0x0CB5) && ((c) <= 0x0CB9)) ||
560 ((c) == 0x0CDE) ||
561 (((c) >= 0x0CE0) && ((c) <= 0x0CE1)) ||
562 (((c) >= 0x0D05) && ((c) <= 0x0D0C)) ||
563 (((c) >= 0x0D0E) && ((c) <= 0x0D10)) ||
564 (((c) >= 0x0D12) && ((c) <= 0x0D28)) ||
565 (((c) >= 0x0D2A) && ((c) <= 0x0D39)) ||
566 (((c) >= 0x0D60) && ((c) <= 0x0D61)) ||
567 (((c) >= 0x0E01) && ((c) <= 0x0E2E)) ||
568 ((c) == 0x0E30) ||
569 (((c) >= 0x0E32) && ((c) <= 0x0E33)) ||
570 (((c) >= 0x0E40) && ((c) <= 0x0E45)) ||
571 (((c) >= 0x0E81) && ((c) <= 0x0E82)) ||
572 ((c) == 0x0E84) ||
573 (((c) >= 0x0E87) && ((c) <= 0x0E88)) ||
574 ((c) == 0x0E8A) ||
575 ((c) == 0x0E8D) ||
576 (((c) >= 0x0E94) && ((c) <= 0x0E97)) ||
577 (((c) >= 0x0E99) && ((c) <= 0x0E9F)) ||
578 (((c) >= 0x0EA1) && ((c) <= 0x0EA3)) ||
579 ((c) == 0x0EA5) ||
580 ((c) == 0x0EA7) ||
581 (((c) >= 0x0EAA) && ((c) <= 0x0EAB)) ||
582 (((c) >= 0x0EAD) && ((c) <= 0x0EAE)) ||
583 ((c) == 0x0EB0) ||
584 (((c) >= 0x0EB2) && ((c) <= 0x0EB3)) ||
585 ((c) == 0x0EBD) ||
586 (((c) >= 0x0EC0) && ((c) <= 0x0EC4)) ||
587 (((c) >= 0x0F40) && ((c) <= 0x0F47)) ||
588 (((c) >= 0x0F49) && ((c) <= 0x0F69)) ||
589 (((c) >= 0x10A0) && ( /* accelerator */
590 (((c) >= 0x10A0) && ((c) <= 0x10C5)) ||
591 (((c) >= 0x10D0) && ((c) <= 0x10F6)) ||
592 ((c) == 0x1100) ||
593 (((c) >= 0x1102) && ((c) <= 0x1103)) ||
594 (((c) >= 0x1105) && ((c) <= 0x1107)) ||
595 ((c) == 0x1109) ||
596 (((c) >= 0x110B) && ((c) <= 0x110C)) ||
597 (((c) >= 0x110E) && ((c) <= 0x1112)) ||
598 ((c) == 0x113C) ||
599 ((c) == 0x113E) ||
600 ((c) == 0x1140) ||
601 ((c) == 0x114C) ||
602 ((c) == 0x114E) ||
603 ((c) == 0x1150) ||
604 (((c) >= 0x1154) && ((c) <= 0x1155)) ||
605 ((c) == 0x1159) ||
606 (((c) >= 0x115F) && ((c) <= 0x1161)) ||
607 ((c) == 0x1163) ||
608 ((c) == 0x1165) ||
609 ((c) == 0x1167) ||
610 ((c) == 0x1169) ||
611 (((c) >= 0x116D) && ((c) <= 0x116E)) ||
612 (((c) >= 0x1172) && ((c) <= 0x1173)) ||
613 ((c) == 0x1175) ||
614 ((c) == 0x119E) ||
615 ((c) == 0x11A8) ||
616 ((c) == 0x11AB) ||
617 (((c) >= 0x11AE) && ((c) <= 0x11AF)) ||
618 (((c) >= 0x11B7) && ((c) <= 0x11B8)) ||
619 ((c) == 0x11BA) ||
620 (((c) >= 0x11BC) && ((c) <= 0x11C2)) ||
621 ((c) == 0x11EB) ||
622 ((c) == 0x11F0) ||
623 ((c) == 0x11F9) ||
624 (((c) >= 0x1E00) && ((c) <= 0x1E9B)) ||
625 (((c) >= 0x1EA0) && ((c) <= 0x1EF9)) ||
626 (((c) >= 0x1F00) && ((c) <= 0x1F15)) ||
627 (((c) >= 0x1F18) && ((c) <= 0x1F1D)) ||
628 (((c) >= 0x1F20) && ((c) <= 0x1F45)) ||
629 (((c) >= 0x1F48) && ((c) <= 0x1F4D)) ||
630 (((c) >= 0x1F50) && ((c) <= 0x1F57)) ||
631 ((c) == 0x1F59) ||
632 ((c) == 0x1F5B) ||
633 ((c) == 0x1F5D) ||
634 (((c) >= 0x1F5F) && ((c) <= 0x1F7D)) ||
635 (((c) >= 0x1F80) && ((c) <= 0x1FB4)) ||
636 (((c) >= 0x1FB6) && ((c) <= 0x1FBC)) ||
637 ((c) == 0x1FBE) ||
638 (((c) >= 0x1FC2) && ((c) <= 0x1FC4)) ||
639 (((c) >= 0x1FC6) && ((c) <= 0x1FCC)) ||
640 (((c) >= 0x1FD0) && ((c) <= 0x1FD3)) ||
641 (((c) >= 0x1FD6) && ((c) <= 0x1FDB)) ||
642 (((c) >= 0x1FE0) && ((c) <= 0x1FEC)) ||
643 (((c) >= 0x1FF2) && ((c) <= 0x1FF4)) ||
644 (((c) >= 0x1FF6) && ((c) <= 0x1FFC)) ||
645 ((c) == 0x2126) ||
646 (((c) >= 0x212A) && ((c) <= 0x212B)) ||
647 ((c) == 0x212E) ||
648 (((c) >= 0x2180) && ((c) <= 0x2182)) ||
649 (((c) >= 0x3041) && ((c) <= 0x3094)) ||
650 (((c) >= 0x30A1) && ((c) <= 0x30FA)) ||
651 (((c) >= 0x3105) && ((c) <= 0x312C)) ||
652 (((c) >= 0xAC00) && ((c) <= 0xD7A3))) /* accelerators */ ))))));
653}
654
655/**
656 * xmlIsDigit:
657 * @c: an unicode character (int)
658 *
659 * Check whether the character is allowed by the production
660 * [88] Digit ::= ... long list see REC ...
661 *
662 * Returns 0 if not, non-zero otherwise
663 */
664int
665xmlIsDigit(int c) {
666 return(
667 (((c) >= 0x0030) && ((c) <= 0x0039)) ||
668 (((c) >= 0x660) && ( /* accelerator */
669 (((c) >= 0x0660) && ((c) <= 0x0669)) ||
670 (((c) >= 0x06F0) && ((c) <= 0x06F9)) ||
671 (((c) >= 0x0966) && ((c) <= 0x096F)) ||
672 (((c) >= 0x09E6) && ((c) <= 0x09EF)) ||
673 (((c) >= 0x0A66) && ((c) <= 0x0A6F)) ||
674 (((c) >= 0x0AE6) && ((c) <= 0x0AEF)) ||
675 (((c) >= 0x0B66) && ((c) <= 0x0B6F)) ||
676 (((c) >= 0x0BE7) && ((c) <= 0x0BEF)) ||
677 (((c) >= 0x0C66) && ((c) <= 0x0C6F)) ||
678 (((c) >= 0x0CE6) && ((c) <= 0x0CEF)) ||
679 (((c) >= 0x0D66) && ((c) <= 0x0D6F)) ||
680 (((c) >= 0x0E50) && ((c) <= 0x0E59)) ||
681 (((c) >= 0x0ED0) && ((c) <= 0x0ED9)) ||
682 (((c) >= 0x0F20) && ((c) <= 0x0F29))) /* accelerator */ ));
683}
684
685/**
686 * xmlIsCombining:
687 * @c: an unicode character (int)
688 *
689 * Check whether the character is allowed by the production
690 * [87] CombiningChar ::= ... long list see REC ...
691 *
692 * Returns 0 if not, non-zero otherwise
693 */
694int
695xmlIsCombining(int c) {
696 return(
697 (((c) >= 0x300) && ( /* accelerator */
698 (((c) >= 0x0300) && ((c) <= 0x0345)) ||
699 (((c) >= 0x0360) && ((c) <= 0x0361)) ||
700 (((c) >= 0x0483) && ((c) <= 0x0486)) ||
701 (((c) >= 0x0591) && ((c) <= 0x05A1)) ||
702 (((c) >= 0x05A3) && ((c) <= 0x05B9)) ||
703 (((c) >= 0x05BB) && ((c) <= 0x05BD)) ||
704 ((c) == 0x05BF) ||
705 (((c) >= 0x05C1) && ((c) <= 0x05C2)) ||
706 ((c) == 0x05C4) ||
707 (((c) >= 0x064B) && ((c) <= 0x0652)) ||
708 ((c) == 0x0670) ||
709 (((c) >= 0x06D6) && ((c) <= 0x06DC)) ||
710 (((c) >= 0x06DD) && ((c) <= 0x06DF)) ||
711 (((c) >= 0x06E0) && ((c) <= 0x06E4)) ||
712 (((c) >= 0x06E7) && ((c) <= 0x06E8)) ||
713 (((c) >= 0x06EA) && ((c) <= 0x06ED)) ||
714 (((c) >= 0x0901) && ( /* accelerator */
715 (((c) >= 0x0901) && ((c) <= 0x0903)) ||
716 ((c) == 0x093C) ||
717 (((c) >= 0x093E) && ((c) <= 0x094C)) ||
718 ((c) == 0x094D) ||
719 (((c) >= 0x0951) && ((c) <= 0x0954)) ||
720 (((c) >= 0x0962) && ((c) <= 0x0963)) ||
721 (((c) >= 0x0981) && ((c) <= 0x0983)) ||
722 ((c) == 0x09BC) ||
723 ((c) == 0x09BE) ||
724 ((c) == 0x09BF) ||
725 (((c) >= 0x09C0) && ((c) <= 0x09C4)) ||
726 (((c) >= 0x09C7) && ((c) <= 0x09C8)) ||
727 (((c) >= 0x09CB) && ((c) <= 0x09CD)) ||
728 ((c) == 0x09D7) ||
729 (((c) >= 0x09E2) && ((c) <= 0x09E3)) ||
730 (((c) >= 0x0A02) && ( /* accelerator */
731 ((c) == 0x0A02) ||
732 ((c) == 0x0A3C) ||
733 ((c) == 0x0A3E) ||
734 ((c) == 0x0A3F) ||
735 (((c) >= 0x0A40) && ((c) <= 0x0A42)) ||
736 (((c) >= 0x0A47) && ((c) <= 0x0A48)) ||
737 (((c) >= 0x0A4B) && ((c) <= 0x0A4D)) ||
738 (((c) >= 0x0A70) && ((c) <= 0x0A71)) ||
739 (((c) >= 0x0A81) && ((c) <= 0x0A83)) ||
740 ((c) == 0x0ABC) ||
741 (((c) >= 0x0ABE) && ((c) <= 0x0AC5)) ||
742 (((c) >= 0x0AC7) && ((c) <= 0x0AC9)) ||
743 (((c) >= 0x0ACB) && ((c) <= 0x0ACD)) ||
744 (((c) >= 0x0B01) && ((c) <= 0x0B03)) ||
745 ((c) == 0x0B3C) ||
746 (((c) >= 0x0B3E) && ((c) <= 0x0B43)) ||
747 (((c) >= 0x0B47) && ((c) <= 0x0B48)) ||
748 (((c) >= 0x0B4B) && ((c) <= 0x0B4D)) ||
749 (((c) >= 0x0B56) && ((c) <= 0x0B57)) ||
750 (((c) >= 0x0B82) && ((c) <= 0x0B83)) ||
751 (((c) >= 0x0BBE) && ((c) <= 0x0BC2)) ||
752 (((c) >= 0x0BC6) && ((c) <= 0x0BC8)) ||
753 (((c) >= 0x0BCA) && ((c) <= 0x0BCD)) ||
754 ((c) == 0x0BD7) ||
755 (((c) >= 0x0C01) && ((c) <= 0x0C03)) ||
756 (((c) >= 0x0C3E) && ((c) <= 0x0C44)) ||
757 (((c) >= 0x0C46) && ((c) <= 0x0C48)) ||
758 (((c) >= 0x0C4A) && ((c) <= 0x0C4D)) ||
759 (((c) >= 0x0C55) && ((c) <= 0x0C56)) ||
760 (((c) >= 0x0C82) && ((c) <= 0x0C83)) ||
761 (((c) >= 0x0CBE) && ((c) <= 0x0CC4)) ||
762 (((c) >= 0x0CC6) && ((c) <= 0x0CC8)) ||
763 (((c) >= 0x0CCA) && ((c) <= 0x0CCD)) ||
764 (((c) >= 0x0CD5) && ((c) <= 0x0CD6)) ||
765 (((c) >= 0x0D02) && ((c) <= 0x0D03)) ||
766 (((c) >= 0x0D3E) && ((c) <= 0x0D43)) ||
767 (((c) >= 0x0D46) && ((c) <= 0x0D48)) ||
768 (((c) >= 0x0D4A) && ((c) <= 0x0D4D)) ||
769 ((c) == 0x0D57) ||
770 (((c) >= 0x0E31) && ( /* accelerator */
771 ((c) == 0x0E31) ||
772 (((c) >= 0x0E34) && ((c) <= 0x0E3A)) ||
773 (((c) >= 0x0E47) && ((c) <= 0x0E4E)) ||
774 ((c) == 0x0EB1) ||
775 (((c) >= 0x0EB4) && ((c) <= 0x0EB9)) ||
776 (((c) >= 0x0EBB) && ((c) <= 0x0EBC)) ||
777 (((c) >= 0x0EC8) && ((c) <= 0x0ECD)) ||
778 (((c) >= 0x0F18) && ((c) <= 0x0F19)) ||
779 ((c) == 0x0F35) ||
780 ((c) == 0x0F37) ||
781 ((c) == 0x0F39) ||
782 ((c) == 0x0F3E) ||
783 ((c) == 0x0F3F) ||
784 (((c) >= 0x0F71) && ((c) <= 0x0F84)) ||
785 (((c) >= 0x0F86) && ((c) <= 0x0F8B)) ||
786 (((c) >= 0x0F90) && ((c) <= 0x0F95)) ||
787 ((c) == 0x0F97) ||
788 (((c) >= 0x0F99) && ((c) <= 0x0FAD)) ||
789 (((c) >= 0x0FB1) && ((c) <= 0x0FB7)) ||
790 ((c) == 0x0FB9) ||
791 (((c) >= 0x20D0) && ((c) <= 0x20DC)) ||
792 ((c) == 0x20E1) ||
793 (((c) >= 0x302A) && ((c) <= 0x302F)) ||
794 ((c) == 0x3099) ||
795 ((c) == 0x309A))))))))));
796}
797
798/**
799 * xmlIsExtender:
800 * @c: an unicode character (int)
801 *
802 * Check whether the character is allowed by the production
803 * [89] Extender ::= #x00B7 | #x02D0 | #x02D1 | #x0387 | #x0640 |
804 * #x0E46 | #x0EC6 | #x3005 | [#x3031-#x3035] |
805 * [#x309D-#x309E] | [#x30FC-#x30FE]
806 *
807 * Returns 0 if not, non-zero otherwise
808 */
809int
810xmlIsExtender(int c) {
811 switch (c) {
812 case 0x00B7: case 0x02D0: case 0x02D1: case 0x0387:
813 case 0x0640: case 0x0E46: case 0x0EC6: case 0x3005:
814 case 0x3031: case 0x3032: case 0x3033: case 0x3034:
815 case 0x3035: case 0x309D: case 0x309E: case 0x30FC:
Daniel Veillard4a7ae502002-02-18 19:18:17 +0000816 case 0x30FD: case 0x30FE:
Owen Taylor3473f882001-02-23 17:55:21 +0000817 return 1;
818 default:
819 return 0;
820 }
821}
822
823/**
824 * xmlIsIdeographic:
825 * @c: an unicode character (int)
826 *
827 * Check whether the character is allowed by the production
828 * [86] Ideographic ::= [#x4E00-#x9FA5] | #x3007 | [#x3021-#x3029]
829 *
830 * Returns 0 if not, non-zero otherwise
831 */
832int
833xmlIsIdeographic(int c) {
834 return(((c) < 0x0100) ? 0 :
835 (((c) >= 0x4e00) && ((c) <= 0x9fa5)) ||
836 (((c) >= 0xf900) && ((c) <= 0xfa2d)) ||
837 (((c) >= 0x3021) && ((c) <= 0x3029)) ||
838 ((c) == 0x3007));
839}
840
841/**
842 * xmlIsLetter:
843 * @c: an unicode character (int)
844 *
845 * Check whether the character is allowed by the production
846 * [84] Letter ::= BaseChar | Ideographic
847 *
848 * Returns 0 if not, non-zero otherwise
849 */
850int
851xmlIsLetter(int c) {
852 return(IS_BASECHAR(c) || IS_IDEOGRAPHIC(c));
853}
854
855/**
856 * xmlIsPubidChar:
857 * @c: an unicode character (int)
858 *
859 * Check whether the character is allowed by the production
860 * [13] PubidChar ::= #x20 | #xD | #xA | [a-zA-Z0-9] | [-'()+,./:=?;!*#@$_%]
861 *
862 * Returns 0 if not, non-zero otherwise
863 */
864int
865xmlIsPubidChar(int c) {
866 return(
867 ((c) == 0x20) || ((c) == 0x0D) || ((c) == 0x0A) ||
868 (((c) >= 'a') && ((c) <= 'z')) ||
869 (((c) >= 'A') && ((c) <= 'Z')) ||
870 (((c) >= '0') && ((c) <= '9')) ||
871 ((c) == '-') || ((c) == '\'') || ((c) == '(') || ((c) == ')') ||
872 ((c) == '+') || ((c) == ',') || ((c) == '.') || ((c) == '/') ||
873 ((c) == ':') || ((c) == '=') || ((c) == '?') || ((c) == ';') ||
874 ((c) == '!') || ((c) == '*') || ((c) == '#') || ((c) == '@') ||
875 ((c) == '$') || ((c) == '_') || ((c) == '%'));
876}
877
878/************************************************************************
879 * *
880 * Input handling functions for progressive parsing *
881 * *
882 ************************************************************************/
883
884/* #define DEBUG_INPUT */
885/* #define DEBUG_STACK */
886/* #define DEBUG_PUSH */
887
888
889/* we need to keep enough input to show errors in context */
890#define LINE_LEN 80
891
892#ifdef DEBUG_INPUT
893#define CHECK_BUFFER(in) check_buffer(in)
894
Daniel Veillard01c13b52002-12-10 15:19:08 +0000895static
Owen Taylor3473f882001-02-23 17:55:21 +0000896void check_buffer(xmlParserInputPtr in) {
897 if (in->base != in->buf->buffer->content) {
898 xmlGenericError(xmlGenericErrorContext,
899 "xmlParserInput: base mismatch problem\n");
900 }
901 if (in->cur < in->base) {
902 xmlGenericError(xmlGenericErrorContext,
903 "xmlParserInput: cur < base problem\n");
904 }
905 if (in->cur > in->base + in->buf->buffer->use) {
906 xmlGenericError(xmlGenericErrorContext,
907 "xmlParserInput: cur > base + use problem\n");
908 }
909 xmlGenericError(xmlGenericErrorContext,"buffer %x : content %x, cur %d, use %d, size %d\n",
910 (int) in, (int) in->buf->buffer->content, in->cur - in->base,
911 in->buf->buffer->use, in->buf->buffer->size);
912}
913
914#else
915#define CHECK_BUFFER(in)
916#endif
917
918
919/**
920 * xmlParserInputRead:
921 * @in: an XML parser input
922 * @len: an indicative size for the lookahead
923 *
924 * This function refresh the input for the parser. It doesn't try to
925 * preserve pointers to the input buffer, and discard already read data
926 *
927 * Returns the number of xmlChars read, or -1 in case of error, 0 indicate the
928 * end of this entity
929 */
930int
931xmlParserInputRead(xmlParserInputPtr in, int len) {
932 int ret;
933 int used;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000934 int indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000935
936#ifdef DEBUG_INPUT
937 xmlGenericError(xmlGenericErrorContext, "Read\n");
938#endif
939 if (in->buf == NULL) return(-1);
940 if (in->base == NULL) return(-1);
941 if (in->cur == NULL) return(-1);
942 if (in->buf->buffer == NULL) return(-1);
943 if (in->buf->readcallback == NULL) return(-1);
944
945 CHECK_BUFFER(in);
946
947 used = in->cur - in->buf->buffer->content;
948 ret = xmlBufferShrink(in->buf->buffer, used);
949 if (ret > 0) {
950 in->cur -= ret;
951 in->consumed += ret;
952 }
953 ret = xmlParserInputBufferRead(in->buf, len);
954 if (in->base != in->buf->buffer->content) {
955 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000956 * the buffer has been reallocated
Owen Taylor3473f882001-02-23 17:55:21 +0000957 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000958 indx = in->cur - in->base;
Owen Taylor3473f882001-02-23 17:55:21 +0000959 in->base = in->buf->buffer->content;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000960 in->cur = &in->buf->buffer->content[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000961 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000962 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +0000963
964 CHECK_BUFFER(in);
965
966 return(ret);
967}
968
969/**
970 * xmlParserInputGrow:
971 * @in: an XML parser input
972 * @len: an indicative size for the lookahead
973 *
974 * This function increase the input for the parser. It tries to
975 * preserve pointers to the input buffer, and keep already read data
976 *
977 * Returns the number of xmlChars read, or -1 in case of error, 0 indicate the
978 * end of this entity
979 */
980int
981xmlParserInputGrow(xmlParserInputPtr in, int len) {
982 int ret;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000983 int indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000984
985#ifdef DEBUG_INPUT
986 xmlGenericError(xmlGenericErrorContext, "Grow\n");
987#endif
988 if (in->buf == NULL) return(-1);
989 if (in->base == NULL) return(-1);
990 if (in->cur == NULL) return(-1);
991 if (in->buf->buffer == NULL) return(-1);
992
993 CHECK_BUFFER(in);
994
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000995 indx = in->cur - in->base;
996 if (in->buf->buffer->use > (unsigned int) indx + INPUT_CHUNK) {
Owen Taylor3473f882001-02-23 17:55:21 +0000997
998 CHECK_BUFFER(in);
999
1000 return(0);
1001 }
1002 if (in->buf->readcallback != NULL)
1003 ret = xmlParserInputBufferGrow(in->buf, len);
1004 else
1005 return(0);
1006
1007 /*
Daniel Veillard48b2f892001-02-25 16:11:03 +00001008 * NOTE : in->base may be a "dangling" i.e. freed pointer in this
Owen Taylor3473f882001-02-23 17:55:21 +00001009 * block, but we use it really as an integer to do some
1010 * pointer arithmetic. Insure will raise it as a bug but in
1011 * that specific case, that's not !
1012 */
1013 if (in->base != in->buf->buffer->content) {
1014 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001015 * the buffer has been reallocated
Owen Taylor3473f882001-02-23 17:55:21 +00001016 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001017 indx = in->cur - in->base;
Owen Taylor3473f882001-02-23 17:55:21 +00001018 in->base = in->buf->buffer->content;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001019 in->cur = &in->buf->buffer->content[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00001020 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001021 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001022
1023 CHECK_BUFFER(in);
1024
1025 return(ret);
1026}
1027
1028/**
1029 * xmlParserInputShrink:
1030 * @in: an XML parser input
1031 *
1032 * This function removes used input for the parser.
1033 */
1034void
1035xmlParserInputShrink(xmlParserInputPtr in) {
1036 int used;
1037 int ret;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001038 int indx;
Owen Taylor3473f882001-02-23 17:55:21 +00001039
1040#ifdef DEBUG_INPUT
1041 xmlGenericError(xmlGenericErrorContext, "Shrink\n");
1042#endif
1043 if (in->buf == NULL) return;
1044 if (in->base == NULL) return;
1045 if (in->cur == NULL) return;
1046 if (in->buf->buffer == NULL) return;
1047
1048 CHECK_BUFFER(in);
1049
1050 used = in->cur - in->buf->buffer->content;
1051 /*
1052 * Do not shrink on large buffers whose only a tiny fraction
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001053 * was consumed
Owen Taylor3473f882001-02-23 17:55:21 +00001054 */
Daniel Veillarda880b122003-04-21 21:36:41 +00001055#if 0
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001056 if ((int) in->buf->buffer->use > used + 2 * INPUT_CHUNK)
Owen Taylor3473f882001-02-23 17:55:21 +00001057 return;
Daniel Veillarda880b122003-04-21 21:36:41 +00001058#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001059 if (used > INPUT_CHUNK) {
1060 ret = xmlBufferShrink(in->buf->buffer, used - LINE_LEN);
1061 if (ret > 0) {
1062 in->cur -= ret;
1063 in->consumed += ret;
1064 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001065 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001066 }
1067
1068 CHECK_BUFFER(in);
1069
1070 if (in->buf->buffer->use > INPUT_CHUNK) {
1071 return;
1072 }
1073 xmlParserInputBufferRead(in->buf, 2 * INPUT_CHUNK);
1074 if (in->base != in->buf->buffer->content) {
1075 /*
Daniel Veillard5e5c2d02002-02-09 18:03:01 +00001076 * the buffer has been reallocated
Owen Taylor3473f882001-02-23 17:55:21 +00001077 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001078 indx = in->cur - in->base;
Owen Taylor3473f882001-02-23 17:55:21 +00001079 in->base = in->buf->buffer->content;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001080 in->cur = &in->buf->buffer->content[indx];
Owen Taylor3473f882001-02-23 17:55:21 +00001081 }
Daniel Veillard48b2f892001-02-25 16:11:03 +00001082 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001083
1084 CHECK_BUFFER(in);
1085}
1086
1087/************************************************************************
1088 * *
1089 * UTF8 character input and related functions *
1090 * *
1091 ************************************************************************/
1092
1093/**
1094 * xmlNextChar:
1095 * @ctxt: the XML parser context
1096 *
1097 * Skip to the next char input char.
1098 */
1099
1100void
Daniel Veillard77a90a72003-03-22 00:04:05 +00001101xmlNextChar(xmlParserCtxtPtr ctxt)
1102{
Owen Taylor3473f882001-02-23 17:55:21 +00001103 if (ctxt->instate == XML_PARSER_EOF)
Daniel Veillard77a90a72003-03-22 00:04:05 +00001104 return;
Owen Taylor3473f882001-02-23 17:55:21 +00001105
Daniel Veillardfdc91562002-07-01 21:52:03 +00001106 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
Daniel Veillard77a90a72003-03-22 00:04:05 +00001107 if ((*ctxt->input->cur == 0) &&
1108 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0) &&
1109 (ctxt->instate != XML_PARSER_COMMENT)) {
1110 /*
1111 * If we are at the end of the current entity and
1112 * the context allows it, we pop consumed entities
1113 * automatically.
1114 * the auto closing should be blocked in other cases
1115 */
1116 xmlPopInput(ctxt);
1117 } else {
1118 const unsigned char *cur;
1119 unsigned char c;
Owen Taylor3473f882001-02-23 17:55:21 +00001120
Daniel Veillard77a90a72003-03-22 00:04:05 +00001121 /*
1122 * 2.11 End-of-Line Handling
1123 * the literal two-character sequence "#xD#xA" or a standalone
1124 * literal #xD, an XML processor must pass to the application
1125 * the single character #xA.
1126 */
1127 if (*(ctxt->input->cur) == '\n') {
1128 ctxt->input->line++;
1129 ctxt->input->col = 1;
1130 } else
1131 ctxt->input->col++;
Owen Taylor3473f882001-02-23 17:55:21 +00001132
Daniel Veillard77a90a72003-03-22 00:04:05 +00001133 /*
1134 * We are supposed to handle UTF8, check it's valid
1135 * From rfc2044: encoding of the Unicode values on UTF-8:
1136 *
1137 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
1138 * 0000 0000-0000 007F 0xxxxxxx
1139 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
1140 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
1141 *
1142 * Check for the 0x110000 limit too
1143 */
1144 cur = ctxt->input->cur;
1145
1146 c = *cur;
1147 if (c & 0x80) {
Daniel Veillard0e0f37a2003-05-20 12:22:41 +00001148 if (c == 0xC0)
1149 goto encoding_error;
Daniel Veillard77a90a72003-03-22 00:04:05 +00001150 if (cur[1] == 0)
1151 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1152 if ((cur[1] & 0xc0) != 0x80)
1153 goto encoding_error;
1154 if ((c & 0xe0) == 0xe0) {
1155 unsigned int val;
1156
1157 if (cur[2] == 0)
1158 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1159 if ((cur[2] & 0xc0) != 0x80)
1160 goto encoding_error;
1161 if ((c & 0xf0) == 0xf0) {
1162 if (cur[3] == 0)
1163 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1164 if (((c & 0xf8) != 0xf0) ||
1165 ((cur[3] & 0xc0) != 0x80))
1166 goto encoding_error;
1167 /* 4-byte code */
1168 ctxt->input->cur += 4;
1169 val = (cur[0] & 0x7) << 18;
1170 val |= (cur[1] & 0x3f) << 12;
1171 val |= (cur[2] & 0x3f) << 6;
1172 val |= cur[3] & 0x3f;
1173 } else {
1174 /* 3-byte code */
1175 ctxt->input->cur += 3;
1176 val = (cur[0] & 0xf) << 12;
1177 val |= (cur[1] & 0x3f) << 6;
1178 val |= cur[2] & 0x3f;
1179 }
1180 if (((val > 0xd7ff) && (val < 0xe000)) ||
1181 ((val > 0xfffd) && (val < 0x10000)) ||
1182 (val >= 0x110000)) {
1183 if ((ctxt->sax != NULL) &&
1184 (ctxt->sax->error != NULL))
1185 ctxt->sax->error(ctxt->userData,
1186 "Char 0x%X out of allowed range\n",
1187 val);
1188 ctxt->errNo = XML_ERR_INVALID_ENCODING;
1189 ctxt->wellFormed = 0;
1190 if (ctxt->recovery == 0)
1191 ctxt->disableSAX = 1;
1192 }
1193 } else
1194 /* 2-byte code */
1195 ctxt->input->cur += 2;
1196 } else
1197 /* 1-byte code */
1198 ctxt->input->cur++;
1199
1200 ctxt->nbChars++;
1201 if (*ctxt->input->cur == 0)
1202 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1203 }
Owen Taylor3473f882001-02-23 17:55:21 +00001204 } else {
Daniel Veillard77a90a72003-03-22 00:04:05 +00001205 /*
1206 * Assume it's a fixed length encoding (1) with
1207 * a compatible encoding for the ASCII set, since
1208 * XML constructs only use < 128 chars
1209 */
1210
1211 if (*(ctxt->input->cur) == '\n') {
1212 ctxt->input->line++;
1213 ctxt->input->col = 1;
1214 } else
1215 ctxt->input->col++;
1216 ctxt->input->cur++;
1217 ctxt->nbChars++;
1218 if (*ctxt->input->cur == 0)
1219 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Owen Taylor3473f882001-02-23 17:55:21 +00001220 }
Daniel Veillard561b7f82002-03-20 21:55:57 +00001221 if ((*ctxt->input->cur == '%') && (!ctxt->html))
Daniel Veillard77a90a72003-03-22 00:04:05 +00001222 xmlParserHandlePEReference(ctxt);
Daniel Veillard561b7f82002-03-20 21:55:57 +00001223 if ((*ctxt->input->cur == 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +00001224 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
Daniel Veillard77a90a72003-03-22 00:04:05 +00001225 xmlPopInput(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +00001226 return;
Daniel Veillard77a90a72003-03-22 00:04:05 +00001227 encoding_error:
Owen Taylor3473f882001-02-23 17:55:21 +00001228 /*
1229 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001230 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +00001231 * declaration header. Report the error and switch the encoding
1232 * to ISO-Latin-1 (if you don't like this policy, just declare the
1233 * encoding !)
1234 */
1235 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) {
Daniel Veillard77a90a72003-03-22 00:04:05 +00001236 ctxt->sax->error(ctxt->userData,
1237 "Input is not proper UTF-8, indicate encoding !\n");
1238 ctxt->sax->error(ctxt->userData,
1239 "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
1240 ctxt->input->cur[0], ctxt->input->cur[1],
1241 ctxt->input->cur[2], ctxt->input->cur[3]);
Owen Taylor3473f882001-02-23 17:55:21 +00001242 }
Daniel Veillard8ab0f582002-02-18 18:31:38 +00001243 ctxt->wellFormed = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001244 ctxt->errNo = XML_ERR_INVALID_ENCODING;
1245
Daniel Veillard77a90a72003-03-22 00:04:05 +00001246 ctxt->charset = XML_CHAR_ENCODING_8859_1;
Daniel Veillard561b7f82002-03-20 21:55:57 +00001247 ctxt->input->cur++;
Owen Taylor3473f882001-02-23 17:55:21 +00001248 return;
1249}
1250
1251/**
1252 * xmlCurrentChar:
1253 * @ctxt: the XML parser context
1254 * @len: pointer to the length of the char read
1255 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001256 * The current char value, if using UTF-8 this may actually span multiple
Owen Taylor3473f882001-02-23 17:55:21 +00001257 * bytes in the input buffer. Implement the end of line normalization:
1258 * 2.11 End-of-Line Handling
1259 * Wherever an external parsed entity or the literal entity value
1260 * of an internal parsed entity contains either the literal two-character
1261 * sequence "#xD#xA" or a standalone literal #xD, an XML processor
1262 * must pass to the application the single character #xA.
1263 * This behavior can conveniently be produced by normalizing all
1264 * line breaks to #xA on input, before parsing.)
1265 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001266 * Returns the current char value and its length
Owen Taylor3473f882001-02-23 17:55:21 +00001267 */
1268
1269int
1270xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
1271 if (ctxt->instate == XML_PARSER_EOF)
1272 return(0);
1273
Daniel Veillard561b7f82002-03-20 21:55:57 +00001274 if ((*ctxt->input->cur >= 0x20) && (*ctxt->input->cur <= 0x7F)) {
1275 *len = 1;
1276 return((int) *ctxt->input->cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001277 }
1278 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
1279 /*
1280 * We are supposed to handle UTF8, check it's valid
1281 * From rfc2044: encoding of the Unicode values on UTF-8:
1282 *
1283 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
1284 * 0000 0000-0000 007F 0xxxxxxx
1285 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
1286 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
1287 *
1288 * Check for the 0x110000 limit too
1289 */
1290 const unsigned char *cur = ctxt->input->cur;
1291 unsigned char c;
1292 unsigned int val;
1293
1294 c = *cur;
1295 if (c & 0x80) {
Daniel Veillard0e0f37a2003-05-20 12:22:41 +00001296 if (c == 0xC0)
1297 goto encoding_error;
Daniel Veillard561b7f82002-03-20 21:55:57 +00001298 if (cur[1] == 0)
1299 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1300 if ((cur[1] & 0xc0) != 0x80)
Owen Taylor3473f882001-02-23 17:55:21 +00001301 goto encoding_error;
1302 if ((c & 0xe0) == 0xe0) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00001303
1304 if (cur[2] == 0)
1305 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1306 if ((cur[2] & 0xc0) != 0x80)
Owen Taylor3473f882001-02-23 17:55:21 +00001307 goto encoding_error;
1308 if ((c & 0xf0) == 0xf0) {
1309 if (cur[3] == 0)
1310 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Daniel Veillard561b7f82002-03-20 21:55:57 +00001311 if (((c & 0xf8) != 0xf0) ||
Owen Taylor3473f882001-02-23 17:55:21 +00001312 ((cur[3] & 0xc0) != 0x80))
1313 goto encoding_error;
1314 /* 4-byte code */
1315 *len = 4;
1316 val = (cur[0] & 0x7) << 18;
1317 val |= (cur[1] & 0x3f) << 12;
1318 val |= (cur[2] & 0x3f) << 6;
1319 val |= cur[3] & 0x3f;
1320 } else {
1321 /* 3-byte code */
1322 *len = 3;
1323 val = (cur[0] & 0xf) << 12;
1324 val |= (cur[1] & 0x3f) << 6;
1325 val |= cur[2] & 0x3f;
1326 }
1327 } else {
1328 /* 2-byte code */
1329 *len = 2;
1330 val = (cur[0] & 0x1f) << 6;
1331 val |= cur[1] & 0x3f;
1332 }
1333 if (!IS_CHAR(val)) {
1334 if ((ctxt->sax != NULL) &&
1335 (ctxt->sax->error != NULL))
1336 ctxt->sax->error(ctxt->userData,
1337 "Char 0x%X out of allowed range\n", val);
1338 ctxt->errNo = XML_ERR_INVALID_ENCODING;
1339 ctxt->wellFormed = 0;
Daniel Veillarddad3f682002-11-17 16:47:27 +00001340 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001341 }
1342 return(val);
1343 } else {
1344 /* 1-byte code */
1345 *len = 1;
1346 if (*ctxt->input->cur == 0xD) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00001347 if (ctxt->input->cur[1] == 0xA) {
Owen Taylor3473f882001-02-23 17:55:21 +00001348 ctxt->nbChars++;
1349 ctxt->input->cur++;
1350 }
1351 return(0xA);
1352 }
1353 return((int) *ctxt->input->cur);
1354 }
1355 }
1356 /*
Daniel Veillard60087f32001-10-10 09:45:09 +00001357 * Assume it's a fixed length encoding (1) with
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001358 * a compatible encoding for the ASCII set, since
Owen Taylor3473f882001-02-23 17:55:21 +00001359 * XML constructs only use < 128 chars
1360 */
1361 *len = 1;
1362 if (*ctxt->input->cur == 0xD) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00001363 if (ctxt->input->cur[1] == 0xA) {
Owen Taylor3473f882001-02-23 17:55:21 +00001364 ctxt->nbChars++;
1365 ctxt->input->cur++;
1366 }
1367 return(0xA);
1368 }
1369 return((int) *ctxt->input->cur);
1370encoding_error:
1371 /*
Daniel Veillardd2ff0392002-11-22 12:28:38 +00001372 * An encoding problem may arise from a truncated input buffer
1373 * splitting a character in the middle. In that case do not raise
1374 * an error but return 0 to endicate an end of stream problem
1375 */
1376 if (ctxt->input->end - ctxt->input->cur < 4) {
1377 *len = 0;
1378 return(0);
1379 }
1380
1381 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001382 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001383 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +00001384 * declaration header. Report the error and switch the encoding
1385 * to ISO-Latin-1 (if you don't like this policy, just declare the
1386 * encoding !)
1387 */
1388 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) {
1389 ctxt->sax->error(ctxt->userData,
1390 "Input is not proper UTF-8, indicate encoding !\n");
1391 ctxt->sax->error(ctxt->userData, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
Daniel Veillard561b7f82002-03-20 21:55:57 +00001392 ctxt->input->cur[0], ctxt->input->cur[1],
1393 ctxt->input->cur[2], ctxt->input->cur[3]);
Owen Taylor3473f882001-02-23 17:55:21 +00001394 }
Daniel Veillard8ab0f582002-02-18 18:31:38 +00001395 ctxt->wellFormed = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001396 ctxt->errNo = XML_ERR_INVALID_ENCODING;
1397
1398 ctxt->charset = XML_CHAR_ENCODING_8859_1;
1399 *len = 1;
1400 return((int) *ctxt->input->cur);
1401}
1402
1403/**
1404 * xmlStringCurrentChar:
1405 * @ctxt: the XML parser context
1406 * @cur: pointer to the beginning of the char
1407 * @len: pointer to the length of the char read
1408 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001409 * The current char value, if using UTF-8 this may actually span multiple
Owen Taylor3473f882001-02-23 17:55:21 +00001410 * bytes in the input buffer.
1411 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001412 * Returns the current char value and its length
Owen Taylor3473f882001-02-23 17:55:21 +00001413 */
1414
1415int
Daniel Veillardd8224e02002-01-13 15:43:22 +00001416xmlStringCurrentChar(xmlParserCtxtPtr ctxt, const xmlChar * cur, int *len)
1417{
Daniel Veillard61d80a22001-04-27 17:13:01 +00001418 if ((ctxt == NULL) || (ctxt->charset == XML_CHAR_ENCODING_UTF8)) {
Daniel Veillardd8224e02002-01-13 15:43:22 +00001419 /*
1420 * We are supposed to handle UTF8, check it's valid
1421 * From rfc2044: encoding of the Unicode values on UTF-8:
1422 *
1423 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
1424 * 0000 0000-0000 007F 0xxxxxxx
1425 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
1426 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
1427 *
1428 * Check for the 0x110000 limit too
1429 */
1430 unsigned char c;
1431 unsigned int val;
Owen Taylor3473f882001-02-23 17:55:21 +00001432
Daniel Veillardd8224e02002-01-13 15:43:22 +00001433 c = *cur;
1434 if (c & 0x80) {
1435 if ((cur[1] & 0xc0) != 0x80)
1436 goto encoding_error;
1437 if ((c & 0xe0) == 0xe0) {
Owen Taylor3473f882001-02-23 17:55:21 +00001438
Daniel Veillardd8224e02002-01-13 15:43:22 +00001439 if ((cur[2] & 0xc0) != 0x80)
1440 goto encoding_error;
1441 if ((c & 0xf0) == 0xf0) {
1442 if (((c & 0xf8) != 0xf0) || ((cur[3] & 0xc0) != 0x80))
1443 goto encoding_error;
1444 /* 4-byte code */
1445 *len = 4;
1446 val = (cur[0] & 0x7) << 18;
1447 val |= (cur[1] & 0x3f) << 12;
1448 val |= (cur[2] & 0x3f) << 6;
1449 val |= cur[3] & 0x3f;
1450 } else {
1451 /* 3-byte code */
1452 *len = 3;
1453 val = (cur[0] & 0xf) << 12;
1454 val |= (cur[1] & 0x3f) << 6;
1455 val |= cur[2] & 0x3f;
1456 }
1457 } else {
1458 /* 2-byte code */
1459 *len = 2;
1460 val = (cur[0] & 0x1f) << 6;
1461 val |= cur[1] & 0x3f;
1462 }
1463 if (!IS_CHAR(val)) {
1464 if ((ctxt != NULL) && (ctxt->sax != NULL) &&
1465 (ctxt->sax->error != NULL))
1466 ctxt->sax->error(ctxt->userData,
1467 "Char 0x%X out of allowed range\n",
1468 val);
Daniel Veillardd076a202002-11-20 13:28:31 +00001469 if (ctxt != NULL) {
1470 ctxt->errNo = XML_ERR_INVALID_ENCODING;
1471 ctxt->wellFormed = 0;
1472 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
1473 }
Daniel Veillardd8224e02002-01-13 15:43:22 +00001474 }
1475 return (val);
1476 } else {
1477 /* 1-byte code */
1478 *len = 1;
1479 return ((int) *cur);
1480 }
Owen Taylor3473f882001-02-23 17:55:21 +00001481 }
1482 /*
Daniel Veillard60087f32001-10-10 09:45:09 +00001483 * Assume it's a fixed length encoding (1) with
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001484 * a compatible encoding for the ASCII set, since
Owen Taylor3473f882001-02-23 17:55:21 +00001485 * XML constructs only use < 128 chars
1486 */
1487 *len = 1;
Daniel Veillardd8224e02002-01-13 15:43:22 +00001488 return ((int) *cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001489encoding_error:
Daniel Veillardd8224e02002-01-13 15:43:22 +00001490
Owen Taylor3473f882001-02-23 17:55:21 +00001491 /*
1492 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001493 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +00001494 * declaration header. Report the error and switch the encoding
1495 * to ISO-Latin-1 (if you don't like this policy, just declare the
1496 * encoding !)
1497 */
Daniel Veillardd8224e02002-01-13 15:43:22 +00001498 if (ctxt != NULL) {
1499 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) {
1500 ctxt->sax->error(ctxt->userData,
1501 "Input is not proper UTF-8, indicate encoding !\n");
1502 ctxt->sax->error(ctxt->userData,
1503 "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
1504 ctxt->input->cur[0], ctxt->input->cur[1],
1505 ctxt->input->cur[2], ctxt->input->cur[3]);
1506 }
1507 ctxt->errNo = XML_ERR_INVALID_ENCODING;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00001508 ctxt->wellFormed = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001509 }
Owen Taylor3473f882001-02-23 17:55:21 +00001510
1511 *len = 1;
Daniel Veillardd8224e02002-01-13 15:43:22 +00001512 return ((int) *cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001513}
1514
1515/**
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001516 * xmlCopyCharMultiByte:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001517 * @out: pointer to an array of xmlChar
Owen Taylor3473f882001-02-23 17:55:21 +00001518 * @val: the char value
1519 *
1520 * append the char value in the array
1521 *
1522 * Returns the number of xmlChar written
1523 */
Owen Taylor3473f882001-02-23 17:55:21 +00001524int
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001525xmlCopyCharMultiByte(xmlChar *out, int val) {
Owen Taylor3473f882001-02-23 17:55:21 +00001526 /*
1527 * We are supposed to handle UTF8, check it's valid
1528 * From rfc2044: encoding of the Unicode values on UTF-8:
1529 *
1530 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
1531 * 0000 0000-0000 007F 0xxxxxxx
1532 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
1533 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
1534 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001535 if (val >= 0x80) {
1536 xmlChar *savedout = out;
1537 int bits;
1538 if (val < 0x800) { *out++= (val >> 6) | 0xC0; bits= 0; }
1539 else if (val < 0x10000) { *out++= (val >> 12) | 0xE0; bits= 6;}
1540 else if (val < 0x110000) { *out++= (val >> 18) | 0xF0; bits= 12; }
1541 else {
Owen Taylor3473f882001-02-23 17:55:21 +00001542 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001543 "Internal error, xmlCopyCharMultiByte 0x%X out of bound\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001544 val);
1545 return(0);
1546 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001547 for ( ; bits >= 0; bits-= 6)
1548 *out++= ((val >> bits) & 0x3F) | 0x80 ;
1549 return (out - savedout);
Owen Taylor3473f882001-02-23 17:55:21 +00001550 }
1551 *out = (xmlChar) val;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001552 return 1;
1553}
1554
1555/**
1556 * xmlCopyChar:
1557 * @len: Ignored, compatibility
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001558 * @out: pointer to an array of xmlChar
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001559 * @val: the char value
1560 *
1561 * append the char value in the array
1562 *
1563 * Returns the number of xmlChar written
1564 */
1565
1566int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001567xmlCopyChar(int len ATTRIBUTE_UNUSED, xmlChar *out, int val) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001568 /* the len parameter is ignored */
1569 if (val >= 0x80) {
1570 return(xmlCopyCharMultiByte (out, val));
1571 }
1572 *out = (xmlChar) val;
1573 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001574}
1575
1576/************************************************************************
1577 * *
1578 * Commodity functions to switch encodings *
1579 * *
1580 ************************************************************************/
1581
1582/**
1583 * xmlSwitchEncoding:
1584 * @ctxt: the parser context
1585 * @enc: the encoding value (number)
1586 *
1587 * change the input functions when discovering the character encoding
1588 * of a given entity.
1589 *
1590 * Returns 0 in case of success, -1 otherwise
1591 */
1592int
1593xmlSwitchEncoding(xmlParserCtxtPtr ctxt, xmlCharEncoding enc)
1594{
1595 xmlCharEncodingHandlerPtr handler;
1596
1597 switch (enc) {
1598 case XML_CHAR_ENCODING_ERROR:
1599 ctxt->errNo = XML_ERR_UNKNOWN_ENCODING;
1600 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1601 ctxt->sax->error(ctxt->userData, "encoding unknown\n");
1602 ctxt->wellFormed = 0;
Daniel Veillarddad3f682002-11-17 16:47:27 +00001603 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001604 break;
1605 case XML_CHAR_ENCODING_NONE:
1606 /* let's assume it's UTF-8 without the XML decl */
1607 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1608 return(0);
1609 case XML_CHAR_ENCODING_UTF8:
1610 /* default encoding, no conversion should be needed */
1611 ctxt->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard87a764e2001-06-20 17:41:10 +00001612
1613 /*
1614 * Errata on XML-1.0 June 20 2001
1615 * Specific handling of the Byte Order Mark for
1616 * UTF-8
1617 */
Daniel Veillard3e5bb8e2001-06-27 16:34:34 +00001618 if ((ctxt->input != NULL) &&
1619 (ctxt->input->cur[0] == 0xEF) &&
Daniel Veillard87a764e2001-06-20 17:41:10 +00001620 (ctxt->input->cur[1] == 0xBB) &&
1621 (ctxt->input->cur[2] == 0xBF)) {
1622 ctxt->input->cur += 3;
1623 }
Owen Taylor3473f882001-02-23 17:55:21 +00001624 return(0);
Daniel Veillard2dcb9372003-07-16 21:18:19 +00001625 case XML_CHAR_ENCODING_UTF16LE:
1626 case XML_CHAR_ENCODING_UTF16BE:
1627 /*The raw input characters are encoded
1628 *in UTF-16. As we expect this function
1629 *to be called after xmlCharEncInFunc, we expect
1630 *ctxt->input->cur to contain UTF-8 encoded characters.
1631 *So the raw UTF16 Byte Order Mark
1632 *has also been converted into
1633 *an UTF-8 BOM. Let's skip that BOM.
1634 */
1635 if ((ctxt->input != NULL) &&
1636 (ctxt->input->cur[0] == 0xEF) &&
1637 (ctxt->input->cur[1] == 0xBB) &&
1638 (ctxt->input->cur[2] == 0xBF)) {
1639 ctxt->input->cur += 3;
1640 }
1641 break ;
Owen Taylor3473f882001-02-23 17:55:21 +00001642 default:
1643 break;
1644 }
1645 handler = xmlGetCharEncodingHandler(enc);
1646 if (handler == NULL) {
1647 /*
1648 * Default handlers.
1649 */
1650 switch (enc) {
1651 case XML_CHAR_ENCODING_ERROR:
1652 ctxt->errNo = XML_ERR_UNKNOWN_ENCODING;
1653 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1654 ctxt->sax->error(ctxt->userData, "encoding unknown\n");
1655 ctxt->wellFormed = 0;
Daniel Veillarddad3f682002-11-17 16:47:27 +00001656 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001657 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1658 break;
1659 case XML_CHAR_ENCODING_NONE:
1660 /* let's assume it's UTF-8 without the XML decl */
1661 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1662 return(0);
1663 case XML_CHAR_ENCODING_UTF8:
1664 case XML_CHAR_ENCODING_ASCII:
1665 /* default encoding, no conversion should be needed */
1666 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1667 return(0);
1668 case XML_CHAR_ENCODING_UTF16LE:
1669 break;
1670 case XML_CHAR_ENCODING_UTF16BE:
1671 break;
1672 case XML_CHAR_ENCODING_UCS4LE:
1673 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1674 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1675 ctxt->sax->error(ctxt->userData,
1676 "char encoding USC4 little endian not supported\n");
1677 break;
1678 case XML_CHAR_ENCODING_UCS4BE:
1679 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1680 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1681 ctxt->sax->error(ctxt->userData,
1682 "char encoding USC4 big endian not supported\n");
1683 break;
1684 case XML_CHAR_ENCODING_EBCDIC:
1685 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1686 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1687 ctxt->sax->error(ctxt->userData,
1688 "char encoding EBCDIC not supported\n");
1689 break;
1690 case XML_CHAR_ENCODING_UCS4_2143:
1691 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1692 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1693 ctxt->sax->error(ctxt->userData,
1694 "char encoding UCS4 2143 not supported\n");
1695 break;
1696 case XML_CHAR_ENCODING_UCS4_3412:
1697 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1698 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1699 ctxt->sax->error(ctxt->userData,
1700 "char encoding UCS4 3412 not supported\n");
1701 break;
1702 case XML_CHAR_ENCODING_UCS2:
1703 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1704 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1705 ctxt->sax->error(ctxt->userData,
1706 "char encoding UCS2 not supported\n");
1707 break;
1708 case XML_CHAR_ENCODING_8859_1:
1709 case XML_CHAR_ENCODING_8859_2:
1710 case XML_CHAR_ENCODING_8859_3:
1711 case XML_CHAR_ENCODING_8859_4:
1712 case XML_CHAR_ENCODING_8859_5:
1713 case XML_CHAR_ENCODING_8859_6:
1714 case XML_CHAR_ENCODING_8859_7:
1715 case XML_CHAR_ENCODING_8859_8:
1716 case XML_CHAR_ENCODING_8859_9:
1717 /*
1718 * We used to keep the internal content in the
1719 * document encoding however this turns being unmaintainable
1720 * So xmlGetCharEncodingHandler() will return non-null
1721 * values for this now.
1722 */
1723 if ((ctxt->inputNr == 1) &&
1724 (ctxt->encoding == NULL) &&
1725 (ctxt->input->encoding != NULL)) {
1726 ctxt->encoding = xmlStrdup(ctxt->input->encoding);
1727 }
1728 ctxt->charset = enc;
1729 return(0);
1730 case XML_CHAR_ENCODING_2022_JP:
1731 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1732 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1733 ctxt->sax->error(ctxt->userData,
1734 "char encoding ISO-2022-JPnot supported\n");
1735 break;
1736 case XML_CHAR_ENCODING_SHIFT_JIS:
1737 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1738 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1739 ctxt->sax->error(ctxt->userData,
1740 "char encoding Shift_JIS not supported\n");
1741 break;
1742 case XML_CHAR_ENCODING_EUC_JP:
1743 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1744 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1745 ctxt->sax->error(ctxt->userData,
1746 "char encoding EUC-JPnot supported\n");
1747 break;
1748 }
1749 }
1750 if (handler == NULL)
1751 return(-1);
1752 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1753 return(xmlSwitchToEncoding(ctxt, handler));
1754}
1755
1756/**
1757 * xmlSwitchToEncoding:
1758 * @ctxt: the parser context
1759 * @handler: the encoding handler
1760 *
1761 * change the input functions when discovering the character encoding
1762 * of a given entity.
1763 *
1764 * Returns 0 in case of success, -1 otherwise
1765 */
1766int
1767xmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler)
1768{
1769 int nbchars;
1770
1771 if (handler != NULL) {
1772 if (ctxt->input != NULL) {
1773 if (ctxt->input->buf != NULL) {
1774 if (ctxt->input->buf->encoder != NULL) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001775 /*
1776 * Check in case the auto encoding detetection triggered
1777 * in already.
1778 */
Owen Taylor3473f882001-02-23 17:55:21 +00001779 if (ctxt->input->buf->encoder == handler)
1780 return(0);
Daniel Veillard878eab02002-02-19 13:46:09 +00001781
1782 /*
1783 * "UTF-16" can be used for both LE and BE
Daniel Veillard878eab02002-02-19 13:46:09 +00001784 if ((!xmlStrncmp(BAD_CAST ctxt->input->buf->encoder->name,
1785 BAD_CAST "UTF-16", 6)) &&
1786 (!xmlStrncmp(BAD_CAST handler->name,
1787 BAD_CAST "UTF-16", 6))) {
1788 return(0);
1789 }
Daniel Veillarda6874ca2003-07-29 16:47:24 +00001790 */
Daniel Veillard878eab02002-02-19 13:46:09 +00001791
Owen Taylor3473f882001-02-23 17:55:21 +00001792 /*
1793 * Note: this is a bit dangerous, but that's what it
1794 * takes to use nearly compatible signature for different
1795 * encodings.
1796 */
1797 xmlCharEncCloseFunc(ctxt->input->buf->encoder);
1798 ctxt->input->buf->encoder = handler;
1799 return(0);
1800 }
1801 ctxt->input->buf->encoder = handler;
1802
1803 /*
1804 * Is there already some content down the pipe to convert ?
1805 */
1806 if ((ctxt->input->buf->buffer != NULL) &&
1807 (ctxt->input->buf->buffer->use > 0)) {
1808 int processed;
1809
1810 /*
1811 * Specific handling of the Byte Order Mark for
1812 * UTF-16
1813 */
1814 if ((handler->name != NULL) &&
1815 (!strcmp(handler->name, "UTF-16LE")) &&
1816 (ctxt->input->cur[0] == 0xFF) &&
1817 (ctxt->input->cur[1] == 0xFE)) {
1818 ctxt->input->cur += 2;
1819 }
1820 if ((handler->name != NULL) &&
1821 (!strcmp(handler->name, "UTF-16BE")) &&
1822 (ctxt->input->cur[0] == 0xFE) &&
1823 (ctxt->input->cur[1] == 0xFF)) {
1824 ctxt->input->cur += 2;
1825 }
Daniel Veillard87a764e2001-06-20 17:41:10 +00001826 /*
1827 * Errata on XML-1.0 June 20 2001
1828 * Specific handling of the Byte Order Mark for
1829 * UTF-8
1830 */
1831 if ((handler->name != NULL) &&
1832 (!strcmp(handler->name, "UTF-8")) &&
1833 (ctxt->input->cur[0] == 0xEF) &&
1834 (ctxt->input->cur[1] == 0xBB) &&
Daniel Veillard7dd05702001-10-04 14:25:12 +00001835 (ctxt->input->cur[2] == 0xBF)) {
Daniel Veillard87a764e2001-06-20 17:41:10 +00001836 ctxt->input->cur += 3;
1837 }
Owen Taylor3473f882001-02-23 17:55:21 +00001838
1839 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001840 * Shrink the current input buffer.
Owen Taylor3473f882001-02-23 17:55:21 +00001841 * Move it as the raw buffer and create a new input buffer
1842 */
1843 processed = ctxt->input->cur - ctxt->input->base;
1844 xmlBufferShrink(ctxt->input->buf->buffer, processed);
1845 ctxt->input->buf->raw = ctxt->input->buf->buffer;
1846 ctxt->input->buf->buffer = xmlBufferCreate();
1847
1848 if (ctxt->html) {
1849 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001850 * convert as much as possible of the buffer
Owen Taylor3473f882001-02-23 17:55:21 +00001851 */
1852 nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder,
1853 ctxt->input->buf->buffer,
1854 ctxt->input->buf->raw);
1855 } else {
1856 /*
1857 * convert just enough to get
1858 * '<?xml version="1.0" encoding="xxx"?>'
1859 * parsed with the autodetected encoding
1860 * into the parser reading buffer.
1861 */
1862 nbchars = xmlCharEncFirstLine(ctxt->input->buf->encoder,
1863 ctxt->input->buf->buffer,
1864 ctxt->input->buf->raw);
1865 }
1866 if (nbchars < 0) {
1867 xmlGenericError(xmlGenericErrorContext,
1868 "xmlSwitchToEncoding: encoder error\n");
1869 return(-1);
1870 }
1871 ctxt->input->base =
1872 ctxt->input->cur = ctxt->input->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001873 ctxt->input->end =
1874 &ctxt->input->base[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001875
1876 }
1877 return(0);
1878 } else {
1879 if ((ctxt->input->length == 0) || (ctxt->input->buf == NULL)) {
1880 /*
1881 * When parsing a static memory array one must know the
1882 * size to be able to convert the buffer.
1883 */
1884 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1885 ctxt->sax->error(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001886 "xmlSwitchToEncoding : no input\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001887 return(-1);
1888 } else {
1889 int processed;
1890
1891 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001892 * Shrink the current input buffer.
Owen Taylor3473f882001-02-23 17:55:21 +00001893 * Move it as the raw buffer and create a new input buffer
1894 */
1895 processed = ctxt->input->cur - ctxt->input->base;
1896
1897 ctxt->input->buf->raw = xmlBufferCreate();
1898 xmlBufferAdd(ctxt->input->buf->raw, ctxt->input->cur,
1899 ctxt->input->length - processed);
1900 ctxt->input->buf->buffer = xmlBufferCreate();
1901
1902 /*
1903 * convert as much as possible of the raw input
1904 * to the parser reading buffer.
1905 */
1906 nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder,
1907 ctxt->input->buf->buffer,
1908 ctxt->input->buf->raw);
1909 if (nbchars < 0) {
1910 xmlGenericError(xmlGenericErrorContext,
1911 "xmlSwitchToEncoding: encoder error\n");
1912 return(-1);
1913 }
1914
1915 /*
1916 * Conversion succeeded, get rid of the old buffer
1917 */
1918 if ((ctxt->input->free != NULL) &&
1919 (ctxt->input->base != NULL))
1920 ctxt->input->free((xmlChar *) ctxt->input->base);
1921 ctxt->input->base =
1922 ctxt->input->cur = ctxt->input->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001923 ctxt->input->end =
1924 &ctxt->input->base[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001925 }
1926 }
1927 } else {
1928 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1929 ctxt->sax->error(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001930 "xmlSwitchToEncoding : no input\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001931 return(-1);
1932 }
1933 /*
1934 * The parsing is now done in UTF8 natively
1935 */
1936 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1937 } else
1938 return(-1);
1939 return(0);
1940
1941}
1942
1943/************************************************************************
1944 * *
1945 * Commodity functions to handle entities processing *
1946 * *
1947 ************************************************************************/
1948
1949/**
1950 * xmlFreeInputStream:
1951 * @input: an xmlParserInputPtr
1952 *
1953 * Free up an input stream.
1954 */
1955void
1956xmlFreeInputStream(xmlParserInputPtr input) {
1957 if (input == NULL) return;
1958
1959 if (input->filename != NULL) xmlFree((char *) input->filename);
1960 if (input->directory != NULL) xmlFree((char *) input->directory);
1961 if (input->encoding != NULL) xmlFree((char *) input->encoding);
1962 if (input->version != NULL) xmlFree((char *) input->version);
1963 if ((input->free != NULL) && (input->base != NULL))
1964 input->free((xmlChar *) input->base);
1965 if (input->buf != NULL)
1966 xmlFreeParserInputBuffer(input->buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001967 xmlFree(input);
1968}
1969
1970/**
1971 * xmlNewInputStream:
1972 * @ctxt: an XML parser context
1973 *
1974 * Create a new input stream structure
1975 * Returns the new input stream or NULL
1976 */
1977xmlParserInputPtr
1978xmlNewInputStream(xmlParserCtxtPtr ctxt) {
1979 xmlParserInputPtr input;
1980
1981 input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput));
1982 if (input == NULL) {
1983 if (ctxt != NULL) {
1984 ctxt->errNo = XML_ERR_NO_MEMORY;
1985 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1986 ctxt->sax->error(ctxt->userData,
1987 "malloc: couldn't allocate a new input stream\n");
1988 ctxt->errNo = XML_ERR_NO_MEMORY;
1989 }
1990 return(NULL);
1991 }
1992 memset(input, 0, sizeof(xmlParserInput));
1993 input->line = 1;
1994 input->col = 1;
1995 input->standalone = -1;
1996 return(input);
1997}
1998
1999/**
2000 * xmlNewIOInputStream:
2001 * @ctxt: an XML parser context
2002 * @input: an I/O Input
2003 * @enc: the charset encoding if known
2004 *
2005 * Create a new input stream structure encapsulating the @input into
2006 * a stream suitable for the parser.
2007 *
2008 * Returns the new input stream or NULL
2009 */
2010xmlParserInputPtr
2011xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input,
2012 xmlCharEncoding enc) {
2013 xmlParserInputPtr inputStream;
2014
2015 if (xmlParserDebugEntities)
2016 xmlGenericError(xmlGenericErrorContext, "new input from I/O\n");
2017 inputStream = xmlNewInputStream(ctxt);
2018 if (inputStream == NULL) {
2019 return(NULL);
2020 }
2021 inputStream->filename = NULL;
2022 inputStream->buf = input;
2023 inputStream->base = inputStream->buf->buffer->content;
2024 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00002025 inputStream->end = &inputStream->base[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00002026 if (enc != XML_CHAR_ENCODING_NONE) {
2027 xmlSwitchEncoding(ctxt, enc);
2028 }
2029
2030 return(inputStream);
2031}
2032
2033/**
2034 * xmlNewEntityInputStream:
2035 * @ctxt: an XML parser context
2036 * @entity: an Entity pointer
2037 *
2038 * Create a new input stream based on an xmlEntityPtr
2039 *
2040 * Returns the new input stream or NULL
2041 */
2042xmlParserInputPtr
2043xmlNewEntityInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
2044 xmlParserInputPtr input;
2045
2046 if (entity == NULL) {
2047 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
2048 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2049 ctxt->sax->error(ctxt->userData,
2050 "internal: xmlNewEntityInputStream entity = NULL\n");
2051 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
2052 return(NULL);
2053 }
2054 if (xmlParserDebugEntities)
2055 xmlGenericError(xmlGenericErrorContext,
2056 "new input from entity: %s\n", entity->name);
2057 if (entity->content == NULL) {
2058 switch (entity->etype) {
2059 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
2060 ctxt->errNo = XML_ERR_UNPARSED_ENTITY;
2061 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2062 ctxt->sax->error(ctxt->userData,
2063 "xmlNewEntityInputStream unparsed entity !\n");
2064 break;
2065 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
2066 case XML_EXTERNAL_PARAMETER_ENTITY:
2067 return(xmlLoadExternalEntity((char *) entity->URI,
2068 (char *) entity->ExternalID, ctxt));
2069 case XML_INTERNAL_GENERAL_ENTITY:
2070 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2071 ctxt->sax->error(ctxt->userData,
2072 "Internal entity %s without content !\n", entity->name);
2073 break;
2074 case XML_INTERNAL_PARAMETER_ENTITY:
2075 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
2076 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2077 ctxt->sax->error(ctxt->userData,
2078 "Internal parameter entity %s without content !\n", entity->name);
2079 break;
2080 case XML_INTERNAL_PREDEFINED_ENTITY:
2081 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
2082 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2083 ctxt->sax->error(ctxt->userData,
2084 "Predefined entity %s without content !\n", entity->name);
2085 break;
2086 }
2087 return(NULL);
2088 }
2089 input = xmlNewInputStream(ctxt);
2090 if (input == NULL) {
2091 return(NULL);
2092 }
2093 input->filename = (char *) entity->URI;
2094 input->base = entity->content;
2095 input->cur = entity->content;
2096 input->length = entity->length;
Daniel Veillard48b2f892001-02-25 16:11:03 +00002097 input->end = &entity->content[input->length];
Owen Taylor3473f882001-02-23 17:55:21 +00002098 return(input);
2099}
2100
2101/**
2102 * xmlNewStringInputStream:
2103 * @ctxt: an XML parser context
2104 * @buffer: an memory buffer
2105 *
2106 * Create a new input stream based on a memory buffer.
2107 * Returns the new input stream
2108 */
2109xmlParserInputPtr
2110xmlNewStringInputStream(xmlParserCtxtPtr ctxt, const xmlChar *buffer) {
2111 xmlParserInputPtr input;
2112
2113 if (buffer == NULL) {
2114 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
2115 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2116 ctxt->sax->error(ctxt->userData,
2117 "internal: xmlNewStringInputStream string = NULL\n");
2118 return(NULL);
2119 }
2120 if (xmlParserDebugEntities)
2121 xmlGenericError(xmlGenericErrorContext,
2122 "new fixed input: %.30s\n", buffer);
2123 input = xmlNewInputStream(ctxt);
2124 if (input == NULL) {
2125 return(NULL);
2126 }
2127 input->base = buffer;
2128 input->cur = buffer;
2129 input->length = xmlStrlen(buffer);
Daniel Veillard48b2f892001-02-25 16:11:03 +00002130 input->end = &buffer[input->length];
Owen Taylor3473f882001-02-23 17:55:21 +00002131 return(input);
2132}
2133
2134/**
2135 * xmlNewInputFromFile:
2136 * @ctxt: an XML parser context
2137 * @filename: the filename to use as entity
2138 *
2139 * Create a new input stream based on a file.
2140 *
2141 * Returns the new input stream or NULL in case of error
2142 */
2143xmlParserInputPtr
2144xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) {
2145 xmlParserInputBufferPtr buf;
2146 xmlParserInputPtr inputStream;
2147 char *directory = NULL;
2148 xmlChar *URI = NULL;
2149
2150 if (xmlParserDebugEntities)
2151 xmlGenericError(xmlGenericErrorContext,
2152 "new input from file: %s\n", filename);
2153 if (ctxt == NULL) return(NULL);
2154 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
2155 if (buf == NULL)
2156 return(NULL);
2157
2158 URI = xmlStrdup((xmlChar *) filename);
2159 directory = xmlParserGetDirectory((const char *) URI);
2160
2161 inputStream = xmlNewInputStream(ctxt);
2162 if (inputStream == NULL) {
2163 if (directory != NULL) xmlFree((char *) directory);
2164 if (URI != NULL) xmlFree((char *) URI);
2165 return(NULL);
2166 }
2167
2168 inputStream->filename = (const char *) URI;
2169 inputStream->directory = directory;
2170 inputStream->buf = buf;
2171
2172 inputStream->base = inputStream->buf->buffer->content;
2173 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00002174 inputStream->end = &inputStream->base[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00002175 if ((ctxt->directory == NULL) && (directory != NULL))
2176 ctxt->directory = (char *) xmlStrdup((const xmlChar *) directory);
2177 return(inputStream);
2178}
2179
2180/************************************************************************
2181 * *
2182 * Commodity functions to handle parser contexts *
2183 * *
2184 ************************************************************************/
2185
2186/**
2187 * xmlInitParserCtxt:
2188 * @ctxt: an XML parser context
2189 *
2190 * Initialize a parser context
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002191 *
2192 * Returns 0 in case of success and -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +00002193 */
2194
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002195int
Owen Taylor3473f882001-02-23 17:55:21 +00002196xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
2197{
Daniel Veillard5d96fff2001-08-31 14:55:30 +00002198 if(ctxt==NULL) {
2199 xmlGenericError(xmlGenericErrorContext,
2200 "xmlInitParserCtxt: NULL context given\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002201 return(-1);
Daniel Veillard5d96fff2001-08-31 14:55:30 +00002202 }
2203
Owen Taylor3473f882001-02-23 17:55:21 +00002204 xmlDefaultSAXHandlerInit();
2205
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002206 ctxt->dict = xmlDictCreate();
2207 if (ctxt->dict == NULL) {
2208 xmlGenericError(xmlGenericErrorContext,
2209 "xmlInitParserCtxt: out of memory\n");
2210 return(-1);
2211 }
William M. Brack8b2c7f12002-11-22 05:07:29 +00002212 ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
2213 if (ctxt->sax == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00002214 xmlGenericError(xmlGenericErrorContext,
2215 "xmlInitParserCtxt: out of memory\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002216 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002217 }
2218 else
William M. Brack8b2c7f12002-11-22 05:07:29 +00002219 memcpy(ctxt->sax, &xmlDefaultSAXHandler, sizeof(xmlSAXHandler));
Owen Taylor3473f882001-02-23 17:55:21 +00002220
2221 /* Allocate the Input stack */
2222 ctxt->inputTab = (xmlParserInputPtr *)
2223 xmlMalloc(5 * sizeof(xmlParserInputPtr));
2224 if (ctxt->inputTab == NULL) {
2225 xmlGenericError(xmlGenericErrorContext,
2226 "xmlInitParserCtxt: out of memory\n");
2227 ctxt->inputNr = 0;
2228 ctxt->inputMax = 0;
2229 ctxt->input = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002230 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002231 }
2232 ctxt->inputNr = 0;
2233 ctxt->inputMax = 5;
2234 ctxt->input = NULL;
2235
2236 ctxt->version = NULL;
2237 ctxt->encoding = NULL;
2238 ctxt->standalone = -1;
2239 ctxt->hasExternalSubset = 0;
2240 ctxt->hasPErefs = 0;
2241 ctxt->html = 0;
2242 ctxt->external = 0;
2243 ctxt->instate = XML_PARSER_START;
2244 ctxt->token = 0;
2245 ctxt->directory = NULL;
2246
2247 /* Allocate the Node stack */
2248 ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr));
2249 if (ctxt->nodeTab == NULL) {
2250 xmlGenericError(xmlGenericErrorContext,
2251 "xmlInitParserCtxt: out of memory\n");
2252 ctxt->nodeNr = 0;
2253 ctxt->nodeMax = 0;
2254 ctxt->node = NULL;
2255 ctxt->inputNr = 0;
2256 ctxt->inputMax = 0;
2257 ctxt->input = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002258 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002259 }
2260 ctxt->nodeNr = 0;
2261 ctxt->nodeMax = 10;
2262 ctxt->node = NULL;
2263
2264 /* Allocate the Name stack */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002265 ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00002266 if (ctxt->nameTab == NULL) {
2267 xmlGenericError(xmlGenericErrorContext,
2268 "xmlInitParserCtxt: out of memory\n");
2269 ctxt->nodeNr = 0;
2270 ctxt->nodeMax = 0;
2271 ctxt->node = NULL;
2272 ctxt->inputNr = 0;
2273 ctxt->inputMax = 0;
2274 ctxt->input = NULL;
2275 ctxt->nameNr = 0;
2276 ctxt->nameMax = 0;
2277 ctxt->name = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002278 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002279 }
2280 ctxt->nameNr = 0;
2281 ctxt->nameMax = 10;
2282 ctxt->name = NULL;
2283
2284 /* Allocate the space stack */
2285 ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int));
2286 if (ctxt->spaceTab == NULL) {
2287 xmlGenericError(xmlGenericErrorContext,
2288 "xmlInitParserCtxt: out of memory\n");
2289 ctxt->nodeNr = 0;
2290 ctxt->nodeMax = 0;
2291 ctxt->node = NULL;
2292 ctxt->inputNr = 0;
2293 ctxt->inputMax = 0;
2294 ctxt->input = NULL;
2295 ctxt->nameNr = 0;
2296 ctxt->nameMax = 0;
2297 ctxt->name = NULL;
2298 ctxt->spaceNr = 0;
2299 ctxt->spaceMax = 0;
2300 ctxt->space = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002301 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002302 }
2303 ctxt->spaceNr = 1;
2304 ctxt->spaceMax = 10;
2305 ctxt->spaceTab[0] = -1;
2306 ctxt->space = &ctxt->spaceTab[0];
Owen Taylor3473f882001-02-23 17:55:21 +00002307 ctxt->userData = ctxt;
2308 ctxt->myDoc = NULL;
2309 ctxt->wellFormed = 1;
2310 ctxt->valid = 1;
2311 ctxt->loadsubset = xmlLoadExtDtdDefaultValue;
2312 ctxt->validate = xmlDoValidityCheckingDefaultValue;
2313 ctxt->pedantic = xmlPedanticParserDefaultValue;
Daniel Veillarda53c6882001-07-25 17:18:57 +00002314 ctxt->linenumbers = xmlLineNumbersDefaultValue;
Owen Taylor3473f882001-02-23 17:55:21 +00002315 ctxt->keepBlanks = xmlKeepBlanksDefaultValue;
Daniel Veillard16698282001-09-14 10:29:27 +00002316 if (ctxt->keepBlanks == 0)
William M. Brack8b2c7f12002-11-22 05:07:29 +00002317 ctxt->sax->ignorableWhitespace = ignorableWhitespace;
Daniel Veillard16698282001-09-14 10:29:27 +00002318
Owen Taylor3473f882001-02-23 17:55:21 +00002319 ctxt->vctxt.userData = ctxt;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00002320 ctxt->vctxt.error = xmlParserValidityError;
2321 ctxt->vctxt.warning = xmlParserValidityWarning;
Owen Taylor3473f882001-02-23 17:55:21 +00002322 if (ctxt->validate) {
Owen Taylor3473f882001-02-23 17:55:21 +00002323 if (xmlGetWarningsDefaultValue == 0)
2324 ctxt->vctxt.warning = NULL;
2325 else
2326 ctxt->vctxt.warning = xmlParserValidityWarning;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00002327 ctxt->vctxt.nodeMax = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002328 }
2329 ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue;
2330 ctxt->record_info = 0;
2331 ctxt->nbChars = 0;
2332 ctxt->checkIndex = 0;
2333 ctxt->inSubset = 0;
2334 ctxt->errNo = XML_ERR_OK;
2335 ctxt->depth = 0;
2336 ctxt->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00002337 ctxt->catalogs = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002338 xmlInitNodeInfoSeq(&ctxt->node_seq);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002339 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002340}
2341
2342/**
2343 * xmlFreeParserCtxt:
2344 * @ctxt: an XML parser context
2345 *
2346 * Free all the memory used by a parser context. However the parsed
2347 * document in ctxt->myDoc is not freed.
2348 */
2349
2350void
2351xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)
2352{
2353 xmlParserInputPtr input;
Owen Taylor3473f882001-02-23 17:55:21 +00002354
2355 if (ctxt == NULL) return;
2356
2357 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
2358 xmlFreeInputStream(input);
2359 }
Owen Taylor3473f882001-02-23 17:55:21 +00002360 if (ctxt->spaceTab != NULL) xmlFree(ctxt->spaceTab);
2361 if (ctxt->nameTab != NULL) xmlFree(ctxt->nameTab);
2362 if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab);
2363 if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab);
2364 if (ctxt->version != NULL) xmlFree((char *) ctxt->version);
2365 if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00002366 if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI);
2367 if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem);
Owen Taylor3473f882001-02-23 17:55:21 +00002368 if ((ctxt->sax != NULL) && (ctxt->sax != &xmlDefaultSAXHandler))
2369 xmlFree(ctxt->sax);
2370 if (ctxt->directory != NULL) xmlFree((char *) ctxt->directory);
Daniel Veillarda9142e72001-06-19 11:07:54 +00002371 if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002372 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00002373#ifdef LIBXML_CATALOG_ENABLED
2374 if (ctxt->catalogs != NULL)
2375 xmlCatalogFreeLocal(ctxt->catalogs);
2376#endif
Owen Taylor3473f882001-02-23 17:55:21 +00002377 xmlFree(ctxt);
2378}
2379
2380/**
2381 * xmlNewParserCtxt:
2382 *
2383 * Allocate and initialize a new parser context.
2384 *
2385 * Returns the xmlParserCtxtPtr or NULL
2386 */
2387
2388xmlParserCtxtPtr
2389xmlNewParserCtxt()
2390{
2391 xmlParserCtxtPtr ctxt;
2392
2393 ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
2394 if (ctxt == NULL) {
2395 xmlGenericError(xmlGenericErrorContext,
2396 "xmlNewParserCtxt : cannot allocate context\n");
Daniel Veillard3487c8d2002-09-05 11:33:25 +00002397 xmlGenericError(xmlGenericErrorContext, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002398 return(NULL);
2399 }
2400 memset(ctxt, 0, sizeof(xmlParserCtxt));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002401 if (xmlInitParserCtxt(ctxt) < 0) {
2402 xmlFreeParserCtxt(ctxt);
2403 return(NULL);
2404 }
Owen Taylor3473f882001-02-23 17:55:21 +00002405 return(ctxt);
2406}
2407
2408/************************************************************************
2409 * *
2410 * Handling of node informations *
2411 * *
2412 ************************************************************************/
2413
2414/**
2415 * xmlClearParserCtxt:
2416 * @ctxt: an XML parser context
2417 *
2418 * Clear (release owned resources) and reinitialize a parser context
2419 */
2420
2421void
2422xmlClearParserCtxt(xmlParserCtxtPtr ctxt)
2423{
Daniel Veillard5d96fff2001-08-31 14:55:30 +00002424 if (ctxt==NULL)
2425 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002426 xmlClearNodeInfoSeq(&ctxt->node_seq);
2427 xmlInitParserCtxt(ctxt);
2428}
2429
2430/**
2431 * xmlParserFindNodeInfo:
Daniel Veillard01c13b52002-12-10 15:19:08 +00002432 * @ctx: an XML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00002433 * @node: an XML node within the tree
2434 *
2435 * Find the parser node info struct for a given node
2436 *
2437 * Returns an xmlParserNodeInfo block pointer or NULL
2438 */
Daniel Veillard963d2ae2002-01-20 22:08:18 +00002439const xmlParserNodeInfo* xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx,
2440 const xmlNodePtr node)
Owen Taylor3473f882001-02-23 17:55:21 +00002441{
2442 unsigned long pos;
2443
2444 /* Find position where node should be at */
2445 pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node);
Daniel Veillardb1d62872001-09-21 09:47:08 +00002446 if (pos < ctx->node_seq.length && ctx->node_seq.buffer[pos].node == node)
Owen Taylor3473f882001-02-23 17:55:21 +00002447 return &ctx->node_seq.buffer[pos];
2448 else
2449 return NULL;
2450}
2451
2452
2453/**
2454 * xmlInitNodeInfoSeq:
2455 * @seq: a node info sequence pointer
2456 *
2457 * -- Initialize (set to initial state) node info sequence
2458 */
2459void
2460xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
2461{
2462 seq->length = 0;
2463 seq->maximum = 0;
2464 seq->buffer = NULL;
2465}
2466
2467/**
2468 * xmlClearNodeInfoSeq:
2469 * @seq: a node info sequence pointer
2470 *
2471 * -- Clear (release memory and reinitialize) node
2472 * info sequence
2473 */
2474void
2475xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
2476{
2477 if ( seq->buffer != NULL )
2478 xmlFree(seq->buffer);
2479 xmlInitNodeInfoSeq(seq);
2480}
2481
2482
2483/**
2484 * xmlParserFindNodeInfoIndex:
2485 * @seq: a node info sequence pointer
2486 * @node: an XML node pointer
2487 *
2488 *
2489 * xmlParserFindNodeInfoIndex : Find the index that the info record for
2490 * the given node is or should be at in a sorted sequence
2491 *
2492 * Returns a long indicating the position of the record
2493 */
Daniel Veillard963d2ae2002-01-20 22:08:18 +00002494unsigned long xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq,
2495 const xmlNodePtr node)
Owen Taylor3473f882001-02-23 17:55:21 +00002496{
2497 unsigned long upper, lower, middle;
2498 int found = 0;
2499
2500 /* Do a binary search for the key */
2501 lower = 1;
2502 upper = seq->length;
2503 middle = 0;
2504 while ( lower <= upper && !found) {
2505 middle = lower + (upper - lower) / 2;
2506 if ( node == seq->buffer[middle - 1].node )
2507 found = 1;
2508 else if ( node < seq->buffer[middle - 1].node )
2509 upper = middle - 1;
2510 else
2511 lower = middle + 1;
2512 }
2513
2514 /* Return position */
2515 if ( middle == 0 || seq->buffer[middle - 1].node < node )
2516 return middle;
2517 else
2518 return middle - 1;
2519}
2520
2521
2522/**
2523 * xmlParserAddNodeInfo:
2524 * @ctxt: an XML parser context
2525 * @info: a node info sequence pointer
2526 *
2527 * Insert node info record into the sorted sequence
2528 */
2529void
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002530xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt,
Daniel Veillard963d2ae2002-01-20 22:08:18 +00002531 const xmlParserNodeInfoPtr info)
Owen Taylor3473f882001-02-23 17:55:21 +00002532{
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002533 unsigned long pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002534
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002535 /* Find pos and check to see if node is already in the sequence */
William M. Brack78637da2003-07-31 14:47:38 +00002536 pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr)
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002537 info->node);
2538 if (pos < ctxt->node_seq.length
2539 && ctxt->node_seq.buffer[pos].node == info->node) {
2540 ctxt->node_seq.buffer[pos] = *info;
Owen Taylor3473f882001-02-23 17:55:21 +00002541 }
2542
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002543 /* Otherwise, we need to add new node to buffer */
2544 else {
2545 if (ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) {
2546 xmlParserNodeInfo *tmp_buffer;
2547 unsigned int byte_size;
Owen Taylor3473f882001-02-23 17:55:21 +00002548
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002549 if (ctxt->node_seq.maximum == 0)
2550 ctxt->node_seq.maximum = 2;
2551 byte_size = (sizeof(*ctxt->node_seq.buffer) *
2552 (2 * ctxt->node_seq.maximum));
2553
2554 if (ctxt->node_seq.buffer == NULL)
Daniel Veillardc4f65ab2003-04-21 23:07:45 +00002555 tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size);
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002556 else
2557 tmp_buffer =
2558 (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer,
2559 byte_size);
2560
2561 if (tmp_buffer == NULL) {
2562 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2563 ctxt->sax->error(ctxt->userData, "Out of memory\n");
2564 ctxt->errNo = XML_ERR_NO_MEMORY;
2565 return;
2566 }
2567 ctxt->node_seq.buffer = tmp_buffer;
2568 ctxt->node_seq.maximum *= 2;
2569 }
2570
2571 /* If position is not at end, move elements out of the way */
2572 if (pos != ctxt->node_seq.length) {
2573 unsigned long i;
2574
2575 for (i = ctxt->node_seq.length; i > pos; i--)
2576 ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1];
2577 }
2578
2579 /* Copy element and increase length */
2580 ctxt->node_seq.buffer[pos] = *info;
2581 ctxt->node_seq.length++;
Owen Taylor3473f882001-02-23 17:55:21 +00002582 }
Owen Taylor3473f882001-02-23 17:55:21 +00002583}
2584
2585/************************************************************************
2586 * *
Daniel Veillarda53c6882001-07-25 17:18:57 +00002587 * Defaults settings *
2588 * *
2589 ************************************************************************/
2590/**
2591 * xmlPedanticParserDefault:
2592 * @val: int 0 or 1
2593 *
2594 * Set and return the previous value for enabling pedantic warnings.
2595 *
2596 * Returns the last value for 0 for no substitution, 1 for substitution.
2597 */
2598
2599int
2600xmlPedanticParserDefault(int val) {
2601 int old = xmlPedanticParserDefaultValue;
2602
2603 xmlPedanticParserDefaultValue = val;
2604 return(old);
2605}
2606
2607/**
2608 * xmlLineNumbersDefault:
2609 * @val: int 0 or 1
2610 *
2611 * Set and return the previous value for enabling line numbers in elements
2612 * contents. This may break on old application and is turned off by default.
2613 *
2614 * Returns the last value for 0 for no substitution, 1 for substitution.
2615 */
2616
2617int
2618xmlLineNumbersDefault(int val) {
2619 int old = xmlLineNumbersDefaultValue;
2620
2621 xmlLineNumbersDefaultValue = val;
2622 return(old);
2623}
2624
2625/**
2626 * xmlSubstituteEntitiesDefault:
2627 * @val: int 0 or 1
2628 *
2629 * Set and return the previous value for default entity support.
2630 * Initially the parser always keep entity references instead of substituting
2631 * entity values in the output. This function has to be used to change the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002632 * default parser behavior
2633 * SAX::substituteEntities() has to be used for changing that on a file by
Daniel Veillarda53c6882001-07-25 17:18:57 +00002634 * file basis.
2635 *
2636 * Returns the last value for 0 for no substitution, 1 for substitution.
2637 */
2638
2639int
2640xmlSubstituteEntitiesDefault(int val) {
2641 int old = xmlSubstituteEntitiesDefaultValue;
2642
2643 xmlSubstituteEntitiesDefaultValue = val;
2644 return(old);
2645}
2646
2647/**
2648 * xmlKeepBlanksDefault:
2649 * @val: int 0 or 1
2650 *
2651 * Set and return the previous value for default blanks text nodes support.
2652 * The 1.x version of the parser used an heuristic to try to detect
2653 * ignorable white spaces. As a result the SAX callback was generating
2654 * ignorableWhitespace() callbacks instead of characters() one, and when
2655 * using the DOM output text nodes containing those blanks were not generated.
2656 * The 2.x and later version will switch to the XML standard way and
2657 * ignorableWhitespace() are only generated when running the parser in
2658 * validating mode and when the current element doesn't allow CDATA or
2659 * mixed content.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002660 * This function is provided as a way to force the standard behavior
Daniel Veillarda53c6882001-07-25 17:18:57 +00002661 * on 1.X libs and to switch back to the old mode for compatibility when
2662 * running 1.X client code on 2.X . Upgrade of 1.X code should be done
2663 * by using xmlIsBlankNode() commodity function to detect the "empty"
2664 * nodes generated.
2665 * This value also affect autogeneration of indentation when saving code
2666 * if blanks sections are kept, indentation is not generated.
2667 *
2668 * Returns the last value for 0 for no substitution, 1 for substitution.
2669 */
2670
2671int
2672xmlKeepBlanksDefault(int val) {
2673 int old = xmlKeepBlanksDefaultValue;
2674
2675 xmlKeepBlanksDefaultValue = val;
2676 xmlIndentTreeOutput = !val;
2677 return(old);
2678}
2679
2680/************************************************************************
2681 * *
Owen Taylor3473f882001-02-23 17:55:21 +00002682 * Deprecated functions kept for compatibility *
2683 * *
2684 ************************************************************************/
2685
Daniel Veillard5e2dace2001-07-18 19:30:27 +00002686/**
2687 * xmlCheckLanguageID:
Owen Taylor3473f882001-02-23 17:55:21 +00002688 * @lang: pointer to the string value
2689 *
2690 * Checks that the value conforms to the LanguageID production:
2691 *
2692 * NOTE: this is somewhat deprecated, those productions were removed from
2693 * the XML Second edition.
2694 *
2695 * [33] LanguageID ::= Langcode ('-' Subcode)*
2696 * [34] Langcode ::= ISO639Code | IanaCode | UserCode
2697 * [35] ISO639Code ::= ([a-z] | [A-Z]) ([a-z] | [A-Z])
2698 * [36] IanaCode ::= ('i' | 'I') '-' ([a-z] | [A-Z])+
2699 * [37] UserCode ::= ('x' | 'X') '-' ([a-z] | [A-Z])+
2700 * [38] Subcode ::= ([a-z] | [A-Z])+
2701 *
2702 * Returns 1 if correct 0 otherwise
2703 **/
2704int
2705xmlCheckLanguageID(const xmlChar *lang) {
2706 const xmlChar *cur = lang;
2707
2708 if (cur == NULL)
2709 return(0);
2710 if (((cur[0] == 'i') && (cur[1] == '-')) ||
2711 ((cur[0] == 'I') && (cur[1] == '-'))) {
2712 /*
2713 * IANA code
2714 */
2715 cur += 2;
2716 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
2717 ((cur[0] >= 'a') && (cur[0] <= 'z')))
2718 cur++;
2719 } else if (((cur[0] == 'x') && (cur[1] == '-')) ||
2720 ((cur[0] == 'X') && (cur[1] == '-'))) {
2721 /*
2722 * User code
2723 */
2724 cur += 2;
2725 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
2726 ((cur[0] >= 'a') && (cur[0] <= 'z')))
2727 cur++;
2728 } else if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
2729 ((cur[0] >= 'a') && (cur[0] <= 'z'))) {
2730 /*
2731 * ISO639
2732 */
2733 cur++;
2734 if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
2735 ((cur[0] >= 'a') && (cur[0] <= 'z')))
2736 cur++;
2737 else
2738 return(0);
2739 } else
2740 return(0);
2741 while (cur[0] != 0) { /* non input consuming */
2742 if (cur[0] != '-')
2743 return(0);
2744 cur++;
2745 if (((cur[0] >= 'A') && (cur[0] <= 'Z')) ||
2746 ((cur[0] >= 'a') && (cur[0] <= 'z')))
2747 cur++;
2748 else
2749 return(0);
2750 while (((cur[0] >= 'A') && (cur[0] <= 'Z')) || /* non input consuming */
2751 ((cur[0] >= 'a') && (cur[0] <= 'z')))
2752 cur++;
2753 }
2754 return(1);
2755}
2756
2757/**
2758 * xmlDecodeEntities:
2759 * @ctxt: the parser context
Owen Taylor3473f882001-02-23 17:55:21 +00002760 * @len: the len to decode (in bytes !), -1 for no size limit
Daniel Veillarda9b66d02002-12-11 14:23:49 +00002761 * @what: combination of XML_SUBSTITUTE_REF and XML_SUBSTITUTE_PEREF
Owen Taylor3473f882001-02-23 17:55:21 +00002762 * @end: an end marker xmlChar, 0 if none
2763 * @end2: an end marker xmlChar, 0 if none
2764 * @end3: an end marker xmlChar, 0 if none
2765 *
2766 * This function is deprecated, we now always process entities content
2767 * through xmlStringDecodeEntities
2768 *
2769 * TODO: remove it in next major release.
2770 *
2771 * [67] Reference ::= EntityRef | CharRef
2772 *
2773 * [69] PEReference ::= '%' Name ';'
2774 *
2775 * Returns A newly allocated string with the substitution done. The caller
2776 * must deallocate it !
2777 */
2778xmlChar *
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002779xmlDecodeEntities(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED, int what ATTRIBUTE_UNUSED,
2780 xmlChar end ATTRIBUTE_UNUSED, xmlChar end2 ATTRIBUTE_UNUSED, xmlChar end3 ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00002781#if 0
2782 xmlChar *buffer = NULL;
2783 unsigned int buffer_size = 0;
2784 unsigned int nbchars = 0;
2785
2786 xmlChar *current = NULL;
2787 xmlEntityPtr ent;
2788 unsigned int max = (unsigned int) len;
2789 int c,l;
2790#endif
2791
2792 static int deprecated = 0;
2793 if (!deprecated) {
2794 xmlGenericError(xmlGenericErrorContext,
2795 "xmlDecodeEntities() deprecated function reached\n");
2796 deprecated = 1;
2797 }
2798
2799#if 0
2800 if (ctxt->depth > 40) {
2801 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2802 ctxt->sax->error(ctxt->userData,
2803 "Detected entity reference loop\n");
2804 ctxt->wellFormed = 0;
Daniel Veillarddad3f682002-11-17 16:47:27 +00002805 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002806 ctxt->errNo = XML_ERR_ENTITY_LOOP;
2807 return(NULL);
2808 }
2809
2810 /*
2811 * allocate a translation buffer.
2812 */
2813 buffer_size = XML_PARSER_BIG_BUFFER_SIZE;
2814 buffer = (xmlChar *) xmlMalloc(buffer_size * sizeof(xmlChar));
2815 if (buffer == NULL) {
Daniel Veillard3487c8d2002-09-05 11:33:25 +00002816 xmlGenericError(xmlGenericErrorContext,
2817 "xmlDecodeEntities: malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002818 return(NULL);
2819 }
2820
2821 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002822 * OK loop until we reach one of the ending char or a size limit.
Owen Taylor3473f882001-02-23 17:55:21 +00002823 */
2824 GROW;
2825 c = CUR_CHAR(l);
2826 while ((nbchars < max) && (c != end) && /* NOTUSED */
2827 (c != end2) && (c != end3)) {
2828 GROW;
2829 if (c == 0) break;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002830 if ((c == '&') && (NXT(1) == '#')) {
Owen Taylor3473f882001-02-23 17:55:21 +00002831 int val = xmlParseCharRef(ctxt);
2832 COPY_BUF(0,buffer,nbchars,val);
2833 NEXTL(l);
Daniel Veillardfdc91562002-07-01 21:52:03 +00002834 } else if (c == '&') &&
Owen Taylor3473f882001-02-23 17:55:21 +00002835 (what & XML_SUBSTITUTE_REF)) {
2836 if (xmlParserDebugEntities)
2837 xmlGenericError(xmlGenericErrorContext,
2838 "decoding Entity Reference\n");
2839 ent = xmlParseEntityRef(ctxt);
2840 if ((ent != NULL) &&
2841 (ctxt->replaceEntities != 0)) {
2842 current = ent->content;
2843 while (*current != 0) { /* non input consuming loop */
2844 buffer[nbchars++] = *current++;
2845 if (nbchars > buffer_size - XML_PARSER_BUFFER_SIZE) {
2846 growBuffer(buffer);
2847 }
2848 }
2849 } else if (ent != NULL) {
2850 const xmlChar *cur = ent->name;
2851
2852 buffer[nbchars++] = '&';
2853 if (nbchars > buffer_size - XML_PARSER_BUFFER_SIZE) {
2854 growBuffer(buffer);
2855 }
2856 while (*cur != 0) { /* non input consuming loop */
2857 buffer[nbchars++] = *cur++;
2858 }
2859 buffer[nbchars++] = ';';
2860 }
2861 } else if (c == '%' && (what & XML_SUBSTITUTE_PEREF)) {
2862 /*
2863 * a PEReference induce to switch the entity flow,
2864 * we break here to flush the current set of chars
2865 * parsed if any. We will be called back later.
2866 */
2867 if (xmlParserDebugEntities)
2868 xmlGenericError(xmlGenericErrorContext,
2869 "decoding PE Reference\n");
2870 if (nbchars != 0) break;
2871
2872 xmlParsePEReference(ctxt);
2873
2874 /*
2875 * Pop-up of finished entities.
2876 */
2877 while ((RAW == 0) && (ctxt->inputNr > 1)) /* non input consuming */
2878 xmlPopInput(ctxt);
2879
2880 break;
2881 } else {
2882 COPY_BUF(l,buffer,nbchars,c);
2883 NEXTL(l);
2884 if (nbchars > buffer_size - XML_PARSER_BUFFER_SIZE) {
2885 growBuffer(buffer);
2886 }
2887 }
2888 c = CUR_CHAR(l);
2889 }
2890 buffer[nbchars++] = 0;
2891 return(buffer);
2892#endif
2893 return(NULL);
2894}
2895
2896/**
2897 * xmlNamespaceParseNCName:
2898 * @ctxt: an XML parser context
2899 *
2900 * parse an XML namespace name.
2901 *
2902 * TODO: this seems not in use anymore, the namespace handling is done on
2903 * top of the SAX interfaces, i.e. not on raw input.
2904 *
2905 * [NS 3] NCName ::= (Letter | '_') (NCNameChar)*
2906 *
2907 * [NS 4] NCNameChar ::= Letter | Digit | '.' | '-' | '_' |
2908 * CombiningChar | Extender
2909 *
2910 * Returns the namespace name or NULL
2911 */
2912
2913xmlChar *
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002914xmlNamespaceParseNCName(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00002915#if 0
2916 xmlChar buf[XML_MAX_NAMELEN + 5];
2917 int len = 0, l;
2918 int cur = CUR_CHAR(l);
2919#endif
2920
2921 static int deprecated = 0;
2922 if (!deprecated) {
2923 xmlGenericError(xmlGenericErrorContext,
2924 "xmlNamespaceParseNCName() deprecated function reached\n");
2925 deprecated = 1;
2926 }
2927
2928#if 0
2929 /* load first the value of the char !!! */
2930 GROW;
2931 if (!IS_LETTER(cur) && (cur != '_')) return(NULL);
2932
2933xmlGenericError(xmlGenericErrorContext,
2934 "xmlNamespaceParseNCName: reached loop 3\n");
2935 while ((IS_LETTER(cur)) || (IS_DIGIT(cur)) || /* NOT REACHED */
2936 (cur == '.') || (cur == '-') ||
2937 (cur == '_') ||
2938 (IS_COMBINING(cur)) ||
2939 (IS_EXTENDER(cur))) {
2940 COPY_BUF(l,buf,len,cur);
2941 NEXTL(l);
2942 cur = CUR_CHAR(l);
2943 if (len >= XML_MAX_NAMELEN) {
2944 xmlGenericError(xmlGenericErrorContext,
2945 "xmlNamespaceParseNCName: reached XML_MAX_NAMELEN limit\n");
2946 while ((IS_LETTER(cur)) || (IS_DIGIT(cur)) ||/* NOT REACHED */
2947 (cur == '.') || (cur == '-') ||
2948 (cur == '_') ||
2949 (IS_COMBINING(cur)) ||
2950 (IS_EXTENDER(cur))) {
2951 NEXTL(l);
2952 cur = CUR_CHAR(l);
2953 }
2954 break;
2955 }
2956 }
2957 return(xmlStrndup(buf, len));
2958#endif
2959 return(NULL);
2960}
2961
2962/**
2963 * xmlNamespaceParseQName:
2964 * @ctxt: an XML parser context
2965 * @prefix: a xmlChar **
2966 *
2967 * TODO: this seems not in use anymore, the namespace handling is done on
2968 * top of the SAX interfaces, i.e. not on raw input.
2969 *
2970 * parse an XML qualified name
2971 *
2972 * [NS 5] QName ::= (Prefix ':')? LocalPart
2973 *
2974 * [NS 6] Prefix ::= NCName
2975 *
2976 * [NS 7] LocalPart ::= NCName
2977 *
2978 * Returns the local part, and prefix is updated
2979 * to get the Prefix if any.
2980 */
2981
2982xmlChar *
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00002983xmlNamespaceParseQName(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlChar **prefix ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00002984
2985 static int deprecated = 0;
2986 if (!deprecated) {
2987 xmlGenericError(xmlGenericErrorContext,
2988 "xmlNamespaceParseQName() deprecated function reached\n");
2989 deprecated = 1;
2990 }
2991
2992#if 0
2993 xmlChar *ret = NULL;
2994
2995 *prefix = NULL;
2996 ret = xmlNamespaceParseNCName(ctxt);
2997 if (RAW == ':') {
2998 *prefix = ret;
2999 NEXT;
3000 ret = xmlNamespaceParseNCName(ctxt);
3001 }
3002
3003 return(ret);
3004#endif
3005 return(NULL);
3006}
3007
3008/**
3009 * xmlNamespaceParseNSDef:
3010 * @ctxt: an XML parser context
3011 *
3012 * parse a namespace prefix declaration
3013 *
3014 * TODO: this seems not in use anymore, the namespace handling is done on
3015 * top of the SAX interfaces, i.e. not on raw input.
3016 *
3017 * [NS 1] NSDef ::= PrefixDef Eq SystemLiteral
3018 *
3019 * [NS 2] PrefixDef ::= 'xmlns' (':' NCName)?
3020 *
3021 * Returns the namespace name
3022 */
3023
3024xmlChar *
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003025xmlNamespaceParseNSDef(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003026 static int deprecated = 0;
3027 if (!deprecated) {
3028 xmlGenericError(xmlGenericErrorContext,
3029 "xmlNamespaceParseNSDef() deprecated function reached\n");
3030 deprecated = 1;
3031 }
3032 return(NULL);
3033#if 0
3034 xmlChar *name = NULL;
3035
3036 if ((RAW == 'x') && (NXT(1) == 'm') &&
3037 (NXT(2) == 'l') && (NXT(3) == 'n') &&
3038 (NXT(4) == 's')) {
3039 SKIP(5);
3040 if (RAW == ':') {
3041 NEXT;
3042 name = xmlNamespaceParseNCName(ctxt);
3043 }
3044 }
3045 return(name);
3046#endif
3047}
3048
3049/**
3050 * xmlParseQuotedString:
3051 * @ctxt: an XML parser context
3052 *
3053 * Parse and return a string between quotes or doublequotes
3054 *
3055 * TODO: Deprecated, to be removed at next drop of binary compatibility
3056 *
3057 * Returns the string parser or NULL.
3058 */
3059xmlChar *
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003060xmlParseQuotedString(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003061 static int deprecated = 0;
3062 if (!deprecated) {
3063 xmlGenericError(xmlGenericErrorContext,
3064 "xmlParseQuotedString() deprecated function reached\n");
3065 deprecated = 1;
3066 }
3067 return(NULL);
3068
3069#if 0
3070 xmlChar *buf = NULL;
3071 int len = 0,l;
3072 int size = XML_PARSER_BUFFER_SIZE;
3073 int c;
3074
3075 buf = (xmlChar *) xmlMalloc(size * sizeof(xmlChar));
3076 if (buf == NULL) {
3077 xmlGenericError(xmlGenericErrorContext,
3078 "malloc of %d byte failed\n", size);
3079 return(NULL);
3080 }
3081xmlGenericError(xmlGenericErrorContext,
3082 "xmlParseQuotedString: reached loop 4\n");
3083 if (RAW == '"') {
3084 NEXT;
3085 c = CUR_CHAR(l);
3086 while (IS_CHAR(c) && (c != '"')) { /* NOTUSED */
3087 if (len + 5 >= size) {
3088 size *= 2;
3089 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3090 if (buf == NULL) {
3091 xmlGenericError(xmlGenericErrorContext,
3092 "realloc of %d byte failed\n", size);
3093 return(NULL);
3094 }
3095 }
3096 COPY_BUF(l,buf,len,c);
3097 NEXTL(l);
3098 c = CUR_CHAR(l);
3099 }
3100 if (c != '"') {
3101 ctxt->errNo = XML_ERR_STRING_NOT_CLOSED;
3102 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3103 ctxt->sax->error(ctxt->userData,
3104 "String not closed \"%.50s\"\n", buf);
3105 ctxt->wellFormed = 0;
Daniel Veillarddad3f682002-11-17 16:47:27 +00003106 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00003107 } else {
3108 NEXT;
3109 }
3110 } else if (RAW == '\''){
3111 NEXT;
3112 c = CUR;
3113 while (IS_CHAR(c) && (c != '\'')) { /* NOTUSED */
3114 if (len + 1 >= size) {
3115 size *= 2;
3116 buf = (xmlChar *) xmlRealloc(buf, size * sizeof(xmlChar));
3117 if (buf == NULL) {
3118 xmlGenericError(xmlGenericErrorContext,
3119 "realloc of %d byte failed\n", size);
3120 return(NULL);
3121 }
3122 }
3123 buf[len++] = c;
3124 NEXT;
3125 c = CUR;
3126 }
3127 if (RAW != '\'') {
3128 ctxt->errNo = XML_ERR_STRING_NOT_CLOSED;
3129 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3130 ctxt->sax->error(ctxt->userData,
3131 "String not closed \"%.50s\"\n", buf);
3132 ctxt->wellFormed = 0;
Daniel Veillarddad3f682002-11-17 16:47:27 +00003133 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00003134 } else {
3135 NEXT;
3136 }
3137 }
3138 return(buf);
3139#endif
3140}
3141
3142/**
3143 * xmlParseNamespace:
3144 * @ctxt: an XML parser context
3145 *
3146 * xmlParseNamespace: parse specific PI '<?namespace ...' constructs.
3147 *
3148 * This is what the older xml-name Working Draft specified, a bunch of
3149 * other stuff may still rely on it, so support is still here as
3150 * if it was declared on the root of the Tree:-(
3151 *
3152 * TODO: remove from library
3153 *
3154 * To be removed at next drop of binary compatibility
3155 */
3156
3157void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003158xmlParseNamespace(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003159 static int deprecated = 0;
3160 if (!deprecated) {
3161 xmlGenericError(xmlGenericErrorContext,
3162 "xmlParseNamespace() deprecated function reached\n");
3163 deprecated = 1;
3164 }
3165
3166#if 0
3167 xmlChar *href = NULL;
3168 xmlChar *prefix = NULL;
3169 int garbage = 0;
3170
3171 /*
3172 * We just skipped "namespace" or "xml:namespace"
3173 */
3174 SKIP_BLANKS;
3175
3176xmlGenericError(xmlGenericErrorContext,
3177 "xmlParseNamespace: reached loop 5\n");
3178 while (IS_CHAR(RAW) && (RAW != '>')) { /* NOT REACHED */
3179 /*
3180 * We can have "ns" or "prefix" attributes
3181 * Old encoding as 'href' or 'AS' attributes is still supported
3182 */
3183 if ((RAW == 'n') && (NXT(1) == 's')) {
3184 garbage = 0;
3185 SKIP(2);
3186 SKIP_BLANKS;
3187
3188 if (RAW != '=') continue;
3189 NEXT;
3190 SKIP_BLANKS;
3191
3192 href = xmlParseQuotedString(ctxt);
3193 SKIP_BLANKS;
3194 } else if ((RAW == 'h') && (NXT(1) == 'r') &&
3195 (NXT(2) == 'e') && (NXT(3) == 'f')) {
3196 garbage = 0;
3197 SKIP(4);
3198 SKIP_BLANKS;
3199
3200 if (RAW != '=') continue;
3201 NEXT;
3202 SKIP_BLANKS;
3203
3204 href = xmlParseQuotedString(ctxt);
3205 SKIP_BLANKS;
3206 } else if ((RAW == 'p') && (NXT(1) == 'r') &&
3207 (NXT(2) == 'e') && (NXT(3) == 'f') &&
3208 (NXT(4) == 'i') && (NXT(5) == 'x')) {
3209 garbage = 0;
3210 SKIP(6);
3211 SKIP_BLANKS;
3212
3213 if (RAW != '=') continue;
3214 NEXT;
3215 SKIP_BLANKS;
3216
3217 prefix = xmlParseQuotedString(ctxt);
3218 SKIP_BLANKS;
3219 } else if ((RAW == 'A') && (NXT(1) == 'S')) {
3220 garbage = 0;
3221 SKIP(2);
3222 SKIP_BLANKS;
3223
3224 if (RAW != '=') continue;
3225 NEXT;
3226 SKIP_BLANKS;
3227
3228 prefix = xmlParseQuotedString(ctxt);
3229 SKIP_BLANKS;
3230 } else if ((RAW == '?') && (NXT(1) == '>')) {
3231 garbage = 0;
3232 NEXT;
3233 } else {
3234 /*
3235 * Found garbage when parsing the namespace
3236 */
3237 if (!garbage) {
3238 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3239 ctxt->sax->error(ctxt->userData,
3240 "xmlParseNamespace found garbage\n");
3241 }
3242 ctxt->errNo = XML_ERR_NS_DECL_ERROR;
3243 ctxt->wellFormed = 0;
Daniel Veillarddad3f682002-11-17 16:47:27 +00003244 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00003245 NEXT;
3246 }
3247 }
3248
3249 MOVETO_ENDTAG(CUR_PTR);
3250 NEXT;
3251
3252 /*
3253 * Register the DTD.
3254 if (href != NULL)
3255 if ((ctxt->sax != NULL) && (ctxt->sax->globalNamespace != NULL))
3256 ctxt->sax->globalNamespace(ctxt->userData, href, prefix);
3257 */
3258
3259 if (prefix != NULL) xmlFree(prefix);
3260 if (href != NULL) xmlFree(href);
3261#endif
3262}
3263
3264/**
3265 * xmlScanName:
3266 * @ctxt: an XML parser context
3267 *
3268 * Trickery: parse an XML name but without consuming the input flow
3269 * Needed for rollback cases. Used only when parsing entities references.
3270 *
3271 * TODO: seems deprecated now, only used in the default part of
3272 * xmlParserHandleReference
3273 *
3274 * [4] NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' |
3275 * CombiningChar | Extender
3276 *
3277 * [5] Name ::= (Letter | '_' | ':') (NameChar)*
3278 *
3279 * [6] Names ::= Name (S Name)*
3280 *
3281 * Returns the Name parsed or NULL
3282 */
3283
3284xmlChar *
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003285xmlScanName(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003286 static int deprecated = 0;
3287 if (!deprecated) {
3288 xmlGenericError(xmlGenericErrorContext,
3289 "xmlScanName() deprecated function reached\n");
3290 deprecated = 1;
3291 }
3292 return(NULL);
3293
3294#if 0
3295 xmlChar buf[XML_MAX_NAMELEN];
3296 int len = 0;
3297
3298 GROW;
3299 if (!IS_LETTER(RAW) && (RAW != '_') &&
3300 (RAW != ':')) {
3301 return(NULL);
3302 }
3303
3304
3305 while ((IS_LETTER(NXT(len))) || (IS_DIGIT(NXT(len))) || /* NOT REACHED */
3306 (NXT(len) == '.') || (NXT(len) == '-') ||
3307 (NXT(len) == '_') || (NXT(len) == ':') ||
3308 (IS_COMBINING(NXT(len))) ||
3309 (IS_EXTENDER(NXT(len)))) {
3310 GROW;
3311 buf[len] = NXT(len);
3312 len++;
3313 if (len >= XML_MAX_NAMELEN) {
3314 xmlGenericError(xmlGenericErrorContext,
3315 "xmlScanName: reached XML_MAX_NAMELEN limit\n");
3316 while ((IS_LETTER(NXT(len))) || /* NOT REACHED */
3317 (IS_DIGIT(NXT(len))) ||
3318 (NXT(len) == '.') || (NXT(len) == '-') ||
3319 (NXT(len) == '_') || (NXT(len) == ':') ||
3320 (IS_COMBINING(NXT(len))) ||
3321 (IS_EXTENDER(NXT(len))))
3322 len++;
3323 break;
3324 }
3325 }
3326 return(xmlStrndup(buf, len));
3327#endif
3328}
3329
3330/**
3331 * xmlParserHandleReference:
3332 * @ctxt: the parser context
3333 *
3334 * TODO: Remove, now deprecated ... the test is done directly in the
3335 * content parsing
3336 * routines.
3337 *
3338 * [67] Reference ::= EntityRef | CharRef
3339 *
3340 * [68] EntityRef ::= '&' Name ';'
3341 *
3342 * [ WFC: Entity Declared ]
3343 * the Name given in the entity reference must match that in an entity
3344 * declaration, except that well-formed documents need not declare any
3345 * of the following entities: amp, lt, gt, apos, quot.
3346 *
3347 * [ WFC: Parsed Entity ]
3348 * An entity reference must not contain the name of an unparsed entity
3349 *
3350 * [66] CharRef ::= '&#' [0-9]+ ';' |
3351 * '&#x' [0-9a-fA-F]+ ';'
3352 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003353 * A PEReference may have been detected in the current input stream
Owen Taylor3473f882001-02-23 17:55:21 +00003354 * the handling is done accordingly to
3355 * http://www.w3.org/TR/REC-xml#entproc
3356 */
3357void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003358xmlParserHandleReference(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003359 static int deprecated = 0;
3360 if (!deprecated) {
3361 xmlGenericError(xmlGenericErrorContext,
3362 "xmlParserHandleReference() deprecated function reached\n");
3363 deprecated = 1;
3364 }
3365
Owen Taylor3473f882001-02-23 17:55:21 +00003366 return;
3367}
3368
3369/**
3370 * xmlHandleEntity:
3371 * @ctxt: an XML parser context
3372 * @entity: an XML entity pointer.
3373 *
3374 * Default handling of defined entities, when should we define a new input
3375 * stream ? When do we just handle that as a set of chars ?
3376 *
3377 * OBSOLETE: to be removed at some point.
3378 */
3379
3380void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003381xmlHandleEntity(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED, xmlEntityPtr entity ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003382 static int deprecated = 0;
3383 if (!deprecated) {
3384 xmlGenericError(xmlGenericErrorContext,
3385 "xmlHandleEntity() deprecated function reached\n");
3386 deprecated = 1;
3387 }
3388
3389#if 0
3390 int len;
3391 xmlParserInputPtr input;
3392
3393 if (entity->content == NULL) {
3394 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
3395 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
3396 ctxt->sax->error(ctxt->userData, "xmlHandleEntity %s: content == NULL\n",
3397 entity->name);
3398 ctxt->wellFormed = 0;
Daniel Veillarddad3f682002-11-17 16:47:27 +00003399 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00003400 return;
3401 }
3402 len = xmlStrlen(entity->content);
3403 if (len <= 2) goto handle_as_char;
3404
3405 /*
3406 * Redefine its content as an input stream.
3407 */
3408 input = xmlNewEntityInputStream(ctxt, entity);
3409 xmlPushInput(ctxt, input);
3410 return;
3411
3412handle_as_char:
3413 /*
3414 * Just handle the content as a set of chars.
3415 */
3416 if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
3417 (ctxt->sax->characters != NULL))
3418 ctxt->sax->characters(ctxt->userData, entity->content, len);
3419#endif
3420}
3421
3422/**
3423 * xmlNewGlobalNs:
3424 * @doc: the document carrying the namespace
3425 * @href: the URI associated
3426 * @prefix: the prefix for the namespace
3427 *
3428 * Creation of a Namespace, the old way using PI and without scoping
3429 * DEPRECATED !!!
3430 * It now create a namespace on the root element of the document if found.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003431 * Returns NULL this functionality had been removed
Owen Taylor3473f882001-02-23 17:55:21 +00003432 */
3433xmlNsPtr
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003434xmlNewGlobalNs(xmlDocPtr doc ATTRIBUTE_UNUSED, const xmlChar *href ATTRIBUTE_UNUSED,
3435 const xmlChar *prefix ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003436 static int deprecated = 0;
3437 if (!deprecated) {
3438 xmlGenericError(xmlGenericErrorContext,
3439 "xmlNewGlobalNs() deprecated function reached\n");
3440 deprecated = 1;
3441 }
3442 return(NULL);
3443#if 0
3444 xmlNodePtr root;
3445
3446 xmlNsPtr cur;
3447
3448 root = xmlDocGetRootElement(doc);
3449 if (root != NULL)
3450 return(xmlNewNs(root, href, prefix));
3451
3452 /*
3453 * if there is no root element yet, create an old Namespace type
3454 * and it will be moved to the root at save time.
3455 */
3456 cur = (xmlNsPtr) xmlMalloc(sizeof(xmlNs));
3457 if (cur == NULL) {
3458 xmlGenericError(xmlGenericErrorContext,
3459 "xmlNewGlobalNs : malloc failed\n");
3460 return(NULL);
3461 }
3462 memset(cur, 0, sizeof(xmlNs));
3463 cur->type = XML_GLOBAL_NAMESPACE;
3464
3465 if (href != NULL)
3466 cur->href = xmlStrdup(href);
3467 if (prefix != NULL)
3468 cur->prefix = xmlStrdup(prefix);
3469
3470 /*
3471 * Add it at the end to preserve parsing order ...
3472 */
3473 if (doc != NULL) {
3474 if (doc->oldNs == NULL) {
3475 doc->oldNs = cur;
3476 } else {
3477 xmlNsPtr prev = doc->oldNs;
3478
3479 while (prev->next != NULL) prev = prev->next;
3480 prev->next = cur;
3481 }
3482 }
3483
3484 return(NULL);
3485#endif
3486}
3487
3488/**
3489 * xmlUpgradeOldNs:
3490 * @doc: a document pointer
3491 *
3492 * Upgrade old style Namespaces (PI) and move them to the root of the document.
3493 * DEPRECATED
3494 */
3495void
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00003496xmlUpgradeOldNs(xmlDocPtr doc ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00003497 static int deprecated = 0;
3498 if (!deprecated) {
3499 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00003500 "xmlUpgradeOldNs() deprecated function reached\n");
Owen Taylor3473f882001-02-23 17:55:21 +00003501 deprecated = 1;
3502 }
3503#if 0
3504 xmlNsPtr cur;
3505
3506 if ((doc == NULL) || (doc->oldNs == NULL)) return;
3507 if (doc->children == NULL) {
3508#ifdef DEBUG_TREE
3509 xmlGenericError(xmlGenericErrorContext,
3510 "xmlUpgradeOldNs: failed no root !\n");
3511#endif
3512 return;
3513 }
3514
3515 cur = doc->oldNs;
3516 while (cur->next != NULL) {
3517 cur->type = XML_LOCAL_NAMESPACE;
3518 cur = cur->next;
3519 }
3520 cur->type = XML_LOCAL_NAMESPACE;
3521 cur->next = doc->children->nsDef;
3522 doc->children->nsDef = doc->oldNs;
3523 doc->oldNs = NULL;
3524#endif
3525}
3526