/src/libxml2/parserInternals.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * parserInternals.c : Internal routines (and obsolete ones) needed for the |
3 | | * XML and HTML parsers. |
4 | | * |
5 | | * See Copyright for the status of this software. |
6 | | * |
7 | | * daniel@veillard.com |
8 | | */ |
9 | | |
10 | | #define IN_LIBXML |
11 | | #include "libxml.h" |
12 | | |
13 | | #if defined(_WIN32) |
14 | | #define XML_DIR_SEP '\\' |
15 | | #else |
16 | | #define XML_DIR_SEP '/' |
17 | | #endif |
18 | | |
19 | | #include <string.h> |
20 | | #include <ctype.h> |
21 | | #include <stdlib.h> |
22 | | |
23 | | #include <libxml/xmlmemory.h> |
24 | | #include <libxml/tree.h> |
25 | | #include <libxml/parser.h> |
26 | | #include <libxml/parserInternals.h> |
27 | | #include <libxml/entities.h> |
28 | | #include <libxml/xmlerror.h> |
29 | | #include <libxml/encoding.h> |
30 | | #include <libxml/xmlIO.h> |
31 | | #include <libxml/uri.h> |
32 | | #include <libxml/dict.h> |
33 | | #include <libxml/xmlsave.h> |
34 | | #ifdef LIBXML_CATALOG_ENABLED |
35 | | #include <libxml/catalog.h> |
36 | | #endif |
37 | | #include <libxml/chvalid.h> |
38 | | #include <libxml/nanohttp.h> |
39 | | |
40 | | #define CUR(ctxt) ctxt->input->cur |
41 | | #define END(ctxt) ctxt->input->end |
42 | | |
43 | | #include "private/buf.h" |
44 | | #include "private/enc.h" |
45 | | #include "private/error.h" |
46 | | #include "private/io.h" |
47 | | #include "private/parser.h" |
48 | | |
49 | 2.05M | #define XML_MAX_ERRORS 100 |
50 | | |
51 | | /* |
52 | | * XML_MAX_AMPLIFICATION_DEFAULT is the default maximum allowed amplification |
53 | | * factor of serialized output after entity expansion. |
54 | | */ |
55 | 17.0k | #define XML_MAX_AMPLIFICATION_DEFAULT 5 |
56 | | |
57 | | /* |
58 | | * Various global defaults for parsing |
59 | | */ |
60 | | |
61 | | /** |
62 | | * xmlCheckVersion: |
63 | | * @version: the include version number |
64 | | * |
65 | | * check the compiled lib version against the include one. |
66 | | */ |
67 | | void |
68 | 0 | xmlCheckVersion(int version) { |
69 | 0 | int myversion = LIBXML_VERSION; |
70 | |
|
71 | 0 | xmlInitParser(); |
72 | |
|
73 | 0 | if ((myversion / 10000) != (version / 10000)) { |
74 | 0 | fprintf(stderr, |
75 | 0 | "Fatal: program compiled against libxml %d using libxml %d\n", |
76 | 0 | (version / 10000), (myversion / 10000)); |
77 | 0 | } else if ((myversion / 100) < (version / 100)) { |
78 | 0 | fprintf(stderr, |
79 | 0 | "Warning: program compiled against libxml %d using older %d\n", |
80 | 0 | (version / 100), (myversion / 100)); |
81 | 0 | } |
82 | 0 | } |
83 | | |
84 | | |
85 | | /************************************************************************ |
86 | | * * |
87 | | * Some factorized error routines * |
88 | | * * |
89 | | ************************************************************************/ |
90 | | |
91 | | |
92 | | /** |
93 | | * xmlCtxtSetErrorHandler: |
94 | | * @ctxt: an XML parser context |
95 | | * @handler: error handler |
96 | | * @data: data for error handler |
97 | | * |
98 | | * Register a callback function that will be called on errors and |
99 | | * warnings. If handler is NULL, the error handler will be deactivated. |
100 | | * |
101 | | * This is the recommended way to collect errors from the parser and |
102 | | * takes precedence over all other error reporting mechanisms. |
103 | | * These are (in order of precedence): |
104 | | * |
105 | | * - per-context structured handler (xmlCtxtSetErrorHandler) |
106 | | * - per-context structured "serror" SAX handler |
107 | | * - global structured handler (xmlSetStructuredErrorFunc) |
108 | | * - per-context generic "error" and "warning" SAX handlers |
109 | | * - global generic handler (xmlSetGenericErrorFunc) |
110 | | * - print to stderr |
111 | | * |
112 | | * Available since 2.13.0. |
113 | | */ |
114 | | void |
115 | | xmlCtxtSetErrorHandler(xmlParserCtxtPtr ctxt, xmlStructuredErrorFunc handler, |
116 | | void *data) |
117 | 0 | { |
118 | 0 | if (ctxt == NULL) |
119 | 0 | return; |
120 | 0 | ctxt->errorHandler = handler; |
121 | 0 | ctxt->errorCtxt = data; |
122 | 0 | } |
123 | | |
124 | | /** |
125 | | * xmlCtxtErrMemory: |
126 | | * @ctxt: an XML parser context |
127 | | * @domain: domain |
128 | | * |
129 | | * Handle an out-of-memory error |
130 | | */ |
131 | | void |
132 | | xmlCtxtErrMemory(xmlParserCtxtPtr ctxt) |
133 | 0 | { |
134 | 0 | xmlStructuredErrorFunc schannel = NULL; |
135 | 0 | xmlGenericErrorFunc channel = NULL; |
136 | 0 | void *data; |
137 | |
|
138 | 0 | ctxt->errNo = XML_ERR_NO_MEMORY; |
139 | 0 | ctxt->instate = XML_PARSER_EOF; /* TODO: Remove after refactoring */ |
140 | 0 | ctxt->wellFormed = 0; |
141 | 0 | ctxt->disableSAX = 2; |
142 | |
|
143 | 0 | if (ctxt->errorHandler) { |
144 | 0 | schannel = ctxt->errorHandler; |
145 | 0 | data = ctxt->errorCtxt; |
146 | 0 | } else if ((ctxt->sax->initialized == XML_SAX2_MAGIC) && |
147 | 0 | (ctxt->sax->serror != NULL)) { |
148 | 0 | schannel = ctxt->sax->serror; |
149 | 0 | data = ctxt->userData; |
150 | 0 | } else { |
151 | 0 | channel = ctxt->sax->error; |
152 | 0 | data = ctxt->userData; |
153 | 0 | } |
154 | |
|
155 | 0 | xmlRaiseMemoryError(schannel, channel, data, XML_FROM_PARSER, |
156 | 0 | &ctxt->lastError); |
157 | 0 | } |
158 | | |
159 | | /** |
160 | | * xmlCtxtErrIO: |
161 | | * @ctxt: parser context |
162 | | * @code: xmlParserErrors code |
163 | | * @uri: filename or URI (optional) |
164 | | * |
165 | | * If filename is empty, use the one from context input if available. |
166 | | * |
167 | | * Report an IO error to the parser context. |
168 | | */ |
169 | | void |
170 | | xmlCtxtErrIO(xmlParserCtxtPtr ctxt, int code, const char *uri) |
171 | 20.5k | { |
172 | 20.5k | const char *errstr, *msg, *str1, *str2; |
173 | 20.5k | xmlErrorLevel level; |
174 | | |
175 | 20.5k | if (ctxt == NULL) |
176 | 0 | return; |
177 | | |
178 | | /* |
179 | | * Don't report a well-formedness error if an external entity could |
180 | | * not be found. We assume that inputNr is zero for the document |
181 | | * entity which is somewhat fragile. |
182 | | */ |
183 | 20.5k | if ((ctxt->inputNr > 0) && |
184 | 20.5k | ((code == XML_IO_ENOENT) || |
185 | 20.5k | (code == XML_IO_NETWORK_ATTEMPT) || |
186 | 20.5k | (code == XML_IO_UNKNOWN))) { |
187 | 0 | if (ctxt->validate == 0) |
188 | 0 | level = XML_ERR_WARNING; |
189 | 0 | else |
190 | 0 | level = XML_ERR_ERROR; |
191 | 20.5k | } else { |
192 | 20.5k | level = XML_ERR_FATAL; |
193 | 20.5k | } |
194 | | |
195 | 20.5k | errstr = xmlErrString(code); |
196 | | |
197 | 20.5k | if (uri == NULL) { |
198 | 20.5k | msg = "%s\n"; |
199 | 20.5k | str1 = errstr; |
200 | 20.5k | str2 = NULL; |
201 | 20.5k | } else { |
202 | 0 | msg = "failed to load \"%s\": %s\n"; |
203 | 0 | str1 = uri; |
204 | 0 | str2 = errstr; |
205 | 0 | } |
206 | | |
207 | 20.5k | xmlCtxtErr(ctxt, NULL, XML_FROM_IO, code, level, |
208 | 20.5k | (const xmlChar *) uri, NULL, NULL, 0, |
209 | 20.5k | msg, str1, str2); |
210 | 20.5k | } |
211 | | |
212 | | void |
213 | | xmlCtxtVErr(xmlParserCtxtPtr ctxt, xmlNodePtr node, xmlErrorDomain domain, |
214 | | xmlParserErrors code, xmlErrorLevel level, |
215 | | const xmlChar *str1, const xmlChar *str2, const xmlChar *str3, |
216 | | int int1, const char *msg, va_list ap) |
217 | 2.05M | { |
218 | 2.05M | xmlStructuredErrorFunc schannel = NULL; |
219 | 2.05M | xmlGenericErrorFunc channel = NULL; |
220 | 2.05M | void *data = NULL; |
221 | 2.05M | const char *file = NULL; |
222 | 2.05M | int line = 0; |
223 | 2.05M | int col = 0; |
224 | 2.05M | int res; |
225 | | |
226 | 2.05M | if (PARSER_STOPPED(ctxt)) |
227 | 296 | return; |
228 | | |
229 | 2.05M | if (code == XML_ERR_NO_MEMORY) { |
230 | 0 | xmlCtxtErrMemory(ctxt); |
231 | 0 | return; |
232 | 0 | } |
233 | | |
234 | 2.05M | if (level == XML_ERR_WARNING) { |
235 | 26.1k | if (ctxt->nbWarnings >= XML_MAX_ERRORS) |
236 | 3.61k | return; |
237 | 22.5k | ctxt->nbWarnings += 1; |
238 | 2.02M | } else { |
239 | 2.02M | if (ctxt->nbErrors >= XML_MAX_ERRORS) |
240 | 1.66M | return; |
241 | 366k | ctxt->nbErrors += 1; |
242 | 366k | } |
243 | | |
244 | 389k | if (((ctxt->options & XML_PARSE_NOERROR) == 0) && |
245 | 389k | ((level != XML_ERR_WARNING) || |
246 | 389k | ((ctxt->options & XML_PARSE_NOWARNING) == 0))) { |
247 | 389k | if (ctxt->errorHandler) { |
248 | 0 | schannel = ctxt->errorHandler; |
249 | 0 | data = ctxt->errorCtxt; |
250 | 389k | } else if ((ctxt->sax->initialized == XML_SAX2_MAGIC) && |
251 | 389k | (ctxt->sax->serror != NULL)) { |
252 | 0 | schannel = ctxt->sax->serror; |
253 | 0 | data = ctxt->userData; |
254 | 389k | } else if ((domain == XML_FROM_VALID) || (domain == XML_FROM_DTD)) { |
255 | 137k | if (level == XML_ERR_WARNING) |
256 | 6.73k | channel = ctxt->vctxt.warning; |
257 | 131k | else |
258 | 131k | channel = ctxt->vctxt.error; |
259 | 137k | data = ctxt->vctxt.userData; |
260 | 251k | } else { |
261 | 251k | if (level == XML_ERR_WARNING) |
262 | 15.7k | channel = ctxt->sax->warning; |
263 | 235k | else |
264 | 235k | channel = ctxt->sax->error; |
265 | 251k | data = ctxt->userData; |
266 | 251k | } |
267 | 389k | } |
268 | | |
269 | 389k | if (ctxt->input != NULL) { |
270 | 389k | xmlParserInputPtr input = ctxt->input; |
271 | | |
272 | 389k | if ((input->filename == NULL) && |
273 | 389k | (ctxt->inputNr > 1)) { |
274 | 9.66k | input = ctxt->inputTab[ctxt->inputNr - 2]; |
275 | 9.66k | } |
276 | 389k | file = input->filename; |
277 | 389k | line = input->line; |
278 | 389k | col = input->col; |
279 | 389k | } |
280 | | |
281 | 389k | res = xmlVRaiseError(schannel, channel, data, ctxt, node, domain, code, |
282 | 389k | level, file, line, (const char *) str1, |
283 | 389k | (const char *) str2, (const char *) str3, int1, col, |
284 | 389k | msg, ap); |
285 | | |
286 | 389k | if (res < 0) { |
287 | 0 | xmlCtxtErrMemory(ctxt); |
288 | 0 | return; |
289 | 0 | } |
290 | | |
291 | 389k | if (level >= XML_ERR_ERROR) |
292 | 366k | ctxt->errNo = code; |
293 | 389k | if (level == XML_ERR_FATAL) { |
294 | 207k | ctxt->wellFormed = 0; |
295 | 207k | if (ctxt->recovery == 0) |
296 | 207k | ctxt->disableSAX = 1; |
297 | 207k | } |
298 | | |
299 | 389k | return; |
300 | 389k | } |
301 | | |
302 | | void |
303 | | xmlCtxtErr(xmlParserCtxtPtr ctxt, xmlNodePtr node, xmlErrorDomain domain, |
304 | | xmlParserErrors code, xmlErrorLevel level, |
305 | | const xmlChar *str1, const xmlChar *str2, const xmlChar *str3, |
306 | | int int1, const char *msg, ...) |
307 | 1.89M | { |
308 | 1.89M | va_list ap; |
309 | | |
310 | 1.89M | va_start(ap, msg); |
311 | 1.89M | xmlCtxtVErr(ctxt, node, domain, code, level, |
312 | 1.89M | str1, str2, str3, int1, msg, ap); |
313 | 1.89M | va_end(ap); |
314 | 1.89M | } |
315 | | |
316 | | /** |
317 | | * xmlFatalErr: |
318 | | * @ctxt: an XML parser context |
319 | | * @error: the error number |
320 | | * @info: extra information string |
321 | | * |
322 | | * Handle a fatal parser error, i.e. violating Well-Formedness constraints |
323 | | */ |
324 | | void |
325 | | xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors code, const char *info) |
326 | 560k | { |
327 | 560k | const char *errmsg; |
328 | 560k | xmlErrorLevel level; |
329 | | |
330 | 560k | if (code == XML_ERR_UNSUPPORTED_ENCODING) |
331 | 675 | level = XML_ERR_WARNING; |
332 | 559k | else |
333 | 559k | level = XML_ERR_FATAL; |
334 | | |
335 | 560k | errmsg = xmlErrString(code); |
336 | | |
337 | 560k | if (info == NULL) { |
338 | 187k | xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, code, level, |
339 | 187k | NULL, NULL, NULL, 0, "%s\n", errmsg); |
340 | 373k | } else { |
341 | 373k | xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, code, level, |
342 | 373k | (const xmlChar *) info, NULL, NULL, 0, |
343 | 373k | "%s: %s\n", errmsg, info); |
344 | 373k | } |
345 | 560k | } |
346 | | |
347 | | /** |
348 | | * xmlIsLetter: |
349 | | * @c: an unicode character (int) |
350 | | * |
351 | | * Check whether the character is allowed by the production |
352 | | * [84] Letter ::= BaseChar | Ideographic |
353 | | * |
354 | | * Returns 0 if not, non-zero otherwise |
355 | | */ |
356 | | int |
357 | 0 | xmlIsLetter(int c) { |
358 | 0 | return(IS_BASECHAR(c) || IS_IDEOGRAPHIC(c)); |
359 | 0 | } |
360 | | |
361 | | /************************************************************************ |
362 | | * * |
363 | | * Input handling functions for progressive parsing * |
364 | | * * |
365 | | ************************************************************************/ |
366 | | |
367 | | /* we need to keep enough input to show errors in context */ |
368 | 15.1k | #define LINE_LEN 80 |
369 | | |
370 | | /** |
371 | | * xmlHaltParser: |
372 | | * @ctxt: an XML parser context |
373 | | * |
374 | | * Blocks further parser processing don't override error |
375 | | * for internal use |
376 | | */ |
377 | | void |
378 | 1.36k | xmlHaltParser(xmlParserCtxtPtr ctxt) { |
379 | 1.36k | if (ctxt == NULL) |
380 | 0 | return; |
381 | 1.36k | ctxt->instate = XML_PARSER_EOF; /* TODO: Remove after refactoring */ |
382 | 1.36k | ctxt->disableSAX = 2; |
383 | 1.36k | } |
384 | | |
385 | | /** |
386 | | * xmlParserInputRead: |
387 | | * @in: an XML parser input |
388 | | * @len: an indicative size for the lookahead |
389 | | * |
390 | | * DEPRECATED: This function was internal and is deprecated. |
391 | | * |
392 | | * Returns -1 as this is an error to use it. |
393 | | */ |
394 | | int |
395 | 0 | xmlParserInputRead(xmlParserInputPtr in ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUSED) { |
396 | 0 | return(-1); |
397 | 0 | } |
398 | | |
399 | | /** |
400 | | * xmlParserGrow: |
401 | | * @ctxt: an XML parser context |
402 | | * |
403 | | * Grow the input buffer. |
404 | | * |
405 | | * Returns the number of bytes read or -1 in case of error. |
406 | | */ |
407 | | int |
408 | 2.68M | xmlParserGrow(xmlParserCtxtPtr ctxt) { |
409 | 2.68M | xmlParserInputPtr in = ctxt->input; |
410 | 2.68M | xmlParserInputBufferPtr buf = in->buf; |
411 | 2.68M | size_t curEnd = in->end - in->cur; |
412 | 2.68M | size_t curBase = in->cur - in->base; |
413 | 2.68M | size_t maxLength = (ctxt->options & XML_PARSE_HUGE) ? |
414 | 0 | XML_MAX_HUGE_LENGTH : |
415 | 2.68M | XML_MAX_LOOKUP_LIMIT; |
416 | 2.68M | int ret; |
417 | | |
418 | 2.68M | if (buf == NULL) |
419 | 0 | return(0); |
420 | | /* Don't grow push parser buffer. */ |
421 | 2.68M | if (PARSER_PROGRESSIVE(ctxt)) |
422 | 0 | return(0); |
423 | | /* Don't grow memory buffers. */ |
424 | 2.68M | if ((buf->encoder == NULL) && (buf->readcallback == NULL)) |
425 | 0 | return(0); |
426 | 2.68M | if (buf->error != 0) |
427 | 31.2k | return(-1); |
428 | | |
429 | 2.65M | if (curBase > maxLength) { |
430 | 0 | xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT, |
431 | 0 | "Buffer size limit exceeded, try XML_PARSE_HUGE\n"); |
432 | 0 | xmlHaltParser(ctxt); |
433 | 0 | return(-1); |
434 | 0 | } |
435 | | |
436 | 2.65M | if (curEnd >= INPUT_CHUNK) |
437 | 24.2k | return(0); |
438 | | |
439 | 2.63M | ret = xmlParserInputBufferGrow(buf, INPUT_CHUNK); |
440 | 2.63M | xmlBufUpdateInput(buf->buffer, in, curBase); |
441 | | |
442 | 2.63M | if (ret < 0) { |
443 | 1.00k | xmlCtxtErrIO(ctxt, buf->error, NULL); |
444 | 1.00k | } |
445 | | |
446 | 2.63M | return(ret); |
447 | 2.65M | } |
448 | | |
449 | | /** |
450 | | * xmlParserInputGrow: |
451 | | * @in: an XML parser input |
452 | | * @len: an indicative size for the lookahead |
453 | | * |
454 | | * DEPRECATED: Don't use. |
455 | | * |
456 | | * This function increase the input for the parser. It tries to |
457 | | * preserve pointers to the input buffer, and keep already read data |
458 | | * |
459 | | * Returns the amount of char read, or -1 in case of error, 0 indicate the |
460 | | * end of this entity |
461 | | */ |
462 | | int |
463 | 0 | xmlParserInputGrow(xmlParserInputPtr in, int len) { |
464 | 0 | int ret; |
465 | 0 | size_t indx; |
466 | |
|
467 | 0 | if ((in == NULL) || (len < 0)) return(-1); |
468 | 0 | if (in->buf == NULL) return(-1); |
469 | 0 | if (in->base == NULL) return(-1); |
470 | 0 | if (in->cur == NULL) return(-1); |
471 | 0 | if (in->buf->buffer == NULL) return(-1); |
472 | | |
473 | | /* Don't grow memory buffers. */ |
474 | 0 | if ((in->buf->encoder == NULL) && (in->buf->readcallback == NULL)) |
475 | 0 | return(0); |
476 | | |
477 | 0 | indx = in->cur - in->base; |
478 | 0 | if (xmlBufUse(in->buf->buffer) > (unsigned int) indx + INPUT_CHUNK) { |
479 | 0 | return(0); |
480 | 0 | } |
481 | 0 | ret = xmlParserInputBufferGrow(in->buf, len); |
482 | |
|
483 | 0 | in->base = xmlBufContent(in->buf->buffer); |
484 | 0 | if (in->base == NULL) { |
485 | 0 | in->base = BAD_CAST ""; |
486 | 0 | in->cur = in->base; |
487 | 0 | in->end = in->base; |
488 | 0 | return(-1); |
489 | 0 | } |
490 | 0 | in->cur = in->base + indx; |
491 | 0 | in->end = xmlBufEnd(in->buf->buffer); |
492 | |
|
493 | 0 | return(ret); |
494 | 0 | } |
495 | | |
496 | | /** |
497 | | * xmlParserShrink: |
498 | | * @ctxt: an XML parser context |
499 | | * |
500 | | * Shrink the input buffer. |
501 | | */ |
502 | | void |
503 | 17.6k | xmlParserShrink(xmlParserCtxtPtr ctxt) { |
504 | 17.6k | xmlParserInputPtr in = ctxt->input; |
505 | 17.6k | xmlParserInputBufferPtr buf = in->buf; |
506 | 17.6k | size_t used; |
507 | | |
508 | 17.6k | if (buf == NULL) |
509 | 0 | return; |
510 | | /* Don't shrink pull parser memory buffers. */ |
511 | 17.6k | if ((!PARSER_PROGRESSIVE(ctxt)) && |
512 | 17.6k | (buf->encoder == NULL) && |
513 | 17.6k | (buf->readcallback == NULL)) |
514 | 0 | return; |
515 | | |
516 | 17.6k | used = in->cur - in->base; |
517 | | /* |
518 | | * Do not shrink on large buffers whose only a tiny fraction |
519 | | * was consumed |
520 | | */ |
521 | 17.6k | if (used > INPUT_CHUNK) { |
522 | 15.1k | size_t res = xmlBufShrink(buf->buffer, used - LINE_LEN); |
523 | | |
524 | 15.1k | if (res > 0) { |
525 | 15.1k | used -= res; |
526 | 15.1k | if ((res > ULONG_MAX) || |
527 | 15.1k | (in->consumed > ULONG_MAX - (unsigned long)res)) |
528 | 0 | in->consumed = ULONG_MAX; |
529 | 15.1k | else |
530 | 15.1k | in->consumed += res; |
531 | 15.1k | } |
532 | 15.1k | } |
533 | | |
534 | 17.6k | xmlBufUpdateInput(buf->buffer, in, used); |
535 | 17.6k | } |
536 | | |
537 | | /** |
538 | | * xmlParserInputShrink: |
539 | | * @in: an XML parser input |
540 | | * |
541 | | * DEPRECATED: Don't use. |
542 | | * |
543 | | * This function removes used input for the parser. |
544 | | */ |
545 | | void |
546 | 0 | xmlParserInputShrink(xmlParserInputPtr in) { |
547 | 0 | size_t used; |
548 | 0 | size_t ret; |
549 | |
|
550 | 0 | if (in == NULL) return; |
551 | 0 | if (in->buf == NULL) return; |
552 | 0 | if (in->base == NULL) return; |
553 | 0 | if (in->cur == NULL) return; |
554 | 0 | if (in->buf->buffer == NULL) return; |
555 | | |
556 | 0 | used = in->cur - in->base; |
557 | | /* |
558 | | * Do not shrink on large buffers whose only a tiny fraction |
559 | | * was consumed |
560 | | */ |
561 | 0 | if (used > INPUT_CHUNK) { |
562 | 0 | ret = xmlBufShrink(in->buf->buffer, used - LINE_LEN); |
563 | 0 | if (ret > 0) { |
564 | 0 | used -= ret; |
565 | 0 | if ((ret > ULONG_MAX) || |
566 | 0 | (in->consumed > ULONG_MAX - (unsigned long)ret)) |
567 | 0 | in->consumed = ULONG_MAX; |
568 | 0 | else |
569 | 0 | in->consumed += ret; |
570 | 0 | } |
571 | 0 | } |
572 | |
|
573 | 0 | if (xmlBufUse(in->buf->buffer) <= INPUT_CHUNK) { |
574 | 0 | xmlParserInputBufferRead(in->buf, 2 * INPUT_CHUNK); |
575 | 0 | } |
576 | |
|
577 | 0 | in->base = xmlBufContent(in->buf->buffer); |
578 | 0 | if (in->base == NULL) { |
579 | | /* TODO: raise error */ |
580 | 0 | in->base = BAD_CAST ""; |
581 | 0 | in->cur = in->base; |
582 | 0 | in->end = in->base; |
583 | 0 | return; |
584 | 0 | } |
585 | 0 | in->cur = in->base + used; |
586 | 0 | in->end = xmlBufEnd(in->buf->buffer); |
587 | 0 | } |
588 | | |
589 | | /************************************************************************ |
590 | | * * |
591 | | * UTF8 character input and related functions * |
592 | | * * |
593 | | ************************************************************************/ |
594 | | |
595 | | /** |
596 | | * xmlNextChar: |
597 | | * @ctxt: the XML parser context |
598 | | * |
599 | | * DEPRECATED: Internal function, do not use. |
600 | | * |
601 | | * Skip to the next char input char. |
602 | | */ |
603 | | |
604 | | void |
605 | | xmlNextChar(xmlParserCtxtPtr ctxt) |
606 | 9.34M | { |
607 | 9.34M | const unsigned char *cur; |
608 | 9.34M | size_t avail; |
609 | 9.34M | int c; |
610 | | |
611 | 9.34M | if ((ctxt == NULL) || (ctxt->input == NULL)) |
612 | 0 | return; |
613 | | |
614 | 9.34M | avail = ctxt->input->end - ctxt->input->cur; |
615 | | |
616 | 9.34M | if (avail < INPUT_CHUNK) { |
617 | 665k | xmlParserGrow(ctxt); |
618 | 665k | if (ctxt->input->cur >= ctxt->input->end) |
619 | 1.66k | return; |
620 | 663k | avail = ctxt->input->end - ctxt->input->cur; |
621 | 663k | } |
622 | | |
623 | 9.34M | cur = ctxt->input->cur; |
624 | 9.34M | c = *cur; |
625 | | |
626 | 9.34M | if (c < 0x80) { |
627 | 1.89M | if (c == '\n') { |
628 | 102k | ctxt->input->cur++; |
629 | 102k | ctxt->input->line++; |
630 | 102k | ctxt->input->col = 1; |
631 | 1.79M | } else if (c == '\r') { |
632 | | /* |
633 | | * 2.11 End-of-Line Handling |
634 | | * the literal two-character sequence "#xD#xA" or a standalone |
635 | | * literal #xD, an XML processor must pass to the application |
636 | | * the single character #xA. |
637 | | */ |
638 | 2.24k | ctxt->input->cur += ((cur[1] == '\n') ? 2 : 1); |
639 | 2.24k | ctxt->input->line++; |
640 | 2.24k | ctxt->input->col = 1; |
641 | 2.24k | return; |
642 | 1.79M | } else { |
643 | 1.79M | ctxt->input->cur++; |
644 | 1.79M | ctxt->input->col++; |
645 | 1.79M | } |
646 | 7.44M | } else { |
647 | 7.44M | ctxt->input->col++; |
648 | | |
649 | 7.44M | if ((avail < 2) || (cur[1] & 0xc0) != 0x80) |
650 | 61.7k | goto encoding_error; |
651 | | |
652 | 7.37M | if (c < 0xe0) { |
653 | | /* 2-byte code */ |
654 | 19.0k | if (c < 0xc2) |
655 | 18.0k | goto encoding_error; |
656 | 975 | ctxt->input->cur += 2; |
657 | 7.35M | } else { |
658 | 7.35M | unsigned int val = (c << 8) | cur[1]; |
659 | | |
660 | 7.35M | if ((avail < 3) || (cur[2] & 0xc0) != 0x80) |
661 | 704 | goto encoding_error; |
662 | | |
663 | 7.35M | if (c < 0xf0) { |
664 | | /* 3-byte code */ |
665 | 7.35M | if ((val < 0xe0a0) || ((val >= 0xeda0) && (val < 0xee00))) |
666 | 432 | goto encoding_error; |
667 | 7.35M | ctxt->input->cur += 3; |
668 | 7.35M | } else { |
669 | 2.84k | if ((avail < 4) || ((cur[3] & 0xc0) != 0x80)) |
670 | 154 | goto encoding_error; |
671 | | |
672 | | /* 4-byte code */ |
673 | 2.68k | if ((val < 0xf090) || (val >= 0xf490)) |
674 | 2.39k | goto encoding_error; |
675 | 296 | ctxt->input->cur += 4; |
676 | 296 | } |
677 | 7.35M | } |
678 | 7.37M | } |
679 | | |
680 | 9.25M | return; |
681 | | |
682 | 9.25M | encoding_error: |
683 | | /* Only report the first error */ |
684 | 83.4k | if ((ctxt->input->flags & XML_INPUT_ENCODING_ERROR) == 0) { |
685 | 4.38k | xmlCtxtErrIO(ctxt, XML_ERR_INVALID_ENCODING, NULL); |
686 | 4.38k | ctxt->input->flags |= XML_INPUT_ENCODING_ERROR; |
687 | 4.38k | } |
688 | 83.4k | ctxt->input->cur++; |
689 | 83.4k | return; |
690 | 9.34M | } |
691 | | |
692 | | /** |
693 | | * xmlCurrentChar: |
694 | | * @ctxt: the XML parser context |
695 | | * @len: pointer to the length of the char read |
696 | | * |
697 | | * DEPRECATED: Internal function, do not use. |
698 | | * |
699 | | * The current char value, if using UTF-8 this may actually span multiple |
700 | | * bytes in the input buffer. Implement the end of line normalization: |
701 | | * 2.11 End-of-Line Handling |
702 | | * Wherever an external parsed entity or the literal entity value |
703 | | * of an internal parsed entity contains either the literal two-character |
704 | | * sequence "#xD#xA" or a standalone literal #xD, an XML processor |
705 | | * must pass to the application the single character #xA. |
706 | | * This behavior can conveniently be produced by normalizing all |
707 | | * line breaks to #xA on input, before parsing.) |
708 | | * |
709 | | * Returns the current char value and its length |
710 | | */ |
711 | | |
712 | | int |
713 | 10.1M | xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) { |
714 | 10.1M | const unsigned char *cur; |
715 | 10.1M | size_t avail; |
716 | 10.1M | int c; |
717 | | |
718 | 10.1M | if ((ctxt == NULL) || (len == NULL) || (ctxt->input == NULL)) return(0); |
719 | | |
720 | 10.1M | avail = ctxt->input->end - ctxt->input->cur; |
721 | | |
722 | 10.1M | if (avail < INPUT_CHUNK) { |
723 | 886k | xmlParserGrow(ctxt); |
724 | 886k | avail = ctxt->input->end - ctxt->input->cur; |
725 | 886k | } |
726 | | |
727 | 10.1M | cur = ctxt->input->cur; |
728 | 10.1M | c = *cur; |
729 | | |
730 | 10.1M | if (c < 0x80) { |
731 | | /* 1-byte code */ |
732 | 4.05M | if (c < 0x20) { |
733 | | /* |
734 | | * 2.11 End-of-Line Handling |
735 | | * the literal two-character sequence "#xD#xA" or a standalone |
736 | | * literal #xD, an XML processor must pass to the application |
737 | | * the single character #xA. |
738 | | */ |
739 | 725k | if (c == '\r') { |
740 | | /* |
741 | | * TODO: This function shouldn't change the 'cur' pointer |
742 | | * as side effect, but the NEXTL macro in parser.c relies |
743 | | * on this behavior when incrementing line numbers. |
744 | | */ |
745 | 33.4k | if (cur[1] == '\n') |
746 | 9.17k | ctxt->input->cur++; |
747 | 33.4k | *len = 1; |
748 | 33.4k | c = '\n'; |
749 | 691k | } else if (c == 0) { |
750 | 381k | if (ctxt->input->cur >= ctxt->input->end) { |
751 | 9.26k | *len = 0; |
752 | 372k | } else { |
753 | 372k | *len = 1; |
754 | | /* |
755 | | * TODO: Null bytes should be handled by callers, |
756 | | * but this can be tricky. |
757 | | */ |
758 | 372k | xmlFatalErr(ctxt, XML_ERR_INVALID_CHAR, |
759 | 372k | "Char 0x0 out of allowed range\n"); |
760 | 372k | } |
761 | 381k | } else { |
762 | 310k | *len = 1; |
763 | 310k | } |
764 | 3.33M | } else { |
765 | 3.33M | *len = 1; |
766 | 3.33M | } |
767 | | |
768 | 4.05M | return(c); |
769 | 6.12M | } else { |
770 | 6.12M | int val; |
771 | | |
772 | 6.12M | if (avail < 2) |
773 | 768 | goto incomplete_sequence; |
774 | 6.12M | if ((cur[1] & 0xc0) != 0x80) |
775 | 998k | goto encoding_error; |
776 | | |
777 | 5.12M | if (c < 0xe0) { |
778 | | /* 2-byte code */ |
779 | 609k | if (c < 0xc2) |
780 | 403k | goto encoding_error; |
781 | 205k | val = (c & 0x1f) << 6; |
782 | 205k | val |= cur[1] & 0x3f; |
783 | 205k | *len = 2; |
784 | 4.52M | } else { |
785 | 4.52M | if (avail < 3) |
786 | 42 | goto incomplete_sequence; |
787 | 4.52M | if ((cur[2] & 0xc0) != 0x80) |
788 | 12.1k | goto encoding_error; |
789 | | |
790 | 4.50M | if (c < 0xf0) { |
791 | | /* 3-byte code */ |
792 | 4.48M | val = (c & 0xf) << 12; |
793 | 4.48M | val |= (cur[1] & 0x3f) << 6; |
794 | 4.48M | val |= cur[2] & 0x3f; |
795 | 4.48M | if ((val < 0x800) || ((val >= 0xd800) && (val < 0xe000))) |
796 | 1.77k | goto encoding_error; |
797 | 4.48M | *len = 3; |
798 | 4.48M | } else { |
799 | 24.2k | if (avail < 4) |
800 | 24 | goto incomplete_sequence; |
801 | 24.2k | if ((cur[3] & 0xc0) != 0x80) |
802 | 4.00k | goto encoding_error; |
803 | | |
804 | | /* 4-byte code */ |
805 | 20.2k | val = (c & 0x0f) << 18; |
806 | 20.2k | val |= (cur[1] & 0x3f) << 12; |
807 | 20.2k | val |= (cur[2] & 0x3f) << 6; |
808 | 20.2k | val |= cur[3] & 0x3f; |
809 | 20.2k | if ((val < 0x10000) || (val >= 0x110000)) |
810 | 3.02k | goto encoding_error; |
811 | 17.1k | *len = 4; |
812 | 17.1k | } |
813 | 4.50M | } |
814 | | |
815 | 4.70M | return(val); |
816 | 5.12M | } |
817 | | |
818 | 1.42M | encoding_error: |
819 | | /* Only report the first error */ |
820 | 1.42M | if ((ctxt->input->flags & XML_INPUT_ENCODING_ERROR) == 0) { |
821 | 12.3k | xmlCtxtErrIO(ctxt, XML_ERR_INVALID_ENCODING, NULL); |
822 | 12.3k | ctxt->input->flags |= XML_INPUT_ENCODING_ERROR; |
823 | 12.3k | } |
824 | 1.42M | *len = 1; |
825 | 1.42M | return(0xFFFD); /* U+FFFD Replacement Character */ |
826 | | |
827 | 834 | incomplete_sequence: |
828 | | /* |
829 | | * An encoding problem may arise from a truncated input buffer |
830 | | * splitting a character in the middle. In that case do not raise |
831 | | * an error but return 0. This should only happen when push parsing |
832 | | * char data. |
833 | | */ |
834 | 834 | *len = 0; |
835 | 834 | return(0); |
836 | 10.1M | } |
837 | | |
838 | | /** |
839 | | * xmlStringCurrentChar: |
840 | | * @ctxt: the XML parser context |
841 | | * @cur: pointer to the beginning of the char |
842 | | * @len: pointer to the length of the char read |
843 | | * |
844 | | * DEPRECATED: Internal function, do not use. |
845 | | * |
846 | | * The current char value, if using UTF-8 this may actually span multiple |
847 | | * bytes in the input buffer. |
848 | | * |
849 | | * Returns the current char value and its length |
850 | | */ |
851 | | |
852 | | int |
853 | | xmlStringCurrentChar(xmlParserCtxtPtr ctxt ATTRIBUTE_UNUSED, |
854 | 1.56M | const xmlChar *cur, int *len) { |
855 | 1.56M | int c; |
856 | | |
857 | 1.56M | if ((cur == NULL) || (len == NULL)) |
858 | 0 | return(0); |
859 | | |
860 | | /* cur is zero-terminated, so we can lie about its length. */ |
861 | 1.56M | *len = 4; |
862 | 1.56M | c = xmlGetUTF8Char(cur, len); |
863 | | |
864 | 1.56M | return((c < 0) ? 0 : c); |
865 | 1.56M | } |
866 | | |
867 | | /** |
868 | | * xmlCopyCharMultiByte: |
869 | | * @out: pointer to an array of xmlChar |
870 | | * @val: the char value |
871 | | * |
872 | | * append the char value in the array |
873 | | * |
874 | | * Returns the number of xmlChar written |
875 | | */ |
876 | | int |
877 | 4.09M | xmlCopyCharMultiByte(xmlChar *out, int val) { |
878 | 4.09M | if ((out == NULL) || (val < 0)) return(0); |
879 | | /* |
880 | | * We are supposed to handle UTF8, check it's valid |
881 | | * From rfc2044: encoding of the Unicode values on UTF-8: |
882 | | * |
883 | | * UCS-4 range (hex.) UTF-8 octet sequence (binary) |
884 | | * 0000 0000-0000 007F 0xxxxxxx |
885 | | * 0000 0080-0000 07FF 110xxxxx 10xxxxxx |
886 | | * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx |
887 | | */ |
888 | 4.09M | if (val >= 0x80) { |
889 | 4.09M | xmlChar *savedout = out; |
890 | 4.09M | int bits; |
891 | 4.09M | if (val < 0x800) { *out++= (val >> 6) | 0xC0; bits= 0; } |
892 | 3.99M | else if (val < 0x10000) { *out++= (val >> 12) | 0xE0; bits= 6;} |
893 | 15.2k | else if (val < 0x110000) { *out++= (val >> 18) | 0xF0; bits= 12; } |
894 | 0 | else { |
895 | 0 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
896 | 0 | fprintf(stderr, "xmlCopyCharMultiByte: codepoint out of range\n"); |
897 | 0 | abort(); |
898 | 0 | #endif |
899 | 0 | return(0); |
900 | 0 | } |
901 | 12.2M | for ( ; bits >= 0; bits-= 6) |
902 | 8.11M | *out++= ((val >> bits) & 0x3F) | 0x80 ; |
903 | 4.09M | return (out - savedout); |
904 | 4.09M | } |
905 | 0 | *out = val; |
906 | 0 | return 1; |
907 | 4.09M | } |
908 | | |
909 | | /** |
910 | | * xmlCopyChar: |
911 | | * @len: Ignored, compatibility |
912 | | * @out: pointer to an array of xmlChar |
913 | | * @val: the char value |
914 | | * |
915 | | * append the char value in the array |
916 | | * |
917 | | * Returns the number of xmlChar written |
918 | | */ |
919 | | |
920 | | int |
921 | 0 | xmlCopyChar(int len ATTRIBUTE_UNUSED, xmlChar *out, int val) { |
922 | 0 | if ((out == NULL) || (val < 0)) return(0); |
923 | | /* the len parameter is ignored */ |
924 | 0 | if (val >= 0x80) { |
925 | 0 | return(xmlCopyCharMultiByte (out, val)); |
926 | 0 | } |
927 | 0 | *out = val; |
928 | 0 | return 1; |
929 | 0 | } |
930 | | |
931 | | /************************************************************************ |
932 | | * * |
933 | | * Commodity functions to switch encodings * |
934 | | * * |
935 | | ************************************************************************/ |
936 | | |
937 | | static int |
938 | 956 | xmlDetectEBCDIC(xmlParserInputPtr input, xmlCharEncodingHandlerPtr *hout) { |
939 | 956 | xmlChar out[200]; |
940 | 956 | xmlCharEncodingHandlerPtr handler; |
941 | 956 | int inlen, outlen, res, i; |
942 | | |
943 | 956 | *hout = NULL; |
944 | | |
945 | | /* |
946 | | * To detect the EBCDIC code page, we convert the first 200 bytes |
947 | | * to EBCDIC-US and try to find the encoding declaration. |
948 | | */ |
949 | 956 | res = xmlLookupCharEncodingHandler(XML_CHAR_ENCODING_EBCDIC, &handler); |
950 | 956 | if (res != 0) |
951 | 0 | return(res); |
952 | 956 | outlen = sizeof(out) - 1; |
953 | 956 | inlen = input->end - input->cur; |
954 | 956 | res = xmlEncInputChunk(handler, out, &outlen, input->cur, &inlen); |
955 | | /* |
956 | | * Return the EBCDIC handler if decoding failed. The error will |
957 | | * be reported later. |
958 | | */ |
959 | 956 | if (res < 0) |
960 | 323 | goto done; |
961 | 633 | out[outlen] = 0; |
962 | | |
963 | 7.86k | for (i = 0; i < outlen; i++) { |
964 | 7.77k | if (out[i] == '>') |
965 | 40 | break; |
966 | 7.73k | if ((out[i] == 'e') && |
967 | 7.73k | (xmlStrncmp(out + i, BAD_CAST "encoding", 8) == 0)) { |
968 | 504 | int start, cur, quote; |
969 | | |
970 | 504 | i += 8; |
971 | 504 | while (IS_BLANK_CH(out[i])) |
972 | 679 | i += 1; |
973 | 504 | if (out[i++] != '=') |
974 | 63 | break; |
975 | 441 | while (IS_BLANK_CH(out[i])) |
976 | 469 | i += 1; |
977 | 441 | quote = out[i++]; |
978 | 441 | if ((quote != '\'') && (quote != '"')) |
979 | 70 | break; |
980 | 371 | start = i; |
981 | 371 | cur = out[i]; |
982 | 1.31k | while (((cur >= 'a') && (cur <= 'z')) || |
983 | 1.31k | ((cur >= 'A') && (cur <= 'Z')) || |
984 | 1.31k | ((cur >= '0') && (cur <= '9')) || |
985 | 1.31k | (cur == '.') || (cur == '_') || |
986 | 1.31k | (cur == '-')) |
987 | 945 | cur = out[++i]; |
988 | 371 | if (cur != quote) |
989 | 366 | break; |
990 | 5 | out[i] = 0; |
991 | 5 | xmlCharEncCloseFunc(handler); |
992 | 5 | res = xmlOpenCharEncodingHandler((char *) out + start, |
993 | 5 | /* output */ 0, &handler); |
994 | 5 | if (res != 0) |
995 | 4 | return(res); |
996 | 1 | *hout = handler; |
997 | 1 | return(0); |
998 | 5 | } |
999 | 7.73k | } |
1000 | | |
1001 | 951 | done: |
1002 | | /* |
1003 | | * Encoding handlers are stateful, so we have to recreate them. |
1004 | | */ |
1005 | 951 | xmlCharEncCloseFunc(handler); |
1006 | 951 | res = xmlLookupCharEncodingHandler(XML_CHAR_ENCODING_EBCDIC, &handler); |
1007 | 951 | if (res != 0) |
1008 | 0 | return(res); |
1009 | 951 | *hout = handler; |
1010 | 951 | return(0); |
1011 | 951 | } |
1012 | | |
1013 | | /** |
1014 | | * xmlSwitchEncoding: |
1015 | | * @ctxt: the parser context |
1016 | | * @enc: the encoding value (number) |
1017 | | * |
1018 | | * Use encoding specified by enum to decode input data. This overrides |
1019 | | * the encoding found in the XML declaration. |
1020 | | * |
1021 | | * This function can also be used to override the encoding of chunks |
1022 | | * passed to xmlParseChunk. |
1023 | | * |
1024 | | * Returns 0 in case of success, -1 otherwise |
1025 | | */ |
1026 | | int |
1027 | | xmlSwitchEncoding(xmlParserCtxtPtr ctxt, xmlCharEncoding enc) |
1028 | 3.10k | { |
1029 | 3.10k | xmlCharEncodingHandlerPtr handler = NULL; |
1030 | 3.10k | int ret; |
1031 | 3.10k | int res; |
1032 | | |
1033 | 3.10k | if ((ctxt == NULL) || (ctxt->input == NULL)) |
1034 | 0 | return(-1); |
1035 | | |
1036 | 3.10k | switch (enc) { |
1037 | 0 | case XML_CHAR_ENCODING_NONE: |
1038 | 563 | case XML_CHAR_ENCODING_UTF8: |
1039 | 563 | case XML_CHAR_ENCODING_ASCII: |
1040 | 563 | res = 0; |
1041 | 563 | break; |
1042 | 956 | case XML_CHAR_ENCODING_EBCDIC: |
1043 | 956 | res = xmlDetectEBCDIC(ctxt->input, &handler); |
1044 | 956 | break; |
1045 | 1.58k | default: |
1046 | 1.58k | res = xmlLookupCharEncodingHandler(enc, &handler); |
1047 | 1.58k | break; |
1048 | 3.10k | } |
1049 | | |
1050 | 3.10k | if (res != 0) { |
1051 | 4 | const char *name = xmlGetCharEncodingName(enc); |
1052 | | |
1053 | 4 | xmlFatalErr(ctxt, res, (name ? name : "<null>")); |
1054 | 4 | return(-1); |
1055 | 4 | } |
1056 | | |
1057 | 3.10k | ret = xmlSwitchInputEncoding(ctxt, ctxt->input, handler); |
1058 | | |
1059 | 3.10k | if ((ret >= 0) && (enc == XML_CHAR_ENCODING_NONE)) { |
1060 | 0 | ctxt->input->flags &= ~XML_INPUT_HAS_ENCODING; |
1061 | 0 | } |
1062 | | |
1063 | 3.10k | return(ret); |
1064 | 3.10k | } |
1065 | | |
1066 | | /** |
1067 | | * xmlSwitchEncodingName: |
1068 | | * @ctxt: the parser context, only for error reporting |
1069 | | * @input: the input strea, |
1070 | | * @encoding: the encoding name |
1071 | | * |
1072 | | * Returns 0 in case of success, -1 otherwise |
1073 | | */ |
1074 | | static int |
1075 | | xmlSwitchInputEncodingName(xmlParserCtxtPtr ctxt, xmlParserInputPtr input, |
1076 | 6.27k | const char *encoding) { |
1077 | 6.27k | xmlCharEncodingHandlerPtr handler; |
1078 | 6.27k | int res; |
1079 | | |
1080 | 6.27k | if (encoding == NULL) |
1081 | 0 | return(-1); |
1082 | | |
1083 | 6.27k | res = xmlOpenCharEncodingHandler(encoding, /* output */ 0, &handler); |
1084 | 6.27k | if (res != 0) { |
1085 | 671 | xmlFatalErr(ctxt, res, encoding); |
1086 | 671 | return(-1); |
1087 | 671 | } |
1088 | | |
1089 | 5.60k | return(xmlSwitchInputEncoding(ctxt, input, handler)); |
1090 | 6.27k | } |
1091 | | |
1092 | | /** |
1093 | | * xmlSwitchEncodingName: |
1094 | | * @ctxt: the parser context |
1095 | | * @encoding: the encoding name |
1096 | | * |
1097 | | * Use specified encoding to decode input data. This overrides the |
1098 | | * encoding found in the XML declaration. |
1099 | | * |
1100 | | * This function can also be used to override the encoding of chunks |
1101 | | * passed to xmlParseChunk. |
1102 | | * |
1103 | | * Available since 2.13.0. |
1104 | | * |
1105 | | * Returns 0 in case of success, -1 otherwise |
1106 | | */ |
1107 | | int |
1108 | 6.27k | xmlSwitchEncodingName(xmlParserCtxtPtr ctxt, const char *encoding) { |
1109 | 6.27k | return(xmlSwitchInputEncodingName(ctxt, ctxt->input, encoding)); |
1110 | 6.27k | } |
1111 | | |
1112 | | /** |
1113 | | * xmlSwitchInputEncoding: |
1114 | | * @ctxt: the parser context, only for error reporting |
1115 | | * @input: the input stream |
1116 | | * @handler: the encoding handler |
1117 | | * |
1118 | | * DEPRECATED: Internal function, don't use. |
1119 | | * |
1120 | | * Use encoding handler to decode input data. |
1121 | | * |
1122 | | * Returns 0 in case of success, -1 otherwise |
1123 | | */ |
1124 | | int |
1125 | | xmlSwitchInputEncoding(xmlParserCtxtPtr ctxt, xmlParserInputPtr input, |
1126 | | xmlCharEncodingHandlerPtr handler) |
1127 | 8.70k | { |
1128 | 8.70k | int nbchars; |
1129 | 8.70k | xmlParserInputBufferPtr in; |
1130 | | |
1131 | 8.70k | if ((input == NULL) || (input->buf == NULL)) { |
1132 | 0 | xmlCharEncCloseFunc(handler); |
1133 | 0 | return (-1); |
1134 | 0 | } |
1135 | 8.70k | in = input->buf; |
1136 | | |
1137 | 8.70k | input->flags |= XML_INPUT_HAS_ENCODING; |
1138 | | |
1139 | | /* |
1140 | | * UTF-8 requires no encoding handler. |
1141 | | */ |
1142 | 8.70k | if ((handler != NULL) && |
1143 | 8.70k | (xmlStrcasecmp(BAD_CAST handler->name, BAD_CAST "UTF-8") == 0)) { |
1144 | 200 | xmlCharEncCloseFunc(handler); |
1145 | 200 | handler = NULL; |
1146 | 200 | } |
1147 | | |
1148 | 8.70k | if (in->encoder == handler) |
1149 | 763 | return (0); |
1150 | | |
1151 | 7.94k | if (in->encoder != NULL) { |
1152 | | /* |
1153 | | * Switching encodings during parsing is a really bad idea, |
1154 | | * but Chromium can switch between ISO-8859-1 and UTF-16 before |
1155 | | * separate calls to xmlParseChunk. |
1156 | | * |
1157 | | * TODO: We should check whether the "raw" input buffer is empty and |
1158 | | * convert the old content using the old encoder. |
1159 | | */ |
1160 | |
|
1161 | 0 | xmlCharEncCloseFunc(in->encoder); |
1162 | 0 | in->encoder = handler; |
1163 | 0 | return (0); |
1164 | 0 | } |
1165 | | |
1166 | 7.94k | in->encoder = handler; |
1167 | | |
1168 | | /* |
1169 | | * Is there already some content down the pipe to convert ? |
1170 | | */ |
1171 | 7.94k | if (xmlBufIsEmpty(in->buffer) == 0) { |
1172 | 7.94k | xmlBufPtr buf; |
1173 | 7.94k | size_t processed; |
1174 | | |
1175 | 7.94k | buf = xmlBufCreate(); |
1176 | 7.94k | if (buf == NULL) { |
1177 | 0 | xmlCtxtErrMemory(ctxt); |
1178 | 0 | return(-1); |
1179 | 0 | } |
1180 | | |
1181 | | /* |
1182 | | * Shrink the current input buffer. |
1183 | | * Move it as the raw buffer and create a new input buffer |
1184 | | */ |
1185 | 7.94k | processed = input->cur - input->base; |
1186 | 7.94k | xmlBufShrink(in->buffer, processed); |
1187 | 7.94k | input->consumed += processed; |
1188 | 7.94k | in->raw = in->buffer; |
1189 | 7.94k | in->buffer = buf; |
1190 | 7.94k | in->rawconsumed = processed; |
1191 | | |
1192 | 7.94k | nbchars = xmlCharEncInput(in); |
1193 | 7.94k | xmlBufResetInput(in->buffer, input); |
1194 | 7.94k | if (nbchars == XML_ENC_ERR_MEMORY) { |
1195 | 0 | xmlCtxtErrMemory(ctxt); |
1196 | 7.94k | } else if (nbchars < 0) { |
1197 | 8 | xmlCtxtErrIO(ctxt, in->error, NULL); |
1198 | 8 | xmlHaltParser(ctxt); |
1199 | 8 | return (-1); |
1200 | 8 | } |
1201 | 7.94k | } |
1202 | 7.93k | return (0); |
1203 | 7.94k | } |
1204 | | |
1205 | | /** |
1206 | | * xmlSwitchToEncoding: |
1207 | | * @ctxt: the parser context |
1208 | | * @handler: the encoding handler |
1209 | | * |
1210 | | * Use encoding handler to decode input data. |
1211 | | * |
1212 | | * This function can be used to enforce the encoding of chunks passed |
1213 | | * to xmlParseChunk. |
1214 | | * |
1215 | | * Returns 0 in case of success, -1 otherwise |
1216 | | */ |
1217 | | int |
1218 | | xmlSwitchToEncoding(xmlParserCtxtPtr ctxt, xmlCharEncodingHandlerPtr handler) |
1219 | 0 | { |
1220 | 0 | if (ctxt == NULL) |
1221 | 0 | return(-1); |
1222 | 0 | return(xmlSwitchInputEncoding(ctxt, ctxt->input, handler)); |
1223 | 0 | } |
1224 | | |
1225 | | /** |
1226 | | * xmlDetectEncoding: |
1227 | | * @ctxt: the parser context |
1228 | | * |
1229 | | * Handle optional BOM, detect and switch to encoding. |
1230 | | * |
1231 | | * Assumes that there are at least four bytes in the input buffer. |
1232 | | */ |
1233 | | void |
1234 | 44.1k | xmlDetectEncoding(xmlParserCtxtPtr ctxt) { |
1235 | 44.1k | const xmlChar *in; |
1236 | 44.1k | xmlCharEncoding enc; |
1237 | 44.1k | int bomSize; |
1238 | 44.1k | int autoFlag = 0; |
1239 | | |
1240 | 44.1k | if (xmlParserGrow(ctxt) < 0) |
1241 | 0 | return; |
1242 | 44.1k | in = ctxt->input->cur; |
1243 | 44.1k | if (ctxt->input->end - in < 4) |
1244 | 526 | return; |
1245 | | |
1246 | 43.6k | if (ctxt->input->flags & XML_INPUT_HAS_ENCODING) { |
1247 | | /* |
1248 | | * If the encoding was already set, only skip the BOM which was |
1249 | | * possibly decoded to UTF-8. |
1250 | | */ |
1251 | 0 | if ((in[0] == 0xEF) && (in[1] == 0xBB) && (in[2] == 0xBF)) { |
1252 | 0 | ctxt->input->cur += 3; |
1253 | 0 | } |
1254 | |
|
1255 | 0 | return; |
1256 | 0 | } |
1257 | | |
1258 | 43.6k | enc = XML_CHAR_ENCODING_NONE; |
1259 | 43.6k | bomSize = 0; |
1260 | | |
1261 | 43.6k | switch (in[0]) { |
1262 | 896 | case 0x00: |
1263 | 896 | if ((in[1] == 0x00) && (in[2] == 0x00) && (in[3] == 0x3C)) { |
1264 | 24 | enc = XML_CHAR_ENCODING_UCS4BE; |
1265 | 24 | autoFlag = XML_INPUT_AUTO_OTHER; |
1266 | 872 | } else if ((in[1] == 0x3C) && (in[2] == 0x00) && (in[3] == 0x3F)) { |
1267 | 274 | enc = XML_CHAR_ENCODING_UTF16BE; |
1268 | 274 | autoFlag = XML_INPUT_AUTO_UTF16BE; |
1269 | 274 | } |
1270 | 896 | break; |
1271 | | |
1272 | 39.5k | case 0x3C: |
1273 | 39.5k | if (in[1] == 0x00) { |
1274 | 833 | if ((in[2] == 0x00) && (in[3] == 0x00)) { |
1275 | 84 | enc = XML_CHAR_ENCODING_UCS4LE; |
1276 | 84 | autoFlag = XML_INPUT_AUTO_OTHER; |
1277 | 749 | } else if ((in[2] == 0x3F) && (in[3] == 0x00)) { |
1278 | 534 | enc = XML_CHAR_ENCODING_UTF16LE; |
1279 | 534 | autoFlag = XML_INPUT_AUTO_UTF16LE; |
1280 | 534 | } |
1281 | 833 | } |
1282 | 39.5k | break; |
1283 | | |
1284 | 1.10k | case 0x4C: |
1285 | 1.10k | if ((in[1] == 0x6F) && (in[2] == 0xA7) && (in[3] == 0x94)) { |
1286 | 956 | enc = XML_CHAR_ENCODING_EBCDIC; |
1287 | 956 | autoFlag = XML_INPUT_AUTO_OTHER; |
1288 | 956 | } |
1289 | 1.10k | break; |
1290 | | |
1291 | 599 | case 0xEF: |
1292 | 599 | if ((in[1] == 0xBB) && (in[2] == 0xBF)) { |
1293 | 563 | enc = XML_CHAR_ENCODING_UTF8; |
1294 | 563 | autoFlag = XML_INPUT_AUTO_UTF8; |
1295 | 563 | bomSize = 3; |
1296 | 563 | } |
1297 | 599 | break; |
1298 | | |
1299 | 324 | case 0xFE: |
1300 | 324 | if (in[1] == 0xFF) { |
1301 | 253 | enc = XML_CHAR_ENCODING_UTF16BE; |
1302 | 253 | autoFlag = XML_INPUT_AUTO_UTF16BE; |
1303 | 253 | bomSize = 2; |
1304 | 253 | } |
1305 | 324 | break; |
1306 | | |
1307 | 501 | case 0xFF: |
1308 | 501 | if (in[1] == 0xFE) { |
1309 | 418 | enc = XML_CHAR_ENCODING_UTF16LE; |
1310 | 418 | autoFlag = XML_INPUT_AUTO_UTF16LE; |
1311 | 418 | bomSize = 2; |
1312 | 418 | } |
1313 | 501 | break; |
1314 | 43.6k | } |
1315 | | |
1316 | 43.6k | if (bomSize > 0) { |
1317 | 1.23k | ctxt->input->cur += bomSize; |
1318 | 1.23k | } |
1319 | | |
1320 | 43.6k | if (enc != XML_CHAR_ENCODING_NONE) { |
1321 | 3.10k | ctxt->input->flags |= autoFlag; |
1322 | 3.10k | xmlSwitchEncoding(ctxt, enc); |
1323 | 3.10k | } |
1324 | 43.6k | } |
1325 | | |
1326 | | /** |
1327 | | * xmlSetDeclaredEncoding: |
1328 | | * @ctxt: the parser context |
1329 | | * @encoding: declared encoding |
1330 | | * |
1331 | | * Set the encoding from a declaration in the document. |
1332 | | * |
1333 | | * If no encoding was set yet, switch the encoding. Otherwise, only warn |
1334 | | * about encoding mismatches. |
1335 | | * |
1336 | | * Takes ownership of 'encoding'. |
1337 | | */ |
1338 | | void |
1339 | 6.51k | xmlSetDeclaredEncoding(xmlParserCtxtPtr ctxt, xmlChar *encoding) { |
1340 | 6.51k | if (((ctxt->input->flags & XML_INPUT_HAS_ENCODING) == 0) && |
1341 | 6.51k | ((ctxt->options & XML_PARSE_IGNORE_ENC) == 0)) { |
1342 | 6.27k | xmlSwitchEncodingName(ctxt, (const char *) encoding); |
1343 | 6.27k | ctxt->input->flags |= XML_INPUT_USES_ENC_DECL; |
1344 | 6.27k | } else if (ctxt->input->flags & XML_INPUT_AUTO_ENCODING) { |
1345 | 244 | static const char *allowedUTF8[] = { |
1346 | 244 | "UTF-8", "UTF8", NULL |
1347 | 244 | }; |
1348 | 244 | static const char *allowedUTF16LE[] = { |
1349 | 244 | "UTF-16", "UTF-16LE", "UTF16", NULL |
1350 | 244 | }; |
1351 | 244 | static const char *allowedUTF16BE[] = { |
1352 | 244 | "UTF-16", "UTF-16BE", "UTF16", NULL |
1353 | 244 | }; |
1354 | 244 | const char **allowed = NULL; |
1355 | 244 | const char *autoEnc = NULL; |
1356 | | |
1357 | 244 | switch (ctxt->input->flags & XML_INPUT_AUTO_ENCODING) { |
1358 | 244 | case XML_INPUT_AUTO_UTF8: |
1359 | 244 | allowed = allowedUTF8; |
1360 | 244 | autoEnc = "UTF-8"; |
1361 | 244 | break; |
1362 | 0 | case XML_INPUT_AUTO_UTF16LE: |
1363 | 0 | allowed = allowedUTF16LE; |
1364 | 0 | autoEnc = "UTF-16LE"; |
1365 | 0 | break; |
1366 | 0 | case XML_INPUT_AUTO_UTF16BE: |
1367 | 0 | allowed = allowedUTF16BE; |
1368 | 0 | autoEnc = "UTF-16BE"; |
1369 | 0 | break; |
1370 | 244 | } |
1371 | | |
1372 | 244 | if (allowed != NULL) { |
1373 | 244 | const char **p; |
1374 | 244 | int match = 0; |
1375 | | |
1376 | 580 | for (p = allowed; *p != NULL; p++) { |
1377 | 412 | if (xmlStrcasecmp(encoding, BAD_CAST *p) == 0) { |
1378 | 76 | match = 1; |
1379 | 76 | break; |
1380 | 76 | } |
1381 | 412 | } |
1382 | | |
1383 | 244 | if (match == 0) { |
1384 | 168 | xmlWarningMsg(ctxt, XML_WAR_ENCODING_MISMATCH, |
1385 | 168 | "Encoding '%s' doesn't match " |
1386 | 168 | "auto-detected '%s'\n", |
1387 | 168 | encoding, BAD_CAST autoEnc); |
1388 | 168 | xmlFree(encoding); |
1389 | 168 | encoding = xmlStrdup(BAD_CAST autoEnc); |
1390 | 168 | if (encoding == NULL) |
1391 | 0 | xmlCtxtErrMemory(ctxt); |
1392 | 168 | } |
1393 | 244 | } |
1394 | 244 | } |
1395 | | |
1396 | 6.51k | if (ctxt->encoding != NULL) |
1397 | 2.01k | xmlFree((xmlChar *) ctxt->encoding); |
1398 | 6.51k | ctxt->encoding = encoding; |
1399 | 6.51k | } |
1400 | | |
1401 | | /************************************************************************ |
1402 | | * * |
1403 | | * Commodity functions to handle entities processing * |
1404 | | * * |
1405 | | ************************************************************************/ |
1406 | | |
1407 | | /** |
1408 | | * xmlFreeInputStream: |
1409 | | * @input: an xmlParserInputPtr |
1410 | | * |
1411 | | * Free up an input stream. |
1412 | | */ |
1413 | | void |
1414 | 51.1k | xmlFreeInputStream(xmlParserInputPtr input) { |
1415 | 51.1k | if (input == NULL) return; |
1416 | | |
1417 | 50.2k | if (input->filename != NULL) xmlFree((char *) input->filename); |
1418 | 50.2k | if (input->version != NULL) xmlFree((char *) input->version); |
1419 | 50.2k | if ((input->free != NULL) && (input->base != NULL)) |
1420 | 0 | input->free((xmlChar *) input->base); |
1421 | 50.2k | if (input->buf != NULL) |
1422 | 50.2k | xmlFreeParserInputBuffer(input->buf); |
1423 | 50.2k | xmlFree(input); |
1424 | 50.2k | } |
1425 | | |
1426 | | /** |
1427 | | * xmlNewInputStream: |
1428 | | * @ctxt: an XML parser context |
1429 | | * |
1430 | | * Create a new input stream structure. |
1431 | | * |
1432 | | * Returns the new input stream or NULL |
1433 | | */ |
1434 | | xmlParserInputPtr |
1435 | 50.2k | xmlNewInputStream(xmlParserCtxtPtr ctxt) { |
1436 | 50.2k | xmlParserInputPtr input; |
1437 | | |
1438 | 50.2k | input = (xmlParserInputPtr) xmlMalloc(sizeof(xmlParserInput)); |
1439 | 50.2k | if (input == NULL) { |
1440 | 0 | xmlCtxtErrMemory(ctxt); |
1441 | 0 | return(NULL); |
1442 | 0 | } |
1443 | 50.2k | memset(input, 0, sizeof(xmlParserInput)); |
1444 | 50.2k | input->line = 1; |
1445 | 50.2k | input->col = 1; |
1446 | | |
1447 | | /* |
1448 | | * If the context is NULL the id cannot be initialized, but that |
1449 | | * should not happen while parsing which is the situation where |
1450 | | * the id is actually needed. |
1451 | | */ |
1452 | 50.2k | if (ctxt != NULL) { |
1453 | 50.2k | if (input->id >= INT_MAX) { |
1454 | 0 | xmlCtxtErrMemory(ctxt); |
1455 | 0 | return(NULL); |
1456 | 0 | } |
1457 | 50.2k | input->id = ctxt->input_id++; |
1458 | 50.2k | } |
1459 | | |
1460 | 50.2k | return(input); |
1461 | 50.2k | } |
1462 | | |
1463 | | /** |
1464 | | * xmlNewInputURL: |
1465 | | * @ctxt: parser context |
1466 | | * @url: filename or URL |
1467 | | * @publicId: publid ID from doctype (optional) |
1468 | | * @encoding: character encoding (optional) |
1469 | | * @flags: unused, pass 0 |
1470 | | * |
1471 | | * Creates a new parser input from the filesystem, the network or |
1472 | | * a user-defined resource loader. |
1473 | | * |
1474 | | * @url is a filename or URL. If if contains the substring "://", |
1475 | | * it is assumed to be a Legacy Extended IRI. Otherwise, it is |
1476 | | * treated as a filesystem path. |
1477 | | * |
1478 | | * @publicId is an optional XML public ID, typically from a doctype |
1479 | | * declaration. It is used for catalog lookups. |
1480 | | * |
1481 | | * If @encoding is specified, it will override any encodings found |
1482 | | * in XML declarations, text declarations, BOMs, etc. Pass NULL |
1483 | | * for auto-detection. |
1484 | | * |
1485 | | * The following resource loaders will be called if they were |
1486 | | * registered (in order of precedence): |
1487 | | * |
1488 | | * - the global external entity loader set with |
1489 | | * xmlSetExternalEntityLoader |
1490 | | * - the per-thread xmlParserInputBufferCreateFilenameFunc set with |
1491 | | * xmlParserInputBufferCreateFilenameDefault |
1492 | | * - the default loader which will return |
1493 | | * - the result from a matching global input callback set with |
1494 | | * xmlRegisterInputCallbacks |
1495 | | * - a HTTP resource if support is compiled in. |
1496 | | * - a file opened from the filesystem, with automatic detection |
1497 | | * of compressed files if support is compiled in. |
1498 | | * |
1499 | | * The returned input can be passed to xmlCtxtParseDocument or |
1500 | | * htmlCtxtParseDocument. |
1501 | | * |
1502 | | * This function should not be invoked from user-defined resource |
1503 | | * loaders to avoid infinite loops. |
1504 | | * |
1505 | | * Returns a new parser input. |
1506 | | */ |
1507 | | xmlParserInputPtr |
1508 | | xmlNewInputURL(xmlParserCtxtPtr ctxt, const char *url, const char *publicId, |
1509 | 17.0k | const char *encoding, int flags ATTRIBUTE_UNUSED) { |
1510 | 17.0k | xmlParserInputPtr input; |
1511 | | |
1512 | 17.0k | if ((ctxt == NULL) || (url == NULL)) |
1513 | 0 | return(NULL); |
1514 | | |
1515 | 17.0k | input = xmlLoadExternalEntity(url, publicId, ctxt); |
1516 | 17.0k | if (input == NULL) |
1517 | 55 | return(NULL); |
1518 | | |
1519 | 16.9k | if (encoding != NULL) |
1520 | 0 | xmlSwitchInputEncodingName(ctxt, input, encoding); |
1521 | | |
1522 | 16.9k | return(input); |
1523 | 17.0k | } |
1524 | | |
1525 | | /** |
1526 | | * xmlNewInputInternal: |
1527 | | * @ctxt: parser context |
1528 | | * @buf: parser input buffer |
1529 | | * @filename: filename or URL |
1530 | | * @encoding: character encoding (optional) |
1531 | | * |
1532 | | * Internal helper function. |
1533 | | * |
1534 | | * Returns a new parser input. |
1535 | | */ |
1536 | | static xmlParserInputPtr |
1537 | | xmlNewInputInternal(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr buf, |
1538 | 6.22k | const char *filename, const char *encoding) { |
1539 | 6.22k | xmlParserInputPtr input; |
1540 | | |
1541 | 6.22k | input = xmlNewInputStream(ctxt); |
1542 | 6.22k | if (input == NULL) { |
1543 | 0 | xmlFreeParserInputBuffer(buf); |
1544 | 0 | return(NULL); |
1545 | 0 | } |
1546 | | |
1547 | 6.22k | input->buf = buf; |
1548 | 6.22k | xmlBufResetInput(input->buf->buffer, input); |
1549 | | |
1550 | 6.22k | if (filename != NULL) { |
1551 | 0 | input->filename = xmlMemStrdup(filename); |
1552 | 0 | if (input->filename == NULL) { |
1553 | 0 | xmlCtxtErrMemory(ctxt); |
1554 | 0 | xmlFreeInputStream(input); |
1555 | 0 | return(NULL); |
1556 | 0 | } |
1557 | 0 | } |
1558 | | |
1559 | 6.22k | if (encoding != NULL) |
1560 | 0 | xmlSwitchInputEncodingName(ctxt, input, encoding); |
1561 | | |
1562 | 6.22k | return(input); |
1563 | 6.22k | } |
1564 | | |
1565 | | /** |
1566 | | * xmlNewInputMemory: |
1567 | | * @ctxt: parser context |
1568 | | * @url: base URL (optional) |
1569 | | * @mem: pointer to char array |
1570 | | * @size: size of array |
1571 | | * @encoding: character encoding (optional) |
1572 | | * @flags: optimization hints |
1573 | | * |
1574 | | * Creates a new parser input to read from a memory area. |
1575 | | * |
1576 | | * @url is used as base to resolve external entities and for |
1577 | | * error reporting. |
1578 | | * |
1579 | | * If the XML_INPUT_BUF_STATIC flag is set, the memory area must |
1580 | | * stay unchanged until parsing has finished. This can avoid |
1581 | | * temporary copies. |
1582 | | * |
1583 | | * If the XML_INPUT_BUF_ZERO_TERMINATED flag is set, the memory |
1584 | | * area must contain a zero byte after the buffer at position @size. |
1585 | | * This can avoid temporary copies. |
1586 | | * |
1587 | | * Returns a new parser input. |
1588 | | */ |
1589 | | xmlParserInputPtr |
1590 | | xmlNewInputMemory(xmlParserCtxtPtr ctxt, const char *url, |
1591 | | const void *mem, size_t size, |
1592 | 115 | const char *encoding, int flags) { |
1593 | 115 | xmlParserInputBufferPtr buf; |
1594 | | |
1595 | 115 | if ((ctxt == NULL) || (mem == NULL)) |
1596 | 0 | return(NULL); |
1597 | | |
1598 | 115 | buf = xmlNewInputBufferMemory(mem, size, flags, XML_CHAR_ENCODING_NONE); |
1599 | 115 | if (buf == NULL) { |
1600 | 0 | xmlCtxtErrMemory(ctxt); |
1601 | 0 | return(NULL); |
1602 | 0 | } |
1603 | | |
1604 | 115 | return(xmlNewInputInternal(ctxt, buf, url, encoding)); |
1605 | 115 | } |
1606 | | |
1607 | | /** |
1608 | | * xmlNewInputString: |
1609 | | * @ctxt: parser context |
1610 | | * @url: base URL (optional) |
1611 | | * @str: zero-terminated string |
1612 | | * @encoding: character encoding (optional) |
1613 | | * @flags: optimization hints |
1614 | | * |
1615 | | * Creates a new parser input to read from a zero-terminated string. |
1616 | | * |
1617 | | * @url is used as base to resolve external entities and for |
1618 | | * error reporting. |
1619 | | * |
1620 | | * If the XML_INPUT_BUF_STATIC flag is set, the string must |
1621 | | * stay unchanged until parsing has finished. This can avoid |
1622 | | * temporary copies. |
1623 | | * |
1624 | | * Returns a new parser input. |
1625 | | */ |
1626 | | xmlParserInputPtr |
1627 | | xmlNewInputString(xmlParserCtxtPtr ctxt, const char *url, |
1628 | 6.10k | const char *str, const char *encoding, int flags) { |
1629 | 6.10k | xmlParserInputBufferPtr buf; |
1630 | | |
1631 | 6.10k | if ((ctxt == NULL) || (str == NULL)) |
1632 | 0 | return(NULL); |
1633 | | |
1634 | 6.10k | buf = xmlNewInputBufferString(str, flags); |
1635 | 6.10k | if (buf == NULL) { |
1636 | 0 | xmlCtxtErrMemory(ctxt); |
1637 | 0 | return(NULL); |
1638 | 0 | } |
1639 | | |
1640 | 6.10k | return(xmlNewInputInternal(ctxt, buf, url, encoding)); |
1641 | 6.10k | } |
1642 | | |
1643 | | /** |
1644 | | * xmlNewInputFd: |
1645 | | * @ctxt: parser context |
1646 | | * @url: base URL (optional) |
1647 | | * @fd: file descriptor |
1648 | | * @encoding: character encoding (optional) |
1649 | | * @flags: unused, pass 0 |
1650 | | * |
1651 | | * Creates a new parser input to read from a zero-terminated string. |
1652 | | * |
1653 | | * @url is used as base to resolve external entities and for |
1654 | | * error reporting. |
1655 | | * |
1656 | | * @fd is closed after parsing has finished. |
1657 | | * |
1658 | | * Returns a new parser input. |
1659 | | */ |
1660 | | xmlParserInputPtr |
1661 | | xmlNewInputFd(xmlParserCtxtPtr ctxt, const char *url, |
1662 | 0 | int fd, const char *encoding, int flags ATTRIBUTE_UNUSED) { |
1663 | 0 | xmlParserInputBufferPtr buf; |
1664 | |
|
1665 | 0 | if ((ctxt == NULL) || (fd < 0)) |
1666 | 0 | return(NULL); |
1667 | | |
1668 | 0 | buf = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE); |
1669 | 0 | if (buf == NULL) { |
1670 | 0 | xmlCtxtErrMemory(ctxt); |
1671 | 0 | return(NULL); |
1672 | 0 | } |
1673 | | |
1674 | 0 | return(xmlNewInputInternal(ctxt, buf, url, encoding)); |
1675 | 0 | } |
1676 | | |
1677 | | /** |
1678 | | * xmlNewInputIO: |
1679 | | * @ctxt: parser context |
1680 | | * @url: base URL (optional) |
1681 | | * @ioRead: read callback |
1682 | | * @ioClose: close callback (optional) |
1683 | | * @ioCtxt: IO context |
1684 | | * @encoding: character encoding (optional) |
1685 | | * @flags: unused, pass 0 |
1686 | | * |
1687 | | * Creates a new parser input to read from input callbacks and |
1688 | | * cintext. |
1689 | | * |
1690 | | * @url is used as base to resolve external entities and for |
1691 | | * error reporting. |
1692 | | * |
1693 | | * @ioRead is called to read new data into a provided buffer. |
1694 | | * It must return the number of bytes written into the buffer |
1695 | | * ot a negative xmlParserErrors code on failure. |
1696 | | * |
1697 | | * @ioClose is called after parsing has finished. |
1698 | | * |
1699 | | * @ioCtxt is an opaque pointer passed to the callbacks. |
1700 | | * |
1701 | | * Returns a new parser input. |
1702 | | */ |
1703 | | xmlParserInputPtr |
1704 | | xmlNewInputIO(xmlParserCtxtPtr ctxt, const char *url, |
1705 | | xmlInputReadCallback ioRead, xmlInputCloseCallback ioClose, |
1706 | | void *ioCtxt, |
1707 | 0 | const char *encoding, int flags ATTRIBUTE_UNUSED) { |
1708 | 0 | xmlParserInputBufferPtr buf; |
1709 | |
|
1710 | 0 | if ((ctxt == NULL) || (ioRead == NULL)) |
1711 | 0 | return(NULL); |
1712 | | |
1713 | 0 | buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE); |
1714 | 0 | if (buf == NULL) { |
1715 | 0 | xmlCtxtErrMemory(ctxt); |
1716 | 0 | if (ioClose != NULL) |
1717 | 0 | ioClose(ioCtxt); |
1718 | 0 | return(NULL); |
1719 | 0 | } |
1720 | | |
1721 | 0 | buf->context = ioCtxt; |
1722 | 0 | buf->readcallback = ioRead; |
1723 | 0 | buf->closecallback = ioClose; |
1724 | |
|
1725 | 0 | return(xmlNewInputInternal(ctxt, buf, url, encoding)); |
1726 | 0 | } |
1727 | | |
1728 | | /** |
1729 | | * xmlNewInputPush: |
1730 | | * @ctxt: parser context |
1731 | | * @url: base URL (optional) |
1732 | | * @chunk: pointer to char array |
1733 | | * @size: size of array |
1734 | | * @encoding: character encoding (optional) |
1735 | | * |
1736 | | * Creates a new parser input for a push parser. |
1737 | | * |
1738 | | * Returns a new parser input. |
1739 | | */ |
1740 | | xmlParserInputPtr |
1741 | | xmlNewInputPush(xmlParserCtxtPtr ctxt, const char *url, |
1742 | 0 | const char *chunk, int size, const char *encoding) { |
1743 | 0 | xmlParserInputBufferPtr buf; |
1744 | 0 | xmlParserInputPtr input; |
1745 | |
|
1746 | 0 | buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE); |
1747 | 0 | if (buf == NULL) { |
1748 | 0 | xmlCtxtErrMemory(ctxt); |
1749 | 0 | return(NULL); |
1750 | 0 | } |
1751 | | |
1752 | 0 | input = xmlNewInputInternal(ctxt, buf, url, encoding); |
1753 | 0 | if (input == NULL) |
1754 | 0 | return(NULL); |
1755 | | |
1756 | 0 | input->flags |= XML_INPUT_PROGRESSIVE; |
1757 | |
|
1758 | 0 | if ((size > 0) && (chunk != NULL)) { |
1759 | 0 | int res; |
1760 | |
|
1761 | 0 | res = xmlParserInputBufferPush(input->buf, size, chunk); |
1762 | 0 | xmlBufResetInput(input->buf->buffer, input); |
1763 | 0 | if (res < 0) { |
1764 | 0 | xmlCtxtErrIO(ctxt, input->buf->error, NULL); |
1765 | 0 | xmlFreeInputStream(input); |
1766 | 0 | return(NULL); |
1767 | 0 | } |
1768 | 0 | } |
1769 | | |
1770 | 0 | return(input); |
1771 | 0 | } |
1772 | | |
1773 | | /** |
1774 | | * xmlNewIOInputStream: |
1775 | | * @ctxt: an XML parser context |
1776 | | * @input: an I/O Input |
1777 | | * @enc: the charset encoding if known |
1778 | | * |
1779 | | * DEPRECATED: Use xmlNewInputURL, xmlNewInputMemory, etc. |
1780 | | * |
1781 | | * Create a new input stream structure encapsulating the @input into |
1782 | | * a stream suitable for the parser. |
1783 | | * |
1784 | | * Returns the new input stream or NULL |
1785 | | */ |
1786 | | xmlParserInputPtr |
1787 | | xmlNewIOInputStream(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr buf, |
1788 | 0 | xmlCharEncoding enc) { |
1789 | 0 | const char *encoding; |
1790 | |
|
1791 | 0 | if (buf == NULL) |
1792 | 0 | return(NULL); |
1793 | | |
1794 | 0 | encoding = xmlGetCharEncodingName(enc); |
1795 | 0 | return(xmlNewInputInternal(ctxt, buf, NULL, encoding)); |
1796 | 0 | } |
1797 | | |
1798 | | /** |
1799 | | * xmlNewEntityInputStream: |
1800 | | * @ctxt: an XML parser context |
1801 | | * @entity: an Entity pointer |
1802 | | * |
1803 | | * DEPRECATED: Internal function, do not use. |
1804 | | * |
1805 | | * Create a new input stream based on an xmlEntityPtr |
1806 | | * |
1807 | | * Returns the new input stream or NULL |
1808 | | */ |
1809 | | xmlParserInputPtr |
1810 | 30.5k | xmlNewEntityInputStream(xmlParserCtxtPtr ctxt, xmlEntityPtr ent) { |
1811 | 30.5k | xmlParserInputPtr input; |
1812 | | |
1813 | 30.5k | if ((ctxt == NULL) || (ent == NULL)) |
1814 | 0 | return(NULL); |
1815 | | |
1816 | 30.5k | if (ent->content != NULL) { |
1817 | 6.10k | input = xmlNewInputString(ctxt, NULL, (const char *) ent->content, |
1818 | 6.10k | NULL, XML_INPUT_BUF_STATIC); |
1819 | 24.4k | } else if (ent->URI != NULL) { |
1820 | 24.3k | input = xmlLoadExternalEntity((char *) ent->URI, |
1821 | 24.3k | (char *) ent->ExternalID, ctxt); |
1822 | 24.3k | } else { |
1823 | 115 | input = xmlNewInputMemory(ctxt, NULL, "", 0, NULL, |
1824 | 115 | XML_INPUT_BUF_STATIC | |
1825 | 115 | XML_INPUT_BUF_ZERO_TERMINATED); |
1826 | 115 | } |
1827 | | |
1828 | 30.5k | if (input == NULL) |
1829 | 1.40k | return(NULL); |
1830 | | |
1831 | 29.1k | input->entity = ent; |
1832 | | |
1833 | 29.1k | return(input); |
1834 | 30.5k | } |
1835 | | |
1836 | | /** |
1837 | | * xmlNewStringInputStream: |
1838 | | * @ctxt: an XML parser context |
1839 | | * @buffer: an memory buffer |
1840 | | * |
1841 | | * DEPRECATED: Use xmlNewInputString. |
1842 | | * |
1843 | | * Create a new input stream based on a memory buffer. |
1844 | | * |
1845 | | * Returns the new input stream |
1846 | | */ |
1847 | | xmlParserInputPtr |
1848 | 0 | xmlNewStringInputStream(xmlParserCtxtPtr ctxt, const xmlChar *buffer) { |
1849 | 0 | return(xmlNewInputString(ctxt, NULL, (const char *) buffer, NULL, 0)); |
1850 | 0 | } |
1851 | | |
1852 | | |
1853 | | /**************************************************************** |
1854 | | * * |
1855 | | * External entities loading * |
1856 | | * * |
1857 | | ****************************************************************/ |
1858 | | |
1859 | | #ifdef LIBXML_CATALOG_ENABLED |
1860 | | |
1861 | | /** |
1862 | | * xmlResolveResourceFromCatalog: |
1863 | | * @URL: the URL for the entity to load |
1864 | | * @ID: the System ID for the entity to load |
1865 | | * @ctxt: the context in which the entity is called or NULL |
1866 | | * |
1867 | | * Resolves the URL and ID against the appropriate catalog. |
1868 | | * This function is used by xmlDefaultExternalEntityLoader and |
1869 | | * xmlNoNetExternalEntityLoader. |
1870 | | * |
1871 | | * Returns a new allocated URL, or NULL. |
1872 | | */ |
1873 | | static xmlChar * |
1874 | | xmlResolveResourceFromCatalog(const char *URL, const char *ID, |
1875 | 0 | xmlParserCtxtPtr ctxt) { |
1876 | 0 | xmlChar *resource = NULL; |
1877 | 0 | xmlCatalogAllow pref; |
1878 | | |
1879 | | /* |
1880 | | * If the resource doesn't exists as a file, |
1881 | | * try to load it from the resource pointed in the catalogs |
1882 | | */ |
1883 | 0 | pref = xmlCatalogGetDefaults(); |
1884 | |
|
1885 | 0 | if ((pref != XML_CATA_ALLOW_NONE) && (!xmlNoNetExists(URL))) { |
1886 | | /* |
1887 | | * Do a local lookup |
1888 | | */ |
1889 | 0 | if ((ctxt != NULL) && (ctxt->catalogs != NULL) && |
1890 | 0 | ((pref == XML_CATA_ALLOW_ALL) || |
1891 | 0 | (pref == XML_CATA_ALLOW_DOCUMENT))) { |
1892 | 0 | resource = xmlCatalogLocalResolve(ctxt->catalogs, |
1893 | 0 | (const xmlChar *)ID, |
1894 | 0 | (const xmlChar *)URL); |
1895 | 0 | } |
1896 | | /* |
1897 | | * Try a global lookup |
1898 | | */ |
1899 | 0 | if ((resource == NULL) && |
1900 | 0 | ((pref == XML_CATA_ALLOW_ALL) || |
1901 | 0 | (pref == XML_CATA_ALLOW_GLOBAL))) { |
1902 | 0 | resource = xmlCatalogResolve((const xmlChar *)ID, |
1903 | 0 | (const xmlChar *)URL); |
1904 | 0 | } |
1905 | 0 | if ((resource == NULL) && (URL != NULL)) |
1906 | 0 | resource = xmlStrdup((const xmlChar *) URL); |
1907 | | |
1908 | | /* |
1909 | | * TODO: do an URI lookup on the reference |
1910 | | */ |
1911 | 0 | if ((resource != NULL) && (!xmlNoNetExists((const char *)resource))) { |
1912 | 0 | xmlChar *tmp = NULL; |
1913 | |
|
1914 | 0 | if ((ctxt != NULL) && (ctxt->catalogs != NULL) && |
1915 | 0 | ((pref == XML_CATA_ALLOW_ALL) || |
1916 | 0 | (pref == XML_CATA_ALLOW_DOCUMENT))) { |
1917 | 0 | tmp = xmlCatalogLocalResolveURI(ctxt->catalogs, resource); |
1918 | 0 | } |
1919 | 0 | if ((tmp == NULL) && |
1920 | 0 | ((pref == XML_CATA_ALLOW_ALL) || |
1921 | 0 | (pref == XML_CATA_ALLOW_GLOBAL))) { |
1922 | 0 | tmp = xmlCatalogResolveURI(resource); |
1923 | 0 | } |
1924 | |
|
1925 | 0 | if (tmp != NULL) { |
1926 | 0 | xmlFree(resource); |
1927 | 0 | resource = tmp; |
1928 | 0 | } |
1929 | 0 | } |
1930 | 0 | } |
1931 | |
|
1932 | 0 | return resource; |
1933 | 0 | } |
1934 | | |
1935 | | #endif |
1936 | | |
1937 | | /** |
1938 | | * xmlCheckHTTPInput: |
1939 | | * @ctxt: an XML parser context |
1940 | | * @ret: an XML parser input |
1941 | | * |
1942 | | * DEPRECATED: Internal function, don't use. |
1943 | | * |
1944 | | * Check an input in case it was created from an HTTP stream, in that |
1945 | | * case it will handle encoding and update of the base URL in case of |
1946 | | * redirection. It also checks for HTTP errors in which case the input |
1947 | | * is cleanly freed up and an appropriate error is raised in context |
1948 | | * |
1949 | | * Returns the input or NULL in case of HTTP error. |
1950 | | */ |
1951 | | xmlParserInputPtr |
1952 | 0 | xmlCheckHTTPInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr ret) { |
1953 | | /* Avoid unused variable warning if features are disabled. */ |
1954 | 0 | (void) ctxt; |
1955 | |
|
1956 | | #ifdef LIBXML_HTTP_ENABLED |
1957 | | if ((ret != NULL) && (ret->buf != NULL) && |
1958 | | (ret->buf->readcallback == xmlIOHTTPRead) && |
1959 | | (ret->buf->context != NULL)) { |
1960 | | const char *encoding; |
1961 | | const char *redir; |
1962 | | const char *mime; |
1963 | | int code; |
1964 | | |
1965 | | code = xmlNanoHTTPReturnCode(ret->buf->context); |
1966 | | if (code >= 400) { |
1967 | | /* fatal error */ |
1968 | | if (ret->filename != NULL) |
1969 | | xmlCtxtErrIO(ctxt, XML_IO_LOAD_ERROR, ret->filename); |
1970 | | else |
1971 | | xmlCtxtErrIO(ctxt, XML_IO_LOAD_ERROR, "<null>"); |
1972 | | xmlFreeInputStream(ret); |
1973 | | ret = NULL; |
1974 | | } else { |
1975 | | |
1976 | | mime = xmlNanoHTTPMimeType(ret->buf->context); |
1977 | | if ((xmlStrstr(BAD_CAST mime, BAD_CAST "/xml")) || |
1978 | | (xmlStrstr(BAD_CAST mime, BAD_CAST "+xml"))) { |
1979 | | encoding = xmlNanoHTTPEncoding(ret->buf->context); |
1980 | | if (encoding != NULL) |
1981 | | xmlSwitchEncodingName(ctxt, encoding); |
1982 | | #if 0 |
1983 | | } else if (xmlStrstr(BAD_CAST mime, BAD_CAST "html")) { |
1984 | | #endif |
1985 | | } |
1986 | | redir = xmlNanoHTTPRedir(ret->buf->context); |
1987 | | if (redir != NULL) { |
1988 | | if (ret->filename != NULL) |
1989 | | xmlFree((xmlChar *) ret->filename); |
1990 | | ret->filename = |
1991 | | (char *) xmlStrdup((const xmlChar *) redir); |
1992 | | } |
1993 | | } |
1994 | | } |
1995 | | #endif |
1996 | 0 | return(ret); |
1997 | 0 | } |
1998 | | |
1999 | | /** |
2000 | | * xmlNewInputFromFile: |
2001 | | * @ctxt: an XML parser context |
2002 | | * @filename: the filename to use as entity |
2003 | | * |
2004 | | * DEPRECATED: Use xmlNewInputURL. |
2005 | | * |
2006 | | * Create a new input stream based on a file or an URL. |
2007 | | * |
2008 | | * Returns the new input stream or NULL in case of error |
2009 | | */ |
2010 | | xmlParserInputPtr |
2011 | 0 | xmlNewInputFromFile(xmlParserCtxtPtr ctxt, const char *filename) { |
2012 | 0 | xmlParserInputBufferPtr buf; |
2013 | 0 | xmlParserInputPtr inputStream; |
2014 | 0 | xmlChar *URI = NULL; |
2015 | 0 | int code; |
2016 | |
|
2017 | 0 | if ((ctxt == NULL) || (filename == NULL)) |
2018 | 0 | return(NULL); |
2019 | | |
2020 | 0 | code = xmlParserInputBufferCreateFilenameSafe(filename, |
2021 | 0 | XML_CHAR_ENCODING_NONE, &buf); |
2022 | 0 | if (buf == NULL) { |
2023 | 0 | xmlCtxtErrIO(ctxt, code, filename); |
2024 | 0 | return(NULL); |
2025 | 0 | } |
2026 | | |
2027 | 0 | inputStream = xmlNewInputStream(ctxt); |
2028 | 0 | if (inputStream == NULL) { |
2029 | 0 | xmlFreeParserInputBuffer(buf); |
2030 | 0 | return(NULL); |
2031 | 0 | } |
2032 | | |
2033 | 0 | inputStream->buf = buf; |
2034 | 0 | inputStream = xmlCheckHTTPInput(ctxt, inputStream); |
2035 | 0 | if (inputStream == NULL) |
2036 | 0 | return(NULL); |
2037 | | |
2038 | 0 | if (inputStream->filename == NULL) |
2039 | 0 | URI = xmlStrdup((xmlChar *) filename); |
2040 | 0 | else |
2041 | 0 | URI = xmlStrdup((xmlChar *) inputStream->filename); |
2042 | 0 | if (inputStream->filename != NULL) xmlFree((char *)inputStream->filename); |
2043 | 0 | inputStream->filename = (char *) xmlCanonicPath((const xmlChar *) URI); |
2044 | 0 | if (URI != NULL) xmlFree((char *) URI); |
2045 | |
|
2046 | 0 | xmlBufResetInput(inputStream->buf->buffer, inputStream); |
2047 | |
|
2048 | 0 | return(inputStream); |
2049 | 0 | } |
2050 | | |
2051 | | /** |
2052 | | * xmlDefaultExternalEntityLoader: |
2053 | | * @URL: the URL for the entity to load |
2054 | | * @ID: the System ID for the entity to load |
2055 | | * @ctxt: the context in which the entity is called or NULL |
2056 | | * |
2057 | | * By default we don't load external entities, yet. |
2058 | | * |
2059 | | * Returns a new allocated xmlParserInputPtr, or NULL. |
2060 | | */ |
2061 | | static xmlParserInputPtr |
2062 | | xmlDefaultExternalEntityLoader(const char *URL, const char *ID, |
2063 | | xmlParserCtxtPtr ctxt) |
2064 | 0 | { |
2065 | 0 | xmlParserInputPtr ret = NULL; |
2066 | 0 | xmlChar *resource = NULL; |
2067 | |
|
2068 | 0 | if (URL == NULL) |
2069 | 0 | return(NULL); |
2070 | | |
2071 | 0 | if ((ctxt != NULL) && (ctxt->options & XML_PARSE_NONET)) { |
2072 | 0 | int options = ctxt->options; |
2073 | |
|
2074 | 0 | ctxt->options -= XML_PARSE_NONET; |
2075 | 0 | ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt); |
2076 | 0 | ctxt->options = options; |
2077 | 0 | return(ret); |
2078 | 0 | } |
2079 | 0 | #ifdef LIBXML_CATALOG_ENABLED |
2080 | 0 | resource = xmlResolveResourceFromCatalog(URL, ID, ctxt); |
2081 | 0 | #endif |
2082 | |
|
2083 | 0 | if (resource == NULL) |
2084 | 0 | resource = (xmlChar *) URL; |
2085 | |
|
2086 | 0 | ret = xmlNewInputFromFile(ctxt, (const char *) resource); |
2087 | 0 | if ((resource != NULL) && (resource != (xmlChar *) URL)) |
2088 | 0 | xmlFree(resource); |
2089 | 0 | return (ret); |
2090 | 0 | } |
2091 | | |
2092 | | /** |
2093 | | * xmlNoNetExternalEntityLoader: |
2094 | | * @URL: the URL for the entity to load |
2095 | | * @ID: the System ID for the entity to load |
2096 | | * @ctxt: the context in which the entity is called or NULL |
2097 | | * |
2098 | | * A specific entity loader disabling network accesses, though still |
2099 | | * allowing local catalog accesses for resolution. |
2100 | | * |
2101 | | * Returns a new allocated xmlParserInputPtr, or NULL. |
2102 | | */ |
2103 | | xmlParserInputPtr |
2104 | | xmlNoNetExternalEntityLoader(const char *URL, const char *ID, |
2105 | 0 | xmlParserCtxtPtr ctxt) { |
2106 | 0 | xmlParserInputPtr input = NULL; |
2107 | 0 | xmlChar *resource = NULL; |
2108 | |
|
2109 | 0 | #ifdef LIBXML_CATALOG_ENABLED |
2110 | 0 | resource = xmlResolveResourceFromCatalog(URL, ID, ctxt); |
2111 | 0 | #endif |
2112 | |
|
2113 | 0 | if (resource == NULL) |
2114 | 0 | resource = (xmlChar *) URL; |
2115 | |
|
2116 | 0 | if (resource != NULL) { |
2117 | 0 | if ((!xmlStrncasecmp(BAD_CAST resource, BAD_CAST "ftp://", 6)) || |
2118 | 0 | (!xmlStrncasecmp(BAD_CAST resource, BAD_CAST "http://", 7))) { |
2119 | 0 | xmlCtxtErrIO(ctxt, XML_IO_NETWORK_ATTEMPT, |
2120 | 0 | (const char *) resource); |
2121 | | /* |
2122 | | * Also forward the error directly to the global error |
2123 | | * handler, which the XML::LibXML test suite expects. |
2124 | | */ |
2125 | 0 | __xmlIOErr(XML_FROM_IO, XML_IO_NETWORK_ATTEMPT, |
2126 | 0 | (const char *) resource); |
2127 | 0 | if (resource != (xmlChar *) URL) |
2128 | 0 | xmlFree(resource); |
2129 | 0 | return(NULL); |
2130 | 0 | } |
2131 | 0 | } |
2132 | 0 | input = xmlDefaultExternalEntityLoader((const char *) resource, ID, ctxt); |
2133 | 0 | if (resource != (xmlChar *) URL) |
2134 | 0 | xmlFree(resource); |
2135 | 0 | return(input); |
2136 | 0 | } |
2137 | | |
2138 | | /* |
2139 | | * This global has to die eventually |
2140 | | */ |
2141 | | static xmlExternalEntityLoader |
2142 | | xmlCurrentExternalEntityLoader = xmlDefaultExternalEntityLoader; |
2143 | | |
2144 | | /** |
2145 | | * xmlSetExternalEntityLoader: |
2146 | | * @f: the new entity resolver function |
2147 | | * |
2148 | | * Changes the defaultexternal entity resolver function for the application |
2149 | | */ |
2150 | | void |
2151 | 2 | xmlSetExternalEntityLoader(xmlExternalEntityLoader f) { |
2152 | 2 | xmlCurrentExternalEntityLoader = f; |
2153 | 2 | } |
2154 | | |
2155 | | /** |
2156 | | * xmlGetExternalEntityLoader: |
2157 | | * |
2158 | | * Get the default external entity resolver function for the application |
2159 | | * |
2160 | | * Returns the xmlExternalEntityLoader function pointer |
2161 | | */ |
2162 | | xmlExternalEntityLoader |
2163 | 0 | xmlGetExternalEntityLoader(void) { |
2164 | 0 | return(xmlCurrentExternalEntityLoader); |
2165 | 0 | } |
2166 | | |
2167 | | /** |
2168 | | * xmlLoadExternalEntity: |
2169 | | * @URL: the URL for the entity to load |
2170 | | * @ID: the Public ID for the entity to load |
2171 | | * @ctxt: the context in which the entity is called or NULL |
2172 | | * |
2173 | | * DEPRECATED: Use xmlNewInputURL. |
2174 | | * |
2175 | | * Returns the xmlParserInputPtr or NULL |
2176 | | */ |
2177 | | xmlParserInputPtr |
2178 | | xmlLoadExternalEntity(const char *URL, const char *ID, |
2179 | 45.7k | xmlParserCtxtPtr ctxt) { |
2180 | 45.7k | char *canonicFilename; |
2181 | 45.7k | xmlParserInputPtr ret; |
2182 | | |
2183 | 45.7k | if (URL == NULL) |
2184 | 44 | return(NULL); |
2185 | | |
2186 | 45.7k | canonicFilename = (char *) xmlCanonicPath((const xmlChar *) URL); |
2187 | 45.7k | if (canonicFilename == NULL) { |
2188 | 0 | xmlCtxtErrMemory(ctxt); |
2189 | 0 | return(NULL); |
2190 | 0 | } |
2191 | | |
2192 | 45.7k | ret = xmlCurrentExternalEntityLoader(canonicFilename, ID, ctxt); |
2193 | 45.7k | xmlFree(canonicFilename); |
2194 | 45.7k | return(ret); |
2195 | 45.7k | } |
2196 | | |
2197 | | /************************************************************************ |
2198 | | * * |
2199 | | * Commodity functions to handle parser contexts * |
2200 | | * * |
2201 | | ************************************************************************/ |
2202 | | |
2203 | | /** |
2204 | | * xmlInitSAXParserCtxt: |
2205 | | * @ctxt: XML parser context |
2206 | | * @sax: SAX handlert |
2207 | | * @userData: user data |
2208 | | * |
2209 | | * Initialize a SAX parser context |
2210 | | * |
2211 | | * Returns 0 in case of success and -1 in case of error |
2212 | | */ |
2213 | | |
2214 | | static int |
2215 | | xmlInitSAXParserCtxt(xmlParserCtxtPtr ctxt, const xmlSAXHandler *sax, |
2216 | | void *userData) |
2217 | 17.0k | { |
2218 | 17.0k | xmlParserInputPtr input; |
2219 | | |
2220 | 17.0k | if (ctxt == NULL) |
2221 | 0 | return(-1); |
2222 | | |
2223 | 17.0k | if (ctxt->dict == NULL) |
2224 | 17.0k | ctxt->dict = xmlDictCreate(); |
2225 | 17.0k | if (ctxt->dict == NULL) |
2226 | 0 | return(-1); |
2227 | 17.0k | xmlDictSetLimit(ctxt->dict, XML_MAX_DICTIONARY_LIMIT); |
2228 | | |
2229 | 17.0k | if (ctxt->sax == NULL) |
2230 | 17.0k | ctxt->sax = (xmlSAXHandler *) xmlMalloc(sizeof(xmlSAXHandler)); |
2231 | 17.0k | if (ctxt->sax == NULL) |
2232 | 0 | return(-1); |
2233 | 17.0k | if (sax == NULL) { |
2234 | 17.0k | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
2235 | 17.0k | xmlSAXVersion(ctxt->sax, 2); |
2236 | 17.0k | ctxt->userData = ctxt; |
2237 | 17.0k | } else { |
2238 | 0 | if (sax->initialized == XML_SAX2_MAGIC) { |
2239 | 0 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandler)); |
2240 | 0 | } else { |
2241 | 0 | memset(ctxt->sax, 0, sizeof(xmlSAXHandler)); |
2242 | 0 | memcpy(ctxt->sax, sax, sizeof(xmlSAXHandlerV1)); |
2243 | 0 | } |
2244 | 0 | ctxt->userData = userData ? userData : ctxt; |
2245 | 0 | } |
2246 | | |
2247 | 17.0k | ctxt->maxatts = 0; |
2248 | 17.0k | ctxt->atts = NULL; |
2249 | | /* Allocate the Input stack */ |
2250 | 17.0k | if (ctxt->inputTab == NULL) { |
2251 | 17.0k | ctxt->inputTab = (xmlParserInputPtr *) |
2252 | 17.0k | xmlMalloc(5 * sizeof(xmlParserInputPtr)); |
2253 | 17.0k | ctxt->inputMax = 5; |
2254 | 17.0k | } |
2255 | 17.0k | if (ctxt->inputTab == NULL) |
2256 | 0 | return(-1); |
2257 | 17.0k | while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */ |
2258 | 0 | xmlFreeInputStream(input); |
2259 | 0 | } |
2260 | 17.0k | ctxt->inputNr = 0; |
2261 | 17.0k | ctxt->input = NULL; |
2262 | | |
2263 | 17.0k | ctxt->version = NULL; |
2264 | 17.0k | ctxt->encoding = NULL; |
2265 | 17.0k | ctxt->standalone = -1; |
2266 | 17.0k | ctxt->hasExternalSubset = 0; |
2267 | 17.0k | ctxt->hasPErefs = 0; |
2268 | 17.0k | ctxt->html = 0; |
2269 | 17.0k | ctxt->instate = XML_PARSER_START; |
2270 | | |
2271 | | /* Allocate the Node stack */ |
2272 | 17.0k | if (ctxt->nodeTab == NULL) { |
2273 | 17.0k | ctxt->nodeTab = (xmlNodePtr *) xmlMalloc(10 * sizeof(xmlNodePtr)); |
2274 | 17.0k | ctxt->nodeMax = 10; |
2275 | 17.0k | } |
2276 | 17.0k | if (ctxt->nodeTab == NULL) |
2277 | 0 | return(-1); |
2278 | 17.0k | ctxt->nodeNr = 0; |
2279 | 17.0k | ctxt->node = NULL; |
2280 | | |
2281 | | /* Allocate the Name stack */ |
2282 | 17.0k | if (ctxt->nameTab == NULL) { |
2283 | 17.0k | ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *)); |
2284 | 17.0k | ctxt->nameMax = 10; |
2285 | 17.0k | } |
2286 | 17.0k | if (ctxt->nameTab == NULL) |
2287 | 0 | return(-1); |
2288 | 17.0k | ctxt->nameNr = 0; |
2289 | 17.0k | ctxt->name = NULL; |
2290 | | |
2291 | | /* Allocate the space stack */ |
2292 | 17.0k | if (ctxt->spaceTab == NULL) { |
2293 | 17.0k | ctxt->spaceTab = (int *) xmlMalloc(10 * sizeof(int)); |
2294 | 17.0k | ctxt->spaceMax = 10; |
2295 | 17.0k | } |
2296 | 17.0k | if (ctxt->spaceTab == NULL) |
2297 | 0 | return(-1); |
2298 | 17.0k | ctxt->spaceNr = 1; |
2299 | 17.0k | ctxt->spaceMax = 10; |
2300 | 17.0k | ctxt->spaceTab[0] = -1; |
2301 | 17.0k | ctxt->space = &ctxt->spaceTab[0]; |
2302 | 17.0k | ctxt->myDoc = NULL; |
2303 | 17.0k | ctxt->wellFormed = 1; |
2304 | 17.0k | ctxt->nsWellFormed = 1; |
2305 | 17.0k | ctxt->valid = 1; |
2306 | | |
2307 | 17.0k | ctxt->options = XML_PARSE_NODICT; |
2308 | | |
2309 | | /* |
2310 | | * Initialize some parser options from deprecated global variables. |
2311 | | * Note that the "modern" API taking options arguments or |
2312 | | * xmlCtxtSetOptions will ignore these defaults. They're only |
2313 | | * relevant if old API functions like xmlParseFile are used. |
2314 | | */ |
2315 | 17.0k | ctxt->loadsubset = xmlLoadExtDtdDefaultValue; |
2316 | 17.0k | if (ctxt->loadsubset) { |
2317 | 0 | ctxt->options |= XML_PARSE_DTDLOAD; |
2318 | 0 | } |
2319 | 17.0k | ctxt->validate = xmlDoValidityCheckingDefaultValue; |
2320 | 17.0k | if (ctxt->validate) { |
2321 | 0 | ctxt->options |= XML_PARSE_DTDVALID; |
2322 | 0 | } |
2323 | 17.0k | ctxt->pedantic = xmlPedanticParserDefaultValue; |
2324 | 17.0k | if (ctxt->pedantic) { |
2325 | 0 | ctxt->options |= XML_PARSE_PEDANTIC; |
2326 | 0 | } |
2327 | 17.0k | ctxt->linenumbers = xmlLineNumbersDefaultValue; |
2328 | 17.0k | ctxt->keepBlanks = xmlKeepBlanksDefaultValue; |
2329 | 17.0k | if (ctxt->keepBlanks == 0) { |
2330 | 0 | ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace; |
2331 | 0 | ctxt->options |= XML_PARSE_NOBLANKS; |
2332 | 0 | } |
2333 | 17.0k | ctxt->replaceEntities = xmlSubstituteEntitiesDefaultValue; |
2334 | 17.0k | if (ctxt->replaceEntities) { |
2335 | 0 | ctxt->options |= XML_PARSE_NOENT; |
2336 | 0 | } |
2337 | 17.0k | if (xmlGetWarningsDefaultValue == 0) |
2338 | 0 | ctxt->options |= XML_PARSE_NOWARNING; |
2339 | | |
2340 | 17.0k | ctxt->vctxt.flags = XML_VCTXT_USE_PCTXT; |
2341 | 17.0k | ctxt->vctxt.userData = ctxt; |
2342 | 17.0k | ctxt->vctxt.error = xmlParserValidityError; |
2343 | 17.0k | ctxt->vctxt.warning = xmlParserValidityWarning; |
2344 | | |
2345 | 17.0k | ctxt->record_info = 0; |
2346 | 17.0k | ctxt->checkIndex = 0; |
2347 | 17.0k | ctxt->inSubset = 0; |
2348 | 17.0k | ctxt->errNo = XML_ERR_OK; |
2349 | 17.0k | ctxt->depth = 0; |
2350 | 17.0k | ctxt->catalogs = NULL; |
2351 | 17.0k | ctxt->sizeentities = 0; |
2352 | 17.0k | ctxt->sizeentcopy = 0; |
2353 | 17.0k | ctxt->input_id = 1; |
2354 | 17.0k | ctxt->maxAmpl = XML_MAX_AMPLIFICATION_DEFAULT; |
2355 | 17.0k | xmlInitNodeInfoSeq(&ctxt->node_seq); |
2356 | | |
2357 | 17.0k | if (ctxt->nsdb == NULL) { |
2358 | 17.0k | ctxt->nsdb = xmlParserNsCreate(); |
2359 | 17.0k | if (ctxt->nsdb == NULL) { |
2360 | 0 | xmlCtxtErrMemory(ctxt); |
2361 | 0 | return(-1); |
2362 | 0 | } |
2363 | 17.0k | } |
2364 | | |
2365 | 17.0k | return(0); |
2366 | 17.0k | } |
2367 | | |
2368 | | /** |
2369 | | * xmlInitParserCtxt: |
2370 | | * @ctxt: an XML parser context |
2371 | | * |
2372 | | * DEPRECATED: Internal function which will be made private in a future |
2373 | | * version. |
2374 | | * |
2375 | | * Initialize a parser context |
2376 | | * |
2377 | | * Returns 0 in case of success and -1 in case of error |
2378 | | */ |
2379 | | |
2380 | | int |
2381 | | xmlInitParserCtxt(xmlParserCtxtPtr ctxt) |
2382 | 0 | { |
2383 | 0 | return(xmlInitSAXParserCtxt(ctxt, NULL, NULL)); |
2384 | 0 | } |
2385 | | |
2386 | | /** |
2387 | | * xmlFreeParserCtxt: |
2388 | | * @ctxt: an XML parser context |
2389 | | * |
2390 | | * Free all the memory used by a parser context. However the parsed |
2391 | | * document in ctxt->myDoc is not freed. |
2392 | | */ |
2393 | | |
2394 | | void |
2395 | | xmlFreeParserCtxt(xmlParserCtxtPtr ctxt) |
2396 | 17.0k | { |
2397 | 17.0k | xmlParserInputPtr input; |
2398 | | |
2399 | 17.0k | if (ctxt == NULL) return; |
2400 | | |
2401 | 17.0k | while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */ |
2402 | 0 | xmlFreeInputStream(input); |
2403 | 0 | } |
2404 | 17.0k | if (ctxt->spaceTab != NULL) xmlFree(ctxt->spaceTab); |
2405 | 17.0k | if (ctxt->nameTab != NULL) xmlFree((xmlChar * *)ctxt->nameTab); |
2406 | 17.0k | if (ctxt->nodeTab != NULL) xmlFree(ctxt->nodeTab); |
2407 | 17.0k | if (ctxt->nodeInfoTab != NULL) xmlFree(ctxt->nodeInfoTab); |
2408 | 17.0k | if (ctxt->inputTab != NULL) xmlFree(ctxt->inputTab); |
2409 | 17.0k | if (ctxt->version != NULL) xmlFree((char *) ctxt->version); |
2410 | 17.0k | if (ctxt->encoding != NULL) xmlFree((char *) ctxt->encoding); |
2411 | 17.0k | if (ctxt->extSubURI != NULL) xmlFree((char *) ctxt->extSubURI); |
2412 | 17.0k | if (ctxt->extSubSystem != NULL) xmlFree((char *) ctxt->extSubSystem); |
2413 | 17.0k | #ifdef LIBXML_SAX1_ENABLED |
2414 | 17.0k | if ((ctxt->sax != NULL) && |
2415 | 17.0k | (ctxt->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)) |
2416 | | #else |
2417 | | if (ctxt->sax != NULL) |
2418 | | #endif /* LIBXML_SAX1_ENABLED */ |
2419 | 17.0k | xmlFree(ctxt->sax); |
2420 | 17.0k | if (ctxt->vctxt.nodeTab != NULL) xmlFree(ctxt->vctxt.nodeTab); |
2421 | 17.0k | if (ctxt->atts != NULL) xmlFree((xmlChar * *)ctxt->atts); |
2422 | 17.0k | if (ctxt->dict != NULL) xmlDictFree(ctxt->dict); |
2423 | 17.0k | if (ctxt->nsTab != NULL) xmlFree(ctxt->nsTab); |
2424 | 17.0k | if (ctxt->nsdb != NULL) xmlParserNsFree(ctxt->nsdb); |
2425 | 17.0k | if (ctxt->attrHash != NULL) xmlFree(ctxt->attrHash); |
2426 | 17.0k | if (ctxt->pushTab != NULL) xmlFree(ctxt->pushTab); |
2427 | 17.0k | if (ctxt->attallocs != NULL) xmlFree(ctxt->attallocs); |
2428 | 17.0k | if (ctxt->attsDefault != NULL) |
2429 | 2.03k | xmlHashFree(ctxt->attsDefault, xmlHashDefaultDeallocator); |
2430 | 17.0k | if (ctxt->attsSpecial != NULL) |
2431 | 2.17k | xmlHashFree(ctxt->attsSpecial, NULL); |
2432 | 17.0k | if (ctxt->freeElems != NULL) { |
2433 | 0 | xmlNodePtr cur, next; |
2434 | |
|
2435 | 0 | cur = ctxt->freeElems; |
2436 | 0 | while (cur != NULL) { |
2437 | 0 | next = cur->next; |
2438 | 0 | xmlFree(cur); |
2439 | 0 | cur = next; |
2440 | 0 | } |
2441 | 0 | } |
2442 | 17.0k | if (ctxt->freeAttrs != NULL) { |
2443 | 0 | xmlAttrPtr cur, next; |
2444 | |
|
2445 | 0 | cur = ctxt->freeAttrs; |
2446 | 0 | while (cur != NULL) { |
2447 | 0 | next = cur->next; |
2448 | 0 | xmlFree(cur); |
2449 | 0 | cur = next; |
2450 | 0 | } |
2451 | 0 | } |
2452 | | /* |
2453 | | * cleanup the error strings |
2454 | | */ |
2455 | 17.0k | if (ctxt->lastError.message != NULL) |
2456 | 12.2k | xmlFree(ctxt->lastError.message); |
2457 | 17.0k | if (ctxt->lastError.file != NULL) |
2458 | 12.2k | xmlFree(ctxt->lastError.file); |
2459 | 17.0k | if (ctxt->lastError.str1 != NULL) |
2460 | 7.18k | xmlFree(ctxt->lastError.str1); |
2461 | 17.0k | if (ctxt->lastError.str2 != NULL) |
2462 | 1.79k | xmlFree(ctxt->lastError.str2); |
2463 | 17.0k | if (ctxt->lastError.str3 != NULL) |
2464 | 253 | xmlFree(ctxt->lastError.str3); |
2465 | | |
2466 | 17.0k | #ifdef LIBXML_CATALOG_ENABLED |
2467 | 17.0k | if (ctxt->catalogs != NULL) |
2468 | 0 | xmlCatalogFreeLocal(ctxt->catalogs); |
2469 | 17.0k | #endif |
2470 | 17.0k | xmlFree(ctxt); |
2471 | 17.0k | } |
2472 | | |
2473 | | /** |
2474 | | * xmlNewParserCtxt: |
2475 | | * |
2476 | | * Allocate and initialize a new parser context. |
2477 | | * |
2478 | | * Returns the xmlParserCtxtPtr or NULL |
2479 | | */ |
2480 | | |
2481 | | xmlParserCtxtPtr |
2482 | | xmlNewParserCtxt(void) |
2483 | 17.0k | { |
2484 | 17.0k | return(xmlNewSAXParserCtxt(NULL, NULL)); |
2485 | 17.0k | } |
2486 | | |
2487 | | /** |
2488 | | * xmlNewSAXParserCtxt: |
2489 | | * @sax: SAX handler |
2490 | | * @userData: user data |
2491 | | * |
2492 | | * Allocate and initialize a new SAX parser context. If userData is NULL, |
2493 | | * the parser context will be passed as user data. |
2494 | | * |
2495 | | * Available since 2.11.0. If you want support older versions, |
2496 | | * it's best to invoke xmlNewParserCtxt and set ctxt->sax with |
2497 | | * struct assignment. |
2498 | | * |
2499 | | * Returns the xmlParserCtxtPtr or NULL if memory allocation failed. |
2500 | | */ |
2501 | | |
2502 | | xmlParserCtxtPtr |
2503 | | xmlNewSAXParserCtxt(const xmlSAXHandler *sax, void *userData) |
2504 | 17.0k | { |
2505 | 17.0k | xmlParserCtxtPtr ctxt; |
2506 | | |
2507 | 17.0k | xmlInitParser(); |
2508 | | |
2509 | 17.0k | ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt)); |
2510 | 17.0k | if (ctxt == NULL) |
2511 | 0 | return(NULL); |
2512 | 17.0k | memset(ctxt, 0, sizeof(xmlParserCtxt)); |
2513 | 17.0k | if (xmlInitSAXParserCtxt(ctxt, sax, userData) < 0) { |
2514 | 0 | xmlFreeParserCtxt(ctxt); |
2515 | 0 | return(NULL); |
2516 | 0 | } |
2517 | 17.0k | return(ctxt); |
2518 | 17.0k | } |
2519 | | |
2520 | | /************************************************************************ |
2521 | | * * |
2522 | | * Handling of node information * |
2523 | | * * |
2524 | | ************************************************************************/ |
2525 | | |
2526 | | /** |
2527 | | * xmlClearParserCtxt: |
2528 | | * @ctxt: an XML parser context |
2529 | | * |
2530 | | * Clear (release owned resources) and reinitialize a parser context |
2531 | | */ |
2532 | | |
2533 | | void |
2534 | | xmlClearParserCtxt(xmlParserCtxtPtr ctxt) |
2535 | 0 | { |
2536 | 0 | if (ctxt==NULL) |
2537 | 0 | return; |
2538 | 0 | xmlClearNodeInfoSeq(&ctxt->node_seq); |
2539 | 0 | xmlCtxtReset(ctxt); |
2540 | 0 | } |
2541 | | |
2542 | | |
2543 | | /** |
2544 | | * xmlParserFindNodeInfo: |
2545 | | * @ctx: an XML parser context |
2546 | | * @node: an XML node within the tree |
2547 | | * |
2548 | | * DEPRECATED: Don't use. |
2549 | | * |
2550 | | * Find the parser node info struct for a given node |
2551 | | * |
2552 | | * Returns an xmlParserNodeInfo block pointer or NULL |
2553 | | */ |
2554 | | const xmlParserNodeInfo * |
2555 | | xmlParserFindNodeInfo(xmlParserCtxtPtr ctx, xmlNodePtr node) |
2556 | 0 | { |
2557 | 0 | unsigned long pos; |
2558 | |
|
2559 | 0 | if ((ctx == NULL) || (node == NULL)) |
2560 | 0 | return (NULL); |
2561 | | /* Find position where node should be at */ |
2562 | 0 | pos = xmlParserFindNodeInfoIndex(&ctx->node_seq, node); |
2563 | 0 | if (pos < ctx->node_seq.length |
2564 | 0 | && ctx->node_seq.buffer[pos].node == node) |
2565 | 0 | return &ctx->node_seq.buffer[pos]; |
2566 | 0 | else |
2567 | 0 | return NULL; |
2568 | 0 | } |
2569 | | |
2570 | | |
2571 | | /** |
2572 | | * xmlInitNodeInfoSeq: |
2573 | | * @seq: a node info sequence pointer |
2574 | | * |
2575 | | * DEPRECATED: Don't use. |
2576 | | * |
2577 | | * -- Initialize (set to initial state) node info sequence |
2578 | | */ |
2579 | | void |
2580 | | xmlInitNodeInfoSeq(xmlParserNodeInfoSeqPtr seq) |
2581 | 34.0k | { |
2582 | 34.0k | if (seq == NULL) |
2583 | 0 | return; |
2584 | 34.0k | seq->length = 0; |
2585 | 34.0k | seq->maximum = 0; |
2586 | 34.0k | seq->buffer = NULL; |
2587 | 34.0k | } |
2588 | | |
2589 | | /** |
2590 | | * xmlClearNodeInfoSeq: |
2591 | | * @seq: a node info sequence pointer |
2592 | | * |
2593 | | * DEPRECATED: Don't use. |
2594 | | * |
2595 | | * -- Clear (release memory and reinitialize) node |
2596 | | * info sequence |
2597 | | */ |
2598 | | void |
2599 | | xmlClearNodeInfoSeq(xmlParserNodeInfoSeqPtr seq) |
2600 | 0 | { |
2601 | 0 | if (seq == NULL) |
2602 | 0 | return; |
2603 | 0 | if (seq->buffer != NULL) |
2604 | 0 | xmlFree(seq->buffer); |
2605 | 0 | xmlInitNodeInfoSeq(seq); |
2606 | 0 | } |
2607 | | |
2608 | | /** |
2609 | | * xmlParserFindNodeInfoIndex: |
2610 | | * @seq: a node info sequence pointer |
2611 | | * @node: an XML node pointer |
2612 | | * |
2613 | | * DEPRECATED: Don't use. |
2614 | | * |
2615 | | * xmlParserFindNodeInfoIndex : Find the index that the info record for |
2616 | | * the given node is or should be at in a sorted sequence |
2617 | | * |
2618 | | * Returns a long indicating the position of the record |
2619 | | */ |
2620 | | unsigned long |
2621 | | xmlParserFindNodeInfoIndex(xmlParserNodeInfoSeqPtr seq, |
2622 | | xmlNodePtr node) |
2623 | 0 | { |
2624 | 0 | unsigned long upper, lower, middle; |
2625 | 0 | int found = 0; |
2626 | |
|
2627 | 0 | if ((seq == NULL) || (node == NULL)) |
2628 | 0 | return ((unsigned long) -1); |
2629 | | |
2630 | | /* Do a binary search for the key */ |
2631 | 0 | lower = 1; |
2632 | 0 | upper = seq->length; |
2633 | 0 | middle = 0; |
2634 | 0 | while (lower <= upper && !found) { |
2635 | 0 | middle = lower + (upper - lower) / 2; |
2636 | 0 | if (node == seq->buffer[middle - 1].node) |
2637 | 0 | found = 1; |
2638 | 0 | else if (node < seq->buffer[middle - 1].node) |
2639 | 0 | upper = middle - 1; |
2640 | 0 | else |
2641 | 0 | lower = middle + 1; |
2642 | 0 | } |
2643 | | |
2644 | | /* Return position */ |
2645 | 0 | if (middle == 0 || seq->buffer[middle - 1].node < node) |
2646 | 0 | return middle; |
2647 | 0 | else |
2648 | 0 | return middle - 1; |
2649 | 0 | } |
2650 | | |
2651 | | |
2652 | | /** |
2653 | | * xmlParserAddNodeInfo: |
2654 | | * @ctxt: an XML parser context |
2655 | | * @info: a node info sequence pointer |
2656 | | * |
2657 | | * DEPRECATED: Don't use. |
2658 | | * |
2659 | | * Insert node info record into the sorted sequence |
2660 | | */ |
2661 | | void |
2662 | | xmlParserAddNodeInfo(xmlParserCtxtPtr ctxt, |
2663 | | xmlParserNodeInfoPtr info) |
2664 | 0 | { |
2665 | 0 | unsigned long pos; |
2666 | |
|
2667 | 0 | if ((ctxt == NULL) || (info == NULL)) return; |
2668 | | |
2669 | | /* Find pos and check to see if node is already in the sequence */ |
2670 | 0 | pos = xmlParserFindNodeInfoIndex(&ctxt->node_seq, (xmlNodePtr) |
2671 | 0 | info->node); |
2672 | |
|
2673 | 0 | if ((pos < ctxt->node_seq.length) && |
2674 | 0 | (ctxt->node_seq.buffer != NULL) && |
2675 | 0 | (ctxt->node_seq.buffer[pos].node == info->node)) { |
2676 | 0 | ctxt->node_seq.buffer[pos] = *info; |
2677 | 0 | } |
2678 | | |
2679 | | /* Otherwise, we need to add new node to buffer */ |
2680 | 0 | else { |
2681 | 0 | if ((ctxt->node_seq.length + 1 > ctxt->node_seq.maximum) || |
2682 | 0 | (ctxt->node_seq.buffer == NULL)) { |
2683 | 0 | xmlParserNodeInfo *tmp_buffer; |
2684 | 0 | unsigned int byte_size; |
2685 | |
|
2686 | 0 | if (ctxt->node_seq.maximum == 0) |
2687 | 0 | ctxt->node_seq.maximum = 2; |
2688 | 0 | byte_size = (sizeof(*ctxt->node_seq.buffer) * |
2689 | 0 | (2 * ctxt->node_seq.maximum)); |
2690 | |
|
2691 | 0 | if (ctxt->node_seq.buffer == NULL) |
2692 | 0 | tmp_buffer = (xmlParserNodeInfo *) xmlMalloc(byte_size); |
2693 | 0 | else |
2694 | 0 | tmp_buffer = |
2695 | 0 | (xmlParserNodeInfo *) xmlRealloc(ctxt->node_seq.buffer, |
2696 | 0 | byte_size); |
2697 | |
|
2698 | 0 | if (tmp_buffer == NULL) { |
2699 | 0 | xmlCtxtErrMemory(ctxt); |
2700 | 0 | return; |
2701 | 0 | } |
2702 | 0 | ctxt->node_seq.buffer = tmp_buffer; |
2703 | 0 | ctxt->node_seq.maximum *= 2; |
2704 | 0 | } |
2705 | | |
2706 | | /* If position is not at end, move elements out of the way */ |
2707 | 0 | if (pos != ctxt->node_seq.length) { |
2708 | 0 | unsigned long i; |
2709 | |
|
2710 | 0 | for (i = ctxt->node_seq.length; i > pos; i--) |
2711 | 0 | ctxt->node_seq.buffer[i] = ctxt->node_seq.buffer[i - 1]; |
2712 | 0 | } |
2713 | | |
2714 | | /* Copy element and increase length */ |
2715 | 0 | ctxt->node_seq.buffer[pos] = *info; |
2716 | 0 | ctxt->node_seq.length++; |
2717 | 0 | } |
2718 | 0 | } |
2719 | | |
2720 | | /************************************************************************ |
2721 | | * * |
2722 | | * Defaults settings * |
2723 | | * * |
2724 | | ************************************************************************/ |
2725 | | /** |
2726 | | * xmlPedanticParserDefault: |
2727 | | * @val: int 0 or 1 |
2728 | | * |
2729 | | * DEPRECATED: Use the modern options API with XML_PARSE_PEDANTIC. |
2730 | | * |
2731 | | * Set and return the previous value for enabling pedantic warnings. |
2732 | | * |
2733 | | * Returns the last value for 0 for no substitution, 1 for substitution. |
2734 | | */ |
2735 | | |
2736 | | int |
2737 | 0 | xmlPedanticParserDefault(int val) { |
2738 | 0 | int old = xmlPedanticParserDefaultValue; |
2739 | |
|
2740 | 0 | xmlPedanticParserDefaultValue = val; |
2741 | 0 | return(old); |
2742 | 0 | } |
2743 | | |
2744 | | /** |
2745 | | * xmlLineNumbersDefault: |
2746 | | * @val: int 0 or 1 |
2747 | | * |
2748 | | * DEPRECATED: The modern options API always enables line numbers. |
2749 | | * |
2750 | | * Set and return the previous value for enabling line numbers in elements |
2751 | | * contents. This may break on old application and is turned off by default. |
2752 | | * |
2753 | | * Returns the last value for 0 for no substitution, 1 for substitution. |
2754 | | */ |
2755 | | |
2756 | | int |
2757 | 0 | xmlLineNumbersDefault(int val) { |
2758 | 0 | int old = xmlLineNumbersDefaultValue; |
2759 | |
|
2760 | 0 | xmlLineNumbersDefaultValue = val; |
2761 | 0 | return(old); |
2762 | 0 | } |
2763 | | |
2764 | | /** |
2765 | | * xmlSubstituteEntitiesDefault: |
2766 | | * @val: int 0 or 1 |
2767 | | * |
2768 | | * DEPRECATED: Use the modern options API with XML_PARSE_NOENT. |
2769 | | * |
2770 | | * Set and return the previous value for default entity support. |
2771 | | * Initially the parser always keep entity references instead of substituting |
2772 | | * entity values in the output. This function has to be used to change the |
2773 | | * default parser behavior |
2774 | | * SAX::substituteEntities() has to be used for changing that on a file by |
2775 | | * file basis. |
2776 | | * |
2777 | | * Returns the last value for 0 for no substitution, 1 for substitution. |
2778 | | */ |
2779 | | |
2780 | | int |
2781 | 0 | xmlSubstituteEntitiesDefault(int val) { |
2782 | 0 | int old = xmlSubstituteEntitiesDefaultValue; |
2783 | |
|
2784 | 0 | xmlSubstituteEntitiesDefaultValue = val; |
2785 | 0 | return(old); |
2786 | 0 | } |
2787 | | |
2788 | | /** |
2789 | | * xmlKeepBlanksDefault: |
2790 | | * @val: int 0 or 1 |
2791 | | * |
2792 | | * DEPRECATED: Use the modern options API with XML_PARSE_NOBLANKS. |
2793 | | * |
2794 | | * Set and return the previous value for default blanks text nodes support. |
2795 | | * The 1.x version of the parser used an heuristic to try to detect |
2796 | | * ignorable white spaces. As a result the SAX callback was generating |
2797 | | * xmlSAX2IgnorableWhitespace() callbacks instead of characters() one, and when |
2798 | | * using the DOM output text nodes containing those blanks were not generated. |
2799 | | * The 2.x and later version will switch to the XML standard way and |
2800 | | * ignorableWhitespace() are only generated when running the parser in |
2801 | | * validating mode and when the current element doesn't allow CDATA or |
2802 | | * mixed content. |
2803 | | * This function is provided as a way to force the standard behavior |
2804 | | * on 1.X libs and to switch back to the old mode for compatibility when |
2805 | | * running 1.X client code on 2.X . Upgrade of 1.X code should be done |
2806 | | * by using xmlIsBlankNode() commodity function to detect the "empty" |
2807 | | * nodes generated. |
2808 | | * This value also affect autogeneration of indentation when saving code |
2809 | | * if blanks sections are kept, indentation is not generated. |
2810 | | * |
2811 | | * Returns the last value for 0 for no substitution, 1 for substitution. |
2812 | | */ |
2813 | | |
2814 | | int |
2815 | 0 | xmlKeepBlanksDefault(int val) { |
2816 | 0 | int old = xmlKeepBlanksDefaultValue; |
2817 | |
|
2818 | 0 | xmlKeepBlanksDefaultValue = val; |
2819 | 0 | #ifdef LIBXML_OUTPUT_ENABLED |
2820 | 0 | if (!val) |
2821 | 0 | xmlIndentTreeOutput = 1; |
2822 | 0 | #endif |
2823 | 0 | return(old); |
2824 | 0 | } |
2825 | | |