| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* | 
 | 2 |  * xmlIO.c : implementation of the I/O interfaces used by the parser | 
 | 3 |  * | 
 | 4 |  * See Copyright for the status of this software. | 
 | 5 |  * | 
| Daniel Veillard | 344cee7 | 2001-08-20 00:08:40 +0000 | [diff] [blame] | 6 |  * daniel@veillard.com | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 7 |  * | 
 | 8 |  * 14 Nov 2000 ht - for VMS, truncated name of long functions to under 32 char | 
 | 9 |  */ | 
 | 10 |  | 
| Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 11 | #define IN_LIBXML | 
| Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 12 | #include "libxml.h" | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 13 |  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 14 | #include <string.h> | 
| Daniel Veillard | 9272704 | 2002-09-17 17:59:20 +0000 | [diff] [blame] | 15 | #ifdef HAVE_ERRNO_H | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 16 | #include <errno.h> | 
| Daniel Veillard | 9272704 | 2002-09-17 17:59:20 +0000 | [diff] [blame] | 17 | #endif | 
 | 18 |  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 19 |  | 
 | 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 |  | 
 | 39 | /* Figure a portable way to know if a file is a directory. */ | 
 | 40 | #ifndef HAVE_STAT | 
 | 41 | #  ifdef HAVE__STAT | 
| Daniel Veillard | 50f3437 | 2001-08-03 12:06:36 +0000 | [diff] [blame] | 42 |      /* MS C library seems to define stat and _stat. The definition | 
 | 43 |         is identical. Still, mapping them to each other causes a warning. */ | 
 | 44 | #    ifndef _MSC_VER | 
 | 45 | #      define stat(x,y) _stat(x,y) | 
 | 46 | #    endif | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 47 | #    define HAVE_STAT | 
 | 48 | #  endif | 
 | 49 | #endif | 
 | 50 | #ifdef HAVE_STAT | 
 | 51 | #  ifndef S_ISDIR | 
 | 52 | #    ifdef _S_ISDIR | 
 | 53 | #      define S_ISDIR(x) _S_ISDIR(x) | 
 | 54 | #    else | 
 | 55 | #      ifdef S_IFDIR | 
 | 56 | #        ifndef S_IFMT | 
 | 57 | #          ifdef _S_IFMT | 
 | 58 | #            define S_IFMT _S_IFMT | 
 | 59 | #          endif | 
 | 60 | #        endif | 
 | 61 | #        ifdef S_IFMT | 
 | 62 | #          define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) | 
 | 63 | #        endif | 
 | 64 | #      endif | 
 | 65 | #    endif | 
 | 66 | #  endif | 
 | 67 | #endif | 
 | 68 |  | 
 | 69 | #include <libxml/xmlmemory.h> | 
 | 70 | #include <libxml/parser.h> | 
 | 71 | #include <libxml/parserInternals.h> | 
 | 72 | #include <libxml/xmlIO.h> | 
| Daniel Veillard | 388236f | 2001-07-08 18:35:48 +0000 | [diff] [blame] | 73 | #include <libxml/uri.h> | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 74 | #include <libxml/nanohttp.h> | 
 | 75 | #include <libxml/nanoftp.h> | 
 | 76 | #include <libxml/xmlerror.h> | 
| Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 77 | #ifdef LIBXML_CATALOG_ENABLED | 
 | 78 | #include <libxml/catalog.h> | 
 | 79 | #endif | 
| Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 80 | #include <libxml/globals.h> | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 81 |  | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 82 | /* #define VERBOSE_FAILURE */ | 
| Daniel Veillard | 1fd36d2 | 2001-07-04 22:54:28 +0000 | [diff] [blame] | 83 | /* #define DEBUG_EXTERNAL_ENTITIES */ | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 84 | /* #define DEBUG_INPUT */ | 
 | 85 |  | 
 | 86 | #ifdef DEBUG_INPUT | 
 | 87 | #define MINLEN 40 | 
 | 88 | #else | 
 | 89 | #define MINLEN 4000 | 
 | 90 | #endif | 
 | 91 |  | 
 | 92 | /* | 
 | 93 |  * Input I/O callback sets | 
 | 94 |  */ | 
 | 95 | typedef struct _xmlInputCallback { | 
 | 96 |     xmlInputMatchCallback matchcallback; | 
 | 97 |     xmlInputOpenCallback opencallback; | 
 | 98 |     xmlInputReadCallback readcallback; | 
 | 99 |     xmlInputCloseCallback closecallback; | 
 | 100 | } xmlInputCallback; | 
 | 101 |  | 
 | 102 | #define MAX_INPUT_CALLBACK 15 | 
 | 103 |  | 
| Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 104 | static xmlInputCallback xmlInputCallbackTable[MAX_INPUT_CALLBACK]; | 
 | 105 | static int xmlInputCallbackNr = 0; | 
 | 106 | static int xmlInputCallbackInitialized = 0; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 107 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 108 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 109 | /* | 
 | 110 |  * Output I/O callback sets | 
 | 111 |  */ | 
 | 112 | typedef struct _xmlOutputCallback { | 
 | 113 |     xmlOutputMatchCallback matchcallback; | 
 | 114 |     xmlOutputOpenCallback opencallback; | 
 | 115 |     xmlOutputWriteCallback writecallback; | 
 | 116 |     xmlOutputCloseCallback closecallback; | 
 | 117 | } xmlOutputCallback; | 
 | 118 |  | 
 | 119 | #define MAX_OUTPUT_CALLBACK 15 | 
 | 120 |  | 
| Daniel Veillard | 2209073 | 2001-07-16 00:06:07 +0000 | [diff] [blame] | 121 | static xmlOutputCallback xmlOutputCallbackTable[MAX_OUTPUT_CALLBACK]; | 
 | 122 | static int xmlOutputCallbackNr = 0; | 
 | 123 | static int xmlOutputCallbackInitialized = 0; | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 124 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 125 |  | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 126 | /************************************************************************ | 
 | 127 |  *									* | 
 | 128 |  * 		Tree memory error handler				* | 
 | 129 |  *									* | 
 | 130 |  ************************************************************************/ | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 131 |  | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 132 | static const char *IOerr[] = { | 
| Daniel Veillard | a885622 | 2003-10-08 19:26:03 +0000 | [diff] [blame] | 133 |     "Unknown IO error",         /* UNKNOWN */ | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 134 |     "Permission denied",	/* EACCES */ | 
 | 135 |     "Resource temporarily unavailable",/* EAGAIN */ | 
 | 136 |     "Bad file descriptor",	/* EBADF */ | 
 | 137 |     "Bad message",		/* EBADMSG */ | 
 | 138 |     "Resource busy",		/* EBUSY */ | 
 | 139 |     "Operation canceled",	/* ECANCELED */ | 
 | 140 |     "No child processes",	/* ECHILD */ | 
 | 141 |     "Resource deadlock avoided",/* EDEADLK */ | 
 | 142 |     "Domain error",		/* EDOM */ | 
 | 143 |     "File exists",		/* EEXIST */ | 
 | 144 |     "Bad address",		/* EFAULT */ | 
 | 145 |     "File too large",		/* EFBIG */ | 
 | 146 |     "Operation in progress",	/* EINPROGRESS */ | 
 | 147 |     "Interrupted function call",/* EINTR */ | 
 | 148 |     "Invalid argument",		/* EINVAL */ | 
 | 149 |     "Input/output error",	/* EIO */ | 
 | 150 |     "Is a directory",		/* EISDIR */ | 
 | 151 |     "Too many open files",	/* EMFILE */ | 
 | 152 |     "Too many links",		/* EMLINK */ | 
 | 153 |     "Inappropriate message buffer length",/* EMSGSIZE */ | 
 | 154 |     "Filename too long",	/* ENAMETOOLONG */ | 
 | 155 |     "Too many open files in system",/* ENFILE */ | 
 | 156 |     "No such device",		/* ENODEV */ | 
 | 157 |     "No such file or directory",/* ENOENT */ | 
 | 158 |     "Exec format error",	/* ENOEXEC */ | 
 | 159 |     "No locks available",	/* ENOLCK */ | 
 | 160 |     "Not enough space",		/* ENOMEM */ | 
 | 161 |     "No space left on device",	/* ENOSPC */ | 
 | 162 |     "Function not implemented",	/* ENOSYS */ | 
 | 163 |     "Not a directory",		/* ENOTDIR */ | 
 | 164 |     "Directory not empty",	/* ENOTEMPTY */ | 
 | 165 |     "Not supported",		/* ENOTSUP */ | 
 | 166 |     "Inappropriate I/O control operation",/* ENOTTY */ | 
 | 167 |     "No such device or address",/* ENXIO */ | 
 | 168 |     "Operation not permitted",	/* EPERM */ | 
 | 169 |     "Broken pipe",		/* EPIPE */ | 
 | 170 |     "Result too large",		/* ERANGE */ | 
 | 171 |     "Read-only file system",	/* EROFS */ | 
 | 172 |     "Invalid seek",		/* ESPIPE */ | 
 | 173 |     "No such process",		/* ESRCH */ | 
 | 174 |     "Operation timed out",	/* ETIMEDOUT */ | 
 | 175 |     "Improper link",		/* EXDEV */ | 
| Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 176 |     "Attempt to load network entity %s", /* XML_IO_NETWORK_ATTEMPT */ | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 177 |     "encoder error",		/* XML_IO_ENCODER */ | 
| Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 178 |     "flush error", | 
 | 179 |     "write error", | 
 | 180 |     "no input", | 
 | 181 |     "buffer full", | 
 | 182 |     "loading error", | 
 | 183 |     "not a socket",		/* ENOTSOCK */ | 
 | 184 |     "already connected",	/* EISCONN */ | 
 | 185 |     "connection refuxed",	/* ECONNREFUSED */ | 
 | 186 |     "unreachable network",	/* ENETUNREACH */ | 
 | 187 |     "adddress in use",		/* EADDRINUSE */ | 
 | 188 |     "already in use",		/* EALREADY */ | 
 | 189 |     "unknown address familly",	/* EAFNOSUPPORT */ | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 190 | }; | 
 | 191 |  | 
 | 192 | /** | 
 | 193 |  * xmlIOErrMemory: | 
 | 194 |  * @extra:  extra informations | 
 | 195 |  * | 
 | 196 |  * Handle an out of memory condition | 
 | 197 |  */ | 
 | 198 | static void | 
 | 199 | xmlIOErrMemory(const char *extra) | 
 | 200 | { | 
 | 201 |     __xmlSimpleError(XML_FROM_IO, XML_ERR_NO_MEMORY, NULL, NULL, extra); | 
 | 202 | } | 
 | 203 |  | 
 | 204 | /** | 
| Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 205 |  * __xmlIOErr: | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 206 |  * @code:  the error number | 
| Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 207 |  * @ | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 208 |  * @extra:  extra informations | 
 | 209 |  * | 
| Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 210 |  * Handle an I/O error | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 211 |  */ | 
| Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 212 | void | 
 | 213 | __xmlIOErr(int domain, int code, const char *extra) | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 214 | { | 
 | 215 |     unsigned int idx; | 
 | 216 |  | 
 | 217 |     if (code == 0) { | 
 | 218 | #ifdef HAVE_ERRNO_H | 
 | 219 | 	if (errno == 0) code = 0; | 
 | 220 | #ifdef EACCES | 
 | 221 |         else if (errno == EACCES) code = XML_IO_EACCES; | 
 | 222 | #endif | 
 | 223 | #ifdef EAGAIN | 
 | 224 |         else if (errno == EAGAIN) code = XML_IO_EAGAIN; | 
 | 225 | #endif | 
 | 226 | #ifdef EBADF | 
 | 227 |         else if (errno == EBADF) code = XML_IO_EBADF; | 
 | 228 | #endif | 
 | 229 | #ifdef EBADMSG | 
 | 230 |         else if (errno == EBADMSG) code = XML_IO_EBADMSG; | 
 | 231 | #endif | 
 | 232 | #ifdef EBUSY | 
 | 233 |         else if (errno == EBUSY) code = XML_IO_EBUSY; | 
 | 234 | #endif | 
 | 235 | #ifdef ECANCELED | 
 | 236 |         else if (errno == ECANCELED) code = XML_IO_ECANCELED; | 
 | 237 | #endif | 
 | 238 | #ifdef ECHILD | 
 | 239 |         else if (errno == ECHILD) code = XML_IO_ECHILD; | 
 | 240 | #endif | 
 | 241 | #ifdef EDEADLK | 
 | 242 |         else if (errno == EDEADLK) code = XML_IO_EDEADLK; | 
 | 243 | #endif | 
 | 244 | #ifdef EDOM | 
 | 245 |         else if (errno == EDOM) code = XML_IO_EDOM; | 
 | 246 | #endif | 
 | 247 | #ifdef EEXIST | 
 | 248 |         else if (errno == EEXIST) code = XML_IO_EEXIST; | 
 | 249 | #endif | 
 | 250 | #ifdef EFAULT | 
 | 251 |         else if (errno == EFAULT) code = XML_IO_EFAULT; | 
 | 252 | #endif | 
 | 253 | #ifdef EFBIG | 
 | 254 |         else if (errno == EFBIG) code = XML_IO_EFBIG; | 
 | 255 | #endif | 
 | 256 | #ifdef EINPROGRESS | 
 | 257 |         else if (errno == EINPROGRESS) code = XML_IO_EINPROGRESS; | 
 | 258 | #endif | 
 | 259 | #ifdef EINTR | 
 | 260 |         else if (errno == EINTR) code = XML_IO_EINTR; | 
 | 261 | #endif | 
 | 262 | #ifdef EINVAL | 
 | 263 |         else if (errno == EINVAL) code = XML_IO_EINVAL; | 
 | 264 | #endif | 
 | 265 | #ifdef EIO | 
 | 266 |         else if (errno == EIO) code = XML_IO_EIO; | 
 | 267 | #endif | 
 | 268 | #ifdef EISDIR | 
 | 269 |         else if (errno == EISDIR) code = XML_IO_EISDIR; | 
 | 270 | #endif | 
 | 271 | #ifdef EMFILE | 
 | 272 |         else if (errno == EMFILE) code = XML_IO_EMFILE; | 
 | 273 | #endif | 
 | 274 | #ifdef EMLINK | 
 | 275 |         else if (errno == EMLINK) code = XML_IO_EMLINK; | 
 | 276 | #endif | 
 | 277 | #ifdef EMSGSIZE | 
 | 278 |         else if (errno == EMSGSIZE) code = XML_IO_EMSGSIZE; | 
 | 279 | #endif | 
 | 280 | #ifdef ENAMETOOLONG | 
 | 281 |         else if (errno == ENAMETOOLONG) code = XML_IO_ENAMETOOLONG; | 
 | 282 | #endif | 
 | 283 | #ifdef ENFILE | 
 | 284 |         else if (errno == ENFILE) code = XML_IO_ENFILE; | 
 | 285 | #endif | 
 | 286 | #ifdef ENODEV | 
 | 287 |         else if (errno == ENODEV) code = XML_IO_ENODEV; | 
 | 288 | #endif | 
 | 289 | #ifdef ENOENT | 
 | 290 |         else if (errno == ENOENT) code = XML_IO_ENOENT; | 
 | 291 | #endif | 
 | 292 | #ifdef ENOEXEC | 
 | 293 |         else if (errno == ENOEXEC) code = XML_IO_ENOEXEC; | 
 | 294 | #endif | 
 | 295 | #ifdef ENOLCK | 
 | 296 |         else if (errno == ENOLCK) code = XML_IO_ENOLCK; | 
 | 297 | #endif | 
 | 298 | #ifdef ENOMEM | 
 | 299 |         else if (errno == ENOMEM) code = XML_IO_ENOMEM; | 
 | 300 | #endif | 
 | 301 | #ifdef ENOSPC | 
 | 302 |         else if (errno == ENOSPC) code = XML_IO_ENOSPC; | 
 | 303 | #endif | 
 | 304 | #ifdef ENOSYS | 
 | 305 |         else if (errno == ENOSYS) code = XML_IO_ENOSYS; | 
 | 306 | #endif | 
 | 307 | #ifdef ENOTDIR | 
 | 308 |         else if (errno == ENOTDIR) code = XML_IO_ENOTDIR; | 
 | 309 | #endif | 
 | 310 | #ifdef ENOTEMPTY | 
 | 311 |         else if (errno == ENOTEMPTY) code = XML_IO_ENOTEMPTY; | 
 | 312 | #endif | 
 | 313 | #ifdef ENOTSUP | 
 | 314 |         else if (errno == ENOTSUP) code = XML_IO_ENOTSUP; | 
 | 315 | #endif | 
 | 316 | #ifdef ENOTTY | 
 | 317 |         else if (errno == ENOTTY) code = XML_IO_ENOTTY; | 
 | 318 | #endif | 
 | 319 | #ifdef ENXIO | 
 | 320 |         else if (errno == ENXIO) code = XML_IO_ENXIO; | 
 | 321 | #endif | 
 | 322 | #ifdef EPERM | 
 | 323 |         else if (errno == EPERM) code = XML_IO_EPERM; | 
 | 324 | #endif | 
 | 325 | #ifdef EPIPE | 
 | 326 |         else if (errno == EPIPE) code = XML_IO_EPIPE; | 
 | 327 | #endif | 
 | 328 | #ifdef ERANGE | 
 | 329 |         else if (errno == ERANGE) code = XML_IO_ERANGE; | 
 | 330 | #endif | 
 | 331 | #ifdef EROFS | 
 | 332 |         else if (errno == EROFS) code = XML_IO_EROFS; | 
 | 333 | #endif | 
 | 334 | #ifdef ESPIPE | 
 | 335 |         else if (errno == ESPIPE) code = XML_IO_ESPIPE; | 
 | 336 | #endif | 
 | 337 | #ifdef ESRCH | 
 | 338 |         else if (errno == ESRCH) code = XML_IO_ESRCH; | 
 | 339 | #endif | 
 | 340 | #ifdef ETIMEDOUT | 
 | 341 |         else if (errno == ETIMEDOUT) code = XML_IO_ETIMEDOUT; | 
 | 342 | #endif | 
 | 343 | #ifdef EXDEV | 
 | 344 |         else if (errno == EXDEV) code = XML_IO_EXDEV; | 
 | 345 | #endif | 
| Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 346 | #ifdef ENOTSOCK | 
 | 347 |         else if (errno == ENOTSOCK) code = XML_IO_ENOTSOCK; | 
 | 348 | #endif | 
 | 349 | #ifdef EISCONN | 
 | 350 |         else if (errno == EISCONN) code = XML_IO_EISCONN; | 
 | 351 | #endif | 
 | 352 | #ifdef ECONNREFUSED | 
 | 353 |         else if (errno == ECONNREFUSED) code = XML_IO_ECONNREFUSED; | 
 | 354 | #endif | 
 | 355 | #ifdef ETIMEDOUT | 
 | 356 |         else if (errno == ETIMEDOUT) code = XML_IO_ETIMEDOUT; | 
 | 357 | #endif | 
 | 358 | #ifdef ENETUNREACH | 
 | 359 |         else if (errno == ENETUNREACH) code = XML_IO_ENETUNREACH; | 
 | 360 | #endif | 
 | 361 | #ifdef EADDRINUSE | 
 | 362 |         else if (errno == EADDRINUSE) code = XML_IO_EADDRINUSE; | 
 | 363 | #endif | 
 | 364 | #ifdef EINPROGRESS | 
 | 365 |         else if (errno == EINPROGRESS) code = XML_IO_EINPROGRESS; | 
 | 366 | #endif | 
 | 367 | #ifdef EALREADY | 
 | 368 |         else if (errno == EALREADY) code = XML_IO_EALREADY; | 
 | 369 | #endif | 
 | 370 | #ifdef EAFNOSUPPORT | 
 | 371 |         else if (errno == EAFNOSUPPORT) code = XML_IO_EAFNOSUPPORT; | 
 | 372 | #endif | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 373 |         else code = XML_IO_UNKNOWN; | 
 | 374 | #endif /* HAVE_ERRNO_H */ | 
 | 375 |     } | 
 | 376 |     idx = 0; | 
 | 377 |     if (code >= XML_IO_UNKNOWN) idx = code - XML_IO_UNKNOWN; | 
 | 378 |     if (idx >= (sizeof(IOerr) / sizeof(IOerr[0]))) idx = 0; | 
 | 379 |      | 
| Daniel Veillard | 2b0f879 | 2003-10-10 19:36:36 +0000 | [diff] [blame] | 380 |     __xmlSimpleError(domain, code, NULL, IOerr[idx], extra); | 
 | 381 | } | 
 | 382 |  | 
 | 383 | /** | 
 | 384 |  * xmlIOErr: | 
 | 385 |  * @code:  the error number | 
 | 386 |  * @extra:  extra informations | 
 | 387 |  * | 
 | 388 |  * Handle an I/O error | 
 | 389 |  */ | 
 | 390 | static void | 
 | 391 | xmlIOErr(int code, const char *extra) | 
 | 392 | { | 
 | 393 |     __xmlIOErr(XML_FROM_IO, code, extra); | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 394 | } | 
 | 395 |  | 
| Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 396 | /** | 
| Daniel Veillard | e8039df | 2003-10-27 11:25:13 +0000 | [diff] [blame] | 397 |  * __xmlLoaderErr: | 
 | 398 |  * @ctx: the parser context | 
| Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 399 |  * @extra:  extra informations | 
 | 400 |  * | 
 | 401 |  * Handle a resource access error | 
 | 402 |  */ | 
| Daniel Veillard | e8039df | 2003-10-27 11:25:13 +0000 | [diff] [blame] | 403 | void | 
 | 404 | __xmlLoaderErr(void *ctx, const char *msg, const char *filename) | 
| Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 405 | { | 
| Daniel Veillard | e8039df | 2003-10-27 11:25:13 +0000 | [diff] [blame] | 406 |     xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; | 
| Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 407 |     xmlStructuredErrorFunc schannel = NULL; | 
| Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 408 |     xmlGenericErrorFunc channel = NULL; | 
 | 409 |     void *data = NULL; | 
 | 410 |     xmlErrorLevel level = XML_ERR_ERROR; | 
 | 411 |  | 
| Daniel Veillard | 157fee0 | 2003-10-31 10:36:03 +0000 | [diff] [blame^] | 412 |     if ((ctxt != NULL) && (ctxt->disableSAX != 0) && | 
 | 413 |         (ctxt->instate == XML_PARSER_EOF)) | 
 | 414 | 	return; | 
| Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 415 |     if ((ctxt != NULL) && (ctxt->sax != NULL)) { | 
 | 416 |         if (ctxt->validate) { | 
 | 417 | 	    channel = ctxt->sax->error; | 
 | 418 | 	    level = XML_ERR_ERROR; | 
 | 419 | 	} else { | 
 | 420 | 	    channel = ctxt->sax->warning; | 
 | 421 | 	    level = XML_ERR_WARNING; | 
 | 422 | 	} | 
| Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 423 | 	schannel = ctxt->sax->serror; | 
| Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 424 | 	data = ctxt->userData; | 
 | 425 |     } | 
| Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 426 |     __xmlRaiseError(schannel, channel, data, ctxt, NULL, XML_FROM_IO, | 
| Daniel Veillard | cd6ff28 | 2003-10-08 22:38:13 +0000 | [diff] [blame] | 427 |                     XML_IO_LOAD_ERROR, level, NULL, 0, | 
 | 428 | 		    filename, NULL, NULL, 0, 0, | 
 | 429 | 		    msg, filename); | 
 | 430 |                      | 
 | 431 | } | 
 | 432 |  | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 433 | /************************************************************************ | 
 | 434 |  *									* | 
 | 435 |  * 		Tree memory error handler				* | 
 | 436 |  *									* | 
 | 437 |  ************************************************************************/ | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 438 | /** | 
| Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 439 |  * xmlNormalizeWindowsPath: | 
| Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 440 |  * @path: the input file path | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 441 |  * | 
| Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 442 |  * This function is obsolete. Please see xmlURIFromPath in uri.c for | 
 | 443 |  * a better solution. | 
| Daniel Veillard | 33300b4 | 2003-04-17 09:09:19 +0000 | [diff] [blame] | 444 |  * | 
 | 445 |  * Returns a canonicalized version of the path | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 446 |  */ | 
 | 447 | xmlChar * | 
 | 448 | xmlNormalizeWindowsPath(const xmlChar *path) | 
 | 449 | { | 
| Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 450 |     return xmlCanonicPath(path); | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 451 | } | 
 | 452 |  | 
| Daniel Veillard | acf7ff0 | 2001-10-29 20:21:47 +0000 | [diff] [blame] | 453 | /** | 
 | 454 |  * xmlCleanupInputCallbacks: | 
 | 455 |  * | 
 | 456 |  * clears the entire input callback table. this includes the | 
 | 457 |  * compiled-in I/O.  | 
 | 458 |  */ | 
 | 459 | void | 
 | 460 | xmlCleanupInputCallbacks(void) | 
 | 461 | { | 
 | 462 |     int i; | 
 | 463 |  | 
 | 464 |     if (!xmlInputCallbackInitialized) | 
 | 465 |         return; | 
 | 466 |  | 
| Daniel Veillard | 107ccaa | 2001-11-27 16:23:50 +0000 | [diff] [blame] | 467 |     for (i = xmlInputCallbackNr - 1; i >= 0; i--) { | 
| Daniel Veillard | acf7ff0 | 2001-10-29 20:21:47 +0000 | [diff] [blame] | 468 |         xmlInputCallbackTable[i].matchcallback = NULL; | 
 | 469 |         xmlInputCallbackTable[i].opencallback = NULL; | 
 | 470 |         xmlInputCallbackTable[i].readcallback = NULL; | 
 | 471 |         xmlInputCallbackTable[i].closecallback = NULL; | 
 | 472 |     } | 
 | 473 |  | 
 | 474 |     xmlInputCallbackNr = 0; | 
| Aleksey Sanin | 9c45ba8 | 2002-06-06 21:46:13 +0000 | [diff] [blame] | 475 |     xmlInputCallbackInitialized = 0; | 
| Daniel Veillard | acf7ff0 | 2001-10-29 20:21:47 +0000 | [diff] [blame] | 476 | } | 
 | 477 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 478 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Daniel Veillard | acf7ff0 | 2001-10-29 20:21:47 +0000 | [diff] [blame] | 479 | /** | 
 | 480 |  * xmlCleanupOutputCallbacks: | 
 | 481 |  * | 
 | 482 |  * clears the entire output callback table. this includes the | 
 | 483 |  * compiled-in I/O callbacks.  | 
 | 484 |  */ | 
 | 485 | void | 
 | 486 | xmlCleanupOutputCallbacks(void) | 
 | 487 | { | 
 | 488 |     int i; | 
 | 489 |  | 
 | 490 |     if (!xmlOutputCallbackInitialized) | 
 | 491 |         return; | 
 | 492 |  | 
| Daniel Veillard | 107ccaa | 2001-11-27 16:23:50 +0000 | [diff] [blame] | 493 |     for (i = xmlOutputCallbackNr - 1; i >= 0; i--) { | 
| Daniel Veillard | acf7ff0 | 2001-10-29 20:21:47 +0000 | [diff] [blame] | 494 |         xmlOutputCallbackTable[i].matchcallback = NULL; | 
 | 495 |         xmlOutputCallbackTable[i].opencallback = NULL; | 
 | 496 |         xmlOutputCallbackTable[i].writecallback = NULL; | 
 | 497 |         xmlOutputCallbackTable[i].closecallback = NULL; | 
 | 498 |     } | 
 | 499 |  | 
 | 500 |     xmlOutputCallbackNr = 0; | 
| Aleksey Sanin | 9c45ba8 | 2002-06-06 21:46:13 +0000 | [diff] [blame] | 501 |     xmlOutputCallbackInitialized = 0; | 
| Daniel Veillard | acf7ff0 | 2001-10-29 20:21:47 +0000 | [diff] [blame] | 502 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 503 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Daniel Veillard | acf7ff0 | 2001-10-29 20:21:47 +0000 | [diff] [blame] | 504 |  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 505 | /************************************************************************ | 
 | 506 |  *									* | 
 | 507 |  *		Standard I/O for file accesses				* | 
 | 508 |  *									* | 
 | 509 |  ************************************************************************/ | 
 | 510 |  | 
 | 511 | /** | 
| Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 512 |  * xmlCheckFilename: | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 513 |  * @path:  the path to check | 
 | 514 |  * | 
 | 515 |  * function checks to see if @path is a valid source | 
 | 516 |  * (file, socket...) for XML. | 
 | 517 |  * | 
 | 518 |  * if stat is not available on the target machine, | 
 | 519 |  * returns 1.  if stat fails, returns 0 (if calling | 
 | 520 |  * stat on the filename fails, it can't be right). | 
 | 521 |  * if stat succeeds and the file is a directory, | 
| Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 522 |  * returns 2.  otherwise returns 1. | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 523 |  */ | 
 | 524 |  | 
| Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 525 | int | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 526 | xmlCheckFilename (const char *path) | 
 | 527 | { | 
 | 528 | #ifdef HAVE_STAT | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 529 |     struct stat stat_buffer; | 
 | 530 |  | 
 | 531 |     if (stat(path, &stat_buffer) == -1) | 
 | 532 |         return 0; | 
 | 533 |  | 
| Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 534 | #ifdef S_ISDIR | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 535 |     if (S_ISDIR(stat_buffer.st_mode)) { | 
| Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 536 |         return 2; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 537 |     } | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 538 | #endif | 
 | 539 | #endif | 
 | 540 |     return 1; | 
 | 541 | } | 
 | 542 |  | 
| Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 543 | static int | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 544 | xmlNop(void) { | 
 | 545 |     return(0); | 
 | 546 | } | 
 | 547 |  | 
 | 548 | /** | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 549 |  * xmlFdRead: | 
 | 550 |  * @context:  the I/O context | 
 | 551 |  * @buffer:  where to drop data | 
 | 552 |  * @len:  number of bytes to read | 
 | 553 |  * | 
 | 554 |  * Read @len bytes to @buffer from the I/O channel. | 
 | 555 |  * | 
 | 556 |  * Returns the number of bytes written | 
 | 557 |  */ | 
| Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 558 | static int | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 559 | xmlFdRead (void * context, char * buffer, int len) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 560 |     int ret; | 
 | 561 |  | 
 | 562 |     ret = read((int) (long) context, &buffer[0], len); | 
 | 563 |     if (ret < 0) xmlIOErr(0, "read()"); | 
 | 564 |     return(ret); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 565 | } | 
 | 566 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 567 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 568 | /** | 
 | 569 |  * xmlFdWrite: | 
 | 570 |  * @context:  the I/O context | 
 | 571 |  * @buffer:  where to get data | 
 | 572 |  * @len:  number of bytes to write | 
 | 573 |  * | 
 | 574 |  * Write @len bytes from @buffer to the I/O channel. | 
 | 575 |  * | 
 | 576 |  * Returns the number of bytes written | 
 | 577 |  */ | 
| Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 578 | static int | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 579 | xmlFdWrite (void * context, const char * buffer, int len) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 580 |     int ret; | 
 | 581 |  | 
 | 582 |     ret = write((int) (long) context, &buffer[0], len); | 
 | 583 |     if (ret < 0) xmlIOErr(0, "write()"); | 
 | 584 |     return(ret); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 585 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 586 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 587 |  | 
 | 588 | /** | 
 | 589 |  * xmlFdClose: | 
 | 590 |  * @context:  the I/O context | 
 | 591 |  * | 
 | 592 |  * Close an I/O channel | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 593 |  * | 
 | 594 |  * Returns 0 in case of success and error code otherwise | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 595 |  */ | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 596 | static int | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 597 | xmlFdClose (void * context) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 598 |     int ret; | 
 | 599 |     ret = close((int) (long) context); | 
 | 600 |     if (ret < 0) xmlIOErr(0, "close()"); | 
 | 601 |     return(ret); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 602 | } | 
 | 603 |  | 
 | 604 | /** | 
 | 605 |  * xmlFileMatch: | 
 | 606 |  * @filename:  the URI for matching | 
 | 607 |  * | 
 | 608 |  * input from FILE * | 
 | 609 |  * | 
 | 610 |  * Returns 1 if matches, 0 otherwise | 
 | 611 |  */ | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 612 | int | 
| Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 613 | xmlFileMatch (const char *filename ATTRIBUTE_UNUSED) { | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 614 |     return(1); | 
 | 615 | } | 
 | 616 |  | 
 | 617 | /** | 
| Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 618 |  * xmlFileOpen_real: | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 619 |  * @filename:  the URI for matching | 
 | 620 |  * | 
 | 621 |  * input from FILE *, supports compressed input | 
 | 622 |  * if @filename is " " then the standard input is used | 
 | 623 |  * | 
 | 624 |  * Returns an I/O context or NULL in case of error | 
 | 625 |  */ | 
| Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 626 | static void * | 
 | 627 | xmlFileOpen_real (const char *filename) { | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 628 |     const char *path = NULL; | 
 | 629 |     FILE *fd; | 
 | 630 |  | 
 | 631 |     if (!strcmp(filename, "-")) { | 
 | 632 | 	fd = stdin; | 
 | 633 | 	return((void *) fd); | 
 | 634 |     } | 
 | 635 |  | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 636 |     if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17)) | 
| Daniel Veillard | 1997c3e | 2003-07-05 20:43:43 +0000 | [diff] [blame] | 637 | #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__) | 
| Daniel Veillard | b212bbb | 2002-08-25 14:39:16 +0000 | [diff] [blame] | 638 | 	path = &filename[17]; | 
 | 639 | #else | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 640 | 	path = &filename[16]; | 
| Daniel Veillard | b212bbb | 2002-08-25 14:39:16 +0000 | [diff] [blame] | 641 | #endif | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 642 |     else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) { | 
| Daniel Veillard | 1997c3e | 2003-07-05 20:43:43 +0000 | [diff] [blame] | 643 | #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__) | 
| Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 644 | 	path = &filename[8]; | 
 | 645 | #else | 
| Daniel Veillard | 3c2758d | 2001-05-31 18:43:43 +0000 | [diff] [blame] | 646 | 	path = &filename[7]; | 
| Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 647 | #endif | 
 | 648 |     } else  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 649 | 	path = filename; | 
 | 650 |  | 
 | 651 |     if (path == NULL) | 
 | 652 | 	return(NULL); | 
 | 653 |     if (!xmlCheckFilename(path)) | 
 | 654 |         return(NULL); | 
 | 655 |  | 
| Daniel Veillard | 1997c3e | 2003-07-05 20:43:43 +0000 | [diff] [blame] | 656 | #if defined(WIN32) || defined (__DJGPP__) && !defined (__CYGWIN__) | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 657 |     fd = fopen(path, "rb"); | 
 | 658 | #else | 
 | 659 |     fd = fopen(path, "r"); | 
 | 660 | #endif /* WIN32 */ | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 661 |     if (fd == NULL) xmlIOErr(0, path); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 662 |     return((void *) fd); | 
 | 663 | } | 
 | 664 |  | 
 | 665 | /** | 
| Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 666 |  * xmlFileOpen: | 
 | 667 |  * @filename:  the URI for matching | 
 | 668 |  * | 
 | 669 |  * Wrapper around xmlFileOpen_real that try it with an unescaped | 
 | 670 |  * version of @filename, if this fails fallback to @filename | 
| Daniel Veillard | 71531f3 | 2003-02-05 13:19:53 +0000 | [diff] [blame] | 671 |  * | 
 | 672 |  * Returns a handler or NULL in case or failure | 
| Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 673 |  */ | 
 | 674 | void * | 
 | 675 | xmlFileOpen (const char *filename) { | 
 | 676 |     char *unescaped; | 
 | 677 |     void *retval; | 
 | 678 |     unescaped = xmlURIUnescapeString(filename, 0, NULL); | 
 | 679 |     if (unescaped != NULL) { | 
 | 680 | 	retval = xmlFileOpen_real(unescaped); | 
 | 681 |     } else { | 
 | 682 | 	retval = xmlFileOpen_real(filename); | 
 | 683 |     } | 
 | 684 |     xmlFree(unescaped); | 
 | 685 |     return retval; | 
 | 686 | } | 
 | 687 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 688 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 689 | /** | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 690 |  * xmlFileOpenW: | 
 | 691 |  * @filename:  the URI for matching | 
 | 692 |  * | 
 | 693 |  * output to from FILE *, | 
 | 694 |  * if @filename is "-" then the standard output is used | 
 | 695 |  * | 
 | 696 |  * Returns an I/O context or NULL in case of error | 
 | 697 |  */ | 
| Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 698 | static void * | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 699 | xmlFileOpenW (const char *filename) { | 
 | 700 |     const char *path = NULL; | 
 | 701 |     FILE *fd; | 
 | 702 |  | 
 | 703 |     if (!strcmp(filename, "-")) { | 
 | 704 | 	fd = stdout; | 
 | 705 | 	return((void *) fd); | 
 | 706 |     } | 
 | 707 |  | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 708 |     if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17)) | 
| Daniel Veillard | 1997c3e | 2003-07-05 20:43:43 +0000 | [diff] [blame] | 709 | #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__) | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 710 | 	path = &filename[17]; | 
 | 711 | #else | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 712 | 	path = &filename[16]; | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 713 | #endif | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 714 |     else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) { | 
| Daniel Veillard | 1997c3e | 2003-07-05 20:43:43 +0000 | [diff] [blame] | 715 | #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__) | 
| Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 716 | 	path = &filename[8]; | 
 | 717 | #else | 
| Daniel Veillard | 3c2758d | 2001-05-31 18:43:43 +0000 | [diff] [blame] | 718 | 	path = &filename[7]; | 
| Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 719 | #endif | 
 | 720 |     } else  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 721 | 	path = filename; | 
 | 722 |  | 
 | 723 |     if (path == NULL) | 
 | 724 | 	return(NULL); | 
 | 725 |  | 
| Daniel Veillard | cbbd78d | 2003-07-20 15:21:30 +0000 | [diff] [blame] | 726 |     fd = fopen(path, "wb"); | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 727 |     if (fd == NULL) xmlIOErr(0, path); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 728 |     return((void *) fd); | 
 | 729 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 730 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 731 |  | 
 | 732 | /** | 
 | 733 |  * xmlFileRead: | 
 | 734 |  * @context:  the I/O context | 
 | 735 |  * @buffer:  where to drop data | 
 | 736 |  * @len:  number of bytes to write | 
 | 737 |  * | 
 | 738 |  * Read @len bytes to @buffer from the I/O channel. | 
 | 739 |  * | 
 | 740 |  * Returns the number of bytes written | 
 | 741 |  */ | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 742 | int | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 743 | xmlFileRead (void * context, char * buffer, int len) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 744 |     int ret; | 
 | 745 |     ret = fread(&buffer[0], 1,  len, (FILE *) context); | 
 | 746 |     if (ret < 0) xmlIOErr(0, "fread()"); | 
 | 747 |     return(ret); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 748 | } | 
 | 749 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 750 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 751 | /** | 
 | 752 |  * xmlFileWrite: | 
 | 753 |  * @context:  the I/O context | 
 | 754 |  * @buffer:  where to drop data | 
 | 755 |  * @len:  number of bytes to write | 
 | 756 |  * | 
 | 757 |  * Write @len bytes from @buffer to the I/O channel. | 
 | 758 |  * | 
 | 759 |  * Returns the number of bytes written | 
 | 760 |  */ | 
| Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 761 | static int | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 762 | xmlFileWrite (void * context, const char * buffer, int len) { | 
| Daniel Veillard | 4a6d39b | 2002-12-17 18:33:01 +0000 | [diff] [blame] | 763 |     int items; | 
 | 764 |  | 
 | 765 |     items = fwrite(&buffer[0], len, 1, (FILE *) context); | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 766 |     if ((items == 0) && (ferror((FILE *) context))) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 767 |         xmlIOErr(0, "fwrite()"); | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 768 | 	return(-1); | 
 | 769 |     } | 
| Daniel Veillard | 4a6d39b | 2002-12-17 18:33:01 +0000 | [diff] [blame] | 770 |     return(items * len); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 771 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 772 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 773 |  | 
 | 774 | /** | 
 | 775 |  * xmlFileClose: | 
 | 776 |  * @context:  the I/O context | 
 | 777 |  * | 
 | 778 |  * Close an I/O channel | 
| Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 779 |  * | 
 | 780 |  * Returns 0 or -1 in case of error | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 781 |  */ | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 782 | int | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 783 | xmlFileClose (void * context) { | 
| Daniel Veillard | a9e65e8 | 2001-10-30 10:32:36 +0000 | [diff] [blame] | 784 |     FILE *fil; | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 785 |     int ret; | 
| Daniel Veillard | a9e65e8 | 2001-10-30 10:32:36 +0000 | [diff] [blame] | 786 |  | 
 | 787 |     fil = (FILE *) context; | 
 | 788 |     if (fil == stdin) | 
 | 789 | 	return(0); | 
 | 790 |     if (fil == stdout) | 
 | 791 | 	return(0); | 
| Daniel Veillard | cd337f0 | 2001-11-22 18:20:37 +0000 | [diff] [blame] | 792 |     if (fil == stderr) | 
 | 793 | 	return(0); | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 794 |     ret = ( fclose((FILE *) context) == EOF ) ? -1 : 0; | 
 | 795 |     if (ret < 0) | 
 | 796 |         xmlIOErr(0, "fclose()"); | 
 | 797 |     return(ret); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 798 | } | 
 | 799 |  | 
 | 800 | /** | 
 | 801 |  * xmlFileFlush: | 
 | 802 |  * @context:  the I/O context | 
 | 803 |  * | 
 | 804 |  * Flush an I/O channel | 
 | 805 |  */ | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 806 | static int | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 807 | xmlFileFlush (void * context) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 808 |     int ret; | 
 | 809 |     ret = ( fflush((FILE *) context) == EOF ) ? -1 : 0; | 
 | 810 |     if (ret < 0) | 
 | 811 |         xmlIOErr(0, "fflush()"); | 
 | 812 |     return(ret); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 813 | } | 
 | 814 |  | 
 | 815 | #ifdef HAVE_ZLIB_H | 
 | 816 | /************************************************************************ | 
 | 817 |  *									* | 
 | 818 |  *		I/O for compressed file accesses			* | 
 | 819 |  *									* | 
 | 820 |  ************************************************************************/ | 
 | 821 | /** | 
 | 822 |  * xmlGzfileMatch: | 
 | 823 |  * @filename:  the URI for matching | 
 | 824 |  * | 
 | 825 |  * input from compressed file test | 
 | 826 |  * | 
 | 827 |  * Returns 1 if matches, 0 otherwise | 
 | 828 |  */ | 
| Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 829 | static int | 
| Daniel Veillard | c86a4fa | 2001-03-26 16:28:29 +0000 | [diff] [blame] | 830 | xmlGzfileMatch (const char *filename ATTRIBUTE_UNUSED) { | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 831 |     return(1); | 
 | 832 | } | 
 | 833 |  | 
 | 834 | /** | 
| Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 835 |  * xmlGzfileOpen_real: | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 836 |  * @filename:  the URI for matching | 
 | 837 |  * | 
 | 838 |  * input from compressed file open | 
 | 839 |  * if @filename is " " then the standard input is used | 
 | 840 |  * | 
 | 841 |  * Returns an I/O context or NULL in case of error | 
 | 842 |  */ | 
| Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 843 | static void * | 
| Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 844 | xmlGzfileOpen_real (const char *filename) { | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 845 |     const char *path = NULL; | 
 | 846 |     gzFile fd; | 
 | 847 |  | 
 | 848 |     if (!strcmp(filename, "-")) { | 
| Daniel Veillard | a9e65e8 | 2001-10-30 10:32:36 +0000 | [diff] [blame] | 849 |         fd = gzdopen(dup(0), "rb"); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 850 | 	return((void *) fd); | 
 | 851 |     } | 
 | 852 |  | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 853 |     if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17)) | 
| Daniel Veillard | 1997c3e | 2003-07-05 20:43:43 +0000 | [diff] [blame] | 854 | #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__) | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 855 | 	path = &filename[17]; | 
 | 856 | #else | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 857 | 	path = &filename[16]; | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 858 | #endif | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 859 |     else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) { | 
| Daniel Veillard | 1997c3e | 2003-07-05 20:43:43 +0000 | [diff] [blame] | 860 | #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__) | 
| Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 861 | 	path = &filename[8]; | 
 | 862 | #else | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 863 | 	path = &filename[7]; | 
| Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 864 | #endif | 
 | 865 |     } else  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 866 | 	path = filename; | 
 | 867 |  | 
 | 868 |     if (path == NULL) | 
 | 869 | 	return(NULL); | 
 | 870 |     if (!xmlCheckFilename(path)) | 
 | 871 |         return(NULL); | 
 | 872 |  | 
 | 873 |     fd = gzopen(path, "rb"); | 
 | 874 |     return((void *) fd); | 
 | 875 | } | 
 | 876 |  | 
 | 877 | /** | 
| Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 878 |  * xmlGzfileOpen: | 
 | 879 |  * @filename:  the URI for matching | 
 | 880 |  * | 
| Daniel Veillard | 4e9b1bc | 2003-06-09 10:30:33 +0000 | [diff] [blame] | 881 |  * Wrapper around xmlGzfileOpen if the open fais, it will | 
 | 882 |  * try to unescape @filename | 
| Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 883 |  */ | 
 | 884 | static void * | 
 | 885 | xmlGzfileOpen (const char *filename) { | 
 | 886 |     char *unescaped; | 
 | 887 |     void *retval; | 
| Daniel Veillard | 4e9b1bc | 2003-06-09 10:30:33 +0000 | [diff] [blame] | 888 |  | 
 | 889 |     retval = xmlGzfileOpen_real(filename); | 
 | 890 |     if (retval == NULL) { | 
 | 891 | 	unescaped = xmlURIUnescapeString(filename, 0, NULL); | 
 | 892 | 	if (unescaped != NULL) { | 
 | 893 | 	    retval = xmlGzfileOpen_real(unescaped); | 
 | 894 | 	} | 
 | 895 | 	xmlFree(unescaped); | 
| Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 896 |     } | 
| Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 897 |     return retval; | 
 | 898 | } | 
 | 899 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 900 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Daniel Veillard | cacbe5d | 2003-01-10 16:09:51 +0000 | [diff] [blame] | 901 | /** | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 902 |  * xmlGzfileOpenW: | 
 | 903 |  * @filename:  the URI for matching | 
 | 904 |  * @compression:  the compression factor (0 - 9 included) | 
 | 905 |  * | 
 | 906 |  * input from compressed file open | 
 | 907 |  * if @filename is " " then the standard input is used | 
 | 908 |  * | 
 | 909 |  * Returns an I/O context or NULL in case of error | 
 | 910 |  */ | 
| Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 911 | static void * | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 912 | xmlGzfileOpenW (const char *filename, int compression) { | 
 | 913 |     const char *path = NULL; | 
 | 914 |     char mode[15]; | 
 | 915 |     gzFile fd; | 
 | 916 |  | 
| Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 917 |     snprintf(mode, sizeof(mode), "wb%d", compression); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 918 |     if (!strcmp(filename, "-")) { | 
| Daniel Veillard | a9e65e8 | 2001-10-30 10:32:36 +0000 | [diff] [blame] | 919 |         fd = gzdopen(dup(1), mode); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 920 | 	return((void *) fd); | 
 | 921 |     } | 
 | 922 |  | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 923 |     if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17)) | 
| Daniel Veillard | 1997c3e | 2003-07-05 20:43:43 +0000 | [diff] [blame] | 924 | #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__) | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 925 | 	path = &filename[17]; | 
 | 926 | #else | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 927 | 	path = &filename[16]; | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 928 | #endif | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 929 |     else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) { | 
| Daniel Veillard | 1997c3e | 2003-07-05 20:43:43 +0000 | [diff] [blame] | 930 | #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__) | 
| Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 931 | 	path = &filename[8]; | 
 | 932 | #else | 
| Daniel Veillard | 3c2758d | 2001-05-31 18:43:43 +0000 | [diff] [blame] | 933 | 	path = &filename[7]; | 
| Daniel Veillard | fe70332 | 2001-08-14 12:18:09 +0000 | [diff] [blame] | 934 | #endif | 
 | 935 |     } else  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 936 | 	path = filename; | 
 | 937 |  | 
 | 938 |     if (path == NULL) | 
 | 939 | 	return(NULL); | 
 | 940 |  | 
 | 941 |     fd = gzopen(path, mode); | 
 | 942 |     return((void *) fd); | 
 | 943 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 944 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 945 |  | 
 | 946 | /** | 
 | 947 |  * xmlGzfileRead: | 
 | 948 |  * @context:  the I/O context | 
 | 949 |  * @buffer:  where to drop data | 
 | 950 |  * @len:  number of bytes to write | 
 | 951 |  * | 
 | 952 |  * Read @len bytes to @buffer from the compressed I/O channel. | 
 | 953 |  * | 
 | 954 |  * Returns the number of bytes written | 
 | 955 |  */ | 
| Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 956 | static int | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 957 | xmlGzfileRead (void * context, char * buffer, int len) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 958 |     int ret; | 
 | 959 |  | 
 | 960 |     ret = gzread((gzFile) context, &buffer[0], len); | 
 | 961 |     if (ret < 0) xmlIOErr(0, "gzread()"); | 
 | 962 |     return(ret); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 963 | } | 
 | 964 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 965 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 966 | /** | 
 | 967 |  * xmlGzfileWrite: | 
 | 968 |  * @context:  the I/O context | 
 | 969 |  * @buffer:  where to drop data | 
 | 970 |  * @len:  number of bytes to write | 
 | 971 |  * | 
 | 972 |  * Write @len bytes from @buffer to the compressed I/O channel. | 
 | 973 |  * | 
 | 974 |  * Returns the number of bytes written | 
 | 975 |  */ | 
| Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 976 | static int | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 977 | xmlGzfileWrite (void * context, const char * buffer, int len) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 978 |     int ret; | 
 | 979 |  | 
 | 980 |     ret = gzwrite((gzFile) context, (char *) &buffer[0], len); | 
 | 981 |     if (ret < 0) xmlIOErr(0, "gzwrite()"); | 
 | 982 |     return(ret); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 983 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 984 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 985 |  | 
 | 986 | /** | 
 | 987 |  * xmlGzfileClose: | 
 | 988 |  * @context:  the I/O context | 
 | 989 |  * | 
 | 990 |  * Close a compressed I/O channel | 
 | 991 |  */ | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 992 | static int | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 993 | xmlGzfileClose (void * context) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 994 |     int ret; | 
 | 995 |  | 
 | 996 |     ret =  (gzclose((gzFile) context) == Z_OK ) ? 0 : -1; | 
 | 997 |     if (ret < 0) xmlIOErr(0, "gzclose()"); | 
 | 998 |     return(ret); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 999 | } | 
 | 1000 | #endif /* HAVE_ZLIB_H */ | 
 | 1001 |  | 
 | 1002 | #ifdef LIBXML_HTTP_ENABLED | 
 | 1003 | /************************************************************************ | 
 | 1004 |  *									* | 
 | 1005 |  *			I/O for HTTP file accesses			* | 
 | 1006 |  *									* | 
 | 1007 |  ************************************************************************/ | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1008 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1009 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1010 | typedef struct xmlIOHTTPWriteCtxt_ | 
 | 1011 | { | 
 | 1012 |     int			compression; | 
 | 1013 |  | 
 | 1014 |     char *		uri; | 
 | 1015 |  | 
 | 1016 |     void *		doc_buff; | 
 | 1017 |  | 
 | 1018 | } xmlIOHTTPWriteCtxt, *xmlIOHTTPWriteCtxtPtr; | 
 | 1019 |  | 
 | 1020 | #ifdef HAVE_ZLIB_H | 
 | 1021 |  | 
 | 1022 | #define DFLT_WBITS		( -15 ) | 
 | 1023 | #define DFLT_MEM_LVL		( 8 ) | 
 | 1024 | #define GZ_MAGIC1		( 0x1f ) | 
 | 1025 | #define GZ_MAGIC2		( 0x8b ) | 
 | 1026 | #define LXML_ZLIB_OS_CODE	( 0x03 ) | 
 | 1027 | #define INIT_HTTP_BUFF_SIZE	( 32768 ) | 
 | 1028 | #define DFLT_ZLIB_RATIO		( 5 ) | 
 | 1029 |  | 
 | 1030 | /* | 
 | 1031 | **  Data structure and functions to work with sending compressed data | 
 | 1032 | **  via HTTP. | 
 | 1033 | */ | 
 | 1034 |  | 
 | 1035 | typedef struct xmlZMemBuff_ | 
 | 1036 | { | 
 | 1037 |    unsigned long	size; | 
 | 1038 |    unsigned long	crc; | 
 | 1039 |  | 
 | 1040 |    unsigned char *	zbuff; | 
 | 1041 |    z_stream		zctrl; | 
 | 1042 |  | 
 | 1043 | } xmlZMemBuff, *xmlZMemBuffPtr; | 
 | 1044 |  | 
 | 1045 | /** | 
 | 1046 |  * append_reverse_ulong | 
 | 1047 |  * @buff:  Compressed memory buffer | 
 | 1048 |  * @data:  Unsigned long to append | 
 | 1049 |  * | 
 | 1050 |  * Append a unsigned long in reverse byte order to the end of the | 
 | 1051 |  * memory buffer. | 
 | 1052 |  */ | 
 | 1053 | static void | 
 | 1054 | append_reverse_ulong( xmlZMemBuff * buff, unsigned long data ) { | 
 | 1055 |  | 
 | 1056 |     int		idx; | 
 | 1057 |  | 
 | 1058 |     if ( buff == NULL ) | 
 | 1059 | 	return; | 
 | 1060 |  | 
 | 1061 |     /* | 
 | 1062 |     **  This is plagiarized from putLong in gzio.c (zlib source) where | 
 | 1063 |     **  the number "4" is hardcoded.  If zlib is ever patched to  | 
 | 1064 |     **  support 64 bit file sizes, this code would need to be patched | 
 | 1065 |     **  as well. | 
 | 1066 |     */ | 
 | 1067 |  | 
 | 1068 |     for ( idx = 0; idx < 4; idx++ ) { | 
 | 1069 | 	*buff->zctrl.next_out = ( data & 0xff ); | 
 | 1070 | 	data >>= 8; | 
 | 1071 | 	buff->zctrl.next_out++; | 
 | 1072 |     } | 
 | 1073 |  | 
 | 1074 |     return; | 
 | 1075 | } | 
 | 1076 |  | 
 | 1077 | /** | 
 | 1078 |  * | 
 | 1079 |  * xmlFreeZMemBuff | 
 | 1080 |  * @buff:  The memory buffer context to clear | 
 | 1081 |  * | 
 | 1082 |  * Release all the resources associated with the compressed memory buffer. | 
 | 1083 |  */ | 
 | 1084 | static void | 
 | 1085 | xmlFreeZMemBuff( xmlZMemBuffPtr buff ) { | 
| William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1086 |  | 
 | 1087 | #ifdef DEBUG_HTTP | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1088 |     int z_err; | 
| William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1089 | #endif | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1090 |  | 
 | 1091 |     if ( buff == NULL ) | 
 | 1092 | 	return; | 
 | 1093 |  | 
 | 1094 |     xmlFree( buff->zbuff ); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1095 | #ifdef DEBUG_HTTP | 
| William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1096 |     z_err = deflateEnd( &buff->zctrl ); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1097 |     if ( z_err != Z_OK ) | 
 | 1098 | 	xmlGenericError( xmlGenericErrorContext, | 
 | 1099 | 			"xmlFreeZMemBuff:  Error releasing zlib context:  %d\n", | 
 | 1100 | 			z_err ); | 
| William M. Brack | 78637da | 2003-07-31 14:47:38 +0000 | [diff] [blame] | 1101 | #else | 
 | 1102 |     deflateEnd( &buff->zctrl ); | 
| William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1103 | #endif | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1104 |  | 
 | 1105 |     xmlFree( buff ); | 
 | 1106 |     return; | 
 | 1107 | } | 
 | 1108 |  | 
 | 1109 | /** | 
 | 1110 |  * xmlCreateZMemBuff | 
 | 1111 |  *@compression:	Compression value to use | 
 | 1112 |  * | 
 | 1113 |  * Create a memory buffer to hold the compressed XML document.  The | 
 | 1114 |  * compressed document in memory will end up being identical to what | 
 | 1115 |  * would be created if gzopen/gzwrite/gzclose were being used to  | 
 | 1116 |  * write the document to disk.  The code for the header/trailer data to | 
 | 1117 |  * the compression is plagiarized from the zlib source files. | 
 | 1118 |  */ | 
 | 1119 | static void * | 
 | 1120 | xmlCreateZMemBuff( int compression ) { | 
 | 1121 |  | 
 | 1122 |     int			z_err; | 
 | 1123 |     int			hdr_lgth; | 
 | 1124 |     xmlZMemBuffPtr	buff = NULL; | 
 | 1125 |  | 
 | 1126 |     if ( ( compression < 1 ) || ( compression > 9 ) ) | 
 | 1127 | 	return ( NULL ); | 
 | 1128 |  | 
 | 1129 |     /*  Create the control and data areas  */ | 
 | 1130 |  | 
 | 1131 |     buff = xmlMalloc( sizeof( xmlZMemBuff ) ); | 
 | 1132 |     if ( buff == NULL ) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 1133 | 	xmlIOErrMemory("creating buffer context"); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1134 | 	return ( NULL ); | 
 | 1135 |     } | 
 | 1136 |  | 
 | 1137 |     (void)memset( buff, 0, sizeof( xmlZMemBuff ) ); | 
 | 1138 |     buff->size = INIT_HTTP_BUFF_SIZE; | 
 | 1139 |     buff->zbuff = xmlMalloc( buff->size ); | 
 | 1140 |     if ( buff->zbuff == NULL ) { | 
 | 1141 | 	xmlFreeZMemBuff( buff ); | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 1142 | 	xmlIOErrMemory("creating buffer"); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1143 | 	return ( NULL ); | 
 | 1144 |     } | 
 | 1145 |  | 
 | 1146 |     z_err = deflateInit2( &buff->zctrl, compression, Z_DEFLATED, | 
 | 1147 | 			    DFLT_WBITS, DFLT_MEM_LVL, Z_DEFAULT_STRATEGY ); | 
 | 1148 |     if ( z_err != Z_OK ) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 1149 | 	xmlChar msg[500]; | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1150 | 	xmlFreeZMemBuff( buff ); | 
 | 1151 | 	buff = NULL; | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 1152 | 	xmlStrPrintf(msg, 500, | 
 | 1153 | 		    (const xmlChar *) "xmlCreateZMemBuff:  %s %d\n", | 
 | 1154 | 		    "Error initializing compression context.  ZLIB error:", | 
 | 1155 | 		    z_err ); | 
 | 1156 | 	xmlIOErr(XML_IO_WRITE, (const char *) msg); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1157 | 	return ( NULL ); | 
 | 1158 |     } | 
 | 1159 |  | 
 | 1160 |     /*  Set the header data.  The CRC will be needed for the trailer  */ | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1161 |     buff->crc = crc32( 0L, Z_NULL, 0 ); | 
| Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1162 |     hdr_lgth = snprintf( (char *)buff->zbuff, buff->size, | 
 | 1163 | 			"%c%c%c%c%c%c%c%c%c%c", | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1164 | 			GZ_MAGIC1, GZ_MAGIC2, Z_DEFLATED,  | 
 | 1165 | 			0, 0, 0, 0, 0, 0, LXML_ZLIB_OS_CODE ); | 
 | 1166 |     buff->zctrl.next_out  = buff->zbuff + hdr_lgth; | 
 | 1167 |     buff->zctrl.avail_out = buff->size - hdr_lgth; | 
 | 1168 |  | 
 | 1169 |     return ( buff ); | 
 | 1170 | } | 
 | 1171 |  | 
 | 1172 | /** | 
 | 1173 |  * xmlZMemBuffExtend | 
 | 1174 |  * @buff:  Buffer used to compress and consolidate data. | 
 | 1175 |  * @ext_amt:   Number of bytes to extend the buffer. | 
 | 1176 |  * | 
 | 1177 |  * Extend the internal buffer used to store the compressed data by the | 
 | 1178 |  * specified amount. | 
 | 1179 |  * | 
 | 1180 |  * Returns 0 on success or -1 on failure to extend the buffer.  On failure | 
 | 1181 |  * the original buffer still exists at the original size. | 
 | 1182 |  */ | 
 | 1183 | static int | 
 | 1184 | xmlZMemBuffExtend( xmlZMemBuffPtr buff, size_t ext_amt ) { | 
 | 1185 |  | 
 | 1186 |     int			rc = -1; | 
 | 1187 |     size_t		new_size; | 
 | 1188 |     size_t		cur_used; | 
 | 1189 |  | 
 | 1190 |     unsigned char *	tmp_ptr = NULL; | 
 | 1191 |  | 
 | 1192 |     if ( buff == NULL ) | 
 | 1193 | 	return ( -1 ); | 
 | 1194 |  | 
 | 1195 |     else if ( ext_amt == 0 ) | 
 | 1196 | 	return ( 0 ); | 
 | 1197 |  | 
 | 1198 |     cur_used = buff->zctrl.next_out - buff->zbuff; | 
 | 1199 |     new_size = buff->size + ext_amt; | 
 | 1200 |  | 
 | 1201 | #ifdef DEBUG_HTTP | 
 | 1202 |     if ( cur_used > new_size )  | 
 | 1203 | 	xmlGenericError( xmlGenericErrorContext, | 
 | 1204 | 			"xmlZMemBuffExtend:  %s\n%s %d bytes.\n", | 
 | 1205 | 			"Buffer overwrite detected during compressed memory", | 
 | 1206 | 			"buffer extension.  Overflowed by",  | 
 | 1207 | 			(cur_used - new_size ) ); | 
 | 1208 | #endif | 
 | 1209 |  | 
 | 1210 |     tmp_ptr = xmlRealloc( buff->zbuff, new_size ); | 
 | 1211 |     if ( tmp_ptr != NULL ) { | 
 | 1212 | 	rc = 0; | 
 | 1213 | 	buff->size  = new_size; | 
 | 1214 | 	buff->zbuff = tmp_ptr; | 
 | 1215 | 	buff->zctrl.next_out  = tmp_ptr + cur_used; | 
 | 1216 | 	buff->zctrl.avail_out = new_size - cur_used; | 
 | 1217 |     } | 
 | 1218 |     else { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 1219 | 	xmlChar msg[500]; | 
 | 1220 | 	xmlStrPrintf(msg, 500, | 
 | 1221 | 		    (const xmlChar *) "xmlZMemBuffExtend:  %s %lu bytes.\n", | 
 | 1222 | 		    "Allocation failure extending output buffer to", | 
 | 1223 | 		    new_size ); | 
 | 1224 | 	xmlIOErr(XML_IO_WRITE, (const char *) msg); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1225 |     } | 
 | 1226 |  | 
 | 1227 |     return ( rc ); | 
 | 1228 | } | 
 | 1229 |  | 
 | 1230 | /** | 
 | 1231 |  * xmlZMemBuffAppend | 
 | 1232 |  * @buff:  Buffer used to compress and consolidate data | 
 | 1233 |  * @src:   Uncompressed source content to append to buffer | 
 | 1234 |  * @len:   Length of source data to append to buffer | 
 | 1235 |  * | 
 | 1236 |  * Compress and append data to the internal buffer.  The data buffer | 
 | 1237 |  * will be expanded if needed to store the additional data. | 
 | 1238 |  * | 
 | 1239 |  * Returns the number of bytes appended to the buffer or -1 on error. | 
 | 1240 |  */ | 
 | 1241 | static int | 
 | 1242 | xmlZMemBuffAppend( xmlZMemBuffPtr buff, const char * src, int len ) { | 
 | 1243 |  | 
 | 1244 |     int		z_err; | 
 | 1245 |     size_t	min_accept; | 
 | 1246 |  | 
 | 1247 |     if ( ( buff == NULL ) || ( src == NULL ) ) | 
 | 1248 | 	return ( -1 ); | 
 | 1249 |  | 
 | 1250 |     buff->zctrl.avail_in = len; | 
 | 1251 |     buff->zctrl.next_in  = (unsigned char *)src; | 
 | 1252 |     while ( buff->zctrl.avail_in > 0 ) { | 
 | 1253 | 	/* | 
 | 1254 | 	**  Extend the buffer prior to deflate call if a reasonable amount | 
 | 1255 | 	**  of output buffer space is not available. | 
 | 1256 | 	*/ | 
 | 1257 | 	min_accept = buff->zctrl.avail_in / DFLT_ZLIB_RATIO; | 
 | 1258 | 	if ( buff->zctrl.avail_out <= min_accept ) { | 
 | 1259 | 	    if ( xmlZMemBuffExtend( buff, buff->size ) == -1 ) | 
 | 1260 | 		return ( -1 ); | 
 | 1261 | 	} | 
 | 1262 |  | 
 | 1263 | 	z_err = deflate( &buff->zctrl, Z_NO_FLUSH ); | 
 | 1264 | 	if ( z_err != Z_OK ) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 1265 | 	    xmlChar msg[500]; | 
 | 1266 | 	    xmlStrPrintf(msg, 500, | 
 | 1267 | 			(const xmlChar *) "xmlZMemBuffAppend:  %s %d %s - %d", | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1268 | 			"Compression error while appending", | 
 | 1269 | 			len, "bytes to buffer.  ZLIB error", z_err ); | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 1270 | 	    xmlIOErr(XML_IO_WRITE, (const char *) msg); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1271 | 	    return ( -1 ); | 
 | 1272 | 	} | 
 | 1273 |     } | 
 | 1274 |  | 
 | 1275 |     buff->crc = crc32( buff->crc, (unsigned char *)src, len ); | 
 | 1276 |  | 
 | 1277 |     return ( len ); | 
 | 1278 | } | 
 | 1279 |  | 
 | 1280 | /** | 
 | 1281 |  * xmlZMemBuffGetContent | 
 | 1282 |  * @buff:  Compressed memory content buffer | 
 | 1283 |  * @data_ref:  Pointer reference to point to compressed content | 
 | 1284 |  * | 
 | 1285 |  * Flushes the compression buffers, appends gzip file trailers and | 
 | 1286 |  * returns the compressed content and length of the compressed data. | 
 | 1287 |  * NOTE:  The gzip trailer code here is plagiarized from zlib source. | 
 | 1288 |  * | 
 | 1289 |  * Returns the length of the compressed data or -1 on error. | 
 | 1290 |  */ | 
 | 1291 | static int | 
 | 1292 | xmlZMemBuffGetContent( xmlZMemBuffPtr buff, char ** data_ref ) { | 
 | 1293 |  | 
 | 1294 |     int		zlgth = -1; | 
 | 1295 |     int		z_err; | 
 | 1296 |  | 
 | 1297 |     if ( ( buff == NULL ) || ( data_ref == NULL ) ) | 
 | 1298 | 	return ( -1 ); | 
 | 1299 |  | 
 | 1300 |     /*  Need to loop until compression output buffers are flushed  */ | 
 | 1301 |  | 
 | 1302 |     do | 
 | 1303 |     { | 
 | 1304 | 	z_err = deflate( &buff->zctrl, Z_FINISH ); | 
 | 1305 | 	if ( z_err == Z_OK ) { | 
 | 1306 | 	    /*  In this case Z_OK means more buffer space needed  */ | 
 | 1307 |  | 
 | 1308 | 	    if ( xmlZMemBuffExtend( buff, buff->size ) == -1 ) | 
 | 1309 | 		return ( -1 ); | 
 | 1310 | 	} | 
 | 1311 |     } | 
 | 1312 |     while ( z_err == Z_OK ); | 
 | 1313 |  | 
 | 1314 |     /*  If the compression state is not Z_STREAM_END, some error occurred  */ | 
 | 1315 |  | 
 | 1316 |     if ( z_err == Z_STREAM_END ) { | 
 | 1317 |  | 
 | 1318 | 	/*  Need to append the gzip data trailer  */ | 
 | 1319 |  | 
 | 1320 | 	if ( buff->zctrl.avail_out < ( 2 * sizeof( unsigned long ) ) ) { | 
 | 1321 | 	    if ( xmlZMemBuffExtend(buff, (2 * sizeof(unsigned long))) == -1 ) | 
 | 1322 | 		return ( -1 ); | 
 | 1323 | 	} | 
 | 1324 |  | 
 | 1325 | 	/* | 
 | 1326 | 	**  For whatever reason, the CRC and length data are pushed out | 
 | 1327 | 	**  in reverse byte order.  So a memcpy can't be used here. | 
 | 1328 | 	*/ | 
 | 1329 |  | 
 | 1330 | 	append_reverse_ulong( buff, buff->crc ); | 
 | 1331 | 	append_reverse_ulong( buff, buff->zctrl.total_in ); | 
 | 1332 |  | 
 | 1333 | 	zlgth = buff->zctrl.next_out - buff->zbuff; | 
 | 1334 | 	*data_ref = (char *)buff->zbuff; | 
 | 1335 |     } | 
 | 1336 |  | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 1337 |     else { | 
 | 1338 | 	xmlChar msg[500]; | 
 | 1339 | 	xmlStrPrintf(msg, 500, | 
 | 1340 | 		    (const xmlChar *) "xmlZMemBuffGetContent:  %s - %d\n", | 
 | 1341 | 		    "Error flushing zlib buffers.  Error code", z_err ); | 
 | 1342 | 	xmlIOErr(XML_IO_WRITE, (const char *) msg); | 
 | 1343 |     } | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1344 |      | 
 | 1345 |     return ( zlgth ); | 
 | 1346 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1347 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1348 | #endif  /*  HAVE_ZLIB_H  */ | 
 | 1349 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1350 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1351 | /** | 
 | 1352 |  * xmlFreeHTTPWriteCtxt | 
 | 1353 |  * @ctxt:  Context to cleanup | 
 | 1354 |  * | 
 | 1355 |  * Free allocated memory and reclaim system resources. | 
 | 1356 |  * | 
 | 1357 |  * No return value. | 
 | 1358 |  */ | 
 | 1359 | static void | 
 | 1360 | xmlFreeHTTPWriteCtxt( xmlIOHTTPWriteCtxtPtr ctxt ) | 
 | 1361 | { | 
 | 1362 |     if ( ctxt->uri != NULL ) | 
| Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 1363 | 	xmlFree( ctxt->uri ); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1364 |  | 
 | 1365 |     if ( ctxt->doc_buff != NULL ) { | 
 | 1366 |  | 
 | 1367 | #ifdef HAVE_ZLIB_H | 
 | 1368 | 	if ( ctxt->compression > 0 ) { | 
 | 1369 | 	    xmlFreeZMemBuff( ctxt->doc_buff ); | 
 | 1370 | 	} | 
 | 1371 | 	else | 
 | 1372 | #endif | 
 | 1373 | 	{ | 
 | 1374 | 	    xmlOutputBufferClose( ctxt->doc_buff ); | 
 | 1375 | 	} | 
 | 1376 |     } | 
 | 1377 |  | 
| Daniel Veillard | 819d5cb | 2002-10-14 11:15:18 +0000 | [diff] [blame] | 1378 |     xmlFree( ctxt ); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1379 |     return; | 
 | 1380 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1381 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1382 |  | 
 | 1383 |  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1384 | /** | 
 | 1385 |  * xmlIOHTTPMatch: | 
 | 1386 |  * @filename:  the URI for matching | 
 | 1387 |  * | 
 | 1388 |  * check if the URI matches an HTTP one | 
 | 1389 |  * | 
 | 1390 |  * Returns 1 if matches, 0 otherwise | 
 | 1391 |  */ | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 1392 | int | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1393 | xmlIOHTTPMatch (const char *filename) { | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 1394 |     if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "http://", 7)) | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1395 | 	return(1); | 
 | 1396 |     return(0); | 
 | 1397 | } | 
 | 1398 |  | 
 | 1399 | /** | 
 | 1400 |  * xmlIOHTTPOpen: | 
 | 1401 |  * @filename:  the URI for matching | 
 | 1402 |  * | 
 | 1403 |  * open an HTTP I/O channel | 
 | 1404 |  * | 
 | 1405 |  * Returns an I/O context or NULL in case of error | 
 | 1406 |  */ | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 1407 | void * | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1408 | xmlIOHTTPOpen (const char *filename) { | 
 | 1409 |     return(xmlNanoHTTPOpen(filename, NULL)); | 
 | 1410 | } | 
 | 1411 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1412 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1413 | /** | 
| Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1414 |  * xmlIOHTTPOpenW: | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1415 |  * @post_uri:  The destination URI for the document | 
 | 1416 |  * @compression:  The compression desired for the document. | 
 | 1417 |  * | 
 | 1418 |  * Open a temporary buffer to collect the document for a subsequent HTTP POST | 
 | 1419 |  * request.  Non-static as is called from the output buffer creation routine. | 
 | 1420 |  * | 
 | 1421 |  * Returns an I/O context or NULL in case of error. | 
 | 1422 |  */ | 
 | 1423 |  | 
 | 1424 | void * | 
