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