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