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