-
Notifications
You must be signed in to change notification settings - Fork 25
/
xmlIO.c
3145 lines (2785 loc) · 74.9 KB
/
xmlIO.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* xmlIO.c : implementation of the I/O interfaces used by the parser
*
* See Copyright for the status of this software.
*
* daniel@veillard.com
*/
#define IN_LIBXML
#include "libxml.h"
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_IO_H
#include <io.h>
#endif
#ifdef HAVE_DIRECT_H
#include <direct.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef LIBXML_ZLIB_ENABLED
#include <zlib.h>
#endif
#ifdef LIBXML_LZMA_ENABLED
#include <lzma.h>
#endif
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <io.h>
#include <direct.h>
#endif
#include <libxml/xmlIO.h>
#include <libxml/xmlmemory.h>
#include <libxml/uri.h>
#include <libxml/nanohttp.h>
#include <libxml/nanoftp.h>
#include <libxml/xmlerror.h>
#ifdef LIBXML_CATALOG_ENABLED
#include <libxml/catalog.h>
#endif
#include "private/buf.h"
#include "private/enc.h"
#include "private/error.h"
#include "private/io.h"
#include "private/parser.h"
/* #define VERBOSE_FAILURE */
#define MINLEN 4000
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
#ifndef S_ISDIR
# ifdef _S_ISDIR
# define S_ISDIR(x) _S_ISDIR(x)
# elif defined(S_IFDIR)
# ifdef S_IFMT
# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
# elif defined(_S_IFMT)
# define S_ISDIR(m) (((m) & _S_IFMT) == S_IFDIR)
# endif
# endif
#endif
/*
* Input I/O callback sets
*/
typedef struct _xmlInputCallback {
xmlInputMatchCallback matchcallback;
xmlInputOpenCallback opencallback;
xmlInputReadCallback readcallback;
xmlInputCloseCallback closecallback;
} xmlInputCallback;
/* This dummy function only marks default IO in the callback table */
static int
xmlIODefaultMatch(const char *filename);
#define MAX_INPUT_CALLBACK 10
static xmlInputCallback xmlInputCallbackTable[MAX_INPUT_CALLBACK];
static int xmlInputCallbackNr;
#ifdef LIBXML_OUTPUT_ENABLED
/*
* Output I/O callback sets
*/
typedef struct _xmlOutputCallback {
xmlOutputMatchCallback matchcallback;
xmlOutputOpenCallback opencallback;
xmlOutputWriteCallback writecallback;
xmlOutputCloseCallback closecallback;
} xmlOutputCallback;
#define MAX_OUTPUT_CALLBACK 10
static xmlOutputCallback xmlOutputCallbackTable[MAX_OUTPUT_CALLBACK];
static int xmlOutputCallbackNr;
#endif /* LIBXML_OUTPUT_ENABLED */
/************************************************************************
* *
* Error handling *
* *
************************************************************************/
/**
* xmlIOErrMemory:
* @extra: extra information
*
* Handle an out of memory condition
*/
static void
xmlIOErrMemory(void)
{
xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_IO, NULL);
}
/**
* __xmlIOErr:
* @code: the error number
* @
* @extra: extra information
*
* Handle an I/O error
*/
int
__xmlIOErr(int domain, int code, const char *extra)
{
xmlStructuredErrorFunc schannel = NULL;
xmlGenericErrorFunc channel = NULL;
void *data = NULL;
const char *fmt, *arg1, *arg2;
int res;
if (code == 0) {
if (errno == 0) code = XML_IO_UNKNOWN;
#ifdef EACCES
else if (errno == EACCES) code = XML_IO_EACCES;
#endif
#ifdef EAGAIN
else if (errno == EAGAIN) code = XML_IO_EAGAIN;
#endif
#ifdef EBADF
else if (errno == EBADF) code = XML_IO_EBADF;
#endif
#ifdef EBADMSG
else if (errno == EBADMSG) code = XML_IO_EBADMSG;
#endif
#ifdef EBUSY
else if (errno == EBUSY) code = XML_IO_EBUSY;
#endif
#ifdef ECANCELED
else if (errno == ECANCELED) code = XML_IO_ECANCELED;
#endif
#ifdef ECHILD
else if (errno == ECHILD) code = XML_IO_ECHILD;
#endif
#ifdef EDEADLK
else if (errno == EDEADLK) code = XML_IO_EDEADLK;
#endif
#ifdef EDOM
else if (errno == EDOM) code = XML_IO_EDOM;
#endif
#ifdef EEXIST
else if (errno == EEXIST) code = XML_IO_EEXIST;
#endif
#ifdef EFAULT
else if (errno == EFAULT) code = XML_IO_EFAULT;
#endif
#ifdef EFBIG
else if (errno == EFBIG) code = XML_IO_EFBIG;
#endif
#ifdef EINPROGRESS
else if (errno == EINPROGRESS) code = XML_IO_EINPROGRESS;
#endif
#ifdef EINTR
else if (errno == EINTR) code = XML_IO_EINTR;
#endif
#ifdef EINVAL
else if (errno == EINVAL) code = XML_IO_EINVAL;
#endif
#ifdef EIO
else if (errno == EIO) code = XML_IO_EIO;
#endif
#ifdef EISDIR
else if (errno == EISDIR) code = XML_IO_EISDIR;
#endif
#ifdef EMFILE
else if (errno == EMFILE) code = XML_IO_EMFILE;
#endif
#ifdef EMLINK
else if (errno == EMLINK) code = XML_IO_EMLINK;
#endif
#ifdef EMSGSIZE
else if (errno == EMSGSIZE) code = XML_IO_EMSGSIZE;
#endif
#ifdef ENAMETOOLONG
else if (errno == ENAMETOOLONG) code = XML_IO_ENAMETOOLONG;
#endif
#ifdef ENFILE
else if (errno == ENFILE) code = XML_IO_ENFILE;
#endif
#ifdef ENODEV
else if (errno == ENODEV) code = XML_IO_ENODEV;
#endif
#ifdef ENOENT
else if (errno == ENOENT) code = XML_IO_ENOENT;
#endif
#ifdef ENOEXEC
else if (errno == ENOEXEC) code = XML_IO_ENOEXEC;
#endif
#ifdef ENOLCK
else if (errno == ENOLCK) code = XML_IO_ENOLCK;
#endif
#ifdef ENOMEM
else if (errno == ENOMEM) code = XML_IO_ENOMEM;
#endif
#ifdef ENOSPC
else if (errno == ENOSPC) code = XML_IO_ENOSPC;
#endif
#ifdef ENOSYS
else if (errno == ENOSYS) code = XML_IO_ENOSYS;
#endif
#ifdef ENOTDIR
else if (errno == ENOTDIR) code = XML_IO_ENOTDIR;
#endif
#ifdef ENOTEMPTY
else if (errno == ENOTEMPTY) code = XML_IO_ENOTEMPTY;
#endif
#ifdef ENOTSUP
else if (errno == ENOTSUP) code = XML_IO_ENOTSUP;
#endif
#ifdef ENOTTY
else if (errno == ENOTTY) code = XML_IO_ENOTTY;
#endif
#ifdef ENXIO
else if (errno == ENXIO) code = XML_IO_ENXIO;
#endif
#ifdef EPERM
else if (errno == EPERM) code = XML_IO_EPERM;
#endif
#ifdef EPIPE
else if (errno == EPIPE) code = XML_IO_EPIPE;
#endif
#ifdef ERANGE
else if (errno == ERANGE) code = XML_IO_ERANGE;
#endif
#ifdef EROFS
else if (errno == EROFS) code = XML_IO_EROFS;
#endif
#ifdef ESPIPE
else if (errno == ESPIPE) code = XML_IO_ESPIPE;
#endif
#ifdef ESRCH
else if (errno == ESRCH) code = XML_IO_ESRCH;
#endif
#ifdef ETIMEDOUT
else if (errno == ETIMEDOUT) code = XML_IO_ETIMEDOUT;
#endif
#ifdef EXDEV
else if (errno == EXDEV) code = XML_IO_EXDEV;
#endif
#ifdef ENOTSOCK
else if (errno == ENOTSOCK) code = XML_IO_ENOTSOCK;
#endif
#ifdef EISCONN
else if (errno == EISCONN) code = XML_IO_EISCONN;
#endif
#ifdef ECONNREFUSED
else if (errno == ECONNREFUSED) code = XML_IO_ECONNREFUSED;
#endif
#ifdef ETIMEDOUT
else if (errno == ETIMEDOUT) code = XML_IO_ETIMEDOUT;
#endif
#ifdef ENETUNREACH
else if (errno == ENETUNREACH) code = XML_IO_ENETUNREACH;
#endif
#ifdef EADDRINUSE
else if (errno == EADDRINUSE) code = XML_IO_EADDRINUSE;
#endif
#ifdef EINPROGRESS
else if (errno == EINPROGRESS) code = XML_IO_EINPROGRESS;
#endif
#ifdef EALREADY
else if (errno == EALREADY) code = XML_IO_EALREADY;
#endif
#ifdef EAFNOSUPPORT
else if (errno == EAFNOSUPPORT) code = XML_IO_EAFNOSUPPORT;
#endif
else code = XML_IO_UNKNOWN;
}
if (xmlStructuredError) {
schannel = xmlStructuredError;
data = xmlStructuredErrorContext;
} else {
channel = xmlGenericError;
data = xmlGenericErrorContext;
}
if (extra != NULL) {
fmt = "%s: %s";
} else {
fmt = "%s";
}
arg1 = xmlErrString(code);
arg2 = extra;
res = __xmlRaiseError(schannel, channel, data, NULL, NULL,
domain, code, XML_ERR_ERROR, NULL, 0,
extra, NULL, NULL, 0, 0,
fmt, arg1, arg2);
if (res < 0) {
xmlIOErrMemory();
return(XML_ERR_NO_MEMORY);
}
return(code);
}
/**
* xmlIOErr:
* @code: the error number
* @extra: extra information
*
* Handle an I/O error
*/
static int
xmlIOErr(int code, const char *extra)
{
return(__xmlIOErr(XML_FROM_IO, code, extra));
}
/************************************************************************
* *
* Standard I/O for file accesses *
* *
************************************************************************/
#ifdef HAVE_STAT
#if defined(_MSC_VER) && _MSC_VER >= 1500
typedef struct _stat64 _stat_t;
#else
typedef struct stat _stat_t;
#endif
#endif
#if defined(_WIN32)
/**
* __xmlIOWin32UTF8ToWChar:
* @u8String: uft-8 string
*
* Convert a string from utf-8 to wchar (WINDOWS ONLY!)
*/
static wchar_t *
__xmlIOWin32UTF8ToWChar(const char *u8String)
{
wchar_t *wString = NULL;
int i;
if (u8String) {
int wLen =
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, u8String,
-1, NULL, 0);
if (wLen) {
wString = xmlMalloc(wLen * sizeof(wchar_t));
if (wString) {
if (MultiByteToWideChar
(CP_UTF8, 0, u8String, -1, wString, wLen) == 0) {
xmlFree(wString);
wString = NULL;
}
}
/*
* Convert to backward slash
*/
for (i = 0; wString[i] != 0; i++) {
if (wString[i] == '/')
wString[i] = '\\';
}
}
}
return wString;
}
#endif
/**
* xmlNormalizeWindowsPath:
* @path: the input file path
*
* DEPRECATED: This never really worked.
*
* Returns a copy of path.
*/
xmlChar *
xmlNormalizeWindowsPath(const xmlChar *path)
{
return xmlStrdup(path);
}
/**
* xmlCheckFilename:
* @path: the path to check
*
* DEPRECATED: Internal function, don't use.
*
* if stat is not available on the target machine,
* returns 1. if stat fails, returns 0 (if calling
* stat on the filename fails, it can't be right).
* if stat succeeds and the file is a directory,
* returns 2. otherwise returns 1.
*/
int
xmlCheckFilename(const char *path)
{
#ifdef HAVE_STAT
#if defined(_MSC_VER) && _MSC_VER >= 1500
struct _stat64 stat_buffer;
#else
struct stat stat_buffer;
#endif
int res;
#endif
if (path == NULL)
return(0);
#ifdef HAVE_STAT
#if defined(_WIN32)
{
wchar_t *wpath;
/*
* On Windows stat and wstat do not work with long pathname,
* which start with '\\?\'
*/
if ((path[0] == '\\') && (path[1] == '\\') && (path[2] == '?') &&
(path[3] == '\\') )
return 1;
wpath = __xmlIOWin32UTF8ToWChar(path);
if (wpath == NULL)
return(0);
#if defined(_MSC_VER) && _MSC_VER >= 1500
res = _wstat64(wpath, &stat_buffer);
#else
res = _wstat(wpath, &stat_buffer);
#endif
xmlFree(wpath);
}
#else
res = stat(path, &stat_buffer);
#endif
if (res < 0)
return 0;
#ifdef S_ISDIR
if (S_ISDIR(stat_buffer.st_mode))
return 2;
#endif
#endif /* HAVE_STAT */
return 1;
}
static int
xmlConvertUriToPath(const char *uri, char **out) {
const char *escaped;
char *unescaped;
*out = NULL;
if (!xmlStrncasecmp(BAD_CAST uri, BAD_CAST "file://localhost/", 17)) {
escaped = &uri[16];
} else if (!xmlStrncasecmp(BAD_CAST uri, BAD_CAST "file:///", 8)) {
escaped = &uri[7];
} else if (!xmlStrncasecmp(BAD_CAST uri, BAD_CAST "file:/", 6)) {
/* lots of generators seems to lazy to read RFC 1738 */
escaped = &uri[5];
} else {
return(1);
}
#ifdef _WIN32
/* Ignore slash like in file:///C:/file.txt */
escaped += 1;
#endif
unescaped = xmlURIUnescapeString(escaped, 0, NULL);
if (unescaped == NULL)
return(-1);
*out = unescaped;
return(0);
}
/**
* xmlFdOpen:
* @filename: the URI for matching
* @out: pointer to resulting context
*
* Returns an xmlParserErrors code
*/
static int
xmlFdOpen(const char *filename, int write, int *out) {
char *fromUri = NULL;
int flags;
int fd;
int ret;
*out = -1;
if (filename == NULL)
return(XML_ERR_ARGUMENT);
if (xmlConvertUriToPath(filename, &fromUri) < 0)
return(XML_ERR_NO_MEMORY);
if (fromUri != NULL)
filename = fromUri;
#if defined(_WIN32)
{
wchar_t *wpath;
wpath = __xmlIOWin32UTF8ToWChar(filename);
if (wpath == NULL) {
xmlFree(fromUri);
return(XML_ERR_NO_MEMORY);
}
if (write)
flags = _O_WRONLY | _O_CREAT | _O_TRUNC;
else
flags = _O_RDONLY;
fd = _wopen(wpath, flags | _O_BINARY, 0666);
xmlFree(wpath);
}
#else
if (write)
flags = O_WRONLY | O_CREAT | O_TRUNC;
else
flags = O_RDONLY;
fd = open(filename, flags, 0666);
#endif /* WIN32 */
if (fd < 0) {
/*
* Windows and possibly other platforms return EINVAL
* for invalid filenames.
*/
if ((errno == ENOENT) || (errno == EINVAL)) {
ret = XML_IO_ENOENT;
} else {
/*
* This error won't be forwarded to the parser context
* which will report it a second time.
*/
ret = xmlIOErr(0, filename);
}
} else {
*out = fd;
ret = XML_ERR_OK;
}
xmlFree(fromUri);
return(ret);
}
/**
* xmlFdRead:
* @context: the I/O context
* @buffer: where to drop data
* @len: number of bytes to read
*
* Read @len bytes to @buffer from the I/O channel.
*
* Returns the number of bytes read
*/
static int
xmlFdRead(void *context, char *buffer, int len) {
int fd = (int) (ptrdiff_t) context;
int ret = 0;
int bytes;
while (len > 0) {
bytes = read(fd, buffer, len);
if (bytes < 0) {
/*
* If we already got some bytes, return them without
* raising an error.
*/
if (ret > 0)
break;
return(-xmlIOErr(0, "read()"));
}
if (bytes == 0)
break;
ret += bytes;
buffer += bytes;
len -= bytes;
}
return(ret);
}
#ifdef LIBXML_OUTPUT_ENABLED
/**
* xmlFdWrite:
* @context: the I/O context
* @buffer: where to get data
* @len: number of bytes to write
*
* Write @len bytes from @buffer to the I/O channel.
*
* Returns the number of bytes written
*/
static int
xmlFdWrite(void *context, const char *buffer, int len) {
int fd = (int) (ptrdiff_t) context;
int ret = 0;
int bytes;
while (len > 0) {
bytes = write(fd, buffer, len);
if (bytes < 0)
return(-xmlIOErr(0, "write()"));
ret += bytes;
buffer += bytes;
len -= bytes;
}
return(ret);
}
#endif /* LIBXML_OUTPUT_ENABLED */
/**
* xmlFdClose:
* @context: the I/O context
*
* Close an I/O channel
*
* Returns 0 in case of success and error code otherwise
*/
static int
xmlFdClose (void * context) {
int ret;
ret = close((int) (ptrdiff_t) context);
if (ret < 0)
return(xmlIOErr(0, "close()"));
return(XML_ERR_OK);
}
/**
* xmlFileMatch:
* @filename: the URI for matching
*
* DEPRECATED: Internal function, don't use.
*
* Returns 1 if matches, 0 otherwise
*/
int
xmlFileMatch (const char *filename ATTRIBUTE_UNUSED) {
return(1);
}
/**
* xmlFileOpenSafe:
* @filename: the URI for matching
* @out: pointer to resulting context
*
* input from FILE *
*
* Returns an I/O context or NULL in case of error
*/
static int
xmlFileOpenSafe(const char *filename, int write, void **out) {
char *fromUri = NULL;
FILE *fd;
int ret = XML_ERR_OK;
*out = NULL;
if (filename == NULL)
return(XML_ERR_ARGUMENT);
if (xmlConvertUriToPath(filename, &fromUri) < 0)
return(XML_ERR_NO_MEMORY);
if (fromUri != NULL)
filename = fromUri;
#if defined(_WIN32)
{
wchar_t *wpath;
wpath = __xmlIOWin32UTF8ToWChar(filename);
if (wpath == NULL) {
xmlFree(fromUri);
return(XML_ERR_NO_MEMORY);
}
fd = _wfopen(wpath, write ? L"wb" : L"rb");
xmlFree(wpath);
}
#else
fd = fopen(filename, write ? "wb" : "rb");
#endif /* WIN32 */
if (fd == NULL) {
/*
* Windows and possibly other platforms return EINVAL
* for invalid filenames.
*/
if ((errno == ENOENT) || (errno == EINVAL)) {
ret = XML_IO_ENOENT;
} else {
/*
* This error won't be forwarded to the parser context
* which will report it a second time.
*/
ret = xmlIOErr(0, filename);
}
}
*out = fd;
xmlFree(fromUri);
return(ret);
}
/**
* xmlFileOpen:
* @filename: the URI for matching
*
* DEPRECATED: Internal function, don't use.
*
* Returns an IO context or NULL in case or failure
*/
void *
xmlFileOpen(const char *filename) {
void *context;
xmlFileOpenSafe(filename, 0, &context);
return(context);
}
/**
* xmlFileRead:
* @context: the I/O context
* @buffer: where to drop data
* @len: number of bytes to write
*
* DEPRECATED: Internal function, don't use.
*
* Returns the number of bytes read or < 0 in case of failure
*/
int
xmlFileRead(void * context, char * buffer, int len) {
FILE *file = context;
size_t bytes;
if ((context == NULL) || (buffer == NULL))
return(-1);
/*
* The C standard doesn't mandate that fread sets errno, only
* POSIX does. The Windows documentation isn't really clear.
* Set errno to zero which will be reported as unknown error
* if fread fails without setting errno.
*/
errno = 0;
bytes = fread(buffer, 1, len, file);
if ((bytes < (size_t) len) && (ferror(file)))
return(-xmlIOErr(0, "fread()"));
return(bytes);
}
#ifdef LIBXML_OUTPUT_ENABLED
/**
* xmlFileWrite:
* @context: the I/O context
* @buffer: where to drop data
* @len: number of bytes to write
*
* Write @len bytes from @buffer to the I/O channel.
*
* Returns the number of bytes written
*/
static int
xmlFileWrite(void *context, const char *buffer, int len) {
FILE *file = context;
size_t bytes;
if ((context == NULL) || (buffer == NULL))
return(-1);
errno = 0;
bytes = fwrite(buffer, 1, len, file);
if (bytes < (size_t) len)
return(-xmlIOErr(0, "fwrite()"));
return(len);
}
#endif /* LIBXML_OUTPUT_ENABLED */
/**
* xmlFileFlush:
* @context: the I/O context
*
* Flush an I/O channel
*/
static int
xmlFileFlush (void * context) {
FILE *file = context;
if (file == NULL)
return(-1);
if (fflush(file) != 0)
return(xmlIOErr(0, "fflush()"));
return(XML_ERR_OK);
}
/**
* xmlFileClose:
* @context: the I/O context
*
* DEPRECATED: Internal function, don't use.
*
* Returns 0 or -1 an error code case of error
*/
int
xmlFileClose (void * context) {
FILE *file = context;
if (context == NULL)
return(-1);
if (file == stdin)
return(0);
if ((file == stdout) || (file == stderr))
return(xmlFileFlush(file));
if (fclose(file) != 0)
return(xmlIOErr(0, "fclose()"));
return(0);
}
#ifdef LIBXML_OUTPUT_ENABLED
/**
* xmlBufferWrite:
* @context: the xmlBuffer
* @buffer: the data to write
* @len: number of bytes to write
*
* Write @len bytes from @buffer to the xml buffer
*
* Returns the number of bytes written or a negative xmlParserErrors
* value.
*/
static int
xmlBufferWrite (void * context, const char * buffer, int len) {
int ret;
ret = xmlBufferAdd((xmlBufferPtr) context, (const xmlChar *) buffer, len);
if (ret != 0)
return(-XML_ERR_NO_MEMORY);
return(len);
}
#endif
#ifdef LIBXML_ZLIB_ENABLED
/************************************************************************
* *
* I/O for compressed file accesses *
* *
************************************************************************/
/**
* xmlGzfileRead:
* @context: the I/O context
* @buffer: where to drop data
* @len: number of bytes to write
*
* Read @len bytes to @buffer from the compressed I/O channel.
*
* Returns the number of bytes read.
*/
static int
xmlGzfileRead (void * context, char * buffer, int len) {
int ret;
ret = gzread((gzFile) context, &buffer[0], len);
if (ret < 0) xmlIOErr(0, "gzread()");
return(ret);
}
#ifdef LIBXML_OUTPUT_ENABLED
/**
* xmlGzfileWrite:
* @context: the I/O context
* @buffer: where to drop data
* @len: number of bytes to write
*
* Write @len bytes from @buffer to the compressed I/O channel.
*
* Returns the number of bytes written
*/
static int
xmlGzfileWrite (void * context, const char * buffer, int len) {
int ret;
ret = gzwrite((gzFile) context, (char *) &buffer[0], len);
if (ret < 0) xmlIOErr(0, "gzwrite()");
return(ret);
}
#endif /* LIBXML_OUTPUT_ENABLED */
/**
* xmlGzfileClose:
* @context: the I/O context
*
* Close a compressed I/O channel
*/
static int
xmlGzfileClose (void * context) {
int ret;
ret = (gzclose((gzFile) context) == Z_OK ) ? 0 : -1;
if (ret < 0) xmlIOErr(0, "gzclose()");
return(ret);
}
#endif /* LIBXML_ZLIB_ENABLED */
/************************************************************************
* *
* I/O for compressed file accesses *
* *
************************************************************************/
#ifdef LIBXML_LZMA_ENABLED
#include "private/xzlib.h"
/**
* xmlXzfileRead:
* @context: the I/O context
* @buffer: where to drop data
* @len: number of bytes to write
*
* Read @len bytes to @buffer from the compressed I/O channel.
*
* Returns the number of bytes written
*/
static int
xmlXzfileRead (void * context, char * buffer, int len) {
int ret;
ret = __libxml2_xzread((xzFile) context, &buffer[0], len);
if (ret < 0) xmlIOErr(0, "xzread()");
return(ret);
}
/**
* xmlXzfileClose:
* @context: the I/O context
*
* Close a compressed I/O channel
*/
static int
xmlXzfileClose (void * context) {
int ret;
ret = (__libxml2_xzclose((xzFile) context) == LZMA_OK ) ? 0 : -1;
if (ret < 0) xmlIOErr(0, "xzclose()");
return(ret);