-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathLibXML.xs
9709 lines (8760 loc) · 259 KB
/
LibXML.xs
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
/* $Id$
*
* This is free software, you may use it and distribute it under the same terms as
* Perl itself.
*
* Copyright 2001-2003 AxKit.com Ltd., 2002-2006 Christian Glahn, 2006-2009 Petr Pajas
*/
#ifdef __cplusplus
extern "C" {
#endif
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_DEPRECATE 1
#define _CRT_NONSTDC_NO_DEPRECATE 1
#endif
/* perl stuff */
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#define NEED_newRV_noinc_GLOBAL
#define NEED_sv_2pv_flags
#include "ppport.h"
#include "Av_CharPtrPtr.h" /* XS_*_charPtrPtr() */
#include <fcntl.h>
#include <limits.h> /* INT_MAX */
#ifndef WIN32
#include <unistd.h>
#endif
/* libxml2 configuration properties */
#include <libxml/xmlversion.h>
#define DEBUG_C14N
/* libxml2 stuff */
#include <libxml/xmlversion.h>
#include <libxml/globals.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>
#include <libxml/c14n.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <libxml/xmlIO.h>
/* #include <libxml/debugXML.h> */
#include <libxml/xmlerror.h>
#include <libxml/xinclude.h>
#include <libxml/valid.h>
#ifdef LIBXML_PATTERN_ENABLED
#include <libxml/pattern.h>
#endif
#ifdef LIBXML_REGEXP_ENABLED
#include <libxml/xmlregexp.h>
#endif
#if LIBXML_VERSION >= 20510
#define HAVE_SCHEMAS
#include <libxml/relaxng.h>
#include <libxml/xmlschemas.h>
#endif
#if LIBXML_VERSION >= 20621
#define WITH_SERRORS
#ifdef LIBXML_READER_ENABLED
#define HAVE_READER_SUPPORT
#include <libxml/xmlreader.h>
#endif
#endif
#ifdef LIBXML_CATALOG_ENABLED
#include <libxml/catalog.h>
#endif
#ifdef HAVE_READER_SUPPORT
typedef enum {
XML_TEXTREADER_NONE = -1,
XML_TEXTREADER_START= 0,
XML_TEXTREADER_ELEMENT= 1,
XML_TEXTREADER_END= 2,
XML_TEXTREADER_EMPTY= 3,
XML_TEXTREADER_BACKTRACK= 4,
XML_TEXTREADER_DONE= 5,
XML_TEXTREADER_ERROR= 6
} xmlTextReaderState;
typedef enum {
XML_TEXTREADER_NOT_VALIDATE = 0,
XML_TEXTREADER_VALIDATE_DTD = 1,
XML_TEXTREADER_VALIDATE_RNG = 2,
XML_TEXTREADER_VALIDATE_XSD = 4
} xmlTextReaderValidate;
#endif /* HAVE_READER_SUPPORT */
/* GDOME support
* libgdome installs only the core functions to the system.
* this is not enough for XML::LibXML <-> XML::GDOME conversion.
* therefore there is the need to ship as well the GDOME core headers.
*/
#ifdef XML_LIBXML_GDOME_SUPPORT
#include <libgdome/gdome.h>
#include <libgdome/gdome-libxml-util.h>
#endif
#if LIBXML_VERSION < 20621
/* HTML_PARSE_RECOVER was added in libxml2 2.6.21 */
# define HTML_PARSE_RECOVER XML_PARSE_RECOVER
#endif
/* XML::LibXML stuff */
#include "perl-libxml-mm.h"
#include "perl-libxml-sax.h"
#include "dom.h"
#include "xpath.h"
#include "xpathcontext.h"
#ifdef __cplusplus
}
#endif
#define TEST_PERL_FLAG(flag) \
SvTRUE(get_sv(flag, FALSE)) ? 1 : 0
#ifdef HAVE_READER_SUPPORT
#define LIBXML_READER_TEST_ELEMENT(reader,name,nsURI) \
(xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) && \
((!nsURI && !name) \
|| \
(!nsURI && xmlStrcmp((const xmlChar*)name, xmlTextReaderConstName(reader) ) == 0 ) \
|| \
(nsURI && xmlStrcmp((const xmlChar*)nsURI, xmlTextReaderConstNamespaceUri(reader))==0 \
&& \
(!name || xmlStrcmp((const xmlChar*)name, xmlTextReaderConstLocalName(reader)) == 0)))
#endif
/* this should keep the default */
static xmlExternalEntityLoader LibXML_old_ext_ent_loader = NULL;
/* global external entity loader */
SV *EXTERNAL_ENTITY_LOADER_FUNC = (SV *)NULL;
SV* PROXY_NODE_REGISTRY_MUTEX = NULL;
/* ****************************************************************
* Error handler
* **************************************************************** */
#ifdef WITH_SERRORS
#define INIT_READER_ERROR_HANDLER(reader)
#define PREINIT_SAVED_ERROR SV* saved_error = sv_2mortal(newSV(0));
#define INIT_ERROR_HANDLER \
xmlSetGenericErrorFunc((void *)saved_error, \
(xmlGenericErrorFunc) LibXML_flat_handler); \
xmlSetStructuredErrorFunc((void *)saved_error, \
(xmlStructuredErrorFunc)LibXML_struct_error_handler)
#define REPORT_ERROR(recover) LibXML_report_error_ctx(saved_error, recover)
#define CLEANUP_ERROR_HANDLER xmlSetGenericErrorFunc(NULL,NULL); \
xmlSetStructuredErrorFunc(NULL,NULL)
#else /* WITH_SERRORS */
#define INIT_READER_ERROR_HANDLER(reader) \
if (reader) \
xmlTextReaderSetErrorHandler(reader, LibXML_reader_error_handler, \
sv_2mortal(newSVpv("",0)));
#define PREINIT_SAVED_ERROR SV* saved_error = sv_2mortal(newSVpv("",0));
#define INIT_ERROR_HANDLER \
xmlSetGenericErrorFunc((void *) saved_error, \
(xmlGenericErrorFunc) LibXML_error_handler_ctx)
#define REPORT_ERROR(recover) LibXML_report_error_ctx(saved_error, recover)
#define CLEANUP_ERROR_HANDLER xmlSetGenericErrorFunc(NULL,NULL);
#endif /* WITH_SERRORS */
#ifdef WITH_SERRORS
void
LibXML_struct_error_callback(SV * saved_error, SV * libErr )
{
dTHX;
dSP;
if ( saved_error == NULL ) {
warn( "have no save_error\n" );
}
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(libErr));
if ( saved_error != NULL && SvOK(saved_error) ) {
XPUSHs(saved_error);
}
PUTBACK;
if ( saved_error != NULL ) {
call_pv( "XML::LibXML::Error::_callback_error", G_SCALAR | G_EVAL );
} else {
call_pv( "XML::LibXML::Error::_instant_error_callback", G_SCALAR );
}
SPAGAIN;
if ( SvTRUE(ERRSV) ) {
(void) POPs;
croak_obj;
} else {
sv_setsv(saved_error, POPs);
}
PUTBACK;
FREETMPS;
LEAVE;
}
void
LibXML_struct_error_handler(SV * saved_error, xmlErrorPtr error )
{
const char * CLASS = "XML::LibXML::LibError";
SV* libErr;
libErr = NEWSV(0,0);
sv_setref_pv( libErr, CLASS, (void*)error );
LibXML_struct_error_callback( saved_error, libErr);
}
void
LibXML_flat_handler(SV * saved_error, const char * msg, ...)
{
SV* sv;
va_list args;
sv = newSVpv("",0);
va_start(args, msg);
sv_vcatpvf(sv, msg, &args);
va_end(args);
xs_warn("flat error\n");
LibXML_struct_error_callback( saved_error, sv);
}
#endif /* WITH_SERRORS */
/* If threads-support is working correctly in libxml2 then
* this method will be called with the correct thread-context */
void
LibXML_error_handler_ctx(void * ctxt, const char * msg, ...)
{
va_list args;
SV * saved_error = (SV *) ctxt;
/* If saved_error is null we croak with the error */
if( NULL == saved_error ) {
SV * sv = sv_2mortal(newSV(0));
va_start(args, msg);
/* vfprintf(stderr, msg, args); */
sv_vsetpvfn(sv, msg, strlen(msg), &args, NULL, 0, NULL);
va_end(args);
croak("%s", SvPV_nolen(sv));
/* Otherwise, save the error */
} else {
va_start(args, msg);
/* vfprintf(stderr, msg, args); */
sv_vcatpvfn(saved_error, msg, strlen(msg), &args, NULL, 0, NULL);
va_end(args);
}
}
static void
LibXML_validity_error_ctx(void * ctxt, const char *msg, ...)
{
va_list args;
SV * saved_error = (SV *) ctxt;
/* If saved_error is null we croak with the error */
if( NULL == saved_error ) {
SV * sv = sv_2mortal(newSV(0));
va_start(args, msg);
sv_vsetpvfn(sv, msg, strlen(msg), &args, NULL, 0, NULL);
va_end(args);
croak("%s", SvPV_nolen(sv));
/* Otherwise, save the error */
} else {
va_start(args, msg);
sv_vcatpvfn(saved_error, msg, strlen(msg), &args, NULL, 0, NULL);
va_end(args);
}
}
static void
LibXML_validity_warning_ctx(void * ctxt, const char *msg, ...)
{
va_list args;
SV * saved_error = (SV *) ctxt;
STRLEN len;
/* If saved_error is null we croak with the error */
if( NULL == saved_error ) {
SV * sv = sv_2mortal(newSV(0));
va_start(args, msg);
sv_vsetpvfn(sv, msg, strlen(msg), &args, NULL, 0, NULL);
va_end(args);
croak("LibXML_validity_warning_ctx internal error: context was null (%s)", SvPV_nolen(sv));
/* Otherwise, give the warning */
} else {
va_start(args, msg);
sv_vcatpvfn(saved_error, msg, strlen(msg), &args, NULL, 0, NULL);
va_end(args);
warn("validation error: %s", SvPV(saved_error, len));
}
}
static int
LibXML_will_die_ctx(SV * saved_error, int recover)
{
#ifdef WITH_SERRORS
if( saved_error!=NULL && SvOK(saved_error) ) {
if ( recover == 0 ) {
return 1;
}
}
#else
if( 0 < SvCUR( saved_error ) ) {
if ( recover == 0 ) {
return 1;
}
}
#endif
return 0;
}
static void
LibXML_report_error_ctx(SV * saved_error, int recover)
{
#ifdef WITH_SERRORS
if( saved_error!=NULL && SvOK( saved_error ) ) {
if (!recover || recover==1) {
dTHX;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 1);
PUSHs(saved_error);
PUTBACK;
if (recover==1) {
call_pv( "XML::LibXML::Error::_report_warning", G_SCALAR | G_DISCARD);
} else {
call_pv( "XML::LibXML::Error::_report_error", G_SCALAR | G_DISCARD);
}
SPAGAIN;
PUTBACK;
FREETMPS;
LEAVE;
}
}
#else
if( 0 < SvCUR( saved_error ) ) {
if( recover ) {
if ( recover == 1 ) {
warn("%s", SvPV_nolen(saved_error));
} /* else recover silently */
} else {
croak("%s", SvPV_nolen(saved_error));
}
}
#endif
}
#ifdef HAVE_READER_SUPPORT
#ifndef WITH_SERRORS
static void
LibXML_reader_error_handler(void * ctxt,
const char * msg,
xmlParserSeverities severity,
xmlTextReaderLocatorPtr locator)
{
int line = xmlTextReaderLocatorLineNumber(locator);
xmlChar * filename = xmlTextReaderLocatorBaseURI(locator);
SV * msg_sv = sv_2mortal(C2Sv((xmlChar*) msg,NULL));
SV * error = sv_2mortal(newSVpv("", 0));
switch (severity) {
case XML_PARSER_SEVERITY_VALIDITY_WARNING:
sv_catpv(error, "Validity WARNING");
break;
case XML_PARSER_SEVERITY_WARNING:
sv_catpv(error, "Reader WARNING");
break;
case XML_PARSER_SEVERITY_VALIDITY_ERROR:
sv_catpv(error, "Validity ERROR");
break;
case XML_PARSER_SEVERITY_ERROR:
sv_catpv(error, "Reader ERROR");
break;
}
if (filename) {
sv_catpvf(error, " in %s", filename);
xmlFree(filename);
}
if (line >= 0) {
sv_catpvf(error, " at line %d", line);
}
sv_catpvf(error, ": %s", SvPV_nolen(msg_sv));
if (severity == XML_PARSER_SEVERITY_VALIDITY_WARNING ||
severity == XML_PARSER_SEVERITY_WARNING ) {
warn("%s", SvPV_nolen(error));
} else {
SV * error_sv = (SV*) ctxt;
if (error_sv) {
sv_catpvf(error_sv, "%s ", SvPV_nolen(error));
} else {
croak("%s",SvPV_nolen(error));
}
}
}
#endif /* !defined WITH_SERRORS */
SV *
LibXML_get_reader_error_data(xmlTextReaderPtr reader)
{
SV * saved_error = NULL;
xmlTextReaderErrorFunc f = NULL;
xmlTextReaderGetErrorHandler(reader, &f, (void **) &saved_error);
return saved_error;
}
#ifndef WITH_SERRORS
static void
LibXML_report_reader_error(xmlTextReaderPtr reader)
{
SV * saved_error = NULL;
xmlTextReaderErrorFunc f = NULL;
xmlTextReaderGetErrorHandler(reader, &f, (void **) &saved_error);
if ( saved_error && SvOK( saved_error) && 0 < SvCUR( saved_error ) ) {
croak("%s", SvPV_nolen(saved_error));
}
}
#endif /* !defined WITH_SERRORS */
#endif /* HAVE_READER_SUPPORT */
static int
LibXML_get_recover(HV * real_obj)
{
SV** item = hv_fetch( real_obj, "XML_LIBXML_RECOVER", 18, 0 );
return ( item != NULL && SvTRUE(*item) ) ? SvIV(*item) : 0;
}
static SV *
LibXML_NodeToSv(HV * real_obj, xmlNodePtr real_doc)
{
SV** item = hv_fetch( real_obj, "XML_LIBXML_GDOME", 16, 0 );
if ( item != NULL && SvTRUE(*item) ) {
return PmmNodeToGdomeSv(real_doc);
}
else {
return PmmNodeToSv(real_doc, NULL);
}
}
/* ****************************************************************
* IO callbacks
* **************************************************************** */
int
LibXML_read_perl (SV * ioref, char * buffer, int len)
{
dTHX;
dSP;
int cnt;
SV * read_results;
IV read_results_iv;
STRLEN read_length;
char * chars;
SV * tbuff = NEWSV(0,len);
SV * tsize = newSViv(len);
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 3);
PUSHs(ioref);
PUSHs(sv_2mortal(tbuff));
PUSHs(sv_2mortal(tsize));
PUTBACK;
if (sv_isobject(ioref)) {
cnt = call_method("read", G_SCALAR | G_EVAL);
}
else {
cnt = call_pv("XML::LibXML::__read", G_SCALAR | G_EVAL);
}
SPAGAIN;
if (cnt != 1) {
croak("read method call failed");
}
if (SvTRUE(ERRSV)) {
(void) POPs;
croak_obj;
}
read_results = POPs;
if (!SvOK(read_results)) {
croak("read error");
}
read_results_iv = SvIV(read_results);
chars = SvPV(tbuff, read_length);
/*
* If the file handle uses an encoding layer, the length parameter is
* interpreted as character count, not as byte count. So it's possible
* that more than len bytes are read which would overflow the buffer.
* Check for this condition also by comparing the return value.
*/
if (read_results_iv != read_length || read_length > len) {
croak("Read more bytes than requested. Do you use an encoding-related"
" PerlIO layer?");
}
strncpy(buffer, chars, read_length);
PUTBACK;
FREETMPS;
LEAVE;
return read_length;
}
/* used only by Reader */
int
LibXML_close_perl (SV * ioref)
{
SvREFCNT_dec(ioref);
return 0;
}
int
LibXML_input_match(char const * filename)
{
int results;
int count;
SV * res;
results = 0;
{
dTHX;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 1);
PUSHs(sv_2mortal(newSVpv((char*)filename, 0)));
PUTBACK;
count = call_pv("XML::LibXML::InputCallback::_callback_match",
G_SCALAR | G_EVAL);
SPAGAIN;
if (count != 1) {
croak("match callback must return a single value");
}
if (SvTRUE(ERRSV)) {
(void) POPs;
croak_obj;
}
res = POPs;
if (SvTRUE(res)) {
results = 1;
}
PUTBACK;
FREETMPS;
LEAVE;
}
return results;
}
void *
LibXML_input_open(char const * filename)
{
SV * results;
int count;
dTHX;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 1);
PUSHs(sv_2mortal(newSVpv((char*)filename, 0)));
PUTBACK;
count = call_pv("XML::LibXML::InputCallback::_callback_open",
G_SCALAR | G_EVAL);
SPAGAIN;
if (count != 1) {
croak("open callback must return a single value");
}
if (SvTRUE(ERRSV)) {
(void) POPs;
croak_obj;
}
results = POPs;
(void)SvREFCNT_inc(results);
PUTBACK;
FREETMPS;
LEAVE;
return (void *)results;
}
int
LibXML_input_read(void * context, char * buffer, int len)
{
STRLEN res_len;
const char * output;
SV * ctxt;
SV * output_sv;
res_len = 0;
ctxt = (SV *)context;
{
int count;
dTHX;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 2);
PUSHs(ctxt);
PUSHs(sv_2mortal(newSViv(len)));
PUTBACK;
count = call_pv("XML::LibXML::InputCallback::_callback_read",
G_SCALAR | G_EVAL);
SPAGAIN;
if (count != 1) {
croak("read callback must return a single value");
}
if (SvTRUE(ERRSV)) {
(void) POPs;
croak_obj;
}
/*
* Handle undef()s gracefully, to avoid using POPpx which warns upon $^W
* being set. See t/49callbacks_returning_undef.t and:
* https://rt.cpan.org/Ticket/Display.html?id=70321
* */
output_sv = POPs;
output = SvOK(output_sv) ? SvPV_nolen(output_sv) : NULL;
if (output != NULL) {
res_len = strlen(output);
if (res_len) {
strncpy(buffer, output, res_len);
}
else {
buffer[0] = 0;
}
}
PUTBACK;
FREETMPS;
LEAVE;
}
return res_len;
}
void
LibXML_input_close(void * context)
{
SV * ctxt;
ctxt = (SV *)context;
{
dTHX;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 1);
PUSHs(ctxt);
PUTBACK;
call_pv("XML::LibXML::InputCallback::_callback_close",
G_SCALAR | G_EVAL | G_DISCARD);
SvREFCNT_dec(ctxt);
if (SvTRUE(ERRSV)) {
croak_obj;
}
FREETMPS;
LEAVE;
}
}
int
LibXML_output_write_handler(void * ioref, char * buffer, int len)
{
if ( buffer != NULL && len > 0) {
dTHX;
dSP;
SV * tbuff = newSVpv(buffer,len);
SV * tsize = newSViv(len);
ENTER;
SAVETMPS;
PUSHMARK(SP);
EXTEND(SP, 3);
PUSHs((SV*)ioref);
PUSHs(sv_2mortal(tbuff));
PUSHs(sv_2mortal(tsize));
PUTBACK;
call_pv("XML::LibXML::__write", G_SCALAR | G_EVAL | G_DISCARD );
if (SvTRUE(ERRSV)) {
croak_obj;
}
FREETMPS;
LEAVE;
}
return len;
}
int
LibXML_output_close_handler( void * handler )
{
return 1;
}
xmlParserInputPtr
LibXML_load_external_entity(
const char * URL,
const char * ID,
xmlParserCtxtPtr ctxt)
{
SV ** func;
int count;
SV * results;
STRLEN results_len;
int int_results_len;
const char * results_pv;
xmlParserInputBufferPtr input_buf;
if (ctxt->_private == NULL && EXTERNAL_ENTITY_LOADER_FUNC == NULL)
{
return xmlNewInputFromFile(ctxt, URL);
}
if (URL == NULL) {
URL = "";
}
if (ID == NULL) {
ID = "";
}
/* fetch entity loader function */
if(EXTERNAL_ENTITY_LOADER_FUNC != NULL)
{
func = &EXTERNAL_ENTITY_LOADER_FUNC;
}
else
{
SV * self;
HV * real_obj;
self = (SV *)ctxt->_private;
real_obj = (HV *)SvRV(self);
func = hv_fetch(real_obj, "ext_ent_handler", 15, 0);
}
if (func != NULL && SvTRUE(*func)) {
dTHX;
dSP;
ENTER;
SAVETMPS;
PUSHMARK(SP) ;
XPUSHs(sv_2mortal(newSVpv((char*)URL, 0)));
XPUSHs(sv_2mortal(newSVpv((char*)ID, 0)));
PUTBACK;
count = call_sv(*func, G_SCALAR | G_EVAL);
SPAGAIN;
if (count == 0) {
croak("external entity handler did not return a value");
}
if (SvTRUE(ERRSV)) {
(void) POPs;
croak_obj;
}
results = POPs;
results_pv = SvPV(results, results_len);
int_results_len = results_len;
if ((results_len > INT_MAX) || (int_results_len != results_len)) {
croak("a buffer would be too big\n");
}
input_buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
if (!input_buf) {
croak("cannot create a buffer!\n");
}
if (-1 == xmlParserInputBufferPush(input_buf, int_results_len, results_pv)) {
xmlFreeParserInputBuffer(input_buf);
croak("cannot push an external entity into a buffer!\n");
}
PUTBACK;
FREETMPS;
LEAVE;
return xmlNewIOInputStream(ctxt, input_buf, XML_CHAR_ENCODING_NONE);
}
else {
if (URL == NULL) {
return NULL;
}
return xmlNewInputFromFile(ctxt, URL);
}
}
/* ****************************************************************
* Helper functions
* **************************************************************** */
HV*
LibXML_init_parser( SV * self, xmlParserCtxtPtr ctxt ) {
/* we fetch all switches and callbacks from the hash */
HV* real_obj = NULL;
SV** item = NULL;
int parserOptions = XML_PARSE_NODICT;
/* A NOTE ABOUT xmlInitParser(); */
/* xmlInitParser() should be used only at startup and*/
/* not for initializing a single parser. libxml2's */
/* documentation is quite clear about this. If */
/* something fails it is a problem elsewhere. Simply */
/* resetting the entire module will lead to unwanted */
/* results in server environments, such as if */
/* mod_perl is used together with php's xml module. */
/* calling xmlInitParser() here is definitely wrong! */
/* xmlInitParser(); */
#ifndef WITH_SERRORS
xmlGetWarningsDefaultValue = 0;
#endif
if ( self != NULL ) {
/* first fetch the values from the hash */
real_obj = (HV *)SvRV(self);
item = hv_fetch( real_obj, "XML_LIBXML_PARSER_OPTIONS", 25, 0 );
if (item != NULL && SvOK(*item)) parserOptions = sv_2iv(*item);
/* compatibility with old implementation:
absence of XML_PARSE_DTDLOAD (load_ext_dtd) implies absence of
all DTD related flags
*/
if ((parserOptions & XML_PARSE_DTDLOAD) == 0) {
parserOptions &= ~(XML_PARSE_DTDVALID | XML_PARSE_DTDATTR | XML_PARSE_NOENT );
}
if (ctxt) xmlCtxtUseOptions(ctxt, parserOptions ); /* Note: sets ctxt->linenumbers = 1 */
/*
* Without this if/else conditional, NOBLANKS has no effect.
*
* For more information, see:
*
* https://rt.cpan.org/Ticket/Display.html?id=76696
*
* */
if (parserOptions & XML_PARSE_NOBLANKS) {
xmlKeepBlanksDefault(0);
}
else {
xmlKeepBlanksDefault(1);
}
item = hv_fetch( real_obj, "XML_LIBXML_LINENUMBERS", 22, 0 );
if ( item != NULL && SvTRUE(*item) ) {
if (ctxt) ctxt->linenumbers = 1;
}
else {
if (ctxt) ctxt->linenumbers = 0;
}
if(EXTERNAL_ENTITY_LOADER_FUNC == NULL)
{
item = hv_fetch(real_obj, "ext_ent_handler", 15, 0);
if (item != NULL && SvTRUE(*item)) {
LibXML_old_ext_ent_loader = xmlGetExternalEntityLoader();
xmlSetExternalEntityLoader( (xmlExternalEntityLoader)LibXML_load_external_entity );
}
else
{
if (parserOptions & XML_PARSE_NONET)
{
LibXML_old_ext_ent_loader = xmlGetExternalEntityLoader();
xmlSetExternalEntityLoader( xmlNoNetExternalEntityLoader );
}
/* LibXML_old_ext_ent_loader = NULL; */
}
}
}
return real_obj;
}
void
LibXML_cleanup_parser() {
#ifndef WITH_SERRORS
xmlGetWarningsDefaultValue = 0;
#endif
if (EXTERNAL_ENTITY_LOADER_FUNC == NULL && LibXML_old_ext_ent_loader != NULL)
{
xmlSetExternalEntityLoader( (xmlExternalEntityLoader)LibXML_old_ext_ent_loader );
}
}
int
LibXML_test_node_name( xmlChar * name )
{