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