blob: 25f3bb1668ed49990acab9399fef44057aace0c2 [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
2 * xmlIO.c : implementation of the I/O interfaces used by the parser
3 *
4 * See Copyright for the status of this software.
5 *
Daniel Veillard344cee72001-08-20 00:08:40 +00006 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +00007 *
8 * 14 Nov 2000 ht - for VMS, truncated name of long functions to under 32 char
9 */
10
Daniel Veillard34ce8be2002-03-18 19:37:11 +000011#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000012#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000013
Owen Taylor3473f882001-02-23 17:55:21 +000014#include <string.h>
Daniel Veillard92727042002-09-17 17:59:20 +000015#ifdef HAVE_ERRNO_H
Owen Taylor3473f882001-02-23 17:55:21 +000016#include <errno.h>
Daniel Veillard92727042002-09-17 17:59:20 +000017#endif
18
Owen Taylor3473f882001-02-23 17:55:21 +000019
20#ifdef HAVE_SYS_TYPES_H
21#include <sys/types.h>
22#endif
23#ifdef HAVE_SYS_STAT_H
24#include <sys/stat.h>
25#endif
26#ifdef HAVE_FCNTL_H
27#include <fcntl.h>
28#endif
29#ifdef HAVE_UNISTD_H
30#include <unistd.h>
31#endif
32#ifdef HAVE_STDLIB_H
33#include <stdlib.h>
34#endif
35#ifdef HAVE_ZLIB_H
36#include <zlib.h>
37#endif
38
Daniel Veillardf7416012006-04-27 08:15:20 +000039#ifdef WIN32
40#include <windows.h>
41#endif
42
Owen Taylor3473f882001-02-23 17:55:21 +000043/* Figure a portable way to know if a file is a directory. */
44#ifndef HAVE_STAT
45# ifdef HAVE__STAT
Daniel Veillard50f34372001-08-03 12:06:36 +000046 /* MS C library seems to define stat and _stat. The definition
47 is identical. Still, mapping them to each other causes a warning. */
48# ifndef _MSC_VER
49# define stat(x,y) _stat(x,y)
50# endif
Owen Taylor3473f882001-02-23 17:55:21 +000051# define HAVE_STAT
52# endif
Daniel Veillard8ca85b22006-09-01 09:56:07 +000053#else
54# ifdef HAVE__STAT
Daniel Veillard0da41662006-10-10 09:05:36 +000055# if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
Daniel Veillard8ca85b22006-09-01 09:56:07 +000056# define stat _stat
Daniel Veillard0da41662006-10-10 09:05:36 +000057# endif
Daniel Veillard8ca85b22006-09-01 09:56:07 +000058# endif
Owen Taylor3473f882001-02-23 17:55:21 +000059#endif
60#ifdef HAVE_STAT
61# ifndef S_ISDIR
62# ifdef _S_ISDIR
63# define S_ISDIR(x) _S_ISDIR(x)
64# else
65# ifdef S_IFDIR
66# ifndef S_IFMT
67# ifdef _S_IFMT
68# define S_IFMT _S_IFMT
69# endif
70# endif
71# ifdef S_IFMT
72# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
73# endif
74# endif
75# endif
76# endif
77#endif
78
79#include <libxml/xmlmemory.h>
80#include <libxml/parser.h>
81#include <libxml/parserInternals.h>
82#include <libxml/xmlIO.h>
Daniel Veillard388236f2001-07-08 18:35:48 +000083#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000084#include <libxml/nanohttp.h>
85#include <libxml/nanoftp.h>
86#include <libxml/xmlerror.h>
Daniel Veillard7d6fd212001-05-10 15:34:11 +000087#ifdef LIBXML_CATALOG_ENABLED
88#include <libxml/catalog.h>
89#endif
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000090#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000091
Daniel Veillardf012a642001-07-23 19:10:52 +000092/* #define VERBOSE_FAILURE */
Daniel Veillard1fd36d22001-07-04 22:54:28 +000093/* #define DEBUG_EXTERNAL_ENTITIES */
Owen Taylor3473f882001-02-23 17:55:21 +000094/* #define DEBUG_INPUT */
95
96#ifdef DEBUG_INPUT
97#define MINLEN 40
98#else
99#define MINLEN 4000
100#endif
101
102/*
103 * Input I/O callback sets
104 */
105typedef struct _xmlInputCallback {
106 xmlInputMatchCallback matchcallback;
107 xmlInputOpenCallback opencallback;
108 xmlInputReadCallback readcallback;
109 xmlInputCloseCallback closecallback;
110} xmlInputCallback;
111
112#define MAX_INPUT_CALLBACK 15
113
Daniel Veillard22090732001-07-16 00:06:07 +0000114static xmlInputCallback xmlInputCallbackTable[MAX_INPUT_CALLBACK];
115static int xmlInputCallbackNr = 0;
116static int xmlInputCallbackInitialized = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000117
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000118#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +0000119/*
120 * Output I/O callback sets
121 */
122typedef struct _xmlOutputCallback {
123 xmlOutputMatchCallback matchcallback;
124 xmlOutputOpenCallback opencallback;
125 xmlOutputWriteCallback writecallback;
126 xmlOutputCloseCallback closecallback;
127} xmlOutputCallback;
128
129#define MAX_OUTPUT_CALLBACK 15
130
Daniel Veillard22090732001-07-16 00:06:07 +0000131static xmlOutputCallback xmlOutputCallbackTable[MAX_OUTPUT_CALLBACK];
132static int xmlOutputCallbackNr = 0;
133static int xmlOutputCallbackInitialized = 0;
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000134#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +0000135
Daniel Veillard05d987b2003-10-08 11:54:57 +0000136/************************************************************************
137 * *
138 * Tree memory error handler *
139 * *
140 ************************************************************************/
Daniel Veillardf4862f02002-09-10 11:13:43 +0000141
Daniel Veillard05d987b2003-10-08 11:54:57 +0000142static const char *IOerr[] = {
Daniel Veillarda8856222003-10-08 19:26:03 +0000143 "Unknown IO error", /* UNKNOWN */
Daniel Veillard05d987b2003-10-08 11:54:57 +0000144 "Permission denied", /* EACCES */
145 "Resource temporarily unavailable",/* EAGAIN */
146 "Bad file descriptor", /* EBADF */
147 "Bad message", /* EBADMSG */
148 "Resource busy", /* EBUSY */
149 "Operation canceled", /* ECANCELED */
150 "No child processes", /* ECHILD */
151 "Resource deadlock avoided",/* EDEADLK */
152 "Domain error", /* EDOM */
153 "File exists", /* EEXIST */
154 "Bad address", /* EFAULT */
155 "File too large", /* EFBIG */
156 "Operation in progress", /* EINPROGRESS */
157 "Interrupted function call",/* EINTR */
158 "Invalid argument", /* EINVAL */
159 "Input/output error", /* EIO */
160 "Is a directory", /* EISDIR */
161 "Too many open files", /* EMFILE */
162 "Too many links", /* EMLINK */
163 "Inappropriate message buffer length",/* EMSGSIZE */
164 "Filename too long", /* ENAMETOOLONG */
165 "Too many open files in system",/* ENFILE */
166 "No such device", /* ENODEV */
167 "No such file or directory",/* ENOENT */
168 "Exec format error", /* ENOEXEC */
169 "No locks available", /* ENOLCK */
170 "Not enough space", /* ENOMEM */
171 "No space left on device", /* ENOSPC */
172 "Function not implemented", /* ENOSYS */
173 "Not a directory", /* ENOTDIR */
174 "Directory not empty", /* ENOTEMPTY */
175 "Not supported", /* ENOTSUP */
176 "Inappropriate I/O control operation",/* ENOTTY */
177 "No such device or address",/* ENXIO */
178 "Operation not permitted", /* EPERM */
179 "Broken pipe", /* EPIPE */
180 "Result too large", /* ERANGE */
181 "Read-only file system", /* EROFS */
182 "Invalid seek", /* ESPIPE */
183 "No such process", /* ESRCH */
184 "Operation timed out", /* ETIMEDOUT */
185 "Improper link", /* EXDEV */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000186 "Attempt to load network entity %s", /* XML_IO_NETWORK_ATTEMPT */
Daniel Veillard05d987b2003-10-08 11:54:57 +0000187 "encoder error", /* XML_IO_ENCODER */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000188 "flush error",
189 "write error",
190 "no input",
191 "buffer full",
192 "loading error",
193 "not a socket", /* ENOTSOCK */
194 "already connected", /* EISCONN */
Daniel Veillard29b17482004-08-16 00:39:03 +0000195 "connection refused", /* ECONNREFUSED */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000196 "unreachable network", /* ENETUNREACH */
197 "adddress in use", /* EADDRINUSE */
198 "already in use", /* EALREADY */
199 "unknown address familly", /* EAFNOSUPPORT */
Daniel Veillard05d987b2003-10-08 11:54:57 +0000200};
201
Daniel Veillard8ca85b22006-09-01 09:56:07 +0000202#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
Daniel Veillardf7416012006-04-27 08:15:20 +0000203/**
204 * __xmlIOWin32UTF8ToWChar:
205 * @u8String: uft-8 string
206 *
207 * Convert a string from utf-8 to wchar (WINDOWS ONLY!)
208 */
209static wchar_t *
210__xmlIOWin32UTF8ToWChar(const char *u8String)
211{
212 wchar_t *wString = NULL;
213
214 if (u8String)
215 {
Daniel Veillarde5a3f372006-08-30 13:11:36 +0000216 int wLen = MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,u8String,-1,NULL,0);
Daniel Veillardf7416012006-04-27 08:15:20 +0000217 if (wLen)
218 {
Daniel Veillard8ca85b22006-09-01 09:56:07 +0000219 wString = xmlMalloc(wLen * sizeof(wchar_t));
Daniel Veillardf7416012006-04-27 08:15:20 +0000220 if (wString)
221 {
Daniel Veillard8ca85b22006-09-01 09:56:07 +0000222 if (MultiByteToWideChar(CP_UTF8,0,u8String,-1,wString,wLen) == 0)
Daniel Veillardf7416012006-04-27 08:15:20 +0000223 {
Daniel Veillard8ca85b22006-09-01 09:56:07 +0000224 xmlFree(wString);
Daniel Veillardf7416012006-04-27 08:15:20 +0000225 wString = NULL;
226 }
227 }
228 }
229 }
230
231 return wString;
232}
233#endif
234
Daniel Veillard05d987b2003-10-08 11:54:57 +0000235/**
236 * xmlIOErrMemory:
237 * @extra: extra informations
238 *
239 * Handle an out of memory condition
240 */
241static void
242xmlIOErrMemory(const char *extra)
243{
244 __xmlSimpleError(XML_FROM_IO, XML_ERR_NO_MEMORY, NULL, NULL, extra);
245}
246
247/**
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000248 * __xmlIOErr:
Daniel Veillard05d987b2003-10-08 11:54:57 +0000249 * @code: the error number
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000250 * @
Daniel Veillard05d987b2003-10-08 11:54:57 +0000251 * @extra: extra informations
252 *
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000253 * Handle an I/O error
Daniel Veillard05d987b2003-10-08 11:54:57 +0000254 */
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000255void
256__xmlIOErr(int domain, int code, const char *extra)
Daniel Veillard05d987b2003-10-08 11:54:57 +0000257{
258 unsigned int idx;
259
260 if (code == 0) {
261#ifdef HAVE_ERRNO_H
262 if (errno == 0) code = 0;
263#ifdef EACCES
264 else if (errno == EACCES) code = XML_IO_EACCES;
265#endif
266#ifdef EAGAIN
267 else if (errno == EAGAIN) code = XML_IO_EAGAIN;
268#endif
269#ifdef EBADF
270 else if (errno == EBADF) code = XML_IO_EBADF;
271#endif
272#ifdef EBADMSG
273 else if (errno == EBADMSG) code = XML_IO_EBADMSG;
274#endif
275#ifdef EBUSY
276 else if (errno == EBUSY) code = XML_IO_EBUSY;
277#endif
278#ifdef ECANCELED
279 else if (errno == ECANCELED) code = XML_IO_ECANCELED;
280#endif
281#ifdef ECHILD
282 else if (errno == ECHILD) code = XML_IO_ECHILD;
283#endif
284#ifdef EDEADLK
285 else if (errno == EDEADLK) code = XML_IO_EDEADLK;
286#endif
287#ifdef EDOM
288 else if (errno == EDOM) code = XML_IO_EDOM;
289#endif
290#ifdef EEXIST
291 else if (errno == EEXIST) code = XML_IO_EEXIST;
292#endif
293#ifdef EFAULT
294 else if (errno == EFAULT) code = XML_IO_EFAULT;
295#endif
296#ifdef EFBIG
297 else if (errno == EFBIG) code = XML_IO_EFBIG;
298#endif
299#ifdef EINPROGRESS
300 else if (errno == EINPROGRESS) code = XML_IO_EINPROGRESS;
301#endif
302#ifdef EINTR
303 else if (errno == EINTR) code = XML_IO_EINTR;
304#endif
305#ifdef EINVAL
306 else if (errno == EINVAL) code = XML_IO_EINVAL;
307#endif
308#ifdef EIO
309 else if (errno == EIO) code = XML_IO_EIO;
310#endif
311#ifdef EISDIR
312 else if (errno == EISDIR) code = XML_IO_EISDIR;
313#endif
314#ifdef EMFILE
315 else if (errno == EMFILE) code = XML_IO_EMFILE;
316#endif
317#ifdef EMLINK
318 else if (errno == EMLINK) code = XML_IO_EMLINK;
319#endif
320#ifdef EMSGSIZE
321 else if (errno == EMSGSIZE) code = XML_IO_EMSGSIZE;
322#endif
323#ifdef ENAMETOOLONG
324 else if (errno == ENAMETOOLONG) code = XML_IO_ENAMETOOLONG;
325#endif
326#ifdef ENFILE
327 else if (errno == ENFILE) code = XML_IO_ENFILE;
328#endif
329#ifdef ENODEV
330 else if (errno == ENODEV) code = XML_IO_ENODEV;
331#endif
332#ifdef ENOENT
333 else if (errno == ENOENT) code = XML_IO_ENOENT;
334#endif
335#ifdef ENOEXEC
336 else if (errno == ENOEXEC) code = XML_IO_ENOEXEC;
337#endif
338#ifdef ENOLCK
339 else if (errno == ENOLCK) code = XML_IO_ENOLCK;
340#endif
341#ifdef ENOMEM
342 else if (errno == ENOMEM) code = XML_IO_ENOMEM;
343#endif
344#ifdef ENOSPC
345 else if (errno == ENOSPC) code = XML_IO_ENOSPC;
346#endif
347#ifdef ENOSYS
348 else if (errno == ENOSYS) code = XML_IO_ENOSYS;
349#endif
350#ifdef ENOTDIR
351 else if (errno == ENOTDIR) code = XML_IO_ENOTDIR;
352#endif
353#ifdef ENOTEMPTY
354 else if (errno == ENOTEMPTY) code = XML_IO_ENOTEMPTY;
355#endif
356#ifdef ENOTSUP
357 else if (errno == ENOTSUP) code = XML_IO_ENOTSUP;
358#endif
359#ifdef ENOTTY
360 else if (errno == ENOTTY) code = XML_IO_ENOTTY;
361#endif
362#ifdef ENXIO
363 else if (errno == ENXIO) code = XML_IO_ENXIO;
364#endif
365#ifdef EPERM
366 else if (errno == EPERM) code = XML_IO_EPERM;
367#endif
368#ifdef EPIPE
369 else if (errno == EPIPE) code = XML_IO_EPIPE;
370#endif
371#ifdef ERANGE
372 else if (errno == ERANGE) code = XML_IO_ERANGE;
373#endif
374#ifdef EROFS
375 else if (errno == EROFS) code = XML_IO_EROFS;
376#endif
377#ifdef ESPIPE
378 else if (errno == ESPIPE) code = XML_IO_ESPIPE;
379#endif
380#ifdef ESRCH
381 else if (errno == ESRCH) code = XML_IO_ESRCH;
382#endif
383#ifdef ETIMEDOUT
384 else if (errno == ETIMEDOUT) code = XML_IO_ETIMEDOUT;
385#endif
386#ifdef EXDEV
387 else if (errno == EXDEV) code = XML_IO_EXDEV;
388#endif
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000389#ifdef ENOTSOCK
390 else if (errno == ENOTSOCK) code = XML_IO_ENOTSOCK;
391#endif
392#ifdef EISCONN
393 else if (errno == EISCONN) code = XML_IO_EISCONN;
394#endif
395#ifdef ECONNREFUSED
396 else if (errno == ECONNREFUSED) code = XML_IO_ECONNREFUSED;
397#endif
398#ifdef ETIMEDOUT
399 else if (errno == ETIMEDOUT) code = XML_IO_ETIMEDOUT;
400#endif
401#ifdef ENETUNREACH
402 else if (errno == ENETUNREACH) code = XML_IO_ENETUNREACH;
403#endif
404#ifdef EADDRINUSE
405 else if (errno == EADDRINUSE) code = XML_IO_EADDRINUSE;
406#endif
407#ifdef EINPROGRESS
408 else if (errno == EINPROGRESS) code = XML_IO_EINPROGRESS;
409#endif
410#ifdef EALREADY
411 else if (errno == EALREADY) code = XML_IO_EALREADY;
412#endif
413#ifdef EAFNOSUPPORT
414 else if (errno == EAFNOSUPPORT) code = XML_IO_EAFNOSUPPORT;
415#endif
Daniel Veillard05d987b2003-10-08 11:54:57 +0000416 else code = XML_IO_UNKNOWN;
417#endif /* HAVE_ERRNO_H */
418 }
419 idx = 0;
420 if (code >= XML_IO_UNKNOWN) idx = code - XML_IO_UNKNOWN;
421 if (idx >= (sizeof(IOerr) / sizeof(IOerr[0]))) idx = 0;
422
Daniel Veillard2b0f8792003-10-10 19:36:36 +0000423 __xmlSimpleError(domain, code, NULL, IOerr[idx], extra);
424}
425
426/**
427 * xmlIOErr:
428 * @code: the error number
429 * @extra: extra informations
430 *
431 * Handle an I/O error
432 */
433static void
434xmlIOErr(int code, const char *extra)
435{
436 __xmlIOErr(XML_FROM_IO, code, extra);
Daniel Veillard05d987b2003-10-08 11:54:57 +0000437}
438
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000439/**
Daniel Veillarde8039df2003-10-27 11:25:13 +0000440 * __xmlLoaderErr:
441 * @ctx: the parser context
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000442 * @extra: extra informations
443 *
444 * Handle a resource access error
445 */
Daniel Veillarde8039df2003-10-27 11:25:13 +0000446void
447__xmlLoaderErr(void *ctx, const char *msg, const char *filename)
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000448{
Daniel Veillarde8039df2003-10-27 11:25:13 +0000449 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000450 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000451 xmlGenericErrorFunc channel = NULL;
452 void *data = NULL;
453 xmlErrorLevel level = XML_ERR_ERROR;
454
Daniel Veillard157fee02003-10-31 10:36:03 +0000455 if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
456 (ctxt->instate == XML_PARSER_EOF))
457 return;
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000458 if ((ctxt != NULL) && (ctxt->sax != NULL)) {
459 if (ctxt->validate) {
460 channel = ctxt->sax->error;
461 level = XML_ERR_ERROR;
462 } else {
463 channel = ctxt->sax->warning;
464 level = XML_ERR_WARNING;
465 }
Daniel Veillard5ea30d72004-11-08 11:54:28 +0000466 if (ctxt->sax->initialized == XML_SAX2_MAGIC)
467 schannel = ctxt->sax->serror;
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000468 data = ctxt->userData;
469 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000470 __xmlRaiseError(schannel, channel, data, ctxt, NULL, XML_FROM_IO,
Daniel Veillardcd6ff282003-10-08 22:38:13 +0000471 XML_IO_LOAD_ERROR, level, NULL, 0,
472 filename, NULL, NULL, 0, 0,
473 msg, filename);
474
475}
476
Daniel Veillard05d987b2003-10-08 11:54:57 +0000477/************************************************************************
478 * *
479 * Tree memory error handler *
480 * *
481 ************************************************************************/
Daniel Veillardf4862f02002-09-10 11:13:43 +0000482/**
Daniel Veillard01c13b52002-12-10 15:19:08 +0000483 * xmlNormalizeWindowsPath:
Daniel Veillard33300b42003-04-17 09:09:19 +0000484 * @path: the input file path
Daniel Veillardf4862f02002-09-10 11:13:43 +0000485 *
Igor Zlatkovic5f9fada2003-02-19 14:51:00 +0000486 * This function is obsolete. Please see xmlURIFromPath in uri.c for
487 * a better solution.
Daniel Veillard33300b42003-04-17 09:09:19 +0000488 *
489 * Returns a canonicalized version of the path
Daniel Veillardf4862f02002-09-10 11:13:43 +0000490 */
491xmlChar *
492xmlNormalizeWindowsPath(const xmlChar *path)
493{
Igor Zlatkovic5f9fada2003-02-19 14:51:00 +0000494 return xmlCanonicPath(path);
Daniel Veillardf4862f02002-09-10 11:13:43 +0000495}
496
Daniel Veillardacf7ff02001-10-29 20:21:47 +0000497/**
498 * xmlCleanupInputCallbacks:
499 *
500 * clears the entire input callback table. this includes the
501 * compiled-in I/O.
502 */
503void
504xmlCleanupInputCallbacks(void)
505{
506 int i;
507
508 if (!xmlInputCallbackInitialized)
509 return;
510
Daniel Veillard107ccaa2001-11-27 16:23:50 +0000511 for (i = xmlInputCallbackNr - 1; i >= 0; i--) {
Daniel Veillardacf7ff02001-10-29 20:21:47 +0000512 xmlInputCallbackTable[i].matchcallback = NULL;
513 xmlInputCallbackTable[i].opencallback = NULL;
514 xmlInputCallbackTable[i].readcallback = NULL;
515 xmlInputCallbackTable[i].closecallback = NULL;
516 }
517
518 xmlInputCallbackNr = 0;
Aleksey Sanin9c45ba82002-06-06 21:46:13 +0000519 xmlInputCallbackInitialized = 0;
Daniel Veillardacf7ff02001-10-29 20:21:47 +0000520}
521
Daniel Veillardaecc0dc2004-05-08 02:32:07 +0000522/**
William M. Brack4e3a9fa2004-08-03 22:41:11 +0000523 * xmlPopInputCallbacks:
Daniel Veillardaecc0dc2004-05-08 02:32:07 +0000524 *
525 * Clear the top input callback from the input stack. this includes the
526 * compiled-in I/O.
527 *
528 * Returns the number of input callback registered or -1 in case of error.
529 */
530int
531xmlPopInputCallbacks(void)
532{
533 if (!xmlInputCallbackInitialized)
534 return(-1);
535
536 if (xmlInputCallbackNr <= 0)
537 return(-1);
538
539 xmlInputCallbackNr--;
540 xmlInputCallbackTable[xmlInputCallbackNr].matchcallback = NULL;
541 xmlInputCallbackTable[xmlInputCallbackNr].opencallback = NULL;
542 xmlInputCallbackTable[xmlInputCallbackNr].readcallback = NULL;
543 xmlInputCallbackTable[xmlInputCallbackNr].closecallback = NULL;
544
545 return(xmlInputCallbackNr);
546}
547
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000548#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardacf7ff02001-10-29 20:21:47 +0000549/**
550 * xmlCleanupOutputCallbacks:
551 *
552 * clears the entire output callback table. this includes the
553 * compiled-in I/O callbacks.
554 */
555void
556xmlCleanupOutputCallbacks(void)
557{
558 int i;
559
560 if (!xmlOutputCallbackInitialized)
561 return;
562
Daniel Veillard107ccaa2001-11-27 16:23:50 +0000563 for (i = xmlOutputCallbackNr - 1; i >= 0; i--) {
Daniel Veillardacf7ff02001-10-29 20:21:47 +0000564 xmlOutputCallbackTable[i].matchcallback = NULL;
565 xmlOutputCallbackTable[i].opencallback = NULL;
566 xmlOutputCallbackTable[i].writecallback = NULL;
567 xmlOutputCallbackTable[i].closecallback = NULL;
568 }
569
570 xmlOutputCallbackNr = 0;
Aleksey Sanin9c45ba82002-06-06 21:46:13 +0000571 xmlOutputCallbackInitialized = 0;
Daniel Veillardacf7ff02001-10-29 20:21:47 +0000572}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000573#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardacf7ff02001-10-29 20:21:47 +0000574
Owen Taylor3473f882001-02-23 17:55:21 +0000575/************************************************************************
576 * *
577 * Standard I/O for file accesses *
578 * *
579 ************************************************************************/
580
Daniel Veillard8ca85b22006-09-01 09:56:07 +0000581#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
582
583/**
584 * xmlWrapOpenUtf8:
585 * @path: the path in utf-8 encoding
586 * @mode: type of access (0 - read, 1 - write)
587 *
588 * function opens the file specified by @path
589 *
590 */
591static FILE*
592xmlWrapOpenUtf8(const char *path,int mode)
593{
594 FILE *fd = NULL;
595 wchar_t *wPath;
596
597 wPath = __xmlIOWin32UTF8ToWChar(path);
598 if(wPath)
599 {
600 fd = _wfopen(wPath, mode ? L"wb" : L"rb");
601 xmlFree(wPath);
602 }
Daniel Veillard0da41662006-10-10 09:05:36 +0000603 /* maybe path in native encoding */
Daniel Veillard8ca85b22006-09-01 09:56:07 +0000604 if(fd == NULL)
605 fd = fopen(path, mode ? "wb" : "rb");
606
607 return fd;
608}
609
610/**
611 * xmlWrapStatUtf8:
612 * @path: the path in utf-8 encoding
613 * @info: structure that stores results
614 *
615 * function obtains information about the file or directory
616 *
617 */
618static int
619xmlWrapStatUtf8(const char *path,struct stat *info)
620{
621#ifdef HAVE_STAT
622 int retval = -1;
623 wchar_t *wPath;
624
625 wPath = __xmlIOWin32UTF8ToWChar(path);
626 if (wPath)
627 {
628 retval = _wstat(wPath,info);
629 xmlFree(wPath);
630 }
Daniel Veillard0da41662006-10-10 09:05:36 +0000631 /* maybe path in native encoding */
Daniel Veillard8ca85b22006-09-01 09:56:07 +0000632 if(retval < 0)
633 retval = stat(path,info);
634 return retval;
635#else
636 return -1;
637#endif
638}
639
640/**
641 * xmlWrapOpenNative:
642 * @path: the path
643 * @mode: type of access (0 - read, 1 - write)
644 *
645 * function opens the file specified by @path
646 *
647 */
648static FILE*
649xmlWrapOpenNative(const char *path,int mode)
650{
651 return fopen(path,mode ? "wb" : "rb");
652}
653
654/**
655 * xmlWrapStatNative:
656 * @path: the path
657 * @info: structure that stores results
658 *
659 * function obtains information about the file or directory
660 *
661 */
662static int
663xmlWrapStatNative(const char *path,struct stat *info)
664{
665#ifdef HAVE_STAT
666 return stat(path,info);
667#else
668 return -1;
669#endif
670}
671
672static int (* xmlWrapStat)(const char *,struct stat *) = xmlWrapStatNative;
673static FILE* (* xmlWrapOpen)(const char *,int mode) = xmlWrapOpenNative;
674
675/**
676 * xmlInitPlatformSpecificIo:
677 *
678 * Initialize platform specific features.
679 */
680static void
681xmlInitPlatformSpecificIo
682(void) {
683 static int xmlPlatformIoInitialized = 0;
684 OSVERSIONINFO osvi;
685
686 if(xmlPlatformIoInitialized)
687 return;
688
689 osvi.dwOSVersionInfoSize = sizeof(osvi);
690
691 if(GetVersionEx(&osvi) && (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)) {
692 xmlWrapStat = xmlWrapStatUtf8;
693 xmlWrapOpen = xmlWrapOpenUtf8;
694 } else {
695 xmlWrapStat = xmlWrapStatNative;
696 xmlWrapOpen = xmlWrapOpenNative;
697 }
698
699 xmlPlatformIoInitialized = 1;
700 return;
701}
702
703#endif
704
Owen Taylor3473f882001-02-23 17:55:21 +0000705/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +0000706 * xmlCheckFilename:
Owen Taylor3473f882001-02-23 17:55:21 +0000707 * @path: the path to check
708 *
709 * function checks to see if @path is a valid source
710 * (file, socket...) for XML.
711 *
712 * if stat is not available on the target machine,
713 * returns 1. if stat fails, returns 0 (if calling
714 * stat on the filename fails, it can't be right).
715 * if stat succeeds and the file is a directory,
Daniel Veillard819d5cb2002-10-14 11:15:18 +0000716 * returns 2. otherwise returns 1.
Owen Taylor3473f882001-02-23 17:55:21 +0000717 */
718
Daniel Veillard819d5cb2002-10-14 11:15:18 +0000719int
Owen Taylor3473f882001-02-23 17:55:21 +0000720xmlCheckFilename (const char *path)
721{
Daniel Veillard8ca85b22006-09-01 09:56:07 +0000722#ifdef HAVE_STAT
Daniel Veillard0b309952006-05-02 20:34:38 +0000723 struct stat stat_buffer;
724#endif
Daniel Veillardf7416012006-04-27 08:15:20 +0000725 if (path == NULL)
Daniel Veillard0b309952006-05-02 20:34:38 +0000726 return(0);
Daniel Veillarde5a3f372006-08-30 13:11:36 +0000727
Owen Taylor3473f882001-02-23 17:55:21 +0000728#ifdef HAVE_STAT
Daniel Veillard8ca85b22006-09-01 09:56:07 +0000729#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
730 if (xmlWrapStat(path, &stat_buffer) == -1)
731 return 0;
732#else
Owen Taylor3473f882001-02-23 17:55:21 +0000733 if (stat(path, &stat_buffer) == -1)
734 return 0;
Daniel Veillard8ca85b22006-09-01 09:56:07 +0000735#endif
Daniel Veillard819d5cb2002-10-14 11:15:18 +0000736#ifdef S_ISDIR
Daniel Veillardf7416012006-04-27 08:15:20 +0000737 if (S_ISDIR(stat_buffer.st_mode))
Daniel Veillard819d5cb2002-10-14 11:15:18 +0000738 return 2;
Daniel Veillard8ca85b22006-09-01 09:56:07 +0000739#endif
Daniel Veillardf7416012006-04-27 08:15:20 +0000740#endif /* HAVE_STAT */
Owen Taylor3473f882001-02-23 17:55:21 +0000741 return 1;
742}
743
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000744static int
Owen Taylor3473f882001-02-23 17:55:21 +0000745xmlNop(void) {
746 return(0);
747}
748
749/**
Owen Taylor3473f882001-02-23 17:55:21 +0000750 * xmlFdRead:
751 * @context: the I/O context
752 * @buffer: where to drop data
753 * @len: number of bytes to read
754 *
755 * Read @len bytes to @buffer from the I/O channel.
756 *
757 * Returns the number of bytes written
758 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000759static int
Owen Taylor3473f882001-02-23 17:55:21 +0000760xmlFdRead (void * context, char * buffer, int len) {
Daniel Veillard05d987b2003-10-08 11:54:57 +0000761 int ret;
762
763 ret = read((int) (long) context, &buffer[0], len);
764 if (ret < 0) xmlIOErr(0, "read()");
765 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +0000766}
767
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000768#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +0000769/**
770 * xmlFdWrite:
771 * @context: the I/O context
772 * @buffer: where to get data
773 * @len: number of bytes to write
774 *
775 * Write @len bytes from @buffer to the I/O channel.
776 *
777 * Returns the number of bytes written
778 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000779static int
Owen Taylor3473f882001-02-23 17:55:21 +0000780xmlFdWrite (void * context, const char * buffer, int len) {
Daniel Veillard9b693b42005-10-28 14:54:17 +0000781 int ret = 0;
Daniel Veillard05d987b2003-10-08 11:54:57 +0000782
Daniel Veillard9b693b42005-10-28 14:54:17 +0000783 if (len > 0) {
784 ret = write((int) (long) context, &buffer[0], len);
785 if (ret < 0) xmlIOErr(0, "write()");
786 }
Daniel Veillard05d987b2003-10-08 11:54:57 +0000787 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +0000788}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000789#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +0000790
791/**
792 * xmlFdClose:
793 * @context: the I/O context
794 *
795 * Close an I/O channel
Daniel Veillardf012a642001-07-23 19:10:52 +0000796 *
797 * Returns 0 in case of success and error code otherwise
Owen Taylor3473f882001-02-23 17:55:21 +0000798 */
Daniel Veillardf012a642001-07-23 19:10:52 +0000799static int
Owen Taylor3473f882001-02-23 17:55:21 +0000800xmlFdClose (void * context) {
Daniel Veillard05d987b2003-10-08 11:54:57 +0000801 int ret;
802 ret = close((int) (long) context);
803 if (ret < 0) xmlIOErr(0, "close()");
804 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +0000805}
806
807/**
808 * xmlFileMatch:
809 * @filename: the URI for matching
810 *
811 * input from FILE *
812 *
813 * Returns 1 if matches, 0 otherwise
814 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +0000815int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +0000816xmlFileMatch (const char *filename ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +0000817 return(1);
818}
819
820/**
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000821 * xmlFileOpen_real:
Owen Taylor3473f882001-02-23 17:55:21 +0000822 * @filename: the URI for matching
823 *
824 * input from FILE *, supports compressed input
825 * if @filename is " " then the standard input is used
826 *
827 * Returns an I/O context or NULL in case of error
828 */
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000829static void *
830xmlFileOpen_real (const char *filename) {
Owen Taylor3473f882001-02-23 17:55:21 +0000831 const char *path = NULL;
Daniel Veillard8ca85b22006-09-01 09:56:07 +0000832 FILE *fd;
Owen Taylor3473f882001-02-23 17:55:21 +0000833
Daniel Veillardb1b3a3e2004-11-03 23:25:47 +0000834 if (filename == NULL)
835 return(NULL);
836
Owen Taylor3473f882001-02-23 17:55:21 +0000837 if (!strcmp(filename, "-")) {
838 fd = stdin;
839 return((void *) fd);
840 }
841
Daniel Veillardf4862f02002-09-10 11:13:43 +0000842 if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17))
Daniel Veillard1997c3e2003-07-05 20:43:43 +0000843#if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
Daniel Veillardb212bbb2002-08-25 14:39:16 +0000844 path = &filename[17];
845#else
Owen Taylor3473f882001-02-23 17:55:21 +0000846 path = &filename[16];
Daniel Veillardb212bbb2002-08-25 14:39:16 +0000847#endif
Aleksey Sanin5aac8b82002-05-01 18:32:28 +0000848 else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) {
Daniel Veillard1997c3e2003-07-05 20:43:43 +0000849#if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
Daniel Veillardfe703322001-08-14 12:18:09 +0000850 path = &filename[8];
851#else
Daniel Veillard3c2758d2001-05-31 18:43:43 +0000852 path = &filename[7];
Daniel Veillardfe703322001-08-14 12:18:09 +0000853#endif
854 } else
Owen Taylor3473f882001-02-23 17:55:21 +0000855 path = filename;
856
857 if (path == NULL)
858 return(NULL);
859 if (!xmlCheckFilename(path))
860 return(NULL);
861
Daniel Veillard8ca85b22006-09-01 09:56:07 +0000862#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
863 fd = xmlWrapOpen(path, 0);
864#else
865 fd = fopen(path, "r");
Owen Taylor3473f882001-02-23 17:55:21 +0000866#endif /* WIN32 */
Daniel Veillard8ca85b22006-09-01 09:56:07 +0000867 if (fd == NULL) xmlIOErr(0, path);
Owen Taylor3473f882001-02-23 17:55:21 +0000868 return((void *) fd);
869}
870
871/**
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000872 * xmlFileOpen:
873 * @filename: the URI for matching
874 *
875 * Wrapper around xmlFileOpen_real that try it with an unescaped
876 * version of @filename, if this fails fallback to @filename
Daniel Veillard71531f32003-02-05 13:19:53 +0000877 *
878 * Returns a handler or NULL in case or failure
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000879 */
880void *
881xmlFileOpen (const char *filename) {
882 char *unescaped;
883 void *retval;
Daniel Veillardb1b3a3e2004-11-03 23:25:47 +0000884
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000885 unescaped = xmlURIUnescapeString(filename, 0, NULL);
886 if (unescaped != NULL) {
887 retval = xmlFileOpen_real(unescaped);
Daniel Veillardb1b3a3e2004-11-03 23:25:47 +0000888 xmlFree(unescaped);
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000889 } else {
890 retval = xmlFileOpen_real(filename);
891 }
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000892 return retval;
893}
894
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000895#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardcacbe5d2003-01-10 16:09:51 +0000896/**
Owen Taylor3473f882001-02-23 17:55:21 +0000897 * xmlFileOpenW:
898 * @filename: the URI for matching
899 *
900 * output to from FILE *,
901 * if @filename is "-" then the standard output is used
902 *
903 * Returns an I/O context or NULL in case of error
904 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000905static void *
Owen Taylor3473f882001-02-23 17:55:21 +0000906xmlFileOpenW (const char *filename) {
907 const char *path = NULL;
Daniel Veillard8ca85b22006-09-01 09:56:07 +0000908 FILE *fd;
Owen Taylor3473f882001-02-23 17:55:21 +0000909
910 if (!strcmp(filename, "-")) {
911 fd = stdout;
912 return((void *) fd);
913 }
914
Daniel Veillardf4862f02002-09-10 11:13:43 +0000915 if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17))
Daniel Veillard1997c3e2003-07-05 20:43:43 +0000916#if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
Daniel Veillardf4862f02002-09-10 11:13:43 +0000917 path = &filename[17];
918#else
Owen Taylor3473f882001-02-23 17:55:21 +0000919 path = &filename[16];
Daniel Veillardf4862f02002-09-10 11:13:43 +0000920#endif
Aleksey Sanin5aac8b82002-05-01 18:32:28 +0000921 else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) {
Daniel Veillard1997c3e2003-07-05 20:43:43 +0000922#if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
Daniel Veillardfe703322001-08-14 12:18:09 +0000923 path = &filename[8];
924#else
Daniel Veillard3c2758d2001-05-31 18:43:43 +0000925 path = &filename[7];
Daniel Veillardfe703322001-08-14 12:18:09 +0000926#endif
927 } else
Owen Taylor3473f882001-02-23 17:55:21 +0000928 path = filename;
929
930 if (path == NULL)
931 return(NULL);
932
Daniel Veillard8ca85b22006-09-01 09:56:07 +0000933#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
934 fd = xmlWrapOpen(path, 1);
935#else
Daniel Veillarde5a3f372006-08-30 13:11:36 +0000936 fd = fopen(path, "wb");
Daniel Veillard8ca85b22006-09-01 09:56:07 +0000937#endif /* WIN32 */
Daniel Veillarde5a3f372006-08-30 13:11:36 +0000938
Daniel Veillardf7416012006-04-27 08:15:20 +0000939 if (fd == NULL) xmlIOErr(0, path);
Owen Taylor3473f882001-02-23 17:55:21 +0000940 return((void *) fd);
941}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000942#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +0000943
944/**
945 * xmlFileRead:
946 * @context: the I/O context
947 * @buffer: where to drop data
948 * @len: number of bytes to write
949 *
950 * Read @len bytes to @buffer from the I/O channel.
951 *
Daniel Veillardce682bc2004-11-05 17:22:25 +0000952 * Returns the number of bytes written or < 0 in case of failure
Owen Taylor3473f882001-02-23 17:55:21 +0000953 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +0000954int
Owen Taylor3473f882001-02-23 17:55:21 +0000955xmlFileRead (void * context, char * buffer, int len) {
Daniel Veillard05d987b2003-10-08 11:54:57 +0000956 int ret;
Daniel Veillardce682bc2004-11-05 17:22:25 +0000957 if ((context == NULL) || (buffer == NULL))
958 return(-1);
Daniel Veillard05d987b2003-10-08 11:54:57 +0000959 ret = fread(&buffer[0], 1, len, (FILE *) context);
960 if (ret < 0) xmlIOErr(0, "fread()");
961 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +0000962}
963
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000964#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +0000965/**
966 * xmlFileWrite:
967 * @context: the I/O context
968 * @buffer: where to drop data
969 * @len: number of bytes to write
970 *
971 * Write @len bytes from @buffer to the I/O channel.
972 *
973 * Returns the number of bytes written
974 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000975static int
Owen Taylor3473f882001-02-23 17:55:21 +0000976xmlFileWrite (void * context, const char * buffer, int len) {
Daniel Veillard4a6d39b2002-12-17 18:33:01 +0000977 int items;
978
Daniel Veillardce682bc2004-11-05 17:22:25 +0000979 if ((context == NULL) || (buffer == NULL))
980 return(-1);
Daniel Veillard4a6d39b2002-12-17 18:33:01 +0000981 items = fwrite(&buffer[0], len, 1, (FILE *) context);
Daniel Veillard97bf4d02003-10-08 18:58:28 +0000982 if ((items == 0) && (ferror((FILE *) context))) {
Daniel Veillard05d987b2003-10-08 11:54:57 +0000983 xmlIOErr(0, "fwrite()");
Daniel Veillard97bf4d02003-10-08 18:58:28 +0000984 return(-1);
985 }
Daniel Veillard4a6d39b2002-12-17 18:33:01 +0000986 return(items * len);
Owen Taylor3473f882001-02-23 17:55:21 +0000987}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +0000988#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +0000989
990/**
991 * xmlFileClose:
992 * @context: the I/O context
993 *
994 * Close an I/O channel
Daniel Veillarda9b66d02002-12-11 14:23:49 +0000995 *
996 * Returns 0 or -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +0000997 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +0000998int
Owen Taylor3473f882001-02-23 17:55:21 +0000999xmlFileClose (void * context) {
Daniel Veillarda9e65e82001-10-30 10:32:36 +00001000 FILE *fil;
Daniel Veillard05d987b2003-10-08 11:54:57 +00001001 int ret;
Daniel Veillarda9e65e82001-10-30 10:32:36 +00001002
Daniel Veillardb1b3a3e2004-11-03 23:25:47 +00001003 if (context == NULL)
1004 return(-1);
Daniel Veillarda9e65e82001-10-30 10:32:36 +00001005 fil = (FILE *) context;
Daniel Veillard500a1de2004-03-22 15:22:58 +00001006 if ((fil == stdout) || (fil == stderr)) {
1007 ret = fflush(fil);
1008 if (ret < 0)
1009 xmlIOErr(0, "fflush()");
1010 return(0);
1011 }
Daniel Veillarda9e65e82001-10-30 10:32:36 +00001012 if (fil == stdin)
1013 return(0);
Daniel Veillard05d987b2003-10-08 11:54:57 +00001014 ret = ( fclose((FILE *) context) == EOF ) ? -1 : 0;
1015 if (ret < 0)
1016 xmlIOErr(0, "fclose()");
1017 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001018}
1019
1020/**
1021 * xmlFileFlush:
1022 * @context: the I/O context
1023 *
1024 * Flush an I/O channel
1025 */
Daniel Veillardf012a642001-07-23 19:10:52 +00001026static int
Owen Taylor3473f882001-02-23 17:55:21 +00001027xmlFileFlush (void * context) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00001028 int ret;
Daniel Veillardb1b3a3e2004-11-03 23:25:47 +00001029
1030 if (context == NULL)
1031 return(-1);
Daniel Veillard05d987b2003-10-08 11:54:57 +00001032 ret = ( fflush((FILE *) context) == EOF ) ? -1 : 0;
1033 if (ret < 0)
1034 xmlIOErr(0, "fflush()");
1035 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001036}
1037
Daniel Veillard9a00fd22005-11-09 08:56:26 +00001038#ifdef LIBXML_OUTPUT_ENABLED
1039/**
1040 * xmlBufferWrite:
1041 * @context: the xmlBuffer
1042 * @buffer: the data to write
1043 * @len: number of bytes to write
1044 *
1045 * Write @len bytes from @buffer to the xml buffer
1046 *
1047 * Returns the number of bytes written
1048 */
1049static int
1050xmlBufferWrite (void * context, const char * buffer, int len) {
1051 int ret;
1052
1053 ret = xmlBufferAdd((xmlBufferPtr) context, (const xmlChar *) buffer, len);
1054 if (ret != 0)
1055 return(-1);
1056 return(len);
1057}
Daniel Veillard9a00fd22005-11-09 08:56:26 +00001058#endif
1059
Owen Taylor3473f882001-02-23 17:55:21 +00001060#ifdef HAVE_ZLIB_H
1061/************************************************************************
1062 * *
1063 * I/O for compressed file accesses *
1064 * *
1065 ************************************************************************/
1066/**
1067 * xmlGzfileMatch:
1068 * @filename: the URI for matching
1069 *
1070 * input from compressed file test
1071 *
1072 * Returns 1 if matches, 0 otherwise
1073 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001074static int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +00001075xmlGzfileMatch (const char *filename ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00001076 return(1);
1077}
1078
1079/**
Daniel Veillardcacbe5d2003-01-10 16:09:51 +00001080 * xmlGzfileOpen_real:
Owen Taylor3473f882001-02-23 17:55:21 +00001081 * @filename: the URI for matching
1082 *
1083 * input from compressed file open
1084 * if @filename is " " then the standard input is used
1085 *
1086 * Returns an I/O context or NULL in case of error
1087 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001088static void *
Daniel Veillardcacbe5d2003-01-10 16:09:51 +00001089xmlGzfileOpen_real (const char *filename) {
Owen Taylor3473f882001-02-23 17:55:21 +00001090 const char *path = NULL;
1091 gzFile fd;
1092
1093 if (!strcmp(filename, "-")) {
Daniel Veillarda9e65e82001-10-30 10:32:36 +00001094 fd = gzdopen(dup(0), "rb");
Owen Taylor3473f882001-02-23 17:55:21 +00001095 return((void *) fd);
1096 }
1097
Daniel Veillardf4862f02002-09-10 11:13:43 +00001098 if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17))
Daniel Veillard1997c3e2003-07-05 20:43:43 +00001099#if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
Daniel Veillardf4862f02002-09-10 11:13:43 +00001100 path = &filename[17];
1101#else
Owen Taylor3473f882001-02-23 17:55:21 +00001102 path = &filename[16];
Daniel Veillardf4862f02002-09-10 11:13:43 +00001103#endif
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001104 else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) {
Daniel Veillard1997c3e2003-07-05 20:43:43 +00001105#if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
Daniel Veillardfe703322001-08-14 12:18:09 +00001106 path = &filename[8];
1107#else
Owen Taylor3473f882001-02-23 17:55:21 +00001108 path = &filename[7];
Daniel Veillardfe703322001-08-14 12:18:09 +00001109#endif
1110 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001111 path = filename;
1112
1113 if (path == NULL)
1114 return(NULL);
1115 if (!xmlCheckFilename(path))
1116 return(NULL);
1117
1118 fd = gzopen(path, "rb");
1119 return((void *) fd);
1120}
1121
1122/**
Daniel Veillardcacbe5d2003-01-10 16:09:51 +00001123 * xmlGzfileOpen:
1124 * @filename: the URI for matching
1125 *
Daniel Veillard4e9b1bc2003-06-09 10:30:33 +00001126 * Wrapper around xmlGzfileOpen if the open fais, it will
1127 * try to unescape @filename
Daniel Veillardcacbe5d2003-01-10 16:09:51 +00001128 */
1129static void *
1130xmlGzfileOpen (const char *filename) {
1131 char *unescaped;
1132 void *retval;
Daniel Veillard4e9b1bc2003-06-09 10:30:33 +00001133
1134 retval = xmlGzfileOpen_real(filename);
1135 if (retval == NULL) {
1136 unescaped = xmlURIUnescapeString(filename, 0, NULL);
1137 if (unescaped != NULL) {
1138 retval = xmlGzfileOpen_real(unescaped);
1139 }
1140 xmlFree(unescaped);
Daniel Veillardcacbe5d2003-01-10 16:09:51 +00001141 }
Daniel Veillardcacbe5d2003-01-10 16:09:51 +00001142 return retval;
1143}
1144
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001145#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardcacbe5d2003-01-10 16:09:51 +00001146/**
Owen Taylor3473f882001-02-23 17:55:21 +00001147 * xmlGzfileOpenW:
1148 * @filename: the URI for matching
1149 * @compression: the compression factor (0 - 9 included)
1150 *
1151 * input from compressed file open
1152 * if @filename is " " then the standard input is used
1153 *
1154 * Returns an I/O context or NULL in case of error
1155 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001156static void *
Owen Taylor3473f882001-02-23 17:55:21 +00001157xmlGzfileOpenW (const char *filename, int compression) {
1158 const char *path = NULL;
1159 char mode[15];
1160 gzFile fd;
1161
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001162 snprintf(mode, sizeof(mode), "wb%d", compression);
Owen Taylor3473f882001-02-23 17:55:21 +00001163 if (!strcmp(filename, "-")) {
Daniel Veillarda9e65e82001-10-30 10:32:36 +00001164 fd = gzdopen(dup(1), mode);
Owen Taylor3473f882001-02-23 17:55:21 +00001165 return((void *) fd);
1166 }
1167
Daniel Veillardf4862f02002-09-10 11:13:43 +00001168 if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17))
Daniel Veillard1997c3e2003-07-05 20:43:43 +00001169#if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
Daniel Veillardf4862f02002-09-10 11:13:43 +00001170 path = &filename[17];
1171#else
Owen Taylor3473f882001-02-23 17:55:21 +00001172 path = &filename[16];
Daniel Veillardf4862f02002-09-10 11:13:43 +00001173#endif
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001174 else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) {
Daniel Veillard1997c3e2003-07-05 20:43:43 +00001175#if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
Daniel Veillardfe703322001-08-14 12:18:09 +00001176 path = &filename[8];
1177#else
Daniel Veillard3c2758d2001-05-31 18:43:43 +00001178 path = &filename[7];
Daniel Veillardfe703322001-08-14 12:18:09 +00001179#endif
1180 } else
Owen Taylor3473f882001-02-23 17:55:21 +00001181 path = filename;
1182
1183 if (path == NULL)
1184 return(NULL);
1185
1186 fd = gzopen(path, mode);
1187 return((void *) fd);
1188}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001189#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001190
1191/**
1192 * xmlGzfileRead:
1193 * @context: the I/O context
1194 * @buffer: where to drop data
1195 * @len: number of bytes to write
1196 *
1197 * Read @len bytes to @buffer from the compressed I/O channel.
1198 *
1199 * Returns the number of bytes written
1200 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001201static int
Owen Taylor3473f882001-02-23 17:55:21 +00001202xmlGzfileRead (void * context, char * buffer, int len) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00001203 int ret;
1204
1205 ret = gzread((gzFile) context, &buffer[0], len);
1206 if (ret < 0) xmlIOErr(0, "gzread()");
1207 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001208}
1209
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001210#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001211/**
1212 * xmlGzfileWrite:
1213 * @context: the I/O context
1214 * @buffer: where to drop data
1215 * @len: number of bytes to write
1216 *
1217 * Write @len bytes from @buffer to the compressed I/O channel.
1218 *
1219 * Returns the number of bytes written
1220 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001221static int
Owen Taylor3473f882001-02-23 17:55:21 +00001222xmlGzfileWrite (void * context, const char * buffer, int len) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00001223 int ret;
1224
1225 ret = gzwrite((gzFile) context, (char *) &buffer[0], len);
1226 if (ret < 0) xmlIOErr(0, "gzwrite()");
1227 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001228}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001229#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00001230
1231/**
1232 * xmlGzfileClose:
1233 * @context: the I/O context
1234 *
1235 * Close a compressed I/O channel
1236 */
Daniel Veillardf012a642001-07-23 19:10:52 +00001237static int
Owen Taylor3473f882001-02-23 17:55:21 +00001238xmlGzfileClose (void * context) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00001239 int ret;
1240
1241 ret = (gzclose((gzFile) context) == Z_OK ) ? 0 : -1;
1242 if (ret < 0) xmlIOErr(0, "gzclose()");
1243 return(ret);
Owen Taylor3473f882001-02-23 17:55:21 +00001244}
1245#endif /* HAVE_ZLIB_H */
1246
1247#ifdef LIBXML_HTTP_ENABLED
1248/************************************************************************
1249 * *
1250 * I/O for HTTP file accesses *
1251 * *
1252 ************************************************************************/
Daniel Veillardf012a642001-07-23 19:10:52 +00001253
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001254#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardf012a642001-07-23 19:10:52 +00001255typedef struct xmlIOHTTPWriteCtxt_
1256{
1257 int compression;
1258
1259 char * uri;
1260
1261 void * doc_buff;
1262
1263} xmlIOHTTPWriteCtxt, *xmlIOHTTPWriteCtxtPtr;
1264
1265#ifdef HAVE_ZLIB_H
1266
1267#define DFLT_WBITS ( -15 )
1268#define DFLT_MEM_LVL ( 8 )
1269#define GZ_MAGIC1 ( 0x1f )
1270#define GZ_MAGIC2 ( 0x8b )
1271#define LXML_ZLIB_OS_CODE ( 0x03 )
1272#define INIT_HTTP_BUFF_SIZE ( 32768 )
1273#define DFLT_ZLIB_RATIO ( 5 )
1274
1275/*
1276** Data structure and functions to work with sending compressed data
1277** via HTTP.
1278*/
1279
1280typedef struct xmlZMemBuff_
1281{
1282 unsigned long size;
1283 unsigned long crc;
1284
1285 unsigned char * zbuff;
1286 z_stream zctrl;
1287
1288} xmlZMemBuff, *xmlZMemBuffPtr;
1289
1290/**
1291 * append_reverse_ulong
1292 * @buff: Compressed memory buffer
1293 * @data: Unsigned long to append
1294 *
1295 * Append a unsigned long in reverse byte order to the end of the
1296 * memory buffer.
1297 */
1298static void
1299append_reverse_ulong( xmlZMemBuff * buff, unsigned long data ) {
1300
1301 int idx;
1302
1303 if ( buff == NULL )
1304 return;
1305
1306 /*
1307 ** This is plagiarized from putLong in gzio.c (zlib source) where
1308 ** the number "4" is hardcoded. If zlib is ever patched to
1309 ** support 64 bit file sizes, this code would need to be patched
1310 ** as well.
1311 */
1312
1313 for ( idx = 0; idx < 4; idx++ ) {
1314 *buff->zctrl.next_out = ( data & 0xff );
1315 data >>= 8;
1316 buff->zctrl.next_out++;
1317 }
1318
1319 return;
1320}
1321
1322/**
1323 *
1324 * xmlFreeZMemBuff
1325 * @buff: The memory buffer context to clear
1326 *
1327 * Release all the resources associated with the compressed memory buffer.
1328 */
1329static void
1330xmlFreeZMemBuff( xmlZMemBuffPtr buff ) {
William M. Brack78637da2003-07-31 14:47:38 +00001331
1332#ifdef DEBUG_HTTP
Daniel Veillardf012a642001-07-23 19:10:52 +00001333 int z_err;
William M. Brack78637da2003-07-31 14:47:38 +00001334#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001335
1336 if ( buff == NULL )
1337 return;
1338
1339 xmlFree( buff->zbuff );
Daniel Veillardf012a642001-07-23 19:10:52 +00001340#ifdef DEBUG_HTTP
William M. Brack78637da2003-07-31 14:47:38 +00001341 z_err = deflateEnd( &buff->zctrl );
Daniel Veillardf012a642001-07-23 19:10:52 +00001342 if ( z_err != Z_OK )
1343 xmlGenericError( xmlGenericErrorContext,
1344 "xmlFreeZMemBuff: Error releasing zlib context: %d\n",
1345 z_err );
William M. Brack78637da2003-07-31 14:47:38 +00001346#else
1347 deflateEnd( &buff->zctrl );
William M. Brack779af002003-08-01 15:55:39 +00001348#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00001349
1350 xmlFree( buff );
1351 return;
1352}
1353
1354/**
1355 * xmlCreateZMemBuff
1356 *@compression: Compression value to use
1357 *
1358 * Create a memory buffer to hold the compressed XML document. The
1359 * compressed document in memory will end up being identical to what
1360 * would be created if gzopen/gzwrite/gzclose were being used to
1361 * write the document to disk. The code for the header/trailer data to
1362 * the compression is plagiarized from the zlib source files.
1363 */
1364static void *
1365xmlCreateZMemBuff( int compression ) {
1366
1367 int z_err;
1368 int hdr_lgth;
1369 xmlZMemBuffPtr buff = NULL;
1370
1371 if ( ( compression < 1 ) || ( compression > 9 ) )
1372 return ( NULL );
1373
1374 /* Create the control and data areas */
1375
1376 buff = xmlMalloc( sizeof( xmlZMemBuff ) );
1377 if ( buff == NULL ) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00001378 xmlIOErrMemory("creating buffer context");
Daniel Veillardf012a642001-07-23 19:10:52 +00001379 return ( NULL );
1380 }
1381
1382 (void)memset( buff, 0, sizeof( xmlZMemBuff ) );
1383 buff->size = INIT_HTTP_BUFF_SIZE;
1384 buff->zbuff = xmlMalloc( buff->size );
1385 if ( buff->zbuff == NULL ) {
1386 xmlFreeZMemBuff( buff );
Daniel Veillard05d987b2003-10-08 11:54:57 +00001387 xmlIOErrMemory("creating buffer");
Daniel Veillardf012a642001-07-23 19:10:52 +00001388 return ( NULL );
1389 }
1390
1391 z_err = deflateInit2( &buff->zctrl, compression, Z_DEFLATED,
1392 DFLT_WBITS, DFLT_MEM_LVL, Z_DEFAULT_STRATEGY );
1393 if ( z_err != Z_OK ) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00001394 xmlChar msg[500];
Daniel Veillardf012a642001-07-23 19:10:52 +00001395 xmlFreeZMemBuff( buff );
1396 buff = NULL;
Daniel Veillard05d987b2003-10-08 11:54:57 +00001397 xmlStrPrintf(msg, 500,
1398 (const xmlChar *) "xmlCreateZMemBuff: %s %d\n",
1399 "Error initializing compression context. ZLIB error:",
1400 z_err );
1401 xmlIOErr(XML_IO_WRITE, (const char *) msg);
Daniel Veillardf012a642001-07-23 19:10:52 +00001402 return ( NULL );
1403 }
1404
1405 /* Set the header data. The CRC will be needed for the trailer */
Daniel Veillard24505b02005-07-28 23:49:35 +00001406 buff->crc = crc32( 0L, NULL, 0 );
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001407 hdr_lgth = snprintf( (char *)buff->zbuff, buff->size,
1408 "%c%c%c%c%c%c%c%c%c%c",
Daniel Veillardf012a642001-07-23 19:10:52 +00001409 GZ_MAGIC1, GZ_MAGIC2, Z_DEFLATED,
1410 0, 0, 0, 0, 0, 0, LXML_ZLIB_OS_CODE );
1411 buff->zctrl.next_out = buff->zbuff + hdr_lgth;
1412 buff->zctrl.avail_out = buff->size - hdr_lgth;
1413
1414 return ( buff );
1415}
1416
1417/**
1418 * xmlZMemBuffExtend
1419 * @buff: Buffer used to compress and consolidate data.
1420 * @ext_amt: Number of bytes to extend the buffer.
1421 *
1422 * Extend the internal buffer used to store the compressed data by the
1423 * specified amount.
1424 *
1425 * Returns 0 on success or -1 on failure to extend the buffer. On failure
1426 * the original buffer still exists at the original size.
1427 */
1428static int
1429xmlZMemBuffExtend( xmlZMemBuffPtr buff, size_t ext_amt ) {
1430
1431 int rc = -1;
1432 size_t new_size;
1433 size_t cur_used;
1434
1435 unsigned char * tmp_ptr = NULL;
1436
1437 if ( buff == NULL )
1438 return ( -1 );
1439
1440 else if ( ext_amt == 0 )
1441 return ( 0 );
1442
1443 cur_used = buff->zctrl.next_out - buff->zbuff;
1444 new_size = buff->size + ext_amt;
1445
1446#ifdef DEBUG_HTTP
1447 if ( cur_used > new_size )
1448 xmlGenericError( xmlGenericErrorContext,
1449 "xmlZMemBuffExtend: %s\n%s %d bytes.\n",
1450 "Buffer overwrite detected during compressed memory",
1451 "buffer extension. Overflowed by",
1452 (cur_used - new_size ) );
1453#endif
1454
1455 tmp_ptr = xmlRealloc( buff->zbuff, new_size );
1456 if ( tmp_ptr != NULL ) {
1457 rc = 0;
1458 buff->size = new_size;
1459 buff->zbuff = tmp_ptr;
1460 buff->zctrl.next_out = tmp_ptr + cur_used;
1461 buff->zctrl.avail_out = new_size - cur_used;
1462 }
1463 else {
Daniel Veillard05d987b2003-10-08 11:54:57 +00001464 xmlChar msg[500];
1465 xmlStrPrintf(msg, 500,
1466 (const xmlChar *) "xmlZMemBuffExtend: %s %lu bytes.\n",
1467 "Allocation failure extending output buffer to",
1468 new_size );
1469 xmlIOErr(XML_IO_WRITE, (const char *) msg);
Daniel Veillardf012a642001-07-23 19:10:52 +00001470 }
1471
1472 return ( rc );
1473}
1474
1475/**
1476 * xmlZMemBuffAppend
1477 * @buff: Buffer used to compress and consolidate data
1478 * @src: Uncompressed source content to append to buffer
1479 * @len: Length of source data to append to buffer
1480 *
1481 * Compress and append data to the internal buffer. The data buffer
1482 * will be expanded if needed to store the additional data.
1483 *
1484 * Returns the number of bytes appended to the buffer or -1 on error.
1485 */
1486static int
1487xmlZMemBuffAppend( xmlZMemBuffPtr buff, const char * src, int len ) {
1488
1489 int z_err;
1490 size_t min_accept;
1491
1492 if ( ( buff == NULL ) || ( src == NULL ) )
1493 return ( -1 );
1494
1495 buff->zctrl.avail_in = len;
1496 buff->zctrl.next_in = (unsigned char *)src;
1497 while ( buff->zctrl.avail_in > 0 ) {
1498 /*
1499 ** Extend the buffer prior to deflate call if a reasonable amount
1500 ** of output buffer space is not available.
1501 */
1502 min_accept = buff->zctrl.avail_in / DFLT_ZLIB_RATIO;
1503 if ( buff->zctrl.avail_out <= min_accept ) {
1504 if ( xmlZMemBuffExtend( buff, buff->size ) == -1 )
1505 return ( -1 );
1506 }
1507
1508 z_err = deflate( &buff->zctrl, Z_NO_FLUSH );
1509 if ( z_err != Z_OK ) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00001510 xmlChar msg[500];
1511 xmlStrPrintf(msg, 500,
1512 (const xmlChar *) "xmlZMemBuffAppend: %s %d %s - %d",
Daniel Veillardf012a642001-07-23 19:10:52 +00001513 "Compression error while appending",
1514 len, "bytes to buffer. ZLIB error", z_err );
Daniel Veillard05d987b2003-10-08 11:54:57 +00001515 xmlIOErr(XML_IO_WRITE, (const char *) msg);
Daniel Veillardf012a642001-07-23 19:10:52 +00001516 return ( -1 );
1517 }
1518 }
1519
1520 buff->crc = crc32( buff->crc, (unsigned char *)src, len );
1521
1522 return ( len );
1523}
1524
1525/**
1526 * xmlZMemBuffGetContent
1527 * @buff: Compressed memory content buffer
1528 * @data_ref: Pointer reference to point to compressed content
1529 *
1530 * Flushes the compression buffers, appends gzip file trailers and
1531 * returns the compressed content and length of the compressed data.
1532 * NOTE: The gzip trailer code here is plagiarized from zlib source.
1533 *
1534 * Returns the length of the compressed data or -1 on error.
1535 */
1536static int
1537xmlZMemBuffGetContent( xmlZMemBuffPtr buff, char ** data_ref ) {
1538
1539 int zlgth = -1;
1540 int z_err;
1541
1542 if ( ( buff == NULL ) || ( data_ref == NULL ) )
1543 return ( -1 );
1544
1545 /* Need to loop until compression output buffers are flushed */
1546
1547 do
1548 {
1549 z_err = deflate( &buff->zctrl, Z_FINISH );
1550 if ( z_err == Z_OK ) {
1551 /* In this case Z_OK means more buffer space needed */
1552
1553 if ( xmlZMemBuffExtend( buff, buff->size ) == -1 )
1554 return ( -1 );
1555 }
1556 }
1557 while ( z_err == Z_OK );
1558
1559 /* If the compression state is not Z_STREAM_END, some error occurred */
1560
1561 if ( z_err == Z_STREAM_END ) {
1562
1563 /* Need to append the gzip data trailer */
1564
1565 if ( buff->zctrl.avail_out < ( 2 * sizeof( unsigned long ) ) ) {
1566 if ( xmlZMemBuffExtend(buff, (2 * sizeof(unsigned long))) == -1 )
1567 return ( -1 );
1568 }
1569
1570 /*
1571 ** For whatever reason, the CRC and length data are pushed out
1572 ** in reverse byte order. So a memcpy can't be used here.
1573 */
1574
1575 append_reverse_ulong( buff, buff->crc );
1576 append_reverse_ulong( buff, buff->zctrl.total_in );
1577
1578 zlgth = buff->zctrl.next_out - buff->zbuff;
1579 *data_ref = (char *)buff->zbuff;
1580 }
1581
Daniel Veillard05d987b2003-10-08 11:54:57 +00001582 else {
1583 xmlChar msg[500];
1584 xmlStrPrintf(msg, 500,
1585 (const xmlChar *) "xmlZMemBuffGetContent: %s - %d\n",
1586 "Error flushing zlib buffers. Error code", z_err );
1587 xmlIOErr(XML_IO_WRITE, (const char *) msg);
1588 }
Daniel Veillardf012a642001-07-23 19:10:52 +00001589
1590 return ( zlgth );
1591}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001592#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf012a642001-07-23 19:10:52 +00001593#endif /* HAVE_ZLIB_H */
1594
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001595#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardf012a642001-07-23 19:10:52 +00001596/**
1597 * xmlFreeHTTPWriteCtxt
1598 * @ctxt: Context to cleanup
1599 *
1600 * Free allocated memory and reclaim system resources.
1601 *
1602 * No return value.
1603 */
1604static void
1605xmlFreeHTTPWriteCtxt( xmlIOHTTPWriteCtxtPtr ctxt )
1606{
1607 if ( ctxt->uri != NULL )
Daniel Veillard819d5cb2002-10-14 11:15:18 +00001608 xmlFree( ctxt->uri );
Daniel Veillardf012a642001-07-23 19:10:52 +00001609
1610 if ( ctxt->doc_buff != NULL ) {
1611
1612#ifdef HAVE_ZLIB_H
1613 if ( ctxt->compression > 0 ) {
1614 xmlFreeZMemBuff( ctxt->doc_buff );
1615 }
1616 else
1617#endif
1618 {
1619 xmlOutputBufferClose( ctxt->doc_buff );
1620 }
1621 }
1622
Daniel Veillard819d5cb2002-10-14 11:15:18 +00001623 xmlFree( ctxt );
Daniel Veillardf012a642001-07-23 19:10:52 +00001624 return;
1625}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001626#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf012a642001-07-23 19:10:52 +00001627
1628
Owen Taylor3473f882001-02-23 17:55:21 +00001629/**
1630 * xmlIOHTTPMatch:
1631 * @filename: the URI for matching
1632 *
1633 * check if the URI matches an HTTP one
1634 *
1635 * Returns 1 if matches, 0 otherwise
1636 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001637int
Owen Taylor3473f882001-02-23 17:55:21 +00001638xmlIOHTTPMatch (const char *filename) {
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001639 if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "http://", 7))
Owen Taylor3473f882001-02-23 17:55:21 +00001640 return(1);
1641 return(0);
1642}
1643
1644/**
1645 * xmlIOHTTPOpen:
1646 * @filename: the URI for matching
1647 *
1648 * open an HTTP I/O channel
1649 *
1650 * Returns an I/O context or NULL in case of error
1651 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001652void *
Owen Taylor3473f882001-02-23 17:55:21 +00001653xmlIOHTTPOpen (const char *filename) {
1654 return(xmlNanoHTTPOpen(filename, NULL));
1655}
1656
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001657#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001658/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001659 * xmlIOHTTPOpenW:
Daniel Veillardf012a642001-07-23 19:10:52 +00001660 * @post_uri: The destination URI for the document
1661 * @compression: The compression desired for the document.
1662 *
1663 * Open a temporary buffer to collect the document for a subsequent HTTP POST
1664 * request. Non-static as is called from the output buffer creation routine.
1665 *
1666 * Returns an I/O context or NULL in case of error.
1667 */
1668
1669void *
Daniel Veillard572577e2002-01-18 16:23:55 +00001670xmlIOHTTPOpenW(const char *post_uri, int compression)
1671{
Daniel Veillardf012a642001-07-23 19:10:52 +00001672
Daniel Veillard572577e2002-01-18 16:23:55 +00001673 xmlIOHTTPWriteCtxtPtr ctxt = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001674
Daniel Veillard572577e2002-01-18 16:23:55 +00001675 if (post_uri == NULL)
1676 return (NULL);
Daniel Veillardf012a642001-07-23 19:10:52 +00001677
Daniel Veillard572577e2002-01-18 16:23:55 +00001678 ctxt = xmlMalloc(sizeof(xmlIOHTTPWriteCtxt));
1679 if (ctxt == NULL) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00001680 xmlIOErrMemory("creating HTTP output context");
Daniel Veillard572577e2002-01-18 16:23:55 +00001681 return (NULL);
Daniel Veillardf012a642001-07-23 19:10:52 +00001682 }
1683
Daniel Veillard572577e2002-01-18 16:23:55 +00001684 (void) memset(ctxt, 0, sizeof(xmlIOHTTPWriteCtxt));
Daniel Veillardf012a642001-07-23 19:10:52 +00001685
Daniel Veillard572577e2002-01-18 16:23:55 +00001686 ctxt->uri = (char *) xmlStrdup((const xmlChar *)post_uri);
1687 if (ctxt->uri == NULL) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00001688 xmlIOErrMemory("copying URI");
Daniel Veillard572577e2002-01-18 16:23:55 +00001689 xmlFreeHTTPWriteCtxt(ctxt);
1690 return (NULL);
Daniel Veillardf012a642001-07-23 19:10:52 +00001691 }
1692
1693 /*
Daniel Veillard572577e2002-01-18 16:23:55 +00001694 * ** Since the document length is required for an HTTP post,
1695 * ** need to put the document into a buffer. A memory buffer
1696 * ** is being used to avoid pushing the data to disk and back.
1697 */
Daniel Veillardf012a642001-07-23 19:10:52 +00001698
1699#ifdef HAVE_ZLIB_H
Daniel Veillard572577e2002-01-18 16:23:55 +00001700 if ((compression > 0) && (compression <= 9)) {
1701
1702 ctxt->compression = compression;
1703 ctxt->doc_buff = xmlCreateZMemBuff(compression);
1704 } else
Daniel Veillardf012a642001-07-23 19:10:52 +00001705#endif
1706 {
Daniel Veillard572577e2002-01-18 16:23:55 +00001707 /* Any character conversions should have been done before this */
Daniel Veillardf012a642001-07-23 19:10:52 +00001708
Daniel Veillard572577e2002-01-18 16:23:55 +00001709 ctxt->doc_buff = xmlAllocOutputBuffer(NULL);
Daniel Veillardf012a642001-07-23 19:10:52 +00001710 }
1711
Daniel Veillard572577e2002-01-18 16:23:55 +00001712 if (ctxt->doc_buff == NULL) {
1713 xmlFreeHTTPWriteCtxt(ctxt);
1714 ctxt = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001715 }
1716
Daniel Veillard572577e2002-01-18 16:23:55 +00001717 return (ctxt);
Daniel Veillardf012a642001-07-23 19:10:52 +00001718}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001719#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf012a642001-07-23 19:10:52 +00001720
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001721#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardf012a642001-07-23 19:10:52 +00001722/**
1723 * xmlIOHTTPDfltOpenW
1724 * @post_uri: The destination URI for this document.
1725 *
1726 * Calls xmlIOHTTPOpenW with no compression to set up for a subsequent
1727 * HTTP post command. This function should generally not be used as
1728 * the open callback is short circuited in xmlOutputBufferCreateFile.
1729 *
1730 * Returns a pointer to the new IO context.
1731 */
1732static void *
1733xmlIOHTTPDfltOpenW( const char * post_uri ) {
1734 return ( xmlIOHTTPOpenW( post_uri, 0 ) );
1735}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001736#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf012a642001-07-23 19:10:52 +00001737
1738/**
Owen Taylor3473f882001-02-23 17:55:21 +00001739 * xmlIOHTTPRead:
1740 * @context: the I/O context
1741 * @buffer: where to drop data
1742 * @len: number of bytes to write
1743 *
1744 * Read @len bytes to @buffer from the I/O channel.
1745 *
1746 * Returns the number of bytes written
1747 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001748int
Owen Taylor3473f882001-02-23 17:55:21 +00001749xmlIOHTTPRead(void * context, char * buffer, int len) {
Daniel Veillard5ea30d72004-11-08 11:54:28 +00001750 if ((buffer == NULL) || (len < 0)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00001751 return(xmlNanoHTTPRead(context, &buffer[0], len));
1752}
1753
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001754#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00001755/**
Daniel Veillardf012a642001-07-23 19:10:52 +00001756 * xmlIOHTTPWrite
1757 * @context: previously opened writing context
1758 * @buffer: data to output to temporary buffer
1759 * @len: bytes to output
1760 *
1761 * Collect data from memory buffer into a temporary file for later
1762 * processing.
1763 *
1764 * Returns number of bytes written.
1765 */
1766
1767static int
1768xmlIOHTTPWrite( void * context, const char * buffer, int len ) {
1769
1770 xmlIOHTTPWriteCtxtPtr ctxt = context;
1771
1772 if ( ( ctxt == NULL ) || ( ctxt->doc_buff == NULL ) || ( buffer == NULL ) )
1773 return ( -1 );
1774
1775 if ( len > 0 ) {
1776
1777 /* Use gzwrite or fwrite as previously setup in the open call */
1778
1779#ifdef HAVE_ZLIB_H
1780 if ( ctxt->compression > 0 )
1781 len = xmlZMemBuffAppend( ctxt->doc_buff, buffer, len );
1782
1783 else
1784#endif
1785 len = xmlOutputBufferWrite( ctxt->doc_buff, len, buffer );
1786
1787 if ( len < 0 ) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00001788 xmlChar msg[500];
1789 xmlStrPrintf(msg, 500,
1790 (const xmlChar *) "xmlIOHTTPWrite: %s\n%s '%s'.\n",
Daniel Veillardf012a642001-07-23 19:10:52 +00001791 "Error appending to internal buffer.",
1792 "Error sending document to URI",
1793 ctxt->uri );
Daniel Veillard05d987b2003-10-08 11:54:57 +00001794 xmlIOErr(XML_IO_WRITE, (const char *) msg);
Daniel Veillardf012a642001-07-23 19:10:52 +00001795 }
1796 }
1797
1798 return ( len );
1799}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001800#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf012a642001-07-23 19:10:52 +00001801
1802
1803/**
Owen Taylor3473f882001-02-23 17:55:21 +00001804 * xmlIOHTTPClose:
1805 * @context: the I/O context
1806 *
1807 * Close an HTTP I/O channel
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001808 *
1809 * Returns 0
Owen Taylor3473f882001-02-23 17:55:21 +00001810 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001811int
Owen Taylor3473f882001-02-23 17:55:21 +00001812xmlIOHTTPClose (void * context) {
1813 xmlNanoHTTPClose(context);
Daniel Veillardf012a642001-07-23 19:10:52 +00001814 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001815}
Daniel Veillardf012a642001-07-23 19:10:52 +00001816
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001817#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillardf012a642001-07-23 19:10:52 +00001818/**
1819 * xmlIOHTTCloseWrite
1820 * @context: The I/O context
1821 * @http_mthd: The HTTP method to be used when sending the data
1822 *
1823 * Close the transmit HTTP I/O channel and actually send the data.
1824 */
1825static int
1826xmlIOHTTPCloseWrite( void * context, const char * http_mthd ) {
1827
1828 int close_rc = -1;
1829 int http_rtn = 0;
1830 int content_lgth = 0;
1831 xmlIOHTTPWriteCtxtPtr ctxt = context;
1832
1833 char * http_content = NULL;
1834 char * content_encoding = NULL;
1835 char * content_type = (char *) "text/xml";
1836 void * http_ctxt = NULL;
1837
1838 if ( ( ctxt == NULL ) || ( http_mthd == NULL ) )
1839 return ( -1 );
1840
1841 /* Retrieve the content from the appropriate buffer */
1842
1843#ifdef HAVE_ZLIB_H
1844
1845 if ( ctxt->compression > 0 ) {
1846 content_lgth = xmlZMemBuffGetContent( ctxt->doc_buff, &http_content );
1847 content_encoding = (char *) "Content-Encoding: gzip";
1848 }
1849 else
1850#endif
1851 {
1852 /* Pull the data out of the memory output buffer */
1853
1854 xmlOutputBufferPtr dctxt = ctxt->doc_buff;
1855 http_content = (char *)dctxt->buffer->content;
1856 content_lgth = dctxt->buffer->use;
1857 }
1858
1859 if ( http_content == NULL ) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00001860 xmlChar msg[500];
1861 xmlStrPrintf(msg, 500,
1862 (const xmlChar *) "xmlIOHTTPCloseWrite: %s '%s' %s '%s'.\n",
1863 "Error retrieving content.\nUnable to",
1864 http_mthd, "data to URI", ctxt->uri );
1865 xmlIOErr(XML_IO_WRITE, (const char *) msg);
Daniel Veillardf012a642001-07-23 19:10:52 +00001866 }
1867
1868 else {
1869
1870 http_ctxt = xmlNanoHTTPMethod( ctxt->uri, http_mthd, http_content,
1871 &content_type, content_encoding,
1872 content_lgth );
1873
1874 if ( http_ctxt != NULL ) {
1875#ifdef DEBUG_HTTP
1876 /* If testing/debugging - dump reply with request content */
1877
1878 FILE * tst_file = NULL;
1879 char buffer[ 4096 ];
1880 char * dump_name = NULL;
1881 int avail;
1882
1883 xmlGenericError( xmlGenericErrorContext,
1884 "xmlNanoHTTPCloseWrite: HTTP %s to\n%s returned %d.\n",
1885 http_mthd, ctxt->uri,
1886 xmlNanoHTTPReturnCode( http_ctxt ) );
1887
1888 /*
1889 ** Since either content or reply may be gzipped,
1890 ** dump them to separate files instead of the
1891 ** standard error context.
1892 */
1893
1894 dump_name = tempnam( NULL, "lxml" );
1895 if ( dump_name != NULL ) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001896 (void)snprintf( buffer, sizeof(buffer), "%s.content", dump_name );
Daniel Veillardf012a642001-07-23 19:10:52 +00001897
Daniel Veillardcbbd78d2003-07-20 15:21:30 +00001898 tst_file = fopen( buffer, "wb" );
Daniel Veillardf012a642001-07-23 19:10:52 +00001899 if ( tst_file != NULL ) {
1900 xmlGenericError( xmlGenericErrorContext,
1901 "Transmitted content saved in file: %s\n", buffer );
1902
1903 fwrite( http_content, sizeof( char ),
1904 content_lgth, tst_file );
1905 fclose( tst_file );
1906 }
1907
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001908 (void)snprintf( buffer, sizeof(buffer), "%s.reply", dump_name );
Daniel Veillardcbbd78d2003-07-20 15:21:30 +00001909 tst_file = fopen( buffer, "wb" );
Daniel Veillardf012a642001-07-23 19:10:52 +00001910 if ( tst_file != NULL ) {
1911 xmlGenericError( xmlGenericErrorContext,
1912 "Reply content saved in file: %s\n", buffer );
1913
1914
1915 while ( (avail = xmlNanoHTTPRead( http_ctxt,
1916 buffer, sizeof( buffer ) )) > 0 ) {
1917
1918 fwrite( buffer, sizeof( char ), avail, tst_file );
1919 }
1920
1921 fclose( tst_file );
1922 }
1923
1924 free( dump_name );
1925 }
1926#endif /* DEBUG_HTTP */
1927
1928 http_rtn = xmlNanoHTTPReturnCode( http_ctxt );
1929 if ( ( http_rtn >= 200 ) && ( http_rtn < 300 ) )
1930 close_rc = 0;
Daniel Veillard05d987b2003-10-08 11:54:57 +00001931 else {
1932 xmlChar msg[500];
1933 xmlStrPrintf(msg, 500,
1934 (const xmlChar *) "xmlIOHTTPCloseWrite: HTTP '%s' of %d %s\n'%s' %s %d\n",
Daniel Veillardf012a642001-07-23 19:10:52 +00001935 http_mthd, content_lgth,
1936 "bytes to URI", ctxt->uri,
1937 "failed. HTTP return code:", http_rtn );
Daniel Veillard05d987b2003-10-08 11:54:57 +00001938 xmlIOErr(XML_IO_WRITE, (const char *) msg);
1939 }
Daniel Veillardf012a642001-07-23 19:10:52 +00001940
1941 xmlNanoHTTPClose( http_ctxt );
1942 xmlFree( content_type );
1943 }
1944 }
1945
1946 /* Final cleanups */
1947
1948 xmlFreeHTTPWriteCtxt( ctxt );
1949
1950 return ( close_rc );
1951}
1952
1953/**
1954 * xmlIOHTTPClosePut
1955 *
1956 * @context: The I/O context
1957 *
1958 * Close the transmit HTTP I/O channel and actually send data using a PUT
1959 * HTTP method.
1960 */
1961static int
1962xmlIOHTTPClosePut( void * ctxt ) {
1963 return ( xmlIOHTTPCloseWrite( ctxt, "PUT" ) );
1964}
1965
1966
1967/**
1968 * xmlIOHTTPClosePost
1969 *
1970 * @context: The I/O context
1971 *
1972 * Close the transmit HTTP I/O channel and actually send data using a POST
1973 * HTTP method.
1974 */
1975static int
1976xmlIOHTTPClosePost( void * ctxt ) {
1977 return ( xmlIOHTTPCloseWrite( ctxt, "POST" ) );
1978}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00001979#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf012a642001-07-23 19:10:52 +00001980
Owen Taylor3473f882001-02-23 17:55:21 +00001981#endif /* LIBXML_HTTP_ENABLED */
1982
1983#ifdef LIBXML_FTP_ENABLED
1984/************************************************************************
1985 * *
1986 * I/O for FTP file accesses *
1987 * *
1988 ************************************************************************/
1989/**
1990 * xmlIOFTPMatch:
1991 * @filename: the URI for matching
1992 *
1993 * check if the URI matches an FTP one
1994 *
1995 * Returns 1 if matches, 0 otherwise
1996 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001997int
Owen Taylor3473f882001-02-23 17:55:21 +00001998xmlIOFTPMatch (const char *filename) {
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001999 if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "ftp://", 6))
Owen Taylor3473f882001-02-23 17:55:21 +00002000 return(1);
2001 return(0);
2002}
2003
2004/**
2005 * xmlIOFTPOpen:
2006 * @filename: the URI for matching
2007 *
2008 * open an FTP I/O channel
2009 *
2010 * Returns an I/O context or NULL in case of error
2011 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00002012void *
Owen Taylor3473f882001-02-23 17:55:21 +00002013xmlIOFTPOpen (const char *filename) {
2014 return(xmlNanoFTPOpen(filename));
2015}
2016
2017/**
2018 * xmlIOFTPRead:
2019 * @context: the I/O context
2020 * @buffer: where to drop data
2021 * @len: number of bytes to write
2022 *
2023 * Read @len bytes to @buffer from the I/O channel.
2024 *
2025 * Returns the number of bytes written
2026 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00002027int
Owen Taylor3473f882001-02-23 17:55:21 +00002028xmlIOFTPRead(void * context, char * buffer, int len) {
Daniel Veillard5ea30d72004-11-08 11:54:28 +00002029 if ((buffer == NULL) || (len < 0)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002030 return(xmlNanoFTPRead(context, &buffer[0], len));
2031}
2032
2033/**
2034 * xmlIOFTPClose:
2035 * @context: the I/O context
2036 *
2037 * Close an FTP I/O channel
Daniel Veillarda9b66d02002-12-11 14:23:49 +00002038 *
2039 * Returns 0
Owen Taylor3473f882001-02-23 17:55:21 +00002040 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00002041int
Owen Taylor3473f882001-02-23 17:55:21 +00002042xmlIOFTPClose (void * context) {
Daniel Veillardf012a642001-07-23 19:10:52 +00002043 return ( xmlNanoFTPClose(context) );
Owen Taylor3473f882001-02-23 17:55:21 +00002044}
2045#endif /* LIBXML_FTP_ENABLED */
2046
2047
2048/**
2049 * xmlRegisterInputCallbacks:
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002050 * @matchFunc: the xmlInputMatchCallback
2051 * @openFunc: the xmlInputOpenCallback
2052 * @readFunc: the xmlInputReadCallback
2053 * @closeFunc: the xmlInputCloseCallback
Owen Taylor3473f882001-02-23 17:55:21 +00002054 *
2055 * Register a new set of I/O callback for handling parser input.
2056 *
2057 * Returns the registered handler number or -1 in case of error
2058 */
2059int
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002060xmlRegisterInputCallbacks(xmlInputMatchCallback matchFunc,
2061 xmlInputOpenCallback openFunc, xmlInputReadCallback readFunc,
2062 xmlInputCloseCallback closeFunc) {
Owen Taylor3473f882001-02-23 17:55:21 +00002063 if (xmlInputCallbackNr >= MAX_INPUT_CALLBACK) {
2064 return(-1);
2065 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002066 xmlInputCallbackTable[xmlInputCallbackNr].matchcallback = matchFunc;
2067 xmlInputCallbackTable[xmlInputCallbackNr].opencallback = openFunc;
2068 xmlInputCallbackTable[xmlInputCallbackNr].readcallback = readFunc;
2069 xmlInputCallbackTable[xmlInputCallbackNr].closecallback = closeFunc;
Daniel Veillard9ec26532003-09-23 07:43:19 +00002070 xmlInputCallbackInitialized = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002071 return(xmlInputCallbackNr++);
2072}
2073
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002074#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002075/**
2076 * xmlRegisterOutputCallbacks:
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002077 * @matchFunc: the xmlOutputMatchCallback
2078 * @openFunc: the xmlOutputOpenCallback
2079 * @writeFunc: the xmlOutputWriteCallback
2080 * @closeFunc: the xmlOutputCloseCallback
Owen Taylor3473f882001-02-23 17:55:21 +00002081 *
2082 * Register a new set of I/O callback for handling output.
2083 *
2084 * Returns the registered handler number or -1 in case of error
2085 */
2086int
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002087xmlRegisterOutputCallbacks(xmlOutputMatchCallback matchFunc,
2088 xmlOutputOpenCallback openFunc, xmlOutputWriteCallback writeFunc,
2089 xmlOutputCloseCallback closeFunc) {
Owen Taylor3473f882001-02-23 17:55:21 +00002090 if (xmlOutputCallbackNr >= MAX_INPUT_CALLBACK) {
2091 return(-1);
2092 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +00002093 xmlOutputCallbackTable[xmlOutputCallbackNr].matchcallback = matchFunc;
2094 xmlOutputCallbackTable[xmlOutputCallbackNr].opencallback = openFunc;
2095 xmlOutputCallbackTable[xmlOutputCallbackNr].writecallback = writeFunc;
2096 xmlOutputCallbackTable[xmlOutputCallbackNr].closecallback = closeFunc;
Daniel Veillard9ec26532003-09-23 07:43:19 +00002097 xmlOutputCallbackInitialized = 1;
Owen Taylor3473f882001-02-23 17:55:21 +00002098 return(xmlOutputCallbackNr++);
2099}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002100#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002101
2102/**
2103 * xmlRegisterDefaultInputCallbacks:
2104 *
2105 * Registers the default compiled-in I/O handlers.
2106 */
2107void
Owen Taylor3473f882001-02-23 17:55:21 +00002108xmlRegisterDefaultInputCallbacks
Owen Taylor3473f882001-02-23 17:55:21 +00002109(void) {
2110 if (xmlInputCallbackInitialized)
2111 return;
2112
Daniel Veillard8ca85b22006-09-01 09:56:07 +00002113#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2114 xmlInitPlatformSpecificIo();
2115#endif
2116
Owen Taylor3473f882001-02-23 17:55:21 +00002117 xmlRegisterInputCallbacks(xmlFileMatch, xmlFileOpen,
2118 xmlFileRead, xmlFileClose);
2119#ifdef HAVE_ZLIB_H
2120 xmlRegisterInputCallbacks(xmlGzfileMatch, xmlGzfileOpen,
2121 xmlGzfileRead, xmlGzfileClose);
2122#endif /* HAVE_ZLIB_H */
2123
2124#ifdef LIBXML_HTTP_ENABLED
2125 xmlRegisterInputCallbacks(xmlIOHTTPMatch, xmlIOHTTPOpen,
2126 xmlIOHTTPRead, xmlIOHTTPClose);
2127#endif /* LIBXML_HTTP_ENABLED */
2128
2129#ifdef LIBXML_FTP_ENABLED
2130 xmlRegisterInputCallbacks(xmlIOFTPMatch, xmlIOFTPOpen,
2131 xmlIOFTPRead, xmlIOFTPClose);
2132#endif /* LIBXML_FTP_ENABLED */
2133 xmlInputCallbackInitialized = 1;
2134}
2135
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002136#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002137/**
2138 * xmlRegisterDefaultOutputCallbacks:
2139 *
2140 * Registers the default compiled-in I/O handlers.
2141 */
2142void
Owen Taylor3473f882001-02-23 17:55:21 +00002143xmlRegisterDefaultOutputCallbacks
Owen Taylor3473f882001-02-23 17:55:21 +00002144(void) {
2145 if (xmlOutputCallbackInitialized)
2146 return;
2147
Daniel Veillard8ca85b22006-09-01 09:56:07 +00002148#if defined(_WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__)
2149 xmlInitPlatformSpecificIo();
2150#endif
2151
Owen Taylor3473f882001-02-23 17:55:21 +00002152 xmlRegisterOutputCallbacks(xmlFileMatch, xmlFileOpenW,
2153 xmlFileWrite, xmlFileClose);
Daniel Veillardf012a642001-07-23 19:10:52 +00002154
2155#ifdef LIBXML_HTTP_ENABLED
2156 xmlRegisterOutputCallbacks(xmlIOHTTPMatch, xmlIOHTTPDfltOpenW,
2157 xmlIOHTTPWrite, xmlIOHTTPClosePut);
2158#endif
2159
Owen Taylor3473f882001-02-23 17:55:21 +00002160/*********************************
2161 No way a-priori to distinguish between gzipped files from
2162 uncompressed ones except opening if existing then closing
2163 and saving with same compression ratio ... a pain.
2164
2165#ifdef HAVE_ZLIB_H
2166 xmlRegisterOutputCallbacks(xmlGzfileMatch, xmlGzfileOpen,
2167 xmlGzfileWrite, xmlGzfileClose);
2168#endif
Owen Taylor3473f882001-02-23 17:55:21 +00002169
2170 Nor FTP PUT ....
2171#ifdef LIBXML_FTP_ENABLED
2172 xmlRegisterOutputCallbacks(xmlIOFTPMatch, xmlIOFTPOpen,
2173 xmlIOFTPWrite, xmlIOFTPClose);
2174#endif
2175 **********************************/
2176 xmlOutputCallbackInitialized = 1;
2177}
2178
Daniel Veillardf012a642001-07-23 19:10:52 +00002179#ifdef LIBXML_HTTP_ENABLED
2180/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00002181 * xmlRegisterHTTPPostCallbacks:
Daniel Veillardf012a642001-07-23 19:10:52 +00002182 *
2183 * By default, libxml submits HTTP output requests using the "PUT" method.
2184 * Calling this method changes the HTTP output method to use the "POST"
2185 * method instead.
2186 *
2187 */
2188void
2189xmlRegisterHTTPPostCallbacks( void ) {
2190
2191 /* Register defaults if not done previously */
2192
2193 if ( xmlOutputCallbackInitialized == 0 )
2194 xmlRegisterDefaultOutputCallbacks( );
2195
2196 xmlRegisterOutputCallbacks(xmlIOHTTPMatch, xmlIOHTTPDfltOpenW,
2197 xmlIOHTTPWrite, xmlIOHTTPClosePost);
2198 return;
2199}
2200#endif
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002201#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardf012a642001-07-23 19:10:52 +00002202
Owen Taylor3473f882001-02-23 17:55:21 +00002203/**
2204 * xmlAllocParserInputBuffer:
2205 * @enc: the charset encoding if known
2206 *
2207 * Create a buffered parser input for progressive parsing
2208 *
2209 * Returns the new parser input or NULL
2210 */
2211xmlParserInputBufferPtr
2212xmlAllocParserInputBuffer(xmlCharEncoding enc) {
2213 xmlParserInputBufferPtr ret;
2214
2215 ret = (xmlParserInputBufferPtr) xmlMalloc(sizeof(xmlParserInputBuffer));
2216 if (ret == NULL) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00002217 xmlIOErrMemory("creating input buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00002218 return(NULL);
2219 }
2220 memset(ret, 0, (size_t) sizeof(xmlParserInputBuffer));
Daniel Veillard6155d8a2003-08-19 15:01:28 +00002221 ret->buffer = xmlBufferCreateSize(2 * xmlDefaultBufferSize);
Owen Taylor3473f882001-02-23 17:55:21 +00002222 if (ret->buffer == NULL) {
2223 xmlFree(ret);
2224 return(NULL);
2225 }
2226 ret->buffer->alloc = XML_BUFFER_ALLOC_DOUBLEIT;
2227 ret->encoder = xmlGetCharEncodingHandler(enc);
2228 if (ret->encoder != NULL)
Daniel Veillard6155d8a2003-08-19 15:01:28 +00002229 ret->raw = xmlBufferCreateSize(2 * xmlDefaultBufferSize);
Owen Taylor3473f882001-02-23 17:55:21 +00002230 else
2231 ret->raw = NULL;
2232 ret->readcallback = NULL;
2233 ret->closecallback = NULL;
2234 ret->context = NULL;
William M. Brackc07329e2003-09-08 01:57:30 +00002235 ret->compressed = -1;
Daniel Veillard36711902004-02-11 13:25:26 +00002236 ret->rawconsumed = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002237
2238 return(ret);
2239}
2240
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002241#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002242/**
2243 * xmlAllocOutputBuffer:
2244 * @encoder: the encoding converter or NULL
2245 *
2246 * Create a buffered parser output
2247 *
2248 * Returns the new parser output or NULL
2249 */
2250xmlOutputBufferPtr
2251xmlAllocOutputBuffer(xmlCharEncodingHandlerPtr encoder) {
2252 xmlOutputBufferPtr ret;
2253
2254 ret = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
2255 if (ret == NULL) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00002256 xmlIOErrMemory("creating output buffer");
Owen Taylor3473f882001-02-23 17:55:21 +00002257 return(NULL);
2258 }
2259 memset(ret, 0, (size_t) sizeof(xmlOutputBuffer));
2260 ret->buffer = xmlBufferCreate();
2261 if (ret->buffer == NULL) {
2262 xmlFree(ret);
2263 return(NULL);
2264 }
2265 ret->buffer->alloc = XML_BUFFER_ALLOC_DOUBLEIT;
2266 ret->encoder = encoder;
2267 if (encoder != NULL) {
2268 ret->conv = xmlBufferCreateSize(4000);
2269 /*
2270 * This call is designed to initiate the encoder state
2271 */
2272 xmlCharEncOutFunc(encoder, ret->conv, NULL);
2273 } else
2274 ret->conv = NULL;
2275 ret->writecallback = NULL;
2276 ret->closecallback = NULL;
2277 ret->context = NULL;
2278 ret->written = 0;
2279
2280 return(ret);
2281}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002282#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002283
2284/**
2285 * xmlFreeParserInputBuffer:
2286 * @in: a buffered parser input
2287 *
2288 * Free up the memory used by a buffered parser input
2289 */
2290void
2291xmlFreeParserInputBuffer(xmlParserInputBufferPtr in) {
Daniel Veillard8d8bf2c2003-09-17 19:36:25 +00002292 if (in == NULL) return;
2293
Owen Taylor3473f882001-02-23 17:55:21 +00002294 if (in->raw) {
2295 xmlBufferFree(in->raw);
2296 in->raw = NULL;
2297 }
2298 if (in->encoder != NULL) {
2299 xmlCharEncCloseFunc(in->encoder);
2300 }
2301 if (in->closecallback != NULL) {
2302 in->closecallback(in->context);
2303 }
2304 if (in->buffer != NULL) {
2305 xmlBufferFree(in->buffer);
2306 in->buffer = NULL;
2307 }
2308
Owen Taylor3473f882001-02-23 17:55:21 +00002309 xmlFree(in);
2310}
2311
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002312#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002313/**
2314 * xmlOutputBufferClose:
2315 * @out: a buffered output
2316 *
2317 * flushes and close the output I/O channel
2318 * and free up all the associated resources
2319 *
2320 * Returns the number of byte written or -1 in case of error.
2321 */
2322int
Daniel Veillard828ce832003-10-08 19:19:10 +00002323xmlOutputBufferClose(xmlOutputBufferPtr out)
2324{
Owen Taylor3473f882001-02-23 17:55:21 +00002325 int written;
Daniel Veillardf012a642001-07-23 19:10:52 +00002326 int err_rc = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002327
2328 if (out == NULL)
Daniel Veillard828ce832003-10-08 19:19:10 +00002329 return (-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002330 if (out->writecallback != NULL)
Daniel Veillard828ce832003-10-08 19:19:10 +00002331 xmlOutputBufferFlush(out);
Owen Taylor3473f882001-02-23 17:55:21 +00002332 if (out->closecallback != NULL) {
Daniel Veillard828ce832003-10-08 19:19:10 +00002333 err_rc = out->closecallback(out->context);
Owen Taylor3473f882001-02-23 17:55:21 +00002334 }
2335 written = out->written;
2336 if (out->conv) {
2337 xmlBufferFree(out->conv);
Daniel Veillard828ce832003-10-08 19:19:10 +00002338 out->conv = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002339 }
2340 if (out->encoder != NULL) {
2341 xmlCharEncCloseFunc(out->encoder);
2342 }
2343 if (out->buffer != NULL) {
2344 xmlBufferFree(out->buffer);
Daniel Veillard828ce832003-10-08 19:19:10 +00002345 out->buffer = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002346 }
2347
Daniel Veillard828ce832003-10-08 19:19:10 +00002348 if (out->error)
2349 err_rc = -1;
Owen Taylor3473f882001-02-23 17:55:21 +00002350 xmlFree(out);
Daniel Veillard828ce832003-10-08 19:19:10 +00002351 return ((err_rc == 0) ? written : err_rc);
Owen Taylor3473f882001-02-23 17:55:21 +00002352}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002353#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002354
Daniel Veillard1b243b42004-06-08 10:16:42 +00002355xmlParserInputBufferPtr
Daniel Veillard0335a842004-06-02 16:18:40 +00002356__xmlParserInputBufferCreateFilename(const char *URI, xmlCharEncoding enc) {
Owen Taylor3473f882001-02-23 17:55:21 +00002357 xmlParserInputBufferPtr ret;
Daniel Veillard388236f2001-07-08 18:35:48 +00002358 int i = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002359 void *context = NULL;
2360
2361 if (xmlInputCallbackInitialized == 0)
2362 xmlRegisterDefaultInputCallbacks();
2363
2364 if (URI == NULL) return(NULL);
2365
2366 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002367 * Try to find one of the input accept method accepting that scheme
Owen Taylor3473f882001-02-23 17:55:21 +00002368 * Go in reverse to give precedence to user defined handlers.
Daniel Veillard388236f2001-07-08 18:35:48 +00002369 */
2370 if (context == NULL) {
2371 for (i = xmlInputCallbackNr - 1;i >= 0;i--) {
2372 if ((xmlInputCallbackTable[i].matchcallback != NULL) &&
2373 (xmlInputCallbackTable[i].matchcallback(URI) != 0)) {
Igor Zlatkovic5f9fada2003-02-19 14:51:00 +00002374 context = xmlInputCallbackTable[i].opencallback(URI);
Daniel Veillard847332a2003-10-18 11:29:40 +00002375 if (context != NULL) {
Daniel Veillard388236f2001-07-08 18:35:48 +00002376 break;
Daniel Veillard847332a2003-10-18 11:29:40 +00002377 }
Daniel Veillard388236f2001-07-08 18:35:48 +00002378 }
Owen Taylor3473f882001-02-23 17:55:21 +00002379 }
2380 }
2381 if (context == NULL) {
2382 return(NULL);
2383 }
2384
2385 /*
2386 * Allocate the Input buffer front-end.
2387 */
2388 ret = xmlAllocParserInputBuffer(enc);
2389 if (ret != NULL) {
2390 ret->context = context;
2391 ret->readcallback = xmlInputCallbackTable[i].readcallback;
2392 ret->closecallback = xmlInputCallbackTable[i].closecallback;
William M. Brackc07329e2003-09-08 01:57:30 +00002393#ifdef HAVE_ZLIB_H
William M. Brackc5cbf992003-10-29 22:15:13 +00002394 if ((xmlInputCallbackTable[i].opencallback == xmlGzfileOpen) &&
2395 (strcmp(URI, "-") != 0)) {
William M. Brackc07329e2003-09-08 01:57:30 +00002396 if (((z_stream *)context)->avail_in > 4) {
2397 char *cptr, buff4[4];
Daniel Veillard07cb8222003-09-10 10:51:05 +00002398 cptr = (char *) ((z_stream *)context)->next_in;
William M. Brackc07329e2003-09-08 01:57:30 +00002399 if (gzread(context, buff4, 4) == 4) {
2400 if (strncmp(buff4, cptr, 4) == 0)
2401 ret->compressed = 0;
2402 else
2403 ret->compressed = 1;
2404 gzrewind(context);
2405 }
2406 }
2407 }
2408#endif
Owen Taylor3473f882001-02-23 17:55:21 +00002409 }
William M. Brack42331a92004-07-29 07:07:16 +00002410 else
2411 xmlInputCallbackTable[i].closecallback (context);
2412
Owen Taylor3473f882001-02-23 17:55:21 +00002413 return(ret);
2414}
2415
2416/**
Daniel Veillard0335a842004-06-02 16:18:40 +00002417 * xmlParserInputBufferCreateFilename:
Owen Taylor3473f882001-02-23 17:55:21 +00002418 * @URI: a C string containing the URI or filename
Daniel Veillard0335a842004-06-02 16:18:40 +00002419 * @enc: the charset encoding if known
Owen Taylor3473f882001-02-23 17:55:21 +00002420 *
Daniel Veillard0335a842004-06-02 16:18:40 +00002421 * Create a buffered parser input for the progressive parsing of a file
2422 * If filename is "-' then we use stdin as the input.
Owen Taylor3473f882001-02-23 17:55:21 +00002423 * Automatic support for ZLIB/Compress compressed document is provided
2424 * by default if found at compile-time.
Daniel Veillard0335a842004-06-02 16:18:40 +00002425 * Do an encoding check if enc == XML_CHAR_ENCODING_NONE
Owen Taylor3473f882001-02-23 17:55:21 +00002426 *
Daniel Veillard0335a842004-06-02 16:18:40 +00002427 * Returns the new parser input or NULL
Owen Taylor3473f882001-02-23 17:55:21 +00002428 */
Daniel Veillard0335a842004-06-02 16:18:40 +00002429xmlParserInputBufferPtr
2430xmlParserInputBufferCreateFilename(const char *URI, xmlCharEncoding enc) {
2431 if ((xmlParserInputBufferCreateFilenameValue)) {
2432 return xmlParserInputBufferCreateFilenameValue(URI, enc);
2433 }
2434 return __xmlParserInputBufferCreateFilename(URI, enc);
2435}
2436
2437#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard1b243b42004-06-08 10:16:42 +00002438xmlOutputBufferPtr
Daniel Veillard0335a842004-06-02 16:18:40 +00002439__xmlOutputBufferCreateFilename(const char *URI,
Owen Taylor3473f882001-02-23 17:55:21 +00002440 xmlCharEncodingHandlerPtr encoder,
Daniel Veillard0335a842004-06-02 16:18:40 +00002441 int compression ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +00002442 xmlOutputBufferPtr ret;
Daniel Veillard966a31e2004-05-09 02:58:44 +00002443 xmlURIPtr puri;
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00002444 int i = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002445 void *context = NULL;
Daniel Veillard966a31e2004-05-09 02:58:44 +00002446 char *unescaped = NULL;
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00002447#ifdef HAVE_ZLIB_H
Daniel Veillard966a31e2004-05-09 02:58:44 +00002448 int is_file_uri = 1;
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00002449#endif
Daniel Veillardf012a642001-07-23 19:10:52 +00002450
Owen Taylor3473f882001-02-23 17:55:21 +00002451 if (xmlOutputCallbackInitialized == 0)
2452 xmlRegisterDefaultOutputCallbacks();
2453
2454 if (URI == NULL) return(NULL);
2455
Daniel Veillard966a31e2004-05-09 02:58:44 +00002456 puri = xmlParseURI(URI);
2457 if (puri != NULL) {
Daniel Veillard8fcd2ca2005-07-03 14:42:56 +00002458#ifdef HAVE_ZLIB_H
Daniel Veillard18a65092004-05-11 15:57:42 +00002459 if ((puri->scheme != NULL) &&
2460 (!xmlStrEqual(BAD_CAST puri->scheme, BAD_CAST "file")))
Daniel Veillard966a31e2004-05-09 02:58:44 +00002461 is_file_uri = 0;
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00002462#endif
Daniel Veillard966a31e2004-05-09 02:58:44 +00002463 /*
2464 * try to limit the damages of the URI unescaping code.
2465 */
2466 if (puri->scheme != NULL)
2467 unescaped = xmlURIUnescapeString(URI, 0, NULL);
2468 xmlFreeURI(puri);
2469 }
Owen Taylor3473f882001-02-23 17:55:21 +00002470
2471 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002472 * Try to find one of the output accept method accepting that scheme
Owen Taylor3473f882001-02-23 17:55:21 +00002473 * Go in reverse to give precedence to user defined handlers.
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00002474 * try with an unescaped version of the URI
Owen Taylor3473f882001-02-23 17:55:21 +00002475 */
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00002476 if (unescaped != NULL) {
2477#ifdef HAVE_ZLIB_H
Daniel Veillard966a31e2004-05-09 02:58:44 +00002478 if ((compression > 0) && (compression <= 9) && (is_file_uri == 1)) {
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00002479 context = xmlGzfileOpenW(unescaped, compression);
2480 if (context != NULL) {
2481 ret = xmlAllocOutputBuffer(encoder);
2482 if (ret != NULL) {
2483 ret->context = context;
2484 ret->writecallback = xmlGzfileWrite;
2485 ret->closecallback = xmlGzfileClose;
2486 }
2487 xmlFree(unescaped);
2488 return(ret);
2489 }
2490 }
Daniel Veillardf012a642001-07-23 19:10:52 +00002491#endif
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00002492 for (i = xmlOutputCallbackNr - 1;i >= 0;i--) {
2493 if ((xmlOutputCallbackTable[i].matchcallback != NULL) &&
2494 (xmlOutputCallbackTable[i].matchcallback(unescaped) != 0)) {
2495#if defined(LIBXML_HTTP_ENABLED) && defined(HAVE_ZLIB_H)
2496 /* Need to pass compression parameter into HTTP open calls */
2497 if (xmlOutputCallbackTable[i].matchcallback == xmlIOHTTPMatch)
2498 context = xmlIOHTTPOpenW(unescaped, compression);
2499 else
2500#endif
2501 context = xmlOutputCallbackTable[i].opencallback(unescaped);
2502 if (context != NULL)
2503 break;
2504 }
2505 }
2506 xmlFree(unescaped);
2507 }
Daniel Veillardf012a642001-07-23 19:10:52 +00002508
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00002509 /*
2510 * If this failed try with a non-escaped URI this may be a strange
2511 * filename
2512 */
2513 if (context == NULL) {
2514#ifdef HAVE_ZLIB_H
Daniel Veillard966a31e2004-05-09 02:58:44 +00002515 if ((compression > 0) && (compression <= 9) && (is_file_uri == 1)) {
Igor Zlatkovic5f9fada2003-02-19 14:51:00 +00002516 context = xmlGzfileOpenW(URI, compression);
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00002517 if (context != NULL) {
2518 ret = xmlAllocOutputBuffer(encoder);
2519 if (ret != NULL) {
2520 ret->context = context;
2521 ret->writecallback = xmlGzfileWrite;
2522 ret->closecallback = xmlGzfileClose;
2523 }
2524 return(ret);
2525 }
2526 }
2527#endif
2528 for (i = xmlOutputCallbackNr - 1;i >= 0;i--) {
2529 if ((xmlOutputCallbackTable[i].matchcallback != NULL) &&
Igor Zlatkovic5f9fada2003-02-19 14:51:00 +00002530 (xmlOutputCallbackTable[i].matchcallback(URI) != 0)) {
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00002531#if defined(LIBXML_HTTP_ENABLED) && defined(HAVE_ZLIB_H)
2532 /* Need to pass compression parameter into HTTP open calls */
2533 if (xmlOutputCallbackTable[i].matchcallback == xmlIOHTTPMatch)
2534 context = xmlIOHTTPOpenW(URI, compression);
2535 else
2536#endif
2537 context = xmlOutputCallbackTable[i].opencallback(URI);
2538 if (context != NULL)
2539 break;
2540 }
Owen Taylor3473f882001-02-23 17:55:21 +00002541 }
2542 }
Daniel Veillardf012a642001-07-23 19:10:52 +00002543
Owen Taylor3473f882001-02-23 17:55:21 +00002544 if (context == NULL) {
2545 return(NULL);
2546 }
2547
2548 /*
2549 * Allocate the Output buffer front-end.
2550 */
2551 ret = xmlAllocOutputBuffer(encoder);
2552 if (ret != NULL) {
2553 ret->context = context;
2554 ret->writecallback = xmlOutputCallbackTable[i].writecallback;
2555 ret->closecallback = xmlOutputCallbackTable[i].closecallback;
2556 }
2557 return(ret);
2558}
Daniel Veillard0335a842004-06-02 16:18:40 +00002559
2560/**
2561 * xmlOutputBufferCreateFilename:
2562 * @URI: a C string containing the URI or filename
2563 * @encoder: the encoding converter or NULL
2564 * @compression: the compression ration (0 none, 9 max).
2565 *
2566 * Create a buffered output for the progressive saving of a file
2567 * If filename is "-' then we use stdout as the output.
2568 * Automatic support for ZLIB/Compress compressed document is provided
2569 * by default if found at compile-time.
2570 * TODO: currently if compression is set, the library only support
2571 * writing to a local file.
2572 *
2573 * Returns the new output or NULL
2574 */
2575xmlOutputBufferPtr
2576xmlOutputBufferCreateFilename(const char *URI,
2577 xmlCharEncodingHandlerPtr encoder,
2578 int compression ATTRIBUTE_UNUSED) {
2579 if ((xmlOutputBufferCreateFilenameValue)) {
2580 return xmlOutputBufferCreateFilenameValue(URI, encoder, compression);
2581 }
2582 return __xmlOutputBufferCreateFilename(URI, encoder, compression);
2583}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002584#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002585
2586/**
2587 * xmlParserInputBufferCreateFile:
2588 * @file: a FILE*
2589 * @enc: the charset encoding if known
2590 *
2591 * Create a buffered parser input for the progressive parsing of a FILE *
2592 * buffered C I/O
2593 *
2594 * Returns the new parser input or NULL
2595 */
2596xmlParserInputBufferPtr
2597xmlParserInputBufferCreateFile(FILE *file, xmlCharEncoding enc) {
2598 xmlParserInputBufferPtr ret;
2599
2600 if (xmlInputCallbackInitialized == 0)
2601 xmlRegisterDefaultInputCallbacks();
2602
2603 if (file == NULL) return(NULL);
2604
2605 ret = xmlAllocParserInputBuffer(enc);
2606 if (ret != NULL) {
2607 ret->context = file;
2608 ret->readcallback = xmlFileRead;
2609 ret->closecallback = xmlFileFlush;
2610 }
2611
2612 return(ret);
2613}
2614
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002615#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002616/**
2617 * xmlOutputBufferCreateFile:
2618 * @file: a FILE*
2619 * @encoder: the encoding converter or NULL
2620 *
2621 * Create a buffered output for the progressive saving to a FILE *
2622 * buffered C I/O
2623 *
2624 * Returns the new parser output or NULL
2625 */
2626xmlOutputBufferPtr
2627xmlOutputBufferCreateFile(FILE *file, xmlCharEncodingHandlerPtr encoder) {
2628 xmlOutputBufferPtr ret;
2629
2630 if (xmlOutputCallbackInitialized == 0)
2631 xmlRegisterDefaultOutputCallbacks();
2632
2633 if (file == NULL) return(NULL);
2634
2635 ret = xmlAllocOutputBuffer(encoder);
2636 if (ret != NULL) {
2637 ret->context = file;
2638 ret->writecallback = xmlFileWrite;
2639 ret->closecallback = xmlFileFlush;
2640 }
2641
2642 return(ret);
2643}
Daniel Veillard9a00fd22005-11-09 08:56:26 +00002644
2645/**
2646 * xmlOutputBufferCreateBuffer:
2647 * @buffer: a xmlBufferPtr
2648 * @encoder: the encoding converter or NULL
2649 *
2650 * Create a buffered output for the progressive saving to a xmlBuffer
2651 *
2652 * Returns the new parser output or NULL
2653 */
2654xmlOutputBufferPtr
2655xmlOutputBufferCreateBuffer(xmlBufferPtr buffer,
2656 xmlCharEncodingHandlerPtr encoder) {
2657 xmlOutputBufferPtr ret;
2658
2659 if (buffer == NULL) return(NULL);
2660
Rob Richardsa44f2342005-11-09 18:03:45 +00002661 ret = xmlOutputBufferCreateIO((xmlOutputWriteCallback)
2662 xmlBufferWrite,
2663 (xmlOutputCloseCallback)
Daniel Veillard4d3866c2005-11-13 12:43:59 +00002664 NULL, (void *) buffer, encoder);
Daniel Veillard9a00fd22005-11-09 08:56:26 +00002665
2666 return(ret);
2667}
2668
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002669#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002670
2671/**
2672 * xmlParserInputBufferCreateFd:
2673 * @fd: a file descriptor number
2674 * @enc: the charset encoding if known
2675 *
2676 * Create a buffered parser input for the progressive parsing for the input
2677 * from a file descriptor
2678 *
2679 * Returns the new parser input or NULL
2680 */
2681xmlParserInputBufferPtr
2682xmlParserInputBufferCreateFd(int fd, xmlCharEncoding enc) {
2683 xmlParserInputBufferPtr ret;
2684
2685 if (fd < 0) return(NULL);
2686
2687 ret = xmlAllocParserInputBuffer(enc);
2688 if (ret != NULL) {
Daniel Veillard8fcc4942001-07-17 20:07:33 +00002689 ret->context = (void *) (long) fd;
Owen Taylor3473f882001-02-23 17:55:21 +00002690 ret->readcallback = xmlFdRead;
2691 ret->closecallback = xmlFdClose;
2692 }
2693
2694 return(ret);
2695}
2696
2697/**
2698 * xmlParserInputBufferCreateMem:
2699 * @mem: the memory input
2700 * @size: the length of the memory block
2701 * @enc: the charset encoding if known
2702 *
2703 * Create a buffered parser input for the progressive parsing for the input
2704 * from a memory area.
2705 *
2706 * Returns the new parser input or NULL
2707 */
2708xmlParserInputBufferPtr
2709xmlParserInputBufferCreateMem(const char *mem, int size, xmlCharEncoding enc) {
2710 xmlParserInputBufferPtr ret;
William M. Bracka3215c72004-07-31 16:24:01 +00002711 int errcode;
Owen Taylor3473f882001-02-23 17:55:21 +00002712
2713 if (size <= 0) return(NULL);
2714 if (mem == NULL) return(NULL);
2715
2716 ret = xmlAllocParserInputBuffer(enc);
2717 if (ret != NULL) {
2718 ret->context = (void *) mem;
2719 ret->readcallback = (xmlInputReadCallback) xmlNop;
2720 ret->closecallback = NULL;
William M. Bracka3215c72004-07-31 16:24:01 +00002721 errcode = xmlBufferAdd(ret->buffer, (const xmlChar *) mem, size);
2722 if (errcode != 0) {
2723 xmlFree(ret);
2724 return(NULL);
2725 }
Owen Taylor3473f882001-02-23 17:55:21 +00002726 }
2727
2728 return(ret);
2729}
2730
2731/**
Daniel Veillard53350552003-09-18 13:35:51 +00002732 * xmlParserInputBufferCreateStatic:
2733 * @mem: the memory input
2734 * @size: the length of the memory block
2735 * @enc: the charset encoding if known
2736 *
2737 * Create a buffered parser input for the progressive parsing for the input
2738 * from an immutable memory area. This will not copy the memory area to
2739 * the buffer, but the memory is expected to be available until the end of
2740 * the parsing, this is useful for example when using mmap'ed file.
2741 *
2742 * Returns the new parser input or NULL
2743 */
2744xmlParserInputBufferPtr
2745xmlParserInputBufferCreateStatic(const char *mem, int size,
2746 xmlCharEncoding enc) {
2747 xmlParserInputBufferPtr ret;
2748
2749 if (size <= 0) return(NULL);
2750 if (mem == NULL) return(NULL);
2751
2752 ret = (xmlParserInputBufferPtr) xmlMalloc(sizeof(xmlParserInputBuffer));
2753 if (ret == NULL) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00002754 xmlIOErrMemory("creating input buffer");
Daniel Veillard53350552003-09-18 13:35:51 +00002755 return(NULL);
2756 }
2757 memset(ret, 0, (size_t) sizeof(xmlParserInputBuffer));
Daniel Veillarde72c5082003-09-19 12:44:05 +00002758 ret->buffer = xmlBufferCreateStatic((void *)mem, (size_t) size);
Daniel Veillard53350552003-09-18 13:35:51 +00002759 if (ret->buffer == NULL) {
2760 xmlFree(ret);
2761 return(NULL);
2762 }
2763 ret->encoder = xmlGetCharEncodingHandler(enc);
2764 if (ret->encoder != NULL)
2765 ret->raw = xmlBufferCreateSize(2 * xmlDefaultBufferSize);
2766 else
2767 ret->raw = NULL;
2768 ret->compressed = -1;
2769 ret->context = (void *) mem;
2770 ret->readcallback = NULL;
2771 ret->closecallback = NULL;
2772
2773 return(ret);
2774}
2775
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002776#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard53350552003-09-18 13:35:51 +00002777/**
Owen Taylor3473f882001-02-23 17:55:21 +00002778 * xmlOutputBufferCreateFd:
2779 * @fd: a file descriptor number
2780 * @encoder: the encoding converter or NULL
2781 *
2782 * Create a buffered output for the progressive saving
2783 * to a file descriptor
2784 *
2785 * Returns the new parser output or NULL
2786 */
2787xmlOutputBufferPtr
2788xmlOutputBufferCreateFd(int fd, xmlCharEncodingHandlerPtr encoder) {
2789 xmlOutputBufferPtr ret;
2790
2791 if (fd < 0) return(NULL);
2792
2793 ret = xmlAllocOutputBuffer(encoder);
2794 if (ret != NULL) {
Daniel Veillard8fcc4942001-07-17 20:07:33 +00002795 ret->context = (void *) (long) fd;
Owen Taylor3473f882001-02-23 17:55:21 +00002796 ret->writecallback = xmlFdWrite;
Daniel Veillard7db38712002-02-07 16:39:11 +00002797 ret->closecallback = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002798 }
2799
2800 return(ret);
2801}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002802#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002803
2804/**
2805 * xmlParserInputBufferCreateIO:
2806 * @ioread: an I/O read function
2807 * @ioclose: an I/O close function
2808 * @ioctx: an I/O handler
2809 * @enc: the charset encoding if known
2810 *
2811 * Create a buffered parser input for the progressive parsing for the input
2812 * from an I/O handler
2813 *
2814 * Returns the new parser input or NULL
2815 */
2816xmlParserInputBufferPtr
2817xmlParserInputBufferCreateIO(xmlInputReadCallback ioread,
2818 xmlInputCloseCallback ioclose, void *ioctx, xmlCharEncoding enc) {
2819 xmlParserInputBufferPtr ret;
2820
2821 if (ioread == NULL) return(NULL);
2822
2823 ret = xmlAllocParserInputBuffer(enc);
2824 if (ret != NULL) {
2825 ret->context = (void *) ioctx;
2826 ret->readcallback = ioread;
2827 ret->closecallback = ioclose;
2828 }
2829
2830 return(ret);
2831}
2832
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002833#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00002834/**
2835 * xmlOutputBufferCreateIO:
2836 * @iowrite: an I/O write function
2837 * @ioclose: an I/O close function
2838 * @ioctx: an I/O handler
Daniel Veillard9d06d302002-01-22 18:15:52 +00002839 * @encoder: the charset encoding if known
Owen Taylor3473f882001-02-23 17:55:21 +00002840 *
2841 * Create a buffered output for the progressive saving
2842 * to an I/O handler
2843 *
2844 * Returns the new parser output or NULL
2845 */
2846xmlOutputBufferPtr
2847xmlOutputBufferCreateIO(xmlOutputWriteCallback iowrite,
2848 xmlOutputCloseCallback ioclose, void *ioctx,
2849 xmlCharEncodingHandlerPtr encoder) {
2850 xmlOutputBufferPtr ret;
2851
2852 if (iowrite == NULL) return(NULL);
2853
2854 ret = xmlAllocOutputBuffer(encoder);
2855 if (ret != NULL) {
2856 ret->context = (void *) ioctx;
2857 ret->writecallback = iowrite;
2858 ret->closecallback = ioclose;
2859 }
2860
2861 return(ret);
2862}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00002863#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00002864
2865/**
Daniel Veillard1b243b42004-06-08 10:16:42 +00002866 * xmlParserInputBufferCreateFilenameDefault:
2867 * @func: function pointer to the new ParserInputBufferCreateFilenameFunc
2868 *
2869 * Registers a callback for URI input file handling
2870 *
2871 * Returns the old value of the registration function
2872 */
2873xmlParserInputBufferCreateFilenameFunc
2874xmlParserInputBufferCreateFilenameDefault(xmlParserInputBufferCreateFilenameFunc func)
2875{
2876 xmlParserInputBufferCreateFilenameFunc old = xmlParserInputBufferCreateFilenameValue;
2877 if (old == NULL) {
2878 old = __xmlParserInputBufferCreateFilename;
2879 }
2880
2881 xmlParserInputBufferCreateFilenameValue = func;
2882 return(old);
2883}
2884
2885/**
2886 * xmlOutputBufferCreateFilenameDefault:
2887 * @func: function pointer to the new OutputBufferCreateFilenameFunc
2888 *
2889 * Registers a callback for URI output file handling
2890 *
2891 * Returns the old value of the registration function
2892 */
2893xmlOutputBufferCreateFilenameFunc
2894xmlOutputBufferCreateFilenameDefault(xmlOutputBufferCreateFilenameFunc func)
2895{
2896 xmlOutputBufferCreateFilenameFunc old = xmlOutputBufferCreateFilenameValue;
2897#ifdef LIBXML_OUTPUT_ENABLED
2898 if (old == NULL) {
2899 old = __xmlOutputBufferCreateFilename;
2900 }
2901#endif
2902 xmlOutputBufferCreateFilenameValue = func;
2903 return(old);
2904}
2905
2906/**
Owen Taylor3473f882001-02-23 17:55:21 +00002907 * xmlParserInputBufferPush:
2908 * @in: a buffered parser input
2909 * @len: the size in bytes of the array.
2910 * @buf: an char array
2911 *
2912 * Push the content of the arry in the input buffer
2913 * This routine handle the I18N transcoding to internal UTF-8
2914 * This is used when operating the parser in progressive (push) mode.
2915 *
2916 * Returns the number of chars read and stored in the buffer, or -1
2917 * in case of error.
2918 */
2919int
2920xmlParserInputBufferPush(xmlParserInputBufferPtr in,
2921 int len, const char *buf) {
2922 int nbchars = 0;
William M. Bracka3215c72004-07-31 16:24:01 +00002923 int ret;
Owen Taylor3473f882001-02-23 17:55:21 +00002924
2925 if (len < 0) return(0);
Daniel Veillard97bf4d02003-10-08 18:58:28 +00002926 if ((in == NULL) || (in->error)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002927 if (in->encoder != NULL) {
Daniel Veillard36711902004-02-11 13:25:26 +00002928 unsigned int use;
2929
Owen Taylor3473f882001-02-23 17:55:21 +00002930 /*
2931 * Store the data in the incoming raw buffer
2932 */
2933 if (in->raw == NULL) {
2934 in->raw = xmlBufferCreate();
2935 }
William M. Bracka3215c72004-07-31 16:24:01 +00002936 ret = xmlBufferAdd(in->raw, (const xmlChar *) buf, len);
2937 if (ret != 0)
2938 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002939
2940 /*
2941 * convert as much as possible to the parser reading buffer.
2942 */
Daniel Veillard36711902004-02-11 13:25:26 +00002943 use = in->raw->use;
Owen Taylor3473f882001-02-23 17:55:21 +00002944 nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
2945 if (nbchars < 0) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00002946 xmlIOErr(XML_IO_ENCODER, NULL);
Daniel Veillard97bf4d02003-10-08 18:58:28 +00002947 in->error = XML_IO_ENCODER;
Owen Taylor3473f882001-02-23 17:55:21 +00002948 return(-1);
2949 }
Daniel Veillard36711902004-02-11 13:25:26 +00002950 in->rawconsumed += (use - in->raw->use);
Owen Taylor3473f882001-02-23 17:55:21 +00002951 } else {
2952 nbchars = len;
William M. Bracka3215c72004-07-31 16:24:01 +00002953 ret = xmlBufferAdd(in->buffer, (xmlChar *) buf, nbchars);
2954 if (ret != 0)
2955 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00002956 }
2957#ifdef DEBUG_INPUT
2958 xmlGenericError(xmlGenericErrorContext,
2959 "I/O: pushed %d chars, buffer %d/%d\n",
2960 nbchars, in->buffer->use, in->buffer->size);
2961#endif
2962 return(nbchars);
2963}
2964
2965/**
Daniel Veillardddffd2a2002-03-05 20:28:20 +00002966 * endOfInput:
2967 *
2968 * When reading from an Input channel indicated end of file or error
2969 * don't reread from it again.
2970 */
2971static int
2972endOfInput (void * context ATTRIBUTE_UNUSED,
2973 char * buffer ATTRIBUTE_UNUSED,
2974 int len ATTRIBUTE_UNUSED) {
2975 return(0);
2976}
2977
2978/**
Owen Taylor3473f882001-02-23 17:55:21 +00002979 * xmlParserInputBufferGrow:
2980 * @in: a buffered parser input
2981 * @len: indicative value of the amount of chars to read
2982 *
2983 * Grow up the content of the input buffer, the old data are preserved
2984 * This routine handle the I18N transcoding to internal UTF-8
2985 * This routine is used when operating the parser in normal (pull) mode
2986 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002987 * TODO: one should be able to remove one extra copy by copying directly
Owen Taylor3473f882001-02-23 17:55:21 +00002988 * onto in->buffer or in->raw
2989 *
2990 * Returns the number of chars read and stored in the buffer, or -1
2991 * in case of error.
2992 */
2993int
2994xmlParserInputBufferGrow(xmlParserInputBufferPtr in, int len) {
2995 char *buffer = NULL;
2996 int res = 0;
2997 int nbchars = 0;
2998 int buffree;
Daniel Veillard9e412302002-06-10 15:59:44 +00002999 unsigned int needSize;
Owen Taylor3473f882001-02-23 17:55:21 +00003000
Daniel Veillard97bf4d02003-10-08 18:58:28 +00003001 if ((in == NULL) || (in->error)) return(-1);
Daniel Veillard6155d8a2003-08-19 15:01:28 +00003002 if ((len <= MINLEN) && (len != 4))
Owen Taylor3473f882001-02-23 17:55:21 +00003003 len = MINLEN;
Daniel Veillard6155d8a2003-08-19 15:01:28 +00003004
Owen Taylor3473f882001-02-23 17:55:21 +00003005 buffree = in->buffer->size - in->buffer->use;
3006 if (buffree <= 0) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00003007 xmlIOErr(XML_IO_BUFFER_FULL, NULL);
Daniel Veillard97bf4d02003-10-08 18:58:28 +00003008 in->error = XML_IO_BUFFER_FULL;
William M. Bracka3215c72004-07-31 16:24:01 +00003009 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003010 }
Owen Taylor3473f882001-02-23 17:55:21 +00003011
Daniel Veillarde5354492002-05-16 08:43:22 +00003012 needSize = in->buffer->use + len + 1;
3013 if (needSize > in->buffer->size){
3014 if (!xmlBufferResize(in->buffer, needSize)){
Daniel Veillard05d987b2003-10-08 11:54:57 +00003015 xmlIOErrMemory("growing input buffer");
Daniel Veillard97bf4d02003-10-08 18:58:28 +00003016 in->error = XML_ERR_NO_MEMORY;
William M. Bracka3215c72004-07-31 16:24:01 +00003017 return(-1);
Daniel Veillarde5354492002-05-16 08:43:22 +00003018 }
Owen Taylor3473f882001-02-23 17:55:21 +00003019 }
Daniel Veillarde5354492002-05-16 08:43:22 +00003020 buffer = (char *)&in->buffer->content[in->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00003021
3022 /*
3023 * Call the read method for this I/O type.
3024 */
3025 if (in->readcallback != NULL) {
3026 res = in->readcallback(in->context, &buffer[0], len);
Daniel Veillardddffd2a2002-03-05 20:28:20 +00003027 if (res <= 0)
3028 in->readcallback = endOfInput;
Owen Taylor3473f882001-02-23 17:55:21 +00003029 } else {
Daniel Veillard05d987b2003-10-08 11:54:57 +00003030 xmlIOErr(XML_IO_NO_INPUT, NULL);
Daniel Veillard97bf4d02003-10-08 18:58:28 +00003031 in->error = XML_IO_NO_INPUT;
Owen Taylor3473f882001-02-23 17:55:21 +00003032 return(-1);
3033 }
3034 if (res < 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00003035 return(-1);
3036 }
3037 len = res;
3038 if (in->encoder != NULL) {
Daniel Veillard36711902004-02-11 13:25:26 +00003039 unsigned int use;
3040
Owen Taylor3473f882001-02-23 17:55:21 +00003041 /*
3042 * Store the data in the incoming raw buffer
3043 */
3044 if (in->raw == NULL) {
3045 in->raw = xmlBufferCreate();
3046 }
William M. Bracka3215c72004-07-31 16:24:01 +00003047 res = xmlBufferAdd(in->raw, (const xmlChar *) buffer, len);
3048 if (res != 0)
3049 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003050
3051 /*
3052 * convert as much as possible to the parser reading buffer.
3053 */
Daniel Veillard36711902004-02-11 13:25:26 +00003054 use = in->raw->use;
Owen Taylor3473f882001-02-23 17:55:21 +00003055 nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
3056 if (nbchars < 0) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00003057 xmlIOErr(XML_IO_ENCODER, NULL);
Daniel Veillard97bf4d02003-10-08 18:58:28 +00003058 in->error = XML_IO_ENCODER;
Owen Taylor3473f882001-02-23 17:55:21 +00003059 return(-1);
3060 }
Daniel Veillard36711902004-02-11 13:25:26 +00003061 in->rawconsumed += (use - in->raw->use);
Owen Taylor3473f882001-02-23 17:55:21 +00003062 } else {
3063 nbchars = len;
Daniel Veillarde5354492002-05-16 08:43:22 +00003064 in->buffer->use += nbchars;
3065 buffer[nbchars] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00003066 }
3067#ifdef DEBUG_INPUT
3068 xmlGenericError(xmlGenericErrorContext,
3069 "I/O: read %d chars, buffer %d/%d\n",
3070 nbchars, in->buffer->use, in->buffer->size);
3071#endif
Owen Taylor3473f882001-02-23 17:55:21 +00003072 return(nbchars);
3073}
3074
3075/**
3076 * xmlParserInputBufferRead:
3077 * @in: a buffered parser input
3078 * @len: indicative value of the amount of chars to read
3079 *
3080 * Refresh the content of the input buffer, the old data are considered
3081 * consumed
3082 * This routine handle the I18N transcoding to internal UTF-8
3083 *
3084 * Returns the number of chars read and stored in the buffer, or -1
3085 * in case of error.
3086 */
3087int
3088xmlParserInputBufferRead(xmlParserInputBufferPtr in, int len) {
Daniel Veillard97bf4d02003-10-08 18:58:28 +00003089 if ((in == NULL) || (in->error)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003090 if (in->readcallback != NULL)
3091 return(xmlParserInputBufferGrow(in, len));
Daniel Veillard53350552003-09-18 13:35:51 +00003092 else if ((in->buffer != NULL) &&
3093 (in->buffer->alloc == XML_BUFFER_ALLOC_IMMUTABLE))
3094 return(0);
Owen Taylor3473f882001-02-23 17:55:21 +00003095 else
3096 return(-1);
3097}
3098
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003099#ifdef LIBXML_OUTPUT_ENABLED
Owen Taylor3473f882001-02-23 17:55:21 +00003100/**
3101 * xmlOutputBufferWrite:
3102 * @out: a buffered parser output
3103 * @len: the size in bytes of the array.
3104 * @buf: an char array
3105 *
3106 * Write the content of the array in the output I/O buffer
3107 * This routine handle the I18N transcoding from internal UTF-8
3108 * The buffer is lossless, i.e. will store in case of partial
3109 * or delayed writes.
3110 *
3111 * Returns the number of chars immediately written, or -1
3112 * in case of error.
3113 */
3114int
3115xmlOutputBufferWrite(xmlOutputBufferPtr out, int len, const char *buf) {
3116 int nbchars = 0; /* number of chars to output to I/O */
3117 int ret; /* return from function call */
3118 int written = 0; /* number of char written to I/O so far */
3119 int chunk; /* number of byte curreent processed from buf */
3120
Daniel Veillard97bf4d02003-10-08 18:58:28 +00003121 if ((out == NULL) || (out->error)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003122 if (len < 0) return(0);
Daniel Veillard97bf4d02003-10-08 18:58:28 +00003123 if (out->error) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003124
3125 do {
3126 chunk = len;
3127 if (chunk > 4 * MINLEN)
3128 chunk = 4 * MINLEN;
3129
3130 /*
3131 * first handle encoding stuff.
3132 */
3133 if (out->encoder != NULL) {
3134 /*
3135 * Store the data in the incoming raw buffer
3136 */
3137 if (out->conv == NULL) {
3138 out->conv = xmlBufferCreate();
3139 }
William M. Bracka3215c72004-07-31 16:24:01 +00003140 ret = xmlBufferAdd(out->buffer, (const xmlChar *) buf, chunk);
3141 if (ret != 0)
3142 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003143
3144 if ((out->buffer->use < MINLEN) && (chunk == len))
3145 goto done;
3146
3147 /*
3148 * convert as much as possible to the parser reading buffer.
3149 */
3150 ret = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer);
Daniel Veillard809faa52003-02-10 15:43:53 +00003151 if ((ret < 0) && (ret != -3)) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00003152 xmlIOErr(XML_IO_ENCODER, NULL);
Daniel Veillard97bf4d02003-10-08 18:58:28 +00003153 out->error = XML_IO_ENCODER;
Owen Taylor3473f882001-02-23 17:55:21 +00003154 return(-1);
3155 }
3156 nbchars = out->conv->use;
3157 } else {
William M. Bracka3215c72004-07-31 16:24:01 +00003158 ret = xmlBufferAdd(out->buffer, (const xmlChar *) buf, chunk);
3159 if (ret != 0)
3160 return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003161 nbchars = out->buffer->use;
3162 }
3163 buf += chunk;
3164 len -= chunk;
3165
3166 if ((nbchars < MINLEN) && (len <= 0))
3167 goto done;
3168
3169 if (out->writecallback) {
3170 /*
3171 * second write the stuff to the I/O channel
3172 */
3173 if (out->encoder != NULL) {
3174 ret = out->writecallback(out->context,
3175 (const char *)out->conv->content, nbchars);
3176 if (ret >= 0)
Daniel Veillardedddff92001-05-02 10:58:52 +00003177 xmlBufferShrink(out->conv, ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003178 } else {
3179 ret = out->writecallback(out->context,
3180 (const char *)out->buffer->content, nbchars);
3181 if (ret >= 0)
Daniel Veillardedddff92001-05-02 10:58:52 +00003182 xmlBufferShrink(out->buffer, ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003183 }
3184 if (ret < 0) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00003185 xmlIOErr(XML_IO_WRITE, NULL);
Daniel Veillard97bf4d02003-10-08 18:58:28 +00003186 out->error = XML_IO_WRITE;
Owen Taylor3473f882001-02-23 17:55:21 +00003187 return(ret);
3188 }
3189 out->written += ret;
3190 }
3191 written += nbchars;
3192 } while (len > 0);
3193
3194done:
3195#ifdef DEBUG_INPUT
3196 xmlGenericError(xmlGenericErrorContext,
3197 "I/O: wrote %d chars\n", written);
3198#endif
3199 return(written);
3200}
3201
3202/**
Daniel Veillard5d1a4d82004-05-13 14:31:25 +00003203 * xmlEscapeContent:
3204 * @out: a pointer to an array of bytes to store the result
3205 * @outlen: the length of @out
3206 * @in: a pointer to an array of unescaped UTF-8 bytes
3207 * @inlen: the length of @in
3208 *
3209 * Take a block of UTF-8 chars in and escape them.
3210 * Returns 0 if success, or -1 otherwise
3211 * The value of @inlen after return is the number of octets consumed
3212 * if the return value is positive, else unpredictable.
3213 * The value of @outlen after return is the number of octets consumed.
3214 */
3215static int
3216xmlEscapeContent(unsigned char* out, int *outlen,
3217 const xmlChar* in, int *inlen) {
3218 unsigned char* outstart = out;
3219 const unsigned char* base = in;
3220 unsigned char* outend = out + *outlen;
3221 const unsigned char* inend;
3222
3223 inend = in + (*inlen);
3224
3225 while ((in < inend) && (out < outend)) {
3226 if (*in == '<') {
3227 if (outend - out < 4) break;
3228 *out++ = '&';
3229 *out++ = 'l';
3230 *out++ = 't';
3231 *out++ = ';';
3232 } else if (*in == '>') {
3233 if (outend - out < 4) break;
3234 *out++ = '&';
3235 *out++ = 'g';
3236 *out++ = 't';
3237 *out++ = ';';
3238 } else if (*in == '&') {
3239 if (outend - out < 5) break;
3240 *out++ = '&';
3241 *out++ = 'a';
3242 *out++ = 'm';
3243 *out++ = 'p';
3244 *out++ = ';';
3245 } else if (*in == '\r') {
3246 if (outend - out < 5) break;
3247 *out++ = '&';
3248 *out++ = '#';
3249 *out++ = '1';
3250 *out++ = '3';
3251 *out++ = ';';
3252 } else {
3253 *out++ = (unsigned char) *in;
3254 }
3255 ++in;
3256 }
3257 *outlen = out - outstart;
3258 *inlen = in - base;
3259 return(0);
3260}
3261
3262/**
3263 * xmlOutputBufferWriteEscape:
3264 * @out: a buffered parser output
3265 * @str: a zero terminated UTF-8 string
Daniel Veillardee8960b2004-05-14 03:25:14 +00003266 * @escaping: an optional escaping function (or NULL)
Daniel Veillard5d1a4d82004-05-13 14:31:25 +00003267 *
3268 * Write the content of the string in the output I/O buffer
3269 * This routine escapes the caracters and then handle the I18N
3270 * transcoding from internal UTF-8
3271 * The buffer is lossless, i.e. will store in case of partial
3272 * or delayed writes.
3273 *
3274 * Returns the number of chars immediately written, or -1
3275 * in case of error.
3276 */
3277int
Daniel Veillardee8960b2004-05-14 03:25:14 +00003278xmlOutputBufferWriteEscape(xmlOutputBufferPtr out, const xmlChar *str,
3279 xmlCharEncodingOutputFunc escaping) {
Daniel Veillard5d1a4d82004-05-13 14:31:25 +00003280 int nbchars = 0; /* number of chars to output to I/O */
3281 int ret; /* return from function call */
3282 int written = 0; /* number of char written to I/O so far */
Daniel Veillard71b95632004-08-31 12:15:36 +00003283 int oldwritten=0;/* loop guard */
Daniel Veillard5d1a4d82004-05-13 14:31:25 +00003284 int chunk; /* number of byte currently processed from str */
3285 int len; /* number of bytes in str */
3286 int cons; /* byte from str consumed */
3287
Daniel Veillardce244ad2004-11-05 10:03:46 +00003288 if ((out == NULL) || (out->error) || (str == NULL) ||
3289 (out->buffer == NULL) ||
3290 (out->buffer->alloc == XML_BUFFER_ALLOC_IMMUTABLE)) return(-1);
Daniel Veillardee8960b2004-05-14 03:25:14 +00003291 len = strlen((const char *)str);
Daniel Veillard5d1a4d82004-05-13 14:31:25 +00003292 if (len < 0) return(0);
3293 if (out->error) return(-1);
Daniel Veillardee8960b2004-05-14 03:25:14 +00003294 if (escaping == NULL) escaping = xmlEscapeContent;
Daniel Veillard5d1a4d82004-05-13 14:31:25 +00003295
3296 do {
Daniel Veillard71b95632004-08-31 12:15:36 +00003297 oldwritten = written;
3298
Daniel Veillard5d1a4d82004-05-13 14:31:25 +00003299 /*
3300 * how many bytes to consume and how many bytes to store.
3301 */
3302 cons = len;
3303 chunk = (out->buffer->size - out->buffer->use) - 1;
3304
3305 /*
3306 * first handle encoding stuff.
3307 */
3308 if (out->encoder != NULL) {
3309 /*
3310 * Store the data in the incoming raw buffer
3311 */
3312 if (out->conv == NULL) {
3313 out->conv = xmlBufferCreate();
3314 }
Daniel Veillardee8960b2004-05-14 03:25:14 +00003315 ret = escaping(out->buffer->content + out->buffer->use ,
3316 &chunk, str, &cons);
William M. Brack66e40b12004-11-26 15:45:19 +00003317 if ((ret < 0) || (chunk == 0)) /* chunk==0 => nothing done */
Daniel Veillard5d1a4d82004-05-13 14:31:25 +00003318 return(-1);
3319 out->buffer->use += chunk;
3320 out->buffer->content[out->buffer->use] = 0;
3321
3322 if ((out->buffer->use < MINLEN) && (cons == len))
3323 goto done;
3324
3325 /*
3326 * convert as much as possible to the output buffer.
3327 */
3328 ret = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer);
3329 if ((ret < 0) && (ret != -3)) {
3330 xmlIOErr(XML_IO_ENCODER, NULL);
3331 out->error = XML_IO_ENCODER;
3332 return(-1);
3333 }
3334 nbchars = out->conv->use;
3335 } else {
Daniel Veillardee8960b2004-05-14 03:25:14 +00003336 ret = escaping(out->buffer->content + out->buffer->use ,
3337 &chunk, str, &cons);
William M. Brack66e40b12004-11-26 15:45:19 +00003338 if ((ret < 0) || (chunk == 0)) /* chunk==0 => nothing done */
Daniel Veillard5d1a4d82004-05-13 14:31:25 +00003339 return(-1);
3340 out->buffer->use += chunk;
3341 out->buffer->content[out->buffer->use] = 0;
3342 nbchars = out->buffer->use;
3343 }
3344 str += cons;
3345 len -= cons;
3346
3347 if ((nbchars < MINLEN) && (len <= 0))
3348 goto done;
3349
3350 if (out->writecallback) {
3351 /*
3352 * second write the stuff to the I/O channel
3353 */
3354 if (out->encoder != NULL) {
3355 ret = out->writecallback(out->context,
3356 (const char *)out->conv->content, nbchars);
3357 if (ret >= 0)
3358 xmlBufferShrink(out->conv, ret);
3359 } else {
3360 ret = out->writecallback(out->context,
3361 (const char *)out->buffer->content, nbchars);
3362 if (ret >= 0)
3363 xmlBufferShrink(out->buffer, ret);
3364 }
3365 if (ret < 0) {
3366 xmlIOErr(XML_IO_WRITE, NULL);
3367 out->error = XML_IO_WRITE;
3368 return(ret);
3369 }
3370 out->written += ret;
Daniel Veillard83a75e02004-05-14 21:50:42 +00003371 } else if (out->buffer->size - out->buffer->use < MINLEN) {
3372 xmlBufferResize(out->buffer, out->buffer->size + MINLEN);
Daniel Veillard5d1a4d82004-05-13 14:31:25 +00003373 }
3374 written += nbchars;
Daniel Veillard71b95632004-08-31 12:15:36 +00003375 } while ((len > 0) && (oldwritten != written));
Daniel Veillard5d1a4d82004-05-13 14:31:25 +00003376
3377done:
3378#ifdef DEBUG_INPUT
3379 xmlGenericError(xmlGenericErrorContext,
3380 "I/O: wrote %d chars\n", written);
3381#endif
3382 return(written);
3383}
3384
3385/**
Owen Taylor3473f882001-02-23 17:55:21 +00003386 * xmlOutputBufferWriteString:
3387 * @out: a buffered parser output
3388 * @str: a zero terminated C string
3389 *
3390 * Write the content of the string in the output I/O buffer
3391 * This routine handle the I18N transcoding from internal UTF-8
3392 * The buffer is lossless, i.e. will store in case of partial
3393 * or delayed writes.
3394 *
3395 * Returns the number of chars immediately written, or -1
3396 * in case of error.
3397 */
3398int
3399xmlOutputBufferWriteString(xmlOutputBufferPtr out, const char *str) {
3400 int len;
3401
Daniel Veillard97bf4d02003-10-08 18:58:28 +00003402 if ((out == NULL) || (out->error)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003403 if (str == NULL)
3404 return(-1);
3405 len = strlen(str);
3406
3407 if (len > 0)
3408 return(xmlOutputBufferWrite(out, len, str));
3409 return(len);
3410}
3411
3412/**
3413 * xmlOutputBufferFlush:
3414 * @out: a buffered output
3415 *
3416 * flushes the output I/O channel
3417 *
3418 * Returns the number of byte written or -1 in case of error.
3419 */
3420int
3421xmlOutputBufferFlush(xmlOutputBufferPtr out) {
3422 int nbchars = 0, ret = 0;
3423
Daniel Veillard97bf4d02003-10-08 18:58:28 +00003424 if ((out == NULL) || (out->error)) return(-1);
Owen Taylor3473f882001-02-23 17:55:21 +00003425 /*
3426 * first handle encoding stuff.
3427 */
3428 if ((out->conv != NULL) && (out->encoder != NULL)) {
3429 /*
3430 * convert as much as possible to the parser reading buffer.
3431 */
3432 nbchars = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer);
3433 if (nbchars < 0) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00003434 xmlIOErr(XML_IO_ENCODER, NULL);
Daniel Veillard97bf4d02003-10-08 18:58:28 +00003435 out->error = XML_IO_ENCODER;
Owen Taylor3473f882001-02-23 17:55:21 +00003436 return(-1);
3437 }
3438 }
3439
3440 /*
3441 * second flush the stuff to the I/O channel
3442 */
3443 if ((out->conv != NULL) && (out->encoder != NULL) &&
3444 (out->writecallback != NULL)) {
3445 ret = out->writecallback(out->context,
3446 (const char *)out->conv->content, out->conv->use);
3447 if (ret >= 0)
3448 xmlBufferShrink(out->conv, ret);
3449 } else if (out->writecallback != NULL) {
3450 ret = out->writecallback(out->context,
3451 (const char *)out->buffer->content, out->buffer->use);
3452 if (ret >= 0)
3453 xmlBufferShrink(out->buffer, ret);
3454 }
3455 if (ret < 0) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00003456 xmlIOErr(XML_IO_FLUSH, NULL);
Daniel Veillard97bf4d02003-10-08 18:58:28 +00003457 out->error = XML_IO_FLUSH;
Owen Taylor3473f882001-02-23 17:55:21 +00003458 return(ret);
3459 }
3460 out->written += ret;
3461
3462#ifdef DEBUG_INPUT
3463 xmlGenericError(xmlGenericErrorContext,
3464 "I/O: flushed %d chars\n", ret);
3465#endif
3466 return(ret);
3467}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00003468#endif /* LIBXML_OUTPUT_ENABLED */
Owen Taylor3473f882001-02-23 17:55:21 +00003469
Daniel Veillard5e2dace2001-07-18 19:30:27 +00003470/**
Owen Taylor3473f882001-02-23 17:55:21 +00003471 * xmlParserGetDirectory:
3472 * @filename: the path to a file
3473 *
3474 * lookup the directory for that file
3475 *
3476 * Returns a new allocated string containing the directory, or NULL.
3477 */
3478char *
3479xmlParserGetDirectory(const char *filename) {
3480 char *ret = NULL;
3481 char dir[1024];
3482 char *cur;
3483 char sep = '/';
3484
Igor Zlatkovic9181cc02002-09-29 17:51:06 +00003485#ifdef _WIN32_WCE /* easy way by now ... wince does not have dirs! */
3486 return NULL;
3487#endif
3488
Owen Taylor3473f882001-02-23 17:55:21 +00003489 if (xmlInputCallbackInitialized == 0)
3490 xmlRegisterDefaultInputCallbacks();
3491
3492 if (filename == NULL) return(NULL);
Daniel Veillard3c5ed912002-01-08 10:36:16 +00003493#if defined(WIN32) && !defined(__CYGWIN__)
Owen Taylor3473f882001-02-23 17:55:21 +00003494 sep = '\\';
3495#endif
3496
3497 strncpy(dir, filename, 1023);
3498 dir[1023] = 0;
3499 cur = &dir[strlen(dir)];
3500 while (cur > dir) {
3501 if (*cur == sep) break;
3502 cur --;
3503 }
3504 if (*cur == sep) {
3505 if (cur == dir) dir[1] = 0;
3506 else *cur = 0;
3507 ret = xmlMemStrdup(dir);
3508 } else {
3509 if (getcwd(dir, 1024) != NULL) {
3510 dir[1023] = 0;
3511 ret = xmlMemStrdup(dir);
3512 }
3513 }
3514 return(ret);
3515}
3516
3517/****************************************************************
3518 * *
3519 * External entities loading *
3520 * *
3521 ****************************************************************/
3522
Daniel Veillarda840b692003-10-19 13:35:37 +00003523/**
3524 * xmlCheckHTTPInput:
3525 * @ctxt: an XML parser context
3526 * @ret: an XML parser input
3527 *
3528 * Check an input in case it was created from an HTTP stream, in that
3529 * case it will handle encoding and update of the base URL in case of
3530 * redirection. It also checks for HTTP errors in which case the input
3531 * is cleanly freed up and an appropriate error is raised in context
3532 *
3533 * Returns the input or NULL in case of HTTP error.
3534 */
3535xmlParserInputPtr
3536xmlCheckHTTPInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr ret) {
3537#ifdef LIBXML_HTTP_ENABLED
3538 if ((ret != NULL) && (ret->buf != NULL) &&
3539 (ret->buf->readcallback == xmlIOHTTPRead) &&
3540 (ret->buf->context != NULL)) {
3541 const char *encoding;
3542 const char *redir;
3543 const char *mime;
3544 int code;
3545
3546 code = xmlNanoHTTPReturnCode(ret->buf->context);
3547 if (code >= 400) {
3548 /* fatal error */
3549 if (ret->filename != NULL)
Daniel Veillarde8039df2003-10-27 11:25:13 +00003550 __xmlLoaderErr(ctxt, "failed to load HTTP resource \"%s\"\n",
Daniel Veillarda840b692003-10-19 13:35:37 +00003551 (const char *) ret->filename);
3552 else
Daniel Veillarde8039df2003-10-27 11:25:13 +00003553 __xmlLoaderErr(ctxt, "failed to load HTTP resource\n", NULL);
Daniel Veillarda840b692003-10-19 13:35:37 +00003554 xmlFreeInputStream(ret);
3555 ret = NULL;
3556 } else {
3557
3558 mime = xmlNanoHTTPMimeType(ret->buf->context);
3559 if ((xmlStrstr(BAD_CAST mime, BAD_CAST "/xml")) ||
3560 (xmlStrstr(BAD_CAST mime, BAD_CAST "+xml"))) {
3561 encoding = xmlNanoHTTPEncoding(ret->buf->context);
3562 if (encoding != NULL) {
3563 xmlCharEncodingHandlerPtr handler;
3564
3565 handler = xmlFindCharEncodingHandler(encoding);
3566 if (handler != NULL) {
3567 xmlSwitchInputEncoding(ctxt, ret, handler);
3568 } else {
3569 __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING,
3570 "Unknown encoding %s",
3571 BAD_CAST encoding, NULL);
3572 }
3573 if (ret->encoding == NULL)
3574 ret->encoding = xmlStrdup(BAD_CAST encoding);
3575 }
3576#if 0
3577 } else if (xmlStrstr(BAD_CAST mime, BAD_CAST "html")) {
3578#endif
3579 }
3580 redir = xmlNanoHTTPRedir(ret->buf->context);
3581 if (redir != NULL) {
3582 if (ret->filename != NULL)
3583 xmlFree((xmlChar *) ret->filename);
3584 if (ret->directory != NULL) {
3585 xmlFree((xmlChar *) ret->directory);
3586 ret->directory = NULL;
3587 }
3588 ret->filename =
3589 (char *) xmlStrdup((const xmlChar *) redir);
3590 }
3591 }
3592 }
3593#endif
3594 return(ret);
3595}
3596
Daniel Veillarde5a3f372006-08-30 13:11:36 +00003597static int xmlNoNetExists(const char *URL) {
Daniel Veillard6990bf32001-08-23 21:17:48 +00003598 const char *path;
3599
3600 if (URL == NULL)
3601 return(0);
3602
Daniel Veillardf4862f02002-09-10 11:13:43 +00003603 if (!xmlStrncasecmp(BAD_CAST URL, BAD_CAST "file://localhost/", 17))
Daniel Veillard1997c3e2003-07-05 20:43:43 +00003604#if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
Daniel Veillardf4862f02002-09-10 11:13:43 +00003605 path = &URL[17];
3606#else
Daniel Veillard6990bf32001-08-23 21:17:48 +00003607 path = &URL[16];
Daniel Veillardf4862f02002-09-10 11:13:43 +00003608#endif
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00003609 else if (!xmlStrncasecmp(BAD_CAST URL, BAD_CAST "file:///", 8)) {
Daniel Veillard1997c3e2003-07-05 20:43:43 +00003610#if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__)
Daniel Veillard6990bf32001-08-23 21:17:48 +00003611 path = &URL[8];
3612#else
3613 path = &URL[7];
3614#endif
3615 } else
3616 path = URL;
Daniel Veillarde5a3f372006-08-30 13:11:36 +00003617
3618 return xmlCheckFilename(path);
Daniel Veillard6990bf32001-08-23 21:17:48 +00003619}
Daniel Veillard6990bf32001-08-23 21:17:48 +00003620
Daniel Veillardad4e2962006-09-21 08:36:38 +00003621#ifdef LIBXML_CATALOG_ENABLED
3622
3623/**
3624 * xmlResolveResourceFromCatalog:
3625 * @URL: the URL for the entity to load
3626 * @ID: the System ID for the entity to load
3627 * @ctxt: the context in which the entity is called or NULL
3628 *
3629 * Resolves the URL and ID against the appropriate catalog.
3630 * This function is used by xmlDefaultExternalEntityLoader and
3631 * xmlNoNetExternalEntityLoader.
3632 *
3633 * Returns a new allocated URL, or NULL.
3634 */
3635xmlChar *
3636xmlResolveResourceFromCatalog(const char *URL, const char *ID,
3637 xmlParserCtxtPtr ctxt) {
3638 xmlChar *resource = NULL;
3639 xmlCatalogAllow pref;
3640
3641 /*
3642 * If the resource doesn't exists as a file,
3643 * try to load it from the resource pointed in the catalogs
3644 */
3645 pref = xmlCatalogGetDefaults();
3646
3647 if ((pref != XML_CATA_ALLOW_NONE) && (!xmlNoNetExists(URL))) {
3648 /*
3649 * Do a local lookup
3650 */
3651 if ((ctxt != NULL) && (ctxt->catalogs != NULL) &&
3652 ((pref == XML_CATA_ALLOW_ALL) ||
3653 (pref == XML_CATA_ALLOW_DOCUMENT))) {
3654 resource = xmlCatalogLocalResolve(ctxt->catalogs,
3655 (const xmlChar *)ID,
3656 (const xmlChar *)URL);
3657 }
3658 /*
3659 * Try a global lookup
3660 */
3661 if ((resource == NULL) &&
3662 ((pref == XML_CATA_ALLOW_ALL) ||
3663 (pref == XML_CATA_ALLOW_GLOBAL))) {
3664 resource = xmlCatalogResolve((const xmlChar *)ID,
3665 (const xmlChar *)URL);
3666 }
3667 if ((resource == NULL) && (URL != NULL))
3668 resource = xmlStrdup((const xmlChar *) URL);
3669
3670 /*
3671 * TODO: do an URI lookup on the reference
3672 */
3673 if ((resource != NULL) && (!xmlNoNetExists((const char *)resource))) {
3674 xmlChar *tmp = NULL;
3675
3676 if ((ctxt != NULL) && (ctxt->catalogs != NULL) &&
3677 ((pref == XML_CATA_ALLOW_ALL) ||
3678 (pref == XML_CATA_ALLOW_DOCUMENT))) {
3679 tmp = xmlCatalogLocalResolveURI(ctxt->catalogs, resource);
3680 }
3681 if ((tmp == NULL) &&
3682 ((pref == XML_CATA_ALLOW_ALL) ||
3683 (pref == XML_CATA_ALLOW_GLOBAL))) {
3684 tmp = xmlCatalogResolveURI(resource);
3685 }
3686
3687 if (tmp != NULL) {
3688 xmlFree(resource);
3689 resource = tmp;
3690 }
3691 }
3692 }
3693
3694 return resource;
3695}
3696
3697#endif
3698
Daniel Veillard5e2dace2001-07-18 19:30:27 +00003699/**
Owen Taylor3473f882001-02-23 17:55:21 +00003700 * xmlDefaultExternalEntityLoader:
3701 * @URL: the URL for the entity to load
3702 * @ID: the System ID for the entity to load
3703 * @ctxt: the context in which the entity is called or NULL
3704 *
3705 * By default we don't load external entitites, yet.
3706 *
3707 * Returns a new allocated xmlParserInputPtr, or NULL.
3708 */
Daniel Veillarda840b692003-10-19 13:35:37 +00003709static xmlParserInputPtr
Owen Taylor3473f882001-02-23 17:55:21 +00003710xmlDefaultExternalEntityLoader(const char *URL, const char *ID,
Daniel Veillarda840b692003-10-19 13:35:37 +00003711 xmlParserCtxtPtr ctxt)
3712{
Owen Taylor3473f882001-02-23 17:55:21 +00003713 xmlParserInputPtr ret = NULL;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00003714 xmlChar *resource = NULL;
Daniel Veillarda840b692003-10-19 13:35:37 +00003715
Owen Taylor3473f882001-02-23 17:55:21 +00003716#ifdef DEBUG_EXTERNAL_ENTITIES
3717 xmlGenericError(xmlGenericErrorContext,
Daniel Veillarda840b692003-10-19 13:35:37 +00003718 "xmlDefaultExternalEntityLoader(%s, xxx)\n", URL);
Owen Taylor3473f882001-02-23 17:55:21 +00003719#endif
Daniel Veillard61b93382003-11-03 14:28:31 +00003720 if ((ctxt != NULL) && (ctxt->options & XML_PARSE_NONET)) {
3721 int options = ctxt->options;
3722
3723 ctxt->options -= XML_PARSE_NONET;
3724 ret = xmlNoNetExternalEntityLoader(URL, ID, ctxt);
3725 ctxt->options = options;
3726 return(ret);
3727 }
Daniel Veillardad4e2962006-09-21 08:36:38 +00003728#ifdef LIBXML_CATALOG_ENABLED
3729 resource = xmlResolveResourceFromCatalog(URL, ID, ctxt);
Daniel Veillard7d6fd212001-05-10 15:34:11 +00003730#endif
3731
3732 if (resource == NULL)
Daniel Veillarda840b692003-10-19 13:35:37 +00003733 resource = (xmlChar *) URL;
Daniel Veillard7d6fd212001-05-10 15:34:11 +00003734
3735 if (resource == NULL) {
Daniel Veillarda840b692003-10-19 13:35:37 +00003736 if (ID == NULL)
3737 ID = "NULL";
Daniel Veillarde8039df2003-10-27 11:25:13 +00003738 __xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n", ID);
Daniel Veillarda840b692003-10-19 13:35:37 +00003739 return (NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00003740 }
Daniel Veillarda840b692003-10-19 13:35:37 +00003741 ret = xmlNewInputFromFile(ctxt, (const char *) resource);
Daniel Veillarddc2cee22001-08-22 16:30:37 +00003742 if ((resource != NULL) && (resource != (xmlChar *) URL))
Daniel Veillarda840b692003-10-19 13:35:37 +00003743 xmlFree(resource);
3744 return (ret);
Owen Taylor3473f882001-02-23 17:55:21 +00003745}
3746
3747static xmlExternalEntityLoader xmlCurrentExternalEntityLoader =
3748 xmlDefaultExternalEntityLoader;
3749
Daniel Veillard5e2dace2001-07-18 19:30:27 +00003750/**
Owen Taylor3473f882001-02-23 17:55:21 +00003751 * xmlSetExternalEntityLoader:
3752 * @f: the new entity resolver function
3753 *
3754 * Changes the defaultexternal entity resolver function for the application
3755 */
3756void
3757xmlSetExternalEntityLoader(xmlExternalEntityLoader f) {
3758 xmlCurrentExternalEntityLoader = f;
3759}
3760
Daniel Veillard5e2dace2001-07-18 19:30:27 +00003761/**
Owen Taylor3473f882001-02-23 17:55:21 +00003762 * xmlGetExternalEntityLoader:
3763 *
3764 * Get the default external entity resolver function for the application
3765 *
3766 * Returns the xmlExternalEntityLoader function pointer
3767 */
3768xmlExternalEntityLoader
3769xmlGetExternalEntityLoader(void) {
3770 return(xmlCurrentExternalEntityLoader);
3771}
3772
Daniel Veillard5e2dace2001-07-18 19:30:27 +00003773/**
Owen Taylor3473f882001-02-23 17:55:21 +00003774 * xmlLoadExternalEntity:
3775 * @URL: the URL for the entity to load
Daniel Veillard9f7b84b2001-08-23 15:31:19 +00003776 * @ID: the Public ID for the entity to load
Owen Taylor3473f882001-02-23 17:55:21 +00003777 * @ctxt: the context in which the entity is called or NULL
3778 *
3779 * Load an external entity, note that the use of this function for
3780 * unparsed entities may generate problems
Owen Taylor3473f882001-02-23 17:55:21 +00003781 *
3782 * Returns the xmlParserInputPtr or NULL
3783 */
3784xmlParserInputPtr
3785xmlLoadExternalEntity(const char *URL, const char *ID,
3786 xmlParserCtxtPtr ctxt) {
Daniel Veillarde5a3f372006-08-30 13:11:36 +00003787 if ((URL != NULL) && (xmlNoNetExists(URL) == 0)) {
Daniel Veillard4e9b1bc2003-06-09 10:30:33 +00003788 char *canonicFilename;
3789 xmlParserInputPtr ret;
3790
3791 canonicFilename = (char *) xmlCanonicPath((const xmlChar *) URL);
3792 if (canonicFilename == NULL) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00003793 xmlIOErrMemory("building canonical path\n");
Daniel Veillard4e9b1bc2003-06-09 10:30:33 +00003794 return(NULL);
3795 }
3796
3797 ret = xmlCurrentExternalEntityLoader(canonicFilename, ID, ctxt);
3798 xmlFree(canonicFilename);
3799 return(ret);
3800 }
Owen Taylor3473f882001-02-23 17:55:21 +00003801 return(xmlCurrentExternalEntityLoader(URL, ID, ctxt));
3802}
3803
Daniel Veillard8bdb91d2001-10-31 17:52:43 +00003804/************************************************************************
3805 * *
3806 * Disabling Network access *
3807 * *
3808 ************************************************************************/
3809
Daniel Veillard8bdb91d2001-10-31 17:52:43 +00003810/**
3811 * xmlNoNetExternalEntityLoader:
3812 * @URL: the URL for the entity to load
3813 * @ID: the System ID for the entity to load
3814 * @ctxt: the context in which the entity is called or NULL
3815 *
3816 * A specific entity loader disabling network accesses, though still
3817 * allowing local catalog accesses for resolution.
3818 *
3819 * Returns a new allocated xmlParserInputPtr, or NULL.
3820 */
3821xmlParserInputPtr
3822xmlNoNetExternalEntityLoader(const char *URL, const char *ID,
3823 xmlParserCtxtPtr ctxt) {
3824 xmlParserInputPtr input = NULL;
3825 xmlChar *resource = NULL;
3826
3827#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillardad4e2962006-09-21 08:36:38 +00003828 resource = xmlResolveResourceFromCatalog(URL, ID, ctxt);
Daniel Veillard8bdb91d2001-10-31 17:52:43 +00003829#endif
Daniel Veillardad4e2962006-09-21 08:36:38 +00003830
Daniel Veillard8bdb91d2001-10-31 17:52:43 +00003831 if (resource == NULL)
3832 resource = (xmlChar *) URL;
3833
3834 if (resource != NULL) {
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00003835 if ((!xmlStrncasecmp(BAD_CAST resource, BAD_CAST "ftp://", 6)) ||
3836 (!xmlStrncasecmp(BAD_CAST resource, BAD_CAST "http://", 7))) {
Daniel Veillard05d987b2003-10-08 11:54:57 +00003837 xmlIOErr(XML_IO_NETWORK_ATTEMPT, (const char *) resource);
Daniel Veillard8bdb91d2001-10-31 17:52:43 +00003838 if (resource != (xmlChar *) URL)
3839 xmlFree(resource);
3840 return(NULL);
3841 }
3842 }
3843 input = xmlDefaultExternalEntityLoader((const char *) resource, ID, ctxt);
3844 if (resource != (xmlChar *) URL)
3845 xmlFree(resource);
3846 return(input);
3847}
3848
Daniel Veillard5d4644e2005-04-01 13:11:58 +00003849#define bottom_xmlIO
3850#include "elfgcchack.h"