PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/xml - xml.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 569 648 87.8 %
Date: 2015-05-21 Functions: 48 56 85.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2015 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       20916 : static PHP_GINIT_FUNCTION(xml)
     295             : {
     296             : #if defined(COMPILE_DL_XML) && defined(ZTS)
     297             :         ZEND_TSRMLS_CACHE_UPDATE();
     298             : #endif
     299       20916 :         xml_globals->default_encoding = (XML_Char*)"UTF-8";
     300       20916 : }
     301             : 
     302           0 : static void *php_xml_malloc_wrapper(size_t sz)
     303             : {
     304           0 :         return emalloc(sz);
     305             : }
     306             : 
     307           0 : static void *php_xml_realloc_wrapper(void *ptr, size_t sz)
     308             : {
     309           0 :         return erealloc(ptr, sz);
     310             : }
     311             : 
     312           0 : static void php_xml_free_wrapper(void *ptr)
     313             : {
     314           0 :         if (ptr != NULL) {
     315           0 :                 efree(ptr);
     316             :         }
     317           0 : }
     318             : 
     319       20916 : PHP_MINIT_FUNCTION(xml)
     320             : {
     321       20916 :         le_xml_parser = zend_register_list_destructors_ex(xml_parser_dtor, NULL, "xml", module_number);
     322             : 
     323       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_NONE", XML_ERROR_NONE, CONST_CS|CONST_PERSISTENT);
     324       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_NO_MEMORY", XML_ERROR_NO_MEMORY, CONST_CS|CONST_PERSISTENT);
     325       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_SYNTAX", XML_ERROR_SYNTAX, CONST_CS|CONST_PERSISTENT);
     326       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_NO_ELEMENTS", XML_ERROR_NO_ELEMENTS, CONST_CS|CONST_PERSISTENT);
     327       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_INVALID_TOKEN", XML_ERROR_INVALID_TOKEN, CONST_CS|CONST_PERSISTENT);
     328       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_UNCLOSED_TOKEN", XML_ERROR_UNCLOSED_TOKEN, CONST_CS|CONST_PERSISTENT);
     329       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_PARTIAL_CHAR", XML_ERROR_PARTIAL_CHAR, CONST_CS|CONST_PERSISTENT);
     330       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_TAG_MISMATCH", XML_ERROR_TAG_MISMATCH, CONST_CS|CONST_PERSISTENT);
     331       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_DUPLICATE_ATTRIBUTE", XML_ERROR_DUPLICATE_ATTRIBUTE, CONST_CS|CONST_PERSISTENT);
     332       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_JUNK_AFTER_DOC_ELEMENT", XML_ERROR_JUNK_AFTER_DOC_ELEMENT, CONST_CS|CONST_PERSISTENT);
     333       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_PARAM_ENTITY_REF", XML_ERROR_PARAM_ENTITY_REF, CONST_CS|CONST_PERSISTENT);
     334       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_UNDEFINED_ENTITY", XML_ERROR_UNDEFINED_ENTITY, CONST_CS|CONST_PERSISTENT);
     335       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_RECURSIVE_ENTITY_REF", XML_ERROR_RECURSIVE_ENTITY_REF, CONST_CS|CONST_PERSISTENT);
     336       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_ASYNC_ENTITY", XML_ERROR_ASYNC_ENTITY, CONST_CS|CONST_PERSISTENT);
     337       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_BAD_CHAR_REF", XML_ERROR_BAD_CHAR_REF, CONST_CS|CONST_PERSISTENT);
     338       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_BINARY_ENTITY_REF", XML_ERROR_BINARY_ENTITY_REF, CONST_CS|CONST_PERSISTENT);
     339       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF", XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF, CONST_CS|CONST_PERSISTENT);
     340       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_MISPLACED_XML_PI", XML_ERROR_MISPLACED_XML_PI, CONST_CS|CONST_PERSISTENT);
     341       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_UNKNOWN_ENCODING", XML_ERROR_UNKNOWN_ENCODING, CONST_CS|CONST_PERSISTENT);
     342       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_INCORRECT_ENCODING", XML_ERROR_INCORRECT_ENCODING, CONST_CS|CONST_PERSISTENT);
     343       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_UNCLOSED_CDATA_SECTION", XML_ERROR_UNCLOSED_CDATA_SECTION, CONST_CS|CONST_PERSISTENT);
     344       20916 :         REGISTER_LONG_CONSTANT("XML_ERROR_EXTERNAL_ENTITY_HANDLING", XML_ERROR_EXTERNAL_ENTITY_HANDLING, CONST_CS|CONST_PERSISTENT);
     345             : 
     346       20916 :         REGISTER_LONG_CONSTANT("XML_OPTION_CASE_FOLDING", PHP_XML_OPTION_CASE_FOLDING, CONST_CS|CONST_PERSISTENT);
     347       20916 :         REGISTER_LONG_CONSTANT("XML_OPTION_TARGET_ENCODING", PHP_XML_OPTION_TARGET_ENCODING, CONST_CS|CONST_PERSISTENT);
     348       20916 :         REGISTER_LONG_CONSTANT("XML_OPTION_SKIP_TAGSTART", PHP_XML_OPTION_SKIP_TAGSTART, CONST_CS|CONST_PERSISTENT);
     349       20916 :         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       20916 :         php_xml_mem_hdlrs.malloc_fcn = php_xml_malloc_wrapper;
     355       20916 :         php_xml_mem_hdlrs.realloc_fcn = php_xml_realloc_wrapper;
     356       20916 :         php_xml_mem_hdlrs.free_fcn = php_xml_free_wrapper;
     357             : 
     358             : #ifdef LIBXML_EXPAT_COMPAT
     359       20916 :         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       20916 :         return SUCCESS;
     365             : }
     366             : 
     367         142 : PHP_MINFO_FUNCTION(xml)
     368             : {
     369         142 :         php_info_print_table_start();
     370         142 :         php_info_print_table_row(2, "XML Support", "active");
     371         142 :         php_info_print_table_row(2, "XML Namespace Support", "active");
     372             : #if defined(LIBXML_DOTTED_VERSION) && defined(LIBXML_EXPAT_COMPAT)
     373         142 :         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         142 :         php_info_print_table_end();
     378         142 : }
     379             : /* }}} */
     380             : 
     381             : /* {{{ extension-internal functions */
     382             : 
     383          52 : static void _xml_xmlchar_zval(const XML_Char *s, int len, const XML_Char *encoding, zval *ret)
     384             : {
     385          52 :         if (s == NULL) {
     386          12 :                 ZVAL_FALSE(ret);
     387          12 :                 return;
     388             :         }
     389          40 :         if (len == 0) {
     390          21 :                 len = _xml_xmlcharlen(s);
     391             :         }
     392          40 :         ZVAL_STR(ret, xml_utf8_decode(s, len, encoding));
     393             : }
     394             : /* }}} */
     395             : 
     396             : /* {{{ xml_parser_dtor() */
     397         161 : static void xml_parser_dtor(zend_resource *rsrc)
     398             : {
     399         161 :         xml_parser *parser = (xml_parser *)rsrc->ptr;
     400             :         
     401         161 :         if (parser->parser) {
     402         161 :                 XML_ParserFree(parser->parser);
     403             :         }
     404         161 :         if (parser->ltags) {
     405             :                 int inx;
     406         261 :                 for (inx = 0; ((inx < parser->level) && (inx < XML_MAXLEVEL)); inx++)
     407         255 :                         efree(parser->ltags[ inx ]);
     408           6 :                 efree(parser->ltags);
     409             :         }
     410         322 :         if (!Z_ISUNDEF(parser->startElementHandler)) {
     411          32 :                 zval_ptr_dtor(&parser->startElementHandler);
     412             :         }
     413         322 :         if (!Z_ISUNDEF(parser->endElementHandler)) {
     414          31 :                 zval_ptr_dtor(&parser->endElementHandler);
     415             :         }
     416         322 :         if (!Z_ISUNDEF(parser->characterDataHandler)) {
     417           3 :                 zval_ptr_dtor(&parser->characterDataHandler);
     418             :         }
     419         322 :         if (!Z_ISUNDEF(parser->processingInstructionHandler)) {
     420           1 :                 zval_ptr_dtor(&parser->processingInstructionHandler);
     421             :         }
     422         322 :         if (!Z_ISUNDEF(parser->defaultHandler)) {
     423           2 :                 zval_ptr_dtor(&parser->defaultHandler);
     424             :         }
     425         322 :         if (!Z_ISUNDEF(parser->unparsedEntityDeclHandler)) {
     426           1 :                 zval_ptr_dtor(&parser->unparsedEntityDeclHandler);
     427             :         }
     428         322 :         if (!Z_ISUNDEF(parser->notationDeclHandler)) {
     429           1 :                 zval_ptr_dtor(&parser->notationDeclHandler);
     430             :         }
     431         322 :         if (!Z_ISUNDEF(parser->externalEntityRefHandler)) {
     432           0 :                 zval_ptr_dtor(&parser->externalEntityRefHandler);
     433             :         }
     434         322 :         if (!Z_ISUNDEF(parser->unknownEncodingHandler)) {
     435           0 :                 zval_ptr_dtor(&parser->unknownEncodingHandler);
     436             :         }
     437         322 :         if (!Z_ISUNDEF(parser->startNamespaceDeclHandler)) {
     438           1 :                 zval_ptr_dtor(&parser->startNamespaceDeclHandler);
     439             :         }
     440         322 :         if (!Z_ISUNDEF(parser->endNamespaceDeclHandler)) {
     441           1 :                 zval_ptr_dtor(&parser->endNamespaceDeclHandler);
     442             :         }
     443         161 :         if (parser->baseURI) {
     444           0 :                 efree(parser->baseURI);
     445             :         }
     446         322 :         if (!Z_ISUNDEF(parser->object)) {
     447          24 :                 zval_ptr_dtor(&parser->object);
     448             :         }
     449             : 
     450         161 :         efree(parser);
     451         161 : }
     452             : /* }}} */
     453             : 
     454             : /* {{{ xml_set_handler() */
     455          76 : static void xml_set_handler(zval *handler, zval *data)
     456             : {
     457             :         /* If we have already a handler, release it */
     458          76 :         if (handler) {
     459          76 :                 zval_ptr_dtor(handler);
     460             :         }
     461             : 
     462             :         /* IS_ARRAY might indicate that we're using array($obj, 'method') syntax */
     463         152 :         if (Z_TYPE_P(data) != IS_ARRAY && Z_TYPE_P(data) != IS_OBJECT) {
     464          75 :                 convert_to_string_ex(data);
     465          74 :                 if (Z_STRLEN_P(data) == 0) {
     466           1 :                         ZVAL_UNDEF(handler);
     467           1 :                         return;
     468             :                 }
     469             :         }
     470             : 
     471          75 :         ZVAL_COPY(handler, data);
     472             : }
     473             : /* }}} */
     474             : 
     475             : /* {{{ xml_call_handler() */
     476         177 : 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         177 :         ZVAL_UNDEF(retval);
     481         177 :         if (parser && handler && !EG(exception)) {
     482             :                 int result;
     483             :                 zend_fcall_info fci;
     484             : 
     485         174 :                 fci.size = sizeof(fci);
     486         174 :                 fci.function_table = EG(function_table);
     487         174 :                 ZVAL_COPY_VALUE(&fci.function_name, handler);
     488         174 :                 fci.symbol_table = NULL;
     489         174 :                 fci.object = Z_OBJ(parser->object);
     490         174 :                 fci.retval = retval;
     491         174 :                 fci.param_count = argc;
     492         174 :                 fci.params = argv;
     493         174 :                 fci.no_separation = 0;
     494             :                 /*fci.function_handler_cache = &function_ptr;*/
     495             : 
     496         174 :                 result = zend_call_function(&fci, NULL);
     497         174 :                 if (result == FAILURE) {
     498             :                         zval *method;
     499             :                         zval *obj;
     500             : 
     501           0 :                         if (Z_TYPE_P(handler) == IS_STRING) {
     502           0 :                                 php_error_docref(NULL, E_WARNING, "Unable to call handler %s()", Z_STRVAL_P(handler));
     503           0 :                         } else if ((obj = zend_hash_index_find(Z_ARRVAL_P(handler), 0)) != NULL &&
     504           0 :                                            (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           0 :                                 php_error_docref(NULL, E_WARNING, "Unable to call handler %s::%s()", Z_OBJCE_P(obj)->name->val, Z_STRVAL_P(method));
     508             :                         } else 
     509           0 :                                 php_error_docref(NULL, E_WARNING, "Unable to call handler");
     510             :                 }
     511             :         } 
     512         631 :         for (i = 0; i < argc; i++) {
     513         454 :                 zval_ptr_dtor(&argv[i]);
     514             :         }
     515         177 : }
     516             : /* }}} */
     517             : 
     518             : /* {{{ xml_encode_iso_8859_1() */
     519         138 : inline static unsigned short xml_encode_iso_8859_1(unsigned char c)
     520             : {
     521         138 :         return (unsigned short)c;
     522             : }
     523             : /* }}} */
     524             : 
     525             : /* {{{ xml_decode_iso_8859_1() */
     526         701 : inline static char xml_decode_iso_8859_1(unsigned short c)
     527             : {
     528         701 :         return (char)(c > 0xff ? '?' : c);
     529             : }
     530             : /* }}} */
     531             : 
     532             : /* {{{ xml_encode_us_ascii() */
     533           0 : inline static unsigned short xml_encode_us_ascii(unsigned char c)
     534             : {
     535           0 :         return (unsigned short)c;
     536             : }
     537             : /* }}} */
     538             : 
     539             : /* {{{ xml_decode_us_ascii() */
     540           0 : inline static char xml_decode_us_ascii(unsigned short c)
     541             : {
     542           0 :         return (char)(c > 0x7f ? '?' : c);
     543             : }
     544             : /* }}} */
     545             : 
     546             : /* {{{ xml_get_encoding() */
     547        1391 : static xml_encoding *xml_get_encoding(const XML_Char *name)
     548             : {
     549        1391 :         xml_encoding *enc = &xml_encodings[0];
     550             : 
     551        5387 :         while (enc && enc->name) {
     552        3996 :                 if (strcasecmp((char *)name, (char *)enc->name) == 0) {
     553        1391 :                         return enc;
     554             :                 }
     555        2605 :                 enc++;
     556             :         }
     557           0 :         return NULL;
     558             : }
     559             : /* }}} */
     560             : 
     561             : /* {{{ xml_utf8_encode() */
     562          25 : PHP_XML_API zend_string *xml_utf8_encode(const char *s, size_t len, const XML_Char *encoding)
     563             : {
     564          25 :         size_t pos = len;
     565             :         zend_string *str;
     566             :         unsigned int c;
     567          25 :         unsigned short (*encoder)(unsigned char) = NULL;
     568          25 :         xml_encoding *enc = xml_get_encoding(encoding);
     569             : 
     570          25 :         if (enc) {
     571          25 :                 encoder = enc->encoding_function;
     572             :         } else {
     573             :                 /* If the target encoding was unknown, fail */
     574           0 :                 return NULL;
     575             :         }
     576          25 :         if (encoder == NULL) {
     577             :                 /* If no encoder function was specified, return the data as-is.
     578             :                  */
     579           0 :                 str = zend_string_init(s, len, 0);
     580           0 :                 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          50 :         str = zend_string_alloc(len * 4, 0);
     585          25 :         str->len = 0;
     586         188 :         while (pos > 0) {
     587         138 :                 c = encoder ? encoder((unsigned char)(*s)) : (unsigned short)(*s);
     588         138 :                 if (c < 0x80) {
     589          66 :                         str->val[str->len++] = (char) c;
     590          72 :                 } else if (c < 0x800) {
     591          72 :                         str->val[str->len++] = (0xc0 | (c >> 6));
     592          72 :                         str->val[str->len++] = (0x80 | (c & 0x3f));
     593           0 :                 } else if (c < 0x10000) {
     594           0 :                         str->val[str->len++] = (0xe0 | (c >> 12));
     595           0 :                         str->val[str->len++] = (0xc0 | ((c >> 6) & 0x3f));
     596           0 :                         str->val[str->len++] = (0x80 | (c & 0x3f));
     597           0 :                 } else if (c < 0x200000) {
     598           0 :                         str->val[str->len++] = (0xf0 | (c >> 18));
     599           0 :                         str->val[str->len++] = (0xe0 | ((c >> 12) & 0x3f));
     600           0 :                         str->val[str->len++] = (0xc0 | ((c >> 6) & 0x3f));
     601           0 :                         str->val[str->len++] = (0x80 | (c & 0x3f));
     602             :                 }
     603         138 :                 pos--;
     604         138 :                 s++;
     605             :         }
     606          25 :         str->val[str->len] = '\0';
     607          50 :         str = zend_string_truncate(str, str->len, 0);
     608          25 :         return str;
     609             : }
     610             : /* }}} */
     611             : 
     612             : /* {{{ xml_utf8_decode() */
     613        1363 : PHP_XML_API zend_string *xml_utf8_decode(const XML_Char *s, size_t len, const XML_Char *encoding)
     614             : {
     615        1363 :         size_t pos = 0;
     616             :         unsigned int c;
     617        1363 :         char (*decoder)(unsigned short) = NULL;
     618        1363 :         xml_encoding *enc = xml_get_encoding(encoding);
     619             :         zend_string *str;
     620             : 
     621        1363 :         if (enc) {
     622        1363 :                 decoder = enc->decoding_function;
     623             :         }
     624             : 
     625        1363 :         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        2602 :                 str = zend_string_init((char *)s, len, 0);
     630        1301 :                 return str;
     631             :         }
     632             : 
     633          62 :         str = zend_string_alloc(len, 0);
     634          62 :         str->len = 0;
     635         825 :         while (pos < len) {
     636         701 :                 int status = FAILURE;
     637         701 :                 c = php_next_utf8_char((const unsigned char*)s, (size_t) len, &pos, &status);
     638             : 
     639         701 :                 if (status == FAILURE || c > 0xFFU) {
     640           8 :                         c = '?';
     641             :                 }
     642             : 
     643         701 :                 str->val[str->len++] = decoder ? decoder(c) : c;
     644             :         }
     645          62 :         str->val[str->len] = '\0';
     646          62 :         if (str->len < len) {
     647          10 :                 str = zend_string_truncate(str, str->len, 0);
     648             :         }
     649             : 
     650          62 :         return str;
     651             : }
     652             : /* }}} */
     653             : 
     654             : /* {{{ _xml_xmlcharlen() */
     655          21 : static int _xml_xmlcharlen(const XML_Char *s)
     656             : {
     657          21 :         int len = 0;
     658             : 
     659         359 :         while (*s) {
     660         317 :                 len++;
     661         317 :                 s++;
     662             :         }
     663          21 :         return len;
     664             : }
     665             : /* }}} */
     666             : 
     667             : /* {{{ _xml_zval_strdup() */
     668           0 : PHP_XML_API char *_xml_zval_strdup(zval *val)
     669             : {
     670           0 :         if (Z_TYPE_P(val) == IS_STRING) {
     671           0 :                 char *buf = emalloc(Z_STRLEN_P(val) + 1);
     672           0 :                 memcpy(buf, Z_STRVAL_P(val), Z_STRLEN_P(val));
     673           0 :                 buf[Z_STRLEN_P(val)] = '\0';
     674           0 :                 return buf;
     675             :         }
     676           0 :         return NULL;
     677             : }
     678             : /* }}} */
     679             : 
     680             : /* {{{ _xml_add_to_info() */
     681         276 : static void _xml_add_to_info(xml_parser *parser,char *name)
     682             : {
     683             :         zval *element;
     684             : 
     685         552 :         if (Z_ISUNDEF(parser->info)) {
     686         255 :                 return;
     687             :         }
     688             : 
     689          21 :         if ((element = zend_hash_str_find(Z_ARRVAL(parser->info), name, strlen(name))) == NULL) {
     690             :                 zval values;
     691          10 :                 array_init(&values);
     692          10 :                 element = zend_hash_str_update(Z_ARRVAL(parser->info), name, strlen(name), &values);
     693             :         } 
     694             :                         
     695          21 :         add_next_index_long(element, parser->curtag);
     696             :         
     697          21 :         parser->curtag++;
     698             : }
     699             : /* }}} */
     700             : 
     701             : /* {{{ _xml_decode_tag() */
     702        1228 : static zend_string *_xml_decode_tag(xml_parser *parser, const char *tag)
     703             : {
     704             :         zend_string *str;
     705             : 
     706        1228 :         str = xml_utf8_decode((const XML_Char *)tag, strlen(tag), parser->target_encoding);
     707             : 
     708        1228 :         if (parser->case_folding) {
     709        1052 :                 php_strtoupper(str->val, str->len);
     710             :         }
     711             : 
     712        1228 :         return str;
     713             : }
     714             : /* }}} */
     715             : 
     716             : /* {{{ _xml_startElementHandler() */
     717        1088 : void _xml_startElementHandler(void *userData, const XML_Char *name, const XML_Char **attributes)
     718             : {
     719        1088 :         xml_parser *parser = (xml_parser *)userData;
     720        1088 :         const char **attrs = (const char **) attributes;
     721             :         zend_string *att, *tag_name, *val;
     722             :         zval retval, args[3];
     723             : 
     724        1088 :         if (parser) {
     725        1088 :                 parser->level++;
     726             : 
     727        1088 :                 tag_name = _xml_decode_tag(parser, (const char *)name);
     728             : 
     729        2176 :                 if (!Z_ISUNDEF(parser->startElementHandler)) {
     730          76 :                         ZVAL_COPY(&args[0], &parser->index);
     731         152 :                         ZVAL_STRING(&args[1], tag_name->val + parser->toffset);
     732          76 :                         array_init(&args[2]);
     733             : 
     734         201 :                         while (attributes && *attributes) {
     735             :                                 zval tmp;
     736             : 
     737          49 :                                 att = _xml_decode_tag(parser, (const char *)attributes[0]);
     738          49 :                                 val = xml_utf8_decode(attributes[1], strlen((char *)attributes[1]), parser->target_encoding);
     739             : 
     740          49 :                                 ZVAL_STR(&tmp, val);
     741          49 :                                 zend_symtable_update(Z_ARRVAL(args[2]), att, &tmp);
     742             : 
     743          49 :                                 attributes += 2;
     744             : 
     745             :                                 zend_string_release(att);
     746             :                         }
     747             :                         
     748          76 :                         xml_call_handler(parser, &parser->startElementHandler, parser->startElementPtr, 3, args, &retval);
     749          76 :                         zval_ptr_dtor(&retval);
     750             :                 } 
     751             : 
     752        2176 :                 if (!Z_ISUNDEF(parser->data)) {
     753        1012 :                         if (parser->level <= XML_MAXLEVEL)  {
     754             :                                 zval tag, atr;
     755         267 :                                 int atcnt = 0;
     756             : 
     757         267 :                                 array_init(&tag);
     758         267 :                                 array_init(&atr);
     759             : 
     760         267 :                                 _xml_add_to_info(parser, tag_name->val + parser->toffset);
     761             : 
     762         267 :                                 add_assoc_string(&tag, "tag", tag_name->val + parser->toffset); /* cast to avoid gcc-warning */
     763         267 :                                 add_assoc_string(&tag, "type", "open");
     764         267 :                                 add_assoc_long(&tag, "level", parser->level);
     765             : 
     766         267 :                                 parser->ltags[parser->level-1] = estrdup(tag_name->val);
     767         267 :                                 parser->lastwasopen = 1;
     768             : 
     769         267 :                                 attributes = (const XML_Char **) attrs;
     770             : 
     771         538 :                                 while (attributes && *attributes) {
     772             :                                         zval tmp;
     773             : 
     774           4 :                                         att = _xml_decode_tag(parser, (const char *)attributes[0]);
     775           4 :                                         val = xml_utf8_decode(attributes[1], strlen((char *)attributes[1]), parser->target_encoding);
     776             : 
     777           4 :                                         ZVAL_STR(&tmp, val);
     778           4 :                                         zend_symtable_update(Z_ARRVAL(atr), att, &tmp);
     779             : 
     780           4 :                                         atcnt++;
     781           4 :                                         attributes += 2;
     782             : 
     783             :                                         zend_string_release(att);
     784             :                                 }
     785             : 
     786         267 :                                 if (atcnt) {
     787           3 :                                         zend_hash_str_add(Z_ARRVAL(tag), "attributes", sizeof("attributes") - 1, &atr);
     788             :                                 } else {
     789         264 :                                         zval_ptr_dtor(&atr);
     790             :                                 }
     791             : 
     792         267 :                                 parser->ctag = zend_hash_next_index_insert(Z_ARRVAL(parser->data), &tag);
     793         745 :                         } else if (parser->level == (XML_MAXLEVEL + 1)) {
     794           1 :                                                         php_error_docref(NULL, E_WARNING, "Maximum depth exceeded - Results truncated");
     795             :                         }
     796             :                 }
     797             : 
     798             :                 zend_string_release(tag_name);
     799             :         }
     800        1088 : }
     801             : /* }}} */
     802             : 
     803             : /* {{{ _xml_endElementHandler() */
     804          87 : void _xml_endElementHandler(void *userData, const XML_Char *name)
     805             : {
     806          87 :         xml_parser *parser = (xml_parser *)userData;
     807             :         zend_string *tag_name;
     808             : 
     809          87 :         if (parser) {
     810             :                 zval retval, args[2];
     811             : 
     812          87 :                 tag_name = _xml_decode_tag(parser, (const char *)name);
     813             : 
     814         174 :                 if (!Z_ISUNDEF(parser->endElementHandler)) {
     815          73 :                         ZVAL_COPY(&args[0], &parser->index);
     816         146 :                         ZVAL_STRING(&args[1], (tag_name->val) + parser->toffset);
     817             : 
     818          73 :                         xml_call_handler(parser, &parser->endElementHandler, parser->endElementPtr, 2, args, &retval);
     819          73 :                         zval_ptr_dtor(&retval);
     820             :                 } 
     821             : 
     822         174 :                 if (!Z_ISUNDEF(parser->data)) {
     823             :                         zval tag;
     824             : 
     825          12 :                         if (parser->lastwasopen) {
     826           6 :                                 add_assoc_string(parser->ctag, "type", "complete");
     827             :                         } else {
     828           6 :                                 array_init(&tag);
     829             :                                   
     830           6 :                                 _xml_add_to_info(parser, tag_name->val + parser->toffset);
     831             : 
     832           6 :                                 add_assoc_string(&tag, "tag", tag_name->val + parser->toffset); /* cast to avoid gcc-warning */
     833           6 :                                 add_assoc_string(&tag, "type", "close");
     834           6 :                                 add_assoc_long(&tag, "level", parser->level);
     835             :                                   
     836           6 :                                 zend_hash_next_index_insert(Z_ARRVAL(parser->data), &tag);
     837             :                         }
     838             : 
     839          12 :                         parser->lastwasopen = 0;
     840             :                 }
     841             : 
     842             :                 zend_string_release(tag_name);
     843             : 
     844          87 :                 if ((parser->ltags) && (parser->level <= XML_MAXLEVEL)) {
     845          12 :                         efree(parser->ltags[parser->level-1]);
     846             :                 }
     847             : 
     848          87 :                 parser->level--;
     849             :         }
     850          87 : }
     851             : /* }}} */
     852             : 
     853             : /* {{{ _xml_characterDataHandler() */
     854          20 : void _xml_characterDataHandler(void *userData, const XML_Char *s, int len)
     855             : {
     856          20 :         xml_parser *parser = (xml_parser *)userData;
     857             : 
     858          20 :         if (parser) {
     859             :                 zval retval, args[2];
     860             : 
     861          40 :                 if (!Z_ISUNDEF(parser->characterDataHandler)) {
     862           6 :                         ZVAL_COPY(&args[0], &parser->index);
     863           6 :                         _xml_xmlchar_zval(s, len, parser->target_encoding, &args[1]);
     864           6 :                         xml_call_handler(parser, &parser->characterDataHandler, parser->characterDataPtr, 2, args, &retval);
     865           6 :                         zval_ptr_dtor(&retval);
     866             :                 } 
     867             : 
     868          40 :                 if (!Z_ISUNDEF(parser->data)) {
     869             :                         int i;
     870          14 :                         int doprint = 0;
     871             :                         zend_string *decoded_value;
     872             : 
     873          14 :                         decoded_value = xml_utf8_decode(s, len, parser->target_encoding);
     874          20 :                         for (i = 0; i < decoded_value->len; i++) {
     875          16 :                                 switch (decoded_value->val[i]) {
     876             :                                         case ' ':
     877             :                                         case '\t':
     878             :                                         case '\n':
     879           6 :                                                 continue;
     880             :                                         default:
     881          10 :                                                 doprint = 1;
     882             :                                                 break;
     883             :                                 }
     884          10 :                                 if (doprint) {
     885          10 :                                         break;
     886             :                                 }
     887             :                         }
     888          26 :                         if (doprint || (! parser->skipwhite)) {
     889          14 :                                 if (parser->lastwasopen) {
     890             :                                         zval *myval;
     891             :                                         
     892             :                                         /* check if the current tag already has a value - if yes append to that! */
     893           9 :                                         if ((myval = zend_hash_str_find(Z_ARRVAL_P(parser->ctag), "value", sizeof("value") - 1))) {
     894           2 :                                                 int newlen = Z_STRLEN_P(myval) + decoded_value->len;
     895           4 :                                                 Z_STR_P(myval) = zend_string_extend(Z_STR_P(myval), newlen, 0);
     896           4 :                                                 strncpy(Z_STRVAL_P(myval) + Z_STRLEN_P(myval) - decoded_value->len,
     897           2 :                                                                 decoded_value->val, decoded_value->len + 1);
     898             :                                                 zend_string_release(decoded_value);
     899             :                                         } else {
     900           7 :                                                 add_assoc_str(parser->ctag, "value", decoded_value);
     901             :                                         }
     902             :                                         
     903             :                                 } else {
     904             :                                         zval tag;
     905             :                                         zval *curtag, *mytype, *myval;
     906             : 
     907          15 :                                         ZEND_HASH_REVERSE_FOREACH_VAL(Z_ARRVAL(parser->data), curtag) {
     908           5 :                                                 if ((mytype = zend_hash_str_find(Z_ARRVAL_P(curtag),"type", sizeof("type") - 1))) {
     909           5 :                                                         if (!strcmp(Z_STRVAL_P(mytype), "cdata")) {
     910           2 :                                                                 if ((myval = zend_hash_str_find(Z_ARRVAL_P(curtag), "value", sizeof("value") - 1))) {
     911           2 :                                                                         int newlen = Z_STRLEN_P(myval) + decoded_value->len;
     912           4 :                                                                         Z_STR_P(myval) = zend_string_extend(Z_STR_P(myval), newlen, 0);
     913           4 :                                                                         strncpy(Z_STRVAL_P(myval) + Z_STRLEN_P(myval) - decoded_value->len,
     914           2 :                                                                                         decoded_value->val, decoded_value->len + 1);
     915             :                                                                         zend_string_release(decoded_value);
     916           2 :                                                                         return;
     917             :                                                                 }
     918             :                                                         }
     919             :                                                 }
     920           3 :                                                 break;
     921             :                                         } ZEND_HASH_FOREACH_END();
     922             : 
     923           3 :                                         if (parser->level <= XML_MAXLEVEL) {
     924           3 :                                                 array_init(&tag);
     925             : 
     926           3 :                                                 _xml_add_to_info(parser,parser->ltags[parser->level-1] + parser->toffset);
     927             : 
     928           3 :                                                 add_assoc_string(&tag, "tag", parser->ltags[parser->level-1] + parser->toffset);
     929           3 :                                                 add_assoc_str(&tag, "value", decoded_value);
     930           3 :                                                 add_assoc_string(&tag, "type", "cdata");
     931           3 :                                                 add_assoc_long(&tag, "level", parser->level);
     932             : 
     933           3 :                                                 zend_hash_next_index_insert(Z_ARRVAL(parser->data), &tag);
     934           0 :                                         } else if (parser->level == (XML_MAXLEVEL + 1)) {
     935           0 :                                                                                         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           1 : void _xml_processingInstructionHandler(void *userData, const XML_Char *target, const XML_Char *data)
     948             : {
     949           1 :         xml_parser *parser = (xml_parser *)userData;
     950             : 
     951           2 :         if (parser && !Z_ISUNDEF(parser->processingInstructionHandler)) {
     952             :                 zval retval, args[3];
     953             : 
     954           1 :                 ZVAL_COPY(&args[0], &parser->index);
     955           1 :                 _xml_xmlchar_zval(target, 0, parser->target_encoding, &args[1]);
     956           1 :                 _xml_xmlchar_zval(data, 0, parser->target_encoding, &args[2]);
     957           1 :                 xml_call_handler(parser, &parser->processingInstructionHandler, parser->processingInstructionPtr, 3, args, &retval);
     958           1 :                 zval_ptr_dtor(&retval);
     959             :         }
     960           1 : }
     961             : /* }}} */
     962             : 
     963             : /* {{{ _xml_defaultHandler() */
     964          13 : void _xml_defaultHandler(void *userData, const XML_Char *s, int len)
     965             : {
     966          13 :         xml_parser *parser = (xml_parser *)userData;
     967             : 
     968          26 :         if (parser && !Z_ISUNDEF(parser->defaultHandler)) {
     969             :                 zval retval, args[2];
     970             : 
     971          13 :                 ZVAL_COPY(&args[0], &parser->index);
     972          13 :                 _xml_xmlchar_zval(s, len, parser->target_encoding, &args[1]);
     973          13 :                 xml_call_handler(parser, &parser->defaultHandler, parser->defaultPtr, 2, args, &retval);
     974          13 :                 zval_ptr_dtor(&retval);
     975             :         }
     976          13 : }
     977             : /* }}} */
     978             : 
     979             : /* {{{ _xml_unparsedEntityDeclHandler() */
     980           3 : 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           3 :         xml_parser *parser = (xml_parser *)userData;
     988             : 
     989           6 :         if (parser && !Z_ISUNDEF(parser->unparsedEntityDeclHandler)) {
     990             :                 zval retval, args[6];
     991             : 
     992           3 :                 ZVAL_COPY(&args[0], &parser->index);
     993           3 :                 _xml_xmlchar_zval(entityName, 0, parser->target_encoding, &args[1]);
     994           3 :                 _xml_xmlchar_zval(base, 0, parser->target_encoding, &args[2]);
     995           3 :                 _xml_xmlchar_zval(systemId, 0, parser->target_encoding, &args[3]);
     996           3 :                 _xml_xmlchar_zval(publicId, 0, parser->target_encoding, &args[4]);
     997           3 :                 _xml_xmlchar_zval(notationName, 0, parser->target_encoding, &args[5]);
     998           3 :                 xml_call_handler(parser, &parser->unparsedEntityDeclHandler, parser->unparsedEntityDeclPtr, 6, args, &retval);
     999           3 :                 zval_ptr_dtor(&retval);
    1000             :         }
    1001           3 : }
    1002             : /* }}} */
    1003             : 
    1004             : /* {{{ _xml_notationDeclHandler() */
    1005           3 : 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           3 :         xml_parser *parser = (xml_parser *)userData;
    1012             : 
    1013           6 :         if (parser && !Z_ISUNDEF(parser->notationDeclHandler)) {
    1014             :                 zval retval, args[5];
    1015             : 
    1016           3 :                 ZVAL_COPY(&args[0], &parser->index);
    1017           3 :                 _xml_xmlchar_zval(notationName, 0, parser->target_encoding, &args[1]);
    1018           3 :                 _xml_xmlchar_zval(base, 0, parser->target_encoding, &args[2]);
    1019           3 :                 _xml_xmlchar_zval(systemId, 0, parser->target_encoding, &args[3]);
    1020           3 :                 _xml_xmlchar_zval(publicId, 0, parser->target_encoding, &args[4]);
    1021           3 :                 xml_call_handler(parser, &parser->notationDeclHandler, parser->notationDeclPtr, 5, args, &retval);
    1022           3 :                 zval_ptr_dtor(&retval);
    1023             :         }
    1024           3 : }
    1025             : /* }}} */
    1026             : 
    1027             : /* {{{ _xml_externalEntityRefHandler() */
    1028           0 : 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           0 :         xml_parser *parser = XML_GetUserData(parserPtr);
    1035           0 :         int ret = 0; /* abort if no handler is set (should be configurable?) */
    1036             : 
    1037           0 :         if (parser && !Z_ISUNDEF(parser->externalEntityRefHandler)) {
    1038             :                 zval retval, args[5];
    1039             : 
    1040           0 :                 ZVAL_COPY(&args[0], &parser->index);
    1041           0 :                 _xml_xmlchar_zval(openEntityNames, 0, parser->target_encoding, &args[1]);
    1042           0 :                 _xml_xmlchar_zval(base, 0, parser->target_encoding, &args[2]);
    1043           0 :                 _xml_xmlchar_zval(systemId, 0, parser->target_encoding, &args[3]);
    1044           0 :                 _xml_xmlchar_zval(publicId, 0, parser->target_encoding, &args[4]);
    1045           0 :                 xml_call_handler(parser, &parser->externalEntityRefHandler, parser->externalEntityRefPtr, 5, args, &retval);
    1046           0 :                 if (!Z_ISUNDEF(retval)) {
    1047           0 :                         convert_to_long(&retval);
    1048           0 :                         ret = Z_LVAL(retval);
    1049             :                 } else {
    1050           0 :                         ret = 0;
    1051             :                 }
    1052             :         }
    1053           0 :         return ret;
    1054             : }
    1055             : /* }}} */
    1056             : 
    1057             : /* {{{ _xml_startNamespaceDeclHandler() */
    1058           2 : void _xml_startNamespaceDeclHandler(void *userData,const XML_Char *prefix, const XML_Char *uri)
    1059             : {
    1060           2 :         xml_parser *parser = (xml_parser *)userData;
    1061             : 
    1062           4 :         if (parser && !Z_ISUNDEF(parser->startNamespaceDeclHandler)) {
    1063             :                 zval retval, args[3];
    1064             : 
    1065           2 :                 ZVAL_COPY(&args[0], &parser->index);
    1066           2 :                 _xml_xmlchar_zval(prefix, 0, parser->target_encoding, &args[1]);
    1067           2 :                 _xml_xmlchar_zval(uri, 0, parser->target_encoding, &args[2]);
    1068           2 :                 xml_call_handler(parser, &parser->startNamespaceDeclHandler, parser->startNamespaceDeclPtr, 3, args, &retval);
    1069           2 :                 zval_ptr_dtor(&retval);
    1070             :         }
    1071           2 : }
    1072             : /* }}} */
    1073             : 
    1074             : /* {{{ _xml_endNamespaceDeclHandler() */
    1075           0 : void _xml_endNamespaceDeclHandler(void *userData, const XML_Char *prefix)
    1076             : {
    1077           0 :         xml_parser *parser = (xml_parser *)userData;
    1078             : 
    1079           0 :         if (parser && !Z_ISUNDEF(parser->endNamespaceDeclHandler)) {
    1080             :                 zval retval, args[2];
    1081             : 
    1082           0 :                 ZVAL_COPY(&args[0], &parser->index);
    1083           0 :                 _xml_xmlchar_zval(prefix, 0, parser->target_encoding, &args[1]);
    1084           0 :                 xml_call_handler(parser, &parser->endNamespaceDeclHandler, parser->endNamespaceDeclPtr, 2, args, &retval);
    1085           0 :                 zval_ptr_dtor(&retval);
    1086             :         }
    1087           0 : }
    1088             : /* }}} */
    1089             : 
    1090             : /************************* EXTENSION FUNCTIONS *************************/
    1091             : 
    1092         206 : static void php_xml_parser_create_impl(INTERNAL_FUNCTION_PARAMETERS, int ns_support) /* {{{ */
    1093             : {
    1094             :         xml_parser *parser;
    1095         206 :         int auto_detect = 0;
    1096             : 
    1097         206 :         char *encoding_param = NULL;
    1098         206 :         size_t encoding_param_len = 0;
    1099             : 
    1100         206 :         char *ns_param = NULL;
    1101         206 :         size_t ns_param_len = 0;
    1102             :         
    1103             :         XML_Char *encoding;
    1104             :         
    1105         206 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), (ns_support ? "|ss": "|s"), &encoding_param, &encoding_param_len, &ns_param, &ns_param_len) == FAILURE) {
    1106          14 :                 RETURN_FALSE;
    1107             :         }
    1108             : 
    1109         192 :         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          83 :                 if (encoding_param_len == 0) {
    1114          36 :                         encoding = XML(default_encoding);
    1115          36 :                         auto_detect = 1;
    1116          47 :                 } else if (strcasecmp(encoding_param, "ISO-8859-1") == 0) {
    1117          11 :                         encoding = (XML_Char*)"ISO-8859-1";
    1118          36 :                 } else if (strcasecmp(encoding_param, "UTF-8") == 0) {
    1119           3 :                         encoding = (XML_Char*)"UTF-8";
    1120          33 :                 } else if (strcasecmp(encoding_param, "US-ASCII") == 0) {
    1121           2 :                         encoding = (XML_Char*)"US-ASCII";
    1122             :                 } else {
    1123          31 :                         php_error_docref(NULL, E_WARNING, "unsupported source encoding \"%s\"", encoding_param);
    1124          31 :                         RETURN_FALSE;
    1125             :                 }
    1126             :         } else {
    1127         109 :                 encoding = XML(default_encoding);
    1128             :         }
    1129             : 
    1130         161 :         if (ns_support && ns_param == NULL){
    1131          21 :                 ns_param = ":";
    1132             :         }
    1133             : 
    1134         161 :         parser = ecalloc(1, sizeof(xml_parser));
    1135         161 :         parser->parser = XML_ParserCreate_MM((auto_detect ? NULL : encoding),
    1136             :                                          &php_xml_mem_hdlrs, (XML_Char*)ns_param);
    1137             : 
    1138         161 :         parser->target_encoding = encoding;
    1139         161 :         parser->case_folding = 1;
    1140         161 :         parser->isparsing = 0;
    1141             : 
    1142         161 :         XML_SetUserData(parser->parser, parser);
    1143             : 
    1144         161 :         RETVAL_RES(zend_register_resource(parser, le_xml_parser));
    1145         161 :         ZVAL_COPY(&parser->index, return_value);
    1146             : }
    1147             : /* }}} */
    1148             : 
    1149             : /* {{{ proto resource xml_parser_create([string encoding]) 
    1150             :    Create an XML parser */
    1151         159 : PHP_FUNCTION(xml_parser_create)
    1152             : {
    1153         159 :         php_xml_parser_create_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);        
    1154         159 : }
    1155             : /* }}} */
    1156             : 
    1157             : /* {{{ proto resource xml_parser_create_ns([string encoding [, string sep]]) 
    1158             :    Create an XML parser */
    1159          47 : PHP_FUNCTION(xml_parser_create_ns)
    1160             : {
    1161          47 :         php_xml_parser_create_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
    1162          47 : }
    1163             : /* }}} */
    1164             : 
    1165             : /* {{{ proto int xml_set_object(resource parser, object &obj) 
    1166             :    Set up object which should be used for callbacks */
    1167          81 : PHP_FUNCTION(xml_set_object)
    1168             : {
    1169             :         xml_parser *parser;
    1170             :         zval *pind, *mythis;
    1171             : 
    1172          81 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ro/", &pind, &mythis) == FAILURE) {
    1173          56 :                 return;
    1174             :         }
    1175             : 
    1176          25 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1177           1 :                 RETURN_FALSE;
    1178             :         }
    1179             : 
    1180             :         /* please leave this commented - or ask thies@thieso.net before doing it (again) */
    1181          48 :         if (!Z_ISUNDEF(parser->object)) {
    1182           0 :                 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          24 :         ZVAL_COPY(&parser->object, mythis);
    1191             : 
    1192          24 :         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          63 : PHP_FUNCTION(xml_set_element_handler)
    1199             : {
    1200             :         xml_parser *parser;
    1201             :         zval *pind, *shdl, *ehdl;
    1202             : 
    1203          63 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rzz", &pind, &shdl, &ehdl) == FAILURE) {
    1204          29 :                 return;
    1205             :         }
    1206             : 
    1207          34 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1208           1 :                 RETURN_FALSE;
    1209             :         }
    1210             : 
    1211          33 :         xml_set_handler(&parser->startElementHandler, shdl);
    1212          33 :         xml_set_handler(&parser->endElementHandler, ehdl);
    1213          33 :         XML_SetElementHandler(parser->parser, _xml_startElementHandler, _xml_endElementHandler);
    1214          33 :         RETVAL_TRUE;
    1215             : }
    1216             : /* }}} */
    1217             : 
    1218             : /* {{{ proto int xml_set_character_data_handler(resource parser, string hdl) 
    1219             :    Set up character data handler */
    1220          33 : PHP_FUNCTION(xml_set_character_data_handler)
    1221             : {
    1222             :         xml_parser *parser;
    1223             :         zval *pind, *hdl;
    1224             : 
    1225          33 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pind, &hdl) == FAILURE) {
    1226          29 :                 return;
    1227             :         }
    1228             : 
    1229           4 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1230           1 :                 RETURN_FALSE;
    1231             :         }
    1232             : 
    1233           3 :         xml_set_handler(&parser->characterDataHandler, hdl);
    1234           3 :         XML_SetCharacterDataHandler(parser->parser, _xml_characterDataHandler);
    1235           3 :         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          31 : PHP_FUNCTION(xml_set_processing_instruction_handler)
    1242             : {
    1243             :         xml_parser *parser;
    1244             :         zval *pind, *hdl;
    1245             : 
    1246          31 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pind, &hdl) == FAILURE) {
    1247          29 :                 return;
    1248             :         }
    1249             : 
    1250           2 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1251           1 :                 RETURN_FALSE;
    1252             :         }
    1253             : 
    1254           1 :         xml_set_handler(&parser->processingInstructionHandler, hdl);
    1255           1 :         XML_SetProcessingInstructionHandler(parser->parser, _xml_processingInstructionHandler);
    1256           1 :         RETVAL_TRUE;
    1257             : }
    1258             : /* }}} */
    1259             : 
    1260             : /* {{{ proto int xml_set_default_handler(resource parser, string hdl) 
    1261             :    Set up default handler */
    1262          32 : PHP_FUNCTION(xml_set_default_handler)
    1263             : {
    1264             :         xml_parser *parser;
    1265             :         zval *pind, *hdl;
    1266             : 
    1267          32 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pind, &hdl) == FAILURE) {
    1268          29 :                 return;
    1269             :         }
    1270             : 
    1271           3 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1272           1 :                 RETURN_FALSE;
    1273             :         }
    1274             : 
    1275           2 :         xml_set_handler(&parser->defaultHandler, hdl);
    1276           2 :         XML_SetDefaultHandler(parser->parser, _xml_defaultHandler);
    1277           2 :         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          31 : PHP_FUNCTION(xml_set_unparsed_entity_decl_handler)
    1284             : {
    1285             :         xml_parser *parser;
    1286             :         zval *pind, *hdl;
    1287             : 
    1288          31 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pind, &hdl) == FAILURE) {
    1289          29 :                 return;
    1290             :         }
    1291             : 
    1292           2 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1293           1 :                 RETURN_FALSE;
    1294             :         }
    1295             : 
    1296           1 :         xml_set_handler(&parser->unparsedEntityDeclHandler, hdl);
    1297           1 :         XML_SetUnparsedEntityDeclHandler(parser->parser, _xml_unparsedEntityDeclHandler);
    1298           1 :         RETVAL_TRUE;
    1299             : }
    1300             : /* }}} */
    1301             : 
    1302             : /* {{{ proto int xml_set_notation_decl_handler(resource parser, string hdl) 
    1303             :    Set up notation declaration handler */
    1304          31 : PHP_FUNCTION(xml_set_notation_decl_handler)
    1305             : {
    1306             :         xml_parser *parser;
    1307             :         zval *pind, *hdl;
    1308             : 
    1309          31 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pind, &hdl) == FAILURE) {
    1310          29 :                 return;
    1311             :         }
    1312             : 
    1313           2 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1314           1 :                 RETURN_FALSE;
    1315             :         }
    1316             : 
    1317           1 :         xml_set_handler(&parser->notationDeclHandler, hdl);
    1318           1 :         XML_SetNotationDeclHandler(parser->parser, _xml_notationDeclHandler);
    1319           1 :         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          30 : PHP_FUNCTION(xml_set_external_entity_ref_handler)
    1326             : {
    1327             :         xml_parser *parser;
    1328             :         zval *pind, *hdl;
    1329             : 
    1330          30 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pind, &hdl) == FAILURE) {
    1331          29 :                 return;
    1332             :         }
    1333             : 
    1334           1 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1335           1 :                 RETURN_FALSE;
    1336             :         }
    1337             : 
    1338           0 :         xml_set_handler(&parser->externalEntityRefHandler, hdl);
    1339           0 :         XML_SetExternalEntityRefHandler(parser->parser, (void *) _xml_externalEntityRefHandler);
    1340           0 :         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          31 : PHP_FUNCTION(xml_set_start_namespace_decl_handler)
    1347             : {
    1348             :         xml_parser *parser;
    1349             :         zval *pind, *hdl;
    1350             : 
    1351          31 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pind, &hdl) == FAILURE) {
    1352          29 :                 return;
    1353             :         }
    1354             : 
    1355           2 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1356           1 :                 RETURN_FALSE;
    1357             :         }
    1358             : 
    1359           1 :         xml_set_handler(&parser->startNamespaceDeclHandler, hdl);
    1360           1 :         XML_SetStartNamespaceDeclHandler(parser->parser, _xml_startNamespaceDeclHandler);
    1361           1 :         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          31 : PHP_FUNCTION(xml_set_end_namespace_decl_handler)
    1368             : {
    1369             :         xml_parser *parser;
    1370             :         zval *pind, *hdl;
    1371             : 
    1372          31 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pind, &hdl) == FAILURE) {
    1373          29 :                 return;
    1374             :         }
    1375             : 
    1376           2 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1377           1 :                 RETURN_FALSE;
    1378             :         }
    1379             : 
    1380           1 :         xml_set_handler(&parser->endNamespaceDeclHandler, hdl);
    1381           1 :         XML_SetEndNamespaceDeclHandler(parser->parser, _xml_endNamespaceDeclHandler);
    1382           1 :         RETVAL_TRUE;
    1383             : }
    1384             : /* }}} */
    1385             : 
    1386             : /* {{{ proto int xml_parse(resource parser, string data [, int isFinal]) 
    1387             :    Start parsing an XML document */
    1388        3225 : PHP_FUNCTION(xml_parse)
    1389             : {
    1390             :         xml_parser *parser;
    1391             :         zval *pind;
    1392             :         char *data;
    1393             :         size_t data_len;
    1394             :         int ret;
    1395        3225 :         zend_long isFinal = 0;
    1396             : 
    1397        3225 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|l", &pind, &data, &data_len, &isFinal) == FAILURE) {
    1398          29 :                 return;
    1399             :         }
    1400             : 
    1401        3196 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1402           1 :                 RETURN_FALSE;
    1403             :         }
    1404             : 
    1405        3195 :         parser->isparsing = 1;
    1406        3195 :         ret = XML_Parse(parser->parser, (XML_Char*)data, data_len, isFinal);
    1407        3195 :         parser->isparsing = 0;
    1408        3195 :         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          36 : PHP_FUNCTION(xml_parse_into_struct)
    1417             : {
    1418             :         xml_parser *parser;
    1419          36 :         zval *pind, *xdata, *info = NULL;
    1420             :         char *data;
    1421             :         size_t data_len;
    1422             :         int ret;
    1423             : 
    1424          36 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rsz/|z/", &pind, &data, &data_len, &xdata, &info) == FAILURE) {
    1425          29 :                 return;
    1426             :         }
    1427             : 
    1428           7 :         if (info) {     
    1429           6 :                 zval_ptr_dtor(info);
    1430           6 :                 array_init(info);
    1431             :         }
    1432             : 
    1433           7 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1434           1 :                 RETURN_FALSE;
    1435             :         }
    1436             : 
    1437           6 :         zval_ptr_dtor(xdata);
    1438           6 :         array_init(xdata);
    1439             : 
    1440           6 :         ZVAL_COPY_VALUE(&parser->data, xdata);
    1441             :         
    1442           6 :         if (info) {
    1443           5 :                 ZVAL_COPY_VALUE(&parser->info, info);
    1444             :         }
    1445             :         
    1446           6 :         parser->level = 0;
    1447           6 :         parser->ltags = safe_emalloc(XML_MAXLEVEL, sizeof(char *), 0);
    1448             : 
    1449           6 :         XML_SetDefaultHandler(parser->parser, _xml_defaultHandler);
    1450           6 :         XML_SetElementHandler(parser->parser, _xml_startElementHandler, _xml_endElementHandler);
    1451           6 :         XML_SetCharacterDataHandler(parser->parser, _xml_characterDataHandler);
    1452             : 
    1453           6 :         parser->isparsing = 1;
    1454           6 :         ret = XML_Parse(parser->parser, (XML_Char*)data, data_len, 1);
    1455           6 :         parser->isparsing = 0;
    1456             : 
    1457           6 :         RETVAL_LONG(ret);
    1458             : }
    1459             : /* }}} */
    1460             : 
    1461             : /* {{{ proto int xml_get_error_code(resource parser) 
    1462             :    Get XML parser error code */
    1463          33 : PHP_FUNCTION(xml_get_error_code)
    1464             : {
    1465             :         xml_parser *parser;
    1466             :         zval *pind;
    1467             : 
    1468          33 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pind) == FAILURE) {
    1469          29 :                 return;
    1470             :         }
    1471             : 
    1472           4 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1473           1 :                 RETURN_FALSE;
    1474             :         }
    1475             : 
    1476           3 :         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          28 : PHP_FUNCTION(xml_error_string)
    1483             : {
    1484             :         zend_long code;
    1485             :         char *str;
    1486             : 
    1487          28 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &code) == FAILURE) {
    1488          12 :                 return;
    1489             :         }
    1490             : 
    1491          16 :         str = (char *)XML_ErrorString((int)code);
    1492          16 :         if (str) {
    1493          32 :                 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          36 : PHP_FUNCTION(xml_get_current_line_number)
    1501             : {
    1502             :         xml_parser *parser;
    1503             :         zval *pind;
    1504             : 
    1505          36 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pind) == FAILURE) {
    1506          29 :                 return;
    1507             :         }
    1508             : 
    1509           7 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1510           1 :                 RETURN_FALSE;
    1511             :         }
    1512             : 
    1513           6 :         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          36 : PHP_FUNCTION(xml_get_current_column_number)
    1520             : {
    1521             :         xml_parser *parser;
    1522             :         zval *pind;
    1523             : 
    1524          36 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pind) == FAILURE) {
    1525          29 :                 return;
    1526             :         }
    1527             : 
    1528           7 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1529           1 :                 RETURN_FALSE;
    1530             :         }
    1531             : 
    1532           6 :         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          36 : PHP_FUNCTION(xml_get_current_byte_index)
    1539             : {
    1540             :         xml_parser *parser;
    1541             :         zval *pind;
    1542             : 
    1543          36 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pind) == FAILURE) {
    1544          29 :                 return;
    1545             :         }
    1546             : 
    1547           7 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1548           1 :                 RETURN_FALSE;
    1549             :         }
    1550             : 
    1551           6 :         RETVAL_LONG(XML_GetCurrentByteIndex(parser->parser));
    1552             : }
    1553             : /* }}} */
    1554             : 
    1555             : /* {{{ proto int xml_parser_free(resource parser) 
    1556             :    Free an XML parser */
    1557          78 : PHP_FUNCTION(xml_parser_free)
    1558             : {
    1559             :         zval *pind;
    1560             :         xml_parser *parser;
    1561             :         zend_resource *res;
    1562             : 
    1563          78 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pind) == FAILURE) {
    1564          29 :                 return;
    1565             :         }
    1566             : 
    1567          49 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1568           1 :                 RETURN_FALSE;
    1569             :         }
    1570             : 
    1571          48 :         if (parser->isparsing == 1) {
    1572           0 :                 php_error_docref(NULL, E_WARNING, "Parser cannot be freed while it is parsing.");
    1573           0 :                 RETURN_FALSE;
    1574             :         }
    1575             : 
    1576          48 :         res = Z_RES(parser->index);
    1577          48 :         ZVAL_UNDEF(&parser->index);
    1578          48 :         zend_list_close(res);
    1579          48 :         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         120 : PHP_FUNCTION(xml_parser_set_option)
    1586             : {
    1587             :         xml_parser *parser;
    1588             :         zval *pind, *val;
    1589             :         zend_long opt;
    1590             : 
    1591         120 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlz", &pind, &opt, &val) == FAILURE) {
    1592          39 :                 return;
    1593             :         }
    1594             : 
    1595          81 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1596           1 :                 RETURN_FALSE;
    1597             :         }
    1598             : 
    1599          80 :         switch (opt) {
    1600             :                 case PHP_XML_OPTION_CASE_FOLDING:
    1601         134 :                         convert_to_long_ex(val);
    1602          62 :                         parser->case_folding = Z_LVAL_P(val);
    1603          62 :                         break;
    1604             :                 case PHP_XML_OPTION_SKIP_TAGSTART:
    1605           4 :                         convert_to_long_ex(val);
    1606           2 :                         parser->toffset = Z_LVAL_P(val);
    1607           2 :                         break;
    1608             :                 case PHP_XML_OPTION_SKIP_WHITE:
    1609           0 :                         convert_to_long_ex(val);
    1610           0 :                         parser->skipwhite = Z_LVAL_P(val);
    1611           0 :                         break;
    1612             :                 case PHP_XML_OPTION_TARGET_ENCODING: {
    1613             :                         xml_encoding *enc;
    1614           6 :                         convert_to_string_ex(val);
    1615           3 :                         enc = xml_get_encoding((XML_Char*)Z_STRVAL_P(val));
    1616           3 :                         if (enc == NULL) {
    1617           0 :                                 php_error_docref(NULL, E_WARNING, "Unsupported target encoding \"%s\"", Z_STRVAL_P(val));
    1618           0 :                                 RETURN_FALSE;
    1619             :                         }
    1620           3 :                         parser->target_encoding = enc->name;
    1621           3 :                         break;
    1622             :                 }
    1623             :                 default:
    1624          13 :                         php_error_docref(NULL, E_WARNING, "Unknown option");
    1625          13 :                         RETURN_FALSE;
    1626             :                         break;
    1627             :         }
    1628          67 :         RETVAL_TRUE;
    1629             : }
    1630             : /* }}} */
    1631             : 
    1632             : /* {{{ proto int xml_parser_get_option(resource parser, int option) 
    1633             :    Get options from an XML parser */
    1634          63 : PHP_FUNCTION(xml_parser_get_option)
    1635             : {
    1636             :         xml_parser *parser;
    1637             :         zval *pind;
    1638             :         zend_long opt;
    1639             : 
    1640          63 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &pind, &opt) == FAILURE) {
    1641          40 :                 return;
    1642             :         }
    1643             : 
    1644          23 :         if ((parser = (xml_parser *)zend_fetch_resource(Z_RES_P(pind), "XML Parser", le_xml_parser)) == NULL) {
    1645           1 :                 RETURN_FALSE;
    1646             :         }
    1647             : 
    1648          22 :         switch (opt) {
    1649             :                 case PHP_XML_OPTION_CASE_FOLDING:
    1650           5 :                         RETURN_LONG(parser->case_folding);
    1651             :                         break;
    1652             :                 case PHP_XML_OPTION_TARGET_ENCODING:
    1653           8 :                         RETURN_STRING((char *)parser->target_encoding);
    1654             :                         break;
    1655             :                 default:
    1656          13 :                         php_error_docref(NULL, E_WARNING, "Unknown option");
    1657          13 :                         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          32 : PHP_FUNCTION(utf8_encode)
    1668             : {
    1669             :         char *arg;
    1670             :         size_t arg_len;
    1671             :         zend_string *encoded;
    1672             : 
    1673          32 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
    1674           7 :                 return;
    1675             :         }
    1676             : 
    1677          25 :         encoded = xml_utf8_encode(arg, arg_len, (XML_Char*)"ISO-8859-1");
    1678          25 :         if (encoded == NULL) {
    1679           0 :                 RETURN_FALSE;
    1680             :         }
    1681          25 :         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          35 : PHP_FUNCTION(utf8_decode)
    1688             : {
    1689             :         char *arg;
    1690             :         size_t arg_len;
    1691             :         zend_string *decoded;
    1692             : 
    1693          35 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
    1694           7 :                 return;
    1695             :         }
    1696             : 
    1697          28 :         decoded = xml_utf8_decode((XML_Char*)arg, arg_len, (XML_Char*)"ISO-8859-1");
    1698          28 :         if (decoded == NULL) {
    1699           0 :                 RETURN_FALSE;
    1700             :         }
    1701          28 :         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             :  */

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:59:07 +0000 (38 hours ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.