1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Stig Sæther Bakken <ssb@php.net> |
14 | Thies C. Arntzen <thies@thieso.net> |
15 | Sterling Hughes <sterling@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "php.h"
24
25 #include "zend_variables.h"
26 #include "ext/standard/info.h"
27 #include "ext/standard/html.h"
28
29 #ifdef HAVE_XML
30
31 #include "php_xml.h"
32 # include "ext/standard/head.h"
33 #ifdef LIBXML_EXPAT_COMPAT
34 #include "ext/libxml/php_libxml.h"
35 #endif
36
37 #include "xml_arginfo.h"
38
39 /* Short-term TODO list:
40 * - Implement XML_ExternalEntityParserCreate()
41 * - XML_SetCommentHandler
42 * - XML_SetCdataSectionHandler
43 * - XML_SetParamEntityParsing
44 */
45
46 /* Long-term TODO list:
47 * - Fix the expat library so you can install your own memory manager
48 * functions
49 */
50
51 /* Known bugs:
52 * - Weird things happen with <![CDATA[]]> sections.
53 */
54
ZEND_BEGIN_MODULE_GLOBALS(xml)55 ZEND_BEGIN_MODULE_GLOBALS(xml)
56 XML_Char *default_encoding;
57 ZEND_END_MODULE_GLOBALS(xml)
58
59 ZEND_DECLARE_MODULE_GLOBALS(xml)
60
61 #define XML(v) ZEND_MODULE_GLOBALS_ACCESSOR(xml, v)
62
63 typedef struct {
64 int case_folding;
65 XML_Parser parser;
66 XML_Char *target_encoding;
67
68 /* Reference to the object itself, for convenience.
69 * It is not owned, do not release it. */
70 zval index;
71
72 /* We return a pointer to these zvals in get_gc(), so it's
73 * important that a) they are adjacent b) object is the first
74 * and c) the number of zvals is kept up to date. */
75 #define XML_PARSER_NUM_ZVALS 12
76 zval object;
77 zval startElementHandler;
78 zval endElementHandler;
79 zval characterDataHandler;
80 zval processingInstructionHandler;
81 zval defaultHandler;
82 zval unparsedEntityDeclHandler;
83 zval notationDeclHandler;
84 zval externalEntityRefHandler;
85 zval unknownEncodingHandler;
86 zval startNamespaceDeclHandler;
87 zval endNamespaceDeclHandler;
88
89 zend_function *startElementPtr;
90 zend_function *endElementPtr;
91 zend_function *characterDataPtr;
92 zend_function *processingInstructionPtr;
93 zend_function *defaultPtr;
94 zend_function *unparsedEntityDeclPtr;
95 zend_function *notationDeclPtr;
96 zend_function *externalEntityRefPtr;
97 zend_function *unknownEncodingPtr;
98 zend_function *startNamespaceDeclPtr;
99 zend_function *endNamespaceDeclPtr;
100
101 zval data;
102 zval info;
103 int level;
104 int toffset;
105 int curtag;
106 zval *ctag;
107 char **ltags;
108 int lastwasopen;
109 int skipwhite;
110 int isparsing;
111
112 XML_Char *baseURI;
113
114 zend_object std;
115 } xml_parser;
116
117
118 typedef struct {
119 XML_Char *name;
120 char (*decoding_function)(unsigned short);
121 unsigned short (*encoding_function)(unsigned char);
122 } xml_encoding;
123
124 /* {{{ dynamically loadable module stuff */
125 #ifdef COMPILE_DL_XML
126 #ifdef ZTS
127 ZEND_TSRMLS_CACHE_DEFINE()
128 #endif
129 ZEND_GET_MODULE(xml)
130 #endif /* COMPILE_DL_XML */
131 /* }}} */
132
133 #define XML_MAXLEVEL 255 /* XXX this should be dynamic */
134
135 #define SKIP_TAGSTART(str) ((str) + (parser->toffset > (int)strlen(str) ? strlen(str) : parser->toffset))
136
137 static zend_class_entry *xml_parser_ce;
138 static zend_object_handlers xml_parser_object_handlers;
139
140 /* {{{ function prototypes */
141 PHP_MINIT_FUNCTION(xml);
142 PHP_MINFO_FUNCTION(xml);
143 static PHP_GINIT_FUNCTION(xml);
144
145 static zend_object *xml_parser_create_object(zend_class_entry *class_type);
146 static void xml_parser_free_obj(zend_object *object);
147 static HashTable *xml_parser_get_gc(zend_object *object, zval **table, int *n);
148 static zend_function *xml_parser_get_constructor(zend_object *object);
149
150 static zend_string *xml_utf8_decode(const XML_Char *, size_t, const XML_Char *);
151 static void xml_set_handler(zval *, zval *);
152 inline static unsigned short xml_encode_iso_8859_1(unsigned char);
153 inline static char xml_decode_iso_8859_1(unsigned short);
154 inline static unsigned short xml_encode_us_ascii(unsigned char);
155 inline static char xml_decode_us_ascii(unsigned short);
156 static void xml_call_handler(xml_parser *, zval *, zend_function *, int, zval *, zval *);
157 static void _xml_xmlchar_zval(const XML_Char *, int, const XML_Char *, zval *);
158 static int _xml_xmlcharlen(const XML_Char *);
159 static void _xml_add_to_info(xml_parser *parser, const char *name);
160 inline static zend_string *_xml_decode_tag(xml_parser *parser, const XML_Char *tag);
161
162 void _xml_startElementHandler(void *, const XML_Char *, const XML_Char **);
163 void _xml_endElementHandler(void *, const XML_Char *);
164 void _xml_characterDataHandler(void *, const XML_Char *, int);
165 void _xml_processingInstructionHandler(void *, const XML_Char *, const XML_Char *);
166 void _xml_defaultHandler(void *, const XML_Char *, int);
167 void _xml_unparsedEntityDeclHandler(void *, const XML_Char *, const XML_Char *, const XML_Char *, const XML_Char *, const XML_Char *);
168 void _xml_notationDeclHandler(void *, const XML_Char *, const XML_Char *, const XML_Char *, const XML_Char *);
169 int _xml_externalEntityRefHandler(XML_Parser, const XML_Char *, const XML_Char *, const XML_Char *, const XML_Char *);
170
171 void _xml_startNamespaceDeclHandler(void *, const XML_Char *, const XML_Char *);
172 void _xml_endNamespaceDeclHandler(void *, const XML_Char *);
173 /* }}} */
174
175 #ifdef LIBXML_EXPAT_COMPAT
176 static const zend_module_dep xml_deps[] = {
177 ZEND_MOD_REQUIRED("libxml")
178 ZEND_MOD_END
179 };
180 #endif
181
182 zend_module_entry xml_module_entry = {
183 #ifdef LIBXML_EXPAT_COMPAT
184 STANDARD_MODULE_HEADER_EX, NULL,
185 xml_deps,
186 #else
187 STANDARD_MODULE_HEADER,
188 #endif
189 "xml", /* extension name */
190 ext_functions, /* extension function list */
191 PHP_MINIT(xml), /* extension-wide startup function */
192 NULL, /* extension-wide shutdown function */
193 NULL, /* per-request startup function */
194 NULL, /* per-request shutdown function */
195 PHP_MINFO(xml), /* information function */
196 PHP_XML_VERSION,
197 PHP_MODULE_GLOBALS(xml), /* globals descriptor */
198 PHP_GINIT(xml), /* globals ctor */
199 NULL, /* globals dtor */
200 NULL, /* post deactivate */
201 STANDARD_MODULE_PROPERTIES_EX
202 };
203
204 /* All the encoding functions are set to NULL right now, since all
205 * the encoding is currently done internally by expat/xmltok.
206 */
207 const xml_encoding xml_encodings[] = {
208 { (XML_Char *)"ISO-8859-1", xml_decode_iso_8859_1, xml_encode_iso_8859_1 },
209 { (XML_Char *)"US-ASCII", xml_decode_us_ascii, xml_encode_us_ascii },
210 { (XML_Char *)"UTF-8", NULL, NULL },
211 { (XML_Char *)NULL, NULL, NULL }
212 };
213
214 static XML_Memory_Handling_Suite php_xml_mem_hdlrs;
215
216 /* }}} */
217
218 /* {{{ startup, shutdown and info functions */
PHP_GINIT_FUNCTION(xml)219 static PHP_GINIT_FUNCTION(xml)
220 {
221 #if defined(COMPILE_DL_XML) && defined(ZTS)
222 ZEND_TSRMLS_CACHE_UPDATE();
223 #endif
224 xml_globals->default_encoding = (XML_Char*)"UTF-8";
225 }
226
php_xml_malloc_wrapper(size_t sz)227 static void *php_xml_malloc_wrapper(size_t sz)
228 {
229 return emalloc(sz);
230 }
231
php_xml_realloc_wrapper(void * ptr,size_t sz)232 static void *php_xml_realloc_wrapper(void *ptr, size_t sz)
233 {
234 return erealloc(ptr, sz);
235 }
236
php_xml_free_wrapper(void * ptr)237 static void php_xml_free_wrapper(void *ptr)
238 {
239 if (ptr != NULL) {
240 efree(ptr);
241 }
242 }
243
PHP_MINIT_FUNCTION(xml)244 PHP_MINIT_FUNCTION(xml)
245 {
246 xml_parser_ce = register_class_XMLParser();
247 xml_parser_ce->create_object = xml_parser_create_object;
248
249 memcpy(&xml_parser_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
250 xml_parser_object_handlers.offset = XtOffsetOf(xml_parser, std);
251 xml_parser_object_handlers.free_obj = xml_parser_free_obj;
252 xml_parser_object_handlers.get_gc = xml_parser_get_gc;
253 xml_parser_object_handlers.get_constructor = xml_parser_get_constructor;
254 xml_parser_object_handlers.clone_obj = NULL;
255 xml_parser_object_handlers.compare = zend_objects_not_comparable;
256
257 register_xml_symbols(module_number);
258
259 /* this object should not be pre-initialised at compile time,
260 as the order of members may vary */
261
262 php_xml_mem_hdlrs.malloc_fcn = php_xml_malloc_wrapper;
263 php_xml_mem_hdlrs.realloc_fcn = php_xml_realloc_wrapper;
264 php_xml_mem_hdlrs.free_fcn = php_xml_free_wrapper;
265
266 #ifdef LIBXML_EXPAT_COMPAT
267 REGISTER_STRING_CONSTANT("XML_SAX_IMPL", "libxml", CONST_CS|CONST_PERSISTENT);
268 #else
269 REGISTER_STRING_CONSTANT("XML_SAX_IMPL", "expat", CONST_CS|CONST_PERSISTENT);
270 #endif
271
272 return SUCCESS;
273 }
274
PHP_MINFO_FUNCTION(xml)275 PHP_MINFO_FUNCTION(xml)
276 {
277 php_info_print_table_start();
278 php_info_print_table_row(2, "XML Support", "active");
279 php_info_print_table_row(2, "XML Namespace Support", "active");
280 #if defined(LIBXML_DOTTED_VERSION) && defined(LIBXML_EXPAT_COMPAT)
281 php_info_print_table_row(2, "libxml2 Version", LIBXML_DOTTED_VERSION);
282 #else
283 php_info_print_table_row(2, "EXPAT Version", XML_ExpatVersion());
284 #endif
285 php_info_print_table_end();
286 }
287 /* }}} */
288
289 /* {{{ extension-internal functions */
290
_xml_xmlchar_zval(const XML_Char * s,int len,const XML_Char * encoding,zval * ret)291 static void _xml_xmlchar_zval(const XML_Char *s, int len, const XML_Char *encoding, zval *ret)
292 {
293 if (s == NULL) {
294 ZVAL_FALSE(ret);
295 return;
296 }
297 if (len == 0) {
298 len = _xml_xmlcharlen(s);
299 }
300 ZVAL_STR(ret, xml_utf8_decode(s, len, encoding));
301 }
302 /* }}} */
303
xml_parser_from_obj(zend_object * obj)304 static inline xml_parser *xml_parser_from_obj(zend_object *obj) {
305 return (xml_parser *)((char *)(obj) - XtOffsetOf(xml_parser, std));
306 }
307
308 #define Z_XMLPARSER_P(zv) xml_parser_from_obj(Z_OBJ_P(zv))
309
xml_parser_create_object(zend_class_entry * class_type)310 static zend_object *xml_parser_create_object(zend_class_entry *class_type) {
311 xml_parser *intern = zend_object_alloc(sizeof(xml_parser), class_type);
312 memset(intern, 0, sizeof(xml_parser) - sizeof(zend_object));
313
314 zend_object_std_init(&intern->std, class_type);
315 object_properties_init(&intern->std, class_type);
316 intern->std.handlers = &xml_parser_object_handlers;
317
318 return &intern->std;
319 }
320
xml_parser_free_ltags(xml_parser * parser)321 static void xml_parser_free_ltags(xml_parser *parser)
322 {
323 if (parser->ltags) {
324 int inx;
325 for (inx = 0; ((inx < parser->level) && (inx < XML_MAXLEVEL)); inx++)
326 efree(parser->ltags[ inx ]);
327 efree(parser->ltags);
328 }
329 }
330
xml_parser_free_obj(zend_object * object)331 static void xml_parser_free_obj(zend_object *object)
332 {
333 xml_parser *parser = xml_parser_from_obj(object);
334
335 if (parser->parser) {
336 XML_ParserFree(parser->parser);
337 }
338 xml_parser_free_ltags(parser);
339 if (!Z_ISUNDEF(parser->startElementHandler)) {
340 zval_ptr_dtor(&parser->startElementHandler);
341 }
342 if (!Z_ISUNDEF(parser->endElementHandler)) {
343 zval_ptr_dtor(&parser->endElementHandler);
344 }
345 if (!Z_ISUNDEF(parser->characterDataHandler)) {
346 zval_ptr_dtor(&parser->characterDataHandler);
347 }
348 if (!Z_ISUNDEF(parser->processingInstructionHandler)) {
349 zval_ptr_dtor(&parser->processingInstructionHandler);
350 }
351 if (!Z_ISUNDEF(parser->defaultHandler)) {
352 zval_ptr_dtor(&parser->defaultHandler);
353 }
354 if (!Z_ISUNDEF(parser->unparsedEntityDeclHandler)) {
355 zval_ptr_dtor(&parser->unparsedEntityDeclHandler);
356 }
357 if (!Z_ISUNDEF(parser->notationDeclHandler)) {
358 zval_ptr_dtor(&parser->notationDeclHandler);
359 }
360 if (!Z_ISUNDEF(parser->externalEntityRefHandler)) {
361 zval_ptr_dtor(&parser->externalEntityRefHandler);
362 }
363 if (!Z_ISUNDEF(parser->unknownEncodingHandler)) {
364 zval_ptr_dtor(&parser->unknownEncodingHandler);
365 }
366 if (!Z_ISUNDEF(parser->startNamespaceDeclHandler)) {
367 zval_ptr_dtor(&parser->startNamespaceDeclHandler);
368 }
369 if (!Z_ISUNDEF(parser->endNamespaceDeclHandler)) {
370 zval_ptr_dtor(&parser->endNamespaceDeclHandler);
371 }
372 if (parser->baseURI) {
373 efree(parser->baseURI);
374 }
375 if (!Z_ISUNDEF(parser->object)) {
376 zval_ptr_dtor(&parser->object);
377 }
378
379 zend_object_std_dtor(&parser->std);
380 }
381
xml_parser_get_gc(zend_object * object,zval ** table,int * n)382 static HashTable *xml_parser_get_gc(zend_object *object, zval **table, int *n)
383 {
384 xml_parser *parser = xml_parser_from_obj(object);
385 *table = &parser->object;
386 *n = XML_PARSER_NUM_ZVALS;
387 return zend_std_get_properties(object);
388 }
389
xml_parser_get_constructor(zend_object * object)390 static zend_function *xml_parser_get_constructor(zend_object *object) {
391 zend_throw_error(NULL, "Cannot directly construct XMLParser, use xml_parser_create() or xml_parser_create_ns() instead");
392 return NULL;
393 }
394
395 /* {{{ xml_set_handler() */
xml_set_handler(zval * handler,zval * data)396 static void xml_set_handler(zval *handler, zval *data)
397 {
398 /* If we have already a handler, release it */
399 if (handler) {
400 zval_ptr_dtor(handler);
401 }
402
403 /* IS_ARRAY might indicate that we're using array($obj, 'method') syntax */
404 if (Z_TYPE_P(data) != IS_ARRAY && Z_TYPE_P(data) != IS_OBJECT) {
405 convert_to_string(data);
406 if (Z_STRLEN_P(data) == 0) {
407 ZVAL_UNDEF(handler);
408 return;
409 }
410 }
411
412 ZVAL_COPY(handler, data);
413 }
414 /* }}} */
415
416 /* {{{ xml_call_handler() */
xml_call_handler(xml_parser * parser,zval * handler,zend_function * function_ptr,int argc,zval * argv,zval * retval)417 static void xml_call_handler(xml_parser *parser, zval *handler, zend_function *function_ptr, int argc, zval *argv, zval *retval)
418 {
419 int i;
420
421 ZVAL_UNDEF(retval);
422 if (parser && handler && !EG(exception)) {
423 int result;
424 zend_fcall_info fci;
425
426 fci.size = sizeof(fci);
427 ZVAL_COPY_VALUE(&fci.function_name, handler);
428 fci.object = Z_OBJ(parser->object);
429 fci.retval = retval;
430 fci.param_count = argc;
431 fci.params = argv;
432 fci.named_params = NULL;
433
434 result = zend_call_function(&fci, NULL);
435 if (result == FAILURE) {
436 zval *method;
437 zval *obj;
438
439 if (Z_TYPE_P(handler) == IS_STRING) {
440 php_error_docref(NULL, E_WARNING, "Unable to call handler %s()", Z_STRVAL_P(handler));
441 } else if (Z_TYPE_P(handler) == IS_ARRAY &&
442 (obj = zend_hash_index_find(Z_ARRVAL_P(handler), 0)) != NULL &&
443 (method = zend_hash_index_find(Z_ARRVAL_P(handler), 1)) != NULL &&
444 Z_TYPE_P(obj) == IS_OBJECT &&
445 Z_TYPE_P(method) == IS_STRING) {
446 php_error_docref(NULL, E_WARNING, "Unable to call handler %s::%s()", ZSTR_VAL(Z_OBJCE_P(obj)->name), Z_STRVAL_P(method));
447 } else
448 php_error_docref(NULL, E_WARNING, "Unable to call handler");
449 }
450 }
451 for (i = 0; i < argc; i++) {
452 zval_ptr_dtor(&argv[i]);
453 }
454 }
455 /* }}} */
456
457 /* {{{ xml_encode_iso_8859_1() */
xml_encode_iso_8859_1(unsigned char c)458 inline static unsigned short xml_encode_iso_8859_1(unsigned char c)
459 {
460 return (unsigned short)c;
461 }
462 /* }}} */
463
464 /* {{{ xml_decode_iso_8859_1() */
xml_decode_iso_8859_1(unsigned short c)465 inline static char xml_decode_iso_8859_1(unsigned short c)
466 {
467 return (char)(c > 0xff ? '?' : c);
468 }
469 /* }}} */
470
471 /* {{{ xml_encode_us_ascii() */
xml_encode_us_ascii(unsigned char c)472 inline static unsigned short xml_encode_us_ascii(unsigned char c)
473 {
474 return (unsigned short)c;
475 }
476 /* }}} */
477
478 /* {{{ xml_decode_us_ascii() */
xml_decode_us_ascii(unsigned short c)479 inline static char xml_decode_us_ascii(unsigned short c)
480 {
481 return (char)(c > 0x7f ? '?' : c);
482 }
483 /* }}} */
484
485 /* {{{ xml_get_encoding() */
xml_get_encoding(const XML_Char * name)486 static const xml_encoding *xml_get_encoding(const XML_Char *name)
487 {
488 const xml_encoding *enc = &xml_encodings[0];
489
490 while (enc && enc->name) {
491 if (strcasecmp((char *)name, (char *)enc->name) == 0) {
492 return enc;
493 }
494 enc++;
495 }
496 return NULL;
497 }
498 /* }}} */
499
500 /* {{{ xml_utf8_decode() */
xml_utf8_decode(const XML_Char * s,size_t len,const XML_Char * encoding)501 static zend_string *xml_utf8_decode(const XML_Char *s, size_t len, const XML_Char *encoding)
502 {
503 size_t pos = 0;
504 unsigned int c;
505 char (*decoder)(unsigned short) = NULL;
506 const xml_encoding *enc = xml_get_encoding(encoding);
507 zend_string *str;
508
509 if (enc) {
510 decoder = enc->decoding_function;
511 }
512
513 if (decoder == NULL) {
514 /* If the target encoding was unknown, or no decoder function
515 * was specified, return the UTF-8-encoded data as-is.
516 */
517 str = zend_string_init((char *)s, len, 0);
518 return str;
519 }
520
521 str = zend_string_alloc(len, 0);
522 ZSTR_LEN(str) = 0;
523 while (pos < len) {
524 zend_result status = FAILURE;
525 c = php_next_utf8_char((const unsigned char*)s, len, &pos, &status);
526
527 if (status == FAILURE || c > 0xFFU) {
528 c = '?';
529 }
530
531 ZSTR_VAL(str)[ZSTR_LEN(str)++] = decoder ? (unsigned int)decoder(c) : c;
532 }
533 ZSTR_VAL(str)[ZSTR_LEN(str)] = '\0';
534 if (ZSTR_LEN(str) < len) {
535 str = zend_string_truncate(str, ZSTR_LEN(str), 0);
536 }
537
538 return str;
539 }
540 /* }}} */
541
542 /* {{{ _xml_xmlcharlen() */
_xml_xmlcharlen(const XML_Char * s)543 static int _xml_xmlcharlen(const XML_Char *s)
544 {
545 int len = 0;
546
547 while (*s) {
548 len++;
549 s++;
550 }
551 return len;
552 }
553 /* }}} */
554
555 /* {{{ _xml_add_to_info() */
_xml_add_to_info(xml_parser * parser,const char * name)556 static void _xml_add_to_info(xml_parser *parser, const char *name)
557 {
558 zval *element;
559
560 if (Z_ISUNDEF(parser->info)) {
561 return;
562 }
563
564 size_t name_len = strlen(name);
565 if ((element = zend_hash_str_find(Z_ARRVAL(parser->info), name, name_len)) == NULL) {
566 zval values;
567 array_init(&values);
568 element = zend_hash_str_update(Z_ARRVAL(parser->info), name, name_len, &values);
569 }
570
571 add_next_index_long(element, parser->curtag);
572
573 parser->curtag++;
574 }
575 /* }}} */
576
577 /* {{{ _xml_decode_tag() */
_xml_decode_tag(xml_parser * parser,const XML_Char * tag)578 static zend_string *_xml_decode_tag(xml_parser *parser, const XML_Char *tag)
579 {
580 zend_string *str;
581
582 str = xml_utf8_decode(tag, _xml_xmlcharlen(tag), parser->target_encoding);
583
584 if (parser->case_folding) {
585 zend_str_toupper(ZSTR_VAL(str), ZSTR_LEN(str));
586 }
587
588 return str;
589 }
590 /* }}} */
591
592 /* {{{ _xml_startElementHandler() */
_xml_startElementHandler(void * userData,const XML_Char * name,const XML_Char ** attributes)593 void _xml_startElementHandler(void *userData, const XML_Char *name, const XML_Char **attributes)
594 {
595 xml_parser *parser = (xml_parser *)userData;
596 const char **attrs = (const char **) attributes;
597 zend_string *att, *tag_name, *val;
598 zval retval, args[3];
599
600 if (!parser) {
601 return;
602 }
603
604 parser->level++;
605
606 tag_name = _xml_decode_tag(parser, name);
607
608 if (!Z_ISUNDEF(parser->startElementHandler)) {
609 ZVAL_COPY(&args[0], &parser->index);
610 ZVAL_STRING(&args[1], SKIP_TAGSTART(ZSTR_VAL(tag_name)));
611 array_init(&args[2]);
612
613 while (attributes && *attributes) {
614 zval tmp;
615
616 att = _xml_decode_tag(parser, attributes[0]);
617 val = xml_utf8_decode(attributes[1], strlen((char *)attributes[1]), parser->target_encoding);
618
619 ZVAL_STR(&tmp, val);
620 zend_symtable_update(Z_ARRVAL(args[2]), att, &tmp);
621
622 attributes += 2;
623
624 zend_string_release_ex(att, 0);
625 }
626
627 xml_call_handler(parser, &parser->startElementHandler, parser->startElementPtr, 3, args, &retval);
628 zval_ptr_dtor(&retval);
629 }
630
631 if (!Z_ISUNDEF(parser->data) && !EG(exception)) {
632 if (parser->level <= XML_MAXLEVEL) {
633 zval tag, atr;
634 int atcnt = 0;
635
636 array_init(&tag);
637 array_init(&atr);
638
639 _xml_add_to_info(parser, ZSTR_VAL(tag_name) + parser->toffset);
640
641 add_assoc_string(&tag, "tag", SKIP_TAGSTART(ZSTR_VAL(tag_name))); /* cast to avoid gcc-warning */
642 add_assoc_string(&tag, "type", "open");
643 add_assoc_long(&tag, "level", parser->level);
644
645 parser->ltags[parser->level-1] = estrdup(ZSTR_VAL(tag_name));
646 parser->lastwasopen = 1;
647
648 attributes = (const XML_Char **) attrs;
649
650 while (attributes && *attributes) {
651 zval tmp;
652
653 att = _xml_decode_tag(parser, attributes[0]);
654 val = xml_utf8_decode(attributes[1], strlen((char *)attributes[1]), parser->target_encoding);
655
656 ZVAL_STR(&tmp, val);
657 zend_symtable_update(Z_ARRVAL(atr), att, &tmp);
658
659 atcnt++;
660 attributes += 2;
661
662 zend_string_release_ex(att, 0);
663 }
664
665 if (atcnt) {
666 zend_hash_str_add(Z_ARRVAL(tag), "attributes", sizeof("attributes") - 1, &atr);
667 } else {
668 zval_ptr_dtor(&atr);
669 }
670
671 parser->ctag = zend_hash_next_index_insert(Z_ARRVAL(parser->data), &tag);
672 } else if (parser->level == (XML_MAXLEVEL + 1)) {
673 php_error_docref(NULL, E_WARNING, "Maximum depth exceeded - Results truncated");
674 }
675 }
676
677 zend_string_release_ex(tag_name, 0);
678 }
679 /* }}} */
680
681 /* {{{ _xml_endElementHandler() */
_xml_endElementHandler(void * userData,const XML_Char * name)682 void _xml_endElementHandler(void *userData, const XML_Char *name)
683 {
684 xml_parser *parser = (xml_parser *)userData;
685
686 if (!parser) {
687 return;
688 }
689
690 zval retval, args[2];
691
692 zend_string *tag_name = _xml_decode_tag(parser, name);
693
694 if (!Z_ISUNDEF(parser->endElementHandler)) {
695 ZVAL_COPY(&args[0], &parser->index);
696 ZVAL_STRING(&args[1], SKIP_TAGSTART(ZSTR_VAL(tag_name)));
697
698 xml_call_handler(parser, &parser->endElementHandler, parser->endElementPtr, 2, args, &retval);
699 zval_ptr_dtor(&retval);
700 }
701
702 if (!Z_ISUNDEF(parser->data) && !EG(exception)) {
703 zval tag;
704
705 if (parser->lastwasopen) {
706 add_assoc_string(parser->ctag, "type", "complete");
707 } else {
708 array_init(&tag);
709
710 _xml_add_to_info(parser, ZSTR_VAL(tag_name) + parser->toffset);
711
712 add_assoc_string(&tag, "tag", SKIP_TAGSTART(ZSTR_VAL(tag_name))); /* cast to avoid gcc-warning */
713 add_assoc_string(&tag, "type", "close");
714 add_assoc_long(&tag, "level", parser->level);
715
716 zend_hash_next_index_insert(Z_ARRVAL(parser->data), &tag);
717 }
718
719 parser->lastwasopen = 0;
720 }
721
722 zend_string_release_ex(tag_name, 0);
723
724 if ((parser->ltags) && (parser->level <= XML_MAXLEVEL)) {
725 efree(parser->ltags[parser->level-1]);
726 }
727
728 parser->level--;
729 }
730 /* }}} */
731
732 /* {{{ _xml_characterDataHandler() */
_xml_characterDataHandler(void * userData,const XML_Char * s,int len)733 void _xml_characterDataHandler(void *userData, const XML_Char *s, int len)
734 {
735 xml_parser *parser = (xml_parser *)userData;
736
737 if (!parser) {
738 return;
739 }
740
741 zval retval, args[2];
742
743 if (!Z_ISUNDEF(parser->characterDataHandler)) {
744 ZVAL_COPY(&args[0], &parser->index);
745 _xml_xmlchar_zval(s, len, parser->target_encoding, &args[1]);
746 xml_call_handler(parser, &parser->characterDataHandler, parser->characterDataPtr, 2, args, &retval);
747 zval_ptr_dtor(&retval);
748 }
749
750 if (Z_ISUNDEF(parser->data) || EG(exception)) {
751 return;
752 }
753
754 bool doprint = 0;
755 zend_string *decoded_value;
756 decoded_value = xml_utf8_decode(s, len, parser->target_encoding);
757 if (parser->skipwhite) {
758 for (size_t i = 0; i < ZSTR_LEN(decoded_value); i++) {
759 switch (ZSTR_VAL(decoded_value)[i]) {
760 case ' ':
761 case '\t':
762 case '\n':
763 continue;
764 default:
765 doprint = 1;
766 break;
767 }
768 if (doprint) {
769 break;
770 }
771 }
772 }
773 if (parser->lastwasopen) {
774 zval *myval;
775 /* check if the current tag already has a value - if yes append to that! */
776 if ((myval = zend_hash_str_find(Z_ARRVAL_P(parser->ctag), "value", sizeof("value") - 1))) {
777 size_t newlen = Z_STRLEN_P(myval) + ZSTR_LEN(decoded_value);
778 Z_STR_P(myval) = zend_string_extend(Z_STR_P(myval), newlen, 0);
779 strncpy(Z_STRVAL_P(myval) + Z_STRLEN_P(myval) - ZSTR_LEN(decoded_value),
780 ZSTR_VAL(decoded_value), ZSTR_LEN(decoded_value) + 1);
781 zend_string_release_ex(decoded_value, 0);
782 } else {
783 if (doprint || (! parser->skipwhite)) {
784 add_assoc_str(parser->ctag, "value", decoded_value);
785 } else {
786 zend_string_release_ex(decoded_value, 0);
787 }
788 }
789 } else {
790 zval tag;
791 zval *curtag, *mytype, *myval;
792 ZEND_HASH_REVERSE_FOREACH_VAL(Z_ARRVAL(parser->data), curtag) {
793 if ((mytype = zend_hash_str_find(Z_ARRVAL_P(curtag),"type", sizeof("type") - 1))) {
794 if (zend_string_equals_literal(Z_STR_P(mytype), "cdata")) {
795 if ((myval = zend_hash_str_find(Z_ARRVAL_P(curtag), "value", sizeof("value") - 1))) {
796 size_t newlen = Z_STRLEN_P(myval) + ZSTR_LEN(decoded_value);
797 Z_STR_P(myval) = zend_string_extend(Z_STR_P(myval), newlen, 0);
798 strncpy(Z_STRVAL_P(myval) + Z_STRLEN_P(myval) - ZSTR_LEN(decoded_value),
799 ZSTR_VAL(decoded_value), ZSTR_LEN(decoded_value) + 1);
800 zend_string_release_ex(decoded_value, 0);
801 return;
802 }
803 }
804 }
805 break;
806 } ZEND_HASH_FOREACH_END();
807 if (parser->level <= XML_MAXLEVEL && parser->level > 0 && (doprint || (! parser->skipwhite))) {
808 array_init(&tag);
809 _xml_add_to_info(parser,SKIP_TAGSTART(parser->ltags[parser->level-1]));
810 add_assoc_string(&tag, "tag", SKIP_TAGSTART(parser->ltags[parser->level-1]));
811 add_assoc_str(&tag, "value", decoded_value);
812 add_assoc_string(&tag, "type", "cdata");
813 add_assoc_long(&tag, "level", parser->level);
814 zend_hash_next_index_insert(Z_ARRVAL(parser->data), &tag);
815 } else if (parser->level == (XML_MAXLEVEL + 1)) {
816 php_error_docref(NULL, E_WARNING, "Maximum depth exceeded - Results truncated");
817 } else {
818 zend_string_release_ex(decoded_value, 0);
819 }
820 }
821 }
822 /* }}} */
823
824 /* {{{ _xml_processingInstructionHandler() */
_xml_processingInstructionHandler(void * userData,const XML_Char * target,const XML_Char * data)825 void _xml_processingInstructionHandler(void *userData, const XML_Char *target, const XML_Char *data)
826 {
827 xml_parser *parser = (xml_parser *)userData;
828
829 if (!parser || Z_ISUNDEF(parser->processingInstructionHandler)) {
830 return;
831 }
832
833 zval retval, args[3];
834
835 ZVAL_COPY(&args[0], &parser->index);
836 _xml_xmlchar_zval(target, 0, parser->target_encoding, &args[1]);
837 _xml_xmlchar_zval(data, 0, parser->target_encoding, &args[2]);
838 xml_call_handler(parser, &parser->processingInstructionHandler, parser->processingInstructionPtr, 3, args, &retval);
839 zval_ptr_dtor(&retval);
840 }
841 /* }}} */
842
843 /* {{{ _xml_defaultHandler() */
_xml_defaultHandler(void * userData,const XML_Char * s,int len)844 void _xml_defaultHandler(void *userData, const XML_Char *s, int len)
845 {
846 xml_parser *parser = (xml_parser *)userData;
847
848 if (!parser || Z_ISUNDEF(parser->defaultHandler)) {
849 return;
850 }
851
852 zval retval, args[2];
853
854 ZVAL_COPY(&args[0], &parser->index);
855 _xml_xmlchar_zval(s, len, parser->target_encoding, &args[1]);
856 xml_call_handler(parser, &parser->defaultHandler, parser->defaultPtr, 2, args, &retval);
857 zval_ptr_dtor(&retval);
858 }
859 /* }}} */
860
861 /* {{{ _xml_unparsedEntityDeclHandler() */
_xml_unparsedEntityDeclHandler(void * userData,const XML_Char * entityName,const XML_Char * base,const XML_Char * systemId,const XML_Char * publicId,const XML_Char * notationName)862 void _xml_unparsedEntityDeclHandler(void *userData,
863 const XML_Char *entityName, const XML_Char *base, const XML_Char *systemId,
864 const XML_Char *publicId, const XML_Char *notationName)
865 {
866 xml_parser *parser = (xml_parser *)userData;
867
868 if (!parser || Z_ISUNDEF(parser->unparsedEntityDeclHandler)) {
869 return;
870 }
871
872 zval retval, args[6];
873
874 ZVAL_COPY(&args[0], &parser->index);
875 _xml_xmlchar_zval(entityName, 0, parser->target_encoding, &args[1]);
876 _xml_xmlchar_zval(base, 0, parser->target_encoding, &args[2]);
877 _xml_xmlchar_zval(systemId, 0, parser->target_encoding, &args[3]);
878 _xml_xmlchar_zval(publicId, 0, parser->target_encoding, &args[4]);
879 _xml_xmlchar_zval(notationName, 0, parser->target_encoding, &args[5]);
880 xml_call_handler(parser, &parser->unparsedEntityDeclHandler, parser->unparsedEntityDeclPtr, 6, args, &retval);
881 zval_ptr_dtor(&retval);
882 }
883 /* }}} */
884
885 /* {{{ _xml_notationDeclHandler() */
_xml_notationDeclHandler(void * userData,const XML_Char * notationName,const XML_Char * base,const XML_Char * systemId,const XML_Char * publicId)886 void _xml_notationDeclHandler(void *userData, const XML_Char *notationName,
887 const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId)
888 {
889 xml_parser *parser = (xml_parser *)userData;
890
891 if (!parser || Z_ISUNDEF(parser->notationDeclHandler)) {
892 return;
893 }
894
895 zval retval, args[5];
896
897 ZVAL_COPY(&args[0], &parser->index);
898 _xml_xmlchar_zval(notationName, 0, parser->target_encoding, &args[1]);
899 _xml_xmlchar_zval(base, 0, parser->target_encoding, &args[2]);
900 _xml_xmlchar_zval(systemId, 0, parser->target_encoding, &args[3]);
901 _xml_xmlchar_zval(publicId, 0, parser->target_encoding, &args[4]);
902 xml_call_handler(parser, &parser->notationDeclHandler, parser->notationDeclPtr, 5, args, &retval);
903 zval_ptr_dtor(&retval);
904 }
905 /* }}} */
906
907 /* {{{ _xml_externalEntityRefHandler() */
_xml_externalEntityRefHandler(XML_Parser parserPtr,const XML_Char * openEntityNames,const XML_Char * base,const XML_Char * systemId,const XML_Char * publicId)908 int _xml_externalEntityRefHandler(XML_Parser parserPtr, const XML_Char *openEntityNames,
909 const XML_Char *base, const XML_Char *systemId, const XML_Char *publicId)
910 {
911 xml_parser *parser = XML_GetUserData(parserPtr);
912
913 if (!parser || Z_ISUNDEF(parser->externalEntityRefHandler)) {
914 return 0;
915 }
916
917 int ret = 0; /* abort if no handler is set (should be configurable?) */
918 zval retval, args[5];
919
920 ZVAL_COPY(&args[0], &parser->index);
921 _xml_xmlchar_zval(openEntityNames, 0, parser->target_encoding, &args[1]);
922 _xml_xmlchar_zval(base, 0, parser->target_encoding, &args[2]);
923 _xml_xmlchar_zval(systemId, 0, parser->target_encoding, &args[3]);
924 _xml_xmlchar_zval(publicId, 0, parser->target_encoding, &args[4]);
925 xml_call_handler(parser, &parser->externalEntityRefHandler, parser->externalEntityRefPtr, 5, args, &retval);
926 if (!Z_ISUNDEF(retval)) {
927 convert_to_long(&retval);
928 ret = Z_LVAL(retval);
929 } else {
930 ret = 0;
931 }
932
933 return ret;
934 }
935 /* }}} */
936
937 /* {{{ _xml_startNamespaceDeclHandler() */
_xml_startNamespaceDeclHandler(void * userData,const XML_Char * prefix,const XML_Char * uri)938 void _xml_startNamespaceDeclHandler(void *userData,const XML_Char *prefix, const XML_Char *uri)
939 {
940 xml_parser *parser = (xml_parser *)userData;
941
942 if (!parser || Z_ISUNDEF(parser->startNamespaceDeclHandler)) {
943 return;
944 }
945
946 zval retval, args[3];
947
948 ZVAL_COPY(&args[0], &parser->index);
949 _xml_xmlchar_zval(prefix, 0, parser->target_encoding, &args[1]);
950 _xml_xmlchar_zval(uri, 0, parser->target_encoding, &args[2]);
951 xml_call_handler(parser, &parser->startNamespaceDeclHandler, parser->startNamespaceDeclPtr, 3, args, &retval);
952 zval_ptr_dtor(&retval);
953 }
954 /* }}} */
955
956 /* {{{ _xml_endNamespaceDeclHandler() */
_xml_endNamespaceDeclHandler(void * userData,const XML_Char * prefix)957 void _xml_endNamespaceDeclHandler(void *userData, const XML_Char *prefix)
958 {
959 xml_parser *parser = (xml_parser *)userData;
960
961 if (!parser || Z_ISUNDEF(parser->endNamespaceDeclHandler)) {
962 return;
963 }
964
965 zval retval, args[2];
966
967 ZVAL_COPY(&args[0], &parser->index);
968 _xml_xmlchar_zval(prefix, 0, parser->target_encoding, &args[1]);
969 xml_call_handler(parser, &parser->endNamespaceDeclHandler, parser->endNamespaceDeclPtr, 2, args, &retval);
970 zval_ptr_dtor(&retval);
971 }
972 /* }}} */
973
974 /************************* EXTENSION FUNCTIONS *************************/
975
php_xml_parser_create_impl(INTERNAL_FUNCTION_PARAMETERS,int ns_support)976 static void php_xml_parser_create_impl(INTERNAL_FUNCTION_PARAMETERS, int ns_support) /* {{{ */
977 {
978 xml_parser *parser;
979 int auto_detect = 0;
980
981 zend_string *encoding_param = NULL;
982
983 char *ns_param = NULL;
984 size_t ns_param_len = 0;
985
986 XML_Char *encoding;
987
988 if (zend_parse_parameters(ZEND_NUM_ARGS(), (ns_support ? "|S!s": "|S!"), &encoding_param, &ns_param, &ns_param_len) == FAILURE) {
989 RETURN_THROWS();
990 }
991
992 if (encoding_param != NULL) {
993 /* The supported encoding types are hardcoded here because
994 * we are limited to the encodings supported by expat/xmltok.
995 */
996 if (ZSTR_LEN(encoding_param) == 0) {
997 encoding = XML(default_encoding);
998 auto_detect = 1;
999 } else if (zend_string_equals_literal_ci(encoding_param, "ISO-8859-1")) {
1000 encoding = (XML_Char*)"ISO-8859-1";
1001 } else if (zend_string_equals_literal_ci(encoding_param, "UTF-8")) {
1002 encoding = (XML_Char*)"UTF-8";
1003 } else if (zend_string_equals_literal_ci(encoding_param, "US-ASCII")) {
1004 encoding = (XML_Char*)"US-ASCII";
1005 } else {
1006 zend_argument_value_error(1, "is not a supported source encoding");
1007 RETURN_THROWS();
1008 }
1009 } else {
1010 encoding = XML(default_encoding);
1011 }
1012
1013 if (ns_support && ns_param == NULL){
1014 ns_param = ":";
1015 }
1016
1017 object_init_ex(return_value, xml_parser_ce);
1018 parser = Z_XMLPARSER_P(return_value);
1019 parser->parser = XML_ParserCreate_MM((auto_detect ? NULL : encoding),
1020 &php_xml_mem_hdlrs, (XML_Char*)ns_param);
1021
1022 parser->target_encoding = encoding;
1023 parser->case_folding = 1;
1024 parser->isparsing = 0;
1025
1026 XML_SetUserData(parser->parser, parser);
1027 ZVAL_COPY_VALUE(&parser->index, return_value);
1028 }
1029 /* }}} */
1030
1031 /* {{{ Create an XML parser */
PHP_FUNCTION(xml_parser_create)1032 PHP_FUNCTION(xml_parser_create)
1033 {
1034 php_xml_parser_create_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1035 }
1036 /* }}} */
1037
1038 /* {{{ Create an XML parser */
PHP_FUNCTION(xml_parser_create_ns)1039 PHP_FUNCTION(xml_parser_create_ns)
1040 {
1041 php_xml_parser_create_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1042 }
1043 /* }}} */
1044
1045 /* {{{ Set up object which should be used for callbacks */
PHP_FUNCTION(xml_set_object)1046 PHP_FUNCTION(xml_set_object)
1047 {
1048 xml_parser *parser;
1049 zval *pind, *mythis;
1050
1051 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oo", &pind, xml_parser_ce, &mythis) == FAILURE) {
1052 RETURN_THROWS();
1053 }
1054
1055 parser = Z_XMLPARSER_P(pind);
1056
1057 zval_ptr_dtor(&parser->object);
1058 ZVAL_OBJ_COPY(&parser->object, Z_OBJ_P(mythis));
1059
1060 RETURN_TRUE;
1061 }
1062 /* }}} */
1063
1064 /* {{{ Set up start and end element handlers */
PHP_FUNCTION(xml_set_element_handler)1065 PHP_FUNCTION(xml_set_element_handler)
1066 {
1067 xml_parser *parser;
1068 zval *pind, *shdl, *ehdl;
1069
1070 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ozz", &pind, xml_parser_ce, &shdl, &ehdl) == FAILURE) {
1071 RETURN_THROWS();
1072 }
1073
1074 parser = Z_XMLPARSER_P(pind);
1075 xml_set_handler(&parser->startElementHandler, shdl);
1076 xml_set_handler(&parser->endElementHandler, ehdl);
1077 XML_SetElementHandler(parser->parser, _xml_startElementHandler, _xml_endElementHandler);
1078
1079 RETURN_TRUE;
1080 }
1081 /* }}} */
1082
1083 /* {{{ Set up character data handler */
PHP_FUNCTION(xml_set_character_data_handler)1084 PHP_FUNCTION(xml_set_character_data_handler)
1085 {
1086 xml_parser *parser;
1087 zval *pind, *hdl;
1088
1089 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz", &pind, xml_parser_ce, &hdl) == FAILURE) {
1090 RETURN_THROWS();
1091 }
1092
1093 parser = Z_XMLPARSER_P(pind);
1094 xml_set_handler(&parser->characterDataHandler, hdl);
1095 XML_SetCharacterDataHandler(parser->parser, _xml_characterDataHandler);
1096
1097 RETURN_TRUE;
1098 }
1099 /* }}} */
1100
1101 /* {{{ Set up processing instruction (PI) handler */
PHP_FUNCTION(xml_set_processing_instruction_handler)1102 PHP_FUNCTION(xml_set_processing_instruction_handler)
1103 {
1104 xml_parser *parser;
1105 zval *pind, *hdl;
1106
1107 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz", &pind, xml_parser_ce, &hdl) == FAILURE) {
1108 RETURN_THROWS();
1109 }
1110
1111 parser = Z_XMLPARSER_P(pind);
1112 xml_set_handler(&parser->processingInstructionHandler, hdl);
1113 XML_SetProcessingInstructionHandler(parser->parser, _xml_processingInstructionHandler);
1114
1115 RETURN_TRUE;
1116 }
1117 /* }}} */
1118
1119 /* {{{ Set up default handler */
PHP_FUNCTION(xml_set_default_handler)1120 PHP_FUNCTION(xml_set_default_handler)
1121 {
1122 xml_parser *parser;
1123 zval *pind, *hdl;
1124
1125 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz", &pind, xml_parser_ce, &hdl) == FAILURE) {
1126 RETURN_THROWS();
1127 }
1128
1129 parser = Z_XMLPARSER_P(pind);
1130 xml_set_handler(&parser->defaultHandler, hdl);
1131 XML_SetDefaultHandler(parser->parser, _xml_defaultHandler);
1132
1133 RETURN_TRUE;
1134 }
1135 /* }}} */
1136
1137 /* {{{ Set up unparsed entity declaration handler */
PHP_FUNCTION(xml_set_unparsed_entity_decl_handler)1138 PHP_FUNCTION(xml_set_unparsed_entity_decl_handler)
1139 {
1140 xml_parser *parser;
1141 zval *pind, *hdl;
1142
1143 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz", &pind, xml_parser_ce, &hdl) == FAILURE) {
1144 RETURN_THROWS();
1145 }
1146
1147 parser = Z_XMLPARSER_P(pind);
1148 xml_set_handler(&parser->unparsedEntityDeclHandler, hdl);
1149 XML_SetUnparsedEntityDeclHandler(parser->parser, _xml_unparsedEntityDeclHandler);
1150
1151 RETURN_TRUE;
1152 }
1153 /* }}} */
1154
1155 /* {{{ Set up notation declaration handler */
PHP_FUNCTION(xml_set_notation_decl_handler)1156 PHP_FUNCTION(xml_set_notation_decl_handler)
1157 {
1158 xml_parser *parser;
1159 zval *pind, *hdl;
1160
1161 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz", &pind, xml_parser_ce, &hdl) == FAILURE) {
1162 RETURN_THROWS();
1163 }
1164
1165 parser = Z_XMLPARSER_P(pind);
1166 xml_set_handler(&parser->notationDeclHandler, hdl);
1167 XML_SetNotationDeclHandler(parser->parser, _xml_notationDeclHandler);
1168
1169 RETURN_TRUE;
1170 }
1171 /* }}} */
1172
1173 /* {{{ Set up external entity reference handler */
PHP_FUNCTION(xml_set_external_entity_ref_handler)1174 PHP_FUNCTION(xml_set_external_entity_ref_handler)
1175 {
1176 xml_parser *parser;
1177 zval *pind, *hdl;
1178
1179 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz", &pind, xml_parser_ce, &hdl) == FAILURE) {
1180 RETURN_THROWS();
1181 }
1182
1183 parser = Z_XMLPARSER_P(pind);
1184 xml_set_handler(&parser->externalEntityRefHandler, hdl);
1185 XML_SetExternalEntityRefHandler(parser->parser, (void *) _xml_externalEntityRefHandler);
1186
1187 RETURN_TRUE;
1188 }
1189 /* }}} */
1190
1191 /* {{{ Set up character data handler */
PHP_FUNCTION(xml_set_start_namespace_decl_handler)1192 PHP_FUNCTION(xml_set_start_namespace_decl_handler)
1193 {
1194 xml_parser *parser;
1195 zval *pind, *hdl;
1196
1197 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz", &pind, xml_parser_ce, &hdl) == FAILURE) {
1198 RETURN_THROWS();
1199 }
1200
1201 parser = Z_XMLPARSER_P(pind);
1202 xml_set_handler(&parser->startNamespaceDeclHandler, hdl);
1203 XML_SetStartNamespaceDeclHandler(parser->parser, _xml_startNamespaceDeclHandler);
1204
1205 RETURN_TRUE;
1206 }
1207 /* }}} */
1208
1209 /* {{{ Set up character data handler */
PHP_FUNCTION(xml_set_end_namespace_decl_handler)1210 PHP_FUNCTION(xml_set_end_namespace_decl_handler)
1211 {
1212 xml_parser *parser;
1213 zval *pind, *hdl;
1214
1215 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Oz", &pind, xml_parser_ce, &hdl) == FAILURE) {
1216 RETURN_THROWS();
1217 }
1218
1219 parser = Z_XMLPARSER_P(pind);
1220 xml_set_handler(&parser->endNamespaceDeclHandler, hdl);
1221 XML_SetEndNamespaceDeclHandler(parser->parser, _xml_endNamespaceDeclHandler);
1222
1223 RETURN_TRUE;
1224 }
1225 /* }}} */
1226
1227 /* {{{ Start parsing an XML document */
PHP_FUNCTION(xml_parse)1228 PHP_FUNCTION(xml_parse)
1229 {
1230 xml_parser *parser;
1231 zval *pind;
1232 char *data;
1233 size_t data_len;
1234 int ret;
1235 bool isFinal = 0;
1236
1237 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Os|b", &pind, xml_parser_ce, &data, &data_len, &isFinal) == FAILURE) {
1238 RETURN_THROWS();
1239 }
1240
1241 parser = Z_XMLPARSER_P(pind);
1242 if (parser->isparsing) {
1243 zend_throw_error(NULL, "Parser must not be called recursively");
1244 RETURN_THROWS();
1245 }
1246 parser->isparsing = 1;
1247 ret = XML_Parse(parser->parser, (XML_Char*)data, data_len, isFinal);
1248 parser->isparsing = 0;
1249 RETVAL_LONG(ret);
1250 }
1251
1252 /* }}} */
1253
1254 /* {{{ Parsing a XML document */
PHP_FUNCTION(xml_parse_into_struct)1255 PHP_FUNCTION(xml_parse_into_struct)
1256 {
1257 xml_parser *parser;
1258 zval *pind, *xdata, *info = NULL;
1259 char *data;
1260 size_t data_len;
1261 int ret;
1262
1263 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Osz|z", &pind, xml_parser_ce, &data, &data_len, &xdata, &info) == FAILURE) {
1264 RETURN_THROWS();
1265 }
1266
1267 parser = Z_XMLPARSER_P(pind);
1268
1269 if (parser->isparsing) {
1270 php_error_docref(NULL, E_WARNING, "Parser must not be called recursively");
1271 RETURN_FALSE;
1272 }
1273
1274 if (info) {
1275 info = zend_try_array_init(info);
1276 if (!info) {
1277 RETURN_THROWS();
1278 }
1279 }
1280
1281 xdata = zend_try_array_init(xdata);
1282 if (!xdata) {
1283 RETURN_THROWS();
1284 }
1285
1286 ZVAL_COPY_VALUE(&parser->data, xdata);
1287
1288 if (info) {
1289 ZVAL_COPY_VALUE(&parser->info, info);
1290 }
1291
1292 parser->level = 0;
1293 xml_parser_free_ltags(parser);
1294 parser->ltags = safe_emalloc(XML_MAXLEVEL, sizeof(char *), 0);
1295 memset(parser->ltags, 0, XML_MAXLEVEL * sizeof(char *));
1296
1297 XML_SetElementHandler(parser->parser, _xml_startElementHandler, _xml_endElementHandler);
1298 XML_SetCharacterDataHandler(parser->parser, _xml_characterDataHandler);
1299
1300 parser->isparsing = 1;
1301 ret = XML_Parse(parser->parser, (XML_Char*)data, data_len, 1);
1302 parser->isparsing = 0;
1303
1304 RETVAL_LONG(ret);
1305 }
1306 /* }}} */
1307
1308 /* {{{ Get XML parser error code */
PHP_FUNCTION(xml_get_error_code)1309 PHP_FUNCTION(xml_get_error_code)
1310 {
1311 xml_parser *parser;
1312 zval *pind;
1313
1314 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pind, xml_parser_ce) == FAILURE) {
1315 RETURN_THROWS();
1316 }
1317
1318 parser = Z_XMLPARSER_P(pind);
1319 RETURN_LONG((zend_long)XML_GetErrorCode(parser->parser));
1320 }
1321 /* }}} */
1322
1323 /* {{{ Get XML parser error string */
PHP_FUNCTION(xml_error_string)1324 PHP_FUNCTION(xml_error_string)
1325 {
1326 zend_long code;
1327 char *str;
1328
1329 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &code) == FAILURE) {
1330 RETURN_THROWS();
1331 }
1332
1333 str = (char *)XML_ErrorString((int)code);
1334 if (str) {
1335 RETVAL_STRING(str);
1336 }
1337 }
1338 /* }}} */
1339
1340 /* {{{ Get current line number for an XML parser */
PHP_FUNCTION(xml_get_current_line_number)1341 PHP_FUNCTION(xml_get_current_line_number)
1342 {
1343 xml_parser *parser;
1344 zval *pind;
1345
1346 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pind, xml_parser_ce) == FAILURE) {
1347 RETURN_THROWS();
1348 }
1349
1350 parser = Z_XMLPARSER_P(pind);
1351 RETVAL_LONG(XML_GetCurrentLineNumber(parser->parser));
1352 }
1353 /* }}} */
1354
1355 /* {{{ Get current column number for an XML parser */
PHP_FUNCTION(xml_get_current_column_number)1356 PHP_FUNCTION(xml_get_current_column_number)
1357 {
1358 xml_parser *parser;
1359 zval *pind;
1360
1361 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pind, xml_parser_ce) == FAILURE) {
1362 RETURN_THROWS();
1363 }
1364
1365 parser = Z_XMLPARSER_P(pind);
1366 RETVAL_LONG(XML_GetCurrentColumnNumber(parser->parser));
1367 }
1368 /* }}} */
1369
1370 /* {{{ Get current byte index for an XML parser */
PHP_FUNCTION(xml_get_current_byte_index)1371 PHP_FUNCTION(xml_get_current_byte_index)
1372 {
1373 xml_parser *parser;
1374 zval *pind;
1375
1376 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pind, xml_parser_ce) == FAILURE) {
1377 RETURN_THROWS();
1378 }
1379
1380 parser = Z_XMLPARSER_P(pind);
1381 RETVAL_LONG(XML_GetCurrentByteIndex(parser->parser));
1382 }
1383 /* }}} */
1384
1385 /* {{{ Free an XML parser */
PHP_FUNCTION(xml_parser_free)1386 PHP_FUNCTION(xml_parser_free)
1387 {
1388 zval *pind;
1389 xml_parser *parser;
1390
1391 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &pind, xml_parser_ce) == FAILURE) {
1392 RETURN_THROWS();
1393 }
1394
1395 parser = Z_XMLPARSER_P(pind);
1396 if (parser->isparsing == 1) {
1397 php_error_docref(NULL, E_WARNING, "Parser cannot be freed while it is parsing");
1398 RETURN_FALSE;
1399 }
1400
1401 RETURN_TRUE;
1402 }
1403 /* }}} */
1404
1405 /* {{{ Set options in an XML parser */
PHP_FUNCTION(xml_parser_set_option)1406 PHP_FUNCTION(xml_parser_set_option)
1407 {
1408 xml_parser *parser;
1409 zval *pind, *val;
1410 zend_long opt;
1411
1412 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Olz", &pind, xml_parser_ce, &opt, &val) == FAILURE) {
1413 RETURN_THROWS();
1414 }
1415
1416 parser = Z_XMLPARSER_P(pind);
1417 switch (opt) {
1418 case PHP_XML_OPTION_CASE_FOLDING:
1419 parser->case_folding = zval_get_long(val);
1420 break;
1421 case PHP_XML_OPTION_SKIP_TAGSTART:
1422 parser->toffset = zval_get_long(val);
1423 if (parser->toffset < 0) {
1424 php_error_docref(NULL, E_WARNING, "tagstart ignored, because it is out of range");
1425 parser->toffset = 0;
1426 /* TODO Promote to ValueError in PHP 9.0 */
1427 RETURN_FALSE;
1428 }
1429 break;
1430 case PHP_XML_OPTION_SKIP_WHITE:
1431 parser->skipwhite = zval_get_long(val);
1432 break;
1433 case PHP_XML_OPTION_TARGET_ENCODING: {
1434 const xml_encoding *enc;
1435 if (!try_convert_to_string(val)) {
1436 RETURN_THROWS();
1437 }
1438
1439 enc = xml_get_encoding((XML_Char*)Z_STRVAL_P(val));
1440 if (enc == NULL) {
1441 zend_argument_value_error(3, "is not a supported target encoding");
1442 RETURN_THROWS();
1443 }
1444
1445 parser->target_encoding = enc->name;
1446 break;
1447 }
1448 default:
1449 zend_argument_value_error(2, "must be a XML_OPTION_* constant");
1450 RETURN_THROWS();
1451 break;
1452 }
1453
1454 RETURN_TRUE;
1455 }
1456 /* }}} */
1457
1458 /* {{{ Get options from an XML parser */
PHP_FUNCTION(xml_parser_get_option)1459 PHP_FUNCTION(xml_parser_get_option)
1460 {
1461 xml_parser *parser;
1462 zval *pind;
1463 zend_long opt;
1464
1465 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &pind, xml_parser_ce, &opt) == FAILURE) {
1466 RETURN_THROWS();
1467 }
1468
1469 parser = Z_XMLPARSER_P(pind);
1470 switch (opt) {
1471 case PHP_XML_OPTION_CASE_FOLDING:
1472 RETURN_LONG(parser->case_folding);
1473 break;
1474 case PHP_XML_OPTION_SKIP_TAGSTART:
1475 RETURN_LONG(parser->toffset);
1476 break;
1477 case PHP_XML_OPTION_SKIP_WHITE:
1478 RETURN_LONG(parser->skipwhite);
1479 break;
1480 case PHP_XML_OPTION_TARGET_ENCODING:
1481 RETURN_STRING((char *)parser->target_encoding);
1482 break;
1483 default:
1484 zend_argument_value_error(2, "must be a XML_OPTION_* constant");
1485 RETURN_THROWS();
1486 }
1487 }
1488 /* }}} */
1489
1490 #endif
1491