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