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