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