| Daniel Veillard | 572577e | 2002-01-18 16:23:55 +0000 | [diff] [blame] | 1425 | xmlIOHTTPOpenW(const char *post_uri, int compression) | 
 | 1426 | { | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1427 |  | 
| Daniel Veillard | 572577e | 2002-01-18 16:23:55 +0000 | [diff] [blame] | 1428 |     xmlIOHTTPWriteCtxtPtr ctxt = NULL; | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1429 |  | 
| Daniel Veillard | 572577e | 2002-01-18 16:23:55 +0000 | [diff] [blame] | 1430 |     if (post_uri == NULL) | 
 | 1431 |         return (NULL); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1432 |  | 
| Daniel Veillard | 572577e | 2002-01-18 16:23:55 +0000 | [diff] [blame] | 1433 |     ctxt = xmlMalloc(sizeof(xmlIOHTTPWriteCtxt)); | 
 | 1434 |     if (ctxt == NULL) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 1435 | 	xmlIOErrMemory("creating HTTP output context"); | 
| Daniel Veillard | 572577e | 2002-01-18 16:23:55 +0000 | [diff] [blame] | 1436 |         return (NULL); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1437 |     } | 
 | 1438 |  | 
| Daniel Veillard | 572577e | 2002-01-18 16:23:55 +0000 | [diff] [blame] | 1439 |     (void) memset(ctxt, 0, sizeof(xmlIOHTTPWriteCtxt)); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1440 |  | 
| Daniel Veillard | 572577e | 2002-01-18 16:23:55 +0000 | [diff] [blame] | 1441 |     ctxt->uri = (char *) xmlStrdup((const xmlChar *)post_uri); | 
 | 1442 |     if (ctxt->uri == NULL) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 1443 | 	xmlIOErrMemory("copying URI"); | 
| Daniel Veillard | 572577e | 2002-01-18 16:23:55 +0000 | [diff] [blame] | 1444 |         xmlFreeHTTPWriteCtxt(ctxt); | 
 | 1445 |         return (NULL); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1446 |     } | 
 | 1447 |  | 
 | 1448 |     /* | 
| Daniel Veillard | 572577e | 2002-01-18 16:23:55 +0000 | [diff] [blame] | 1449 |      * **  Since the document length is required for an HTTP post, | 
 | 1450 |      * **  need to put the document into a buffer.  A memory buffer | 
 | 1451 |      * **  is being used to avoid pushing the data to disk and back. | 
 | 1452 |      */ | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1453 |  | 
 | 1454 | #ifdef HAVE_ZLIB_H | 
| Daniel Veillard | 572577e | 2002-01-18 16:23:55 +0000 | [diff] [blame] | 1455 |     if ((compression > 0) && (compression <= 9)) { | 
 | 1456 |  | 
 | 1457 |         ctxt->compression = compression; | 
 | 1458 |         ctxt->doc_buff = xmlCreateZMemBuff(compression); | 
 | 1459 |     } else | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1460 | #endif | 
 | 1461 |     { | 
| Daniel Veillard | 572577e | 2002-01-18 16:23:55 +0000 | [diff] [blame] | 1462 |         /*  Any character conversions should have been done before this  */ | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1463 |  | 
| Daniel Veillard | 572577e | 2002-01-18 16:23:55 +0000 | [diff] [blame] | 1464 |         ctxt->doc_buff = xmlAllocOutputBuffer(NULL); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1465 |     } | 
 | 1466 |  | 
| Daniel Veillard | 572577e | 2002-01-18 16:23:55 +0000 | [diff] [blame] | 1467 |     if (ctxt->doc_buff == NULL) { | 
 | 1468 |         xmlFreeHTTPWriteCtxt(ctxt); | 
 | 1469 |         ctxt = NULL; | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1470 |     } | 
 | 1471 |  | 
| Daniel Veillard | 572577e | 2002-01-18 16:23:55 +0000 | [diff] [blame] | 1472 |     return (ctxt); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1473 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1474 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1475 | 				 | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1476 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1477 | /** | 
 | 1478 |  * xmlIOHTTPDfltOpenW | 
 | 1479 |  * @post_uri:  The destination URI for this document. | 
 | 1480 |  * | 
 | 1481 |  * Calls xmlIOHTTPOpenW with no compression to set up for a subsequent | 
 | 1482 |  * HTTP post command.  This function should generally not be used as | 
 | 1483 |  * the open callback is short circuited in xmlOutputBufferCreateFile. | 
 | 1484 |  * | 
 | 1485 |  * Returns a pointer to the new IO context. | 
 | 1486 |  */ | 
 | 1487 | static void * | 
 | 1488 | xmlIOHTTPDfltOpenW( const char * post_uri ) { | 
 | 1489 |     return ( xmlIOHTTPOpenW( post_uri, 0 ) ); | 
 | 1490 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1491 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1492 |  | 
 | 1493 | /** | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1494 |  * xmlIOHTTPRead: | 
 | 1495 |  * @context:  the I/O context | 
 | 1496 |  * @buffer:  where to drop data | 
 | 1497 |  * @len:  number of bytes to write | 
 | 1498 |  * | 
 | 1499 |  * Read @len bytes to @buffer from the I/O channel. | 
 | 1500 |  * | 
 | 1501 |  * Returns the number of bytes written | 
 | 1502 |  */ | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 1503 | int  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1504 | xmlIOHTTPRead(void * context, char * buffer, int len) { | 
 | 1505 |     return(xmlNanoHTTPRead(context, &buffer[0], len)); | 
 | 1506 | } | 
 | 1507 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1508 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1509 | /** | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1510 |  * xmlIOHTTPWrite | 
 | 1511 |  * @context:  previously opened writing context | 
 | 1512 |  * @buffer:   data to output to temporary buffer | 
 | 1513 |  * @len:      bytes to output | 
 | 1514 |  * | 
 | 1515 |  * Collect data from memory buffer into a temporary file for later | 
 | 1516 |  * processing. | 
 | 1517 |  * | 
 | 1518 |  * Returns number of bytes written. | 
 | 1519 |  */ | 
 | 1520 |  | 
 | 1521 | static int | 
 | 1522 | xmlIOHTTPWrite( void * context, const char * buffer, int len ) {  | 
 | 1523 |  | 
 | 1524 |     xmlIOHTTPWriteCtxtPtr	ctxt = context; | 
 | 1525 |  | 
 | 1526 |     if ( ( ctxt == NULL ) || ( ctxt->doc_buff == NULL ) || ( buffer == NULL ) ) | 
 | 1527 | 	return ( -1 ); | 
 | 1528 |  | 
 | 1529 |     if ( len > 0 ) { | 
 | 1530 |  | 
 | 1531 | 	/*  Use gzwrite or fwrite as previously setup in the open call  */ | 
 | 1532 |  | 
 | 1533 | #ifdef HAVE_ZLIB_H | 
 | 1534 | 	if ( ctxt->compression > 0 )  | 
 | 1535 | 	    len = xmlZMemBuffAppend( ctxt->doc_buff, buffer, len ); | 
 | 1536 |  | 
 | 1537 | 	else | 
 | 1538 | #endif | 
 | 1539 | 	    len = xmlOutputBufferWrite( ctxt->doc_buff, len, buffer ); | 
 | 1540 |  | 
 | 1541 | 	if ( len < 0 ) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 1542 | 	    xmlChar msg[500]; | 
 | 1543 | 	    xmlStrPrintf(msg, 500, | 
 | 1544 | 			(const xmlChar *) "xmlIOHTTPWrite:  %s\n%s '%s'.\n", | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1545 | 			"Error appending to internal buffer.", | 
 | 1546 | 			"Error sending document to URI", | 
 | 1547 | 			ctxt->uri ); | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 1548 | 	    xmlIOErr(XML_IO_WRITE, (const char *) msg); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1549 | 	} | 
 | 1550 |     } | 
 | 1551 |  | 
 | 1552 |     return ( len ); | 
 | 1553 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1554 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1555 |  | 
 | 1556 |  | 
 | 1557 | /** | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1558 |  * xmlIOHTTPClose: | 
 | 1559 |  * @context:  the I/O context | 
 | 1560 |  * | 
 | 1561 |  * Close an HTTP I/O channel | 
| Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1562 |  * | 
 | 1563 |  * Returns 0 | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1564 |  */ | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 1565 | int | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1566 | xmlIOHTTPClose (void * context) { | 
 | 1567 |     xmlNanoHTTPClose(context); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1568 |     return 0; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1569 | } | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1570 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1571 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1572 | /** | 
 | 1573 |  * xmlIOHTTCloseWrite | 
 | 1574 |  * @context:  The I/O context | 
 | 1575 |  * @http_mthd: The HTTP method to be used when sending the data | 
 | 1576 |  * | 
 | 1577 |  * Close the transmit HTTP I/O channel and actually send the data. | 
 | 1578 |  */ | 
 | 1579 | static int | 
 | 1580 | xmlIOHTTPCloseWrite( void * context, const char * http_mthd ) { | 
 | 1581 |  | 
 | 1582 |     int				close_rc = -1; | 
 | 1583 |     int				http_rtn = 0; | 
 | 1584 |     int				content_lgth = 0; | 
 | 1585 |     xmlIOHTTPWriteCtxtPtr	ctxt = context; | 
 | 1586 |  | 
 | 1587 |     char *			http_content = NULL; | 
 | 1588 |     char *			content_encoding = NULL; | 
 | 1589 |     char *			content_type = (char *) "text/xml"; | 
 | 1590 |     void *			http_ctxt = NULL; | 
 | 1591 |  | 
 | 1592 |     if ( ( ctxt == NULL ) || ( http_mthd == NULL ) ) | 
 | 1593 | 	return ( -1 ); | 
 | 1594 |  | 
 | 1595 |     /*  Retrieve the content from the appropriate buffer  */ | 
 | 1596 |  | 
 | 1597 | #ifdef HAVE_ZLIB_H | 
 | 1598 |  | 
 | 1599 |     if ( ctxt->compression > 0 ) { | 
 | 1600 | 	content_lgth = xmlZMemBuffGetContent( ctxt->doc_buff, &http_content ); | 
 | 1601 | 	content_encoding = (char *) "Content-Encoding: gzip"; | 
 | 1602 |     } | 
 | 1603 |     else | 
 | 1604 | #endif | 
 | 1605 |     { | 
 | 1606 | 	/*  Pull the data out of the memory output buffer  */ | 
 | 1607 |  | 
 | 1608 | 	xmlOutputBufferPtr	dctxt = ctxt->doc_buff; | 
 | 1609 | 	http_content = (char *)dctxt->buffer->content; | 
 | 1610 | 	content_lgth = dctxt->buffer->use; | 
 | 1611 |     } | 
 | 1612 |  | 
 | 1613 |     if ( http_content == NULL ) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 1614 | 	xmlChar msg[500]; | 
 | 1615 | 	xmlStrPrintf(msg, 500, | 
 | 1616 | 		     (const xmlChar *) "xmlIOHTTPCloseWrite:  %s '%s' %s '%s'.\n", | 
 | 1617 | 		     "Error retrieving content.\nUnable to", | 
 | 1618 | 		     http_mthd, "data to URI", ctxt->uri ); | 
 | 1619 | 	xmlIOErr(XML_IO_WRITE, (const char *) msg); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1620 |     } | 
 | 1621 |  | 
 | 1622 |     else { | 
 | 1623 |  | 
 | 1624 | 	http_ctxt = xmlNanoHTTPMethod( ctxt->uri, http_mthd, http_content, | 
 | 1625 | 					&content_type, content_encoding,  | 
 | 1626 | 					content_lgth ); | 
 | 1627 |  | 
 | 1628 | 	if ( http_ctxt != NULL ) { | 
 | 1629 | #ifdef DEBUG_HTTP | 
 | 1630 | 	    /*  If testing/debugging - dump reply with request content  */ | 
 | 1631 |  | 
 | 1632 | 	    FILE *	tst_file = NULL; | 
 | 1633 | 	    char	buffer[ 4096 ]; | 
 | 1634 | 	    char *	dump_name = NULL; | 
 | 1635 | 	    int		avail; | 
 | 1636 |  | 
 | 1637 | 	    xmlGenericError( xmlGenericErrorContext, | 
 | 1638 | 			"xmlNanoHTTPCloseWrite:  HTTP %s to\n%s returned %d.\n", | 
 | 1639 | 			http_mthd, ctxt->uri, | 
 | 1640 | 			xmlNanoHTTPReturnCode( http_ctxt ) ); | 
 | 1641 |  | 
 | 1642 | 	    /* | 
 | 1643 | 	    **  Since either content or reply may be gzipped, | 
 | 1644 | 	    **  dump them to separate files instead of the  | 
 | 1645 | 	    **  standard error context. | 
 | 1646 | 	    */ | 
 | 1647 |  | 
 | 1648 | 	    dump_name = tempnam( NULL, "lxml" ); | 
 | 1649 | 	    if ( dump_name != NULL ) { | 
| Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1650 | 		(void)snprintf( buffer, sizeof(buffer), "%s.content", dump_name ); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1651 |  | 
| Daniel Veillard | cbbd78d | 2003-07-20 15:21:30 +0000 | [diff] [blame] | 1652 | 		tst_file = fopen( buffer, "wb" ); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1653 | 		if ( tst_file != NULL ) { | 
 | 1654 | 		    xmlGenericError( xmlGenericErrorContext, | 
 | 1655 | 			"Transmitted content saved in file:  %s\n", buffer ); | 
 | 1656 |  | 
 | 1657 | 		    fwrite( http_content, sizeof( char ), | 
 | 1658 | 					content_lgth, tst_file ); | 
 | 1659 | 		    fclose( tst_file ); | 
 | 1660 | 		} | 
 | 1661 |  | 
| Aleksey Sanin | 49cc975 | 2002-06-14 17:07:10 +0000 | [diff] [blame] | 1662 | 		(void)snprintf( buffer, sizeof(buffer), "%s.reply", dump_name ); | 
| Daniel Veillard | cbbd78d | 2003-07-20 15:21:30 +0000 | [diff] [blame] | 1663 | 		tst_file = fopen( buffer, "wb" ); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1664 | 		if ( tst_file != NULL ) { | 
 | 1665 | 		    xmlGenericError( xmlGenericErrorContext, | 
 | 1666 | 			"Reply content saved in file:  %s\n", buffer ); | 
 | 1667 |  | 
 | 1668 |  | 
 | 1669 | 		    while ( (avail = xmlNanoHTTPRead( http_ctxt, | 
 | 1670 | 					buffer, sizeof( buffer ) )) > 0 ) { | 
 | 1671 |  | 
 | 1672 | 			fwrite( buffer, sizeof( char ), avail, tst_file ); | 
 | 1673 | 		    } | 
 | 1674 |  | 
 | 1675 | 		    fclose( tst_file ); | 
 | 1676 | 		} | 
 | 1677 |  | 
 | 1678 | 		free( dump_name ); | 
 | 1679 | 	    } | 
 | 1680 | #endif  /*  DEBUG_HTTP  */ | 
 | 1681 |  | 
 | 1682 | 	    http_rtn = xmlNanoHTTPReturnCode( http_ctxt ); | 
 | 1683 | 	    if ( ( http_rtn >= 200 ) && ( http_rtn < 300 ) ) | 
 | 1684 | 		close_rc = 0; | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 1685 | 	    else { | 
 | 1686 |                 xmlChar msg[500]; | 
 | 1687 |                 xmlStrPrintf(msg, 500, | 
 | 1688 |     (const xmlChar *) "xmlIOHTTPCloseWrite: HTTP '%s' of %d %s\n'%s' %s %d\n", | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1689 | 			    http_mthd, content_lgth, | 
 | 1690 | 			    "bytes to URI", ctxt->uri, | 
 | 1691 | 			    "failed.  HTTP return code:", http_rtn ); | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 1692 | 		xmlIOErr(XML_IO_WRITE, (const char *) msg); | 
 | 1693 |             } | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1694 |  | 
 | 1695 | 	    xmlNanoHTTPClose( http_ctxt ); | 
 | 1696 | 	    xmlFree( content_type ); | 
 | 1697 | 	} | 
 | 1698 |     } | 
 | 1699 |  | 
 | 1700 |     /*  Final cleanups  */ | 
 | 1701 |  | 
 | 1702 |     xmlFreeHTTPWriteCtxt( ctxt ); | 
 | 1703 |  | 
 | 1704 |     return ( close_rc ); | 
 | 1705 | } | 
 | 1706 |  | 
 | 1707 | /** | 
 | 1708 |  * xmlIOHTTPClosePut | 
 | 1709 |  * | 
 | 1710 |  * @context:  The I/O context | 
 | 1711 |  * | 
 | 1712 |  * Close the transmit HTTP I/O channel and actually send data using a PUT | 
 | 1713 |  * HTTP method. | 
 | 1714 |  */ | 
 | 1715 | static int | 
 | 1716 | xmlIOHTTPClosePut( void * ctxt ) { | 
 | 1717 |     return ( xmlIOHTTPCloseWrite( ctxt, "PUT" ) ); | 
 | 1718 | } | 
 | 1719 |  | 
 | 1720 |  | 
 | 1721 | /** | 
 | 1722 |  * xmlIOHTTPClosePost | 
 | 1723 |  * | 
 | 1724 |  * @context:  The I/O context | 
 | 1725 |  * | 
 | 1726 |  * Close the transmit HTTP I/O channel and actually send data using a POST | 
 | 1727 |  * HTTP method. | 
 | 1728 |  */ | 
 | 1729 | static int | 
 | 1730 | xmlIOHTTPClosePost( void * ctxt ) { | 
 | 1731 |     return ( xmlIOHTTPCloseWrite( ctxt, "POST" ) ); | 
 | 1732 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1733 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1734 |  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1735 | #endif /* LIBXML_HTTP_ENABLED */ | 
 | 1736 |  | 
 | 1737 | #ifdef LIBXML_FTP_ENABLED | 
 | 1738 | /************************************************************************ | 
 | 1739 |  *									* | 
 | 1740 |  *			I/O for FTP file accesses			* | 
 | 1741 |  *									* | 
 | 1742 |  ************************************************************************/ | 
 | 1743 | /** | 
 | 1744 |  * xmlIOFTPMatch: | 
 | 1745 |  * @filename:  the URI for matching | 
 | 1746 |  * | 
 | 1747 |  * check if the URI matches an FTP one | 
 | 1748 |  * | 
 | 1749 |  * Returns 1 if matches, 0 otherwise | 
 | 1750 |  */ | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 1751 | int | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1752 | xmlIOFTPMatch (const char *filename) { | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 1753 |     if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "ftp://", 6)) | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1754 | 	return(1); | 
 | 1755 |     return(0); | 
 | 1756 | } | 
 | 1757 |  | 
 | 1758 | /** | 
 | 1759 |  * xmlIOFTPOpen: | 
 | 1760 |  * @filename:  the URI for matching | 
 | 1761 |  * | 
 | 1762 |  * open an FTP I/O channel | 
 | 1763 |  * | 
 | 1764 |  * Returns an I/O context or NULL in case of error | 
 | 1765 |  */ | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 1766 | void * | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1767 | xmlIOFTPOpen (const char *filename) { | 
 | 1768 |     return(xmlNanoFTPOpen(filename)); | 
 | 1769 | } | 
 | 1770 |  | 
 | 1771 | /** | 
 | 1772 |  * xmlIOFTPRead: | 
 | 1773 |  * @context:  the I/O context | 
 | 1774 |  * @buffer:  where to drop data | 
 | 1775 |  * @len:  number of bytes to write | 
 | 1776 |  * | 
 | 1777 |  * Read @len bytes to @buffer from the I/O channel. | 
 | 1778 |  * | 
 | 1779 |  * Returns the number of bytes written | 
 | 1780 |  */ | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 1781 | int  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1782 | xmlIOFTPRead(void * context, char * buffer, int len) { | 
 | 1783 |     return(xmlNanoFTPRead(context, &buffer[0], len)); | 
 | 1784 | } | 
 | 1785 |  | 
 | 1786 | /** | 
 | 1787 |  * xmlIOFTPClose: | 
 | 1788 |  * @context:  the I/O context | 
 | 1789 |  * | 
 | 1790 |  * Close an FTP I/O channel | 
| Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1791 |  * | 
 | 1792 |  * Returns 0 | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1793 |  */ | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 1794 | int | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1795 | xmlIOFTPClose (void * context) { | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1796 |     return ( xmlNanoFTPClose(context) ); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1797 | } | 
 | 1798 | #endif /* LIBXML_FTP_ENABLED */ | 
 | 1799 |  | 
 | 1800 |  | 
 | 1801 | /** | 
 | 1802 |  * xmlRegisterInputCallbacks: | 
| Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1803 |  * @matchFunc:  the xmlInputMatchCallback | 
 | 1804 |  * @openFunc:  the xmlInputOpenCallback | 
 | 1805 |  * @readFunc:  the xmlInputReadCallback | 
 | 1806 |  * @closeFunc:  the xmlInputCloseCallback | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1807 |  * | 
 | 1808 |  * Register a new set of I/O callback for handling parser input. | 
 | 1809 |  * | 
 | 1810 |  * Returns the registered handler number or -1 in case of error | 
 | 1811 |  */ | 
 | 1812 | int | 
| Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1813 | xmlRegisterInputCallbacks(xmlInputMatchCallback matchFunc, | 
 | 1814 | 	xmlInputOpenCallback openFunc, xmlInputReadCallback readFunc, | 
 | 1815 | 	xmlInputCloseCallback closeFunc) { | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1816 |     if (xmlInputCallbackNr >= MAX_INPUT_CALLBACK) { | 
 | 1817 | 	return(-1); | 
 | 1818 |     } | 
| Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1819 |     xmlInputCallbackTable[xmlInputCallbackNr].matchcallback = matchFunc; | 
 | 1820 |     xmlInputCallbackTable[xmlInputCallbackNr].opencallback = openFunc; | 
 | 1821 |     xmlInputCallbackTable[xmlInputCallbackNr].readcallback = readFunc; | 
 | 1822 |     xmlInputCallbackTable[xmlInputCallbackNr].closecallback = closeFunc; | 
| Daniel Veillard | 9ec2653 | 2003-09-23 07:43:19 +0000 | [diff] [blame] | 1823 |     xmlInputCallbackInitialized = 1; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1824 |     return(xmlInputCallbackNr++); | 
 | 1825 | } | 
 | 1826 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1827 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1828 | /** | 
 | 1829 |  * xmlRegisterOutputCallbacks: | 
| Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1830 |  * @matchFunc:  the xmlOutputMatchCallback | 
 | 1831 |  * @openFunc:  the xmlOutputOpenCallback | 
 | 1832 |  * @writeFunc:  the xmlOutputWriteCallback | 
 | 1833 |  * @closeFunc:  the xmlOutputCloseCallback | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1834 |  * | 
 | 1835 |  * Register a new set of I/O callback for handling output. | 
 | 1836 |  * | 
 | 1837 |  * Returns the registered handler number or -1 in case of error | 
 | 1838 |  */ | 
 | 1839 | int | 
| Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1840 | xmlRegisterOutputCallbacks(xmlOutputMatchCallback matchFunc, | 
 | 1841 | 	xmlOutputOpenCallback openFunc, xmlOutputWriteCallback writeFunc, | 
 | 1842 | 	xmlOutputCloseCallback closeFunc) { | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1843 |     if (xmlOutputCallbackNr >= MAX_INPUT_CALLBACK) { | 
 | 1844 | 	return(-1); | 
 | 1845 |     } | 
| Daniel Veillard | 56a4cb8 | 2001-03-24 17:00:36 +0000 | [diff] [blame] | 1846 |     xmlOutputCallbackTable[xmlOutputCallbackNr].matchcallback = matchFunc; | 
 | 1847 |     xmlOutputCallbackTable[xmlOutputCallbackNr].opencallback = openFunc; | 
 | 1848 |     xmlOutputCallbackTable[xmlOutputCallbackNr].writecallback = writeFunc; | 
 | 1849 |     xmlOutputCallbackTable[xmlOutputCallbackNr].closecallback = closeFunc; | 
| Daniel Veillard | 9ec2653 | 2003-09-23 07:43:19 +0000 | [diff] [blame] | 1850 |     xmlOutputCallbackInitialized = 1; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1851 |     return(xmlOutputCallbackNr++); | 
 | 1852 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1853 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1854 |  | 
 | 1855 | /** | 
 | 1856 |  * xmlRegisterDefaultInputCallbacks: | 
 | 1857 |  * | 
 | 1858 |  * Registers the default compiled-in I/O handlers. | 
 | 1859 |  */ | 
 | 1860 | void | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1861 | xmlRegisterDefaultInputCallbacks | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1862 | (void) { | 
 | 1863 |     if (xmlInputCallbackInitialized) | 
 | 1864 | 	return; | 
 | 1865 |  | 
 | 1866 |     xmlRegisterInputCallbacks(xmlFileMatch, xmlFileOpen, | 
 | 1867 | 	                      xmlFileRead, xmlFileClose); | 
 | 1868 | #ifdef HAVE_ZLIB_H | 
 | 1869 |     xmlRegisterInputCallbacks(xmlGzfileMatch, xmlGzfileOpen, | 
 | 1870 | 	                      xmlGzfileRead, xmlGzfileClose); | 
 | 1871 | #endif /* HAVE_ZLIB_H */ | 
 | 1872 |  | 
 | 1873 | #ifdef LIBXML_HTTP_ENABLED | 
 | 1874 |     xmlRegisterInputCallbacks(xmlIOHTTPMatch, xmlIOHTTPOpen, | 
 | 1875 | 	                      xmlIOHTTPRead, xmlIOHTTPClose); | 
 | 1876 | #endif /* LIBXML_HTTP_ENABLED */ | 
 | 1877 |  | 
 | 1878 | #ifdef LIBXML_FTP_ENABLED | 
 | 1879 |     xmlRegisterInputCallbacks(xmlIOFTPMatch, xmlIOFTPOpen, | 
 | 1880 | 	                      xmlIOFTPRead, xmlIOFTPClose); | 
 | 1881 | #endif /* LIBXML_FTP_ENABLED */ | 
 | 1882 |     xmlInputCallbackInitialized = 1; | 
 | 1883 | } | 
 | 1884 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1885 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1886 | /** | 
 | 1887 |  * xmlRegisterDefaultOutputCallbacks: | 
 | 1888 |  * | 
 | 1889 |  * Registers the default compiled-in I/O handlers. | 
 | 1890 |  */ | 
 | 1891 | void | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1892 | xmlRegisterDefaultOutputCallbacks | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1893 | (void) { | 
 | 1894 |     if (xmlOutputCallbackInitialized) | 
 | 1895 | 	return; | 
 | 1896 |  | 
 | 1897 |     xmlRegisterOutputCallbacks(xmlFileMatch, xmlFileOpenW, | 
 | 1898 | 	                      xmlFileWrite, xmlFileClose); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1899 |  | 
 | 1900 | #ifdef LIBXML_HTTP_ENABLED | 
 | 1901 |     xmlRegisterOutputCallbacks(xmlIOHTTPMatch, xmlIOHTTPDfltOpenW, | 
 | 1902 | 	                       xmlIOHTTPWrite, xmlIOHTTPClosePut); | 
 | 1903 | #endif | 
 | 1904 |  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1905 | /********************************* | 
 | 1906 |  No way a-priori to distinguish between gzipped files from | 
 | 1907 |  uncompressed ones except opening if existing then closing | 
 | 1908 |  and saving with same compression ratio ... a pain. | 
 | 1909 |  | 
 | 1910 | #ifdef HAVE_ZLIB_H | 
 | 1911 |     xmlRegisterOutputCallbacks(xmlGzfileMatch, xmlGzfileOpen, | 
 | 1912 | 	                       xmlGzfileWrite, xmlGzfileClose); | 
 | 1913 | #endif | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1914 |  | 
 | 1915 |  Nor FTP PUT .... | 
 | 1916 | #ifdef LIBXML_FTP_ENABLED | 
 | 1917 |     xmlRegisterOutputCallbacks(xmlIOFTPMatch, xmlIOFTPOpen, | 
 | 1918 | 	                       xmlIOFTPWrite, xmlIOFTPClose); | 
 | 1919 | #endif | 
 | 1920 |  **********************************/ | 
 | 1921 |     xmlOutputCallbackInitialized = 1; | 
 | 1922 | } | 
 | 1923 |  | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1924 | #ifdef LIBXML_HTTP_ENABLED | 
 | 1925 | /** | 
| Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 1926 |  * xmlRegisterHTTPPostCallbacks: | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1927 |  * | 
 | 1928 |  * By default, libxml submits HTTP output requests using the "PUT" method. | 
 | 1929 |  * Calling this method changes the HTTP output method to use the "POST" | 
 | 1930 |  * method instead. | 
 | 1931 |  * | 
 | 1932 |  */ | 
 | 1933 | void | 
 | 1934 | xmlRegisterHTTPPostCallbacks( void ) { | 
 | 1935 |  | 
 | 1936 |     /*  Register defaults if not done previously  */ | 
 | 1937 |  | 
 | 1938 |     if ( xmlOutputCallbackInitialized == 0 ) | 
 | 1939 | 	xmlRegisterDefaultOutputCallbacks( ); | 
 | 1940 |  | 
 | 1941 |     xmlRegisterOutputCallbacks(xmlIOHTTPMatch, xmlIOHTTPDfltOpenW, | 
 | 1942 | 	                       xmlIOHTTPWrite, xmlIOHTTPClosePost); | 
 | 1943 |     return; | 
 | 1944 | } | 
 | 1945 | #endif | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1946 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 1947 |  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1948 | /** | 
 | 1949 |  * xmlAllocParserInputBuffer: | 
 | 1950 |  * @enc:  the charset encoding if known | 
 | 1951 |  * | 
 | 1952 |  * Create a buffered parser input for progressive parsing | 
 | 1953 |  * | 
 | 1954 |  * Returns the new parser input or NULL | 
 | 1955 |  */ | 
 | 1956 | xmlParserInputBufferPtr | 
 | 1957 | xmlAllocParserInputBuffer(xmlCharEncoding enc) { | 
 | 1958 |     xmlParserInputBufferPtr ret; | 
 | 1959 |  | 
 | 1960 |     ret = (xmlParserInputBufferPtr) xmlMalloc(sizeof(xmlParserInputBuffer)); | 
 | 1961 |     if (ret == NULL) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 1962 | 	xmlIOErrMemory("creating input buffer"); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1963 | 	return(NULL); | 
 | 1964 |     } | 
 | 1965 |     memset(ret, 0, (size_t) sizeof(xmlParserInputBuffer)); | 
| Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 1966 |     ret->buffer = xmlBufferCreateSize(2 * xmlDefaultBufferSize); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1967 |     if (ret->buffer == NULL) { | 
 | 1968 |         xmlFree(ret); | 
 | 1969 | 	return(NULL); | 
 | 1970 |     } | 
 | 1971 |     ret->buffer->alloc = XML_BUFFER_ALLOC_DOUBLEIT; | 
 | 1972 |     ret->encoder = xmlGetCharEncodingHandler(enc); | 
 | 1973 |     if (ret->encoder != NULL) | 
| Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 1974 |         ret->raw = xmlBufferCreateSize(2 * xmlDefaultBufferSize); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1975 |     else | 
 | 1976 |         ret->raw = NULL; | 
 | 1977 |     ret->readcallback = NULL; | 
 | 1978 |     ret->closecallback = NULL; | 
 | 1979 |     ret->context = NULL; | 
| William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 1980 |     ret->compressed = -1; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1981 |  | 
 | 1982 |     return(ret); | 
 | 1983 | } | 
 | 1984 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 1985 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1986 | /** | 
 | 1987 |  * xmlAllocOutputBuffer: | 
 | 1988 |  * @encoder:  the encoding converter or NULL | 
 | 1989 |  * | 
 | 1990 |  * Create a buffered parser output | 
 | 1991 |  * | 
 | 1992 |  * Returns the new parser output or NULL | 
 | 1993 |  */ | 
 | 1994 | xmlOutputBufferPtr | 
 | 1995 | xmlAllocOutputBuffer(xmlCharEncodingHandlerPtr encoder) { | 
 | 1996 |     xmlOutputBufferPtr ret; | 
 | 1997 |  | 
 | 1998 |     ret = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer)); | 
 | 1999 |     if (ret == NULL) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 2000 | 	xmlIOErrMemory("creating output buffer"); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2001 | 	return(NULL); | 
 | 2002 |     } | 
 | 2003 |     memset(ret, 0, (size_t) sizeof(xmlOutputBuffer)); | 
 | 2004 |     ret->buffer = xmlBufferCreate(); | 
 | 2005 |     if (ret->buffer == NULL) { | 
 | 2006 |         xmlFree(ret); | 
 | 2007 | 	return(NULL); | 
 | 2008 |     } | 
 | 2009 |     ret->buffer->alloc = XML_BUFFER_ALLOC_DOUBLEIT; | 
 | 2010 |     ret->encoder = encoder; | 
 | 2011 |     if (encoder != NULL) { | 
 | 2012 |         ret->conv = xmlBufferCreateSize(4000); | 
 | 2013 | 	/* | 
 | 2014 | 	 * This call is designed to initiate the encoder state | 
 | 2015 | 	 */ | 
 | 2016 | 	xmlCharEncOutFunc(encoder, ret->conv, NULL);  | 
 | 2017 |     } else | 
 | 2018 |         ret->conv = NULL; | 
 | 2019 |     ret->writecallback = NULL; | 
 | 2020 |     ret->closecallback = NULL; | 
 | 2021 |     ret->context = NULL; | 
 | 2022 |     ret->written = 0; | 
 | 2023 |  | 
 | 2024 |     return(ret); | 
 | 2025 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2026 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2027 |  | 
 | 2028 | /** | 
 | 2029 |  * xmlFreeParserInputBuffer: | 
 | 2030 |  * @in:  a buffered parser input | 
 | 2031 |  * | 
 | 2032 |  * Free up the memory used by a buffered parser input | 
 | 2033 |  */ | 
 | 2034 | void | 
 | 2035 | xmlFreeParserInputBuffer(xmlParserInputBufferPtr in) { | 
| Daniel Veillard | 8d8bf2c | 2003-09-17 19:36:25 +0000 | [diff] [blame] | 2036 |     if (in == NULL) return; | 
 | 2037 |  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2038 |     if (in->raw) { | 
 | 2039 |         xmlBufferFree(in->raw); | 
 | 2040 | 	in->raw = NULL; | 
 | 2041 |     } | 
 | 2042 |     if (in->encoder != NULL) { | 
 | 2043 |         xmlCharEncCloseFunc(in->encoder); | 
 | 2044 |     } | 
 | 2045 |     if (in->closecallback != NULL) { | 
 | 2046 | 	in->closecallback(in->context); | 
 | 2047 |     } | 
 | 2048 |     if (in->buffer != NULL) { | 
 | 2049 |         xmlBufferFree(in->buffer); | 
 | 2050 | 	in->buffer = NULL; | 
 | 2051 |     } | 
 | 2052 |  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2053 |     xmlFree(in); | 
 | 2054 | } | 
 | 2055 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2056 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2057 | /** | 
 | 2058 |  * xmlOutputBufferClose: | 
 | 2059 |  * @out:  a buffered output | 
 | 2060 |  * | 
 | 2061 |  * flushes and close the output I/O channel | 
 | 2062 |  * and free up all the associated resources | 
 | 2063 |  * | 
 | 2064 |  * Returns the number of byte written or -1 in case of error. | 
 | 2065 |  */ | 
 | 2066 | int | 
