forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml.c
1686 lines (1421 loc) · 50.5 KB
/
xml.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
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Stig Sæther Bakken <ssb@php.net> |
| Thies C. Arntzen <thies@thieso.net> |
| Sterling Hughes <sterling@php.net> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "php.h"
#include "zend_variables.h"
#include "zend_attributes.h"
#include "ext/standard/info.h"
#include "ext/standard/html.h" /* For php_next_utf8_char() */
#ifdef HAVE_XML
#include "php_xml.h"
#ifdef LIBXML_EXPAT_COMPAT
#include "ext/libxml/php_libxml.h"
#endif
#include "xml_arginfo.h"
/* Short-term TODO list:
* - Implement XML_ExternalEntityParserCreate()
* - XML_SetCommentHandler
* - XML_SetCdataSectionHandler
* - XML_SetParamEntityParsing
*/
/* Long-term TODO list:
* - Fix the expat library so you can install your own memory manager
* functions
*/
/* Known bugs:
* - Weird things happen with <![CDATA[]]> sections.
*/
ZEND_BEGIN_MODULE_GLOBALS(xml)
XML_Char *default_encoding;
ZEND_END_MODULE_GLOBALS(xml)
ZEND_DECLARE_MODULE_GLOBALS(xml)
#define XML(v) ZEND_MODULE_GLOBALS_ACCESSOR(xml, v)
typedef struct {
XML_Parser parser;
XML_Char *target_encoding;
/* Reference to the object itself, for convenience.
* It is not owned, do not release it. */
zval index;
zend_object *object;
zend_fcall_info_cache startElementHandler;
zend_fcall_info_cache endElementHandler;
zend_fcall_info_cache characterDataHandler;
zend_fcall_info_cache processingInstructionHandler;
zend_fcall_info_cache defaultHandler;
zend_fcall_info_cache unparsedEntityDeclHandler;
zend_fcall_info_cache notationDeclHandler;
zend_fcall_info_cache externalEntityRefHandler;
zend_fcall_info_cache startNamespaceDeclHandler;
zend_fcall_info_cache endNamespaceDeclHandler;
zval data;
zval info;
int level;
int toffset;
int curtag;
zend_long ctag_index;
zend_string **ltags;
bool lastwasopen;
bool skipwhite;
bool isparsing;
bool parsehuge;
bool case_folding;
XML_Char *baseURI;
zend_object std;
} xml_parser;
typedef struct {
XML_Char *name;
char (*decoding_function)(unsigned short);
unsigned short (*encoding_function)(unsigned char);
} xml_encoding;
/* {{{ dynamically loadable module stuff */
#ifdef COMPILE_DL_XML
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(xml)
#endif /* COMPILE_DL_XML */
/* }}} */
#define XML_MAXLEVEL 255 /* XXX this should be dynamic */
static zend_class_entry *xml_parser_ce;
static zend_object_handlers xml_parser_object_handlers;
/* {{{ function prototypes */
PHP_MINIT_FUNCTION(xml);
PHP_MINFO_FUNCTION(xml);
static PHP_GINIT_FUNCTION(xml);
static zend_object *xml_parser_create_object(zend_class_entry *class_type);
static void xml_parser_free_obj(zend_object *object);
static HashTable *xml_parser_get_gc(zend_object *object, zval **table, int *n);
static zend_function *xml_parser_get_constructor(zend_object *object);
static zend_string *xml_utf8_decode(const XML_Char *, size_t, const XML_Char *);
inline static unsigned short xml_encode_iso_8859_1(unsigned char);
inline static char xml_decode_iso_8859_1(unsigned short);
inline static unsigned short xml_encode_us_ascii(unsigned char);
inline static char xml_decode_us_ascii(unsigned short);
static void xml_xmlchar_zval(const XML_Char *, int, const XML_Char *, zval *);
static int xml_xmlcharlen(const XML_Char *);
static void xml_add_to_info(xml_parser *parser, zend_string *name);
inline static zend_string *xml_decode_tag(xml_parser *parser, const XML_Char *tag);
void xml_startElementHandler(void *, const XML_Char *, const XML_Char **);
void xml_endElementHandler(void *, const XML_Char *);
void xml_characterDataHandler(void *, const XML_Char *, int);
void xml_processingInstructionHandler(void *, const XML_Char *, const XML_Char *);
void xml_defaultHandler(void *, const XML_Char *, int);
void xml_unparsedEntityDeclHandler(void *, const XML_Char *, const XML_Char *, const XML_Char *, const XML_Char *, const XML_Char *);
void xml_notationDeclHandler(void *, const XML_Char *, const XML_Char *, const XML_Char *, const XML_Char *);
int xml_externalEntityRefHandler(XML_Parser, const XML_Char *, const XML_Char *, const XML_Char *, const XML_Char *);
void xml_startNamespaceDeclHandler(void *, const XML_Char *, const XML_Char *);
void xml_endNamespaceDeclHandler(void *, const XML_Char *);
/* }}} */
#ifdef LIBXML_EXPAT_COMPAT
static const zend_module_dep xml_deps[] = {
ZEND_MOD_REQUIRED("libxml")
ZEND_MOD_END
};
#endif
zend_module_entry xml_module_entry = {
#ifdef LIBXML_EXPAT_COMPAT
STANDARD_MODULE_HEADER_EX, NULL,
xml_deps,
#else
STANDARD_MODULE_HEADER,
#endif
"xml", /* extension name */
ext_functions, /* extension function list */
PHP_MINIT(xml), /* extension-wide startup function */
NULL, /* extension-wide shutdown function */
NULL, /* per-request startup function */
NULL, /* per-request shutdown function */
PHP_MINFO(xml), /* information function */
PHP_XML_VERSION,
PHP_MODULE_GLOBALS(xml), /* globals descriptor */
PHP_GINIT(xml), /* globals ctor */
NULL, /* globals dtor */
NULL, /* post deactivate */
STANDARD_MODULE_PROPERTIES_EX
};
/* All the encoding functions are set to NULL right now, since all
* the encoding is currently done internally by expat/xmltok.
*/
static const xml_encoding xml_encodings[] = {
{ (XML_Char *)"ISO-8859-1", xml_decode_iso_8859_1, xml_encode_iso_8859_1 },
{ (XML_Char *)"US-ASCII", xml_decode_us_ascii, xml_encode_us_ascii },
{ (XML_Char *)"UTF-8", NULL, NULL },
{ (XML_Char *)NULL, NULL, NULL }
};
static XML_Memory_Handling_Suite php_xml_mem_hdlrs;
/* }}} */
/* {{{ startup, shutdown and info functions */
static PHP_GINIT_FUNCTION(xml)
{
#if defined(COMPILE_DL_XML) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
xml_globals->default_encoding = (XML_Char*)"UTF-8";
}
static void *php_xml_malloc_wrapper(size_t sz)
{
return emalloc(sz);
}
static void *php_xml_realloc_wrapper(void *ptr, size_t sz)
{
return erealloc(ptr, sz);
}
static void php_xml_free_wrapper(void *ptr)
{
if (ptr != NULL) {
efree(ptr);
}
}
PHP_MINIT_FUNCTION(xml)
{
xml_parser_ce = register_class_XMLParser();
xml_parser_ce->create_object = xml_parser_create_object;
xml_parser_ce->default_object_handlers = &xml_parser_object_handlers;
memcpy(&xml_parser_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
xml_parser_object_handlers.offset = XtOffsetOf(xml_parser, std);
xml_parser_object_handlers.free_obj = xml_parser_free_obj;
xml_parser_object_handlers.get_gc = xml_parser_get_gc;
xml_parser_object_handlers.get_constructor = xml_parser_get_constructor;
xml_parser_object_handlers.clone_obj = NULL;
xml_parser_object_handlers.compare = zend_objects_not_comparable;
register_xml_symbols(module_number);
/* this object should not be pre-initialised at compile time,
as the order of members may vary */
php_xml_mem_hdlrs.malloc_fcn = php_xml_malloc_wrapper;
php_xml_mem_hdlrs.realloc_fcn = php_xml_realloc_wrapper;
php_xml_mem_hdlrs.free_fcn = php_xml_free_wrapper;
return SUCCESS;
}
PHP_MINFO_FUNCTION(xml)
{
php_info_print_table_start();
php_info_print_table_row(2, "XML Support", "active");
php_info_print_table_row(2, "XML Namespace Support", "active");
#if defined(LIBXML_DOTTED_VERSION) && defined(LIBXML_EXPAT_COMPAT)
php_info_print_table_row(2, "libxml2 Version", LIBXML_DOTTED_VERSION);
#else
php_info_print_table_row(2, "EXPAT Version", XML_ExpatVersion());
#endif
php_info_print_table_end();
}
/* }}} */
/* {{{ extension-internal functions */
static int xml_parse_helper(xml_parser *parser, const char *data, size_t data_len, bool is_final)
{
ZEND_ASSERT(!parser->isparsing);
/* libxml2 specific options */
#ifdef LIBXML_EXPAT_COMPAT
/* TODO: In libxml2 2.14.0 change this to the new options API so we don't rely on deprecated APIs. */
ZEND_DIAGNOSTIC_IGNORED_START("-Wdeprecated-declarations")
/* See xmlInitSAXParserCtxt() and xmlCtxtUseOptions() */
if (parser->parsehuge) {
parser->parser->parser->options |= XML_PARSE_HUGE;
xmlDictSetLimit(parser->parser->parser->dict, 0);
} else {
parser->parser->parser->options &= ~XML_PARSE_HUGE;
xmlDictSetLimit(parser->parser->parser->dict, XML_MAX_DICTIONARY_LIMIT);
}
ZEND_DIAGNOSTIC_IGNORED_END
#endif
parser->isparsing = 1;
int ret = XML_Parse(parser->parser, (const XML_Char *) data, data_len, is_final);
parser->isparsing = 0;
return ret;
}
static void xml_xmlchar_zval(const XML_Char *s, int len, const XML_Char *encoding, zval *ret)
{
if (s == NULL) {
ZVAL_FALSE(ret);
return;
}
if (len == 0) {
len = xml_xmlcharlen(s);
}
ZVAL_STR(ret, xml_utf8_decode(s, len, encoding));
}
/* }}} */
static inline xml_parser *xml_parser_from_obj(zend_object *obj) {
return (xml_parser *)((char *)(obj) - XtOffsetOf(xml_parser, std));
}
#define Z_XMLPARSER_P(zv) xml_parser_from_obj(Z_OBJ_P(zv))
static zend_object *xml_parser_create_object(zend_class_entry *class_type) {
xml_parser *intern = zend_object_alloc(sizeof(xml_parser), class_type);
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
return &intern->std;
}
static void xml_parser_free_ltags(xml_parser *parser)
{
if (parser->ltags) {
int inx;
for (inx = 0; ((inx < parser->level) && (inx < XML_MAXLEVEL)); inx++) {
if (parser->ltags[inx]) {
zend_string_release_ex(parser->ltags[inx], false);
}
}
efree(parser->ltags);
}
}
static void xml_parser_free_obj(zend_object *object)
{
xml_parser *parser = xml_parser_from_obj(object);
zval_ptr_dtor(&parser->info);
zval_ptr_dtor(&parser->data);
if (parser->parser) {
XML_ParserFree(parser->parser);
}
xml_parser_free_ltags(parser);
if (ZEND_FCC_INITIALIZED(parser->startElementHandler)) {
zend_fcc_dtor(&parser->startElementHandler);
parser->startElementHandler.function_handler = NULL;
}
if (ZEND_FCC_INITIALIZED(parser->endElementHandler)) {
zend_fcc_dtor(&parser->endElementHandler);
parser->endElementHandler.function_handler = NULL;
}
if (ZEND_FCC_INITIALIZED(parser->characterDataHandler)) {
zend_fcc_dtor(&parser->characterDataHandler);
parser->characterDataHandler.function_handler = NULL;
}
if (ZEND_FCC_INITIALIZED(parser->processingInstructionHandler)) {
zend_fcc_dtor(&parser->processingInstructionHandler);
parser->processingInstructionHandler.function_handler = NULL;
}
if (ZEND_FCC_INITIALIZED(parser->defaultHandler)) {
zend_fcc_dtor(&parser->defaultHandler);
parser->defaultHandler.function_handler = NULL;
}
if (ZEND_FCC_INITIALIZED(parser->unparsedEntityDeclHandler)) {
zend_fcc_dtor(&parser->unparsedEntityDeclHandler);
parser->unparsedEntityDeclHandler.function_handler = NULL;
}
if (ZEND_FCC_INITIALIZED(parser->notationDeclHandler)) {
zend_fcc_dtor(&parser->notationDeclHandler);
parser->notationDeclHandler.function_handler = NULL;
}
if (ZEND_FCC_INITIALIZED(parser->externalEntityRefHandler)) {
zend_fcc_dtor(&parser->externalEntityRefHandler);
parser->externalEntityRefHandler.function_handler = NULL;
}
if (ZEND_FCC_INITIALIZED(parser->startNamespaceDeclHandler)) {
zend_fcc_dtor(&parser->startNamespaceDeclHandler);
parser->startNamespaceDeclHandler.function_handler = NULL;
}
if (ZEND_FCC_INITIALIZED(parser->endNamespaceDeclHandler)) {
zend_fcc_dtor(&parser->endNamespaceDeclHandler);
parser->endNamespaceDeclHandler.function_handler = NULL;
}
if (parser->baseURI) {
efree(parser->baseURI);
}
if (parser->object) {
OBJ_RELEASE(parser->object);
}
zend_object_std_dtor(&parser->std);
}
static HashTable *xml_parser_get_gc(zend_object *object, zval **table, int *n)
{
xml_parser *parser = xml_parser_from_obj(object);
zend_get_gc_buffer *gc_buffer = zend_get_gc_buffer_create();
if (parser->object) {
zend_get_gc_buffer_add_obj(gc_buffer, parser->object);
}
if (ZEND_FCC_INITIALIZED(parser->startElementHandler)) {
zend_get_gc_buffer_add_fcc(gc_buffer, &parser->startElementHandler);
}
if (ZEND_FCC_INITIALIZED(parser->endElementHandler)) {
zend_get_gc_buffer_add_fcc(gc_buffer, &parser->endElementHandler);
}
if (ZEND_FCC_INITIALIZED(parser->characterDataHandler)) {
zend_get_gc_buffer_add_fcc(gc_buffer, &parser->characterDataHandler);
}
if (ZEND_FCC_INITIALIZED(parser->processingInstructionHandler)) {
zend_get_gc_buffer_add_fcc(gc_buffer, &parser->processingInstructionHandler);
}
if (ZEND_FCC_INITIALIZED(parser->defaultHandler)) {
zend_get_gc_buffer_add_fcc(gc_buffer, &parser->defaultHandler);
}
if (ZEND_FCC_INITIALIZED(parser->unparsedEntityDeclHandler)) {
zend_get_gc_buffer_add_fcc(gc_buffer, &parser->unparsedEntityDeclHandler);
}
if (ZEND_FCC_INITIALIZED(parser->notationDeclHandler)) {
zend_get_gc_buffer_add_fcc(gc_buffer, &parser->notationDeclHandler);
}
if (ZEND_FCC_INITIALIZED(parser->externalEntityRefHandler)) {
zend_get_gc_buffer_add_fcc(gc_buffer, &parser->externalEntityRefHandler);
}
if (ZEND_FCC_INITIALIZED(parser->startNamespaceDeclHandler)) {
zend_get_gc_buffer_add_fcc(gc_buffer, &parser->startNamespaceDeclHandler);
}
if (ZEND_FCC_INITIALIZED(parser->endNamespaceDeclHandler)) {
zend_get_gc_buffer_add_fcc(gc_buffer, &parser->endNamespaceDeclHandler);
}
zend_get_gc_buffer_add_zval(gc_buffer, &parser->data);
zend_get_gc_buffer_add_zval(gc_buffer, &parser->info);
zend_get_gc_buffer_use(gc_buffer, table, n);
return zend_std_get_properties(object);
}
static zend_function *xml_parser_get_constructor(zend_object *object) {
zend_throw_error(NULL, "Cannot directly construct XMLParser, use xml_parser_create() or xml_parser_create_ns() instead");
return NULL;
}
/* This is always called to simplify the mess to deal with BC breaks, but only set a new handler if it is initialized */
static void xml_set_handler(zend_fcall_info_cache *const parser_handler, const zend_fcall_info_cache *const fn)
{
/* If we have already a handler, release it */
if (ZEND_FCC_INITIALIZED(*parser_handler)) {
zend_fcc_dtor(parser_handler);
parser_handler->function_handler = NULL;
}
if (ZEND_FCC_INITIALIZED(*fn)) {
zend_fcc_dup(parser_handler, fn);
}
}
/* {{{ xml_encode_iso_8859_1() */
inline static unsigned short xml_encode_iso_8859_1(unsigned char c)
{
return (unsigned short)c;
}
/* }}} */
/* {{{ xml_decode_iso_8859_1() */
inline static char xml_decode_iso_8859_1(unsigned short c)
{
return (char)(c > 0xff ? '?' : c);
}
/* }}} */
/* {{{ xml_encode_us_ascii() */
inline static unsigned short xml_encode_us_ascii(unsigned char c)
{
return (unsigned short)c;
}
/* }}} */
/* {{{ xml_decode_us_ascii() */
inline static char xml_decode_us_ascii(unsigned short c)
{
return (char)(c > 0x7f ? '?' : c);
}
/* }}} */
/* {{{ xml_get_encoding() */
static const xml_encoding *xml_get_encoding(const XML_Char *name)
{
const xml_encoding *enc = &xml_encodings[0];
while (enc && enc->name) {
if (strcasecmp((char *)name, (char *)enc->name) == 0) {
return enc;
}
enc++;
}
return NULL;
}
/* }}} */
/* {{{ xml_utf8_decode() */
static zend_string *xml_utf8_decode(const XML_Char *s, size_t len, const XML_Char *encoding)
{
size_t pos = 0;
unsigned int c;
char (*decoder)(unsigned short) = NULL;
const xml_encoding *enc = xml_get_encoding(encoding);
zend_string *str;
if (enc) {
decoder = enc->decoding_function;
}
if (decoder == NULL) {
/* If the target encoding was unknown, or no decoder function
* was specified, return the UTF-8-encoded data as-is.
*/
str = zend_string_init((char *)s, len, 0);
return str;
}
str = zend_string_alloc(len, 0);
ZSTR_LEN(str) = 0;
while (pos < len) {
zend_result status = FAILURE;
c = php_next_utf8_char((const unsigned char*)s, len, &pos, &status);
if (status == FAILURE || c > 0xFFU) {
c = '?';
}
ZSTR_VAL(str)[ZSTR_LEN(str)++] = decoder(c);
}
ZSTR_VAL(str)[ZSTR_LEN(str)] = '\0';
if (ZSTR_LEN(str) < len) {
str = zend_string_truncate(str, ZSTR_LEN(str), 0);
}
return str;
}
/* }}} */
/* {{{ xml_xmlcharlen() */
static int xml_xmlcharlen(const XML_Char *s)
{
int len = 0;
while (*s) {
len++;
s++;
}
return len;
}
/* }}} */
/* {{{ xml_add_to_info() */
static void xml_add_to_info(xml_parser *parser, zend_string *name)
{
zval *element;
if (Z_ISUNDEF(parser->info) || UNEXPECTED(Z_TYPE_P(Z_REFVAL(parser->info)) != IS_ARRAY)) {
return;
}
SEPARATE_ARRAY(Z_REFVAL(parser->info));
zend_array *arr = Z_ARRVAL_P(Z_REFVAL(parser->info));
if ((element = zend_hash_find(arr, name)) == NULL) {
zval values;
array_init(&values);
element = zend_hash_update(arr, name, &values);
}
add_next_index_long(element, parser->curtag);
parser->curtag++;
}
/* }}} */
/* {{{ xml_decode_tag() */
static zend_string *xml_decode_tag(xml_parser *parser, const XML_Char *tag)
{
zend_string *str;
str = xml_utf8_decode(tag, xml_xmlcharlen(tag), parser->target_encoding);
if (parser->case_folding) {
zend_str_toupper(ZSTR_VAL(str), ZSTR_LEN(str));
}
return str;
}
/* }}} */
static zend_string *xml_stripped_tag(zend_string *tag_name, int offset)
{
if (offset == 0) {
return zend_string_copy(tag_name);
} else if (offset >= ZSTR_LEN(tag_name)) {
return ZSTR_EMPTY_ALLOC();
} else {
return zend_string_init(ZSTR_VAL(tag_name) + offset, ZSTR_LEN(tag_name) - offset, false);
}
}
static zval *xml_get_separated_data(xml_parser *parser)
{
if (EXPECTED(Z_TYPE_P(Z_REFVAL(parser->data)) == IS_ARRAY)) {
SEPARATE_ARRAY(Z_REFVAL(parser->data));
return Z_REFVAL(parser->data);
}
return NULL;
}
static zval *xml_get_ctag(xml_parser *parser)
{
zval *data = xml_get_separated_data(parser);
if (EXPECTED(data)) {
zval *zv = zend_hash_index_find_deref(Z_ARRVAL_P(data), parser->ctag_index);
if (EXPECTED(zv && Z_TYPE_P(zv) == IS_ARRAY)) {
SEPARATE_ARRAY(zv);
return zv;
}
}
return NULL;
}
/* {{{ xml_startElementHandler() */
void xml_startElementHandler(void *userData, const XML_Char *name, const XML_Char **attributes)
{
xml_parser *parser = (xml_parser *)userData;
const char **attrs = (const char **) attributes;
zend_string *att, *tag_name, *val;
if (!parser) {
return;
}
parser->level++;
tag_name = xml_decode_tag(parser, name);
if (ZEND_FCC_INITIALIZED(parser->startElementHandler)) {
zval args[3];
ZVAL_COPY(&args[0], &parser->index);
ZVAL_STR(&args[1], xml_stripped_tag(tag_name, parser->toffset));
array_init(&args[2]);
while (attributes && *attributes) {
zval tmp;
att = xml_decode_tag(parser, attributes[0]);
val = xml_utf8_decode(attributes[1], strlen((char *)attributes[1]), parser->target_encoding);
ZVAL_STR(&tmp, val);
zend_symtable_update(Z_ARRVAL(args[2]), att, &tmp);
attributes += 2;
zend_string_release_ex(att, 0);
}
zend_call_known_fcc(&parser->startElementHandler, /* retval */ NULL, /* param_count */ 3, args, /* named_params */ NULL);
zval_ptr_dtor(&args[0]);
zval_ptr_dtor_str(&args[1]);
zval_ptr_dtor(&args[2]);
}
if (!Z_ISUNDEF(parser->data) && !EG(exception)) {
if (parser->level <= XML_MAXLEVEL) {
zval tag, atr;
int atcnt = 0;
array_init(&tag);
array_init(&atr);
zend_string *stripped_tag = xml_stripped_tag(tag_name, parser->toffset);
xml_add_to_info(parser, stripped_tag);
add_assoc_str(&tag, "tag", stripped_tag); /* transfer lifetime */
add_assoc_string(&tag, "type", "open");
add_assoc_long(&tag, "level", parser->level);
/* Because toffset may change, we should use the original tag name */
parser->ltags[parser->level - 1] = zend_string_copy(tag_name);
parser->lastwasopen = 1;
attributes = (const XML_Char **) attrs;
while (attributes && *attributes) {
zval tmp;
att = xml_decode_tag(parser, attributes[0]);
val = xml_utf8_decode(attributes[1], strlen((char *)attributes[1]), parser->target_encoding);
ZVAL_STR(&tmp, val);
zend_symtable_update(Z_ARRVAL(atr), att, &tmp);
atcnt++;
attributes += 2;
zend_string_release_ex(att, 0);
}
if (atcnt) {
zend_hash_str_add(Z_ARRVAL(tag), "attributes", sizeof("attributes") - 1, &atr);
} else {
zval_ptr_dtor(&atr);
}
zval *data = xml_get_separated_data(parser);
if (EXPECTED(data)) {
/* Note: due to array resizes or user interference,
* we have to store an index instead of a zval into the array's memory. */
zend_array *arr = Z_ARRVAL_P(data);
if (EXPECTED(zend_hash_next_index_insert(arr, &tag))) {
parser->ctag_index = arr->nNextFreeElement - 1;
} else {
zval_ptr_dtor(&tag);
}
} else {
zval_ptr_dtor(&tag);
}
} else if (parser->level == (XML_MAXLEVEL + 1)) {
php_error_docref(NULL, E_WARNING, "Maximum depth exceeded - Results truncated");
}
}
zend_string_release_ex(tag_name, 0);
}
/* }}} */
/* {{{ xml_endElementHandler() */
void xml_endElementHandler(void *userData, const XML_Char *name)
{
xml_parser *parser = (xml_parser *)userData;
if (!parser) {
return;
}
zend_string *tag_name = xml_decode_tag(parser, name);
if (ZEND_FCC_INITIALIZED(parser->endElementHandler)) {
zval args[2];
ZVAL_COPY(&args[0], &parser->index);
ZVAL_STR(&args[1], xml_stripped_tag(tag_name, parser->toffset));
zend_call_known_fcc(&parser->endElementHandler, /* retval */ NULL, /* param_count */ 2, args, /* named_params */ NULL);
zval_ptr_dtor(&args[0]);
zval_ptr_dtor_str(&args[1]);
}
if (!Z_ISUNDEF(parser->data) && !EG(exception)) {
zval tag;
if (parser->lastwasopen) {
zval *zv = xml_get_ctag(parser);
if (EXPECTED(zv)) {
add_assoc_string(zv, "type", "complete");
}
} else {
zend_string *stripped_tag = xml_stripped_tag(tag_name, parser->toffset);
xml_add_to_info(parser, stripped_tag);
zval *data = xml_get_separated_data(parser);
if (EXPECTED(data)) {
array_init(&tag);
add_assoc_str(&tag, "tag", stripped_tag); /* transfer lifetime */
add_assoc_string(&tag, "type", "close");
add_assoc_long(&tag, "level", parser->level);
zend_hash_next_index_insert(Z_ARRVAL_P(data), &tag);
} else {
zend_string_release_ex(stripped_tag, false);
}
}
parser->lastwasopen = 0;
}
zend_string_release_ex(tag_name, 0);
if ((parser->ltags) && (parser->level <= XML_MAXLEVEL)) {
zend_string **str = &parser->ltags[parser->level - 1];
if (*str) {
zend_string_release_ex(*str, false);
*str = NULL;
}
}
parser->level--;
}
/* }}} */
/* {{{ xml_characterDataHandler() */
void xml_characterDataHandler(void *userData, const XML_Char *s, int len)
{
xml_parser *parser = (xml_parser *)userData;
if (!parser) {
return;
}
if (ZEND_FCC_INITIALIZED(parser->characterDataHandler)) {
zval args[2];
ZVAL_COPY(&args[0], &parser->index);
xml_xmlchar_zval(s, len, parser->target_encoding, &args[1]);
zend_call_known_fcc(&parser->characterDataHandler, /* retval */ NULL, /* param_count */ 2, args, /* named_params */ NULL);
zval_ptr_dtor(&args[0]);
zval_ptr_dtor_str(&args[1]);
}
if (Z_ISUNDEF(parser->data) || EG(exception)) {
return;
}
bool doprint = 0;
zend_string *decoded_value;
decoded_value = xml_utf8_decode(s, len, parser->target_encoding);
if (parser->skipwhite) {
for (size_t i = 0; i < ZSTR_LEN(decoded_value); i++) {
switch (ZSTR_VAL(decoded_value)[i]) {
case ' ':
case '\t':
case '\n':
continue;
default:
doprint = 1;
break;
}
if (doprint) {
break;
}
}
}
if (parser->lastwasopen) {
zval *ctag = xml_get_ctag(parser);
if (UNEXPECTED(!ctag)) {
zend_string_release_ex(decoded_value, false);
return;
}
zval *myval;
/* check if the current tag already has a value - if yes append to that! */
if ((myval = zend_hash_find(Z_ARRVAL_P(ctag), ZSTR_KNOWN(ZEND_STR_VALUE))) && Z_TYPE_P(myval) == IS_STRING) {
size_t newlen = Z_STRLEN_P(myval) + ZSTR_LEN(decoded_value);
Z_STR_P(myval) = zend_string_extend(Z_STR_P(myval), newlen, 0);
strncpy(Z_STRVAL_P(myval) + Z_STRLEN_P(myval) - ZSTR_LEN(decoded_value),
ZSTR_VAL(decoded_value), ZSTR_LEN(decoded_value) + 1);
zend_string_release_ex(decoded_value, 0);
} else {
if (doprint || (! parser->skipwhite)) {
add_assoc_str(ctag, "value", decoded_value);
} else {
zend_string_release_ex(decoded_value, 0);
}
}
} else {
zval tag;
zval *curtag, *mytype, *myval;
zval *data = xml_get_separated_data(parser);
if (UNEXPECTED(!data)) {
zend_string_release_ex(decoded_value, false);
return;
}
ZEND_HASH_REVERSE_FOREACH_VAL(Z_ARRVAL_P(data), curtag) {
if (EXPECTED(Z_TYPE_P(curtag) == IS_ARRAY) && (mytype = zend_hash_str_find(Z_ARRVAL_P(curtag),"type", sizeof("type") - 1))) {
if (EXPECTED(Z_TYPE_P(mytype) == IS_STRING) && zend_string_equals_literal(Z_STR_P(mytype), "cdata")) {
SEPARATE_ARRAY(curtag);
if ((myval = zend_hash_find(Z_ARRVAL_P(curtag), ZSTR_KNOWN(ZEND_STR_VALUE)))) {
size_t newlen = Z_STRLEN_P(myval) + ZSTR_LEN(decoded_value);
Z_STR_P(myval) = zend_string_extend(Z_STR_P(myval), newlen, 0);
strncpy(Z_STRVAL_P(myval) + Z_STRLEN_P(myval) - ZSTR_LEN(decoded_value),
ZSTR_VAL(decoded_value), ZSTR_LEN(decoded_value) + 1);
zend_string_release_ex(decoded_value, 0);
return;
}
}
}
break;
} ZEND_HASH_FOREACH_END();
if (parser->level <= XML_MAXLEVEL && parser->level > 0 && (doprint || (! parser->skipwhite))) {
array_init(&tag);
zend_string *stripped_tag = xml_stripped_tag(parser->ltags[parser->level - 1], parser->toffset);
xml_add_to_info(parser, stripped_tag);
add_assoc_str(&tag, "tag", stripped_tag); /* transfer lifetime */
add_assoc_str(&tag, "value", decoded_value);
add_assoc_string(&tag, "type", "cdata");
add_assoc_long(&tag, "level", parser->level);
zend_hash_next_index_insert(Z_ARRVAL_P(data), &tag);
} else if (parser->level == (XML_MAXLEVEL + 1)) {
php_error_docref(NULL, E_WARNING, "Maximum depth exceeded - Results truncated");
} else {
zend_string_release_ex(decoded_value, 0);
}
}
}
/* }}} */
/* {{{ xml_processingInstructionHandler() */
void xml_processingInstructionHandler(void *userData, const XML_Char *target, const XML_Char *data)
{
xml_parser *parser = (xml_parser *)userData;
if (!parser || !ZEND_FCC_INITIALIZED(parser->processingInstructionHandler)) {
return;
}
zval args[3];
ZVAL_COPY(&args[0], &parser->index);
xml_xmlchar_zval(target, 0, parser->target_encoding, &args[1]);
xml_xmlchar_zval(data, 0, parser->target_encoding, &args[2]);
zend_call_known_fcc(&parser->processingInstructionHandler, /* retval */ NULL, /* param_count */ 3, args, /* named_params */ NULL);
zval_ptr_dtor(&args[0]);
zval_ptr_dtor_str(&args[1]);
zval_ptr_dtor_str(&args[2]);
}
/* }}} */
/* {{{ xml_defaultHandler() */
void xml_defaultHandler(void *userData, const XML_Char *s, int len)
{
xml_parser *parser = (xml_parser *)userData;
if (!parser || !ZEND_FCC_INITIALIZED(parser->defaultHandler)) {
return;
}
zval args[2];
ZVAL_COPY(&args[0], &parser->index);
xml_xmlchar_zval(s, len, parser->target_encoding, &args[1]);
zend_call_known_fcc(&parser->defaultHandler, /* retval */ NULL, /* param_count */ 2, args, /* named_params */ NULL);
zval_ptr_dtor(&args[0]);
zval_ptr_dtor_str(&args[1]);
}
/* }}} */
/* {{{ xml_unparsedEntityDeclHandler() */
void xml_unparsedEntityDeclHandler(void *userData,
const XML_Char *entityName, const XML_Char *base, const XML_Char *systemId,
const XML_Char *publicId, const XML_Char *notationName)
{
xml_parser *parser = (xml_parser *)userData;
if (!parser || !ZEND_FCC_INITIALIZED(parser->unparsedEntityDeclHandler)) {
return;
}
zval args[6];
ZVAL_COPY(&args[0], &parser->index);
xml_xmlchar_zval(entityName, 0, parser->target_encoding, &args[1]);
xml_xmlchar_zval(base, 0, parser->target_encoding, &args[2]);
xml_xmlchar_zval(systemId, 0, parser->target_encoding, &args[3]);
xml_xmlchar_zval(publicId, 0, parser->target_encoding, &args[4]);
xml_xmlchar_zval(notationName, 0, parser->target_encoding, &args[5]);
zend_call_known_fcc(&parser->unparsedEntityDeclHandler, /* retval */ NULL, /* param_count */ 6, args, /* named_params */ NULL);
zval_ptr_dtor(&args[0]);
zval_ptr_dtor_str(&args[1]);
zval_ptr_dtor_str(&args[2]);
zval_ptr_dtor_str(&args[3]);
zval_ptr_dtor_str(&args[4]);
zval_ptr_dtor_str(&args[5]);
}
/* }}} */
/* {{{ xml_notationDeclHandler() */
void xml_notationDeclHandler(void *userData, const XML_Char *notationName,
const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId)
{
xml_parser *parser = (xml_parser *)userData;
if (!parser || !ZEND_FCC_INITIALIZED(parser->notationDeclHandler)) {
return;
}
zval args[5];
ZVAL_COPY(&args[0], &parser->index);
xml_xmlchar_zval(notationName, 0, parser->target_encoding, &args[1]);
xml_xmlchar_zval(base, 0, parser->target_encoding, &args[2]);
xml_xmlchar_zval(systemId, 0, parser->target_encoding, &args[3]);
xml_xmlchar_zval(publicId, 0, parser->target_encoding, &args[4]);
zend_call_known_fcc(&parser->notationDeclHandler, /* retval */ NULL, /* param_count */ 5, args, /* named_params */ NULL);
zval_ptr_dtor(&args[0]);
zval_ptr_dtor_str(&args[1]);
zval_ptr_dtor_str(&args[2]);
zval_ptr_dtor_str(&args[3]);
zval_ptr_dtor_str(&args[4]);
}
/* }}} */