PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/standard - streamsfuncs.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 589 696 84.6 %
Date: 2016-09-27 Functions: 42 43 97.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 7                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 1997-2016 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.01 of the PHP license,      |
       8             :   | that is bundled with this package in the file LICENSE, and is        |
       9             :   | available through the world-wide-web at the following url:           |
      10             :   | http://www.php.net/license/3_01.txt                                  |
      11             :   | If you did not receive a copy of the PHP license and are unable to   |
      12             :   | obtain it through the world-wide-web, please send a note to          |
      13             :   | license@php.net so we can mail you a copy immediately.               |
      14             :   +----------------------------------------------------------------------+
      15             :   | Authors: Wez Furlong <wez@thebrainroom.com>                          |
      16             :   |          Sara Golemon <pollita@php.net>                              |
      17             :   +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #include "php.h"
      23             : #include "php_globals.h"
      24             : #include "ext/standard/flock_compat.h"
      25             : #include "ext/standard/file.h"
      26             : #include "ext/standard/php_filestat.h"
      27             : #include "php_open_temporary_file.h"
      28             : #include "ext/standard/basic_functions.h"
      29             : #include "php_ini.h"
      30             : #include "streamsfuncs.h"
      31             : #include "php_network.h"
      32             : #include "php_string.h"
      33             : 
      34             : #ifndef PHP_WIN32
      35             : #define php_select(m, r, w, e, t)       select(m, r, w, e, t)
      36             : typedef unsigned long long php_timeout_ull;
      37             : #else
      38             : #include "win32/select.h"
      39             : #include "win32/sockets.h"
      40             : typedef unsigned __int64 php_timeout_ull;
      41             : #endif
      42             : 
      43             : #define GET_CTX_OPT(stream, wrapper, name, val) (PHP_STREAM_CONTEXT(stream) && NULL != (val = php_stream_context_get_option(PHP_STREAM_CONTEXT(stream), wrapper, name)))
      44             : 
      45             : static php_stream_context *decode_context_param(zval *contextresource);
      46             : 
      47             : /* Streams based network functions */
      48             : 
      49             : #if HAVE_SOCKETPAIR
      50             : /* {{{ proto array stream_socket_pair(int domain, int type, int protocol)
      51             :    Creates a pair of connected, indistinguishable socket streams */
      52           8 : PHP_FUNCTION(stream_socket_pair)
      53             : {
      54             :         zend_long domain, type, protocol;
      55             :         php_stream *s1, *s2;
      56             :         php_socket_t pair[2];
      57             : 
      58           8 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "lll",
      59             :                         &domain, &type, &protocol)) {
      60           0 :                 RETURN_FALSE;
      61             :         }
      62             : 
      63           8 :         if (0 != socketpair((int)domain, (int)type, (int)protocol, pair)) {
      64             :                 char errbuf[256];
      65           0 :                 php_error_docref(NULL, E_WARNING, "failed to create sockets: [%d]: %s",
      66           0 :                         php_socket_errno(), php_socket_strerror(php_socket_errno(), errbuf, sizeof(errbuf)));
      67           0 :                 RETURN_FALSE;
      68             :         }
      69             : 
      70           8 :         array_init(return_value);
      71             : 
      72           8 :         s1 = php_stream_sock_open_from_socket(pair[0], 0);
      73           8 :         s2 = php_stream_sock_open_from_socket(pair[1], 0);
      74             : 
      75             :         /* set the __exposed flag.
      76             :          * php_stream_to_zval() does, add_next_index_resource() does not */
      77           8 :         php_stream_auto_cleanup(s1);
      78           8 :         php_stream_auto_cleanup(s2);
      79             : 
      80           8 :         add_next_index_resource(return_value, s1->res);
      81           8 :         add_next_index_resource(return_value, s2->res);
      82             : }
      83             : /* }}} */
      84             : #endif
      85             : 
      86             : /* {{{ proto resource stream_socket_client(string remoteaddress [, long &errcode [, string &errstring [, double timeout [, long flags [, resource context]]]]])
      87             :    Open a client connection to a remote address */
      88         709 : PHP_FUNCTION(stream_socket_client)
      89             : {
      90             :         zend_string *host;
      91         709 :         zval *zerrno = NULL, *zerrstr = NULL, *zcontext = NULL;
      92         709 :         double timeout = (double)FG(default_socket_timeout);
      93             :         php_timeout_ull conv;
      94             :         struct timeval tv;
      95         709 :         char *hashkey = NULL;
      96         709 :         php_stream *stream = NULL;
      97             :         int err;
      98         709 :         zend_long flags = PHP_STREAM_CLIENT_CONNECT;
      99         709 :         zend_string *errstr = NULL;
     100         709 :         php_stream_context *context = NULL;
     101             : 
     102         709 :         RETVAL_FALSE;
     103             : 
     104         709 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|z/z/dlr", &host, &zerrno, &zerrstr, &timeout, &flags, &zcontext) == FAILURE) {
     105           1 :                 RETURN_FALSE;
     106             :         }
     107             : 
     108         708 :         context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);
     109             : 
     110         708 :         if (flags & PHP_STREAM_CLIENT_PERSISTENT) {
     111           1 :                 spprintf(&hashkey, 0, "stream_socket_client__%s", ZSTR_VAL(host));
     112             :         }
     113             : 
     114             :         /* prepare the timeout value for use */
     115         708 :         conv = (php_timeout_ull) (timeout * 1000000.0);
     116             : #ifdef PHP_WIN32
     117             :         tv.tv_sec = (long)(conv / 1000000);
     118             :         tv.tv_usec =(long)(conv % 1000000);
     119             : #else
     120         708 :         tv.tv_sec = conv / 1000000;
     121         708 :         tv.tv_usec = conv % 1000000;
     122             : #endif
     123         708 :         if (zerrno)     {
     124         673 :                 zval_dtor(zerrno);
     125         673 :                 ZVAL_LONG(zerrno, 0);
     126             :         }
     127         708 :         if (zerrstr) {
     128         671 :                 zval_dtor(zerrstr);
     129         671 :                 ZVAL_EMPTY_STRING(zerrstr);
     130             :         }
     131             : 
     132         708 :         stream = php_stream_xport_create(ZSTR_VAL(host), ZSTR_LEN(host), REPORT_ERRORS,
     133             :                         STREAM_XPORT_CLIENT | (flags & PHP_STREAM_CLIENT_CONNECT ? STREAM_XPORT_CONNECT : 0) |
     134             :                         (flags & PHP_STREAM_CLIENT_ASYNC_CONNECT ? STREAM_XPORT_CONNECT_ASYNC : 0),
     135             :                         hashkey, &tv, context, &errstr, &err);
     136             : 
     137             : 
     138         708 :         if (stream == NULL) {
     139             :                 /* host might contain binary characters */
     140          47 :                 zend_string *quoted_host = php_addslashes(host, 0);
     141             : 
     142          47 :                 php_error_docref(NULL, E_WARNING, "unable to connect to %s (%s)", ZSTR_VAL(quoted_host), errstr == NULL ? "Unknown error" : ZSTR_VAL(errstr));
     143             :                 zend_string_release(quoted_host);
     144             :         }
     145             : 
     146         708 :         if (hashkey) {
     147           1 :                 efree(hashkey);
     148             :         }
     149             : 
     150         708 :         if (stream == NULL)     {
     151          47 :                 if (zerrno) {
     152          47 :                         zval_dtor(zerrno);
     153          47 :                         ZVAL_LONG(zerrno, err);
     154             :                 }
     155          53 :                 if (zerrstr && errstr) {
     156           6 :                         zval_dtor(zerrstr);
     157           6 :                         ZVAL_STR(zerrstr, errstr);
     158          41 :                 } else if (errstr) {
     159           2 :                         zend_string_release(errstr);
     160             :                 }
     161          47 :                 RETURN_FALSE;
     162             :         }
     163             : 
     164         661 :         if (errstr) {
     165           0 :                 zend_string_release(errstr);
     166             :         }
     167             : 
     168         661 :         php_stream_to_zval(stream, return_value);
     169             : 
     170             : }
     171             : /* }}} */
     172             : 
     173             : /* {{{ proto resource stream_socket_server(string localaddress [, long &errcode [, string &errstring [, long flags [, resource context]]]])
     174             :    Create a server socket bound to localaddress */
     175         156 : PHP_FUNCTION(stream_socket_server)
     176             : {
     177             :         char *host;
     178             :         size_t host_len;
     179         156 :         zval *zerrno = NULL, *zerrstr = NULL, *zcontext = NULL;
     180         156 :         php_stream *stream = NULL;
     181         156 :         int err = 0;
     182         156 :         zend_long flags = STREAM_XPORT_BIND | STREAM_XPORT_LISTEN;
     183         156 :         zend_string *errstr = NULL;
     184         156 :         php_stream_context *context = NULL;
     185             : 
     186         156 :         RETVAL_FALSE;
     187             : 
     188         156 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|z/z/lr", &host, &host_len, &zerrno, &zerrstr, &flags, &zcontext) == FAILURE) {
     189           0 :                 RETURN_FALSE;
     190             :         }
     191             : 
     192         156 :         context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);
     193             : 
     194         156 :         if (context) {
     195         156 :                 GC_REFCOUNT(context->res)++;
     196             :         }
     197             : 
     198         156 :         if (zerrno)     {
     199         121 :                 zval_dtor(zerrno);
     200         121 :                 ZVAL_LONG(zerrno, 0);
     201             :         }
     202         156 :         if (zerrstr) {
     203         121 :                 zval_dtor(zerrstr);
     204         121 :                 ZVAL_EMPTY_STRING(zerrstr);
     205             :         }
     206             : 
     207         156 :         stream = php_stream_xport_create(host, host_len, REPORT_ERRORS,
     208             :                         STREAM_XPORT_SERVER | (int)flags,
     209             :                         NULL, NULL, context, &errstr, &err);
     210             : 
     211         156 :         if (stream == NULL) {
     212           0 :                 php_error_docref(NULL, E_WARNING, "unable to connect to %s (%s)", host, errstr == NULL ? "Unknown error" : ZSTR_VAL(errstr));
     213             :         }
     214             : 
     215         156 :         if (stream == NULL)     {
     216           0 :                 if (zerrno) {
     217           0 :                         zval_dtor(zerrno);
     218           0 :                         ZVAL_LONG(zerrno, err);
     219             :                 }
     220           0 :                 if (zerrstr && errstr) {
     221           0 :                         zval_dtor(zerrstr);
     222           0 :                         ZVAL_STR(zerrstr, errstr);
     223           0 :                 } else if (errstr) {
     224           0 :                         zend_string_release(errstr);
     225             :                 }
     226           0 :                 RETURN_FALSE;
     227             :         }
     228             : 
     229         156 :         if (errstr) {
     230           0 :                 zend_string_release(errstr);
     231             :         }
     232             : 
     233         156 :         php_stream_to_zval(stream, return_value);
     234             : }
     235             : /* }}} */
     236             : 
     237             : /* {{{ proto resource stream_socket_accept(resource serverstream, [ double timeout [, string &peername ]])
     238             :    Accept a client connection from a server socket */
     239         117 : PHP_FUNCTION(stream_socket_accept)
     240             : {
     241         117 :         double timeout = (double)FG(default_socket_timeout);
     242         117 :         zval *zpeername = NULL;
     243         117 :         zend_string *peername = NULL;
     244             :         php_timeout_ull conv;
     245             :         struct timeval tv;
     246         117 :         php_stream *stream = NULL, *clistream = NULL;
     247             :         zval *zstream;
     248         117 :         zend_string *errstr = NULL;
     249             : 
     250         117 :         ZEND_PARSE_PARAMETERS_START(1, 3)
     251         351 :                 Z_PARAM_RESOURCE(zstream)
     252         117 :                 Z_PARAM_OPTIONAL
     253         245 :                 Z_PARAM_DOUBLE(timeout)
     254          64 :                 Z_PARAM_ZVAL_EX(zpeername, 0, 1)
     255         117 :         ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
     256             : 
     257         117 :         php_stream_from_zval(stream, zstream);
     258             : 
     259             :         /* prepare the timeout value for use */
     260         117 :         conv = (php_timeout_ull) (timeout * 1000000.0);
     261             : #ifdef PHP_WIN32
     262             :         tv.tv_sec = (long)(conv / 1000000);
     263             :         tv.tv_usec = (long)(conv % 1000000);
     264             : #else
     265         117 :         tv.tv_sec = conv / 1000000;
     266         117 :         tv.tv_usec = conv % 1000000;
     267             : #endif
     268         117 :         if (zpeername) {
     269           0 :                 zval_dtor(zpeername);
     270           0 :                 ZVAL_NULL(zpeername);
     271             :         }
     272             : 
     273         301 :         if (0 == php_stream_xport_accept(stream, &clistream,
     274         117 :                                 zpeername ? &peername : NULL,
     275             :                                 NULL, NULL,
     276             :                                 &tv, &errstr
     277         210 :                                 ) && clistream) {
     278             : 
     279          93 :                 if (peername) {
     280           0 :                         ZVAL_STR(zpeername, peername);
     281             :                 }
     282          93 :                 php_stream_to_zval(clistream, return_value);
     283             :         } else {
     284          22 :                 php_error_docref(NULL, E_WARNING, "accept failed: %s", errstr ? ZSTR_VAL(errstr) : "Unknown error");
     285          22 :                 RETVAL_FALSE;
     286             :         }
     287             : 
     288         115 :         if (errstr) {
     289         115 :                 zend_string_release(errstr);
     290             :         }
     291             : }
     292             : /* }}} */
     293             : 
     294             : /* {{{ proto string stream_socket_get_name(resource stream, bool want_peer)
     295             :    Returns either the locally bound or remote name for a socket stream */
     296           4 : PHP_FUNCTION(stream_socket_get_name)
     297             : {
     298             :         php_stream *stream;
     299             :         zval *zstream;
     300             :         zend_bool want_peer;
     301           4 :         zend_string *name = NULL;
     302             : 
     303           4 :         ZEND_PARSE_PARAMETERS_START(2, 2)
     304          12 :                 Z_PARAM_RESOURCE(zstream)
     305          12 :                 Z_PARAM_BOOL(want_peer)
     306           4 :         ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
     307             : 
     308           4 :         php_stream_from_zval(stream, zstream);
     309             : 
     310           5 :         if (0 != php_stream_xport_get_name(stream, want_peer,
     311             :                                 &name,
     312             :                                 NULL, NULL
     313           5 :                                 ) || !name) {
     314           3 :                 RETURN_FALSE;
     315             :         }
     316             : 
     317           1 :         RETVAL_STR(name);
     318             : }
     319             : /* }}} */
     320             : 
     321             : /* {{{ proto long stream_socket_sendto(resouce stream, string data [, long flags [, string target_addr]])
     322             :    Send data to a socket stream.  If target_addr is specified it must be in dotted quad (or [ipv6]) format */
     323           0 : PHP_FUNCTION(stream_socket_sendto)
     324             : {
     325             :         php_stream *stream;
     326             :         zval *zstream;
     327           0 :         zend_long flags = 0;
     328           0 :         char *data, *target_addr = NULL;
     329           0 :         size_t datalen, target_addr_len = 0;
     330             :         php_sockaddr_storage sa;
     331           0 :         socklen_t sl = 0;
     332             : 
     333           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|ls", &zstream, &data, &datalen, &flags, &target_addr, &target_addr_len) == FAILURE) {
     334           0 :                 RETURN_FALSE;
     335             :         }
     336           0 :         php_stream_from_zval(stream, zstream);
     337             : 
     338           0 :         if (target_addr_len) {
     339             :                 /* parse the address */
     340           0 :                 if (FAILURE == php_network_parse_network_address_with_port(target_addr, target_addr_len, (struct sockaddr*)&sa, &sl)) {
     341           0 :                         php_error_docref(NULL, E_WARNING, "Failed to parse `%s' into a valid network address", target_addr);
     342           0 :                         RETURN_FALSE;
     343             :                 }
     344             :         }
     345             : 
     346           0 :         RETURN_LONG(php_stream_xport_sendto(stream, data, datalen, (int)flags, target_addr ? &sa : NULL, sl));
     347             : }
     348             : /* }}} */
     349             : 
     350             : /* {{{ proto string stream_socket_recvfrom(resource stream, long amount [, long flags [, string &remote_addr]])
     351             :    Receives data from a socket stream */
     352           4 : PHP_FUNCTION(stream_socket_recvfrom)
     353             : {
     354             :         php_stream *stream;
     355           4 :         zval *zstream, *zremote = NULL;
     356           4 :         zend_string *remote_addr = NULL;
     357           4 :         zend_long to_read = 0;
     358             :         zend_string *read_buf;
     359           4 :         zend_long flags = 0;
     360             :         int recvd;
     361             : 
     362           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl|lz/", &zstream, &to_read, &flags, &zremote) == FAILURE) {
     363           0 :                 RETURN_FALSE;
     364             :         }
     365             : 
     366           4 :         php_stream_from_zval(stream, zstream);
     367             : 
     368           4 :         if (zremote) {
     369           1 :                 zval_dtor(zremote);
     370           1 :                 ZVAL_NULL(zremote);
     371             :         }
     372             : 
     373           4 :         if (to_read <= 0) {
     374           0 :                 php_error_docref(NULL, E_WARNING, "Length parameter must be greater than 0");
     375           0 :                 RETURN_FALSE;
     376             :         }
     377             : 
     378           8 :         read_buf = zend_string_alloc(to_read, 0);
     379             : 
     380           4 :         recvd = php_stream_xport_recvfrom(stream, ZSTR_VAL(read_buf), to_read, (int)flags, NULL, NULL,
     381           4 :                         zremote ? &remote_addr : NULL
     382             :                         );
     383             : 
     384           4 :         if (recvd >= 0) {
     385           0 :                 if (zremote && remote_addr) {
     386           0 :                         ZVAL_STR(zremote, remote_addr);
     387             :                 }
     388           0 :                 ZSTR_VAL(read_buf)[recvd] = '\0';
     389           0 :                 ZSTR_LEN(read_buf) = recvd;
     390           0 :                 RETURN_NEW_STR(read_buf);
     391             :         }
     392             : 
     393             :         zend_string_free(read_buf);
     394           4 :         RETURN_FALSE;
     395             : }
     396             : /* }}} */
     397             : 
     398             : /* {{{ proto string stream_get_contents(resource source [, long maxlen [, long offset]])
     399             :    Reads all remaining bytes (or up to maxlen bytes) from a stream and returns them as a string. */
     400       27537 : PHP_FUNCTION(stream_get_contents)
     401             : {
     402             :         php_stream      *stream;
     403             :         zval            *zsrc;
     404       27537 :         zend_long               maxlen          = (ssize_t) PHP_STREAM_COPY_ALL,
     405       27537 :                                 desiredpos      = -1L;
     406             :         zend_string *contents;
     407             : 
     408       27537 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r|ll", &zsrc, &maxlen, &desiredpos) == FAILURE) {
     409           2 :                 RETURN_FALSE;
     410             :         }
     411             : 
     412       27535 :         php_stream_from_zval(stream, zsrc);
     413             : 
     414       27535 :         if (desiredpos >= 0) {
     415          38 :                 int             seek_res = 0;
     416             :                 zend_off_t      position;
     417             : 
     418          38 :                 position = php_stream_tell(stream);
     419          41 :                 if (position >= 0 && desiredpos > position) {
     420             :                         /* use SEEK_CUR to allow emulation in streams that don't support seeking */
     421           3 :                         seek_res = php_stream_seek(stream, desiredpos - position, SEEK_CUR);
     422          35 :                 } else if (desiredpos < position)  {
     423             :                         /* desired position before position or error on tell */
     424          22 :                         seek_res = php_stream_seek(stream, desiredpos, SEEK_SET);
     425             :                 }
     426             : 
     427          38 :                 if (seek_res != 0) {
     428           0 :                         php_error_docref(NULL, E_WARNING,
     429             :                                 "Failed to seek to position " ZEND_LONG_FMT " in the stream", desiredpos);
     430           0 :                         RETURN_FALSE;
     431             :                 }
     432             :         }
     433             : 
     434       27535 :         if (maxlen > INT_MAX) {
     435           0 :                 php_error_docref(NULL, E_WARNING, "maxlen truncated from " ZEND_LONG_FMT " to %d bytes", maxlen, INT_MAX);
     436           0 :                 maxlen = INT_MAX;
     437             :         }
     438       27535 :         if ((contents = php_stream_copy_to_mem(stream, maxlen, 0))) {
     439       20129 :                 RETURN_STR(contents);
     440             :         } else {
     441        7406 :                 RETURN_EMPTY_STRING();
     442             :         }
     443             : }
     444             : /* }}} */
     445             : 
     446             : /* {{{ proto long stream_copy_to_stream(resource source, resource dest [, long maxlen [, long pos]])
     447             :    Reads up to maxlen bytes from source stream and writes them to dest stream. */
     448          15 : PHP_FUNCTION(stream_copy_to_stream)
     449             : {
     450             :         php_stream *src, *dest;
     451             :         zval *zsrc, *zdest;
     452          15 :         zend_long maxlen = PHP_STREAM_COPY_ALL, pos = 0;
     453             :         size_t len;
     454             :         int ret;
     455             : 
     456          15 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rr|ll", &zsrc, &zdest, &maxlen, &pos) == FAILURE) {
     457           0 :                 RETURN_FALSE;
     458             :         }
     459             : 
     460          15 :         php_stream_from_zval(src, zsrc);
     461          15 :         php_stream_from_zval(dest, zdest);
     462             : 
     463          15 :         if (pos > 0 && php_stream_seek(src, pos, SEEK_SET) < 0) {
     464           0 :                 php_error_docref(NULL, E_WARNING, "Failed to seek to position " ZEND_LONG_FMT " in the stream", pos);
     465           0 :                 RETURN_FALSE;
     466             :         }
     467             : 
     468          15 :         ret = php_stream_copy_to_stream_ex(src, dest, maxlen, &len);
     469             : 
     470          15 :         if (ret != SUCCESS) {
     471           0 :                 RETURN_FALSE;
     472             :         }
     473          15 :         RETURN_LONG(len);
     474             : }
     475             : /* }}} */
     476             : 
     477             : /* {{{ proto array stream_get_meta_data(resource fp)
     478             :     Retrieves header/meta data from streams/file pointers */
     479         110 : PHP_FUNCTION(stream_get_meta_data)
     480             : {
     481             :         zval *zstream;
     482             :         php_stream *stream;
     483             : 
     484         110 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     485         324 :                 Z_PARAM_RESOURCE(zstream)
     486         110 :         ZEND_PARSE_PARAMETERS_END();
     487             : 
     488         100 :         php_stream_from_zval(stream, zstream);
     489             : 
     490          99 :         array_init(return_value);
     491             : 
     492          99 :         if (!php_stream_populate_meta_data(stream, return_value)) {
     493          28 :                 add_assoc_bool(return_value, "timed_out", 0);
     494          28 :                 add_assoc_bool(return_value, "blocked", 1);
     495          28 :                 add_assoc_bool(return_value, "eof", php_stream_eof(stream));
     496             :         }
     497             : 
     498         198 :         if (!Z_ISUNDEF(stream->wrapperdata)) {
     499          33 :                 Z_ADDREF_P(&stream->wrapperdata);
     500          33 :                 add_assoc_zval(return_value, "wrapper_data", &stream->wrapperdata);
     501             :         }
     502          99 :         if (stream->wrapper) {
     503          78 :                 add_assoc_string(return_value, "wrapper_type", (char *)stream->wrapper->wops->label);
     504             :         }
     505          99 :         add_assoc_string(return_value, "stream_type", (char *)stream->ops->label);
     506             : 
     507          99 :         add_assoc_string(return_value, "mode", stream->mode);
     508             : 
     509             : #if 0   /* TODO: needs updating for new filter API */
     510             :         if (stream->filterhead) {
     511             :                 php_stream_filter *filter;
     512             : 
     513             :                 MAKE_STD_ZVAL(newval);
     514             :                 array_init(newval);
     515             : 
     516             :                 for (filter = stream->filterhead; filter != NULL; filter = filter->next) {
     517             :                         add_next_index_string(newval, (char *)filter->fops->label);
     518             :                 }
     519             : 
     520             :                 add_assoc_zval(return_value, "filters", newval);
     521             :         }
     522             : #endif
     523             : 
     524          99 :         add_assoc_long(return_value, "unread_bytes", stream->writepos - stream->readpos);
     525             : 
     526          99 :         add_assoc_bool(return_value, "seekable", (stream->ops->seek) && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0);
     527          99 :         if (stream->orig_path) {
     528          77 :                 add_assoc_string(return_value, "uri", stream->orig_path);
     529             :         }
     530             : 
     531             : }
     532             : /* }}} */
     533             : 
     534             : /* {{{ proto array stream_get_transports()
     535             :    Retrieves list of registered socket transports */
     536           4 : PHP_FUNCTION(stream_get_transports)
     537             : {
     538             :         HashTable *stream_xport_hash;
     539             :         zend_string *stream_xport;
     540             : 
     541           4 :         if (zend_parse_parameters_none() == FAILURE) {
     542           0 :                 return;
     543             :         }
     544             : 
     545           4 :         if ((stream_xport_hash = php_stream_xport_get_hash())) {
     546           4 :                 array_init(return_value);
     547          84 :                 ZEND_HASH_FOREACH_STR_KEY(stream_xport_hash, stream_xport) {
     548          40 :                         add_next_index_str(return_value, zend_string_copy(stream_xport));
     549             :                 } ZEND_HASH_FOREACH_END();
     550             :         } else {
     551           0 :                 RETURN_FALSE;
     552             :         }
     553             : }
     554             : /* }}} */
     555             : 
     556             : /* {{{ proto array stream_get_wrappers()
     557             :     Retrieves list of registered stream wrappers */
     558          11 : PHP_FUNCTION(stream_get_wrappers)
     559             : {
     560             :         HashTable *url_stream_wrappers_hash;
     561             :         zend_string *stream_protocol;
     562             : 
     563          11 :         if (zend_parse_parameters_none() == FAILURE) {
     564           0 :                 return;
     565             :         }
     566             : 
     567          11 :         if ((url_stream_wrappers_hash = php_stream_get_url_stream_wrappers_hash())) {
     568          11 :                 array_init(return_value);
     569         277 :                 ZEND_HASH_FOREACH_STR_KEY(url_stream_wrappers_hash, stream_protocol) {
     570         133 :                         if (stream_protocol) {
     571         133 :                                 add_next_index_str(return_value, zend_string_copy(stream_protocol));
     572             :                         }
     573             :                 } ZEND_HASH_FOREACH_END();
     574             :         } else {
     575           0 :                 RETURN_FALSE;
     576             :         }
     577             : 
     578             : }
     579             : /* }}} */
     580             : 
     581             : /* {{{ stream_select related functions */
     582      814837 : static int stream_array_to_fd_set(zval *stream_array, fd_set *fds, php_socket_t *max_fd)
     583             : {
     584             :         zval *elem;
     585             :         php_stream *stream;
     586      814837 :         int cnt = 0;
     587             : 
     588      814837 :         if (Z_TYPE_P(stream_array) != IS_ARRAY) {
     589           0 :                 return 0;
     590             :         }
     591             : 
     592     5703537 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(stream_array), elem) {
     593             :                 /* Temporary int fd is needed for the STREAM data type on windows, passing this_fd directly to php_stream_cast()
     594             :                         would eventually bring a wrong result on x64. php_stream_cast() casts to int internally, and this will leave
     595             :                         the higher bits of a SOCKET variable uninitialized on systems with little endian. */
     596             :                 php_socket_t this_fd;
     597             : 
     598     1629579 :                 ZVAL_DEREF(elem);
     599     1629579 :                 php_stream_from_zval_no_verify(stream, elem);
     600     1629579 :                 if (stream == NULL) {
     601           0 :                         continue;
     602             :                 }
     603             :                 /* get the fd.
     604             :                  * NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag
     605             :                  * when casting.  It is only used here so that the buffered data warning
     606             :                  * is not displayed.
     607             :                  * */
     608     1629579 :                 if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) && this_fd != -1) {
     609             : 
     610     1629574 :                         PHP_SAFE_FD_SET(this_fd, fds);
     611             : 
     612     1629574 :                         if (this_fd > *max_fd) {
     613     1629568 :                                 *max_fd = this_fd;
     614             :                         }
     615     1629574 :                         cnt++;
     616             :                 }
     617             :         } ZEND_HASH_FOREACH_END();
     618      814837 :         return cnt ? 1 : 0;
     619             : }
     620             : 
     621      814822 : static int stream_array_from_fd_set(zval *stream_array, fd_set *fds)
     622             : {
     623             :         zval *elem, *dest_elem, new_array;
     624             :         php_stream *stream;
     625      814822 :         int ret = 0;
     626             :         zend_string *key;
     627             :         zend_ulong num_ind;
     628             : 
     629      814822 :         if (Z_TYPE_P(stream_array) != IS_ARRAY) {
     630           0 :                 return 0;
     631             :         }
     632      814822 :         ZVAL_NEW_ARR(&new_array);
     633      814822 :         zend_hash_init(Z_ARRVAL(new_array), zend_hash_num_elements(Z_ARRVAL_P(stream_array)), NULL, ZVAL_PTR_DTOR, 0);
     634             : 
     635     5703500 :         ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(stream_array), num_ind, key, elem) {
     636             :                 php_socket_t this_fd;
     637             : 
     638     1629568 :                 ZVAL_DEREF(elem);
     639     1629568 :                 php_stream_from_zval_no_verify(stream, elem);
     640     1629568 :                 if (stream == NULL) {
     641           0 :                         continue;
     642             :                 }
     643             :                 /* get the fd
     644             :                  * NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag
     645             :                  * when casting.  It is only used here so that the buffered data warning
     646             :                  * is not displayed.
     647             :                  */
     648     1629568 :                 if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) && this_fd != SOCK_ERR) {
     649     1629568 :                         if (PHP_SAFE_FD_ISSET(this_fd, fds)) {
     650      838311 :                                 if (!key) {
     651      838310 :                                         dest_elem = zend_hash_index_update(Z_ARRVAL(new_array), num_ind, elem);
     652             :                                 } else {
     653           1 :                                         dest_elem = zend_hash_update(Z_ARRVAL(new_array), key, elem);
     654             :                                 }
     655             : 
     656      838311 :                                 if (dest_elem) {
     657      838311 :                                         zval_add_ref(dest_elem);
     658             :                                 }
     659      838311 :                                 ret++;
     660      838311 :                                 continue;
     661             :                         }
     662             :                 }
     663             :         } ZEND_HASH_FOREACH_END();
     664             : 
     665             :         /* destroy old array and add new one */
     666      814822 :         zend_array_destroy(Z_ARR_P(stream_array));
     667      814822 :         Z_ARR_P(stream_array) = Z_ARR(new_array);
     668             : 
     669      814822 :         return ret;
     670             : }
     671             : 
     672      814797 : static int stream_array_emulate_read_fd_set(zval *stream_array)
     673             : {
     674             :         zval *elem, *dest_elem, new_array;
     675             :         php_stream *stream;
     676      814797 :         int ret = 0;
     677             : 
     678      814797 :         if (Z_TYPE_P(stream_array) != IS_ARRAY) {
     679           0 :                 return 0;
     680             :         }
     681      814797 :         ZVAL_NEW_ARR(&new_array);
     682      814797 :         zend_hash_init(Z_ARRVAL(new_array), zend_hash_num_elements(Z_ARRVAL_P(stream_array)), NULL, ZVAL_PTR_DTOR, 0);
     683             : 
     684     5703477 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(stream_array), elem) {
     685     1629569 :                 ZVAL_DEREF(elem);
     686     1629569 :                 php_stream_from_zval_no_verify(stream, elem);
     687     1629569 :                 if (stream == NULL) {
     688           0 :                         continue;
     689             :                 }
     690     1629569 :                 if ((stream->writepos - stream->readpos) > 0) {
     691             :                         /* allow readable non-descriptor based streams to participate in stream_select.
     692             :                          * Non-descriptor streams will only "work" if they have previously buffered the
     693             :                          * data.  Not ideal, but better than nothing.
     694             :                          * This branch of code also allows blocking streams with buffered data to
     695             :                          * operate correctly in stream_select.
     696             :                          * */
     697           5 :                         dest_elem = zend_hash_next_index_insert(Z_ARRVAL(new_array), elem);
     698           5 :                         if (dest_elem) {
     699           5 :                                 zval_add_ref(dest_elem);
     700             :                         }
     701           5 :                         ret++;
     702           5 :                         continue;
     703             :                 }
     704             :         } ZEND_HASH_FOREACH_END();
     705             : 
     706      814797 :         if (ret > 0) {
     707             :                 /* destroy old array and add new one */
     708           5 :                 zend_array_destroy(Z_ARR_P(stream_array));
     709           5 :                 Z_ARR_P(stream_array) = Z_ARR(new_array);
     710             :         } else {
     711      814792 :                 zend_array_destroy(Z_ARR(new_array));
     712             :         }
     713             : 
     714      814797 :         return ret;
     715             : }
     716             : /* }}} */
     717             : 
     718             : /* {{{ proto int stream_select(array &read_streams, array &write_streams, array &except_streams, int tv_sec[, int tv_usec])
     719             :    Runs the select() system call on the sets of streams with a timeout specified by tv_sec and tv_usec */
     720      814802 : PHP_FUNCTION(stream_select)
     721             : {
     722             :         zval *r_array, *w_array, *e_array;
     723      814802 :         struct timeval tv, *tv_p = NULL;
     724             :         fd_set rfds, wfds, efds;
     725      814802 :         php_socket_t max_fd = 0;
     726      814802 :         int retval, sets = 0;
     727      814802 :         zend_long sec, usec = 0;
     728             :         zend_bool secnull;
     729      814802 :         int set_count, max_set_count = 0;
     730             : 
     731      814802 :         ZEND_PARSE_PARAMETERS_START(4, 5)
     732     4073978 :                 Z_PARAM_ARRAY_EX(r_array, 1, 1)
     733     2444447 :                 Z_PARAM_ARRAY_EX(w_array, 1, 1)
     734     2444432 :                 Z_PARAM_ARRAY_EX(e_array, 1, 1)
     735     2444406 :                 Z_PARAM_LONG_EX(sec, secnull, 1, 0)
     736      814802 :                 Z_PARAM_OPTIONAL
     737      814834 :                 Z_PARAM_LONG(usec)
     738      814802 :         ZEND_PARSE_PARAMETERS_END();
     739             : 
     740      814802 :         FD_ZERO(&rfds);
     741      814802 :         FD_ZERO(&wfds);
     742      814802 :         FD_ZERO(&efds);
     743             : 
     744      814802 :         if (r_array != NULL) {
     745      814802 :                 set_count = stream_array_to_fd_set(r_array, &rfds, &max_fd);
     746      814802 :                 if (set_count > max_set_count)
     747      814796 :                         max_set_count = set_count;
     748      814802 :                 sets += set_count;
     749             :         }
     750             : 
     751      814802 :         if (w_array != NULL) {
     752          22 :                 set_count = stream_array_to_fd_set(w_array, &wfds, &max_fd);
     753          22 :                 if (set_count > max_set_count)
     754           1 :                         max_set_count = set_count;
     755          22 :                 sets += set_count;
     756             :         }
     757             : 
     758      814802 :         if (e_array != NULL) {
     759          13 :                 set_count = stream_array_to_fd_set(e_array, &efds, &max_fd);
     760          13 :                 if (set_count > max_set_count)
     761           0 :                         max_set_count = set_count;
     762          13 :                 sets += set_count;
     763             :         }
     764             : 
     765      814802 :         if (!sets) {
     766           5 :                 php_error_docref(NULL, E_WARNING, "No stream arrays were passed");
     767           5 :                 RETURN_FALSE;
     768             :         }
     769             : 
     770      814797 :         PHP_SAFE_MAX_FD(max_fd, max_set_count);
     771             : 
     772             :         /* If seconds is not set to null, build the timeval, else we wait indefinitely */
     773      814797 :         if (!secnull) {
     774      814797 :                 if (sec < 0) {
     775           0 :                         php_error_docref(NULL, E_WARNING, "The seconds parameter must be greater than 0");
     776           0 :                         RETURN_FALSE;
     777      814797 :                 } else if (usec < 0) {
     778           0 :                         php_error_docref(NULL, E_WARNING, "The microseconds parameter must be greater than 0");
     779           0 :                         RETURN_FALSE;
     780             :                 }
     781             : 
     782             :                 /* Windows, Solaris and BSD do not like microsecond values which are >= 1 sec */
     783      814797 :                 tv.tv_sec = (long)(sec + (usec / 1000000));
     784      814797 :                 tv.tv_usec = (long)(usec % 1000000);
     785      814797 :                 tv_p = &tv;
     786             :         }
     787             : 
     788             :         /* slight hack to support buffered data; if there is data sitting in the
     789             :          * read buffer of any of the streams in the read array, let's pretend
     790             :          * that we selected, but return only the readable sockets */
     791      814797 :         if (r_array != NULL) {
     792      814797 :                 retval = stream_array_emulate_read_fd_set(r_array);
     793      814797 :                 if (retval > 0) {
     794           5 :                         if (w_array != NULL) {
     795           5 :                                 zend_hash_clean(Z_ARRVAL_P(w_array));
     796             :                         }
     797           5 :                         if (e_array != NULL) {
     798           0 :                                 zend_hash_clean(Z_ARRVAL_P(e_array));
     799             :                         }
     800           5 :                         RETURN_LONG(retval);
     801             :                 }
     802             :         }
     803             : 
     804      814792 :         retval = php_select(max_fd+1, &rfds, &wfds, &efds, tv_p);
     805             : 
     806      814792 :         if (retval == -1) {
     807           0 :                 php_error_docref(NULL, E_WARNING, "unable to select [%d]: %s (max_fd=%d)",
     808             :                                 errno, strerror(errno), max_fd);
     809           0 :                 RETURN_FALSE;
     810             :         }
     811             : 
     812      814792 :         if (r_array != NULL) stream_array_from_fd_set(r_array, &rfds);
     813      814792 :         if (w_array != NULL) stream_array_from_fd_set(w_array, &wfds);
     814      814792 :         if (e_array != NULL) stream_array_from_fd_set(e_array, &efds);
     815             : 
     816      814792 :         RETURN_LONG(retval);
     817             : }
     818             : /* }}} */
     819             : 
     820             : /* {{{ stream_context related functions */
     821           6 : static void user_space_stream_notifier(php_stream_context *context, int notifycode, int severity,
     822             :                 char *xmsg, int xcode, size_t bytes_sofar, size_t bytes_max, void * ptr)
     823             : {
     824           6 :         zval *callback = &context->notifier->ptr;
     825             :         zval retval;
     826             :         zval zvs[6];
     827             :         int i;
     828             : 
     829           6 :         ZVAL_LONG(&zvs[0], notifycode);
     830           6 :         ZVAL_LONG(&zvs[1], severity);
     831           6 :         if (xmsg) {
     832           2 :                 ZVAL_STRING(&zvs[2], xmsg);
     833             :         } else {
     834           5 :                 ZVAL_NULL(&zvs[2]);
     835             :         }
     836           6 :         ZVAL_LONG(&zvs[3], xcode);
     837           6 :         ZVAL_LONG(&zvs[4], bytes_sofar);
     838           6 :         ZVAL_LONG(&zvs[5], bytes_max);
     839             : 
     840           6 :         if (FAILURE == call_user_function_ex(EG(function_table), NULL, callback, &retval, 6, zvs, 0, NULL)) {
     841           0 :                 php_error_docref(NULL, E_WARNING, "failed to call user notifier");
     842             :         }
     843          42 :         for (i = 0; i < 6; i++) {
     844          36 :                 zval_ptr_dtor(&zvs[i]);
     845             :         }
     846           6 :         zval_ptr_dtor(&retval);
     847           6 : }
     848             : 
     849           3 : static void user_space_stream_notifier_dtor(php_stream_notifier *notifier)
     850             : {
     851           6 :         if (notifier && Z_TYPE(notifier->ptr) != IS_UNDEF) {
     852           3 :                 zval_ptr_dtor(&notifier->ptr);
     853           3 :                 ZVAL_UNDEF(&notifier->ptr);
     854             :         }
     855           3 : }
     856             : 
     857         765 : static int parse_context_options(php_stream_context *context, zval *options)
     858             : {
     859             :         zval *wval, *oval;
     860             :         zend_string *wkey, *okey;
     861         765 :         int ret = SUCCESS;
     862             : 
     863        1083 :         ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(options), wkey, wval) {
     864         159 :                 ZVAL_DEREF(wval);
     865         473 :                 if (wkey && Z_TYPE_P(wval) == IS_ARRAY) {
     866         689 :                         ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(wval), okey, oval) {
     867         266 :                                 if (okey) {
     868         266 :                                         php_stream_context_set_option(context, ZSTR_VAL(wkey), ZSTR_VAL(okey), oval);
     869             :                                 }
     870             :                         } ZEND_HASH_FOREACH_END();
     871             :                 } else {
     872           2 :                         php_error_docref(NULL, E_WARNING, "options should have the form [\"wrappername\"][\"optionname\"] = $value");
     873             :                 }
     874             :         } ZEND_HASH_FOREACH_END();
     875             : 
     876         765 :         return ret;
     877             : }
     878             : 
     879           5 : static int parse_context_params(php_stream_context *context, zval *params)
     880             : {
     881           5 :         int ret = SUCCESS;
     882             :         zval *tmp;
     883             : 
     884           5 :         if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(params), "notification", sizeof("notification")-1))) {
     885             : 
     886           3 :                 if (context->notifier) {
     887           1 :                         php_stream_notification_free(context->notifier);
     888           1 :                         context->notifier = NULL;
     889             :                 }
     890             : 
     891           3 :                 context->notifier = php_stream_notification_alloc();
     892           3 :                 context->notifier->func = user_space_stream_notifier;
     893           3 :                 ZVAL_COPY(&context->notifier->ptr, tmp);
     894           3 :                 context->notifier->dtor = user_space_stream_notifier_dtor;
     895             :         }
     896           5 :         if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(params), "options", sizeof("options")-1))) {
     897           1 :                 if (Z_TYPE_P(tmp) == IS_ARRAY) {
     898           0 :                         parse_context_options(context, tmp);
     899             :                 } else {
     900           1 :                         php_error_docref(NULL, E_WARNING, "Invalid stream/context parameter");
     901             :                 }
     902             :         }
     903             : 
     904           5 :         return ret;
     905             : }
     906             : 
     907             : /* given a zval which is either a stream or a context, return the underlying
     908             :  * stream_context.  If it is a stream that does not have a context assigned, it
     909             :  * will create and assign a context and return that.  */
     910          47 : static php_stream_context *decode_context_param(zval *contextresource)
     911             : {
     912          47 :         php_stream_context *context = NULL;
     913             : 
     914          47 :         context = zend_fetch_resource_ex(contextresource, NULL, php_le_stream_context());
     915          47 :         if (context == NULL) {
     916             :                 php_stream *stream;
     917             : 
     918           1 :                 stream = zend_fetch_resource2_ex(contextresource, NULL, php_file_le_stream(), php_file_le_pstream());
     919             : 
     920           1 :                 if (stream) {
     921           1 :                         context = PHP_STREAM_CONTEXT(stream);
     922           1 :                         if (context == NULL) {
     923             :                                 /* Only way this happens is if file is opened with NO_DEFAULT_CONTEXT
     924             :                                    param, but then something is called which requires a context.
     925             :                                    Don't give them the default one though since they already said they
     926             :                                    didn't want it. */
     927           1 :                                 context = php_stream_context_alloc();
     928           1 :                                 stream->ctx = context->res;
     929             :                         }
     930             :                 }
     931             :         }
     932             : 
     933          47 :         return context;
     934             : }
     935             : /* }}} */
     936             : 
     937             : /* {{{ proto array stream_context_get_options(resource context|resource stream)
     938             :    Retrieve options for a stream/wrapper/context */
     939          14 : PHP_FUNCTION(stream_context_get_options)
     940             : {
     941             :         zval *zcontext;
     942             :         php_stream_context *context;
     943             : 
     944          14 :         ZEND_PARSE_PARAMETERS_START(1, 1)
     945          42 :                 Z_PARAM_RESOURCE(zcontext)
     946          14 :         ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
     947             : 
     948          14 :         context = decode_context_param(zcontext);
     949          14 :         if (!context) {
     950           0 :                 php_error_docref(NULL, E_WARNING, "Invalid stream/context parameter");
     951           0 :                 RETURN_FALSE;
     952             :         }
     953             : 
     954          14 :         ZVAL_COPY(return_value, &context->options);
     955             : }
     956             : /* }}} */
     957             : 
     958             : /* {{{ proto bool stream_context_set_option(resource context|resource stream, string wrappername, string optionname, mixed value)
     959             :    Set an option for a wrapper */
     960          26 : PHP_FUNCTION(stream_context_set_option)
     961             : {
     962          26 :         zval *zcontext = NULL;
     963             :         php_stream_context *context;
     964             : 
     965          26 :         if (ZEND_NUM_ARGS() == 2) {
     966             :                 zval *options;
     967             : 
     968           2 :                 ZEND_PARSE_PARAMETERS_START(2, 2)
     969           6 :                         Z_PARAM_RESOURCE(zcontext)
     970           6 :                         Z_PARAM_ARRAY(options)
     971           2 :                 ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
     972             : 
     973             :                 /* figure out where the context is coming from exactly */
     974           1 :                 if (!(context = decode_context_param(zcontext))) {
     975           0 :                         php_error_docref(NULL, E_WARNING, "Invalid stream/context parameter");
     976           0 :                         RETURN_FALSE;
     977             :                 }
     978             : 
     979           1 :                 RETURN_BOOL(parse_context_options(context, options) == SUCCESS);
     980             :         } else {
     981             :                 zval *zvalue;
     982             :                 char *wrappername, *optionname;
     983             :                 size_t wrapperlen, optionlen;
     984             : 
     985          24 :                 ZEND_PARSE_PARAMETERS_START(4, 4)
     986          66 :                         Z_PARAM_RESOURCE(zcontext)
     987          66 :                         Z_PARAM_STRING(wrappername, wrapperlen)
     988          63 :                         Z_PARAM_STRING(optionname, optionlen)
     989          21 :                         Z_PARAM_ZVAL(zvalue)
     990          24 :                 ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
     991             : 
     992             :                 /* figure out where the context is coming from exactly */
     993          21 :                 if (!(context = decode_context_param(zcontext))) {
     994           0 :                         php_error_docref(NULL, E_WARNING, "Invalid stream/context parameter");
     995           0 :                         RETURN_FALSE;
     996             :                 }
     997             : 
     998          21 :                 RETURN_BOOL(php_stream_context_set_option(context, wrappername, optionname, zvalue) == SUCCESS);
     999             :         }
    1000             : }
    1001             : /* }}} */
    1002             : 
    1003             : /* {{{ proto bool stream_context_set_params(resource context|resource stream, array options)
    1004             :    Set parameters for a file context */
    1005           5 : PHP_FUNCTION(stream_context_set_params)
    1006             : {
    1007             :         zval *params, *zcontext;
    1008             :         php_stream_context *context;
    1009             : 
    1010           5 :         ZEND_PARSE_PARAMETERS_START(2, 2)
    1011          15 :                 Z_PARAM_RESOURCE(zcontext)
    1012          15 :                 Z_PARAM_ARRAY(params)
    1013           5 :         ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
    1014             : 
    1015           5 :         context = decode_context_param(zcontext);
    1016           5 :         if (!context) {
    1017           0 :                 php_error_docref(NULL, E_WARNING, "Invalid stream/context parameter");
    1018           0 :                 RETURN_FALSE;
    1019             :         }
    1020             : 
    1021           5 :         RETVAL_BOOL(parse_context_params(context, params) == SUCCESS);
    1022             : }
    1023             : /* }}} */
    1024             : 
    1025             : /* {{{ proto array stream_context_get_params(resource context|resource stream)
    1026             :    Get parameters of a file context */
    1027           6 : PHP_FUNCTION(stream_context_get_params)
    1028             : {
    1029             :         zval *zcontext;
    1030             :         php_stream_context *context;
    1031             : 
    1032           6 :         ZEND_PARSE_PARAMETERS_START(1, 1)
    1033          18 :                 Z_PARAM_RESOURCE(zcontext)
    1034           6 :         ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
    1035             : 
    1036           6 :         context = decode_context_param(zcontext);
    1037           6 :         if (!context) {
    1038           0 :                 php_error_docref(NULL, E_WARNING, "Invalid stream/context parameter");
    1039           0 :                 RETURN_FALSE;
    1040             :         }
    1041             : 
    1042           6 :         array_init(return_value);
    1043          10 :         if (context->notifier && Z_TYPE(context->notifier->ptr) != IS_UNDEF && context->notifier->func == user_space_stream_notifier) {
    1044           4 :                 add_assoc_zval_ex(return_value, "notification", sizeof("notification")-1, &context->notifier->ptr);
    1045           4 :                 if (Z_REFCOUNTED(context->notifier->ptr)) Z_ADDREF(context->notifier->ptr);
    1046             :         }
    1047           6 :         if (Z_REFCOUNTED(context->options)) Z_ADDREF(context->options);
    1048           6 :         add_assoc_zval_ex(return_value, "options", sizeof("options")-1, &context->options);
    1049             : }
    1050             : /* }}} */
    1051             : 
    1052             : /* {{{ proto resource stream_context_get_default([array options])
    1053             :    Get a handle on the default file/stream context and optionally set parameters */
    1054           4 : PHP_FUNCTION(stream_context_get_default)
    1055             : {
    1056           4 :         zval *params = NULL;
    1057             :         php_stream_context *context;
    1058             : 
    1059           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|a", &params) == FAILURE) {
    1060           0 :                 RETURN_FALSE;
    1061             :         }
    1062             : 
    1063           4 :         if (FG(default_context) == NULL) {
    1064           4 :                 FG(default_context) = php_stream_context_alloc();
    1065             :         }
    1066           4 :         context = FG(default_context);
    1067             : 
    1068           4 :         if (params) {
    1069           1 :                 parse_context_options(context, params);
    1070             :         }
    1071             : 
    1072           4 :         php_stream_context_to_zval(context, return_value);
    1073             : }
    1074             : /* }}} */
    1075             : 
    1076             : /* {{{ proto resource stream_context_set_default(array options)
    1077             :    Set default file/stream context, returns the context as a resource */
    1078           2 : PHP_FUNCTION(stream_context_set_default)
    1079             : {
    1080           2 :         zval *options = NULL;
    1081             :         php_stream_context *context;
    1082             : 
    1083           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &options) == FAILURE) {
    1084           0 :                 return;
    1085             :         }
    1086             : 
    1087           2 :         if (FG(default_context) == NULL) {
    1088           1 :                 FG(default_context) = php_stream_context_alloc();
    1089             :         }
    1090           2 :         context = FG(default_context);
    1091             : 
    1092           2 :         parse_context_options(context, options);
    1093             : 
    1094           2 :         php_stream_context_to_zval(context, return_value);
    1095             : }
    1096             : /* }}} */
    1097             : 
    1098             : /* {{{ proto resource stream_context_create([array options[, array params]])
    1099             :    Create a file context and optionally set parameters */
    1100         791 : PHP_FUNCTION(stream_context_create)
    1101             : {
    1102         791 :         zval *options = NULL, *params = NULL;
    1103             :         php_stream_context *context;
    1104             : 
    1105         791 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|a!a!", &options, &params) == FAILURE) {
    1106           0 :                 RETURN_FALSE;
    1107             :         }
    1108             : 
    1109         791 :         context = php_stream_context_alloc();
    1110             : 
    1111         791 :         if (options) {
    1112         761 :                 parse_context_options(context, options);
    1113             :         }
    1114             : 
    1115         791 :         if (params) {
    1116           0 :                 parse_context_params(context, params);
    1117             :         }
    1118             : 
    1119         791 :         RETURN_RES(context->res);
    1120             : }
    1121             : /* }}} */
    1122             : 
    1123             : /* {{{ streams filter functions */
    1124         103 : static void apply_filter_to_stream(int append, INTERNAL_FUNCTION_PARAMETERS)
    1125             : {
    1126             :         zval *zstream;
    1127             :         php_stream *stream;
    1128             :         char *filtername;
    1129             :         size_t filternamelen;
    1130         103 :         zend_long read_write = 0;
    1131         103 :         zval *filterparams = NULL;
    1132         103 :         php_stream_filter *filter = NULL;
    1133             :         int ret;
    1134             : 
    1135         103 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|lz", &zstream,
    1136             :                                 &filtername, &filternamelen, &read_write, &filterparams) == FAILURE) {
    1137           0 :                 RETURN_FALSE;
    1138             :         }
    1139             : 
    1140         103 :         php_stream_from_zval(stream, zstream);
    1141             : 
    1142         103 :         if ((read_write & PHP_STREAM_FILTER_ALL) == 0) {
    1143             :                 /* Chain not specified.
    1144             :                  * Examine stream->mode to determine which filters are needed
    1145             :                  * There's no harm in attaching a filter to an unused chain,
    1146             :                  * but why waste the memory and clock cycles?
    1147             :                  */
    1148          52 :                 if (strchr(stream->mode, 'r') || strchr(stream->mode, '+')) {
    1149          47 :                         read_write |= PHP_STREAM_FILTER_READ;
    1150             :                 }
    1151          52 :                 if (strchr(stream->mode, 'w') || strchr(stream->mode, '+') || strchr(stream->mode, 'a')) {
    1152          45 :                         read_write |= PHP_STREAM_FILTER_WRITE;
    1153             :                 }
    1154             :         }
    1155             : 
    1156         103 :         if (read_write & PHP_STREAM_FILTER_READ) {
    1157          78 :                 filter = php_stream_filter_create(filtername, filterparams, php_stream_is_persistent(stream));
    1158          78 :                 if (filter == NULL) {
    1159           1 :                         RETURN_FALSE;
    1160             :                 }
    1161             : 
    1162          77 :                 if (append) {
    1163          72 :                         ret = php_stream_filter_append_ex(&stream->readfilters, filter);
    1164             :                 } else {
    1165           5 :                         ret = php_stream_filter_prepend_ex(&stream->readfilters, filter);
    1166             :                 }
    1167          77 :                 if (ret != SUCCESS) {
    1168           7 :                         php_stream_filter_remove(filter, 1);
    1169           7 :                         RETURN_FALSE;
    1170             :                 }
    1171             :         }
    1172             : 
    1173          95 :         if (read_write & PHP_STREAM_FILTER_WRITE) {
    1174          59 :                 filter = php_stream_filter_create(filtername, filterparams, php_stream_is_persistent(stream));
    1175          59 :                 if (filter == NULL) {
    1176           0 :                         RETURN_FALSE;
    1177             :                 }
    1178             : 
    1179          59 :                 if (append) {
    1180          52 :                         ret = php_stream_filter_append_ex(&stream->writefilters, filter);
    1181             :                 } else {
    1182           7 :                         ret = php_stream_filter_prepend_ex(&stream->writefilters, filter);
    1183             :                 }
    1184          59 :                 if (ret != SUCCESS) {
    1185           0 :                         php_stream_filter_remove(filter, 1);
    1186           0 :                         RETURN_FALSE;
    1187             :                 }
    1188             :         }
    1189             : 
    1190          95 :         if (filter) {
    1191          95 :                 filter->res = zend_register_resource(filter, php_file_le_stream_filter());
    1192          95 :                 GC_REFCOUNT(filter->res)++;
    1193          95 :                 RETURN_RES(filter->res);
    1194             :         } else {
    1195           0 :                 RETURN_FALSE;
    1196             :         }
    1197             : }
    1198             : /* }}} */
    1199             : 
    1200             : /* {{{ proto resource stream_filter_prepend(resource stream, string filtername[, int read_write[, string filterparams]])
    1201             :    Prepend a filter to a stream */
    1202           7 : PHP_FUNCTION(stream_filter_prepend)
    1203             : {
    1204           7 :         apply_filter_to_stream(0, INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1205           7 : }
    1206             : /* }}} */
    1207             : 
    1208             : /* {{{ proto resource stream_filter_append(resource stream, string filtername[, int read_write[, string filterparams]])
    1209             :    Append a filter to a stream */
    1210          96 : PHP_FUNCTION(stream_filter_append)
    1211             : {
    1212          96 :         apply_filter_to_stream(1, INTERNAL_FUNCTION_PARAM_PASSTHRU);
    1213          96 : }
    1214             : /* }}} */
    1215             : 
    1216             : /* {{{ proto bool stream_filter_remove(resource stream_filter)
    1217             :         Flushes any data in the filter's internal buffer, removes it from the chain, and frees the resource */
    1218          26 : PHP_FUNCTION(stream_filter_remove)
    1219             : {
    1220             :         zval *zfilter;
    1221             :         php_stream_filter *filter;
    1222             : 
    1223          26 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zfilter) == FAILURE) {
    1224           3 :                 RETURN_FALSE;
    1225             :         }
    1226             : 
    1227          23 :         filter = zend_fetch_resource(Z_RES_P(zfilter), NULL, php_file_le_stream_filter());
    1228          23 :         if (!filter) {
    1229           2 :                 php_error_docref(NULL, E_WARNING, "Invalid resource given, not a stream filter");
    1230           2 :                 RETURN_FALSE;
    1231             :         }
    1232             : 
    1233          21 :         if (php_stream_filter_flush(filter, 1) == FAILURE) {
    1234          10 :                 php_error_docref(NULL, E_WARNING, "Unable to flush filter, not removing");
    1235          10 :                 RETURN_FALSE;
    1236             :         }
    1237             : 
    1238          11 :         if (zend_list_close(Z_RES_P(zfilter)) == FAILURE) {
    1239           0 :                 php_error_docref(NULL, E_WARNING, "Could not invalidate filter, not removing");
    1240           0 :                 RETURN_FALSE;
    1241             :         } else {
    1242          11 :                 php_stream_filter_remove(filter, 1);
    1243          11 :                 RETURN_TRUE;
    1244             :         }
    1245             : }
    1246             : /* }}} */
    1247             : 
    1248             : /* {{{ proto string stream_get_line(resource stream, int maxlen [, string ending])
    1249             :    Read up to maxlen bytes from a stream or until the ending string is found */
    1250          58 : PHP_FUNCTION(stream_get_line)
    1251             : {
    1252          58 :         char *str = NULL;
    1253          58 :         size_t str_len = 0;
    1254             :         zend_long max_length;
    1255             :         zval *zstream;
    1256             :         zend_string *buf;
    1257             :         php_stream *stream;
    1258             : 
    1259          58 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl|s", &zstream, &max_length, &str, &str_len) == FAILURE) {
    1260           0 :                 RETURN_FALSE;
    1261             :         }
    1262             : 
    1263          58 :         if (max_length < 0) {
    1264           0 :                 php_error_docref(NULL, E_WARNING, "The maximum allowed length must be greater than or equal to zero");
    1265           0 :                 RETURN_FALSE;
    1266             :         }
    1267          58 :         if (!max_length) {
    1268           3 :                 max_length = PHP_SOCK_CHUNK_SIZE;
    1269             :         }
    1270             : 
    1271          58 :         php_stream_from_zval(stream, zstream);
    1272             : 
    1273          58 :         if ((buf = php_stream_get_record(stream, max_length, str, str_len))) {
    1274          48 :                 RETURN_STR(buf);
    1275             :         } else {
    1276          10 :                 RETURN_FALSE;
    1277             :         }
    1278             : }
    1279             : 
    1280             : /* }}} */
    1281             : 
    1282             : /* {{{ proto bool stream_set_blocking(resource socket, bool mode)
    1283             :    Set blocking/non-blocking mode on a socket or stream */
    1284          33 : PHP_FUNCTION(stream_set_blocking)
    1285             : {
    1286             :         zval *zstream;
    1287             :         zend_bool block;
    1288             :         php_stream *stream;
    1289             : 
    1290          33 :         ZEND_PARSE_PARAMETERS_START(2, 2)
    1291          99 :                 Z_PARAM_RESOURCE(zstream)
    1292          99 :                 Z_PARAM_BOOL(block)
    1293          33 :         ZEND_PARSE_PARAMETERS_END();
    1294             : 
    1295          33 :         php_stream_from_zval(stream, zstream);
    1296             : 
    1297          29 :         if (php_stream_set_option(stream, PHP_STREAM_OPTION_BLOCKING, block, NULL) == -1) {
    1298           2 :                 RETURN_FALSE;
    1299             :         }
    1300             : 
    1301          27 :         RETURN_TRUE;
    1302             : }
    1303             : 
    1304             : /* }}} */
    1305             : 
    1306             : /* {{{ proto bool stream_set_timeout(resource stream, int seconds [, int microseconds])
    1307             :    Set timeout on stream read to seconds + microseonds */
    1308             : #if HAVE_SYS_TIME_H || defined(PHP_WIN32)
    1309          10 : PHP_FUNCTION(stream_set_timeout)
    1310             : {
    1311             :         zval *socket;
    1312          10 :         zend_long seconds, microseconds = 0;
    1313             :         struct timeval t;
    1314             :         php_stream *stream;
    1315          10 :         int argc = ZEND_NUM_ARGS();
    1316             : 
    1317          10 :         if (zend_parse_parameters(argc, "rl|l", &socket, &seconds, &microseconds) == FAILURE) {
    1318           3 :                 return;
    1319             :         }
    1320             : 
    1321           7 :         php_stream_from_zval(stream, socket);
    1322             : 
    1323             : #ifdef PHP_WIN32
    1324             :         t.tv_sec = (long)seconds;
    1325             : 
    1326             :         if (argc == 3) {
    1327             :                 t.tv_usec = (long)(microseconds % 1000000);
    1328             :                 t.tv_sec +=(long)(microseconds / 1000000);
    1329             :         } else {
    1330             :                 t.tv_usec = 0;
    1331             :         }
    1332             : #else
    1333           6 :         t.tv_sec = seconds;
    1334             : 
    1335           6 :         if (argc == 3) {
    1336           4 :                 t.tv_usec = microseconds % 1000000;
    1337           4 :                 t.tv_sec += microseconds / 1000000;
    1338             :         } else {
    1339           2 :                 t.tv_usec = 0;
    1340             :         }
    1341             : #endif
    1342             : 
    1343           6 :         if (PHP_STREAM_OPTION_RETURN_OK == php_stream_set_option(stream, PHP_STREAM_OPTION_READ_TIMEOUT, 0, &t)) {
    1344           4 :                 RETURN_TRUE;
    1345             :         }
    1346             : 
    1347           2 :         RETURN_FALSE;
    1348             : }
    1349             : #endif /* HAVE_SYS_TIME_H || defined(PHP_WIN32) */
    1350             : /* }}} */
    1351             : 
    1352             : /* {{{ proto int stream_set_write_buffer(resource fp, int buffer)
    1353             :    Set file write buffer */
    1354           6 : PHP_FUNCTION(stream_set_write_buffer)
    1355             : {
    1356             :         zval *arg1;
    1357             :         int ret;
    1358             :         zend_long arg2;
    1359             :         size_t buff;
    1360             :         php_stream *stream;
    1361             : 
    1362           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &arg1, &arg2) == FAILURE) {
    1363           0 :                 RETURN_FALSE;
    1364             :         }
    1365             : 
    1366           6 :         php_stream_from_zval(stream, arg1);
    1367             : 
    1368           6 :         buff = arg2;
    1369             : 
    1370             :         /* if buff is 0 then set to non-buffered */
    1371           6 :         if (buff == 0) {
    1372           2 :                 ret = php_stream_set_option(stream, PHP_STREAM_OPTION_WRITE_BUFFER, PHP_STREAM_BUFFER_NONE, NULL);
    1373             :         } else {
    1374           4 :                 ret = php_stream_set_option(stream, PHP_STREAM_OPTION_WRITE_BUFFER, PHP_STREAM_BUFFER_FULL, &buff);
    1375             :         }
    1376             : 
    1377           6 :         RETURN_LONG(ret == 0 ? 0 : EOF);
    1378             : }
    1379             : /* }}} */
    1380             : 
    1381             : /* {{{ proto int stream_set_chunk_size(resource fp, int chunk_size)
    1382             :    Set the stream chunk size */
    1383           7 : PHP_FUNCTION(stream_set_chunk_size)
    1384             : {
    1385             :         int                     ret;
    1386             :         zend_long               csize;
    1387             :         zval            *zstream;
    1388             :         php_stream      *stream;
    1389             : 
    1390           7 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zstream, &csize) == FAILURE) {
    1391           1 :                 RETURN_FALSE;
    1392             :         }
    1393             : 
    1394           6 :         if (csize <= 0) {
    1395           2 :                 php_error_docref(NULL, E_WARNING, "The chunk size must be a positive integer, given " ZEND_LONG_FMT, csize);
    1396           2 :                 RETURN_FALSE;
    1397             :         }
    1398             :         /* stream.chunk_size is actually a size_t, but php_stream_set_option
    1399             :          * can only use an int to accept the new value and return the old one.
    1400             :          * In any case, values larger than INT_MAX for a chunk size make no sense.
    1401             :          */
    1402           4 :         if (csize > INT_MAX) {
    1403           0 :                 php_error_docref(NULL, E_WARNING, "The chunk size cannot be larger than %d", INT_MAX);
    1404           0 :                 RETURN_FALSE;
    1405             :         }
    1406             : 
    1407           4 :         php_stream_from_zval(stream, zstream);
    1408             : 
    1409           4 :         ret = php_stream_set_option(stream, PHP_STREAM_OPTION_SET_CHUNK_SIZE, (int)csize, NULL);
    1410             : 
    1411           4 :         RETURN_LONG(ret > 0 ? (zend_long)ret : (zend_long)EOF);
    1412             : }
    1413             : /* }}} */
    1414             : 
    1415             : /* {{{ proto int stream_set_read_buffer(resource fp, int buffer)
    1416             :    Set file read buffer */
    1417           2 : PHP_FUNCTION(stream_set_read_buffer)
    1418             : {
    1419             :         zval *arg1;
    1420             :         int ret;
    1421             :         zend_long arg2;
    1422             :         size_t buff;
    1423             :         php_stream *stream;
    1424             : 
    1425           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &arg1, &arg2) == FAILURE) {
    1426           0 :                 RETURN_FALSE;
    1427             :         }
    1428             : 
    1429           2 :         php_stream_from_zval(stream, arg1);
    1430             : 
    1431           2 :         buff = arg2;
    1432             : 
    1433             :         /* if buff is 0 then set to non-buffered */
    1434           2 :         if (buff == 0) {
    1435           2 :                 ret = php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER, PHP_STREAM_BUFFER_NONE, NULL);
    1436             :         } else {
    1437           0 :                 ret = php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER, PHP_STREAM_BUFFER_FULL, &buff);
    1438             :         }
    1439             : 
    1440           2 :         RETURN_LONG(ret == 0 ? 0 : EOF);
    1441             : }
    1442             : /* }}} */
    1443             : 
    1444             : /* {{{ proto int stream_socket_enable_crypto(resource stream, bool enable [, int cryptokind [, resource sessionstream]])
    1445             :    Enable or disable a specific kind of crypto on the stream */
    1446          14 : PHP_FUNCTION(stream_socket_enable_crypto)
    1447             : {
    1448          14 :         zend_long cryptokind = 0;
    1449          14 :         zval *zstream, *zsessstream = NULL;
    1450          14 :         php_stream *stream, *sessstream = NULL;
    1451             :         zend_bool enable, cryptokindnull;
    1452             :         int ret;
    1453             : 
    1454          14 :         ZEND_PARSE_PARAMETERS_START(2, 4)
    1455          42 :                 Z_PARAM_RESOURCE(zstream)
    1456          42 :                 Z_PARAM_BOOL(enable)
    1457          14 :                 Z_PARAM_OPTIONAL
    1458          38 :                 Z_PARAM_LONG_EX(cryptokind, cryptokindnull, 1, 0)
    1459          12 :                 Z_PARAM_RESOURCE(zsessstream)
    1460          14 :         ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
    1461             : 
    1462          14 :         php_stream_from_zval(stream, zstream);
    1463             : 
    1464          14 :         if (enable) {
    1465          13 :                 if (ZEND_NUM_ARGS() < 3 || cryptokindnull) {
    1466             :                         zval *val;
    1467             : 
    1468           1 :                         if (!GET_CTX_OPT(stream, "ssl", "crypto_method", val)) {
    1469           1 :                                 php_error_docref(NULL, E_WARNING, "When enabling encryption you must specify the crypto type");
    1470           1 :                                 RETURN_FALSE;
    1471             :                         }
    1472             : 
    1473           0 :                         cryptokind = Z_LVAL_P(val);
    1474             :                 }
    1475             : 
    1476          12 :                 if (zsessstream) {
    1477           0 :                         php_stream_from_zval(sessstream, zsessstream);
    1478             :                 }
    1479             : 
    1480          12 :                 if (php_stream_xport_crypto_setup(stream, cryptokind, sessstream) < 0) {
    1481           7 :                         RETURN_FALSE;
    1482             :                 }
    1483             :         }
    1484             : 
    1485           6 :         ret = php_stream_xport_crypto_enable(stream, enable);
    1486           6 :         switch (ret) {
    1487             :                 case -1:
    1488           2 :                         RETURN_FALSE;
    1489             : 
    1490             :                 case 0:
    1491           0 :                         RETURN_LONG(0);
    1492             : 
    1493             :                 default:
    1494           4 :                         RETURN_TRUE;
    1495             :         }
    1496             : }
    1497             : /* }}} */
    1498             : 
    1499             : /* {{{ proto string stream_resolve_include_path(string filename)
    1500             : Determine what file will be opened by calls to fopen() with a relative path */
    1501           4 : PHP_FUNCTION(stream_resolve_include_path)
    1502             : {
    1503             :         char *filename;
    1504             :         size_t filename_len;
    1505             :         zend_string *resolved_path;
    1506             : 
    1507           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
    1508           1 :                 return;
    1509             :         }
    1510             : 
    1511           3 :         resolved_path = zend_resolve_path(filename, (int)filename_len);
    1512             : 
    1513           3 :         if (resolved_path) {
    1514           2 :                 RETURN_STR(resolved_path);
    1515             :         }
    1516           1 :         RETURN_FALSE;
    1517             : }
    1518             : /* }}} */
    1519             : 
    1520             : /* {{{ proto bool stream_is_local(resource stream|string url) U
    1521             : */
    1522           3 : PHP_FUNCTION(stream_is_local)
    1523             : {
    1524             :         zval *zstream;
    1525           3 :         php_stream *stream = NULL;
    1526           3 :         php_stream_wrapper *wrapper = NULL;
    1527             : 
    1528           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zstream) == FAILURE) {
    1529           0 :                 RETURN_FALSE;
    1530             :         }
    1531             : 
    1532           6 :         if (Z_TYPE_P(zstream) == IS_RESOURCE) {
    1533           1 :                 php_stream_from_zval(stream, zstream);
    1534           1 :                 if (stream == NULL) {
    1535           0 :                         RETURN_FALSE;
    1536             :                 }
    1537           1 :                 wrapper = stream->wrapper;
    1538             :         } else {
    1539           5 :                 convert_to_string_ex(zstream);
    1540             : 
    1541           2 :                 wrapper = php_stream_locate_url_wrapper(Z_STRVAL_P(zstream), NULL, 0);
    1542             :         }
    1543             : 
    1544           3 :         if (!wrapper) {
    1545           0 :                 RETURN_FALSE;
    1546             :         }
    1547             : 
    1548           3 :         RETURN_BOOL(wrapper->is_url==0);
    1549             : }
    1550             : /* }}} */
    1551             : 
    1552             : /* {{{ proto bool stream_supports_lock(resource stream)
    1553             :    Tells whether the stream supports locking through flock(). */
    1554           5 : PHP_FUNCTION(stream_supports_lock)
    1555             : {
    1556             :         php_stream *stream;
    1557             :         zval *zsrc;
    1558             : 
    1559           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zsrc) == FAILURE) {
    1560           0 :                 RETURN_FALSE;
    1561             :         }
    1562             : 
    1563           5 :         php_stream_from_zval(stream, zsrc);
    1564             : 
    1565           4 :         if (!php_stream_supports_lock(stream)) {
    1566           2 :                 RETURN_FALSE;
    1567             :         }
    1568             : 
    1569           2 :         RETURN_TRUE;
    1570             : }
    1571             : 
    1572             : #ifdef HAVE_SHUTDOWN
    1573             : /* {{{ proto int stream_socket_shutdown(resource stream, int how)
    1574             :         causes all or part of a full-duplex connection on the socket associated
    1575             :         with stream to be shut down.  If how is SHUT_RD,  further receptions will
    1576             :         be disallowed. If how is SHUT_WR, further transmissions will be disallowed.
    1577             :         If how is SHUT_RDWR,  further  receptions and transmissions will be
    1578             :         disallowed. */
    1579           4 : PHP_FUNCTION(stream_socket_shutdown)
    1580             : {
    1581             :         zend_long how;
    1582             :         zval *zstream;
    1583             :         php_stream *stream;
    1584             : 
    1585           4 :         ZEND_PARSE_PARAMETERS_START(2, 2)
    1586          12 :                 Z_PARAM_RESOURCE(zstream)
    1587          12 :                 Z_PARAM_LONG(how)
    1588           4 :         ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
    1589             : 
    1590           9 :         if (how != STREAM_SHUT_RD &&
    1591           4 :             how != STREAM_SHUT_WR &&
    1592           1 :             how != STREAM_SHUT_RDWR) {
    1593           0 :                 php_error_docref(NULL, E_WARNING, "Second parameter $how needs to be one of STREAM_SHUT_RD, STREAM_SHUT_WR or STREAM_SHUT_RDWR");
    1594           0 :                 RETURN_FALSE;
    1595             :         }
    1596             : 
    1597           4 :         php_stream_from_zval(stream, zstream);
    1598             : 
    1599           4 :         RETURN_BOOL(php_stream_xport_shutdown(stream, (stream_shutdown_t)how) == 0);
    1600             : }
    1601             : /* }}} */
    1602             : #endif
    1603             : 
    1604             : /*
    1605             :  * Local variables:
    1606             :  * tab-width: 4
    1607             :  * c-basic-offset: 4
    1608             :  * End:
    1609             :  * vim600: noet sw=4 ts=4 fdm=marker
    1610             :  * vim<600: noet sw=4 ts=4
    1611             :  */
    1612             : 

Generated by: LCOV version 1.10

Generated at Tue, 27 Sep 2016 10:26:08 +0000 (47 hours ago)

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