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