summaryrefslogtreecommitdiff
path: root/ext/standard/streamsfuncs.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/standard/streamsfuncs.c')
-rw-r--r--ext/standard/streamsfuncs.c372
1 files changed, 164 insertions, 208 deletions
diff --git a/ext/standard/streamsfuncs.c b/ext/standard/streamsfuncs.c
index 8d42a51af6..81afb2228e 100644
--- a/ext/standard/streamsfuncs.c
+++ b/ext/standard/streamsfuncs.c
@@ -75,8 +75,8 @@ PHP_FUNCTION(stream_socket_pair)
php_stream_auto_cleanup(s1);
php_stream_auto_cleanup(s2);
- add_next_index_resource(return_value, php_stream_get_resource_id(s1));
- add_next_index_resource(return_value, php_stream_get_resource_id(s2));
+ add_next_index_resource(return_value, s1->res);
+ add_next_index_resource(return_value, s2->res);
}
/* }}} */
#endif
@@ -120,12 +120,14 @@ PHP_FUNCTION(stream_socket_client)
tv.tv_usec = conv % 1000000;
#endif
if (zerrno) {
+ zerrno = Z_REFVAL_P(zerrno);
zval_dtor(zerrno);
ZVAL_LONG(zerrno, 0);
}
if (zerrstr) {
+ zerrstr = Z_REFVAL_P(zerrstr);
zval_dtor(zerrstr);
- ZVAL_STRING(zerrstr, "", 1);
+ ZVAL_EMPTY_STRING(zerrstr);
}
stream = php_stream_xport_create(host, host_len, REPORT_ERRORS,
@@ -136,10 +138,10 @@ PHP_FUNCTION(stream_socket_client)
if (stream == NULL) {
/* host might contain binary characters */
- char *quoted_host = php_addslashes(host, host_len, NULL, 0 TSRMLS_CC);
+ zend_string *quoted_host = php_addslashes(host, host_len, 0 TSRMLS_CC);
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s (%s)", quoted_host, errstr == NULL ? "Unknown error" : errstr);
- efree(quoted_host);
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to connect to %s (%s)", quoted_host->val, errstr == NULL ? "Unknown error" : errstr);
+ STR_RELEASE(quoted_host);
}
if (hashkey) {
@@ -154,7 +156,9 @@ PHP_FUNCTION(stream_socket_client)
if (zerrstr && errstr) {
/* no need to dup; we need to efree buf anyway */
zval_dtor(zerrstr);
- ZVAL_STRING(zerrstr, errstr, 0);
+ // TODO: avoid reallocation ???
+ ZVAL_STRING(zerrstr, errstr);
+ efree(errstr);
} else if (errstr) {
efree(errstr);
}
@@ -192,7 +196,7 @@ PHP_FUNCTION(stream_socket_server)
context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);
if (context) {
- zend_list_addref(context->rsrc_id);
+ GC_REFCOUNT(context->res)++;
}
if (zerrno) {
@@ -201,7 +205,7 @@ PHP_FUNCTION(stream_socket_server)
}
if (zerrstr) {
zval_dtor(zerrstr);
- ZVAL_STRING(zerrstr, "", 1);
+ ZVAL_EMPTY_STRING(zerrstr);
}
stream = php_stream_xport_create(host, host_len, REPORT_ERRORS,
@@ -220,7 +224,9 @@ PHP_FUNCTION(stream_socket_server)
if (zerrstr && errstr) {
/* no need to dup; we need to efree buf anyway */
zval_dtor(zerrstr);
- ZVAL_STRING(zerrstr, errstr, 0);
+ // TODO: avoid reallocation ???
+ ZVAL_STRING(zerrstr, errstr);
+ efree(errstr);
} else if (errstr) {
efree(errstr);
}
@@ -254,7 +260,7 @@ PHP_FUNCTION(stream_socket_accept)
RETURN_FALSE;
}
- php_stream_from_zval(stream, &zstream);
+ php_stream_from_zval(stream, zstream);
/* prepare the timeout value for use */
conv = (php_timeout_ull) (timeout * 1000000.0);
@@ -278,7 +284,9 @@ PHP_FUNCTION(stream_socket_accept)
TSRMLS_CC) && clistream) {
if (peername) {
- ZVAL_STRINGL(zpeername, peername, peername_len, 0);
+ // TODO: avoid reallocation ???
+ ZVAL_STRINGL(zpeername, peername, peername_len);
+ efree(peername);
}
php_stream_to_zval(clistream, return_value);
} else {
@@ -306,7 +314,7 @@ PHP_FUNCTION(stream_socket_get_name)
RETURN_FALSE;
}
- php_stream_from_zval(stream, &zstream);
+ php_stream_from_zval(stream, zstream);
if (0 != php_stream_xport_get_name(stream, want_peer,
&name,
@@ -316,7 +324,9 @@ PHP_FUNCTION(stream_socket_get_name)
RETURN_FALSE;
}
- RETURN_STRINGL(name, name_len, 0);
+ // TODO: avoid reallocation ???
+ RETVAL_STRINGL(name, name_len);
+ efree(name);
}
/* }}} */
@@ -335,7 +345,7 @@ PHP_FUNCTION(stream_socket_sendto)
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|ls", &zstream, &data, &datalen, &flags, &target_addr, &target_addr_len) == FAILURE) {
RETURN_FALSE;
}
- php_stream_from_zval(stream, &zstream);
+ php_stream_from_zval(stream, zstream);
if (target_addr_len) {
/* parse the address */
@@ -358,7 +368,7 @@ PHP_FUNCTION(stream_socket_recvfrom)
char *remote_addr = NULL;
int remote_addr_len;
long to_read = 0;
- char *read_buf;
+ zend_string *read_buf;
long flags = 0;
int recvd;
@@ -366,7 +376,7 @@ PHP_FUNCTION(stream_socket_recvfrom)
RETURN_FALSE;
}
- php_stream_from_zval(stream, &zstream);
+ php_stream_from_zval(stream, zstream);
if (zremote) {
zval_dtor(zremote);
@@ -378,23 +388,25 @@ PHP_FUNCTION(stream_socket_recvfrom)
RETURN_FALSE;
}
- read_buf = safe_emalloc(1, to_read, 1);
+ read_buf = STR_ALLOC(to_read, 0);
- recvd = php_stream_xport_recvfrom(stream, read_buf, to_read, flags, NULL, NULL,
+ recvd = php_stream_xport_recvfrom(stream, read_buf->val, to_read, flags, NULL, NULL,
zremote ? &remote_addr : NULL,
zremote ? &remote_addr_len : NULL
TSRMLS_CC);
if (recvd >= 0) {
if (zremote) {
- ZVAL_STRINGL(zremote, remote_addr, remote_addr_len, 0);
+ // TODO: avoid reallocation ???
+ ZVAL_STRINGL(zremote, remote_addr, remote_addr_len);
+ efree(remote_addr);
}
- read_buf[recvd] = '\0';
-
- RETURN_STRINGL(read_buf, recvd, 0);
+ read_buf->val[recvd] = '\0';
+ read_buf->len = recvd;
+ RETURN_NEW_STR(read_buf);
}
- efree(read_buf);
+ STR_FREE(read_buf);
RETURN_FALSE;
}
/* }}} */
@@ -407,14 +419,13 @@ PHP_FUNCTION(stream_get_contents)
zval *zsrc;
long maxlen = PHP_STREAM_COPY_ALL,
desiredpos = -1L;
- int len;
- char *contents = NULL;
+ zend_string *contents;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|ll", &zsrc, &maxlen, &desiredpos) == FAILURE) {
RETURN_FALSE;
}
- php_stream_from_zval(stream, &zsrc);
+ php_stream_from_zval(stream, zsrc);
if (desiredpos >= 0) {
int seek_res = 0;
@@ -436,12 +447,10 @@ PHP_FUNCTION(stream_get_contents)
}
}
- len = php_stream_copy_to_mem(stream, &contents, maxlen, 0);
-
- if (contents) {
- RETVAL_STRINGL(contents, len, 0);
+ if ((contents = php_stream_copy_to_mem(stream, maxlen, 0))) {
+ RETURN_STR(contents);
} else {
- RETVAL_EMPTY_STRING();
+ RETURN_EMPTY_STRING();
}
}
/* }}} */
@@ -460,8 +469,8 @@ PHP_FUNCTION(stream_copy_to_stream)
RETURN_FALSE;
}
- php_stream_from_zval(src, &zsrc);
- php_stream_from_zval(dest, &zdest);
+ php_stream_from_zval(src, zsrc);
+ php_stream_from_zval(dest, zdest);
if (pos > 0 && php_stream_seek(src, pos, SEEK_SET) < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to seek to position %ld in the stream", pos);
@@ -483,27 +492,24 @@ PHP_FUNCTION(stream_get_meta_data)
{
zval *arg1;
php_stream *stream;
- zval *newval;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &arg1) == FAILURE) {
return;
}
- php_stream_from_zval(stream, &arg1);
+ php_stream_from_zval(stream, arg1);
array_init(return_value);
- if (stream->wrapperdata) {
- MAKE_STD_ZVAL(newval);
- MAKE_COPY_ZVAL(&stream->wrapperdata, newval);
-
- add_assoc_zval(return_value, "wrapper_data", newval);
+ if (!Z_ISUNDEF(stream->wrapperdata)) {
+ Z_ADDREF_P(&stream->wrapperdata);
+ add_assoc_zval(return_value, "wrapper_data", &stream->wrapperdata);
}
if (stream->wrapper) {
- add_assoc_string(return_value, "wrapper_type", (char *)stream->wrapper->wops->label, 1);
+ add_assoc_string(return_value, "wrapper_type", (char *)stream->wrapper->wops->label);
}
- add_assoc_string(return_value, "stream_type", (char *)stream->ops->label, 1);
+ add_assoc_string(return_value, "stream_type", (char *)stream->ops->label);
- add_assoc_string(return_value, "mode", stream->mode, 1);
+ add_assoc_string(return_value, "mode", stream->mode);
#if 0 /* TODO: needs updating for new filter API */
if (stream->filterhead) {
@@ -513,7 +519,7 @@ PHP_FUNCTION(stream_get_meta_data)
array_init(newval);
for (filter = stream->filterhead; filter != NULL; filter = filter->next) {
- add_next_index_string(newval, (char *)filter->fops->label, 1);
+ add_next_index_string(newval, (char *)filter->fops->label);
}
add_assoc_zval(return_value, "filters", newval);
@@ -524,7 +530,7 @@ PHP_FUNCTION(stream_get_meta_data)
add_assoc_bool(return_value, "seekable", (stream->ops->seek) && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0);
if (stream->orig_path) {
- add_assoc_string(return_value, "uri", stream->orig_path, 1);
+ add_assoc_string(return_value, "uri", stream->orig_path);
}
if (!php_stream_populate_meta_data(stream, return_value)) {
@@ -541,24 +547,17 @@ PHP_FUNCTION(stream_get_meta_data)
PHP_FUNCTION(stream_get_transports)
{
HashTable *stream_xport_hash;
- char *stream_xport;
- uint stream_xport_len;
- ulong num_key;
+ zend_string *stream_xport;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
if ((stream_xport_hash = php_stream_xport_get_hash())) {
- HashPosition pos;
array_init(return_value);
- zend_hash_internal_pointer_reset_ex(stream_xport_hash, &pos);
- while (zend_hash_get_current_key_ex(stream_xport_hash,
- &stream_xport, &stream_xport_len,
- &num_key, 0, &pos) == HASH_KEY_IS_STRING) {
- add_next_index_stringl(return_value, stream_xport, stream_xport_len - 1, 1);
- zend_hash_move_forward_ex(stream_xport_hash, &pos);
- }
+ ZEND_HASH_FOREACH_STR_KEY(stream_xport_hash, stream_xport) {
+ add_next_index_str(return_value, STR_COPY(stream_xport));
+ } ZEND_HASH_FOREACH_END();
} else {
RETURN_FALSE;
}
@@ -570,25 +569,19 @@ PHP_FUNCTION(stream_get_transports)
PHP_FUNCTION(stream_get_wrappers)
{
HashTable *url_stream_wrappers_hash;
- char *stream_protocol;
- int key_flags;
- uint stream_protocol_len = 0;
- ulong num_key;
+ zend_string *stream_protocol;
if (zend_parse_parameters_none() == FAILURE) {
return;
}
if ((url_stream_wrappers_hash = php_stream_get_url_stream_wrappers_hash())) {
- HashPosition pos;
array_init(return_value);
- for (zend_hash_internal_pointer_reset_ex(url_stream_wrappers_hash, &pos);
- (key_flags = zend_hash_get_current_key_ex(url_stream_wrappers_hash, &stream_protocol, &stream_protocol_len, &num_key, 0, &pos)) != HASH_KEY_NON_EXISTENT;
- zend_hash_move_forward_ex(url_stream_wrappers_hash, &pos)) {
- if (key_flags == HASH_KEY_IS_STRING) {
- add_next_index_stringl(return_value, stream_protocol, stream_protocol_len - 1, 1);
- }
- }
+ ZEND_HASH_FOREACH_STR_KEY(url_stream_wrappers_hash, stream_protocol) {
+ if (stream_protocol) {
+ add_next_index_str(return_value, STR_COPY(stream_protocol));
+ }
+ } ZEND_HASH_FOREACH_END();
} else {
RETURN_FALSE;
}
@@ -599,17 +592,18 @@ PHP_FUNCTION(stream_get_wrappers)
/* {{{ stream_select related functions */
static int stream_array_to_fd_set(zval *stream_array, fd_set *fds, php_socket_t *max_fd TSRMLS_DC)
{
- zval **elem;
+ zval *elem;
php_stream *stream;
int cnt = 0;
if (Z_TYPE_P(stream_array) != IS_ARRAY) {
return 0;
}
- for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(stream_array));
- zend_hash_get_current_data(Z_ARRVAL_P(stream_array), (void **) &elem) == SUCCESS;
- zend_hash_move_forward(Z_ARRVAL_P(stream_array))) {
+ ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(stream_array), elem) {
+ /* Temporary int fd is needed for the STREAM data type on windows, passing this_fd directly to php_stream_cast()
+ would eventually bring a wrong result on x64. php_stream_cast() casts to int internally, and this will leave
+ the higher bits of a SOCKET variable uninitialized on systems with little endian. */
php_socket_t this_fd;
php_stream_from_zval_no_verify(stream, elem);
@@ -630,41 +624,27 @@ static int stream_array_to_fd_set(zval *stream_array, fd_set *fds, php_socket_t
}
cnt++;
}
- }
+ } ZEND_HASH_FOREACH_END();
return cnt ? 1 : 0;
}
static int stream_array_from_fd_set(zval *stream_array, fd_set *fds TSRMLS_DC)
{
- zval **elem, **dest_elem;
+ zval *elem, *dest_elem, new_array;
php_stream *stream;
- HashTable *new_hash;
int ret = 0;
+ zend_string *key;
+ ulong num_ind;
if (Z_TYPE_P(stream_array) != IS_ARRAY) {
return 0;
}
- ALLOC_HASHTABLE(new_hash);
- zend_hash_init(new_hash, zend_hash_num_elements(Z_ARRVAL_P(stream_array)), NULL, ZVAL_PTR_DTOR, 0);
+ ZVAL_NEW_ARR(&new_array);
+ zend_hash_init(Z_ARRVAL(new_array), zend_hash_num_elements(Z_ARRVAL_P(stream_array)), NULL, ZVAL_PTR_DTOR, 0);
- for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(stream_array));
- zend_hash_has_more_elements(Z_ARRVAL_P(stream_array)) == SUCCESS;
- zend_hash_move_forward(Z_ARRVAL_P(stream_array))) {
-
- int type;
- char *key;
- uint key_len;
- ulong num_ind;
+ ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(stream_array), num_ind, key, elem) {
php_socket_t this_fd;
-
- type = zend_hash_get_current_key_ex(Z_ARRVAL_P(stream_array),
- &key, &key_len, &num_ind, 0, NULL);
- if (type == HASH_KEY_NON_EXISTENT ||
- zend_hash_get_current_data(Z_ARRVAL_P(stream_array), (void **) &elem) == FAILURE) {
- continue; /* should not happen */
- }
-
php_stream_from_zval_no_verify(stream, elem);
if (stream == NULL) {
continue;
@@ -676,10 +656,10 @@ static int stream_array_from_fd_set(zval *stream_array, fd_set *fds TSRMLS_DC)
*/
if (SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD_FOR_SELECT | PHP_STREAM_CAST_INTERNAL, (void*)&this_fd, 1) && this_fd != SOCK_ERR) {
if (PHP_SAFE_FD_ISSET(this_fd, fds)) {
- if (type == HASH_KEY_IS_LONG) {
- zend_hash_index_update(new_hash, num_ind, (void *)elem, sizeof(zval *), (void **)&dest_elem);
- } else { /* HASH_KEY_IS_STRING */
- zend_hash_update(new_hash, key, key_len, (void *)elem, sizeof(zval *), (void **)&dest_elem);
+ if (!key) {
+ dest_elem = zend_hash_index_update(Z_ARRVAL(new_array), num_ind, elem);
+ } else {
+ dest_elem = zend_hash_update(Z_ARRVAL(new_array), key, elem);
}
if (dest_elem) {
@@ -689,35 +669,31 @@ static int stream_array_from_fd_set(zval *stream_array, fd_set *fds TSRMLS_DC)
continue;
}
}
- }
+ } ZEND_HASH_FOREACH_END();
/* destroy old array and add new one */
zend_hash_destroy(Z_ARRVAL_P(stream_array));
- efree(Z_ARRVAL_P(stream_array));
+ GC_REMOVE_FROM_BUFFER(Z_ARR_P(stream_array));
+ efree(Z_ARR_P(stream_array));
- zend_hash_internal_pointer_reset(new_hash);
- Z_ARRVAL_P(stream_array) = new_hash;
+ Z_ARR_P(stream_array) = Z_ARR(new_array);
return ret;
}
static int stream_array_emulate_read_fd_set(zval *stream_array TSRMLS_DC)
{
- zval **elem, **dest_elem;
+ zval *elem, *dest_elem, new_array;
php_stream *stream;
- HashTable *new_hash;
int ret = 0;
if (Z_TYPE_P(stream_array) != IS_ARRAY) {
return 0;
}
- ALLOC_HASHTABLE(new_hash);
- zend_hash_init(new_hash, zend_hash_num_elements(Z_ARRVAL_P(stream_array)), NULL, ZVAL_PTR_DTOR, 0);
-
- for (zend_hash_internal_pointer_reset(Z_ARRVAL_P(stream_array));
- zend_hash_get_current_data(Z_ARRVAL_P(stream_array), (void **) &elem) == SUCCESS;
- zend_hash_move_forward(Z_ARRVAL_P(stream_array))) {
+ ZVAL_NEW_ARR(&new_array);
+ zend_hash_init(Z_ARRVAL(new_array), zend_hash_num_elements(Z_ARRVAL_P(stream_array)), NULL, ZVAL_PTR_DTOR, 0);
+ ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(stream_array), elem) {
php_stream_from_zval_no_verify(stream, elem);
if (stream == NULL) {
continue;
@@ -729,25 +705,23 @@ static int stream_array_emulate_read_fd_set(zval *stream_array TSRMLS_DC)
* This branch of code also allows blocking streams with buffered data to
* operate correctly in stream_select.
* */
- zend_hash_next_index_insert(new_hash, (void *)elem, sizeof(zval *), (void **)&dest_elem);
+ dest_elem = zend_hash_next_index_insert(Z_ARRVAL(new_array), elem);
if (dest_elem) {
zval_add_ref(dest_elem);
}
ret++;
continue;
}
- }
+ } ZEND_HASH_FOREACH_END();
if (ret > 0) {
/* destroy old array and add new one */
zend_hash_destroy(Z_ARRVAL_P(stream_array));
- efree(Z_ARRVAL_P(stream_array));
-
- zend_hash_internal_pointer_reset(new_hash);
- Z_ARRVAL_P(stream_array) = new_hash;
+ efree(Z_ARR_P(stream_array));
+ Z_ARR_P(stream_array) = Z_ARR(new_array);
} else {
- zend_hash_destroy(new_hash);
- FREE_HASHTABLE(new_hash);
+ zend_hash_destroy(Z_ARRVAL(new_array));
+ efree(Z_ARR(new_array));
}
return ret;
@@ -758,7 +732,7 @@ static int stream_array_emulate_read_fd_set(zval *stream_array TSRMLS_DC)
Runs the select() system call on the sets of streams with a timeout specified by tv_sec and tv_usec */
PHP_FUNCTION(stream_select)
{
- zval *r_array, *w_array, *e_array, **sec = NULL;
+ zval *r_array, *w_array, *e_array, *sec = NULL;
struct timeval tv;
struct timeval *tv_p = NULL;
fd_set rfds, wfds, efds;
@@ -767,7 +741,7 @@ PHP_FUNCTION(stream_select)
long usec = 0;
int set_count, max_set_count = 0;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!a!a!Z!|l", &r_array, &w_array, &e_array, &sec, &usec) == FAILURE)
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a!a!a!z!|l", &r_array, &w_array, &e_array, &sec, &usec) == FAILURE)
return;
FD_ZERO(&rfds);
@@ -806,7 +780,7 @@ PHP_FUNCTION(stream_select)
if (sec != NULL) {
convert_to_long_ex(sec);
- if (Z_LVAL_PP(sec) < 0) {
+ if (Z_LVAL_P(sec) < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The seconds parameter must be greater than 0");
RETURN_FALSE;
} else if (usec < 0) {
@@ -816,10 +790,10 @@ PHP_FUNCTION(stream_select)
/* Solaris + BSD do not like microsecond values which are >= 1 sec */
if (usec > 999999) {
- tv.tv_sec = Z_LVAL_PP(sec) + (usec / 1000000);
+ tv.tv_sec = Z_LVAL_P(sec) + (usec / 1000000);
tv.tv_usec = usec % 1000000;
} else {
- tv.tv_sec = Z_LVAL_PP(sec);
+ tv.tv_sec = Z_LVAL_P(sec);
tv.tv_usec = usec;
}
@@ -863,78 +837,58 @@ PHP_FUNCTION(stream_select)
static void user_space_stream_notifier(php_stream_context *context, int notifycode, int severity,
char *xmsg, int xcode, size_t bytes_sofar, size_t bytes_max, void * ptr TSRMLS_DC)
{
- zval *callback = (zval*)context->notifier->ptr;
- zval *retval = NULL;
+ zval *callback = &context->notifier->ptr;
+ zval retval;
zval zvs[6];
- zval *ps[6];
- zval **ptps[6];
int i;
- for (i = 0; i < 6; i++) {
- INIT_ZVAL(zvs[i]);
- ps[i] = &zvs[i];
- ptps[i] = &ps[i];
- MAKE_STD_ZVAL(ps[i]);
- }
-
- ZVAL_LONG(ps[0], notifycode);
- ZVAL_LONG(ps[1], severity);
+ ZVAL_LONG(&zvs[0], notifycode);
+ ZVAL_LONG(&zvs[1], severity);
if (xmsg) {
- ZVAL_STRING(ps[2], xmsg, 1);
+ ZVAL_STRING(&zvs[2], xmsg);
} else {
- ZVAL_NULL(ps[2]);
+ ZVAL_NULL(&zvs[2]);
}
- ZVAL_LONG(ps[3], xcode);
- ZVAL_LONG(ps[4], bytes_sofar);
- ZVAL_LONG(ps[5], bytes_max);
+ ZVAL_LONG(&zvs[3], xcode);
+ ZVAL_LONG(&zvs[4], bytes_sofar);
+ ZVAL_LONG(&zvs[5], bytes_max);
- if (FAILURE == call_user_function_ex(EG(function_table), NULL, callback, &retval, 6, ptps, 0, NULL TSRMLS_CC)) {
+ if (FAILURE == call_user_function_ex(EG(function_table), NULL, callback, &retval, 6, zvs, 0, NULL TSRMLS_CC)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "failed to call user notifier");
}
for (i = 0; i < 6; i++) {
- zval_ptr_dtor(&ps[i]);
- }
- if (retval) {
- zval_ptr_dtor(&retval);
+ zval_ptr_dtor(&zvs[i]);
}
+ zval_ptr_dtor(&retval);
}
static void user_space_stream_notifier_dtor(php_stream_notifier *notifier)
{
- if (notifier && notifier->ptr) {
- zval_ptr_dtor((zval **)&(notifier->ptr));
- notifier->ptr = NULL;
+ if (notifier && Z_TYPE(notifier->ptr) != IS_UNDEF) {
+ zval_ptr_dtor(&notifier->ptr);
+ ZVAL_UNDEF(&notifier->ptr);
}
}
static int parse_context_options(php_stream_context *context, zval *options TSRMLS_DC)
{
- HashPosition pos, opos;
- zval **wval, **oval;
- char *wkey, *okey;
- uint wkey_len, okey_len;
+ zval *wval, *oval;
+ zend_string *wkey, *okey;
int ret = SUCCESS;
- ulong num_key;
- zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(options), &pos);
- while (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(options), (void**)&wval, &pos)) {
- if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(Z_ARRVAL_P(options), &wkey, &wkey_len, &num_key, 0, &pos)
- && Z_TYPE_PP(wval) == IS_ARRAY) {
+ ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(options), wkey, wval) {
+ if (wkey && Z_TYPE_P(wval) == IS_ARRAY) {
- zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(wval), &opos);
- while (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(wval), (void**)&oval, &opos)) {
-
- if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(Z_ARRVAL_PP(wval), &okey, &okey_len, &num_key, 0, &opos)) {
- php_stream_context_set_option(context, wkey, okey, *oval);
+ ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(wval), okey, oval) {
+ if (okey) {
+ php_stream_context_set_option(context, wkey->val, okey->val, oval);
}
- zend_hash_move_forward_ex(Z_ARRVAL_PP(wval), &opos);
- }
+ } ZEND_HASH_FOREACH_END();
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "options should have the form [\"wrappername\"][\"optionname\"] = $value");
}
- zend_hash_move_forward_ex(Z_ARRVAL_P(options), &pos);
- }
+ } ZEND_HASH_FOREACH_END();
return ret;
}
@@ -942,9 +896,9 @@ static int parse_context_options(php_stream_context *context, zval *options TSRM
static int parse_context_params(php_stream_context *context, zval *params TSRMLS_DC)
{
int ret = SUCCESS;
- zval **tmp;
+ zval *tmp;
- if (SUCCESS == zend_hash_find(Z_ARRVAL_P(params), "notification", sizeof("notification"), (void**)&tmp)) {
+ if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(params), "notification", sizeof("notification")-1))) {
if (context->notifier) {
php_stream_notification_free(context->notifier);
@@ -953,13 +907,12 @@ static int parse_context_params(php_stream_context *context, zval *params TSRMLS
context->notifier = php_stream_notification_alloc();
context->notifier->func = user_space_stream_notifier;
- context->notifier->ptr = *tmp;
- Z_ADDREF_P(*tmp);
+ ZVAL_COPY(&context->notifier->ptr, tmp);
context->notifier->dtor = user_space_stream_notifier_dtor;
}
- if (SUCCESS == zend_hash_find(Z_ARRVAL_P(params), "options", sizeof("options"), (void**)&tmp)) {
- if (Z_TYPE_PP(tmp) == IS_ARRAY) {
- parse_context_options(context, *tmp TSRMLS_CC);
+ if (NULL != (tmp = zend_hash_str_find(Z_ARRVAL_P(params), "options", sizeof("options")-1))) {
+ if (Z_TYPE_P(tmp) == IS_ARRAY) {
+ parse_context_options(context, tmp TSRMLS_CC);
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid stream/context parameter");
}
@@ -975,11 +928,11 @@ static php_stream_context *decode_context_param(zval *contextresource TSRMLS_DC)
{
php_stream_context *context = NULL;
- context = zend_fetch_resource(&contextresource TSRMLS_CC, -1, NULL, NULL, 1, php_le_stream_context(TSRMLS_C));
+ context = zend_fetch_resource(contextresource TSRMLS_CC, -1, NULL, NULL, 1, php_le_stream_context(TSRMLS_C));
if (context == NULL) {
php_stream *stream;
- stream = zend_fetch_resource(&contextresource TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream);
+ stream = zend_fetch_resource(contextresource TSRMLS_CC, -1, NULL, NULL, 2, php_file_le_stream(), php_file_le_pstream);
if (stream) {
context = stream->context;
@@ -1013,7 +966,7 @@ PHP_FUNCTION(stream_context_get_options)
RETURN_FALSE;
}
- RETURN_ZVAL(context->options, 1, 0);
+ RETURN_ZVAL(&context->options, 1, 0);
}
/* }}} */
@@ -1078,7 +1031,7 @@ PHP_FUNCTION(stream_context_set_params)
Get parameters of a file context */
PHP_FUNCTION(stream_context_get_params)
{
- zval *zcontext, *options;
+ zval *zcontext, options;
php_stream_context *context;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zcontext) == FAILURE) {
@@ -1092,13 +1045,12 @@ PHP_FUNCTION(stream_context_get_params)
}
array_init(return_value);
- if (context->notifier && context->notifier->ptr && context->notifier->func == user_space_stream_notifier) {
- add_assoc_zval_ex(return_value, ZEND_STRS("notification"), context->notifier->ptr);
- Z_ADDREF_P(context->notifier->ptr);
+ if (context->notifier && Z_TYPE(context->notifier->ptr) != IS_UNDEF && context->notifier->func == user_space_stream_notifier) {
+ add_assoc_zval_ex(return_value, "notification", sizeof("notification")-1, &context->notifier->ptr);
+ if (Z_REFCOUNTED(context->notifier->ptr)) Z_ADDREF(context->notifier->ptr);
}
- ALLOC_INIT_ZVAL(options);
- ZVAL_ZVAL(options, context->options, 1, 0);
- add_assoc_zval_ex(return_value, ZEND_STRS("options"), options);
+ ZVAL_ZVAL(&options, &context->options, 1, 0);
+ add_assoc_zval_ex(return_value, "options", sizeof("options")-1, &options);
}
/* }}} */
@@ -1169,7 +1121,7 @@ PHP_FUNCTION(stream_context_create)
parse_context_params(context, params TSRMLS_CC);
}
- RETURN_RESOURCE(context->rsrc_id);
+ RETURN_RES(context->res);
}
/* }}} */
@@ -1190,7 +1142,7 @@ static void apply_filter_to_stream(int append, INTERNAL_FUNCTION_PARAMETERS)
RETURN_FALSE;
}
- php_stream_from_zval(stream, &zstream);
+ php_stream_from_zval(stream, zstream);
if ((read_write & PHP_STREAM_FILTER_ALL) == 0) {
/* Chain not specified.
@@ -1241,7 +1193,9 @@ static void apply_filter_to_stream(int append, INTERNAL_FUNCTION_PARAMETERS)
}
if (filter) {
- RETURN_RESOURCE(filter->rsrc_id = ZEND_REGISTER_RESOURCE(NULL, filter, php_file_le_stream_filter()));
+ filter->res = ZEND_REGISTER_RESOURCE(NULL, filter, php_file_le_stream_filter());
+ GC_REFCOUNT(filter->res)++;
+ RETURN_RES(filter->res);
} else {
RETURN_FALSE;
}
@@ -1275,7 +1229,7 @@ PHP_FUNCTION(stream_filter_remove)
RETURN_FALSE;
}
- filter = zend_fetch_resource(&zfilter TSRMLS_CC, -1, NULL, NULL, 1, php_file_le_stream_filter());
+ filter = zend_fetch_resource(zfilter TSRMLS_CC, -1, NULL, NULL, 1, php_file_le_stream_filter());
if (!filter) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid resource given, not a stream filter");
RETURN_FALSE;
@@ -1286,7 +1240,7 @@ PHP_FUNCTION(stream_filter_remove)
RETURN_FALSE;
}
- if (zend_list_delete(Z_LVAL_P(zfilter)) == FAILURE) {
+ if (zend_list_close(Z_RES_P(zfilter)) == FAILURE) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not invalidate filter, not removing");
RETURN_FALSE;
} else {
@@ -1304,8 +1258,7 @@ PHP_FUNCTION(stream_get_line)
int str_len = 0;
long max_length;
zval *zstream;
- char *buf;
- size_t buf_size;
+ zend_string *buf;
php_stream *stream;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|s", &zstream, &max_length, &str, &str_len) == FAILURE) {
@@ -1320,10 +1273,10 @@ PHP_FUNCTION(stream_get_line)
max_length = PHP_SOCK_CHUNK_SIZE;
}
- php_stream_from_zval(stream, &zstream);
+ php_stream_from_zval(stream, zstream);
- if ((buf = php_stream_get_record(stream, max_length, &buf_size, str, str_len TSRMLS_CC))) {
- RETURN_STRINGL(buf, buf_size, 0);
+ if ((buf = php_stream_get_record(stream, max_length, str, str_len TSRMLS_CC))) {
+ RETURN_STR(buf);
} else {
RETURN_FALSE;
}
@@ -1344,7 +1297,7 @@ PHP_FUNCTION(stream_set_blocking)
return;
}
- php_stream_from_zval(stream, &arg1);
+ php_stream_from_zval(stream, arg1);
block = arg2;
@@ -1372,7 +1325,7 @@ PHP_FUNCTION(stream_set_timeout)
return;
}
- php_stream_from_zval(stream, &socket);
+ php_stream_from_zval(stream, socket);
t.tv_sec = seconds;
@@ -1406,7 +1359,7 @@ PHP_FUNCTION(stream_set_write_buffer)
RETURN_FALSE;
}
- php_stream_from_zval(stream, &arg1);
+ php_stream_from_zval(stream, arg1);
buff = arg2;
@@ -1447,7 +1400,7 @@ PHP_FUNCTION(stream_set_chunk_size)
RETURN_FALSE;
}
- php_stream_from_zval(stream, &zstream);
+ php_stream_from_zval(stream, zstream);
ret = php_stream_set_option(stream, PHP_STREAM_OPTION_SET_CHUNK_SIZE, (int)csize, NULL);
@@ -1469,7 +1422,7 @@ PHP_FUNCTION(stream_set_read_buffer)
RETURN_FALSE;
}
- php_stream_from_zval(stream, &arg1);
+ php_stream_from_zval(stream, arg1);
buff = arg2;
@@ -1498,11 +1451,11 @@ PHP_FUNCTION(stream_socket_enable_crypto)
RETURN_FALSE;
}
- php_stream_from_zval(stream, &zstream);
+ php_stream_from_zval(stream, zstream);
if (ZEND_NUM_ARGS() >= 3) {
if (zsessstream) {
- php_stream_from_zval(sessstream, &zsessstream);
+ php_stream_from_zval(sessstream, zsessstream);
}
if (php_stream_xport_crypto_setup(stream, cryptokind, sessstream TSRMLS_CC) < 0) {
@@ -1541,7 +1494,10 @@ PHP_FUNCTION(stream_resolve_include_path)
resolved_path = zend_resolve_path(filename, filename_len TSRMLS_CC);
if (resolved_path) {
- RETURN_STRING(resolved_path, 0);
+ // TODO: avoid reallocation ???
+ RETVAL_STRING(resolved_path);
+ efree(resolved_path);
+ return;
}
RETURN_FALSE;
}
@@ -1551,15 +1507,15 @@ PHP_FUNCTION(stream_resolve_include_path)
*/
PHP_FUNCTION(stream_is_local)
{
- zval **zstream;
+ zval *zstream;
php_stream *stream = NULL;
php_stream_wrapper *wrapper = NULL;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &zstream) == FAILURE) {
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zstream) == FAILURE) {
RETURN_FALSE;
}
- if (Z_TYPE_PP(zstream) == IS_RESOURCE) {
+ if (Z_TYPE_P(zstream) == IS_RESOURCE) {
php_stream_from_zval(stream, zstream);
if (stream == NULL) {
RETURN_FALSE;
@@ -1568,7 +1524,7 @@ PHP_FUNCTION(stream_is_local)
} else {
convert_to_string_ex(zstream);
- wrapper = php_stream_locate_url_wrapper(Z_STRVAL_PP(zstream), NULL, 0 TSRMLS_CC);
+ wrapper = php_stream_locate_url_wrapper(Z_STRVAL_P(zstream), NULL, 0 TSRMLS_CC);
}
if (!wrapper) {
@@ -1590,7 +1546,7 @@ PHP_FUNCTION(stream_supports_lock)
RETURN_FALSE;
}
- php_stream_from_zval(stream, &zsrc);
+ php_stream_from_zval(stream, zsrc);
if (!php_stream_supports_lock(stream)) {
RETURN_FALSE;
@@ -1622,7 +1578,7 @@ PHP_FUNCTION(stream_socket_shutdown)
RETURN_FALSE;
}
- php_stream_from_zval(stream, &zstream);
+ php_stream_from_zval(stream, zstream);
RETURN_BOOL(php_stream_xport_shutdown(stream, (stream_shutdown_t)how TSRMLS_CC) == 0);
}