root/ext/xml/xml.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. PHP_GINIT_FUNCTION
  2. php_xml_malloc_wrapper
  3. php_xml_realloc_wrapper
  4. php_xml_free_wrapper
  5. PHP_MINIT_FUNCTION
  6. PHP_MINFO_FUNCTION
  7. _xml_xmlchar_zval
  8. xml_parser_dtor
  9. xml_set_handler
  10. xml_call_handler
  11. xml_encode_iso_8859_1
  12. xml_decode_iso_8859_1
  13. xml_encode_us_ascii
  14. xml_decode_us_ascii
  15. xml_get_encoding
  16. xml_utf8_encode
  17. xml_utf8_decode
  18. _xml_xmlcharlen
  19. _xml_zval_strdup
  20. _xml_add_to_info
  21. _xml_decode_tag
  22. _xml_startElementHandler
  23. _xml_endElementHandler
  24. _xml_characterDataHandler
  25. _xml_processingInstructionHandler
  26. _xml_defaultHandler
  27. _xml_unparsedEntityDeclHandler
  28. _xml_notationDeclHandler
  29. _xml_externalEntityRefHandler
  30. _xml_startNamespaceDeclHandler
  31. _xml_endNamespaceDeclHandler
  32. php_xml_parser_create_impl
  33. PHP_FUNCTION
  34. PHP_FUNCTION
  35. PHP_FUNCTION
  36. PHP_FUNCTION
  37. PHP_FUNCTION
  38. PHP_FUNCTION
  39. PHP_FUNCTION
  40. PHP_FUNCTION
  41. PHP_FUNCTION
  42. PHP_FUNCTION
  43. PHP_FUNCTION
  44. PHP_FUNCTION
  45. PHP_FUNCTION
  46. PHP_FUNCTION
  47. PHP_FUNCTION
  48. PHP_FUNCTION
  49. PHP_FUNCTION
  50. PHP_FUNCTION
  51. PHP_FUNCTION
  52. PHP_FUNCTION
  53. PHP_FUNCTION
  54. PHP_FUNCTION
  55. PHP_FUNCTION
  56. PHP_FUNCTION

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

/* [<][>][^][v][top][bottom][index][help] */