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

Generated by: LCOV version 1.10

Generated at Sat, 30 May 2015 05:53:03 +0000 (12 hours ago)

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