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 - main/streams - userspace.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 515 611 84.3 %
Date: 2016-08-24 Functions: 25 26 96.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2016 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Wez Furlong <wez@thebrainroom.com>                          |
      16             :    |          Sara Golemon <pollita@php.net>                              |
      17             :    +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #include "php.h"
      23             : #include "php_globals.h"
      24             : #include "ext/standard/file.h"
      25             : #include "ext/standard/flock_compat.h"
      26             : #ifdef HAVE_SYS_FILE_H
      27             : #include <sys/file.h>
      28             : #endif
      29             : #include <stddef.h>
      30             : 
      31             : #if HAVE_UTIME
      32             : # ifdef PHP_WIN32
      33             : #  include <sys/utime.h>
      34             : # else
      35             : #  include <utime.h>
      36             : # endif
      37             : #endif
      38             : 
      39             : static int le_protocols;
      40             : 
      41             : struct php_user_stream_wrapper {
      42             :         char * protoname;
      43             :         char * classname;
      44             :         zend_class_entry *ce;
      45             :         php_stream_wrapper wrapper;
      46             : };
      47             : 
      48             : static php_stream *user_wrapper_opener(php_stream_wrapper *wrapper, const char *filename, const char *mode, int options, zend_string **opened_path, php_stream_context *context STREAMS_DC);
      49             : static int user_wrapper_stat_url(php_stream_wrapper *wrapper, const char *url, int flags, php_stream_statbuf *ssb, php_stream_context *context);
      50             : static int user_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context);
      51             : static int user_wrapper_rename(php_stream_wrapper *wrapper, const char *url_from, const char *url_to, int options, php_stream_context *context);
      52             : static int user_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url, int mode, int options, php_stream_context *context);
      53             : static int user_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context);
      54             : static int user_wrapper_metadata(php_stream_wrapper *wrapper, const char *url, int option, void *value, php_stream_context *context);
      55             : static php_stream *user_wrapper_opendir(php_stream_wrapper *wrapper, const char *filename, const char *mode,
      56             :                 int options, zend_string **opened_path, php_stream_context *context STREAMS_DC);
      57             : 
      58             : static php_stream_wrapper_ops user_stream_wops = {
      59             :         user_wrapper_opener,
      60             :         NULL, /* close - the streams themselves know how */
      61             :         NULL, /* stat - the streams themselves know how */
      62             :         user_wrapper_stat_url,
      63             :         user_wrapper_opendir,
      64             :         "user-space",
      65             :         user_wrapper_unlink,
      66             :         user_wrapper_rename,
      67             :         user_wrapper_mkdir,
      68             :         user_wrapper_rmdir,
      69             :         user_wrapper_metadata
      70             : };
      71             : 
      72             : 
      73          60 : static void stream_wrapper_dtor(zend_resource *rsrc)
      74             : {
      75          60 :         struct php_user_stream_wrapper * uwrap = (struct php_user_stream_wrapper*)rsrc->ptr;
      76             : 
      77          60 :         efree(uwrap->protoname);
      78          60 :         efree(uwrap->classname);
      79          60 :         efree(uwrap);
      80          60 : }
      81             : 
      82             : 
      83       23504 : PHP_MINIT_FUNCTION(user_streams)
      84             : {
      85       23504 :         le_protocols = zend_register_list_destructors_ex(stream_wrapper_dtor, NULL, "stream factory", 0);
      86       23504 :         if (le_protocols == FAILURE)
      87           0 :                 return FAILURE;
      88             : 
      89       23504 :         REGISTER_LONG_CONSTANT("STREAM_USE_PATH",                     USE_PATH, CONST_CS|CONST_PERSISTENT);
      90       23504 :         REGISTER_LONG_CONSTANT("STREAM_IGNORE_URL",           IGNORE_URL, CONST_CS|CONST_PERSISTENT);
      91       23504 :         REGISTER_LONG_CONSTANT("STREAM_REPORT_ERRORS",                REPORT_ERRORS, CONST_CS|CONST_PERSISTENT);
      92       23504 :         REGISTER_LONG_CONSTANT("STREAM_MUST_SEEK",                    STREAM_MUST_SEEK, CONST_CS|CONST_PERSISTENT);
      93             : 
      94       23504 :         REGISTER_LONG_CONSTANT("STREAM_URL_STAT_LINK",                PHP_STREAM_URL_STAT_LINK,               CONST_CS|CONST_PERSISTENT);
      95       23504 :         REGISTER_LONG_CONSTANT("STREAM_URL_STAT_QUIET",       PHP_STREAM_URL_STAT_QUIET,              CONST_CS|CONST_PERSISTENT);
      96       23504 :         REGISTER_LONG_CONSTANT("STREAM_MKDIR_RECURSIVE",      PHP_STREAM_MKDIR_RECURSIVE,             CONST_CS|CONST_PERSISTENT);
      97             : 
      98       23504 :         REGISTER_LONG_CONSTANT("STREAM_IS_URL",       PHP_STREAM_IS_URL,              CONST_CS|CONST_PERSISTENT);
      99             : 
     100       23504 :         REGISTER_LONG_CONSTANT("STREAM_OPTION_BLOCKING",      PHP_STREAM_OPTION_BLOCKING,             CONST_CS|CONST_PERSISTENT);
     101       23504 :         REGISTER_LONG_CONSTANT("STREAM_OPTION_READ_TIMEOUT",  PHP_STREAM_OPTION_READ_TIMEOUT,         CONST_CS|CONST_PERSISTENT);
     102       23504 :         REGISTER_LONG_CONSTANT("STREAM_OPTION_READ_BUFFER",   PHP_STREAM_OPTION_READ_BUFFER,          CONST_CS|CONST_PERSISTENT);
     103       23504 :         REGISTER_LONG_CONSTANT("STREAM_OPTION_WRITE_BUFFER",  PHP_STREAM_OPTION_WRITE_BUFFER,         CONST_CS|CONST_PERSISTENT);
     104             : 
     105       23504 :         REGISTER_LONG_CONSTANT("STREAM_BUFFER_NONE",          PHP_STREAM_BUFFER_NONE,                 CONST_CS|CONST_PERSISTENT);
     106       23504 :         REGISTER_LONG_CONSTANT("STREAM_BUFFER_LINE",          PHP_STREAM_BUFFER_LINE,                 CONST_CS|CONST_PERSISTENT);
     107       23504 :         REGISTER_LONG_CONSTANT("STREAM_BUFFER_FULL",          PHP_STREAM_BUFFER_FULL,                 CONST_CS|CONST_PERSISTENT);
     108             : 
     109       23504 :         REGISTER_LONG_CONSTANT("STREAM_CAST_AS_STREAM",               PHP_STREAM_AS_STDIO,                    CONST_CS|CONST_PERSISTENT);
     110       23504 :         REGISTER_LONG_CONSTANT("STREAM_CAST_FOR_SELECT",      PHP_STREAM_AS_FD_FOR_SELECT,            CONST_CS|CONST_PERSISTENT);
     111             : 
     112       23504 :         REGISTER_LONG_CONSTANT("STREAM_META_TOUCH",                   PHP_STREAM_META_TOUCH,                  CONST_CS|CONST_PERSISTENT);
     113       23504 :         REGISTER_LONG_CONSTANT("STREAM_META_OWNER",                   PHP_STREAM_META_OWNER,                  CONST_CS|CONST_PERSISTENT);
     114       23504 :         REGISTER_LONG_CONSTANT("STREAM_META_OWNER_NAME",      PHP_STREAM_META_OWNER_NAME,             CONST_CS|CONST_PERSISTENT);
     115       23504 :         REGISTER_LONG_CONSTANT("STREAM_META_GROUP",                   PHP_STREAM_META_GROUP,                  CONST_CS|CONST_PERSISTENT);
     116       23504 :         REGISTER_LONG_CONSTANT("STREAM_META_GROUP_NAME",      PHP_STREAM_META_GROUP_NAME,             CONST_CS|CONST_PERSISTENT);
     117       23504 :         REGISTER_LONG_CONSTANT("STREAM_META_ACCESS",          PHP_STREAM_META_ACCESS,                 CONST_CS|CONST_PERSISTENT);
     118       23504 :         return SUCCESS;
     119             : }
     120             : 
     121             : struct _php_userstream_data {
     122             :         struct php_user_stream_wrapper * wrapper;
     123             :         zval object;
     124             : };
     125             : typedef struct _php_userstream_data php_userstream_data_t;
     126             : 
     127             : /* names of methods */
     128             : #define USERSTREAM_OPEN         "stream_open"
     129             : #define USERSTREAM_CLOSE        "stream_close"
     130             : #define USERSTREAM_READ         "stream_read"
     131             : #define USERSTREAM_WRITE        "stream_write"
     132             : #define USERSTREAM_FLUSH        "stream_flush"
     133             : #define USERSTREAM_SEEK         "stream_seek"
     134             : #define USERSTREAM_TELL         "stream_tell"
     135             : #define USERSTREAM_EOF          "stream_eof"
     136             : #define USERSTREAM_STAT         "stream_stat"
     137             : #define USERSTREAM_STATURL      "url_stat"
     138             : #define USERSTREAM_UNLINK       "unlink"
     139             : #define USERSTREAM_RENAME       "rename"
     140             : #define USERSTREAM_MKDIR        "mkdir"
     141             : #define USERSTREAM_RMDIR        "rmdir"
     142             : #define USERSTREAM_DIR_OPEN             "dir_opendir"
     143             : #define USERSTREAM_DIR_READ             "dir_readdir"
     144             : #define USERSTREAM_DIR_REWIND   "dir_rewinddir"
     145             : #define USERSTREAM_DIR_CLOSE    "dir_closedir"
     146             : #define USERSTREAM_LOCK     "stream_lock"
     147             : #define USERSTREAM_CAST         "stream_cast"
     148             : #define USERSTREAM_SET_OPTION   "stream_set_option"
     149             : #define USERSTREAM_TRUNCATE     "stream_truncate"
     150             : #define USERSTREAM_METADATA     "stream_metadata"
     151             : 
     152             : /* {{{ class should have methods like these:
     153             : 
     154             :         function stream_open($path, $mode, $options, &$opened_path)
     155             :         {
     156             :                 return true/false;
     157             :         }
     158             : 
     159             :         function stream_read($count)
     160             :         {
     161             :                 return false on error;
     162             :                 else return string;
     163             :         }
     164             : 
     165             :         function stream_write($data)
     166             :         {
     167             :                 return false on error;
     168             :                 else return count written;
     169             :         }
     170             : 
     171             :         function stream_close()
     172             :         {
     173             :         }
     174             : 
     175             :         function stream_flush()
     176             :         {
     177             :                 return true/false;
     178             :         }
     179             : 
     180             :         function stream_seek($offset, $whence)
     181             :         {
     182             :                 return true/false;
     183             :         }
     184             : 
     185             :         function stream_tell()
     186             :         {
     187             :                 return (int)$position;
     188             :         }
     189             : 
     190             :         function stream_eof()
     191             :         {
     192             :                 return true/false;
     193             :         }
     194             : 
     195             :         function stream_stat()
     196             :         {
     197             :                 return array( just like that returned by fstat() );
     198             :         }
     199             : 
     200             :         function stream_cast($castas)
     201             :         {
     202             :                 if ($castas == STREAM_CAST_FOR_SELECT) {
     203             :                         return $this->underlying_stream;
     204             :                 }
     205             :                 return false;
     206             :         }
     207             : 
     208             :         function stream_set_option($option, $arg1, $arg2)
     209             :         {
     210             :                 switch($option) {
     211             :                 case STREAM_OPTION_BLOCKING:
     212             :                         $blocking = $arg1;
     213             :                         ...
     214             :                 case STREAM_OPTION_READ_TIMEOUT:
     215             :                         $sec = $arg1;
     216             :                         $usec = $arg2;
     217             :                         ...
     218             :                 case STREAM_OPTION_WRITE_BUFFER:
     219             :                         $mode = $arg1;
     220             :                         $size = $arg2;
     221             :                         ...
     222             :                 default:
     223             :                         return false;
     224             :                 }
     225             :         }
     226             : 
     227             :         function url_stat(string $url, int $flags)
     228             :         {
     229             :                 return array( just like that returned by stat() );
     230             :         }
     231             : 
     232             :         function unlink(string $url)
     233             :         {
     234             :                 return true / false;
     235             :         }
     236             : 
     237             :         function rename(string $from, string $to)
     238             :         {
     239             :                 return true / false;
     240             :         }
     241             : 
     242             :         function mkdir($dir, $mode, $options)
     243             :         {
     244             :                 return true / false;
     245             :         }
     246             : 
     247             :         function rmdir($dir, $options)
     248             :         {
     249             :                 return true / false;
     250             :         }
     251             : 
     252             :         function dir_opendir(string $url, int $options)
     253             :         {
     254             :                 return true / false;
     255             :         }
     256             : 
     257             :         function dir_readdir()
     258             :         {
     259             :                 return string next filename in dir ;
     260             :         }
     261             : 
     262             :         function dir_closedir()
     263             :         {
     264             :                 release dir related resources;
     265             :         }
     266             : 
     267             :         function dir_rewinddir()
     268             :         {
     269             :                 reset to start of dir list;
     270             :         }
     271             : 
     272             :         function stream_lock($operation)
     273             :         {
     274             :                 return true / false;
     275             :         }
     276             : 
     277             :         function stream_truncate($new_size)
     278             :         {
     279             :                 return true / false;
     280             :         }
     281             : 
     282             :         }}} **/
     283             : 
     284         102 : static void user_stream_create_object(struct php_user_stream_wrapper *uwrap, php_stream_context *context, zval *object)
     285             : {
     286             :         /* create an instance of our class */
     287         102 :         object_init_ex(object, uwrap->ce);
     288             : 
     289         102 :         if (context) {
     290          63 :                 add_property_resource(object, "context", context->res);
     291          63 :                 GC_REFCOUNT(context->res)++;
     292             :         } else {
     293          39 :                 add_property_null(object, "context");
     294             :         }
     295             : 
     296         102 :         if (uwrap->ce->constructor) {
     297             :                 zend_fcall_info fci;
     298             :                 zend_fcall_info_cache fcc;
     299             :                 zval retval;
     300             : 
     301          12 :                 fci.size = sizeof(fci);
     302          12 :                 ZVAL_UNDEF(&fci.function_name);
     303          12 :                 fci.object = Z_OBJ_P(object);
     304          12 :                 fci.retval = &retval;
     305          12 :                 fci.param_count = 0;
     306          12 :                 fci.params = NULL;
     307          12 :                 fci.no_separation = 1;
     308             : 
     309          12 :                 fcc.initialized = 1;
     310          12 :                 fcc.function_handler = uwrap->ce->constructor;
     311          12 :                 fcc.calling_scope = zend_get_executed_scope();
     312          12 :                 fcc.called_scope = Z_OBJCE_P(object);
     313          12 :                 fcc.object = Z_OBJ_P(object);
     314             : 
     315          12 :                 if (zend_call_function(&fci, &fcc) == FAILURE) {
     316           0 :                         php_error_docref(NULL, E_WARNING, "Could not execute %s::%s()", ZSTR_VAL(uwrap->ce->name), ZSTR_VAL(uwrap->ce->constructor->common.function_name));
     317             :                         zval_dtor(object);
     318           0 :                         ZVAL_UNDEF(object);
     319             :                 } else {
     320          12 :                         zval_ptr_dtor(&retval);
     321             :                 }
     322             :         }
     323         102 : }
     324             : 
     325          68 : static php_stream *user_wrapper_opener(php_stream_wrapper *wrapper, const char *filename, const char *mode,
     326             :                                                                            int options, zend_string **opened_path, php_stream_context *context STREAMS_DC)
     327             : {
     328          68 :         struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract;
     329             :         php_userstream_data_t *us;
     330             :         zval zretval, zfuncname;
     331             :         zval args[4];
     332             :         int call_result;
     333          68 :         php_stream *stream = NULL;
     334             :         zend_bool old_in_user_include;
     335             : 
     336             :         /* Try to catch bad usage without preventing flexibility */
     337          68 :         if (FG(user_stream_current_filename) != NULL && strcmp(filename, FG(user_stream_current_filename)) == 0) {
     338           0 :                 php_stream_wrapper_log_error(wrapper, options, "infinite recursion prevented");
     339           0 :                 return NULL;
     340             :         }
     341          68 :         FG(user_stream_current_filename) = filename;
     342             : 
     343             :         /* if the user stream was registered as local and we are in include context,
     344             :                 we add allow_url_include restrictions to allow_url_fopen ones */
     345             :         /* we need only is_url == 0 here since if is_url == 1 and remote wrappers
     346             :                 were restricted we wouldn't get here */
     347          68 :         old_in_user_include = PG(in_user_include);
     348         145 :         if(uwrap->wrapper.is_url == 0 &&
     349          64 :                 (options & STREAM_OPEN_FOR_INCLUDE) &&
     350          13 :                 !PG(allow_url_include)) {
     351          12 :                 PG(in_user_include) = 1;
     352             :         }
     353             : 
     354          68 :         us = emalloc(sizeof(*us));
     355          68 :         us->wrapper = uwrap;
     356             : 
     357          68 :         user_stream_create_object(uwrap, context, &us->object);
     358         136 :         if (Z_TYPE(us->object) == IS_UNDEF) {
     359           0 :                 FG(user_stream_current_filename) = NULL;
     360           0 :                 PG(in_user_include) = old_in_user_include;
     361           0 :                 efree(us);
     362           0 :                 return NULL;
     363             :         }
     364             : 
     365             :         /* call it's stream_open method - set up params first */
     366         136 :         ZVAL_STRING(&args[0], filename);
     367         136 :         ZVAL_STRING(&args[1], mode);
     368          68 :         ZVAL_LONG(&args[2], options);
     369          68 :         ZVAL_NEW_REF(&args[3], &EG(uninitialized_zval));
     370             : 
     371         136 :         ZVAL_STRING(&zfuncname, USERSTREAM_OPEN);
     372             : 
     373         136 :         call_result = call_user_function_ex(NULL,
     374             :                         Z_ISUNDEF(us->object)? NULL : &us->object,
     375             :                         &zfuncname,
     376             :                         &zretval,
     377             :                         4, args,
     378             :                         0, NULL );
     379             : 
     380         190 :         if (call_result == SUCCESS && Z_TYPE(zretval) != IS_UNDEF && zval_is_true(&zretval)) {
     381             :                 /* the stream is now open! */
     382          58 :                 stream = php_stream_alloc_rel(&php_stream_userspace_ops, us, 0, mode);
     383             : 
     384             :                 /* if the opened path is set, copy it out */
     385         116 :                 if (Z_ISREF(args[3]) && Z_TYPE_P(Z_REFVAL(args[3])) == IS_STRING && opened_path) {
     386           0 :                         *opened_path = zend_string_copy(Z_STR_P(Z_REFVAL(args[3])));
     387             :                 }
     388             : 
     389             :                 /* set wrapper data to be a reference to our object */
     390          58 :                 ZVAL_COPY(&stream->wrapperdata, &us->object);
     391             :         } else {
     392          10 :                 php_stream_wrapper_log_error(wrapper, options, "\"%s::" USERSTREAM_OPEN "\" call failed",
     393          10 :                         us->wrapper->classname);
     394             :         }
     395             : 
     396             :         /* destroy everything else */
     397          68 :         if (stream == NULL) {
     398          10 :                 zval_ptr_dtor(&us->object);
     399          10 :                 ZVAL_UNDEF(&us->object);
     400          10 :                 efree(us);
     401             :         }
     402          68 :         zval_ptr_dtor(&zretval);
     403          68 :         zval_ptr_dtor(&zfuncname);
     404          68 :         zval_ptr_dtor(&args[3]);
     405          68 :         zval_ptr_dtor(&args[2]);
     406          68 :         zval_ptr_dtor(&args[1]);
     407          68 :         zval_ptr_dtor(&args[0]);
     408             : 
     409          68 :         FG(user_stream_current_filename) = NULL;
     410             : 
     411          68 :         PG(in_user_include) = old_in_user_include;
     412          68 :         return stream;
     413             : }
     414             : 
     415           2 : static php_stream *user_wrapper_opendir(php_stream_wrapper *wrapper, const char *filename, const char *mode,
     416             :                 int options, zend_string **opened_path, php_stream_context *context STREAMS_DC)
     417             : {
     418           2 :         struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract;
     419             :         php_userstream_data_t *us;
     420             :         zval zretval, zfuncname;
     421             :         zval args[2];
     422             :         int call_result;
     423           2 :         php_stream *stream = NULL;
     424             : 
     425             :         /* Try to catch bad usage without preventing flexibility */
     426           2 :         if (FG(user_stream_current_filename) != NULL && strcmp(filename, FG(user_stream_current_filename)) == 0) {
     427           0 :                 php_stream_wrapper_log_error(wrapper, options, "infinite recursion prevented");
     428           0 :                 return NULL;
     429             :         }
     430           2 :         FG(user_stream_current_filename) = filename;
     431             : 
     432           2 :         us = emalloc(sizeof(*us));
     433           2 :         us->wrapper = uwrap;
     434             : 
     435           2 :         user_stream_create_object(uwrap, context, &us->object);
     436           4 :         if (Z_TYPE(us->object) == IS_UNDEF) {
     437           0 :                 FG(user_stream_current_filename) = NULL;
     438           0 :                 efree(us);
     439           0 :                 return NULL;
     440             :         }
     441             : 
     442             :         /* call it's dir_open method - set up params first */
     443           4 :         ZVAL_STRING(&args[0], filename);
     444           2 :         ZVAL_LONG(&args[1], options);
     445             : 
     446           4 :         ZVAL_STRING(&zfuncname, USERSTREAM_DIR_OPEN);
     447             : 
     448           4 :         call_result = call_user_function_ex(NULL,
     449             :                         Z_ISUNDEF(us->object)? NULL : &us->object,
     450             :                         &zfuncname,
     451             :                         &zretval,
     452             :                         2, args,
     453             :                         0, NULL );
     454             : 
     455           6 :         if (call_result == SUCCESS && Z_TYPE(zretval) != IS_UNDEF && zval_is_true(&zretval)) {
     456             :                 /* the stream is now open! */
     457           2 :                 stream = php_stream_alloc_rel(&php_stream_userspace_dir_ops, us, 0, mode);
     458             : 
     459             :                 /* set wrapper data to be a reference to our object */
     460           2 :                 ZVAL_COPY(&stream->wrapperdata, &us->object);
     461             :         } else {
     462           0 :                 php_stream_wrapper_log_error(wrapper, options, "\"%s::" USERSTREAM_DIR_OPEN "\" call failed",
     463           0 :                         us->wrapper->classname);
     464             :         }
     465             : 
     466             :         /* destroy everything else */
     467           2 :         if (stream == NULL) {
     468           0 :                 zval_ptr_dtor(&us->object);
     469           0 :                 ZVAL_UNDEF(&us->object);
     470           0 :                 efree(us);
     471             :         }
     472           2 :         zval_ptr_dtor(&zretval);
     473             : 
     474           2 :         zval_ptr_dtor(&zfuncname);
     475           2 :         zval_ptr_dtor(&args[1]);
     476           2 :         zval_ptr_dtor(&args[0]);
     477             : 
     478           2 :         FG(user_stream_current_filename) = NULL;
     479             : 
     480           2 :         return stream;
     481             : }
     482             : 
     483             : 
     484             : /* {{{ proto bool stream_wrapper_register(string protocol, string classname[, integer flags])
     485             :    Registers a custom URL protocol handler class */
     486          60 : PHP_FUNCTION(stream_wrapper_register)
     487             : {
     488             :         zend_string *protocol, *classname;
     489             :         struct php_user_stream_wrapper * uwrap;
     490             :         zend_resource *rsrc;
     491          60 :         zend_long flags = 0;
     492             : 
     493          60 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|l", &protocol, &classname, &flags) == FAILURE) {
     494           0 :                 RETURN_FALSE;
     495             :         }
     496             : 
     497          60 :         uwrap = (struct php_user_stream_wrapper *)ecalloc(1, sizeof(*uwrap));
     498          60 :         uwrap->protoname = estrndup(ZSTR_VAL(protocol), ZSTR_LEN(protocol));
     499          60 :         uwrap->classname = estrndup(ZSTR_VAL(classname), ZSTR_LEN(classname));
     500          60 :         uwrap->wrapper.wops = &user_stream_wops;
     501          60 :         uwrap->wrapper.abstract = uwrap;
     502          60 :         uwrap->wrapper.is_url = ((flags & PHP_STREAM_IS_URL) != 0);
     503             : 
     504          60 :         rsrc = zend_register_resource(uwrap, le_protocols);
     505             : 
     506          60 :         if ((uwrap->ce = zend_lookup_class(classname)) != NULL) {
     507          59 :                 if (php_register_url_stream_wrapper_volatile(ZSTR_VAL(protocol), &uwrap->wrapper) == SUCCESS) {
     508          59 :                         RETURN_TRUE;
     509             :                 } else {
     510             :                         /* We failed.  But why? */
     511           0 :                         if (zend_hash_exists(php_stream_get_url_stream_wrappers_hash(), protocol)) {
     512           0 :                                 php_error_docref(NULL, E_WARNING, "Protocol %s:// is already defined.", ZSTR_VAL(protocol));
     513             :                         } else {
     514             :                                 /* Hash doesn't exist so it must have been an invalid protocol scheme */
     515           0 :                                 php_error_docref(NULL, E_WARNING, "Invalid protocol scheme specified. Unable to register wrapper class %s to %s://", ZSTR_VAL(classname), ZSTR_VAL(protocol));
     516             :                         }
     517             :                 }
     518             :         } else {
     519           1 :                 php_error_docref(NULL, E_WARNING, "class '%s' is undefined", ZSTR_VAL(classname));
     520             :         }
     521             : 
     522           1 :         zend_list_delete(rsrc);
     523           1 :         RETURN_FALSE;
     524             : }
     525             : /* }}} */
     526             : 
     527             : /* {{{ proto bool stream_wrapper_unregister(string protocol)
     528             :         Unregister a wrapper for the life of the current request. */
     529           1 : PHP_FUNCTION(stream_wrapper_unregister)
     530             : {
     531             :         char *protocol;
     532             :         size_t protocol_len;
     533             : 
     534           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &protocol, &protocol_len) == FAILURE) {
     535           0 :                 RETURN_FALSE;
     536             :         }
     537             : 
     538           1 :         if (php_unregister_url_stream_wrapper_volatile(protocol) == FAILURE) {
     539             :                 /* We failed */
     540           0 :                 php_error_docref(NULL, E_WARNING, "Unable to unregister protocol %s://", protocol);
     541           0 :                 RETURN_FALSE;
     542             :         }
     543             : 
     544           1 :         RETURN_TRUE;
     545             : }
     546             : /* }}} */
     547             : 
     548             : /* {{{ proto bool stream_wrapper_restore(string protocol)
     549             :         Restore the original protocol handler, overriding if necessary */
     550           1 : PHP_FUNCTION(stream_wrapper_restore)
     551             : {
     552             :         zend_string *protocol;
     553             :         php_stream_wrapper *wrapper;
     554             :         HashTable *global_wrapper_hash;
     555             : 
     556           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &protocol) == FAILURE) {
     557           0 :                 RETURN_FALSE;
     558             :         }
     559             : 
     560           1 :         global_wrapper_hash = php_stream_get_url_stream_wrappers_hash_global();
     561           1 :         if (php_stream_get_url_stream_wrappers_hash() == global_wrapper_hash) {
     562           0 :                 php_error_docref(NULL, E_NOTICE, "%s:// was never changed, nothing to restore", ZSTR_VAL(protocol));
     563           0 :                 RETURN_TRUE;
     564             :         }
     565             : 
     566           2 :         if ((wrapper = zend_hash_find_ptr(global_wrapper_hash, protocol)) == NULL) {
     567           0 :                 php_error_docref(NULL, E_WARNING, "%s:// never existed, nothing to restore", ZSTR_VAL(protocol));
     568           0 :                 RETURN_FALSE;
     569             :         }
     570             : 
     571             :         /* A failure here could be okay given that the protocol might have been merely unregistered */
     572           1 :         php_unregister_url_stream_wrapper_volatile(ZSTR_VAL(protocol));
     573             : 
     574           1 :         if (php_register_url_stream_wrapper_volatile(ZSTR_VAL(protocol), wrapper) == FAILURE) {
     575           0 :                 php_error_docref(NULL, E_WARNING, "Unable to restore original %s:// wrapper", ZSTR_VAL(protocol));
     576           0 :                 RETURN_FALSE;
     577             :         }
     578             : 
     579           1 :         RETURN_TRUE;
     580             : }
     581             : /* }}} */
     582             : 
     583          22 : static size_t php_userstreamop_write(php_stream *stream, const char *buf, size_t count)
     584             : {
     585             :         zval func_name;
     586             :         zval retval;
     587             :         int call_result;
     588          22 :         php_userstream_data_t *us = (php_userstream_data_t *)stream->abstract;
     589             :         zval args[1];
     590          22 :         size_t didwrite = 0;
     591             : 
     592             :         assert(us != NULL);
     593             : 
     594          44 :         ZVAL_STRINGL(&func_name, USERSTREAM_WRITE, sizeof(USERSTREAM_WRITE)-1);
     595             : 
     596          44 :         ZVAL_STRINGL(&args[0], (char*)buf, count);
     597             : 
     598          44 :         call_result = call_user_function_ex(NULL,
     599             :                         Z_ISUNDEF(us->object)? NULL : &us->object,
     600             :                         &func_name,
     601             :                         &retval,
     602             :                         1, args,
     603             :                         0, NULL);
     604          22 :         zval_ptr_dtor(&args[0]);
     605          22 :         zval_ptr_dtor(&func_name);
     606             : 
     607          22 :         didwrite = 0;
     608             : 
     609          22 :         if (EG(exception)) {
     610           1 :                 return 0;
     611             :         }
     612             : 
     613          63 :         if (call_result == SUCCESS && Z_TYPE(retval) != IS_UNDEF) {
     614          21 :                 convert_to_long(&retval);
     615          21 :                 didwrite = Z_LVAL(retval);
     616           0 :         } else if (call_result == FAILURE) {
     617           0 :                 php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_WRITE " is not implemented!",
     618           0 :                                 us->wrapper->classname);
     619             :         }
     620             : 
     621             :         /* don't allow strange buffer overruns due to bogus return */
     622          21 :         if (didwrite > count) {
     623           0 :                 php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_WRITE " wrote " ZEND_LONG_FMT " bytes more data than requested (" ZEND_LONG_FMT " written, " ZEND_LONG_FMT " max)",
     624           0 :                                 us->wrapper->classname,
     625           0 :                                 (zend_long)(didwrite - count), (zend_long)didwrite, (zend_long)count);
     626           0 :                 didwrite = count;
     627             :         }
     628             : 
     629          21 :         zval_ptr_dtor(&retval);
     630             : 
     631          21 :         return didwrite;
     632             : }
     633             : 
     634        1109 : static size_t php_userstreamop_read(php_stream *stream, char *buf, size_t count)
     635             : {
     636             :         zval func_name;
     637             :         zval retval;
     638             :         zval args[1];
     639             :         int call_result;
     640        1109 :         size_t didread = 0;
     641        1109 :         php_userstream_data_t *us = (php_userstream_data_t *)stream->abstract;
     642             : 
     643             :         assert(us != NULL);
     644             : 
     645        2218 :         ZVAL_STRINGL(&func_name, USERSTREAM_READ, sizeof(USERSTREAM_READ)-1);
     646             : 
     647        1109 :         ZVAL_LONG(&args[0], count);
     648             : 
     649        2218 :         call_result = call_user_function_ex(NULL,
     650             :                         Z_ISUNDEF(us->object)? NULL : &us->object,
     651             :                         &func_name,
     652             :                         &retval,
     653             :                         1, args,
     654             :                         0, NULL);
     655             : 
     656        1109 :         zval_ptr_dtor(&args[0]);
     657        1109 :         zval_ptr_dtor(&func_name);
     658             : 
     659        1109 :         if (EG(exception)) {
     660           1 :                 return -1;
     661             :         }
     662             : 
     663        3324 :         if (call_result == SUCCESS && Z_TYPE(retval) != IS_UNDEF) {
     664        1108 :                 convert_to_string(&retval);
     665        1108 :                 didread = Z_STRLEN(retval);
     666        1108 :                 if (didread > count) {
     667           0 :                         php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_READ " - read " ZEND_LONG_FMT " bytes more data than requested (" ZEND_LONG_FMT " read, " ZEND_LONG_FMT " max) - excess data will be lost",
     668           0 :                                         us->wrapper->classname, (zend_long)(didread - count), (zend_long)didread, (zend_long)count);
     669           0 :                         didread = count;
     670             :                 }
     671        1108 :                 if (didread > 0)
     672        1081 :                         memcpy(buf, Z_STRVAL(retval), didread);
     673           0 :         } else if (call_result == FAILURE) {
     674           0 :                 php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_READ " is not implemented!",
     675           0 :                                 us->wrapper->classname);
     676             :         }
     677             : 
     678        1108 :         zval_ptr_dtor(&retval);
     679        1108 :         ZVAL_UNDEF(&retval);
     680             : 
     681             :         /* since the user stream has no way of setting the eof flag directly, we need to ask it if we hit eof */
     682             : 
     683        2216 :         ZVAL_STRINGL(&func_name, USERSTREAM_EOF, sizeof(USERSTREAM_EOF)-1);
     684             : 
     685        2216 :         call_result = call_user_function_ex(NULL,
     686             :                         Z_ISUNDEF(us->object)? NULL : &us->object,
     687             :                         &func_name,
     688             :                         &retval,
     689             :                         0, NULL, 0, NULL);
     690             : 
     691        2491 :         if (call_result == SUCCESS && Z_TYPE(retval) != IS_UNDEF && zval_is_true(&retval)) {
     692         275 :                 stream->eof = 1;
     693         833 :         } else if (call_result == FAILURE) {
     694           0 :                 php_error_docref(NULL, E_WARNING,
     695             :                                 "%s::" USERSTREAM_EOF " is not implemented! Assuming EOF",
     696           0 :                                 us->wrapper->classname);
     697             : 
     698           0 :                 stream->eof = 1;
     699             :         }
     700             : 
     701        1108 :         zval_ptr_dtor(&retval);
     702        1108 :         zval_ptr_dtor(&func_name);
     703             : 
     704        1108 :         return didread;
     705             : }
     706             : 
     707          58 : static int php_userstreamop_close(php_stream *stream, int close_handle)
     708             : {
     709             :         zval func_name;
     710             :         zval retval;
     711          58 :         php_userstream_data_t *us = (php_userstream_data_t *)stream->abstract;
     712             : 
     713             :         assert(us != NULL);
     714             : 
     715         116 :         ZVAL_STRINGL(&func_name, USERSTREAM_CLOSE, sizeof(USERSTREAM_CLOSE)-1);
     716             : 
     717         116 :         call_user_function_ex(NULL,
     718             :                         Z_ISUNDEF(us->object)? NULL : &us->object,
     719             :                         &func_name,
     720             :                         &retval,
     721             :                         0, NULL, 0, NULL);
     722             : 
     723          58 :         zval_ptr_dtor(&retval);
     724          58 :         zval_ptr_dtor(&func_name);
     725             : 
     726          58 :         zval_ptr_dtor(&us->object);
     727          58 :         ZVAL_UNDEF(&us->object);
     728             : 
     729          58 :         efree(us);
     730             : 
     731          58 :         return 0;
     732             : }
     733             : 
     734           8 : static int php_userstreamop_flush(php_stream *stream)
     735             : {
     736             :         zval func_name;
     737             :         zval retval;
     738             :         int call_result;
     739           8 :         php_userstream_data_t *us = (php_userstream_data_t *)stream->abstract;
     740             : 
     741             :         assert(us != NULL);
     742             : 
     743          16 :         ZVAL_STRINGL(&func_name, USERSTREAM_FLUSH, sizeof(USERSTREAM_FLUSH)-1);
     744             : 
     745          16 :         call_result = call_user_function_ex(NULL,
     746             :                         Z_ISUNDEF(us->object)? NULL : &us->object,
     747             :                         &func_name,
     748             :                         &retval,
     749             :                         0, NULL, 0, NULL);
     750             : 
     751           8 :         if (call_result == SUCCESS && Z_TYPE(retval) != IS_UNDEF && zval_is_true(&retval))
     752           0 :                 call_result = 0;
     753             :         else
     754           8 :                 call_result = -1;
     755             : 
     756           8 :         zval_ptr_dtor(&retval);
     757           8 :         zval_ptr_dtor(&func_name);
     758             : 
     759           8 :         return call_result;
     760             : }
     761             : 
     762        2098 : static int php_userstreamop_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
     763             : {
     764             :         zval func_name;
     765             :         zval retval;
     766             :         int call_result, ret;
     767        2098 :         php_userstream_data_t *us = (php_userstream_data_t *)stream->abstract;
     768             :         zval args[2];
     769             : 
     770             :         assert(us != NULL);
     771             : 
     772        4196 :         ZVAL_STRINGL(&func_name, USERSTREAM_SEEK, sizeof(USERSTREAM_SEEK)-1);
     773             : 
     774        2098 :         ZVAL_LONG(&args[0], offset);
     775        2098 :         ZVAL_LONG(&args[1], whence);
     776             : 
     777        4196 :         call_result = call_user_function_ex(NULL,
     778             :                         Z_ISUNDEF(us->object)? NULL : &us->object,
     779             :                         &func_name,
     780             :                         &retval,
     781             :                         2, args,
     782             :                         0, NULL);
     783             : 
     784        2098 :         zval_ptr_dtor(&args[0]);
     785        2098 :         zval_ptr_dtor(&args[1]);
     786        2098 :         zval_ptr_dtor(&func_name);
     787             : 
     788        2098 :         if (call_result == FAILURE) {
     789             :                 /* stream_seek is not implemented, so disable seeks for this stream */
     790           0 :                 stream->flags |= PHP_STREAM_FLAG_NO_SEEK;
     791             :                 /* there should be no retval to clean up */
     792             : 
     793           0 :                 zval_ptr_dtor(&retval);
     794             : 
     795           0 :                 return -1;
     796        6294 :         } else if (call_result == SUCCESS && Z_TYPE(retval) != IS_UNDEF && zval_is_true(&retval)) {
     797        2098 :                 ret = 0;
     798             :         } else {
     799           0 :                 ret = -1;
     800             :         }
     801             : 
     802        2098 :         zval_ptr_dtor(&retval);
     803        2098 :         ZVAL_UNDEF(&retval);
     804             : 
     805        2098 :         if (ret) {
     806           0 :                 return ret;
     807             :         }
     808             : 
     809             :         /* now determine where we are */
     810        4196 :         ZVAL_STRINGL(&func_name, USERSTREAM_TELL, sizeof(USERSTREAM_TELL)-1);
     811             : 
     812        4196 :         call_result = call_user_function_ex(NULL,
     813             :                 Z_ISUNDEF(us->object)? NULL : &us->object,
     814             :                 &func_name,
     815             :                 &retval,
     816             :                 0, NULL, 0, NULL);
     817             : 
     818        6294 :         if (call_result == SUCCESS && Z_TYPE(retval) == IS_LONG) {
     819        2098 :                 *newoffs = Z_LVAL(retval);
     820        2098 :                 ret = 0;
     821           0 :         } else if (call_result == FAILURE) {
     822           0 :                 php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_TELL " is not implemented!", us->wrapper->classname);
     823           0 :                 ret = -1;
     824             :         } else {
     825           0 :                 ret = -1;
     826             :         }
     827             : 
     828        2098 :         zval_ptr_dtor(&retval);
     829        2098 :         zval_ptr_dtor(&func_name);
     830        2098 :         return ret;
     831             : }
     832             : 
     833             : /* parse the return value from one of the stat functions and store the
     834             :  * relevant fields into the statbuf provided */
     835          39 : static int statbuf_from_array(zval *array, php_stream_statbuf *ssb)
     836             : {
     837             :         zval *elem;
     838             : 
     839             : #define STAT_PROP_ENTRY_EX(name, name2)                        \
     840             :         if (NULL != (elem = zend_hash_str_find(Z_ARRVAL_P(array), #name, sizeof(#name)-1))) {     \
     841             :                 ssb->sb.st_##name2 = zval_get_long(elem);                                                      \
     842             :         }
     843             : 
     844             : #define STAT_PROP_ENTRY(name) STAT_PROP_ENTRY_EX(name,name)
     845             : 
     846          39 :         memset(ssb, 0, sizeof(php_stream_statbuf));
     847          40 :         STAT_PROP_ENTRY(dev);
     848          40 :         STAT_PROP_ENTRY(ino);
     849          40 :         STAT_PROP_ENTRY(mode);
     850          40 :         STAT_PROP_ENTRY(nlink);
     851          40 :         STAT_PROP_ENTRY(uid);
     852          40 :         STAT_PROP_ENTRY(gid);
     853             : #if HAVE_ST_RDEV
     854          40 :         STAT_PROP_ENTRY(rdev);
     855             : #endif
     856          45 :         STAT_PROP_ENTRY(size);
     857             : #ifdef NETWARE
     858             :         STAT_PROP_ENTRY_EX(atime, atime.tv_sec);
     859             :         STAT_PROP_ENTRY_EX(mtime, mtime.tv_sec);
     860             :         STAT_PROP_ENTRY_EX(ctime, ctime.tv_sec);
     861             : #else
     862          41 :         STAT_PROP_ENTRY(atime);
     863          40 :         STAT_PROP_ENTRY(mtime);
     864          40 :         STAT_PROP_ENTRY(ctime);
     865             : #endif
     866             : #ifdef HAVE_ST_BLKSIZE
     867          40 :         STAT_PROP_ENTRY(blksize);
     868             : #endif
     869             : #ifdef HAVE_ST_BLOCKS
     870          40 :         STAT_PROP_ENTRY(blocks);
     871             : #endif
     872             : 
     873             : #undef STAT_PROP_ENTRY
     874             : #undef STAT_PROP_ENTRY_EX
     875          39 :         return SUCCESS;
     876             : }
     877             : 
     878          32 : static int php_userstreamop_stat(php_stream *stream, php_stream_statbuf *ssb)
     879             : {
     880             :         zval func_name;
     881             :         zval retval;
     882             :         int call_result;
     883          32 :         php_userstream_data_t *us = (php_userstream_data_t *)stream->abstract;
     884          32 :         int ret = -1;
     885             : 
     886          64 :         ZVAL_STRINGL(&func_name, USERSTREAM_STAT, sizeof(USERSTREAM_STAT)-1);
     887             : 
     888          64 :         call_result = call_user_function_ex(NULL,
     889             :                         Z_ISUNDEF(us->object)? NULL : &us->object,
     890             :                         &func_name,
     891             :                         &retval,
     892             :                         0, NULL, 0, NULL);
     893             : 
     894          84 :         if (call_result == SUCCESS && Z_TYPE(retval) == IS_ARRAY) {
     895          26 :                 if (SUCCESS == statbuf_from_array(&retval, ssb))
     896          26 :                         ret = 0;
     897             :         } else {
     898           6 :                 if (call_result == FAILURE) {
     899           6 :                         php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_STAT " is not implemented!",
     900           6 :                                         us->wrapper->classname);
     901             :                 }
     902             :         }
     903             : 
     904          32 :         zval_ptr_dtor(&retval);
     905          32 :         zval_ptr_dtor(&func_name);
     906             : 
     907          32 :         return ret;
     908             : }
     909             : 
     910             : 
     911          98 : static int php_userstreamop_set_option(php_stream *stream, int option, int value, void *ptrparam) {
     912             :         zval func_name;
     913             :         zval retval;
     914             :         int call_result;
     915          98 :         php_userstream_data_t *us = (php_userstream_data_t *)stream->abstract;
     916          98 :         int ret = PHP_STREAM_OPTION_RETURN_NOTIMPL;
     917             :         zval args[3];
     918             : 
     919          98 :         switch (option) {
     920             :         case PHP_STREAM_OPTION_CHECK_LIVENESS:
     921          84 :                 ZVAL_STRINGL(&func_name, USERSTREAM_EOF, sizeof(USERSTREAM_EOF)-1);
     922          84 :                 call_result = call_user_function_ex(NULL, Z_ISUNDEF(us->object)? NULL : &us->object, &func_name, &retval, 0, NULL, 0, NULL);
     923         126 :                 if (call_result == SUCCESS && (Z_TYPE(retval) == IS_FALSE || Z_TYPE(retval) == IS_TRUE)) {
     924          41 :                         ret = zval_is_true(&retval) ? PHP_STREAM_OPTION_RETURN_ERR : PHP_STREAM_OPTION_RETURN_OK;
     925             :                 } else {
     926           1 :                         ret = PHP_STREAM_OPTION_RETURN_ERR;
     927           1 :                         php_error_docref(NULL, E_WARNING,
     928             :                                         "%s::" USERSTREAM_EOF " is not implemented! Assuming EOF",
     929           1 :                                         us->wrapper->classname);
     930             :                 }
     931          42 :                 zval_ptr_dtor(&retval);
     932          42 :                 zval_ptr_dtor(&func_name);
     933          42 :                 break;
     934             : 
     935             :         case PHP_STREAM_OPTION_LOCKING:
     936           7 :                 ZVAL_LONG(&args[0], 0);
     937             : 
     938           7 :                 if (value & LOCK_NB) {
     939           3 :                         Z_LVAL_P(&args[0]) |= PHP_LOCK_NB;
     940             :                 }
     941           7 :                 switch(value & ~LOCK_NB) {
     942             :                 case LOCK_SH:
     943           2 :                         Z_LVAL_P(&args[0]) |= PHP_LOCK_SH;
     944           2 :                         break;
     945             :                 case LOCK_EX:
     946           3 :                         Z_LVAL_P(&args[0]) |= PHP_LOCK_EX;
     947           3 :                         break;
     948             :                 case LOCK_UN:
     949           2 :                         Z_LVAL_P(&args[0]) |= PHP_LOCK_UN;
     950             :                         break;
     951             :                 }
     952             : 
     953             :                 /* TODO wouldblock */
     954          14 :                 ZVAL_STRINGL(&func_name, USERSTREAM_LOCK, sizeof(USERSTREAM_LOCK)-1);
     955             : 
     956          14 :                 call_result = call_user_function_ex(NULL,
     957             :                                                 Z_ISUNDEF(us->object)? NULL : &us->object,
     958             :                                                 &func_name,
     959             :                                                 &retval,
     960             :                                                 1, args, 0, NULL);
     961             : 
     962          19 :                 if (call_result == SUCCESS && (Z_TYPE(retval) == IS_FALSE || Z_TYPE(retval) == IS_TRUE)) {
     963           0 :                         ret = (Z_TYPE(retval) == IS_FALSE);
     964           7 :                 } else if (call_result == FAILURE) {
     965           1 :                         if (value == 0) {
     966             :                                 /* lock support test (TODO: more check) */
     967           0 :                                 ret = PHP_STREAM_OPTION_RETURN_OK;
     968             :                         } else {
     969           1 :                                 php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_LOCK " is not implemented!",
     970           1 :                                                                  us->wrapper->classname);
     971           1 :                                 ret = PHP_STREAM_OPTION_RETURN_ERR;
     972             :                         }
     973             :                 }
     974             : 
     975           7 :                 zval_ptr_dtor(&retval);
     976           7 :                 zval_ptr_dtor(&func_name);
     977           7 :                 zval_ptr_dtor(&args[0]);
     978           7 :                 break;
     979             : 
     980             :         case PHP_STREAM_OPTION_TRUNCATE_API:
     981          20 :                 ZVAL_STRINGL(&func_name, USERSTREAM_TRUNCATE, sizeof(USERSTREAM_TRUNCATE)-1);
     982             : 
     983          10 :                 switch (value) {
     984             :                 case PHP_STREAM_TRUNCATE_SUPPORTED:
     985           6 :                         if (zend_is_callable_ex(&func_name,
     986           6 :                                         Z_ISUNDEF(us->object)? NULL : Z_OBJ(us->object),
     987             :                                         IS_CALLABLE_CHECK_SILENT, NULL, NULL, NULL))
     988           4 :                                 ret = PHP_STREAM_OPTION_RETURN_OK;
     989             :                         else
     990           2 :                                 ret = PHP_STREAM_OPTION_RETURN_ERR;
     991           6 :                         break;
     992             : 
     993             :                 case PHP_STREAM_TRUNCATE_SET_SIZE: {
     994           4 :                         ptrdiff_t new_size = *(ptrdiff_t*) ptrparam;
     995           4 :                         if (new_size >= 0 && new_size <= (ptrdiff_t)LONG_MAX) {
     996           3 :                                 ZVAL_LONG(&args[0], (zend_long)new_size);
     997           6 :                                 call_result = call_user_function_ex(NULL,
     998             :                                                                 Z_ISUNDEF(us->object)? NULL : &us->object,
     999             :                                                                 &func_name,
    1000             :                                                                 &retval,
    1001             :                                                                 1, args, 0, NULL);
    1002           9 :                                 if (call_result == SUCCESS && Z_TYPE(retval) != IS_UNDEF) {
    1003           8 :                                         if (Z_TYPE(retval) == IS_FALSE || Z_TYPE(retval) == IS_TRUE) {
    1004           2 :                                                 ret = (Z_TYPE(retval) == IS_TRUE) ? PHP_STREAM_OPTION_RETURN_OK :
    1005             :                                                                                            PHP_STREAM_OPTION_RETURN_ERR;
    1006             :                                         } else {
    1007           1 :                                                 php_error_docref(NULL, E_WARNING,
    1008             :                                                                 "%s::" USERSTREAM_TRUNCATE " did not return a boolean!",
    1009           1 :                                                                 us->wrapper->classname);
    1010             :                                         }
    1011             :                                 } else {
    1012           0 :                                         php_error_docref(NULL, E_WARNING,
    1013             :                                                         "%s::" USERSTREAM_TRUNCATE " is not implemented!",
    1014           0 :                                                         us->wrapper->classname);
    1015             :                                 }
    1016           3 :                                 zval_ptr_dtor(&retval);
    1017           3 :                                 zval_ptr_dtor(&args[0]);
    1018             :                         } else { /* bad new size */
    1019           1 :                                 ret = PHP_STREAM_OPTION_RETURN_ERR;
    1020             :                         }
    1021             :                         break;
    1022             :                 }
    1023             :                 }
    1024          10 :                 zval_ptr_dtor(&func_name);
    1025          10 :                 break;
    1026             : 
    1027             :         case PHP_STREAM_OPTION_READ_BUFFER:
    1028             :         case PHP_STREAM_OPTION_WRITE_BUFFER:
    1029             :         case PHP_STREAM_OPTION_READ_TIMEOUT:
    1030             :         case PHP_STREAM_OPTION_BLOCKING: {
    1031             : 
    1032          22 :                 ZVAL_STRINGL(&func_name, USERSTREAM_SET_OPTION, sizeof(USERSTREAM_SET_OPTION)-1);
    1033             : 
    1034          11 :                 ZVAL_LONG(&args[0], option);
    1035          11 :                 ZVAL_NULL(&args[1]);
    1036          11 :                 ZVAL_NULL(&args[2]);
    1037             : 
    1038          11 :                 switch(option) {
    1039             :                 case PHP_STREAM_OPTION_READ_BUFFER:
    1040             :                 case PHP_STREAM_OPTION_WRITE_BUFFER:
    1041           5 :                         ZVAL_LONG(&args[1], value);
    1042           5 :                         if (ptrparam) {
    1043           4 :                                 ZVAL_LONG(&args[2], *(long *)ptrparam);
    1044             :                         } else {
    1045           1 :                                 ZVAL_LONG(&args[2], BUFSIZ);
    1046             :                         }
    1047           5 :                         break;
    1048             :                 case PHP_STREAM_OPTION_READ_TIMEOUT: {
    1049           2 :                         struct timeval tv = *(struct timeval*)ptrparam;
    1050           2 :                         ZVAL_LONG(&args[1], tv.tv_sec);
    1051           2 :                         ZVAL_LONG(&args[2], tv.tv_usec);
    1052           2 :                         break;
    1053             :                         }
    1054             :                 case PHP_STREAM_OPTION_BLOCKING:
    1055           4 :                         ZVAL_LONG(&args[1], value);
    1056             :                         break;
    1057             :                 default:
    1058             :                         break;
    1059             :                 }
    1060             : 
    1061          22 :                 call_result = call_user_function_ex(NULL,
    1062             :                         Z_ISUNDEF(us->object)? NULL : &us->object,
    1063             :                         &func_name,
    1064             :                         &retval,
    1065             :                         3, args, 0, NULL);
    1066             : 
    1067          11 :                 if (call_result == FAILURE) {
    1068           1 :                         php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_SET_OPTION " is not implemented!",
    1069           1 :                                         us->wrapper->classname);
    1070           1 :                         ret = PHP_STREAM_OPTION_RETURN_ERR;
    1071          15 :                 } else if (Z_TYPE(retval) != IS_UNDEF && zend_is_true(&retval)) {
    1072           5 :                         ret = PHP_STREAM_OPTION_RETURN_OK;
    1073             :                 } else {
    1074           5 :                         ret = PHP_STREAM_OPTION_RETURN_ERR;
    1075             :                 }
    1076             : 
    1077          11 :                 zval_ptr_dtor(&retval);
    1078          11 :                 zval_ptr_dtor(&args[2]);
    1079          11 :                 zval_ptr_dtor(&args[1]);
    1080          11 :                 zval_ptr_dtor(&args[0]);
    1081          11 :                 zval_ptr_dtor(&func_name);
    1082             : 
    1083             :                 break;
    1084             :                 }
    1085             :         }
    1086             : 
    1087          98 :         return ret;
    1088             : }
    1089             : 
    1090             : 
    1091           2 : static int user_wrapper_unlink(php_stream_wrapper *wrapper, const char *url, int options, php_stream_context *context)
    1092             : {
    1093           2 :         struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract;
    1094             :         zval zfuncname, zretval;
    1095             :         zval args[1];
    1096             :         int call_result;
    1097             :         zval object;
    1098           2 :         int ret = 0;
    1099             : 
    1100             :         /* create an instance of our class */
    1101           2 :         user_stream_create_object(uwrap, context, &object);
    1102           2 :         if (Z_TYPE(object) == IS_UNDEF) {
    1103           0 :                 return ret;
    1104             :         }
    1105             : 
    1106             :         /* call the unlink method */
    1107           4 :         ZVAL_STRING(&args[0], url);
    1108             : 
    1109           4 :         ZVAL_STRING(&zfuncname, USERSTREAM_UNLINK);
    1110             : 
    1111           2 :         call_result = call_user_function_ex(NULL,
    1112             :                         &object,
    1113             :                         &zfuncname,
    1114             :                         &zretval,
    1115             :                         1, args,
    1116             :                         0, NULL );
    1117             : 
    1118           6 :         if (call_result == SUCCESS && (Z_TYPE(zretval) == IS_FALSE || Z_TYPE(zretval) == IS_TRUE)) {
    1119           0 :                 ret = (Z_TYPE(zretval) == IS_TRUE);
    1120           2 :         } else if (call_result == FAILURE) {
    1121           0 :                 php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_UNLINK " is not implemented!", uwrap->classname);
    1122             :         }
    1123             : 
    1124             :         /* clean up */
    1125           2 :         zval_ptr_dtor(&object);
    1126           2 :         zval_ptr_dtor(&zretval);
    1127           2 :         zval_ptr_dtor(&zfuncname);
    1128             : 
    1129           2 :         zval_ptr_dtor(&args[0]);
    1130             : 
    1131           2 :         return ret;
    1132             : }
    1133             : 
    1134           2 : static int user_wrapper_rename(php_stream_wrapper *wrapper, const char *url_from, const char *url_to,
    1135             :                                                            int options, php_stream_context *context)
    1136             : {
    1137           2 :         struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract;
    1138             :         zval zfuncname, zretval;
    1139             :         zval args[2];
    1140             :         int call_result;
    1141             :         zval object;
    1142           2 :         int ret = 0;
    1143             : 
    1144             :         /* create an instance of our class */
    1145           2 :         user_stream_create_object(uwrap, context, &object);
    1146           2 :         if (Z_TYPE(object) == IS_UNDEF) {
    1147           0 :                 return ret;
    1148             :         }
    1149             : 
    1150             :         /* call the rename method */
    1151           4 :         ZVAL_STRING(&args[0], url_from);
    1152           4 :         ZVAL_STRING(&args[1], url_to);
    1153             : 
    1154           4 :         ZVAL_STRING(&zfuncname, USERSTREAM_RENAME);
    1155             : 
    1156           2 :         call_result = call_user_function_ex(NULL,
    1157             :                         &object,
    1158             :                         &zfuncname,
    1159             :                         &zretval,
    1160             :                         2, args,
    1161             :                         0, NULL );
    1162             : 
    1163           6 :         if (call_result == SUCCESS && (Z_TYPE(zretval) == IS_FALSE || Z_TYPE(zretval) == IS_TRUE)) {
    1164           0 :                 ret = (Z_TYPE(zretval) == IS_TRUE);
    1165           2 :         } else if (call_result == FAILURE) {
    1166           0 :                 php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_RENAME " is not implemented!", uwrap->classname);
    1167             :         }
    1168             : 
    1169             :         /* clean up */
    1170           2 :         zval_ptr_dtor(&object);
    1171           2 :         zval_ptr_dtor(&zretval);
    1172             : 
    1173           2 :         zval_ptr_dtor(&zfuncname);
    1174           2 :         zval_ptr_dtor(&args[1]);
    1175           2 :         zval_ptr_dtor(&args[0]);
    1176             : 
    1177           2 :         return ret;
    1178             : }
    1179             : 
    1180           2 : static int user_wrapper_mkdir(php_stream_wrapper *wrapper, const char *url, int mode,
    1181             :                                                           int options, php_stream_context *context)
    1182             : {
    1183           2 :         struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract;
    1184             :         zval zfuncname, zretval;
    1185             :         zval args[3];
    1186             :         int call_result;
    1187             :         zval object;
    1188           2 :         int ret = 0;
    1189             : 
    1190             :         /* create an instance of our class */
    1191           2 :         user_stream_create_object(uwrap, context, &object);
    1192           2 :         if (Z_TYPE(object) == IS_UNDEF) {
    1193           0 :                 return ret;
    1194             :         }
    1195             : 
    1196             :         /* call the mkdir method */
    1197           4 :         ZVAL_STRING(&args[0], url);
    1198           2 :         ZVAL_LONG(&args[1], mode);
    1199           2 :         ZVAL_LONG(&args[2], options);
    1200             : 
    1201           4 :         ZVAL_STRING(&zfuncname, USERSTREAM_MKDIR);
    1202             : 
    1203           2 :         call_result = call_user_function_ex(NULL,
    1204             :                         &object,
    1205             :                         &zfuncname,
    1206             :                         &zretval,
    1207             :                         3, args,
    1208             :                         0, NULL );
    1209             : 
    1210           6 :         if (call_result == SUCCESS && (Z_TYPE(zretval) == IS_FALSE || Z_TYPE(zretval) == IS_TRUE)) {
    1211           0 :                 ret = (Z_TYPE(zretval) == IS_TRUE);
    1212           2 :         } else if (call_result == FAILURE) {
    1213           0 :                 php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_MKDIR " is not implemented!", uwrap->classname);
    1214             :         }
    1215             : 
    1216             :         /* clean up */
    1217           2 :         zval_ptr_dtor(&object);
    1218           2 :         zval_ptr_dtor(&zretval);
    1219             : 
    1220           2 :         zval_ptr_dtor(&zfuncname);
    1221           2 :         zval_ptr_dtor(&args[2]);
    1222           2 :         zval_ptr_dtor(&args[1]);
    1223           2 :         zval_ptr_dtor(&args[0]);
    1224             : 
    1225           2 :         return ret;
    1226             : }
    1227             : 
    1228           2 : static int user_wrapper_rmdir(php_stream_wrapper *wrapper, const char *url,
    1229             :                                                           int options, php_stream_context *context)
    1230             : {
    1231           2 :         struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract;
    1232             :         zval zfuncname, zretval;
    1233             :         zval args[2];
    1234             :         int call_result;
    1235             :         zval object;
    1236           2 :         int ret = 0;
    1237             : 
    1238             :         /* create an instance of our class */
    1239           2 :         user_stream_create_object(uwrap, context, &object);
    1240           2 :         if (Z_TYPE(object) == IS_UNDEF) {
    1241           0 :                 return ret;
    1242             :         }
    1243             : 
    1244             :         /* call the rmdir method */
    1245           4 :         ZVAL_STRING(&args[0], url);
    1246           2 :         ZVAL_LONG(&args[1], options);
    1247             : 
    1248           4 :         ZVAL_STRING(&zfuncname, USERSTREAM_RMDIR);
    1249             : 
    1250           2 :         call_result = call_user_function_ex(NULL,
    1251             :                         &object,
    1252             :                         &zfuncname,
    1253             :                         &zretval,
    1254             :                         2, args,
    1255             :                         0, NULL );
    1256             : 
    1257           6 :         if (call_result == SUCCESS && (Z_TYPE(zretval) == IS_FALSE || Z_TYPE(zretval) == IS_TRUE)) {
    1258           0 :                 ret = (Z_TYPE(zretval) == IS_TRUE);
    1259           2 :         } else if (call_result == FAILURE) {
    1260           0 :                 php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_RMDIR " is not implemented!", uwrap->classname);
    1261             :         }
    1262             : 
    1263             :         /* clean up */
    1264           2 :         zval_ptr_dtor(&object);
    1265           2 :         zval_ptr_dtor(&zretval);
    1266             : 
    1267           2 :         zval_ptr_dtor(&zfuncname);
    1268           2 :         zval_ptr_dtor(&args[1]);
    1269           2 :         zval_ptr_dtor(&args[0]);
    1270             : 
    1271           2 :         return ret;
    1272             : }
    1273             : 
    1274           9 : static int user_wrapper_metadata(php_stream_wrapper *wrapper, const char *url, int option,
    1275             :                                                                  void *value, php_stream_context *context)
    1276             : {
    1277           9 :         struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract;
    1278             :         zval zfuncname, zretval;
    1279             :         zval args[3];
    1280             :         int call_result;
    1281             :         zval object;
    1282           9 :         int ret = 0;
    1283             : 
    1284           9 :         switch(option) {
    1285             :                 case PHP_STREAM_META_TOUCH:
    1286           4 :                         array_init(&args[2]);
    1287           4 :                         if(value) {
    1288           3 :                                 struct utimbuf *newtime = (struct utimbuf *)value;
    1289           3 :                                 add_index_long(&args[2], 0, newtime->modtime);
    1290           3 :                                 add_index_long(&args[2], 1, newtime->actime);
    1291             :                         }
    1292           4 :                         break;
    1293             :                 case PHP_STREAM_META_GROUP:
    1294             :                 case PHP_STREAM_META_OWNER:
    1295             :                 case PHP_STREAM_META_ACCESS:
    1296           3 :                         ZVAL_LONG(&args[2], *(long *)value);
    1297           3 :                         break;
    1298             :                 case PHP_STREAM_META_GROUP_NAME:
    1299             :                 case PHP_STREAM_META_OWNER_NAME:
    1300           4 :                         ZVAL_STRING(&args[2], value);
    1301           2 :                         break;
    1302             :                 default:
    1303           0 :                         php_error_docref(NULL, E_WARNING, "Unknown option %d for " USERSTREAM_METADATA, option);
    1304           0 :                         zval_ptr_dtor(&args[2]);
    1305           0 :                         return ret;
    1306             :         }
    1307             : 
    1308             :         /* create an instance of our class */
    1309           9 :         user_stream_create_object(uwrap, context, &object);
    1310           9 :         if (Z_TYPE(object) == IS_UNDEF) {
    1311           0 :                 zval_ptr_dtor(&args[2]);
    1312           0 :                 return ret;
    1313             :         }
    1314             : 
    1315             :         /* call the mkdir method */
    1316          18 :         ZVAL_STRING(&args[0], url);
    1317           9 :         ZVAL_LONG(&args[1], option);
    1318             : 
    1319          18 :         ZVAL_STRING(&zfuncname, USERSTREAM_METADATA);
    1320             : 
    1321           9 :         call_result = call_user_function_ex(NULL,
    1322             :                         &object,
    1323             :                         &zfuncname,
    1324             :                         &zretval,
    1325             :                         3, args,
    1326             :                         0, NULL );
    1327             : 
    1328          28 :         if (call_result == SUCCESS && (Z_TYPE(zretval) == IS_FALSE || Z_TYPE(zretval) == IS_TRUE)) {
    1329           9 :                 ret = Z_TYPE(zretval) == IS_TRUE;
    1330           0 :         } else if (call_result == FAILURE) {
    1331           0 :                 php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_METADATA " is not implemented!", uwrap->classname);
    1332             :         }
    1333             : 
    1334             :         /* clean up */
    1335           9 :         zval_ptr_dtor(&object);
    1336           9 :         zval_ptr_dtor(&zretval);
    1337             : 
    1338           9 :         zval_ptr_dtor(&zfuncname);
    1339           9 :         zval_ptr_dtor(&args[0]);
    1340           9 :         zval_ptr_dtor(&args[1]);
    1341           9 :         zval_ptr_dtor(&args[2]);
    1342             : 
    1343           9 :         return ret;
    1344             : }
    1345             : 
    1346             : 
    1347          15 : static int user_wrapper_stat_url(php_stream_wrapper *wrapper, const char *url, int flags,
    1348             :                                                                  php_stream_statbuf *ssb, php_stream_context *context)
    1349             : {
    1350          15 :         struct php_user_stream_wrapper *uwrap = (struct php_user_stream_wrapper*)wrapper->abstract;
    1351             :         zval zfuncname, zretval;
    1352             :         zval args[2];
    1353             :         int call_result;
    1354             :         zval object;
    1355          15 :         int ret = -1;
    1356             : 
    1357             :         /* create an instance of our class */
    1358          15 :         user_stream_create_object(uwrap, context, &object);
    1359          15 :         if (Z_TYPE(object) == IS_UNDEF) {
    1360           0 :                 return ret;
    1361             :         }
    1362             : 
    1363             :         /* call it's stat_url method - set up params first */
    1364          30 :         ZVAL_STRING(&args[0], url);
    1365          15 :         ZVAL_LONG(&args[1], flags);
    1366             : 
    1367          30 :         ZVAL_STRING(&zfuncname, USERSTREAM_STATURL);
    1368             : 
    1369          15 :         call_result = call_user_function_ex(NULL,
    1370             :                         &object,
    1371             :                         &zfuncname,
    1372             :                         &zretval,
    1373             :                         2, args,
    1374             :                         0, NULL );
    1375             : 
    1376          43 :         if (call_result == SUCCESS && Z_TYPE(zretval) == IS_ARRAY) {
    1377             :                 /* We got the info we needed */
    1378          13 :                 if (SUCCESS == statbuf_from_array(&zretval, ssb))
    1379          13 :                         ret = 0;
    1380             :         } else {
    1381           2 :                 if (call_result == FAILURE) {
    1382           0 :                         php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_STATURL " is not implemented!",
    1383             :                                         uwrap->classname);
    1384             :                 }
    1385             :         }
    1386             : 
    1387             :         /* clean up */
    1388          15 :         zval_ptr_dtor(&object);
    1389          15 :         zval_ptr_dtor(&zretval);
    1390             : 
    1391          15 :         zval_ptr_dtor(&zfuncname);
    1392          15 :         zval_ptr_dtor(&args[1]);
    1393          15 :         zval_ptr_dtor(&args[0]);
    1394             : 
    1395          15 :         return ret;
    1396             : 
    1397             : }
    1398             : 
    1399           5 : static size_t php_userstreamop_readdir(php_stream *stream, char *buf, size_t count)
    1400             : {
    1401             :         zval func_name;
    1402             :         zval retval;
    1403             :         int call_result;
    1404           5 :         size_t didread = 0;
    1405           5 :         php_userstream_data_t *us = (php_userstream_data_t *)stream->abstract;
    1406           5 :         php_stream_dirent *ent = (php_stream_dirent*)buf;
    1407             : 
    1408             :         /* avoid problems if someone mis-uses the stream */
    1409           5 :         if (count != sizeof(php_stream_dirent))
    1410           0 :                 return 0;
    1411             : 
    1412          10 :         ZVAL_STRINGL(&func_name, USERSTREAM_DIR_READ, sizeof(USERSTREAM_DIR_READ)-1);
    1413             : 
    1414          10 :         call_result = call_user_function_ex(NULL,
    1415             :                         Z_ISUNDEF(us->object)? NULL : &us->object,
    1416             :                         &func_name,
    1417             :                         &retval,
    1418             :                         0, NULL,
    1419             :                         0, NULL);
    1420             : 
    1421          18 :         if (call_result == SUCCESS && Z_TYPE(retval) != IS_FALSE && Z_TYPE(retval) != IS_TRUE) {
    1422           4 :                 convert_to_string(&retval);
    1423           4 :                 PHP_STRLCPY(ent->d_name, Z_STRVAL(retval), sizeof(ent->d_name), Z_STRLEN(retval));
    1424             : 
    1425           4 :                 didread = sizeof(php_stream_dirent);
    1426           1 :         } else if (call_result == FAILURE) {
    1427           0 :                 php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_DIR_READ " is not implemented!",
    1428           0 :                                 us->wrapper->classname);
    1429             :         }
    1430             : 
    1431           5 :         zval_ptr_dtor(&retval);
    1432           5 :         zval_ptr_dtor(&func_name);
    1433             : 
    1434           5 :         return didread;
    1435             : }
    1436             : 
    1437           2 : static int php_userstreamop_closedir(php_stream *stream, int close_handle)
    1438             : {
    1439             :         zval func_name;
    1440             :         zval retval;
    1441           2 :         php_userstream_data_t *us = (php_userstream_data_t *)stream->abstract;
    1442             : 
    1443             :         assert(us != NULL);
    1444             : 
    1445           4 :         ZVAL_STRINGL(&func_name, USERSTREAM_DIR_CLOSE, sizeof(USERSTREAM_DIR_CLOSE)-1);
    1446             : 
    1447           4 :         call_user_function_ex(NULL,
    1448             :                         Z_ISUNDEF(us->object)? NULL : &us->object,
    1449             :                         &func_name,
    1450             :                         &retval,
    1451             :                         0, NULL, 0, NULL);
    1452             : 
    1453           2 :         zval_ptr_dtor(&retval);
    1454           2 :         zval_ptr_dtor(&func_name);
    1455           2 :         zval_ptr_dtor(&us->object);
    1456           2 :         ZVAL_UNDEF(&us->object);
    1457             : 
    1458           2 :         efree(us);
    1459             : 
    1460           2 :         return 0;
    1461             : }
    1462             : 
    1463           0 : static int php_userstreamop_rewinddir(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
    1464             : {
    1465             :         zval func_name;
    1466             :         zval retval;
    1467           0 :         php_userstream_data_t *us = (php_userstream_data_t *)stream->abstract;
    1468             : 
    1469           0 :         ZVAL_STRINGL(&func_name, USERSTREAM_DIR_REWIND, sizeof(USERSTREAM_DIR_REWIND)-1);
    1470             : 
    1471           0 :         call_user_function_ex(NULL,
    1472             :                         Z_ISUNDEF(us->object)? NULL : &us->object,
    1473             :                         &func_name,
    1474             :                         &retval,
    1475             :                         0, NULL, 0, NULL);
    1476             : 
    1477           0 :         zval_ptr_dtor(&retval);
    1478           0 :         zval_ptr_dtor(&func_name);
    1479             : 
    1480           0 :         return 0;
    1481             : 
    1482             : }
    1483             : 
    1484           8 : static int php_userstreamop_cast(php_stream *stream, int castas, void **retptr)
    1485             : {
    1486           8 :         php_userstream_data_t *us = (php_userstream_data_t *)stream->abstract;
    1487             :         zval func_name;
    1488             :         zval retval;
    1489             :         zval args[1];
    1490           8 :         php_stream * intstream = NULL;
    1491             :         int call_result;
    1492           8 :         int ret = FAILURE;
    1493             : 
    1494          16 :         ZVAL_STRINGL(&func_name, USERSTREAM_CAST, sizeof(USERSTREAM_CAST)-1);
    1495             : 
    1496           8 :         switch(castas) {
    1497             :         case PHP_STREAM_AS_FD_FOR_SELECT:
    1498           8 :                 ZVAL_LONG(&args[0], PHP_STREAM_AS_FD_FOR_SELECT);
    1499           8 :                 break;
    1500             :         default:
    1501           0 :                 ZVAL_LONG(&args[0], PHP_STREAM_AS_STDIO);
    1502             :                 break;
    1503             :         }
    1504             : 
    1505          16 :         call_result = call_user_function_ex(NULL,
    1506             :                         Z_ISUNDEF(us->object)? NULL : &us->object,
    1507             :                         &func_name,
    1508             :                         &retval,
    1509             :                         1, args, 0, NULL);
    1510             : 
    1511             :         do {
    1512           8 :                 if (call_result == FAILURE) {
    1513           2 :                         php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_CAST " is not implemented!",
    1514           2 :                                         us->wrapper->classname);
    1515           2 :                         break;
    1516             :                 }
    1517           6 :                 if (Z_ISUNDEF(retval) || !zend_is_true(&retval)) {
    1518             :                         break;
    1519             :                 }
    1520           5 :                 php_stream_from_zval_no_verify(intstream, &retval);
    1521           5 :                 if (!intstream) {
    1522           1 :                         php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_CAST " must return a stream resource",
    1523           1 :                                         us->wrapper->classname);
    1524           1 :                         break;
    1525             :                 }
    1526           4 :                 if (intstream == stream) {
    1527           1 :                         php_error_docref(NULL, E_WARNING, "%s::" USERSTREAM_CAST " must not return itself",
    1528           1 :                                         us->wrapper->classname);
    1529           1 :                         intstream = NULL;
    1530           1 :                         break;
    1531             :                 }
    1532           3 :                 ret = php_stream_cast(intstream, castas, retptr, 1);
    1533             :         } while (0);
    1534             : 
    1535           8 :         zval_ptr_dtor(&retval);
    1536           8 :         zval_ptr_dtor(&func_name);
    1537           8 :         zval_ptr_dtor(&args[0]);
    1538             : 
    1539           8 :         return ret;
    1540             : }
    1541             : 
    1542             : php_stream_ops php_stream_userspace_ops = {
    1543             :         php_userstreamop_write, php_userstreamop_read,
    1544             :         php_userstreamop_close, php_userstreamop_flush,
    1545             :         "user-space",
    1546             :         php_userstreamop_seek,
    1547             :         php_userstreamop_cast,
    1548             :         php_userstreamop_stat,
    1549             :         php_userstreamop_set_option,
    1550             : };
    1551             : 
    1552             : php_stream_ops php_stream_userspace_dir_ops = {
    1553             :         NULL, /* write */
    1554             :         php_userstreamop_readdir,
    1555             :         php_userstreamop_closedir,
    1556             :         NULL, /* flush */
    1557             :         "user-space-dir",
    1558             :         php_userstreamop_rewinddir,
    1559             :         NULL, /* cast */
    1560             :         NULL, /* stat */
    1561             :         NULL  /* set_option */
    1562             : };
    1563             : 
    1564             : 

Generated by: LCOV version 1.10

Generated at Wed, 24 Aug 2016 12:20:36 +0000 (3 days ago)

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