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/standard - user_filters.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 177 227 78.0 %
Date: 2014-10-30 Functions: 14 15 93.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 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:                                                             |
      16             :    | Wez Furlong (wez@thebrainroom.com)                                   |
      17             :    | Sara Golemon (pollita@php.net)                                       |
      18             :    +----------------------------------------------------------------------+
      19             : */
      20             : 
      21             : /* $Id$ */
      22             : 
      23             : #include "php.h"
      24             : #include "php_globals.h"
      25             : #include "ext/standard/basic_functions.h"
      26             : #include "ext/standard/file.h"
      27             : 
      28             : #define PHP_STREAM_BRIGADE_RES_NAME     "userfilter.bucket brigade"
      29             : #define PHP_STREAM_BUCKET_RES_NAME "userfilter.bucket"
      30             : #define PHP_STREAM_FILTER_RES_NAME "userfilter.filter"
      31             : 
      32             : struct php_user_filter_data {
      33             :         zend_class_entry *ce;
      34             :         /* variable length; this *must* be last in the structure */
      35             :         zend_string *classname;
      36             : };
      37             : 
      38             : /* to provide context for calling into the next filter from user-space */
      39             : static int le_userfilters;
      40             : static int le_bucket_brigade;
      41             : static int le_bucket;
      42             : 
      43             : #define GET_FILTER_FROM_OBJ()   { \
      44             :         zval **tmp; \
      45             :         if (FAILURE == zend_hash_index_find(Z_OBJPROP_P(this_ptr), 0, (void**)&tmp)) { \
      46             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "filter property vanished"); \
      47             :                 RETURN_FALSE; \
      48             :         } \
      49             :         ZEND_FETCH_RESOURCE(filter, php_stream_filter*, tmp, -1, "filter", le_userfilters); \
      50             : }
      51             : 
      52             : /* define the base filter class */
      53             : 
      54         107 : PHP_FUNCTION(user_filter_nop)
      55             : {
      56         107 : }
      57             : ZEND_BEGIN_ARG_INFO(arginfo_php_user_filter_filter, 0)
      58             :         ZEND_ARG_INFO(0, in)
      59             :         ZEND_ARG_INFO(0, out)
      60             :         ZEND_ARG_INFO(1, consumed)
      61             :         ZEND_ARG_INFO(0, closing)
      62             : ZEND_END_ARG_INFO()
      63             : 
      64             : ZEND_BEGIN_ARG_INFO(arginfo_php_user_filter_onCreate, 0)
      65             : ZEND_END_ARG_INFO()
      66             : 
      67             : ZEND_BEGIN_ARG_INFO(arginfo_php_user_filter_onClose, 0)
      68             : ZEND_END_ARG_INFO()
      69             : 
      70             : static const zend_function_entry user_filter_class_funcs[] = {
      71             :         PHP_NAMED_FE(filter,    PHP_FN(user_filter_nop),                arginfo_php_user_filter_filter)
      72             :         PHP_NAMED_FE(onCreate,  PHP_FN(user_filter_nop),                arginfo_php_user_filter_onCreate)
      73             :         PHP_NAMED_FE(onClose,   PHP_FN(user_filter_nop),                arginfo_php_user_filter_onClose)
      74             :         PHP_FE_END
      75             : };
      76             : 
      77             : static zend_class_entry user_filter_class_entry;
      78             : 
      79          23 : static ZEND_RSRC_DTOR_FUNC(php_bucket_dtor)
      80             : {
      81          23 :         php_stream_bucket *bucket = (php_stream_bucket *)res->ptr;
      82          23 :         if (bucket) {
      83          23 :                 php_stream_bucket_delref(bucket TSRMLS_CC);
      84          23 :                 bucket = NULL;
      85             :         }
      86          23 : }
      87             : 
      88       20423 : PHP_MINIT_FUNCTION(user_filters)
      89             : {
      90             :         zend_class_entry *php_user_filter;
      91             :         /* init the filter class ancestor */
      92       20423 :         INIT_CLASS_ENTRY(user_filter_class_entry, "php_user_filter", user_filter_class_funcs);
      93       20423 :         if ((php_user_filter = zend_register_internal_class(&user_filter_class_entry TSRMLS_CC)) == NULL) {
      94           0 :                 return FAILURE;
      95             :         }
      96       20423 :         zend_declare_property_string(php_user_filter, "filtername", sizeof("filtername")-1, "", ZEND_ACC_PUBLIC TSRMLS_CC);
      97       20423 :         zend_declare_property_string(php_user_filter, "params", sizeof("params")-1, "", ZEND_ACC_PUBLIC TSRMLS_CC);
      98             : 
      99             :         /* init the filter resource; it has no dtor, as streams will always clean it up
     100             :          * at the correct time */
     101       20423 :         le_userfilters = zend_register_list_destructors_ex(NULL, NULL, PHP_STREAM_FILTER_RES_NAME, 0);
     102             : 
     103       20423 :         if (le_userfilters == FAILURE) {
     104           0 :                 return FAILURE;
     105             :         }
     106             : 
     107             :         /* Filters will dispose of their brigades */
     108       20423 :         le_bucket_brigade = zend_register_list_destructors_ex(NULL, NULL, PHP_STREAM_BRIGADE_RES_NAME, module_number);
     109             :         /* Brigades will dispose of their buckets */
     110       20423 :         le_bucket = zend_register_list_destructors_ex(php_bucket_dtor, NULL, PHP_STREAM_BUCKET_RES_NAME, module_number);
     111             :         
     112       20423 :         if (le_bucket_brigade == FAILURE) {
     113           0 :                 return FAILURE;
     114             :         }
     115             : 
     116       20423 :         REGISTER_LONG_CONSTANT("PSFS_PASS_ON",                        PSFS_PASS_ON,                   CONST_CS | CONST_PERSISTENT);
     117       20423 :         REGISTER_LONG_CONSTANT("PSFS_FEED_ME",                        PSFS_FEED_ME,                   CONST_CS | CONST_PERSISTENT);
     118       20423 :         REGISTER_LONG_CONSTANT("PSFS_ERR_FATAL",              PSFS_ERR_FATAL,                 CONST_CS | CONST_PERSISTENT);
     119             : 
     120       20423 :         REGISTER_LONG_CONSTANT("PSFS_FLAG_NORMAL",            PSFS_FLAG_NORMAL,               CONST_CS | CONST_PERSISTENT);
     121       20423 :         REGISTER_LONG_CONSTANT("PSFS_FLAG_FLUSH_INC", PSFS_FLAG_FLUSH_INC,    CONST_CS | CONST_PERSISTENT);
     122       20423 :         REGISTER_LONG_CONSTANT("PSFS_FLAG_FLUSH_CLOSE",       PSFS_FLAG_FLUSH_CLOSE,  CONST_CS | CONST_PERSISTENT);
     123             :         
     124       20423 :         return SUCCESS;
     125             : }
     126             : 
     127       20416 : PHP_RSHUTDOWN_FUNCTION(user_filters)
     128             : {
     129       20416 :         if (BG(user_filter_map)) {
     130           9 :                 zend_hash_destroy(BG(user_filter_map));
     131           9 :                 efree(BG(user_filter_map));
     132           9 :                 BG(user_filter_map) = NULL;
     133             :         }
     134             : 
     135       20416 :         return SUCCESS;
     136             : }
     137             : 
     138          57 : static void userfilter_dtor(php_stream_filter *thisfilter TSRMLS_DC)
     139             : {
     140          57 :         zval *obj = &thisfilter->abstract;
     141             :         zval func_name;
     142             :         zval retval;
     143             : 
     144          57 :         if (obj == NULL) {
     145             :                 /* If there's no object associated then there's nothing to dispose of */
     146           0 :                 return;
     147             :         }
     148             : 
     149         114 :         ZVAL_STRINGL(&func_name, "onclose", sizeof("onclose")-1);
     150             : 
     151          57 :         call_user_function_ex(NULL,
     152             :                         obj,
     153             :                         &func_name,
     154             :                         &retval,
     155             :                         0, NULL,
     156             :                         0, NULL TSRMLS_CC);
     157             : 
     158          57 :         zval_ptr_dtor(&retval);
     159          57 :         zval_ptr_dtor(&func_name);
     160             : 
     161             :         /* kill the object */
     162          57 :         zval_ptr_dtor(obj);
     163             : }
     164             : 
     165          57 : php_stream_filter_status_t userfilter_filter(
     166             :                         php_stream *stream,
     167             :                         php_stream_filter *thisfilter,
     168             :                         php_stream_bucket_brigade *buckets_in,
     169             :                         php_stream_bucket_brigade *buckets_out,
     170             :                         size_t *bytes_consumed,
     171             :                         int flags
     172             :                         TSRMLS_DC)
     173             : {
     174          57 :         int ret = PSFS_ERR_FATAL;
     175          57 :         zval *obj = &thisfilter->abstract;
     176             :         zval func_name;
     177             :         zval retval;
     178             :         zval args[4];
     179             :         zval zpropname;
     180             :         int call_result;
     181             : 
     182             :         /* the userfilter object probably doesn't exist anymore */
     183          57 :         if (CG(unclean_shutdown)) {
     184           0 :                 return ret;
     185             :         }
     186             : 
     187          57 :         if (!zend_hash_str_exists(Z_OBJPROP_P(obj), "stream", sizeof("stream")-1)) {
     188             :                 zval tmp;
     189             : 
     190             :                 /* Give the userfilter class a hook back to the stream */
     191          57 :                 php_stream_to_zval(stream, &tmp);
     192             :                 zval_copy_ctor(&tmp);
     193          57 :                 add_property_zval(obj, "stream", &tmp);
     194             :                 /* add_property_zval increments the refcount which is unwanted here */
     195          57 :                 zval_ptr_dtor(&tmp);
     196             :         }
     197             : 
     198         114 :         ZVAL_STRINGL(&func_name, "filter", sizeof("filter")-1);
     199             : 
     200             :         /* Setup calling arguments */
     201          57 :         ZEND_REGISTER_RESOURCE(&args[0], buckets_in, le_bucket_brigade);
     202             : 
     203          57 :         ZEND_REGISTER_RESOURCE(&args[1], buckets_out, le_bucket_brigade);
     204             : 
     205          57 :         if (bytes_consumed) {
     206          25 :                 ZVAL_LONG(&args[2], *bytes_consumed);
     207             :         } else {
     208          32 :                 ZVAL_NULL(&args[2]);
     209             :         }
     210             : 
     211          57 :         ZVAL_BOOL(&args[3], flags & PSFS_FLAG_FLUSH_CLOSE);
     212             : 
     213          57 :         call_result = call_user_function_ex(NULL,
     214             :                         obj,
     215             :                         &func_name,
     216             :                         &retval,
     217             :                         4, args,
     218             :                         0, NULL TSRMLS_CC);
     219             : 
     220          57 :         zval_ptr_dtor(&func_name);
     221             : 
     222         171 :         if (call_result == SUCCESS && Z_TYPE(retval) != IS_UNDEF) {
     223          57 :                 convert_to_long(&retval);
     224          57 :                 ret = Z_LVAL(retval);
     225           0 :         } else if (call_result == FAILURE) {
     226           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to call filter function");
     227             :         }
     228             : 
     229          57 :         if (bytes_consumed) {
     230          25 :                 *bytes_consumed = Z_LVAL_P(&args[2]);
     231             :         }
     232             : 
     233          57 :         if (buckets_in->head) {
     234          11 :                 php_stream_bucket *bucket = buckets_in->head;
     235             : 
     236          11 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unprocessed filter buckets remaining on input brigade");
     237          33 :                 while ((bucket = buckets_in->head)) {
     238             :                         /* Remove unconsumed buckets from the brigade */
     239          11 :                         php_stream_bucket_unlink(bucket TSRMLS_CC);
     240          11 :                         php_stream_bucket_delref(bucket TSRMLS_CC);
     241             :                 }
     242             :         }
     243          57 :         if (ret != PSFS_PASS_ON) {
     244          39 :                 php_stream_bucket *bucket = buckets_out->head;
     245          84 :                 while (bucket != NULL) {
     246           6 :                         php_stream_bucket_unlink(bucket TSRMLS_CC);
     247           6 :                         php_stream_bucket_delref(bucket TSRMLS_CC);
     248           6 :                         bucket = buckets_out->head;
     249             :                 }
     250             :         }
     251             : 
     252             :         /* filter resources are cleaned up by the stream destructor,
     253             :          * keeping a reference to the stream resource here would prevent it
     254             :          * from being destroyed properly */
     255         114 :         ZVAL_STRINGL(&zpropname, "stream", sizeof("stream")-1);
     256          57 :         Z_OBJ_HANDLER_P(obj, unset_property)(obj, &zpropname, NULL TSRMLS_CC);
     257          57 :         zval_ptr_dtor(&zpropname);
     258             : 
     259          57 :         zval_ptr_dtor(&args[3]);
     260          57 :         zval_ptr_dtor(&args[2]);
     261          57 :         zval_ptr_dtor(&args[1]);
     262          57 :         zval_ptr_dtor(&args[0]);
     263             : 
     264          57 :         return ret;
     265             : }
     266             : 
     267             : static php_stream_filter_ops userfilter_ops = {
     268             :         userfilter_filter,
     269             :         userfilter_dtor,
     270             :         "user-filter"
     271             : };
     272             : 
     273          57 : static php_stream_filter *user_filter_factory_create(const char *filtername,
     274             :                 zval *filterparams, int persistent TSRMLS_DC)
     275             : {
     276          57 :         struct php_user_filter_data *fdat = NULL;
     277             :         php_stream_filter *filter;
     278             :         zval obj, zfilter;
     279             :         zval func_name;
     280             :         zval retval;
     281             :         int len;
     282             :         
     283             :         /* some sanity checks */
     284          57 :         if (persistent) {
     285           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING,
     286             :                                 "cannot use a user-space filter with a persistent stream");
     287           0 :                 return NULL;
     288             :         }
     289             : 
     290          57 :         len = strlen(filtername);
     291             : 
     292             :         /* determine the classname/class entry */
     293         114 :         if (NULL == (fdat = zend_hash_str_find_ptr(BG(user_filter_map), (char*)filtername, len))) {
     294             :                 char *period;
     295             : 
     296             :                 /* Userspace Filters using ambiguous wildcards could cause problems.
     297             :            i.e.: myfilter.foo.bar will always call into myfilter.foo.*
     298             :                  never seeing myfilter.* 
     299             :            TODO: Allow failed userfilter creations to continue
     300             :                  scanning through the list */
     301           0 :                 if ((period = strrchr(filtername, '.'))) {
     302           0 :                         char *wildcard = emalloc(len + 3);
     303             : 
     304             :                         /* Search for wildcard matches instead */
     305           0 :                         memcpy(wildcard, filtername, len + 1); /* copy \0 */
     306           0 :                         period = wildcard + (period - filtername);
     307           0 :                         while (period) {
     308           0 :                                 *period = '\0';
     309           0 :                                 strncat(wildcard, ".*", 2);
     310           0 :                                 if (NULL != (fdat = zend_hash_str_find_ptr(BG(user_filter_map), wildcard, strlen(wildcard)))) {
     311           0 :                                         period = NULL;
     312             :                                 } else {
     313           0 :                                         *period = '\0';
     314           0 :                                         period = strrchr(wildcard, '.');
     315             :                                 }
     316             :                         }
     317           0 :                         efree(wildcard);
     318             :                 }
     319           0 :                 if (fdat == NULL) {
     320           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING,
     321             :                                         "Err, filter \"%s\" is not in the user-filter map, but somehow the user-filter-factory was invoked for it!?", filtername);
     322           0 :                         return NULL;
     323             :                 }
     324             :         }
     325             : 
     326             :         /* bind the classname to the actual class */
     327          57 :         if (fdat->ce == NULL) {
     328          12 :                 if (NULL == (fdat->ce = zend_lookup_class(fdat->classname TSRMLS_CC))) {
     329           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING,
     330             :                                         "user-filter \"%s\" requires class \"%s\", but that class is not defined",
     331           0 :                                         filtername, fdat->classname->val);
     332           0 :                         return NULL;
     333             :                 }
     334             :         }
     335             : 
     336          57 :         filter = php_stream_filter_alloc(&userfilter_ops, NULL, 0);
     337          57 :         if (filter == NULL) {
     338           0 :                 return NULL;
     339             :         }
     340             : 
     341             :         /* create the object */
     342          57 :         object_init_ex(&obj, fdat->ce);
     343             : 
     344             :         /* filtername */
     345          57 :         add_property_string(&obj, "filtername", (char*)filtername);
     346             :         
     347             :         /* and the parameters, if any */
     348          57 :         if (filterparams) {
     349           1 :                 add_property_zval(&obj, "params", filterparams);
     350             :         } else {
     351          56 :                 add_property_null(&obj, "params");
     352             :         }
     353             : 
     354             :         /* invoke the constructor */
     355         114 :         ZVAL_STRINGL(&func_name, "oncreate", sizeof("oncreate")-1);
     356             : 
     357          57 :         call_user_function_ex(NULL,
     358             :                         &obj,
     359             :                         &func_name,
     360             :                         &retval,
     361             :                         0, NULL,
     362             :                         0, NULL TSRMLS_CC);
     363             : 
     364          57 :         if (Z_TYPE(retval) != IS_UNDEF) {
     365          55 :                 if (Z_TYPE(retval) == IS_FALSE) {
     366             :                         /* User reported filter creation error "return false;" */
     367           0 :                         zval_ptr_dtor(&retval);
     368             : 
     369             :                         /* Kill the filter (safely) */
     370           0 :                         ZVAL_UNDEF(&filter->abstract);
     371           0 :                         php_stream_filter_free(filter TSRMLS_CC);
     372             : 
     373             :                         /* Kill the object */
     374           0 :                         zval_ptr_dtor(&obj);
     375             : 
     376             :                         /* Report failure to filter_alloc */
     377           0 :                         return NULL;
     378             :                 }                       
     379          55 :                 zval_ptr_dtor(&retval);
     380             :         }
     381          57 :         zval_ptr_dtor(&func_name);
     382             : 
     383             :         /* set the filter property, this will be used during cleanup */
     384          57 :         ZEND_REGISTER_RESOURCE(&zfilter, filter, le_userfilters);
     385          57 :         ZVAL_COPY_VALUE(&filter->abstract, &obj);
     386          57 :         add_property_zval(&obj, "filter", &zfilter);
     387             :         /* add_property_zval increments the refcount which is unwanted here */
     388          57 :         zval_ptr_dtor(&zfilter);
     389             : 
     390          57 :         return filter;
     391             : }
     392             : 
     393             : static php_stream_filter_factory user_filter_factory = {
     394             :         user_filter_factory_create
     395             : };
     396             : 
     397          13 : static void filter_item_dtor(zval *zv)
     398             : {
     399          13 :         struct php_user_filter_data *fdat = Z_PTR_P(zv);
     400          13 :         zend_string_release(fdat->classname);
     401          13 :         efree(fdat);
     402          13 : }
     403             : 
     404             : /* {{{ proto object stream_bucket_make_writeable(resource brigade)
     405             :    Return a bucket object from the brigade for operating on */
     406          40 : PHP_FUNCTION(stream_bucket_make_writeable)
     407             : {
     408             :         zval *zbrigade, zbucket;
     409             :         php_stream_bucket_brigade *brigade;
     410             :         php_stream_bucket *bucket;
     411             : 
     412          40 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zbrigade) == FAILURE) {
     413           0 :                 RETURN_FALSE;
     414             :         }
     415             : 
     416          40 :         ZEND_FETCH_RESOURCE(brigade, php_stream_bucket_brigade *, zbrigade, -1, PHP_STREAM_BRIGADE_RES_NAME, le_bucket_brigade); 
     417             : 
     418          40 :         ZVAL_NULL(return_value);
     419             : 
     420          40 :         if (brigade->head && (bucket = php_stream_bucket_make_writeable(brigade->head TSRMLS_CC))) {
     421          13 :                 ZEND_REGISTER_RESOURCE(&zbucket, bucket, le_bucket);
     422          13 :                 object_init(return_value);
     423          13 :                 add_property_zval(return_value, "bucket", &zbucket);
     424             :                 /* add_property_zval increments the refcount which is unwanted here */
     425          13 :                 zval_ptr_dtor(&zbucket);
     426          13 :                 add_property_stringl(return_value, "data", bucket->buf, bucket->buflen);
     427          13 :                 add_property_long(return_value, "datalen", bucket->buflen);
     428             :         }
     429             : }
     430             : /* }}} */
     431             : 
     432             : /* {{{ php_stream_bucket_attach */
     433          17 : static void php_stream_bucket_attach(int append, INTERNAL_FUNCTION_PARAMETERS)
     434             : {
     435             :         zval *zbrigade, *zobject;
     436             :         zval *pzbucket, *pzdata;
     437             :         php_stream_bucket_brigade *brigade;
     438             :         php_stream_bucket *bucket;
     439             : 
     440          17 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zo", &zbrigade, &zobject) == FAILURE) {
     441           0 :                 RETURN_FALSE;
     442             :         }
     443             : 
     444          17 :         if (NULL == (pzbucket = zend_hash_str_find(Z_OBJPROP_P(zobject), "bucket", sizeof("bucket")-1))) {
     445           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Object has no bucket property");
     446           0 :                 RETURN_FALSE;
     447             :         }
     448             : 
     449          17 :         ZEND_FETCH_RESOURCE(brigade, php_stream_bucket_brigade *, zbrigade, -1, PHP_STREAM_BRIGADE_RES_NAME, le_bucket_brigade);
     450          17 :         ZEND_FETCH_RESOURCE(bucket, php_stream_bucket *, pzbucket, -1, PHP_STREAM_BUCKET_RES_NAME, le_bucket);
     451             : 
     452          34 :         if (NULL != (pzdata = zend_hash_str_find(Z_OBJPROP_P(zobject), "data", sizeof("data")-1)) && Z_TYPE_P(pzdata) == IS_STRING) {
     453          17 :                 if (!bucket->own_buf) {
     454           0 :                         bucket = php_stream_bucket_make_writeable(bucket TSRMLS_CC);
     455             :                 }
     456          17 :                 if ((int)bucket->buflen != Z_STRLEN_P(pzdata)) {
     457           0 :                         bucket->buf = perealloc(bucket->buf, Z_STRLEN_P(pzdata), bucket->is_persistent);
     458           0 :                         bucket->buflen = Z_STRLEN_P(pzdata);
     459             :                 }
     460          17 :                 memcpy(bucket->buf, Z_STRVAL_P(pzdata), bucket->buflen);
     461             :         }
     462             : 
     463          17 :         if (append) {
     464          17 :                 php_stream_bucket_append(brigade, bucket TSRMLS_CC);
     465             :         } else {
     466           0 :                 php_stream_bucket_prepend(brigade, bucket TSRMLS_CC);
     467             :         }
     468             :         /* This is a hack necessary to accommodate situations where bucket is appended to the stream
     469             :          * multiple times. See bug35916.phpt for reference.
     470             :          */
     471          17 :         if (bucket->refcount == 1) {
     472          16 :                 bucket->refcount++;
     473             :         }
     474             : }
     475             : /* }}} */
     476             : 
     477             : /* {{{ proto void stream_bucket_prepend(resource brigade, resource bucket)
     478             :    Prepend bucket to brigade */
     479           0 : PHP_FUNCTION(stream_bucket_prepend)
     480             : {
     481           0 :         php_stream_bucket_attach(0, INTERNAL_FUNCTION_PARAM_PASSTHRU);
     482           0 : }
     483             : /* }}} */
     484             : 
     485             : /* {{{ proto void stream_bucket_append(resource brigade, resource bucket)
     486             :    Append bucket to brigade */
     487          17 : PHP_FUNCTION(stream_bucket_append)
     488             : {
     489          17 :         php_stream_bucket_attach(1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
     490          17 : }
     491             : /* }}} */
     492             : 
     493             : /* {{{ proto resource stream_bucket_new(resource stream, string buffer)
     494             :    Create a new bucket for use on the current stream */
     495          10 : PHP_FUNCTION(stream_bucket_new)
     496             : {
     497             :         zval *zstream, zbucket;
     498             :         php_stream *stream;
     499             :         char *buffer;
     500             :         char *pbuffer;
     501             :         size_t buffer_len;
     502             :         php_stream_bucket *bucket;
     503             : 
     504          10 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs", &zstream, &buffer, &buffer_len) == FAILURE) {
     505           0 :                 RETURN_FALSE;
     506             :         }
     507             : 
     508          10 :         php_stream_from_zval(stream, zstream);
     509             : 
     510          20 :         if (!(pbuffer = pemalloc(buffer_len, php_stream_is_persistent(stream)))) {
     511           0 :                 RETURN_FALSE;
     512             :         }
     513             : 
     514          10 :         memcpy(pbuffer, buffer, buffer_len);
     515             : 
     516          10 :         bucket = php_stream_bucket_new(stream, pbuffer, buffer_len, 1, php_stream_is_persistent(stream) TSRMLS_CC);
     517             :         
     518          10 :         if (bucket == NULL) {
     519           0 :                 RETURN_FALSE;
     520             :         }
     521             : 
     522          10 :         ZEND_REGISTER_RESOURCE(&zbucket, bucket, le_bucket);
     523          10 :         object_init(return_value);
     524          10 :         add_property_zval(return_value, "bucket", &zbucket);
     525             :         /* add_property_zval increments the refcount which is unwanted here */
     526          10 :         zval_ptr_dtor(&zbucket);
     527          10 :         add_property_stringl(return_value, "data", bucket->buf, bucket->buflen);
     528          10 :         add_property_long(return_value, "datalen", bucket->buflen);
     529             : }
     530             : /* }}} */
     531             : 
     532             : /* {{{ proto array stream_get_filters(void)
     533             :    Returns a list of registered filters */
     534           6 : PHP_FUNCTION(stream_get_filters)
     535             : {
     536             :         zend_string *filter_name;
     537             :         HashTable *filters_hash;
     538             : 
     539           6 :         if (zend_parse_parameters_none() == FAILURE) {
     540           0 :                 return;
     541             :         }
     542             : 
     543           6 :         array_init(return_value);
     544             : 
     545           6 :         filters_hash = php_get_stream_filters_hash();
     546             : 
     547           6 :         if (filters_hash) {
     548         150 :                 ZEND_HASH_FOREACH_STR_KEY(filters_hash, filter_name) {
     549          72 :                         if (filter_name) {
     550          72 :                                 add_next_index_str(return_value, zend_string_copy(filter_name));
     551             :                         }
     552             :                 } ZEND_HASH_FOREACH_END();
     553             :         }
     554             :         /* It's okay to return an empty array if no filters are registered */
     555             : }
     556             : /* }}} */       
     557             : 
     558             : /* {{{ proto bool stream_filter_register(string filtername, string classname)
     559             :    Registers a custom filter handler class */
     560          18 : PHP_FUNCTION(stream_filter_register)
     561             : {
     562             :         zend_string *filtername, *classname;
     563             :         struct php_user_filter_data *fdat;
     564             :         
     565          18 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SS", &filtername, &classname) == FAILURE) {
     566           2 :                 RETURN_FALSE;
     567             :         }
     568             : 
     569          16 :         RETVAL_FALSE;
     570             : 
     571          16 :         if (!filtername->len) {
     572           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Filter name cannot be empty");
     573           2 :                 return;
     574             :         }
     575             : 
     576          14 :         if (!classname->len) {
     577           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Class name cannot be empty");
     578           1 :                 return;
     579             :         }
     580             : 
     581          13 :         if (!BG(user_filter_map)) {
     582           9 :                 BG(user_filter_map) = (HashTable*) emalloc(sizeof(HashTable));
     583           9 :                 zend_hash_init(BG(user_filter_map), 8, NULL, (dtor_func_t) filter_item_dtor, 0);
     584             :         }
     585             : 
     586          13 :         fdat = ecalloc(1, sizeof(struct php_user_filter_data));
     587          26 :         fdat->classname = zend_string_copy(classname);
     588             : 
     589          52 :         if (zend_hash_add_ptr(BG(user_filter_map), filtername, fdat) != NULL &&
     590          13 :                         php_stream_filter_register_factory_volatile(filtername->val, &user_filter_factory TSRMLS_CC) == SUCCESS) {
     591          13 :                 RETVAL_TRUE;
     592             :         } else {
     593           0 :                 zend_string_release(classname);
     594           0 :                 efree(fdat);
     595             :         }
     596             : }
     597             : /* }}} */
     598             : 
     599             : 
     600             : /*
     601             :  * Local variables:
     602             :  * tab-width: 4
     603             :  * c-basic-offset: 4
     604             :  * End:
     605             :  * vim600: sw=4 ts=4 fdm=marker
     606             :  * vim<600: sw=4 ts=4
     607             :  */

Generated by: LCOV version 1.10

Generated at Thu, 30 Oct 2014 07:41:42 +0000 (38 hours ago)

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