2001-04-18 01:03:18 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2014-09-20 00:33:14 +08:00
|
|
|
| PHP Version 7 |
|
2001-04-18 01:03:18 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2017-01-02 23:30:12 +08:00
|
|
|
| Copyright (c) 1997-2017 The PHP Group |
|
2001-04-18 01:03:18 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 20:51:34 +08:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
2001-04-18 01:03:18 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2006-01-01 20:51:34 +08:00
|
|
|
| http://www.php.net/license/3_01.txt |
|
2001-04-18 01:03:18 +08:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
2003-02-19 16:40:19 +08:00
|
|
|
| Authors: Wez Furlong <wez@thebrainroom.com> |
|
2002-03-16 05:03:08 +08:00
|
|
|
| Borrowed code from: |
|
2003-02-19 16:40:19 +08:00
|
|
|
| Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
|
|
|
|
| Jim Winstead <jimw@php.net> |
|
2001-04-18 01:03:18 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2002-06-12 02:55:48 +08:00
|
|
|
/* $Id$ */
|
|
|
|
|
2001-04-18 01:03:18 +08:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include "php.h"
|
2002-03-16 05:03:08 +08:00
|
|
|
#include "php_globals.h"
|
|
|
|
#include "php_network.h"
|
|
|
|
#include "php_open_temporary_file.h"
|
2002-03-20 22:21:30 +08:00
|
|
|
#include "ext/standard/file.h"
|
2002-03-21 09:11:52 +08:00
|
|
|
#include "ext/standard/basic_functions.h" /* for BG(mmap_file) (not strictly required) */
|
2003-02-10 04:43:05 +08:00
|
|
|
#include "ext/standard/php_string.h" /* for php_memnstr, used by php_stream_get_record() */
|
2002-09-23 23:21:16 +08:00
|
|
|
#include <stddef.h>
|
2002-08-20 06:59:10 +08:00
|
|
|
#include <fcntl.h>
|
2003-02-17 06:19:28 +08:00
|
|
|
#include "php_streams_int.h"
|
2002-08-20 06:59:10 +08:00
|
|
|
|
2003-02-17 06:19:28 +08:00
|
|
|
/* {{{ resource and registration code */
|
2004-06-22 02:58:55 +08:00
|
|
|
/* Global wrapper hash, copied to FG(stream_wrappers) on registration of volatile wrapper */
|
2002-03-16 05:03:08 +08:00
|
|
|
static HashTable url_stream_wrappers_hash;
|
2002-09-25 23:25:12 +08:00
|
|
|
static int le_stream = FAILURE; /* true global */
|
|
|
|
static int le_pstream = FAILURE; /* true global */
|
2004-09-14 11:48:17 +08:00
|
|
|
static int le_stream_filter = FAILURE; /* true global */
|
2002-09-25 23:25:12 +08:00
|
|
|
|
|
|
|
PHPAPI int php_file_le_stream(void)
|
|
|
|
{
|
|
|
|
return le_stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
PHPAPI int php_file_le_pstream(void)
|
|
|
|
{
|
|
|
|
return le_pstream;
|
|
|
|
}
|
|
|
|
|
2004-09-14 11:48:17 +08:00
|
|
|
PHPAPI int php_file_le_stream_filter(void)
|
|
|
|
{
|
|
|
|
return le_stream_filter;
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI HashTable *_php_stream_get_url_stream_wrappers_hash(void)
|
2003-02-17 06:19:28 +08:00
|
|
|
{
|
2004-06-22 02:58:55 +08:00
|
|
|
return (FG(stream_wrappers) ? FG(stream_wrappers) : &url_stream_wrappers_hash);
|
2003-02-17 06:19:28 +08:00
|
|
|
}
|
|
|
|
|
2004-09-11 04:45:35 +08:00
|
|
|
PHPAPI HashTable *php_stream_get_url_stream_wrappers_hash_global(void)
|
|
|
|
{
|
|
|
|
return &url_stream_wrappers_hash;
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static int forget_persistent_resource_id_numbers(zval *el)
|
2002-09-25 23:25:12 +08:00
|
|
|
{
|
|
|
|
php_stream *stream;
|
2014-05-19 14:53:02 +08:00
|
|
|
zend_resource *rsrc = Z_RES_P(el);
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (rsrc->type != le_pstream) {
|
2002-09-25 23:25:12 +08:00
|
|
|
return 0;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-09-25 23:25:12 +08:00
|
|
|
|
|
|
|
stream = (php_stream*)rsrc->ptr;
|
|
|
|
|
|
|
|
#if STREAM_DEBUG
|
|
|
|
fprintf(stderr, "forget_persistent: %s:%p\n", stream->ops->label, stream);
|
|
|
|
#endif
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
stream->res = NULL;
|
2002-09-25 23:25:12 +08:00
|
|
|
|
2015-04-07 19:19:12 +08:00
|
|
|
if (stream->ctx) {
|
|
|
|
zend_list_delete(stream->ctx);
|
2015-11-03 05:58:04 +08:00
|
|
|
stream->ctx = NULL;
|
2003-05-21 21:33:55 +08:00
|
|
|
}
|
2010-04-08 15:55:35 +08:00
|
|
|
|
2002-09-25 23:25:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PHP_RSHUTDOWN_FUNCTION(streams)
|
|
|
|
{
|
2014-12-14 06:06:14 +08:00
|
|
|
zend_hash_apply(&EG(persistent_list), forget_persistent_resource_id_numbers);
|
2002-09-25 23:25:12 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2010-11-15 11:05:32 +08:00
|
|
|
PHPAPI php_stream *php_stream_encloses(php_stream *enclosing, php_stream *enclosed)
|
|
|
|
{
|
|
|
|
php_stream *orig = enclosed->enclosing_stream;
|
|
|
|
|
|
|
|
php_stream_auto_cleanup(enclosed);
|
|
|
|
enclosed->enclosing_stream = enclosing;
|
|
|
|
return orig;
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int php_stream_from_persistent_id(const char *persistent_id, php_stream **stream)
|
2002-09-25 23:25:12 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_resource *le;
|
2002-09-25 23:25:12 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if ((le = zend_hash_str_find_ptr(&EG(persistent_list), persistent_id, strlen(persistent_id))) != NULL) {
|
|
|
|
if (le->type == le_pstream) {
|
2002-09-25 23:25:12 +08:00
|
|
|
if (stream) {
|
2015-02-10 20:43:12 +08:00
|
|
|
zend_resource *regentry = NULL;
|
2011-05-01 11:57:01 +08:00
|
|
|
|
|
|
|
/* see if this persistent resource already has been loaded to the
|
|
|
|
* regular list; allowing the same resource in several entries in the
|
|
|
|
* regular list causes trouble (see bug #54623) */
|
2015-02-10 20:43:12 +08:00
|
|
|
*stream = (php_stream*)le->ptr;
|
|
|
|
ZEND_HASH_FOREACH_PTR(&EG(regular_list), regentry) {
|
2011-05-01 11:57:01 +08:00
|
|
|
if (regentry->ptr == le->ptr) {
|
2015-02-10 20:43:12 +08:00
|
|
|
GC_REFCOUNT(regentry)++;
|
|
|
|
(*stream)->res = regentry;
|
|
|
|
return PHP_STREAM_PERSISTENT_SUCCESS;
|
2011-05-01 11:57:01 +08:00
|
|
|
}
|
2015-02-10 20:43:12 +08:00
|
|
|
} ZEND_HASH_FOREACH_END();
|
|
|
|
GC_REFCOUNT(le)++;
|
|
|
|
(*stream)->res = zend_register_resource(*stream, le_pstream);
|
2002-09-25 23:25:12 +08:00
|
|
|
}
|
|
|
|
return PHP_STREAM_PERSISTENT_SUCCESS;
|
|
|
|
}
|
|
|
|
return PHP_STREAM_PERSISTENT_FAILURE;
|
|
|
|
}
|
|
|
|
return PHP_STREAM_PERSISTENT_NOT_EXIST;
|
|
|
|
}
|
2001-04-18 01:03:18 +08:00
|
|
|
|
2003-02-17 06:19:28 +08:00
|
|
|
/* }}} */
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static zend_llist *php_get_wrapper_errors_list(php_stream_wrapper *wrapper)
|
2012-03-08 20:30:59 +08:00
|
|
|
{
|
|
|
|
if (!FG(wrapper_errors)) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
2014-03-14 19:35:57 +08:00
|
|
|
return (zend_llist*) zend_hash_str_find_ptr(FG(wrapper_errors), (const char*)&wrapper, sizeof(wrapper));
|
2012-03-08 20:30:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-17 06:19:28 +08:00
|
|
|
/* {{{ wrapper error reporting */
|
2014-12-14 06:06:14 +08:00
|
|
|
void php_stream_display_wrapper_errors(php_stream_wrapper *wrapper, const char *path, const char *caption)
|
2002-09-28 21:05:47 +08:00
|
|
|
{
|
|
|
|
char *tmp = estrdup(path);
|
|
|
|
char *msg;
|
|
|
|
int free_msg = 0;
|
|
|
|
|
|
|
|
if (wrapper) {
|
2014-12-14 06:06:14 +08:00
|
|
|
zend_llist *err_list = php_get_wrapper_errors_list(wrapper);
|
2012-03-08 20:30:59 +08:00
|
|
|
if (err_list) {
|
|
|
|
size_t l = 0;
|
2002-09-28 21:05:47 +08:00
|
|
|
int brlen;
|
2012-03-08 20:30:59 +08:00
|
|
|
int i;
|
2014-10-27 19:00:25 +08:00
|
|
|
int count = (int)zend_llist_count(err_list);
|
2012-03-08 20:30:59 +08:00
|
|
|
const char *br;
|
|
|
|
const char **err_buf_p;
|
|
|
|
zend_llist_position pos;
|
2002-09-28 21:05:47 +08:00
|
|
|
|
|
|
|
if (PG(html_errors)) {
|
|
|
|
brlen = 7;
|
|
|
|
br = "<br />\n";
|
|
|
|
} else {
|
|
|
|
brlen = 1;
|
|
|
|
br = "\n";
|
|
|
|
}
|
|
|
|
|
2012-03-08 20:30:59 +08:00
|
|
|
for (err_buf_p = zend_llist_get_first_ex(err_list, &pos), i = 0;
|
|
|
|
err_buf_p;
|
|
|
|
err_buf_p = zend_llist_get_next_ex(err_list, &pos), i++) {
|
|
|
|
l += strlen(*err_buf_p);
|
|
|
|
if (i < count - 1) {
|
2002-09-28 21:05:47 +08:00
|
|
|
l += brlen;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-09-28 21:05:47 +08:00
|
|
|
}
|
|
|
|
msg = emalloc(l + 1);
|
|
|
|
msg[0] = '\0';
|
2012-03-08 20:30:59 +08:00
|
|
|
for (err_buf_p = zend_llist_get_first_ex(err_list, &pos), i = 0;
|
|
|
|
err_buf_p;
|
|
|
|
err_buf_p = zend_llist_get_next_ex(err_list, &pos), i++) {
|
|
|
|
strcat(msg, *err_buf_p);
|
|
|
|
if (i < count - 1) {
|
2012-03-08 20:39:48 +08:00
|
|
|
strcat(msg, br);
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-09-28 21:05:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
free_msg = 1;
|
|
|
|
} else {
|
2008-04-29 06:37:31 +08:00
|
|
|
if (wrapper == &php_plain_files_wrapper) {
|
2012-03-08 20:30:59 +08:00
|
|
|
msg = strerror(errno); /* TODO: not ts on linux */
|
2008-04-29 06:37:31 +08:00
|
|
|
} else {
|
|
|
|
msg = "operation failed";
|
|
|
|
}
|
2002-09-28 21:05:47 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
msg = "no suitable wrapper could be found";
|
|
|
|
}
|
|
|
|
|
|
|
|
php_strip_url_passwd(tmp);
|
2014-12-14 06:06:14 +08:00
|
|
|
php_error_docref1(NULL, tmp, E_WARNING, "%s: %s", caption, msg);
|
2002-09-28 21:05:47 +08:00
|
|
|
efree(tmp);
|
2003-04-01 06:13:01 +08:00
|
|
|
if (free_msg) {
|
2002-09-28 21:05:47 +08:00
|
|
|
efree(msg);
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-09-28 21:05:47 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
void php_stream_tidy_wrapper_error_log(php_stream_wrapper *wrapper)
|
2002-09-28 21:05:47 +08:00
|
|
|
{
|
2012-03-08 20:30:59 +08:00
|
|
|
if (wrapper && FG(wrapper_errors)) {
|
2014-03-14 19:35:57 +08:00
|
|
|
zend_hash_str_del(FG(wrapper_errors), (const char*)&wrapper, sizeof(wrapper));
|
2002-09-28 21:05:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-08 20:30:59 +08:00
|
|
|
static void wrapper_error_dtor(void *error)
|
|
|
|
{
|
|
|
|
efree(*(char**)error);
|
|
|
|
}
|
|
|
|
|
2014-02-21 16:32:46 +08:00
|
|
|
static void wrapper_list_dtor(zval *item) {
|
|
|
|
zend_llist *list = (zend_llist*)Z_PTR_P(item);
|
|
|
|
zend_llist_destroy(list);
|
|
|
|
efree(list);
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI void php_stream_wrapper_log_error(php_stream_wrapper *wrapper, int options, const char *fmt, ...)
|
2003-02-17 06:19:28 +08:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char *buffer = NULL;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vspprintf(&buffer, 0, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (options & REPORT_ERRORS || wrapper == NULL) {
|
2014-12-14 06:06:14 +08:00
|
|
|
php_error_docref(NULL, E_WARNING, "%s", buffer);
|
2003-02-17 06:19:28 +08:00
|
|
|
efree(buffer);
|
|
|
|
} else {
|
2012-03-08 20:30:59 +08:00
|
|
|
zend_llist *list = NULL;
|
|
|
|
if (!FG(wrapper_errors)) {
|
|
|
|
ALLOC_HASHTABLE(FG(wrapper_errors));
|
2014-02-21 16:32:46 +08:00
|
|
|
zend_hash_init(FG(wrapper_errors), 8, NULL, wrapper_list_dtor, 0);
|
2012-03-08 20:30:59 +08:00
|
|
|
} else {
|
2014-03-14 19:35:57 +08:00
|
|
|
list = zend_hash_str_find_ptr(FG(wrapper_errors), (const char*)&wrapper, sizeof(wrapper));
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2012-03-08 20:30:59 +08:00
|
|
|
|
|
|
|
if (!list) {
|
|
|
|
zend_llist new_list;
|
2014-02-21 16:32:46 +08:00
|
|
|
zend_llist_init(&new_list, sizeof(buffer), wrapper_error_dtor, 0);
|
2015-01-03 17:22:58 +08:00
|
|
|
list = zend_hash_str_update_mem(FG(wrapper_errors), (const char*)&wrapper,
|
2014-03-14 19:35:57 +08:00
|
|
|
sizeof(wrapper), &new_list, sizeof(new_list));
|
2012-03-08 20:30:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* append to linked list */
|
|
|
|
zend_llist_add_element(list, &buffer);
|
2003-02-17 06:19:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-28 21:05:47 +08:00
|
|
|
|
2003-02-17 06:19:28 +08:00
|
|
|
/* }}} */
|
2002-09-28 21:05:47 +08:00
|
|
|
|
2001-04-18 01:03:18 +08:00
|
|
|
/* allocate a new stream for a particular ops */
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI php_stream *_php_stream_alloc(php_stream_ops *ops, void *abstract, const char *persistent_id, const char *mode STREAMS_DC) /* {{{ */
|
2001-04-18 01:03:18 +08:00
|
|
|
{
|
2002-03-16 08:05:47 +08:00
|
|
|
php_stream *ret;
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2002-09-25 23:25:12 +08:00
|
|
|
ret = (php_stream*) pemalloc_rel_orig(sizeof(php_stream), persistent_id ? 1 : 0);
|
2001-04-18 01:03:18 +08:00
|
|
|
|
|
|
|
memset(ret, 0, sizeof(php_stream));
|
|
|
|
|
2004-04-01 07:48:59 +08:00
|
|
|
ret->readfilters.stream = ret;
|
|
|
|
ret->writefilters.stream = ret;
|
|
|
|
|
2002-03-28 08:49:00 +08:00
|
|
|
#if STREAM_DEBUG
|
2002-09-25 23:25:12 +08:00
|
|
|
fprintf(stderr, "stream_alloc: %s:%p persistent=%s\n", ops->label, ret, persistent_id);
|
2002-03-28 08:49:00 +08:00
|
|
|
#endif
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2001-04-18 01:03:18 +08:00
|
|
|
ret->ops = ops;
|
|
|
|
ret->abstract = abstract;
|
2002-09-25 23:25:12 +08:00
|
|
|
ret->is_persistent = persistent_id ? 1 : 0;
|
2002-09-23 09:47:04 +08:00
|
|
|
ret->chunk_size = FG(def_chunk_size);
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2007-11-06 19:02:36 +08:00
|
|
|
#if ZEND_DEBUG
|
|
|
|
ret->open_filename = __zend_orig_filename ? __zend_orig_filename : __zend_filename;
|
|
|
|
ret->open_lineno = __zend_orig_lineno ? __zend_orig_lineno : __zend_lineno;
|
|
|
|
#endif
|
|
|
|
|
2003-04-01 06:13:01 +08:00
|
|
|
if (FG(auto_detect_line_endings)) {
|
2002-09-24 02:12:39 +08:00
|
|
|
ret->flags |= PHP_STREAM_FLAG_DETECT_EOL;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-09-25 23:25:12 +08:00
|
|
|
|
|
|
|
if (persistent_id) {
|
2014-02-10 14:04:30 +08:00
|
|
|
zval tmp;
|
2002-09-25 23:25:12 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
ZVAL_NEW_PERSISTENT_RES(&tmp, -1, ret, le_pstream);
|
2010-04-08 15:55:35 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (NULL == zend_hash_str_update(&EG(persistent_list), persistent_id,
|
|
|
|
strlen(persistent_id), &tmp)) {
|
2002-09-25 23:25:12 +08:00
|
|
|
pefree(ret, 1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2015-02-02 13:23:16 +08:00
|
|
|
ret->res = zend_register_resource(ret, persistent_id ? le_pstream : le_stream);
|
2002-03-16 08:05:47 +08:00
|
|
|
strlcpy(ret->mode, mode, sizeof(ret->mode));
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2010-11-15 11:05:32 +08:00
|
|
|
ret->wrapper = NULL;
|
|
|
|
ret->wrapperthis = NULL;
|
2014-02-10 14:04:30 +08:00
|
|
|
ZVAL_UNDEF(&ret->wrapperdata);
|
2010-11-15 11:05:32 +08:00
|
|
|
ret->stdiocast = NULL;
|
|
|
|
ret->orig_path = NULL;
|
2014-08-07 19:55:37 +08:00
|
|
|
ret->ctx = NULL;
|
2010-11-15 11:05:32 +08:00
|
|
|
ret->readbuf = NULL;
|
|
|
|
ret->enclosing_stream = NULL;
|
|
|
|
|
2001-04-18 01:03:18 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
/* }}} */
|
2001-04-18 01:03:18 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int _php_stream_free_enclosed(php_stream *stream_enclosed, int close_options) /* {{{ */
|
2010-11-15 11:05:32 +08:00
|
|
|
{
|
2015-04-07 19:19:12 +08:00
|
|
|
return php_stream_free(stream_enclosed,
|
2014-12-14 06:06:14 +08:00
|
|
|
close_options | PHP_STREAM_FREE_IGNORE_ENCLOSING);
|
2010-11-15 11:05:32 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
#if STREAM_DEBUG
|
|
|
|
static const char *_php_stream_pretty_free_options(int close_options, char *out)
|
|
|
|
{
|
|
|
|
if (close_options & PHP_STREAM_FREE_CALL_DTOR)
|
|
|
|
strcat(out, "CALL_DTOR, ");
|
|
|
|
if (close_options & PHP_STREAM_FREE_RELEASE_STREAM)
|
|
|
|
strcat(out, "RELEASE_STREAM, ");
|
|
|
|
if (close_options & PHP_STREAM_FREE_PRESERVE_HANDLE)
|
|
|
|
strcat(out, "PREVERSE_HANDLE, ");
|
|
|
|
if (close_options & PHP_STREAM_FREE_RSRC_DTOR)
|
|
|
|
strcat(out, "RSRC_DTOR, ");
|
|
|
|
if (close_options & PHP_STREAM_FREE_PERSISTENT)
|
|
|
|
strcat(out, "PERSISTENT, ");
|
|
|
|
if (close_options & PHP_STREAM_FREE_IGNORE_ENCLOSING)
|
|
|
|
strcat(out, "IGNORE_ENCLOSING, ");
|
|
|
|
if (out[0] != '\0')
|
|
|
|
out[strlen(out) - 2] = '\0';
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static int _php_stream_free_persistent(zval *zv, void *pStream)
|
2003-04-14 03:47:55 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_resource *le = Z_RES_P(zv);
|
2003-07-30 02:26:34 +08:00
|
|
|
return le->ptr == pStream;
|
2003-04-14 03:47:55 +08:00
|
|
|
}
|
|
|
|
|
2010-11-15 11:05:32 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int _php_stream_free(php_stream *stream, int close_options) /* {{{ */
|
2001-04-18 01:03:18 +08:00
|
|
|
{
|
|
|
|
int ret = 1;
|
2002-11-18 19:39:42 +08:00
|
|
|
int preserve_handle = close_options & PHP_STREAM_FREE_PRESERVE_HANDLE ? 1 : 0;
|
|
|
|
int release_cast = 1;
|
2012-02-25 06:56:21 +08:00
|
|
|
php_stream_context *context = NULL;
|
|
|
|
|
|
|
|
/* on an resource list destruction, the context, another resource, may have
|
|
|
|
* already been freed (if it was created after the stream resource), so
|
|
|
|
* don't reference it */
|
2012-03-18 03:37:30 +08:00
|
|
|
if (EG(active)) {
|
2014-08-07 19:55:37 +08:00
|
|
|
context = PHP_STREAM_CONTEXT(stream);
|
2012-02-25 06:56:21 +08:00
|
|
|
}
|
2001-04-18 01:03:18 +08:00
|
|
|
|
2007-02-22 05:57:21 +08:00
|
|
|
if (stream->flags & PHP_STREAM_FLAG_NO_CLOSE) {
|
|
|
|
preserve_handle = 1;
|
|
|
|
}
|
|
|
|
|
2002-03-28 08:49:00 +08:00
|
|
|
#if STREAM_DEBUG
|
2010-11-15 11:05:32 +08:00
|
|
|
{
|
|
|
|
char out[200] = "";
|
|
|
|
fprintf(stderr, "stream_free: %s:%p[%s] in_free=%d opts=%s\n",
|
|
|
|
stream->ops->label, stream, stream->orig_path, stream->in_free, _php_stream_pretty_free_options(close_options, out));
|
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2002-03-28 08:49:00 +08:00
|
|
|
#endif
|
2003-04-01 06:13:01 +08:00
|
|
|
|
|
|
|
if (stream->in_free) {
|
2010-11-15 11:05:32 +08:00
|
|
|
/* hopefully called recursively from the enclosing stream; the pointer was NULLed below */
|
|
|
|
if ((stream->in_free == 1) && (close_options & PHP_STREAM_FREE_IGNORE_ENCLOSING) && (stream->enclosing_stream == NULL)) {
|
|
|
|
close_options |= PHP_STREAM_FREE_RSRC_DTOR; /* restore flag */
|
|
|
|
} else {
|
|
|
|
return 1; /* recursion protection */
|
|
|
|
}
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-03-20 22:21:30 +08:00
|
|
|
|
|
|
|
stream->in_free++;
|
|
|
|
|
2010-11-15 11:05:32 +08:00
|
|
|
/* force correct order on enclosing/enclosed stream destruction (only from resource
|
|
|
|
* destructor as in when reverse destroying the resource list) */
|
|
|
|
if ((close_options & PHP_STREAM_FREE_RSRC_DTOR) &&
|
|
|
|
!(close_options & PHP_STREAM_FREE_IGNORE_ENCLOSING) &&
|
|
|
|
(close_options & (PHP_STREAM_FREE_CALL_DTOR | PHP_STREAM_FREE_RELEASE_STREAM)) && /* always? */
|
|
|
|
(stream->enclosing_stream != NULL)) {
|
|
|
|
php_stream *enclosing_stream = stream->enclosing_stream;
|
|
|
|
stream->enclosing_stream = NULL;
|
|
|
|
/* we force PHP_STREAM_CALL_DTOR because that's from where the
|
|
|
|
* enclosing stream can free this stream. We remove rsrc_dtor because
|
|
|
|
* we want the enclosing stream to be deleted from the resource list */
|
2015-04-07 19:19:12 +08:00
|
|
|
return php_stream_free(enclosing_stream,
|
2014-12-14 06:06:14 +08:00
|
|
|
(close_options | PHP_STREAM_FREE_CALL_DTOR) & ~PHP_STREAM_FREE_RSRC_DTOR);
|
2010-11-15 11:05:32 +08:00
|
|
|
}
|
|
|
|
|
2002-11-18 19:39:42 +08:00
|
|
|
/* if we are releasing the stream only (and preserving the underlying handle),
|
|
|
|
* we need to do things a little differently.
|
|
|
|
* We are only ever called like this when the stream is cast to a FILE*
|
|
|
|
* for include (or other similar) purposes.
|
|
|
|
* */
|
|
|
|
if (preserve_handle) {
|
|
|
|
if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FOPENCOOKIE) {
|
|
|
|
/* If the stream was fopencookied, we must NOT touch anything
|
|
|
|
* here, as the cookied stream relies on it all.
|
|
|
|
* Instead, mark the stream as OK to auto-clean */
|
|
|
|
php_stream_auto_cleanup(stream);
|
|
|
|
stream->in_free--;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* otherwise, make sure that we don't close the FILE* from a cast */
|
|
|
|
release_cast = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if STREAM_DEBUG
|
|
|
|
fprintf(stderr, "stream_free: %s:%p[%s] preserve_handle=%d release_cast=%d remove_rsrc=%d\n",
|
2011-03-21 10:58:54 +08:00
|
|
|
stream->ops->label, stream, stream->orig_path, preserve_handle, release_cast,
|
|
|
|
(close_options & PHP_STREAM_FREE_RSRC_DTOR) == 0);
|
2002-11-18 19:39:42 +08:00
|
|
|
#endif
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2015-06-30 09:49:54 +08:00
|
|
|
if (stream->flags & PHP_STREAM_FLAG_WAS_WRITTEN) {
|
|
|
|
/* make sure everything is saved */
|
|
|
|
_php_stream_flush(stream, 1);
|
|
|
|
}
|
2010-04-08 15:55:35 +08:00
|
|
|
|
2003-04-01 06:13:01 +08:00
|
|
|
/* If not called from the resource dtor, remove the stream from the resource list. */
|
2014-05-21 13:11:56 +08:00
|
|
|
if ((close_options & PHP_STREAM_FREE_RSRC_DTOR) == 0 && stream->res) {
|
2015-04-08 18:29:42 +08:00
|
|
|
/* Close resource, but keep it in resource list */
|
2014-02-27 16:28:55 +08:00
|
|
|
zend_list_close(stream->res);
|
2015-04-08 18:29:42 +08:00
|
|
|
if ((close_options & PHP_STREAM_FREE_KEEP_RSRC) == 0) {
|
|
|
|
/* Completely delete zend_resource, if not referenced */
|
2014-07-31 14:53:31 +08:00
|
|
|
zend_list_delete(stream->res);
|
|
|
|
stream->res = NULL;
|
|
|
|
}
|
2002-03-20 22:21:30 +08:00
|
|
|
}
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2002-03-19 02:54:32 +08:00
|
|
|
if (close_options & PHP_STREAM_FREE_CALL_DTOR) {
|
2002-11-18 19:39:42 +08:00
|
|
|
if (release_cast && stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FOPENCOOKIE) {
|
2001-05-06 02:36:22 +08:00
|
|
|
/* calling fclose on an fopencookied stream will ultimately
|
|
|
|
call this very same function. If we were called via fclose,
|
|
|
|
the cookie_closer unsets the fclose_stdiocast flags, so
|
|
|
|
we can be sure that we only reach here when PHP code calls
|
|
|
|
php_stream_free.
|
|
|
|
Lets let the cookie code clean it all up.
|
|
|
|
*/
|
2002-03-20 22:21:30 +08:00
|
|
|
stream->in_free = 0;
|
2001-05-06 02:36:22 +08:00
|
|
|
return fclose(stream->stdiocast);
|
|
|
|
}
|
2002-03-17 04:05:08 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
ret = stream->ops->close(stream, preserve_handle ? 0 : 1);
|
2002-03-19 02:54:32 +08:00
|
|
|
stream->abstract = NULL;
|
2002-03-17 04:05:08 +08:00
|
|
|
|
2001-05-06 02:36:22 +08:00
|
|
|
/* tidy up any FILE* that might have been fdopened */
|
2002-11-18 19:39:42 +08:00
|
|
|
if (release_cast && stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FDOPEN && stream->stdiocast) {
|
2001-05-06 02:36:22 +08:00
|
|
|
fclose(stream->stdiocast);
|
|
|
|
stream->stdiocast = NULL;
|
2002-11-18 19:39:42 +08:00
|
|
|
stream->fclose_stdiocast = PHP_STREAM_FCLOSE_NONE;
|
2001-05-06 02:36:22 +08:00
|
|
|
}
|
2001-04-18 01:03:18 +08:00
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2002-03-19 02:54:32 +08:00
|
|
|
if (close_options & PHP_STREAM_FREE_RELEASE_STREAM) {
|
2003-02-18 09:22:21 +08:00
|
|
|
while (stream->readfilters.head) {
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_filter_remove(stream->readfilters.head, 1);
|
2003-02-18 09:22:21 +08:00
|
|
|
}
|
|
|
|
while (stream->writefilters.head) {
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_filter_remove(stream->writefilters.head, 1);
|
2002-08-21 04:47:47 +08:00
|
|
|
}
|
2002-03-19 02:54:32 +08:00
|
|
|
|
2002-04-17 06:14:27 +08:00
|
|
|
if (stream->wrapper && stream->wrapper->wops && stream->wrapper->wops->stream_closer) {
|
2014-12-14 06:06:14 +08:00
|
|
|
stream->wrapper->wops->stream_closer(stream->wrapper, stream);
|
2002-03-19 02:54:32 +08:00
|
|
|
stream->wrapper = NULL;
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (Z_TYPE(stream->wrapperdata) != IS_UNDEF) {
|
2002-03-19 21:42:25 +08:00
|
|
|
zval_ptr_dtor(&stream->wrapperdata);
|
2014-02-10 14:04:30 +08:00
|
|
|
ZVAL_UNDEF(&stream->wrapperdata);
|
2002-03-19 02:54:32 +08:00
|
|
|
}
|
|
|
|
|
2002-09-23 09:47:04 +08:00
|
|
|
if (stream->readbuf) {
|
|
|
|
pefree(stream->readbuf, stream->is_persistent);
|
|
|
|
stream->readbuf = NULL;
|
|
|
|
}
|
2003-07-29 02:57:28 +08:00
|
|
|
|
2003-07-30 02:26:34 +08:00
|
|
|
if (stream->is_persistent && (close_options & PHP_STREAM_FREE_PERSISTENT)) {
|
2003-07-29 02:57:28 +08:00
|
|
|
/* we don't work with *stream but need its value for comparison */
|
2014-12-14 06:06:14 +08:00
|
|
|
zend_hash_apply_with_argument(&EG(persistent_list), _php_stream_free_persistent, stream);
|
2003-07-29 02:57:28 +08:00
|
|
|
}
|
2002-03-20 22:21:30 +08:00
|
|
|
#if ZEND_DEBUG
|
|
|
|
if ((close_options & PHP_STREAM_FREE_RSRC_DTOR) && (stream->__exposed == 0) && (EG(error_reporting) & E_WARNING)) {
|
|
|
|
/* it leaked: Lets deliberately NOT pefree it so that the memory manager shows it
|
|
|
|
* as leaked; it will log a warning, but lets help it out and display what kind
|
|
|
|
* of stream it was. */
|
2014-06-20 17:31:43 +08:00
|
|
|
if (!CG(unclean_shutdown)) {
|
|
|
|
char *leakinfo;
|
|
|
|
spprintf(&leakinfo, 0, __FILE__ "(%d) : Stream of type '%s' %p (path:%s) was not closed\n", __LINE__, stream->ops->label, stream, stream->orig_path);
|
2002-08-21 06:08:55 +08:00
|
|
|
|
2014-06-20 17:31:43 +08:00
|
|
|
if (stream->orig_path) {
|
|
|
|
pefree(stream->orig_path, stream->is_persistent);
|
|
|
|
stream->orig_path = NULL;
|
|
|
|
}
|
2010-04-08 15:55:35 +08:00
|
|
|
|
2011-05-01 12:16:28 +08:00
|
|
|
# if defined(PHP_WIN32)
|
2014-06-20 17:31:43 +08:00
|
|
|
OutputDebugString(leakinfo);
|
2002-03-20 22:21:30 +08:00
|
|
|
# else
|
2014-06-20 17:31:43 +08:00
|
|
|
fprintf(stderr, "%s", leakinfo);
|
2002-03-20 22:21:30 +08:00
|
|
|
# endif
|
2014-06-20 17:31:43 +08:00
|
|
|
efree(leakinfo);
|
|
|
|
}
|
2002-08-21 06:08:55 +08:00
|
|
|
} else {
|
2003-11-03 22:12:46 +08:00
|
|
|
if (stream->orig_path) {
|
|
|
|
pefree(stream->orig_path, stream->is_persistent);
|
|
|
|
stream->orig_path = NULL;
|
2003-03-20 09:23:04 +08:00
|
|
|
}
|
|
|
|
|
2002-08-21 06:08:55 +08:00
|
|
|
pefree(stream, stream->is_persistent);
|
2002-03-20 22:21:30 +08:00
|
|
|
}
|
2002-08-21 06:08:55 +08:00
|
|
|
#else
|
2003-11-03 22:12:46 +08:00
|
|
|
if (stream->orig_path) {
|
|
|
|
pefree(stream->orig_path, stream->is_persistent);
|
|
|
|
stream->orig_path = NULL;
|
|
|
|
}
|
|
|
|
|
2002-03-19 02:54:32 +08:00
|
|
|
pefree(stream, stream->is_persistent);
|
2002-08-21 06:08:55 +08:00
|
|
|
#endif
|
2002-03-16 05:03:08 +08:00
|
|
|
}
|
2001-04-18 01:03:18 +08:00
|
|
|
|
2008-07-11 18:25:15 +08:00
|
|
|
if (context) {
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_list_delete(context->res);
|
2008-07-11 18:25:15 +08:00
|
|
|
}
|
|
|
|
|
2001-04-18 01:03:18 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
/* }}} */
|
2001-04-18 01:03:18 +08:00
|
|
|
|
2002-03-16 05:03:08 +08:00
|
|
|
/* {{{ generic stream operations */
|
2002-09-23 09:47:04 +08:00
|
|
|
|
2015-03-06 08:53:04 +08:00
|
|
|
PHPAPI void _php_stream_fill_read_buffer(php_stream *stream, size_t size)
|
2001-04-18 01:03:18 +08:00
|
|
|
{
|
2002-09-23 09:47:04 +08:00
|
|
|
/* allocate/fill the buffer */
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2003-02-18 09:22:21 +08:00
|
|
|
if (stream->readfilters.head) {
|
|
|
|
char *chunk_buf;
|
|
|
|
int err_flag = 0;
|
|
|
|
php_stream_bucket_brigade brig_in = { NULL, NULL }, brig_out = { NULL, NULL };
|
|
|
|
php_stream_bucket_brigade *brig_inp = &brig_in, *brig_outp = &brig_out, *brig_swap;
|
|
|
|
|
2008-09-22 09:26:14 +08:00
|
|
|
/* Invalidate the existing cache, otherwise reads can fail, see note in
|
|
|
|
main/streams/filter.c::_php_stream_filter_append */
|
|
|
|
stream->writepos = stream->readpos = 0;
|
|
|
|
|
2003-02-18 09:22:21 +08:00
|
|
|
/* allocate a buffer for reading chunks */
|
|
|
|
chunk_buf = emalloc(stream->chunk_size);
|
|
|
|
|
2016-10-06 17:01:27 +08:00
|
|
|
while (!stream->eof && !err_flag && (stream->writepos - stream->readpos < (zend_off_t)size)) {
|
2003-02-18 09:22:21 +08:00
|
|
|
size_t justread = 0;
|
|
|
|
int flags;
|
|
|
|
php_stream_bucket *bucket;
|
2003-06-15 03:30:42 +08:00
|
|
|
php_stream_filter_status_t status = PSFS_ERR_FATAL;
|
2003-02-18 09:22:21 +08:00
|
|
|
php_stream_filter *filter;
|
|
|
|
|
|
|
|
/* read a chunk into a bucket */
|
2014-12-14 06:06:14 +08:00
|
|
|
justread = stream->ops->read(stream, chunk_buf, stream->chunk_size);
|
2006-04-21 01:43:18 +08:00
|
|
|
if (justread && justread != (size_t)-1) {
|
2014-12-14 06:06:14 +08:00
|
|
|
bucket = php_stream_bucket_new(stream, chunk_buf, justread, 0, 0);
|
2003-02-18 09:22:21 +08:00
|
|
|
|
|
|
|
/* after this call, bucket is owned by the brigade */
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_bucket_append(brig_inp, bucket);
|
2003-02-18 09:22:21 +08:00
|
|
|
|
|
|
|
flags = PSFS_FLAG_NORMAL;
|
|
|
|
} else {
|
|
|
|
flags = stream->eof ? PSFS_FLAG_FLUSH_CLOSE : PSFS_FLAG_FLUSH_INC;
|
|
|
|
}
|
2010-04-08 15:55:35 +08:00
|
|
|
|
2003-02-18 09:22:21 +08:00
|
|
|
/* wind the handle... */
|
|
|
|
for (filter = stream->readfilters.head; filter; filter = filter->next) {
|
2014-12-14 06:06:14 +08:00
|
|
|
status = filter->fops->filter(stream, filter, brig_inp, brig_outp, NULL, flags);
|
2003-02-18 09:22:21 +08:00
|
|
|
|
|
|
|
if (status != PSFS_PASS_ON) {
|
|
|
|
break;
|
|
|
|
}
|
2010-04-08 15:55:35 +08:00
|
|
|
|
2003-02-18 09:22:21 +08:00
|
|
|
/* brig_out becomes brig_in.
|
|
|
|
* brig_in will always be empty here, as the filter MUST attach any un-consumed buckets
|
|
|
|
* to its own brigade */
|
|
|
|
brig_swap = brig_inp;
|
|
|
|
brig_inp = brig_outp;
|
|
|
|
brig_outp = brig_swap;
|
|
|
|
memset(brig_outp, 0, sizeof(*brig_outp));
|
|
|
|
}
|
2010-04-08 15:55:35 +08:00
|
|
|
|
2003-02-18 09:22:21 +08:00
|
|
|
switch (status) {
|
|
|
|
case PSFS_PASS_ON:
|
|
|
|
/* we get here when the last filter in the chain has data to pass on.
|
|
|
|
* in this situation, we are passing the brig_in brigade into the
|
|
|
|
* stream read buffer */
|
|
|
|
while (brig_inp->head) {
|
|
|
|
bucket = brig_inp->head;
|
|
|
|
/* grow buffer to hold this bucket
|
|
|
|
* TODO: this can fail for persistent streams */
|
|
|
|
if (stream->readbuflen - stream->writepos < bucket->buflen) {
|
|
|
|
stream->readbuflen += bucket->buflen;
|
|
|
|
stream->readbuf = perealloc(stream->readbuf, stream->readbuflen,
|
|
|
|
stream->is_persistent);
|
|
|
|
}
|
|
|
|
memcpy(stream->readbuf + stream->writepos, bucket->buf, bucket->buflen);
|
|
|
|
stream->writepos += bucket->buflen;
|
2007-11-06 19:02:36 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_bucket_unlink(bucket);
|
|
|
|
php_stream_bucket_delref(bucket);
|
2003-02-18 09:22:21 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PSFS_FEED_ME:
|
|
|
|
/* when a filter needs feeding, there is no brig_out to deal with.
|
|
|
|
* we simply continue the loop; if the caller needs more data,
|
|
|
|
* we will read again, otherwise out job is done here */
|
|
|
|
if (justread == 0) {
|
|
|
|
/* there is no data */
|
|
|
|
err_flag = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case PSFS_ERR_FATAL:
|
|
|
|
/* some fatal error. Theoretically, the stream is borked, so all
|
|
|
|
* further reads should fail. */
|
|
|
|
err_flag = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-12-23 22:32:11 +08:00
|
|
|
if (justread == 0 || justread == (size_t)-1) {
|
2003-02-18 09:22:21 +08:00
|
|
|
break;
|
|
|
|
}
|
2002-09-23 09:47:04 +08:00
|
|
|
}
|
2003-02-18 09:22:21 +08:00
|
|
|
|
|
|
|
efree(chunk_buf);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* is there enough data in the buffer ? */
|
2014-08-16 17:16:11 +08:00
|
|
|
if (stream->writepos - stream->readpos < (zend_off_t)size) {
|
2003-02-18 09:22:21 +08:00
|
|
|
size_t justread = 0;
|
2009-03-20 01:55:10 +08:00
|
|
|
|
|
|
|
/* reduce buffer memory consumption if possible, to avoid a realloc */
|
|
|
|
if (stream->readbuf && stream->readbuflen - stream->writepos < stream->chunk_size) {
|
|
|
|
memmove(stream->readbuf, stream->readbuf + stream->readpos, stream->readbuflen - stream->readpos);
|
|
|
|
stream->writepos -= stream->readpos;
|
|
|
|
stream->readpos = 0;
|
|
|
|
}
|
2003-02-18 09:22:21 +08:00
|
|
|
|
|
|
|
/* grow the buffer if required
|
|
|
|
* TODO: this can fail for persistent streams */
|
|
|
|
if (stream->readbuflen - stream->writepos < stream->chunk_size) {
|
|
|
|
stream->readbuflen += stream->chunk_size;
|
|
|
|
stream->readbuf = perealloc(stream->readbuf, stream->readbuflen,
|
|
|
|
stream->is_persistent);
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
justread = stream->ops->read(stream, (char*)stream->readbuf + stream->writepos,
|
2009-03-20 01:55:10 +08:00
|
|
|
stream->readbuflen - stream->writepos
|
2014-12-14 21:07:59 +08:00
|
|
|
);
|
2002-10-05 18:35:13 +08:00
|
|
|
|
2003-02-18 09:22:21 +08:00
|
|
|
if (justread != (size_t)-1) {
|
|
|
|
stream->writepos += justread;
|
|
|
|
}
|
2002-10-05 18:35:13 +08:00
|
|
|
}
|
2002-09-23 09:47:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI size_t _php_stream_read(php_stream *stream, char *buf, size_t size)
|
2002-09-23 09:47:04 +08:00
|
|
|
{
|
2002-10-07 07:27:53 +08:00
|
|
|
size_t toread = 0, didread = 0;
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2002-10-07 07:27:53 +08:00
|
|
|
while (size > 0) {
|
|
|
|
|
|
|
|
/* take from the read buffer first.
|
|
|
|
* It is possible that a buffered stream was switched to non-buffered, so we
|
|
|
|
* drain the remainder of the buffer before using the "raw" read mode for
|
|
|
|
* the excess */
|
|
|
|
if (stream->writepos > stream->readpos) {
|
|
|
|
|
|
|
|
toread = stream->writepos - stream->readpos;
|
2003-04-01 06:13:01 +08:00
|
|
|
if (toread > size) {
|
2002-10-07 07:27:53 +08:00
|
|
|
toread = size;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-10-07 07:27:53 +08:00
|
|
|
|
|
|
|
memcpy(buf, stream->readbuf + stream->readpos, toread);
|
|
|
|
stream->readpos += toread;
|
|
|
|
size -= toread;
|
|
|
|
buf += toread;
|
|
|
|
didread += toread;
|
|
|
|
}
|
2002-09-23 21:22:10 +08:00
|
|
|
|
2003-01-31 05:06:34 +08:00
|
|
|
/* ignore eof here; the underlying state might have changed */
|
|
|
|
if (size == 0) {
|
2002-10-07 07:27:53 +08:00
|
|
|
break;
|
|
|
|
}
|
2002-10-05 18:35:13 +08:00
|
|
|
|
2003-02-18 09:22:21 +08:00
|
|
|
if (!stream->readfilters.head && (stream->flags & PHP_STREAM_FLAG_NO_BUFFER || stream->chunk_size == 1)) {
|
2014-12-14 06:06:14 +08:00
|
|
|
toread = stream->ops->read(stream, buf, size);
|
2014-04-02 21:36:39 +08:00
|
|
|
if (toread == (size_t) -1) {
|
|
|
|
/* e.g. underlying read(2) returned -1 */
|
|
|
|
break;
|
|
|
|
}
|
2002-09-23 21:22:10 +08:00
|
|
|
} else {
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_fill_read_buffer(stream, size);
|
2002-09-23 09:47:04 +08:00
|
|
|
|
2002-10-07 07:27:53 +08:00
|
|
|
toread = stream->writepos - stream->readpos;
|
2003-04-01 06:13:01 +08:00
|
|
|
if (toread > size) {
|
2002-10-07 07:27:53 +08:00
|
|
|
toread = size;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-10-07 07:27:53 +08:00
|
|
|
|
2002-10-14 07:43:46 +08:00
|
|
|
if (toread > 0) {
|
2002-10-07 07:27:53 +08:00
|
|
|
memcpy(buf, stream->readbuf + stream->readpos, toread);
|
|
|
|
stream->readpos += toread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (toread > 0) {
|
|
|
|
didread += toread;
|
|
|
|
buf += toread;
|
|
|
|
size -= toread;
|
|
|
|
} else {
|
|
|
|
/* EOF, or temporary end of data (for non-blocking mode). */
|
|
|
|
break;
|
2002-09-24 07:39:46 +08:00
|
|
|
}
|
2004-06-30 05:51:53 +08:00
|
|
|
|
|
|
|
/* just break anyway, to avoid greedy read */
|
2005-05-23 23:36:58 +08:00
|
|
|
if (stream->wrapper != &php_plain_files_wrapper) {
|
2004-07-10 18:54:09 +08:00
|
|
|
break;
|
2005-05-23 19:51:53 +08:00
|
|
|
}
|
2002-09-23 09:47:04 +08:00
|
|
|
}
|
2002-10-05 18:35:13 +08:00
|
|
|
|
2003-04-01 06:13:01 +08:00
|
|
|
if (didread > 0) {
|
2002-10-07 07:27:53 +08:00
|
|
|
stream->position += didread;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-10-05 18:35:13 +08:00
|
|
|
|
2002-09-23 21:22:10 +08:00
|
|
|
return didread;
|
2001-04-18 01:03:18 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int _php_stream_eof(php_stream *stream)
|
2001-04-18 01:03:18 +08:00
|
|
|
{
|
2002-09-23 09:47:04 +08:00
|
|
|
/* if there is data in the buffer, it's not EOF */
|
2003-04-01 06:13:01 +08:00
|
|
|
if (stream->writepos - stream->readpos > 0) {
|
2002-09-23 09:47:04 +08:00
|
|
|
return 0;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-09-23 09:47:04 +08:00
|
|
|
|
2004-02-05 06:46:44 +08:00
|
|
|
/* use the configured timeout when checking eof */
|
2003-11-29 07:20:23 +08:00
|
|
|
if (!stream->eof && PHP_STREAM_OPTION_RETURN_ERR ==
|
|
|
|
php_stream_set_option(stream, PHP_STREAM_OPTION_CHECK_LIVENESS,
|
2008-11-24 23:36:47 +08:00
|
|
|
0, NULL)) {
|
2003-11-29 07:20:23 +08:00
|
|
|
stream->eof = 1;
|
|
|
|
}
|
|
|
|
|
2002-10-05 18:35:13 +08:00
|
|
|
return stream->eof;
|
2001-04-18 01:03:18 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int _php_stream_putc(php_stream *stream, int c)
|
2002-03-16 05:03:08 +08:00
|
|
|
{
|
|
|
|
unsigned char buf = c;
|
2002-03-16 08:05:47 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (php_stream_write(stream, (char*)&buf, 1) > 0) {
|
2002-03-16 05:03:08 +08:00
|
|
|
return 1;
|
2002-03-16 08:05:47 +08:00
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int _php_stream_getc(php_stream *stream)
|
2001-04-18 01:03:18 +08:00
|
|
|
{
|
|
|
|
char buf;
|
|
|
|
|
2002-03-16 08:05:47 +08:00
|
|
|
if (php_stream_read(stream, &buf, 1) > 0) {
|
2002-03-16 10:39:39 +08:00
|
|
|
return buf & 0xff;
|
2002-03-16 08:05:47 +08:00
|
|
|
}
|
2001-04-18 01:03:18 +08:00
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int _php_stream_puts(php_stream *stream, const char *buf)
|
2002-03-16 05:03:08 +08:00
|
|
|
{
|
2014-10-28 00:28:01 +08:00
|
|
|
size_t len;
|
2002-03-16 05:03:08 +08:00
|
|
|
char newline[2] = "\n"; /* is this OK for Win? */
|
|
|
|
len = strlen(buf);
|
|
|
|
|
2002-03-16 08:05:47 +08:00
|
|
|
if (len > 0 && php_stream_write(stream, buf, len) && php_stream_write(stream, newline, 1)) {
|
2002-03-16 05:03:08 +08:00
|
|
|
return 1;
|
2002-03-16 08:05:47 +08:00
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int _php_stream_stat(php_stream *stream, php_stream_statbuf *ssb)
|
2002-03-28 08:49:00 +08:00
|
|
|
{
|
|
|
|
memset(ssb, 0, sizeof(*ssb));
|
|
|
|
|
|
|
|
/* if the stream was wrapped, allow the wrapper to stat it */
|
|
|
|
if (stream->wrapper && stream->wrapper->wops->stream_stat != NULL) {
|
2014-12-14 06:06:14 +08:00
|
|
|
return stream->wrapper->wops->stream_stat(stream->wrapper, stream, ssb);
|
2002-03-28 08:49:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if the stream doesn't directly support stat-ing, return with failure.
|
|
|
|
* We could try and emulate this by casting to a FD and fstat-ing it,
|
|
|
|
* but since the fd might not represent the actual underlying content
|
|
|
|
* this would give bogus results. */
|
|
|
|
if (stream->ops->stat == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
return (stream->ops->stat)(stream, ssb);
|
2002-03-28 08:49:00 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI const char *php_stream_locate_eol(php_stream *stream, zend_string *buf)
|
2001-04-18 01:03:18 +08:00
|
|
|
{
|
2002-10-05 02:21:40 +08:00
|
|
|
size_t avail;
|
2013-07-30 18:49:36 +08:00
|
|
|
const char *cr, *lf, *eol = NULL;
|
|
|
|
const char *readptr;
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2002-10-19 04:39:49 +08:00
|
|
|
if (!buf) {
|
2014-02-10 14:04:30 +08:00
|
|
|
readptr = (char*)stream->readbuf + stream->readpos;
|
2002-10-19 04:39:49 +08:00
|
|
|
avail = stream->writepos - stream->readpos;
|
|
|
|
} else {
|
2015-06-30 18:59:27 +08:00
|
|
|
readptr = ZSTR_VAL(buf);
|
|
|
|
avail = ZSTR_LEN(buf);
|
2010-04-08 15:55:35 +08:00
|
|
|
}
|
2002-10-05 02:21:40 +08:00
|
|
|
|
|
|
|
/* Look for EOL */
|
|
|
|
if (stream->flags & PHP_STREAM_FLAG_DETECT_EOL) {
|
|
|
|
cr = memchr(readptr, '\r', avail);
|
|
|
|
lf = memchr(readptr, '\n', avail);
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2002-10-22 23:34:50 +08:00
|
|
|
if (cr && lf != cr + 1 && !(lf && lf < cr)) {
|
2002-10-05 02:21:40 +08:00
|
|
|
/* mac */
|
|
|
|
stream->flags ^= PHP_STREAM_FLAG_DETECT_EOL;
|
|
|
|
stream->flags |= PHP_STREAM_FLAG_EOL_MAC;
|
|
|
|
eol = cr;
|
|
|
|
} else if ((cr && lf && cr == lf - 1) || (lf)) {
|
|
|
|
/* dos or unix endings */
|
|
|
|
stream->flags ^= PHP_STREAM_FLAG_DETECT_EOL;
|
|
|
|
eol = lf;
|
|
|
|
}
|
|
|
|
} else if (stream->flags & PHP_STREAM_FLAG_EOL_MAC) {
|
|
|
|
eol = memchr(readptr, '\r', avail);
|
|
|
|
} else {
|
|
|
|
/* unix (and dos) line endings */
|
|
|
|
eol = memchr(readptr, '\n', avail);
|
|
|
|
}
|
|
|
|
|
|
|
|
return eol;
|
|
|
|
}
|
|
|
|
|
2002-10-14 06:52:33 +08:00
|
|
|
/* If buf == NULL, the buffer will be allocated automatically and will be of an
|
|
|
|
* appropriate length to hold the line, regardless of the line length, memory
|
|
|
|
* permitting */
|
2002-10-19 21:11:48 +08:00
|
|
|
PHPAPI char *_php_stream_get_line(php_stream *stream, char *buf, size_t maxlen,
|
2014-12-14 06:06:14 +08:00
|
|
|
size_t *returned_len)
|
2002-10-05 02:21:40 +08:00
|
|
|
{
|
|
|
|
size_t avail = 0;
|
2002-10-14 06:52:33 +08:00
|
|
|
size_t current_buf_size = 0;
|
2002-10-14 10:28:35 +08:00
|
|
|
size_t total_copied = 0;
|
2002-10-14 06:52:33 +08:00
|
|
|
int grow_mode = 0;
|
|
|
|
char *bufstart = buf;
|
2002-10-05 18:35:13 +08:00
|
|
|
|
2003-04-01 06:13:01 +08:00
|
|
|
if (buf == NULL) {
|
2002-10-14 06:52:33 +08:00
|
|
|
grow_mode = 1;
|
2003-04-01 06:13:01 +08:00
|
|
|
} else if (maxlen == 0) {
|
2002-04-17 06:14:27 +08:00
|
|
|
return NULL;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2001-04-18 01:03:18 +08:00
|
|
|
|
2002-10-05 02:21:40 +08:00
|
|
|
/*
|
|
|
|
* If the underlying stream operations block when no new data is readable,
|
|
|
|
* we need to take extra precautions.
|
|
|
|
*
|
|
|
|
* If there is buffered data available, we check for a EOL. If it exists,
|
|
|
|
* we pass the data immediately back to the caller. This saves a call
|
|
|
|
* to the read implementation and will not block where blocking
|
|
|
|
* is not necessary at all.
|
|
|
|
*
|
|
|
|
* If the stream buffer contains more data than the caller requested,
|
|
|
|
* we can also avoid that costly step and simply return that data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (;;) {
|
2002-09-23 09:47:04 +08:00
|
|
|
avail = stream->writepos - stream->readpos;
|
2001-04-18 01:03:18 +08:00
|
|
|
|
2002-10-05 02:21:40 +08:00
|
|
|
if (avail > 0) {
|
|
|
|
size_t cpysz = 0;
|
|
|
|
char *readptr;
|
2013-07-30 18:49:36 +08:00
|
|
|
const char *eol;
|
2002-10-05 02:21:40 +08:00
|
|
|
int done = 0;
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
readptr = (char*)stream->readbuf + stream->readpos;
|
2014-12-14 06:06:14 +08:00
|
|
|
eol = php_stream_locate_eol(stream, NULL);
|
2002-10-05 02:21:40 +08:00
|
|
|
|
|
|
|
if (eol) {
|
|
|
|
cpysz = eol - readptr + 1;
|
|
|
|
done = 1;
|
|
|
|
} else {
|
|
|
|
cpysz = avail;
|
2002-09-23 09:47:04 +08:00
|
|
|
}
|
2002-08-21 04:47:47 +08:00
|
|
|
|
2002-10-14 06:52:33 +08:00
|
|
|
if (grow_mode) {
|
|
|
|
/* allow room for a NUL. If this realloc is really a realloc
|
|
|
|
* (ie: second time around), we get an extra byte. In most
|
|
|
|
* cases, with the default chunk size of 8K, we will only
|
|
|
|
* incur that overhead once. When people have lines longer
|
|
|
|
* than 8K, we waste 1 byte per additional 8K or so.
|
|
|
|
* That seems acceptable to me, to avoid making this code
|
|
|
|
* hard to follow */
|
|
|
|
bufstart = erealloc(bufstart, current_buf_size + cpysz + 1);
|
|
|
|
current_buf_size += cpysz + 1;
|
2002-10-14 10:28:35 +08:00
|
|
|
buf = bufstart + total_copied;
|
2002-10-14 06:52:33 +08:00
|
|
|
} else {
|
|
|
|
if (cpysz >= maxlen - 1) {
|
|
|
|
cpysz = maxlen - 1;
|
|
|
|
done = 1;
|
|
|
|
}
|
2002-10-05 02:21:40 +08:00
|
|
|
}
|
2002-09-23 09:47:04 +08:00
|
|
|
|
2002-10-05 02:21:40 +08:00
|
|
|
memcpy(buf, readptr, cpysz);
|
2002-08-21 04:47:47 +08:00
|
|
|
|
2002-10-05 02:21:40 +08:00
|
|
|
stream->position += cpysz;
|
|
|
|
stream->readpos += cpysz;
|
|
|
|
buf += cpysz;
|
|
|
|
maxlen -= cpysz;
|
2002-10-14 10:28:35 +08:00
|
|
|
total_copied += cpysz;
|
2002-09-23 09:47:04 +08:00
|
|
|
|
2002-10-05 02:21:40 +08:00
|
|
|
if (done) {
|
|
|
|
break;
|
|
|
|
}
|
2002-10-12 10:56:34 +08:00
|
|
|
} else if (stream->eof) {
|
|
|
|
break;
|
2002-10-05 02:21:40 +08:00
|
|
|
} else {
|
2002-10-05 03:08:43 +08:00
|
|
|
/* XXX: Should be fine to always read chunk_size */
|
2002-10-14 06:52:33 +08:00
|
|
|
size_t toread;
|
2010-04-08 15:55:35 +08:00
|
|
|
|
2002-10-14 06:52:33 +08:00
|
|
|
if (grow_mode) {
|
2002-10-05 02:21:40 +08:00
|
|
|
toread = stream->chunk_size;
|
2002-10-14 06:52:33 +08:00
|
|
|
} else {
|
|
|
|
toread = maxlen - 1;
|
2003-04-01 06:13:01 +08:00
|
|
|
if (toread > stream->chunk_size) {
|
2002-10-14 06:52:33 +08:00
|
|
|
toread = stream->chunk_size;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-10-14 06:52:33 +08:00
|
|
|
}
|
2002-10-05 02:21:40 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_fill_read_buffer(stream, toread);
|
2002-10-05 03:36:09 +08:00
|
|
|
|
|
|
|
if (stream->writepos - stream->readpos == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2002-10-05 02:21:40 +08:00
|
|
|
}
|
|
|
|
}
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2002-10-14 10:28:35 +08:00
|
|
|
if (total_copied == 0) {
|
2002-10-14 13:38:50 +08:00
|
|
|
if (grow_mode) {
|
|
|
|
assert(bufstart == NULL);
|
|
|
|
}
|
2002-10-05 03:48:59 +08:00
|
|
|
return NULL;
|
2002-10-14 10:28:35 +08:00
|
|
|
}
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2002-10-05 02:21:40 +08:00
|
|
|
buf[0] = '\0';
|
2003-04-01 06:13:01 +08:00
|
|
|
if (returned_len) {
|
2002-10-19 21:11:48 +08:00
|
|
|
*returned_len = total_copied;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-09-25 23:25:12 +08:00
|
|
|
|
2002-10-14 06:52:33 +08:00
|
|
|
return bufstart;
|
2001-04-18 01:03:18 +08:00
|
|
|
}
|
|
|
|
|
2012-01-23 04:30:37 +08:00
|
|
|
#define STREAM_BUFFERED_AMOUNT(stream) \
|
|
|
|
((size_t)(((stream)->writepos) - (stream)->readpos))
|
|
|
|
|
2013-07-30 18:49:36 +08:00
|
|
|
static const char *_php_stream_search_delim(php_stream *stream,
|
|
|
|
size_t maxlen,
|
|
|
|
size_t skiplen,
|
|
|
|
const char *delim, /* non-empty! */
|
2014-12-14 06:06:14 +08:00
|
|
|
size_t delim_len)
|
2012-01-23 04:30:37 +08:00
|
|
|
{
|
|
|
|
size_t seek_len;
|
|
|
|
|
|
|
|
/* set the maximum number of bytes we're allowed to read from buffer */
|
|
|
|
seek_len = MIN(STREAM_BUFFERED_AMOUNT(stream), maxlen);
|
|
|
|
if (seek_len <= skiplen) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delim_len == 1) {
|
|
|
|
return memchr(&stream->readbuf[stream->readpos + skiplen],
|
|
|
|
delim[0], seek_len - skiplen);
|
|
|
|
} else {
|
|
|
|
return php_memnstr((char*)&stream->readbuf[stream->readpos + skiplen],
|
|
|
|
delim, delim_len,
|
|
|
|
(char*)&stream->readbuf[stream->readpos + seek_len]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI zend_string *php_stream_get_record(php_stream *stream, size_t maxlen, const char *delim, size_t delim_len)
|
2003-02-17 06:19:28 +08:00
|
|
|
{
|
2014-03-05 11:41:21 +08:00
|
|
|
zend_string *ret_buf; /* returned buffer */
|
2013-07-30 18:49:36 +08:00
|
|
|
const char *found_delim = NULL;
|
2012-01-23 04:30:37 +08:00
|
|
|
size_t buffered_len,
|
2012-04-07 23:32:19 +08:00
|
|
|
tent_ret_len; /* tentative returned length */
|
2014-03-05 11:41:21 +08:00
|
|
|
int has_delim = delim_len > 0;
|
2003-02-17 06:19:28 +08:00
|
|
|
|
2012-01-23 04:30:37 +08:00
|
|
|
if (maxlen == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-03-20 01:55:10 +08:00
|
|
|
|
2012-01-23 04:30:37 +08:00
|
|
|
if (has_delim) {
|
|
|
|
found_delim = _php_stream_search_delim(
|
2014-12-14 06:06:14 +08:00
|
|
|
stream, maxlen, 0, delim, delim_len);
|
2012-01-23 04:30:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
buffered_len = STREAM_BUFFERED_AMOUNT(stream);
|
|
|
|
/* try to read up to maxlen length bytes while we don't find the delim */
|
|
|
|
while (!found_delim && buffered_len < maxlen) {
|
|
|
|
size_t just_read,
|
|
|
|
to_read_now;
|
2009-03-20 01:55:10 +08:00
|
|
|
|
2012-01-23 04:30:37 +08:00
|
|
|
to_read_now = MIN(maxlen - buffered_len, stream->chunk_size);
|
2009-03-20 01:55:10 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_fill_read_buffer(stream, buffered_len + to_read_now);
|
2009-03-20 01:55:10 +08:00
|
|
|
|
2012-01-23 04:30:37 +08:00
|
|
|
just_read = STREAM_BUFFERED_AMOUNT(stream) - buffered_len;
|
2009-03-20 01:55:10 +08:00
|
|
|
|
2011-12-12 05:08:15 +08:00
|
|
|
/* Assume the stream is temporarily or permanently out of data */
|
|
|
|
if (just_read == 0) {
|
2009-03-20 01:55:10 +08:00
|
|
|
break;
|
|
|
|
}
|
2003-02-17 06:19:28 +08:00
|
|
|
|
2012-01-23 04:30:37 +08:00
|
|
|
if (has_delim) {
|
|
|
|
/* search for delimiter, but skip buffered_len (the number of bytes
|
|
|
|
* buffered before this loop iteration), as they have already been
|
2012-10-13 02:00:37 +08:00
|
|
|
* searched for the delimiter.
|
|
|
|
* The left part of the delimiter may still remain in the buffer,
|
|
|
|
* so subtract up to <delim_len - 1> from buffered_len, which is
|
2014-11-20 03:59:31 +08:00
|
|
|
* the amount of data we skip on this search as an optimization
|
2012-10-13 02:00:37 +08:00
|
|
|
*/
|
2012-01-23 04:30:37 +08:00
|
|
|
found_delim = _php_stream_search_delim(
|
2012-10-13 02:00:37 +08:00
|
|
|
stream, maxlen,
|
|
|
|
buffered_len >= (delim_len - 1)
|
|
|
|
? buffered_len - (delim_len - 1)
|
|
|
|
: 0,
|
2014-12-14 06:06:14 +08:00
|
|
|
delim, delim_len);
|
2012-01-23 04:30:37 +08:00
|
|
|
if (found_delim) {
|
|
|
|
break;
|
2008-11-05 01:05:55 +08:00
|
|
|
}
|
2003-02-17 06:19:28 +08:00
|
|
|
}
|
2012-01-23 04:30:37 +08:00
|
|
|
buffered_len += just_read;
|
2003-02-17 06:19:28 +08:00
|
|
|
}
|
|
|
|
|
2012-01-23 04:30:37 +08:00
|
|
|
if (has_delim && found_delim) {
|
|
|
|
tent_ret_len = found_delim - (char*)&stream->readbuf[stream->readpos];
|
|
|
|
} else if (!has_delim && STREAM_BUFFERED_AMOUNT(stream) >= maxlen) {
|
|
|
|
tent_ret_len = maxlen;
|
|
|
|
} else {
|
|
|
|
/* return with error if the delimiter string (if any) was not found, we
|
|
|
|
* could not completely fill the read buffer with maxlen bytes and we
|
|
|
|
* don't know we've reached end of file. Added with non-blocking streams
|
|
|
|
* in mind, where this situation is frequent */
|
|
|
|
if (STREAM_BUFFERED_AMOUNT(stream) < maxlen && !stream->eof) {
|
|
|
|
return NULL;
|
|
|
|
} else if (STREAM_BUFFERED_AMOUNT(stream) == 0 && stream->eof) {
|
|
|
|
/* refuse to return an empty string just because by accident
|
|
|
|
* we knew of EOF in a read that returned no data */
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
tent_ret_len = MIN(STREAM_BUFFERED_AMOUNT(stream), maxlen);
|
|
|
|
}
|
2003-04-04 09:34:00 +08:00
|
|
|
}
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
ret_buf = zend_string_alloc(tent_ret_len, 0);
|
2012-01-23 04:30:37 +08:00
|
|
|
/* php_stream_read will not call ops->read here because the necessary
|
|
|
|
* data is guaranteedly buffered */
|
2015-06-30 18:59:27 +08:00
|
|
|
ZSTR_LEN(ret_buf) = php_stream_read(stream, ZSTR_VAL(ret_buf), tent_ret_len);
|
2005-04-05 11:00:04 +08:00
|
|
|
|
2012-01-23 04:30:37 +08:00
|
|
|
if (found_delim) {
|
2011-02-02 02:11:16 +08:00
|
|
|
stream->readpos += delim_len;
|
|
|
|
stream->position += delim_len;
|
2003-02-17 06:19:28 +08:00
|
|
|
}
|
2015-06-30 18:59:27 +08:00
|
|
|
ZSTR_VAL(ret_buf)[ZSTR_LEN(ret_buf)] = '\0';
|
2012-01-23 04:30:37 +08:00
|
|
|
return ret_buf;
|
2003-02-17 06:19:28 +08:00
|
|
|
}
|
|
|
|
|
2003-02-18 09:22:21 +08:00
|
|
|
/* Writes a buffer directly to a stream, using multiple of the chunk size */
|
2014-12-14 06:06:14 +08:00
|
|
|
static size_t _php_stream_write_buffer(php_stream *stream, const char *buf, size_t count)
|
2001-04-18 01:03:18 +08:00
|
|
|
{
|
2002-10-12 10:31:42 +08:00
|
|
|
size_t didwrite = 0, towrite, justwrote;
|
2002-08-21 04:47:47 +08:00
|
|
|
|
2003-02-25 05:40:23 +08:00
|
|
|
/* if we have a seekable stream we need to ensure that data is written at the
|
|
|
|
* current stream->position. This means invalidating the read buffer and then
|
|
|
|
* performing a low-level seek */
|
2005-04-30 03:01:33 +08:00
|
|
|
if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0 && stream->readpos != stream->writepos) {
|
2003-02-25 05:40:23 +08:00
|
|
|
stream->readpos = stream->writepos = 0;
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
stream->ops->seek(stream, stream->position, SEEK_SET, &stream->position);
|
2003-02-25 05:40:23 +08:00
|
|
|
}
|
|
|
|
|
2010-04-08 15:55:35 +08:00
|
|
|
|
2002-10-12 10:31:42 +08:00
|
|
|
while (count > 0) {
|
|
|
|
towrite = count;
|
|
|
|
if (towrite > stream->chunk_size)
|
|
|
|
towrite = stream->chunk_size;
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
justwrote = stream->ops->write(stream, buf, towrite);
|
2003-02-18 09:22:21 +08:00
|
|
|
|
2003-09-11 13:07:26 +08:00
|
|
|
/* convert justwrote to an integer, since normally it is unsigned */
|
|
|
|
if ((int)justwrote > 0) {
|
2002-10-12 10:31:42 +08:00
|
|
|
buf += justwrote;
|
|
|
|
count -= justwrote;
|
|
|
|
didwrite += justwrote;
|
2010-04-08 15:55:35 +08:00
|
|
|
|
2003-02-14 05:08:04 +08:00
|
|
|
/* Only screw with the buffer if we can seek, otherwise we lose data
|
|
|
|
* buffered from fifos and sockets */
|
2003-04-01 07:22:10 +08:00
|
|
|
if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) {
|
2003-02-14 05:08:04 +08:00
|
|
|
stream->position += justwrote;
|
|
|
|
}
|
2002-10-12 10:31:42 +08:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2002-08-21 04:47:47 +08:00
|
|
|
}
|
2002-09-23 09:47:04 +08:00
|
|
|
return didwrite;
|
2003-02-18 09:22:21 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* push some data through the write filter chain.
|
|
|
|
* buf may be NULL, if flags are set to indicate a flush.
|
|
|
|
* This may trigger a real write to the stream.
|
|
|
|
* Returns the number of bytes consumed from buf by the first filter in the chain.
|
|
|
|
* */
|
2014-12-14 06:06:14 +08:00
|
|
|
static size_t _php_stream_write_filtered(php_stream *stream, const char *buf, size_t count, int flags)
|
2003-02-18 09:22:21 +08:00
|
|
|
{
|
|
|
|
size_t consumed = 0;
|
|
|
|
php_stream_bucket *bucket;
|
|
|
|
php_stream_bucket_brigade brig_in = { NULL, NULL }, brig_out = { NULL, NULL };
|
|
|
|
php_stream_bucket_brigade *brig_inp = &brig_in, *brig_outp = &brig_out, *brig_swap;
|
2003-06-15 03:30:42 +08:00
|
|
|
php_stream_filter_status_t status = PSFS_ERR_FATAL;
|
2003-02-18 09:22:21 +08:00
|
|
|
php_stream_filter *filter;
|
|
|
|
|
|
|
|
if (buf) {
|
2014-12-14 06:06:14 +08:00
|
|
|
bucket = php_stream_bucket_new(stream, (char *)buf, count, 0, 0);
|
|
|
|
php_stream_bucket_append(&brig_in, bucket);
|
2003-02-18 09:22:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (filter = stream->writefilters.head; filter; filter = filter->next) {
|
|
|
|
/* for our return value, we are interested in the number of bytes consumed from
|
|
|
|
* the first filter in the chain */
|
|
|
|
status = filter->fops->filter(stream, filter, brig_inp, brig_outp,
|
2014-12-14 06:06:14 +08:00
|
|
|
filter == stream->writefilters.head ? &consumed : NULL, flags);
|
2003-02-18 09:22:21 +08:00
|
|
|
|
|
|
|
if (status != PSFS_PASS_ON) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* brig_out becomes brig_in.
|
|
|
|
* brig_in will always be empty here, as the filter MUST attach any un-consumed buckets
|
|
|
|
* to its own brigade */
|
|
|
|
brig_swap = brig_inp;
|
|
|
|
brig_inp = brig_outp;
|
|
|
|
brig_outp = brig_swap;
|
|
|
|
memset(brig_outp, 0, sizeof(*brig_outp));
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
case PSFS_PASS_ON:
|
|
|
|
/* filter chain generated some output; push it through to the
|
|
|
|
* underlying stream */
|
|
|
|
while (brig_inp->head) {
|
|
|
|
bucket = brig_inp->head;
|
2014-12-14 06:06:14 +08:00
|
|
|
_php_stream_write_buffer(stream, bucket->buf, bucket->buflen);
|
2003-02-18 09:22:21 +08:00
|
|
|
/* Potential error situation - eg: no space on device. Perhaps we should keep this brigade
|
|
|
|
* hanging around and try to write it later.
|
|
|
|
* At the moment, we just drop it on the floor
|
|
|
|
* */
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_bucket_unlink(bucket);
|
|
|
|
php_stream_bucket_delref(bucket);
|
2003-02-18 09:22:21 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PSFS_FEED_ME:
|
|
|
|
/* need more data before we can push data through to the stream */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PSFS_ERR_FATAL:
|
|
|
|
/* some fatal error. Theoretically, the stream is borked, so all
|
|
|
|
* further writes should fail. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int _php_stream_flush(php_stream *stream, int closing)
|
2003-02-18 09:22:21 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2003-02-18 09:22:21 +08:00
|
|
|
if (stream->writefilters.head) {
|
2014-12-14 06:06:14 +08:00
|
|
|
_php_stream_write_filtered(stream, NULL, 0, closing ? PSFS_FLAG_FLUSH_CLOSE : PSFS_FLAG_FLUSH_INC );
|
2003-02-18 09:22:21 +08:00
|
|
|
}
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2015-06-30 09:49:54 +08:00
|
|
|
stream->flags &= ~PHP_STREAM_FLAG_WAS_WRITTEN;
|
|
|
|
|
2003-02-18 09:22:21 +08:00
|
|
|
if (stream->ops->flush) {
|
2014-12-14 06:06:14 +08:00
|
|
|
ret = stream->ops->flush(stream);
|
2003-02-18 09:22:21 +08:00
|
|
|
}
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2003-02-18 09:22:21 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI size_t _php_stream_write(php_stream *stream, const char *buf, size_t count)
|
2003-02-18 09:22:21 +08:00
|
|
|
{
|
2015-06-30 09:49:54 +08:00
|
|
|
size_t bytes;
|
|
|
|
|
2003-04-01 06:13:01 +08:00
|
|
|
if (buf == NULL || count == 0 || stream->ops->write == NULL) {
|
2003-02-18 09:22:21 +08:00
|
|
|
return 0;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2003-02-18 09:22:21 +08:00
|
|
|
|
|
|
|
if (stream->writefilters.head) {
|
2015-06-30 09:49:54 +08:00
|
|
|
bytes = _php_stream_write_filtered(stream, buf, count, PSFS_FLAG_NORMAL);
|
2003-02-18 09:22:21 +08:00
|
|
|
} else {
|
2015-06-30 09:49:54 +08:00
|
|
|
bytes = _php_stream_write_buffer(stream, buf, count);
|
2003-02-18 09:22:21 +08:00
|
|
|
}
|
2015-06-30 09:49:54 +08:00
|
|
|
|
|
|
|
if (bytes) {
|
|
|
|
stream->flags |= PHP_STREAM_FLAG_WAS_WRITTEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes;
|
2001-04-18 01:03:18 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI size_t _php_stream_printf(php_stream *stream, const char *fmt, ...)
|
2002-11-06 08:15:24 +08:00
|
|
|
{
|
|
|
|
size_t count;
|
|
|
|
char *buf;
|
|
|
|
va_list ap;
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2002-11-06 08:15:24 +08:00
|
|
|
va_start(ap, fmt);
|
|
|
|
count = vspprintf(&buf, 0, fmt, ap);
|
|
|
|
va_end(ap);
|
2003-04-01 06:13:01 +08:00
|
|
|
|
|
|
|
if (!buf) {
|
2002-11-06 08:15:24 +08:00
|
|
|
return 0; /* error condition */
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
|
|
|
|
2002-11-06 08:15:24 +08:00
|
|
|
count = php_stream_write(stream, buf, count);
|
|
|
|
efree(buf);
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2002-11-06 08:15:24 +08:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI zend_off_t _php_stream_tell(php_stream *stream)
|
2001-04-18 01:03:18 +08:00
|
|
|
{
|
2002-09-23 09:47:04 +08:00
|
|
|
return stream->position;
|
2001-04-18 01:03:18 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int _php_stream_seek(php_stream *stream, zend_off_t offset, int whence)
|
2001-04-18 01:03:18 +08:00
|
|
|
{
|
2010-11-16 02:22:52 +08:00
|
|
|
if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FOPENCOOKIE) {
|
|
|
|
/* flush to commit data written to the fopencookie FILE* */
|
|
|
|
fflush(stream->stdiocast);
|
|
|
|
}
|
|
|
|
|
2002-09-23 09:47:04 +08:00
|
|
|
/* handle the case where we are in the buffer */
|
2002-10-16 00:04:46 +08:00
|
|
|
if ((stream->flags & PHP_STREAM_FLAG_NO_BUFFER) == 0) {
|
|
|
|
switch(whence) {
|
|
|
|
case SEEK_CUR:
|
2010-10-13 11:13:29 +08:00
|
|
|
if (offset > 0 && offset <= stream->writepos - stream->readpos) {
|
|
|
|
stream->readpos += offset; /* if offset = ..., then readpos = writepos */
|
2002-10-16 00:04:46 +08:00
|
|
|
stream->position += offset;
|
|
|
|
stream->eof = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SEEK_SET:
|
2016-09-24 00:45:03 +08:00
|
|
|
if (offset > stream->position &&
|
2010-10-13 11:13:29 +08:00
|
|
|
offset <= stream->position + stream->writepos - stream->readpos) {
|
2002-10-16 00:04:46 +08:00
|
|
|
stream->readpos += offset - stream->position;
|
|
|
|
stream->position = offset;
|
|
|
|
stream->eof = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2002-09-23 09:47:04 +08:00
|
|
|
}
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2002-09-23 09:47:04 +08:00
|
|
|
|
|
|
|
if (stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) {
|
2002-09-23 21:22:10 +08:00
|
|
|
int ret;
|
2010-04-08 15:55:35 +08:00
|
|
|
|
2003-02-18 09:22:21 +08:00
|
|
|
if (stream->writefilters.head) {
|
2014-12-14 06:06:14 +08:00
|
|
|
_php_stream_flush(stream, 0);
|
2003-02-18 09:22:21 +08:00
|
|
|
}
|
2010-04-08 15:55:35 +08:00
|
|
|
|
2002-09-23 09:47:04 +08:00
|
|
|
switch(whence) {
|
|
|
|
case SEEK_CUR:
|
|
|
|
offset = stream->position + offset;
|
|
|
|
whence = SEEK_SET;
|
|
|
|
break;
|
|
|
|
}
|
2014-12-14 06:06:14 +08:00
|
|
|
ret = stream->ops->seek(stream, offset, whence, &stream->position);
|
2002-09-23 21:22:10 +08:00
|
|
|
|
2002-10-05 18:35:13 +08:00
|
|
|
if (((stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0) || ret == 0) {
|
2003-04-01 06:13:01 +08:00
|
|
|
if (ret == 0) {
|
2002-10-05 18:35:13 +08:00
|
|
|
stream->eof = 0;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2003-02-25 05:40:23 +08:00
|
|
|
|
|
|
|
/* invalidate the buffer contents */
|
|
|
|
stream->readpos = stream->writepos = 0;
|
|
|
|
|
2002-09-23 21:22:10 +08:00
|
|
|
return ret;
|
2002-10-05 18:35:13 +08:00
|
|
|
}
|
2002-09-23 21:22:10 +08:00
|
|
|
/* else the stream has decided that it can't support seeking after all;
|
|
|
|
* fall through to attempt emulation */
|
2002-03-16 08:05:47 +08:00
|
|
|
}
|
2001-04-18 01:03:18 +08:00
|
|
|
|
2002-03-16 05:03:08 +08:00
|
|
|
/* emulate forward moving seeks with reads */
|
2011-06-06 05:57:01 +08:00
|
|
|
if (whence == SEEK_CUR && offset >= 0) {
|
2002-03-16 10:39:39 +08:00
|
|
|
char tmp[1024];
|
2010-10-13 11:13:29 +08:00
|
|
|
size_t didread;
|
|
|
|
while(offset > 0) {
|
|
|
|
if ((didread = php_stream_read(stream, tmp, MIN(offset, sizeof(tmp)))) == 0) {
|
2002-03-16 10:39:39 +08:00
|
|
|
return -1;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2010-10-13 11:13:29 +08:00
|
|
|
offset -= didread;
|
2002-03-16 08:05:47 +08:00
|
|
|
}
|
2002-10-05 18:35:13 +08:00
|
|
|
stream->eof = 0;
|
2002-03-16 05:03:08 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2002-03-17 04:05:08 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
php_error_docref(NULL, E_WARNING, "stream does not support seeking");
|
2002-09-23 09:47:04 +08:00
|
|
|
|
2001-04-18 01:03:18 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int _php_stream_set_option(php_stream *stream, int option, int value, void *ptrparam)
|
2002-08-20 06:59:10 +08:00
|
|
|
{
|
2002-09-23 21:22:10 +08:00
|
|
|
int ret = PHP_STREAM_OPTION_RETURN_NOTIMPL;
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2002-09-23 21:22:10 +08:00
|
|
|
if (stream->ops->set_option) {
|
2014-12-14 06:06:14 +08:00
|
|
|
ret = stream->ops->set_option(stream, option, value, ptrparam);
|
2002-09-23 21:22:10 +08:00
|
|
|
}
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2002-09-23 21:22:10 +08:00
|
|
|
if (ret == PHP_STREAM_OPTION_RETURN_NOTIMPL) {
|
|
|
|
switch(option) {
|
2002-09-29 06:10:47 +08:00
|
|
|
case PHP_STREAM_OPTION_SET_CHUNK_SIZE:
|
2014-10-28 00:28:01 +08:00
|
|
|
/* XXX chunk size itself is of size_t, that might be ok or not for a particular case*/
|
|
|
|
ret = stream->chunk_size > INT_MAX ? INT_MAX : (int)stream->chunk_size;
|
2002-09-29 06:10:47 +08:00
|
|
|
stream->chunk_size = value;
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
case PHP_STREAM_OPTION_READ_BUFFER:
|
2002-09-23 21:22:10 +08:00
|
|
|
/* try to match the buffer mode as best we can */
|
|
|
|
if (value == PHP_STREAM_BUFFER_NONE) {
|
|
|
|
stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
|
2010-05-19 03:39:39 +08:00
|
|
|
} else if (stream->flags & PHP_STREAM_FLAG_NO_BUFFER) {
|
2002-09-23 21:22:10 +08:00
|
|
|
stream->flags ^= PHP_STREAM_FLAG_NO_BUFFER;
|
|
|
|
}
|
|
|
|
ret = PHP_STREAM_OPTION_RETURN_OK;
|
|
|
|
break;
|
2010-04-08 15:55:35 +08:00
|
|
|
|
2002-09-23 21:22:10 +08:00
|
|
|
default:
|
2003-11-29 07:20:23 +08:00
|
|
|
;
|
2002-09-23 21:22:10 +08:00
|
|
|
}
|
|
|
|
}
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2002-09-23 21:22:10 +08:00
|
|
|
return ret;
|
2002-08-20 06:59:10 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int _php_stream_truncate_set_size(php_stream *stream, size_t newsize)
|
2003-02-28 01:43:38 +08:00
|
|
|
{
|
|
|
|
return php_stream_set_option(stream, PHP_STREAM_OPTION_TRUNCATE_API, PHP_STREAM_TRUNCATE_SET_SIZE, &newsize);
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI size_t _php_stream_passthru(php_stream * stream STREAMS_DC)
|
2002-03-21 09:11:52 +08:00
|
|
|
{
|
|
|
|
size_t bcount = 0;
|
|
|
|
char buf[8192];
|
2014-10-28 00:28:01 +08:00
|
|
|
size_t b;
|
2002-03-21 09:11:52 +08:00
|
|
|
|
2003-02-28 01:43:38 +08:00
|
|
|
if (php_stream_mmap_possible(stream)) {
|
|
|
|
char *p;
|
|
|
|
size_t mapped;
|
|
|
|
|
2009-05-17 22:58:10 +08:00
|
|
|
p = php_stream_mmap_range(stream, php_stream_tell(stream), PHP_STREAM_MMAP_ALL, PHP_STREAM_MAP_MODE_SHARED_READONLY, &mapped);
|
2003-02-28 01:43:38 +08:00
|
|
|
|
|
|
|
if (p) {
|
2014-04-03 16:40:06 +08:00
|
|
|
do {
|
|
|
|
/* output functions return int, so pass in int max */
|
2016-06-28 11:11:25 +08:00
|
|
|
if (0 < (b = PHPWRITE(p + bcount, MIN(mapped - bcount, INT_MAX)))) {
|
2014-04-03 16:40:06 +08:00
|
|
|
bcount += b;
|
|
|
|
}
|
|
|
|
} while (b > 0 && mapped > bcount);
|
2003-02-28 01:43:38 +08:00
|
|
|
|
2009-05-17 22:58:10 +08:00
|
|
|
php_stream_mmap_unmap_ex(stream, mapped);
|
2003-02-28 01:43:38 +08:00
|
|
|
|
2014-04-03 16:40:06 +08:00
|
|
|
return bcount;
|
2002-03-21 09:11:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-28 01:43:38 +08:00
|
|
|
while ((b = php_stream_read(stream, buf, sizeof(buf))) > 0) {
|
|
|
|
PHPWRITE(buf, b);
|
|
|
|
bcount += b;
|
2002-03-21 09:11:52 +08:00
|
|
|
}
|
2003-02-28 01:43:38 +08:00
|
|
|
|
2002-03-21 09:11:52 +08:00
|
|
|
return bcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI zend_string *_php_stream_copy_to_mem(php_stream *src, size_t maxlen, int persistent STREAMS_DC)
|
2002-03-16 05:03:08 +08:00
|
|
|
{
|
|
|
|
size_t ret = 0;
|
2002-03-16 09:58:13 +08:00
|
|
|
char *ptr;
|
2002-03-16 05:03:08 +08:00
|
|
|
size_t len = 0, max_len;
|
|
|
|
int step = CHUNK_SIZE;
|
|
|
|
int min_room = CHUNK_SIZE / 4;
|
2003-03-18 22:35:01 +08:00
|
|
|
php_stream_statbuf ssbuf;
|
2014-02-24 18:12:30 +08:00
|
|
|
zend_string *result;
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2010-04-08 15:55:35 +08:00
|
|
|
if (maxlen == 0) {
|
2015-06-29 21:44:54 +08:00
|
|
|
return ZSTR_EMPTY_ALLOC();
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-03-16 21:48:57 +08:00
|
|
|
|
2003-04-01 06:13:01 +08:00
|
|
|
if (maxlen == PHP_STREAM_COPY_ALL) {
|
2002-03-16 21:48:57 +08:00
|
|
|
maxlen = 0;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-03-16 21:48:57 +08:00
|
|
|
|
2004-09-09 02:42:15 +08:00
|
|
|
if (maxlen > 0) {
|
2014-08-26 01:24:55 +08:00
|
|
|
result = zend_string_alloc(maxlen, persistent);
|
2015-06-30 18:59:27 +08:00
|
|
|
ptr = ZSTR_VAL(result);
|
2008-11-11 09:55:51 +08:00
|
|
|
while ((len < maxlen) && !php_stream_eof(src)) {
|
2004-09-09 02:42:15 +08:00
|
|
|
ret = php_stream_read(src, ptr, maxlen - len);
|
2011-05-29 20:29:19 +08:00
|
|
|
if (!ret) {
|
|
|
|
break;
|
|
|
|
}
|
2004-09-09 02:42:15 +08:00
|
|
|
len += ret;
|
|
|
|
ptr += ret;
|
|
|
|
}
|
2012-05-06 18:27:26 +08:00
|
|
|
if (len) {
|
|
|
|
*ptr = '\0';
|
2015-06-30 18:59:27 +08:00
|
|
|
ZSTR_LEN(result) = len;
|
2012-05-06 18:27:26 +08:00
|
|
|
} else {
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string_free(result);
|
2014-02-24 18:12:30 +08:00
|
|
|
result = NULL;
|
2012-05-06 18:27:26 +08:00
|
|
|
}
|
2014-02-24 18:12:30 +08:00
|
|
|
return result;
|
2004-09-09 02:42:15 +08:00
|
|
|
}
|
|
|
|
|
2003-03-19 06:26:02 +08:00
|
|
|
/* avoid many reallocs by allocating a good sized chunk to begin with, if
|
|
|
|
* we can. Note that the stream may be filtered, in which case the stat
|
|
|
|
* result may be inaccurate, as the filter may inflate or deflate the
|
|
|
|
* number of bytes that we can read. In order to avoid an upsize followed
|
|
|
|
* by a downsize of the buffer, overestimate by the step size (which is
|
|
|
|
* 2K). */
|
2003-03-18 22:35:01 +08:00
|
|
|
if (php_stream_stat(src, &ssbuf) == 0 && ssbuf.sb.st_size > 0) {
|
|
|
|
max_len = ssbuf.sb.st_size + step;
|
|
|
|
} else {
|
|
|
|
max_len = step;
|
|
|
|
}
|
2002-03-17 04:05:08 +08:00
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
result = zend_string_alloc(max_len, persistent);
|
2015-06-30 18:59:27 +08:00
|
|
|
ptr = ZSTR_VAL(result);
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2014-02-24 18:12:30 +08:00
|
|
|
while ((ret = php_stream_read(src, ptr, max_len - len))) {
|
2002-03-16 05:03:08 +08:00
|
|
|
len += ret;
|
2002-03-16 08:05:47 +08:00
|
|
|
if (len + min_room >= max_len) {
|
2015-03-20 07:02:42 +08:00
|
|
|
result = zend_string_extend(result, max_len + step, persistent);
|
2002-03-16 05:03:08 +08:00
|
|
|
max_len += step;
|
2015-06-30 18:59:27 +08:00
|
|
|
ptr = ZSTR_VAL(result) + len;
|
2004-07-13 04:59:24 +08:00
|
|
|
} else {
|
|
|
|
ptr += ret;
|
2002-03-16 05:03:08 +08:00
|
|
|
}
|
|
|
|
}
|
2002-03-16 08:05:47 +08:00
|
|
|
if (len) {
|
2015-03-20 07:02:42 +08:00
|
|
|
result = zend_string_truncate(result, len, persistent);
|
2015-06-30 18:59:27 +08:00
|
|
|
ZSTR_VAL(result)[len] = '\0';
|
2002-03-16 08:05:47 +08:00
|
|
|
} else {
|
2014-08-26 01:24:55 +08:00
|
|
|
zend_string_free(result);
|
2014-02-24 18:12:30 +08:00
|
|
|
result = NULL;
|
2002-03-16 05:03:08 +08:00
|
|
|
}
|
2014-02-24 18:12:30 +08:00
|
|
|
|
|
|
|
return result;
|
2002-03-16 05:03:08 +08:00
|
|
|
}
|
|
|
|
|
2009-04-20 01:10:35 +08:00
|
|
|
/* Returns SUCCESS/FAILURE and sets *len to the number of bytes moved */
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int _php_stream_copy_to_stream_ex(php_stream *src, php_stream *dest, size_t maxlen, size_t *len STREAMS_DC)
|
2002-03-16 05:03:08 +08:00
|
|
|
{
|
|
|
|
char buf[CHUNK_SIZE];
|
|
|
|
size_t readchunk;
|
|
|
|
size_t haveread = 0;
|
2013-04-02 22:18:26 +08:00
|
|
|
size_t didread, didwrite, towrite;
|
2009-04-20 01:10:35 +08:00
|
|
|
size_t dummy;
|
2003-02-19 03:03:44 +08:00
|
|
|
php_stream_statbuf ssbuf;
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2009-04-20 01:10:35 +08:00
|
|
|
if (!len) {
|
|
|
|
len = &dummy;
|
|
|
|
}
|
|
|
|
|
2003-04-01 06:13:01 +08:00
|
|
|
if (maxlen == 0) {
|
2009-04-20 01:10:35 +08:00
|
|
|
*len = 0;
|
|
|
|
return SUCCESS;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-03-16 10:48:35 +08:00
|
|
|
|
2003-04-01 06:13:01 +08:00
|
|
|
if (maxlen == PHP_STREAM_COPY_ALL) {
|
2002-03-16 10:48:35 +08:00
|
|
|
maxlen = 0;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-03-17 04:05:08 +08:00
|
|
|
|
2003-02-19 03:03:44 +08:00
|
|
|
if (php_stream_stat(src, &ssbuf) == 0) {
|
2003-03-18 06:25:55 +08:00
|
|
|
if (ssbuf.sb.st_size == 0
|
2009-05-17 04:23:06 +08:00
|
|
|
#ifdef S_ISREG
|
|
|
|
&& S_ISREG(ssbuf.sb.st_mode)
|
2003-03-18 06:25:55 +08:00
|
|
|
#endif
|
|
|
|
) {
|
2009-04-20 01:10:35 +08:00
|
|
|
*len = 0;
|
|
|
|
return SUCCESS;
|
2003-02-16 03:56:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-28 01:43:38 +08:00
|
|
|
if (php_stream_mmap_possible(src)) {
|
|
|
|
char *p;
|
|
|
|
size_t mapped;
|
|
|
|
|
|
|
|
p = php_stream_mmap_range(src, php_stream_tell(src), maxlen, PHP_STREAM_MAP_MODE_SHARED_READONLY, &mapped);
|
|
|
|
|
|
|
|
if (p) {
|
2013-04-02 22:18:26 +08:00
|
|
|
didwrite = php_stream_write(dest, p, mapped);
|
2003-02-28 01:43:38 +08:00
|
|
|
|
2009-05-17 22:58:10 +08:00
|
|
|
php_stream_mmap_unmap_ex(src, mapped);
|
2009-04-20 01:10:35 +08:00
|
|
|
|
2013-04-02 22:18:26 +08:00
|
|
|
*len = didwrite;
|
2010-04-08 15:55:35 +08:00
|
|
|
|
2013-04-02 22:31:36 +08:00
|
|
|
/* we've got at least 1 byte to read
|
|
|
|
* less than 1 is an error
|
|
|
|
* AND read bytes match written */
|
|
|
|
if (mapped > 0 && mapped == didwrite) {
|
2009-04-20 01:10:35 +08:00
|
|
|
return SUCCESS;
|
2009-04-19 21:46:47 +08:00
|
|
|
}
|
2009-04-20 01:10:35 +08:00
|
|
|
return FAILURE;
|
2003-02-28 01:43:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-16 08:05:47 +08:00
|
|
|
while(1) {
|
2002-03-16 05:03:08 +08:00
|
|
|
readchunk = sizeof(buf);
|
|
|
|
|
2007-11-06 19:02:36 +08:00
|
|
|
if (maxlen && (maxlen - haveread) < readchunk) {
|
2002-03-16 05:03:08 +08:00
|
|
|
readchunk = maxlen - haveread;
|
2007-11-06 19:02:36 +08:00
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
|
|
|
|
didread = php_stream_read(src, buf, readchunk);
|
2002-03-20 07:29:37 +08:00
|
|
|
|
2002-03-16 08:05:47 +08:00
|
|
|
if (didread) {
|
2002-03-16 05:03:08 +08:00
|
|
|
/* extra paranoid */
|
2002-03-16 08:05:47 +08:00
|
|
|
char *writeptr;
|
2002-03-16 05:03:08 +08:00
|
|
|
|
|
|
|
towrite = didread;
|
|
|
|
writeptr = buf;
|
|
|
|
haveread += didread;
|
2002-03-17 04:05:08 +08:00
|
|
|
|
2002-03-16 08:05:47 +08:00
|
|
|
while(towrite) {
|
2002-03-16 05:03:08 +08:00
|
|
|
didwrite = php_stream_write(dest, writeptr, towrite);
|
2003-04-01 06:13:01 +08:00
|
|
|
if (didwrite == 0) {
|
2009-04-20 01:10:35 +08:00
|
|
|
*len = haveread - (didread - towrite);
|
|
|
|
return FAILURE;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-03-17 04:05:08 +08:00
|
|
|
|
2002-03-16 05:03:08 +08:00
|
|
|
towrite -= didwrite;
|
|
|
|
writeptr += didwrite;
|
|
|
|
}
|
2002-03-16 08:05:47 +08:00
|
|
|
} else {
|
2009-04-19 21:46:47 +08:00
|
|
|
break;
|
2002-03-16 08:05:47 +08:00
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2002-03-16 08:05:47 +08:00
|
|
|
if (maxlen - haveread == 0) {
|
2002-03-16 05:03:08 +08:00
|
|
|
break;
|
2002-03-16 08:05:47 +08:00
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
}
|
2002-03-17 04:05:08 +08:00
|
|
|
|
2009-04-20 01:10:35 +08:00
|
|
|
*len = haveread;
|
|
|
|
|
2010-04-08 15:55:35 +08:00
|
|
|
/* we've got at least 1 byte to read.
|
2009-04-19 21:46:47 +08:00
|
|
|
* less than 1 is an error */
|
|
|
|
|
2010-01-06 20:54:53 +08:00
|
|
|
if (haveread > 0 || src->eof) {
|
2009-04-20 01:10:35 +08:00
|
|
|
return SUCCESS;
|
2009-04-19 21:46:47 +08:00
|
|
|
}
|
2009-04-20 01:10:35 +08:00
|
|
|
return FAILURE;
|
2009-04-19 21:46:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the number of bytes moved.
|
2010-04-08 15:55:35 +08:00
|
|
|
* Returns 1 when source len is 0.
|
2009-04-19 21:46:47 +08:00
|
|
|
* Deprecated in favor of php_stream_copy_to_stream_ex() */
|
|
|
|
ZEND_ATTRIBUTE_DEPRECATED
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC)
|
2009-04-19 21:46:47 +08:00
|
|
|
{
|
2009-04-20 01:10:35 +08:00
|
|
|
size_t len;
|
2014-12-14 06:06:14 +08:00
|
|
|
int ret = _php_stream_copy_to_stream_ex(src, dest, maxlen, &len STREAMS_REL_CC);
|
2009-04-20 01:10:35 +08:00
|
|
|
if (ret == SUCCESS && len == 0 && maxlen != 0) {
|
2009-04-19 21:46:47 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2009-04-20 01:10:35 +08:00
|
|
|
return len;
|
2002-03-16 05:03:08 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-02-17 06:19:28 +08:00
|
|
|
/* {{{ wrapper init and registration */
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static void stream_resource_regular_dtor(zend_resource *rsrc)
|
2002-03-16 05:03:08 +08:00
|
|
|
{
|
2003-02-17 06:19:28 +08:00
|
|
|
php_stream *stream = (php_stream*)rsrc->ptr;
|
|
|
|
/* set the return value for pclose */
|
|
|
|
FG(pclose_ret) = php_stream_free(stream, PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_RSRC_DTOR);
|
|
|
|
}
|
2002-03-17 04:05:08 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static void stream_resource_persistent_dtor(zend_resource *rsrc)
|
2003-02-17 06:19:28 +08:00
|
|
|
{
|
|
|
|
php_stream *stream = (php_stream*)rsrc->ptr;
|
|
|
|
FG(pclose_ret) = php_stream_free(stream, PHP_STREAM_FREE_CLOSE | PHP_STREAM_FREE_RSRC_DTOR);
|
2002-03-16 05:03:08 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
void php_shutdown_stream_hashes(void)
|
2005-05-16 16:37:14 +08:00
|
|
|
{
|
|
|
|
if (FG(stream_wrappers)) {
|
|
|
|
zend_hash_destroy(FG(stream_wrappers));
|
|
|
|
efree(FG(stream_wrappers));
|
|
|
|
FG(stream_wrappers) = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FG(stream_filters)) {
|
|
|
|
zend_hash_destroy(FG(stream_filters));
|
|
|
|
efree(FG(stream_filters));
|
|
|
|
FG(stream_filters) = NULL;
|
|
|
|
}
|
2015-01-03 17:22:58 +08:00
|
|
|
|
2012-03-08 20:30:59 +08:00
|
|
|
if (FG(wrapper_errors)) {
|
|
|
|
zend_hash_destroy(FG(wrapper_errors));
|
|
|
|
efree(FG(wrapper_errors));
|
|
|
|
FG(wrapper_errors) = NULL;
|
|
|
|
}
|
2005-05-16 16:37:14 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
int php_init_stream_wrappers(int module_number)
|
2002-03-16 05:03:08 +08:00
|
|
|
{
|
2003-02-17 06:19:28 +08:00
|
|
|
le_stream = zend_register_list_destructors_ex(stream_resource_regular_dtor, NULL, "stream", module_number);
|
|
|
|
le_pstream = zend_register_list_destructors_ex(NULL, stream_resource_persistent_dtor, "persistent stream", module_number);
|
2002-03-17 04:05:08 +08:00
|
|
|
|
2004-09-14 11:48:17 +08:00
|
|
|
/* Filters are cleaned up by the streams they're attached to */
|
|
|
|
le_stream_filter = zend_register_list_destructors_ex(NULL, NULL, "stream filter", module_number);
|
|
|
|
|
2014-04-21 22:25:34 +08:00
|
|
|
zend_hash_init(&url_stream_wrappers_hash, 8, NULL, NULL, 1);
|
|
|
|
zend_hash_init(php_get_stream_filters_hash_global(), 8, NULL, NULL, 1);
|
|
|
|
zend_hash_init(php_stream_xport_get_hash(), 8, NULL, NULL, 1);
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
return (php_stream_xport_register("tcp", php_stream_generic_socket_factory) == SUCCESS
|
2003-02-28 01:43:38 +08:00
|
|
|
&&
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_xport_register("udp", php_stream_generic_socket_factory) == SUCCESS
|
2016-11-12 18:20:01 +08:00
|
|
|
#if defined(AF_UNIX) && !(defined(PHP_WIN32) || defined(__riscos__))
|
2003-02-28 01:43:38 +08:00
|
|
|
&&
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_xport_register("unix", php_stream_generic_socket_factory) == SUCCESS
|
2003-02-28 01:43:38 +08:00
|
|
|
&&
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_xport_register("udg", php_stream_generic_socket_factory) == SUCCESS
|
2003-02-28 01:43:38 +08:00
|
|
|
#endif
|
2003-02-17 06:19:28 +08:00
|
|
|
) ? SUCCESS : FAILURE;
|
|
|
|
}
|
2002-11-18 19:39:42 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
int php_shutdown_stream_wrappers(int module_number)
|
2003-02-17 06:19:28 +08:00
|
|
|
{
|
|
|
|
zend_hash_destroy(&url_stream_wrappers_hash);
|
2004-07-14 00:34:56 +08:00
|
|
|
zend_hash_destroy(php_get_stream_filters_hash_global());
|
2003-02-28 01:43:38 +08:00
|
|
|
zend_hash_destroy(php_stream_xport_get_hash());
|
2003-02-17 06:19:28 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
2002-11-18 19:39:42 +08:00
|
|
|
|
2005-04-10 03:36:49 +08:00
|
|
|
/* Validate protocol scheme names during registration
|
|
|
|
* Must conform to /^[a-zA-Z0-9+.-]+$/
|
|
|
|
*/
|
2013-07-30 18:49:36 +08:00
|
|
|
static inline int php_stream_wrapper_scheme_validate(const char *protocol, unsigned int protocol_len)
|
2003-02-17 06:19:28 +08:00
|
|
|
{
|
2013-07-30 18:49:36 +08:00
|
|
|
unsigned int i;
|
2005-04-05 03:34:32 +08:00
|
|
|
|
|
|
|
for(i = 0; i < protocol_len; i++) {
|
|
|
|
if (!isalnum((int)protocol[i]) &&
|
|
|
|
protocol[i] != '+' &&
|
|
|
|
protocol[i] != '-' &&
|
|
|
|
protocol[i] != '.') {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-10 03:36:49 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* API for registering GLOBAL wrappers */
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int php_register_url_stream_wrapper(const char *protocol, php_stream_wrapper *wrapper)
|
2005-04-10 03:36:49 +08:00
|
|
|
{
|
2014-10-28 00:28:01 +08:00
|
|
|
unsigned int protocol_len = (unsigned int)strlen(protocol);
|
2005-04-10 03:36:49 +08:00
|
|
|
|
|
|
|
if (php_stream_wrapper_scheme_validate(protocol, protocol_len) == FAILURE) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-03-04 17:39:13 +08:00
|
|
|
return zend_hash_add_ptr(&url_stream_wrappers_hash, zend_string_init_interned(protocol, protocol_len, 1), wrapper) ? SUCCESS : FAILURE;
|
2003-02-17 06:19:28 +08:00
|
|
|
}
|
2002-11-18 19:39:42 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int php_unregister_url_stream_wrapper(const char *protocol)
|
2003-02-17 06:19:28 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
return zend_hash_str_del(&url_stream_wrappers_hash, protocol, strlen(protocol));
|
2002-03-16 05:03:08 +08:00
|
|
|
}
|
2004-06-22 02:58:55 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
static void clone_wrapper_hash(void)
|
2006-04-23 01:17:40 +08:00
|
|
|
{
|
|
|
|
ALLOC_HASHTABLE(FG(stream_wrappers));
|
2006-10-04 03:51:01 +08:00
|
|
|
zend_hash_init(FG(stream_wrappers), zend_hash_num_elements(&url_stream_wrappers_hash), NULL, NULL, 1);
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_hash_copy(FG(stream_wrappers), &url_stream_wrappers_hash, NULL);
|
2006-04-23 01:17:40 +08:00
|
|
|
}
|
|
|
|
|
2004-06-22 02:58:55 +08:00
|
|
|
/* API for registering VOLATILE wrappers */
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int php_register_url_stream_wrapper_volatile(const char *protocol, php_stream_wrapper *wrapper)
|
2004-06-22 02:58:55 +08:00
|
|
|
{
|
2014-10-28 00:28:01 +08:00
|
|
|
unsigned int protocol_len = (unsigned int)strlen(protocol);
|
2005-04-05 03:34:32 +08:00
|
|
|
|
2005-04-10 03:36:49 +08:00
|
|
|
if (php_stream_wrapper_scheme_validate(protocol, protocol_len) == FAILURE) {
|
|
|
|
return FAILURE;
|
2005-04-05 03:34:32 +08:00
|
|
|
}
|
|
|
|
|
2004-06-22 02:58:55 +08:00
|
|
|
if (!FG(stream_wrappers)) {
|
2014-12-14 06:06:14 +08:00
|
|
|
clone_wrapper_hash();
|
2004-06-22 02:58:55 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
return zend_hash_str_add_ptr(FG(stream_wrappers), protocol, protocol_len, wrapper) ? SUCCESS : FAILURE;
|
2004-06-22 02:58:55 +08:00
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int php_unregister_url_stream_wrapper_volatile(const char *protocol)
|
2004-09-11 04:45:35 +08:00
|
|
|
{
|
|
|
|
if (!FG(stream_wrappers)) {
|
2014-12-14 06:06:14 +08:00
|
|
|
clone_wrapper_hash();
|
2004-09-11 04:45:35 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
return zend_hash_str_del(FG(stream_wrappers), protocol, strlen(protocol));
|
2004-09-11 04:45:35 +08:00
|
|
|
}
|
2003-02-17 06:19:28 +08:00
|
|
|
/* }}} */
|
2001-04-18 01:03:18 +08:00
|
|
|
|
2003-02-17 06:19:28 +08:00
|
|
|
/* {{{ php_stream_locate_url_wrapper */
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI php_stream_wrapper *php_stream_locate_url_wrapper(const char *path, const char **path_for_open, int options)
|
2003-02-13 21:40:34 +08:00
|
|
|
{
|
2004-06-22 02:58:55 +08:00
|
|
|
HashTable *wrapper_hash = (FG(stream_wrappers) ? FG(stream_wrappers) : &url_stream_wrappers_hash);
|
2014-02-10 14:04:30 +08:00
|
|
|
php_stream_wrapper *wrapper = NULL;
|
2003-02-17 06:19:28 +08:00
|
|
|
const char *p, *protocol = NULL;
|
2016-06-22 01:12:29 +08:00
|
|
|
size_t n = 0;
|
2003-02-17 06:19:28 +08:00
|
|
|
|
2003-04-01 06:13:01 +08:00
|
|
|
if (path_for_open) {
|
2003-02-17 06:19:28 +08:00
|
|
|
*path_for_open = (char*)path;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2003-02-17 06:19:28 +08:00
|
|
|
|
2003-04-01 06:13:01 +08:00
|
|
|
if (options & IGNORE_URL) {
|
2003-02-17 06:19:28 +08:00
|
|
|
return (options & STREAM_LOCATE_WRAPPERS_ONLY) ? NULL : &php_plain_files_wrapper;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
|
|
|
|
2003-02-17 06:19:28 +08:00
|
|
|
for (p = path; isalnum((int)*p) || *p == '+' || *p == '-' || *p == '.'; p++) {
|
|
|
|
n++;
|
2003-02-13 21:40:34 +08:00
|
|
|
}
|
2003-02-16 11:48:49 +08:00
|
|
|
|
2008-11-03 23:47:31 +08:00
|
|
|
if ((*p == ':') && (n > 1) && (!strncmp("//", p+1, 2) || (n == 4 && !memcmp("data:", path, 5)))) {
|
2003-02-17 06:19:28 +08:00
|
|
|
protocol = path;
|
2003-02-16 11:48:49 +08:00
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2007-01-16 01:07:08 +08:00
|
|
|
if (protocol) {
|
|
|
|
char *tmp = estrndup(protocol, n);
|
2014-02-10 14:04:30 +08:00
|
|
|
if (NULL == (wrapper = zend_hash_str_find_ptr(wrapper_hash, (char*)tmp, n))) {
|
2006-09-04 00:32:27 +08:00
|
|
|
php_strtolower(tmp, n);
|
2014-02-10 14:04:30 +08:00
|
|
|
if (NULL == (wrapper = zend_hash_str_find_ptr(wrapper_hash, (char*)tmp, n))) {
|
2006-09-04 00:32:27 +08:00
|
|
|
char wrapper_name[32];
|
|
|
|
|
|
|
|
if (n >= sizeof(wrapper_name)) {
|
|
|
|
n = sizeof(wrapper_name) - 1;
|
|
|
|
}
|
|
|
|
PHP_STRLCPY(wrapper_name, protocol, sizeof(wrapper_name), n);
|
2008-12-15 00:25:37 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
php_error_docref(NULL, E_WARNING, "Unable to find the wrapper \"%s\" - did you forget to enable it when you configured PHP?", wrapper_name);
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
wrapper = NULL;
|
2006-09-04 00:32:27 +08:00
|
|
|
protocol = NULL;
|
|
|
|
}
|
2003-02-17 06:19:28 +08:00
|
|
|
}
|
2007-01-16 01:07:08 +08:00
|
|
|
efree(tmp);
|
2002-08-25 19:23:19 +08:00
|
|
|
}
|
2003-02-17 06:19:28 +08:00
|
|
|
/* TODO: curl based streams probably support file:// properly */
|
|
|
|
if (!protocol || !strncasecmp(protocol, "file", n)) {
|
2007-11-06 19:02:36 +08:00
|
|
|
/* fall back on regular file access */
|
|
|
|
php_stream_wrapper *plain_files_wrapper = &php_plain_files_wrapper;
|
|
|
|
|
2004-11-11 21:08:32 +08:00
|
|
|
if (protocol) {
|
|
|
|
int localhost = 0;
|
|
|
|
|
|
|
|
if (!strncasecmp(path, "file://localhost/", 17)) {
|
|
|
|
localhost = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef PHP_WIN32
|
|
|
|
if (localhost == 0 && path[n+3] != '\0' && path[n+3] != '/' && path[n+4] != ':') {
|
2004-08-31 19:37:02 +08:00
|
|
|
#else
|
2007-07-11 22:33:30 +08:00
|
|
|
if (localhost == 0 && path[n+3] != '\0' && path[n+3] != '/') {
|
2004-08-31 19:37:02 +08:00
|
|
|
#endif
|
2004-11-11 21:08:32 +08:00
|
|
|
if (options & REPORT_ERRORS) {
|
2014-12-14 06:06:14 +08:00
|
|
|
php_error_docref(NULL, E_WARNING, "remote host file access not supported, %s", path);
|
2004-11-11 21:08:32 +08:00
|
|
|
}
|
|
|
|
return NULL;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2004-11-11 21:08:32 +08:00
|
|
|
|
|
|
|
if (path_for_open) {
|
|
|
|
/* skip past protocol and :/, but handle windows correctly */
|
|
|
|
*path_for_open = (char*)path + n + 1;
|
|
|
|
if (localhost == 1) {
|
|
|
|
(*path_for_open) += 11;
|
|
|
|
}
|
2015-03-04 11:34:53 +08:00
|
|
|
while (*(++*path_for_open)=='/') {
|
|
|
|
/* intentionally empty */
|
|
|
|
}
|
2003-10-20 04:59:12 +08:00
|
|
|
#ifdef PHP_WIN32
|
2004-11-11 21:08:32 +08:00
|
|
|
if (*(*path_for_open + 1) != ':')
|
2003-10-20 04:59:12 +08:00
|
|
|
#endif
|
2004-11-11 21:08:32 +08:00
|
|
|
(*path_for_open)--;
|
|
|
|
}
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2004-09-11 05:50:29 +08:00
|
|
|
|
|
|
|
if (options & STREAM_LOCATE_WRAPPERS_ONLY) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-12-15 00:25:37 +08:00
|
|
|
|
2004-09-11 05:50:29 +08:00
|
|
|
if (FG(stream_wrappers)) {
|
2008-12-15 00:25:37 +08:00
|
|
|
/* The file:// wrapper may have been disabled/overridden */
|
2004-09-11 05:50:29 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (wrapper) {
|
2004-09-11 05:50:29 +08:00
|
|
|
/* It was found so go ahead and provide it */
|
2014-02-10 14:04:30 +08:00
|
|
|
return wrapper;
|
2004-09-11 05:50:29 +08:00
|
|
|
}
|
2008-12-15 00:25:37 +08:00
|
|
|
|
2004-09-11 05:50:29 +08:00
|
|
|
/* Check again, the original check might have not known the protocol name */
|
2014-02-10 14:04:30 +08:00
|
|
|
if ((wrapper = zend_hash_str_find_ptr(wrapper_hash, "file", sizeof("file")-1)) != NULL) {
|
|
|
|
return wrapper;
|
2004-09-11 05:50:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options & REPORT_ERRORS) {
|
2014-12-14 06:06:14 +08:00
|
|
|
php_error_docref(NULL, E_WARNING, "file:// wrapper is disabled in the server configuration");
|
2004-09-11 05:50:29 +08:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-04-08 15:55:35 +08:00
|
|
|
|
2007-11-06 19:02:36 +08:00
|
|
|
return plain_files_wrapper;
|
2003-02-16 11:48:49 +08:00
|
|
|
}
|
2002-12-20 04:23:50 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (wrapper && wrapper->is_url &&
|
2007-07-10 01:27:24 +08:00
|
|
|
(options & STREAM_DISABLE_URL_PROTECTION) == 0 &&
|
2010-04-08 15:55:35 +08:00
|
|
|
(!PG(allow_url_fopen) ||
|
2007-07-10 19:57:34 +08:00
|
|
|
(((options & STREAM_OPEN_FOR_INCLUDE) ||
|
|
|
|
PG(in_user_include)) && !PG(allow_url_include)))) {
|
2003-04-01 06:13:01 +08:00
|
|
|
if (options & REPORT_ERRORS) {
|
2007-11-06 19:02:36 +08:00
|
|
|
/* protocol[n] probably isn't '\0' */
|
|
|
|
char *protocol_dup = estrndup(protocol, n);
|
2008-01-12 23:50:57 +08:00
|
|
|
if (!PG(allow_url_fopen)) {
|
2014-12-14 06:06:14 +08:00
|
|
|
php_error_docref(NULL, E_WARNING, "%s:// wrapper is disabled in the server configuration by allow_url_fopen=0", protocol_dup);
|
2008-01-12 23:50:57 +08:00
|
|
|
} else {
|
2014-12-14 06:06:14 +08:00
|
|
|
php_error_docref(NULL, E_WARNING, "%s:// wrapper is disabled in the server configuration by allow_url_include=0", protocol_dup);
|
2008-01-12 23:50:57 +08:00
|
|
|
}
|
2007-11-06 19:02:36 +08:00
|
|
|
efree(protocol_dup);
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2003-02-17 06:19:28 +08:00
|
|
|
return NULL;
|
2003-01-01 17:58:17 +08:00
|
|
|
}
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
return wrapper;
|
2002-03-16 05:03:08 +08:00
|
|
|
}
|
2003-02-17 06:19:28 +08:00
|
|
|
/* }}} */
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2003-12-13 12:07:18 +08:00
|
|
|
/* {{{ _php_stream_mkdir
|
|
|
|
*/
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int _php_stream_mkdir(const char *path, int mode, int options, php_stream_context *context)
|
2003-12-13 12:07:18 +08:00
|
|
|
{
|
|
|
|
php_stream_wrapper *wrapper = NULL;
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
wrapper = php_stream_locate_url_wrapper(path, NULL, 0);
|
2003-12-14 02:48:39 +08:00
|
|
|
if (!wrapper || !wrapper->wops || !wrapper->wops->stream_mkdir) {
|
2003-12-13 12:07:18 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
return wrapper->wops->stream_mkdir(wrapper, path, mode, options, context);
|
2003-12-13 12:07:18 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ _php_stream_rmdir
|
|
|
|
*/
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int _php_stream_rmdir(const char *path, int options, php_stream_context *context)
|
2003-12-13 12:07:18 +08:00
|
|
|
{
|
|
|
|
php_stream_wrapper *wrapper = NULL;
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
wrapper = php_stream_locate_url_wrapper(path, NULL, 0);
|
2003-12-14 02:48:39 +08:00
|
|
|
if (!wrapper || !wrapper->wops || !wrapper->wops->stream_rmdir) {
|
2003-12-13 12:07:18 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
return wrapper->wops->stream_rmdir(wrapper, path, options, context);
|
2003-12-13 12:07:18 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-02-17 06:19:28 +08:00
|
|
|
/* {{{ _php_stream_stat_path */
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI int _php_stream_stat_path(const char *path, int flags, php_stream_statbuf *ssb, php_stream_context *context)
|
2002-04-17 06:14:27 +08:00
|
|
|
{
|
|
|
|
php_stream_wrapper *wrapper = NULL;
|
2013-07-30 18:49:36 +08:00
|
|
|
const char *path_to_open = path;
|
2003-11-29 07:25:28 +08:00
|
|
|
int ret;
|
|
|
|
|
2014-04-14 16:02:11 +08:00
|
|
|
if (!(flags & PHP_STREAM_URL_STAT_NOCACHE)) {
|
|
|
|
/* Try to hit the cache first */
|
|
|
|
if (flags & PHP_STREAM_URL_STAT_LINK) {
|
|
|
|
if (BG(CurrentLStatFile) && strcmp(path, BG(CurrentLStatFile)) == 0) {
|
|
|
|
memcpy(ssb, &BG(lssb), sizeof(php_stream_statbuf));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (BG(CurrentStatFile) && strcmp(path, BG(CurrentStatFile)) == 0) {
|
|
|
|
memcpy(ssb, &BG(ssb), sizeof(php_stream_statbuf));
|
|
|
|
return 0;
|
|
|
|
}
|
2003-11-29 07:25:28 +08:00
|
|
|
}
|
|
|
|
}
|
2002-04-17 06:14:27 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
wrapper = php_stream_locate_url_wrapper(path, &path_to_open, 0);
|
2002-04-17 06:14:27 +08:00
|
|
|
if (wrapper && wrapper->wops->url_stat) {
|
2014-12-14 06:06:14 +08:00
|
|
|
ret = wrapper->wops->url_stat(wrapper, path_to_open, flags, ssb, context);
|
2003-11-29 07:25:28 +08:00
|
|
|
if (ret == 0) {
|
2014-04-14 16:02:11 +08:00
|
|
|
if (!(flags & PHP_STREAM_URL_STAT_NOCACHE)) {
|
|
|
|
/* Drop into cache */
|
|
|
|
if (flags & PHP_STREAM_URL_STAT_LINK) {
|
|
|
|
if (BG(CurrentLStatFile)) {
|
|
|
|
efree(BG(CurrentLStatFile));
|
|
|
|
}
|
|
|
|
BG(CurrentLStatFile) = estrdup(path);
|
|
|
|
memcpy(&BG(lssb), ssb, sizeof(php_stream_statbuf));
|
|
|
|
} else {
|
|
|
|
if (BG(CurrentStatFile)) {
|
|
|
|
efree(BG(CurrentStatFile));
|
|
|
|
}
|
|
|
|
BG(CurrentStatFile) = estrdup(path);
|
|
|
|
memcpy(&BG(ssb), ssb, sizeof(php_stream_statbuf));
|
2003-11-29 07:25:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2002-04-17 06:14:27 +08:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2003-02-17 06:19:28 +08:00
|
|
|
/* }}} */
|
2002-04-17 06:14:27 +08:00
|
|
|
|
|
|
|
/* {{{ php_stream_opendir */
|
2013-07-30 18:49:36 +08:00
|
|
|
PHPAPI php_stream *_php_stream_opendir(const char *path, int options,
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_context *context STREAMS_DC)
|
2002-04-17 06:14:27 +08:00
|
|
|
{
|
|
|
|
php_stream *stream = NULL;
|
|
|
|
php_stream_wrapper *wrapper = NULL;
|
2013-07-30 18:49:36 +08:00
|
|
|
const char *path_to_open;
|
2003-04-01 06:13:01 +08:00
|
|
|
|
|
|
|
if (!path || !*path) {
|
2002-04-17 06:14:27 +08:00
|
|
|
return NULL;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-04-17 06:14:27 +08:00
|
|
|
|
|
|
|
path_to_open = path;
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
wrapper = php_stream_locate_url_wrapper(path, &path_to_open, options);
|
2002-04-17 06:14:27 +08:00
|
|
|
|
2003-04-01 06:13:01 +08:00
|
|
|
if (wrapper && wrapper->wops->dir_opener) {
|
2002-04-17 06:14:27 +08:00
|
|
|
stream = wrapper->wops->dir_opener(wrapper,
|
2002-09-28 21:05:47 +08:00
|
|
|
path_to_open, "r", options ^ REPORT_ERRORS, NULL,
|
2014-12-14 06:06:14 +08:00
|
|
|
context STREAMS_REL_CC);
|
2002-04-17 06:14:27 +08:00
|
|
|
|
2002-09-23 09:47:04 +08:00
|
|
|
if (stream) {
|
2002-04-17 06:14:27 +08:00
|
|
|
stream->wrapper = wrapper;
|
2008-06-11 17:01:56 +08:00
|
|
|
stream->flags |= PHP_STREAM_FLAG_NO_BUFFER | PHP_STREAM_FLAG_IS_DIR;
|
2002-09-23 09:47:04 +08:00
|
|
|
}
|
2002-09-28 21:05:47 +08:00
|
|
|
} else if (wrapper) {
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_wrapper_log_error(wrapper, options ^ REPORT_ERRORS, "not implemented");
|
2002-04-17 06:14:27 +08:00
|
|
|
}
|
|
|
|
if (stream == NULL && (options & REPORT_ERRORS)) {
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_display_wrapper_errors(wrapper, path, "failed to open dir");
|
2002-04-17 06:14:27 +08:00
|
|
|
}
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_tidy_wrapper_error_log(wrapper);
|
2002-09-28 21:05:47 +08:00
|
|
|
|
2002-04-17 06:14:27 +08:00
|
|
|
return stream;
|
2002-03-16 05:03:08 +08:00
|
|
|
}
|
2002-03-20 22:21:30 +08:00
|
|
|
/* }}} */
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2003-02-17 06:19:28 +08:00
|
|
|
/* {{{ _php_stream_readdir */
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI php_stream_dirent *_php_stream_readdir(php_stream *dirstream, php_stream_dirent *ent)
|
2002-04-17 06:14:27 +08:00
|
|
|
{
|
2002-09-23 09:47:04 +08:00
|
|
|
|
2003-04-01 06:13:01 +08:00
|
|
|
if (sizeof(php_stream_dirent) == php_stream_read(dirstream, (char*)ent, sizeof(php_stream_dirent))) {
|
2002-04-17 06:14:27 +08:00
|
|
|
return ent;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
|
|
|
|
2002-04-17 06:14:27 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-02-17 06:19:28 +08:00
|
|
|
/* }}} */
|
2002-08-11 18:53:10 +08:00
|
|
|
|
2002-04-11 06:42:32 +08:00
|
|
|
/* {{{ php_stream_open_wrapper_ex */
|
2013-07-30 18:49:36 +08:00
|
|
|
PHPAPI php_stream *_php_stream_open_wrapper_ex(const char *path, const char *mode, int options,
|
2015-03-04 07:05:28 +08:00
|
|
|
zend_string **opened_path, php_stream_context *context STREAMS_DC)
|
2002-03-16 05:03:08 +08:00
|
|
|
{
|
2002-03-16 08:05:47 +08:00
|
|
|
php_stream *stream = NULL;
|
2002-04-17 06:14:27 +08:00
|
|
|
php_stream_wrapper *wrapper = NULL;
|
2013-07-30 18:49:36 +08:00
|
|
|
const char *path_to_open;
|
2003-08-29 01:07:40 +08:00
|
|
|
int persistent = options & STREAM_OPEN_PERSISTENT;
|
2015-03-04 07:05:28 +08:00
|
|
|
zend_string *resolved_path = NULL;
|
2002-08-21 04:47:47 +08:00
|
|
|
char *copy_of_path = NULL;
|
2003-04-01 06:13:01 +08:00
|
|
|
|
|
|
|
if (opened_path) {
|
2002-03-16 05:03:08 +08:00
|
|
|
*opened_path = NULL;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-03-17 04:05:08 +08:00
|
|
|
|
2003-04-01 06:13:01 +08:00
|
|
|
if (!path || !*path) {
|
2014-12-14 06:06:14 +08:00
|
|
|
php_error_docref(NULL, E_WARNING, "Filename cannot be empty");
|
2002-03-16 05:03:08 +08:00
|
|
|
return NULL;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2008-03-27 18:33:40 +08:00
|
|
|
if (options & USE_PATH) {
|
2014-12-14 06:06:14 +08:00
|
|
|
resolved_path = zend_resolve_path(path, (int)strlen(path));
|
2008-03-27 18:33:40 +08:00
|
|
|
if (resolved_path) {
|
2015-06-30 18:59:27 +08:00
|
|
|
path = ZSTR_VAL(resolved_path);
|
2008-03-27 18:33:40 +08:00
|
|
|
/* we've found this file, don't re-check include_path or run realpath */
|
|
|
|
options |= STREAM_ASSUME_REALPATH;
|
|
|
|
options &= ~USE_PATH;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-17 06:14:27 +08:00
|
|
|
path_to_open = path;
|
2002-03-17 04:05:08 +08:00
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
wrapper = php_stream_locate_url_wrapper(path, &path_to_open, options);
|
2003-02-08 05:33:35 +08:00
|
|
|
if (options & STREAM_USE_URL && (!wrapper || !wrapper->is_url)) {
|
2014-12-14 06:06:14 +08:00
|
|
|
php_error_docref(NULL, E_WARNING, "This function may only be used against URLs");
|
2008-03-27 18:33:40 +08:00
|
|
|
if (resolved_path) {
|
2015-03-04 07:05:28 +08:00
|
|
|
zend_string_release(resolved_path);
|
2008-03-27 18:33:40 +08:00
|
|
|
}
|
2003-02-08 05:33:35 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2003-04-01 06:13:01 +08:00
|
|
|
if (wrapper) {
|
2007-03-04 03:01:34 +08:00
|
|
|
if (!wrapper->wops->stream_opener) {
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_wrapper_log_error(wrapper, options ^ REPORT_ERRORS,
|
2007-03-04 03:01:34 +08:00
|
|
|
"wrapper does not support stream open");
|
|
|
|
} else {
|
|
|
|
stream = wrapper->wops->stream_opener(wrapper,
|
2002-08-11 18:53:10 +08:00
|
|
|
path_to_open, mode, options ^ REPORT_ERRORS,
|
2014-12-14 06:06:14 +08:00
|
|
|
opened_path, context STREAMS_REL_CC);
|
2008-07-11 18:25:15 +08:00
|
|
|
}
|
|
|
|
|
2003-03-19 08:17:15 +08:00
|
|
|
/* if the caller asked for a persistent stream but the wrapper did not
|
|
|
|
* return one, force an error here */
|
|
|
|
if (stream && (options & STREAM_OPEN_PERSISTENT) && !stream->is_persistent) {
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_wrapper_log_error(wrapper, options ^ REPORT_ERRORS,
|
2003-03-19 08:17:15 +08:00
|
|
|
"wrapper does not support persistent streams");
|
|
|
|
php_stream_close(stream);
|
|
|
|
stream = NULL;
|
|
|
|
}
|
2010-04-08 15:55:35 +08:00
|
|
|
|
2003-02-28 01:43:38 +08:00
|
|
|
if (stream) {
|
2002-04-17 06:14:27 +08:00
|
|
|
stream->wrapper = wrapper;
|
2003-02-28 01:43:38 +08:00
|
|
|
}
|
2002-04-17 06:14:27 +08:00
|
|
|
}
|
2002-03-20 07:29:37 +08:00
|
|
|
|
2002-08-21 04:47:47 +08:00
|
|
|
if (stream) {
|
2008-03-27 18:33:40 +08:00
|
|
|
if (opened_path && !*opened_path && resolved_path) {
|
|
|
|
*opened_path = resolved_path;
|
|
|
|
resolved_path = NULL;
|
|
|
|
}
|
2004-04-01 09:07:54 +08:00
|
|
|
if (stream->orig_path) {
|
|
|
|
pefree(stream->orig_path, persistent);
|
|
|
|
}
|
2003-03-20 09:23:04 +08:00
|
|
|
copy_of_path = pestrdup(path, persistent);
|
2003-11-03 22:12:46 +08:00
|
|
|
stream->orig_path = copy_of_path;
|
2007-11-06 19:02:36 +08:00
|
|
|
#if ZEND_DEBUG
|
|
|
|
stream->open_filename = __zend_orig_filename ? __zend_orig_filename : __zend_filename;
|
|
|
|
stream->open_lineno = __zend_orig_lineno ? __zend_orig_lineno : __zend_lineno;
|
|
|
|
#endif
|
2002-08-21 04:47:47 +08:00
|
|
|
}
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2002-03-17 22:21:01 +08:00
|
|
|
if (stream != NULL && (options & STREAM_MUST_SEEK)) {
|
|
|
|
php_stream *newstream;
|
|
|
|
|
2002-09-24 03:10:33 +08:00
|
|
|
switch(php_stream_make_seekable_rel(stream, &newstream,
|
|
|
|
(options & STREAM_WILL_CAST)
|
|
|
|
? PHP_STREAM_PREFER_STDIO : PHP_STREAM_NO_PREFERENCE)) {
|
2002-03-17 22:21:01 +08:00
|
|
|
case PHP_STREAM_UNCHANGED:
|
2008-03-27 18:33:40 +08:00
|
|
|
if (resolved_path) {
|
2015-03-04 07:05:28 +08:00
|
|
|
zend_string_release(resolved_path);
|
2008-03-27 18:33:40 +08:00
|
|
|
}
|
2002-03-17 22:21:01 +08:00
|
|
|
return stream;
|
|
|
|
case PHP_STREAM_RELEASED:
|
2007-05-08 20:08:17 +08:00
|
|
|
if (newstream->orig_path) {
|
|
|
|
pefree(newstream->orig_path, persistent);
|
|
|
|
}
|
2003-11-03 22:12:46 +08:00
|
|
|
newstream->orig_path = pestrdup(path, persistent);
|
2008-03-27 18:33:40 +08:00
|
|
|
if (resolved_path) {
|
2015-03-04 07:05:28 +08:00
|
|
|
zend_string_release(resolved_path);
|
2008-03-27 18:33:40 +08:00
|
|
|
}
|
2002-03-17 22:21:01 +08:00
|
|
|
return newstream;
|
|
|
|
default:
|
|
|
|
php_stream_close(stream);
|
|
|
|
stream = NULL;
|
|
|
|
if (options & REPORT_ERRORS) {
|
|
|
|
char *tmp = estrdup(path);
|
|
|
|
php_strip_url_passwd(tmp);
|
2014-12-14 06:06:14 +08:00
|
|
|
php_error_docref1(NULL, tmp, E_WARNING, "could not make seekable - %s",
|
2003-08-29 01:07:40 +08:00
|
|
|
tmp);
|
2002-03-17 22:21:01 +08:00
|
|
|
efree(tmp);
|
|
|
|
|
|
|
|
options ^= REPORT_ERRORS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-12-23 02:05:36 +08:00
|
|
|
|
2003-01-01 17:58:17 +08:00
|
|
|
if (stream && stream->ops->seek && (stream->flags & PHP_STREAM_FLAG_NO_SEEK) == 0 && strchr(mode, 'a') && stream->position == 0) {
|
2014-08-16 17:16:11 +08:00
|
|
|
zend_off_t newpos = 0;
|
2002-12-23 02:05:36 +08:00
|
|
|
|
|
|
|
/* if opened for append, we need to revise our idea of the initial file position */
|
2014-12-14 06:06:14 +08:00
|
|
|
if (0 == stream->ops->seek(stream, 0, SEEK_CUR, &newpos)) {
|
2002-12-23 02:05:36 +08:00
|
|
|
stream->position = newpos;
|
|
|
|
}
|
|
|
|
}
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2002-03-17 22:21:01 +08:00
|
|
|
if (stream == NULL && (options & REPORT_ERRORS)) {
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_display_wrapper_errors(wrapper, path, "failed to open stream");
|
2005-12-20 17:47:57 +08:00
|
|
|
if (opened_path && *opened_path) {
|
2015-03-04 07:05:28 +08:00
|
|
|
zend_string_release(*opened_path);
|
2005-12-20 17:47:57 +08:00
|
|
|
*opened_path = NULL;
|
|
|
|
}
|
2002-08-11 18:53:10 +08:00
|
|
|
}
|
2014-12-14 06:06:14 +08:00
|
|
|
php_stream_tidy_wrapper_error_log(wrapper);
|
2002-08-21 04:47:47 +08:00
|
|
|
#if ZEND_DEBUG
|
2002-12-09 18:38:35 +08:00
|
|
|
if (stream == NULL && copy_of_path != NULL) {
|
2003-03-20 09:23:04 +08:00
|
|
|
pefree(copy_of_path, persistent);
|
2002-12-09 18:38:35 +08:00
|
|
|
}
|
2002-08-21 04:47:47 +08:00
|
|
|
#endif
|
2008-03-27 18:33:40 +08:00
|
|
|
if (resolved_path) {
|
2015-03-04 07:05:28 +08:00
|
|
|
zend_string_release(resolved_path);
|
2008-03-27 18:33:40 +08:00
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
return stream;
|
|
|
|
}
|
2002-03-20 22:21:30 +08:00
|
|
|
/* }}} */
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2003-02-17 06:19:28 +08:00
|
|
|
/* {{{ context API */
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI php_stream_context *php_stream_context_set(php_stream *stream, php_stream_context *context)
|
2002-04-11 06:42:32 +08:00
|
|
|
{
|
2014-08-07 19:55:37 +08:00
|
|
|
php_stream_context *oldcontext = PHP_STREAM_CONTEXT(stream);
|
2009-11-16 04:30:57 +08:00
|
|
|
|
|
|
|
if (context) {
|
2014-08-07 19:55:37 +08:00
|
|
|
stream->ctx = context->res;
|
2014-04-02 18:34:44 +08:00
|
|
|
GC_REFCOUNT(context->res)++;
|
2014-08-07 19:55:37 +08:00
|
|
|
} else {
|
|
|
|
stream->ctx = NULL;
|
2009-11-16 04:30:57 +08:00
|
|
|
}
|
|
|
|
if (oldcontext) {
|
2014-02-10 14:04:30 +08:00
|
|
|
zend_list_delete(oldcontext->res);
|
2009-11-16 04:30:57 +08:00
|
|
|
}
|
|
|
|
|
2002-04-11 06:42:32 +08:00
|
|
|
return oldcontext;
|
|
|
|
}
|
|
|
|
|
|
|
|
PHPAPI void php_stream_notification_notify(php_stream_context *context, int notifycode, int severity,
|
2014-12-14 06:06:14 +08:00
|
|
|
char *xmsg, int xcode, size_t bytes_sofar, size_t bytes_max, void * ptr)
|
2002-04-11 06:42:32 +08:00
|
|
|
{
|
|
|
|
if (context && context->notifier)
|
2014-12-14 06:06:14 +08:00
|
|
|
context->notifier->func(context, notifycode, severity, xmsg, xcode, bytes_sofar, bytes_max, ptr);
|
2002-04-11 06:42:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PHPAPI void php_stream_context_free(php_stream_context *context)
|
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
if (Z_TYPE(context->options) != IS_UNDEF) {
|
2003-06-15 03:30:42 +08:00
|
|
|
zval_ptr_dtor(&context->options);
|
2014-02-10 14:04:30 +08:00
|
|
|
ZVAL_UNDEF(&context->options);
|
2003-06-15 03:30:42 +08:00
|
|
|
}
|
2003-06-27 12:27:18 +08:00
|
|
|
if (context->notifier) {
|
|
|
|
php_stream_notification_free(context->notifier);
|
|
|
|
context->notifier = NULL;
|
|
|
|
}
|
2002-04-11 06:42:32 +08:00
|
|
|
efree(context);
|
|
|
|
}
|
|
|
|
|
2014-12-14 06:06:14 +08:00
|
|
|
PHPAPI php_stream_context *php_stream_context_alloc(void)
|
2002-04-11 06:42:32 +08:00
|
|
|
{
|
2002-04-30 08:16:00 +08:00
|
|
|
php_stream_context *context;
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2002-04-30 08:16:00 +08:00
|
|
|
context = ecalloc(1, sizeof(php_stream_context));
|
2003-06-27 12:27:18 +08:00
|
|
|
context->notifier = NULL;
|
2014-02-10 14:04:30 +08:00
|
|
|
array_init(&context->options);
|
2002-04-30 08:16:00 +08:00
|
|
|
|
2015-02-02 13:23:16 +08:00
|
|
|
context->res = zend_register_resource(context, php_le_stream_context());
|
2002-04-30 08:16:00 +08:00
|
|
|
return context;
|
2002-04-11 06:42:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PHPAPI php_stream_notifier *php_stream_notification_alloc(void)
|
|
|
|
{
|
|
|
|
return ecalloc(1, sizeof(php_stream_notifier));
|
|
|
|
}
|
|
|
|
|
|
|
|
PHPAPI void php_stream_notification_free(php_stream_notifier *notifier)
|
|
|
|
{
|
2003-06-28 00:23:58 +08:00
|
|
|
if (notifier->dtor) {
|
|
|
|
notifier->dtor(notifier);
|
|
|
|
}
|
2002-04-11 06:42:32 +08:00
|
|
|
efree(notifier);
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
PHPAPI zval *php_stream_context_get_option(php_stream_context *context,
|
|
|
|
const char *wrappername, const char *optionname)
|
2002-04-30 08:16:00 +08:00
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
zval *wrapperhash;
|
2003-04-01 06:13:01 +08:00
|
|
|
|
2014-02-10 14:04:30 +08:00
|
|
|
if (NULL == (wrapperhash = zend_hash_str_find(Z_ARRVAL(context->options), wrappername, strlen(wrappername)))) {
|
|
|
|
return NULL;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2014-02-10 14:04:30 +08:00
|
|
|
return zend_hash_str_find(Z_ARRVAL_P(wrapperhash), optionname, strlen(optionname));
|
2002-04-30 08:16:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PHPAPI int php_stream_context_set_option(php_stream_context *context,
|
|
|
|
const char *wrappername, const char *optionname, zval *optionvalue)
|
|
|
|
{
|
2014-02-10 14:04:30 +08:00
|
|
|
zval *wrapperhash;
|
2015-10-30 01:06:55 +08:00
|
|
|
zval category;
|
2002-04-30 08:16:00 +08:00
|
|
|
|
2015-10-30 01:06:55 +08:00
|
|
|
wrapperhash = zend_hash_str_find(Z_ARRVAL(context->options), wrappername, strlen(wrappername));
|
|
|
|
if (NULL == wrapperhash) {
|
2014-02-10 14:04:30 +08:00
|
|
|
array_init(&category);
|
2015-10-30 01:06:55 +08:00
|
|
|
wrapperhash = zend_hash_str_update(Z_ARRVAL(context->options), (char*)wrappername, strlen(wrappername), &category);
|
|
|
|
if (NULL == wrapperhash) {
|
2002-04-30 08:16:00 +08:00
|
|
|
return FAILURE;
|
2003-04-01 06:13:01 +08:00
|
|
|
}
|
2002-04-30 08:16:00 +08:00
|
|
|
}
|
2015-12-30 21:10:05 +08:00
|
|
|
ZVAL_DEREF(optionvalue);
|
|
|
|
Z_TRY_ADDREF_P(optionvalue);
|
2015-10-30 01:06:55 +08:00
|
|
|
return zend_hash_str_update(Z_ARRVAL_P(wrapperhash), optionname, strlen(optionname), optionvalue) ? SUCCESS : FAILURE;
|
2002-04-30 08:16:00 +08:00
|
|
|
}
|
2003-02-17 06:19:28 +08:00
|
|
|
/* }}} */
|
2003-02-10 04:43:05 +08:00
|
|
|
|
2004-01-29 06:21:54 +08:00
|
|
|
/* {{{ php_stream_dirent_alphasort
|
|
|
|
*/
|
2014-03-02 22:47:59 +08:00
|
|
|
PHPAPI int php_stream_dirent_alphasort(const zend_string **a, const zend_string **b)
|
2004-01-29 06:21:54 +08:00
|
|
|
{
|
2015-06-30 18:59:27 +08:00
|
|
|
return strcoll(ZSTR_VAL(*a), ZSTR_VAL(*b));
|
2004-01-29 06:21:54 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ php_stream_dirent_alphasortr
|
|
|
|
*/
|
2014-03-02 22:47:59 +08:00
|
|
|
PHPAPI int php_stream_dirent_alphasortr(const zend_string **a, const zend_string **b)
|
2004-01-29 06:21:54 +08:00
|
|
|
{
|
2015-06-30 18:59:27 +08:00
|
|
|
return strcoll(ZSTR_VAL(*b), ZSTR_VAL(*a));
|
2004-01-29 06:21:54 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* {{{ php_stream_scandir
|
|
|
|
*/
|
2014-03-02 22:47:59 +08:00
|
|
|
PHPAPI int _php_stream_scandir(const char *dirname, zend_string **namelist[], int flags, php_stream_context *context,
|
2014-12-14 06:06:14 +08:00
|
|
|
int (*compare) (const zend_string **a, const zend_string **b))
|
2004-01-29 06:21:54 +08:00
|
|
|
{
|
|
|
|
php_stream *stream;
|
|
|
|
php_stream_dirent sdp;
|
2014-03-02 22:47:59 +08:00
|
|
|
zend_string **vector = NULL;
|
2012-06-08 14:05:23 +08:00
|
|
|
unsigned int vector_size = 0;
|
|
|
|
unsigned int nfiles = 0;
|
2004-01-29 06:21:54 +08:00
|
|
|
|
|
|
|
if (!namelist) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2010-04-27 07:53:30 +08:00
|
|
|
stream = php_stream_opendir(dirname, REPORT_ERRORS, context);
|
2004-01-29 06:21:54 +08:00
|
|
|
if (!stream) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (php_stream_readdir(stream, &sdp)) {
|
|
|
|
if (nfiles == vector_size) {
|
|
|
|
if (vector_size == 0) {
|
|
|
|
vector_size = 10;
|
|
|
|
} else {
|
2012-06-08 16:21:37 +08:00
|
|
|
if(vector_size*2 < vector_size) {
|
|
|
|
/* overflow */
|
2013-07-10 19:00:47 +08:00
|
|
|
php_stream_closedir(stream);
|
2012-06-08 16:21:37 +08:00
|
|
|
efree(vector);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
2004-01-29 06:21:54 +08:00
|
|
|
vector_size *= 2;
|
|
|
|
}
|
2014-03-02 22:47:59 +08:00
|
|
|
vector = (zend_string **) safe_erealloc(vector, vector_size, sizeof(char *), 0);
|
2004-01-29 06:21:54 +08:00
|
|
|
}
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
vector[nfiles] = zend_string_init(sdp.d_name, strlen(sdp.d_name), 0);
|
2004-03-08 05:36:55 +08:00
|
|
|
|
|
|
|
nfiles++;
|
2012-06-08 14:05:23 +08:00
|
|
|
if(vector_size < 10 || nfiles == 0) {
|
|
|
|
/* overflow */
|
2013-07-10 19:00:47 +08:00
|
|
|
php_stream_closedir(stream);
|
2012-06-08 14:05:23 +08:00
|
|
|
efree(vector);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
2004-01-29 06:21:54 +08:00
|
|
|
}
|
|
|
|
php_stream_closedir(stream);
|
|
|
|
|
|
|
|
*namelist = vector;
|
|
|
|
|
2013-07-10 19:00:47 +08:00
|
|
|
if (nfiles > 0 && compare) {
|
2014-03-02 22:47:59 +08:00
|
|
|
qsort(*namelist, nfiles, sizeof(zend_string *), (int(*)(const void *, const void *))compare);
|
2004-01-29 06:21:54 +08:00
|
|
|
}
|
|
|
|
return nfiles;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2001-05-06 02:36:22 +08:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
2001-09-09 21:29:31 +08:00
|
|
|
* vim600: noet sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: noet sw=4 ts=4
|
2001-05-06 02:36:22 +08:00
|
|
|
*/
|