Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * xmlIO.c : implementation of the I/O interfaces used by the parser |
3 | | * |
4 | | * See Copyright for the status of this software. |
5 | | * |
6 | | * daniel@veillard.com |
7 | | */ |
8 | | |
9 | | #define IN_LIBXML |
10 | | #include "libxml.h" |
11 | | |
12 | | #include <string.h> |
13 | | #include <stdlib.h> |
14 | | #include <errno.h> |
15 | | |
16 | | #ifdef HAVE_SYS_STAT_H |
17 | | #include <sys/stat.h> |
18 | | #endif |
19 | | #ifdef HAVE_FCNTL_H |
20 | | #include <fcntl.h> |
21 | | #endif |
22 | | #ifdef HAVE_UNISTD_H |
23 | | #include <unistd.h> |
24 | | #endif |
25 | | #ifdef LIBXML_ZLIB_ENABLED |
26 | | #include <zlib.h> |
27 | | #endif |
28 | | #ifdef LIBXML_LZMA_ENABLED |
29 | | #include <lzma.h> |
30 | | #endif |
31 | | |
32 | | #if defined(_WIN32) |
33 | | #define WIN32_LEAN_AND_MEAN |
34 | | #include <windows.h> |
35 | | #include <io.h> |
36 | | #include <direct.h> |
37 | | #endif |
38 | | |
39 | | #include <libxml/xmlIO.h> |
40 | | #include <libxml/xmlmemory.h> |
41 | | #include <libxml/uri.h> |
42 | | #include <libxml/nanohttp.h> |
43 | | #include <libxml/nanoftp.h> |
44 | | #include <libxml/xmlerror.h> |
45 | | #ifdef LIBXML_CATALOG_ENABLED |
46 | | #include <libxml/catalog.h> |
47 | | #endif |
48 | | |
49 | | #include "private/buf.h" |
50 | | #include "private/enc.h" |
51 | | #include "private/error.h" |
52 | | #include "private/io.h" |
53 | | #include "private/parser.h" |
54 | | |
55 | | /* #define VERBOSE_FAILURE */ |
56 | | |
57 | 127M | #define MINLEN 4000 |
58 | | |
59 | | #ifndef STDOUT_FILENO |
60 | | #define STDOUT_FILENO 1 |
61 | | #endif |
62 | | |
63 | | #ifndef S_ISDIR |
64 | | # ifdef _S_ISDIR |
65 | | # define S_ISDIR(x) _S_ISDIR(x) |
66 | | # elif defined(S_IFDIR) |
67 | | # ifdef S_IFMT |
68 | | # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) |
69 | | # elif defined(_S_IFMT) |
70 | | # define S_ISDIR(m) (((m) & _S_IFMT) == S_IFDIR) |
71 | | # endif |
72 | | # endif |
73 | | #endif |
74 | | |
75 | | /* |
76 | | * Input I/O callback sets |
77 | | */ |
78 | | typedef struct _xmlInputCallback { |
79 | | xmlInputMatchCallback matchcallback; |
80 | | xmlInputOpenCallback opencallback; |
81 | | xmlInputReadCallback readcallback; |
82 | | xmlInputCloseCallback closecallback; |
83 | | } xmlInputCallback; |
84 | | |
85 | | /* This dummy function only marks default IO in the callback table */ |
86 | | static int |
87 | | xmlIODefaultMatch(const char *filename); |
88 | | |
89 | 0 | #define MAX_INPUT_CALLBACK 10 |
90 | | |
91 | | static xmlInputCallback xmlInputCallbackTable[MAX_INPUT_CALLBACK]; |
92 | | static int xmlInputCallbackNr; |
93 | | |
94 | | #ifdef LIBXML_OUTPUT_ENABLED |
95 | | /* |
96 | | * Output I/O callback sets |
97 | | */ |
98 | | typedef struct _xmlOutputCallback { |
99 | | xmlOutputMatchCallback matchcallback; |
100 | | xmlOutputOpenCallback opencallback; |
101 | | xmlOutputWriteCallback writecallback; |
102 | | xmlOutputCloseCallback closecallback; |
103 | | } xmlOutputCallback; |
104 | | |
105 | 0 | #define MAX_OUTPUT_CALLBACK 10 |
106 | | |
107 | | static xmlOutputCallback xmlOutputCallbackTable[MAX_OUTPUT_CALLBACK]; |
108 | | static int xmlOutputCallbackNr; |
109 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
110 | | |
111 | | /************************************************************************ |
112 | | * * |
113 | | * Error handling * |
114 | | * * |
115 | | ************************************************************************/ |
116 | | |
117 | | /** |
118 | | * xmlIOErrMemory: |
119 | | * @extra: extra information |
120 | | * |
121 | | * Handle an out of memory condition |
122 | | */ |
123 | | static void |
124 | | xmlIOErrMemory(void) |
125 | 0 | { |
126 | 0 | xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_IO, NULL); |
127 | 0 | } |
128 | | |
129 | | /** |
130 | | * __xmlIOErr: |
131 | | * @code: the error number |
132 | | * @ |
133 | | * @extra: extra information |
134 | | * |
135 | | * Handle an I/O error |
136 | | */ |
137 | | int |
138 | | __xmlIOErr(int domain, int code, const char *extra) |
139 | 51 | { |
140 | 51 | xmlStructuredErrorFunc schannel = NULL; |
141 | 51 | xmlGenericErrorFunc channel = NULL; |
142 | 51 | void *data = NULL; |
143 | 51 | const char *fmt, *arg1, *arg2; |
144 | 51 | int res; |
145 | | |
146 | 51 | if (code == 0) { |
147 | 0 | if (errno == 0) code = XML_IO_UNKNOWN; |
148 | 0 | #ifdef EACCES |
149 | 0 | else if (errno == EACCES) code = XML_IO_EACCES; |
150 | 0 | #endif |
151 | 0 | #ifdef EAGAIN |
152 | 0 | else if (errno == EAGAIN) code = XML_IO_EAGAIN; |
153 | 0 | #endif |
154 | 0 | #ifdef EBADF |
155 | 0 | else if (errno == EBADF) code = XML_IO_EBADF; |
156 | 0 | #endif |
157 | 0 | #ifdef EBADMSG |
158 | 0 | else if (errno == EBADMSG) code = XML_IO_EBADMSG; |
159 | 0 | #endif |
160 | 0 | #ifdef EBUSY |
161 | 0 | else if (errno == EBUSY) code = XML_IO_EBUSY; |
162 | 0 | #endif |
163 | 0 | #ifdef ECANCELED |
164 | 0 | else if (errno == ECANCELED) code = XML_IO_ECANCELED; |
165 | 0 | #endif |
166 | 0 | #ifdef ECHILD |
167 | 0 | else if (errno == ECHILD) code = XML_IO_ECHILD; |
168 | 0 | #endif |
169 | 0 | #ifdef EDEADLK |
170 | 0 | else if (errno == EDEADLK) code = XML_IO_EDEADLK; |
171 | 0 | #endif |
172 | 0 | #ifdef EDOM |
173 | 0 | else if (errno == EDOM) code = XML_IO_EDOM; |
174 | 0 | #endif |
175 | 0 | #ifdef EEXIST |
176 | 0 | else if (errno == EEXIST) code = XML_IO_EEXIST; |
177 | 0 | #endif |
178 | 0 | #ifdef EFAULT |
179 | 0 | else if (errno == EFAULT) code = XML_IO_EFAULT; |
180 | 0 | #endif |
181 | 0 | #ifdef EFBIG |
182 | 0 | else if (errno == EFBIG) code = XML_IO_EFBIG; |
183 | 0 | #endif |
184 | 0 | #ifdef EINPROGRESS |
185 | 0 | else if (errno == EINPROGRESS) code = XML_IO_EINPROGRESS; |
186 | 0 | #endif |
187 | 0 | #ifdef EINTR |
188 | 0 | else if (errno == EINTR) code = XML_IO_EINTR; |
189 | 0 | #endif |
190 | 0 | #ifdef EINVAL |
191 | 0 | else if (errno == EINVAL) code = XML_IO_EINVAL; |
192 | 0 | #endif |
193 | 0 | #ifdef EIO |
194 | 0 | else if (errno == EIO) code = XML_IO_EIO; |
195 | 0 | #endif |
196 | 0 | #ifdef EISDIR |
197 | 0 | else if (errno == EISDIR) code = XML_IO_EISDIR; |
198 | 0 | #endif |
199 | 0 | #ifdef EMFILE |
200 | 0 | else if (errno == EMFILE) code = XML_IO_EMFILE; |
201 | 0 | #endif |
202 | 0 | #ifdef EMLINK |
203 | 0 | else if (errno == EMLINK) code = XML_IO_EMLINK; |
204 | 0 | #endif |
205 | 0 | #ifdef EMSGSIZE |
206 | 0 | else if (errno == EMSGSIZE) code = XML_IO_EMSGSIZE; |
207 | 0 | #endif |
208 | 0 | #ifdef ENAMETOOLONG |
209 | 0 | else if (errno == ENAMETOOLONG) code = XML_IO_ENAMETOOLONG; |
210 | 0 | #endif |
211 | 0 | #ifdef ENFILE |
212 | 0 | else if (errno == ENFILE) code = XML_IO_ENFILE; |
213 | 0 | #endif |
214 | 0 | #ifdef ENODEV |
215 | 0 | else if (errno == ENODEV) code = XML_IO_ENODEV; |
216 | 0 | #endif |
217 | 0 | #ifdef ENOENT |
218 | 0 | else if (errno == ENOENT) code = XML_IO_ENOENT; |
219 | 0 | #endif |
220 | 0 | #ifdef ENOEXEC |
221 | 0 | else if (errno == ENOEXEC) code = XML_IO_ENOEXEC; |
222 | 0 | #endif |
223 | 0 | #ifdef ENOLCK |
224 | 0 | else if (errno == ENOLCK) code = XML_IO_ENOLCK; |
225 | 0 | #endif |
226 | 0 | #ifdef ENOMEM |
227 | 0 | else if (errno == ENOMEM) code = XML_IO_ENOMEM; |
228 | 0 | #endif |
229 | 0 | #ifdef ENOSPC |
230 | 0 | else if (errno == ENOSPC) code = XML_IO_ENOSPC; |
231 | 0 | #endif |
232 | 0 | #ifdef ENOSYS |
233 | 0 | else if (errno == ENOSYS) code = XML_IO_ENOSYS; |
234 | 0 | #endif |
235 | 0 | #ifdef ENOTDIR |
236 | 0 | else if (errno == ENOTDIR) code = XML_IO_ENOTDIR; |
237 | 0 | #endif |
238 | 0 | #ifdef ENOTEMPTY |
239 | 0 | else if (errno == ENOTEMPTY) code = XML_IO_ENOTEMPTY; |
240 | 0 | #endif |
241 | 0 | #ifdef ENOTSUP |
242 | 0 | else if (errno == ENOTSUP) code = XML_IO_ENOTSUP; |
243 | 0 | #endif |
244 | 0 | #ifdef ENOTTY |
245 | 0 | else if (errno == ENOTTY) code = XML_IO_ENOTTY; |
246 | 0 | #endif |
247 | 0 | #ifdef ENXIO |
248 | 0 | else if (errno == ENXIO) code = XML_IO_ENXIO; |
249 | 0 | #endif |
250 | 0 | #ifdef EPERM |
251 | 0 | else if (errno == EPERM) code = XML_IO_EPERM; |
252 | 0 | #endif |
253 | 0 | #ifdef EPIPE |
254 | 0 | else if (errno == EPIPE) code = XML_IO_EPIPE; |
255 | 0 | #endif |
256 | 0 | #ifdef ERANGE |
257 | 0 | else if (errno == ERANGE) code = XML_IO_ERANGE; |
258 | 0 | #endif |
259 | 0 | #ifdef EROFS |
260 | 0 | else if (errno == EROFS) code = XML_IO_EROFS; |
261 | 0 | #endif |
262 | 0 | #ifdef ESPIPE |
263 | 0 | else if (errno == ESPIPE) code = XML_IO_ESPIPE; |
264 | 0 | #endif |
265 | 0 | #ifdef ESRCH |
266 | 0 | else if (errno == ESRCH) code = XML_IO_ESRCH; |
267 | 0 | #endif |
268 | 0 | #ifdef ETIMEDOUT |
269 | 0 | else if (errno == ETIMEDOUT) code = XML_IO_ETIMEDOUT; |
270 | 0 | #endif |
271 | 0 | #ifdef EXDEV |
272 | 0 | else if (errno == EXDEV) code = XML_IO_EXDEV; |
273 | 0 | #endif |
274 | 0 | #ifdef ENOTSOCK |
275 | 0 | else if (errno == ENOTSOCK) code = XML_IO_ENOTSOCK; |
276 | 0 | #endif |
277 | 0 | #ifdef EISCONN |
278 | 0 | else if (errno == EISCONN) code = XML_IO_EISCONN; |
279 | 0 | #endif |
280 | 0 | #ifdef ECONNREFUSED |
281 | 0 | else if (errno == ECONNREFUSED) code = XML_IO_ECONNREFUSED; |
282 | 0 | #endif |
283 | 0 | #ifdef ETIMEDOUT |
284 | 0 | else if (errno == ETIMEDOUT) code = XML_IO_ETIMEDOUT; |
285 | 0 | #endif |
286 | 0 | #ifdef ENETUNREACH |
287 | 0 | else if (errno == ENETUNREACH) code = XML_IO_ENETUNREACH; |
288 | 0 | #endif |
289 | 0 | #ifdef EADDRINUSE |
290 | 0 | else if (errno == EADDRINUSE) code = XML_IO_EADDRINUSE; |
291 | 0 | #endif |
292 | 0 | #ifdef EINPROGRESS |
293 | 0 | else if (errno == EINPROGRESS) code = XML_IO_EINPROGRESS; |
294 | 0 | #endif |
295 | 0 | #ifdef EALREADY |
296 | 0 | else if (errno == EALREADY) code = XML_IO_EALREADY; |
297 | 0 | #endif |
298 | 0 | #ifdef EAFNOSUPPORT |
299 | 0 | else if (errno == EAFNOSUPPORT) code = XML_IO_EAFNOSUPPORT; |
300 | 0 | #endif |
301 | 0 | else code = XML_IO_UNKNOWN; |
302 | 0 | } |
303 | | |
304 | 51 | if (xmlStructuredError) { |
305 | 0 | schannel = xmlStructuredError; |
306 | 0 | data = xmlStructuredErrorContext; |
307 | 51 | } else { |
308 | 51 | channel = xmlGenericError; |
309 | 51 | data = xmlGenericErrorContext; |
310 | 51 | } |
311 | | |
312 | 51 | if (extra != NULL) { |
313 | 0 | fmt = "%s: %s"; |
314 | 51 | } else { |
315 | 51 | fmt = "%s"; |
316 | 51 | } |
317 | | |
318 | 51 | arg1 = xmlErrString(code); |
319 | 51 | arg2 = extra; |
320 | | |
321 | 51 | res = __xmlRaiseError(schannel, channel, data, NULL, NULL, |
322 | 51 | domain, code, XML_ERR_ERROR, NULL, 0, |
323 | 51 | extra, NULL, NULL, 0, 0, |
324 | 51 | fmt, arg1, arg2); |
325 | 51 | if (res < 0) { |
326 | 0 | xmlIOErrMemory(); |
327 | 0 | return(XML_ERR_NO_MEMORY); |
328 | 0 | } |
329 | | |
330 | 51 | return(code); |
331 | 51 | } |
332 | | |
333 | | /** |
334 | | * xmlIOErr: |
335 | | * @code: the error number |
336 | | * @extra: extra information |
337 | | * |
338 | | * Handle an I/O error |
339 | | */ |
340 | | static int |
341 | | xmlIOErr(int code, const char *extra) |
342 | 51 | { |
343 | 51 | return(__xmlIOErr(XML_FROM_IO, code, extra)); |
344 | 51 | } |
345 | | |
346 | | /************************************************************************ |
347 | | * * |
348 | | * Standard I/O for file accesses * |
349 | | * * |
350 | | ************************************************************************/ |
351 | | |
352 | | #if defined(_WIN32) |
353 | | |
354 | | /** |
355 | | * __xmlIOWin32UTF8ToWChar: |
356 | | * @u8String: uft-8 string |
357 | | * |
358 | | * Convert a string from utf-8 to wchar (WINDOWS ONLY!) |
359 | | */ |
360 | | static wchar_t * |
361 | | __xmlIOWin32UTF8ToWChar(const char *u8String) |
362 | | { |
363 | | wchar_t *wString = NULL; |
364 | | int i; |
365 | | |
366 | | if (u8String) { |
367 | | int wLen = |
368 | | MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, u8String, |
369 | | -1, NULL, 0); |
370 | | if (wLen) { |
371 | | wString = xmlMalloc(wLen * sizeof(wchar_t)); |
372 | | if (wString) { |
373 | | if (MultiByteToWideChar |
374 | | (CP_UTF8, 0, u8String, -1, wString, wLen) == 0) { |
375 | | xmlFree(wString); |
376 | | wString = NULL; |
377 | | } |
378 | | } |
379 | | |
380 | | /* |
381 | | * Convert to backward slash |
382 | | */ |
383 | | for (i = 0; wString[i] != 0; i++) { |
384 | | if (wString[i] == '/') |
385 | | wString[i] = '\\'; |
386 | | } |
387 | | } |
388 | | } |
389 | | |
390 | | return wString; |
391 | | } |
392 | | |
393 | | #endif |
394 | | |
395 | | /** |
396 | | * xmlNormalizeWindowsPath: |
397 | | * @path: the input file path |
398 | | * |
399 | | * DEPRECATED: This never really worked. |
400 | | * |
401 | | * Returns a copy of path. |
402 | | */ |
403 | | xmlChar * |
404 | | xmlNormalizeWindowsPath(const xmlChar *path) |
405 | 0 | { |
406 | 0 | return xmlStrdup(path); |
407 | 0 | } |
408 | | |
409 | | /** |
410 | | * xmlCheckFilename: |
411 | | * @path: the path to check |
412 | | * |
413 | | * DEPRECATED: Internal function, don't use. |
414 | | * |
415 | | * if stat is not available on the target machine, |
416 | | * returns 1. if stat fails, returns 0 (if calling |
417 | | * stat on the filename fails, it can't be right). |
418 | | * if stat succeeds and the file is a directory, |
419 | | * returns 2. otherwise returns 1. |
420 | | */ |
421 | | int |
422 | | xmlCheckFilename(const char *path) |
423 | 0 | { |
424 | 0 | #ifdef HAVE_STAT |
425 | | #if defined(_WIN32) |
426 | | struct _stat stat_buffer; |
427 | | #else |
428 | 0 | struct stat stat_buffer; |
429 | 0 | #endif |
430 | 0 | int res; |
431 | 0 | #endif |
432 | |
|
433 | 0 | if (path == NULL) |
434 | 0 | return(0); |
435 | | |
436 | 0 | #ifdef HAVE_STAT |
437 | | #if defined(_WIN32) |
438 | | { |
439 | | wchar_t *wpath; |
440 | | |
441 | | /* |
442 | | * On Windows stat and wstat do not work with long pathname, |
443 | | * which start with '\\?\' |
444 | | */ |
445 | | if ((path[0] == '\\') && (path[1] == '\\') && (path[2] == '?') && |
446 | | (path[3] == '\\') ) |
447 | | return 1; |
448 | | |
449 | | wpath = __xmlIOWin32UTF8ToWChar(path); |
450 | | if (wpath == NULL) |
451 | | return(0); |
452 | | res = _wstat(wpath, &stat_buffer); |
453 | | xmlFree(wpath); |
454 | | } |
455 | | #else |
456 | 0 | res = stat(path, &stat_buffer); |
457 | 0 | #endif |
458 | |
|
459 | 0 | if (res < 0) |
460 | 0 | return 0; |
461 | | |
462 | 0 | #ifdef S_ISDIR |
463 | 0 | if (S_ISDIR(stat_buffer.st_mode)) |
464 | 0 | return 2; |
465 | 0 | #endif |
466 | 0 | #endif /* HAVE_STAT */ |
467 | | |
468 | 0 | return 1; |
469 | 0 | } |
470 | | |
471 | | static int |
472 | 0 | xmlConvertUriToPath(const char *uri, char **out) { |
473 | 0 | const char *escaped; |
474 | 0 | char *unescaped; |
475 | |
|
476 | 0 | *out = NULL; |
477 | |
|
478 | 0 | if (!xmlStrncasecmp(BAD_CAST uri, BAD_CAST "file://localhost/", 17)) { |
479 | 0 | escaped = &uri[16]; |
480 | 0 | } else if (!xmlStrncasecmp(BAD_CAST uri, BAD_CAST "file:///", 8)) { |
481 | 0 | escaped = &uri[7]; |
482 | 0 | } else if (!xmlStrncasecmp(BAD_CAST uri, BAD_CAST "file:/", 6)) { |
483 | | /* lots of generators seems to lazy to read RFC 1738 */ |
484 | 0 | escaped = &uri[5]; |
485 | 0 | } else { |
486 | 0 | return(1); |
487 | 0 | } |
488 | | |
489 | | #ifdef _WIN32 |
490 | | /* Ignore slash like in file:///C:/file.txt */ |
491 | | escaped += 1; |
492 | | #endif |
493 | | |
494 | 0 | unescaped = xmlURIUnescapeString(escaped, 0, NULL); |
495 | 0 | if (unescaped == NULL) |
496 | 0 | return(-1); |
497 | | |
498 | 0 | *out = unescaped; |
499 | 0 | return(0); |
500 | 0 | } |
501 | | |
502 | | /** |
503 | | * xmlFdOpen: |
504 | | * @filename: the URI for matching |
505 | | * @out: pointer to resulting context |
506 | | * |
507 | | * Returns an xmlParserErrors code |
508 | | */ |
509 | | static int |
510 | 0 | xmlFdOpen(const char *filename, int write, int *out) { |
511 | 0 | char *fromUri = NULL; |
512 | 0 | int flags; |
513 | 0 | int fd; |
514 | 0 | int ret; |
515 | |
|
516 | 0 | *out = -1; |
517 | 0 | if (filename == NULL) |
518 | 0 | return(XML_ERR_ARGUMENT); |
519 | | |
520 | 0 | if (xmlConvertUriToPath(filename, &fromUri) < 0) |
521 | 0 | return(XML_ERR_NO_MEMORY); |
522 | | |
523 | 0 | if (fromUri != NULL) |
524 | 0 | filename = fromUri; |
525 | |
|
526 | | #if defined(_WIN32) |
527 | | { |
528 | | wchar_t *wpath; |
529 | | |
530 | | wpath = __xmlIOWin32UTF8ToWChar(filename); |
531 | | if (wpath == NULL) { |
532 | | xmlFree(fromUri); |
533 | | return(XML_ERR_NO_MEMORY); |
534 | | } |
535 | | if (write) |
536 | | flags = _O_WRONLY | _O_CREAT | _O_TRUNC; |
537 | | else |
538 | | flags = _O_RDONLY; |
539 | | fd = _wopen(wpath, flags | _O_BINARY, 0777); |
540 | | xmlFree(wpath); |
541 | | } |
542 | | #else |
543 | 0 | if (write) |
544 | 0 | flags = O_WRONLY | O_CREAT | O_TRUNC; |
545 | 0 | else |
546 | 0 | flags = O_RDONLY; |
547 | 0 | fd = open(filename, flags, 0777); |
548 | 0 | #endif /* WIN32 */ |
549 | |
|
550 | 0 | if (fd < 0) { |
551 | | /* |
552 | | * Windows and possibly other platforms return EINVAL |
553 | | * for invalid filenames. |
554 | | */ |
555 | 0 | if ((errno == ENOENT) || (errno == EINVAL)) { |
556 | 0 | ret = XML_IO_ENOENT; |
557 | 0 | } else { |
558 | | /* |
559 | | * This error won't be forwarded to the parser context |
560 | | * which will report it a second time. |
561 | | */ |
562 | 0 | ret = xmlIOErr(0, filename); |
563 | 0 | } |
564 | 0 | } else { |
565 | 0 | *out = fd; |
566 | 0 | ret = XML_ERR_OK; |
567 | 0 | } |
568 | |
|
569 | 0 | xmlFree(fromUri); |
570 | 0 | return(ret); |
571 | 0 | } |
572 | | |
573 | | /** |
574 | | * xmlFdRead: |
575 | | * @context: the I/O context |
576 | | * @buffer: where to drop data |
577 | | * @len: number of bytes to read |
578 | | * |
579 | | * Read @len bytes to @buffer from the I/O channel. |
580 | | * |
581 | | * Returns the number of bytes read |
582 | | */ |
583 | | static int |
584 | 0 | xmlFdRead(void *context, char *buffer, int len) { |
585 | 0 | int fd = (int) (ptrdiff_t) context; |
586 | 0 | int ret = 0; |
587 | 0 | int bytes; |
588 | |
|
589 | 0 | while (len > 0) { |
590 | 0 | bytes = read(fd, buffer, len); |
591 | 0 | if (bytes < 0) { |
592 | | /* |
593 | | * If we already got some bytes, return them without |
594 | | * raising an error. |
595 | | */ |
596 | 0 | if (ret > 0) |
597 | 0 | break; |
598 | 0 | return(-xmlIOErr(0, "read()")); |
599 | 0 | } |
600 | 0 | if (bytes == 0) |
601 | 0 | break; |
602 | 0 | ret += bytes; |
603 | 0 | buffer += bytes; |
604 | 0 | len -= bytes; |
605 | 0 | } |
606 | | |
607 | 0 | return(ret); |
608 | 0 | } |
609 | | |
610 | | #ifdef LIBXML_OUTPUT_ENABLED |
611 | | /** |
612 | | * xmlFdWrite: |
613 | | * @context: the I/O context |
614 | | * @buffer: where to get data |
615 | | * @len: number of bytes to write |
616 | | * |
617 | | * Write @len bytes from @buffer to the I/O channel. |
618 | | * |
619 | | * Returns the number of bytes written |
620 | | */ |
621 | | static int |
622 | 0 | xmlFdWrite(void *context, const char *buffer, int len) { |
623 | 0 | int fd = (int) (ptrdiff_t) context; |
624 | 0 | int ret = 0; |
625 | 0 | int bytes; |
626 | |
|
627 | 0 | while (len > 0) { |
628 | 0 | bytes = write(fd, buffer, len); |
629 | 0 | if (bytes < 0) |
630 | 0 | return(-xmlIOErr(0, "write()")); |
631 | 0 | ret += bytes; |
632 | 0 | buffer += bytes; |
633 | 0 | len -= bytes; |
634 | 0 | } |
635 | | |
636 | 0 | return(ret); |
637 | 0 | } |
638 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
639 | | |
640 | | /** |
641 | | * xmlFdClose: |
642 | | * @context: the I/O context |
643 | | * |
644 | | * Close an I/O channel |
645 | | * |
646 | | * Returns 0 in case of success and error code otherwise |
647 | | */ |
648 | | static int |
649 | 0 | xmlFdClose (void * context) { |
650 | 0 | int ret; |
651 | |
|
652 | 0 | ret = close((int) (ptrdiff_t) context); |
653 | 0 | if (ret < 0) |
654 | 0 | return(xmlIOErr(0, "close()")); |
655 | | |
656 | 0 | return(XML_ERR_OK); |
657 | 0 | } |
658 | | |
659 | | /** |
660 | | * xmlFileMatch: |
661 | | * @filename: the URI for matching |
662 | | * |
663 | | * DEPRECATED: Internal function, don't use. |
664 | | * |
665 | | * Returns 1 if matches, 0 otherwise |
666 | | */ |
667 | | int |
668 | 0 | xmlFileMatch (const char *filename ATTRIBUTE_UNUSED) { |
669 | 0 | return(1); |
670 | 0 | } |
671 | | |
672 | | /** |
673 | | * xmlFileOpenSafe: |
674 | | * @filename: the URI for matching |
675 | | * @out: pointer to resulting context |
676 | | * |
677 | | * input from FILE * |
678 | | * |
679 | | * Returns an I/O context or NULL in case of error |
680 | | */ |
681 | | static int |
682 | 0 | xmlFileOpenSafe(const char *filename, int write, void **out) { |
683 | 0 | char *fromUri = NULL; |
684 | 0 | FILE *fd; |
685 | 0 | int ret = XML_ERR_OK; |
686 | |
|
687 | 0 | *out = NULL; |
688 | 0 | if (filename == NULL) |
689 | 0 | return(XML_ERR_ARGUMENT); |
690 | | |
691 | 0 | if (xmlConvertUriToPath(filename, &fromUri) < 0) |
692 | 0 | return(XML_ERR_NO_MEMORY); |
693 | | |
694 | 0 | if (fromUri != NULL) |
695 | 0 | filename = fromUri; |
696 | |
|
697 | | #if defined(_WIN32) |
698 | | { |
699 | | wchar_t *wpath; |
700 | | |
701 | | wpath = __xmlIOWin32UTF8ToWChar(filename); |
702 | | if (wpath == NULL) { |
703 | | xmlFree(fromUri); |
704 | | return(XML_ERR_NO_MEMORY); |
705 | | } |
706 | | fd = _wfopen(wpath, write ? L"wb" : L"rb"); |
707 | | xmlFree(wpath); |
708 | | } |
709 | | #else |
710 | 0 | fd = fopen(filename, write ? "wb" : "rb"); |
711 | 0 | #endif /* WIN32 */ |
712 | |
|
713 | 0 | if (fd == NULL) { |
714 | | /* |
715 | | * Windows and possibly other platforms return EINVAL |
716 | | * for invalid filenames. |
717 | | */ |
718 | 0 | if ((errno == ENOENT) || (errno == EINVAL)) { |
719 | 0 | ret = XML_IO_ENOENT; |
720 | 0 | } else { |
721 | | /* |
722 | | * This error won't be forwarded to the parser context |
723 | | * which will report it a second time. |
724 | | */ |
725 | 0 | ret = xmlIOErr(0, filename); |
726 | 0 | } |
727 | 0 | } |
728 | |
|
729 | 0 | *out = fd; |
730 | 0 | xmlFree(fromUri); |
731 | 0 | return(ret); |
732 | 0 | } |
733 | | |
734 | | /** |
735 | | * xmlFileOpen: |
736 | | * @filename: the URI for matching |
737 | | * |
738 | | * DEPRECATED: Internal function, don't use. |
739 | | * |
740 | | * Returns an IO context or NULL in case or failure |
741 | | */ |
742 | | void * |
743 | 0 | xmlFileOpen(const char *filename) { |
744 | 0 | void *context; |
745 | |
|
746 | 0 | xmlFileOpenSafe(filename, 0, &context); |
747 | 0 | return(context); |
748 | 0 | } |
749 | | |
750 | | /** |
751 | | * xmlFileRead: |
752 | | * @context: the I/O context |
753 | | * @buffer: where to drop data |
754 | | * @len: number of bytes to write |
755 | | * |
756 | | * DEPRECATED: Internal function, don't use. |
757 | | * |
758 | | * Returns the number of bytes read or < 0 in case of failure |
759 | | */ |
760 | | int |
761 | 0 | xmlFileRead(void * context, char * buffer, int len) { |
762 | 0 | FILE *file = context; |
763 | 0 | size_t bytes; |
764 | |
|
765 | 0 | if ((context == NULL) || (buffer == NULL)) |
766 | 0 | return(-1); |
767 | | |
768 | | /* |
769 | | * The C standard doesn't mandate that fread sets errno, only |
770 | | * POSIX does. The Windows documentation isn't really clear. |
771 | | * Set errno to zero which will be reported as unknown error |
772 | | * if fread fails without setting errno. |
773 | | */ |
774 | 0 | errno = 0; |
775 | 0 | bytes = fread(buffer, 1, len, file); |
776 | 0 | if ((bytes < (size_t) len) && (ferror(file))) |
777 | 0 | return(-xmlIOErr(0, "fread()")); |
778 | | |
779 | 0 | return(len); |
780 | 0 | } |
781 | | |
782 | | #ifdef LIBXML_OUTPUT_ENABLED |
783 | | /** |
784 | | * xmlFileWrite: |
785 | | * @context: the I/O context |
786 | | * @buffer: where to drop data |
787 | | * @len: number of bytes to write |
788 | | * |
789 | | * Write @len bytes from @buffer to the I/O channel. |
790 | | * |
791 | | * Returns the number of bytes written |
792 | | */ |
793 | | static int |
794 | 0 | xmlFileWrite(void *context, const char *buffer, int len) { |
795 | 0 | FILE *file = context; |
796 | 0 | size_t bytes; |
797 | |
|
798 | 0 | if ((context == NULL) || (buffer == NULL)) |
799 | 0 | return(-1); |
800 | | |
801 | 0 | errno = 0; |
802 | 0 | bytes = fwrite(buffer, 1, len, file); |
803 | 0 | if (bytes < (size_t) len) |
804 | 0 | return(-xmlIOErr(0, "fwrite()")); |
805 | | |
806 | 0 | return(len); |
807 | 0 | } |
808 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
809 | | |
810 | | /** |
811 | | * xmlFileFlush: |
812 | | * @context: the I/O context |
813 | | * |
814 | | * Flush an I/O channel |
815 | | */ |
816 | | static int |
817 | 0 | xmlFileFlush (void * context) { |
818 | 0 | FILE *file = context; |
819 | |
|
820 | 0 | if (file == NULL) |
821 | 0 | return(-1); |
822 | | |
823 | 0 | if (fflush(file) != 0) |
824 | 0 | return(xmlIOErr(0, "fflush()")); |
825 | | |
826 | 0 | return(XML_ERR_OK); |
827 | 0 | } |
828 | | |
829 | | /** |
830 | | * xmlFileClose: |
831 | | * @context: the I/O context |
832 | | * |
833 | | * DEPRECATED: Internal function, don't use. |
834 | | * |
835 | | * Returns 0 or -1 an error code case of error |
836 | | */ |
837 | | int |
838 | 0 | xmlFileClose (void * context) { |
839 | 0 | FILE *file = context; |
840 | |
|
841 | 0 | if (context == NULL) |
842 | 0 | return(-1); |
843 | | |
844 | 0 | if (file == stdin) |
845 | 0 | return(0); |
846 | 0 | if ((file == stdout) || (file == stderr)) |
847 | 0 | return(xmlFileFlush(file)); |
848 | | |
849 | 0 | if (fclose(file) != 0) |
850 | 0 | return(xmlIOErr(0, "fclose()")); |
851 | | |
852 | 0 | return(0); |
853 | 0 | } |
854 | | |
855 | | #ifdef LIBXML_OUTPUT_ENABLED |
856 | | /** |
857 | | * xmlBufferWrite: |
858 | | * @context: the xmlBuffer |
859 | | * @buffer: the data to write |
860 | | * @len: number of bytes to write |
861 | | * |
862 | | * Write @len bytes from @buffer to the xml buffer |
863 | | * |
864 | | * Returns the number of bytes written or a negative xmlParserErrors |
865 | | * value. |
866 | | */ |
867 | | static int |
868 | 132k | xmlBufferWrite (void * context, const char * buffer, int len) { |
869 | 132k | int ret; |
870 | | |
871 | 132k | ret = xmlBufferAdd((xmlBufferPtr) context, (const xmlChar *) buffer, len); |
872 | 132k | if (ret != 0) |
873 | 51 | return(-XML_ERR_NO_MEMORY); |
874 | 132k | return(len); |
875 | 132k | } |
876 | | #endif |
877 | | |
878 | | #ifdef LIBXML_ZLIB_ENABLED |
879 | | /************************************************************************ |
880 | | * * |
881 | | * I/O for compressed file accesses * |
882 | | * * |
883 | | ************************************************************************/ |
884 | | |
885 | | /** |
886 | | * xmlGzfileRead: |
887 | | * @context: the I/O context |
888 | | * @buffer: where to drop data |
889 | | * @len: number of bytes to write |
890 | | * |
891 | | * Read @len bytes to @buffer from the compressed I/O channel. |
892 | | * |
893 | | * Returns the number of bytes read. |
894 | | */ |
895 | | static int |
896 | 0 | xmlGzfileRead (void * context, char * buffer, int len) { |
897 | 0 | int ret; |
898 | |
|
899 | 0 | ret = gzread((gzFile) context, &buffer[0], len); |
900 | 0 | if (ret < 0) xmlIOErr(0, "gzread()"); |
901 | 0 | return(ret); |
902 | 0 | } |
903 | | |
904 | | #ifdef LIBXML_OUTPUT_ENABLED |
905 | | /** |
906 | | * xmlGzfileWrite: |
907 | | * @context: the I/O context |
908 | | * @buffer: where to drop data |
909 | | * @len: number of bytes to write |
910 | | * |
911 | | * Write @len bytes from @buffer to the compressed I/O channel. |
912 | | * |
913 | | * Returns the number of bytes written |
914 | | */ |
915 | | static int |
916 | 0 | xmlGzfileWrite (void * context, const char * buffer, int len) { |
917 | 0 | int ret; |
918 | |
|
919 | 0 | ret = gzwrite((gzFile) context, (char *) &buffer[0], len); |
920 | 0 | if (ret < 0) xmlIOErr(0, "gzwrite()"); |
921 | 0 | return(ret); |
922 | 0 | } |
923 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
924 | | |
925 | | /** |
926 | | * xmlGzfileClose: |
927 | | * @context: the I/O context |
928 | | * |
929 | | * Close a compressed I/O channel |
930 | | */ |
931 | | static int |
932 | 0 | xmlGzfileClose (void * context) { |
933 | 0 | int ret; |
934 | |
|
935 | 0 | ret = (gzclose((gzFile) context) == Z_OK ) ? 0 : -1; |
936 | 0 | if (ret < 0) xmlIOErr(0, "gzclose()"); |
937 | 0 | return(ret); |
938 | 0 | } |
939 | | #endif /* LIBXML_ZLIB_ENABLED */ |
940 | | |
941 | | /************************************************************************ |
942 | | * * |
943 | | * I/O for compressed file accesses * |
944 | | * * |
945 | | ************************************************************************/ |
946 | | |
947 | | #ifdef LIBXML_LZMA_ENABLED |
948 | | |
949 | | #include "private/xzlib.h" |
950 | | |
951 | | /** |
952 | | * xmlXzfileRead: |
953 | | * @context: the I/O context |
954 | | * @buffer: where to drop data |
955 | | * @len: number of bytes to write |
956 | | * |
957 | | * Read @len bytes to @buffer from the compressed I/O channel. |
958 | | * |
959 | | * Returns the number of bytes written |
960 | | */ |
961 | | static int |
962 | 0 | xmlXzfileRead (void * context, char * buffer, int len) { |
963 | 0 | int ret; |
964 | |
|
965 | 0 | ret = __libxml2_xzread((xzFile) context, &buffer[0], len); |
966 | 0 | if (ret < 0) xmlIOErr(0, "xzread()"); |
967 | 0 | return(ret); |
968 | 0 | } |
969 | | |
970 | | /** |
971 | | * xmlXzfileClose: |
972 | | * @context: the I/O context |
973 | | * |
974 | | * Close a compressed I/O channel |
975 | | */ |
976 | | static int |
977 | 0 | xmlXzfileClose (void * context) { |
978 | 0 | int ret; |
979 | |
|
980 | 0 | ret = (__libxml2_xzclose((xzFile) context) == LZMA_OK ) ? 0 : -1; |
981 | 0 | if (ret < 0) xmlIOErr(0, "xzclose()"); |
982 | 0 | return(ret); |
983 | 0 | } |
984 | | #endif /* LIBXML_LZMA_ENABLED */ |
985 | | |
986 | | /************************************************************************ |
987 | | * * |
988 | | * I/O for HTTP file accesses * |
989 | | * * |
990 | | ************************************************************************/ |
991 | | |
992 | | #ifdef LIBXML_HTTP_ENABLED |
993 | | /** |
994 | | * xmlIOHTTPMatch: |
995 | | * @filename: the URI for matching |
996 | | * |
997 | | * DEPRECATED: Internal function, don't use. |
998 | | * |
999 | | * check if the URI matches an HTTP one |
1000 | | * |
1001 | | * Returns 1 if matches, 0 otherwise |
1002 | | */ |
1003 | | int |
1004 | | xmlIOHTTPMatch (const char *filename) { |
1005 | | if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "http://", 7)) |
1006 | | return(1); |
1007 | | return(0); |
1008 | | } |
1009 | | |
1010 | | /** |
1011 | | * xmlIOHTTPOpen: |
1012 | | * @filename: the URI for matching |
1013 | | * |
1014 | | * DEPRECATED: Internal function, don't use. |
1015 | | * |
1016 | | * open an HTTP I/O channel |
1017 | | * |
1018 | | * Returns an I/O context or NULL in case of error |
1019 | | */ |
1020 | | void * |
1021 | | xmlIOHTTPOpen (const char *filename) { |
1022 | | return(xmlNanoHTTPOpen(filename, NULL)); |
1023 | | } |
1024 | | |
1025 | | #ifdef LIBXML_OUTPUT_ENABLED |
1026 | | /** |
1027 | | * xmlIOHTTPOpenW: |
1028 | | * @post_uri: The destination URI for the document |
1029 | | * @compression: The compression desired for the document. |
1030 | | * |
1031 | | * DEPRECATED: Support for HTTP POST has been removed. |
1032 | | * |
1033 | | * Returns NULL. |
1034 | | */ |
1035 | | void * |
1036 | | xmlIOHTTPOpenW(const char *post_uri ATTRIBUTE_UNUSED, |
1037 | | int compression ATTRIBUTE_UNUSED) |
1038 | | { |
1039 | | return(NULL); |
1040 | | } |
1041 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
1042 | | |
1043 | | /** |
1044 | | * xmlIOHTTPRead: |
1045 | | * @context: the I/O context |
1046 | | * @buffer: where to drop data |
1047 | | * @len: number of bytes to write |
1048 | | * |
1049 | | * DEPRECATED: Internal function, don't use. |
1050 | | * |
1051 | | * Read @len bytes to @buffer from the I/O channel. |
1052 | | * |
1053 | | * Returns the number of bytes written |
1054 | | */ |
1055 | | int |
1056 | | xmlIOHTTPRead(void * context, char * buffer, int len) { |
1057 | | if ((buffer == NULL) || (len < 0)) return(-1); |
1058 | | return(xmlNanoHTTPRead(context, &buffer[0], len)); |
1059 | | } |
1060 | | |
1061 | | /** |
1062 | | * xmlIOHTTPClose: |
1063 | | * @context: the I/O context |
1064 | | * |
1065 | | * DEPRECATED: Internal function, don't use. |
1066 | | * |
1067 | | * Close an HTTP I/O channel |
1068 | | * |
1069 | | * Returns 0 |
1070 | | */ |
1071 | | int |
1072 | | xmlIOHTTPClose (void * context) { |
1073 | | xmlNanoHTTPClose(context); |
1074 | | return 0; |
1075 | | } |
1076 | | #endif /* LIBXML_HTTP_ENABLED */ |
1077 | | |
1078 | | #ifdef LIBXML_FTP_ENABLED |
1079 | | /************************************************************************ |
1080 | | * * |
1081 | | * I/O for FTP file accesses * |
1082 | | * * |
1083 | | ************************************************************************/ |
1084 | | /** |
1085 | | * xmlIOFTPMatch: |
1086 | | * @filename: the URI for matching |
1087 | | * |
1088 | | * DEPRECATED: Internal function, don't use. |
1089 | | * |
1090 | | * check if the URI matches an FTP one |
1091 | | * |
1092 | | * Returns 1 if matches, 0 otherwise |
1093 | | */ |
1094 | | int |
1095 | | xmlIOFTPMatch (const char *filename) { |
1096 | | if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "ftp://", 6)) |
1097 | | return(1); |
1098 | | return(0); |
1099 | | } |
1100 | | |
1101 | | /** |
1102 | | * xmlIOFTPOpen: |
1103 | | * @filename: the URI for matching |
1104 | | * |
1105 | | * DEPRECATED: Internal function, don't use. |
1106 | | * |
1107 | | * open an FTP I/O channel |
1108 | | * |
1109 | | * Returns an I/O context or NULL in case of error |
1110 | | */ |
1111 | | void * |
1112 | | xmlIOFTPOpen (const char *filename) { |
1113 | | return(xmlNanoFTPOpen(filename)); |
1114 | | } |
1115 | | |
1116 | | /** |
1117 | | * xmlIOFTPRead: |
1118 | | * @context: the I/O context |
1119 | | * @buffer: where to drop data |
1120 | | * @len: number of bytes to write |
1121 | | * |
1122 | | * DEPRECATED: Internal function, don't use. |
1123 | | * |
1124 | | * Read @len bytes to @buffer from the I/O channel. |
1125 | | * |
1126 | | * Returns the number of bytes written |
1127 | | */ |
1128 | | int |
1129 | | xmlIOFTPRead(void * context, char * buffer, int len) { |
1130 | | if ((buffer == NULL) || (len < 0)) return(-1); |
1131 | | return(xmlNanoFTPRead(context, &buffer[0], len)); |
1132 | | } |
1133 | | |
1134 | | /** |
1135 | | * xmlIOFTPClose: |
1136 | | * @context: the I/O context |
1137 | | * |
1138 | | * DEPRECATED: Internal function, don't use. |
1139 | | * |
1140 | | * Close an FTP I/O channel |
1141 | | * |
1142 | | * Returns 0 |
1143 | | */ |
1144 | | int |
1145 | | xmlIOFTPClose (void * context) { |
1146 | | return ( xmlNanoFTPClose(context) ); |
1147 | | } |
1148 | | #endif /* LIBXML_FTP_ENABLED */ |
1149 | | |
1150 | | /************************************************************************ |
1151 | | * * |
1152 | | * Input/output buffers * |
1153 | | * * |
1154 | | ************************************************************************/ |
1155 | | |
1156 | | static int |
1157 | 0 | xmlIODefaultMatch(const char *filename ATTRIBUTE_UNUSED) { |
1158 | 0 | return(1); |
1159 | 0 | } |
1160 | | |
1161 | | /** |
1162 | | * xmlInputDefaultOpen: |
1163 | | * @buf: input buffer to be filled |
1164 | | * @filename: filename or URI |
1165 | | * |
1166 | | * Returns an xmlParserErrors code. |
1167 | | */ |
1168 | | static int |
1169 | 0 | xmlInputDefaultOpen(xmlParserInputBufferPtr buf, const char *filename) { |
1170 | 0 | int ret; |
1171 | 0 | int fd; |
1172 | |
|
1173 | | #ifdef LIBXML_FTP_ENABLED |
1174 | | if (xmlIOFTPMatch(filename)) { |
1175 | | buf->context = xmlIOFTPOpen(filename); |
1176 | | |
1177 | | if (buf->context != NULL) { |
1178 | | buf->readcallback = xmlIOFTPRead; |
1179 | | buf->closecallback = xmlIOFTPClose; |
1180 | | return(XML_ERR_OK); |
1181 | | } |
1182 | | } |
1183 | | #endif /* LIBXML_FTP_ENABLED */ |
1184 | |
|
1185 | | #ifdef LIBXML_HTTP_ENABLED |
1186 | | if (xmlIOHTTPMatch(filename)) { |
1187 | | buf->context = xmlIOHTTPOpen(filename); |
1188 | | |
1189 | | if (buf->context != NULL) { |
1190 | | buf->readcallback = xmlIOHTTPRead; |
1191 | | buf->closecallback = xmlIOHTTPClose; |
1192 | | return(XML_ERR_OK); |
1193 | | } |
1194 | | } |
1195 | | #endif /* LIBXML_HTTP_ENABLED */ |
1196 | |
|
1197 | 0 | if (!xmlFileMatch(filename)) |
1198 | 0 | return(XML_IO_ENOENT); |
1199 | | |
1200 | 0 | #ifdef LIBXML_LZMA_ENABLED |
1201 | 0 | { |
1202 | 0 | xzFile xzStream; |
1203 | |
|
1204 | 0 | ret = xmlFdOpen(filename, 0, &fd); |
1205 | 0 | if (ret != XML_ERR_OK) |
1206 | 0 | return(ret); |
1207 | | |
1208 | 0 | xzStream = __libxml2_xzdopen(filename, fd, "rb"); |
1209 | |
|
1210 | 0 | if (xzStream == NULL) { |
1211 | 0 | close(fd); |
1212 | 0 | } else { |
1213 | 0 | if (__libxml2_xzcompressed(xzStream) > 0) { |
1214 | 0 | buf->context = xzStream; |
1215 | 0 | buf->readcallback = xmlXzfileRead; |
1216 | 0 | buf->closecallback = xmlXzfileClose; |
1217 | 0 | buf->compressed = 1; |
1218 | |
|
1219 | 0 | return(XML_ERR_OK); |
1220 | 0 | } |
1221 | | |
1222 | 0 | xmlXzfileClose(xzStream); |
1223 | 0 | } |
1224 | 0 | } |
1225 | 0 | #endif /* LIBXML_LZMA_ENABLED */ |
1226 | | |
1227 | 0 | #ifdef LIBXML_ZLIB_ENABLED |
1228 | 0 | { |
1229 | 0 | gzFile gzStream; |
1230 | |
|
1231 | 0 | ret = xmlFdOpen(filename, 0, &fd); |
1232 | 0 | if (ret != XML_ERR_OK) |
1233 | 0 | return(ret); |
1234 | | |
1235 | 0 | gzStream = gzdopen(fd, "rb"); |
1236 | |
|
1237 | 0 | if (gzStream == NULL) { |
1238 | 0 | close(fd); |
1239 | 0 | } else { |
1240 | 0 | char buff4[4]; |
1241 | |
|
1242 | 0 | if ((gzread(gzStream, buff4, 4) > 0) && |
1243 | 0 | (gzdirect(gzStream) == 0)) { |
1244 | 0 | gzrewind(gzStream); |
1245 | |
|
1246 | 0 | buf->context = gzStream; |
1247 | 0 | buf->readcallback = xmlGzfileRead; |
1248 | 0 | buf->closecallback = xmlGzfileClose; |
1249 | 0 | buf->compressed = 1; |
1250 | |
|
1251 | 0 | return(XML_ERR_OK); |
1252 | 0 | } |
1253 | | |
1254 | 0 | xmlGzfileClose(gzStream); |
1255 | 0 | } |
1256 | 0 | } |
1257 | 0 | #endif /* LIBXML_ZLIB_ENABLED */ |
1258 | | |
1259 | 0 | ret = xmlFdOpen(filename, 0, &fd); |
1260 | 0 | if (ret != XML_ERR_OK) |
1261 | 0 | return(ret); |
1262 | | |
1263 | 0 | buf->context = (void *) (ptrdiff_t) fd; |
1264 | 0 | buf->readcallback = xmlFdRead; |
1265 | 0 | buf->closecallback = xmlFdClose; |
1266 | 0 | return(XML_ERR_OK); |
1267 | 0 | } |
1268 | | |
1269 | | #ifdef LIBXML_OUTPUT_ENABLED |
1270 | | /** |
1271 | | * xmlOutputDefaultOpen: |
1272 | | * @buf: input buffer to be filled |
1273 | | * @filename: filename or URI |
1274 | | * @compression: compression level or 0 |
1275 | | * @is_file_uri: whether filename is a file URI |
1276 | | * |
1277 | | * Returns an xmlParserErrors code. |
1278 | | */ |
1279 | | static int |
1280 | | xmlOutputDefaultOpen(xmlOutputBufferPtr buf, const char *filename, |
1281 | 0 | int compression) { |
1282 | 0 | int fd; |
1283 | |
|
1284 | 0 | (void) compression; |
1285 | |
|
1286 | 0 | if (!strcmp(filename, "-")) { |
1287 | 0 | fd = dup(STDOUT_FILENO); |
1288 | |
|
1289 | 0 | if (fd < 0) |
1290 | 0 | return(xmlIOErr(0, "dup()")); |
1291 | 0 | } else { |
1292 | 0 | int ret; |
1293 | |
|
1294 | 0 | ret = xmlFdOpen(filename, /* write */ 1, &fd); |
1295 | 0 | if (ret != XML_ERR_OK) |
1296 | 0 | return(ret); |
1297 | 0 | } |
1298 | | |
1299 | 0 | #ifdef LIBXML_ZLIB_ENABLED |
1300 | 0 | if ((compression > 0) && (compression <= 9)) { |
1301 | 0 | gzFile gzStream; |
1302 | 0 | char mode[15]; |
1303 | |
|
1304 | 0 | snprintf(mode, sizeof(mode), "wb%d", compression); |
1305 | 0 | gzStream = gzdopen(fd, mode); |
1306 | |
|
1307 | 0 | if (gzStream == NULL) { |
1308 | 0 | close(fd); |
1309 | 0 | return(xmlIOErr(XML_IO_UNKNOWN, "gzdopen()")); |
1310 | 0 | } |
1311 | | |
1312 | 0 | buf->context = gzStream; |
1313 | 0 | buf->writecallback = xmlGzfileWrite; |
1314 | 0 | buf->closecallback = xmlGzfileClose; |
1315 | |
|
1316 | 0 | return(XML_ERR_OK); |
1317 | 0 | } |
1318 | 0 | #endif /* LIBXML_ZLIB_ENABLED */ |
1319 | | |
1320 | 0 | buf->context = (void *) (ptrdiff_t) fd; |
1321 | 0 | buf->writecallback = xmlFdWrite; |
1322 | 0 | buf->closecallback = xmlFdClose; |
1323 | 0 | return(XML_ERR_OK); |
1324 | 0 | } |
1325 | | #endif |
1326 | | |
1327 | | /** |
1328 | | * xmlAllocParserInputBuffer: |
1329 | | * @enc: the charset encoding if known (deprecated) |
1330 | | * |
1331 | | * Create a buffered parser input for progressive parsing. |
1332 | | * |
1333 | | * The encoding argument is deprecated and should be set to |
1334 | | * XML_CHAR_ENCODING_NONE. The encoding can be changed with |
1335 | | * xmlSwitchEncoding or xmlSwitchEncodingName later on. |
1336 | | * |
1337 | | * Returns the new parser input or NULL |
1338 | | */ |
1339 | | xmlParserInputBufferPtr |
1340 | 944k | xmlAllocParserInputBuffer(xmlCharEncoding enc) { |
1341 | 944k | xmlParserInputBufferPtr ret; |
1342 | | |
1343 | 944k | ret = (xmlParserInputBufferPtr) xmlMalloc(sizeof(xmlParserInputBuffer)); |
1344 | 944k | if (ret == NULL) { |
1345 | 228 | return(NULL); |
1346 | 228 | } |
1347 | 944k | memset(ret, 0, sizeof(xmlParserInputBuffer)); |
1348 | 944k | ret->buffer = xmlBufCreateSize(2 * xmlDefaultBufferSize); |
1349 | 944k | if (ret->buffer == NULL) { |
1350 | 504 | xmlFree(ret); |
1351 | 504 | return(NULL); |
1352 | 504 | } |
1353 | 944k | xmlBufSetAllocationScheme(ret->buffer, XML_BUFFER_ALLOC_DOUBLEIT); |
1354 | 944k | if (enc != XML_CHAR_ENCODING_NONE) { |
1355 | 0 | if (xmlLookupCharEncodingHandler(enc, &ret->encoder) != 0) { |
1356 | | /* We can't handle errors properly here. */ |
1357 | 0 | xmlFreeParserInputBuffer(ret); |
1358 | 0 | return(NULL); |
1359 | 0 | } |
1360 | 0 | } |
1361 | 944k | if (ret->encoder != NULL) |
1362 | 0 | ret->raw = xmlBufCreateSize(2 * xmlDefaultBufferSize); |
1363 | 944k | else |
1364 | 944k | ret->raw = NULL; |
1365 | 944k | ret->readcallback = NULL; |
1366 | 944k | ret->closecallback = NULL; |
1367 | 944k | ret->context = NULL; |
1368 | 944k | ret->compressed = -1; |
1369 | 944k | ret->rawconsumed = 0; |
1370 | | |
1371 | 944k | return(ret); |
1372 | 944k | } |
1373 | | |
1374 | | #ifdef LIBXML_OUTPUT_ENABLED |
1375 | | /** |
1376 | | * xmlAllocOutputBuffer: |
1377 | | * @encoder: the encoding converter or NULL |
1378 | | * |
1379 | | * Create a buffered parser output |
1380 | | * |
1381 | | * Returns the new parser output or NULL |
1382 | | */ |
1383 | | xmlOutputBufferPtr |
1384 | 10.1k | xmlAllocOutputBuffer(xmlCharEncodingHandlerPtr encoder) { |
1385 | 10.1k | xmlOutputBufferPtr ret; |
1386 | | |
1387 | 10.1k | ret = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer)); |
1388 | 10.1k | if (ret == NULL) { |
1389 | 60 | return(NULL); |
1390 | 60 | } |
1391 | 10.0k | memset(ret, 0, sizeof(xmlOutputBuffer)); |
1392 | 10.0k | ret->buffer = xmlBufCreate(); |
1393 | 10.0k | if (ret->buffer == NULL) { |
1394 | 115 | xmlFree(ret); |
1395 | 115 | return(NULL); |
1396 | 115 | } |
1397 | 9.94k | xmlBufSetAllocationScheme(ret->buffer, XML_BUFFER_ALLOC_DOUBLEIT); |
1398 | | |
1399 | 9.94k | ret->encoder = encoder; |
1400 | 9.94k | if (encoder != NULL) { |
1401 | 0 | ret->conv = xmlBufCreateSize(4000); |
1402 | 0 | if (ret->conv == NULL) { |
1403 | 0 | xmlBufFree(ret->buffer); |
1404 | 0 | xmlFree(ret); |
1405 | 0 | return(NULL); |
1406 | 0 | } |
1407 | | |
1408 | | /* |
1409 | | * This call is designed to initiate the encoder state |
1410 | | */ |
1411 | 0 | xmlCharEncOutput(ret, 1); |
1412 | 0 | } else |
1413 | 9.94k | ret->conv = NULL; |
1414 | 9.94k | ret->writecallback = NULL; |
1415 | 9.94k | ret->closecallback = NULL; |
1416 | 9.94k | ret->context = NULL; |
1417 | 9.94k | ret->written = 0; |
1418 | | |
1419 | 9.94k | return(ret); |
1420 | 9.94k | } |
1421 | | |
1422 | | /** |
1423 | | * xmlAllocOutputBufferInternal: |
1424 | | * @encoder: the encoding converter or NULL |
1425 | | * |
1426 | | * Create a buffered parser output |
1427 | | * |
1428 | | * Returns the new parser output or NULL |
1429 | | */ |
1430 | | xmlOutputBufferPtr |
1431 | 13.9k | xmlAllocOutputBufferInternal(xmlCharEncodingHandlerPtr encoder) { |
1432 | 13.9k | xmlOutputBufferPtr ret; |
1433 | | |
1434 | 13.9k | ret = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer)); |
1435 | 13.9k | if (ret == NULL) { |
1436 | 61 | return(NULL); |
1437 | 61 | } |
1438 | 13.9k | memset(ret, 0, sizeof(xmlOutputBuffer)); |
1439 | 13.9k | ret->buffer = xmlBufCreate(); |
1440 | 13.9k | if (ret->buffer == NULL) { |
1441 | 112 | xmlFree(ret); |
1442 | 112 | return(NULL); |
1443 | 112 | } |
1444 | | |
1445 | | |
1446 | | /* |
1447 | | * For conversion buffers we use the special IO handling |
1448 | | */ |
1449 | 13.8k | xmlBufSetAllocationScheme(ret->buffer, XML_BUFFER_ALLOC_IO); |
1450 | | |
1451 | 13.8k | ret->encoder = encoder; |
1452 | 13.8k | if (encoder != NULL) { |
1453 | 0 | ret->conv = xmlBufCreateSize(4000); |
1454 | 0 | if (ret->conv == NULL) { |
1455 | 0 | xmlBufFree(ret->buffer); |
1456 | 0 | xmlFree(ret); |
1457 | 0 | return(NULL); |
1458 | 0 | } |
1459 | | |
1460 | | /* |
1461 | | * This call is designed to initiate the encoder state |
1462 | | */ |
1463 | 0 | xmlCharEncOutput(ret, 1); |
1464 | 0 | } else |
1465 | 13.8k | ret->conv = NULL; |
1466 | 13.8k | ret->writecallback = NULL; |
1467 | 13.8k | ret->closecallback = NULL; |
1468 | 13.8k | ret->context = NULL; |
1469 | 13.8k | ret->written = 0; |
1470 | | |
1471 | 13.8k | return(ret); |
1472 | 13.8k | } |
1473 | | |
1474 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
1475 | | |
1476 | | /** |
1477 | | * xmlFreeParserInputBuffer: |
1478 | | * @in: a buffered parser input |
1479 | | * |
1480 | | * Free up the memory used by a buffered parser input |
1481 | | */ |
1482 | | void |
1483 | 944k | xmlFreeParserInputBuffer(xmlParserInputBufferPtr in) { |
1484 | 944k | if (in == NULL) return; |
1485 | | |
1486 | 944k | if (in->raw) { |
1487 | 50.7k | xmlBufFree(in->raw); |
1488 | 50.7k | in->raw = NULL; |
1489 | 50.7k | } |
1490 | 944k | if (in->encoder != NULL) { |
1491 | 51.1k | xmlCharEncCloseFunc(in->encoder); |
1492 | 51.1k | } |
1493 | 944k | if (in->closecallback != NULL) { |
1494 | 838k | in->closecallback(in->context); |
1495 | 838k | } |
1496 | 944k | if (in->buffer != NULL) { |
1497 | 944k | xmlBufFree(in->buffer); |
1498 | 944k | in->buffer = NULL; |
1499 | 944k | } |
1500 | | |
1501 | 944k | xmlFree(in); |
1502 | 944k | } |
1503 | | |
1504 | | #ifdef LIBXML_OUTPUT_ENABLED |
1505 | | /** |
1506 | | * xmlOutputBufferClose: |
1507 | | * @out: a buffered output |
1508 | | * |
1509 | | * flushes and close the output I/O channel |
1510 | | * and free up all the associated resources |
1511 | | * |
1512 | | * Returns the number of byte written or a negative xmlParserErrors |
1513 | | * code in case of error. |
1514 | | */ |
1515 | | int |
1516 | | xmlOutputBufferClose(xmlOutputBufferPtr out) |
1517 | 23.9k | { |
1518 | 23.9k | int ret; |
1519 | | |
1520 | 23.9k | if (out == NULL) |
1521 | 175 | return (-1); |
1522 | | |
1523 | 23.7k | if (out->writecallback != NULL) |
1524 | 13.8k | xmlOutputBufferFlush(out); |
1525 | | |
1526 | 23.7k | if (out->closecallback != NULL) { |
1527 | 0 | int code = out->closecallback(out->context); |
1528 | |
|
1529 | 0 | if ((code != XML_ERR_OK) && (out->error == XML_ERR_OK)) { |
1530 | 0 | if (code < 0) |
1531 | 0 | out->error = XML_IO_UNKNOWN; |
1532 | 0 | else |
1533 | 0 | out->error = code; |
1534 | 0 | } |
1535 | 0 | } |
1536 | | |
1537 | 23.7k | if (out->error != XML_ERR_OK) |
1538 | 516 | ret = -out->error; |
1539 | 23.2k | else |
1540 | 23.2k | ret = out->written; |
1541 | | |
1542 | 23.7k | if (out->conv) { |
1543 | 0 | xmlBufFree(out->conv); |
1544 | 0 | out->conv = NULL; |
1545 | 0 | } |
1546 | 23.7k | if (out->encoder != NULL) { |
1547 | 0 | xmlCharEncCloseFunc(out->encoder); |
1548 | 0 | } |
1549 | 23.7k | if (out->buffer != NULL) { |
1550 | 23.7k | xmlBufFree(out->buffer); |
1551 | 23.7k | out->buffer = NULL; |
1552 | 23.7k | } |
1553 | | |
1554 | 23.7k | xmlFree(out); |
1555 | | |
1556 | 23.7k | return(ret); |
1557 | 23.9k | } |
1558 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
1559 | | |
1560 | | /** |
1561 | | * xmlParserInputBufferCreateFilenameInt: |
1562 | | * @URI: the filename or URI |
1563 | | * @enc: encoding enum (deprecated) |
1564 | | * @out: pointer to resulting input buffer |
1565 | | * |
1566 | | * Returns an xmlParserErrors code. |
1567 | | */ |
1568 | | static int |
1569 | | xmlParserInputBufferCreateFilenameInt(const char *URI, xmlCharEncoding enc, |
1570 | 0 | xmlParserInputBufferPtr *out) { |
1571 | 0 | xmlParserInputBufferPtr buf; |
1572 | 0 | int ret; |
1573 | 0 | int i; |
1574 | |
|
1575 | 0 | *out = NULL; |
1576 | 0 | if (URI == NULL) |
1577 | 0 | return(XML_ERR_ARGUMENT); |
1578 | | |
1579 | | /* |
1580 | | * Allocate the Input buffer front-end. |
1581 | | */ |
1582 | 0 | buf = xmlAllocParserInputBuffer(enc); |
1583 | 0 | if (buf == NULL) |
1584 | 0 | return(XML_ERR_NO_MEMORY); |
1585 | | |
1586 | | /* |
1587 | | * Try to find one of the input accept method accepting that scheme |
1588 | | * Go in reverse to give precedence to user defined handlers. |
1589 | | */ |
1590 | 0 | ret = XML_IO_ENOENT; |
1591 | 0 | for (i = xmlInputCallbackNr - 1; i >= 0; i--) { |
1592 | 0 | xmlInputCallback *cb = &xmlInputCallbackTable[i]; |
1593 | |
|
1594 | 0 | if (cb->matchcallback == xmlIODefaultMatch) { |
1595 | 0 | ret = xmlInputDefaultOpen(buf, URI); |
1596 | |
|
1597 | 0 | if ((ret == XML_ERR_OK) || (ret != XML_IO_ENOENT)) |
1598 | 0 | break; |
1599 | 0 | } else if ((cb->matchcallback != NULL) && |
1600 | 0 | (cb->matchcallback(URI) != 0)) { |
1601 | 0 | buf->context = cb->opencallback(URI); |
1602 | 0 | if (buf->context != NULL) { |
1603 | 0 | buf->readcallback = cb->readcallback; |
1604 | 0 | buf->closecallback = cb->closecallback; |
1605 | 0 | ret = XML_ERR_OK; |
1606 | 0 | break; |
1607 | 0 | } |
1608 | 0 | } |
1609 | 0 | } |
1610 | 0 | if (ret != XML_ERR_OK) { |
1611 | 0 | xmlFreeParserInputBuffer(buf); |
1612 | 0 | *out = NULL; |
1613 | 0 | return(ret); |
1614 | 0 | } |
1615 | | |
1616 | 0 | *out = buf; |
1617 | 0 | return(ret); |
1618 | 0 | } |
1619 | | |
1620 | | xmlParserInputBufferPtr |
1621 | 0 | __xmlParserInputBufferCreateFilename(const char *URI, xmlCharEncoding enc) { |
1622 | 0 | xmlParserInputBufferPtr ret; |
1623 | |
|
1624 | 0 | xmlParserInputBufferCreateFilenameInt(URI, enc, &ret); |
1625 | 0 | return(ret); |
1626 | 0 | } |
1627 | | |
1628 | | /** |
1629 | | * xmlParserInputBufferCreateFilename: |
1630 | | * @URI: a C string containing the URI or filename |
1631 | | * @enc: the charset encoding if known |
1632 | | * |
1633 | | * Create a buffered parser input for the progressive parsing of a file |
1634 | | * Automatic support for ZLIB/Compress compressed document is provided |
1635 | | * by default if found at compile-time. |
1636 | | * Do an encoding check if enc == XML_CHAR_ENCODING_NONE |
1637 | | * |
1638 | | * Returns the new parser input or NULL |
1639 | | */ |
1640 | | xmlParserInputBufferPtr |
1641 | 0 | xmlParserInputBufferCreateFilename(const char *URI, xmlCharEncoding enc) { |
1642 | 0 | if (xmlParserInputBufferCreateFilenameValue != NULL) |
1643 | 0 | return(xmlParserInputBufferCreateFilenameValue(URI, enc)); |
1644 | | |
1645 | 0 | return(__xmlParserInputBufferCreateFilename(URI, enc)); |
1646 | 0 | } |
1647 | | |
1648 | | /** |
1649 | | * xmlParserInputBufferCreateFilenameSafe: |
1650 | | * @URI: the filename or URI |
1651 | | * @enc: encoding enum (deprecated) |
1652 | | * @out: pointer to resulting input buffer |
1653 | | * |
1654 | | * Returns an xmlParserErrors code. |
1655 | | */ |
1656 | | int |
1657 | | xmlParserInputBufferCreateFilenameSafe(const char *URI, xmlCharEncoding enc, |
1658 | 0 | xmlParserInputBufferPtr *out) { |
1659 | 0 | if (xmlParserInputBufferCreateFilenameValue != NULL) { |
1660 | 0 | *out = xmlParserInputBufferCreateFilenameValue(URI, enc); |
1661 | |
|
1662 | 0 | if (*out == NULL) |
1663 | 0 | return(XML_IO_ENOENT); |
1664 | 0 | return(XML_ERR_OK); |
1665 | 0 | } |
1666 | | |
1667 | 0 | return(xmlParserInputBufferCreateFilenameInt(URI, enc, out)); |
1668 | 0 | } |
1669 | | |
1670 | | #ifdef LIBXML_OUTPUT_ENABLED |
1671 | | xmlOutputBufferPtr |
1672 | | __xmlOutputBufferCreateFilename(const char *URI, |
1673 | | xmlCharEncodingHandlerPtr encoder, |
1674 | 0 | int compression) { |
1675 | 0 | xmlOutputBufferPtr ret; |
1676 | 0 | xmlURIPtr puri; |
1677 | 0 | int i = 0; |
1678 | 0 | char *unescaped = NULL; |
1679 | |
|
1680 | 0 | if (URI == NULL) |
1681 | 0 | return(NULL); |
1682 | | |
1683 | 0 | puri = xmlParseURI(URI); |
1684 | 0 | if (puri != NULL) { |
1685 | | /* |
1686 | | * try to limit the damages of the URI unescaping code. |
1687 | | */ |
1688 | 0 | if (puri->scheme == NULL) { |
1689 | 0 | unescaped = xmlURIUnescapeString(URI, 0, NULL); |
1690 | 0 | if (unescaped == NULL) { |
1691 | 0 | xmlFreeURI(puri); |
1692 | 0 | return(NULL); |
1693 | 0 | } |
1694 | 0 | URI = unescaped; |
1695 | 0 | } |
1696 | 0 | xmlFreeURI(puri); |
1697 | 0 | } |
1698 | | |
1699 | | /* |
1700 | | * Allocate the Output buffer front-end. |
1701 | | */ |
1702 | 0 | ret = xmlAllocOutputBufferInternal(encoder); |
1703 | 0 | if (ret == NULL) { |
1704 | 0 | xmlFree(unescaped); |
1705 | 0 | return(NULL); |
1706 | 0 | } |
1707 | | |
1708 | | /* |
1709 | | * Try to find one of the output accept method accepting that scheme |
1710 | | * Go in reverse to give precedence to user defined handlers. |
1711 | | */ |
1712 | 0 | for (i = xmlOutputCallbackNr - 1; i >= 0; i--) { |
1713 | 0 | xmlOutputCallback *cb = &xmlOutputCallbackTable[i]; |
1714 | 0 | int code; |
1715 | |
|
1716 | 0 | if (cb->matchcallback == xmlIODefaultMatch) { |
1717 | 0 | code = xmlOutputDefaultOpen(ret, URI, compression); |
1718 | | /* TODO: Handle other errors */ |
1719 | 0 | if (code == XML_ERR_OK) |
1720 | 0 | break; |
1721 | 0 | } else if ((cb->matchcallback != NULL) && |
1722 | 0 | (cb->matchcallback(URI) != 0)) { |
1723 | 0 | ret->context = cb->opencallback(URI); |
1724 | 0 | if (ret->context != NULL) { |
1725 | 0 | ret->writecallback = cb->writecallback; |
1726 | 0 | ret->closecallback = cb->closecallback; |
1727 | 0 | break; |
1728 | 0 | } |
1729 | 0 | } |
1730 | 0 | } |
1731 | |
|
1732 | 0 | if (ret->context == NULL) { |
1733 | 0 | xmlOutputBufferClose(ret); |
1734 | 0 | ret = NULL; |
1735 | 0 | } |
1736 | |
|
1737 | 0 | xmlFree(unescaped); |
1738 | 0 | return(ret); |
1739 | 0 | } |
1740 | | |
1741 | | /** |
1742 | | * xmlOutputBufferCreateFilename: |
1743 | | * @URI: a C string containing the URI or filename |
1744 | | * @encoder: the encoding converter or NULL |
1745 | | * @compression: the compression ration (0 none, 9 max). |
1746 | | * |
1747 | | * Create a buffered output for the progressive saving of a file |
1748 | | * If filename is "-' then we use stdout as the output. |
1749 | | * Automatic support for ZLIB/Compress compressed document is provided |
1750 | | * by default if found at compile-time. |
1751 | | * TODO: currently if compression is set, the library only support |
1752 | | * writing to a local file. |
1753 | | * |
1754 | | * Returns the new output or NULL |
1755 | | */ |
1756 | | xmlOutputBufferPtr |
1757 | | xmlOutputBufferCreateFilename(const char *URI, |
1758 | | xmlCharEncodingHandlerPtr encoder, |
1759 | 0 | int compression ATTRIBUTE_UNUSED) { |
1760 | 0 | if ((xmlOutputBufferCreateFilenameValue)) { |
1761 | 0 | return xmlOutputBufferCreateFilenameValue(URI, encoder, compression); |
1762 | 0 | } |
1763 | 0 | return __xmlOutputBufferCreateFilename(URI, encoder, compression); |
1764 | 0 | } |
1765 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
1766 | | |
1767 | | /** |
1768 | | * xmlParserInputBufferCreateFile: |
1769 | | * @file: a FILE* |
1770 | | * @enc: the charset encoding if known (deprecated) |
1771 | | * |
1772 | | * Create a buffered parser input for the progressive parsing of a FILE * |
1773 | | * buffered C I/O |
1774 | | * |
1775 | | * The encoding argument is deprecated and should be set to |
1776 | | * XML_CHAR_ENCODING_NONE. The encoding can be changed with |
1777 | | * xmlSwitchEncoding or xmlSwitchEncodingName later on. |
1778 | | * |
1779 | | * Returns the new parser input or NULL |
1780 | | */ |
1781 | | xmlParserInputBufferPtr |
1782 | 0 | xmlParserInputBufferCreateFile(FILE *file, xmlCharEncoding enc) { |
1783 | 0 | xmlParserInputBufferPtr ret; |
1784 | |
|
1785 | 0 | if (file == NULL) return(NULL); |
1786 | | |
1787 | 0 | ret = xmlAllocParserInputBuffer(enc); |
1788 | 0 | if (ret != NULL) { |
1789 | 0 | ret->context = file; |
1790 | 0 | ret->readcallback = xmlFileRead; |
1791 | 0 | ret->closecallback = NULL; |
1792 | 0 | } |
1793 | |
|
1794 | 0 | return(ret); |
1795 | 0 | } |
1796 | | |
1797 | | #ifdef LIBXML_OUTPUT_ENABLED |
1798 | | /** |
1799 | | * xmlOutputBufferCreateFile: |
1800 | | * @file: a FILE* |
1801 | | * @encoder: the encoding converter or NULL |
1802 | | * |
1803 | | * Create a buffered output for the progressive saving to a FILE * |
1804 | | * buffered C I/O |
1805 | | * |
1806 | | * Returns the new parser output or NULL |
1807 | | */ |
1808 | | xmlOutputBufferPtr |
1809 | 0 | xmlOutputBufferCreateFile(FILE *file, xmlCharEncodingHandlerPtr encoder) { |
1810 | 0 | xmlOutputBufferPtr ret; |
1811 | |
|
1812 | 0 | if (file == NULL) return(NULL); |
1813 | | |
1814 | 0 | ret = xmlAllocOutputBufferInternal(encoder); |
1815 | 0 | if (ret != NULL) { |
1816 | 0 | ret->context = file; |
1817 | 0 | ret->writecallback = xmlFileWrite; |
1818 | 0 | ret->closecallback = xmlFileFlush; |
1819 | 0 | } |
1820 | |
|
1821 | 0 | return(ret); |
1822 | 0 | } |
1823 | | |
1824 | | /** |
1825 | | * xmlOutputBufferCreateBuffer: |
1826 | | * @buffer: a xmlBufferPtr |
1827 | | * @encoder: the encoding converter or NULL |
1828 | | * |
1829 | | * Create a buffered output for the progressive saving to a xmlBuffer |
1830 | | * |
1831 | | * Returns the new parser output or NULL |
1832 | | */ |
1833 | | xmlOutputBufferPtr |
1834 | | xmlOutputBufferCreateBuffer(xmlBufferPtr buffer, |
1835 | 14.1k | xmlCharEncodingHandlerPtr encoder) { |
1836 | 14.1k | xmlOutputBufferPtr ret; |
1837 | | |
1838 | 14.1k | if (buffer == NULL) return(NULL); |
1839 | | |
1840 | 13.9k | ret = xmlOutputBufferCreateIO(xmlBufferWrite, NULL, (void *) buffer, |
1841 | 13.9k | encoder); |
1842 | | |
1843 | 13.9k | return(ret); |
1844 | 14.1k | } |
1845 | | |
1846 | | /** |
1847 | | * xmlOutputBufferGetContent: |
1848 | | * @out: an xmlOutputBufferPtr |
1849 | | * |
1850 | | * Gives a pointer to the data currently held in the output buffer |
1851 | | * |
1852 | | * Returns a pointer to the data or NULL in case of error |
1853 | | */ |
1854 | | const xmlChar * |
1855 | 10.1k | xmlOutputBufferGetContent(xmlOutputBufferPtr out) { |
1856 | 10.1k | if ((out == NULL) || (out->buffer == NULL) || (out->error != 0)) |
1857 | 311 | return(NULL); |
1858 | | |
1859 | 9.81k | return(xmlBufContent(out->buffer)); |
1860 | 10.1k | } |
1861 | | |
1862 | | /** |
1863 | | * xmlOutputBufferGetSize: |
1864 | | * @out: an xmlOutputBufferPtr |
1865 | | * |
1866 | | * Gives the length of the data currently held in the output buffer |
1867 | | * |
1868 | | * Returns 0 in case or error or no data is held, the size otherwise |
1869 | | */ |
1870 | | size_t |
1871 | 0 | xmlOutputBufferGetSize(xmlOutputBufferPtr out) { |
1872 | 0 | if ((out == NULL) || (out->buffer == NULL) || (out->error != 0)) |
1873 | 0 | return(0); |
1874 | | |
1875 | 0 | return(xmlBufUse(out->buffer)); |
1876 | 0 | } |
1877 | | |
1878 | | |
1879 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
1880 | | |
1881 | | /** |
1882 | | * xmlParserInputBufferCreateFd: |
1883 | | * @fd: a file descriptor number |
1884 | | * @enc: the charset encoding if known (deprecated) |
1885 | | * |
1886 | | * Create a buffered parser input for the progressive parsing for the input |
1887 | | * from a file descriptor |
1888 | | * |
1889 | | * The encoding argument is deprecated and should be set to |
1890 | | * XML_CHAR_ENCODING_NONE. The encoding can be changed with |
1891 | | * xmlSwitchEncoding or xmlSwitchEncodingName later on. |
1892 | | * |
1893 | | * Returns the new parser input or NULL |
1894 | | */ |
1895 | | xmlParserInputBufferPtr |
1896 | 0 | xmlParserInputBufferCreateFd(int fd, xmlCharEncoding enc) { |
1897 | 0 | xmlParserInputBufferPtr ret; |
1898 | |
|
1899 | 0 | if (fd < 0) return(NULL); |
1900 | | |
1901 | 0 | ret = xmlAllocParserInputBuffer(enc); |
1902 | 0 | if (ret != NULL) { |
1903 | 0 | ret->context = (void *) (ptrdiff_t) fd; |
1904 | 0 | ret->readcallback = xmlFdRead; |
1905 | 0 | ret->closecallback = xmlFdClose; |
1906 | 0 | } |
1907 | |
|
1908 | 0 | return(ret); |
1909 | 0 | } |
1910 | | |
1911 | | typedef struct { |
1912 | | char *mem; |
1913 | | const char *cur; |
1914 | | size_t size; |
1915 | | } xmlMemIOCtxt; |
1916 | | |
1917 | | static int |
1918 | 1.60M | xmlMemRead(void *vctxt, char *buf, int size) { |
1919 | 1.60M | xmlMemIOCtxt *ctxt = vctxt; |
1920 | | |
1921 | 1.60M | if ((size_t) size > ctxt->size) |
1922 | 1.30M | size = ctxt->size; |
1923 | | |
1924 | 1.60M | memcpy(buf, ctxt->cur, size); |
1925 | 1.60M | ctxt->cur += size; |
1926 | 1.60M | ctxt->size -= size; |
1927 | | |
1928 | 1.60M | return size; |
1929 | 1.60M | } |
1930 | | |
1931 | | static int |
1932 | 690k | xmlMemClose(void *vctxt) { |
1933 | 690k | xmlMemIOCtxt *ctxt = vctxt; |
1934 | | |
1935 | 690k | if (ctxt->mem != NULL) |
1936 | 568k | xmlFree(ctxt->mem); |
1937 | 690k | xmlFree(ctxt); |
1938 | 690k | return(0); |
1939 | 690k | } |
1940 | | |
1941 | | xmlParserInputBufferPtr |
1942 | | xmlNewInputBufferMemory(const void *mem, size_t size, int flags, |
1943 | 690k | xmlCharEncoding enc) { |
1944 | 690k | xmlParserInputBufferPtr ret; |
1945 | 690k | xmlMemIOCtxt *ctxt; |
1946 | 690k | char *copy = NULL; |
1947 | | |
1948 | 690k | if ((flags & XML_INPUT_BUF_STATIC) == 0) { |
1949 | 569k | if (size + 1 == 0) |
1950 | 0 | return(NULL); |
1951 | 569k | copy = xmlMalloc(size + 1); |
1952 | 569k | if (copy == NULL) |
1953 | 33 | return(NULL); |
1954 | 569k | memcpy(copy, mem, size); |
1955 | 569k | copy[size] = 0; |
1956 | | |
1957 | 569k | mem = copy; |
1958 | 569k | } |
1959 | | |
1960 | 690k | ret = xmlAllocParserInputBuffer(enc); |
1961 | 690k | if (ret == NULL) { |
1962 | 228 | xmlFree(copy); |
1963 | 228 | return(NULL); |
1964 | 228 | } |
1965 | | |
1966 | 690k | ctxt = xmlMalloc(sizeof(*ctxt)); |
1967 | 690k | if (ctxt == NULL) { |
1968 | 101 | xmlFreeParserInputBuffer(ret); |
1969 | 101 | xmlFree(copy); |
1970 | 101 | return(NULL); |
1971 | 101 | } |
1972 | | |
1973 | 690k | ctxt->mem = copy; |
1974 | 690k | ctxt->cur = mem; |
1975 | 690k | ctxt->size = size; |
1976 | | |
1977 | 690k | ret->context = ctxt; |
1978 | 690k | ret->readcallback = xmlMemRead; |
1979 | 690k | ret->closecallback = xmlMemClose; |
1980 | | |
1981 | 690k | return(ret); |
1982 | 690k | } |
1983 | | |
1984 | | /** |
1985 | | * xmlParserInputBufferCreateMem: |
1986 | | * @mem: the memory input |
1987 | | * @size: the length of the memory block |
1988 | | * @enc: the charset encoding if known (deprecated) |
1989 | | * |
1990 | | * Create a parser input buffer for parsing from a memory area. |
1991 | | * |
1992 | | * This function makes a copy of the whole input buffer. If you are sure |
1993 | | * that the contents of the buffer will remain valid until the document |
1994 | | * was parsed, you can avoid the copy by using |
1995 | | * xmlParserInputBufferCreateStatic. |
1996 | | * |
1997 | | * The encoding argument is deprecated and should be set to |
1998 | | * XML_CHAR_ENCODING_NONE. The encoding can be changed with |
1999 | | * xmlSwitchEncoding or xmlSwitchEncodingName later on. |
2000 | | * |
2001 | | * Returns the new parser input or NULL in case of error. |
2002 | | */ |
2003 | | xmlParserInputBufferPtr |
2004 | 569k | xmlParserInputBufferCreateMem(const char *mem, int size, xmlCharEncoding enc) { |
2005 | 569k | if ((mem == NULL) || (size < 0)) |
2006 | 0 | return(NULL); |
2007 | | |
2008 | 569k | return(xmlNewInputBufferMemory(mem, size, 0, enc)); |
2009 | 569k | } |
2010 | | |
2011 | | /** |
2012 | | * xmlParserInputBufferCreateStatic: |
2013 | | * @mem: the memory input |
2014 | | * @size: the length of the memory block |
2015 | | * @enc: the charset encoding if known |
2016 | | * |
2017 | | * Create a parser input buffer for parsing from a memory area. |
2018 | | * |
2019 | | * This functions assumes that the contents of the input buffer remain |
2020 | | * valid until the document was parsed. Use xmlParserInputBufferCreateMem |
2021 | | * otherwise. |
2022 | | * |
2023 | | * The encoding argument is deprecated and should be set to |
2024 | | * XML_CHAR_ENCODING_NONE. The encoding can be changed with |
2025 | | * xmlSwitchEncoding or xmlSwitchEncodingName later on. |
2026 | | * |
2027 | | * Returns the new parser input or NULL in case of error. |
2028 | | */ |
2029 | | xmlParserInputBufferPtr |
2030 | | xmlParserInputBufferCreateStatic(const char *mem, int size, |
2031 | 0 | xmlCharEncoding enc) { |
2032 | 0 | if ((mem == NULL) || (size < 0)) |
2033 | 0 | return(NULL); |
2034 | | |
2035 | 0 | return(xmlNewInputBufferMemory(mem, size, XML_INPUT_BUF_STATIC, enc)); |
2036 | 0 | } |
2037 | | |
2038 | | typedef struct { |
2039 | | const char *str; |
2040 | | } xmlStringIOCtxt; |
2041 | | |
2042 | | static int |
2043 | 349k | xmlStringRead(void *vctxt, char *buf, int size) { |
2044 | 349k | xmlStringIOCtxt *ctxt = vctxt; |
2045 | 349k | const char *zero; |
2046 | 349k | size_t len; |
2047 | | |
2048 | 349k | zero = memchr(ctxt->str, 0, size); |
2049 | 349k | len = zero ? zero - ctxt->str : size; |
2050 | | |
2051 | 349k | memcpy(buf, ctxt->str, len); |
2052 | 349k | ctxt->str += len; |
2053 | | |
2054 | 349k | return(len); |
2055 | 349k | } |
2056 | | |
2057 | | static int |
2058 | 148k | xmlStringClose(void *vctxt) { |
2059 | 148k | xmlFree(vctxt); |
2060 | 148k | return(0); |
2061 | 148k | } |
2062 | | |
2063 | | xmlParserInputBufferPtr |
2064 | 148k | xmlNewInputBufferString(const char *str, int flags) { |
2065 | 148k | xmlParserInputBufferPtr ret; |
2066 | 148k | xmlStringIOCtxt *ctxt; |
2067 | | |
2068 | 148k | if ((flags & XML_INPUT_BUF_STATIC) == 0) |
2069 | 0 | return(xmlNewInputBufferMemory(str, strlen(str), flags, |
2070 | 0 | XML_CHAR_ENCODING_NONE)); |
2071 | | |
2072 | 148k | ret = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE); |
2073 | 148k | if (ret == NULL) |
2074 | 125 | return(NULL); |
2075 | | |
2076 | 148k | ctxt = xmlMalloc(sizeof(*ctxt)); |
2077 | 148k | if (ctxt == NULL) { |
2078 | 55 | xmlFreeParserInputBuffer(ret); |
2079 | 55 | return(NULL); |
2080 | 55 | } |
2081 | | |
2082 | 148k | ctxt->str = str; |
2083 | | |
2084 | 148k | ret->context = ctxt; |
2085 | 148k | ret->readcallback = xmlStringRead; |
2086 | 148k | ret->closecallback = xmlStringClose; |
2087 | | |
2088 | 148k | return(ret); |
2089 | 148k | } |
2090 | | |
2091 | | #ifdef LIBXML_OUTPUT_ENABLED |
2092 | | /** |
2093 | | * xmlOutputBufferCreateFd: |
2094 | | * @fd: a file descriptor number |
2095 | | * @encoder: the encoding converter or NULL |
2096 | | * |
2097 | | * Create a buffered output for the progressive saving |
2098 | | * to a file descriptor |
2099 | | * |
2100 | | * Returns the new parser output or NULL |
2101 | | */ |
2102 | | xmlOutputBufferPtr |
2103 | 0 | xmlOutputBufferCreateFd(int fd, xmlCharEncodingHandlerPtr encoder) { |
2104 | 0 | xmlOutputBufferPtr ret; |
2105 | |
|
2106 | 0 | if (fd < 0) return(NULL); |
2107 | | |
2108 | 0 | ret = xmlAllocOutputBufferInternal(encoder); |
2109 | 0 | if (ret != NULL) { |
2110 | 0 | ret->context = (void *) (ptrdiff_t) fd; |
2111 | 0 | ret->writecallback = xmlFdWrite; |
2112 | 0 | ret->closecallback = NULL; |
2113 | 0 | } |
2114 | |
|
2115 | 0 | return(ret); |
2116 | 0 | } |
2117 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
2118 | | |
2119 | | /** |
2120 | | * xmlParserInputBufferCreateIO: |
2121 | | * @ioread: an I/O read function |
2122 | | * @ioclose: an I/O close function |
2123 | | * @ioctx: an I/O handler |
2124 | | * @enc: the charset encoding if known (deprecated) |
2125 | | * |
2126 | | * Create a buffered parser input for the progressive parsing for the input |
2127 | | * from an I/O handler |
2128 | | * |
2129 | | * The encoding argument is deprecated and should be set to |
2130 | | * XML_CHAR_ENCODING_NONE. The encoding can be changed with |
2131 | | * xmlSwitchEncoding or xmlSwitchEncodingName later on. |
2132 | | * |
2133 | | * Returns the new parser input or NULL |
2134 | | */ |
2135 | | xmlParserInputBufferPtr |
2136 | | xmlParserInputBufferCreateIO(xmlInputReadCallback ioread, |
2137 | 0 | xmlInputCloseCallback ioclose, void *ioctx, xmlCharEncoding enc) { |
2138 | 0 | xmlParserInputBufferPtr ret; |
2139 | |
|
2140 | 0 | if (ioread == NULL) return(NULL); |
2141 | | |
2142 | 0 | ret = xmlAllocParserInputBuffer(enc); |
2143 | 0 | if (ret != NULL) { |
2144 | 0 | ret->context = (void *) ioctx; |
2145 | 0 | ret->readcallback = ioread; |
2146 | 0 | ret->closecallback = ioclose; |
2147 | 0 | } |
2148 | |
|
2149 | 0 | return(ret); |
2150 | 0 | } |
2151 | | |
2152 | | #ifdef LIBXML_OUTPUT_ENABLED |
2153 | | /** |
2154 | | * xmlOutputBufferCreateIO: |
2155 | | * @iowrite: an I/O write function |
2156 | | * @ioclose: an I/O close function |
2157 | | * @ioctx: an I/O handler |
2158 | | * @encoder: the charset encoding if known |
2159 | | * |
2160 | | * Create a buffered output for the progressive saving |
2161 | | * to an I/O handler |
2162 | | * |
2163 | | * Returns the new parser output or NULL |
2164 | | */ |
2165 | | xmlOutputBufferPtr |
2166 | | xmlOutputBufferCreateIO(xmlOutputWriteCallback iowrite, |
2167 | | xmlOutputCloseCallback ioclose, void *ioctx, |
2168 | 13.9k | xmlCharEncodingHandlerPtr encoder) { |
2169 | 13.9k | xmlOutputBufferPtr ret; |
2170 | | |
2171 | 13.9k | if (iowrite == NULL) return(NULL); |
2172 | | |
2173 | 13.9k | ret = xmlAllocOutputBufferInternal(encoder); |
2174 | 13.9k | if (ret != NULL) { |
2175 | 13.8k | ret->context = (void *) ioctx; |
2176 | 13.8k | ret->writecallback = iowrite; |
2177 | 13.8k | ret->closecallback = ioclose; |
2178 | 13.8k | } |
2179 | | |
2180 | 13.9k | return(ret); |
2181 | 13.9k | } |
2182 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
2183 | | |
2184 | | /** |
2185 | | * xmlParserInputBufferCreateFilenameDefault: |
2186 | | * @func: function pointer to the new ParserInputBufferCreateFilenameFunc |
2187 | | * |
2188 | | * Registers a callback for URI input file handling |
2189 | | * |
2190 | | * Returns the old value of the registration function |
2191 | | */ |
2192 | | xmlParserInputBufferCreateFilenameFunc |
2193 | | xmlParserInputBufferCreateFilenameDefault(xmlParserInputBufferCreateFilenameFunc func) |
2194 | 0 | { |
2195 | 0 | xmlParserInputBufferCreateFilenameFunc old = xmlParserInputBufferCreateFilenameValue; |
2196 | 0 | if (old == NULL) { |
2197 | 0 | old = __xmlParserInputBufferCreateFilename; |
2198 | 0 | } |
2199 | |
|
2200 | 0 | xmlParserInputBufferCreateFilenameValue = func; |
2201 | 0 | return(old); |
2202 | 0 | } |
2203 | | |
2204 | | /** |
2205 | | * xmlOutputBufferCreateFilenameDefault: |
2206 | | * @func: function pointer to the new OutputBufferCreateFilenameFunc |
2207 | | * |
2208 | | * Registers a callback for URI output file handling |
2209 | | * |
2210 | | * Returns the old value of the registration function |
2211 | | */ |
2212 | | xmlOutputBufferCreateFilenameFunc |
2213 | | xmlOutputBufferCreateFilenameDefault(xmlOutputBufferCreateFilenameFunc func) |
2214 | 0 | { |
2215 | 0 | xmlOutputBufferCreateFilenameFunc old = xmlOutputBufferCreateFilenameValue; |
2216 | 0 | #ifdef LIBXML_OUTPUT_ENABLED |
2217 | 0 | if (old == NULL) { |
2218 | 0 | old = __xmlOutputBufferCreateFilename; |
2219 | 0 | } |
2220 | 0 | #endif |
2221 | 0 | xmlOutputBufferCreateFilenameValue = func; |
2222 | 0 | return(old); |
2223 | 0 | } |
2224 | | |
2225 | | /** |
2226 | | * xmlParserInputBufferPush: |
2227 | | * @in: a buffered parser input |
2228 | | * @len: the size in bytes of the array. |
2229 | | * @buf: an char array |
2230 | | * |
2231 | | * Push the content of the arry in the input buffer |
2232 | | * This routine handle the I18N transcoding to internal UTF-8 |
2233 | | * This is used when operating the parser in progressive (push) mode. |
2234 | | * |
2235 | | * Returns the number of chars read and stored in the buffer, or -1 |
2236 | | * in case of error. |
2237 | | */ |
2238 | | int |
2239 | | xmlParserInputBufferPush(xmlParserInputBufferPtr in, |
2240 | 4.01M | int len, const char *buf) { |
2241 | 4.01M | int nbchars = 0; |
2242 | 4.01M | int ret; |
2243 | | |
2244 | 4.01M | if (len < 0) return(0); |
2245 | 4.01M | if ((in == NULL) || (in->error)) return(-1); |
2246 | 4.01M | if (in->encoder != NULL) { |
2247 | | /* |
2248 | | * Store the data in the incoming raw buffer |
2249 | | */ |
2250 | 2.05M | if (in->raw == NULL) { |
2251 | 9 | in->raw = xmlBufCreate(); |
2252 | 9 | if (in->raw == NULL) { |
2253 | 0 | in->error = XML_ERR_NO_MEMORY; |
2254 | 0 | return(-1); |
2255 | 0 | } |
2256 | 9 | } |
2257 | 2.05M | ret = xmlBufAdd(in->raw, (const xmlChar *) buf, len); |
2258 | 2.05M | if (ret != 0) { |
2259 | 0 | in->error = XML_ERR_NO_MEMORY; |
2260 | 0 | return(-1); |
2261 | 0 | } |
2262 | | |
2263 | | /* |
2264 | | * convert as much as possible to the parser reading buffer. |
2265 | | */ |
2266 | 2.05M | nbchars = xmlCharEncInput(in); |
2267 | 2.05M | if (nbchars < 0) |
2268 | 143 | return(-1); |
2269 | 2.05M | } else { |
2270 | 1.95M | nbchars = len; |
2271 | 1.95M | ret = xmlBufAdd(in->buffer, (xmlChar *) buf, nbchars); |
2272 | 1.95M | if (ret != 0) { |
2273 | 13 | in->error = XML_ERR_NO_MEMORY; |
2274 | 13 | return(-1); |
2275 | 13 | } |
2276 | 1.95M | } |
2277 | 4.01M | return(nbchars); |
2278 | 4.01M | } |
2279 | | |
2280 | | /** |
2281 | | * endOfInput: |
2282 | | * |
2283 | | * When reading from an Input channel indicated end of file or error |
2284 | | * don't reread from it again. |
2285 | | */ |
2286 | | static int |
2287 | | endOfInput (void * context ATTRIBUTE_UNUSED, |
2288 | | char * buffer ATTRIBUTE_UNUSED, |
2289 | 36.7M | int len ATTRIBUTE_UNUSED) { |
2290 | 36.7M | return(0); |
2291 | 36.7M | } |
2292 | | |
2293 | | /** |
2294 | | * xmlParserInputBufferGrow: |
2295 | | * @in: a buffered parser input |
2296 | | * @len: indicative value of the amount of chars to read |
2297 | | * |
2298 | | * Grow up the content of the input buffer, the old data are preserved |
2299 | | * This routine handle the I18N transcoding to internal UTF-8 |
2300 | | * This routine is used when operating the parser in normal (pull) mode |
2301 | | * |
2302 | | * TODO: one should be able to remove one extra copy by copying directly |
2303 | | * onto in->buffer or in->raw |
2304 | | * |
2305 | | * Returns the number of chars read and stored in the buffer, or -1 |
2306 | | * in case of error. |
2307 | | */ |
2308 | | int |
2309 | 38.7M | xmlParserInputBufferGrow(xmlParserInputBufferPtr in, int len) { |
2310 | 38.7M | xmlBufPtr buf; |
2311 | 38.7M | int res = 0; |
2312 | | |
2313 | 38.7M | if ((in == NULL) || (in->error)) return(-1); |
2314 | 38.7M | if ((len <= MINLEN) && (len != 4)) |
2315 | 38.4M | len = MINLEN; |
2316 | | |
2317 | 38.7M | if (in->encoder == NULL) { |
2318 | 37.3M | if (in->readcallback == NULL) |
2319 | 0 | return(0); |
2320 | 37.3M | buf = in->buffer; |
2321 | 37.3M | } else { |
2322 | 1.38M | if (in->raw == NULL) { |
2323 | 291 | in->raw = xmlBufCreate(); |
2324 | 291 | } |
2325 | 1.38M | buf = in->raw; |
2326 | 1.38M | } |
2327 | | |
2328 | | /* |
2329 | | * Call the read method for this I/O type. |
2330 | | */ |
2331 | 38.7M | if (in->readcallback != NULL) { |
2332 | 38.7M | if (xmlBufGrow(buf, len + 1) < 0) { |
2333 | 16 | in->error = XML_ERR_NO_MEMORY; |
2334 | 16 | return(-1); |
2335 | 16 | } |
2336 | | |
2337 | 38.7M | res = in->readcallback(in->context, (char *)xmlBufEnd(buf), len); |
2338 | 38.7M | if (res <= 0) |
2339 | 37.5M | in->readcallback = endOfInput; |
2340 | 38.7M | if (res < 0) { |
2341 | 0 | if (res == -1) |
2342 | 0 | in->error = XML_IO_UNKNOWN; |
2343 | 0 | else |
2344 | 0 | in->error = -res; |
2345 | 0 | return(-1); |
2346 | 0 | } |
2347 | | |
2348 | 38.7M | if (xmlBufAddLen(buf, res) < 0) { |
2349 | 0 | in->error = XML_ERR_NO_MEMORY; |
2350 | 0 | return(-1); |
2351 | 0 | } |
2352 | 38.7M | } |
2353 | | |
2354 | | /* |
2355 | | * try to establish compressed status of input if not done already |
2356 | | */ |
2357 | 38.7M | if (in->compressed == -1) { |
2358 | 38.7M | #ifdef LIBXML_LZMA_ENABLED |
2359 | 38.7M | if (in->readcallback == xmlXzfileRead) |
2360 | 0 | in->compressed = __libxml2_xzcompressed(in->context); |
2361 | 38.7M | #endif |
2362 | 38.7M | } |
2363 | | |
2364 | 38.7M | if (in->encoder != NULL) { |
2365 | 1.38M | res = xmlCharEncInput(in); |
2366 | 1.38M | if (res < 0) |
2367 | 5.05k | return(-1); |
2368 | 1.38M | } |
2369 | 38.7M | return(res); |
2370 | 38.7M | } |
2371 | | |
2372 | | /** |
2373 | | * xmlParserInputBufferRead: |
2374 | | * @in: a buffered parser input |
2375 | | * @len: indicative value of the amount of chars to read |
2376 | | * |
2377 | | * Refresh the content of the input buffer, the old data are considered |
2378 | | * consumed |
2379 | | * This routine handle the I18N transcoding to internal UTF-8 |
2380 | | * |
2381 | | * Returns the number of chars read and stored in the buffer, or -1 |
2382 | | * in case of error. |
2383 | | */ |
2384 | | int |
2385 | 195k | xmlParserInputBufferRead(xmlParserInputBufferPtr in, int len) { |
2386 | 195k | return(xmlParserInputBufferGrow(in, len)); |
2387 | 195k | } |
2388 | | |
2389 | | #ifdef LIBXML_OUTPUT_ENABLED |
2390 | | /** |
2391 | | * xmlOutputBufferWrite: |
2392 | | * @out: a buffered parser output |
2393 | | * @len: the size in bytes of the array. |
2394 | | * @buf: an char array |
2395 | | * |
2396 | | * Write the content of the array in the output I/O buffer |
2397 | | * This routine handle the I18N transcoding from internal UTF-8 |
2398 | | * The buffer is lossless, i.e. will store in case of partial |
2399 | | * or delayed writes. |
2400 | | * |
2401 | | * Returns the number of chars immediately written, or -1 |
2402 | | * in case of error. |
2403 | | */ |
2404 | | int |
2405 | 25.9M | xmlOutputBufferWrite(xmlOutputBufferPtr out, int len, const char *buf) { |
2406 | 25.9M | int nbchars = 0; /* number of chars to output to I/O */ |
2407 | 25.9M | int ret; /* return from function call */ |
2408 | 25.9M | int written = 0; /* number of char written to I/O so far */ |
2409 | 25.9M | int chunk; /* number of byte current processed from buf */ |
2410 | | |
2411 | 25.9M | if ((out == NULL) || (out->error)) return(-1); |
2412 | 25.9M | if (len < 0) return(0); |
2413 | 25.9M | if (out->error) return(-1); |
2414 | | |
2415 | 26.0M | do { |
2416 | 26.0M | chunk = len; |
2417 | 26.0M | if (chunk > 4 * MINLEN) |
2418 | 72.7k | chunk = 4 * MINLEN; |
2419 | | |
2420 | | /* |
2421 | | * first handle encoding stuff. |
2422 | | */ |
2423 | 26.0M | if (out->encoder != NULL) { |
2424 | | /* |
2425 | | * Store the data in the incoming raw buffer |
2426 | | */ |
2427 | 562k | if (out->conv == NULL) { |
2428 | 0 | out->conv = xmlBufCreate(); |
2429 | 0 | if (out->conv == NULL) { |
2430 | 0 | out->error = XML_ERR_NO_MEMORY; |
2431 | 0 | return(-1); |
2432 | 0 | } |
2433 | 0 | } |
2434 | 562k | ret = xmlBufAdd(out->buffer, (const xmlChar *) buf, chunk); |
2435 | 562k | if (ret != 0) { |
2436 | 2 | out->error = XML_ERR_NO_MEMORY; |
2437 | 2 | return(-1); |
2438 | 2 | } |
2439 | | |
2440 | 562k | if ((xmlBufUse(out->buffer) < MINLEN) && (chunk == len)) |
2441 | 543k | goto done; |
2442 | | |
2443 | | /* |
2444 | | * convert as much as possible to the parser reading buffer. |
2445 | | */ |
2446 | 18.4k | ret = xmlCharEncOutput(out, 0); |
2447 | 18.4k | if (ret < 0) |
2448 | 9 | return(-1); |
2449 | 18.4k | if (out->writecallback) |
2450 | 18.4k | nbchars = xmlBufUse(out->conv); |
2451 | 0 | else |
2452 | 0 | nbchars = ret >= 0 ? ret : 0; |
2453 | 25.4M | } else { |
2454 | 25.4M | ret = xmlBufAdd(out->buffer, (const xmlChar *) buf, chunk); |
2455 | 25.4M | if (ret != 0) |
2456 | 11.6k | return(-1); |
2457 | 25.4M | if (out->writecallback) |
2458 | 23.5M | nbchars = xmlBufUse(out->buffer); |
2459 | 1.93M | else |
2460 | 1.93M | nbchars = chunk; |
2461 | 25.4M | } |
2462 | 25.4M | buf += chunk; |
2463 | 25.4M | len -= chunk; |
2464 | | |
2465 | 25.4M | if (out->writecallback) { |
2466 | 23.5M | if ((nbchars < MINLEN) && (len <= 0)) |
2467 | 23.4M | goto done; |
2468 | | |
2469 | | /* |
2470 | | * second write the stuff to the I/O channel |
2471 | | */ |
2472 | 61.7k | if (out->encoder != NULL) { |
2473 | 17.3k | ret = out->writecallback(out->context, |
2474 | 17.3k | (const char *)xmlBufContent(out->conv), nbchars); |
2475 | 17.3k | if (ret >= 0) |
2476 | 17.3k | xmlBufShrink(out->conv, ret); |
2477 | 44.3k | } else { |
2478 | 44.3k | ret = out->writecallback(out->context, |
2479 | 44.3k | (const char *)xmlBufContent(out->buffer), nbchars); |
2480 | 44.3k | if (ret >= 0) |
2481 | 44.3k | xmlBufShrink(out->buffer, ret); |
2482 | 44.3k | } |
2483 | 61.7k | if (ret < 0) { |
2484 | 26 | int errNo = (ret == -1) ? XML_IO_WRITE : -ret; |
2485 | | |
2486 | 26 | xmlIOErr(errNo, NULL); |
2487 | 26 | out->error = errNo; |
2488 | 26 | return(ret); |
2489 | 26 | } |
2490 | 61.6k | if (out->written > INT_MAX - ret) |
2491 | 0 | out->written = INT_MAX; |
2492 | 61.6k | else |
2493 | 61.6k | out->written += ret; |
2494 | 61.6k | } |
2495 | 1.99M | written += nbchars; |
2496 | 1.99M | } while (len > 0); |
2497 | | |
2498 | 25.9M | done: |
2499 | 25.9M | return(written); |
2500 | 25.9M | } |
2501 | | |
2502 | | /** |
2503 | | * xmlEscapeContent: |
2504 | | * @out: a pointer to an array of bytes to store the result |
2505 | | * @outlen: the length of @out |
2506 | | * @in: a pointer to an array of unescaped UTF-8 bytes |
2507 | | * @inlen: the length of @in |
2508 | | * |
2509 | | * Take a block of UTF-8 chars in and escape them. |
2510 | | * Returns 0 if success, or -1 otherwise |
2511 | | * The value of @inlen after return is the number of octets consumed |
2512 | | * if the return value is positive, else unpredictable. |
2513 | | * The value of @outlen after return is the number of octets consumed. |
2514 | | */ |
2515 | | static int |
2516 | | xmlEscapeContent(unsigned char* out, int *outlen, |
2517 | 59.3k | const xmlChar* in, int *inlen) { |
2518 | 59.3k | unsigned char* outstart = out; |
2519 | 59.3k | const unsigned char* base = in; |
2520 | 59.3k | unsigned char* outend = out + *outlen; |
2521 | 59.3k | const unsigned char* inend; |
2522 | | |
2523 | 59.3k | inend = in + (*inlen); |
2524 | | |
2525 | 220M | while ((in < inend) && (out < outend)) { |
2526 | 220M | if (*in == '<') { |
2527 | 233k | if (outend - out < 4) break; |
2528 | 233k | *out++ = '&'; |
2529 | 233k | *out++ = 'l'; |
2530 | 233k | *out++ = 't'; |
2531 | 233k | *out++ = ';'; |
2532 | 220M | } else if (*in == '>') { |
2533 | 293k | if (outend - out < 4) break; |
2534 | 292k | *out++ = '&'; |
2535 | 292k | *out++ = 'g'; |
2536 | 292k | *out++ = 't'; |
2537 | 292k | *out++ = ';'; |
2538 | 220M | } else if (*in == '&') { |
2539 | 9.67k | if (outend - out < 5) break; |
2540 | 9.63k | *out++ = '&'; |
2541 | 9.63k | *out++ = 'a'; |
2542 | 9.63k | *out++ = 'm'; |
2543 | 9.63k | *out++ = 'p'; |
2544 | 9.63k | *out++ = ';'; |
2545 | 220M | } else if (*in == '\r') { |
2546 | 34.5k | if (outend - out < 5) break; |
2547 | 34.4k | *out++ = '&'; |
2548 | 34.4k | *out++ = '#'; |
2549 | 34.4k | *out++ = '1'; |
2550 | 34.4k | *out++ = '3'; |
2551 | 34.4k | *out++ = ';'; |
2552 | 219M | } else { |
2553 | 219M | *out++ = *in; |
2554 | 219M | } |
2555 | 220M | ++in; |
2556 | 220M | } |
2557 | 59.3k | *outlen = out - outstart; |
2558 | 59.3k | *inlen = in - base; |
2559 | 59.3k | return(0); |
2560 | 59.3k | } |
2561 | | |
2562 | | /** |
2563 | | * xmlOutputBufferWriteEscape: |
2564 | | * @out: a buffered parser output |
2565 | | * @str: a zero terminated UTF-8 string |
2566 | | * @escaping: an optional escaping function (or NULL) |
2567 | | * |
2568 | | * Write the content of the string in the output I/O buffer |
2569 | | * This routine escapes the characters and then handle the I18N |
2570 | | * transcoding from internal UTF-8 |
2571 | | * The buffer is lossless, i.e. will store in case of partial |
2572 | | * or delayed writes. |
2573 | | * |
2574 | | * Returns the number of chars immediately written, or -1 |
2575 | | * in case of error. |
2576 | | */ |
2577 | | int |
2578 | | xmlOutputBufferWriteEscape(xmlOutputBufferPtr out, const xmlChar *str, |
2579 | 313k | xmlCharEncodingOutputFunc escaping) { |
2580 | 313k | int nbchars = 0; /* number of chars to output to I/O */ |
2581 | 313k | int ret; /* return from function call */ |
2582 | 313k | int written = 0; /* number of char written to I/O so far */ |
2583 | 313k | int oldwritten=0;/* loop guard */ |
2584 | 313k | int chunk; /* number of byte currently processed from str */ |
2585 | 313k | int len; /* number of bytes in str */ |
2586 | 313k | int cons; /* byte from str consumed */ |
2587 | | |
2588 | 313k | if ((out == NULL) || (out->error) || (str == NULL) || |
2589 | 313k | (out->buffer == NULL)) |
2590 | 369 | return(-1); |
2591 | 312k | len = strlen((const char *)str); |
2592 | 312k | if (len < 0) return(0); |
2593 | 312k | if (out->error) return(-1); |
2594 | 312k | if (escaping == NULL) escaping = xmlEscapeContent; |
2595 | | |
2596 | 352k | do { |
2597 | 352k | oldwritten = written; |
2598 | | |
2599 | | /* |
2600 | | * how many bytes to consume and how many bytes to store. |
2601 | | */ |
2602 | 352k | cons = len; |
2603 | 352k | chunk = xmlBufAvail(out->buffer); |
2604 | | |
2605 | | /* |
2606 | | * make sure we have enough room to save first, if this is |
2607 | | * not the case force a flush, but make sure we stay in the loop |
2608 | | */ |
2609 | 352k | if (chunk < 40) { |
2610 | 490 | if (xmlBufGrow(out->buffer, 100) < 0) |
2611 | 465 | return(-1); |
2612 | 25 | oldwritten = -1; |
2613 | 25 | continue; |
2614 | 490 | } |
2615 | | |
2616 | | /* |
2617 | | * first handle encoding stuff. |
2618 | | */ |
2619 | 352k | if (out->encoder != NULL) { |
2620 | | /* |
2621 | | * Store the data in the incoming raw buffer |
2622 | | */ |
2623 | 57.0k | if (out->conv == NULL) { |
2624 | 0 | out->conv = xmlBufCreate(); |
2625 | 0 | } |
2626 | 57.0k | ret = escaping(xmlBufEnd(out->buffer) , |
2627 | 57.0k | &chunk, str, &cons); |
2628 | 57.0k | if ((ret < 0) || (chunk == 0)) /* chunk==0 => nothing done */ |
2629 | 604 | return(-1); |
2630 | 56.4k | xmlBufAddLen(out->buffer, chunk); |
2631 | | |
2632 | 56.4k | if ((xmlBufUse(out->buffer) < MINLEN) && (cons == len)) |
2633 | 27.5k | goto done; |
2634 | | |
2635 | | /* |
2636 | | * convert as much as possible to the output buffer. |
2637 | | */ |
2638 | 28.8k | ret = xmlCharEncOutput(out, 0); |
2639 | 28.8k | if (ret < 0) |
2640 | 5 | return(-1); |
2641 | 28.8k | if (out->writecallback) |
2642 | 28.8k | nbchars = xmlBufUse(out->conv); |
2643 | 0 | else |
2644 | 0 | nbchars = ret >= 0 ? ret : 0; |
2645 | 295k | } else { |
2646 | 295k | ret = escaping(xmlBufEnd(out->buffer), &chunk, str, &cons); |
2647 | 295k | if ((ret < 0) || (chunk == 0)) /* chunk==0 => nothing done */ |
2648 | 291 | return(-1); |
2649 | 294k | xmlBufAddLen(out->buffer, chunk); |
2650 | 294k | if (out->writecallback) |
2651 | 294k | nbchars = xmlBufUse(out->buffer); |
2652 | 0 | else |
2653 | 0 | nbchars = chunk; |
2654 | 294k | } |
2655 | 323k | str += cons; |
2656 | 323k | len -= cons; |
2657 | | |
2658 | 323k | if (out->writecallback) { |
2659 | 323k | if ((nbchars < MINLEN) && (len <= 0)) |
2660 | 280k | goto done; |
2661 | | |
2662 | | /* |
2663 | | * second write the stuff to the I/O channel |
2664 | | */ |
2665 | 42.6k | if (out->encoder != NULL) { |
2666 | 28.4k | ret = out->writecallback(out->context, |
2667 | 28.4k | (const char *)xmlBufContent(out->conv), nbchars); |
2668 | 28.4k | if (ret >= 0) |
2669 | 28.4k | xmlBufShrink(out->conv, ret); |
2670 | 28.4k | } else { |
2671 | 14.1k | ret = out->writecallback(out->context, |
2672 | 14.1k | (const char *)xmlBufContent(out->buffer), nbchars); |
2673 | 14.1k | if (ret >= 0) |
2674 | 14.1k | xmlBufShrink(out->buffer, ret); |
2675 | 14.1k | } |
2676 | 42.6k | if (ret < 0) { |
2677 | 5 | int errNo = (ret == -1) ? XML_IO_WRITE : -ret; |
2678 | 5 | xmlIOErr(errNo, NULL); |
2679 | 5 | out->error = errNo; |
2680 | 5 | return(ret); |
2681 | 5 | } |
2682 | 42.5k | if (out->written > INT_MAX - ret) |
2683 | 0 | out->written = INT_MAX; |
2684 | 42.5k | else |
2685 | 42.5k | out->written += ret; |
2686 | 42.5k | } else if (xmlBufAvail(out->buffer) < MINLEN) { |
2687 | 0 | xmlBufGrow(out->buffer, MINLEN); |
2688 | 0 | } |
2689 | 42.5k | written += nbchars; |
2690 | 42.6k | } while ((len > 0) && (oldwritten != written)); |
2691 | | |
2692 | 311k | done: |
2693 | 311k | return(written); |
2694 | 312k | } |
2695 | | |
2696 | | /** |
2697 | | * xmlOutputBufferWriteString: |
2698 | | * @out: a buffered parser output |
2699 | | * @str: a zero terminated C string |
2700 | | * |
2701 | | * Write the content of the string in the output I/O buffer |
2702 | | * This routine handle the I18N transcoding from internal UTF-8 |
2703 | | * The buffer is lossless, i.e. will store in case of partial |
2704 | | * or delayed writes. |
2705 | | * |
2706 | | * Returns the number of chars immediately written, or -1 |
2707 | | * in case of error. |
2708 | | */ |
2709 | | int |
2710 | 21.4M | xmlOutputBufferWriteString(xmlOutputBufferPtr out, const char *str) { |
2711 | 21.4M | int len; |
2712 | | |
2713 | 21.4M | if ((out == NULL) || (out->error)) return(-1); |
2714 | 21.1M | if (str == NULL) |
2715 | 2.11k | return(-1); |
2716 | 21.1M | len = strlen(str); |
2717 | | |
2718 | 21.1M | if (len > 0) |
2719 | 21.0M | return(xmlOutputBufferWrite(out, len, str)); |
2720 | 128k | return(len); |
2721 | 21.1M | } |
2722 | | |
2723 | | /** |
2724 | | * xmlOutputBufferWriteQuotedString: |
2725 | | * @buf: output buffer |
2726 | | * @string: the string to add |
2727 | | * |
2728 | | * routine which manage and grows an output buffer. This one writes |
2729 | | * a quoted or double quoted #xmlChar string, checking first if it holds |
2730 | | * quote or double-quotes internally |
2731 | | */ |
2732 | | void |
2733 | | xmlOutputBufferWriteQuotedString(xmlOutputBufferPtr buf, |
2734 | 272k | const xmlChar *string) { |
2735 | 272k | const xmlChar *cur, *base; |
2736 | | |
2737 | 272k | if ((buf == NULL) || (buf->error)) |
2738 | 20.6k | return; |
2739 | | |
2740 | 251k | if (xmlStrchr(string, '\"')) { |
2741 | 5.39k | if (xmlStrchr(string, '\'')) { |
2742 | 1.88k | xmlOutputBufferWrite(buf, 1, "\""); |
2743 | 1.88k | base = cur = string; |
2744 | 172M | while(*cur != 0){ |
2745 | 172M | if(*cur == '"'){ |
2746 | 8.73k | if (base != cur) |
2747 | 3.57k | xmlOutputBufferWrite(buf, cur - base, |
2748 | 3.57k | (const char *) base); |
2749 | 8.73k | xmlOutputBufferWrite(buf, 6, """); |
2750 | 8.73k | cur++; |
2751 | 8.73k | base = cur; |
2752 | 8.73k | } |
2753 | 172M | else { |
2754 | 172M | cur++; |
2755 | 172M | } |
2756 | 172M | } |
2757 | 1.88k | if (base != cur) |
2758 | 1.30k | xmlOutputBufferWrite(buf, cur - base, (const char *) base); |
2759 | 1.88k | xmlOutputBufferWrite(buf, 1, "\""); |
2760 | 1.88k | } |
2761 | 3.51k | else{ |
2762 | 3.51k | xmlOutputBufferWrite(buf, 1, "'"); |
2763 | 3.51k | xmlOutputBufferWriteString(buf, (const char *) string); |
2764 | 3.51k | xmlOutputBufferWrite(buf, 1, "'"); |
2765 | 3.51k | } |
2766 | 246k | } else { |
2767 | 246k | xmlOutputBufferWrite(buf, 1, "\""); |
2768 | 246k | xmlOutputBufferWriteString(buf, (const char *) string); |
2769 | 246k | xmlOutputBufferWrite(buf, 1, "\""); |
2770 | 246k | } |
2771 | 251k | } |
2772 | | |
2773 | | /** |
2774 | | * xmlOutputBufferFlush: |
2775 | | * @out: a buffered output |
2776 | | * |
2777 | | * flushes the output I/O channel |
2778 | | * |
2779 | | * Returns the number of byte written or -1 in case of error. |
2780 | | */ |
2781 | | int |
2782 | 29.2k | xmlOutputBufferFlush(xmlOutputBufferPtr out) { |
2783 | 29.2k | int nbchars = 0, ret = 0; |
2784 | | |
2785 | 29.2k | if ((out == NULL) || (out->error)) return(-1); |
2786 | | /* |
2787 | | * first handle encoding stuff. |
2788 | | */ |
2789 | 28.5k | if ((out->conv != NULL) && (out->encoder != NULL)) { |
2790 | | /* |
2791 | | * convert as much as possible to the parser output buffer. |
2792 | | */ |
2793 | 3.28k | do { |
2794 | 3.28k | nbchars = xmlCharEncOutput(out, 0); |
2795 | 3.28k | if (nbchars < 0) |
2796 | 82 | return(-1); |
2797 | 3.28k | } while (nbchars); |
2798 | 1.65k | } |
2799 | | |
2800 | | /* |
2801 | | * second flush the stuff to the I/O channel |
2802 | | */ |
2803 | 28.4k | if ((out->conv != NULL) && (out->encoder != NULL) && |
2804 | 28.4k | (out->writecallback != NULL)) { |
2805 | 1.56k | ret = out->writecallback(out->context, |
2806 | 1.56k | (const char *)xmlBufContent(out->conv), |
2807 | 1.56k | xmlBufUse(out->conv)); |
2808 | 1.56k | if (ret >= 0) |
2809 | 1.56k | xmlBufShrink(out->conv, ret); |
2810 | 26.8k | } else if (out->writecallback != NULL) { |
2811 | 26.8k | ret = out->writecallback(out->context, |
2812 | 26.8k | (const char *)xmlBufContent(out->buffer), |
2813 | 26.8k | xmlBufUse(out->buffer)); |
2814 | 26.8k | if (ret >= 0) |
2815 | 26.8k | xmlBufShrink(out->buffer, ret); |
2816 | 26.8k | } |
2817 | 28.4k | if (ret < 0) { |
2818 | 20 | int errNo = (ret == -1) ? XML_IO_WRITE : -ret; |
2819 | | |
2820 | 20 | xmlIOErr(errNo, NULL); |
2821 | 20 | out->error = errNo; |
2822 | 20 | return(ret); |
2823 | 20 | } |
2824 | 28.4k | if (out->written > INT_MAX - ret) |
2825 | 0 | out->written = INT_MAX; |
2826 | 28.4k | else |
2827 | 28.4k | out->written += ret; |
2828 | | |
2829 | 28.4k | return(ret); |
2830 | 28.4k | } |
2831 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
2832 | | |
2833 | | /** |
2834 | | * xmlParserGetDirectory: |
2835 | | * @filename: the path to a file |
2836 | | * |
2837 | | * lookup the directory for that file |
2838 | | * |
2839 | | * Returns a new allocated string containing the directory, or NULL. |
2840 | | */ |
2841 | | char * |
2842 | 0 | xmlParserGetDirectory(const char *filename) { |
2843 | 0 | char *ret = NULL; |
2844 | 0 | char dir[1024]; |
2845 | 0 | char *cur; |
2846 | |
|
2847 | 0 | if (filename == NULL) return(NULL); |
2848 | | |
2849 | | #if defined(_WIN32) |
2850 | | # define IS_XMLPGD_SEP(ch) ((ch=='/')||(ch=='\\')) |
2851 | | #else |
2852 | 0 | # define IS_XMLPGD_SEP(ch) (ch=='/') |
2853 | 0 | #endif |
2854 | | |
2855 | 0 | strncpy(dir, filename, 1023); |
2856 | 0 | dir[1023] = 0; |
2857 | 0 | cur = &dir[strlen(dir)]; |
2858 | 0 | while (cur > dir) { |
2859 | 0 | if (IS_XMLPGD_SEP(*cur)) break; |
2860 | 0 | cur --; |
2861 | 0 | } |
2862 | 0 | if (IS_XMLPGD_SEP(*cur)) { |
2863 | 0 | if (cur == dir) dir[1] = 0; |
2864 | 0 | else *cur = 0; |
2865 | 0 | ret = xmlMemStrdup(dir); |
2866 | 0 | } else { |
2867 | 0 | if (getcwd(dir, 1024) != NULL) { |
2868 | 0 | dir[1023] = 0; |
2869 | 0 | ret = xmlMemStrdup(dir); |
2870 | 0 | } |
2871 | 0 | } |
2872 | 0 | return(ret); |
2873 | 0 | #undef IS_XMLPGD_SEP |
2874 | 0 | } |
2875 | | |
2876 | | /** |
2877 | | * xmlNoNetExists: |
2878 | | * @filename: the path to check |
2879 | | * |
2880 | | * DEPRECATED: Internal function, don't use. |
2881 | | * |
2882 | | * Like xmlCheckFilename but handles file URIs. |
2883 | | */ |
2884 | | int |
2885 | 0 | xmlNoNetExists(const char *filename) { |
2886 | 0 | char *fromUri; |
2887 | 0 | int ret; |
2888 | |
|
2889 | 0 | if (filename == NULL) |
2890 | 0 | return(0); |
2891 | | |
2892 | 0 | if (xmlConvertUriToPath(filename, &fromUri) < 0) |
2893 | 0 | return(0); |
2894 | | |
2895 | 0 | if (fromUri != NULL) |
2896 | 0 | filename = fromUri; |
2897 | |
|
2898 | 0 | ret = xmlCheckFilename(filename); |
2899 | |
|
2900 | 0 | xmlFree(fromUri); |
2901 | 0 | return(ret); |
2902 | 0 | } |
2903 | | |
2904 | | /************************************************************************ |
2905 | | * * |
2906 | | * Input/output callbacks * |
2907 | | * * |
2908 | | ************************************************************************/ |
2909 | | |
2910 | | /** |
2911 | | * xmlInitIOCallbacks: |
2912 | | * |
2913 | | * Initialize callback tables. |
2914 | | */ |
2915 | | void |
2916 | | xmlInitIOCallbacks(void) |
2917 | 16 | { |
2918 | 16 | xmlInputCallbackNr = 1; |
2919 | 16 | xmlInputCallbackTable[0].matchcallback = xmlIODefaultMatch; |
2920 | | |
2921 | 16 | #ifdef LIBXML_OUTPUT_ENABLED |
2922 | 16 | xmlOutputCallbackNr = 1; |
2923 | 16 | xmlOutputCallbackTable[0].matchcallback = xmlIODefaultMatch; |
2924 | 16 | #endif |
2925 | 16 | } |
2926 | | |
2927 | | /** |
2928 | | * xmlRegisterInputCallbacks: |
2929 | | * @matchFunc: the xmlInputMatchCallback |
2930 | | * @openFunc: the xmlInputOpenCallback |
2931 | | * @readFunc: the xmlInputReadCallback |
2932 | | * @closeFunc: the xmlInputCloseCallback |
2933 | | * |
2934 | | * Register a new set of I/O callback for handling parser input. |
2935 | | * |
2936 | | * Returns the registered handler number or -1 in case of error |
2937 | | */ |
2938 | | int |
2939 | | xmlRegisterInputCallbacks(xmlInputMatchCallback matchFunc, |
2940 | | xmlInputOpenCallback openFunc, xmlInputReadCallback readFunc, |
2941 | 0 | xmlInputCloseCallback closeFunc) { |
2942 | 0 | if (xmlInputCallbackNr >= MAX_INPUT_CALLBACK) { |
2943 | 0 | return(-1); |
2944 | 0 | } |
2945 | 0 | xmlInputCallbackTable[xmlInputCallbackNr].matchcallback = matchFunc; |
2946 | 0 | xmlInputCallbackTable[xmlInputCallbackNr].opencallback = openFunc; |
2947 | 0 | xmlInputCallbackTable[xmlInputCallbackNr].readcallback = readFunc; |
2948 | 0 | xmlInputCallbackTable[xmlInputCallbackNr].closecallback = closeFunc; |
2949 | 0 | return(xmlInputCallbackNr++); |
2950 | 0 | } |
2951 | | |
2952 | | /** |
2953 | | * xmlRegisterDefaultInputCallbacks: |
2954 | | * |
2955 | | * Registers the default compiled-in I/O handlers. |
2956 | | */ |
2957 | | void |
2958 | 0 | xmlRegisterDefaultInputCallbacks(void) { |
2959 | 0 | xmlRegisterInputCallbacks(xmlIODefaultMatch, NULL, NULL, NULL); |
2960 | 0 | } |
2961 | | |
2962 | | /** |
2963 | | * xmlPopInputCallbacks: |
2964 | | * |
2965 | | * Clear the top input callback from the input stack. this includes the |
2966 | | * compiled-in I/O. |
2967 | | * |
2968 | | * Returns the number of input callback registered or -1 in case of error. |
2969 | | */ |
2970 | | int |
2971 | | xmlPopInputCallbacks(void) |
2972 | 0 | { |
2973 | 0 | if (xmlInputCallbackNr <= 0) |
2974 | 0 | return(-1); |
2975 | | |
2976 | 0 | xmlInputCallbackNr--; |
2977 | |
|
2978 | 0 | return(xmlInputCallbackNr); |
2979 | 0 | } |
2980 | | |
2981 | | /** |
2982 | | * xmlCleanupInputCallbacks: |
2983 | | * |
2984 | | * clears the entire input callback table. this includes the |
2985 | | * compiled-in I/O. |
2986 | | */ |
2987 | | void |
2988 | | xmlCleanupInputCallbacks(void) |
2989 | 0 | { |
2990 | 0 | xmlInputCallbackNr = 0; |
2991 | 0 | } |
2992 | | |
2993 | | #ifdef LIBXML_OUTPUT_ENABLED |
2994 | | /** |
2995 | | * xmlRegisterOutputCallbacks: |
2996 | | * @matchFunc: the xmlOutputMatchCallback |
2997 | | * @openFunc: the xmlOutputOpenCallback |
2998 | | * @writeFunc: the xmlOutputWriteCallback |
2999 | | * @closeFunc: the xmlOutputCloseCallback |
3000 | | * |
3001 | | * Register a new set of I/O callback for handling output. |
3002 | | * |
3003 | | * Returns the registered handler number or -1 in case of error |
3004 | | */ |
3005 | | int |
3006 | | xmlRegisterOutputCallbacks(xmlOutputMatchCallback matchFunc, |
3007 | | xmlOutputOpenCallback openFunc, xmlOutputWriteCallback writeFunc, |
3008 | 0 | xmlOutputCloseCallback closeFunc) { |
3009 | 0 | if (xmlOutputCallbackNr >= MAX_OUTPUT_CALLBACK) { |
3010 | 0 | return(-1); |
3011 | 0 | } |
3012 | 0 | xmlOutputCallbackTable[xmlOutputCallbackNr].matchcallback = matchFunc; |
3013 | 0 | xmlOutputCallbackTable[xmlOutputCallbackNr].opencallback = openFunc; |
3014 | 0 | xmlOutputCallbackTable[xmlOutputCallbackNr].writecallback = writeFunc; |
3015 | 0 | xmlOutputCallbackTable[xmlOutputCallbackNr].closecallback = closeFunc; |
3016 | 0 | return(xmlOutputCallbackNr++); |
3017 | 0 | } |
3018 | | |
3019 | | /** |
3020 | | * xmlRegisterDefaultOutputCallbacks: |
3021 | | * |
3022 | | * Registers the default compiled-in I/O handlers. |
3023 | | */ |
3024 | | void |
3025 | 0 | xmlRegisterDefaultOutputCallbacks (void) { |
3026 | 0 | xmlRegisterOutputCallbacks(xmlIODefaultMatch, NULL, NULL, NULL); |
3027 | 0 | } |
3028 | | |
3029 | | /** |
3030 | | * xmlPopOutputCallbacks: |
3031 | | * |
3032 | | * Remove the top output callbacks from the output stack. This includes the |
3033 | | * compiled-in I/O. |
3034 | | * |
3035 | | * Returns the number of output callback registered or -1 in case of error. |
3036 | | */ |
3037 | | int |
3038 | | xmlPopOutputCallbacks(void) |
3039 | 0 | { |
3040 | 0 | if (xmlOutputCallbackNr <= 0) |
3041 | 0 | return(-1); |
3042 | | |
3043 | 0 | xmlOutputCallbackNr--; |
3044 | |
|
3045 | 0 | return(xmlOutputCallbackNr); |
3046 | 0 | } |
3047 | | |
3048 | | /** |
3049 | | * xmlCleanupOutputCallbacks: |
3050 | | * |
3051 | | * clears the entire output callback table. this includes the |
3052 | | * compiled-in I/O callbacks. |
3053 | | */ |
3054 | | void |
3055 | | xmlCleanupOutputCallbacks(void) |
3056 | 0 | { |
3057 | 0 | xmlOutputCallbackNr = 0; |
3058 | 0 | } |
3059 | | |
3060 | | #ifdef LIBXML_HTTP_ENABLED |
3061 | | /** |
3062 | | * xmlRegisterHTTPPostCallbacks: |
3063 | | * |
3064 | | * DEPRECATED: Support for HTTP POST has been removed. |
3065 | | */ |
3066 | | void |
3067 | | xmlRegisterHTTPPostCallbacks(void) { |
3068 | | xmlRegisterDefaultOutputCallbacks(); |
3069 | | } |
3070 | | #endif |
3071 | | #endif /* LIBXML_OUTPUT_ENABLED */ |
3072 | | |