blob: 8bd9f9ca180dd0a739bc39a558d172af8f1bf06e [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
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 Veillard344cee72001-08-20 00:08:40 +00006 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +00007 *
8 * 14 Nov 2000 ht - for VMS, truncated name of long functions to under 32 char
9 */
10
Daniel Veillard34ce8be2002-03-18 19:37:11 +000011#define IN_LIBXML
Bjorn Reese70a9da52001-04-21 16:57:29 +000012#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000013
Owen Taylor3473f882001-02-23 17:55:21 +000014#include <string.h>
Daniel Veillard92727042002-09-17 17:59:20 +000015#ifdef HAVE_ERRNO_H
Owen Taylor3473f882001-02-23 17:55:21 +000016#include <errno.h>
Daniel Veillard92727042002-09-17 17:59:20 +000017#endif
18
Owen Taylor3473f882001-02-23 17:55:21 +000019
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 Veillard50f34372001-08-03 12:06:36 +000042 /* 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 Taylor3473f882001-02-23 17:55:21 +000047# 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 Veillard388236f2001-07-08 18:35:48 +000073#include <libxml/uri.h>
Owen Taylor3473f882001-02-23 17:55:21 +000074#include <libxml/nanohttp.h>
75#include <libxml/nanoftp.h>
76#include <libxml/xmlerror.h>
Daniel Veillard7d6fd212001-05-10 15:34:11 +000077#ifdef LIBXML_CATALOG_ENABLED
78#include <libxml/catalog.h>
79#endif
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000080#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000081
Daniel Veillardf012a642001-07-23 19:10:52 +000082/* #define VERBOSE_FAILURE */
Daniel Veillard1fd36d22001-07-04 22:54:28 +000083/* #define DEBUG_EXTERNAL_ENTITIES */
Owen Taylor3473f882001-02-23 17:55:21 +000084/* #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 */
95typedef 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 Veillard22090732001-07-16 00:06:07 +0000104static xmlInputCallback xmlInputCallbackTable[MAX_INPUT_CALLBACK];
105static int xmlInputCallbackNr = 0;
106static int xmlInputCallbackInitialized = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000107
108/*
109 * Output I/O callback sets
110 */
111typedef 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 Veillard22090732001-07-16 00:06:07 +0000120static xmlOutputCallback xmlOutputCallbackTable[MAX_OUTPUT_CALLBACK];
121static int xmlOutputCallbackNr = 0;
122static int xmlOutputCallbackInitialized = 0;
Owen Taylor3473f882001-02-23 17:55:21 +0000123
Daniel Veillardf4862f02002-09-10 11:13:43 +0000124/************************************************************************
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 Veillard01c13b52002-12-10 15:19:08 +0000138 * xmlNormalizeWindowsPath:
Daniel Veillardf4862f02002-09-10 11:13:43 +0000139 * @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 */
146xmlChar *
147xmlNormalizeWindowsPath(const xmlChar *path)
148{
Igor Zlatkovic043620e2002-10-04 13:32:07 +0000149 int len, i = 0, j;
Daniel Veillardf4862f02002-09-10 11:13:43 +0000150 xmlChar *ret;
151
152 if (path == NULL)
153 return(NULL);
Daniel Veillardf4862f02002-09-10 11:13:43 +0000154
155 len = xmlStrlen(path);
Igor Zlatkovic043620e2002-10-04 13:32:07 +0000156 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 Veillardf4862f02002-09-10 11:13:43 +0000175
Igor Zlatkovic043620e2002-10-04 13:32:07 +0000176 while (i < len) {
Daniel Veillardf4862f02002-09-10 11:13:43 +0000177 /* TODO: UTF8 conversion + URI escaping ??? */
178 if (path[i] == '\\')
179 ret[j] = '/';
180 else
181 ret[j] = path[i];
Igor Zlatkovic043620e2002-10-04 13:32:07 +0000182 i++;
183 j++;
Daniel Veillardf4862f02002-09-10 11:13:43 +0000184 }
185 ret[j] = 0;
Igor Zlatkovic043620e2002-10-04 13:32:07 +0000186
Daniel Veillardf4862f02002-09-10 11:13:43 +0000187 return(ret);
188}
189
Daniel Veillardacf7ff02001-10-29 20:21:47 +0000190/**
191 * xmlCleanupInputCallbacks:
192 *
193 * clears the entire input callback table. this includes the
194 * compiled-in I/O.
195 */
196void
197xmlCleanupInputCallbacks(void)
198{
199 int i;
200
201 if (!xmlInputCallbackInitialized)
202 return;
203
Daniel Veillard107ccaa2001-11-27 16:23:50 +0000204 for (i = xmlInputCallbackNr - 1; i >= 0; i--) {
Daniel Veillardacf7ff02001-10-29 20:21:47 +0000205 xmlInputCallbackTable[i].matchcallback = NULL;
206 xmlInputCallbackTable[i].opencallback = NULL;
207 xmlInputCallbackTable[i].readcallback = NULL;
208 xmlInputCallbackTable[i].closecallback = NULL;
209 }
Daniel Veillard9e412302002-06-10 15:59:44 +0000210 xmlInputCallbackInitialized = 0;
Daniel Veillardacf7ff02001-10-29 20:21:47 +0000211
212 xmlInputCallbackNr = 0;
Aleksey Sanin9c45ba82002-06-06 21:46:13 +0000213 xmlInputCallbackInitialized = 0;
Daniel Veillardacf7ff02001-10-29 20:21:47 +0000214}
215
216/**
217 * xmlCleanupOutputCallbacks:
218 *
219 * clears the entire output callback table. this includes the
220 * compiled-in I/O callbacks.
221 */
222void
223xmlCleanupOutputCallbacks(void)
224{
225 int i;
226
227 if (!xmlOutputCallbackInitialized)
228 return;
229
Daniel Veillard107ccaa2001-11-27 16:23:50 +0000230 for (i = xmlOutputCallbackNr - 1; i >= 0; i--) {
Daniel Veillardacf7ff02001-10-29 20:21:47 +0000231 xmlOutputCallbackTable[i].matchcallback = NULL;
232 xmlOutputCallbackTable[i].opencallback = NULL;
233 xmlOutputCallbackTable[i].writecallback = NULL;
234 xmlOutputCallbackTable[i].closecallback = NULL;
235 }
Daniel Veillard9e412302002-06-10 15:59:44 +0000236 xmlOutputCallbackInitialized = 0;
Daniel Veillardacf7ff02001-10-29 20:21:47 +0000237
238 xmlOutputCallbackNr = 0;
Aleksey Sanin9c45ba82002-06-06 21:46:13 +0000239 xmlOutputCallbackInitialized = 0;
Daniel Veillardacf7ff02001-10-29 20:21:47 +0000240}
241
Owen Taylor3473f882001-02-23 17:55:21 +0000242/************************************************************************
243 * *
244 * Standard I/O for file accesses *
245 * *
246 ************************************************************************/
247
248/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +0000249 * xmlCheckFilename:
Owen Taylor3473f882001-02-23 17:55:21 +0000250 * @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 Veillard819d5cb2002-10-14 11:15:18 +0000259 * returns 2. otherwise returns 1.
Owen Taylor3473f882001-02-23 17:55:21 +0000260 */
261
Daniel Veillard819d5cb2002-10-14 11:15:18 +0000262int
Owen Taylor3473f882001-02-23 17:55:21 +0000263xmlCheckFilename (const char *path)
264{
265#ifdef HAVE_STAT
Owen Taylor3473f882001-02-23 17:55:21 +0000266 struct stat stat_buffer;
267
268 if (stat(path, &stat_buffer) == -1)
269 return 0;
270
Daniel Veillard819d5cb2002-10-14 11:15:18 +0000271#ifdef S_ISDIR
Owen Taylor3473f882001-02-23 17:55:21 +0000272 if (S_ISDIR(stat_buffer.st_mode)) {
Daniel Veillard819d5cb2002-10-14 11:15:18 +0000273 return 2;
Owen Taylor3473f882001-02-23 17:55:21 +0000274 }
Owen Taylor3473f882001-02-23 17:55:21 +0000275#endif
276#endif
277 return 1;
278}
279
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000280static int
Owen Taylor3473f882001-02-23 17:55:21 +0000281xmlNop(void) {
282 return(0);
283}
284
285/**
Owen Taylor3473f882001-02-23 17:55:21 +0000286 * 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 Veillard56a4cb82001-03-24 17:00:36 +0000295static int
Owen Taylor3473f882001-02-23 17:55:21 +0000296xmlFdRead (void * context, char * buffer, int len) {
Daniel Veillard8fcc4942001-07-17 20:07:33 +0000297 return(read((int) (long) context, &buffer[0], len));
Owen Taylor3473f882001-02-23 17:55:21 +0000298}
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 Veillard56a4cb82001-03-24 17:00:36 +0000310static int
Owen Taylor3473f882001-02-23 17:55:21 +0000311xmlFdWrite (void * context, const char * buffer, int len) {
Daniel Veillard8fcc4942001-07-17 20:07:33 +0000312 return(write((int) (long) context, &buffer[0], len));
Owen Taylor3473f882001-02-23 17:55:21 +0000313}
314
315/**
316 * xmlFdClose:
317 * @context: the I/O context
318 *
319 * Close an I/O channel
Daniel Veillardf012a642001-07-23 19:10:52 +0000320 *
321 * Returns 0 in case of success and error code otherwise
Owen Taylor3473f882001-02-23 17:55:21 +0000322 */
Daniel Veillardf012a642001-07-23 19:10:52 +0000323static int
Owen Taylor3473f882001-02-23 17:55:21 +0000324xmlFdClose (void * context) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000325 return ( close((int) (long) context) );
Owen Taylor3473f882001-02-23 17:55:21 +0000326}
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 Sanin5aac8b82002-05-01 18:32:28 +0000336int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +0000337xmlFileMatch (const char *filename ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +0000338 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 Sanin5aac8b82002-05-01 18:32:28 +0000350void *
Owen Taylor3473f882001-02-23 17:55:21 +0000351xmlFileOpen (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 Veillardf4862f02002-09-10 11:13:43 +0000360 if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17))
Daniel Veillardb212bbb2002-08-25 14:39:16 +0000361#if defined (_WIN32) && !defined(__CYGWIN__)
362 path = &filename[17];
363#else
Owen Taylor3473f882001-02-23 17:55:21 +0000364 path = &filename[16];
Daniel Veillardb212bbb2002-08-25 14:39:16 +0000365#endif
Aleksey Sanin5aac8b82002-05-01 18:32:28 +0000366 else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) {
Daniel Veillard3c5ed912002-01-08 10:36:16 +0000367#if defined (_WIN32) && !defined(__CYGWIN__)
Daniel Veillardfe703322001-08-14 12:18:09 +0000368 path = &filename[8];
369#else
Daniel Veillard3c2758d2001-05-31 18:43:43 +0000370 path = &filename[7];
Daniel Veillardfe703322001-08-14 12:18:09 +0000371#endif
372 } else
Owen Taylor3473f882001-02-23 17:55:21 +0000373 path = filename;
374
375 if (path == NULL)
376 return(NULL);
377 if (!xmlCheckFilename(path))
378 return(NULL);
379
Daniel Veillard3c5ed912002-01-08 10:36:16 +0000380#if defined(WIN32) || defined (__CYGWIN__)
Owen Taylor3473f882001-02-23 17:55:21 +0000381 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 Veillard56a4cb82001-03-24 17:00:36 +0000397static void *
Owen Taylor3473f882001-02-23 17:55:21 +0000398xmlFileOpenW (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 Veillardf4862f02002-09-10 11:13:43 +0000407 if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17))
408#if defined (_WIN32) && !defined(__CYGWIN__)
409 path = &filename[17];
410#else
Owen Taylor3473f882001-02-23 17:55:21 +0000411 path = &filename[16];
Daniel Veillardf4862f02002-09-10 11:13:43 +0000412#endif
Aleksey Sanin5aac8b82002-05-01 18:32:28 +0000413 else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) {
Daniel Veillard3c5ed912002-01-08 10:36:16 +0000414#if defined (_WIN32) && !defined(__CYGWIN__)
Daniel Veillardfe703322001-08-14 12:18:09 +0000415 path = &filename[8];
416#else
Daniel Veillard3c2758d2001-05-31 18:43:43 +0000417 path = &filename[7];
Daniel Veillardfe703322001-08-14 12:18:09 +0000418#endif
419 } else
Owen Taylor3473f882001-02-23 17:55:21 +0000420 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 Sanin5aac8b82002-05-01 18:32:28 +0000439int
Owen Taylor3473f882001-02-23 17:55:21 +0000440xmlFileRead (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 Veillard56a4cb82001-03-24 17:00:36 +0000454static int
Owen Taylor3473f882001-02-23 17:55:21 +0000455xmlFileWrite (void * context, const char * buffer, int len) {
456 return(fwrite(&buffer[0], 1, len, (FILE *) context));
457}
458
459/**
460 * xmlFileClose:
461 * @context: the I/O context
462 *
463 * Close an I/O channel
Daniel Veillarda9b66d02002-12-11 14:23:49 +0000464 *
465 * Returns 0 or -1 in case of error
Owen Taylor3473f882001-02-23 17:55:21 +0000466 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +0000467int
Owen Taylor3473f882001-02-23 17:55:21 +0000468xmlFileClose (void * context) {
Daniel Veillarda9e65e82001-10-30 10:32:36 +0000469 FILE *fil;
470
471 fil = (FILE *) context;
472 if (fil == stdin)
473 return(0);
474 if (fil == stdout)
475 return(0);
Daniel Veillardcd337f02001-11-22 18:20:37 +0000476 if (fil == stderr)
477 return(0);
Daniel Veillardf012a642001-07-23 19:10:52 +0000478 return ( ( fclose((FILE *) context) == EOF ) ? -1 : 0 );
Owen Taylor3473f882001-02-23 17:55:21 +0000479}
480
481/**
482 * xmlFileFlush:
483 * @context: the I/O context
484 *
485 * Flush an I/O channel
486 */
Daniel Veillardf012a642001-07-23 19:10:52 +0000487static int
Owen Taylor3473f882001-02-23 17:55:21 +0000488xmlFileFlush (void * context) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000489 return ( ( fflush((FILE *) context) == EOF ) ? -1 : 0 );
Owen Taylor3473f882001-02-23 17:55:21 +0000490}
491
492#ifdef HAVE_ZLIB_H
493/************************************************************************
494 * *
495 * I/O for compressed file accesses *
496 * *
497 ************************************************************************/
498/**
499 * xmlGzfileMatch:
500 * @filename: the URI for matching
501 *
502 * input from compressed file test
503 *
504 * Returns 1 if matches, 0 otherwise
505 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000506static int
Daniel Veillardc86a4fa2001-03-26 16:28:29 +0000507xmlGzfileMatch (const char *filename ATTRIBUTE_UNUSED) {
Owen Taylor3473f882001-02-23 17:55:21 +0000508 return(1);
509}
510
511/**
512 * xmlGzfileOpen:
513 * @filename: the URI for matching
514 *
515 * input from compressed file open
516 * if @filename is " " then the standard input is used
517 *
518 * Returns an I/O context or NULL in case of error
519 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000520static void *
Owen Taylor3473f882001-02-23 17:55:21 +0000521xmlGzfileOpen (const char *filename) {
522 const char *path = NULL;
523 gzFile fd;
524
525 if (!strcmp(filename, "-")) {
Daniel Veillarda9e65e82001-10-30 10:32:36 +0000526 fd = gzdopen(dup(0), "rb");
Owen Taylor3473f882001-02-23 17:55:21 +0000527 return((void *) fd);
528 }
529
Daniel Veillardf4862f02002-09-10 11:13:43 +0000530 if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17))
531#if defined (_WIN32) && !defined(__CYGWIN__)
532 path = &filename[17];
533#else
Owen Taylor3473f882001-02-23 17:55:21 +0000534 path = &filename[16];
Daniel Veillardf4862f02002-09-10 11:13:43 +0000535#endif
Aleksey Sanin5aac8b82002-05-01 18:32:28 +0000536 else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) {
Daniel Veillard3c5ed912002-01-08 10:36:16 +0000537#if defined (_WIN32) && !defined(__CYGWIN__)
Daniel Veillardfe703322001-08-14 12:18:09 +0000538 path = &filename[8];
539#else
Owen Taylor3473f882001-02-23 17:55:21 +0000540 path = &filename[7];
Daniel Veillardfe703322001-08-14 12:18:09 +0000541#endif
542 } else
Owen Taylor3473f882001-02-23 17:55:21 +0000543 path = filename;
544
545 if (path == NULL)
546 return(NULL);
547 if (!xmlCheckFilename(path))
548 return(NULL);
549
550 fd = gzopen(path, "rb");
551 return((void *) fd);
552}
553
554/**
555 * xmlGzfileOpenW:
556 * @filename: the URI for matching
557 * @compression: the compression factor (0 - 9 included)
558 *
559 * input from compressed file open
560 * if @filename is " " then the standard input is used
561 *
562 * Returns an I/O context or NULL in case of error
563 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000564static void *
Owen Taylor3473f882001-02-23 17:55:21 +0000565xmlGzfileOpenW (const char *filename, int compression) {
566 const char *path = NULL;
567 char mode[15];
568 gzFile fd;
569
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000570 snprintf(mode, sizeof(mode), "wb%d", compression);
Owen Taylor3473f882001-02-23 17:55:21 +0000571 if (!strcmp(filename, "-")) {
Daniel Veillarda9e65e82001-10-30 10:32:36 +0000572 fd = gzdopen(dup(1), mode);
Owen Taylor3473f882001-02-23 17:55:21 +0000573 return((void *) fd);
574 }
575
Daniel Veillardf4862f02002-09-10 11:13:43 +0000576 if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17))
577#if defined (_WIN32) && !defined(__CYGWIN__)
578 path = &filename[17];
579#else
Owen Taylor3473f882001-02-23 17:55:21 +0000580 path = &filename[16];
Daniel Veillardf4862f02002-09-10 11:13:43 +0000581#endif
Aleksey Sanin5aac8b82002-05-01 18:32:28 +0000582 else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) {
Daniel Veillard3c5ed912002-01-08 10:36:16 +0000583#if defined (_WIN32) && !defined(__CYGWIN__)
Daniel Veillardfe703322001-08-14 12:18:09 +0000584 path = &filename[8];
585#else
Daniel Veillard3c2758d2001-05-31 18:43:43 +0000586 path = &filename[7];
Daniel Veillardfe703322001-08-14 12:18:09 +0000587#endif
588 } else
Owen Taylor3473f882001-02-23 17:55:21 +0000589 path = filename;
590
591 if (path == NULL)
592 return(NULL);
593
594 fd = gzopen(path, mode);
595 return((void *) fd);
596}
597
598/**
599 * xmlGzfileRead:
600 * @context: the I/O context
601 * @buffer: where to drop data
602 * @len: number of bytes to write
603 *
604 * Read @len bytes to @buffer from the compressed I/O channel.
605 *
606 * Returns the number of bytes written
607 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000608static int
Owen Taylor3473f882001-02-23 17:55:21 +0000609xmlGzfileRead (void * context, char * buffer, int len) {
610 return(gzread((gzFile) context, &buffer[0], len));
611}
612
613/**
614 * xmlGzfileWrite:
615 * @context: the I/O context
616 * @buffer: where to drop data
617 * @len: number of bytes to write
618 *
619 * Write @len bytes from @buffer to the compressed I/O channel.
620 *
621 * Returns the number of bytes written
622 */
Daniel Veillard56a4cb82001-03-24 17:00:36 +0000623static int
Owen Taylor3473f882001-02-23 17:55:21 +0000624xmlGzfileWrite (void * context, const char * buffer, int len) {
625 return(gzwrite((gzFile) context, (char *) &buffer[0], len));
626}
627
628/**
629 * xmlGzfileClose:
630 * @context: the I/O context
631 *
632 * Close a compressed I/O channel
633 */
Daniel Veillardf012a642001-07-23 19:10:52 +0000634static int
Owen Taylor3473f882001-02-23 17:55:21 +0000635xmlGzfileClose (void * context) {
Daniel Veillardf012a642001-07-23 19:10:52 +0000636 return ( ( gzclose((gzFile) context) == Z_OK ) ? 0 : -1 );
Owen Taylor3473f882001-02-23 17:55:21 +0000637}
638#endif /* HAVE_ZLIB_H */
639
640#ifdef LIBXML_HTTP_ENABLED
641/************************************************************************
642 * *
643 * I/O for HTTP file accesses *
644 * *
645 ************************************************************************/
Daniel Veillardf012a642001-07-23 19:10:52 +0000646
647typedef struct xmlIOHTTPWriteCtxt_
648{
649 int compression;
650
651 char * uri;
652
653 void * doc_buff;
654
655} xmlIOHTTPWriteCtxt, *xmlIOHTTPWriteCtxtPtr;
656
657#ifdef HAVE_ZLIB_H
658
659#define DFLT_WBITS ( -15 )
660#define DFLT_MEM_LVL ( 8 )
661#define GZ_MAGIC1 ( 0x1f )
662#define GZ_MAGIC2 ( 0x8b )
663#define LXML_ZLIB_OS_CODE ( 0x03 )
664#define INIT_HTTP_BUFF_SIZE ( 32768 )
665#define DFLT_ZLIB_RATIO ( 5 )
666
667/*
668** Data structure and functions to work with sending compressed data
669** via HTTP.
670*/
671
672typedef struct xmlZMemBuff_
673{
674 unsigned long size;
675 unsigned long crc;
676
677 unsigned char * zbuff;
678 z_stream zctrl;
679
680} xmlZMemBuff, *xmlZMemBuffPtr;
681
682/**
683 * append_reverse_ulong
684 * @buff: Compressed memory buffer
685 * @data: Unsigned long to append
686 *
687 * Append a unsigned long in reverse byte order to the end of the
688 * memory buffer.
689 */
690static void
691append_reverse_ulong( xmlZMemBuff * buff, unsigned long data ) {
692
693 int idx;
694
695 if ( buff == NULL )
696 return;
697
698 /*
699 ** This is plagiarized from putLong in gzio.c (zlib source) where
700 ** the number "4" is hardcoded. If zlib is ever patched to
701 ** support 64 bit file sizes, this code would need to be patched
702 ** as well.
703 */
704
705 for ( idx = 0; idx < 4; idx++ ) {
706 *buff->zctrl.next_out = ( data & 0xff );
707 data >>= 8;
708 buff->zctrl.next_out++;
709 }
710
711 return;
712}
713
714/**
715 *
716 * xmlFreeZMemBuff
717 * @buff: The memory buffer context to clear
718 *
719 * Release all the resources associated with the compressed memory buffer.
720 */
721static void
722xmlFreeZMemBuff( xmlZMemBuffPtr buff ) {
723
724 int z_err;
725
726 if ( buff == NULL )
727 return;
728
729 xmlFree( buff->zbuff );
730 z_err = deflateEnd( &buff->zctrl );
731#ifdef DEBUG_HTTP
732 if ( z_err != Z_OK )
733 xmlGenericError( xmlGenericErrorContext,
734 "xmlFreeZMemBuff: Error releasing zlib context: %d\n",
735 z_err );
736#endif
737
738 xmlFree( buff );
739 return;
740}
741
742/**
743 * xmlCreateZMemBuff
744 *@compression: Compression value to use
745 *
746 * Create a memory buffer to hold the compressed XML document. The
747 * compressed document in memory will end up being identical to what
748 * would be created if gzopen/gzwrite/gzclose were being used to
749 * write the document to disk. The code for the header/trailer data to
750 * the compression is plagiarized from the zlib source files.
751 */
752static void *
753xmlCreateZMemBuff( int compression ) {
754
755 int z_err;
756 int hdr_lgth;
757 xmlZMemBuffPtr buff = NULL;
758
759 if ( ( compression < 1 ) || ( compression > 9 ) )
760 return ( NULL );
761
762 /* Create the control and data areas */
763
764 buff = xmlMalloc( sizeof( xmlZMemBuff ) );
765 if ( buff == NULL ) {
766 xmlGenericError( xmlGenericErrorContext,
767 "xmlCreateZMemBuff: %s\n",
768 "Failure allocating buffer context." );
769 return ( NULL );
770 }
771
772 (void)memset( buff, 0, sizeof( xmlZMemBuff ) );
773 buff->size = INIT_HTTP_BUFF_SIZE;
774 buff->zbuff = xmlMalloc( buff->size );
775 if ( buff->zbuff == NULL ) {
776 xmlFreeZMemBuff( buff );
777 xmlGenericError( xmlGenericErrorContext,
778 "xmlCreateZMemBuff: %s\n",
779 "Failure allocating data buffer." );
780 return ( NULL );
781 }
782
783 z_err = deflateInit2( &buff->zctrl, compression, Z_DEFLATED,
784 DFLT_WBITS, DFLT_MEM_LVL, Z_DEFAULT_STRATEGY );
785 if ( z_err != Z_OK ) {
786 xmlFreeZMemBuff( buff );
787 buff = NULL;
788 xmlGenericError( xmlGenericErrorContext,
789 "xmlCreateZMemBuff: %s %d\n",
790 "Error initializing compression context. ZLIB error:",
791 z_err );
792 return ( NULL );
793 }
794
795 /* Set the header data. The CRC will be needed for the trailer */
Daniel Veillardf012a642001-07-23 19:10:52 +0000796 buff->crc = crc32( 0L, Z_NULL, 0 );
Aleksey Sanin49cc9752002-06-14 17:07:10 +0000797 hdr_lgth = snprintf( (char *)buff->zbuff, buff->size,
798 "%c%c%c%c%c%c%c%c%c%c",
Daniel Veillardf012a642001-07-23 19:10:52 +0000799 GZ_MAGIC1, GZ_MAGIC2, Z_DEFLATED,
800 0, 0, 0, 0, 0, 0, LXML_ZLIB_OS_CODE );
801 buff->zctrl.next_out = buff->zbuff + hdr_lgth;
802 buff->zctrl.avail_out = buff->size - hdr_lgth;
803
804 return ( buff );
805}
806
807/**
808 * xmlZMemBuffExtend
809 * @buff: Buffer used to compress and consolidate data.
810 * @ext_amt: Number of bytes to extend the buffer.
811 *
812 * Extend the internal buffer used to store the compressed data by the
813 * specified amount.
814 *
815 * Returns 0 on success or -1 on failure to extend the buffer. On failure
816 * the original buffer still exists at the original size.
817 */
818static int
819xmlZMemBuffExtend( xmlZMemBuffPtr buff, size_t ext_amt ) {
820
821 int rc = -1;
822 size_t new_size;
823 size_t cur_used;
824
825 unsigned char * tmp_ptr = NULL;
826
827 if ( buff == NULL )
828 return ( -1 );
829
830 else if ( ext_amt == 0 )
831 return ( 0 );
832
833 cur_used = buff->zctrl.next_out - buff->zbuff;
834 new_size = buff->size + ext_amt;
835
836#ifdef DEBUG_HTTP
837 if ( cur_used > new_size )
838 xmlGenericError( xmlGenericErrorContext,
839 "xmlZMemBuffExtend: %s\n%s %d bytes.\n",
840 "Buffer overwrite detected during compressed memory",
841 "buffer extension. Overflowed by",
842 (cur_used - new_size ) );
843#endif
844
845 tmp_ptr = xmlRealloc( buff->zbuff, new_size );
846 if ( tmp_ptr != NULL ) {
847 rc = 0;
848 buff->size = new_size;
849 buff->zbuff = tmp_ptr;
850 buff->zctrl.next_out = tmp_ptr + cur_used;
851 buff->zctrl.avail_out = new_size - cur_used;
852 }
853 else {
854 xmlGenericError( xmlGenericErrorContext,
855 "xmlZMemBuffExtend: %s %lu bytes.\n",
856 "Allocation failure extending output buffer to",
857 new_size );
858 }
859
860 return ( rc );
861}
862
863/**
864 * xmlZMemBuffAppend
865 * @buff: Buffer used to compress and consolidate data
866 * @src: Uncompressed source content to append to buffer
867 * @len: Length of source data to append to buffer
868 *
869 * Compress and append data to the internal buffer. The data buffer
870 * will be expanded if needed to store the additional data.
871 *
872 * Returns the number of bytes appended to the buffer or -1 on error.
873 */
874static int
875xmlZMemBuffAppend( xmlZMemBuffPtr buff, const char * src, int len ) {
876
877 int z_err;
878 size_t min_accept;
879
880 if ( ( buff == NULL ) || ( src == NULL ) )
881 return ( -1 );
882
883 buff->zctrl.avail_in = len;
884 buff->zctrl.next_in = (unsigned char *)src;
885 while ( buff->zctrl.avail_in > 0 ) {
886 /*
887 ** Extend the buffer prior to deflate call if a reasonable amount
888 ** of output buffer space is not available.
889 */
890 min_accept = buff->zctrl.avail_in / DFLT_ZLIB_RATIO;
891 if ( buff->zctrl.avail_out <= min_accept ) {
892 if ( xmlZMemBuffExtend( buff, buff->size ) == -1 )
893 return ( -1 );
894 }
895
896 z_err = deflate( &buff->zctrl, Z_NO_FLUSH );
897 if ( z_err != Z_OK ) {
898 xmlGenericError( xmlGenericErrorContext,
899 "xmlZMemBuffAppend: %s %d %s - %d",
900 "Compression error while appending",
901 len, "bytes to buffer. ZLIB error", z_err );
902 return ( -1 );
903 }
904 }
905
906 buff->crc = crc32( buff->crc, (unsigned char *)src, len );
907
908 return ( len );
909}
910
911/**
912 * xmlZMemBuffGetContent
913 * @buff: Compressed memory content buffer
914 * @data_ref: Pointer reference to point to compressed content
915 *
916 * Flushes the compression buffers, appends gzip file trailers and
917 * returns the compressed content and length of the compressed data.
918 * NOTE: The gzip trailer code here is plagiarized from zlib source.
919 *
920 * Returns the length of the compressed data or -1 on error.
921 */
922static int
923xmlZMemBuffGetContent( xmlZMemBuffPtr buff, char ** data_ref ) {
924
925 int zlgth = -1;
926 int z_err;
927
928 if ( ( buff == NULL ) || ( data_ref == NULL ) )
929 return ( -1 );
930
931 /* Need to loop until compression output buffers are flushed */
932
933 do
934 {
935 z_err = deflate( &buff->zctrl, Z_FINISH );
936 if ( z_err == Z_OK ) {
937 /* In this case Z_OK means more buffer space needed */
938
939 if ( xmlZMemBuffExtend( buff, buff->size ) == -1 )
940 return ( -1 );
941 }
942 }
943 while ( z_err == Z_OK );
944
945 /* If the compression state is not Z_STREAM_END, some error occurred */
946
947 if ( z_err == Z_STREAM_END ) {
948
949 /* Need to append the gzip data trailer */
950
951 if ( buff->zctrl.avail_out < ( 2 * sizeof( unsigned long ) ) ) {
952 if ( xmlZMemBuffExtend(buff, (2 * sizeof(unsigned long))) == -1 )
953 return ( -1 );
954 }
955
956 /*
957 ** For whatever reason, the CRC and length data are pushed out
958 ** in reverse byte order. So a memcpy can't be used here.
959 */
960
961 append_reverse_ulong( buff, buff->crc );
962 append_reverse_ulong( buff, buff->zctrl.total_in );
963
964 zlgth = buff->zctrl.next_out - buff->zbuff;
965 *data_ref = (char *)buff->zbuff;
966 }
967
968 else
969 xmlGenericError( xmlGenericErrorContext,
970 "xmlZMemBuffGetContent: %s - %d\n",
971 "Error flushing zlib buffers. Error code", z_err );
972
973 return ( zlgth );
974}
975#endif /* HAVE_ZLIB_H */
976
977/**
978 * xmlFreeHTTPWriteCtxt
979 * @ctxt: Context to cleanup
980 *
981 * Free allocated memory and reclaim system resources.
982 *
983 * No return value.
984 */
985static void
986xmlFreeHTTPWriteCtxt( xmlIOHTTPWriteCtxtPtr ctxt )
987{
988 if ( ctxt->uri != NULL )
Daniel Veillard819d5cb2002-10-14 11:15:18 +0000989 xmlFree( ctxt->uri );
Daniel Veillardf012a642001-07-23 19:10:52 +0000990
991 if ( ctxt->doc_buff != NULL ) {
992
993#ifdef HAVE_ZLIB_H
994 if ( ctxt->compression > 0 ) {
995 xmlFreeZMemBuff( ctxt->doc_buff );
996 }
997 else
998#endif
999 {
1000 xmlOutputBufferClose( ctxt->doc_buff );
1001 }
1002 }
1003
Daniel Veillard819d5cb2002-10-14 11:15:18 +00001004 xmlFree( ctxt );
Daniel Veillardf012a642001-07-23 19:10:52 +00001005 return;
1006}
1007
1008
Owen Taylor3473f882001-02-23 17:55:21 +00001009/**
1010 * xmlIOHTTPMatch:
1011 * @filename: the URI for matching
1012 *
1013 * check if the URI matches an HTTP one
1014 *
1015 * Returns 1 if matches, 0 otherwise
1016 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001017int
Owen Taylor3473f882001-02-23 17:55:21 +00001018xmlIOHTTPMatch (const char *filename) {
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001019 if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "http://", 7))
Owen Taylor3473f882001-02-23 17:55:21 +00001020 return(1);
1021 return(0);
1022}
1023
1024/**
1025 * xmlIOHTTPOpen:
1026 * @filename: the URI for matching
1027 *
1028 * open an HTTP I/O channel
1029 *
1030 * Returns an I/O context or NULL in case of error
1031 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001032void *
Owen Taylor3473f882001-02-23 17:55:21 +00001033xmlIOHTTPOpen (const char *filename) {
1034 return(xmlNanoHTTPOpen(filename, NULL));
1035}
1036
1037/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001038 * xmlIOHTTPOpenW:
Daniel Veillardf012a642001-07-23 19:10:52 +00001039 * @post_uri: The destination URI for the document
1040 * @compression: The compression desired for the document.
1041 *
1042 * Open a temporary buffer to collect the document for a subsequent HTTP POST
1043 * request. Non-static as is called from the output buffer creation routine.
1044 *
1045 * Returns an I/O context or NULL in case of error.
1046 */
1047
1048void *
Daniel Veillard572577e2002-01-18 16:23:55 +00001049xmlIOHTTPOpenW(const char *post_uri, int compression)
1050{
Daniel Veillardf012a642001-07-23 19:10:52 +00001051
Daniel Veillard572577e2002-01-18 16:23:55 +00001052 xmlIOHTTPWriteCtxtPtr ctxt = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001053
Daniel Veillard572577e2002-01-18 16:23:55 +00001054 if (post_uri == NULL)
1055 return (NULL);
Daniel Veillardf012a642001-07-23 19:10:52 +00001056
Daniel Veillard572577e2002-01-18 16:23:55 +00001057 ctxt = xmlMalloc(sizeof(xmlIOHTTPWriteCtxt));
1058 if (ctxt == NULL) {
1059 xmlGenericError(xmlGenericErrorContext,
1060 "xmlIOHTTPOpenW: Failed to create output HTTP context.\n");
1061 return (NULL);
Daniel Veillardf012a642001-07-23 19:10:52 +00001062 }
1063
Daniel Veillard572577e2002-01-18 16:23:55 +00001064 (void) memset(ctxt, 0, sizeof(xmlIOHTTPWriteCtxt));
Daniel Veillardf012a642001-07-23 19:10:52 +00001065
Daniel Veillard572577e2002-01-18 16:23:55 +00001066 ctxt->uri = (char *) xmlStrdup((const xmlChar *)post_uri);
1067 if (ctxt->uri == NULL) {
1068 xmlGenericError(xmlGenericErrorContext,
1069 "xmlIOHTTPOpenW: Failed to duplicate destination URI.\n");
1070 xmlFreeHTTPWriteCtxt(ctxt);
1071 return (NULL);
Daniel Veillardf012a642001-07-23 19:10:52 +00001072 }
1073
1074 /*
Daniel Veillard572577e2002-01-18 16:23:55 +00001075 * ** Since the document length is required for an HTTP post,
1076 * ** need to put the document into a buffer. A memory buffer
1077 * ** is being used to avoid pushing the data to disk and back.
1078 */
Daniel Veillardf012a642001-07-23 19:10:52 +00001079
1080#ifdef HAVE_ZLIB_H
Daniel Veillard572577e2002-01-18 16:23:55 +00001081 if ((compression > 0) && (compression <= 9)) {
1082
1083 ctxt->compression = compression;
1084 ctxt->doc_buff = xmlCreateZMemBuff(compression);
1085 } else
Daniel Veillardf012a642001-07-23 19:10:52 +00001086#endif
1087 {
Daniel Veillard572577e2002-01-18 16:23:55 +00001088 /* Any character conversions should have been done before this */
Daniel Veillardf012a642001-07-23 19:10:52 +00001089
Daniel Veillard572577e2002-01-18 16:23:55 +00001090 ctxt->doc_buff = xmlAllocOutputBuffer(NULL);
Daniel Veillardf012a642001-07-23 19:10:52 +00001091 }
1092
Daniel Veillard572577e2002-01-18 16:23:55 +00001093 if (ctxt->doc_buff == NULL) {
1094 xmlFreeHTTPWriteCtxt(ctxt);
1095 ctxt = NULL;
Daniel Veillardf012a642001-07-23 19:10:52 +00001096 }
1097
Daniel Veillard572577e2002-01-18 16:23:55 +00001098 return (ctxt);
Daniel Veillardf012a642001-07-23 19:10:52 +00001099}
1100
1101/**
1102 * xmlIOHTTPDfltOpenW
1103 * @post_uri: The destination URI for this document.
1104 *
1105 * Calls xmlIOHTTPOpenW with no compression to set up for a subsequent
1106 * HTTP post command. This function should generally not be used as
1107 * the open callback is short circuited in xmlOutputBufferCreateFile.
1108 *
1109 * Returns a pointer to the new IO context.
1110 */
1111static void *
1112xmlIOHTTPDfltOpenW( const char * post_uri ) {
1113 return ( xmlIOHTTPOpenW( post_uri, 0 ) );
1114}
1115
1116/**
Owen Taylor3473f882001-02-23 17:55:21 +00001117 * xmlIOHTTPRead:
1118 * @context: the I/O context
1119 * @buffer: where to drop data
1120 * @len: number of bytes to write
1121 *
1122 * Read @len bytes to @buffer from the I/O channel.
1123 *
1124 * Returns the number of bytes written
1125 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001126int
Owen Taylor3473f882001-02-23 17:55:21 +00001127xmlIOHTTPRead(void * context, char * buffer, int len) {
1128 return(xmlNanoHTTPRead(context, &buffer[0], len));
1129}
1130
1131/**
Daniel Veillardf012a642001-07-23 19:10:52 +00001132 * xmlIOHTTPWrite
1133 * @context: previously opened writing context
1134 * @buffer: data to output to temporary buffer
1135 * @len: bytes to output
1136 *
1137 * Collect data from memory buffer into a temporary file for later
1138 * processing.
1139 *
1140 * Returns number of bytes written.
1141 */
1142
1143static int
1144xmlIOHTTPWrite( void * context, const char * buffer, int len ) {
1145
1146 xmlIOHTTPWriteCtxtPtr ctxt = context;
1147
1148 if ( ( ctxt == NULL ) || ( ctxt->doc_buff == NULL ) || ( buffer == NULL ) )
1149 return ( -1 );
1150
1151 if ( len > 0 ) {
1152
1153 /* Use gzwrite or fwrite as previously setup in the open call */
1154
1155#ifdef HAVE_ZLIB_H
1156 if ( ctxt->compression > 0 )
1157 len = xmlZMemBuffAppend( ctxt->doc_buff, buffer, len );
1158
1159 else
1160#endif
1161 len = xmlOutputBufferWrite( ctxt->doc_buff, len, buffer );
1162
1163 if ( len < 0 ) {
1164 xmlGenericError( xmlGenericErrorContext,
1165 "xmlIOHTTPWrite: %s\n%s '%s'.\n",
1166 "Error appending to internal buffer.",
1167 "Error sending document to URI",
1168 ctxt->uri );
1169 }
1170 }
1171
1172 return ( len );
1173}
1174
1175
1176/**
Owen Taylor3473f882001-02-23 17:55:21 +00001177 * xmlIOHTTPClose:
1178 * @context: the I/O context
1179 *
1180 * Close an HTTP I/O channel
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001181 *
1182 * Returns 0
Owen Taylor3473f882001-02-23 17:55:21 +00001183 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001184int
Owen Taylor3473f882001-02-23 17:55:21 +00001185xmlIOHTTPClose (void * context) {
1186 xmlNanoHTTPClose(context);
Daniel Veillardf012a642001-07-23 19:10:52 +00001187 return 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001188}
Daniel Veillardf012a642001-07-23 19:10:52 +00001189
1190/**
1191 * xmlIOHTTCloseWrite
1192 * @context: The I/O context
1193 * @http_mthd: The HTTP method to be used when sending the data
1194 *
1195 * Close the transmit HTTP I/O channel and actually send the data.
1196 */
1197static int
1198xmlIOHTTPCloseWrite( void * context, const char * http_mthd ) {
1199
1200 int close_rc = -1;
1201 int http_rtn = 0;
1202 int content_lgth = 0;
1203 xmlIOHTTPWriteCtxtPtr ctxt = context;
1204
1205 char * http_content = NULL;
1206 char * content_encoding = NULL;
1207 char * content_type = (char *) "text/xml";
1208 void * http_ctxt = NULL;
1209
1210 if ( ( ctxt == NULL ) || ( http_mthd == NULL ) )
1211 return ( -1 );
1212
1213 /* Retrieve the content from the appropriate buffer */
1214
1215#ifdef HAVE_ZLIB_H
1216
1217 if ( ctxt->compression > 0 ) {
1218 content_lgth = xmlZMemBuffGetContent( ctxt->doc_buff, &http_content );
1219 content_encoding = (char *) "Content-Encoding: gzip";
1220 }
1221 else
1222#endif
1223 {
1224 /* Pull the data out of the memory output buffer */
1225
1226 xmlOutputBufferPtr dctxt = ctxt->doc_buff;
1227 http_content = (char *)dctxt->buffer->content;
1228 content_lgth = dctxt->buffer->use;
1229 }
1230
1231 if ( http_content == NULL ) {
1232 xmlGenericError( xmlGenericErrorContext,
1233 "xmlIOHTTPCloseWrite: %s '%s' %s '%s'.\n",
1234 "Error retrieving content.\nUnable to",
1235 http_mthd, "data to URI", ctxt->uri );
1236 }
1237
1238 else {
1239
1240 http_ctxt = xmlNanoHTTPMethod( ctxt->uri, http_mthd, http_content,
1241 &content_type, content_encoding,
1242 content_lgth );
1243
1244 if ( http_ctxt != NULL ) {
1245#ifdef DEBUG_HTTP
1246 /* If testing/debugging - dump reply with request content */
1247
1248 FILE * tst_file = NULL;
1249 char buffer[ 4096 ];
1250 char * dump_name = NULL;
1251 int avail;
1252
1253 xmlGenericError( xmlGenericErrorContext,
1254 "xmlNanoHTTPCloseWrite: HTTP %s to\n%s returned %d.\n",
1255 http_mthd, ctxt->uri,
1256 xmlNanoHTTPReturnCode( http_ctxt ) );
1257
1258 /*
1259 ** Since either content or reply may be gzipped,
1260 ** dump them to separate files instead of the
1261 ** standard error context.
1262 */
1263
1264 dump_name = tempnam( NULL, "lxml" );
1265 if ( dump_name != NULL ) {
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001266 (void)snprintf( buffer, sizeof(buffer), "%s.content", dump_name );
Daniel Veillardf012a642001-07-23 19:10:52 +00001267
1268 tst_file = fopen( buffer, "w" );
1269 if ( tst_file != NULL ) {
1270 xmlGenericError( xmlGenericErrorContext,
1271 "Transmitted content saved in file: %s\n", buffer );
1272
1273 fwrite( http_content, sizeof( char ),
1274 content_lgth, tst_file );
1275 fclose( tst_file );
1276 }
1277
Aleksey Sanin49cc9752002-06-14 17:07:10 +00001278 (void)snprintf( buffer, sizeof(buffer), "%s.reply", dump_name );
Daniel Veillardf012a642001-07-23 19:10:52 +00001279 tst_file = fopen( buffer, "w" );
1280 if ( tst_file != NULL ) {
1281 xmlGenericError( xmlGenericErrorContext,
1282 "Reply content saved in file: %s\n", buffer );
1283
1284
1285 while ( (avail = xmlNanoHTTPRead( http_ctxt,
1286 buffer, sizeof( buffer ) )) > 0 ) {
1287
1288 fwrite( buffer, sizeof( char ), avail, tst_file );
1289 }
1290
1291 fclose( tst_file );
1292 }
1293
1294 free( dump_name );
1295 }
1296#endif /* DEBUG_HTTP */
1297
1298 http_rtn = xmlNanoHTTPReturnCode( http_ctxt );
1299 if ( ( http_rtn >= 200 ) && ( http_rtn < 300 ) )
1300 close_rc = 0;
1301 else
1302 xmlGenericError( xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001303 "xmlIOHTTPCloseWrite: HTTP '%s' of %d %s\n'%s' %s %d\n",
Daniel Veillardf012a642001-07-23 19:10:52 +00001304 http_mthd, content_lgth,
1305 "bytes to URI", ctxt->uri,
1306 "failed. HTTP return code:", http_rtn );
1307
1308 xmlNanoHTTPClose( http_ctxt );
1309 xmlFree( content_type );
1310 }
1311 }
1312
1313 /* Final cleanups */
1314
1315 xmlFreeHTTPWriteCtxt( ctxt );
1316
1317 return ( close_rc );
1318}
1319
1320/**
1321 * xmlIOHTTPClosePut
1322 *
1323 * @context: The I/O context
1324 *
1325 * Close the transmit HTTP I/O channel and actually send data using a PUT
1326 * HTTP method.
1327 */
1328static int
1329xmlIOHTTPClosePut( void * ctxt ) {
1330 return ( xmlIOHTTPCloseWrite( ctxt, "PUT" ) );
1331}
1332
1333
1334/**
1335 * xmlIOHTTPClosePost
1336 *
1337 * @context: The I/O context
1338 *
1339 * Close the transmit HTTP I/O channel and actually send data using a POST
1340 * HTTP method.
1341 */
1342static int
1343xmlIOHTTPClosePost( void * ctxt ) {
1344 return ( xmlIOHTTPCloseWrite( ctxt, "POST" ) );
1345}
1346
Owen Taylor3473f882001-02-23 17:55:21 +00001347#endif /* LIBXML_HTTP_ENABLED */
1348
1349#ifdef LIBXML_FTP_ENABLED
1350/************************************************************************
1351 * *
1352 * I/O for FTP file accesses *
1353 * *
1354 ************************************************************************/
1355/**
1356 * xmlIOFTPMatch:
1357 * @filename: the URI for matching
1358 *
1359 * check if the URI matches an FTP one
1360 *
1361 * Returns 1 if matches, 0 otherwise
1362 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001363int
Owen Taylor3473f882001-02-23 17:55:21 +00001364xmlIOFTPMatch (const char *filename) {
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001365 if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "ftp://", 6))
Owen Taylor3473f882001-02-23 17:55:21 +00001366 return(1);
1367 return(0);
1368}
1369
1370/**
1371 * xmlIOFTPOpen:
1372 * @filename: the URI for matching
1373 *
1374 * open an FTP I/O channel
1375 *
1376 * Returns an I/O context or NULL in case of error
1377 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001378void *
Owen Taylor3473f882001-02-23 17:55:21 +00001379xmlIOFTPOpen (const char *filename) {
1380 return(xmlNanoFTPOpen(filename));
1381}
1382
1383/**
1384 * xmlIOFTPRead:
1385 * @context: the I/O context
1386 * @buffer: where to drop data
1387 * @len: number of bytes to write
1388 *
1389 * Read @len bytes to @buffer from the I/O channel.
1390 *
1391 * Returns the number of bytes written
1392 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001393int
Owen Taylor3473f882001-02-23 17:55:21 +00001394xmlIOFTPRead(void * context, char * buffer, int len) {
1395 return(xmlNanoFTPRead(context, &buffer[0], len));
1396}
1397
1398/**
1399 * xmlIOFTPClose:
1400 * @context: the I/O context
1401 *
1402 * Close an FTP I/O channel
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001403 *
1404 * Returns 0
Owen Taylor3473f882001-02-23 17:55:21 +00001405 */
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00001406int
Owen Taylor3473f882001-02-23 17:55:21 +00001407xmlIOFTPClose (void * context) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001408 return ( xmlNanoFTPClose(context) );
Owen Taylor3473f882001-02-23 17:55:21 +00001409}
1410#endif /* LIBXML_FTP_ENABLED */
1411
1412
1413/**
1414 * xmlRegisterInputCallbacks:
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001415 * @matchFunc: the xmlInputMatchCallback
1416 * @openFunc: the xmlInputOpenCallback
1417 * @readFunc: the xmlInputReadCallback
1418 * @closeFunc: the xmlInputCloseCallback
Owen Taylor3473f882001-02-23 17:55:21 +00001419 *
1420 * Register a new set of I/O callback for handling parser input.
1421 *
1422 * Returns the registered handler number or -1 in case of error
1423 */
1424int
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001425xmlRegisterInputCallbacks(xmlInputMatchCallback matchFunc,
1426 xmlInputOpenCallback openFunc, xmlInputReadCallback readFunc,
1427 xmlInputCloseCallback closeFunc) {
Owen Taylor3473f882001-02-23 17:55:21 +00001428 if (xmlInputCallbackNr >= MAX_INPUT_CALLBACK) {
1429 return(-1);
1430 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001431 xmlInputCallbackTable[xmlInputCallbackNr].matchcallback = matchFunc;
1432 xmlInputCallbackTable[xmlInputCallbackNr].opencallback = openFunc;
1433 xmlInputCallbackTable[xmlInputCallbackNr].readcallback = readFunc;
1434 xmlInputCallbackTable[xmlInputCallbackNr].closecallback = closeFunc;
Owen Taylor3473f882001-02-23 17:55:21 +00001435 return(xmlInputCallbackNr++);
1436}
1437
1438/**
1439 * xmlRegisterOutputCallbacks:
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001440 * @matchFunc: the xmlOutputMatchCallback
1441 * @openFunc: the xmlOutputOpenCallback
1442 * @writeFunc: the xmlOutputWriteCallback
1443 * @closeFunc: the xmlOutputCloseCallback
Owen Taylor3473f882001-02-23 17:55:21 +00001444 *
1445 * Register a new set of I/O callback for handling output.
1446 *
1447 * Returns the registered handler number or -1 in case of error
1448 */
1449int
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001450xmlRegisterOutputCallbacks(xmlOutputMatchCallback matchFunc,
1451 xmlOutputOpenCallback openFunc, xmlOutputWriteCallback writeFunc,
1452 xmlOutputCloseCallback closeFunc) {
Owen Taylor3473f882001-02-23 17:55:21 +00001453 if (xmlOutputCallbackNr >= MAX_INPUT_CALLBACK) {
1454 return(-1);
1455 }
Daniel Veillard56a4cb82001-03-24 17:00:36 +00001456 xmlOutputCallbackTable[xmlOutputCallbackNr].matchcallback = matchFunc;
1457 xmlOutputCallbackTable[xmlOutputCallbackNr].opencallback = openFunc;
1458 xmlOutputCallbackTable[xmlOutputCallbackNr].writecallback = writeFunc;
1459 xmlOutputCallbackTable[xmlOutputCallbackNr].closecallback = closeFunc;
Owen Taylor3473f882001-02-23 17:55:21 +00001460 return(xmlOutputCallbackNr++);
1461}
1462
1463/**
1464 * xmlRegisterDefaultInputCallbacks:
1465 *
1466 * Registers the default compiled-in I/O handlers.
1467 */
1468void
Owen Taylor3473f882001-02-23 17:55:21 +00001469xmlRegisterDefaultInputCallbacks
Owen Taylor3473f882001-02-23 17:55:21 +00001470(void) {
1471 if (xmlInputCallbackInitialized)
1472 return;
1473
1474 xmlRegisterInputCallbacks(xmlFileMatch, xmlFileOpen,
1475 xmlFileRead, xmlFileClose);
1476#ifdef HAVE_ZLIB_H
1477 xmlRegisterInputCallbacks(xmlGzfileMatch, xmlGzfileOpen,
1478 xmlGzfileRead, xmlGzfileClose);
1479#endif /* HAVE_ZLIB_H */
1480
1481#ifdef LIBXML_HTTP_ENABLED
1482 xmlRegisterInputCallbacks(xmlIOHTTPMatch, xmlIOHTTPOpen,
1483 xmlIOHTTPRead, xmlIOHTTPClose);
1484#endif /* LIBXML_HTTP_ENABLED */
1485
1486#ifdef LIBXML_FTP_ENABLED
1487 xmlRegisterInputCallbacks(xmlIOFTPMatch, xmlIOFTPOpen,
1488 xmlIOFTPRead, xmlIOFTPClose);
1489#endif /* LIBXML_FTP_ENABLED */
1490 xmlInputCallbackInitialized = 1;
1491}
1492
1493/**
1494 * xmlRegisterDefaultOutputCallbacks:
1495 *
1496 * Registers the default compiled-in I/O handlers.
1497 */
1498void
Owen Taylor3473f882001-02-23 17:55:21 +00001499xmlRegisterDefaultOutputCallbacks
Owen Taylor3473f882001-02-23 17:55:21 +00001500(void) {
1501 if (xmlOutputCallbackInitialized)
1502 return;
1503
1504 xmlRegisterOutputCallbacks(xmlFileMatch, xmlFileOpenW,
1505 xmlFileWrite, xmlFileClose);
Daniel Veillardf012a642001-07-23 19:10:52 +00001506
1507#ifdef LIBXML_HTTP_ENABLED
1508 xmlRegisterOutputCallbacks(xmlIOHTTPMatch, xmlIOHTTPDfltOpenW,
1509 xmlIOHTTPWrite, xmlIOHTTPClosePut);
1510#endif
1511
Owen Taylor3473f882001-02-23 17:55:21 +00001512/*********************************
1513 No way a-priori to distinguish between gzipped files from
1514 uncompressed ones except opening if existing then closing
1515 and saving with same compression ratio ... a pain.
1516
1517#ifdef HAVE_ZLIB_H
1518 xmlRegisterOutputCallbacks(xmlGzfileMatch, xmlGzfileOpen,
1519 xmlGzfileWrite, xmlGzfileClose);
1520#endif
Owen Taylor3473f882001-02-23 17:55:21 +00001521
1522 Nor FTP PUT ....
1523#ifdef LIBXML_FTP_ENABLED
1524 xmlRegisterOutputCallbacks(xmlIOFTPMatch, xmlIOFTPOpen,
1525 xmlIOFTPWrite, xmlIOFTPClose);
1526#endif
1527 **********************************/
1528 xmlOutputCallbackInitialized = 1;
1529}
1530
Daniel Veillardf012a642001-07-23 19:10:52 +00001531#ifdef LIBXML_HTTP_ENABLED
1532/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00001533 * xmlRegisterHTTPPostCallbacks:
Daniel Veillardf012a642001-07-23 19:10:52 +00001534 *
1535 * By default, libxml submits HTTP output requests using the "PUT" method.
1536 * Calling this method changes the HTTP output method to use the "POST"
1537 * method instead.
1538 *
1539 */
1540void
1541xmlRegisterHTTPPostCallbacks( void ) {
1542
1543 /* Register defaults if not done previously */
1544
1545 if ( xmlOutputCallbackInitialized == 0 )
1546 xmlRegisterDefaultOutputCallbacks( );
1547
1548 xmlRegisterOutputCallbacks(xmlIOHTTPMatch, xmlIOHTTPDfltOpenW,
1549 xmlIOHTTPWrite, xmlIOHTTPClosePost);
1550 return;
1551}
1552#endif
1553
Owen Taylor3473f882001-02-23 17:55:21 +00001554/**
1555 * xmlAllocParserInputBuffer:
1556 * @enc: the charset encoding if known
1557 *
1558 * Create a buffered parser input for progressive parsing
1559 *
1560 * Returns the new parser input or NULL
1561 */
1562xmlParserInputBufferPtr
1563xmlAllocParserInputBuffer(xmlCharEncoding enc) {
1564 xmlParserInputBufferPtr ret;
1565
1566 ret = (xmlParserInputBufferPtr) xmlMalloc(sizeof(xmlParserInputBuffer));
1567 if (ret == NULL) {
1568 xmlGenericError(xmlGenericErrorContext,
1569 "xmlAllocParserInputBuffer : out of memory!\n");
1570 return(NULL);
1571 }
1572 memset(ret, 0, (size_t) sizeof(xmlParserInputBuffer));
1573 ret->buffer = xmlBufferCreate();
1574 if (ret->buffer == NULL) {
1575 xmlFree(ret);
1576 return(NULL);
1577 }
1578 ret->buffer->alloc = XML_BUFFER_ALLOC_DOUBLEIT;
1579 ret->encoder = xmlGetCharEncodingHandler(enc);
1580 if (ret->encoder != NULL)
1581 ret->raw = xmlBufferCreate();
1582 else
1583 ret->raw = NULL;
1584 ret->readcallback = NULL;
1585 ret->closecallback = NULL;
1586 ret->context = NULL;
1587
1588 return(ret);
1589}
1590
1591/**
1592 * xmlAllocOutputBuffer:
1593 * @encoder: the encoding converter or NULL
1594 *
1595 * Create a buffered parser output
1596 *
1597 * Returns the new parser output or NULL
1598 */
1599xmlOutputBufferPtr
1600xmlAllocOutputBuffer(xmlCharEncodingHandlerPtr encoder) {
1601 xmlOutputBufferPtr ret;
1602
1603 ret = (xmlOutputBufferPtr) xmlMalloc(sizeof(xmlOutputBuffer));
1604 if (ret == NULL) {
1605 xmlGenericError(xmlGenericErrorContext,
1606 "xmlAllocOutputBuffer : out of memory!\n");
1607 return(NULL);
1608 }
1609 memset(ret, 0, (size_t) sizeof(xmlOutputBuffer));
1610 ret->buffer = xmlBufferCreate();
1611 if (ret->buffer == NULL) {
1612 xmlFree(ret);
1613 return(NULL);
1614 }
1615 ret->buffer->alloc = XML_BUFFER_ALLOC_DOUBLEIT;
1616 ret->encoder = encoder;
1617 if (encoder != NULL) {
1618 ret->conv = xmlBufferCreateSize(4000);
1619 /*
1620 * This call is designed to initiate the encoder state
1621 */
1622 xmlCharEncOutFunc(encoder, ret->conv, NULL);
1623 } else
1624 ret->conv = NULL;
1625 ret->writecallback = NULL;
1626 ret->closecallback = NULL;
1627 ret->context = NULL;
1628 ret->written = 0;
1629
1630 return(ret);
1631}
1632
1633/**
1634 * xmlFreeParserInputBuffer:
1635 * @in: a buffered parser input
1636 *
1637 * Free up the memory used by a buffered parser input
1638 */
1639void
1640xmlFreeParserInputBuffer(xmlParserInputBufferPtr in) {
1641 if (in->raw) {
1642 xmlBufferFree(in->raw);
1643 in->raw = NULL;
1644 }
1645 if (in->encoder != NULL) {
1646 xmlCharEncCloseFunc(in->encoder);
1647 }
1648 if (in->closecallback != NULL) {
1649 in->closecallback(in->context);
1650 }
1651 if (in->buffer != NULL) {
1652 xmlBufferFree(in->buffer);
1653 in->buffer = NULL;
1654 }
1655
Owen Taylor3473f882001-02-23 17:55:21 +00001656 xmlFree(in);
1657}
1658
1659/**
1660 * xmlOutputBufferClose:
1661 * @out: a buffered output
1662 *
1663 * flushes and close the output I/O channel
1664 * and free up all the associated resources
1665 *
1666 * Returns the number of byte written or -1 in case of error.
1667 */
1668int
1669xmlOutputBufferClose(xmlOutputBufferPtr out) {
1670 int written;
Daniel Veillardf012a642001-07-23 19:10:52 +00001671 int err_rc = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001672
1673 if (out == NULL)
1674 return(-1);
1675 if (out->writecallback != NULL)
1676 xmlOutputBufferFlush(out);
1677 if (out->closecallback != NULL) {
Daniel Veillardf012a642001-07-23 19:10:52 +00001678 err_rc = out->closecallback(out->context);
Owen Taylor3473f882001-02-23 17:55:21 +00001679 }
1680 written = out->written;
1681 if (out->conv) {
1682 xmlBufferFree(out->conv);
1683 out->conv = NULL;
1684 }
1685 if (out->encoder != NULL) {
1686 xmlCharEncCloseFunc(out->encoder);
1687 }
1688 if (out->buffer != NULL) {
1689 xmlBufferFree(out->buffer);
1690 out->buffer = NULL;
1691 }
1692
Owen Taylor3473f882001-02-23 17:55:21 +00001693 xmlFree(out);
Daniel Veillardf012a642001-07-23 19:10:52 +00001694 return( ( err_rc == 0 ) ? written : err_rc );
Owen Taylor3473f882001-02-23 17:55:21 +00001695}
1696
1697/**
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001698 * xmlParserInputBufferCreateFname:
1699 * @URI: a C string containing the URI or filename
1700 * @enc: the charset encoding if known
1701 *
Daniel Veillard5e2dace2001-07-18 19:30:27 +00001702 * Returns the new parser input or NULL
1703 */
1704/**
Owen Taylor3473f882001-02-23 17:55:21 +00001705 * xmlParserInputBufferCreateFilename:
1706 * @URI: a C string containing the URI or filename
1707 * @enc: the charset encoding if known
1708 *
1709 * Create a buffered parser input for the progressive parsing of a file
1710 * If filename is "-' then we use stdin as the input.
1711 * Automatic support for ZLIB/Compress compressed document is provided
1712 * by default if found at compile-time.
1713 * Do an encoding check if enc == XML_CHAR_ENCODING_NONE
1714 *
1715 * Returns the new parser input or NULL
1716 */
1717xmlParserInputBufferPtr
Owen Taylor3473f882001-02-23 17:55:21 +00001718xmlParserInputBufferCreateFilename
Owen Taylor3473f882001-02-23 17:55:21 +00001719(const char *URI, xmlCharEncoding enc) {
1720 xmlParserInputBufferPtr ret;
Daniel Veillard388236f2001-07-08 18:35:48 +00001721 int i = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001722 void *context = NULL;
Daniel Veillard388236f2001-07-08 18:35:48 +00001723 char *unescaped;
Daniel Veillardf4862f02002-09-10 11:13:43 +00001724 char *normalized;
Owen Taylor3473f882001-02-23 17:55:21 +00001725
1726 if (xmlInputCallbackInitialized == 0)
1727 xmlRegisterDefaultInputCallbacks();
1728
1729 if (URI == NULL) return(NULL);
Daniel Veillardf4862f02002-09-10 11:13:43 +00001730 normalized = (char *) xmlNormalizeWindowsPath((const xmlChar *)URI);
1731 if (normalized == NULL) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001732
Daniel Veillard9f7b84b2001-08-23 15:31:19 +00001733#ifdef LIBXML_CATALOG_ENABLED
1734#endif
1735
Owen Taylor3473f882001-02-23 17:55:21 +00001736 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001737 * Try to find one of the input accept method accepting that scheme
Owen Taylor3473f882001-02-23 17:55:21 +00001738 * Go in reverse to give precedence to user defined handlers.
Daniel Veillard388236f2001-07-08 18:35:48 +00001739 * try with an unescaped version of the URI
Owen Taylor3473f882001-02-23 17:55:21 +00001740 */
Daniel Veillardf4862f02002-09-10 11:13:43 +00001741 unescaped = xmlURIUnescapeString((char *) normalized, 0, NULL);
Daniel Veillard388236f2001-07-08 18:35:48 +00001742 if (unescaped != NULL) {
1743 for (i = xmlInputCallbackNr - 1;i >= 0;i--) {
1744 if ((xmlInputCallbackTable[i].matchcallback != NULL) &&
1745 (xmlInputCallbackTable[i].matchcallback(unescaped) != 0)) {
1746 context = xmlInputCallbackTable[i].opencallback(unescaped);
1747 if (context != NULL)
1748 break;
1749 }
1750 }
1751 xmlFree(unescaped);
1752 }
1753
1754 /*
1755 * If this failed try with a non-escaped URI this may be a strange
1756 * filename
1757 */
1758 if (context == NULL) {
1759 for (i = xmlInputCallbackNr - 1;i >= 0;i--) {
1760 if ((xmlInputCallbackTable[i].matchcallback != NULL) &&
1761 (xmlInputCallbackTable[i].matchcallback(URI) != 0)) {
Daniel Veillardf4862f02002-09-10 11:13:43 +00001762 context = xmlInputCallbackTable[i].opencallback(normalized);
Daniel Veillard388236f2001-07-08 18:35:48 +00001763 if (context != NULL)
1764 break;
1765 }
Owen Taylor3473f882001-02-23 17:55:21 +00001766 }
1767 }
Daniel Veillardf4862f02002-09-10 11:13:43 +00001768 xmlFree(normalized);
Owen Taylor3473f882001-02-23 17:55:21 +00001769 if (context == NULL) {
1770 return(NULL);
1771 }
1772
1773 /*
1774 * Allocate the Input buffer front-end.
1775 */
1776 ret = xmlAllocParserInputBuffer(enc);
1777 if (ret != NULL) {
1778 ret->context = context;
1779 ret->readcallback = xmlInputCallbackTable[i].readcallback;
1780 ret->closecallback = xmlInputCallbackTable[i].closecallback;
1781 }
1782 return(ret);
1783}
1784
1785/**
1786 * xmlOutputBufferCreateFilename:
1787 * @URI: a C string containing the URI or filename
1788 * @encoder: the encoding converter or NULL
1789 * @compression: the compression ration (0 none, 9 max).
1790 *
1791 * Create a buffered output for the progressive saving of a file
1792 * If filename is "-' then we use stdout as the output.
1793 * Automatic support for ZLIB/Compress compressed document is provided
1794 * by default if found at compile-time.
1795 * TODO: currently if compression is set, the library only support
1796 * writing to a local file.
1797 *
1798 * Returns the new output or NULL
1799 */
1800xmlOutputBufferPtr
1801xmlOutputBufferCreateFilename(const char *URI,
1802 xmlCharEncodingHandlerPtr encoder,
1803 int compression) {
1804 xmlOutputBufferPtr ret;
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00001805 int i = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00001806 void *context = NULL;
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00001807 char *unescaped;
Daniel Veillardf4862f02002-09-10 11:13:43 +00001808 char *normalized;
Owen Taylor3473f882001-02-23 17:55:21 +00001809
Daniel Veillardf012a642001-07-23 19:10:52 +00001810 int is_http_uri = 0; /* Can't change if HTTP disabled */
1811
Owen Taylor3473f882001-02-23 17:55:21 +00001812 if (xmlOutputCallbackInitialized == 0)
1813 xmlRegisterDefaultOutputCallbacks();
1814
1815 if (URI == NULL) return(NULL);
Daniel Veillardf4862f02002-09-10 11:13:43 +00001816 normalized = (char *) xmlNormalizeWindowsPath((const xmlChar *)URI);
1817 if (normalized == NULL) return(NULL);
Owen Taylor3473f882001-02-23 17:55:21 +00001818
Daniel Veillardf012a642001-07-23 19:10:52 +00001819#ifdef LIBXML_HTTP_ENABLED
1820 /* Need to prevent HTTP URI's from falling into zlib short circuit */
1821
Daniel Veillardf4862f02002-09-10 11:13:43 +00001822 is_http_uri = xmlIOHTTPMatch( normalized );
Daniel Veillardf012a642001-07-23 19:10:52 +00001823#endif
1824
Owen Taylor3473f882001-02-23 17:55:21 +00001825
1826 /*
Daniel Veillardcbaf3992001-12-31 16:16:02 +00001827 * Try to find one of the output accept method accepting that scheme
Owen Taylor3473f882001-02-23 17:55:21 +00001828 * Go in reverse to give precedence to user defined handlers.
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00001829 * try with an unescaped version of the URI
Owen Taylor3473f882001-02-23 17:55:21 +00001830 */
Daniel Veillardf4862f02002-09-10 11:13:43 +00001831 unescaped = xmlURIUnescapeString(normalized, 0, NULL);
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00001832 if (unescaped != NULL) {
1833#ifdef HAVE_ZLIB_H
1834 if ((compression > 0) && (compression <= 9) && (is_http_uri == 0)) {
1835 context = xmlGzfileOpenW(unescaped, compression);
1836 if (context != NULL) {
1837 ret = xmlAllocOutputBuffer(encoder);
1838 if (ret != NULL) {
1839 ret->context = context;
1840 ret->writecallback = xmlGzfileWrite;
1841 ret->closecallback = xmlGzfileClose;
1842 }
1843 xmlFree(unescaped);
Daniel Veillardf4862f02002-09-10 11:13:43 +00001844 xmlFree(normalized);
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00001845 return(ret);
1846 }
1847 }
Daniel Veillardf012a642001-07-23 19:10:52 +00001848#endif
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00001849 for (i = xmlOutputCallbackNr - 1;i >= 0;i--) {
1850 if ((xmlOutputCallbackTable[i].matchcallback != NULL) &&
1851 (xmlOutputCallbackTable[i].matchcallback(unescaped) != 0)) {
1852#if defined(LIBXML_HTTP_ENABLED) && defined(HAVE_ZLIB_H)
1853 /* Need to pass compression parameter into HTTP open calls */
1854 if (xmlOutputCallbackTable[i].matchcallback == xmlIOHTTPMatch)
1855 context = xmlIOHTTPOpenW(unescaped, compression);
1856 else
1857#endif
1858 context = xmlOutputCallbackTable[i].opencallback(unescaped);
1859 if (context != NULL)
1860 break;
1861 }
1862 }
1863 xmlFree(unescaped);
1864 }
Daniel Veillardf012a642001-07-23 19:10:52 +00001865
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00001866 /*
1867 * If this failed try with a non-escaped URI this may be a strange
1868 * filename
1869 */
1870 if (context == NULL) {
1871#ifdef HAVE_ZLIB_H
1872 if ((compression > 0) && (compression <= 9) && (is_http_uri == 0)) {
Daniel Veillardf4862f02002-09-10 11:13:43 +00001873 context = xmlGzfileOpenW(normalized, compression);
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00001874 if (context != NULL) {
1875 ret = xmlAllocOutputBuffer(encoder);
1876 if (ret != NULL) {
1877 ret->context = context;
1878 ret->writecallback = xmlGzfileWrite;
1879 ret->closecallback = xmlGzfileClose;
1880 }
Daniel Veillardf4862f02002-09-10 11:13:43 +00001881 xmlFree(normalized);
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00001882 return(ret);
1883 }
1884 }
1885#endif
1886 for (i = xmlOutputCallbackNr - 1;i >= 0;i--) {
1887 if ((xmlOutputCallbackTable[i].matchcallback != NULL) &&
Daniel Veillardf4862f02002-09-10 11:13:43 +00001888 (xmlOutputCallbackTable[i].matchcallback(normalized) != 0)) {
Daniel Veillardecb6f5b2001-08-15 08:47:42 +00001889#if defined(LIBXML_HTTP_ENABLED) && defined(HAVE_ZLIB_H)
1890 /* Need to pass compression parameter into HTTP open calls */
1891 if (xmlOutputCallbackTable[i].matchcallback == xmlIOHTTPMatch)
1892 context = xmlIOHTTPOpenW(URI, compression);
1893 else
1894#endif
1895 context = xmlOutputCallbackTable[i].opencallback(URI);
1896 if (context != NULL)
1897 break;
1898 }
Owen Taylor3473f882001-02-23 17:55:21 +00001899 }
1900 }
Daniel Veillardf4862f02002-09-10 11:13:43 +00001901 xmlFree(normalized);
Daniel Veillardf012a642001-07-23 19:10:52 +00001902
Owen Taylor3473f882001-02-23 17:55:21 +00001903 if (context == NULL) {
1904 return(NULL);
1905 }
1906
1907 /*
1908 * Allocate the Output buffer front-end.
1909 */
1910 ret = xmlAllocOutputBuffer(encoder);
1911 if (ret != NULL) {
1912 ret->context = context;
1913 ret->writecallback = xmlOutputCallbackTable[i].writecallback;
1914 ret->closecallback = xmlOutputCallbackTable[i].closecallback;
1915 }
1916 return(ret);
1917}
1918
1919/**
1920 * xmlParserInputBufferCreateFile:
1921 * @file: a FILE*
1922 * @enc: the charset encoding if known
1923 *
1924 * Create a buffered parser input for the progressive parsing of a FILE *
1925 * buffered C I/O
1926 *
1927 * Returns the new parser input or NULL
1928 */
1929xmlParserInputBufferPtr
1930xmlParserInputBufferCreateFile(FILE *file, xmlCharEncoding enc) {
1931 xmlParserInputBufferPtr ret;
1932
1933 if (xmlInputCallbackInitialized == 0)
1934 xmlRegisterDefaultInputCallbacks();
1935
1936 if (file == NULL) return(NULL);
1937
1938 ret = xmlAllocParserInputBuffer(enc);
1939 if (ret != NULL) {
1940 ret->context = file;
1941 ret->readcallback = xmlFileRead;
1942 ret->closecallback = xmlFileFlush;
1943 }
1944
1945 return(ret);
1946}
1947
1948/**
1949 * xmlOutputBufferCreateFile:
1950 * @file: a FILE*
1951 * @encoder: the encoding converter or NULL
1952 *
1953 * Create a buffered output for the progressive saving to a FILE *
1954 * buffered C I/O
1955 *
1956 * Returns the new parser output or NULL
1957 */
1958xmlOutputBufferPtr
1959xmlOutputBufferCreateFile(FILE *file, xmlCharEncodingHandlerPtr encoder) {
1960 xmlOutputBufferPtr ret;
1961
1962 if (xmlOutputCallbackInitialized == 0)
1963 xmlRegisterDefaultOutputCallbacks();
1964
1965 if (file == NULL) return(NULL);
1966
1967 ret = xmlAllocOutputBuffer(encoder);
1968 if (ret != NULL) {
1969 ret->context = file;
1970 ret->writecallback = xmlFileWrite;
1971 ret->closecallback = xmlFileFlush;
1972 }
1973
1974 return(ret);
1975}
1976
1977/**
1978 * xmlParserInputBufferCreateFd:
1979 * @fd: a file descriptor number
1980 * @enc: the charset encoding if known
1981 *
1982 * Create a buffered parser input for the progressive parsing for the input
1983 * from a file descriptor
1984 *
1985 * Returns the new parser input or NULL
1986 */
1987xmlParserInputBufferPtr
1988xmlParserInputBufferCreateFd(int fd, xmlCharEncoding enc) {
1989 xmlParserInputBufferPtr ret;
1990
1991 if (fd < 0) return(NULL);
1992
1993 ret = xmlAllocParserInputBuffer(enc);
1994 if (ret != NULL) {
Daniel Veillard8fcc4942001-07-17 20:07:33 +00001995 ret->context = (void *) (long) fd;
Owen Taylor3473f882001-02-23 17:55:21 +00001996 ret->readcallback = xmlFdRead;
1997 ret->closecallback = xmlFdClose;
1998 }
1999
2000 return(ret);
2001}
2002
2003/**
2004 * xmlParserInputBufferCreateMem:
2005 * @mem: the memory input
2006 * @size: the length of the memory block
2007 * @enc: the charset encoding if known
2008 *
2009 * Create a buffered parser input for the progressive parsing for the input
2010 * from a memory area.
2011 *
2012 * Returns the new parser input or NULL
2013 */
2014xmlParserInputBufferPtr
2015xmlParserInputBufferCreateMem(const char *mem, int size, xmlCharEncoding enc) {
2016 xmlParserInputBufferPtr ret;
2017
2018 if (size <= 0) return(NULL);
2019 if (mem == NULL) return(NULL);
2020
2021 ret = xmlAllocParserInputBuffer(enc);
2022 if (ret != NULL) {
2023 ret->context = (void *) mem;
2024 ret->readcallback = (xmlInputReadCallback) xmlNop;
2025 ret->closecallback = NULL;
2026 xmlBufferAdd(ret->buffer, (const xmlChar *) mem, size);
2027 }
2028
2029 return(ret);
2030}
2031
2032/**
2033 * xmlOutputBufferCreateFd:
2034 * @fd: a file descriptor number
2035 * @encoder: the encoding converter or NULL
2036 *
2037 * Create a buffered output for the progressive saving
2038 * to a file descriptor
2039 *
2040 * Returns the new parser output or NULL
2041 */
2042xmlOutputBufferPtr
2043xmlOutputBufferCreateFd(int fd, xmlCharEncodingHandlerPtr encoder) {
2044 xmlOutputBufferPtr ret;
2045
2046 if (fd < 0) return(NULL);
2047
2048 ret = xmlAllocOutputBuffer(encoder);
2049 if (ret != NULL) {
Daniel Veillard8fcc4942001-07-17 20:07:33 +00002050 ret->context = (void *) (long) fd;
Owen Taylor3473f882001-02-23 17:55:21 +00002051 ret->writecallback = xmlFdWrite;
Daniel Veillard7db38712002-02-07 16:39:11 +00002052 ret->closecallback = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +00002053 }
2054
2055 return(ret);
2056}
2057
2058/**
2059 * xmlParserInputBufferCreateIO:
2060 * @ioread: an I/O read function
2061 * @ioclose: an I/O close function
2062 * @ioctx: an I/O handler
2063 * @enc: the charset encoding if known
2064 *
2065 * Create a buffered parser input for the progressive parsing for the input
2066 * from an I/O handler
2067 *
2068 * Returns the new parser input or NULL
2069 */
2070xmlParserInputBufferPtr
2071xmlParserInputBufferCreateIO(xmlInputReadCallback ioread,
2072 xmlInputCloseCallback ioclose, void *ioctx, xmlCharEncoding enc) {
2073 xmlParserInputBufferPtr ret;
2074
2075 if (ioread == NULL) return(NULL);
2076
2077 ret = xmlAllocParserInputBuffer(enc);
2078 if (ret != NULL) {
2079 ret->context = (void *) ioctx;
2080 ret->readcallback = ioread;
2081 ret->closecallback = ioclose;
2082 }
2083
2084 return(ret);
2085}
2086
2087/**
2088 * xmlOutputBufferCreateIO:
2089 * @iowrite: an I/O write function
2090 * @ioclose: an I/O close function
2091 * @ioctx: an I/O handler
Daniel Veillard9d06d302002-01-22 18:15:52 +00002092 * @encoder: the charset encoding if known
Owen Taylor3473f882001-02-23 17:55:21 +00002093 *
2094 * Create a buffered output for the progressive saving
2095 * to an I/O handler
2096 *
2097 * Returns the new parser output or NULL
2098 */
2099xmlOutputBufferPtr
2100xmlOutputBufferCreateIO(xmlOutputWriteCallback iowrite,
2101 xmlOutputCloseCallback ioclose, void *ioctx,
2102 xmlCharEncodingHandlerPtr encoder) {
2103 xmlOutputBufferPtr ret;
2104
2105 if (iowrite == NULL) return(NULL);
2106
2107 ret = xmlAllocOutputBuffer(encoder);
2108 if (ret != NULL) {
2109 ret->context = (void *) ioctx;
2110 ret->writecallback = iowrite;
2111 ret->closecallback = ioclose;
2112 }
2113
2114 return(ret);
2115}
2116
2117/**
2118 * xmlParserInputBufferPush:
2119 * @in: a buffered parser input
2120 * @len: the size in bytes of the array.
2121 * @buf: an char array
2122 *
2123 * Push the content of the arry in the input buffer
2124 * This routine handle the I18N transcoding to internal UTF-8
2125 * This is used when operating the parser in progressive (push) mode.
2126 *
2127 * Returns the number of chars read and stored in the buffer, or -1
2128 * in case of error.
2129 */
2130int
2131xmlParserInputBufferPush(xmlParserInputBufferPtr in,
2132 int len, const char *buf) {
2133 int nbchars = 0;
2134
2135 if (len < 0) return(0);
2136 if (in->encoder != NULL) {
2137 /*
2138 * Store the data in the incoming raw buffer
2139 */
2140 if (in->raw == NULL) {
2141 in->raw = xmlBufferCreate();
2142 }
2143 xmlBufferAdd(in->raw, (const xmlChar *) buf, len);
2144
2145 /*
2146 * convert as much as possible to the parser reading buffer.
2147 */
2148 nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
2149 if (nbchars < 0) {
2150 xmlGenericError(xmlGenericErrorContext,
2151 "xmlParserInputBufferPush: encoder error\n");
2152 return(-1);
2153 }
2154 } else {
2155 nbchars = len;
2156 xmlBufferAdd(in->buffer, (xmlChar *) buf, nbchars);
2157 }
2158#ifdef DEBUG_INPUT
2159 xmlGenericError(xmlGenericErrorContext,
2160 "I/O: pushed %d chars, buffer %d/%d\n",
2161 nbchars, in->buffer->use, in->buffer->size);
2162#endif
2163 return(nbchars);
2164}
2165
2166/**
Daniel Veillardddffd2a2002-03-05 20:28:20 +00002167 * endOfInput:
2168 *
2169 * When reading from an Input channel indicated end of file or error
2170 * don't reread from it again.
2171 */
2172static int
2173endOfInput (void * context ATTRIBUTE_UNUSED,
2174 char * buffer ATTRIBUTE_UNUSED,
2175 int len ATTRIBUTE_UNUSED) {
2176 return(0);
2177}
2178
2179/**
Owen Taylor3473f882001-02-23 17:55:21 +00002180 * xmlParserInputBufferGrow:
2181 * @in: a buffered parser input
2182 * @len: indicative value of the amount of chars to read
2183 *
2184 * Grow up the content of the input buffer, the old data are preserved
2185 * This routine handle the I18N transcoding to internal UTF-8
2186 * This routine is used when operating the parser in normal (pull) mode
2187 *
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002188 * TODO: one should be able to remove one extra copy by copying directly
Owen Taylor3473f882001-02-23 17:55:21 +00002189 * onto in->buffer or in->raw
2190 *
2191 * Returns the number of chars read and stored in the buffer, or -1
2192 * in case of error.
2193 */
2194int
2195xmlParserInputBufferGrow(xmlParserInputBufferPtr in, int len) {
2196 char *buffer = NULL;
2197 int res = 0;
2198 int nbchars = 0;
2199 int buffree;
Daniel Veillard9e412302002-06-10 15:59:44 +00002200 unsigned int needSize;
Owen Taylor3473f882001-02-23 17:55:21 +00002201
2202 if ((len <= MINLEN) && (len != 4))
2203 len = MINLEN;
2204 buffree = in->buffer->size - in->buffer->use;
2205 if (buffree <= 0) {
2206 xmlGenericError(xmlGenericErrorContext,
2207 "xmlParserInputBufferGrow : buffer full !\n");
2208 return(0);
2209 }
2210 if (len > buffree)
2211 len = buffree;
2212
Daniel Veillarde5354492002-05-16 08:43:22 +00002213 needSize = in->buffer->use + len + 1;
2214 if (needSize > in->buffer->size){
2215 if (!xmlBufferResize(in->buffer, needSize)){
2216 xmlGenericError(xmlGenericErrorContext,
2217 "xmlBufferAdd : out of memory!\n");
2218 return(0);
2219 }
Owen Taylor3473f882001-02-23 17:55:21 +00002220 }
Daniel Veillarde5354492002-05-16 08:43:22 +00002221 buffer = (char *)&in->buffer->content[in->buffer->use];
Owen Taylor3473f882001-02-23 17:55:21 +00002222
2223 /*
2224 * Call the read method for this I/O type.
2225 */
2226 if (in->readcallback != NULL) {
2227 res = in->readcallback(in->context, &buffer[0], len);
Daniel Veillardddffd2a2002-03-05 20:28:20 +00002228 if (res <= 0)
2229 in->readcallback = endOfInput;
Owen Taylor3473f882001-02-23 17:55:21 +00002230 } else {
2231 xmlGenericError(xmlGenericErrorContext,
2232 "xmlParserInputBufferGrow : no input !\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002233 return(-1);
2234 }
2235 if (res < 0) {
Owen Taylor3473f882001-02-23 17:55:21 +00002236 return(-1);
2237 }
2238 len = res;
2239 if (in->encoder != NULL) {
2240 /*
2241 * Store the data in the incoming raw buffer
2242 */
2243 if (in->raw == NULL) {
2244 in->raw = xmlBufferCreate();
2245 }
2246 xmlBufferAdd(in->raw, (const xmlChar *) buffer, len);
2247
2248 /*
2249 * convert as much as possible to the parser reading buffer.
2250 */
2251 nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
2252 if (nbchars < 0) {
2253 xmlGenericError(xmlGenericErrorContext,
2254 "xmlParserInputBufferGrow: encoder error\n");
2255 return(-1);
2256 }
2257 } else {
2258 nbchars = len;
Daniel Veillarde5354492002-05-16 08:43:22 +00002259 in->buffer->use += nbchars;
2260 buffer[nbchars] = 0;
Owen Taylor3473f882001-02-23 17:55:21 +00002261 }
2262#ifdef DEBUG_INPUT
2263 xmlGenericError(xmlGenericErrorContext,
2264 "I/O: read %d chars, buffer %d/%d\n",
2265 nbchars, in->buffer->use, in->buffer->size);
2266#endif
Owen Taylor3473f882001-02-23 17:55:21 +00002267 return(nbchars);
2268}
2269
2270/**
2271 * xmlParserInputBufferRead:
2272 * @in: a buffered parser input
2273 * @len: indicative value of the amount of chars to read
2274 *
2275 * Refresh the content of the input buffer, the old data are considered
2276 * consumed
2277 * This routine handle the I18N transcoding to internal UTF-8
2278 *
2279 * Returns the number of chars read and stored in the buffer, or -1
2280 * in case of error.
2281 */
2282int
2283xmlParserInputBufferRead(xmlParserInputBufferPtr in, int len) {
2284 /* xmlBufferEmpty(in->buffer); */
2285 if (in->readcallback != NULL)
2286 return(xmlParserInputBufferGrow(in, len));
2287 else
2288 return(-1);
2289}
2290
2291/**
2292 * xmlOutputBufferWrite:
2293 * @out: a buffered parser output
2294 * @len: the size in bytes of the array.
2295 * @buf: an char array
2296 *
2297 * Write the content of the array in the output I/O buffer
2298 * This routine handle the I18N transcoding from internal UTF-8
2299 * The buffer is lossless, i.e. will store in case of partial
2300 * or delayed writes.
2301 *
2302 * Returns the number of chars immediately written, or -1
2303 * in case of error.
2304 */
2305int
2306xmlOutputBufferWrite(xmlOutputBufferPtr out, int len, const char *buf) {
2307 int nbchars = 0; /* number of chars to output to I/O */
2308 int ret; /* return from function call */
2309 int written = 0; /* number of char written to I/O so far */
2310 int chunk; /* number of byte curreent processed from buf */
2311
2312 if (len < 0) return(0);
2313
2314 do {
2315 chunk = len;
2316 if (chunk > 4 * MINLEN)
2317 chunk = 4 * MINLEN;
2318
2319 /*
2320 * first handle encoding stuff.
2321 */
2322 if (out->encoder != NULL) {
2323 /*
2324 * Store the data in the incoming raw buffer
2325 */
2326 if (out->conv == NULL) {
2327 out->conv = xmlBufferCreate();
2328 }
2329 xmlBufferAdd(out->buffer, (const xmlChar *) buf, chunk);
2330
2331 if ((out->buffer->use < MINLEN) && (chunk == len))
2332 goto done;
2333
2334 /*
2335 * convert as much as possible to the parser reading buffer.
2336 */
2337 ret = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer);
2338 if (ret < 0) {
2339 xmlGenericError(xmlGenericErrorContext,
2340 "xmlOutputBufferWrite: encoder error\n");
2341 return(-1);
2342 }
2343 nbchars = out->conv->use;
2344 } else {
2345 xmlBufferAdd(out->buffer, (const xmlChar *) buf, chunk);
2346 nbchars = out->buffer->use;
2347 }
2348 buf += chunk;
2349 len -= chunk;
2350
2351 if ((nbchars < MINLEN) && (len <= 0))
2352 goto done;
2353
2354 if (out->writecallback) {
2355 /*
2356 * second write the stuff to the I/O channel
2357 */
2358 if (out->encoder != NULL) {
2359 ret = out->writecallback(out->context,
2360 (const char *)out->conv->content, nbchars);
2361 if (ret >= 0)
Daniel Veillardedddff92001-05-02 10:58:52 +00002362 xmlBufferShrink(out->conv, ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002363 } else {
2364 ret = out->writecallback(out->context,
2365 (const char *)out->buffer->content, nbchars);
2366 if (ret >= 0)
Daniel Veillardedddff92001-05-02 10:58:52 +00002367 xmlBufferShrink(out->buffer, ret);
Owen Taylor3473f882001-02-23 17:55:21 +00002368 }
2369 if (ret < 0) {
2370 xmlGenericError(xmlGenericErrorContext,
2371 "I/O: error %d writing %d bytes\n", ret, nbchars);
2372 return(ret);
2373 }
2374 out->written += ret;
2375 }
2376 written += nbchars;
2377 } while (len > 0);
2378
2379done:
2380#ifdef DEBUG_INPUT
2381 xmlGenericError(xmlGenericErrorContext,
2382 "I/O: wrote %d chars\n", written);
2383#endif
2384 return(written);
2385}
2386
2387/**
2388 * xmlOutputBufferWriteString:
2389 * @out: a buffered parser output
2390 * @str: a zero terminated C string
2391 *
2392 * Write the content of the string in the output I/O buffer
2393 * This routine handle the I18N transcoding from internal UTF-8
2394 * The buffer is lossless, i.e. will store in case of partial
2395 * or delayed writes.
2396 *
2397 * Returns the number of chars immediately written, or -1
2398 * in case of error.
2399 */
2400int
2401xmlOutputBufferWriteString(xmlOutputBufferPtr out, const char *str) {
2402 int len;
2403
2404 if (str == NULL)
2405 return(-1);
2406 len = strlen(str);
2407
2408 if (len > 0)
2409 return(xmlOutputBufferWrite(out, len, str));
2410 return(len);
2411}
2412
2413/**
2414 * xmlOutputBufferFlush:
2415 * @out: a buffered output
2416 *
2417 * flushes the output I/O channel
2418 *
2419 * Returns the number of byte written or -1 in case of error.
2420 */
2421int
2422xmlOutputBufferFlush(xmlOutputBufferPtr out) {
2423 int nbchars = 0, ret = 0;
2424
2425 /*
2426 * first handle encoding stuff.
2427 */
2428 if ((out->conv != NULL) && (out->encoder != NULL)) {
2429 /*
2430 * convert as much as possible to the parser reading buffer.
2431 */
2432 nbchars = xmlCharEncOutFunc(out->encoder, out->conv, out->buffer);
2433 if (nbchars < 0) {
2434 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002435 "xmlOutputBufferFlush: encoder error\n");
Owen Taylor3473f882001-02-23 17:55:21 +00002436 return(-1);
2437 }
2438 }
2439
2440 /*
2441 * second flush the stuff to the I/O channel
2442 */
2443 if ((out->conv != NULL) && (out->encoder != NULL) &&
2444 (out->writecallback != NULL)) {
2445 ret = out->writecallback(out->context,
2446 (const char *)out->conv->content, out->conv->use);
2447 if (ret >= 0)
2448 xmlBufferShrink(out->conv, ret);
2449 } else if (out->writecallback != NULL) {
2450 ret = out->writecallback(out->context,
2451 (const char *)out->buffer->content, out->buffer->use);
2452 if (ret >= 0)
2453 xmlBufferShrink(out->buffer, ret);
2454 }
2455 if (ret < 0) {
2456 xmlGenericError(xmlGenericErrorContext,
2457 "I/O: error %d flushing %d bytes\n", ret, nbchars);
2458 return(ret);
2459 }
2460 out->written += ret;
2461
2462#ifdef DEBUG_INPUT
2463 xmlGenericError(xmlGenericErrorContext,
2464 "I/O: flushed %d chars\n", ret);
2465#endif
2466 return(ret);
2467}
2468
Daniel Veillard5e2dace2001-07-18 19:30:27 +00002469/**
Owen Taylor3473f882001-02-23 17:55:21 +00002470 * xmlParserGetDirectory:
2471 * @filename: the path to a file
2472 *
2473 * lookup the directory for that file
2474 *
2475 * Returns a new allocated string containing the directory, or NULL.
2476 */
2477char *
2478xmlParserGetDirectory(const char *filename) {
2479 char *ret = NULL;
2480 char dir[1024];
2481 char *cur;
2482 char sep = '/';
2483
Igor Zlatkovic9181cc02002-09-29 17:51:06 +00002484#ifdef _WIN32_WCE /* easy way by now ... wince does not have dirs! */
2485 return NULL;
2486#endif
2487
Owen Taylor3473f882001-02-23 17:55:21 +00002488 if (xmlInputCallbackInitialized == 0)
2489 xmlRegisterDefaultInputCallbacks();
2490
2491 if (filename == NULL) return(NULL);
Daniel Veillard3c5ed912002-01-08 10:36:16 +00002492#if defined(WIN32) && !defined(__CYGWIN__)
Owen Taylor3473f882001-02-23 17:55:21 +00002493 sep = '\\';
2494#endif
2495
2496 strncpy(dir, filename, 1023);
2497 dir[1023] = 0;
2498 cur = &dir[strlen(dir)];
2499 while (cur > dir) {
2500 if (*cur == sep) break;
2501 cur --;
2502 }
2503 if (*cur == sep) {
2504 if (cur == dir) dir[1] = 0;
2505 else *cur = 0;
2506 ret = xmlMemStrdup(dir);
2507 } else {
2508 if (getcwd(dir, 1024) != NULL) {
2509 dir[1023] = 0;
2510 ret = xmlMemStrdup(dir);
2511 }
2512 }
2513 return(ret);
2514}
2515
2516/****************************************************************
2517 * *
2518 * External entities loading *
2519 * *
2520 ****************************************************************/
2521
Daniel Veillard561b7f82002-03-20 21:55:57 +00002522#ifdef LIBXML_CATALOG_ENABLED
2523static int xmlSysIDExists(const char *URL) {
Daniel Veillard6990bf32001-08-23 21:17:48 +00002524#ifdef HAVE_STAT
2525 int ret;
2526 struct stat info;
2527 const char *path;
2528
2529 if (URL == NULL)
2530 return(0);
2531
Daniel Veillardf4862f02002-09-10 11:13:43 +00002532 if (!xmlStrncasecmp(BAD_CAST URL, BAD_CAST "file://localhost/", 17))
2533#if defined (_WIN32) && !defined(__CYGWIN__)
2534 path = &URL[17];
2535#else
Daniel Veillard6990bf32001-08-23 21:17:48 +00002536 path = &URL[16];
Daniel Veillardf4862f02002-09-10 11:13:43 +00002537#endif
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00002538 else if (!xmlStrncasecmp(BAD_CAST URL, BAD_CAST "file:///", 8)) {
Daniel Veillard3c5ed912002-01-08 10:36:16 +00002539#if defined (_WIN32) && !defined(__CYGWIN__)
Daniel Veillard6990bf32001-08-23 21:17:48 +00002540 path = &URL[8];
2541#else
2542 path = &URL[7];
2543#endif
2544 } else
2545 path = URL;
2546 ret = stat(path, &info);
Daniel Veillard561b7f82002-03-20 21:55:57 +00002547 if (ret == 0)
2548 return(1);
Daniel Veillard6990bf32001-08-23 21:17:48 +00002549#endif
Daniel Veillard561b7f82002-03-20 21:55:57 +00002550 return(0);
Daniel Veillard6990bf32001-08-23 21:17:48 +00002551}
Daniel Veillard561b7f82002-03-20 21:55:57 +00002552#endif
Daniel Veillard6990bf32001-08-23 21:17:48 +00002553
Daniel Veillard5e2dace2001-07-18 19:30:27 +00002554/**
Owen Taylor3473f882001-02-23 17:55:21 +00002555 * xmlDefaultExternalEntityLoader:
2556 * @URL: the URL for the entity to load
2557 * @ID: the System ID for the entity to load
2558 * @ctxt: the context in which the entity is called or NULL
2559 *
2560 * By default we don't load external entitites, yet.
2561 *
2562 * Returns a new allocated xmlParserInputPtr, or NULL.
2563 */
2564static
2565xmlParserInputPtr
2566xmlDefaultExternalEntityLoader(const char *URL, const char *ID,
2567 xmlParserCtxtPtr ctxt) {
2568 xmlParserInputPtr ret = NULL;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002569 xmlChar *resource = NULL;
2570#ifdef LIBXML_CATALOG_ENABLED
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00002571 xmlCatalogAllow pref;
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002572#endif
Owen Taylor3473f882001-02-23 17:55:21 +00002573
2574#ifdef DEBUG_EXTERNAL_ENTITIES
2575 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardf012a642001-07-23 19:10:52 +00002576 "xmlDefaultExternalEntityLoader(%s, xxx)\n", URL);
Owen Taylor3473f882001-02-23 17:55:21 +00002577#endif
Daniel Veillard7d6fd212001-05-10 15:34:11 +00002578#ifdef LIBXML_CATALOG_ENABLED
2579 /*
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002580 * If the resource doesn't exists as a file,
Daniel Veillarddc2cee22001-08-22 16:30:37 +00002581 * try to load it from the resource pointed in the catalogs
Daniel Veillard7d6fd212001-05-10 15:34:11 +00002582 */
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00002583 pref = xmlCatalogGetDefaults();
2584
Daniel Veillard561b7f82002-03-20 21:55:57 +00002585 if ((pref != XML_CATA_ALLOW_NONE) && (!xmlSysIDExists(URL))) {
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00002586 /*
2587 * Do a local lookup
2588 */
2589 if ((ctxt->catalogs != NULL) &&
2590 ((pref == XML_CATA_ALLOW_ALL) ||
2591 (pref == XML_CATA_ALLOW_DOCUMENT))) {
2592 resource = xmlCatalogLocalResolve(ctxt->catalogs,
2593 (const xmlChar *)ID,
2594 (const xmlChar *)URL);
2595 }
2596 /*
Daniel Veillarddc2cee22001-08-22 16:30:37 +00002597 * Try a global lookup
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00002598 */
Daniel Veillarddc2cee22001-08-22 16:30:37 +00002599 if ((resource == NULL) &&
2600 ((pref == XML_CATA_ALLOW_ALL) ||
2601 (pref == XML_CATA_ALLOW_GLOBAL))) {
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00002602 resource = xmlCatalogResolve((const xmlChar *)ID,
2603 (const xmlChar *)URL);
2604 }
Daniel Veillarddc2cee22001-08-22 16:30:37 +00002605 if ((resource == NULL) && (URL != NULL))
Daniel Veillard9f7b84b2001-08-23 15:31:19 +00002606 resource = xmlStrdup((const xmlChar *) URL);
Daniel Veillarddc2cee22001-08-22 16:30:37 +00002607
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00002608 /*
2609 * TODO: do an URI lookup on the reference
2610 */
Daniel Veillard561b7f82002-03-20 21:55:57 +00002611 if ((resource != NULL) && (!xmlSysIDExists((const char *)resource))) {
Daniel Veillarddc2cee22001-08-22 16:30:37 +00002612 xmlChar *tmp = NULL;
2613
2614 if ((ctxt->catalogs != NULL) &&
2615 ((pref == XML_CATA_ALLOW_ALL) ||
2616 (pref == XML_CATA_ALLOW_DOCUMENT))) {
2617 tmp = xmlCatalogLocalResolveURI(ctxt->catalogs, resource);
2618 }
2619 if ((tmp == NULL) &&
Daniel Veillard9f7b84b2001-08-23 15:31:19 +00002620 ((pref == XML_CATA_ALLOW_ALL) ||
2621 (pref == XML_CATA_ALLOW_GLOBAL))) {
Daniel Veillarddc2cee22001-08-22 16:30:37 +00002622 tmp = xmlCatalogResolveURI(resource);
2623 }
2624
2625 if (tmp != NULL) {
2626 xmlFree(resource);
2627 resource = tmp;
2628 }
2629 }
Daniel Veillard5d90b6c2001-08-22 14:29:45 +00002630 }
Daniel Veillard7d6fd212001-05-10 15:34:11 +00002631#endif
2632
2633 if (resource == NULL)
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002634 resource = (xmlChar *) URL;
Daniel Veillard7d6fd212001-05-10 15:34:11 +00002635
2636 if (resource == NULL) {
Daniel Veillardc6613042002-03-02 09:34:02 +00002637 if (ID == NULL)
2638 ID = "NULL";
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00002639 if ((ctxt->validate) && (ctxt->sax != NULL) &&
2640 (ctxt->sax->error != NULL))
2641 ctxt->sax->error(ctxt,
2642 "failed to load external entity \"%s\"\n", ID);
2643 else if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002644 ctxt->sax->warning(ctxt,
2645 "failed to load external entity \"%s\"\n", ID);
2646 return(NULL);
2647 }
Daniel Veillard7d6fd212001-05-10 15:34:11 +00002648 ret = xmlNewInputFromFile(ctxt, (const char *)resource);
Owen Taylor3473f882001-02-23 17:55:21 +00002649 if (ret == NULL) {
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00002650 if ((ctxt->validate) && (ctxt->sax != NULL) &&
2651 (ctxt->sax->error != NULL))
2652 ctxt->sax->error(ctxt,
Daniel Veillard7d6fd212001-05-10 15:34:11 +00002653 "failed to load external entity \"%s\"\n", resource);
Daniel Veillard34b1b3a2001-04-21 14:16:10 +00002654 else if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
Owen Taylor3473f882001-02-23 17:55:21 +00002655 ctxt->sax->warning(ctxt,
Daniel Veillard7d6fd212001-05-10 15:34:11 +00002656 "failed to load external entity \"%s\"\n", resource);
Owen Taylor3473f882001-02-23 17:55:21 +00002657 }
Daniel Veillarddc2cee22001-08-22 16:30:37 +00002658 if ((resource != NULL) && (resource != (xmlChar *) URL))
Daniel Veillarde2940dd2001-08-22 00:06:49 +00002659 xmlFree(resource);
Owen Taylor3473f882001-02-23 17:55:21 +00002660 return(ret);
2661}
2662
2663static xmlExternalEntityLoader xmlCurrentExternalEntityLoader =
2664 xmlDefaultExternalEntityLoader;
2665
Daniel Veillard5e2dace2001-07-18 19:30:27 +00002666/**
Owen Taylor3473f882001-02-23 17:55:21 +00002667 * xmlSetExternalEntityLoader:
2668 * @f: the new entity resolver function
2669 *
2670 * Changes the defaultexternal entity resolver function for the application
2671 */
2672void
2673xmlSetExternalEntityLoader(xmlExternalEntityLoader f) {
2674 xmlCurrentExternalEntityLoader = f;
2675}
2676
Daniel Veillard5e2dace2001-07-18 19:30:27 +00002677/**
Owen Taylor3473f882001-02-23 17:55:21 +00002678 * xmlGetExternalEntityLoader:
2679 *
2680 * Get the default external entity resolver function for the application
2681 *
2682 * Returns the xmlExternalEntityLoader function pointer
2683 */
2684xmlExternalEntityLoader
2685xmlGetExternalEntityLoader(void) {
2686 return(xmlCurrentExternalEntityLoader);
2687}
2688
Daniel Veillard5e2dace2001-07-18 19:30:27 +00002689/**
Owen Taylor3473f882001-02-23 17:55:21 +00002690 * xmlLoadExternalEntity:
2691 * @URL: the URL for the entity to load
Daniel Veillard9f7b84b2001-08-23 15:31:19 +00002692 * @ID: the Public ID for the entity to load
Owen Taylor3473f882001-02-23 17:55:21 +00002693 * @ctxt: the context in which the entity is called or NULL
2694 *
2695 * Load an external entity, note that the use of this function for
2696 * unparsed entities may generate problems
Daniel Veillardcbaf3992001-12-31 16:16:02 +00002697 * TODO: a more generic External entity API must be designed
Owen Taylor3473f882001-02-23 17:55:21 +00002698 *
2699 * Returns the xmlParserInputPtr or NULL
2700 */
2701xmlParserInputPtr
2702xmlLoadExternalEntity(const char *URL, const char *ID,
2703 xmlParserCtxtPtr ctxt) {
2704 return(xmlCurrentExternalEntityLoader(URL, ID, ctxt));
2705}
2706
Daniel Veillard8bdb91d2001-10-31 17:52:43 +00002707/************************************************************************
2708 * *
2709 * Disabling Network access *
2710 * *
2711 ************************************************************************/
2712
2713#ifdef LIBXML_CATALOG_ENABLED
2714static int
2715xmlNoNetExists(const char *URL)
2716{
2717#ifdef HAVE_STAT
2718 int ret;
2719 struct stat info;
2720 const char *path;
2721
2722 if (URL == NULL)
2723 return (0);
2724
Daniel Veillardf4862f02002-09-10 11:13:43 +00002725 if (!xmlStrncasecmp(BAD_CAST URL, BAD_CAST "file://localhost/", 17))
2726#if defined (_WIN32) && !defined(__CYGWIN__)
2727 path = &URL[17];
2728#else
2729 path = &URL[16];
2730#endif
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00002731 else if (!xmlStrncasecmp(BAD_CAST URL, BAD_CAST "file:///", 8)) {
Daniel Veillard3c5ed912002-01-08 10:36:16 +00002732#if defined (_WIN32) && !defined(__CYGWIN__)
Daniel Veillard8bdb91d2001-10-31 17:52:43 +00002733 path = &URL[8];
2734#else
2735 path = &URL[7];
2736#endif
2737 } else
2738 path = URL;
2739 ret = stat(path, &info);
2740 if (ret == 0)
2741 return (1);
2742#endif
2743 return (0);
2744}
2745#endif
2746
2747/**
2748 * xmlNoNetExternalEntityLoader:
2749 * @URL: the URL for the entity to load
2750 * @ID: the System ID for the entity to load
2751 * @ctxt: the context in which the entity is called or NULL
2752 *
2753 * A specific entity loader disabling network accesses, though still
2754 * allowing local catalog accesses for resolution.
2755 *
2756 * Returns a new allocated xmlParserInputPtr, or NULL.
2757 */
2758xmlParserInputPtr
2759xmlNoNetExternalEntityLoader(const char *URL, const char *ID,
2760 xmlParserCtxtPtr ctxt) {
2761 xmlParserInputPtr input = NULL;
2762 xmlChar *resource = NULL;
2763
2764#ifdef LIBXML_CATALOG_ENABLED
2765 xmlCatalogAllow pref;
2766
2767 /*
2768 * If the resource doesn't exists as a file,
2769 * try to load it from the resource pointed in the catalogs
2770 */
2771 pref = xmlCatalogGetDefaults();
2772
2773 if ((pref != XML_CATA_ALLOW_NONE) && (!xmlNoNetExists(URL))) {
2774 /*
2775 * Do a local lookup
2776 */
2777 if ((ctxt->catalogs != NULL) &&
2778 ((pref == XML_CATA_ALLOW_ALL) ||
2779 (pref == XML_CATA_ALLOW_DOCUMENT))) {
2780 resource = xmlCatalogLocalResolve(ctxt->catalogs,
2781 (const xmlChar *)ID,
2782 (const xmlChar *)URL);
2783 }
2784 /*
2785 * Try a global lookup
2786 */
2787 if ((resource == NULL) &&
2788 ((pref == XML_CATA_ALLOW_ALL) ||
2789 (pref == XML_CATA_ALLOW_GLOBAL))) {
2790 resource = xmlCatalogResolve((const xmlChar *)ID,
2791 (const xmlChar *)URL);
2792 }
2793 if ((resource == NULL) && (URL != NULL))
2794 resource = xmlStrdup((const xmlChar *) URL);
2795
2796 /*
2797 * TODO: do an URI lookup on the reference
2798 */
2799 if ((resource != NULL) && (!xmlNoNetExists((const char *)resource))) {
2800 xmlChar *tmp = NULL;
2801
2802 if ((ctxt->catalogs != NULL) &&
2803 ((pref == XML_CATA_ALLOW_ALL) ||
2804 (pref == XML_CATA_ALLOW_DOCUMENT))) {
2805 tmp = xmlCatalogLocalResolveURI(ctxt->catalogs, resource);
2806 }
2807 if ((tmp == NULL) &&
2808 ((pref == XML_CATA_ALLOW_ALL) ||
2809 (pref == XML_CATA_ALLOW_GLOBAL))) {
2810 tmp = xmlCatalogResolveURI(resource);
2811 }
2812
2813 if (tmp != NULL) {
2814 xmlFree(resource);
2815 resource = tmp;
2816 }
2817 }
2818 }
2819#endif
2820 if (resource == NULL)
2821 resource = (xmlChar *) URL;
2822
2823 if (resource != NULL) {
Aleksey Sanin5aac8b82002-05-01 18:32:28 +00002824 if ((!xmlStrncasecmp(BAD_CAST resource, BAD_CAST "ftp://", 6)) ||
2825 (!xmlStrncasecmp(BAD_CAST resource, BAD_CAST "http://", 7))) {
Daniel Veillard8bdb91d2001-10-31 17:52:43 +00002826 xmlGenericError(xmlGenericErrorContext,
2827 "Attempt to load network entity %s \n", resource);
2828
2829 if (resource != (xmlChar *) URL)
2830 xmlFree(resource);
2831 return(NULL);
2832 }
2833 }
2834 input = xmlDefaultExternalEntityLoader((const char *) resource, ID, ctxt);
2835 if (resource != (xmlChar *) URL)
2836 xmlFree(resource);
2837 return(input);
2838}
2839