| Daniel Veillard | 828ce83 | 2003-10-08 19:19:10 +0000 | [diff] [blame] | 2067 | xmlOutputBufferClose(xmlOutputBufferPtr out) | 
 | 2068 | { | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2069 |     int written; | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 2070 |     int err_rc = 0; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2071 |  | 
 | 2072 |     if (out == NULL) | 
| Daniel Veillard | 828ce83 | 2003-10-08 19:19:10 +0000 | [diff] [blame] | 2073 |         return (-1); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2074 |     if (out->writecallback != NULL) | 
| Daniel Veillard | 828ce83 | 2003-10-08 19:19:10 +0000 | [diff] [blame] | 2075 |         xmlOutputBufferFlush(out); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2076 |     if (out->closecallback != NULL) { | 
| Daniel Veillard | 828ce83 | 2003-10-08 19:19:10 +0000 | [diff] [blame] | 2077 |         err_rc = out->closecallback(out->context); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2078 |     } | 
 | 2079 |     written = out->written; | 
 | 2080 |     if (out->conv) { | 
 | 2081 |         xmlBufferFree(out->conv); | 
| Daniel Veillard | 828ce83 | 2003-10-08 19:19:10 +0000 | [diff] [blame] | 2082 |         out->conv = NULL; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2083 |     } | 
 | 2084 |     if (out->encoder != NULL) { | 
 | 2085 |         xmlCharEncCloseFunc(out->encoder); | 
 | 2086 |     } | 
 | 2087 |     if (out->buffer != NULL) { | 
 | 2088 |         xmlBufferFree(out->buffer); | 
| Daniel Veillard | 828ce83 | 2003-10-08 19:19:10 +0000 | [diff] [blame] | 2089 |         out->buffer = NULL; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2090 |     } | 
 | 2091 |  | 
| Daniel Veillard | 828ce83 | 2003-10-08 19:19:10 +0000 | [diff] [blame] | 2092 |     if (out->error) | 
 | 2093 |         err_rc = -1; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2094 |     xmlFree(out); | 
| Daniel Veillard | 828ce83 | 2003-10-08 19:19:10 +0000 | [diff] [blame] | 2095 |     return ((err_rc == 0) ? written : err_rc); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2096 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2097 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2098 |  | 
 | 2099 | /** | 
 | 2100 |  * xmlParserInputBufferCreateFilename: | 
 | 2101 |  * @URI:  a C string containing the URI or filename | 
 | 2102 |  * @enc:  the charset encoding if known | 
 | 2103 |  * | 
 | 2104 |  * Create a buffered parser input for the progressive parsing of a file | 
 | 2105 |  * If filename is "-' then we use stdin as the input. | 
 | 2106 |  * Automatic support for ZLIB/Compress compressed document is provided | 
 | 2107 |  * by default if found at compile-time. | 
 | 2108 |  * Do an encoding check if enc == XML_CHAR_ENCODING_NONE | 
 | 2109 |  * | 
 | 2110 |  * Returns the new parser input or NULL | 
 | 2111 |  */ | 
 | 2112 | xmlParserInputBufferPtr | 
| Daniel Veillard | 3e59fc5 | 2003-04-18 12:34:58 +0000 | [diff] [blame] | 2113 | xmlParserInputBufferCreateFilename(const char *URI, xmlCharEncoding enc) { | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2114 |     xmlParserInputBufferPtr ret; | 
| Daniel Veillard | 388236f | 2001-07-08 18:35:48 +0000 | [diff] [blame] | 2115 |     int i = 0; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2116 |     void *context = NULL; | 
 | 2117 |  | 
 | 2118 |     if (xmlInputCallbackInitialized == 0) | 
 | 2119 | 	xmlRegisterDefaultInputCallbacks(); | 
 | 2120 |  | 
 | 2121 |     if (URI == NULL) return(NULL); | 
 | 2122 |  | 
 | 2123 |     /* | 
| Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2124 |      * Try to find one of the input accept method accepting that scheme | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2125 |      * Go in reverse to give precedence to user defined handlers. | 
| Daniel Veillard | 388236f | 2001-07-08 18:35:48 +0000 | [diff] [blame] | 2126 |      */ | 
 | 2127 |     if (context == NULL) { | 
 | 2128 | 	for (i = xmlInputCallbackNr - 1;i >= 0;i--) { | 
 | 2129 | 	    if ((xmlInputCallbackTable[i].matchcallback != NULL) && | 
 | 2130 | 		(xmlInputCallbackTable[i].matchcallback(URI) != 0)) { | 
| Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 2131 | 		context = xmlInputCallbackTable[i].opencallback(URI); | 
| Daniel Veillard | 847332a | 2003-10-18 11:29:40 +0000 | [diff] [blame] | 2132 | 		if (context != NULL) { | 
| Daniel Veillard | 388236f | 2001-07-08 18:35:48 +0000 | [diff] [blame] | 2133 | 		    break; | 
| Daniel Veillard | 847332a | 2003-10-18 11:29:40 +0000 | [diff] [blame] | 2134 | 		} | 
| Daniel Veillard | 388236f | 2001-07-08 18:35:48 +0000 | [diff] [blame] | 2135 | 	    } | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2136 | 	} | 
 | 2137 |     } | 
 | 2138 |     if (context == NULL) { | 
 | 2139 | 	return(NULL); | 
 | 2140 |     } | 
 | 2141 |  | 
 | 2142 |     /* | 
 | 2143 |      * Allocate the Input buffer front-end. | 
 | 2144 |      */ | 
 | 2145 |     ret = xmlAllocParserInputBuffer(enc); | 
 | 2146 |     if (ret != NULL) { | 
 | 2147 | 	ret->context = context; | 
 | 2148 | 	ret->readcallback = xmlInputCallbackTable[i].readcallback; | 
 | 2149 | 	ret->closecallback = xmlInputCallbackTable[i].closecallback; | 
| William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 2150 | #ifdef HAVE_ZLIB_H | 
| William M. Brack | c5cbf99 | 2003-10-29 22:15:13 +0000 | [diff] [blame] | 2151 | 	if ((xmlInputCallbackTable[i].opencallback == xmlGzfileOpen) && | 
 | 2152 | 		(strcmp(URI, "-") != 0)) { | 
| William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 2153 | 	    if (((z_stream *)context)->avail_in > 4) { | 
 | 2154 | 	        char *cptr, buff4[4]; | 
| Daniel Veillard | 07cb822 | 2003-09-10 10:51:05 +0000 | [diff] [blame] | 2155 | 		cptr = (char *) ((z_stream *)context)->next_in; | 
| William M. Brack | c07329e | 2003-09-08 01:57:30 +0000 | [diff] [blame] | 2156 | 		if (gzread(context, buff4, 4) == 4) { | 
 | 2157 | 		    if (strncmp(buff4, cptr, 4) == 0) | 
 | 2158 | 		        ret->compressed = 0; | 
 | 2159 | 		    else | 
 | 2160 | 		        ret->compressed = 1; | 
 | 2161 | 		    gzrewind(context); | 
 | 2162 | 		} | 
 | 2163 | 	    } | 
 | 2164 | 	} | 
 | 2165 | #endif | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2166 |     } | 
 | 2167 |     return(ret); | 
 | 2168 | } | 
 | 2169 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2170 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2171 | /** | 
 | 2172 |  * xmlOutputBufferCreateFilename: | 
 | 2173 |  * @URI:  a C string containing the URI or filename | 
 | 2174 |  * @encoder:  the encoding converter or NULL | 
 | 2175 |  * @compression:  the compression ration (0 none, 9 max). | 
 | 2176 |  * | 
 | 2177 |  * Create a buffered  output for the progressive saving of a file | 
 | 2178 |  * If filename is "-' then we use stdout as the output. | 
 | 2179 |  * Automatic support for ZLIB/Compress compressed document is provided | 
 | 2180 |  * by default if found at compile-time. | 
 | 2181 |  * TODO: currently if compression is set, the library only support | 
 | 2182 |  *       writing to a local file. | 
 | 2183 |  * | 
 | 2184 |  * Returns the new output or NULL | 
 | 2185 |  */ | 
 | 2186 | xmlOutputBufferPtr | 
 | 2187 | xmlOutputBufferCreateFilename(const char *URI, | 
 | 2188 |                               xmlCharEncodingHandlerPtr encoder, | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2189 | 			      int compression ATTRIBUTE_UNUSED) { | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2190 |     xmlOutputBufferPtr ret; | 
| Daniel Veillard | ecb6f5b | 2001-08-15 08:47:42 +0000 | [diff] [blame] | 2191 |     int i = 0; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2192 |     void *context = NULL; | 
| Daniel Veillard | ecb6f5b | 2001-08-15 08:47:42 +0000 | [diff] [blame] | 2193 |     char *unescaped; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2194 |  | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 2195 |     int is_http_uri = 0;	/*   Can't change if HTTP disabled  */ | 
 | 2196 |  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2197 |     if (xmlOutputCallbackInitialized == 0) | 
 | 2198 | 	xmlRegisterDefaultOutputCallbacks(); | 
 | 2199 |  | 
 | 2200 |     if (URI == NULL) return(NULL); | 
 | 2201 |  | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 2202 | #ifdef LIBXML_HTTP_ENABLED | 
 | 2203 |     /*  Need to prevent HTTP URI's from falling into zlib short circuit  */ | 
 | 2204 |  | 
| Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 2205 |     is_http_uri = xmlIOHTTPMatch( URI ); | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 2206 | #endif | 
 | 2207 |  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2208 |  | 
 | 2209 |     /* | 
| Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2210 |      * Try to find one of the output accept method accepting that scheme | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2211 |      * Go in reverse to give precedence to user defined handlers. | 
| Daniel Veillard | ecb6f5b | 2001-08-15 08:47:42 +0000 | [diff] [blame] | 2212 |      * try with an unescaped version of the URI | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2213 |      */ | 
| Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 2214 |     unescaped = xmlURIUnescapeString(URI, 0, NULL); | 
| Daniel Veillard | ecb6f5b | 2001-08-15 08:47:42 +0000 | [diff] [blame] | 2215 |     if (unescaped != NULL) { | 
 | 2216 | #ifdef HAVE_ZLIB_H | 
 | 2217 | 	if ((compression > 0) && (compression <= 9) && (is_http_uri == 0)) { | 
 | 2218 | 	    context = xmlGzfileOpenW(unescaped, compression); | 
 | 2219 | 	    if (context != NULL) { | 
 | 2220 | 		ret = xmlAllocOutputBuffer(encoder); | 
 | 2221 | 		if (ret != NULL) { | 
 | 2222 | 		    ret->context = context; | 
 | 2223 | 		    ret->writecallback = xmlGzfileWrite; | 
 | 2224 | 		    ret->closecallback = xmlGzfileClose; | 
 | 2225 | 		} | 
 | 2226 | 		xmlFree(unescaped); | 
 | 2227 | 		return(ret); | 
 | 2228 | 	    } | 
 | 2229 | 	} | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 2230 | #endif | 
| Daniel Veillard | ecb6f5b | 2001-08-15 08:47:42 +0000 | [diff] [blame] | 2231 | 	for (i = xmlOutputCallbackNr - 1;i >= 0;i--) { | 
 | 2232 | 	    if ((xmlOutputCallbackTable[i].matchcallback != NULL) && | 
 | 2233 | 		(xmlOutputCallbackTable[i].matchcallback(unescaped) != 0)) { | 
 | 2234 | #if defined(LIBXML_HTTP_ENABLED) && defined(HAVE_ZLIB_H) | 
 | 2235 | 		/*  Need to pass compression parameter into HTTP open calls  */ | 
 | 2236 | 		if (xmlOutputCallbackTable[i].matchcallback == xmlIOHTTPMatch) | 
 | 2237 | 		    context = xmlIOHTTPOpenW(unescaped, compression); | 
 | 2238 | 		else | 
 | 2239 | #endif | 
 | 2240 | 		    context = xmlOutputCallbackTable[i].opencallback(unescaped); | 
 | 2241 | 		if (context != NULL) | 
 | 2242 | 		    break; | 
 | 2243 | 	    } | 
 | 2244 | 	} | 
 | 2245 | 	xmlFree(unescaped); | 
 | 2246 |     } | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 2247 |  | 
| Daniel Veillard | ecb6f5b | 2001-08-15 08:47:42 +0000 | [diff] [blame] | 2248 |     /* | 
 | 2249 |      * If this failed try with a non-escaped URI this may be a strange | 
 | 2250 |      * filename | 
 | 2251 |      */ | 
 | 2252 |     if (context == NULL) { | 
 | 2253 | #ifdef HAVE_ZLIB_H | 
 | 2254 | 	if ((compression > 0) && (compression <= 9) && (is_http_uri == 0)) { | 
| Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 2255 | 	    context = xmlGzfileOpenW(URI, compression); | 
| Daniel Veillard | ecb6f5b | 2001-08-15 08:47:42 +0000 | [diff] [blame] | 2256 | 	    if (context != NULL) { | 
 | 2257 | 		ret = xmlAllocOutputBuffer(encoder); | 
 | 2258 | 		if (ret != NULL) { | 
 | 2259 | 		    ret->context = context; | 
 | 2260 | 		    ret->writecallback = xmlGzfileWrite; | 
 | 2261 | 		    ret->closecallback = xmlGzfileClose; | 
 | 2262 | 		} | 
 | 2263 | 		return(ret); | 
 | 2264 | 	    } | 
 | 2265 | 	} | 
 | 2266 | #endif | 
 | 2267 | 	for (i = xmlOutputCallbackNr - 1;i >= 0;i--) { | 
 | 2268 | 	    if ((xmlOutputCallbackTable[i].matchcallback != NULL) && | 
| Igor Zlatkovic | 5f9fada | 2003-02-19 14:51:00 +0000 | [diff] [blame] | 2269 | 		(xmlOutputCallbackTable[i].matchcallback(URI) != 0)) { | 
| Daniel Veillard | ecb6f5b | 2001-08-15 08:47:42 +0000 | [diff] [blame] | 2270 | #if defined(LIBXML_HTTP_ENABLED) && defined(HAVE_ZLIB_H) | 
 | 2271 | 		/*  Need to pass compression parameter into HTTP open calls  */ | 
 | 2272 | 		if (xmlOutputCallbackTable[i].matchcallback == xmlIOHTTPMatch) | 
 | 2273 | 		    context = xmlIOHTTPOpenW(URI, compression); | 
 | 2274 | 		else | 
 | 2275 | #endif | 
 | 2276 | 		    context = xmlOutputCallbackTable[i].opencallback(URI); | 
 | 2277 | 		if (context != NULL) | 
 | 2278 | 		    break; | 
 | 2279 | 	    } | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2280 | 	} | 
 | 2281 |     } | 
| Daniel Veillard | f012a64 | 2001-07-23 19:10:52 +0000 | [diff] [blame] | 2282 |  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2283 |     if (context == NULL) { | 
 | 2284 | 	return(NULL); | 
 | 2285 |     } | 
 | 2286 |  | 
 | 2287 |     /* | 
 | 2288 |      * Allocate the Output buffer front-end. | 
 | 2289 |      */ | 
 | 2290 |     ret = xmlAllocOutputBuffer(encoder); | 
 | 2291 |     if (ret != NULL) { | 
 | 2292 | 	ret->context = context; | 
 | 2293 | 	ret->writecallback = xmlOutputCallbackTable[i].writecallback; | 
 | 2294 | 	ret->closecallback = xmlOutputCallbackTable[i].closecallback; | 
 | 2295 |     } | 
 | 2296 |     return(ret); | 
 | 2297 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2298 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2299 |  | 
 | 2300 | /** | 
 | 2301 |  * xmlParserInputBufferCreateFile: | 
 | 2302 |  * @file:  a FILE*  | 
 | 2303 |  * @enc:  the charset encoding if known | 
 | 2304 |  * | 
 | 2305 |  * Create a buffered parser input for the progressive parsing of a FILE * | 
 | 2306 |  * buffered C I/O | 
 | 2307 |  * | 
 | 2308 |  * Returns the new parser input or NULL | 
 | 2309 |  */ | 
 | 2310 | xmlParserInputBufferPtr | 
 | 2311 | xmlParserInputBufferCreateFile(FILE *file, xmlCharEncoding enc) { | 
 | 2312 |     xmlParserInputBufferPtr ret; | 
 | 2313 |  | 
 | 2314 |     if (xmlInputCallbackInitialized == 0) | 
 | 2315 | 	xmlRegisterDefaultInputCallbacks(); | 
 | 2316 |  | 
 | 2317 |     if (file == NULL) return(NULL); | 
 | 2318 |  | 
 | 2319 |     ret = xmlAllocParserInputBuffer(enc); | 
 | 2320 |     if (ret != NULL) { | 
 | 2321 |         ret->context = file; | 
 | 2322 | 	ret->readcallback = xmlFileRead; | 
 | 2323 | 	ret->closecallback = xmlFileFlush; | 
 | 2324 |     } | 
 | 2325 |  | 
 | 2326 |     return(ret); | 
 | 2327 | } | 
 | 2328 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2329 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2330 | /** | 
 | 2331 |  * xmlOutputBufferCreateFile: | 
 | 2332 |  * @file:  a FILE*  | 
 | 2333 |  * @encoder:  the encoding converter or NULL | 
 | 2334 |  * | 
 | 2335 |  * Create a buffered output for the progressive saving to a FILE * | 
 | 2336 |  * buffered C I/O | 
 | 2337 |  * | 
 | 2338 |  * Returns the new parser output or NULL | 
 | 2339 |  */ | 
 | 2340 | xmlOutputBufferPtr | 
 | 2341 | xmlOutputBufferCreateFile(FILE *file, xmlCharEncodingHandlerPtr encoder) { | 
 | 2342 |     xmlOutputBufferPtr ret; | 
 | 2343 |  | 
 | 2344 |     if (xmlOutputCallbackInitialized == 0) | 
 | 2345 | 	xmlRegisterDefaultOutputCallbacks(); | 
 | 2346 |  | 
 | 2347 |     if (file == NULL) return(NULL); | 
 | 2348 |  | 
 | 2349 |     ret = xmlAllocOutputBuffer(encoder); | 
 | 2350 |     if (ret != NULL) { | 
 | 2351 |         ret->context = file; | 
 | 2352 | 	ret->writecallback = xmlFileWrite; | 
 | 2353 | 	ret->closecallback = xmlFileFlush; | 
 | 2354 |     } | 
 | 2355 |  | 
 | 2356 |     return(ret); | 
 | 2357 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2358 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2359 |  | 
 | 2360 | /** | 
 | 2361 |  * xmlParserInputBufferCreateFd: | 
 | 2362 |  * @fd:  a file descriptor number | 
 | 2363 |  * @enc:  the charset encoding if known | 
 | 2364 |  * | 
 | 2365 |  * Create a buffered parser input for the progressive parsing for the input | 
 | 2366 |  * from a file descriptor | 
 | 2367 |  * | 
 | 2368 |  * Returns the new parser input or NULL | 
 | 2369 |  */ | 
 | 2370 | xmlParserInputBufferPtr | 
 | 2371 | xmlParserInputBufferCreateFd(int fd, xmlCharEncoding enc) { | 
 | 2372 |     xmlParserInputBufferPtr ret; | 
 | 2373 |  | 
 | 2374 |     if (fd < 0) return(NULL); | 
 | 2375 |  | 
 | 2376 |     ret = xmlAllocParserInputBuffer(enc); | 
 | 2377 |     if (ret != NULL) { | 
| Daniel Veillard | 8fcc494 | 2001-07-17 20:07:33 +0000 | [diff] [blame] | 2378 |         ret->context = (void *) (long) fd; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2379 | 	ret->readcallback = xmlFdRead; | 
 | 2380 | 	ret->closecallback = xmlFdClose; | 
 | 2381 |     } | 
 | 2382 |  | 
 | 2383 |     return(ret); | 
 | 2384 | } | 
 | 2385 |  | 
 | 2386 | /** | 
 | 2387 |  * xmlParserInputBufferCreateMem: | 
 | 2388 |  * @mem:  the memory input | 
 | 2389 |  * @size:  the length of the memory block | 
 | 2390 |  * @enc:  the charset encoding if known | 
 | 2391 |  * | 
 | 2392 |  * Create a buffered parser input for the progressive parsing for the input | 
 | 2393 |  * from a memory area. | 
 | 2394 |  * | 
 | 2395 |  * Returns the new parser input or NULL | 
 | 2396 |  */ | 
 | 2397 | xmlParserInputBufferPtr | 
 | 2398 | xmlParserInputBufferCreateMem(const char *mem, int size, xmlCharEncoding enc) { | 
 | 2399 |     xmlParserInputBufferPtr ret; | 
 | 2400 |  | 
 | 2401 |     if (size <= 0) return(NULL); | 
 | 2402 |     if (mem == NULL) return(NULL); | 
 | 2403 |  | 
 | 2404 |     ret = xmlAllocParserInputBuffer(enc); | 
 | 2405 |     if (ret != NULL) { | 
 | 2406 |         ret->context = (void *) mem; | 
 | 2407 | 	ret->readcallback = (xmlInputReadCallback) xmlNop; | 
 | 2408 | 	ret->closecallback = NULL; | 
 | 2409 | 	xmlBufferAdd(ret->buffer, (const xmlChar *) mem, size); | 
 | 2410 |     } | 
 | 2411 |  | 
 | 2412 |     return(ret); | 
 | 2413 | } | 
 | 2414 |  | 
 | 2415 | /** | 
| Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 2416 |  * xmlParserInputBufferCreateStatic: | 
 | 2417 |  * @mem:  the memory input | 
 | 2418 |  * @size:  the length of the memory block | 
 | 2419 |  * @enc:  the charset encoding if known | 
 | 2420 |  * | 
 | 2421 |  * Create a buffered parser input for the progressive parsing for the input | 
 | 2422 |  * from an immutable memory area. This will not copy the memory area to | 
 | 2423 |  * the buffer, but the memory is expected to be available until the end of | 
 | 2424 |  * the parsing, this is useful for example when using mmap'ed file. | 
 | 2425 |  * | 
 | 2426 |  * Returns the new parser input or NULL | 
 | 2427 |  */ | 
 | 2428 | xmlParserInputBufferPtr | 
 | 2429 | xmlParserInputBufferCreateStatic(const char *mem, int size, | 
 | 2430 |                                  xmlCharEncoding enc) { | 
 | 2431 |     xmlParserInputBufferPtr ret; | 
 | 2432 |  | 
 | 2433 |     if (size <= 0) return(NULL); | 
 | 2434 |     if (mem == NULL) return(NULL); | 
 | 2435 |  | 
 | 2436 |     ret = (xmlParserInputBufferPtr) xmlMalloc(sizeof(xmlParserInputBuffer)); | 
 | 2437 |     if (ret == NULL) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 2438 | 	xmlIOErrMemory("creating input buffer"); | 
| Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 2439 | 	return(NULL); | 
 | 2440 |     } | 
 | 2441 |     memset(ret, 0, (size_t) sizeof(xmlParserInputBuffer)); | 
| Daniel Veillard | e72c508 | 2003-09-19 12:44:05 +0000 | [diff] [blame] | 2442 |     ret->buffer = xmlBufferCreateStatic((void *)mem, (size_t) size); | 
| Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 2443 |     if (ret->buffer == NULL) { | 
 | 2444 |         xmlFree(ret); | 
 | 2445 | 	return(NULL); | 
 | 2446 |     } | 
 | 2447 |     ret->encoder = xmlGetCharEncodingHandler(enc); | 
 | 2448 |     if (ret->encoder != NULL) | 
 | 2449 |         ret->raw = xmlBufferCreateSize(2 * xmlDefaultBufferSize); | 
 | 2450 |     else | 
 | 2451 |         ret->raw = NULL; | 
 | 2452 |     ret->compressed = -1; | 
 | 2453 |     ret->context = (void *) mem; | 
 | 2454 |     ret->readcallback = NULL; | 
 | 2455 |     ret->closecallback = NULL; | 
 | 2456 |  | 
 | 2457 |     return(ret); | 
 | 2458 | } | 
 | 2459 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2460 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 2461 | /** | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2462 |  * xmlOutputBufferCreateFd: | 
 | 2463 |  * @fd:  a file descriptor number | 
 | 2464 |  * @encoder:  the encoding converter or NULL | 
 | 2465 |  * | 
 | 2466 |  * Create a buffered output for the progressive saving  | 
 | 2467 |  * to a file descriptor | 
 | 2468 |  * | 
 | 2469 |  * Returns the new parser output or NULL | 
 | 2470 |  */ | 
 | 2471 | xmlOutputBufferPtr | 
 | 2472 | xmlOutputBufferCreateFd(int fd, xmlCharEncodingHandlerPtr encoder) { | 
 | 2473 |     xmlOutputBufferPtr ret; | 
 | 2474 |  | 
 | 2475 |     if (fd < 0) return(NULL); | 
 | 2476 |  | 
 | 2477 |     ret = xmlAllocOutputBuffer(encoder); | 
 | 2478 |     if (ret != NULL) { | 
| Daniel Veillard | 8fcc494 | 2001-07-17 20:07:33 +0000 | [diff] [blame] | 2479 |         ret->context = (void *) (long) fd; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2480 | 	ret->writecallback = xmlFdWrite; | 
| Daniel Veillard | 7db3871 | 2002-02-07 16:39:11 +0000 | [diff] [blame] | 2481 | 	ret->closecallback = NULL; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2482 |     } | 
 | 2483 |  | 
 | 2484 |     return(ret); | 
 | 2485 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2486 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2487 |  | 
 | 2488 | /** | 
 | 2489 |  * xmlParserInputBufferCreateIO: | 
 | 2490 |  * @ioread:  an I/O read function | 
 | 2491 |  * @ioclose:  an I/O close function | 
 | 2492 |  * @ioctx:  an I/O handler | 
 | 2493 |  * @enc:  the charset encoding if known | 
 | 2494 |  * | 
 | 2495 |  * Create a buffered parser input for the progressive parsing for the input | 
 | 2496 |  * from an I/O handler | 
 | 2497 |  * | 
 | 2498 |  * Returns the new parser input or NULL | 
 | 2499 |  */ | 
 | 2500 | xmlParserInputBufferPtr | 
 | 2501 | xmlParserInputBufferCreateIO(xmlInputReadCallback   ioread, | 
 | 2502 | 	 xmlInputCloseCallback  ioclose, void *ioctx, xmlCharEncoding enc) { | 
 | 2503 |     xmlParserInputBufferPtr ret; | 
 | 2504 |  | 
 | 2505 |     if (ioread == NULL) return(NULL); | 
 | 2506 |  | 
 | 2507 |     ret = xmlAllocParserInputBuffer(enc); | 
 | 2508 |     if (ret != NULL) { | 
 | 2509 |         ret->context = (void *) ioctx; | 
 | 2510 | 	ret->readcallback = ioread; | 
 | 2511 | 	ret->closecallback = ioclose; | 
 | 2512 |     } | 
 | 2513 |  | 
 | 2514 |     return(ret); | 
 | 2515 | } | 
 | 2516 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2517 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2518 | /** | 
 | 2519 |  * xmlOutputBufferCreateIO: | 
 | 2520 |  * @iowrite:  an I/O write function | 
 | 2521 |  * @ioclose:  an I/O close function | 
 | 2522 |  * @ioctx:  an I/O handler | 
| Daniel Veillard | 9d06d30 | 2002-01-22 18:15:52 +0000 | [diff] [blame] | 2523 |  * @encoder:  the charset encoding if known | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2524 |  * | 
 | 2525 |  * Create a buffered output for the progressive saving | 
 | 2526 |  * to an I/O handler | 
 | 2527 |  * | 
 | 2528 |  * Returns the new parser output or NULL | 
 | 2529 |  */ | 
 | 2530 | xmlOutputBufferPtr | 
 | 2531 | xmlOutputBufferCreateIO(xmlOutputWriteCallback   iowrite, | 
 | 2532 | 	 xmlOutputCloseCallback  ioclose, void *ioctx, | 
 | 2533 | 	 xmlCharEncodingHandlerPtr encoder) { | 
 | 2534 |     xmlOutputBufferPtr ret; | 
 | 2535 |  | 
 | 2536 |     if (iowrite == NULL) return(NULL); | 
 | 2537 |  | 
 | 2538 |     ret = xmlAllocOutputBuffer(encoder); | 
 | 2539 |     if (ret != NULL) { | 
 | 2540 |         ret->context = (void *) ioctx; | 
 | 2541 | 	ret->writecallback = iowrite; | 
 | 2542 | 	ret->closecallback = ioclose; | 
 | 2543 |     } | 
 | 2544 |  | 
 | 2545 |     return(ret); | 
 | 2546 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2547 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2548 |  | 
 | 2549 | /** | 
 | 2550 |  * xmlParserInputBufferPush: | 
 | 2551 |  * @in:  a buffered parser input | 
 | 2552 |  * @len:  the size in bytes of the array. | 
 | 2553 |  * @buf:  an char array | 
 | 2554 |  * | 
 | 2555 |  * Push the content of the arry in the input buffer | 
 | 2556 |  * This routine handle the I18N transcoding to internal UTF-8 | 
 | 2557 |  * This is used when operating the parser in progressive (push) mode. | 
 | 2558 |  * | 
 | 2559 |  * Returns the number of chars read and stored in the buffer, or -1 | 
 | 2560 |  *         in case of error. | 
 | 2561 |  */ | 
 | 2562 | int | 
 | 2563 | xmlParserInputBufferPush(xmlParserInputBufferPtr in, | 
 | 2564 | 	                 int len, const char *buf) { | 
 | 2565 |     int nbchars = 0; | 
 | 2566 |  | 
 | 2567 |     if (len < 0) return(0); | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 2568 |     if ((in == NULL) || (in->error)) return(-1); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2569 |     if (in->encoder != NULL) { | 
 | 2570 |         /* | 
 | 2571 | 	 * Store the data in the incoming raw buffer | 
 | 2572 | 	 */ | 
 | 2573 |         if (in->raw == NULL) { | 
 | 2574 | 	    in->raw = xmlBufferCreate(); | 
 | 2575 | 	} | 
 | 2576 | 	xmlBufferAdd(in->raw, (const xmlChar *) buf, len); | 
 | 2577 |  | 
 | 2578 | 	/* | 
 | 2579 | 	 * convert as much as possible to the parser reading buffer. | 
 | 2580 | 	 */ | 
 | 2581 | 	nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw); | 
 | 2582 | 	if (nbchars < 0) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 2583 | 	    xmlIOErr(XML_IO_ENCODER, NULL); | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 2584 | 	    in->error = XML_IO_ENCODER; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2585 | 	    return(-1); | 
 | 2586 | 	} | 
 | 2587 |     } else { | 
 | 2588 | 	nbchars = len; | 
 | 2589 |         xmlBufferAdd(in->buffer, (xmlChar *) buf, nbchars); | 
 | 2590 |     } | 
 | 2591 | #ifdef DEBUG_INPUT | 
 | 2592 |     xmlGenericError(xmlGenericErrorContext, | 
 | 2593 | 	    "I/O: pushed %d chars, buffer %d/%d\n", | 
 | 2594 |             nbchars, in->buffer->use, in->buffer->size); | 
 | 2595 | #endif | 
 | 2596 |     return(nbchars); | 
 | 2597 | } | 
 | 2598 |  | 
 | 2599 | /** | 
| Daniel Veillard | ddffd2a | 2002-03-05 20:28:20 +0000 | [diff] [blame] | 2600 |  * endOfInput: | 
 | 2601 |  * | 
 | 2602 |  * When reading from an Input channel indicated end of file or error | 
 | 2603 |  * don't reread from it again. | 
 | 2604 |  */ | 
 | 2605 | static int | 
 | 2606 | endOfInput (void * context ATTRIBUTE_UNUSED, | 
 | 2607 | 	    char * buffer ATTRIBUTE_UNUSED, | 
 | 2608 | 	    int len ATTRIBUTE_UNUSED) { | 
 | 2609 |     return(0); | 
 | 2610 | } | 
 | 2611 |  | 
 | 2612 | /** | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2613 |  * xmlParserInputBufferGrow: | 
 | 2614 |  * @in:  a buffered parser input | 
 | 2615 |  * @len:  indicative value of the amount of chars to read | 
 | 2616 |  * | 
 | 2617 |  * Grow up the content of the input buffer, the old data are preserved | 
 | 2618 |  * This routine handle the I18N transcoding to internal UTF-8 | 
 | 2619 |  * This routine is used when operating the parser in normal (pull) mode | 
 | 2620 |  * | 
| Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 2621 |  * TODO: one should be able to remove one extra copy by copying directly | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2622 |  *       onto in->buffer or in->raw | 
 | 2623 |  * | 
 | 2624 |  * Returns the number of chars read and stored in the buffer, or -1 | 
 | 2625 |  *         in case of error. | 
 | 2626 |  */ | 
 | 2627 | int | 
 | 2628 | xmlParserInputBufferGrow(xmlParserInputBufferPtr in, int len) { | 
 | 2629 |     char *buffer = NULL; | 
 | 2630 |     int res = 0; | 
 | 2631 |     int nbchars = 0; | 
 | 2632 |     int buffree; | 
| Daniel Veillard | 9e41230 | 2002-06-10 15:59:44 +0000 | [diff] [blame] | 2633 |     unsigned int needSize; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2634 |  | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 2635 |     if ((in == NULL) || (in->error)) return(-1); | 
| Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 2636 |     if ((len <= MINLEN) && (len != 4)) | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2637 |         len = MINLEN; | 
| Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame] | 2638 |  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2639 |     buffree = in->buffer->size - in->buffer->use; | 
 | 2640 |     if (buffree <= 0) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 2641 | 	xmlIOErr(XML_IO_BUFFER_FULL, NULL); | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 2642 | 	in->error = XML_IO_BUFFER_FULL; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2643 | 	return(0); | 
 | 2644 |     } | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2645 |  | 
| Daniel Veillard | e535449 | 2002-05-16 08:43:22 +0000 | [diff] [blame] | 2646 |     needSize = in->buffer->use + len + 1; | 
 | 2647 |     if (needSize > in->buffer->size){ | 
 | 2648 |         if (!xmlBufferResize(in->buffer, needSize)){ | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 2649 | 	    xmlIOErrMemory("growing input buffer"); | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 2650 | 	    in->error = XML_ERR_NO_MEMORY; | 
| Daniel Veillard | e535449 | 2002-05-16 08:43:22 +0000 | [diff] [blame] | 2651 |             return(0); | 
 | 2652 |         } | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2653 |     } | 
| Daniel Veillard | e535449 | 2002-05-16 08:43:22 +0000 | [diff] [blame] | 2654 |     buffer = (char *)&in->buffer->content[in->buffer->use]; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2655 |  | 
 | 2656 |     /* | 
 | 2657 |      * Call the read method for this I/O type. | 
 | 2658 |      */ | 
 | 2659 |     if (in->readcallback != NULL) { | 
 | 2660 | 	res = in->readcallback(in->context, &buffer[0], len); | 
| Daniel Veillard | ddffd2a | 2002-03-05 20:28:20 +0000 | [diff] [blame] | 2661 | 	if (res <= 0) | 
 | 2662 | 	    in->readcallback = endOfInput; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2663 |     } else { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 2664 | 	xmlIOErr(XML_IO_NO_INPUT, NULL); | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 2665 | 	in->error = XML_IO_NO_INPUT; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2666 | 	return(-1); | 
 | 2667 |     } | 
 | 2668 |     if (res < 0) { | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2669 | 	return(-1); | 
 | 2670 |     } | 
 | 2671 |     len = res; | 
 | 2672 |     if (in->encoder != NULL) { | 
 | 2673 |         /* | 
 | 2674 | 	 * Store the data in the incoming raw buffer | 
 | 2675 | 	 */ | 
 | 2676 |         if (in->raw == NULL) { | 
 | 2677 | 	    in->raw = xmlBufferCreate(); | 
 | 2678 | 	} | 
 | 2679 | 	xmlBufferAdd(in->raw, (const xmlChar *) buffer, len); | 
 | 2680 |  | 
 | 2681 | 	/* | 
 | 2682 | 	 * convert as much as possible to the parser reading buffer. | 
 | 2683 | 	 */ | 
 | 2684 | 	nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw); | 
 | 2685 | 	if (nbchars < 0) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 2686 | 	    xmlIOErr(XML_IO_ENCODER, NULL); | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 2687 | 	    in->error = XML_IO_ENCODER; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2688 | 	    return(-1); | 
 | 2689 | 	} | 
 | 2690 |     } else { | 
 | 2691 | 	nbchars = len; | 
| Daniel Veillard | e535449 | 2002-05-16 08:43:22 +0000 | [diff] [blame] | 2692 |     	in->buffer->use += nbchars; | 
 | 2693 | 	buffer[nbchars] = 0; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2694 |     } | 
 | 2695 | #ifdef DEBUG_INPUT | 
 | 2696 |     xmlGenericError(xmlGenericErrorContext, | 
 | 2697 | 	    "I/O: read %d chars, buffer %d/%d\n", | 
 | 2698 |             nbchars, in->buffer->use, in->buffer->size); | 
 | 2699 | #endif | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2700 |     return(nbchars); | 
 | 2701 | } | 
 | 2702 |  | 
 | 2703 | /** | 
 | 2704 |  * xmlParserInputBufferRead: | 
 | 2705 |  * @in:  a buffered parser input | 
 | 2706 |  * @len:  indicative value of the amount of chars to read | 
 | 2707 |  * | 
 | 2708 |  * Refresh the content of the input buffer, the old data are considered | 
 | 2709 |  * consumed | 
 | 2710 |  * This routine handle the I18N transcoding to internal UTF-8 | 
 | 2711 |  * | 
 | 2712 |  * Returns the number of chars read and stored in the buffer, or -1 | 
 | 2713 |  *         in case of error. | 
 | 2714 |  */ | 
 | 2715 | int | 
 | 2716 | xmlParserInputBufferRead(xmlParserInputBufferPtr in, int len) { | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 2717 |     if ((in == NULL) || (in->error)) return(-1); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2718 |     if (in->readcallback != NULL) | 
 | 2719 | 	return(xmlParserInputBufferGrow(in, len)); | 
| Daniel Veillard | 5335055 | 2003-09-18 13:35:51 +0000 | [diff] [blame] | 2720 |     else if ((in->buffer != NULL) && | 
 | 2721 |              (in->buffer->alloc == XML_BUFFER_ALLOC_IMMUTABLE)) | 
 | 2722 | 	return(0); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2723 |     else | 
 | 2724 |         return(-1); | 
 | 2725 | } | 
 | 2726 |  | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2727 | #ifdef LIBXML_OUTPUT_ENABLED | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2728 | /** | 
 | 2729 |  * xmlOutputBufferWrite: | 
 | 2730 |  * @out:  a buffered parser output | 
 | 2731 |  * @len:  the size in bytes of the array. | 
 | 2732 |  * @buf:  an char array | 
 | 2733 |  * | 
 | 2734 |  * Write the content of the array in the output I/O buffer | 
 | 2735 |  * This routine handle the I18N transcoding from internal UTF-8 | 
 | 2736 |  * The buffer is lossless, i.e. will store in case of partial | 
 | 2737 |  * or delayed writes. | 
 | 2738 |  * | 
 | 2739 |  * Returns the number of chars immediately written, or -1 | 
 | 2740 |  *         in case of error. | 
 | 2741 |  */ | 
 | 2742 | int | 
 | 2743 | xmlOutputBufferWrite(xmlOutputBufferPtr out, int len, const char *buf) { | 
 | 2744 |     int nbchars = 0; /* number of chars to output to I/O */ | 
 | 2745 |     int ret;         /* return from function call */ | 
 | 2746 |     int written = 0; /* number of char written to I/O so far */ | 
 | 2747 |     int chunk;       /* number of byte curreent processed from buf */ | 
 | 2748 |  | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 2749 |     if ((out == NULL) || (out->error)) return(-1); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2750 |     if (len < 0) return(0); | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 2751 |     if (out->error) return(-1); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2752 |  | 
 | 2753 |     do { | 
 | 2754 | 	chunk = len; | 
 | 2755 | 	if (chunk > 4 * MINLEN) | 
 | 2756 | 	    chunk = 4 * MINLEN; | 
 | 2757 |  | 
 | 2758 | 	/* | 
 | 2759 | 	 * first handle encoding stuff. | 
 | 2760 | 	 */ | 
 | 2761 | 	if (out->encoder != NULL) { | 
 | 2762 | 	    /* | 
 | 2763 | 	     * Store the data in the incoming raw buffer | 
 | 2764 | 	     */ | 
 | 2765 | 	    if (out->conv == NULL) { | 
 | 2766 | 		out->conv = xmlBufferCreate(); | 
 | 2767 | 	    } | 
 | 2768 | 	    xmlBufferAdd(out->buffer, (const xmlChar *) buf, chunk); | 
 | 2769 |  | 
 | 2770 | 	    if ((out->buffer->use < MINLEN) && (chunk == len)) | 
 | 2771 | 		goto done; | 
 | 2772 |  | 
 | 2773 | 	    /* | 
 | 2774 | 	     * convert as much as possible to the parser reading buffer. | 
 | 2775 | 	     */ | 
 | 2776 | 	    ret = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer); | 
| Daniel Veillard | 809faa5 | 2003-02-10 15:43:53 +0000 | [diff] [blame] | 2777 | 	    if ((ret < 0) && (ret != -3)) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 2778 | 		xmlIOErr(XML_IO_ENCODER, NULL); | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 2779 | 		out->error = XML_IO_ENCODER; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2780 | 		return(-1); | 
 | 2781 | 	    } | 
 | 2782 | 	    nbchars = out->conv->use; | 
 | 2783 | 	} else { | 
 | 2784 | 	    xmlBufferAdd(out->buffer, (const xmlChar *) buf, chunk); | 
 | 2785 | 	    nbchars = out->buffer->use; | 
 | 2786 | 	} | 
 | 2787 | 	buf += chunk; | 
 | 2788 | 	len -= chunk; | 
 | 2789 |  | 
 | 2790 | 	if ((nbchars < MINLEN) && (len <= 0)) | 
 | 2791 | 	    goto done; | 
 | 2792 |  | 
 | 2793 | 	if (out->writecallback) { | 
 | 2794 | 	    /* | 
 | 2795 | 	     * second write the stuff to the I/O channel | 
 | 2796 | 	     */ | 
 | 2797 | 	    if (out->encoder != NULL) { | 
 | 2798 | 		ret = out->writecallback(out->context,  | 
 | 2799 | 				 (const char *)out->conv->content, nbchars); | 
 | 2800 | 		if (ret >= 0) | 
| Daniel Veillard | edddff9 | 2001-05-02 10:58:52 +0000 | [diff] [blame] | 2801 | 		    xmlBufferShrink(out->conv, ret); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2802 | 	    } else { | 
 | 2803 | 		ret = out->writecallback(out->context,  | 
 | 2804 | 				 (const char *)out->buffer->content, nbchars); | 
 | 2805 | 		if (ret >= 0) | 
| Daniel Veillard | edddff9 | 2001-05-02 10:58:52 +0000 | [diff] [blame] | 2806 | 		    xmlBufferShrink(out->buffer, ret); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2807 | 	    } | 
 | 2808 | 	    if (ret < 0) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 2809 | 		xmlIOErr(XML_IO_WRITE, NULL); | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 2810 | 		out->error = XML_IO_WRITE; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2811 | 		return(ret); | 
 | 2812 | 	    } | 
 | 2813 | 	    out->written += ret; | 
 | 2814 | 	} | 
 | 2815 | 	written += nbchars; | 
 | 2816 |     } while (len > 0); | 
 | 2817 |  | 
 | 2818 | done: | 
 | 2819 | #ifdef DEBUG_INPUT | 
 | 2820 |     xmlGenericError(xmlGenericErrorContext, | 
 | 2821 | 	    "I/O: wrote %d chars\n", written); | 
 | 2822 | #endif | 
 | 2823 |     return(written); | 
 | 2824 | } | 
 | 2825 |  | 
 | 2826 | /** | 
 | 2827 |  * xmlOutputBufferWriteString: | 
 | 2828 |  * @out:  a buffered parser output | 
 | 2829 |  * @str:  a zero terminated C string | 
 | 2830 |  * | 
 | 2831 |  * Write the content of the string in the output I/O buffer | 
 | 2832 |  * This routine handle the I18N transcoding from internal UTF-8 | 
 | 2833 |  * The buffer is lossless, i.e. will store in case of partial | 
 | 2834 |  * or delayed writes. | 
 | 2835 |  * | 
 | 2836 |  * Returns the number of chars immediately written, or -1 | 
 | 2837 |  *         in case of error. | 
 | 2838 |  */ | 
 | 2839 | int | 
 | 2840 | xmlOutputBufferWriteString(xmlOutputBufferPtr out, const char *str) { | 
 | 2841 |     int len; | 
 | 2842 |      | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 2843 |     if ((out == NULL) || (out->error)) return(-1); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2844 |     if (str == NULL) | 
 | 2845 |         return(-1); | 
 | 2846 |     len = strlen(str); | 
 | 2847 |  | 
 | 2848 |     if (len > 0) | 
 | 2849 | 	return(xmlOutputBufferWrite(out, len, str)); | 
 | 2850 |     return(len); | 
 | 2851 | } | 
 | 2852 |  | 
 | 2853 | /** | 
 | 2854 |  * xmlOutputBufferFlush: | 
 | 2855 |  * @out:  a buffered output | 
 | 2856 |  * | 
 | 2857 |  * flushes the output I/O channel | 
 | 2858 |  * | 
 | 2859 |  * Returns the number of byte written or -1 in case of error. | 
 | 2860 |  */ | 
 | 2861 | int | 
 | 2862 | xmlOutputBufferFlush(xmlOutputBufferPtr out) { | 
 | 2863 |     int nbchars = 0, ret = 0; | 
 | 2864 |  | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 2865 |     if ((out == NULL) || (out->error)) return(-1); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2866 |     /* | 
 | 2867 |      * first handle encoding stuff. | 
 | 2868 |      */ | 
 | 2869 |     if ((out->conv != NULL) && (out->encoder != NULL)) { | 
 | 2870 | 	/* | 
 | 2871 | 	 * convert as much as possible to the parser reading buffer. | 
 | 2872 | 	 */ | 
 | 2873 | 	nbchars = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer); | 
 | 2874 | 	if (nbchars < 0) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 2875 | 	    xmlIOErr(XML_IO_ENCODER, NULL); | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 2876 | 	    out->error = XML_IO_ENCODER; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2877 | 	    return(-1); | 
 | 2878 | 	} | 
 | 2879 |     } | 
 | 2880 |  | 
 | 2881 |     /* | 
 | 2882 |      * second flush the stuff to the I/O channel | 
 | 2883 |      */ | 
 | 2884 |     if ((out->conv != NULL) && (out->encoder != NULL) && | 
 | 2885 | 	(out->writecallback != NULL)) { | 
 | 2886 | 	ret = out->writecallback(out->context, | 
 | 2887 | 	           (const char *)out->conv->content, out->conv->use); | 
 | 2888 | 	if (ret >= 0) | 
 | 2889 | 	    xmlBufferShrink(out->conv, ret); | 
 | 2890 |     } else if (out->writecallback != NULL) { | 
 | 2891 | 	ret = out->writecallback(out->context, | 
 | 2892 | 	           (const char *)out->buffer->content, out->buffer->use); | 
 | 2893 | 	if (ret >= 0) | 
 | 2894 | 	    xmlBufferShrink(out->buffer, ret); | 
 | 2895 |     } | 
 | 2896 |     if (ret < 0) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 2897 | 	xmlIOErr(XML_IO_FLUSH, NULL); | 
