blob: 8966d8adf9a11514c9a8c11d04fd388ec168a252 [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 Veillarda53c6882001-07-25 17:18:57 +000057/*
58 * Various global defaults for parsing
59 */
Owen Taylor3473f882001-02-23 17:55:21 +000060
Daniel Veillard5e2dace2001-07-18 19:30:27 +000061/**
Owen Taylor3473f882001-02-23 17:55:21 +000062 * xmlCheckVersion:
63 * @version: the include version number
64 *
65 * check the compiled lib version against the include one.
66 * This can warn or immediately kill the application
67 */
68void
69xmlCheckVersion(int version) {
70 int myversion = (int) LIBXML_VERSION;
71
Daniel Veillard6f350292001-10-14 09:56:15 +000072 xmlInitParser();
Daniel Veillard4de4d3b2001-05-07 20:50:47 +000073
Owen Taylor3473f882001-02-23 17:55:21 +000074 if ((myversion / 10000) != (version / 10000)) {
75 xmlGenericError(xmlGenericErrorContext,
76 "Fatal: program compiled against libxml %d using libxml %d\n",
77 (version / 10000), (myversion / 10000));
Daniel Veillardc69e0b12001-11-20 08:35:07 +000078 fprintf(stderr,
79 "Fatal: program compiled against libxml %d using libxml %d\n",
80 (version / 10000), (myversion / 10000));
Owen Taylor3473f882001-02-23 17:55:21 +000081 }
82 if ((myversion / 100) < (version / 100)) {
83 xmlGenericError(xmlGenericErrorContext,
84 "Warning: program compiled against libxml %d using older %d\n",
85 (version / 100), (myversion / 100));
86 }
87}
88
Owen Taylor3473f882001-02-23 17:55:21 +000089/************************************************************************
90 * *
91 * Some functions to avoid too large macros *
92 * *
93 ************************************************************************/
94
95/**
96 * xmlIsChar:
97 * @c: an unicode character (int)
98 *
99 * Check whether the character is allowed by the production
100 * [2] Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD]
101 * | [#x10000-#x10FFFF]
102 * any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
103 * Also available as a macro IS_CHAR()
104 *
105 * Returns 0 if not, non-zero otherwise
106 */
107int
108xmlIsChar(int c) {
109 return(
110 ((c) == 0x09) || ((c) == 0x0A) || ((c) == 0x0D) ||
111 (((c) >= 0x20) && ((c) <= 0xD7FF)) ||
112 (((c) >= 0xE000) && ((c) <= 0xFFFD)) ||
113 (((c) >= 0x10000) && ((c) <= 0x10FFFF)));
114}
115
116/**
117 * xmlIsBlank:
118 * @c: an unicode character (int)
119 *
120 * Check whether the character is allowed by the production
121 * [3] S ::= (#x20 | #x9 | #xD | #xA)+
122 * Also available as a macro IS_BLANK()
123 *
124 * Returns 0 if not, non-zero otherwise
125 */
126int
127xmlIsBlank(int c) {
128 return(((c) == 0x20) || ((c) == 0x09) || ((c) == 0xA) || ((c) == 0x0D));
129}
130
Owen Taylor3473f882001-02-23 17:55:21 +0000131static int xmlBaseArray[] = {
132 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0000 - 0x000F */
133 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0010 - 0x001F */
134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0020 - 0x002F */
135 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0030 - 0x003F */
136 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x0040 - 0x004F */
137 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x0050 - 0x005F */
138 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x0060 - 0x006F */
139 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x0070 - 0x007F */
140 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0080 - 0x008F */
141 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0090 - 0x009F */
142 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00A0 - 0x00AF */
143 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00B0 - 0x00BF */
144 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x00C0 - 0x00CF */
145 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x00D0 - 0x00DF */
146 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x00E0 - 0x00EF */
147 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x00F0 - 0x00FF */
148};
149
Daniel Veillard01c13b52002-12-10 15:19:08 +0000150/**
151 * xmlIsBaseChar:
152 * @c: an unicode character (int)
153 *
154 * Check whether the character is allowed by the production
155 * [85] BaseChar ::= ... long list see REC ...
156 *
157 * VI is your friend !
158 * :1,$ s/\[#x\([0-9A-Z]*\)-#x\([0-9A-Z]*\)\]/ (((c) >= 0x\1) \&\& ((c) <= 0x\2)) ||/
159 * and
160 * :1,$ s/#x\([0-9A-Z]*\)/ ((c) == 0x\1) ||/
161 *
162 * Returns 0 if not, non-zero otherwise
163 */
Owen Taylor3473f882001-02-23 17:55:21 +0000164int
165xmlIsBaseChar(int c) {
Daniel Veillard73b013f2003-09-30 12:36:01 +0000166 if (c < 0x0100) return(xmlBaseArray[c]);
167 return((((c) >= 0x0100) && ((c) <= 0x0131)) ||
Owen Taylor3473f882001-02-23 17:55:21 +0000168 (((c) >= 0x0134) && ((c) <= 0x013E)) ||
169 (((c) >= 0x0141) && ((c) <= 0x0148)) ||
170 (((c) >= 0x014A) && ((c) <= 0x017E)) ||
171 (((c) >= 0x0180) && ((c) <= 0x01C3)) ||
172 (((c) >= 0x01CD) && ((c) <= 0x01F0)) ||
173 (((c) >= 0x01F4) && ((c) <= 0x01F5)) ||
174 (((c) >= 0x01FA) && ((c) <= 0x0217)) ||
175 (((c) >= 0x0250) && ((c) <= 0x02A8)) ||
176 (((c) >= 0x02BB) && ((c) <= 0x02C1)) ||
177 ((c) == 0x0386) ||
178 (((c) >= 0x0388) && ((c) <= 0x038A)) ||
179 ((c) == 0x038C) ||
180 (((c) >= 0x038E) && ((c) <= 0x03A1)) ||
181 (((c) >= 0x03A3) && ((c) <= 0x03CE)) ||
182 (((c) >= 0x03D0) && ((c) <= 0x03D6)) ||
183 ((c) == 0x03DA) ||
184 ((c) == 0x03DC) ||
185 ((c) == 0x03DE) ||
186 ((c) == 0x03E0) ||
187 (((c) >= 0x03E2) && ((c) <= 0x03F3)) ||
188 (((c) >= 0x0401) && ((c) <= 0x040C)) ||
189 (((c) >= 0x040E) && ((c) <= 0x044F)) ||
190 (((c) >= 0x0451) && ((c) <= 0x045C)) ||
191 (((c) >= 0x045E) && ((c) <= 0x0481)) ||
192 (((c) >= 0x0490) && ((c) <= 0x04C4)) ||
193 (((c) >= 0x04C7) && ((c) <= 0x04C8)) ||
194 (((c) >= 0x04CB) && ((c) <= 0x04CC)) ||
195 (((c) >= 0x04D0) && ((c) <= 0x04EB)) ||
196 (((c) >= 0x04EE) && ((c) <= 0x04F5)) ||
197 (((c) >= 0x04F8) && ((c) <= 0x04F9)) ||
198 (((c) >= 0x0531) && ((c) <= 0x0556)) ||
199 ((c) == 0x0559) ||
200 (((c) >= 0x0561) && ((c) <= 0x0586)) ||
201 (((c) >= 0x05D0) && ((c) <= 0x05EA)) ||
202 (((c) >= 0x05F0) && ((c) <= 0x05F2)) ||
203 (((c) >= 0x0621) && ((c) <= 0x063A)) ||
204 (((c) >= 0x0641) && ((c) <= 0x064A)) ||
205 (((c) >= 0x0671) && ((c) <= 0x06B7)) ||
206 (((c) >= 0x06BA) && ((c) <= 0x06BE)) ||
207 (((c) >= 0x06C0) && ((c) <= 0x06CE)) ||
208 (((c) >= 0x06D0) && ((c) <= 0x06D3)) ||
209 ((c) == 0x06D5) ||
210 (((c) >= 0x06E5) && ((c) <= 0x06E6)) ||
211 (((c) >= 0x905) && ( /* accelerator */
212 (((c) >= 0x0905) && ((c) <= 0x0939)) ||
213 ((c) == 0x093D) ||
214 (((c) >= 0x0958) && ((c) <= 0x0961)) ||
215 (((c) >= 0x0985) && ((c) <= 0x098C)) ||
216 (((c) >= 0x098F) && ((c) <= 0x0990)) ||
217 (((c) >= 0x0993) && ((c) <= 0x09A8)) ||
218 (((c) >= 0x09AA) && ((c) <= 0x09B0)) ||
219 ((c) == 0x09B2) ||
220 (((c) >= 0x09B6) && ((c) <= 0x09B9)) ||
221 (((c) >= 0x09DC) && ((c) <= 0x09DD)) ||
222 (((c) >= 0x09DF) && ((c) <= 0x09E1)) ||
223 (((c) >= 0x09F0) && ((c) <= 0x09F1)) ||
224 (((c) >= 0x0A05) && ((c) <= 0x0A0A)) ||
225 (((c) >= 0x0A0F) && ((c) <= 0x0A10)) ||
226 (((c) >= 0x0A13) && ((c) <= 0x0A28)) ||
227 (((c) >= 0x0A2A) && ((c) <= 0x0A30)) ||
228 (((c) >= 0x0A32) && ((c) <= 0x0A33)) ||
229 (((c) >= 0x0A35) && ((c) <= 0x0A36)) ||
230 (((c) >= 0x0A38) && ((c) <= 0x0A39)) ||
231 (((c) >= 0x0A59) && ((c) <= 0x0A5C)) ||
232 ((c) == 0x0A5E) ||
233 (((c) >= 0x0A72) && ((c) <= 0x0A74)) ||
234 (((c) >= 0x0A85) && ((c) <= 0x0A8B)) ||
235 ((c) == 0x0A8D) ||
236 (((c) >= 0x0A8F) && ((c) <= 0x0A91)) ||
237 (((c) >= 0x0A93) && ((c) <= 0x0AA8)) ||
238 (((c) >= 0x0AAA) && ((c) <= 0x0AB0)) ||
239 (((c) >= 0x0AB2) && ((c) <= 0x0AB3)) ||
240 (((c) >= 0x0AB5) && ((c) <= 0x0AB9)) ||
241 ((c) == 0x0ABD) ||
242 ((c) == 0x0AE0) ||
243 (((c) >= 0x0B05) && ((c) <= 0x0B0C)) ||
244 (((c) >= 0x0B0F) && ((c) <= 0x0B10)) ||
245 (((c) >= 0x0B13) && ((c) <= 0x0B28)) ||
246 (((c) >= 0x0B2A) && ((c) <= 0x0B30)) ||
247 (((c) >= 0x0B32) && ((c) <= 0x0B33)) ||
248 (((c) >= 0x0B36) && ((c) <= 0x0B39)) ||
249 ((c) == 0x0B3D) ||
250 (((c) >= 0x0B5C) && ((c) <= 0x0B5D)) ||
251 (((c) >= 0x0B5F) && ((c) <= 0x0B61)) ||
252 (((c) >= 0x0B85) && ((c) <= 0x0B8A)) ||
253 (((c) >= 0x0B8E) && ((c) <= 0x0B90)) ||
254 (((c) >= 0x0B92) && ((c) <= 0x0B95)) ||
255 (((c) >= 0x0B99) && ((c) <= 0x0B9A)) ||
256 ((c) == 0x0B9C) ||
257 (((c) >= 0x0B9E) && ((c) <= 0x0B9F)) ||
258 (((c) >= 0x0BA3) && ((c) <= 0x0BA4)) ||
259 (((c) >= 0x0BA8) && ((c) <= 0x0BAA)) ||
260 (((c) >= 0x0BAE) && ((c) <= 0x0BB5)) ||
261 (((c) >= 0x0BB7) && ((c) <= 0x0BB9)) ||
262 (((c) >= 0x0C05) && ((c) <= 0x0C0C)) ||
263 (((c) >= 0x0C0E) && ((c) <= 0x0C10)) ||
264 (((c) >= 0x0C12) && ((c) <= 0x0C28)) ||
265 (((c) >= 0x0C2A) && ((c) <= 0x0C33)) ||
266 (((c) >= 0x0C35) && ((c) <= 0x0C39)) ||
267 (((c) >= 0x0C60) && ((c) <= 0x0C61)) ||
268 (((c) >= 0x0C85) && ((c) <= 0x0C8C)) ||
269 (((c) >= 0x0C8E) && ((c) <= 0x0C90)) ||
270 (((c) >= 0x0C92) && ((c) <= 0x0CA8)) ||
271 (((c) >= 0x0CAA) && ((c) <= 0x0CB3)) ||
272 (((c) >= 0x0CB5) && ((c) <= 0x0CB9)) ||
273 ((c) == 0x0CDE) ||
274 (((c) >= 0x0CE0) && ((c) <= 0x0CE1)) ||
275 (((c) >= 0x0D05) && ((c) <= 0x0D0C)) ||
276 (((c) >= 0x0D0E) && ((c) <= 0x0D10)) ||
277 (((c) >= 0x0D12) && ((c) <= 0x0D28)) ||
278 (((c) >= 0x0D2A) && ((c) <= 0x0D39)) ||
279 (((c) >= 0x0D60) && ((c) <= 0x0D61)) ||
280 (((c) >= 0x0E01) && ((c) <= 0x0E2E)) ||
281 ((c) == 0x0E30) ||
282 (((c) >= 0x0E32) && ((c) <= 0x0E33)) ||
283 (((c) >= 0x0E40) && ((c) <= 0x0E45)) ||
284 (((c) >= 0x0E81) && ((c) <= 0x0E82)) ||
285 ((c) == 0x0E84) ||
286 (((c) >= 0x0E87) && ((c) <= 0x0E88)) ||
287 ((c) == 0x0E8A) ||
288 ((c) == 0x0E8D) ||
289 (((c) >= 0x0E94) && ((c) <= 0x0E97)) ||
290 (((c) >= 0x0E99) && ((c) <= 0x0E9F)) ||
291 (((c) >= 0x0EA1) && ((c) <= 0x0EA3)) ||
292 ((c) == 0x0EA5) ||
293 ((c) == 0x0EA7) ||
294 (((c) >= 0x0EAA) && ((c) <= 0x0EAB)) ||
295 (((c) >= 0x0EAD) && ((c) <= 0x0EAE)) ||
296 ((c) == 0x0EB0) ||
297 (((c) >= 0x0EB2) && ((c) <= 0x0EB3)) ||
298 ((c) == 0x0EBD) ||
299 (((c) >= 0x0EC0) && ((c) <= 0x0EC4)) ||
300 (((c) >= 0x0F40) && ((c) <= 0x0F47)) ||
301 (((c) >= 0x0F49) && ((c) <= 0x0F69)) ||
302 (((c) >= 0x10A0) && ( /* accelerator */
303 (((c) >= 0x10A0) && ((c) <= 0x10C5)) ||
304 (((c) >= 0x10D0) && ((c) <= 0x10F6)) ||
305 ((c) == 0x1100) ||
306 (((c) >= 0x1102) && ((c) <= 0x1103)) ||
307 (((c) >= 0x1105) && ((c) <= 0x1107)) ||
308 ((c) == 0x1109) ||
309 (((c) >= 0x110B) && ((c) <= 0x110C)) ||
310 (((c) >= 0x110E) && ((c) <= 0x1112)) ||
311 ((c) == 0x113C) ||
312 ((c) == 0x113E) ||
313 ((c) == 0x1140) ||
314 ((c) == 0x114C) ||
315 ((c) == 0x114E) ||
316 ((c) == 0x1150) ||
317 (((c) >= 0x1154) && ((c) <= 0x1155)) ||
318 ((c) == 0x1159) ||
319 (((c) >= 0x115F) && ((c) <= 0x1161)) ||
320 ((c) == 0x1163) ||
321 ((c) == 0x1165) ||
322 ((c) == 0x1167) ||
323 ((c) == 0x1169) ||
324 (((c) >= 0x116D) && ((c) <= 0x116E)) ||
325 (((c) >= 0x1172) && ((c) <= 0x1173)) ||
326 ((c) == 0x1175) ||
327 ((c) == 0x119E) ||
328 ((c) == 0x11A8) ||
329 ((c) == 0x11AB) ||
330 (((c) >= 0x11AE) && ((c) <= 0x11AF)) ||
331 (((c) >= 0x11B7) && ((c) <= 0x11B8)) ||
332 ((c) == 0x11BA) ||
333 (((c) >= 0x11BC) && ((c) <= 0x11C2)) ||
334 ((c) == 0x11EB) ||
335 ((c) == 0x11F0) ||
336 ((c) == 0x11F9) ||
337 (((c) >= 0x1E00) && ((c) <= 0x1E9B)) ||
338 (((c) >= 0x1EA0) && ((c) <= 0x1EF9)) ||
339 (((c) >= 0x1F00) && ((c) <= 0x1F15)) ||
340 (((c) >= 0x1F18) && ((c) <= 0x1F1D)) ||
341 (((c) >= 0x1F20) && ((c) <= 0x1F45)) ||
342 (((c) >= 0x1F48) && ((c) <= 0x1F4D)) ||
343 (((c) >= 0x1F50) && ((c) <= 0x1F57)) ||
344 ((c) == 0x1F59) ||
345 ((c) == 0x1F5B) ||
346 ((c) == 0x1F5D) ||
347 (((c) >= 0x1F5F) && ((c) <= 0x1F7D)) ||
348 (((c) >= 0x1F80) && ((c) <= 0x1FB4)) ||
349 (((c) >= 0x1FB6) && ((c) <= 0x1FBC)) ||
350 ((c) == 0x1FBE) ||
351 (((c) >= 0x1FC2) && ((c) <= 0x1FC4)) ||
352 (((c) >= 0x1FC6) && ((c) <= 0x1FCC)) ||
353 (((c) >= 0x1FD0) && ((c) <= 0x1FD3)) ||
354 (((c) >= 0x1FD6) && ((c) <= 0x1FDB)) ||
355 (((c) >= 0x1FE0) && ((c) <= 0x1FEC)) ||
356 (((c) >= 0x1FF2) && ((c) <= 0x1FF4)) ||
357 (((c) >= 0x1FF6) && ((c) <= 0x1FFC)) ||
358 ((c) == 0x2126) ||
359 (((c) >= 0x212A) && ((c) <= 0x212B)) ||
360 ((c) == 0x212E) ||
361 (((c) >= 0x2180) && ((c) <= 0x2182)) ||
362 (((c) >= 0x3041) && ((c) <= 0x3094)) ||
363 (((c) >= 0x30A1) && ((c) <= 0x30FA)) ||
364 (((c) >= 0x3105) && ((c) <= 0x312C)) ||
Daniel Veillard73b013f2003-09-30 12:36:01 +0000365 (((c) >= 0xAC00) && ((c) <= 0xD7A3))) /* accelerators */
366 ))));
Owen Taylor3473f882001-02-23 17:55:21 +0000367}
368
369/**
370 * xmlIsDigit:
371 * @c: an unicode character (int)
372 *
373 * Check whether the character is allowed by the production
374 * [88] Digit ::= ... long list see REC ...
375 *
376 * Returns 0 if not, non-zero otherwise
377 */
378int
379xmlIsDigit(int c) {
380 return(
381 (((c) >= 0x0030) && ((c) <= 0x0039)) ||
382 (((c) >= 0x660) && ( /* accelerator */
383 (((c) >= 0x0660) && ((c) <= 0x0669)) ||
384 (((c) >= 0x06F0) && ((c) <= 0x06F9)) ||
385 (((c) >= 0x0966) && ((c) <= 0x096F)) ||
386 (((c) >= 0x09E6) && ((c) <= 0x09EF)) ||
387 (((c) >= 0x0A66) && ((c) <= 0x0A6F)) ||
388 (((c) >= 0x0AE6) && ((c) <= 0x0AEF)) ||
389 (((c) >= 0x0B66) && ((c) <= 0x0B6F)) ||
390 (((c) >= 0x0BE7) && ((c) <= 0x0BEF)) ||
391 (((c) >= 0x0C66) && ((c) <= 0x0C6F)) ||
392 (((c) >= 0x0CE6) && ((c) <= 0x0CEF)) ||
393 (((c) >= 0x0D66) && ((c) <= 0x0D6F)) ||
394 (((c) >= 0x0E50) && ((c) <= 0x0E59)) ||
395 (((c) >= 0x0ED0) && ((c) <= 0x0ED9)) ||
396 (((c) >= 0x0F20) && ((c) <= 0x0F29))) /* accelerator */ ));
397}
398
399/**
400 * xmlIsCombining:
401 * @c: an unicode character (int)
402 *
403 * Check whether the character is allowed by the production
404 * [87] CombiningChar ::= ... long list see REC ...
405 *
406 * Returns 0 if not, non-zero otherwise
407 */
408int
409xmlIsCombining(int c) {
410 return(
411 (((c) >= 0x300) && ( /* accelerator */
412 (((c) >= 0x0300) && ((c) <= 0x0345)) ||
413 (((c) >= 0x0360) && ((c) <= 0x0361)) ||
414 (((c) >= 0x0483) && ((c) <= 0x0486)) ||
415 (((c) >= 0x0591) && ((c) <= 0x05A1)) ||
416 (((c) >= 0x05A3) && ((c) <= 0x05B9)) ||
417 (((c) >= 0x05BB) && ((c) <= 0x05BD)) ||
418 ((c) == 0x05BF) ||
419 (((c) >= 0x05C1) && ((c) <= 0x05C2)) ||
420 ((c) == 0x05C4) ||
421 (((c) >= 0x064B) && ((c) <= 0x0652)) ||
422 ((c) == 0x0670) ||
423 (((c) >= 0x06D6) && ((c) <= 0x06DC)) ||
424 (((c) >= 0x06DD) && ((c) <= 0x06DF)) ||
425 (((c) >= 0x06E0) && ((c) <= 0x06E4)) ||
426 (((c) >= 0x06E7) && ((c) <= 0x06E8)) ||
427 (((c) >= 0x06EA) && ((c) <= 0x06ED)) ||
428 (((c) >= 0x0901) && ( /* accelerator */
429 (((c) >= 0x0901) && ((c) <= 0x0903)) ||
430 ((c) == 0x093C) ||
431 (((c) >= 0x093E) && ((c) <= 0x094C)) ||
432 ((c) == 0x094D) ||
433 (((c) >= 0x0951) && ((c) <= 0x0954)) ||
434 (((c) >= 0x0962) && ((c) <= 0x0963)) ||
435 (((c) >= 0x0981) && ((c) <= 0x0983)) ||
436 ((c) == 0x09BC) ||
437 ((c) == 0x09BE) ||
438 ((c) == 0x09BF) ||
439 (((c) >= 0x09C0) && ((c) <= 0x09C4)) ||
440 (((c) >= 0x09C7) && ((c) <= 0x09C8)) ||
441 (((c) >= 0x09CB) && ((c) <= 0x09CD)) ||
442 ((c) == 0x09D7) ||
443 (((c) >= 0x09E2) && ((c) <= 0x09E3)) ||
444 (((c) >= 0x0A02) && ( /* accelerator */
445 ((c) == 0x0A02) ||
446 ((c) == 0x0A3C) ||
447 ((c) == 0x0A3E) ||
448 ((c) == 0x0A3F) ||
449 (((c) >= 0x0A40) && ((c) <= 0x0A42)) ||
450 (((c) >= 0x0A47) && ((c) <= 0x0A48)) ||
451 (((c) >= 0x0A4B) && ((c) <= 0x0A4D)) ||
452 (((c) >= 0x0A70) && ((c) <= 0x0A71)) ||
453 (((c) >= 0x0A81) && ((c) <= 0x0A83)) ||
454 ((c) == 0x0ABC) ||
455 (((c) >= 0x0ABE) && ((c) <= 0x0AC5)) ||
456 (((c) >= 0x0AC7) && ((c) <= 0x0AC9)) ||
457 (((c) >= 0x0ACB) && ((c) <= 0x0ACD)) ||
458 (((c) >= 0x0B01) && ((c) <= 0x0B03)) ||
459 ((c) == 0x0B3C) ||
460 (((c) >= 0x0B3E) && ((c) <= 0x0B43)) ||
461 (((c) >= 0x0B47) && ((c) <= 0x0B48)) ||
462 (((c) >= 0x0B4B) && ((c) <= 0x0B4D)) ||
463 (((c) >= 0x0B56) && ((c) <= 0x0B57)) ||
464 (((c) >= 0x0B82) && ((c) <= 0x0B83)) ||
465 (((c) >= 0x0BBE) && ((c) <= 0x0BC2)) ||
466 (((c) >= 0x0BC6) && ((c) <= 0x0BC8)) ||
467 (((c) >= 0x0BCA) && ((c) <= 0x0BCD)) ||
468 ((c) == 0x0BD7) ||
469 (((c) >= 0x0C01) && ((c) <= 0x0C03)) ||
470 (((c) >= 0x0C3E) && ((c) <= 0x0C44)) ||
471 (((c) >= 0x0C46) && ((c) <= 0x0C48)) ||
472 (((c) >= 0x0C4A) && ((c) <= 0x0C4D)) ||
473 (((c) >= 0x0C55) && ((c) <= 0x0C56)) ||
474 (((c) >= 0x0C82) && ((c) <= 0x0C83)) ||
475 (((c) >= 0x0CBE) && ((c) <= 0x0CC4)) ||
476 (((c) >= 0x0CC6) && ((c) <= 0x0CC8)) ||
477 (((c) >= 0x0CCA) && ((c) <= 0x0CCD)) ||
478 (((c) >= 0x0CD5) && ((c) <= 0x0CD6)) ||
479 (((c) >= 0x0D02) && ((c) <= 0x0D03)) ||
480 (((c) >= 0x0D3E) && ((c) <= 0x0D43)) ||
481 (((c) >= 0x0D46) && ((c) <= 0x0D48)) ||
482 (((c) >= 0x0D4A) && ((c) <= 0x0D4D)) ||
483 ((c) == 0x0D57) ||
484 (((c) >= 0x0E31) && ( /* accelerator */
485 ((c) == 0x0E31) ||
486 (((c) >= 0x0E34) && ((c) <= 0x0E3A)) ||
487 (((c) >= 0x0E47) && ((c) <= 0x0E4E)) ||
488 ((c) == 0x0EB1) ||
489 (((c) >= 0x0EB4) && ((c) <= 0x0EB9)) ||
490 (((c) >= 0x0EBB) && ((c) <= 0x0EBC)) ||
491 (((c) >= 0x0EC8) && ((c) <= 0x0ECD)) ||
492 (((c) >= 0x0F18) && ((c) <= 0x0F19)) ||
493 ((c) == 0x0F35) ||
494 ((c) == 0x0F37) ||
495 ((c) == 0x0F39) ||
496 ((c) == 0x0F3E) ||
497 ((c) == 0x0F3F) ||
498 (((c) >= 0x0F71) && ((c) <= 0x0F84)) ||
499 (((c) >= 0x0F86) && ((c) <= 0x0F8B)) ||
500 (((c) >= 0x0F90) && ((c) <= 0x0F95)) ||
501 ((c) == 0x0F97) ||
502 (((c) >= 0x0F99) && ((c) <= 0x0FAD)) ||
503 (((c) >= 0x0FB1) && ((c) <= 0x0FB7)) ||
504 ((c) == 0x0FB9) ||
505 (((c) >= 0x20D0) && ((c) <= 0x20DC)) ||
506 ((c) == 0x20E1) ||
507 (((c) >= 0x302A) && ((c) <= 0x302F)) ||
508 ((c) == 0x3099) ||
509 ((c) == 0x309A))))))))));
510}
511
512/**
513 * xmlIsExtender:
514 * @c: an unicode character (int)
515 *
516 * Check whether the character is allowed by the production
517 * [89] Extender ::= #x00B7 | #x02D0 | #x02D1 | #x0387 | #x0640 |
518 * #x0E46 | #x0EC6 | #x3005 | [#x3031-#x3035] |
519 * [#x309D-#x309E] | [#x30FC-#x30FE]
520 *
521 * Returns 0 if not, non-zero otherwise
522 */
523int
524xmlIsExtender(int c) {
525 switch (c) {
526 case 0x00B7: case 0x02D0: case 0x02D1: case 0x0387:
527 case 0x0640: case 0x0E46: case 0x0EC6: case 0x3005:
528 case 0x3031: case 0x3032: case 0x3033: case 0x3034:
529 case 0x3035: case 0x309D: case 0x309E: case 0x30FC:
Daniel Veillard4a7ae502002-02-18 19:18:17 +0000530 case 0x30FD: case 0x30FE:
Owen Taylor3473f882001-02-23 17:55:21 +0000531 return 1;
532 default:
533 return 0;
534 }
535}
536
537/**
538 * xmlIsIdeographic:
539 * @c: an unicode character (int)
540 *
541 * Check whether the character is allowed by the production
542 * [86] Ideographic ::= [#x4E00-#x9FA5] | #x3007 | [#x3021-#x3029]
543 *
544 * Returns 0 if not, non-zero otherwise
545 */
546int
547xmlIsIdeographic(int c) {
548 return(((c) < 0x0100) ? 0 :
549 (((c) >= 0x4e00) && ((c) <= 0x9fa5)) ||
550 (((c) >= 0xf900) && ((c) <= 0xfa2d)) ||
551 (((c) >= 0x3021) && ((c) <= 0x3029)) ||
552 ((c) == 0x3007));
553}
554
555/**
556 * xmlIsLetter:
557 * @c: an unicode character (int)
558 *
559 * Check whether the character is allowed by the production
560 * [84] Letter ::= BaseChar | Ideographic
561 *
562 * Returns 0 if not, non-zero otherwise
563 */
564int
565xmlIsLetter(int c) {
566 return(IS_BASECHAR(c) || IS_IDEOGRAPHIC(c));
567}
568
569/**
570 * xmlIsPubidChar:
571 * @c: an unicode character (int)
572 *
573 * Check whether the character is allowed by the production
574 * [13] PubidChar ::= #x20 | #xD | #xA | [a-zA-Z0-9] | [-'()+,./:=?;!*#@$_%]
575 *
576 * Returns 0 if not, non-zero otherwise
577 */
578int
579xmlIsPubidChar(int c) {
580 return(
581 ((c) == 0x20) || ((c) == 0x0D) || ((c) == 0x0A) ||
582 (((c) >= 'a') && ((c) <= 'z')) ||
583 (((c) >= 'A') && ((c) <= 'Z')) ||
584 (((c) >= '0') && ((c) <= '9')) ||
585 ((c) == '-') || ((c) == '\'') || ((c) == '(') || ((c) == ')') ||
586 ((c) == '+') || ((c) == ',') || ((c) == '.') || ((c) == '/') ||
587 ((c) == ':') || ((c) == '=') || ((c) == '?') || ((c) == ';') ||
588 ((c) == '!') || ((c) == '*') || ((c) == '#') || ((c) == '@') ||
589 ((c) == '$') || ((c) == '_') || ((c) == '%'));
590}
591
592/************************************************************************
593 * *
594 * Input handling functions for progressive parsing *
595 * *
596 ************************************************************************/
597
598/* #define DEBUG_INPUT */
599/* #define DEBUG_STACK */
600/* #define DEBUG_PUSH */
601
602
603/* we need to keep enough input to show errors in context */
604#define LINE_LEN 80
605
606#ifdef DEBUG_INPUT
607#define CHECK_BUFFER(in) check_buffer(in)
608
Daniel Veillard01c13b52002-12-10 15:19:08 +0000609static
Owen Taylor3473f882001-02-23 17:55:21 +0000610void check_buffer(xmlParserInputPtr in) {
611 if (in->base != in->buf->buffer->content) {
612 xmlGenericError(xmlGenericErrorContext,
613 "xmlParserInput: base mismatch problem\n");
614 }
615 if (in->cur < in->base) {
616 xmlGenericError(xmlGenericErrorContext,
617 "xmlParserInput: cur < base problem\n");
618 }
619 if (in->cur > in->base + in->buf->buffer->use) {
620 xmlGenericError(xmlGenericErrorContext,
621 "xmlParserInput: cur > base + use problem\n");
622 }
623 xmlGenericError(xmlGenericErrorContext,"buffer %x : content %x, cur %d, use %d, size %d\n",
624 (int) in, (int) in->buf->buffer->content, in->cur - in->base,
625 in->buf->buffer->use, in->buf->buffer->size);
626}
627
628#else
629#define CHECK_BUFFER(in)
630#endif
631
632
633/**
634 * xmlParserInputRead:
635 * @in: an XML parser input
636 * @len: an indicative size for the lookahead
637 *
638 * This function refresh the input for the parser. It doesn't try to
639 * preserve pointers to the input buffer, and discard already read data
640 *
641 * Returns the number of xmlChars read, or -1 in case of error, 0 indicate the
642 * end of this entity
643 */
644int
645xmlParserInputRead(xmlParserInputPtr in, int len) {
646 int ret;
647 int used;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000648 int indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000649
650#ifdef DEBUG_INPUT
651 xmlGenericError(xmlGenericErrorContext, "Read\n");
652#endif
653 if (in->buf == NULL) return(-1);
654 if (in->base == NULL) return(-1);
655 if (in->cur == NULL) return(-1);
656 if (in->buf->buffer == NULL) return(-1);
657 if (in->buf->readcallback == NULL) return(-1);
658
659 CHECK_BUFFER(in);
660
661 used = in->cur - in->buf->buffer->content;
662 ret = xmlBufferShrink(in->buf->buffer, used);
663 if (ret > 0) {
664 in->cur -= ret;
665 in->consumed += ret;
666 }
667 ret = xmlParserInputBufferRead(in->buf, len);
668 if (in->base != in->buf->buffer->content) {
669 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000670 * the buffer has been reallocated
Owen Taylor3473f882001-02-23 17:55:21 +0000671 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000672 indx = in->cur - in->base;
Owen Taylor3473f882001-02-23 17:55:21 +0000673 in->base = in->buf->buffer->content;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000674 in->cur = &in->buf->buffer->content[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000675 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000676 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +0000677
678 CHECK_BUFFER(in);
679
680 return(ret);
681}
682
683/**
684 * xmlParserInputGrow:
685 * @in: an XML parser input
686 * @len: an indicative size for the lookahead
687 *
688 * This function increase the input for the parser. It tries to
689 * preserve pointers to the input buffer, and keep already read data
690 *
691 * Returns the number of xmlChars read, or -1 in case of error, 0 indicate the
692 * end of this entity
693 */
694int
695xmlParserInputGrow(xmlParserInputPtr in, int len) {
696 int ret;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000697 int indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000698
699#ifdef DEBUG_INPUT
700 xmlGenericError(xmlGenericErrorContext, "Grow\n");
701#endif
702 if (in->buf == NULL) return(-1);
703 if (in->base == NULL) return(-1);
704 if (in->cur == NULL) return(-1);
705 if (in->buf->buffer == NULL) return(-1);
706
707 CHECK_BUFFER(in);
708
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000709 indx = in->cur - in->base;
710 if (in->buf->buffer->use > (unsigned int) indx + INPUT_CHUNK) {
Owen Taylor3473f882001-02-23 17:55:21 +0000711
712 CHECK_BUFFER(in);
713
714 return(0);
715 }
716 if (in->buf->readcallback != NULL)
717 ret = xmlParserInputBufferGrow(in->buf, len);
718 else
719 return(0);
720
721 /*
Daniel Veillard48b2f892001-02-25 16:11:03 +0000722 * NOTE : in->base may be a "dangling" i.e. freed pointer in this
Owen Taylor3473f882001-02-23 17:55:21 +0000723 * block, but we use it really as an integer to do some
724 * pointer arithmetic. Insure will raise it as a bug but in
725 * that specific case, that's not !
726 */
727 if (in->base != in->buf->buffer->content) {
728 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000729 * the buffer has been reallocated
Owen Taylor3473f882001-02-23 17:55:21 +0000730 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000731 indx = in->cur - in->base;
Owen Taylor3473f882001-02-23 17:55:21 +0000732 in->base = in->buf->buffer->content;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000733 in->cur = &in->buf->buffer->content[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000734 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000735 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +0000736
737 CHECK_BUFFER(in);
738
739 return(ret);
740}
741
742/**
743 * xmlParserInputShrink:
744 * @in: an XML parser input
745 *
746 * This function removes used input for the parser.
747 */
748void
749xmlParserInputShrink(xmlParserInputPtr in) {
750 int used;
751 int ret;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000752 int indx;
Owen Taylor3473f882001-02-23 17:55:21 +0000753
754#ifdef DEBUG_INPUT
755 xmlGenericError(xmlGenericErrorContext, "Shrink\n");
756#endif
757 if (in->buf == NULL) return;
758 if (in->base == NULL) return;
759 if (in->cur == NULL) return;
760 if (in->buf->buffer == NULL) return;
761
762 CHECK_BUFFER(in);
763
764 used = in->cur - in->buf->buffer->content;
765 /*
766 * Do not shrink on large buffers whose only a tiny fraction
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000767 * was consumed
Owen Taylor3473f882001-02-23 17:55:21 +0000768 */
Owen Taylor3473f882001-02-23 17:55:21 +0000769 if (used > INPUT_CHUNK) {
770 ret = xmlBufferShrink(in->buf->buffer, used - LINE_LEN);
771 if (ret > 0) {
772 in->cur -= ret;
773 in->consumed += ret;
774 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000775 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +0000776 }
777
778 CHECK_BUFFER(in);
779
780 if (in->buf->buffer->use > INPUT_CHUNK) {
781 return;
782 }
783 xmlParserInputBufferRead(in->buf, 2 * INPUT_CHUNK);
784 if (in->base != in->buf->buffer->content) {
785 /*
Daniel Veillard5e5c2d02002-02-09 18:03:01 +0000786 * the buffer has been reallocated
Owen Taylor3473f882001-02-23 17:55:21 +0000787 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000788 indx = in->cur - in->base;
Owen Taylor3473f882001-02-23 17:55:21 +0000789 in->base = in->buf->buffer->content;
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000790 in->cur = &in->buf->buffer->content[indx];
Owen Taylor3473f882001-02-23 17:55:21 +0000791 }
Daniel Veillard48b2f892001-02-25 16:11:03 +0000792 in->end = &in->buf->buffer->content[in->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +0000793
794 CHECK_BUFFER(in);
795}
796
797/************************************************************************
798 * *
799 * UTF8 character input and related functions *
800 * *
801 ************************************************************************/
802
803/**
804 * xmlNextChar:
805 * @ctxt: the XML parser context
806 *
807 * Skip to the next char input char.
808 */
809
810void
Daniel Veillard77a90a72003-03-22 00:04:05 +0000811xmlNextChar(xmlParserCtxtPtr ctxt)
812{
Owen Taylor3473f882001-02-23 17:55:21 +0000813 if (ctxt->instate == XML_PARSER_EOF)
Daniel Veillard77a90a72003-03-22 00:04:05 +0000814 return;
Owen Taylor3473f882001-02-23 17:55:21 +0000815
Daniel Veillardfdc91562002-07-01 21:52:03 +0000816 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
Daniel Veillard77a90a72003-03-22 00:04:05 +0000817 if ((*ctxt->input->cur == 0) &&
818 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0) &&
819 (ctxt->instate != XML_PARSER_COMMENT)) {
820 /*
821 * If we are at the end of the current entity and
822 * the context allows it, we pop consumed entities
823 * automatically.
824 * the auto closing should be blocked in other cases
825 */
826 xmlPopInput(ctxt);
827 } else {
828 const unsigned char *cur;
829 unsigned char c;
Owen Taylor3473f882001-02-23 17:55:21 +0000830
Daniel Veillard77a90a72003-03-22 00:04:05 +0000831 /*
832 * 2.11 End-of-Line Handling
833 * the literal two-character sequence "#xD#xA" or a standalone
834 * literal #xD, an XML processor must pass to the application
835 * the single character #xA.
836 */
837 if (*(ctxt->input->cur) == '\n') {
838 ctxt->input->line++;
839 ctxt->input->col = 1;
840 } else
841 ctxt->input->col++;
Owen Taylor3473f882001-02-23 17:55:21 +0000842
Daniel Veillard77a90a72003-03-22 00:04:05 +0000843 /*
844 * We are supposed to handle UTF8, check it's valid
845 * From rfc2044: encoding of the Unicode values on UTF-8:
846 *
847 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
848 * 0000 0000-0000 007F 0xxxxxxx
849 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
850 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
851 *
852 * Check for the 0x110000 limit too
853 */
854 cur = ctxt->input->cur;
855
856 c = *cur;
857 if (c & 0x80) {
Daniel Veillard0e0f37a2003-05-20 12:22:41 +0000858 if (c == 0xC0)
859 goto encoding_error;
Daniel Veillard77a90a72003-03-22 00:04:05 +0000860 if (cur[1] == 0)
861 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
862 if ((cur[1] & 0xc0) != 0x80)
863 goto encoding_error;
864 if ((c & 0xe0) == 0xe0) {
865 unsigned int val;
866
867 if (cur[2] == 0)
868 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
869 if ((cur[2] & 0xc0) != 0x80)
870 goto encoding_error;
871 if ((c & 0xf0) == 0xf0) {
872 if (cur[3] == 0)
873 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
874 if (((c & 0xf8) != 0xf0) ||
875 ((cur[3] & 0xc0) != 0x80))
876 goto encoding_error;
877 /* 4-byte code */
878 ctxt->input->cur += 4;
879 val = (cur[0] & 0x7) << 18;
880 val |= (cur[1] & 0x3f) << 12;
881 val |= (cur[2] & 0x3f) << 6;
882 val |= cur[3] & 0x3f;
883 } else {
884 /* 3-byte code */
885 ctxt->input->cur += 3;
886 val = (cur[0] & 0xf) << 12;
887 val |= (cur[1] & 0x3f) << 6;
888 val |= cur[2] & 0x3f;
889 }
890 if (((val > 0xd7ff) && (val < 0xe000)) ||
891 ((val > 0xfffd) && (val < 0x10000)) ||
892 (val >= 0x110000)) {
893 if ((ctxt->sax != NULL) &&
894 (ctxt->sax->error != NULL))
895 ctxt->sax->error(ctxt->userData,
896 "Char 0x%X out of allowed range\n",
897 val);
898 ctxt->errNo = XML_ERR_INVALID_ENCODING;
899 ctxt->wellFormed = 0;
900 if (ctxt->recovery == 0)
901 ctxt->disableSAX = 1;
902 }
903 } else
904 /* 2-byte code */
905 ctxt->input->cur += 2;
906 } else
907 /* 1-byte code */
908 ctxt->input->cur++;
909
910 ctxt->nbChars++;
911 if (*ctxt->input->cur == 0)
912 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
913 }
Owen Taylor3473f882001-02-23 17:55:21 +0000914 } else {
Daniel Veillard77a90a72003-03-22 00:04:05 +0000915 /*
916 * Assume it's a fixed length encoding (1) with
917 * a compatible encoding for the ASCII set, since
918 * XML constructs only use < 128 chars
919 */
920
921 if (*(ctxt->input->cur) == '\n') {
922 ctxt->input->line++;
923 ctxt->input->col = 1;
924 } else
925 ctxt->input->col++;
926 ctxt->input->cur++;
927 ctxt->nbChars++;
928 if (*ctxt->input->cur == 0)
929 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Owen Taylor3473f882001-02-23 17:55:21 +0000930 }
Daniel Veillard561b7f82002-03-20 21:55:57 +0000931 if ((*ctxt->input->cur == '%') && (!ctxt->html))
Daniel Veillard77a90a72003-03-22 00:04:05 +0000932 xmlParserHandlePEReference(ctxt);
Daniel Veillard561b7f82002-03-20 21:55:57 +0000933 if ((*ctxt->input->cur == 0) &&
Owen Taylor3473f882001-02-23 17:55:21 +0000934 (xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0))
Daniel Veillard77a90a72003-03-22 00:04:05 +0000935 xmlPopInput(ctxt);
Owen Taylor3473f882001-02-23 17:55:21 +0000936 return;
Daniel Veillard77a90a72003-03-22 00:04:05 +0000937 encoding_error:
Owen Taylor3473f882001-02-23 17:55:21 +0000938 /*
939 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000940 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +0000941 * declaration header. Report the error and switch the encoding
942 * to ISO-Latin-1 (if you don't like this policy, just declare the
943 * encoding !)
944 */
945 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) {
Daniel Veillard77a90a72003-03-22 00:04:05 +0000946 ctxt->sax->error(ctxt->userData,
947 "Input is not proper UTF-8, indicate encoding !\n");
948 ctxt->sax->error(ctxt->userData,
949 "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
950 ctxt->input->cur[0], ctxt->input->cur[1],
951 ctxt->input->cur[2], ctxt->input->cur[3]);
Owen Taylor3473f882001-02-23 17:55:21 +0000952 }
Daniel Veillard8ab0f582002-02-18 18:31:38 +0000953 ctxt->wellFormed = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000954 ctxt->errNo = XML_ERR_INVALID_ENCODING;
955
Daniel Veillard77a90a72003-03-22 00:04:05 +0000956 ctxt->charset = XML_CHAR_ENCODING_8859_1;
Daniel Veillard561b7f82002-03-20 21:55:57 +0000957 ctxt->input->cur++;
Owen Taylor3473f882001-02-23 17:55:21 +0000958 return;
959}
960
961/**
962 * xmlCurrentChar:
963 * @ctxt: the XML parser context
964 * @len: pointer to the length of the char read
965 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +0000966 * The current char value, if using UTF-8 this may actually span multiple
Owen Taylor3473f882001-02-23 17:55:21 +0000967 * bytes in the input buffer. Implement the end of line normalization:
968 * 2.11 End-of-Line Handling
969 * Wherever an external parsed entity or the literal entity value
970 * of an internal parsed entity contains either the literal two-character
971 * sequence "#xD#xA" or a standalone literal #xD, an XML processor
972 * must pass to the application the single character #xA.
973 * This behavior can conveniently be produced by normalizing all
974 * line breaks to #xA on input, before parsing.)
975 *
Daniel Veillard60087f32001-10-10 09:45:09 +0000976 * Returns the current char value and its length
Owen Taylor3473f882001-02-23 17:55:21 +0000977 */
978
979int
980xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
981 if (ctxt->instate == XML_PARSER_EOF)
982 return(0);
983
Daniel Veillard561b7f82002-03-20 21:55:57 +0000984 if ((*ctxt->input->cur >= 0x20) && (*ctxt->input->cur <= 0x7F)) {
985 *len = 1;
986 return((int) *ctxt->input->cur);
Owen Taylor3473f882001-02-23 17:55:21 +0000987 }
988 if (ctxt->charset == XML_CHAR_ENCODING_UTF8) {
989 /*
990 * We are supposed to handle UTF8, check it's valid
991 * From rfc2044: encoding of the Unicode values on UTF-8:
992 *
993 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
994 * 0000 0000-0000 007F 0xxxxxxx
995 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
996 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
997 *
998 * Check for the 0x110000 limit too
999 */
1000 const unsigned char *cur = ctxt->input->cur;
1001 unsigned char c;
1002 unsigned int val;
1003
1004 c = *cur;
1005 if (c & 0x80) {
Daniel Veillard0e0f37a2003-05-20 12:22:41 +00001006 if (c == 0xC0)
1007 goto encoding_error;
Daniel Veillard561b7f82002-03-20 21:55:57 +00001008 if (cur[1] == 0)
1009 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1010 if ((cur[1] & 0xc0) != 0x80)
Owen Taylor3473f882001-02-23 17:55:21 +00001011 goto encoding_error;
1012 if ((c & 0xe0) == 0xe0) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00001013
1014 if (cur[2] == 0)
1015 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
1016 if ((cur[2] & 0xc0) != 0x80)
Owen Taylor3473f882001-02-23 17:55:21 +00001017 goto encoding_error;
1018 if ((c & 0xf0) == 0xf0) {
1019 if (cur[3] == 0)
1020 xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
Daniel Veillard561b7f82002-03-20 21:55:57 +00001021 if (((c & 0xf8) != 0xf0) ||
Owen Taylor3473f882001-02-23 17:55:21 +00001022 ((cur[3] & 0xc0) != 0x80))
1023 goto encoding_error;
1024 /* 4-byte code */
1025 *len = 4;
1026 val = (cur[0] & 0x7) << 18;
1027 val |= (cur[1] & 0x3f) << 12;
1028 val |= (cur[2] & 0x3f) << 6;
1029 val |= cur[3] & 0x3f;
1030 } else {
1031 /* 3-byte code */
1032 *len = 3;
1033 val = (cur[0] & 0xf) << 12;
1034 val |= (cur[1] & 0x3f) << 6;
1035 val |= cur[2] & 0x3f;
1036 }
1037 } else {
1038 /* 2-byte code */
1039 *len = 2;
1040 val = (cur[0] & 0x1f) << 6;
1041 val |= cur[1] & 0x3f;
1042 }
1043 if (!IS_CHAR(val)) {
1044 if ((ctxt->sax != NULL) &&
1045 (ctxt->sax->error != NULL))
1046 ctxt->sax->error(ctxt->userData,
1047 "Char 0x%X out of allowed range\n", val);
1048 ctxt->errNo = XML_ERR_INVALID_ENCODING;
1049 ctxt->wellFormed = 0;
Daniel Veillarddad3f682002-11-17 16:47:27 +00001050 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001051 }
1052 return(val);
1053 } else {
1054 /* 1-byte code */
1055 *len = 1;
1056 if (*ctxt->input->cur == 0xD) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00001057 if (ctxt->input->cur[1] == 0xA) {
Owen Taylor3473f882001-02-23 17:55:21 +00001058 ctxt->nbChars++;
1059 ctxt->input->cur++;
1060 }
1061 return(0xA);
1062 }
1063 return((int) *ctxt->input->cur);
1064 }
1065 }
1066 /*
Daniel Veillard60087f32001-10-10 09:45:09 +00001067 * Assume it's a fixed length encoding (1) with
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001068 * a compatible encoding for the ASCII set, since
Owen Taylor3473f882001-02-23 17:55:21 +00001069 * XML constructs only use < 128 chars
1070 */
1071 *len = 1;
1072 if (*ctxt->input->cur == 0xD) {
Daniel Veillard561b7f82002-03-20 21:55:57 +00001073 if (ctxt->input->cur[1] == 0xA) {
Owen Taylor3473f882001-02-23 17:55:21 +00001074 ctxt->nbChars++;
1075 ctxt->input->cur++;
1076 }
1077 return(0xA);
1078 }
1079 return((int) *ctxt->input->cur);
1080encoding_error:
1081 /*
Daniel Veillardd2ff0392002-11-22 12:28:38 +00001082 * An encoding problem may arise from a truncated input buffer
1083 * splitting a character in the middle. In that case do not raise
1084 * an error but return 0 to endicate an end of stream problem
1085 */
1086 if (ctxt->input->end - ctxt->input->cur < 4) {
1087 *len = 0;
1088 return(0);
1089 }
1090
1091 /*
Owen Taylor3473f882001-02-23 17:55:21 +00001092 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001093 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +00001094 * declaration header. Report the error and switch the encoding
1095 * to ISO-Latin-1 (if you don't like this policy, just declare the
1096 * encoding !)
1097 */
1098 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) {
1099 ctxt->sax->error(ctxt->userData,
1100 "Input is not proper UTF-8, indicate encoding !\n");
1101 ctxt->sax->error(ctxt->userData, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
Daniel Veillard561b7f82002-03-20 21:55:57 +00001102 ctxt->input->cur[0], ctxt->input->cur[1],
1103 ctxt->input->cur[2], ctxt->input->cur[3]);
Owen Taylor3473f882001-02-23 17:55:21 +00001104 }
Daniel Veillard8ab0f582002-02-18 18:31:38 +00001105 ctxt->wellFormed = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001106 ctxt->errNo = XML_ERR_INVALID_ENCODING;
1107
1108 ctxt->charset = XML_CHAR_ENCODING_8859_1;
1109 *len = 1;
1110 return((int) *ctxt->input->cur);
1111}
1112
1113/**
1114 * xmlStringCurrentChar:
1115 * @ctxt: the XML parser context
1116 * @cur: pointer to the beginning of the char
1117 * @len: pointer to the length of the char read
1118 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001119 * The current char value, if using UTF-8 this may actually span multiple
Owen Taylor3473f882001-02-23 17:55:21 +00001120 * bytes in the input buffer.
1121 *
Daniel Veillard60087f32001-10-10 09:45:09 +00001122 * Returns the current char value and its length
Owen Taylor3473f882001-02-23 17:55:21 +00001123 */
1124
1125int
Daniel Veillardd8224e02002-01-13 15:43:22 +00001126xmlStringCurrentChar(xmlParserCtxtPtr ctxt, const xmlChar * cur, int *len)
1127{
Daniel Veillard61d80a22001-04-27 17:13:01 +00001128 if ((ctxt == NULL) || (ctxt->charset == XML_CHAR_ENCODING_UTF8)) {
Daniel Veillardd8224e02002-01-13 15:43:22 +00001129 /*
1130 * We are supposed to handle UTF8, check it's valid
1131 * From rfc2044: encoding of the Unicode values on UTF-8:
1132 *
1133 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
1134 * 0000 0000-0000 007F 0xxxxxxx
1135 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
1136 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
1137 *
1138 * Check for the 0x110000 limit too
1139 */
1140 unsigned char c;
1141 unsigned int val;
Owen Taylor3473f882001-02-23 17:55:21 +00001142
Daniel Veillardd8224e02002-01-13 15:43:22 +00001143 c = *cur;
1144 if (c & 0x80) {
1145 if ((cur[1] & 0xc0) != 0x80)
1146 goto encoding_error;
1147 if ((c & 0xe0) == 0xe0) {
Owen Taylor3473f882001-02-23 17:55:21 +00001148
Daniel Veillardd8224e02002-01-13 15:43:22 +00001149 if ((cur[2] & 0xc0) != 0x80)
1150 goto encoding_error;
1151 if ((c & 0xf0) == 0xf0) {
1152 if (((c & 0xf8) != 0xf0) || ((cur[3] & 0xc0) != 0x80))
1153 goto encoding_error;
1154 /* 4-byte code */
1155 *len = 4;
1156 val = (cur[0] & 0x7) << 18;
1157 val |= (cur[1] & 0x3f) << 12;
1158 val |= (cur[2] & 0x3f) << 6;
1159 val |= cur[3] & 0x3f;
1160 } else {
1161 /* 3-byte code */
1162 *len = 3;
1163 val = (cur[0] & 0xf) << 12;
1164 val |= (cur[1] & 0x3f) << 6;
1165 val |= cur[2] & 0x3f;
1166 }
1167 } else {
1168 /* 2-byte code */
1169 *len = 2;
1170 val = (cur[0] & 0x1f) << 6;
1171 val |= cur[1] & 0x3f;
1172 }
1173 if (!IS_CHAR(val)) {
1174 if ((ctxt != NULL) && (ctxt->sax != NULL) &&
1175 (ctxt->sax->error != NULL))
1176 ctxt->sax->error(ctxt->userData,
1177 "Char 0x%X out of allowed range\n",
1178 val);
Daniel Veillardd076a202002-11-20 13:28:31 +00001179 if (ctxt != NULL) {
1180 ctxt->errNo = XML_ERR_INVALID_ENCODING;
1181 ctxt->wellFormed = 0;
1182 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
1183 }
Daniel Veillardd8224e02002-01-13 15:43:22 +00001184 }
1185 return (val);
1186 } else {
1187 /* 1-byte code */
1188 *len = 1;
1189 return ((int) *cur);
1190 }
Owen Taylor3473f882001-02-23 17:55:21 +00001191 }
1192 /*
Daniel Veillard60087f32001-10-10 09:45:09 +00001193 * Assume it's a fixed length encoding (1) with
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001194 * a compatible encoding for the ASCII set, since
Owen Taylor3473f882001-02-23 17:55:21 +00001195 * XML constructs only use < 128 chars
1196 */
1197 *len = 1;
Daniel Veillardd8224e02002-01-13 15:43:22 +00001198 return ((int) *cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001199encoding_error:
Daniel Veillardd8224e02002-01-13 15:43:22 +00001200
Owen Taylor3473f882001-02-23 17:55:21 +00001201 /*
1202 * If we detect an UTF8 error that probably mean that the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001203 * input encoding didn't get properly advertised in the
Owen Taylor3473f882001-02-23 17:55:21 +00001204 * declaration header. Report the error and switch the encoding
1205 * to ISO-Latin-1 (if you don't like this policy, just declare the
1206 * encoding !)
1207 */
Daniel Veillardd8224e02002-01-13 15:43:22 +00001208 if (ctxt != NULL) {
1209 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL)) {
1210 ctxt->sax->error(ctxt->userData,
1211 "Input is not proper UTF-8, indicate encoding !\n");
1212 ctxt->sax->error(ctxt->userData,
1213 "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
1214 ctxt->input->cur[0], ctxt->input->cur[1],
1215 ctxt->input->cur[2], ctxt->input->cur[3]);
1216 }
1217 ctxt->errNo = XML_ERR_INVALID_ENCODING;
Daniel Veillard8ab0f582002-02-18 18:31:38 +00001218 ctxt->wellFormed = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001219 }
Owen Taylor3473f882001-02-23 17:55:21 +00001220
1221 *len = 1;
Daniel Veillardd8224e02002-01-13 15:43:22 +00001222 return ((int) *cur);
Owen Taylor3473f882001-02-23 17:55:21 +00001223}
1224
1225/**
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001226 * xmlCopyCharMultiByte:
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001227 * @out: pointer to an array of xmlChar
Owen Taylor3473f882001-02-23 17:55:21 +00001228 * @val: the char value
1229 *
1230 * append the char value in the array
1231 *
1232 * Returns the number of xmlChar written
1233 */
Owen Taylor3473f882001-02-23 17:55:21 +00001234int
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001235xmlCopyCharMultiByte(xmlChar *out, int val) {
Owen Taylor3473f882001-02-23 17:55:21 +00001236 /*
1237 * We are supposed to handle UTF8, check it's valid
1238 * From rfc2044: encoding of the Unicode values on UTF-8:
1239 *
1240 * UCS-4 range (hex.) UTF-8 octet sequence (binary)
1241 * 0000 0000-0000 007F 0xxxxxxx
1242 * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
1243 * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
1244 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001245 if (val >= 0x80) {
1246 xmlChar *savedout = out;
1247 int bits;
1248 if (val < 0x800) { *out++= (val >> 6) | 0xC0; bits= 0; }
1249 else if (val < 0x10000) { *out++= (val >> 12) | 0xE0; bits= 6;}
1250 else if (val < 0x110000) { *out++= (val >> 18) | 0xF0; bits= 12; }
1251 else {
Owen Taylor3473f882001-02-23 17:55:21 +00001252 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001253 "Internal error, xmlCopyCharMultiByte 0x%X out of bound\n",
Owen Taylor3473f882001-02-23 17:55:21 +00001254 val);
1255 return(0);
1256 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001257 for ( ; bits >= 0; bits-= 6)
1258 *out++= ((val >> bits) & 0x3F) | 0x80 ;
1259 return (out - savedout);
Owen Taylor3473f882001-02-23 17:55:21 +00001260 }
1261 *out = (xmlChar) val;
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001262 return 1;
1263}
1264
1265/**
1266 * xmlCopyChar:
1267 * @len: Ignored, compatibility
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001268 * @out: pointer to an array of xmlChar
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001269 * @val: the char value
1270 *
1271 * append the char value in the array
1272 *
1273 * Returns the number of xmlChar written
1274 */
1275
1276int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001277xmlCopyChar(int len ATTRIBUTE_UNUSED, xmlChar *out, int val) {
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001278 /* the len parameter is ignored */
1279 if (val >= 0x80) {
1280 return(xmlCopyCharMultiByte (out, val));
1281 }
1282 *out = (xmlChar) val;
1283 return 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001284}
1285
1286/************************************************************************
1287 * *
1288 * Commodity functions to switch encodings *
1289 * *
1290 ************************************************************************/
1291
1292/**
1293 * xmlSwitchEncoding:
1294 * @ctxt: the parser context
1295 * @enc: the encoding value (number)
1296 *
1297 * change the input functions when discovering the character encoding
1298 * of a given entity.
1299 *
1300 * Returns 0 in case of success, -1 otherwise
1301 */
1302int
1303xmlSwitchEncoding(xmlParserCtxtPtr ctxt, xmlCharEncoding enc)
1304{
1305 xmlCharEncodingHandlerPtr handler;
1306
1307 switch (enc) {
1308 case XML_CHAR_ENCODING_ERROR:
1309 ctxt->errNo = XML_ERR_UNKNOWN_ENCODING;
1310 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1311 ctxt->sax->error(ctxt->userData, "encoding unknown\n");
1312 ctxt->wellFormed = 0;
Daniel Veillarddad3f682002-11-17 16:47:27 +00001313 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001314 break;
1315 case XML_CHAR_ENCODING_NONE:
1316 /* let's assume it's UTF-8 without the XML decl */
1317 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1318 return(0);
1319 case XML_CHAR_ENCODING_UTF8:
1320 /* default encoding, no conversion should be needed */
1321 ctxt->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard87a764e2001-06-20 17:41:10 +00001322
1323 /*
1324 * Errata on XML-1.0 June 20 2001
1325 * Specific handling of the Byte Order Mark for
1326 * UTF-8
1327 */
Daniel Veillard3e5bb8e2001-06-27 16:34:34 +00001328 if ((ctxt->input != NULL) &&
1329 (ctxt->input->cur[0] == 0xEF) &&
Daniel Veillard87a764e2001-06-20 17:41:10 +00001330 (ctxt->input->cur[1] == 0xBB) &&
1331 (ctxt->input->cur[2] == 0xBF)) {
1332 ctxt->input->cur += 3;
1333 }
Owen Taylor3473f882001-02-23 17:55:21 +00001334 return(0);
Daniel Veillard2dcb9372003-07-16 21:18:19 +00001335 case XML_CHAR_ENCODING_UTF16LE:
1336 case XML_CHAR_ENCODING_UTF16BE:
1337 /*The raw input characters are encoded
1338 *in UTF-16. As we expect this function
1339 *to be called after xmlCharEncInFunc, we expect
1340 *ctxt->input->cur to contain UTF-8 encoded characters.
1341 *So the raw UTF16 Byte Order Mark
1342 *has also been converted into
1343 *an UTF-8 BOM. Let's skip that BOM.
1344 */
1345 if ((ctxt->input != NULL) &&
1346 (ctxt->input->cur[0] == 0xEF) &&
1347 (ctxt->input->cur[1] == 0xBB) &&
1348 (ctxt->input->cur[2] == 0xBF)) {
1349 ctxt->input->cur += 3;
1350 }
1351 break ;
Owen Taylor3473f882001-02-23 17:55:21 +00001352 default:
1353 break;
1354 }
1355 handler = xmlGetCharEncodingHandler(enc);
1356 if (handler == NULL) {
1357 /*
1358 * Default handlers.
1359 */
1360 switch (enc) {
1361 case XML_CHAR_ENCODING_ERROR:
1362 ctxt->errNo = XML_ERR_UNKNOWN_ENCODING;
1363 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1364 ctxt->sax->error(ctxt->userData, "encoding unknown\n");
1365 ctxt->wellFormed = 0;
Daniel Veillarddad3f682002-11-17 16:47:27 +00001366 if (ctxt->recovery == 0) ctxt->disableSAX = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00001367 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1368 break;
1369 case XML_CHAR_ENCODING_NONE:
1370 /* let's assume it's UTF-8 without the XML decl */
1371 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1372 return(0);
1373 case XML_CHAR_ENCODING_UTF8:
1374 case XML_CHAR_ENCODING_ASCII:
1375 /* default encoding, no conversion should be needed */
1376 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1377 return(0);
1378 case XML_CHAR_ENCODING_UTF16LE:
1379 break;
1380 case XML_CHAR_ENCODING_UTF16BE:
1381 break;
1382 case XML_CHAR_ENCODING_UCS4LE:
1383 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1384 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1385 ctxt->sax->error(ctxt->userData,
1386 "char encoding USC4 little endian not supported\n");
1387 break;
1388 case XML_CHAR_ENCODING_UCS4BE:
1389 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1390 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1391 ctxt->sax->error(ctxt->userData,
1392 "char encoding USC4 big endian not supported\n");
1393 break;
1394 case XML_CHAR_ENCODING_EBCDIC:
1395 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1396 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1397 ctxt->sax->error(ctxt->userData,
1398 "char encoding EBCDIC not supported\n");
1399 break;
1400 case XML_CHAR_ENCODING_UCS4_2143:
1401 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1402 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1403 ctxt->sax->error(ctxt->userData,
1404 "char encoding UCS4 2143 not supported\n");
1405 break;
1406 case XML_CHAR_ENCODING_UCS4_3412:
1407 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1408 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1409 ctxt->sax->error(ctxt->userData,
1410 "char encoding UCS4 3412 not supported\n");
1411 break;
1412 case XML_CHAR_ENCODING_UCS2:
1413 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1414 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1415 ctxt->sax->error(ctxt->userData,
1416 "char encoding UCS2 not supported\n");
1417 break;
1418 case XML_CHAR_ENCODING_8859_1:
1419 case XML_CHAR_ENCODING_8859_2:
1420 case XML_CHAR_ENCODING_8859_3:
1421 case XML_CHAR_ENCODING_8859_4:
1422 case XML_CHAR_ENCODING_8859_5:
1423 case XML_CHAR_ENCODING_8859_6:
1424 case XML_CHAR_ENCODING_8859_7:
1425 case XML_CHAR_ENCODING_8859_8:
1426 case XML_CHAR_ENCODING_8859_9:
1427 /*
1428 * We used to keep the internal content in the
1429 * document encoding however this turns being unmaintainable
1430 * So xmlGetCharEncodingHandler() will return non-null
1431 * values for this now.
1432 */
1433 if ((ctxt->inputNr == 1) &&
1434 (ctxt->encoding == NULL) &&
1435 (ctxt->input->encoding != NULL)) {
1436 ctxt->encoding = xmlStrdup(ctxt->input->encoding);
1437 }
1438 ctxt->charset = enc;
1439 return(0);
1440 case XML_CHAR_ENCODING_2022_JP:
1441 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1442 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1443 ctxt->sax->error(ctxt->userData,
1444 "char encoding ISO-2022-JPnot supported\n");
1445 break;
1446 case XML_CHAR_ENCODING_SHIFT_JIS:
1447 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1448 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1449 ctxt->sax->error(ctxt->userData,
1450 "char encoding Shift_JIS not supported\n");
1451 break;
1452 case XML_CHAR_ENCODING_EUC_JP:
1453 ctxt->errNo = XML_ERR_UNSUPPORTED_ENCODING;
1454 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1455 ctxt->sax->error(ctxt->userData,
1456 "char encoding EUC-JPnot supported\n");
1457 break;
1458 }
1459 }
1460 if (handler == NULL)
1461 return(-1);
1462 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1463 return(xmlSwitchToEncoding(ctxt, handler));
1464}
1465
1466/**
1467 * xmlSwitchToEncoding:
1468 * @ctxt: the parser context
1469 * @handler: the encoding handler
1470 *
1471 * change the input functions when discovering the character encoding
1472 * of a given entity.
1473 *
1474 * Returns 0 in case of success, -1 otherwise
1475 */
1476int
1477xmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler)
1478{
1479 int nbchars;
1480
1481 if (handler != NULL) {
1482 if (ctxt->input != NULL) {
1483 if (ctxt->input->buf != NULL) {
1484 if (ctxt->input->buf->encoder != NULL) {
Daniel Veillard878eab02002-02-19 13:46:09 +00001485 /*
1486 * Check in case the auto encoding detetection triggered
1487 * in already.
1488 */
Owen Taylor3473f882001-02-23 17:55:21 +00001489 if (ctxt->input->buf->encoder == handler)
1490 return(0);
Daniel Veillard878eab02002-02-19 13:46:09 +00001491
1492 /*
1493 * "UTF-16" can be used for both LE and BE
Daniel Veillard878eab02002-02-19 13:46:09 +00001494 if ((!xmlStrncmp(BAD_CAST ctxt->input->buf->encoder->name,
1495 BAD_CAST "UTF-16", 6)) &&
1496 (!xmlStrncmp(BAD_CAST handler->name,
1497 BAD_CAST "UTF-16", 6))) {
1498 return(0);
1499 }
Daniel Veillarda6874ca2003-07-29 16:47:24 +00001500 */
Daniel Veillard878eab02002-02-19 13:46:09 +00001501
Owen Taylor3473f882001-02-23 17:55:21 +00001502 /*
1503 * Note: this is a bit dangerous, but that's what it
1504 * takes to use nearly compatible signature for different
1505 * encodings.
1506 */
1507 xmlCharEncCloseFunc(ctxt->input->buf->encoder);
1508 ctxt->input->buf->encoder = handler;
1509 return(0);
1510 }
1511 ctxt->input->buf->encoder = handler;
1512
1513 /*
1514 * Is there already some content down the pipe to convert ?
1515 */
1516 if ((ctxt->input->buf->buffer != NULL) &&
1517 (ctxt->input->buf->buffer->use > 0)) {
1518 int processed;
1519
1520 /*
1521 * Specific handling of the Byte Order Mark for
1522 * UTF-16
1523 */
1524 if ((handler->name != NULL) &&
1525 (!strcmp(handler->name, "UTF-16LE")) &&
1526 (ctxt->input->cur[0] == 0xFF) &&
1527 (ctxt->input->cur[1] == 0xFE)) {
1528 ctxt->input->cur += 2;
1529 }
1530 if ((handler->name != NULL) &&
1531 (!strcmp(handler->name, "UTF-16BE")) &&
1532 (ctxt->input->cur[0] == 0xFE) &&
1533 (ctxt->input->cur[1] == 0xFF)) {
1534 ctxt->input->cur += 2;
1535 }
Daniel Veillard87a764e2001-06-20 17:41:10 +00001536 /*
1537 * Errata on XML-1.0 June 20 2001
1538 * Specific handling of the Byte Order Mark for
1539 * UTF-8
1540 */
1541 if ((handler->name != NULL) &&
1542 (!strcmp(handler->name, "UTF-8")) &&
1543 (ctxt->input->cur[0] == 0xEF) &&
1544 (ctxt->input->cur[1] == 0xBB) &&
Daniel Veillard7dd05702001-10-04 14:25:12 +00001545 (ctxt->input->cur[2] == 0xBF)) {
Daniel Veillard87a764e2001-06-20 17:41:10 +00001546 ctxt->input->cur += 3;
1547 }
Owen Taylor3473f882001-02-23 17:55:21 +00001548
1549 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001550 * Shrink the current input buffer.
Owen Taylor3473f882001-02-23 17:55:21 +00001551 * Move it as the raw buffer and create a new input buffer
1552 */
1553 processed = ctxt->input->cur - ctxt->input->base;
1554 xmlBufferShrink(ctxt->input->buf->buffer, processed);
1555 ctxt->input->buf->raw = ctxt->input->buf->buffer;
1556 ctxt->input->buf->buffer = xmlBufferCreate();
1557
1558 if (ctxt->html) {
1559 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001560 * convert as much as possible of the buffer
Owen Taylor3473f882001-02-23 17:55:21 +00001561 */
1562 nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder,
1563 ctxt->input->buf->buffer,
1564 ctxt->input->buf->raw);
1565 } else {
1566 /*
1567 * convert just enough to get
1568 * '<?xml version="1.0" encoding="xxx"?>'
1569 * parsed with the autodetected encoding
1570 * into the parser reading buffer.
1571 */
1572 nbchars = xmlCharEncFirstLine(ctxt->input->buf->encoder,
1573 ctxt->input->buf->buffer,
1574 ctxt->input->buf->raw);
1575 }
1576 if (nbchars < 0) {
1577 xmlGenericError(xmlGenericErrorContext,
1578 "xmlSwitchToEncoding: encoder error\n");
1579 return(-1);
1580 }
1581 ctxt->input->base =
1582 ctxt->input->cur = ctxt->input->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001583 ctxt->input->end =
1584 &ctxt->input->base[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001585
1586 }
1587 return(0);
1588 } else {
1589 if ((ctxt->input->length == 0) || (ctxt->input->buf == NULL)) {
1590 /*
1591 * When parsing a static memory array one must know the
1592 * size to be able to convert the buffer.
1593 */
1594 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1595 ctxt->sax->error(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001596 "xmlSwitchToEncoding : no input\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001597 return(-1);
1598 } else {
1599 int processed;
1600
1601 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001602 * Shrink the current input buffer.
Owen Taylor3473f882001-02-23 17:55:21 +00001603 * Move it as the raw buffer and create a new input buffer
1604 */
1605 processed = ctxt->input->cur - ctxt->input->base;
1606
1607 ctxt->input->buf->raw = xmlBufferCreate();
1608 xmlBufferAdd(ctxt->input->buf->raw, ctxt->input->cur,
1609 ctxt->input->length - processed);
1610 ctxt->input->buf->buffer = xmlBufferCreate();
1611
1612 /*
1613 * convert as much as possible of the raw input
1614 * to the parser reading buffer.
1615 */
1616 nbchars = xmlCharEncInFunc(ctxt->input->buf->encoder,
1617 ctxt->input->buf->buffer,
1618 ctxt->input->buf->raw);
1619 if (nbchars < 0) {
1620 xmlGenericError(xmlGenericErrorContext,
1621 "xmlSwitchToEncoding: encoder error\n");
1622 return(-1);
1623 }
1624
1625 /*
1626 * Conversion succeeded, get rid of the old buffer
1627 */
1628 if ((ctxt->input->free != NULL) &&
1629 (ctxt->input->base != NULL))
1630 ctxt->input->free((xmlChar *) ctxt->input->base);
1631 ctxt->input->base =
1632 ctxt->input->cur = ctxt->input->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001633 ctxt->input->end =
1634 &ctxt->input->base[ctxt->input->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001635 }
1636 }
1637 } else {
1638 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1639 ctxt->sax->error(ctxt->userData,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001640 "xmlSwitchToEncoding : no input\n");
Owen Taylor3473f882001-02-23 17:55:21 +00001641 return(-1);
1642 }
1643 /*
1644 * The parsing is now done in UTF8 natively
1645 */
1646 ctxt->charset = XML_CHAR_ENCODING_UTF8;
1647 } else
1648 return(-1);
1649 return(0);
1650
1651}
1652
1653/************************************************************************
1654 * *
1655 * Commodity functions to handle entities processing *
1656 * *
1657 ************************************************************************/
1658
1659/**
1660 * xmlFreeInputStream:
1661 * @input: an xmlParserInputPtr
1662 *
1663 * Free up an input stream.
1664 */
1665void
1666xmlFreeInputStream(xmlParserInputPtr input) {
1667 if (input == NULL) return;
1668
1669 if (input->filename != NULL) xmlFree((char *) input->filename);
1670 if (input->directory != NULL) xmlFree((char *) input->directory);
1671 if (input->encoding != NULL) xmlFree((char *) input->encoding);
1672 if (input->version != NULL) xmlFree((char *) input->version);
1673 if ((input->free != NULL) && (input->base != NULL))
1674 input->free((xmlChar *) input->base);
1675 if (input->buf != NULL)
1676 xmlFreeParserInputBuffer(input->buf);
Owen Taylor3473f882001-02-23 17:55:21 +00001677 xmlFree(input);
1678}
1679
1680/**
1681 * xmlNewInputStream:
1682 * @ctxt: an XML parser context
1683 *
1684 * Create a new input stream structure
1685 * Returns the new input stream or NULL
1686 */
1687xmlParserInputPtr
1688xmlNewInputStream(xmlParserCtxtPtr ctxt) {
1689 xmlParserInputPtr input;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001690 static int id = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001691
1692 input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput));
1693 if (input == NULL) {
1694 if (ctxt != NULL) {
1695 ctxt->errNo = XML_ERR_NO_MEMORY;
1696 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1697 ctxt->sax->error(ctxt->userData,
1698 "malloc: couldn't allocate a new input stream\n");
1699 ctxt->errNo = XML_ERR_NO_MEMORY;
1700 }
1701 return(NULL);
1702 }
1703 memset(input, 0, sizeof(xmlParserInput));
1704 input->line = 1;
1705 input->col = 1;
1706 input->standalone = -1;
Daniel Veillardbdbe0d42003-09-14 19:56:14 +00001707 /*
1708 * we don't care about thread reentrancy unicity for a single
1709 * parser context (and hence thread) is sufficient.
1710 */
1711 input->id = id++;
Owen Taylor3473f882001-02-23 17:55:21 +00001712 return(input);
1713}
1714
1715/**
1716 * xmlNewIOInputStream:
1717 * @ctxt: an XML parser context
1718 * @input: an I/O Input
1719 * @enc: the charset encoding if known
1720 *
1721 * Create a new input stream structure encapsulating the @input into
1722 * a stream suitable for the parser.
1723 *
1724 * Returns the new input stream or NULL
1725 */
1726xmlParserInputPtr
1727xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr input,
1728 xmlCharEncoding enc) {
1729 xmlParserInputPtr inputStream;
1730
1731 if (xmlParserDebugEntities)
1732 xmlGenericError(xmlGenericErrorContext, "new input from I/O\n");
1733 inputStream = xmlNewInputStream(ctxt);
1734 if (inputStream == NULL) {
1735 return(NULL);
1736 }
1737 inputStream->filename = NULL;
1738 inputStream->buf = input;
1739 inputStream->base = inputStream->buf->buffer->content;
1740 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001741 inputStream->end = &inputStream->base[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001742 if (enc != XML_CHAR_ENCODING_NONE) {
1743 xmlSwitchEncoding(ctxt, enc);
1744 }
1745
1746 return(inputStream);
1747}
1748
1749/**
1750 * xmlNewEntityInputStream:
1751 * @ctxt: an XML parser context
1752 * @entity: an Entity pointer
1753 *
1754 * Create a new input stream based on an xmlEntityPtr
1755 *
1756 * Returns the new input stream or NULL
1757 */
1758xmlParserInputPtr
1759xmlNewEntityInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr entity) {
1760 xmlParserInputPtr input;
1761
1762 if (entity == NULL) {
1763 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
1764 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1765 ctxt->sax->error(ctxt->userData,
1766 "internal: xmlNewEntityInputStream entity = NULL\n");
1767 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
1768 return(NULL);
1769 }
1770 if (xmlParserDebugEntities)
1771 xmlGenericError(xmlGenericErrorContext,
1772 "new input from entity: %s\n", entity->name);
1773 if (entity->content == NULL) {
1774 switch (entity->etype) {
1775 case XML_EXTERNAL_GENERAL_UNPARSED_ENTITY:
1776 ctxt->errNo = XML_ERR_UNPARSED_ENTITY;
1777 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1778 ctxt->sax->error(ctxt->userData,
1779 "xmlNewEntityInputStream unparsed entity !\n");
1780 break;
1781 case XML_EXTERNAL_GENERAL_PARSED_ENTITY:
1782 case XML_EXTERNAL_PARAMETER_ENTITY:
1783 return(xmlLoadExternalEntity((char *) entity->URI,
1784 (char *) entity->ExternalID, ctxt));
1785 case XML_INTERNAL_GENERAL_ENTITY:
1786 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1787 ctxt->sax->error(ctxt->userData,
1788 "Internal entity %s without content !\n", entity->name);
1789 break;
1790 case XML_INTERNAL_PARAMETER_ENTITY:
1791 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
1792 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1793 ctxt->sax->error(ctxt->userData,
1794 "Internal parameter entity %s without content !\n", entity->name);
1795 break;
1796 case XML_INTERNAL_PREDEFINED_ENTITY:
1797 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
1798 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1799 ctxt->sax->error(ctxt->userData,
1800 "Predefined entity %s without content !\n", entity->name);
1801 break;
1802 }
1803 return(NULL);
1804 }
1805 input = xmlNewInputStream(ctxt);
1806 if (input == NULL) {
1807 return(NULL);
1808 }
1809 input->filename = (char *) entity->URI;
1810 input->base = entity->content;
1811 input->cur = entity->content;
1812 input->length = entity->length;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001813 input->end = &entity->content[input->length];
Owen Taylor3473f882001-02-23 17:55:21 +00001814 return(input);
1815}
1816
1817/**
1818 * xmlNewStringInputStream:
1819 * @ctxt: an XML parser context
1820 * @buffer: an memory buffer
1821 *
1822 * Create a new input stream based on a memory buffer.
1823 * Returns the new input stream
1824 */
1825xmlParserInputPtr
1826xmlNewStringInputStream(xmlParserCtxtPtr ctxt, const xmlChar *buffer) {
1827 xmlParserInputPtr input;
1828
1829 if (buffer == NULL) {
1830 ctxt->errNo = XML_ERR_INTERNAL_ERROR;
1831 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
1832 ctxt->sax->error(ctxt->userData,
1833 "internal: xmlNewStringInputStream string = NULL\n");
1834 return(NULL);
1835 }
1836 if (xmlParserDebugEntities)
1837 xmlGenericError(xmlGenericErrorContext,
1838 "new fixed input: %.30s\n", buffer);
1839 input = xmlNewInputStream(ctxt);
1840 if (input == NULL) {
1841 return(NULL);
1842 }
1843 input->base = buffer;
1844 input->cur = buffer;
1845 input->length = xmlStrlen(buffer);
Daniel Veillard48b2f892001-02-25 16:11:03 +00001846 input->end = &buffer[input->length];
Owen Taylor3473f882001-02-23 17:55:21 +00001847 return(input);
1848}
1849
1850/**
1851 * xmlNewInputFromFile:
1852 * @ctxt: an XML parser context
1853 * @filename: the filename to use as entity
1854 *
1855 * Create a new input stream based on a file.
1856 *
1857 * Returns the new input stream or NULL in case of error
1858 */
1859xmlParserInputPtr
1860xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) {
1861 xmlParserInputBufferPtr buf;
1862 xmlParserInputPtr inputStream;
1863 char *directory = NULL;
1864 xmlChar *URI = NULL;
1865
1866 if (xmlParserDebugEntities)
1867 xmlGenericError(xmlGenericErrorContext,
1868 "new input from file: %s\n", filename);
1869 if (ctxt == NULL) return(NULL);
1870 buf = xmlParserInputBufferCreateFilename(filename, XML_CHAR_ENCODING_NONE);
1871 if (buf == NULL)
1872 return(NULL);
1873
1874 URI = xmlStrdup((xmlChar *) filename);
1875 directory = xmlParserGetDirectory((const char *) URI);
1876
1877 inputStream = xmlNewInputStream(ctxt);
1878 if (inputStream == NULL) {
1879 if (directory != NULL) xmlFree((char *) directory);
1880 if (URI != NULL) xmlFree((char *) URI);
1881 return(NULL);
1882 }
1883
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00001884 inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) URI);
Daniel Veillarda66b1d12003-09-17 20:54:38 +00001885 if (URI != NULL) xmlFree((char *) URI);
Owen Taylor3473f882001-02-23 17:55:21 +00001886 inputStream->directory = directory;
1887 inputStream->buf = buf;
1888
1889 inputStream->base = inputStream->buf->buffer->content;
1890 inputStream->cur = inputStream->buf->buffer->content;
Daniel Veillard48b2f892001-02-25 16:11:03 +00001891 inputStream->end = &inputStream->base[inputStream->buf->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00001892 if ((ctxt->directory == NULL) && (directory != NULL))
1893 ctxt->directory = (char *) xmlStrdup((const xmlChar *) directory);
1894 return(inputStream);
1895}
1896
1897/************************************************************************
1898 * *
1899 * Commodity functions to handle parser contexts *
1900 * *
1901 ************************************************************************/
1902
1903/**
1904 * xmlInitParserCtxt:
1905 * @ctxt: an XML parser context
1906 *
1907 * Initialize a parser context
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001908 *
1909 * Returns 0 in case of success and -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +00001910 */
1911
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001912int
Owen Taylor3473f882001-02-23 17:55:21 +00001913xmlInitParserCtxt(xmlParserCtxtPtr ctxt)
1914{
Daniel Veillard5d96fff2001-08-31 14:55:30 +00001915 if(ctxt==NULL) {
1916 xmlGenericError(xmlGenericErrorContext,
1917 "xmlInitParserCtxt: NULL context given\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001918 return(-1);
Daniel Veillard5d96fff2001-08-31 14:55:30 +00001919 }
1920
Owen Taylor3473f882001-02-23 17:55:21 +00001921 xmlDefaultSAXHandlerInit();
1922
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001923 ctxt->dict = xmlDictCreate();
1924 if (ctxt->dict == NULL) {
1925 xmlGenericError(xmlGenericErrorContext,
1926 "xmlInitParserCtxt: out of memory\n");
1927 return(-1);
1928 }
William M. Brack8b2c7f12002-11-22 05:07:29 +00001929 ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler));
1930 if (ctxt->sax == NULL) {
Owen Taylor3473f882001-02-23 17:55:21 +00001931 xmlGenericError(xmlGenericErrorContext,
1932 "xmlInitParserCtxt: out of memory\n");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001933 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001934 }
1935 else
Daniel Veillard092643b2003-09-25 14:29:29 +00001936 xmlSAXVersion(ctxt->sax, 2);
Owen Taylor3473f882001-02-23 17:55:21 +00001937
Daniel Veillard6155d8a2003-08-19 15:01:28 +00001938 ctxt->maxatts = 0;
1939 ctxt->atts = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00001940 /* Allocate the Input stack */
1941 ctxt->inputTab = (xmlParserInputPtr *)
1942 xmlMalloc(5 * sizeof(xmlParserInputPtr));
1943 if (ctxt->inputTab == NULL) {
1944 xmlGenericError(xmlGenericErrorContext,
1945 "xmlInitParserCtxt: out of memory\n");
1946 ctxt->inputNr = 0;
1947 ctxt->inputMax = 0;
1948 ctxt->input = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001949 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001950 }
1951 ctxt->inputNr = 0;
1952 ctxt->inputMax = 5;
1953 ctxt->input = NULL;
1954
1955 ctxt->version = NULL;
1956 ctxt->encoding = NULL;
1957 ctxt->standalone = -1;
1958 ctxt->hasExternalSubset = 0;
1959 ctxt->hasPErefs = 0;
1960 ctxt->html = 0;
1961 ctxt->external = 0;
1962 ctxt->instate = XML_PARSER_START;
1963 ctxt->token = 0;
1964 ctxt->directory = NULL;
1965
1966 /* Allocate the Node stack */
1967 ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr));
1968 if (ctxt->nodeTab == NULL) {
1969 xmlGenericError(xmlGenericErrorContext,
1970 "xmlInitParserCtxt: out of memory\n");
1971 ctxt->nodeNr = 0;
1972 ctxt->nodeMax = 0;
1973 ctxt->node = NULL;
1974 ctxt->inputNr = 0;
1975 ctxt->inputMax = 0;
1976 ctxt->input = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001977 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001978 }
1979 ctxt->nodeNr = 0;
1980 ctxt->nodeMax = 10;
1981 ctxt->node = NULL;
1982
1983 /* Allocate the Name stack */
Daniel Veillard2fdbd322003-08-18 12:15:38 +00001984 ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
Owen Taylor3473f882001-02-23 17:55:21 +00001985 if (ctxt->nameTab == NULL) {
1986 xmlGenericError(xmlGenericErrorContext,
1987 "xmlInitParserCtxt: out of memory\n");
1988 ctxt->nodeNr = 0;
1989 ctxt->nodeMax = 0;
1990 ctxt->node = NULL;
1991 ctxt->inputNr = 0;
1992 ctxt->inputMax = 0;
1993 ctxt->input = NULL;
1994 ctxt->nameNr = 0;
1995 ctxt->nameMax = 0;
1996 ctxt->name = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001997 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001998 }
1999 ctxt->nameNr = 0;
2000 ctxt->nameMax = 10;
2001 ctxt->name = NULL;
2002
2003 /* Allocate the space stack */
2004 ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int));
2005 if (ctxt->spaceTab == NULL) {
2006 xmlGenericError(xmlGenericErrorContext,
2007 "xmlInitParserCtxt: out of memory\n");
2008 ctxt->nodeNr = 0;
2009 ctxt->nodeMax = 0;
2010 ctxt->node = NULL;
2011 ctxt->inputNr = 0;
2012 ctxt->inputMax = 0;
2013 ctxt->input = NULL;
2014 ctxt->nameNr = 0;
2015 ctxt->nameMax = 0;
2016 ctxt->name = NULL;
2017 ctxt->spaceNr = 0;
2018 ctxt->spaceMax = 0;
2019 ctxt->space = NULL;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002020 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002021 }
2022 ctxt->spaceNr = 1;
2023 ctxt->spaceMax = 10;
2024 ctxt->spaceTab[0] = -1;
2025 ctxt->space = &ctxt->spaceTab[0];
Owen Taylor3473f882001-02-23 17:55:21 +00002026 ctxt->userData = ctxt;
2027 ctxt->myDoc = NULL;
2028 ctxt->wellFormed = 1;
Daniel Veillard3b7840c2003-09-11 23:42:01 +00002029 ctxt->nsWellFormed = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002030 ctxt->valid = 1;
2031 ctxt->loadsubset = xmlLoadExtDtdDefaultValue;
2032 ctxt->validate = xmlDoValidityCheckingDefaultValue;
2033 ctxt->pedantic = xmlPedanticParserDefaultValue;
Daniel Veillarda53c6882001-07-25 17:18:57 +00002034 ctxt->linenumbers = xmlLineNumbersDefaultValue;
Owen Taylor3473f882001-02-23 17:55:21 +00002035 ctxt->keepBlanks = xmlKeepBlanksDefaultValue;
Daniel Veillard16698282001-09-14 10:29:27 +00002036 if (ctxt->keepBlanks == 0)
Daniel Veillard11476b42003-09-26 14:51:39 +00002037 ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
Daniel Veillard16698282001-09-14 10:29:27 +00002038
Owen Taylor3473f882001-02-23 17:55:21 +00002039 ctxt->vctxt.userData = ctxt;
Daniel Veillard4e1b26c2002-02-03 20:13:06 +00002040 ctxt->vctxt.error = xmlParserValidityError;
2041 ctxt->vctxt.warning = xmlParserValidityWarning;
Owen Taylor3473f882001-02-23 17:55:21 +00002042 if (ctxt->validate) {
Owen Taylor3473f882001-02-23 17:55:21 +00002043 if (xmlGetWarningsDefaultValue == 0)
2044 ctxt->vctxt.warning = NULL;
2045 else
2046 ctxt->vctxt.warning = xmlParserValidityWarning;
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00002047 ctxt->vctxt.nodeMax = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002048 }
2049 ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue;
2050 ctxt->record_info = 0;
2051 ctxt->nbChars = 0;
2052 ctxt->checkIndex = 0;
2053 ctxt->inSubset = 0;
2054 ctxt->errNo = XML_ERR_OK;
2055 ctxt->depth = 0;
2056 ctxt->charset = XML_CHAR_ENCODING_UTF8;
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00002057 ctxt->catalogs = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002058 xmlInitNodeInfoSeq(&ctxt->node_seq);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002059 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00002060}
2061
2062/**
2063 * xmlFreeParserCtxt:
2064 * @ctxt: an XML parser context
2065 *
2066 * Free all the memory used by a parser context. However the parsed
2067 * document in ctxt->myDoc is not freed.
2068 */
2069
2070void
2071xmlFreeParserCtxt(xmlParserCtxtPtr ctxt)
2072{
2073 xmlParserInputPtr input;
Owen Taylor3473f882001-02-23 17:55:21 +00002074
2075 if (ctxt == NULL) return;
2076
2077 while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
2078 xmlFreeInputStream(input);
2079 }
Owen Taylor3473f882001-02-23 17:55:21 +00002080 if (ctxt->spaceTab != NULL) xmlFree(ctxt->spaceTab);
Igor Zlatkovicd37c1392003-08-28 10:34:33 +00002081 if (ctxt->nameTab != NULL) xmlFree((xmlChar * *)ctxt->nameTab);
Owen Taylor3473f882001-02-23 17:55:21 +00002082 if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab);
2083 if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab);
2084 if (ctxt->version != NULL) xmlFree((char *) ctxt->version);
2085 if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding);
Owen Taylor3473f882001-02-23 17:55:21 +00002086 if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI);
2087 if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem);
Daniel Veillard81273902003-09-30 00:43:48 +00002088#ifdef LIBXML_SAX1_ENABLED
Daniel Veillard092643b2003-09-25 14:29:29 +00002089 if ((ctxt->sax != NULL) &&
2090 (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler))
Daniel Veillard81273902003-09-30 00:43:48 +00002091#else
2092 if (ctxt->sax != NULL)
2093#endif /* LIBXML_SAX1_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002094 xmlFree(ctxt->sax);
2095 if (ctxt->directory != NULL) xmlFree((char *) ctxt->directory);
Daniel Veillarda9142e72001-06-19 11:07:54 +00002096 if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab);
Igor Zlatkovicd37c1392003-08-28 10:34:33 +00002097 if (ctxt->atts != NULL) xmlFree((xmlChar * *)ctxt->atts);
Daniel Veillard2fdbd322003-08-18 12:15:38 +00002098 if (ctxt->dict != NULL) xmlDictFree(ctxt->dict);
Daniel Veillard0fb18932003-09-07 09:14:37 +00002099 if (ctxt->nsTab != NULL) xmlFree(ctxt->nsTab);
Daniel Veillarde57ec792003-09-10 10:50:59 +00002100 if (ctxt->pushTab != NULL) xmlFree(ctxt->pushTab);
2101 if (ctxt->attallocs != NULL) xmlFree(ctxt->attallocs);
2102 if (ctxt->attsDefault != NULL)
2103 xmlHashFree(ctxt->attsDefault, (xmlHashDeallocator) xmlFree);
Daniel Veillard8e36e6a2003-09-10 10:50:59 +00002104 if (ctxt->attsSpecial != NULL)
2105 xmlHashFree(ctxt->attsSpecial, NULL);
Daniel Veillard9f7eb0b2003-09-17 10:26:25 +00002106 if (ctxt->freeElems != NULL) {
2107 xmlNodePtr cur, next;
2108
2109 cur = ctxt->freeElems;
2110 while (cur != NULL) {
2111 next = cur->next;
2112 xmlFree(cur);
2113 cur = next;
2114 }
2115 }
2116 if (ctxt->freeAttrs != NULL) {
2117 xmlAttrPtr cur, next;
2118
2119 cur = ctxt->freeAttrs;
2120 while (cur != NULL) {
2121 next = cur->next;
2122 xmlFree(cur);
2123 cur = next;
2124 }
2125 }
Daniel Veillard2b8c4a12003-10-02 22:28:19 +00002126 /*
2127 * cleanup the error strings
2128 */
2129 if (ctxt->lastError.message != NULL)
2130 xmlFree(ctxt->lastError.message);
2131 if (ctxt->lastError.file != NULL)
2132 xmlFree(ctxt->lastError.file);
2133 if (ctxt->lastError.str1 != NULL)
2134 xmlFree(ctxt->lastError.str1);
2135 if (ctxt->lastError.str2 != NULL)
2136 xmlFree(ctxt->lastError.str2);
2137 if (ctxt->lastError.str3 != NULL)
2138 xmlFree(ctxt->lastError.str3);
Daniel Veillard0fb18932003-09-07 09:14:37 +00002139
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00002140#ifdef LIBXML_CATALOG_ENABLED
2141 if (ctxt->catalogs != NULL)
2142 xmlCatalogFreeLocal(ctxt->catalogs);
2143#endif
Owen Taylor3473f882001-02-23 17:55:21 +00002144 xmlFree(ctxt);
2145}
2146
2147/**
2148 * xmlNewParserCtxt:
2149 *
2150 * Allocate and initialize a new parser context.
2151 *
2152 * Returns the xmlParserCtxtPtr or NULL
2153 */
2154
2155xmlParserCtxtPtr
2156xmlNewParserCtxt()
2157{
2158 xmlParserCtxtPtr ctxt;
2159
2160 ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
2161 if (ctxt == NULL) {
2162 xmlGenericError(xmlGenericErrorContext,
2163 "xmlNewParserCtxt : cannot allocate context\n");
Daniel Veillard3487c8d2002-09-05 11:33:25 +00002164 xmlGenericError(xmlGenericErrorContext, "malloc failed");
Owen Taylor3473f882001-02-23 17:55:21 +00002165 return(NULL);
2166 }
2167 memset(ctxt, 0, sizeof(xmlParserCtxt));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002168 if (xmlInitParserCtxt(ctxt) < 0) {
2169 xmlFreeParserCtxt(ctxt);
2170 return(NULL);
2171 }
Owen Taylor3473f882001-02-23 17:55:21 +00002172 return(ctxt);
2173}
2174
2175/************************************************************************
2176 * *
2177 * Handling of node informations *
2178 * *
2179 ************************************************************************/
2180
2181/**
2182 * xmlClearParserCtxt:
2183 * @ctxt: an XML parser context
2184 *
2185 * Clear (release owned resources) and reinitialize a parser context
2186 */
2187
2188void
2189xmlClearParserCtxt(xmlParserCtxtPtr ctxt)
2190{
Daniel Veillard5d96fff2001-08-31 14:55:30 +00002191 if (ctxt==NULL)
2192 return;
Owen Taylor3473f882001-02-23 17:55:21 +00002193 xmlClearNodeInfoSeq(&ctxt->node_seq);
2194 xmlInitParserCtxt(ctxt);
2195}
2196
2197/**
2198 * xmlParserFindNodeInfo:
Daniel Veillard01c13b52002-12-10 15:19:08 +00002199 * @ctx: an XML parser context
Owen Taylor3473f882001-02-23 17:55:21 +00002200 * @node: an XML node within the tree
2201 *
2202 * Find the parser node info struct for a given node
2203 *
2204 * Returns an xmlParserNodeInfo block pointer or NULL
2205 */
Daniel Veillard963d2ae2002-01-20 22:08:18 +00002206const xmlParserNodeInfo* xmlParserFindNodeInfo(const xmlParserCtxtPtr ctx,
2207 const xmlNodePtr node)
Owen Taylor3473f882001-02-23 17:55:21 +00002208{
2209 unsigned long pos;
2210
2211 /* Find position where node should be at */
2212 pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node);
Daniel Veillardb1d62872001-09-21 09:47:08 +00002213 if (pos < ctx->node_seq.length && ctx->node_seq.buffer[pos].node == node)
Owen Taylor3473f882001-02-23 17:55:21 +00002214 return &ctx->node_seq.buffer[pos];
2215 else
2216 return NULL;
2217}
2218
2219
2220/**
2221 * xmlInitNodeInfoSeq:
2222 * @seq: a node info sequence pointer
2223 *
2224 * -- Initialize (set to initial state) node info sequence
2225 */
2226void
2227xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
2228{
2229 seq->length = 0;
2230 seq->maximum = 0;
2231 seq->buffer = NULL;
2232}
2233
2234/**
2235 * xmlClearNodeInfoSeq:
2236 * @seq: a node info sequence pointer
2237 *
2238 * -- Clear (release memory and reinitialize) node
2239 * info sequence
2240 */
2241void
2242xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq)
2243{
2244 if ( seq->buffer != NULL )
2245 xmlFree(seq->buffer);
2246 xmlInitNodeInfoSeq(seq);
2247}
2248
2249
2250/**
2251 * xmlParserFindNodeInfoIndex:
2252 * @seq: a node info sequence pointer
2253 * @node: an XML node pointer
2254 *
2255 *
2256 * xmlParserFindNodeInfoIndex : Find the index that the info record for
2257 * the given node is or should be at in a sorted sequence
2258 *
2259 * Returns a long indicating the position of the record
2260 */
Daniel Veillard963d2ae2002-01-20 22:08:18 +00002261unsigned long xmlParserFindNodeInfoIndex(const xmlParserNodeInfoSeqPtr seq,
2262 const xmlNodePtr node)
Owen Taylor3473f882001-02-23 17:55:21 +00002263{
2264 unsigned long upper, lower, middle;
2265 int found = 0;
2266
2267 /* Do a binary search for the key */
2268 lower = 1;
2269 upper = seq->length;
2270 middle = 0;
2271 while ( lower <= upper && !found) {
2272 middle = lower + (upper - lower) / 2;
2273 if ( node == seq->buffer[middle - 1].node )
2274 found = 1;
2275 else if ( node < seq->buffer[middle - 1].node )
2276 upper = middle - 1;
2277 else
2278 lower = middle + 1;
2279 }
2280
2281 /* Return position */
2282 if ( middle == 0 || seq->buffer[middle - 1].node < node )
2283 return middle;
2284 else
2285 return middle - 1;
2286}
2287
2288
2289/**
2290 * xmlParserAddNodeInfo:
2291 * @ctxt: an XML parser context
2292 * @info: a node info sequence pointer
2293 *
2294 * Insert node info record into the sorted sequence
2295 */
2296void
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002297xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt,
Daniel Veillard963d2ae2002-01-20 22:08:18 +00002298 const xmlParserNodeInfoPtr info)
Owen Taylor3473f882001-02-23 17:55:21 +00002299{
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002300 unsigned long pos;
Owen Taylor3473f882001-02-23 17:55:21 +00002301
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002302 /* Find pos and check to see if node is already in the sequence */
William M. Brack78637da2003-07-31 14:47:38 +00002303 pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr)
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002304 info->node);
2305 if (pos < ctxt->node_seq.length
2306 && ctxt->node_seq.buffer[pos].node == info->node) {
2307 ctxt->node_seq.buffer[pos] = *info;
Owen Taylor3473f882001-02-23 17:55:21 +00002308 }
2309
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002310 /* Otherwise, we need to add new node to buffer */
2311 else {
2312 if (ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) {
2313 xmlParserNodeInfo *tmp_buffer;
2314 unsigned int byte_size;
Owen Taylor3473f882001-02-23 17:55:21 +00002315
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002316 if (ctxt->node_seq.maximum == 0)
2317 ctxt->node_seq.maximum = 2;
2318 byte_size = (sizeof(*ctxt->node_seq.buffer) *
2319 (2 * ctxt->node_seq.maximum));
2320
2321 if (ctxt->node_seq.buffer == NULL)
Daniel Veillardc4f65ab2003-04-21 23:07:45 +00002322 tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size);
Daniel Veillardc8c7be42002-01-23 17:53:44 +00002323 else
2324 tmp_buffer =
2325 (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer,
2326 byte_size);
2327
2328 if (tmp_buffer == NULL) {
2329 if ((ctxt->sax != NULL) && (ctxt->sax->error != NULL))
2330 ctxt->sax->error(ctxt->userData, "Out of memory\n");
2331 ctxt->errNo = XML_ERR_NO_MEMORY;
2332 return;
2333 }
2334 ctxt->node_seq.buffer = tmp_buffer;
2335 ctxt->node_seq.maximum *= 2;
2336 }
2337
2338 /* If position is not at end, move elements out of the way */
2339 if (pos != ctxt->node_seq.length) {
2340 unsigned long i;
2341
2342 for (i = ctxt->node_seq.length; i > pos; i--)
2343 ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1];
2344 }
2345
2346 /* Copy element and increase length */
2347 ctxt->node_seq.buffer[pos] = *info;
2348 ctxt->node_seq.length++;
Owen Taylor3473f882001-02-23 17:55:21 +00002349 }
Owen Taylor3473f882001-02-23 17:55:21 +00002350}
2351
2352/************************************************************************
2353 * *
Daniel Veillarda53c6882001-07-25 17:18:57 +00002354 * Defaults settings *
2355 * *
2356 ************************************************************************/
2357/**
2358 * xmlPedanticParserDefault:
2359 * @val: int 0 or 1
2360 *
2361 * Set and return the previous value for enabling pedantic warnings.
2362 *
2363 * Returns the last value for 0 for no substitution, 1 for substitution.
2364 */
2365
2366int
2367xmlPedanticParserDefault(int val) {
2368 int old = xmlPedanticParserDefaultValue;
2369
2370 xmlPedanticParserDefaultValue = val;
2371 return(old);
2372}
2373
2374/**
2375 * xmlLineNumbersDefault:
2376 * @val: int 0 or 1
2377 *
2378 * Set and return the previous value for enabling line numbers in elements
2379 * contents. This may break on old application and is turned off by default.
2380 *
2381 * Returns the last value for 0 for no substitution, 1 for substitution.
2382 */
2383
2384int
2385xmlLineNumbersDefault(int val) {
2386 int old = xmlLineNumbersDefaultValue;
2387
2388 xmlLineNumbersDefaultValue = val;
2389 return(old);
2390}
2391
2392/**
2393 * xmlSubstituteEntitiesDefault:
2394 * @val: int 0 or 1
2395 *
2396 * Set and return the previous value for default entity support.
2397 * Initially the parser always keep entity references instead of substituting
2398 * entity values in the output. This function has to be used to change the
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002399 * default parser behavior
2400 * SAX::substituteEntities() has to be used for changing that on a file by
Daniel Veillarda53c6882001-07-25 17:18:57 +00002401 * file basis.
2402 *
2403 * Returns the last value for 0 for no substitution, 1 for substitution.
2404 */
2405
2406int
2407xmlSubstituteEntitiesDefault(int val) {
2408 int old = xmlSubstituteEntitiesDefaultValue;
2409
2410 xmlSubstituteEntitiesDefaultValue = val;
2411 return(old);
2412}
2413
2414/**
2415 * xmlKeepBlanksDefault:
2416 * @val: int 0 or 1
2417 *
2418 * Set and return the previous value for default blanks text nodes support.
2419 * The 1.x version of the parser used an heuristic to try to detect
2420 * ignorable white spaces. As a result the SAX callback was generating
Daniel Veillard11476b42003-09-26 14:51:39 +00002421 * xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when
Daniel Veillarda53c6882001-07-25 17:18:57 +00002422 * using the DOM output text nodes containing those blanks were not generated.
2423 * The 2.x and later version will switch to the XML standard way and
2424 * ignorableWhitespace() are only generated when running the parser in
2425 * validating mode and when the current element doesn't allow CDATA or
2426 * mixed content.
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002427 * This function is provided as a way to force the standard behavior
Daniel Veillarda53c6882001-07-25 17:18:57 +00002428 * on 1.X libs and to switch back to the old mode for compatibility when
2429 * running 1.X client code on 2.X . Upgrade of 1.X code should be done
2430 * by using xmlIsBlankNode() commodity function to detect the "empty"
2431 * nodes generated.
2432 * This value also affect autogeneration of indentation when saving code
2433 * if blanks sections are kept, indentation is not generated.
2434 *
2435 * Returns the last value for 0 for no substitution, 1 for substitution.
2436 */
2437
2438int
2439xmlKeepBlanksDefault(int val) {
2440 int old = xmlKeepBlanksDefaultValue;
2441
2442 xmlKeepBlanksDefaultValue = val;
2443 xmlIndentTreeOutput = !val;
2444 return(old);
2445}
2446