helly Sat May 13 17:58:58 2006 UTC
Added files: (Branch: PHP_5_2)
/php-src/ext/standard/tests/file stream_rfc2397_001.phpt
Modified files:
/php-src/ext/standard basic_functions.c
/php-src/main php_memory_streams.h
/php-src/main/streams memory.c
Log:
- MFH Initial support for RFC2397
http://cvs.php.net/viewcvs.cgi/php-src/ext/standard/basic_functions.c?r1=1.725.2.31&r2=1.725.2.31.2.1&diff_format=u
Index: php-src/ext/standard/basic_functions.c
diff -u php-src/ext/standard/basic_functions.c:1.725.2.31
php-src/ext/standard/basic_functions.c:1.725.2.31.2.1
--- php-src/ext/standard/basic_functions.c:1.725.2.31 Mon Apr 3 13:46:11 2006
+++ php-src/ext/standard/basic_functions.c Sat May 13 17:58:58 2006
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: basic_functions.c,v 1.725.2.31 2006/04/03 13:46:11 iliaa Exp $ */
+/* $Id: basic_functions.c,v 1.725.2.31.2.1 2006/05/13 17:58:58 helly Exp $ */
#include "php.h"
#include "php_streams.h"
@@ -1109,6 +1109,7 @@
php_register_url_stream_wrapper("php", &php_stream_php_wrapper
TSRMLS_CC);
php_register_url_stream_wrapper("file", &php_plain_files_wrapper
TSRMLS_CC);
+ php_register_url_stream_wrapper("data", &php_stream_rfc2397_wrapper
TSRMLS_CC);
#ifndef PHP_CURL_URL_WRAPPERS
php_register_url_stream_wrapper("http", &php_stream_http_wrapper
TSRMLS_CC);
php_register_url_stream_wrapper("ftp", &php_stream_ftp_wrapper
TSRMLS_CC);
http://cvs.php.net/viewcvs.cgi/php-src/main/php_memory_streams.h?r1=1.13.2.1&r2=1.13.2.1.2.1&diff_format=u
Index: php-src/main/php_memory_streams.h
diff -u php-src/main/php_memory_streams.h:1.13.2.1
php-src/main/php_memory_streams.h:1.13.2.1.2.1
--- php-src/main/php_memory_streams.h:1.13.2.1 Sun Jan 1 12:50:17 2006
+++ php-src/main/php_memory_streams.h Sat May 13 17:58:58 2006
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_memory_streams.h,v 1.13.2.1 2006/01/01 12:50:17 sniper Exp $ */
+/* $Id: php_memory_streams.h,v 1.13.2.1.2.1 2006/05/13 17:58:58 helly Exp $ */
#ifndef PHP_MEMORY_STREAM_H
#define PHP_MEMORY_STREAM_H
@@ -50,6 +50,8 @@
extern php_stream_ops php_stream_memory_ops;
extern php_stream_ops php_stream_temp_ops;
+extern php_stream_ops php_stream_rfc2397_ops;
+extern php_stream_wrapper php_stream_rfc2397_wrapper;
#define PHP_STREAM_IS_MEMORY &php_stream_memory_ops
#define PHP_STREAM_IS_TEMP &php_stream_temp_ops
http://cvs.php.net/viewcvs.cgi/php-src/main/streams/memory.c?r1=1.8.2.6&r2=1.8.2.6.2.1&diff_format=u
Index: php-src/main/streams/memory.c
diff -u php-src/main/streams/memory.c:1.8.2.6
php-src/main/streams/memory.c:1.8.2.6.2.1
--- php-src/main/streams/memory.c:1.8.2.6 Sat Mar 18 19:57:00 2006
+++ php-src/main/streams/memory.c Sat May 13 17:58:58 2006
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: memory.c,v 1.8.2.6 2006/03/18 19:57:00 helly Exp $ */
+/* $Id: memory.c,v 1.8.2.6.2.1 2006/05/13 17:58:58 helly Exp $ */
#define _GNU_SOURCE
#include "php.h"
@@ -46,10 +46,7 @@
/* {{{ */
static size_t php_stream_memory_write(php_stream *stream, const char *buf,
size_t count TSRMLS_DC)
{
- php_stream_memory_data *ms;
-
- assert(stream != NULL);
- ms = stream->abstract;
+ php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
assert(ms != NULL);
if (ms->mode & TEMP_STREAM_READONLY) {
@@ -85,10 +82,7 @@
/* {{{ */
static size_t php_stream_memory_read(php_stream *stream, char *buf, size_t
count TSRMLS_DC)
{
- php_stream_memory_data *ms;
-
- assert(stream != NULL);
- ms = stream->abstract;
+ php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
assert(ms != NULL);
if (ms->fpos + count > ms->fsize) {
@@ -110,10 +104,7 @@
/* {{{ */
static int php_stream_memory_close(php_stream *stream, int close_handle
TSRMLS_DC)
{
- php_stream_memory_data *ms;
-
- assert(stream != NULL);
- ms = stream->abstract;
+ php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
assert(ms != NULL);
if (ms->data && close_handle && ms->mode != TEMP_STREAM_READONLY) {
@@ -140,10 +131,7 @@
/* {{{ */
static int php_stream_memory_seek(php_stream *stream, off_t offset, int
whence, off_t *newoffs TSRMLS_DC)
{
- php_stream_memory_data *ms;
-
- assert(stream != NULL);
- ms = stream->abstract;
+ php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
assert(ms != NULL);
switch(whence) {
@@ -207,6 +195,45 @@
}
/* }}} */
+static int php_stream_memory_stat(php_stream *stream, php_stream_statbuf *ssb
TSRMLS_DC) /* {{{ */
+{
+ time_t timestamp = 0;
+ php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
+ assert(ms != NULL);
+
+ memset(ssb, 0, sizeof(php_stream_statbuf));
+ /* read-only across the board */
+
+ ssb->sb.st_mode = ms->mode & TEMP_STREAM_READONLY ? 0444 : 0666;
+
+ ssb->sb.st_size = ms->fsize;
+ ssb->sb.st_mode |= S_IFREG; /* regular file */
+
+#ifdef NETWARE
+ ssb->sb.st_mtime.tv_sec = timestamp;
+ ssb->sb.st_atime.tv_sec = timestamp;
+ ssb->sb.st_ctime.tv_sec = timestamp;
+#else
+ ssb->sb.st_mtime = timestamp;
+ ssb->sb.st_atime = timestamp;
+ ssb->sb.st_ctime = timestamp;
+#endif
+
+ ssb->sb.st_nlink = 1;
+ ssb->sb.st_rdev = -1;
+ /* this is only for APC, so use /dev/null device - no chance of
conflict there! */
+ ssb->sb.st_dev = 0xC;
+ /* generate unique inode number for alias/filename, so no phars will
conflict */
+ ssb->sb.st_ino = 0;
+
+#ifndef PHP_WIN32
+ ssb->sb.st_blksize = -1;
+ ssb->sb.st_blocks = -1;
+#endif
+
+ return 0;
+}
+/* }}} */
php_stream_ops php_stream_memory_ops = {
php_stream_memory_write, php_stream_memory_read,
@@ -214,7 +241,7 @@
"MEMORY",
php_stream_memory_seek,
php_stream_memory_cast,
- NULL, /* stat */
+ php_stream_memory_stat,
NULL /* set_option */
};
@@ -229,7 +256,7 @@
self->data = NULL;
self->fpos = 0;
self->fsize = 0;
- self->smax = -1;
+ self->smax = ~0u;
self->mode = mode;
self->owner_ptr = NULL;
@@ -247,7 +274,7 @@
php_stream_memory_data *ms;
if ((stream = php_stream_memory_create_rel(mode)) != NULL) {
- ms = stream->abstract;
+ ms = (php_stream_memory_data*)stream->abstract;
if (mode == TEMP_STREAM_READONLY || mode ==
TEMP_STREAM_TAKE_BUFFER) {
/* use the buffer directly */
@@ -268,10 +295,8 @@
/* {{{ */
PHPAPI char *_php_stream_memory_get_buffer(php_stream *stream, size_t *length
STREAMS_DC TSRMLS_DC)
{
- php_stream_memory_data *ms;
+ php_stream_memory_data *ms = (php_stream_memory_data*)stream->abstract;
- assert(stream != NULL);
- ms = stream->abstract;
assert(ms != NULL);
assert(length != 0);
@@ -288,16 +313,14 @@
php_stream *innerstream;
size_t smax;
int mode;
+ zval* meta;
} php_stream_temp_data;
/* {{{ */
static size_t php_stream_temp_write(php_stream *stream, const char *buf,
size_t count TSRMLS_DC)
{
- php_stream_temp_data *ts;
-
- assert(stream != NULL);
- ts = stream->abstract;
+ php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
assert(ts != NULL);
if (!ts->innerstream) {
@@ -322,11 +345,9 @@
/* {{{ */
static size_t php_stream_temp_read(php_stream *stream, char *buf, size_t count
TSRMLS_DC)
{
- php_stream_temp_data *ts;
+ php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
size_t got;
- assert(stream != NULL);
- ts = stream->abstract;
assert(ts != NULL);
if (!ts->innerstream) {
@@ -347,11 +368,9 @@
/* {{{ */
static int php_stream_temp_close(php_stream *stream, int close_handle
TSRMLS_DC)
{
- php_stream_temp_data *ts;
+ php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
int ret;
- assert(stream != NULL);
- ts = stream->abstract;
assert(ts != NULL);
if (ts->innerstream) {
@@ -359,6 +378,10 @@
} else {
ret = 0;
}
+
+ if (ts->meta) {
+ zval_ptr_dtor(&ts->meta);
+ }
efree(ts);
@@ -370,10 +393,7 @@
/* {{{ */
static int php_stream_temp_flush(php_stream *stream TSRMLS_DC)
{
- php_stream_temp_data *ts;
-
- assert(stream != NULL);
- ts = stream->abstract;
+ php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
assert(ts != NULL);
return ts->innerstream ? php_stream_flush(ts->innerstream) : -1;
@@ -384,11 +404,9 @@
/* {{{ */
static int php_stream_temp_seek(php_stream *stream, off_t offset, int whence,
off_t *newoffs TSRMLS_DC)
{
- php_stream_temp_data *ts;
+ php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
int ret;
- assert(stream != NULL);
- ts = stream->abstract;
assert(ts != NULL);
if (!ts->innerstream) {
@@ -405,14 +423,12 @@
/* {{{ */
static int php_stream_temp_cast(php_stream *stream, int castas, void **ret
TSRMLS_DC)
{
- php_stream_temp_data *ts;
+ php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
php_stream *file;
size_t memsize;
char *membuf;
off_t pos;
- assert(stream != NULL);
- ts = stream->abstract;
assert(ts != NULL);
if (!ts->innerstream) {
@@ -450,14 +466,41 @@
}
/* }}} */
+static int php_stream_temp_stat(php_stream *stream, php_stream_statbuf *ssb
TSRMLS_DC) /* {{{ */
+{
+ php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
+
+ if (!ts || !ts->innerstream) {
+ return -1;
+ }
+ return php_stream_stat(ts->innerstream, ssb);
+}
+/* }}} */
+
+static int php_stream_temp_set_option(php_stream *stream, int option, int
value, void *ptrparam TSRMLS_DC) /* {{{ */
+{
+ php_stream_temp_data *ts = (php_stream_temp_data*)stream->abstract;
+
+ switch(option) {
+ case PHP_STREAM_OPTION_META_DATA_API:
+ if (ts->meta) {
+ zend_hash_copy(Z_ARRVAL_P((zval*)ptrparam),
Z_ARRVAL_P(ts->meta), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval*));
+ }
+ return PHP_STREAM_OPTION_RETURN_OK;
+ default:
+ return PHP_STREAM_OPTION_RETURN_NOTIMPL;
+ }
+}
+/* }}} */
+
php_stream_ops php_stream_temp_ops = {
php_stream_temp_write, php_stream_temp_read,
php_stream_temp_close, php_stream_temp_flush,
"TEMP",
php_stream_temp_seek,
php_stream_temp_cast,
- NULL, /* stat */
- NULL /* set_option */
+ php_stream_temp_stat,
+ php_stream_temp_set_option
};
/* }}} */
@@ -471,6 +514,7 @@
self = ecalloc(1, sizeof(*self));
self->smax = max_memory_usage;
self->mode = mode;
+ self->meta = NULL;
stream = php_stream_alloc(&php_stream_temp_ops, self, 0, mode &
TEMP_STREAM_READONLY ? "r+b" : "w+b");
stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
self->innerstream = php_stream_memory_create(mode);
@@ -485,21 +529,97 @@
PHPAPI php_stream *_php_stream_temp_open(int mode, size_t max_memory_usage,
char *buf, size_t length STREAMS_DC TSRMLS_DC)
{
php_stream *stream;
- php_stream_temp_data *ms;
+ php_stream_temp_data *ts;
+ off_t newoffs;
if ((stream = php_stream_temp_create_rel(mode, max_memory_usage)) !=
NULL) {
if (length) {
assert(buf != NULL);
php_stream_temp_write(stream, buf, length TSRMLS_CC);
+ php_stream_temp_seek(stream, 0, SEEK_SET, &newoffs
TSRMLS_CC);
}
- ms = stream->abstract;
- assert(ms != NULL);
- ms->mode = mode;
+ ts = (php_stream_temp_data*)stream->abstract;
+ assert(ts != NULL);
+ ts->mode = mode;
}
return stream;
}
/* }}} */
+php_stream_ops php_stream_rfc2397_ops = {
+ php_stream_temp_write, php_stream_temp_read,
+ php_stream_temp_close, php_stream_temp_flush,
+ "RFC2397",
+ php_stream_temp_seek,
+ php_stream_temp_cast,
+ php_stream_temp_stat,
+ php_stream_temp_set_option
+};
+
+static php_stream * php_stream_url_wrap_rfc2397(php_stream_wrapper *wrapper,
char *path, char *mode, int options, char **opened_path, php_stream_context
*context STREAMS_DC TSRMLS_DC) /* {{{ */
+{
+ php_stream *stream;
+ php_stream_temp_data *ts;
+ char *comma, *semi;
+ size_t mlen, dlen;
+ off_t newoffs;
+
+ if (memcmp(path, "data:", 5)) {
+ return NULL;
+ }
+
+ path += 5;
+ dlen = strlen(path);
+
+ if (dlen >= 2 && path[0] == '/' && path[1] == '/') {
+ dlen -= 2;
+ path += 2;
+ }
+
+ if ((comma = memchr(path, ',', dlen)) == NULL) {
+ php_stream_wrapper_log_error(wrapper, options TSRMLS_CC,
"rfc2397: no comma in url");
+ return NULL;
+ }
+
+ if ((stream = php_stream_temp_create_rel(0, ~0u)) != NULL) {
+ if (comma != path) {
+ /* meta info */
+ mlen = comma - path;
+ dlen -= mlen;
+ semi = memchr(path, ';', mlen);
+ }
+ /* skip ',' */
+ comma++;
+ dlen--;
+ /* store data */
+ php_stream_temp_write(stream, comma, dlen TSRMLS_CC);
+ php_stream_temp_seek(stream, 0, SEEK_SET, &newoffs TSRMLS_CC);
+ ts = (php_stream_temp_data*)stream->abstract;
+ assert(ts != NULL);
+ ts->mode = mode && mode[0] == 'r' ? TEMP_STREAM_READONLY : 0;
+ }
+
+ return stream;
+}
+
+static php_stream_wrapper_ops php_stream_rfc2397_wops = {
+ php_stream_url_wrap_rfc2397,
+ NULL, /* close */
+ NULL, /* fstat */
+ NULL, /* stat */
+ NULL, /* opendir */
+ "RFC2397",
+ NULL, /* unlink */
+ NULL, /* rename */
+ NULL, /* mkdir */
+ NULL /* rmdir */
+};
+
+php_stream_wrapper php_stream_rfc2397_wrapper = {
+ &php_stream_rfc2397_wops,
+ NULL,
+ 1, /* is_url */
+};
/*
* Local variables:
http://cvs.php.net/viewcvs.cgi/php-src/ext/standard/tests/file/stream_rfc2397_001.phpt?view=markup&rev=1.1
Index: php-src/ext/standard/tests/file/stream_rfc2397_001.phpt
+++ php-src/ext/standard/tests/file/stream_rfc2397_001.phpt
--TEST--
Stream: RFC2397
--FILE--
<?php
$data = 'data://,hello world';
var_dump(file_get_contents($data));
$file = fopen($data, 'r');
unset($data);
var_dump(stream_get_contents($file));
?>
===DONE===
--EXPECT--
string(11) "hello world"
string(11) "hello world"
===DONE===
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php