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: 550 629 87.4 %
Date: 2015-01-26 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             :     NO_VERSION_YET,
     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       20786 : static PHP_GINIT_FUNCTION(xml)
     295             : {
     296             : #if defined(COMPILE_DL_XML) && defined(ZTS)
     297             :         ZEND_TSRMLS_CACHE_UPDATE;
     298             : #endif
     299       20786 :         xml_globals->default_encoding = (XML_Char*)"UTF-8";
     300       20786 : }
     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       20786 : PHP_MINIT_FUNCTION(xml)
     320             : {
     321       20786 :         le_xml_parser = zend_register_list_destructors_ex(xml_parser_dtor, NULL, "xml", module_number);
     322             : 
     323       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_NONE", XML_ERROR_NONE, CONST_CS|CONST_PERSISTENT);
     324       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_NO_MEMORY", XML_ERROR_NO_MEMORY, CONST_CS|CONST_PERSISTENT);
     325       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_SYNTAX", XML_ERROR_SYNTAX, CONST_CS|CONST_PERSISTENT);
     326       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_NO_ELEMENTS", XML_ERROR_NO_ELEMENTS, CONST_CS|CONST_PERSISTENT);
     327       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_INVALID_TOKEN", XML_ERROR_INVALID_TOKEN, CONST_CS|CONST_PERSISTENT);
     328       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_UNCLOSED_TOKEN", XML_ERROR_UNCLOSED_TOKEN, CONST_CS|CONST_PERSISTENT);
     329       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_PARTIAL_CHAR", XML_ERROR_PARTIAL_CHAR, CONST_CS|CONST_PERSISTENT);
     330       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_TAG_MISMATCH", XML_ERROR_TAG_MISMATCH, CONST_CS|CONST_PERSISTENT);
     331       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_DUPLICATE_ATTRIBUTE", XML_ERROR_DUPLICATE_ATTRIBUTE, CONST_CS|CONST_PERSISTENT);
     332       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_JUNK_AFTER_DOC_ELEMENT", XML_ERROR_JUNK_AFTER_DOC_ELEMENT, CONST_CS|CONST_PERSISTENT);
     333       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_PARAM_ENTITY_REF", XML_ERROR_PARAM_ENTITY_REF, CONST_CS|CONST_PERSISTENT);
     334       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_UNDEFINED_ENTITY", XML_ERROR_UNDEFINED_ENTITY, CONST_CS|CONST_PERSISTENT);
     335       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_RECURSIVE_ENTITY_REF", XML_ERROR_RECURSIVE_ENTITY_REF, CONST_CS|CONST_PERSISTENT);
     336       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_ASYNC_ENTITY", XML_ERROR_ASYNC_ENTITY, CONST_CS|CONST_PERSISTENT);
     337       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_BAD_CHAR_REF", XML_ERROR_BAD_CHAR_REF, CONST_CS|CONST_PERSISTENT);
     338       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_BINARY_ENTITY_REF", XML_ERROR_BINARY_ENTITY_REF, CONST_CS|CONST_PERSISTENT);
     339       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF", XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF, CONST_CS|CONST_PERSISTENT);
     340       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_MISPLACED_XML_PI", XML_ERROR_MISPLACED_XML_PI, CONST_CS|CONST_PERSISTENT);
     341       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_UNKNOWN_ENCODING", XML_ERROR_UNKNOWN_ENCODING, CONST_CS|CONST_PERSISTENT);
     342       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_INCORRECT_ENCODING", XML_ERROR_INCORRECT_ENCODING, CONST_CS|CONST_PERSISTENT);
     343       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_UNCLOSED_CDATA_SECTION", XML_ERROR_UNCLOSED_CDATA_SECTION, CONST_CS|CONST_PERSISTENT);
     344       20786 :         REGISTER_LONG_CONSTANT("XML_ERROR_EXTERNAL_ENTITY_HANDLING", XML_ERROR_EXTERNAL_ENTITY_HANDLING, CONST_CS|CONST_PERSISTENT);
     345             : 
     346       20786 :         REGISTER_LONG_CONSTANT("XML_OPTION_CASE_FOLDING", PHP_XML_OPTION_CASE_FOLDING, CONST_CS|CONST_PERSISTENT);
     347       20786 :         REGISTER_LONG_CONSTANT("XML_OPTION_TARGET_ENCODING", PHP_XML_OPTION_TARGET_ENCODING, CONST_CS|CONST_PERSISTENT);
     348       20786 :         REGISTER_LONG_CONSTANT("XML_OPTION_SKIP_TAGSTART", PHP_XML_OPTION_SKIP_TAGSTART, CONST_CS|CONST_PERSISTENT);
     349       20786 :         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       20786 :         php_xml_mem_hdlrs.malloc_fcn = php_xml_malloc_wrapper;
     355       20786 :         php_xml_mem_hdlrs.realloc_fcn = php_xml_realloc_wrapper;
     356       20786 :         php_xml_mem_hdlrs.free_fcn = php_xml_free_wrapper;
     357             : 
     358             : #ifdef LIBXML_EXPAT_COMPAT
     359       20786 :         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       20786 :         return SUCCESS;
     365             : }
     366             : 
     367         144 : PHP_MINFO_FUNCTION(xml)
     368             : {
     369         144 :         php_info_print_table_start();
     370         144 :         php_info_print_table_row(2, "XML Support", "active");
     371         144 :         php_info_print_table_row(2, "XML Namespace Support", "active");
     372             : #if defined(LIBXML_DOTTED_VERSION) && defined(LIBXML_EXPAT_COMPAT)
     373         144 :         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         144 :         php_info_print_table_end();
     378         144 : }
     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         160 : static void xml_parser_dtor(zend_resource *rsrc)
     398             : {
     399         160 :         xml_parser *parser = (xml_parser *)rsrc->ptr;
     400             :         
     401         160 :         if (parser->parser) {
     402         160 :                 XML_ParserFree(parser->parser);
     403             :         }
     404         160 :         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         320 :         if (!Z_ISUNDEF(parser->startElementHandler)) {
     411          32 :                 zval_ptr_dtor(&parser->startElementHandler);
     412             :         }
     413         320 :         if (!Z_ISUNDEF(parser->endElementHandler)) {
     414          31 :                 zval_ptr_dtor(&parser->endElementHandler);
     415             :         }
     416         320 :         if (!Z_ISUNDEF(parser->characterDataHandler)) {
     417           3 :                 zval_ptr_dtor(&parser->characterDataHandler);
     418             :         }
     419         320 :         if (!Z_ISUNDEF(parser->processingInstructionHandler)) {
     420           1 :                 zval_ptr_dtor(&parser->processingInstructionHandler);
     421             :         }
     422         320 :         if (!Z_ISUNDEF(parser->defaultHandler)) {
     423           2 :                 zval_ptr_dtor(&parser->defaultHandler);
     424             :         }
     425         320 :         if (!Z_ISUNDEF(parser->unparsedEntityDeclHandler)) {
     426           1 :                 zval_ptr_dtor(&parser->unparsedEntityDeclHandler);
     427             :         }
     428         320 :         if (!Z_ISUNDEF(parser->notationDeclHandler)) {
     429           1 :                 zval_ptr_dtor(&parser->notationDeclHandler);
     430             :         }
     431         320 :         if (!Z_ISUNDEF(parser->externalEntityRefHandler)) {
     432           0 :                 zval_ptr_dtor(&parser->externalEntityRefHandler);
     433             :         }
     434         320 :         if (!Z_ISUNDEF(parser->unknownEncodingHandler)) {
     435           0 :                 zval_ptr_dtor(&parser->unknownEncodingHandler);
     436             :         }
     437         320 :         if (!Z_ISUNDEF(parser->startNamespaceDeclHandler)) {
     438           1 :                 zval_ptr_dtor(&parser->startNamespaceDeclHandler);
     439             :         }
     440         320 :         if (!Z_ISUNDEF(parser->endNamespaceDeclHandler)) {
     441           1 :                 zval_ptr_dtor(&parser->endNamespaceDeclHandler);
     442             :         }
     443         160 :         if (parser->baseURI) {
     444           0 :                 efree(parser->baseURI);
     445             :         }
     446         320 :         if (!Z_ISUNDEF(parser->object)) {
     447          24 :                 zval_ptr_dtor(&parser->object);
     448             :         }
     449             : 
     450         160 :         efree(parser);
     451         160 : }
     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 : PHPAPI 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_realloc(str, str->len, 0);
     608          25 :         return str;
     609             : }
     610             : /* }}} */
     611             : 
     612             : /* {{{ xml_utf8_decode() */
     613        1363 : PHPAPI 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_realloc(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 : PHPAPI 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_realloc(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_realloc(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         205 : static void php_xml_parser_create_impl(INTERNAL_FUNCTION_PARAMETERS, int ns_support) /* {{{ */
    1093             : {
    1094             :         xml_parser *parser;
    1095         205 :         int auto_detect = 0;
    1096             : 
    1097         205 :         char *encoding_param = NULL;
    1098         205 :         size_t encoding_param_len = 0;
    1099             : 
    1100         205 :         char *ns_param = NULL;
    1101         205 :         size_t ns_param_len = 0;
    1102             :         
    1103             :         XML_Char *encoding;
    1104             :         
    1105         205 :         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         191 :         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         108 :                 encoding = XML(default_encoding);
    1128             :         }
    1129             : 
    1130         160 :         if (ns_support && ns_param == NULL){
    1131          20 :                 ns_param = ":";
    1132             :         }
    1133             : 
    1134         160 :         parser = ecalloc(1, sizeof(xml_parser));
    1135         160 :         parser->parser = XML_ParserCreate_MM((auto_detect ? NULL : encoding),
    1136             :                                          &php_xml_mem_hdlrs, (XML_Char*)ns_param);
    1137             : 
    1138         160 :         parser->target_encoding = encoding;
    1139         160 :         parser->case_folding = 1;
    1140         160 :         parser->isparsing = 0;
    1141             : 
    1142         160 :         XML_SetUserData(parser->parser, parser);
    1143             : 
    1144         160 :         ZEND_REGISTER_RESOURCE(return_value, parser, le_xml_parser);
    1145         160 :         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          46 : PHP_FUNCTION(xml_parser_create_ns)
    1160             : {
    1161          46 :         php_xml_parser_create_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
    1162          46 : }
    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 :         ZEND_FETCH_RESOURCE(parser,xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1177             : 
    1178             :         /* please leave this commented - or ask thies@thieso.net before doing it (again) */
    1179          48 :         if (!Z_ISUNDEF(parser->object)) {
    1180           0 :                 zval_ptr_dtor(&parser->object);
    1181             :         }
    1182             : 
    1183             :         /* please leave this commented - or ask thies@thieso.net before doing it (again) */
    1184             : /* #ifdef ZEND_ENGINE_2
    1185             :         zval_add_ref(&parser->object); 
    1186             : #endif */
    1187             : 
    1188          24 :         ZVAL_COPY(&parser->object, mythis);
    1189             : 
    1190          24 :         RETVAL_TRUE;
    1191             : }
    1192             : /* }}} */
    1193             : 
    1194             : /* {{{ proto int xml_set_element_handler(resource parser, string shdl, string ehdl) 
    1195             :    Set up start and end element handlers */
    1196          63 : PHP_FUNCTION(xml_set_element_handler)
    1197             : {
    1198             :         xml_parser *parser;
    1199             :         zval *pind, *shdl, *ehdl;
    1200             : 
    1201          63 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rzz", &pind, &shdl, &ehdl) == FAILURE) {
    1202          29 :                 return;
    1203             :         }
    1204             : 
    1205          34 :         ZEND_FETCH_RESOURCE(parser, xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1206             : 
    1207          33 :         xml_set_handler(&parser->startElementHandler, shdl);
    1208          33 :         xml_set_handler(&parser->endElementHandler, ehdl);
    1209          33 :         XML_SetElementHandler(parser->parser, _xml_startElementHandler, _xml_endElementHandler);
    1210          33 :         RETVAL_TRUE;
    1211             : }
    1212             : /* }}} */
    1213             : 
    1214             : /* {{{ proto int xml_set_character_data_handler(resource parser, string hdl) 
    1215             :    Set up character data handler */
    1216          33 : PHP_FUNCTION(xml_set_character_data_handler)
    1217             : {
    1218             :         xml_parser *parser;
    1219             :         zval *pind, *hdl;
    1220             : 
    1221          33 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pind, &hdl) == FAILURE) {
    1222          29 :                 return;
    1223             :         }
    1224             : 
    1225           4 :         ZEND_FETCH_RESOURCE(parser, xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1226             : 
    1227           3 :         xml_set_handler(&parser->characterDataHandler, hdl);
    1228           3 :         XML_SetCharacterDataHandler(parser->parser, _xml_characterDataHandler);
    1229           3 :         RETVAL_TRUE;
    1230             : }
    1231             : /* }}} */
    1232             : 
    1233             : /* {{{ proto int xml_set_processing_instruction_handler(resource parser, string hdl) 
    1234             :    Set up processing instruction (PI) handler */
    1235          31 : PHP_FUNCTION(xml_set_processing_instruction_handler)
    1236             : {
    1237             :         xml_parser *parser;
    1238             :         zval *pind, *hdl;
    1239             : 
    1240          31 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pind, &hdl) == FAILURE) {
    1241          29 :                 return;
    1242             :         }
    1243             : 
    1244           2 :         ZEND_FETCH_RESOURCE(parser, xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1245             : 
    1246           1 :         xml_set_handler(&parser->processingInstructionHandler, hdl);
    1247           1 :         XML_SetProcessingInstructionHandler(parser->parser, _xml_processingInstructionHandler);
    1248           1 :         RETVAL_TRUE;
    1249             : }
    1250             : /* }}} */
    1251             : 
    1252             : /* {{{ proto int xml_set_default_handler(resource parser, string hdl) 
    1253             :    Set up default handler */
    1254          32 : PHP_FUNCTION(xml_set_default_handler)
    1255             : {
    1256             :         xml_parser *parser;
    1257             :         zval *pind, *hdl;
    1258             : 
    1259          32 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pind, &hdl) == FAILURE) {
    1260          29 :                 return;
    1261             :         }
    1262             : 
    1263           3 :         ZEND_FETCH_RESOURCE(parser, xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1264             : 
    1265           2 :         xml_set_handler(&parser->defaultHandler, hdl);
    1266           2 :         XML_SetDefaultHandler(parser->parser, _xml_defaultHandler);
    1267           2 :         RETVAL_TRUE;
    1268             : }
    1269             : /* }}} */
    1270             : 
    1271             : /* {{{ proto int xml_set_unparsed_entity_decl_handler(resource parser, string hdl) 
    1272             :    Set up unparsed entity declaration handler */
    1273          31 : PHP_FUNCTION(xml_set_unparsed_entity_decl_handler)
    1274             : {
    1275             :         xml_parser *parser;
    1276             :         zval *pind, *hdl;
    1277             : 
    1278          31 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pind, &hdl) == FAILURE) {
    1279          29 :                 return;
    1280             :         }
    1281             : 
    1282           2 :         ZEND_FETCH_RESOURCE(parser, xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1283             : 
    1284           1 :         xml_set_handler(&parser->unparsedEntityDeclHandler, hdl);
    1285           1 :         XML_SetUnparsedEntityDeclHandler(parser->parser, _xml_unparsedEntityDeclHandler);
    1286           1 :         RETVAL_TRUE;
    1287             : }
    1288             : /* }}} */
    1289             : 
    1290             : /* {{{ proto int xml_set_notation_decl_handler(resource parser, string hdl) 
    1291             :    Set up notation declaration handler */
    1292          31 : PHP_FUNCTION(xml_set_notation_decl_handler)
    1293             : {
    1294             :         xml_parser *parser;
    1295             :         zval *pind, *hdl;
    1296             : 
    1297          31 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pind, &hdl) == FAILURE) {
    1298          29 :                 return;
    1299             :         }
    1300           2 :         ZEND_FETCH_RESOURCE(parser, xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1301             : 
    1302           1 :         xml_set_handler(&parser->notationDeclHandler, hdl);
    1303           1 :         XML_SetNotationDeclHandler(parser->parser, _xml_notationDeclHandler);
    1304           1 :         RETVAL_TRUE;
    1305             : }
    1306             : /* }}} */
    1307             : 
    1308             : /* {{{ proto int xml_set_external_entity_ref_handler(resource parser, string hdl) 
    1309             :    Set up external entity reference handler */
    1310          30 : PHP_FUNCTION(xml_set_external_entity_ref_handler)
    1311             : {
    1312             :         xml_parser *parser;
    1313             :         zval *pind, *hdl;
    1314             : 
    1315          30 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pind, &hdl) == FAILURE) {
    1316          29 :                 return;
    1317             :         }
    1318           1 :         ZEND_FETCH_RESOURCE(parser, xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1319             : 
    1320           0 :         xml_set_handler(&parser->externalEntityRefHandler, hdl);
    1321           0 :         XML_SetExternalEntityRefHandler(parser->parser, (void *) _xml_externalEntityRefHandler);
    1322           0 :         RETVAL_TRUE;
    1323             : }
    1324             : /* }}} */
    1325             : 
    1326             : /* {{{ proto int xml_set_start_namespace_decl_handler(resource parser, string hdl) 
    1327             :    Set up character data handler */
    1328          31 : PHP_FUNCTION(xml_set_start_namespace_decl_handler)
    1329             : {
    1330             :         xml_parser *parser;
    1331             :         zval *pind, *hdl;
    1332             : 
    1333          31 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pind, &hdl) == FAILURE) {
    1334          29 :                 return;
    1335             :         }
    1336             : 
    1337           2 :         ZEND_FETCH_RESOURCE(parser, xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1338             : 
    1339           1 :         xml_set_handler(&parser->startNamespaceDeclHandler, hdl);
    1340           1 :         XML_SetStartNamespaceDeclHandler(parser->parser, _xml_startNamespaceDeclHandler);
    1341           1 :         RETVAL_TRUE;
    1342             : }
    1343             : /* }}} */
    1344             : 
    1345             : /* {{{ proto int xml_set_end_namespace_decl_handler(resource parser, string hdl) 
    1346             :    Set up character data handler */
    1347          31 : PHP_FUNCTION(xml_set_end_namespace_decl_handler)
    1348             : {
    1349             :         xml_parser *parser;
    1350             :         zval *pind, *hdl;
    1351             : 
    1352          31 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &pind, &hdl) == FAILURE) {
    1353          29 :                 return;
    1354             :         }
    1355             : 
    1356           2 :         ZEND_FETCH_RESOURCE(parser, xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1357             : 
    1358           1 :         xml_set_handler(&parser->endNamespaceDeclHandler, hdl);
    1359           1 :         XML_SetEndNamespaceDeclHandler(parser->parser, _xml_endNamespaceDeclHandler);
    1360           1 :         RETVAL_TRUE;
    1361             : }
    1362             : /* }}} */
    1363             : 
    1364             : /* {{{ proto int xml_parse(resource parser, string data [, int isFinal]) 
    1365             :    Start parsing an XML document */
    1366        3225 : PHP_FUNCTION(xml_parse)
    1367             : {
    1368             :         xml_parser *parser;
    1369             :         zval *pind;
    1370             :         char *data;
    1371             :         size_t data_len;
    1372             :         int ret;
    1373        3225 :         zend_long isFinal = 0;
    1374             : 
    1375        3225 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|l", &pind, &data, &data_len, &isFinal) == FAILURE) {
    1376          29 :                 return;
    1377             :         }
    1378        3196 :         ZEND_FETCH_RESOURCE(parser, xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1379             : 
    1380        3195 :         parser->isparsing = 1;
    1381        3195 :         ret = XML_Parse(parser->parser, (XML_Char*)data, data_len, isFinal);
    1382        3195 :         parser->isparsing = 0;
    1383        3195 :         RETVAL_LONG(ret);
    1384             : }
    1385             : 
    1386             : /* }}} */
    1387             : 
    1388             : /* {{{ proto int xml_parse_into_struct(resource parser, string data, array &values [, array &index ])
    1389             :    Parsing a XML document */
    1390             : 
    1391          36 : PHP_FUNCTION(xml_parse_into_struct)
    1392             : {
    1393             :         xml_parser *parser;
    1394          36 :         zval *pind, *xdata, *info = NULL;
    1395             :         char *data;
    1396             :         size_t data_len;
    1397             :         int ret;
    1398             : 
    1399          36 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rsz/|z/", &pind, &data, &data_len, &xdata, &info) == FAILURE) {
    1400          29 :                 return;
    1401             :         }
    1402             : 
    1403           7 :         if (info) {     
    1404           6 :                 zval_ptr_dtor(info);
    1405           6 :                 array_init(info);
    1406             :         }
    1407             : 
    1408           7 :         ZEND_FETCH_RESOURCE(parser, xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1409             : 
    1410           6 :         zval_ptr_dtor(xdata);
    1411           6 :         array_init(xdata);
    1412             : 
    1413           6 :         ZVAL_COPY_VALUE(&parser->data, xdata);
    1414             :         
    1415           6 :         if (info) {
    1416           5 :                 ZVAL_COPY_VALUE(&parser->info, info);
    1417             :         }
    1418             :         
    1419           6 :         parser->level = 0;
    1420           6 :         parser->ltags = safe_emalloc(XML_MAXLEVEL, sizeof(char *), 0);
    1421             : 
    1422           6 :         XML_SetDefaultHandler(parser->parser, _xml_defaultHandler);
    1423           6 :         XML_SetElementHandler(parser->parser, _xml_startElementHandler, _xml_endElementHandler);
    1424           6 :         XML_SetCharacterDataHandler(parser->parser, _xml_characterDataHandler);
    1425             : 
    1426           6 :         parser->isparsing = 1;
    1427           6 :         ret = XML_Parse(parser->parser, (XML_Char*)data, data_len, 1);
    1428           6 :         parser->isparsing = 0;
    1429             : 
    1430           6 :         RETVAL_LONG(ret);
    1431             : }
    1432             : /* }}} */
    1433             : 
    1434             : /* {{{ proto int xml_get_error_code(resource parser) 
    1435             :    Get XML parser error code */
    1436          33 : PHP_FUNCTION(xml_get_error_code)
    1437             : {
    1438             :         xml_parser *parser;
    1439             :         zval *pind;
    1440             : 
    1441          33 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pind) == FAILURE) {
    1442          29 :                 return;
    1443             :         }
    1444             : 
    1445           4 :         ZEND_FETCH_RESOURCE(parser,xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1446             : 
    1447           3 :         RETURN_LONG((zend_long)XML_GetErrorCode(parser->parser));
    1448             : }
    1449             : /* }}} */
    1450             : 
    1451             : /* {{{ proto string xml_error_string(int code)
    1452             :    Get XML parser error string */
    1453          28 : PHP_FUNCTION(xml_error_string)
    1454             : {
    1455             :         zend_long code;
    1456             :         char *str;
    1457             : 
    1458          28 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &code) == FAILURE) {
    1459          12 :                 return;
    1460             :         }
    1461             : 
    1462          16 :         str = (char *)XML_ErrorString((int)code);
    1463          16 :         if (str) {
    1464          32 :                 RETVAL_STRING(str);
    1465             :         }
    1466             : }
    1467             : /* }}} */
    1468             : 
    1469             : /* {{{ proto int xml_get_current_line_number(resource parser) 
    1470             :    Get current line number for an XML parser */
    1471          36 : PHP_FUNCTION(xml_get_current_line_number)
    1472             : {
    1473             :         xml_parser *parser;
    1474             :         zval *pind;
    1475             : 
    1476          36 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pind) == FAILURE) {
    1477          29 :                 return;
    1478             :         }
    1479             : 
    1480           7 :         ZEND_FETCH_RESOURCE(parser, xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1481             : 
    1482           6 :         RETVAL_LONG(XML_GetCurrentLineNumber(parser->parser));
    1483             : }
    1484             : /* }}} */
    1485             : 
    1486             : /* {{{ proto int xml_get_current_column_number(resource parser)
    1487             :    Get current column number for an XML parser */
    1488          36 : PHP_FUNCTION(xml_get_current_column_number)
    1489             : {
    1490             :         xml_parser *parser;
    1491             :         zval *pind;
    1492             : 
    1493          36 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pind) == FAILURE) {
    1494          29 :                 return;
    1495             :         }
    1496             : 
    1497           7 :         ZEND_FETCH_RESOURCE(parser, xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1498             : 
    1499           6 :         RETVAL_LONG(XML_GetCurrentColumnNumber(parser->parser));
    1500             : }
    1501             : /* }}} */
    1502             : 
    1503             : /* {{{ proto int xml_get_current_byte_index(resource parser) 
    1504             :    Get current byte index for an XML parser */
    1505          36 : PHP_FUNCTION(xml_get_current_byte_index)
    1506             : {
    1507             :         xml_parser *parser;
    1508             :         zval *pind;
    1509             : 
    1510          36 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pind) == FAILURE) {
    1511          29 :                 return;
    1512             :         }
    1513             : 
    1514           7 :         ZEND_FETCH_RESOURCE(parser, xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1515             : 
    1516           6 :         RETVAL_LONG(XML_GetCurrentByteIndex(parser->parser));
    1517             : }
    1518             : /* }}} */
    1519             : 
    1520             : /* {{{ proto int xml_parser_free(resource parser) 
    1521             :    Free an XML parser */
    1522          78 : PHP_FUNCTION(xml_parser_free)
    1523             : {
    1524             :         zval *pind;
    1525             :         xml_parser *parser;
    1526             :         zend_resource *res;
    1527             : 
    1528          78 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &pind) == FAILURE) {
    1529          29 :                 return;
    1530             :         }
    1531             : 
    1532          49 :         ZEND_FETCH_RESOURCE(parser, xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1533             : 
    1534          48 :         if (parser->isparsing == 1) {
    1535           0 :                 php_error_docref(NULL, E_WARNING, "Parser cannot be freed while it is parsing.");
    1536           0 :                 RETURN_FALSE;
    1537             :         }
    1538             : 
    1539          48 :         res = Z_RES(parser->index);
    1540          48 :         ZVAL_UNDEF(&parser->index);
    1541          48 :         zend_list_close(res);
    1542          48 :         RETURN_TRUE;
    1543             : }
    1544             : /* }}} */
    1545             : 
    1546             : /* {{{ proto int xml_parser_set_option(resource parser, int option, mixed value) 
    1547             :    Set options in an XML parser */
    1548         120 : PHP_FUNCTION(xml_parser_set_option)
    1549             : {
    1550             :         xml_parser *parser;
    1551             :         zval *pind, *val;
    1552             :         zend_long opt;
    1553             : 
    1554         120 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rlz", &pind, &opt, &val) == FAILURE) {
    1555          39 :                 return;
    1556             :         }
    1557             : 
    1558          81 :         ZEND_FETCH_RESOURCE(parser, xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1559             : 
    1560          80 :         switch (opt) {
    1561             :                 case PHP_XML_OPTION_CASE_FOLDING:
    1562         134 :                         convert_to_long_ex(val);
    1563          62 :                         parser->case_folding = Z_LVAL_P(val);
    1564          62 :                         break;
    1565             :                 case PHP_XML_OPTION_SKIP_TAGSTART:
    1566           4 :                         convert_to_long_ex(val);
    1567           2 :                         parser->toffset = Z_LVAL_P(val);
    1568           2 :                         break;
    1569             :                 case PHP_XML_OPTION_SKIP_WHITE:
    1570           0 :                         convert_to_long_ex(val);
    1571           0 :                         parser->skipwhite = Z_LVAL_P(val);
    1572           0 :                         break;
    1573             :                 case PHP_XML_OPTION_TARGET_ENCODING: {
    1574             :                         xml_encoding *enc;
    1575           6 :                         convert_to_string_ex(val);
    1576           3 :                         enc = xml_get_encoding((XML_Char*)Z_STRVAL_P(val));
    1577           3 :                         if (enc == NULL) {
    1578           0 :                                 php_error_docref(NULL, E_WARNING, "Unsupported target encoding \"%s\"", Z_STRVAL_P(val));
    1579           0 :                                 RETURN_FALSE;
    1580             :                         }
    1581           3 :                         parser->target_encoding = enc->name;
    1582           3 :                         break;
    1583             :                 }
    1584             :                 default:
    1585          13 :                         php_error_docref(NULL, E_WARNING, "Unknown option");
    1586          13 :                         RETURN_FALSE;
    1587             :                         break;
    1588             :         }
    1589          67 :         RETVAL_TRUE;
    1590             : }
    1591             : /* }}} */
    1592             : 
    1593             : /* {{{ proto int xml_parser_get_option(resource parser, int option) 
    1594             :    Get options from an XML parser */
    1595          63 : PHP_FUNCTION(xml_parser_get_option)
    1596             : {
    1597             :         xml_parser *parser;
    1598             :         zval *pind;
    1599             :         zend_long opt;
    1600             : 
    1601          63 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &pind, &opt) == FAILURE) {
    1602          40 :                 return;
    1603             :         }
    1604          23 :         ZEND_FETCH_RESOURCE(parser, xml_parser *, pind, -1, "XML Parser", le_xml_parser);
    1605             : 
    1606          22 :         switch (opt) {
    1607             :                 case PHP_XML_OPTION_CASE_FOLDING:
    1608           5 :                         RETURN_LONG(parser->case_folding);
    1609             :                         break;
    1610             :                 case PHP_XML_OPTION_TARGET_ENCODING:
    1611           8 :                         RETURN_STRING((char *)parser->target_encoding);
    1612             :                         break;
    1613             :                 default:
    1614          13 :                         php_error_docref(NULL, E_WARNING, "Unknown option");
    1615          13 :                         RETURN_FALSE;
    1616             :                         break;
    1617             :         }
    1618             : 
    1619             :         RETVAL_FALSE;   /* never reached */
    1620             : }
    1621             : /* }}} */
    1622             : 
    1623             : /* {{{ proto string utf8_encode(string data) 
    1624             :    Encodes an ISO-8859-1 string to UTF-8 */
    1625          32 : PHP_FUNCTION(utf8_encode)
    1626             : {
    1627             :         char *arg;
    1628             :         size_t arg_len;
    1629             :         zend_string *encoded;
    1630             : 
    1631          32 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
    1632           7 :                 return;
    1633             :         }
    1634             : 
    1635          25 :         encoded = xml_utf8_encode(arg, arg_len, (XML_Char*)"ISO-8859-1");
    1636          25 :         if (encoded == NULL) {
    1637           0 :                 RETURN_FALSE;
    1638             :         }
    1639          25 :         RETURN_STR(encoded);
    1640             : }
    1641             : /* }}} */
    1642             : 
    1643             : /* {{{ proto string utf8_decode(string data) 
    1644             :    Converts a UTF-8 encoded string to ISO-8859-1 */
    1645          35 : PHP_FUNCTION(utf8_decode)
    1646             : {
    1647             :         char *arg;
    1648             :         size_t arg_len;
    1649             :         zend_string *decoded;
    1650             : 
    1651          35 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
    1652           7 :                 return;
    1653             :         }
    1654             : 
    1655          28 :         decoded = xml_utf8_decode((XML_Char*)arg, arg_len, (XML_Char*)"ISO-8859-1");
    1656          28 :         if (decoded == NULL) {
    1657           0 :                 RETURN_FALSE;
    1658             :         }
    1659          28 :         RETURN_STR(decoded);
    1660             : }
    1661             : /* }}} */
    1662             : 
    1663             : #endif
    1664             : 
    1665             : /*
    1666             :  * Local variables:
    1667             :  * tab-width: 4
    1668             :  * c-basic-offset: 4
    1669             :  * End:
    1670             :  * vim600: sw=4 ts=4 fdm=marker
    1671             :  * vim<600: sw=4 ts=4
    1672             :  */

Generated by: LCOV version 1.10

Generated at Mon, 26 Jan 2015 14:46:56 +0000 (6 days ago)

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