Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +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 | * |
| 6 | * Daniel.Veillard@w3.org |
| 7 | */ |
| 8 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 9 | #ifdef WIN32 |
Daniel Veillard | 3c558c3 | 1999-12-22 11:30:41 +0000 | [diff] [blame] | 10 | #include "win32config.h" |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 11 | #else |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 12 | #include "config.h" |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 13 | #endif |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 14 | |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
| 16 | #include <string.h> |
| 17 | |
| 18 | #ifdef HAVE_SYS_TYPES_H |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 19 | #include <sys/types.h> |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 20 | #endif |
| 21 | #ifdef HAVE_SYS_STAT_H |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 22 | #include <sys/stat.h> |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 23 | #endif |
| 24 | #ifdef HAVE_FCNTL_H |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 25 | #include <fcntl.h> |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 26 | #endif |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 27 | #ifdef HAVE_UNISTD_H |
| 28 | #include <unistd.h> |
| 29 | #endif |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 30 | #ifdef HAVE_STDLIB_H |
| 31 | #include <stdlib.h> |
| 32 | #endif |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 33 | #ifdef HAVE_ZLIB_H |
| 34 | #include <zlib.h> |
| 35 | #endif |
| 36 | |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 37 | #include <libxml/xmlmemory.h> |
| 38 | #include <libxml/parser.h> |
| 39 | #include <libxml/parserInternals.h> |
| 40 | #include <libxml/xmlIO.h> |
| 41 | #include <libxml/nanohttp.h> |
| 42 | #include <libxml/nanoftp.h> |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 43 | #include <libxml/xmlerror.h> |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 44 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 45 | /* #define VERBOSE_FAILURE */ |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 46 | /* #define DEBUG_EXTERNAL_ENTITIES */ |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 47 | /* #define DEBUG_INPUT */ |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 48 | |
| 49 | #ifdef DEBUG_INPUT |
| 50 | #define MINLEN 40 |
| 51 | #else |
| 52 | #define MINLEN 4000 |
| 53 | #endif |
| 54 | |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 55 | /* |
| 56 | * Input I/O callback sets |
| 57 | */ |
| 58 | typedef struct _xmlInputCallback { |
| 59 | xmlInputMatchCallback matchcallback; |
| 60 | xmlInputOpenCallback opencallback; |
| 61 | xmlInputReadCallback readcallback; |
| 62 | xmlInputCloseCallback closecallback; |
| 63 | } xmlInputCallback; |
| 64 | |
| 65 | #define MAX_INPUT_CALLBACK 15 |
| 66 | |
| 67 | xmlInputCallback xmlInputCallbackTable[MAX_INPUT_CALLBACK]; |
| 68 | int xmlInputCallbackNr = 0; |
| 69 | int xmlInputCallbackInitialized = 0; |
| 70 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 71 | /* |
| 72 | * Output I/O callback sets |
| 73 | */ |
| 74 | typedef struct _xmlOutputCallback { |
| 75 | xmlOutputMatchCallback matchcallback; |
| 76 | xmlOutputOpenCallback opencallback; |
| 77 | xmlOutputWriteCallback writecallback; |
| 78 | xmlOutputCloseCallback closecallback; |
| 79 | } xmlOutputCallback; |
| 80 | |
| 81 | #define MAX_OUTPUT_CALLBACK 15 |
| 82 | |
| 83 | xmlOutputCallback xmlOutputCallbackTable[MAX_OUTPUT_CALLBACK]; |
| 84 | int xmlOutputCallbackNr = 0; |
| 85 | int xmlOutputCallbackInitialized = 0; |
| 86 | |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 87 | /************************************************************************ |
| 88 | * * |
| 89 | * Standard I/O for file accesses * |
| 90 | * * |
| 91 | ************************************************************************/ |
| 92 | |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 93 | int |
| 94 | xmlNop(void) { |
| 95 | return(0); |
| 96 | } |
| 97 | |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 98 | /** |
| 99 | * xmlFdMatch: |
| 100 | * @filename: the URI for matching |
| 101 | * |
| 102 | * input from file descriptor |
| 103 | * |
| 104 | * Returns 1 if matches, 0 otherwise |
| 105 | */ |
| 106 | int |
| 107 | xmlFdMatch (const char *filename) { |
| 108 | return(1); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * xmlFdOpen: |
| 113 | * @filename: the URI for matching |
| 114 | * |
| 115 | * input from file descriptor, supports compressed input |
| 116 | * if @filename is " " then the standard input is used |
| 117 | * |
| 118 | * Returns an I/O context or NULL in case of error |
| 119 | */ |
| 120 | void * |
| 121 | xmlFdOpen (const char *filename) { |
| 122 | const char *path = NULL; |
| 123 | int fd; |
| 124 | |
| 125 | if (!strcmp(filename, "-")) { |
| 126 | fd = 0; |
| 127 | return((void *) fd); |
| 128 | } |
| 129 | |
| 130 | if (!strncmp(filename, "file://localhost", 16)) |
| 131 | path = &filename[16]; |
| 132 | else if (!strncmp(filename, "file:///", 8)) |
| 133 | path = &filename[8]; |
| 134 | else if (filename[0] == '/') |
| 135 | path = filename; |
| 136 | if (path == NULL) |
| 137 | return(NULL); |
| 138 | |
| 139 | #ifdef WIN32 |
| 140 | fd = _open (filename, O_RDONLY | _O_BINARY); |
| 141 | #else |
| 142 | fd = open (filename, O_RDONLY); |
| 143 | #endif |
| 144 | |
| 145 | return((void *) fd); |
| 146 | } |
| 147 | |
| 148 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 149 | * xmlFdOpenW: |
| 150 | * @filename: the URI for matching |
| 151 | * |
| 152 | * input from file descriptor, |
| 153 | * if @filename is "-" then the standard output is used |
| 154 | * |
| 155 | * Returns an I/O context or NULL in case of error |
| 156 | */ |
| 157 | void * |
| 158 | xmlFdOpenW (const char *filename) { |
| 159 | const char *path = NULL; |
| 160 | int fd; |
| 161 | |
| 162 | if (!strcmp(filename, "-")) { |
| 163 | fd = 1; |
| 164 | return((void *) fd); |
| 165 | } |
| 166 | |
| 167 | if (!strncmp(filename, "file://localhost", 16)) |
| 168 | path = &filename[16]; |
| 169 | else if (!strncmp(filename, "file:///", 8)) |
| 170 | path = &filename[8]; |
| 171 | else if (filename[0] == '/') |
| 172 | path = filename; |
| 173 | if (path == NULL) |
| 174 | return(NULL); |
| 175 | |
| 176 | fd = open (filename, O_WRONLY); |
| 177 | |
| 178 | return((void *) fd); |
| 179 | } |
| 180 | |
| 181 | /** |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 182 | * xmlFdRead: |
| 183 | * @context: the I/O context |
| 184 | * @buffer: where to drop data |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 185 | * @len: number of bytes to read |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 186 | * |
| 187 | * Read @len bytes to @buffer from the I/O channel. |
| 188 | * |
| 189 | * Returns the number of bytes written |
| 190 | */ |
| 191 | int |
| 192 | xmlFdRead (void * context, char * buffer, int len) { |
| 193 | return(read((int) context, &buffer[0], len)); |
| 194 | } |
| 195 | |
| 196 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 197 | * xmlFdWrite: |
| 198 | * @context: the I/O context |
| 199 | * @buffer: where to get data |
| 200 | * @len: number of bytes to write |
| 201 | * |
| 202 | * Write @len bytes from @buffer to the I/O channel. |
| 203 | * |
| 204 | * Returns the number of bytes written |
| 205 | */ |
| 206 | int |
| 207 | xmlFdWrite (void * context, const char * buffer, int len) { |
| 208 | return(write((int) context, &buffer[0], len)); |
| 209 | } |
| 210 | |
| 211 | /** |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 212 | * xmlFdClose: |
| 213 | * @context: the I/O context |
| 214 | * |
| 215 | * Close an I/O channel |
| 216 | */ |
| 217 | void |
| 218 | xmlFdClose (void * context) { |
| 219 | close((int) context); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * xmlFileMatch: |
| 224 | * @filename: the URI for matching |
| 225 | * |
| 226 | * input from FILE * |
| 227 | * |
| 228 | * Returns 1 if matches, 0 otherwise |
| 229 | */ |
| 230 | int |
| 231 | xmlFileMatch (const char *filename) { |
| 232 | return(1); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * xmlFileOpen: |
| 237 | * @filename: the URI for matching |
| 238 | * |
| 239 | * input from FILE *, supports compressed input |
| 240 | * if @filename is " " then the standard input is used |
| 241 | * |
| 242 | * Returns an I/O context or NULL in case of error |
| 243 | */ |
| 244 | void * |
| 245 | xmlFileOpen (const char *filename) { |
| 246 | const char *path = NULL; |
| 247 | FILE *fd; |
| 248 | |
| 249 | if (!strcmp(filename, "-")) { |
| 250 | fd = stdin; |
| 251 | return((void *) fd); |
| 252 | } |
| 253 | |
| 254 | if (!strncmp(filename, "file://localhost", 16)) |
| 255 | path = &filename[16]; |
| 256 | else if (!strncmp(filename, "file:///", 8)) |
| 257 | path = &filename[8]; |
| 258 | else |
| 259 | path = filename; |
| 260 | if (path == NULL) |
| 261 | return(NULL); |
| 262 | |
| 263 | #ifdef WIN32 |
| 264 | fd = fopen(path, "rb"); |
| 265 | #else |
| 266 | fd = fopen(path, "r"); |
| 267 | #endif /* WIN32 */ |
| 268 | return((void *) fd); |
| 269 | } |
| 270 | |
| 271 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 272 | * xmlFileOpenW: |
| 273 | * @filename: the URI for matching |
| 274 | * |
| 275 | * output to from FILE *, |
| 276 | * if @filename is "-" then the standard output is used |
| 277 | * |
| 278 | * Returns an I/O context or NULL in case of error |
| 279 | */ |
| 280 | void * |
| 281 | xmlFileOpenW (const char *filename) { |
| 282 | const char *path = NULL; |
| 283 | FILE *fd; |
| 284 | |
| 285 | if (!strcmp(filename, "-")) { |
| 286 | fd = stdout; |
| 287 | return((void *) fd); |
| 288 | } |
| 289 | |
| 290 | if (!strncmp(filename, "file://localhost", 16)) |
| 291 | path = &filename[16]; |
| 292 | else if (!strncmp(filename, "file:///", 8)) |
| 293 | path = &filename[8]; |
| 294 | else |
| 295 | path = filename; |
| 296 | if (path == NULL) |
| 297 | return(NULL); |
| 298 | |
| 299 | fd = fopen(path, "w"); |
| 300 | return((void *) fd); |
| 301 | } |
| 302 | |
| 303 | /** |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 304 | * xmlFileRead: |
| 305 | * @context: the I/O context |
| 306 | * @buffer: where to drop data |
| 307 | * @len: number of bytes to write |
| 308 | * |
| 309 | * Read @len bytes to @buffer from the I/O channel. |
| 310 | * |
| 311 | * Returns the number of bytes written |
| 312 | */ |
| 313 | int |
| 314 | xmlFileRead (void * context, char * buffer, int len) { |
| 315 | return(fread(&buffer[0], 1, len, (FILE *) context)); |
| 316 | } |
| 317 | |
| 318 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 319 | * xmlFileWrite: |
| 320 | * @context: the I/O context |
| 321 | * @buffer: where to drop data |
| 322 | * @len: number of bytes to write |
| 323 | * |
| 324 | * Write @len bytes from @buffer to the I/O channel. |
| 325 | * |
| 326 | * Returns the number of bytes written |
| 327 | */ |
| 328 | int |
| 329 | xmlFileWrite (void * context, const char * buffer, int len) { |
| 330 | return(fwrite(&buffer[0], 1, len, (FILE *) context)); |
| 331 | } |
| 332 | |
| 333 | /** |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 334 | * xmlFileClose: |
| 335 | * @context: the I/O context |
| 336 | * |
| 337 | * Close an I/O channel |
| 338 | */ |
| 339 | void |
| 340 | xmlFileClose (void * context) { |
| 341 | fclose((FILE *) context); |
| 342 | } |
| 343 | |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 344 | /** |
| 345 | * xmlFileFlush: |
| 346 | * @context: the I/O context |
| 347 | * |
| 348 | * Flush an I/O channel |
| 349 | */ |
| 350 | void |
| 351 | xmlFileFlush (void * context) { |
| 352 | fflush((FILE *) context); |
| 353 | } |
| 354 | |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 355 | #ifdef HAVE_ZLIB_H |
| 356 | /************************************************************************ |
| 357 | * * |
| 358 | * I/O for compressed file accesses * |
| 359 | * * |
| 360 | ************************************************************************/ |
| 361 | /** |
| 362 | * xmlGzfileMatch: |
| 363 | * @filename: the URI for matching |
| 364 | * |
| 365 | * input from compressed file test |
| 366 | * |
| 367 | * Returns 1 if matches, 0 otherwise |
| 368 | */ |
| 369 | int |
| 370 | xmlGzfileMatch (const char *filename) { |
| 371 | return(1); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * xmlGzfileOpen: |
| 376 | * @filename: the URI for matching |
| 377 | * |
| 378 | * input from compressed file open |
| 379 | * if @filename is " " then the standard input is used |
| 380 | * |
| 381 | * Returns an I/O context or NULL in case of error |
| 382 | */ |
| 383 | void * |
| 384 | xmlGzfileOpen (const char *filename) { |
| 385 | const char *path = NULL; |
| 386 | gzFile fd; |
| 387 | |
| 388 | if (!strcmp(filename, "-")) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 389 | fd = gzdopen(fileno(stdin), "rb"); |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 390 | return((void *) fd); |
| 391 | } |
| 392 | |
| 393 | if (!strncmp(filename, "file://localhost", 16)) |
| 394 | path = &filename[16]; |
| 395 | else if (!strncmp(filename, "file:///", 8)) |
| 396 | path = &filename[8]; |
| 397 | else |
| 398 | path = filename; |
| 399 | |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 400 | fd = gzopen(filename, "rb"); |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 401 | return((void *) fd); |
| 402 | } |
| 403 | |
| 404 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 405 | * xmlGzfileOpenW: |
| 406 | * @filename: the URI for matching |
| 407 | * @compression: the compression factor (0 - 9 included) |
| 408 | * |
| 409 | * input from compressed file open |
| 410 | * if @filename is " " then the standard input is used |
| 411 | * |
| 412 | * Returns an I/O context or NULL in case of error |
| 413 | */ |
| 414 | void * |
| 415 | xmlGzfileOpenW (const char *filename, int compression) { |
| 416 | const char *path = NULL; |
| 417 | char mode[15]; |
| 418 | gzFile fd; |
| 419 | |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 420 | sprintf(mode, "wb%d", compression); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 421 | if (!strcmp(filename, "-")) { |
| 422 | fd = gzdopen(1, mode); |
| 423 | return((void *) fd); |
| 424 | } |
| 425 | |
| 426 | if (!strncmp(filename, "file://localhost", 16)) |
| 427 | path = &filename[16]; |
| 428 | else if (!strncmp(filename, "file:///", 8)) |
| 429 | path = &filename[8]; |
| 430 | else |
| 431 | path = filename; |
| 432 | |
| 433 | fd = gzopen(filename, mode); |
| 434 | return((void *) fd); |
| 435 | } |
| 436 | |
| 437 | /** |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 438 | * xmlGzfileRead: |
| 439 | * @context: the I/O context |
| 440 | * @buffer: where to drop data |
| 441 | * @len: number of bytes to write |
| 442 | * |
| 443 | * Read @len bytes to @buffer from the compressed I/O channel. |
| 444 | * |
| 445 | * Returns the number of bytes written |
| 446 | */ |
| 447 | int |
| 448 | xmlGzfileRead (void * context, char * buffer, int len) { |
| 449 | return(gzread((gzFile) context, &buffer[0], len)); |
| 450 | } |
| 451 | |
| 452 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 453 | * xmlGzfileWrite: |
| 454 | * @context: the I/O context |
| 455 | * @buffer: where to drop data |
| 456 | * @len: number of bytes to write |
| 457 | * |
| 458 | * Write @len bytes from @buffer to the compressed I/O channel. |
| 459 | * |
| 460 | * Returns the number of bytes written |
| 461 | */ |
| 462 | int |
| 463 | xmlGzfileWrite (void * context, const char * buffer, int len) { |
| 464 | return(gzwrite((gzFile) context, (char *) &buffer[0], len)); |
| 465 | } |
| 466 | |
| 467 | /** |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 468 | * xmlGzfileClose: |
| 469 | * @context: the I/O context |
| 470 | * |
| 471 | * Close a compressed I/O channel |
| 472 | */ |
| 473 | void |
| 474 | xmlGzfileClose (void * context) { |
| 475 | gzclose((gzFile) context); |
| 476 | } |
| 477 | #endif /* HAVE_ZLIB_H */ |
| 478 | |
| 479 | #ifdef LIBXML_HTTP_ENABLED |
| 480 | /************************************************************************ |
| 481 | * * |
| 482 | * I/O for HTTP file accesses * |
| 483 | * * |
| 484 | ************************************************************************/ |
| 485 | /** |
| 486 | * xmlIOHTTPMatch: |
| 487 | * @filename: the URI for matching |
| 488 | * |
| 489 | * check if the URI matches an HTTP one |
| 490 | * |
| 491 | * Returns 1 if matches, 0 otherwise |
| 492 | */ |
| 493 | int |
| 494 | xmlIOHTTPMatch (const char *filename) { |
| 495 | if (!strncmp(filename, "http://", 7)) |
| 496 | return(1); |
| 497 | return(0); |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * xmlIOHTTPOpen: |
| 502 | * @filename: the URI for matching |
| 503 | * |
| 504 | * open an HTTP I/O channel |
| 505 | * |
| 506 | * Returns an I/O context or NULL in case of error |
| 507 | */ |
| 508 | void * |
| 509 | xmlIOHTTPOpen (const char *filename) { |
| 510 | return(xmlNanoHTTPOpen(filename, NULL)); |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * xmlIOHTTPRead: |
| 515 | * @context: the I/O context |
| 516 | * @buffer: where to drop data |
| 517 | * @len: number of bytes to write |
| 518 | * |
| 519 | * Read @len bytes to @buffer from the I/O channel. |
| 520 | * |
| 521 | * Returns the number of bytes written |
| 522 | */ |
| 523 | int |
| 524 | xmlIOHTTPRead(void * context, char * buffer, int len) { |
| 525 | return(xmlNanoHTTPRead(context, &buffer[0], len)); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * xmlIOHTTPClose: |
| 530 | * @context: the I/O context |
| 531 | * |
| 532 | * Close an HTTP I/O channel |
| 533 | */ |
| 534 | void |
| 535 | xmlIOHTTPClose (void * context) { |
| 536 | xmlNanoHTTPClose(context); |
| 537 | } |
| 538 | #endif /* LIBXML_HTTP_ENABLED */ |
| 539 | |
| 540 | #ifdef LIBXML_FTP_ENABLED |
| 541 | /************************************************************************ |
| 542 | * * |
| 543 | * I/O for FTP file accesses * |
| 544 | * * |
| 545 | ************************************************************************/ |
| 546 | /** |
| 547 | * xmlIOFTPMatch: |
| 548 | * @filename: the URI for matching |
| 549 | * |
| 550 | * check if the URI matches an FTP one |
| 551 | * |
| 552 | * Returns 1 if matches, 0 otherwise |
| 553 | */ |
| 554 | int |
| 555 | xmlIOFTPMatch (const char *filename) { |
| 556 | if (!strncmp(filename, "ftp://", 6)) |
| 557 | return(1); |
| 558 | return(0); |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * xmlIOFTPOpen: |
| 563 | * @filename: the URI for matching |
| 564 | * |
| 565 | * open an FTP I/O channel |
| 566 | * |
| 567 | * Returns an I/O context or NULL in case of error |
| 568 | */ |
| 569 | void * |
| 570 | xmlIOFTPOpen (const char *filename) { |
| 571 | return(xmlNanoFTPOpen(filename)); |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * xmlIOFTPRead: |
| 576 | * @context: the I/O context |
| 577 | * @buffer: where to drop data |
| 578 | * @len: number of bytes to write |
| 579 | * |
| 580 | * Read @len bytes to @buffer from the I/O channel. |
| 581 | * |
| 582 | * Returns the number of bytes written |
| 583 | */ |
| 584 | int |
| 585 | xmlIOFTPRead(void * context, char * buffer, int len) { |
| 586 | return(xmlNanoFTPRead(context, &buffer[0], len)); |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * xmlIOFTPClose: |
| 591 | * @context: the I/O context |
| 592 | * |
| 593 | * Close an FTP I/O channel |
| 594 | */ |
| 595 | void |
| 596 | xmlIOFTPClose (void * context) { |
| 597 | xmlNanoFTPClose(context); |
| 598 | } |
| 599 | #endif /* LIBXML_FTP_ENABLED */ |
| 600 | |
| 601 | |
| 602 | /** |
| 603 | * xmlRegisterInputCallbacks: |
| 604 | * @match: the xmlInputMatchCallback |
| 605 | * @open: the xmlInputOpenCallback |
| 606 | * @read: the xmlInputReadCallback |
| 607 | * @close: the xmlInputCloseCallback |
| 608 | * |
| 609 | * Register a new set of I/O callback for handling parser input. |
| 610 | * |
| 611 | * Returns the registered handler number or -1 in case of error |
| 612 | */ |
| 613 | int |
| 614 | xmlRegisterInputCallbacks(xmlInputMatchCallback match, |
| 615 | xmlInputOpenCallback open, xmlInputReadCallback read, |
| 616 | xmlInputCloseCallback close) { |
| 617 | if (xmlInputCallbackNr >= MAX_INPUT_CALLBACK) { |
| 618 | return(-1); |
| 619 | } |
| 620 | xmlInputCallbackTable[xmlInputCallbackNr].matchcallback = match; |
| 621 | xmlInputCallbackTable[xmlInputCallbackNr].opencallback = open; |
| 622 | xmlInputCallbackTable[xmlInputCallbackNr].readcallback = read; |
| 623 | xmlInputCallbackTable[xmlInputCallbackNr].closecallback = close; |
| 624 | return(xmlInputCallbackNr++); |
| 625 | } |
| 626 | |
| 627 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 628 | * xmlRegisterOutputCallbacks: |
| 629 | * @match: the xmlOutputMatchCallback |
| 630 | * @open: the xmlOutputOpenCallback |
| 631 | * @write: the xmlOutputWriteCallback |
| 632 | * @close: the xmlOutputCloseCallback |
| 633 | * |
| 634 | * Register a new set of I/O callback for handling output. |
| 635 | * |
| 636 | * Returns the registered handler number or -1 in case of error |
| 637 | */ |
| 638 | int |
| 639 | xmlRegisterOutputCallbacks(xmlOutputMatchCallback match, |
| 640 | xmlOutputOpenCallback open, xmlOutputWriteCallback write, |
| 641 | xmlOutputCloseCallback close) { |
| 642 | if (xmlOutputCallbackNr >= MAX_INPUT_CALLBACK) { |
| 643 | return(-1); |
| 644 | } |
| 645 | xmlOutputCallbackTable[xmlOutputCallbackNr].matchcallback = match; |
| 646 | xmlOutputCallbackTable[xmlOutputCallbackNr].opencallback = open; |
| 647 | xmlOutputCallbackTable[xmlOutputCallbackNr].writecallback = write; |
| 648 | xmlOutputCallbackTable[xmlOutputCallbackNr].closecallback = close; |
| 649 | return(xmlOutputCallbackNr++); |
| 650 | } |
| 651 | |
| 652 | /** |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 653 | * xmlRegisterDefaultInputCallbacks: |
| 654 | * |
| 655 | * Registers the default compiled-in I/O handlers. |
| 656 | */ |
| 657 | void |
| 658 | xmlRegisterDefaultInputCallbacks(void) { |
Daniel Veillard | 7cfce32 | 2000-10-04 12:40:27 +0000 | [diff] [blame] | 659 | if (xmlInputCallbackInitialized) |
| 660 | return; |
| 661 | |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 662 | xmlRegisterInputCallbacks(xmlFileMatch, xmlFileOpen, |
| 663 | xmlFileRead, xmlFileClose); |
| 664 | #ifdef HAVE_ZLIB_H |
| 665 | xmlRegisterInputCallbacks(xmlGzfileMatch, xmlGzfileOpen, |
| 666 | xmlGzfileRead, xmlGzfileClose); |
| 667 | #endif /* HAVE_ZLIB_H */ |
| 668 | |
| 669 | #ifdef LIBXML_HTTP_ENABLED |
| 670 | xmlRegisterInputCallbacks(xmlIOHTTPMatch, xmlIOHTTPOpen, |
| 671 | xmlIOHTTPRead, xmlIOHTTPClose); |
| 672 | #endif /* LIBXML_HTTP_ENABLED */ |
| 673 | |
| 674 | #ifdef LIBXML_FTP_ENABLED |
| 675 | xmlRegisterInputCallbacks(xmlIOFTPMatch, xmlIOFTPOpen, |
| 676 | xmlIOFTPRead, xmlIOFTPClose); |
| 677 | #endif /* LIBXML_FTP_ENABLED */ |
Daniel Veillard | 7cfce32 | 2000-10-04 12:40:27 +0000 | [diff] [blame] | 678 | xmlInputCallbackInitialized = 1; |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 681 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 682 | * xmlRegisterDefaultOutputCallbacks: |
| 683 | * |
| 684 | * Registers the default compiled-in I/O handlers. |
| 685 | */ |
| 686 | void |
| 687 | xmlRegisterDefaultOutputCallbacks(void) { |
Daniel Veillard | 7cfce32 | 2000-10-04 12:40:27 +0000 | [diff] [blame] | 688 | if (xmlOutputCallbackInitialized) |
| 689 | return; |
| 690 | |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 691 | xmlRegisterOutputCallbacks(xmlFileMatch, xmlFileOpenW, |
| 692 | xmlFileWrite, xmlFileClose); |
| 693 | /********************************* |
| 694 | No way a-priori to distinguish between gzipped files from |
| 695 | uncompressed ones except opening if existing then closing |
| 696 | and saving with same compression ratio ... a pain. |
| 697 | |
| 698 | #ifdef HAVE_ZLIB_H |
| 699 | xmlRegisterOutputCallbacks(xmlGzfileMatch, xmlGzfileOpen, |
| 700 | xmlGzfileWrite, xmlGzfileClose); |
| 701 | #endif |
| 702 | No HTTP PUT support yet, patches welcome |
| 703 | |
| 704 | #ifdef LIBXML_HTTP_ENABLED |
| 705 | xmlRegisterOutputCallbacks(xmlIOHTTPMatch, xmlIOHTTPOpen, |
| 706 | xmlIOHTTPWrite, xmlIOHTTPClose); |
| 707 | #endif |
| 708 | |
| 709 | Nor FTP PUT .... |
| 710 | #ifdef LIBXML_FTP_ENABLED |
| 711 | xmlRegisterOutputCallbacks(xmlIOFTPMatch, xmlIOFTPOpen, |
| 712 | xmlIOFTPWrite, xmlIOFTPClose); |
| 713 | #endif |
| 714 | **********************************/ |
Daniel Veillard | 7cfce32 | 2000-10-04 12:40:27 +0000 | [diff] [blame] | 715 | xmlOutputCallbackInitialized = 1; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | /** |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 719 | * xmlAllocParserInputBuffer: |
| 720 | * @enc: the charset encoding if known |
| 721 | * |
| 722 | * Create a buffered parser input for progressive parsing |
| 723 | * |
| 724 | * Returns the new parser input or NULL |
| 725 | */ |
| 726 | xmlParserInputBufferPtr |
| 727 | xmlAllocParserInputBuffer(xmlCharEncoding enc) { |
| 728 | xmlParserInputBufferPtr ret; |
| 729 | |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 730 | ret = (xmlParserInputBufferPtr) xmlMalloc(sizeof(xmlParserInputBuffer)); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 731 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 732 | xmlGenericError(xmlGenericErrorContext, |
| 733 | "xmlAllocParserInputBuffer : out of memory!\n"); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 734 | return(NULL); |
| 735 | } |
| 736 | memset(ret, 0, (size_t) sizeof(xmlParserInputBuffer)); |
| 737 | ret->buffer = xmlBufferCreate(); |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 738 | if (ret->buffer == NULL) { |
| 739 | xmlFree(ret); |
| 740 | return(NULL); |
| 741 | } |
| 742 | ret->buffer->alloc = XML_BUFFER_ALLOC_DOUBLEIT; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 743 | ret->encoder = xmlGetCharEncodingHandler(enc); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 744 | if (ret->encoder != NULL) |
| 745 | ret->raw = xmlBufferCreate(); |
| 746 | else |
| 747 | ret->raw = NULL; |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 748 | ret->readcallback = NULL; |
| 749 | ret->closecallback = NULL; |
| 750 | ret->context = NULL; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 751 | |
| 752 | return(ret); |
| 753 | } |
| 754 | |
| 755 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 756 | * xmlAllocOutputBuffer: |
| 757 | * @encoder: the encoding converter or NULL |
| 758 | * |
| 759 | * Create a buffered parser output |
| 760 | * |
| 761 | * Returns the new parser output or NULL |
| 762 | */ |
| 763 | xmlOutputBufferPtr |
| 764 | xmlAllocOutputBuffer(xmlCharEncodingHandlerPtr encoder) { |
| 765 | xmlOutputBufferPtr ret; |
| 766 | |
| 767 | ret = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer)); |
| 768 | if (ret == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 769 | xmlGenericError(xmlGenericErrorContext, |
| 770 | "xmlAllocOutputBuffer : out of memory!\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 771 | return(NULL); |
| 772 | } |
| 773 | memset(ret, 0, (size_t) sizeof(xmlOutputBuffer)); |
| 774 | ret->buffer = xmlBufferCreate(); |
| 775 | if (ret->buffer == NULL) { |
| 776 | xmlFree(ret); |
| 777 | return(NULL); |
| 778 | } |
| 779 | ret->buffer->alloc = XML_BUFFER_ALLOC_DOUBLEIT; |
| 780 | ret->encoder = encoder; |
| 781 | if (encoder != NULL) { |
| 782 | ret->conv = xmlBufferCreateSize(4000); |
| 783 | /* |
| 784 | * This call is designed to initiate the encoder state |
| 785 | */ |
| 786 | xmlCharEncOutFunc(encoder, ret->conv, NULL); |
| 787 | } else |
| 788 | ret->conv = NULL; |
| 789 | ret->writecallback = NULL; |
| 790 | ret->closecallback = NULL; |
| 791 | ret->context = NULL; |
| 792 | ret->written = 0; |
| 793 | |
| 794 | return(ret); |
| 795 | } |
| 796 | |
| 797 | /** |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 798 | * xmlFreeParserInputBuffer: |
| 799 | * @in: a buffered parser input |
| 800 | * |
| 801 | * Free up the memory used by a buffered parser input |
| 802 | */ |
| 803 | void |
| 804 | xmlFreeParserInputBuffer(xmlParserInputBufferPtr in) { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 805 | if (in->raw) { |
| 806 | xmlBufferFree(in->raw); |
| 807 | in->raw = NULL; |
| 808 | } |
| 809 | if (in->encoder != NULL) { |
| 810 | xmlCharEncCloseFunc(in->encoder); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 811 | } |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 812 | if (in->closecallback != NULL) { |
| 813 | in->closecallback(in->context); |
| 814 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 815 | if (in->buffer != NULL) { |
| 816 | xmlBufferFree(in->buffer); |
| 817 | in->buffer = NULL; |
| 818 | } |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 819 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 820 | memset(in, 0xbe, (size_t) sizeof(xmlParserInputBuffer)); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 821 | xmlFree(in); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 825 | * xmlOutputBufferClose: |
| 826 | * @out: a buffered output |
| 827 | * |
| 828 | * flushes and close the output I/O channel |
| 829 | * and free up all the associated resources |
| 830 | * |
| 831 | * Returns the number of byte written or -1 in case of error. |
| 832 | */ |
| 833 | int |
| 834 | xmlOutputBufferClose(xmlOutputBufferPtr out) { |
| 835 | int written; |
| 836 | |
| 837 | if (out == NULL) |
| 838 | return(-1); |
| 839 | xmlOutputBufferFlush(out); |
| 840 | if (out->closecallback != NULL) { |
| 841 | out->closecallback(out->context); |
| 842 | } |
| 843 | written = out->written; |
| 844 | if (out->conv) { |
| 845 | xmlBufferFree(out->conv); |
| 846 | out->conv = NULL; |
| 847 | } |
| 848 | if (out->encoder != NULL) { |
| 849 | xmlCharEncCloseFunc(out->encoder); |
| 850 | } |
| 851 | if (out->buffer != NULL) { |
| 852 | xmlBufferFree(out->buffer); |
| 853 | out->buffer = NULL; |
| 854 | } |
| 855 | |
| 856 | memset(out, 0xbe, (size_t) sizeof(xmlOutputBuffer)); |
| 857 | xmlFree(out); |
| 858 | return(written); |
| 859 | } |
| 860 | |
| 861 | /** |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 862 | * xmlParserInputBufferCreateFilename: |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 863 | * @URI: a C string containing the URI or filename |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 864 | * @enc: the charset encoding if known |
| 865 | * |
| 866 | * Create a buffered parser input for the progressive parsing of a file |
| 867 | * If filename is "-' then we use stdin as the input. |
| 868 | * Automatic support for ZLIB/Compress compressed document is provided |
| 869 | * by default if found at compile-time. |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 870 | * Do an encoding check if enc == XML_CHAR_ENCODING_NONE |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 871 | * |
| 872 | * Returns the new parser input or NULL |
| 873 | */ |
| 874 | xmlParserInputBufferPtr |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 875 | xmlParserInputBufferCreateFilename(const char *URI, xmlCharEncoding enc) { |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 876 | xmlParserInputBufferPtr ret; |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 877 | int i; |
| 878 | void *context = NULL; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 879 | |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 880 | if (xmlInputCallbackInitialized == 0) |
| 881 | xmlRegisterDefaultInputCallbacks(); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 882 | |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 883 | if (URI == NULL) return(NULL); |
| 884 | |
| 885 | /* |
| 886 | * Try to find one of the input accept method accepting taht scheme |
| 887 | * Go in reverse to give precedence to user defined handlers. |
| 888 | */ |
| 889 | for (i = xmlInputCallbackNr - 1;i >= 0;i--) { |
| 890 | if ((xmlInputCallbackTable[i].matchcallback != NULL) && |
| 891 | (xmlInputCallbackTable[i].matchcallback(URI) != 0)) { |
| 892 | context = xmlInputCallbackTable[i].opencallback(URI); |
| 893 | if (context != NULL) |
| 894 | break; |
Daniel Veillard | 7f7d111 | 1999-09-22 09:46:25 +0000 | [diff] [blame] | 895 | } |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 896 | } |
| 897 | if (context == NULL) { |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 898 | return(NULL); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 899 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 900 | |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 901 | /* |
| 902 | * Allocate the Input buffer front-end. |
| 903 | */ |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 904 | ret = xmlAllocParserInputBuffer(enc); |
| 905 | if (ret != NULL) { |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 906 | ret->context = context; |
| 907 | ret->readcallback = xmlInputCallbackTable[i].readcallback; |
| 908 | ret->closecallback = xmlInputCallbackTable[i].closecallback; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 909 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 910 | return(ret); |
| 911 | } |
| 912 | |
| 913 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 914 | * xmlOutputBufferCreateFilename: |
| 915 | * @URI: a C string containing the URI or filename |
| 916 | * @encoder: the encoding converter or NULL |
| 917 | * @compression: the compression ration (0 none, 9 max). |
| 918 | * |
| 919 | * Create a buffered output for the progressive saving of a file |
| 920 | * If filename is "-' then we use stdout as the output. |
| 921 | * Automatic support for ZLIB/Compress compressed document is provided |
| 922 | * by default if found at compile-time. |
| 923 | * TODO: currently if compression is set, the library only support |
| 924 | * writing to a local file. |
| 925 | * |
| 926 | * Returns the new output or NULL |
| 927 | */ |
| 928 | xmlOutputBufferPtr |
| 929 | xmlOutputBufferCreateFilename(const char *URI, |
| 930 | xmlCharEncodingHandlerPtr encoder, |
| 931 | int compression) { |
| 932 | xmlOutputBufferPtr ret; |
| 933 | int i; |
| 934 | void *context = NULL; |
| 935 | |
| 936 | if (xmlOutputCallbackInitialized == 0) |
| 937 | xmlRegisterDefaultOutputCallbacks(); |
| 938 | |
| 939 | if (URI == NULL) return(NULL); |
| 940 | |
| 941 | #ifdef HAVE_ZLIB_H |
| 942 | if ((compression > 0) && (compression <= 9)) { |
| 943 | context = xmlGzfileOpenW(URI, compression); |
| 944 | if (context != NULL) { |
| 945 | ret = xmlAllocOutputBuffer(encoder); |
| 946 | if (ret != NULL) { |
| 947 | ret->context = context; |
| 948 | ret->writecallback = xmlGzfileWrite; |
| 949 | ret->closecallback = xmlGzfileClose; |
| 950 | } |
| 951 | return(ret); |
| 952 | } |
| 953 | } |
| 954 | #endif |
| 955 | |
| 956 | /* |
| 957 | * Try to find one of the output accept method accepting taht scheme |
| 958 | * Go in reverse to give precedence to user defined handlers. |
| 959 | */ |
| 960 | for (i = xmlOutputCallbackNr - 1;i >= 0;i--) { |
| 961 | if ((xmlOutputCallbackTable[i].matchcallback != NULL) && |
| 962 | (xmlOutputCallbackTable[i].matchcallback(URI) != 0)) { |
| 963 | context = xmlOutputCallbackTable[i].opencallback(URI); |
| 964 | if (context != NULL) |
| 965 | break; |
| 966 | } |
| 967 | } |
| 968 | if (context == NULL) { |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 969 | return(NULL); |
| 970 | } |
| 971 | |
| 972 | /* |
| 973 | * Allocate the Output buffer front-end. |
| 974 | */ |
| 975 | ret = xmlAllocOutputBuffer(encoder); |
| 976 | if (ret != NULL) { |
| 977 | ret->context = context; |
| 978 | ret->writecallback = xmlOutputCallbackTable[i].writecallback; |
| 979 | ret->closecallback = xmlOutputCallbackTable[i].closecallback; |
| 980 | } |
| 981 | return(ret); |
| 982 | } |
| 983 | |
| 984 | /** |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 985 | * xmlParserInputBufferCreateFile: |
| 986 | * @file: a FILE* |
| 987 | * @enc: the charset encoding if known |
| 988 | * |
| 989 | * Create a buffered parser input for the progressive parsing of a FILE * |
| 990 | * buffered C I/O |
| 991 | * |
| 992 | * Returns the new parser input or NULL |
| 993 | */ |
| 994 | xmlParserInputBufferPtr |
| 995 | xmlParserInputBufferCreateFile(FILE *file, xmlCharEncoding enc) { |
| 996 | xmlParserInputBufferPtr ret; |
| 997 | |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 998 | if (xmlInputCallbackInitialized == 0) |
| 999 | xmlRegisterDefaultInputCallbacks(); |
| 1000 | |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1001 | if (file == NULL) return(NULL); |
| 1002 | |
| 1003 | ret = xmlAllocParserInputBuffer(enc); |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 1004 | if (ret != NULL) { |
| 1005 | ret->context = file; |
| 1006 | ret->readcallback = xmlFileRead; |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 1007 | ret->closecallback = xmlFileFlush; |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 1008 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1009 | |
| 1010 | return(ret); |
| 1011 | } |
| 1012 | |
| 1013 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1014 | * xmlOutputBufferCreateFile: |
| 1015 | * @file: a FILE* |
| 1016 | * @encoder: the encoding converter or NULL |
| 1017 | * |
| 1018 | * Create a buffered output for the progressive saving to a FILE * |
| 1019 | * buffered C I/O |
| 1020 | * |
| 1021 | * Returns the new parser output or NULL |
| 1022 | */ |
| 1023 | xmlOutputBufferPtr |
| 1024 | xmlOutputBufferCreateFile(FILE *file, xmlCharEncodingHandlerPtr encoder) { |
| 1025 | xmlOutputBufferPtr ret; |
| 1026 | |
| 1027 | if (xmlOutputCallbackInitialized == 0) |
| 1028 | xmlRegisterDefaultOutputCallbacks(); |
| 1029 | |
| 1030 | if (file == NULL) return(NULL); |
| 1031 | |
| 1032 | ret = xmlAllocOutputBuffer(encoder); |
| 1033 | if (ret != NULL) { |
| 1034 | ret->context = file; |
| 1035 | ret->writecallback = xmlFileWrite; |
Daniel Veillard | 52afe80 | 2000-10-22 16:56:02 +0000 | [diff] [blame] | 1036 | ret->closecallback = xmlFileFlush; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | return(ret); |
| 1040 | } |
| 1041 | |
| 1042 | /** |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1043 | * xmlParserInputBufferCreateFd: |
| 1044 | * @fd: a file descriptor number |
| 1045 | * @enc: the charset encoding if known |
| 1046 | * |
| 1047 | * Create a buffered parser input for the progressive parsing for the input |
| 1048 | * from a file descriptor |
| 1049 | * |
| 1050 | * Returns the new parser input or NULL |
| 1051 | */ |
| 1052 | xmlParserInputBufferPtr |
| 1053 | xmlParserInputBufferCreateFd(int fd, xmlCharEncoding enc) { |
| 1054 | xmlParserInputBufferPtr ret; |
| 1055 | |
| 1056 | if (fd < 0) return(NULL); |
| 1057 | |
| 1058 | ret = xmlAllocParserInputBuffer(enc); |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 1059 | if (ret != NULL) { |
| 1060 | ret->context = (void *) fd; |
| 1061 | ret->readcallback = xmlFdRead; |
| 1062 | ret->closecallback = xmlFdClose; |
| 1063 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1064 | |
| 1065 | return(ret); |
| 1066 | } |
| 1067 | |
| 1068 | /** |
Daniel Veillard | 46e370e | 2000-07-21 20:32:03 +0000 | [diff] [blame] | 1069 | * xmlParserInputBufferCreateMem: |
| 1070 | * @mem: the memory input |
| 1071 | * @size: the length of the memory block |
| 1072 | * @enc: the charset encoding if known |
| 1073 | * |
| 1074 | * Create a buffered parser input for the progressive parsing for the input |
| 1075 | * from a file descriptor |
| 1076 | * |
| 1077 | * Returns the new parser input or NULL |
| 1078 | */ |
| 1079 | xmlParserInputBufferPtr |
| 1080 | xmlParserInputBufferCreateMem(const char *mem, int size, xmlCharEncoding enc) { |
| 1081 | xmlParserInputBufferPtr ret; |
| 1082 | |
| 1083 | if (size <= 0) return(NULL); |
| 1084 | if (mem == NULL) return(NULL); |
| 1085 | |
| 1086 | ret = xmlAllocParserInputBuffer(enc); |
| 1087 | if (ret != NULL) { |
| 1088 | ret->context = (void *) mem; |
| 1089 | ret->readcallback = (xmlInputReadCallback) xmlNop; |
| 1090 | ret->closecallback = NULL; |
| 1091 | xmlBufferAdd(ret->buffer, (const xmlChar *) mem, size); |
| 1092 | } |
| 1093 | |
| 1094 | return(ret); |
| 1095 | } |
| 1096 | |
| 1097 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1098 | * xmlOutputBufferCreateFd: |
| 1099 | * @fd: a file descriptor number |
| 1100 | * @encoder: the encoding converter or NULL |
| 1101 | * |
| 1102 | * Create a buffered output for the progressive saving |
| 1103 | * to a file descriptor |
| 1104 | * |
| 1105 | * Returns the new parser output or NULL |
| 1106 | */ |
| 1107 | xmlOutputBufferPtr |
| 1108 | xmlOutputBufferCreateFd(int fd, xmlCharEncodingHandlerPtr encoder) { |
| 1109 | xmlOutputBufferPtr ret; |
| 1110 | |
| 1111 | if (fd < 0) return(NULL); |
| 1112 | |
| 1113 | ret = xmlAllocOutputBuffer(encoder); |
| 1114 | if (ret != NULL) { |
| 1115 | ret->context = (void *) fd; |
| 1116 | ret->writecallback = xmlFdWrite; |
| 1117 | ret->closecallback = xmlFdClose; |
| 1118 | } |
| 1119 | |
| 1120 | return(ret); |
| 1121 | } |
| 1122 | |
| 1123 | /** |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 1124 | * xmlParserInputBufferCreateIO: |
| 1125 | * @ioread: an I/O read function |
| 1126 | * @ioclose: an I/O close function |
| 1127 | * @ioctx: an I/O handler |
| 1128 | * @enc: the charset encoding if known |
| 1129 | * |
| 1130 | * Create a buffered parser input for the progressive parsing for the input |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1131 | * from an I/O handler |
Daniel Veillard | 5e873c4 | 2000-04-12 13:27:38 +0000 | [diff] [blame] | 1132 | * |
| 1133 | * Returns the new parser input or NULL |
| 1134 | */ |
| 1135 | xmlParserInputBufferPtr |
| 1136 | xmlParserInputBufferCreateIO(xmlInputReadCallback ioread, |
| 1137 | xmlInputCloseCallback ioclose, void *ioctx, xmlCharEncoding enc) { |
| 1138 | xmlParserInputBufferPtr ret; |
| 1139 | |
| 1140 | if (ioread == NULL) return(NULL); |
| 1141 | |
| 1142 | ret = xmlAllocParserInputBuffer(enc); |
| 1143 | if (ret != NULL) { |
| 1144 | ret->context = (void *) ioctx; |
| 1145 | ret->readcallback = ioread; |
| 1146 | ret->closecallback = ioclose; |
| 1147 | } |
| 1148 | |
| 1149 | return(ret); |
| 1150 | } |
| 1151 | |
| 1152 | /** |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1153 | * xmlOutputBufferCreateIO: |
| 1154 | * @iowrite: an I/O write function |
| 1155 | * @ioclose: an I/O close function |
| 1156 | * @ioctx: an I/O handler |
| 1157 | * @enc: the charset encoding if known |
| 1158 | * |
| 1159 | * Create a buffered output for the progressive saving |
| 1160 | * to an I/O handler |
| 1161 | * |
| 1162 | * Returns the new parser output or NULL |
| 1163 | */ |
| 1164 | xmlOutputBufferPtr |
| 1165 | xmlOutputBufferCreateIO(xmlOutputWriteCallback iowrite, |
| 1166 | xmlOutputCloseCallback ioclose, void *ioctx, |
| 1167 | xmlCharEncodingHandlerPtr encoder) { |
| 1168 | xmlOutputBufferPtr ret; |
| 1169 | |
| 1170 | if (iowrite == NULL) return(NULL); |
| 1171 | |
| 1172 | ret = xmlAllocOutputBuffer(encoder); |
| 1173 | if (ret != NULL) { |
| 1174 | ret->context = (void *) ioctx; |
| 1175 | ret->writecallback = iowrite; |
| 1176 | ret->closecallback = ioclose; |
| 1177 | } |
| 1178 | |
| 1179 | return(ret); |
| 1180 | } |
| 1181 | |
| 1182 | /** |
Daniel Veillard | 7f85850 | 1999-11-17 17:32:38 +0000 | [diff] [blame] | 1183 | * xmlParserInputBufferPush: |
| 1184 | * @in: a buffered parser input |
Daniel Veillard | 7f85850 | 1999-11-17 17:32:38 +0000 | [diff] [blame] | 1185 | * @len: the size in bytes of the array. |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1186 | * @buf: an char array |
Daniel Veillard | 7f85850 | 1999-11-17 17:32:38 +0000 | [diff] [blame] | 1187 | * |
| 1188 | * Push the content of the arry in the input buffer |
| 1189 | * This routine handle the I18N transcoding to internal UTF-8 |
| 1190 | * This is used when operating the parser in progressive (push) mode. |
| 1191 | * |
| 1192 | * Returns the number of chars read and stored in the buffer, or -1 |
| 1193 | * in case of error. |
| 1194 | */ |
| 1195 | int |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 1196 | xmlParserInputBufferPush(xmlParserInputBufferPtr in, |
| 1197 | int len, const char *buf) { |
Daniel Veillard | 7f85850 | 1999-11-17 17:32:38 +0000 | [diff] [blame] | 1198 | int nbchars = 0; |
| 1199 | |
| 1200 | if (len < 0) return(0); |
| 1201 | if (in->encoder != NULL) { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1202 | /* |
| 1203 | * Store the data in the incoming raw buffer |
Daniel Veillard | 7f85850 | 1999-11-17 17:32:38 +0000 | [diff] [blame] | 1204 | */ |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1205 | if (in->raw == NULL) { |
| 1206 | in->raw = xmlBufferCreate(); |
| 1207 | } |
| 1208 | xmlBufferAdd(in->raw, (const xmlChar *) buf, len); |
| 1209 | |
| 1210 | /* |
| 1211 | * convert as much as possible to the parser reading buffer. |
| 1212 | */ |
| 1213 | nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1214 | if (nbchars < 0) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 1215 | xmlGenericError(xmlGenericErrorContext, |
| 1216 | "xmlParserInputBufferPush: encoder error\n"); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1217 | return(-1); |
| 1218 | } |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 1219 | } else { |
| 1220 | nbchars = len; |
| 1221 | xmlBufferAdd(in->buffer, (xmlChar *) buf, nbchars); |
Daniel Veillard | 7f85850 | 1999-11-17 17:32:38 +0000 | [diff] [blame] | 1222 | } |
| 1223 | #ifdef DEBUG_INPUT |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 1224 | xmlGenericError(xmlGenericErrorContext, |
| 1225 | "I/O: pushed %d chars, buffer %d/%d\n", |
Daniel Veillard | 7f85850 | 1999-11-17 17:32:38 +0000 | [diff] [blame] | 1226 | nbchars, in->buffer->use, in->buffer->size); |
| 1227 | #endif |
| 1228 | return(nbchars); |
| 1229 | } |
| 1230 | |
| 1231 | /** |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1232 | * xmlParserInputBufferGrow: |
| 1233 | * @in: a buffered parser input |
| 1234 | * @len: indicative value of the amount of chars to read |
| 1235 | * |
| 1236 | * Grow up the content of the input buffer, the old data are preserved |
| 1237 | * This routine handle the I18N transcoding to internal UTF-8 |
Daniel Veillard | 7f85850 | 1999-11-17 17:32:38 +0000 | [diff] [blame] | 1238 | * This routine is used when operating the parser in normal (pull) mode |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1239 | * |
| 1240 | * TODO: one should be able to remove one extra copy by copying directy |
| 1241 | * onto in->buffer or in->raw |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1242 | * |
| 1243 | * Returns the number of chars read and stored in the buffer, or -1 |
| 1244 | * in case of error. |
| 1245 | */ |
| 1246 | int |
| 1247 | xmlParserInputBufferGrow(xmlParserInputBufferPtr in, int len) { |
| 1248 | char *buffer = NULL; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1249 | int res = 0; |
| 1250 | int nbchars = 0; |
| 1251 | int buffree; |
| 1252 | |
| 1253 | if ((len <= MINLEN) && (len != 4)) |
| 1254 | len = MINLEN; |
| 1255 | buffree = in->buffer->size - in->buffer->use; |
| 1256 | if (buffree <= 0) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 1257 | xmlGenericError(xmlGenericErrorContext, |
| 1258 | "xmlParserInputBufferGrow : buffer full !\n"); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1259 | return(0); |
| 1260 | } |
| 1261 | if (len > buffree) |
| 1262 | len = buffree; |
| 1263 | |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 1264 | buffer = (char *) xmlMalloc((len + 1) * sizeof(char)); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1265 | if (buffer == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 1266 | xmlGenericError(xmlGenericErrorContext, |
| 1267 | "xmlParserInputBufferGrow : out of memory !\n"); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1268 | return(-1); |
| 1269 | } |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 1270 | |
| 1271 | /* |
| 1272 | * Call the read method for this I/O type. |
| 1273 | */ |
| 1274 | if (in->readcallback != NULL) { |
| 1275 | res = in->readcallback(in->context, &buffer[0], len); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1276 | } else { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 1277 | xmlGenericError(xmlGenericErrorContext, |
| 1278 | "xmlParserInputBufferGrow : no input !\n"); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1279 | xmlFree(buffer); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1280 | return(-1); |
| 1281 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1282 | if (res < 0) { |
| 1283 | perror ("read error"); |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1284 | xmlFree(buffer); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1285 | return(-1); |
| 1286 | } |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1287 | len = res; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1288 | if (in->encoder != NULL) { |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1289 | /* |
| 1290 | * Store the data in the incoming raw buffer |
| 1291 | */ |
| 1292 | if (in->raw == NULL) { |
| 1293 | in->raw = xmlBufferCreate(); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1294 | } |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1295 | xmlBufferAdd(in->raw, (const xmlChar *) buffer, len); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1296 | |
| 1297 | /* |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1298 | * convert as much as possible to the parser reading buffer. |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1299 | */ |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1300 | nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw); |
| 1301 | if (nbchars < 0) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 1302 | xmlGenericError(xmlGenericErrorContext, |
| 1303 | "xmlParserInputBufferGrow: encoder error\n"); |
Daniel Veillard | 496a1cf | 2000-05-03 14:20:55 +0000 | [diff] [blame] | 1304 | return(-1); |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1305 | } |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1306 | } else { |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1307 | nbchars = len; |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1308 | buffer[nbchars] = 0; |
Daniel Veillard | dd6b367 | 1999-09-23 22:19:22 +0000 | [diff] [blame] | 1309 | xmlBufferAdd(in->buffer, (xmlChar *) buffer, nbchars); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1310 | } |
| 1311 | #ifdef DEBUG_INPUT |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 1312 | xmlGenericError(xmlGenericErrorContext, |
| 1313 | "I/O: read %d chars, buffer %d/%d\n", |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1314 | nbchars, in->buffer->use, in->buffer->size); |
| 1315 | #endif |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1316 | xmlFree(buffer); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1317 | return(nbchars); |
| 1318 | } |
| 1319 | |
| 1320 | /** |
| 1321 | * xmlParserInputBufferRead: |
| 1322 | * @in: a buffered parser input |
| 1323 | * @len: indicative value of the amount of chars to read |
| 1324 | * |
| 1325 | * Refresh the content of the input buffer, the old data are considered |
| 1326 | * consumed |
| 1327 | * This routine handle the I18N transcoding to internal UTF-8 |
| 1328 | * |
| 1329 | * Returns the number of chars read and stored in the buffer, or -1 |
| 1330 | * in case of error. |
| 1331 | */ |
| 1332 | int |
| 1333 | xmlParserInputBufferRead(xmlParserInputBufferPtr in, int len) { |
| 1334 | /* xmlBufferEmpty(in->buffer); */ |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 1335 | if (in->readcallback != NULL) |
Daniel Veillard | dbfd641 | 1999-12-28 16:35:14 +0000 | [diff] [blame] | 1336 | return(xmlParserInputBufferGrow(in, len)); |
| 1337 | else |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1338 | return(-1); |
| 1339 | } |
| 1340 | |
| 1341 | /** |
| 1342 | * xmlOutputBufferWrite: |
| 1343 | * @out: a buffered parser output |
| 1344 | * @len: the size in bytes of the array. |
| 1345 | * @buf: an char array |
| 1346 | * |
| 1347 | * Write the content of the array in the output I/O buffer |
| 1348 | * This routine handle the I18N transcoding from internal UTF-8 |
| 1349 | * The buffer is lossless, i.e. will store in case of partial |
| 1350 | * or delayed writes. |
| 1351 | * |
| 1352 | * Returns the number of chars immediately written, or -1 |
| 1353 | * in case of error. |
| 1354 | */ |
| 1355 | int |
| 1356 | xmlOutputBufferWrite(xmlOutputBufferPtr out, int len, const char *buf) { |
| 1357 | int nbchars = 0, ret; |
| 1358 | |
| 1359 | if (len < 0) return(0); |
| 1360 | |
| 1361 | /* |
| 1362 | * first handle encoding stuff. |
| 1363 | */ |
| 1364 | if (out->encoder != NULL) { |
| 1365 | /* |
| 1366 | * Store the data in the incoming raw buffer |
| 1367 | */ |
| 1368 | if (out->conv == NULL) { |
| 1369 | out->conv = xmlBufferCreate(); |
| 1370 | } |
| 1371 | xmlBufferAdd(out->buffer, (const xmlChar *) buf, len); |
| 1372 | |
| 1373 | if (out->buffer->use < MINLEN) |
| 1374 | return(0); |
| 1375 | |
| 1376 | /* |
| 1377 | * convert as much as possible to the parser reading buffer. |
| 1378 | */ |
| 1379 | nbchars = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer); |
| 1380 | if (nbchars < 0) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 1381 | xmlGenericError(xmlGenericErrorContext, |
| 1382 | "xmlOutputBufferWrite: encoder error\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1383 | return(-1); |
| 1384 | } |
| 1385 | nbchars = out->conv->use; |
| 1386 | } else { |
| 1387 | xmlBufferAdd(out->buffer, (const xmlChar *) buf, len); |
| 1388 | nbchars = out->buffer->use; |
| 1389 | } |
| 1390 | if (nbchars < MINLEN) |
| 1391 | return(0); |
| 1392 | |
| 1393 | /* |
| 1394 | * second write the stuff to the I/O channel |
| 1395 | */ |
| 1396 | if (out->encoder != NULL) { |
| 1397 | ret = out->writecallback(out->context, |
| 1398 | (const char *)out->conv->content, nbchars); |
| 1399 | if (ret >= 0) |
| 1400 | xmlBufferShrink(out->conv, nbchars); |
| 1401 | } else { |
| 1402 | ret = out->writecallback(out->context, |
| 1403 | (const char *)out->buffer->content, nbchars); |
| 1404 | if (ret >= 0) |
| 1405 | xmlBufferShrink(out->buffer, nbchars); |
| 1406 | } |
| 1407 | if (ret < 0) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 1408 | xmlGenericError(xmlGenericErrorContext, |
| 1409 | "I/O: error %d writing %d bytes\n", ret, nbchars); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1410 | return(ret); |
| 1411 | } |
| 1412 | out->written += ret; |
| 1413 | |
| 1414 | #ifdef DEBUG_INPUT |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 1415 | xmlGenericError(xmlGenericErrorContext, |
| 1416 | "I/O: wrote %d chars\n", ret); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1417 | #endif |
| 1418 | return(nbchars); |
| 1419 | } |
| 1420 | |
| 1421 | /** |
| 1422 | * xmlOutputBufferWriteString: |
| 1423 | * @out: a buffered parser output |
| 1424 | * @str: a zero terminated C string |
| 1425 | * |
| 1426 | * Write the content of the string in the output I/O buffer |
| 1427 | * This routine handle the I18N transcoding from internal UTF-8 |
| 1428 | * The buffer is lossless, i.e. will store in case of partial |
| 1429 | * or delayed writes. |
| 1430 | * |
| 1431 | * Returns the number of chars immediately written, or -1 |
| 1432 | * in case of error. |
| 1433 | */ |
| 1434 | int |
| 1435 | xmlOutputBufferWriteString(xmlOutputBufferPtr out, const char *str) { |
| 1436 | int len; |
| 1437 | |
| 1438 | if (str == NULL) |
| 1439 | return(-1); |
| 1440 | len = strlen(str); |
| 1441 | |
| 1442 | if (len > 0) |
| 1443 | return(xmlOutputBufferWrite(out, len, str)); |
| 1444 | return(len); |
| 1445 | } |
| 1446 | |
| 1447 | /** |
| 1448 | * xmlOutputBufferFlush: |
| 1449 | * @out: a buffered output |
| 1450 | * |
| 1451 | * flushes the output I/O channel |
| 1452 | * |
| 1453 | * Returns the number of byte written or -1 in case of error. |
| 1454 | */ |
| 1455 | int |
| 1456 | xmlOutputBufferFlush(xmlOutputBufferPtr out) { |
| 1457 | int nbchars = 0, ret; |
| 1458 | |
| 1459 | /* |
| 1460 | * first handle encoding stuff. |
| 1461 | */ |
| 1462 | if ((out->conv != NULL) && (out->encoder != NULL)) { |
| 1463 | /* |
| 1464 | * convert as much as possible to the parser reading buffer. |
| 1465 | */ |
| 1466 | nbchars = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer); |
| 1467 | if (nbchars < 0) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 1468 | xmlGenericError(xmlGenericErrorContext, |
| 1469 | "xmlOutputBufferWrite: encoder error\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1470 | return(-1); |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | /* |
| 1475 | * second flush the stuff to the I/O channel |
| 1476 | */ |
| 1477 | if ((out->conv != NULL) && (out->encoder != NULL)) { |
| 1478 | ret = out->writecallback(out->context, |
| 1479 | (const char *)out->conv->content, out->conv->use); |
| 1480 | if (ret >= 0) |
| 1481 | xmlBufferShrink(out->conv, ret); |
| 1482 | } else { |
| 1483 | ret = out->writecallback(out->context, |
| 1484 | (const char *)out->buffer->content, out->buffer->use); |
| 1485 | if (ret >= 0) |
| 1486 | xmlBufferShrink(out->buffer, ret); |
| 1487 | } |
| 1488 | if (ret < 0) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 1489 | xmlGenericError(xmlGenericErrorContext, |
| 1490 | "I/O: error %d flushing %d bytes\n", ret, nbchars); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1491 | return(ret); |
| 1492 | } |
| 1493 | out->written += ret; |
| 1494 | |
| 1495 | #ifdef DEBUG_INPUT |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 1496 | xmlGenericError(xmlGenericErrorContext, |
| 1497 | "I/O: flushed %d chars\n", ret); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1498 | #endif |
| 1499 | return(ret); |
Daniel Veillard | e2d034d | 1999-07-27 19:52:06 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1502 | /* |
| 1503 | * xmlParserGetDirectory: |
| 1504 | * @filename: the path to a file |
| 1505 | * |
| 1506 | * lookup the directory for that file |
| 1507 | * |
| 1508 | * Returns a new allocated string containing the directory, or NULL. |
| 1509 | */ |
| 1510 | char * |
| 1511 | xmlParserGetDirectory(const char *filename) { |
| 1512 | char *ret = NULL; |
| 1513 | char dir[1024]; |
| 1514 | char *cur; |
| 1515 | char sep = '/'; |
| 1516 | |
Daniel Veillard | 5d211f4 | 2000-04-07 17:00:24 +0000 | [diff] [blame] | 1517 | if (xmlInputCallbackInitialized == 0) |
| 1518 | xmlRegisterDefaultInputCallbacks(); |
| 1519 | |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1520 | if (filename == NULL) return(NULL); |
| 1521 | #ifdef WIN32 |
| 1522 | sep = '\\'; |
| 1523 | #endif |
| 1524 | |
| 1525 | strncpy(dir, filename, 1023); |
| 1526 | dir[1023] = 0; |
| 1527 | cur = &dir[strlen(dir)]; |
| 1528 | while (cur > dir) { |
| 1529 | if (*cur == sep) break; |
| 1530 | cur --; |
| 1531 | } |
| 1532 | if (*cur == sep) { |
| 1533 | if (cur == dir) dir[1] = 0; |
| 1534 | else *cur = 0; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1535 | ret = xmlMemStrdup(dir); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1536 | } else { |
| 1537 | if (getcwd(dir, 1024) != NULL) { |
| 1538 | dir[1023] = 0; |
Daniel Veillard | 6454aec | 1999-09-02 22:04:43 +0000 | [diff] [blame] | 1539 | ret = xmlMemStrdup(dir); |
Daniel Veillard | b05deb7 | 1999-08-10 19:04:08 +0000 | [diff] [blame] | 1540 | } |
| 1541 | } |
| 1542 | return(ret); |
| 1543 | } |
| 1544 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1545 | /**************************************************************** |
| 1546 | * * |
| 1547 | * External entities loading * |
| 1548 | * * |
| 1549 | ****************************************************************/ |
| 1550 | |
| 1551 | /* |
| 1552 | * xmlDefaultExternalEntityLoader: |
| 1553 | * @URL: the URL for the entity to load |
| 1554 | * @ID: the System ID for the entity to load |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1555 | * @ctxt: the context in which the entity is called or NULL |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1556 | * |
| 1557 | * By default we don't load external entitites, yet. |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1558 | * |
| 1559 | * Returns a new allocated xmlParserInputPtr, or NULL. |
| 1560 | */ |
| 1561 | static |
| 1562 | xmlParserInputPtr |
| 1563 | xmlDefaultExternalEntityLoader(const char *URL, const char *ID, |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1564 | xmlParserCtxtPtr ctxt) { |
| 1565 | xmlParserInputPtr ret = NULL; |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1566 | |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1567 | #ifdef DEBUG_EXTERNAL_ENTITIES |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 1568 | xmlGenericError(xmlGenericErrorContext, |
| 1569 | "xmlDefaultExternalEntityLoader(%s, xxx)\n", URL); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1570 | #endif |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1571 | if (URL == NULL) { |
| 1572 | if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 1573 | ctxt->sax->warning(ctxt, |
| 1574 | "failed to load external entity \"%s\"\n", ID); |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1575 | return(NULL); |
| 1576 | } |
| 1577 | ret = xmlNewInputFromFile(ctxt, URL); |
| 1578 | if (ret == NULL) { |
| 1579 | if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame^] | 1580 | ctxt->sax->warning(ctxt, |
| 1581 | "failed to load external entity \"%s\"\n", URL); |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1582 | } |
| 1583 | return(ret); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
| 1586 | static xmlExternalEntityLoader xmlCurrentExternalEntityLoader = |
| 1587 | xmlDefaultExternalEntityLoader; |
| 1588 | |
| 1589 | /* |
| 1590 | * xmlSetExternalEntityLoader: |
| 1591 | * @f: the new entity resolver function |
| 1592 | * |
| 1593 | * Changes the defaultexternal entity resolver function for the application |
| 1594 | */ |
| 1595 | void |
| 1596 | xmlSetExternalEntityLoader(xmlExternalEntityLoader f) { |
| 1597 | xmlCurrentExternalEntityLoader = f; |
| 1598 | } |
| 1599 | |
| 1600 | /* |
| 1601 | * xmlGetExternalEntityLoader: |
| 1602 | * |
| 1603 | * Get the default external entity resolver function for the application |
| 1604 | * |
| 1605 | * Returns the xmlExternalEntityLoader function pointer |
| 1606 | */ |
| 1607 | xmlExternalEntityLoader |
| 1608 | xmlGetExternalEntityLoader(void) { |
| 1609 | return(xmlCurrentExternalEntityLoader); |
| 1610 | } |
| 1611 | |
| 1612 | /* |
| 1613 | * xmlLoadExternalEntity: |
| 1614 | * @URL: the URL for the entity to load |
| 1615 | * @ID: the System ID for the entity to load |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1616 | * @ctxt: the context in which the entity is called or NULL |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1617 | * |
| 1618 | * Load an external entity, note that the use of this function for |
| 1619 | * unparsed entities may generate problems |
| 1620 | * TODO: a more generic External entitiy API must be designed |
| 1621 | * |
| 1622 | * Returns the xmlParserInputPtr or NULL |
| 1623 | */ |
| 1624 | xmlParserInputPtr |
| 1625 | xmlLoadExternalEntity(const char *URL, const char *ID, |
Daniel Veillard | 686d6b6 | 2000-01-03 11:08:02 +0000 | [diff] [blame] | 1626 | xmlParserCtxtPtr ctxt) { |
| 1627 | return(xmlCurrentExternalEntityLoader(URL, ID, ctxt)); |
Daniel Veillard | b96e643 | 1999-08-29 21:02:19 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |