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