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