| Daniel Veillard | 97bf4d0 | 2003-10-08 18:58:28 +0000 | [diff] [blame] | 2898 | 	out->error = XML_IO_FLUSH; | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2899 | 	return(ret); | 
 | 2900 |     } | 
 | 2901 |     out->written += ret; | 
 | 2902 |  | 
 | 2903 | #ifdef DEBUG_INPUT | 
 | 2904 |     xmlGenericError(xmlGenericErrorContext, | 
 | 2905 | 	    "I/O: flushed %d chars\n", ret); | 
 | 2906 | #endif | 
 | 2907 |     return(ret); | 
 | 2908 | } | 
| Daniel Veillard | a9cce9c | 2003-09-29 13:20:24 +0000 | [diff] [blame] | 2909 | #endif /* LIBXML_OUTPUT_ENABLED */ | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2910 |  | 
| Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 2911 | /** | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2912 |  * xmlParserGetDirectory: | 
 | 2913 |  * @filename:  the path to a file | 
 | 2914 |  * | 
 | 2915 |  * lookup the directory for that file | 
 | 2916 |  * | 
 | 2917 |  * Returns a new allocated string containing the directory, or NULL. | 
 | 2918 |  */ | 
 | 2919 | char * | 
 | 2920 | xmlParserGetDirectory(const char *filename) { | 
 | 2921 |     char *ret = NULL; | 
 | 2922 |     char dir[1024]; | 
 | 2923 |     char *cur; | 
 | 2924 |     char sep = '/'; | 
 | 2925 |  | 
| Igor Zlatkovic | 9181cc0 | 2002-09-29 17:51:06 +0000 | [diff] [blame] | 2926 | #ifdef _WIN32_WCE  /* easy way by now ... wince does not have dirs! */ | 
 | 2927 |     return NULL; | 
 | 2928 | #endif | 
 | 2929 |  | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2930 |     if (xmlInputCallbackInitialized == 0) | 
 | 2931 | 	xmlRegisterDefaultInputCallbacks(); | 
 | 2932 |  | 
 | 2933 |     if (filename == NULL) return(NULL); | 
| Daniel Veillard | 3c5ed91 | 2002-01-08 10:36:16 +0000 | [diff] [blame] | 2934 | #if defined(WIN32) && !defined(__CYGWIN__) | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 2935 |     sep = '\\'; | 
 | 2936 | #endif | 
 | 2937 |  | 
 | 2938 |     strncpy(dir, filename, 1023); | 
 | 2939 |     dir[1023] = 0; | 
 | 2940 |     cur = &dir[strlen(dir)]; | 
 | 2941 |     while (cur > dir) { | 
 | 2942 |          if (*cur == sep) break; | 
 | 2943 | 	 cur --; | 
 | 2944 |     } | 
 | 2945 |     if (*cur == sep) { | 
 | 2946 |         if (cur == dir) dir[1] = 0; | 
 | 2947 | 	else *cur = 0; | 
 | 2948 | 	ret = xmlMemStrdup(dir); | 
 | 2949 |     } else { | 
 | 2950 |         if (getcwd(dir, 1024) != NULL) { | 
 | 2951 | 	    dir[1023] = 0; | 
 | 2952 | 	    ret = xmlMemStrdup(dir); | 
 | 2953 | 	} | 
 | 2954 |     } | 
 | 2955 |     return(ret); | 
 | 2956 | } | 
 | 2957 |  | 
 | 2958 | /**************************************************************** | 
 | 2959 |  *								* | 
 | 2960 |  *		External entities loading			* | 
 | 2961 |  *								* | 
 | 2962 |  ****************************************************************/ | 
 | 2963 |  | 
| Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 2964 | /** | 
 | 2965 |  * xmlCheckHTTPInput: | 
 | 2966 |  * @ctxt: an XML parser context | 
 | 2967 |  * @ret: an XML parser input | 
 | 2968 |  * | 
 | 2969 |  * Check an input in case it was created from an HTTP stream, in that | 
 | 2970 |  * case it will handle encoding and update of the base URL in case of | 
 | 2971 |  * redirection. It also checks for HTTP errors in which case the input | 
 | 2972 |  * is cleanly freed up and an appropriate error is raised in context | 
 | 2973 |  * | 
 | 2974 |  * Returns the input or NULL in case of HTTP error. | 
 | 2975 |  */ | 
 | 2976 | xmlParserInputPtr | 
 | 2977 | xmlCheckHTTPInput(xmlParserCtxtPtr ctxt, xmlParserInputPtr ret) { | 
 | 2978 | #ifdef LIBXML_HTTP_ENABLED | 
 | 2979 |     if ((ret != NULL) && (ret->buf != NULL) && | 
 | 2980 |         (ret->buf->readcallback == xmlIOHTTPRead) && | 
 | 2981 |         (ret->buf->context != NULL)) { | 
 | 2982 |         const char *encoding; | 
 | 2983 |         const char *redir; | 
 | 2984 |         const char *mime; | 
 | 2985 |         int code; | 
 | 2986 |  | 
 | 2987 |         code = xmlNanoHTTPReturnCode(ret->buf->context); | 
 | 2988 |         if (code >= 400) { | 
 | 2989 |             /* fatal error */ | 
 | 2990 | 	    if (ret->filename != NULL) | 
| Daniel Veillard | e8039df | 2003-10-27 11:25:13 +0000 | [diff] [blame] | 2991 | 		__xmlLoaderErr(ctxt, "failed to load HTTP resource \"%s\"\n", | 
| Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 2992 |                          (const char *) ret->filename); | 
 | 2993 | 	    else | 
| Daniel Veillard | e8039df | 2003-10-27 11:25:13 +0000 | [diff] [blame] | 2994 | 		__xmlLoaderErr(ctxt, "failed to load HTTP resource\n", NULL); | 
| Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 2995 |             xmlFreeInputStream(ret); | 
 | 2996 |             ret = NULL; | 
 | 2997 |         } else { | 
 | 2998 |  | 
 | 2999 |             mime = xmlNanoHTTPMimeType(ret->buf->context); | 
 | 3000 |             if ((xmlStrstr(BAD_CAST mime, BAD_CAST "/xml")) || | 
 | 3001 |                 (xmlStrstr(BAD_CAST mime, BAD_CAST "+xml"))) { | 
 | 3002 |                 encoding = xmlNanoHTTPEncoding(ret->buf->context); | 
 | 3003 |                 if (encoding != NULL) { | 
 | 3004 |                     xmlCharEncodingHandlerPtr handler; | 
 | 3005 |  | 
 | 3006 |                     handler = xmlFindCharEncodingHandler(encoding); | 
 | 3007 |                     if (handler != NULL) { | 
 | 3008 |                         xmlSwitchInputEncoding(ctxt, ret, handler); | 
 | 3009 |                     } else { | 
 | 3010 |                         __xmlErrEncoding(ctxt, XML_ERR_UNKNOWN_ENCODING, | 
 | 3011 |                                          "Unknown encoding %s", | 
 | 3012 |                                          BAD_CAST encoding, NULL); | 
 | 3013 |                     } | 
 | 3014 |                     if (ret->encoding == NULL) | 
 | 3015 |                         ret->encoding = xmlStrdup(BAD_CAST encoding); | 
 | 3016 |                 } | 
 | 3017 | #if 0 | 
 | 3018 |             } else if (xmlStrstr(BAD_CAST mime, BAD_CAST "html")) { | 
 | 3019 | #endif | 
 | 3020 |             } | 
 | 3021 |             redir = xmlNanoHTTPRedir(ret->buf->context); | 
 | 3022 |             if (redir != NULL) { | 
 | 3023 |                 if (ret->filename != NULL) | 
 | 3024 |                     xmlFree((xmlChar *) ret->filename); | 
 | 3025 |                 if (ret->directory != NULL) { | 
 | 3026 |                     xmlFree((xmlChar *) ret->directory); | 
 | 3027 |                     ret->directory = NULL; | 
 | 3028 |                 } | 
 | 3029 |                 ret->filename = | 
 | 3030 |                     (char *) xmlStrdup((const xmlChar *) redir); | 
 | 3031 |             } | 
 | 3032 |         } | 
 | 3033 |     } | 
 | 3034 | #endif | 
 | 3035 |     return(ret); | 
 | 3036 | } | 
 | 3037 |  | 
| Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3038 | static int xmlSysIDExists(const char *URL) { | 
| Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 3039 | #ifdef HAVE_STAT | 
 | 3040 |     int ret; | 
 | 3041 |     struct stat info; | 
 | 3042 |     const char *path; | 
 | 3043 |  | 
 | 3044 |     if (URL == NULL) | 
 | 3045 | 	return(0); | 
 | 3046 |  | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 3047 |     if (!xmlStrncasecmp(BAD_CAST URL, BAD_CAST "file://localhost/", 17)) | 
| Daniel Veillard | 1997c3e | 2003-07-05 20:43:43 +0000 | [diff] [blame] | 3048 | #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__) | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 3049 | 	path = &URL[17]; | 
 | 3050 | #else | 
| Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 3051 | 	path = &URL[16]; | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 3052 | #endif | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 3053 |     else if (!xmlStrncasecmp(BAD_CAST URL, BAD_CAST "file:///", 8)) { | 
| Daniel Veillard | 1997c3e | 2003-07-05 20:43:43 +0000 | [diff] [blame] | 3054 | #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__) | 
| Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 3055 | 	path = &URL[8]; | 
 | 3056 | #else | 
 | 3057 | 	path = &URL[7]; | 
 | 3058 | #endif | 
 | 3059 |     } else  | 
 | 3060 | 	path = URL; | 
 | 3061 |     ret = stat(path, &info); | 
| Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3062 |     if (ret == 0) | 
 | 3063 | 	return(1); | 
| Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 3064 | #endif | 
| Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3065 |     return(0); | 
| Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 3066 | } | 
| Daniel Veillard | 6990bf3 | 2001-08-23 21:17:48 +0000 | [diff] [blame] | 3067 |  | 
| Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 3068 | /** | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3069 |  * xmlDefaultExternalEntityLoader: | 
 | 3070 |  * @URL:  the URL for the entity to load | 
 | 3071 |  * @ID:  the System ID for the entity to load | 
 | 3072 |  * @ctxt:  the context in which the entity is called or NULL | 
 | 3073 |  * | 
 | 3074 |  * By default we don't load external entitites, yet. | 
 | 3075 |  * | 
 | 3076 |  * Returns a new allocated xmlParserInputPtr, or NULL. | 
 | 3077 |  */ | 
| Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 3078 | static xmlParserInputPtr | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3079 | xmlDefaultExternalEntityLoader(const char *URL, const char *ID, | 
| Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 3080 |                                xmlParserCtxtPtr ctxt) | 
 | 3081 | { | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3082 |     xmlParserInputPtr ret = NULL; | 
| Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 3083 |     xmlChar *resource = NULL; | 
| Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 3084 |  | 
| Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 3085 | #ifdef LIBXML_CATALOG_ENABLED | 
| Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3086 |     xmlCatalogAllow pref; | 
| Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 3087 | #endif | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3088 |  | 
 | 3089 | #ifdef DEBUG_EXTERNAL_ENTITIES | 
 | 3090 |     xmlGenericError(xmlGenericErrorContext, | 
| Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 3091 |                     "xmlDefaultExternalEntityLoader(%s, xxx)\n", URL); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3092 | #endif | 
| Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 3093 | #ifdef LIBXML_CATALOG_ENABLED | 
 | 3094 |     /* | 
| Daniel Veillard | e2940dd | 2001-08-22 00:06:49 +0000 | [diff] [blame] | 3095 |      * If the resource doesn't exists as a file, | 
| Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 3096 |      * try to load it from the resource pointed in the catalogs | 
| Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 3097 |      */ | 
| Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3098 |     pref = xmlCatalogGetDefaults(); | 
 | 3099 |  | 
| Daniel Veillard | 561b7f8 | 2002-03-20 21:55:57 +0000 | [diff] [blame] | 3100 |     if ((pref != XML_CATA_ALLOW_NONE) && (!xmlSysIDExists(URL))) { | 
| Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 3101 |         /* | 
 | 3102 |          * Do a local lookup | 
 | 3103 |          */ | 
 | 3104 |         if ((ctxt->catalogs != NULL) && | 
 | 3105 |             ((pref == XML_CATA_ALLOW_ALL) || | 
 | 3106 |              (pref == XML_CATA_ALLOW_DOCUMENT))) { | 
 | 3107 |             resource = xmlCatalogLocalResolve(ctxt->catalogs, | 
 | 3108 |                                               (const xmlChar *) ID, | 
 | 3109 |                                               (const xmlChar *) URL); | 
| Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3110 |         } | 
| Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 3111 |         /* | 
 | 3112 |          * Try a global lookup | 
 | 3113 |          */ | 
 | 3114 |         if ((resource == NULL) && | 
 | 3115 |             ((pref == XML_CATA_ALLOW_ALL) || | 
 | 3116 |              (pref == XML_CATA_ALLOW_GLOBAL))) { | 
 | 3117 |             resource = xmlCatalogResolve((const xmlChar *) ID, | 
 | 3118 |                                          (const xmlChar *) URL); | 
 | 3119 |         } | 
 | 3120 |         if ((resource == NULL) && (URL != NULL)) | 
 | 3121 |             resource = xmlStrdup((const xmlChar *) URL); | 
| Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 3122 |  | 
| Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 3123 |         /* | 
 | 3124 |          * TODO: do an URI lookup on the reference | 
 | 3125 |          */ | 
 | 3126 |         if ((resource != NULL) | 
 | 3127 |             && (!xmlSysIDExists((const char *) resource))) { | 
 | 3128 |             xmlChar *tmp = NULL; | 
| Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 3129 |  | 
| Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 3130 |             if ((ctxt->catalogs != NULL) && | 
 | 3131 |                 ((pref == XML_CATA_ALLOW_ALL) || | 
 | 3132 |                  (pref == XML_CATA_ALLOW_DOCUMENT))) { | 
 | 3133 |                 tmp = xmlCatalogLocalResolveURI(ctxt->catalogs, resource); | 
 | 3134 |             } | 
 | 3135 |             if ((tmp == NULL) && | 
 | 3136 |                 ((pref == XML_CATA_ALLOW_ALL) || | 
 | 3137 |                  (pref == XML_CATA_ALLOW_GLOBAL))) { | 
 | 3138 |                 tmp = xmlCatalogResolveURI(resource); | 
 | 3139 |             } | 
| Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 3140 |  | 
| Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 3141 |             if (tmp != NULL) { | 
 | 3142 |                 xmlFree(resource); | 
 | 3143 |                 resource = tmp; | 
 | 3144 |             } | 
 | 3145 |         } | 
| Daniel Veillard | 5d90b6c | 2001-08-22 14:29:45 +0000 | [diff] [blame] | 3146 |     } | 
| Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 3147 | #endif | 
 | 3148 |  | 
 | 3149 |     if (resource == NULL) | 
| Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 3150 |         resource = (xmlChar *) URL; | 
| Daniel Veillard | 7d6fd21 | 2001-05-10 15:34:11 +0000 | [diff] [blame] | 3151 |  | 
 | 3152 |     if (resource == NULL) { | 
| Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 3153 |         if (ID == NULL) | 
 | 3154 |             ID = "NULL"; | 
| Daniel Veillard | e8039df | 2003-10-27 11:25:13 +0000 | [diff] [blame] | 3155 |         __xmlLoaderErr(ctxt, "failed to load external entity \"%s\"\n", ID); | 
| Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 3156 |         return (NULL); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3157 |     } | 
| Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 3158 |     ret = xmlNewInputFromFile(ctxt, (const char *) resource); | 
| Daniel Veillard | dc2cee2 | 2001-08-22 16:30:37 +0000 | [diff] [blame] | 3159 |     if ((resource != NULL) && (resource != (xmlChar *) URL)) | 
| Daniel Veillard | a840b69 | 2003-10-19 13:35:37 +0000 | [diff] [blame] | 3160 |         xmlFree(resource); | 
 | 3161 |     return (ret); | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3162 | } | 
 | 3163 |  | 
 | 3164 | static xmlExternalEntityLoader xmlCurrentExternalEntityLoader = | 
 | 3165 |        xmlDefaultExternalEntityLoader; | 
 | 3166 |  | 
| Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 3167 | /** | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3168 |  * xmlSetExternalEntityLoader: | 
 | 3169 |  * @f:  the new entity resolver function | 
 | 3170 |  * | 
 | 3171 |  * Changes the defaultexternal entity resolver function for the application | 
 | 3172 |  */ | 
 | 3173 | void | 
 | 3174 | xmlSetExternalEntityLoader(xmlExternalEntityLoader f) { | 
 | 3175 |     xmlCurrentExternalEntityLoader = f; | 
 | 3176 | } | 
 | 3177 |  | 
| Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 3178 | /** | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3179 |  * xmlGetExternalEntityLoader: | 
 | 3180 |  * | 
 | 3181 |  * Get the default external entity resolver function for the application | 
 | 3182 |  * | 
 | 3183 |  * Returns the xmlExternalEntityLoader function pointer | 
 | 3184 |  */ | 
 | 3185 | xmlExternalEntityLoader | 
 | 3186 | xmlGetExternalEntityLoader(void) { | 
 | 3187 |     return(xmlCurrentExternalEntityLoader); | 
 | 3188 | } | 
 | 3189 |  | 
| Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 3190 | /** | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3191 |  * xmlLoadExternalEntity: | 
 | 3192 |  * @URL:  the URL for the entity to load | 
| Daniel Veillard | 9f7b84b | 2001-08-23 15:31:19 +0000 | [diff] [blame] | 3193 |  * @ID:  the Public ID for the entity to load | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3194 |  * @ctxt:  the context in which the entity is called or NULL | 
 | 3195 |  * | 
 | 3196 |  * Load an external entity, note that the use of this function for | 
 | 3197 |  * unparsed entities may generate problems | 
| Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 3198 |  * TODO: a more generic External entity API must be designed | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3199 |  * | 
 | 3200 |  * Returns the xmlParserInputPtr or NULL | 
 | 3201 |  */ | 
 | 3202 | xmlParserInputPtr | 
 | 3203 | xmlLoadExternalEntity(const char *URL, const char *ID, | 
 | 3204 |                       xmlParserCtxtPtr ctxt) { | 
| Daniel Veillard | 4e9b1bc | 2003-06-09 10:30:33 +0000 | [diff] [blame] | 3205 |     if ((URL != NULL) && (xmlSysIDExists(URL) == 0)) { | 
 | 3206 | 	char *canonicFilename; | 
 | 3207 | 	xmlParserInputPtr ret; | 
 | 3208 |  | 
 | 3209 | 	canonicFilename = (char *) xmlCanonicPath((const xmlChar *) URL); | 
 | 3210 | 	if (canonicFilename == NULL) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 3211 |             xmlIOErrMemory("building canonical path\n"); | 
| Daniel Veillard | 4e9b1bc | 2003-06-09 10:30:33 +0000 | [diff] [blame] | 3212 | 	    return(NULL); | 
 | 3213 | 	} | 
 | 3214 |  | 
 | 3215 | 	ret = xmlCurrentExternalEntityLoader(canonicFilename, ID, ctxt); | 
 | 3216 | 	xmlFree(canonicFilename); | 
 | 3217 | 	return(ret); | 
 | 3218 |     } | 
| Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 3219 |     return(xmlCurrentExternalEntityLoader(URL, ID, ctxt)); | 
 | 3220 | } | 
 | 3221 |  | 
| Daniel Veillard | 8bdb91d | 2001-10-31 17:52:43 +0000 | [diff] [blame] | 3222 | /************************************************************************ | 
 | 3223 |  * 									* | 
 | 3224 |  * 		Disabling Network access				* | 
 | 3225 |  * 									* | 
 | 3226 |  ************************************************************************/ | 
 | 3227 |  | 
 | 3228 | #ifdef LIBXML_CATALOG_ENABLED | 
 | 3229 | static int | 
 | 3230 | xmlNoNetExists(const char *URL) | 
 | 3231 | { | 
 | 3232 | #ifdef HAVE_STAT | 
 | 3233 |     int ret; | 
 | 3234 |     struct stat info; | 
 | 3235 |     const char *path; | 
 | 3236 |  | 
 | 3237 |     if (URL == NULL) | 
 | 3238 |         return (0); | 
 | 3239 |  | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 3240 |     if (!xmlStrncasecmp(BAD_CAST URL, BAD_CAST "file://localhost/", 17)) | 
| Daniel Veillard | 1997c3e | 2003-07-05 20:43:43 +0000 | [diff] [blame] | 3241 | #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__) | 
| Daniel Veillard | f4862f0 | 2002-09-10 11:13:43 +0000 | [diff] [blame] | 3242 | 	path = &URL[17]; | 
 | 3243 | #else | 
 | 3244 | 	path = &URL[16]; | 
 | 3245 | #endif | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 3246 |     else if (!xmlStrncasecmp(BAD_CAST URL, BAD_CAST "file:///", 8)) { | 
| Daniel Veillard | 1997c3e | 2003-07-05 20:43:43 +0000 | [diff] [blame] | 3247 | #if defined (_WIN32) || defined (__DJGPP__) && !defined(__CYGWIN__) | 
| Daniel Veillard | 8bdb91d | 2001-10-31 17:52:43 +0000 | [diff] [blame] | 3248 |         path = &URL[8]; | 
 | 3249 | #else | 
 | 3250 |         path = &URL[7]; | 
 | 3251 | #endif | 
 | 3252 |     } else | 
 | 3253 |         path = URL; | 
 | 3254 |     ret = stat(path, &info); | 
 | 3255 |     if (ret == 0) | 
 | 3256 |         return (1); | 
 | 3257 | #endif | 
 | 3258 |     return (0); | 
 | 3259 | } | 
 | 3260 | #endif | 
 | 3261 |  | 
 | 3262 | /** | 
 | 3263 |  * xmlNoNetExternalEntityLoader: | 
 | 3264 |  * @URL:  the URL for the entity to load | 
 | 3265 |  * @ID:  the System ID for the entity to load | 
 | 3266 |  * @ctxt:  the context in which the entity is called or NULL | 
 | 3267 |  * | 
 | 3268 |  * A specific entity loader disabling network accesses, though still | 
 | 3269 |  * allowing local catalog accesses for resolution. | 
 | 3270 |  * | 
 | 3271 |  * Returns a new allocated xmlParserInputPtr, or NULL. | 
 | 3272 |  */ | 
 | 3273 | xmlParserInputPtr | 
 | 3274 | xmlNoNetExternalEntityLoader(const char *URL, const char *ID, | 
 | 3275 |                              xmlParserCtxtPtr ctxt) { | 
 | 3276 |     xmlParserInputPtr input = NULL; | 
 | 3277 |     xmlChar *resource = NULL; | 
 | 3278 |  | 
 | 3279 | #ifdef LIBXML_CATALOG_ENABLED | 
 | 3280 |     xmlCatalogAllow pref; | 
 | 3281 |  | 
 | 3282 |     /* | 
 | 3283 |      * If the resource doesn't exists as a file, | 
 | 3284 |      * try to load it from the resource pointed in the catalogs | 
 | 3285 |      */ | 
 | 3286 |     pref = xmlCatalogGetDefaults(); | 
 | 3287 |  | 
 | 3288 |     if ((pref != XML_CATA_ALLOW_NONE) && (!xmlNoNetExists(URL))) { | 
 | 3289 | 	/* | 
 | 3290 | 	 * Do a local lookup | 
 | 3291 | 	 */ | 
 | 3292 | 	if ((ctxt->catalogs != NULL) && | 
 | 3293 | 	    ((pref == XML_CATA_ALLOW_ALL) || | 
 | 3294 | 	     (pref == XML_CATA_ALLOW_DOCUMENT))) { | 
 | 3295 | 	    resource = xmlCatalogLocalResolve(ctxt->catalogs, | 
 | 3296 | 					      (const xmlChar *)ID, | 
 | 3297 | 					      (const xmlChar *)URL); | 
 | 3298 |         } | 
 | 3299 | 	/* | 
 | 3300 | 	 * Try a global lookup | 
 | 3301 | 	 */ | 
 | 3302 | 	if ((resource == NULL) && | 
 | 3303 | 	    ((pref == XML_CATA_ALLOW_ALL) || | 
 | 3304 | 	     (pref == XML_CATA_ALLOW_GLOBAL))) { | 
 | 3305 | 	    resource = xmlCatalogResolve((const xmlChar *)ID, | 
 | 3306 | 					 (const xmlChar *)URL); | 
 | 3307 | 	} | 
 | 3308 | 	if ((resource == NULL) && (URL != NULL)) | 
 | 3309 | 	    resource = xmlStrdup((const xmlChar *) URL); | 
 | 3310 |  | 
 | 3311 | 	/* | 
 | 3312 | 	 * TODO: do an URI lookup on the reference | 
 | 3313 | 	 */ | 
 | 3314 | 	if ((resource != NULL) && (!xmlNoNetExists((const char *)resource))) { | 
 | 3315 | 	    xmlChar *tmp = NULL; | 
 | 3316 |  | 
 | 3317 | 	    if ((ctxt->catalogs != NULL) && | 
 | 3318 | 		((pref == XML_CATA_ALLOW_ALL) || | 
 | 3319 | 		 (pref == XML_CATA_ALLOW_DOCUMENT))) { | 
 | 3320 | 		tmp = xmlCatalogLocalResolveURI(ctxt->catalogs, resource); | 
 | 3321 | 	    } | 
 | 3322 | 	    if ((tmp == NULL) && | 
 | 3323 | 		((pref == XML_CATA_ALLOW_ALL) || | 
 | 3324 | 	         (pref == XML_CATA_ALLOW_GLOBAL))) { | 
 | 3325 | 		tmp = xmlCatalogResolveURI(resource); | 
 | 3326 | 	    } | 
 | 3327 |  | 
 | 3328 | 	    if (tmp != NULL) { | 
 | 3329 | 		xmlFree(resource); | 
 | 3330 | 		resource = tmp; | 
 | 3331 | 	    } | 
 | 3332 | 	} | 
 | 3333 |     } | 
 | 3334 | #endif | 
 | 3335 |     if (resource == NULL) | 
 | 3336 | 	resource = (xmlChar *) URL; | 
 | 3337 |  | 
 | 3338 |     if (resource != NULL) { | 
| Aleksey Sanin | 5aac8b8 | 2002-05-01 18:32:28 +0000 | [diff] [blame] | 3339 |         if ((!xmlStrncasecmp(BAD_CAST resource, BAD_CAST "ftp://", 6)) || | 
 | 3340 |             (!xmlStrncasecmp(BAD_CAST resource, BAD_CAST "http://", 7))) { | 
| Daniel Veillard | 05d987b | 2003-10-08 11:54:57 +0000 | [diff] [blame] | 3341 |             xmlIOErr(XML_IO_NETWORK_ATTEMPT, (const char *) resource); | 
| Daniel Veillard | 8bdb91d | 2001-10-31 17:52:43 +0000 | [diff] [blame] | 3342 | 	    if (resource != (xmlChar *) URL) | 
 | 3343 | 		xmlFree(resource); | 
 | 3344 | 	    return(NULL); | 
 | 3345 | 	} | 
 | 3346 |     } | 
 | 3347 |     input = xmlDefaultExternalEntityLoader((const char *) resource, ID, ctxt); | 
 | 3348 |     if (resource != (xmlChar *) URL) | 
 | 3349 | 	xmlFree(resource); | 
 | 3350 |     return(input); | 
 | 3351 | } | 
 | 3352 |  |