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