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