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/libxml - libxml.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 517 585 88.4 %
Date: 2015-03-01 Functions: 51 52 98.1 %
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: Shane Caraveo <shane@php.net>                               |
      16             :    |          Wez Furlong <wez@thebrainroom.com>                          |
      17             :    +----------------------------------------------------------------------+
      18             :  */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #define IS_EXT_MODULE
      23             : 
      24             : #ifdef HAVE_CONFIG_H
      25             : #include "config.h"
      26             : #endif
      27             : 
      28             : #include "php.h"
      29             : #include "SAPI.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/file.h"
      36             : 
      37             : #if HAVE_LIBXML
      38             : 
      39             : #include <libxml/parser.h>
      40             : #include <libxml/parserInternals.h>
      41             : #include <libxml/tree.h>
      42             : #include <libxml/uri.h>
      43             : #include <libxml/xmlerror.h>
      44             : #include <libxml/xmlsave.h>
      45             : #ifdef LIBXML_SCHEMAS_ENABLED
      46             : #include <libxml/relaxng.h>
      47             : #include <libxml/xmlschemas.h>
      48             : #endif
      49             : 
      50             : #include "php_libxml.h"
      51             : 
      52             : #define PHP_LIBXML_ERROR 0
      53             : #define PHP_LIBXML_CTX_ERROR 1
      54             : #define PHP_LIBXML_CTX_WARNING 2
      55             : 
      56             : /* a true global for initialization */
      57             : static int _php_libxml_initialized = 0;
      58             : static int _php_libxml_per_request_initialization = 1;
      59             : static xmlExternalEntityLoader _php_libxml_default_entity_loader;
      60             : 
      61             : typedef struct _php_libxml_func_handler {
      62             :         php_libxml_export_node export_func;
      63             : } php_libxml_func_handler;
      64             : 
      65             : static HashTable php_libxml_exports;
      66             : 
      67             : static ZEND_DECLARE_MODULE_GLOBALS(libxml)
      68             : static PHP_GINIT_FUNCTION(libxml);
      69             : 
      70             : static PHP_FUNCTION(libxml_set_streams_context);
      71             : static PHP_FUNCTION(libxml_use_internal_errors);
      72             : static PHP_FUNCTION(libxml_get_last_error);
      73             : static PHP_FUNCTION(libxml_clear_errors);
      74             : static PHP_FUNCTION(libxml_get_errors);
      75             : static PHP_FUNCTION(libxml_set_external_entity_loader);
      76             : static PHP_FUNCTION(libxml_disable_entity_loader);
      77             : 
      78             : static zend_class_entry *libxmlerror_class_entry;
      79             : 
      80             : /* {{{ dynamically loadable module stuff */
      81             : #ifdef COMPILE_DL_LIBXML
      82             : #ifdef ZTS
      83             : ZEND_TSRMLS_CACHE_DEFINE;
      84             : #endif
      85             : ZEND_GET_MODULE(libxml)
      86             : #endif /* COMPILE_DL_LIBXML */
      87             : /* }}} */
      88             : 
      89             : /* {{{ function prototypes */
      90             : static PHP_MINIT_FUNCTION(libxml);
      91             : static PHP_RINIT_FUNCTION(libxml);
      92             : static PHP_RSHUTDOWN_FUNCTION(libxml);
      93             : static PHP_MSHUTDOWN_FUNCTION(libxml);
      94             : static PHP_MINFO_FUNCTION(libxml);
      95             : static int php_libxml_post_deactivate(void);
      96             : 
      97             : /* }}} */
      98             : 
      99             : /* {{{ arginfo */
     100             : ZEND_BEGIN_ARG_INFO(arginfo_libxml_set_streams_context, 0)
     101             :         ZEND_ARG_INFO(0, context)
     102             : ZEND_END_ARG_INFO()
     103             : 
     104             : ZEND_BEGIN_ARG_INFO_EX(arginfo_libxml_use_internal_errors, 0, 0, 0)
     105             :         ZEND_ARG_INFO(0, use_errors)
     106             : ZEND_END_ARG_INFO()
     107             : 
     108             : ZEND_BEGIN_ARG_INFO(arginfo_libxml_get_last_error, 0)
     109             : ZEND_END_ARG_INFO()
     110             : 
     111             : ZEND_BEGIN_ARG_INFO(arginfo_libxml_get_errors, 0)
     112             : ZEND_END_ARG_INFO()
     113             : 
     114             : ZEND_BEGIN_ARG_INFO(arginfo_libxml_clear_errors, 0)
     115             : ZEND_END_ARG_INFO()
     116             : 
     117             : ZEND_BEGIN_ARG_INFO_EX(arginfo_libxml_disable_entity_loader, 0, 0, 0)
     118             :         ZEND_ARG_INFO(0, disable)
     119             : ZEND_END_ARG_INFO()
     120             : 
     121             : ZEND_BEGIN_ARG_INFO_EX(arginfo_libxml_set_external_entity_loader, 0, 0, 1)
     122             :         ZEND_ARG_INFO(0, resolver_function)
     123             : ZEND_END_ARG_INFO()
     124             : /* }}} */
     125             : 
     126             : /* {{{ extension definition structures */
     127             : static const zend_function_entry libxml_functions[] = {
     128             :         PHP_FE(libxml_set_streams_context, arginfo_libxml_set_streams_context)
     129             :         PHP_FE(libxml_use_internal_errors, arginfo_libxml_use_internal_errors)
     130             :         PHP_FE(libxml_get_last_error, arginfo_libxml_get_last_error)
     131             :         PHP_FE(libxml_clear_errors, arginfo_libxml_clear_errors)
     132             :         PHP_FE(libxml_get_errors, arginfo_libxml_get_errors)
     133             :         PHP_FE(libxml_disable_entity_loader, arginfo_libxml_disable_entity_loader)
     134             :         PHP_FE(libxml_set_external_entity_loader, arginfo_libxml_set_external_entity_loader)
     135             :         PHP_FE_END
     136             : };
     137             : 
     138             : zend_module_entry libxml_module_entry = {
     139             :         STANDARD_MODULE_HEADER,
     140             :         "libxml",                /* extension name */
     141             :         libxml_functions,        /* extension function list */
     142             :         PHP_MINIT(libxml),       /* extension-wide startup function */
     143             :         PHP_MSHUTDOWN(libxml),   /* extension-wide shutdown function */
     144             :         PHP_RINIT(libxml),       /* per-request startup function */
     145             :         PHP_RSHUTDOWN(libxml),   /* per-request shutdown function */
     146             :         PHP_MINFO(libxml),       /* information function */
     147             :         NO_VERSION_YET,
     148             :         PHP_MODULE_GLOBALS(libxml), /* globals descriptor */
     149             :         PHP_GINIT(libxml),          /* globals ctor */
     150             :         NULL,                       /* globals dtor */
     151             :         php_libxml_post_deactivate, /* post deactivate */
     152             :         STANDARD_MODULE_PROPERTIES_EX
     153             : };
     154             : 
     155             : /* }}} */
     156             : 
     157             : /* {{{ internal functions for interoperability */
     158           7 : static int php_libxml_clear_object(php_libxml_node_object *object)
     159             : {
     160           7 :         if (object->properties) {
     161           7 :                 object->properties = NULL;
     162             :         }
     163           7 :         php_libxml_decrement_node_ptr(object);
     164           7 :         return php_libxml_decrement_doc_ref(object);
     165             : }
     166             : 
     167      203619 : static int php_libxml_unregister_node(xmlNodePtr nodep)
     168             : {
     169             :         php_libxml_node_object *wrapper;
     170             : 
     171      203619 :         php_libxml_node_ptr *nodeptr = nodep->_private;
     172             : 
     173      203619 :         if (nodeptr != NULL) {
     174          10 :                 wrapper = nodeptr->_private;
     175          10 :                 if (wrapper) {
     176           7 :                         php_libxml_clear_object(wrapper);
     177             :                 } else {
     178           3 :                         if (nodeptr->node != NULL && nodeptr->node->type != XML_DOCUMENT_NODE) {
     179           3 :                                 nodeptr->node->_private = NULL;
     180             :                         }
     181           3 :                         nodeptr->node = NULL;
     182             :                 }
     183             :         }
     184             : 
     185      203619 :         return -1;
     186             : }
     187             : 
     188         303 : static void php_libxml_node_free(xmlNodePtr node)
     189             : {
     190         303 :         if(node) {
     191         303 :                 if (node->_private != NULL) {
     192           0 :                         ((php_libxml_node_ptr *) node->_private)->node = NULL;
     193             :                 }
     194         303 :                 switch (node->type) {
     195             :                         case XML_ATTRIBUTE_NODE:
     196          32 :                                 xmlFreeProp((xmlAttrPtr) node);
     197          32 :                                 break;
     198             :                         case XML_ENTITY_DECL:
     199             :                         case XML_ELEMENT_DECL:
     200             :                         case XML_ATTRIBUTE_DECL:
     201           2 :                                 break;
     202             :                         case XML_NOTATION_NODE:
     203             :                                 /* These require special handling */
     204           5 :                                 if (node->name != NULL) {
     205           5 :                                         xmlFree((char *) node->name);
     206             :                                 }
     207           5 :                                 if (((xmlEntityPtr) node)->ExternalID != NULL) {
     208           5 :                                         xmlFree((char *) ((xmlEntityPtr) node)->ExternalID);
     209             :                                 }
     210           5 :                                 if (((xmlEntityPtr) node)->SystemID != NULL) {
     211           4 :                                         xmlFree((char *) ((xmlEntityPtr) node)->SystemID);
     212             :                                 }
     213           5 :                                 xmlFree(node);
     214           5 :                                 break;
     215             :                         case XML_NAMESPACE_DECL:
     216           0 :                                 if (node->ns) {
     217           0 :                                         xmlFreeNs(node->ns);
     218           0 :                                         node->ns = NULL;
     219             :                                 }
     220           0 :                                 node->type = XML_ELEMENT_NODE;
     221             :                         default:
     222         264 :                                 xmlFreeNode(node);
     223             :                 }
     224             :         }
     225         303 : }
     226             : 
     227         439 : static void php_libxml_node_free_list(xmlNodePtr node)
     228             : {
     229             :         xmlNodePtr curnode;
     230             : 
     231         439 :         if (node != NULL) {
     232          92 :                 curnode = node;
     233         340 :                 while (curnode != NULL) {
     234         156 :                         node = curnode;
     235         156 :                         switch (node->type) {
     236             :                                 /* Skip property freeing for the following types */
     237             :                                 case XML_NOTATION_NODE:
     238             :                                 case XML_ENTITY_DECL:
     239           2 :                                         break;
     240             :                                 case XML_ENTITY_REF_NODE:
     241           0 :                                         php_libxml_node_free_list((xmlNodePtr) node->properties);
     242           0 :                                         break;
     243             :                                 case XML_ATTRIBUTE_NODE:
     244          18 :                                                 if ((node->doc != NULL) && (((xmlAttrPtr) node)->atype == XML_ATTRIBUTE_ID)) {
     245           1 :                                                         xmlRemoveID(node->doc, (xmlAttrPtr) node);
     246             :                                                 }
     247             :                                 case XML_ATTRIBUTE_DECL:
     248             :                                 case XML_DTD_NODE:
     249             :                                 case XML_DOCUMENT_TYPE_NODE:
     250             :                                 case XML_NAMESPACE_DECL:
     251             :                                 case XML_TEXT_NODE:
     252         117 :                                         php_libxml_node_free_list(node->children);
     253         117 :                                         break;
     254             :                                 default:
     255          37 :                                         php_libxml_node_free_list(node->children);
     256          37 :                                         php_libxml_node_free_list((xmlNodePtr) node->properties);
     257             :                         }
     258             : 
     259         156 :                         curnode = node->next;
     260         156 :                         xmlUnlinkNode(node);
     261         156 :                         if (php_libxml_unregister_node(node) == 0) {
     262           0 :                                 node->doc = NULL;
     263             :                         }
     264         156 :                         php_libxml_node_free(node);
     265             :                 }
     266             :         }
     267         439 : }
     268             : 
     269             : /* }}} */
     270             : 
     271             : /* {{{ startup, shutdown and info functions */
     272       20871 : static PHP_GINIT_FUNCTION(libxml)
     273             : {
     274             : #if defined(COMPILE_DL_LIBXML) && defined(ZTS)
     275             :         ZEND_TSRMLS_CACHE_UPDATE;
     276             : #endif
     277       20871 :         ZVAL_UNDEF(&libxml_globals->stream_context);
     278       20871 :         libxml_globals->error_buffer.s = NULL;
     279       20871 :         libxml_globals->error_list = NULL;
     280       20871 :         ZVAL_UNDEF(&libxml_globals->entity_loader.object);
     281       20871 :         libxml_globals->entity_loader.fci.size = 0;
     282       20871 :         libxml_globals->entity_loader_disabled = 0;
     283       20871 : }
     284             : 
     285       20869 : static void _php_libxml_destroy_fci(zend_fcall_info *fci, zval *object)
     286             : {
     287       20869 :         if (fci->size > 0) {
     288           4 :                 zval_ptr_dtor(&fci->function_name);
     289           4 :                 fci->size = 0;
     290             :         }
     291       20869 :         if (!Z_ISUNDEF_P(object)) {
     292           0 :                 zval_ptr_dtor(object);
     293           0 :                 ZVAL_UNDEF(object);
     294             :         }
     295       20869 : }
     296             : 
     297             : /* Channel libxml file io layer through the PHP streams subsystem.
     298             :  * This allows use of ftps:// and https:// urls */
     299             : 
     300         980 : static void *php_libxml_streams_IO_open_wrapper(const char *filename, const char *mode, const int read_only)
     301             : {
     302             :         php_stream_statbuf ssbuf;
     303         980 :         php_stream_context *context = NULL;
     304         980 :         php_stream_wrapper *wrapper = NULL;
     305             :         char *resolved_path;
     306         980 :         const char *path_to_open = NULL;
     307         980 :         void *ret_val = NULL;
     308         980 :         int isescaped=0;
     309             :         xmlURI *uri;
     310             : 
     311             : 
     312         980 :         uri = xmlParseURI(filename);
     313        1978 :         if (uri && (uri->scheme == NULL ||
     314          31 :                         (xmlStrncmp(BAD_CAST uri->scheme, BAD_CAST "file", 4) == 0))) {
     315         967 :                 resolved_path = xmlURIUnescapeString(filename, 0, NULL);
     316         967 :                 isescaped = 1;
     317             :         } else {
     318          13 :                 resolved_path = (char *)filename;
     319             :         }
     320             : 
     321         980 :         if (uri) {
     322         980 :                 xmlFreeURI(uri);
     323             :         }
     324             : 
     325         980 :         if (resolved_path == NULL) {
     326           0 :                 return NULL;
     327             :         }
     328             : 
     329             :         /* logic copied from _php_stream_stat, but we only want to fail
     330             :            if the wrapper supports stat, otherwise, figure it out from
     331             :            the open.  This logic is only to support hiding warnings
     332             :            that the streams layer puts out at times, but for libxml we
     333             :            may try to open files that don't exist, but it is not a failure
     334             :            in xml processing (eg. DTD files)  */
     335         980 :         wrapper = php_stream_locate_url_wrapper(resolved_path, &path_to_open, 0);
     336         980 :         if (wrapper && read_only && wrapper->wops->url_stat) {
     337         949 :                 if (wrapper->wops->url_stat(wrapper, path_to_open, PHP_STREAM_URL_STAT_QUIET, &ssbuf, NULL) == -1) {
     338          20 :                         if (isescaped) {
     339          19 :                                 xmlFree(resolved_path);
     340             :                         }
     341          20 :                         return NULL;
     342             :                 }
     343             :         }
     344             : 
     345        1695 :         context = php_stream_context_from_zval(Z_ISUNDEF(LIBXML(stream_context))? NULL : &LIBXML(stream_context), 0);
     346             : 
     347         960 :         ret_val = php_stream_open_wrapper_ex(path_to_open, (char *)mode, REPORT_ERRORS, NULL, context);
     348         960 :         if (isescaped) {
     349         948 :                 xmlFree(resolved_path);
     350             :         }
     351         960 :         return ret_val;
     352             : }
     353             : 
     354         958 : static void *php_libxml_streams_IO_open_read_wrapper(const char *filename)
     355             : {
     356         958 :         return php_libxml_streams_IO_open_wrapper(filename, "rb", 1);
     357             : }
     358             : 
     359          22 : static void *php_libxml_streams_IO_open_write_wrapper(const char *filename)
     360             : {
     361          22 :         return php_libxml_streams_IO_open_wrapper(filename, "wb", 0);
     362             : }
     363             : 
     364        3836 : static int php_libxml_streams_IO_read(void *context, char *buffer, int len)
     365             : {
     366        3836 :         return php_stream_read((php_stream*)context, buffer, len);
     367             : }
     368             : 
     369          41 : static int php_libxml_streams_IO_write(void *context, const char *buffer, int len)
     370             : {
     371          41 :         return php_stream_write((php_stream*)context, buffer, len);
     372             : }
     373             : 
     374         960 : static int php_libxml_streams_IO_close(void *context)
     375             : {
     376         960 :         return php_stream_close((php_stream*)context);
     377             : }
     378             : 
     379             : static xmlParserInputBufferPtr
     380         959 : php_libxml_input_buffer_create_filename(const char *URI, xmlCharEncoding enc)
     381             : {
     382             :         xmlParserInputBufferPtr ret;
     383         959 :         void *context = NULL;
     384             : 
     385         959 :         if (LIBXML(entity_loader_disabled)) {
     386           1 :                 return NULL;
     387             :         }
     388             : 
     389         958 :         if (URI == NULL)
     390           0 :                 return(NULL);
     391             : 
     392         958 :         context = php_libxml_streams_IO_open_read_wrapper(URI);
     393             : 
     394         958 :         if (context == NULL) {
     395          22 :                 return(NULL);
     396             :         }
     397             : 
     398             :         /* Allocate the Input buffer front-end. */
     399         936 :         ret = xmlAllocParserInputBuffer(enc);
     400         936 :         if (ret != NULL) {
     401         936 :                 ret->context = context;
     402         936 :                 ret->readcallback = php_libxml_streams_IO_read;
     403         936 :                 ret->closecallback = php_libxml_streams_IO_close;
     404             :         } else
     405           0 :                 php_libxml_streams_IO_close(context);
     406             : 
     407         936 :         return(ret);
     408             : }
     409             : 
     410             : static xmlOutputBufferPtr
     411          22 : php_libxml_output_buffer_create_filename(const char *URI,
     412             :                               xmlCharEncodingHandlerPtr encoder,
     413             :                               int compression ATTRIBUTE_UNUSED)
     414             : {
     415             :         xmlOutputBufferPtr ret;
     416             :         xmlURIPtr puri;
     417          22 :         void *context = NULL;
     418          22 :         char *unescaped = NULL;
     419             : 
     420          22 :         if (URI == NULL)
     421           0 :                 return(NULL);
     422             : 
     423          22 :         puri = xmlParseURI(URI);
     424          22 :         if (puri != NULL) {
     425          22 :                 if (puri->scheme != NULL)
     426           2 :                         unescaped = xmlURIUnescapeString(URI, 0, NULL);
     427          22 :                 xmlFreeURI(puri);
     428             :         }
     429             : 
     430          22 :         if (unescaped != NULL) {
     431           2 :                 context = php_libxml_streams_IO_open_write_wrapper(unescaped);
     432           2 :                 xmlFree(unescaped);
     433             :         }
     434             : 
     435             :         /* try with a non-escaped URI this may be a strange filename */
     436          22 :         if (context == NULL) {
     437          20 :                 context = php_libxml_streams_IO_open_write_wrapper(URI);
     438             :         }
     439             : 
     440          22 :         if (context == NULL) {
     441           1 :                 return(NULL);
     442             :         }
     443             : 
     444             :         /* Allocate the Output buffer front-end. */
     445          21 :         ret = xmlAllocOutputBuffer(encoder);
     446          21 :         if (ret != NULL) {
     447          21 :                 ret->context = context;
     448          21 :                 ret->writecallback = php_libxml_streams_IO_write;
     449          21 :                 ret->closecallback = php_libxml_streams_IO_close;
     450             :         }
     451             : 
     452          21 :         return(ret);
     453             : }
     454             : 
     455           3 : static int _php_libxml_free_error(xmlErrorPtr error)
     456             : {
     457             :         /* This will free the libxml alloc'd memory */
     458           3 :         xmlResetError(error);
     459           3 :         return 1;
     460             : }
     461             : 
     462           3 : static void _php_list_set_error_structure(xmlErrorPtr error, const char *msg)
     463             : {
     464             :         xmlError error_copy;
     465             :         int ret;
     466             : 
     467             : 
     468           3 :         memset(&error_copy, 0, sizeof(xmlError));
     469             : 
     470           3 :         if (error) {
     471           3 :                 ret = xmlCopyError(error, &error_copy);
     472             :         } else {
     473           0 :                 error_copy.domain = 0;
     474           0 :                 error_copy.code = XML_ERR_INTERNAL_ERROR;
     475           0 :                 error_copy.level = XML_ERR_ERROR;
     476           0 :                 error_copy.line = 0;
     477           0 :                 error_copy.node = NULL;
     478           0 :                 error_copy.int1 = 0;
     479           0 :                 error_copy.int2 = 0;
     480           0 :                 error_copy.ctxt = NULL;
     481           0 :                 error_copy.message = (char*)xmlStrdup((xmlChar*)msg);
     482           0 :                 error_copy.file = NULL;
     483           0 :                 error_copy.str1 = NULL;
     484           0 :                 error_copy.str2 = NULL;
     485           0 :                 error_copy.str3 = NULL;
     486           0 :                 ret = 0;
     487             :         }
     488             : 
     489           3 :         if (ret == 0) {
     490           3 :                 zend_llist_add_element(LIBXML(error_list), &error_copy);
     491             :         }
     492           3 : }
     493             : 
     494          35 : static void php_libxml_ctx_error_level(int level, void *ctx, const char *msg)
     495             : {
     496             :         xmlParserCtxtPtr parser;
     497             : 
     498          35 :         parser = (xmlParserCtxtPtr) ctx;
     499             : 
     500          35 :         if (parser != NULL && parser->input != NULL) {
     501          33 :                 if (parser->input->filename) {
     502          13 :                         php_error_docref(NULL, level, "%s in %s, line: %d", msg, parser->input->filename, parser->input->line);
     503             :                 } else {
     504          20 :                         php_error_docref(NULL, level, "%s in Entity, line: %d", msg, parser->input->line);
     505             :                 }
     506             :         }
     507          35 : }
     508             : 
     509           9 : void php_libxml_issue_error(int level, const char *msg)
     510             : {
     511           9 :         if (LIBXML(error_list)) {
     512           0 :                 _php_list_set_error_structure(NULL, msg);
     513             :         } else {
     514           9 :                 php_error_docref(NULL, level, "%s", msg);
     515             :         }
     516           9 : }
     517             : 
     518         175 : static void php_libxml_internal_error_handler(int error_type, void *ctx, const char **msg, va_list ap)
     519             : {
     520             :         char *buf;
     521         175 :         int len, len_iter, output = 0;
     522             : 
     523             : 
     524         175 :         len = vspprintf(&buf, 0, *msg, ap);
     525         175 :         len_iter = len;
     526             : 
     527             :         /* remove any trailing \n */
     528         486 :         while (len_iter && buf[--len_iter] == '\n') {
     529         136 :                 buf[len_iter] = '\0';
     530         136 :                 output = 1;
     531             :         }
     532             : 
     533         175 :         smart_str_appendl(&LIBXML(error_buffer), buf, len);
     534             : 
     535         175 :         efree(buf);
     536             : 
     537         175 :         if (output == 1) {
     538         136 :                 if (LIBXML(error_list)) {
     539           0 :                         _php_list_set_error_structure(NULL, LIBXML(error_buffer).s->val);
     540             :                 } else {
     541         136 :                         switch (error_type) {
     542             :                                 case PHP_LIBXML_CTX_ERROR:
     543          35 :                                         php_libxml_ctx_error_level(E_WARNING, ctx, LIBXML(error_buffer).s->val);
     544          35 :                                         break;
     545             :                                 case PHP_LIBXML_CTX_WARNING:
     546           0 :                                         php_libxml_ctx_error_level(E_NOTICE, ctx, LIBXML(error_buffer).s->val);
     547           0 :                                         break;
     548             :                                 default:
     549         101 :                                         php_error_docref(NULL, E_WARNING, "%s", LIBXML(error_buffer).s->val);
     550             :                         }
     551             :                 }
     552             :                 smart_str_free(&LIBXML(error_buffer));
     553             :         }
     554         175 : }
     555             : 
     556         941 : static xmlParserInputPtr _php_libxml_external_entity_loader(const char *URL,
     557             :                 const char *ID, xmlParserCtxtPtr context)
     558             : {
     559         941 :         xmlParserInputPtr       ret                     = NULL;
     560         941 :         const char                      *resource       = NULL;
     561             :         zval                            *ctxzv, retval;
     562             :         zval                            params[3];
     563             :         int                                     status;
     564             :         zend_fcall_info         *fci;
     565             : 
     566         941 :         fci = &LIBXML(entity_loader).fci;
     567             : 
     568         941 :         if (fci->size == 0) {
     569             :                 /* no custom user-land callback set up; delegate to original loader */
     570         936 :                 return _php_libxml_default_entity_loader(URL, ID, context);
     571             :         }
     572             : 
     573           5 :         if (ID != NULL) {
     574          10 :                 ZVAL_STRING(&params[0], ID);
     575             :         } else {
     576           0 :                 ZVAL_UNDEF(&params[0]);
     577             :         }
     578           5 :         if (URL != NULL) {
     579          10 :                 ZVAL_STRING(&params[1], URL);
     580             :         } else {
     581           0 :                 ZVAL_UNDEF(&params[1]);
     582             :         }
     583           5 :         ctxzv = &params[2];
     584           5 :         array_init_size(ctxzv, 4);
     585             : 
     586             : #define ADD_NULL_OR_STRING_KEY(memb) \
     587             :         if (context->memb == NULL) { \
     588             :                 add_assoc_null_ex(ctxzv, #memb, sizeof(#memb) - 1); \
     589             :         } else { \
     590             :                 add_assoc_string_ex(ctxzv, #memb, sizeof(#memb) - 1, \
     591             :                                 (char *)context->memb); \
     592             :         }
     593             : 
     594           5 :         ADD_NULL_OR_STRING_KEY(directory)
     595           5 :         ADD_NULL_OR_STRING_KEY(intSubName)
     596           5 :         ADD_NULL_OR_STRING_KEY(extSubURI)
     597           5 :         ADD_NULL_OR_STRING_KEY(extSubSystem)
     598             : 
     599             : #undef ADD_NULL_OR_STRING_KEY
     600             : 
     601           5 :         fci->retval  = &retval;
     602           5 :         fci->params  = params;
     603           5 :         fci->param_count = sizeof(params)/sizeof(*params);
     604           5 :         fci->no_separation   = 1;
     605             : 
     606           5 :         status = zend_call_function(fci, &LIBXML(entity_loader).fcc);
     607          10 :         if (status != SUCCESS || Z_ISUNDEF(retval)) {
     608           0 :                 php_libxml_ctx_error(context,
     609             :                                 "Call to user entity loader callback '%s' has failed",
     610           0 :                                 Z_STRVAL(fci->function_name));
     611             :         } else {
     612             :                 /*
     613             :                 retval_ptr = *fci->retval_ptr_ptr;
     614             :                 if (retval_ptr == NULL) {
     615             :                         php_libxml_ctx_error(context,
     616             :                                         "Call to user entity loader callback '%s' has failed; "
     617             :                                         "probably it has thrown an exception",
     618             :                                         fci->function_name);
     619           5 :                 } else */ if (Z_TYPE(retval) == IS_STRING) {
     620             : is_string:
     621           0 :                         resource = Z_STRVAL(retval);
     622           5 :                 } else if (Z_TYPE(retval) == IS_RESOURCE) {
     623             :                         php_stream *stream;
     624           3 :                         php_stream_from_zval_no_verify(stream, &retval);
     625           3 :                         if (stream == NULL) {
     626           0 :                                 php_libxml_ctx_error(context,
     627             :                                                 "The user entity loader callback '%s' has returned a "
     628             :                                                 "resource, but it is not a stream",
     629           0 :                                                 Z_STRVAL(fci->function_name));
     630             :                         } else {
     631             :                                 /* TODO: allow storing the encoding in the stream context? */
     632           3 :                                 xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;
     633           3 :                                 xmlParserInputBufferPtr pib = xmlAllocParserInputBuffer(enc);
     634           3 :                                 if (pib == NULL) {
     635           0 :                                         php_libxml_ctx_error(context, "Could not allocate parser "
     636             :                                                         "input buffer");
     637             :                                 } else {
     638             :                                         /* make stream not being closed when the zval is freed */
     639           3 :                                         ++GC_REFCOUNT(stream->res);
     640           3 :                                         pib->context = stream;
     641           3 :                                         pib->readcallback = php_libxml_streams_IO_read;
     642           3 :                                         pib->closecallback = php_libxml_streams_IO_close;
     643             : 
     644           3 :                                         ret = xmlNewIOInputStream(context, pib, enc);
     645           3 :                                         if (ret == NULL) {
     646           0 :                                                 xmlFreeParserInputBuffer(pib);
     647             :                                         }
     648             :                                 }
     649             :                         }
     650           2 :                 } else if (Z_TYPE(retval) != IS_NULL) {
     651             :                         /* retval not string nor resource nor null; convert to string */
     652           0 :                         SEPARATE_ZVAL(&retval);
     653           0 :                         convert_to_string(&retval);
     654           0 :                         goto is_string;
     655             :                 } /* else is null; don't try anything */
     656             :         }
     657             : 
     658           5 :         if (ret == NULL) {
     659           2 :                 if (resource == NULL) {
     660           2 :                         if (ID == NULL) {
     661           0 :                                 ID = "NULL";
     662             :                         }
     663           2 :                         php_libxml_ctx_error(context,
     664             :                                         "Failed to load external entity \"%s\"\n", ID);
     665             :                 } else {
     666             :                         /* we got the resource in the form of a string; open it */
     667           0 :                         ret = xmlNewInputFromFile(context, resource);
     668             :                 }
     669             :         }
     670             : 
     671           5 :         zval_ptr_dtor(&params[0]);
     672           5 :         zval_ptr_dtor(&params[1]);
     673           5 :         zval_ptr_dtor(&params[2]);
     674           5 :         zval_ptr_dtor(&retval);
     675           5 :         return ret;
     676             : }
     677             : 
     678         941 : static xmlParserInputPtr _php_libxml_pre_ext_ent_loader(const char *URL,
     679             :                 const char *ID, xmlParserCtxtPtr context)
     680             : {
     681             : 
     682             :         /* Check whether we're running in a PHP context, since the entity loader
     683             :          * we've defined is an application level (true global) setting.
     684             :          * If we are, we also want to check whether we've finished activating
     685             :          * the modules (RINIT phase). Using our external entity loader during a
     686             :          * RINIT should not be problem per se (though during MINIT it is, because
     687             :          * we don't even have a resource list by then), but then whether one
     688             :          * extension would be using the custom external entity loader or not
     689             :          * could depend on extension loading order
     690             :          * (if _php_libxml_per_request_initialization */
     691         941 :         if (xmlGenericError == php_libxml_error_handler && PG(modules_activated)) {
     692         941 :                 return _php_libxml_external_entity_loader(URL, ID, context);
     693             :         } else {
     694           0 :                 return _php_libxml_default_entity_loader(URL, ID, context);
     695             :         }
     696             : }
     697             : 
     698          35 : PHP_LIBXML_API void php_libxml_ctx_error(void *ctx, const char *msg, ...)
     699             : {
     700             :         va_list args;
     701          35 :         va_start(args, msg);
     702          35 :         php_libxml_internal_error_handler(PHP_LIBXML_CTX_ERROR, ctx, &msg, args);
     703          35 :         va_end(args);
     704          35 : }
     705             : 
     706           0 : PHP_LIBXML_API void php_libxml_ctx_warning(void *ctx, const char *msg, ...)
     707             : {
     708             :         va_list args;
     709           0 :         va_start(args, msg);
     710           0 :         php_libxml_internal_error_handler(PHP_LIBXML_CTX_WARNING, ctx, &msg, args);
     711           0 :         va_end(args);
     712           0 : }
     713             : 
     714           3 : PHP_LIBXML_API void php_libxml_structured_error_handler(void *userData, xmlErrorPtr error)
     715             : {
     716           3 :         _php_list_set_error_structure(error, NULL);
     717             : 
     718           3 :         return;
     719             : }
     720             : 
     721         140 : PHP_LIBXML_API void php_libxml_error_handler(void *ctx, const char *msg, ...)
     722             : {
     723             :         va_list args;
     724         140 :         va_start(args, msg);
     725         140 :         php_libxml_internal_error_handler(PHP_LIBXML_ERROR, ctx, &msg, args);
     726         140 :         va_end(args);
     727         140 : }
     728             : 
     729       41810 : static void php_libxml_exports_dtor(zval *zv)
     730             : {
     731       41810 :         free(Z_PTR_P(zv));
     732       41810 : }
     733             : 
     734       62613 : PHP_LIBXML_API void php_libxml_initialize(void)
     735             : {
     736       62613 :         if (!_php_libxml_initialized) {
     737             :                 /* we should be the only one's to ever init!! */
     738       20871 :                 xmlInitParser();
     739             : 
     740       20871 :                 _php_libxml_default_entity_loader = xmlGetExternalEntityLoader();
     741       20871 :                 xmlSetExternalEntityLoader(_php_libxml_pre_ext_ent_loader);
     742             : 
     743       20871 :                 zend_hash_init(&php_libxml_exports, 0, NULL, php_libxml_exports_dtor, 1);
     744             : 
     745       20871 :                 _php_libxml_initialized = 1;
     746             :         }
     747       62613 : }
     748             : 
     749       20905 : PHP_LIBXML_API void php_libxml_shutdown(void)
     750             : {
     751       20905 :         if (_php_libxml_initialized) {
     752             : #if defined(LIBXML_SCHEMAS_ENABLED)
     753       20905 :                 xmlRelaxNGCleanupTypes();
     754             : #endif
     755       20905 :                 xmlCleanupParser();
     756       20905 :                 zend_hash_destroy(&php_libxml_exports);
     757             : 
     758       20905 :                 xmlSetExternalEntityLoader(_php_libxml_default_entity_loader);
     759       20905 :                 _php_libxml_initialized = 0;
     760             :         }
     761       20905 : }
     762             : 
     763        1413 : PHP_LIBXML_API void php_libxml_switch_context(zval *context, zval *oldcontext)
     764             : {
     765        1413 :         if (oldcontext) {
     766         709 :                 ZVAL_COPY_VALUE(oldcontext, &LIBXML(stream_context));
     767             :         }
     768        1413 :         if (context) {
     769        1413 :                 ZVAL_COPY_VALUE(&LIBXML(stream_context), context);
     770             :         }
     771        1413 : }
     772             : 
     773       20871 : static PHP_MINIT_FUNCTION(libxml)
     774             : {
     775             :         zend_class_entry ce;
     776             : 
     777       20871 :         php_libxml_initialize();
     778             : 
     779       20871 :         REGISTER_LONG_CONSTANT("LIBXML_VERSION",                      LIBXML_VERSION,                 CONST_CS | CONST_PERSISTENT);
     780       20871 :         REGISTER_STRING_CONSTANT("LIBXML_DOTTED_VERSION",     LIBXML_DOTTED_VERSION,  CONST_CS | CONST_PERSISTENT);
     781       20871 :         REGISTER_STRING_CONSTANT("LIBXML_LOADED_VERSION",     (char *)xmlParserVersion,               CONST_CS | CONST_PERSISTENT);
     782             : 
     783             :         /* For use with loading xml */
     784       20871 :         REGISTER_LONG_CONSTANT("LIBXML_NOENT",                XML_PARSE_NOENT,                CONST_CS | CONST_PERSISTENT);
     785       20871 :         REGISTER_LONG_CONSTANT("LIBXML_DTDLOAD",      XML_PARSE_DTDLOAD,              CONST_CS | CONST_PERSISTENT);
     786       20871 :         REGISTER_LONG_CONSTANT("LIBXML_DTDATTR",      XML_PARSE_DTDATTR,              CONST_CS | CONST_PERSISTENT);
     787       20871 :         REGISTER_LONG_CONSTANT("LIBXML_DTDVALID",     XML_PARSE_DTDVALID,             CONST_CS | CONST_PERSISTENT);
     788       20871 :         REGISTER_LONG_CONSTANT("LIBXML_NOERROR",      XML_PARSE_NOERROR,              CONST_CS | CONST_PERSISTENT);
     789       20871 :         REGISTER_LONG_CONSTANT("LIBXML_NOWARNING",    XML_PARSE_NOWARNING,    CONST_CS | CONST_PERSISTENT);
     790       20871 :         REGISTER_LONG_CONSTANT("LIBXML_NOBLANKS",     XML_PARSE_NOBLANKS,             CONST_CS | CONST_PERSISTENT);
     791       20871 :         REGISTER_LONG_CONSTANT("LIBXML_XINCLUDE",     XML_PARSE_XINCLUDE,             CONST_CS | CONST_PERSISTENT);
     792       20871 :         REGISTER_LONG_CONSTANT("LIBXML_NSCLEAN",      XML_PARSE_NSCLEAN,              CONST_CS | CONST_PERSISTENT);
     793       20871 :         REGISTER_LONG_CONSTANT("LIBXML_NOCDATA",      XML_PARSE_NOCDATA,              CONST_CS | CONST_PERSISTENT);
     794       20871 :         REGISTER_LONG_CONSTANT("LIBXML_NONET",                XML_PARSE_NONET,                CONST_CS | CONST_PERSISTENT);
     795       20871 :         REGISTER_LONG_CONSTANT("LIBXML_PEDANTIC",     XML_PARSE_PEDANTIC,             CONST_CS | CONST_PERSISTENT);
     796             : #if LIBXML_VERSION >= 20621
     797       20871 :         REGISTER_LONG_CONSTANT("LIBXML_COMPACT",      XML_PARSE_COMPACT,              CONST_CS | CONST_PERSISTENT);
     798       20871 :         REGISTER_LONG_CONSTANT("LIBXML_NOXMLDECL",    XML_SAVE_NO_DECL,               CONST_CS | CONST_PERSISTENT);
     799             : #endif
     800             : #if LIBXML_VERSION >= 20703
     801       20871 :         REGISTER_LONG_CONSTANT("LIBXML_PARSEHUGE",    XML_PARSE_HUGE,                 CONST_CS | CONST_PERSISTENT);
     802             : #endif
     803       20871 :         REGISTER_LONG_CONSTANT("LIBXML_NOEMPTYTAG",   LIBXML_SAVE_NOEMPTYTAG, CONST_CS | CONST_PERSISTENT);
     804             : 
     805             :         /* Schema validation options */
     806             : #if defined(LIBXML_SCHEMAS_ENABLED) && LIBXML_VERSION >= 20614
     807       20871 :         REGISTER_LONG_CONSTANT("LIBXML_SCHEMA_CREATE",        XML_SCHEMA_VAL_VC_I_CREATE,     CONST_CS | CONST_PERSISTENT);
     808             : #endif
     809             : 
     810             :         /* Additional constants for use with loading html */
     811             : #if LIBXML_VERSION >= 20707
     812             :         REGISTER_LONG_CONSTANT("LIBXML_HTML_NOIMPLIED",       HTML_PARSE_NOIMPLIED,           CONST_CS | CONST_PERSISTENT);
     813             : #endif
     814             : 
     815             : #if LIBXML_VERSION >= 20708
     816             :         REGISTER_LONG_CONSTANT("LIBXML_HTML_NODEFDTD",        HTML_PARSE_NODEFDTD,            CONST_CS | CONST_PERSISTENT);
     817             : #endif
     818             : 
     819             :         /* Error levels */
     820       20871 :         REGISTER_LONG_CONSTANT("LIBXML_ERR_NONE",             XML_ERR_NONE,           CONST_CS | CONST_PERSISTENT);
     821       20871 :         REGISTER_LONG_CONSTANT("LIBXML_ERR_WARNING",  XML_ERR_WARNING,        CONST_CS | CONST_PERSISTENT);
     822       20871 :         REGISTER_LONG_CONSTANT("LIBXML_ERR_ERROR",            XML_ERR_ERROR,          CONST_CS | CONST_PERSISTENT);
     823       20871 :         REGISTER_LONG_CONSTANT("LIBXML_ERR_FATAL",            XML_ERR_FATAL,          CONST_CS | CONST_PERSISTENT);
     824             : 
     825       20871 :         INIT_CLASS_ENTRY(ce, "LibXMLError", NULL);
     826       20871 :         libxmlerror_class_entry = zend_register_internal_class(&ce);
     827             : 
     828       20871 :         if (sapi_module.name) {
     829             :                 static const char * const supported_sapis[] = {
     830             :                         "cgi-fcgi",
     831             :                         "fpm-fcgi",
     832             :                         "litespeed",
     833             :                         NULL
     834             :                 };
     835             :                 const char * const *sapi_name;
     836             : 
     837       82272 :                 for (sapi_name = supported_sapis; *sapi_name; sapi_name++) {
     838       61805 :                         if (strcmp(sapi_module.name, *sapi_name) == 0) {
     839         404 :                                 _php_libxml_per_request_initialization = 0;
     840         404 :                                 break;
     841             :                         }
     842             :                 }
     843             :         }
     844             : 
     845       20871 :         if (!_php_libxml_per_request_initialization) {
     846             :                 /* report errors via handler rather than stderr */
     847         404 :                 xmlSetGenericErrorFunc(NULL, php_libxml_error_handler);
     848         404 :                 xmlParserInputBufferCreateFilenameDefault(php_libxml_input_buffer_create_filename);
     849         404 :                 xmlOutputBufferCreateFilenameDefault(php_libxml_output_buffer_create_filename);
     850             :         }
     851             : 
     852       20871 :         return SUCCESS;
     853             : }
     854             : 
     855             : 
     856       20828 : static PHP_RINIT_FUNCTION(libxml)
     857             : {
     858       20828 :         if (_php_libxml_per_request_initialization) {
     859             :                 /* report errors via handler rather than stderr */
     860       20424 :                 xmlSetGenericErrorFunc(NULL, php_libxml_error_handler);
     861       20424 :                 xmlParserInputBufferCreateFilenameDefault(php_libxml_input_buffer_create_filename);
     862       20424 :                 xmlOutputBufferCreateFilenameDefault(php_libxml_output_buffer_create_filename);
     863             : 
     864             :                 /* Enable the entity loader by default. This ensures that
     865             :                  * other threads/requests that might have disabled the loader
     866             :                  * do not affect the current request.
     867             :                  */
     868       20424 :                 LIBXML(entity_loader_disabled) = 0;
     869             :         }
     870       20828 :         return SUCCESS;
     871             : }
     872             : 
     873       20864 : static PHP_RSHUTDOWN_FUNCTION(libxml)
     874             : {
     875       20864 :         _php_libxml_destroy_fci(&LIBXML(entity_loader).fci, &LIBXML(entity_loader).object);
     876             : 
     877       20864 :         return SUCCESS;
     878             : }
     879             : 
     880       20905 : static PHP_MSHUTDOWN_FUNCTION(libxml)
     881             : {
     882       20905 :         if (!_php_libxml_per_request_initialization) {
     883         401 :                 xmlSetGenericErrorFunc(NULL, NULL);
     884             : 
     885         401 :                 xmlParserInputBufferCreateFilenameDefault(NULL);
     886         401 :                 xmlOutputBufferCreateFilenameDefault(NULL);
     887             :         }
     888       20905 :         php_libxml_shutdown();
     889             : 
     890       20905 :         return SUCCESS;
     891             : }
     892             : 
     893       20864 : static int php_libxml_post_deactivate(void)
     894             : {
     895             :         /* reset libxml generic error handling */
     896       20864 :         if (_php_libxml_per_request_initialization) {
     897       20463 :                 xmlSetGenericErrorFunc(NULL, NULL);
     898             : 
     899       20463 :                 xmlParserInputBufferCreateFilenameDefault(NULL);
     900       20463 :                 xmlOutputBufferCreateFilenameDefault(NULL);
     901             :         }
     902       20864 :         xmlSetStructuredErrorFunc(NULL, NULL);
     903             : 
     904             :         /* the steam_context resource will be released by resource list destructor */
     905       20864 :         ZVAL_UNDEF(&LIBXML(stream_context));
     906             :         smart_str_free(&LIBXML(error_buffer));
     907       20864 :         if (LIBXML(error_list)) {
     908           2 :                 zend_llist_destroy(LIBXML(error_list));
     909           2 :                 efree(LIBXML(error_list));
     910           2 :                 LIBXML(error_list) = NULL;
     911             :         }
     912       20864 :         xmlResetLastError();
     913             : 
     914       20864 :         return SUCCESS;
     915             : }
     916             : 
     917             : 
     918         145 : static PHP_MINFO_FUNCTION(libxml)
     919             : {
     920         145 :         php_info_print_table_start();
     921         145 :         php_info_print_table_row(2, "libXML support", "active");
     922         145 :         php_info_print_table_row(2, "libXML Compiled Version", LIBXML_DOTTED_VERSION);
     923         145 :         php_info_print_table_row(2, "libXML Loaded Version", (char *)xmlParserVersion);
     924         145 :         php_info_print_table_row(2, "libXML streams", "enabled");
     925         145 :         php_info_print_table_end();
     926         145 : }
     927             : /* }}} */
     928             : 
     929             : /* {{{ proto void libxml_set_streams_context(resource streams_context)
     930             :    Set the streams context for the next libxml document load or write */
     931          11 : static PHP_FUNCTION(libxml_set_streams_context)
     932             : {
     933             :         zval *arg;
     934             : 
     935          11 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &arg) == FAILURE) {
     936           6 :                 return;
     937             :         }
     938           5 :         if (!Z_ISUNDEF(LIBXML(stream_context))) {
     939           2 :                 zval_ptr_dtor(&LIBXML(stream_context));
     940           2 :                 ZVAL_UNDEF(&LIBXML(stream_context));
     941             :         }
     942           5 :         ZVAL_COPY(&LIBXML(stream_context), arg);
     943             : }
     944             : /* }}} */
     945             : 
     946             : /* {{{ proto bool libxml_use_internal_errors([boolean use_errors])
     947             :    Disable libxml errors and allow user to fetch error information as needed */
     948           9 : static PHP_FUNCTION(libxml_use_internal_errors)
     949             : {
     950             :         xmlStructuredErrorFunc current_handler;
     951           9 :         zend_bool use_errors=0, retval;
     952             : 
     953           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &use_errors) == FAILURE) {
     954           1 :                 return;
     955             :         }
     956             : 
     957           8 :         current_handler = xmlStructuredError;
     958          11 :         if (current_handler && current_handler == php_libxml_structured_error_handler) {
     959           3 :                 retval = 1;
     960             :         } else {
     961           5 :                 retval = 0;
     962             :         }
     963             : 
     964           8 :         if (ZEND_NUM_ARGS() == 0) {
     965           1 :                 RETURN_BOOL(retval);
     966             :         }
     967             : 
     968           7 :         if (use_errors == 0) {
     969           3 :                 xmlSetStructuredErrorFunc(NULL, NULL);
     970           3 :                 if (LIBXML(error_list)) {
     971           2 :                         zend_llist_destroy(LIBXML(error_list));
     972           2 :                         efree(LIBXML(error_list));
     973           2 :                         LIBXML(error_list) = NULL;
     974             :                 }
     975             :         } else {
     976           4 :                 xmlSetStructuredErrorFunc(NULL, php_libxml_structured_error_handler);
     977           4 :                 if (LIBXML(error_list) == NULL) {
     978           4 :                         LIBXML(error_list) = (zend_llist *) emalloc(sizeof(zend_llist));
     979           4 :                         zend_llist_init(LIBXML(error_list), sizeof(xmlError), (llist_dtor_func_t) _php_libxml_free_error, 0);
     980             :                 }
     981             :         }
     982           7 :         RETURN_BOOL(retval);
     983             : }
     984             : /* }}} */
     985             : 
     986             : /* {{{ proto object libxml_get_last_error()
     987             :    Retrieve last error from libxml */
     988           2 : static PHP_FUNCTION(libxml_get_last_error)
     989             : {
     990             :         xmlErrorPtr error;
     991             : 
     992           2 :         error = xmlGetLastError();
     993             : 
     994           2 :         if (error) {
     995           1 :                 object_init_ex(return_value, libxmlerror_class_entry);
     996           1 :                 add_property_long(return_value, "level", error->level);
     997           1 :                 add_property_long(return_value, "code", error->code);
     998           1 :                 add_property_long(return_value, "column", error->int2);
     999           1 :                 if (error->message) {
    1000           1 :                         add_property_string(return_value, "message", error->message);
    1001             :                 } else {
    1002           0 :                         add_property_stringl(return_value, "message", "", 0);
    1003             :                 }
    1004           1 :                 if (error->file) {
    1005           0 :                         add_property_string(return_value, "file", error->file);
    1006             :                 } else {
    1007           1 :                         add_property_stringl(return_value, "file", "", 0);
    1008             :                 }
    1009           1 :                 add_property_long(return_value, "line", error->line);
    1010             :         } else {
    1011           1 :                 RETURN_FALSE;
    1012             :         }
    1013             : }
    1014             : /* }}} */
    1015             : 
    1016             : /* {{{ proto object libxml_get_errors()
    1017             :    Retrieve array of errors */
    1018           4 : static PHP_FUNCTION(libxml_get_errors)
    1019             : {
    1020             : 
    1021             :         xmlErrorPtr error;
    1022             : 
    1023           4 :         if (array_init(return_value) == FAILURE) {
    1024           0 :                 RETURN_FALSE;
    1025             :         }
    1026             : 
    1027           4 :         if (LIBXML(error_list)) {
    1028             : 
    1029           3 :                 error = zend_llist_get_first(LIBXML(error_list));
    1030             : 
    1031           8 :                 while (error != NULL) {
    1032             :                         zval z_error;
    1033             : 
    1034           2 :                         object_init_ex(&z_error, libxmlerror_class_entry);
    1035           2 :                         add_property_long_ex(&z_error, "level", sizeof("level") - 1, error->level);
    1036           2 :                         add_property_long_ex(&z_error, "code", sizeof("code") - 1, error->code);
    1037           2 :                         add_property_long_ex(&z_error, "column", sizeof("column") - 1, error->int2 );
    1038           2 :                         if (error->message) {
    1039           2 :                                 add_property_string_ex(&z_error, "message", sizeof("message") - 1, error->message);
    1040             :                         } else {
    1041           0 :                                 add_property_stringl_ex(&z_error, "message", sizeof("message") - 1, "", 0);
    1042             :                         }
    1043           2 :                         if (error->file) {
    1044           1 :                                 add_property_string_ex(&z_error, "file", sizeof("file") - 1, error->file);
    1045             :                         } else {
    1046           1 :                                 add_property_stringl_ex(&z_error, "file", sizeof("file") - 1, "", 0);
    1047             :                         }
    1048           2 :                         add_property_long_ex(&z_error, "line", sizeof("line") - 1, error->line);
    1049           2 :                         add_next_index_zval(return_value, &z_error);
    1050             : 
    1051           2 :                         error = zend_llist_get_next(LIBXML(error_list));
    1052             :                 }
    1053             :         }
    1054             : }
    1055             : /* }}} */
    1056             : 
    1057             : /* {{{ proto void libxml_clear_errors()
    1058             :    Clear last error from libxml */
    1059           3 : static PHP_FUNCTION(libxml_clear_errors)
    1060             : {
    1061           3 :         xmlResetLastError();
    1062           3 :         if (LIBXML(error_list)) {
    1063           2 :                 zend_llist_clean(LIBXML(error_list));
    1064             :         }
    1065           3 : }
    1066             : /* }}} */
    1067             : 
    1068        2567 : PHP_LIBXML_API zend_bool php_libxml_disable_entity_loader(zend_bool disable) /* {{{ */
    1069             : {
    1070        2567 :         zend_bool old = LIBXML(entity_loader_disabled);
    1071             : 
    1072        2567 :         LIBXML(entity_loader_disabled) = disable;
    1073        2567 :         return old;
    1074             : } /* }}} */
    1075             : 
    1076             : /* {{{ proto bool libxml_disable_entity_loader([boolean disable])
    1077             :    Disable/Enable ability to load external entities */
    1078           1 : static PHP_FUNCTION(libxml_disable_entity_loader)
    1079             : {
    1080           1 :         zend_bool disable = 1;
    1081             : 
    1082           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &disable) == FAILURE) {
    1083           0 :                 return;
    1084             :         }
    1085             : 
    1086           1 :         RETURN_BOOL(php_libxml_disable_entity_loader(disable));
    1087             : }
    1088             : /* }}} */
    1089             : 
    1090             : /* {{{ proto void libxml_set_external_entity_loader(callback resolver_function)
    1091             :    Changes the default external entity loader */
    1092           8 : static PHP_FUNCTION(libxml_set_external_entity_loader)
    1093             : {
    1094             :         zend_fcall_info                 fci;
    1095             :         zend_fcall_info_cache   fcc;
    1096           8 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "f!", &fci, &fcc)
    1097             :                         == FAILURE) {
    1098           3 :                 return;
    1099             :         }
    1100             : 
    1101           5 :         _php_libxml_destroy_fci(&LIBXML(entity_loader).fci, &LIBXML(entity_loader).object);
    1102             : 
    1103           5 :         if (fci.size > 0) { /* argument not null */
    1104           4 :                 LIBXML(entity_loader).fci = fci;
    1105             :                 Z_ADDREF(fci.function_name);
    1106           4 :                 if (fci.object != NULL) {
    1107           0 :                         ZVAL_OBJ(&LIBXML(entity_loader).object, fci.object);
    1108             :                         Z_ADDREF(LIBXML(entity_loader).object);
    1109             :                 }
    1110           4 :                 LIBXML(entity_loader).fcc = fcc;
    1111             :         }
    1112             : 
    1113           5 :         RETURN_TRUE;
    1114             : }
    1115             : /* }}} */
    1116             : 
    1117             : /* {{{ Common functions shared by extensions */
    1118         809 : int php_libxml_xmlCheckUTF8(const unsigned char *s)
    1119             : {
    1120             :         int i;
    1121             :         unsigned char c;
    1122             : 
    1123        9344 :         for (i = 0; (c = s[i++]);) {
    1124        7727 :                 if ((c & 0x80) == 0) {
    1125         157 :                 } else if ((c & 0xe0) == 0xc0) {
    1126         156 :                         if ((s[i++] & 0xc0) != 0x80) {
    1127           0 :                                 return 0;
    1128             :                         }
    1129           1 :                 } else if ((c & 0xf0) == 0xe0) {
    1130           0 :                         if ((s[i++] & 0xc0) != 0x80 || (s[i++] & 0xc0) != 0x80) {
    1131           0 :                                 return 0;
    1132             :                         }
    1133           1 :                 } else if ((c & 0xf8) == 0xf0) {
    1134           0 :                         if ((s[i++] & 0xc0) != 0x80 || (s[i++] & 0xc0) != 0x80 || (s[i++] & 0xc0) != 0x80) {
    1135           0 :                                 return 0;
    1136             :                         }
    1137             :                 } else {
    1138           1 :                         return 0;
    1139             :                 }
    1140             :         }
    1141         808 :         return 1;
    1142             : }
    1143             : 
    1144       41742 : zval *php_libxml_register_export(zend_class_entry *ce, php_libxml_export_node export_function)
    1145             : {
    1146             :         php_libxml_func_handler export_hnd;
    1147             : 
    1148             :         /* Initialize in case this module hasn't been loaded yet */
    1149       41742 :         php_libxml_initialize();
    1150       41742 :         export_hnd.export_func = export_function;
    1151             : 
    1152       83484 :         return zend_hash_add_mem(&php_libxml_exports, ce->name, &export_hnd, sizeof(export_hnd));
    1153             : }
    1154             : 
    1155         113 : PHP_LIBXML_API xmlNodePtr php_libxml_import_node(zval *object)
    1156             : {
    1157         113 :         zend_class_entry *ce = NULL;
    1158         113 :         xmlNodePtr node = NULL;
    1159             :         php_libxml_func_handler *export_hnd;
    1160             : 
    1161         113 :         if (Z_TYPE_P(object) == IS_OBJECT) {
    1162         113 :                 ce = Z_OBJCE_P(object);
    1163         338 :                 while (ce->parent != NULL) {
    1164         112 :                         ce = ce->parent;
    1165             :                 }
    1166         226 :                 if ((export_hnd = zend_hash_find_ptr(&php_libxml_exports, ce->name))) {
    1167         113 :                         node = export_hnd->export_func(object);
    1168             :                 }
    1169             :         }
    1170         113 :         return node;
    1171             : }
    1172             : 
    1173      404719 : PHP_LIBXML_API int php_libxml_increment_node_ptr(php_libxml_node_object *object, xmlNodePtr node, void *private_data)
    1174             : {
    1175      404719 :         int ret_refcount = -1;
    1176             : 
    1177      404719 :         if (object != NULL && node != NULL) {
    1178      404718 :                 if (object->node != NULL) {
    1179           0 :                         if (object->node->node == node) {
    1180           0 :                                 return object->node->refcount;
    1181             :                         } else {
    1182           0 :                                 php_libxml_decrement_node_ptr(object);
    1183             :                         }
    1184             :                 }
    1185      404718 :                 if (node->_private != NULL) {
    1186      200647 :                         object->node = node->_private;
    1187      200647 :                         ret_refcount = ++object->node->refcount;
    1188             :                         /* Only dom uses _private */
    1189      200647 :                         if (object->node->_private == NULL) {
    1190      200647 :                                 object->node->_private = private_data;
    1191             :                         }
    1192             :                 } else {
    1193      204071 :                         ret_refcount = 1;
    1194      204071 :                         object->node = emalloc(sizeof(php_libxml_node_ptr));
    1195      204071 :                         object->node->node = node;
    1196      204071 :                         object->node->refcount = 1;
    1197      204071 :                         object->node->_private = private_data;
    1198      204071 :                         node->_private = object->node;
    1199             :                 }
    1200             :         }
    1201             : 
    1202      404719 :         return ret_refcount;
    1203             : }
    1204             : 
    1205      404718 : PHP_LIBXML_API int php_libxml_decrement_node_ptr(php_libxml_node_object *object)
    1206             : {
    1207      404718 :         int ret_refcount = -1;
    1208             :         php_libxml_node_ptr *obj_node;
    1209             : 
    1210      404718 :         if (object != NULL && object->node != NULL) {
    1211      404718 :                 obj_node = (php_libxml_node_ptr *) object->node;
    1212      404718 :                 ret_refcount = --obj_node->refcount;
    1213      404718 :                 if (ret_refcount == 0) {
    1214      204071 :                         if (obj_node->node != NULL) {
    1215      204068 :                                 obj_node->node->_private = NULL;
    1216             :                         }
    1217      204071 :                         efree(obj_node);
    1218             :                 }
    1219      404718 :                 object->node = NULL;
    1220             :         }
    1221             : 
    1222      404718 :         return ret_refcount;
    1223             : }
    1224             : 
    1225        3413 : PHP_LIBXML_API int php_libxml_increment_doc_ref(php_libxml_node_object *object, xmlDocPtr docp)
    1226             : {
    1227        3413 :         int ret_refcount = -1;
    1228             : 
    1229        3413 :         if (object->document != NULL) {
    1230         633 :                 object->document->refcount++;
    1231         633 :                 ret_refcount = object->document->refcount;
    1232        2780 :         } else if (docp != NULL) {
    1233        2780 :                 ret_refcount = 1;
    1234        2780 :                 object->document = emalloc(sizeof(php_libxml_ref_obj));
    1235        2780 :                 object->document->ptr = docp;
    1236        2780 :                 object->document->refcount = ret_refcount;
    1237        2780 :                 object->document->doc_props = NULL;
    1238             :         }
    1239             : 
    1240        3413 :         return ret_refcount;
    1241             : }
    1242             : 
    1243      404919 : PHP_LIBXML_API int php_libxml_decrement_doc_ref(php_libxml_node_object *object)
    1244             : {
    1245      404919 :         int ret_refcount = -1;
    1246             : 
    1247      404919 :         if (object != NULL && object->document != NULL) {
    1248      404753 :                 ret_refcount = --object->document->refcount;
    1249      404753 :                 if (ret_refcount == 0) {
    1250        2780 :                         if (object->document->ptr != NULL) {
    1251        2780 :                                 xmlFreeDoc((xmlDoc *) object->document->ptr);
    1252             :                         }
    1253        2780 :                         if (object->document->doc_props != NULL) {
    1254         346 :                                 if (object->document->doc_props->classmap) {
    1255           1 :                                         zend_hash_destroy(object->document->doc_props->classmap);
    1256           1 :                                         FREE_HASHTABLE(object->document->doc_props->classmap);
    1257             :                                 }
    1258         346 :                                 efree(object->document->doc_props);
    1259             :                         }
    1260        2780 :                         efree(object->document);
    1261        2780 :                         object->document = NULL;
    1262             :                 }
    1263             :         }
    1264             : 
    1265      404919 :         return ret_refcount;
    1266             : }
    1267             : 
    1268      203466 : PHP_LIBXML_API void php_libxml_node_free_resource(xmlNodePtr node)
    1269             : {
    1270      203466 :         if (!node) {
    1271           3 :                 return;
    1272             :         }
    1273             : 
    1274      203463 :         switch (node->type) {
    1275             :                 case XML_DOCUMENT_NODE:
    1276             :                 case XML_HTML_DOCUMENT_NODE:
    1277           0 :                         break;
    1278             :                 default:
    1279      203610 :                         if (node->parent == NULL || node->type == XML_NAMESPACE_DECL) {
    1280         147 :                                 php_libxml_node_free_list((xmlNodePtr) node->children);
    1281         147 :                                 switch (node->type) {
    1282             :                                         /* Skip property freeing for the following types */
    1283             :                                         case XML_ATTRIBUTE_DECL:
    1284             :                                         case XML_DTD_NODE:
    1285             :                                         case XML_DOCUMENT_TYPE_NODE:
    1286             :                                         case XML_ENTITY_DECL:
    1287             :                                         case XML_ATTRIBUTE_NODE:
    1288             :                                         case XML_NAMESPACE_DECL:
    1289             :                                         case XML_TEXT_NODE:
    1290          46 :                                                 break;
    1291             :                                         default:
    1292         101 :                                                 php_libxml_node_free_list((xmlNodePtr) node->properties);
    1293             :                                 }
    1294         147 :                                 if (php_libxml_unregister_node(node) == 0) {
    1295           0 :                                         node->doc = NULL;
    1296             :                                 }
    1297         147 :                                 php_libxml_node_free(node);
    1298             :                         } else {
    1299      203316 :                                 php_libxml_unregister_node(node);
    1300             :                         }
    1301             :         }
    1302             : }
    1303             : 
    1304      404077 : PHP_LIBXML_API void php_libxml_node_decrement_resource(php_libxml_node_object *object)
    1305             : {
    1306      404077 :         int ret_refcount = -1;
    1307             :         xmlNodePtr nodep;
    1308             :         php_libxml_node_ptr *obj_node;
    1309             : 
    1310      404077 :         if (object != NULL && object->node != NULL) {
    1311      404075 :                 obj_node = (php_libxml_node_ptr *) object->node;
    1312      404075 :                 nodep = object->node->node;
    1313      404075 :                 ret_refcount = php_libxml_decrement_node_ptr(object);
    1314      404075 :                 if (ret_refcount == 0) {
    1315      203428 :                         php_libxml_node_free_resource(nodep);
    1316             :                 } else {
    1317      200647 :                         if (obj_node && object == obj_node->_private) {
    1318           1 :                                 obj_node->_private = NULL;
    1319             :                         }
    1320             :                 }
    1321             :         }
    1322      404077 :         if (object != NULL && object->document != NULL) {
    1323             :                 /* Safe to call as if the resource were freed then doc pointer is NULL */
    1324      404053 :                 php_libxml_decrement_doc_ref(object);
    1325             :         }
    1326      404077 : }
    1327             : /* }}} */
    1328             : 
    1329             : #ifdef PHP_WIN32
    1330             : PHP_LIBXML_API BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
    1331             : {
    1332             :         return xmlDllMain(hinstDLL, fdwReason, lpvReserved);
    1333             : }
    1334             : #endif
    1335             : 
    1336             : #endif
    1337             : 
    1338             : /*
    1339             :  * Local variables:
    1340             :  * tab-width: 4
    1341             :  * c-basic-offset: 4
    1342             :  * End:
    1343             :  * vim600: sw=4 ts=4 fdm=marker
    1344             :  * vim<600: sw=4 ts=4
    1345             :  */

Generated by: LCOV version 1.10

Generated at Sun, 01 Mar 2015 23:22:27 +0000 (2 days ago)

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