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