2002-03-17 04:11:06 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| PHP Version 4 |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Copyright (c) 1997-2002 The PHP Group |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 2.02 of the PHP license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available at through the world-wide-web at |
|
|
|
|
| http://www.php.net/license/2_02.txt. |
|
|
|
|
| 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. |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| Author: |
|
|
|
|
| Marcus Boerger <helly@php.net> |
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include "php.h"
|
2002-03-18 12:57:06 +08:00
|
|
|
|
|
|
|
/* Memory streams use a dynamic memory buffer to emulate a stream.
|
|
|
|
* You can use php_stream_memory_open to create a readonly stream
|
|
|
|
* from an existing memory buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Temp streams are streams that uses memory streams as long their
|
|
|
|
* size is less than a given memory amount. When a write operation
|
|
|
|
* exceeds that limit the content is written to a temporary file.
|
|
|
|
*/
|
2002-03-17 04:11:06 +08:00
|
|
|
|
|
|
|
/* {{{ ------- MEMORY stream implementation -------*/
|
|
|
|
|
|
|
|
typedef struct {
|
2002-03-18 07:50:31 +08:00
|
|
|
char *data;
|
|
|
|
size_t fpos;
|
|
|
|
size_t fsize;
|
|
|
|
size_t smax;
|
|
|
|
int mode;
|
2002-03-18 12:57:06 +08:00
|
|
|
} php_stream_memory_data;
|
2002-03-17 04:11:06 +08:00
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
|
|
|
|
/* {{{ */
|
|
|
|
static size_t php_stream_memory_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
|
|
|
|
{
|
2002-03-18 12:57:06 +08:00
|
|
|
php_stream_memory_data *ms;
|
2002-03-17 04:11:06 +08:00
|
|
|
|
|
|
|
assert(stream != NULL);
|
|
|
|
ms = stream->abstract;
|
|
|
|
assert(ms != NULL);
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-18 12:57:06 +08:00
|
|
|
if (ms->mode & TEMP_STREAM_READONLY) {
|
2002-03-18 07:50:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2002-03-18 12:57:06 +08:00
|
|
|
if (ms->fpos + count > ms->fsize) {
|
2002-03-17 04:11:06 +08:00
|
|
|
char *tmp;
|
|
|
|
|
2002-03-18 12:57:06 +08:00
|
|
|
if (!ms->data) {
|
|
|
|
tmp = emalloc(ms->fpos + count);
|
2002-03-17 04:11:06 +08:00
|
|
|
} else {
|
2002-03-18 12:57:06 +08:00
|
|
|
tmp = erealloc(ms->data, ms->fpos + count);
|
2002-03-17 04:11:06 +08:00
|
|
|
}
|
2002-03-18 12:57:06 +08:00
|
|
|
if (!tmp) {
|
2002-03-17 04:11:06 +08:00
|
|
|
count = ms->fsize - ms->fpos + 1;
|
|
|
|
} else {
|
|
|
|
ms->data = tmp;
|
|
|
|
ms->fsize = ms->fpos + count;
|
|
|
|
}
|
|
|
|
}
|
2002-03-18 12:57:06 +08:00
|
|
|
if (!ms->data)
|
|
|
|
count = 0;
|
|
|
|
if (count) {
|
2002-03-17 04:11:06 +08:00
|
|
|
assert(buf!= NULL);
|
2002-03-18 12:57:06 +08:00
|
|
|
memcpy(ms->data+ms->fpos, (char*)buf, count);
|
2002-03-17 04:11:06 +08:00
|
|
|
ms->fpos += count;
|
|
|
|
}
|
|
|
|
return count;
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-03-17 04:11:06 +08:00
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
/* {{{ */
|
|
|
|
static size_t php_stream_memory_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
|
2002-03-22 04:37:04 +08:00
|
|
|
{
|
2002-03-18 12:57:06 +08:00
|
|
|
php_stream_memory_data *ms;
|
2002-03-17 04:11:06 +08:00
|
|
|
|
|
|
|
assert(stream != NULL);
|
|
|
|
ms = stream->abstract;
|
|
|
|
assert(ms != NULL);
|
|
|
|
|
|
|
|
if (buf == NULL && count == 0) {
|
|
|
|
/* check for EOF condition */
|
|
|
|
if (ms->fpos >= ms->fsize) {
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-03-18 12:57:06 +08:00
|
|
|
if (ms->fpos + count > ms->fsize) {
|
2002-03-17 04:11:06 +08:00
|
|
|
count = ms->fsize - ms->fpos;
|
|
|
|
}
|
2002-03-18 12:57:06 +08:00
|
|
|
if (count) {
|
|
|
|
assert(ms->data!= NULL);
|
2002-03-17 04:11:06 +08:00
|
|
|
assert(buf!= NULL);
|
2002-03-18 12:57:06 +08:00
|
|
|
memcpy(buf, ms->data+ms->fpos, count);
|
2002-03-17 04:11:06 +08:00
|
|
|
ms->fpos += count;
|
|
|
|
}
|
|
|
|
return count;
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2002-03-17 04:11:06 +08:00
|
|
|
|
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
/* {{{ */
|
|
|
|
static int php_stream_memory_close(php_stream *stream, int close_handle TSRMLS_DC)
|
2002-03-22 04:37:04 +08:00
|
|
|
{
|
2002-03-18 12:57:06 +08:00
|
|
|
php_stream_memory_data *ms;
|
2002-03-17 04:11:06 +08:00
|
|
|
|
|
|
|
assert(stream != NULL);
|
|
|
|
ms = stream->abstract;
|
|
|
|
assert(ms != NULL);
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-04-03 00:46:33 +08:00
|
|
|
if (ms->data && close_handle && ms->mode != TEMP_STREAM_READONLY) {
|
2002-03-17 04:11:06 +08:00
|
|
|
efree(ms->data);
|
|
|
|
}
|
2002-03-22 04:37:04 +08:00
|
|
|
efree(ms);
|
2002-03-18 07:50:31 +08:00
|
|
|
return 0;
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2002-03-17 04:11:06 +08:00
|
|
|
|
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
/* {{{ */
|
2002-03-22 04:37:04 +08:00
|
|
|
static int php_stream_memory_flush(php_stream *stream TSRMLS_DC) {
|
2002-03-18 12:57:06 +08:00
|
|
|
/* nothing to do here */
|
2002-03-17 04:11:06 +08:00
|
|
|
return 0;
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2002-03-17 04:11:06 +08:00
|
|
|
|
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
/* {{{ */
|
|
|
|
static int php_stream_memory_seek(php_stream *stream, off_t offset, int whence TSRMLS_DC)
|
|
|
|
{
|
2002-03-18 12:57:06 +08:00
|
|
|
php_stream_memory_data *ms;
|
2002-03-17 04:11:06 +08:00
|
|
|
|
|
|
|
assert(stream != NULL);
|
|
|
|
ms = stream->abstract;
|
|
|
|
assert(ms != NULL);
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-17 04:11:06 +08:00
|
|
|
if (offset == 0 && whence == SEEK_CUR)
|
|
|
|
return ms->fpos;
|
2002-03-18 12:57:06 +08:00
|
|
|
switch(whence) {
|
2002-03-17 04:11:06 +08:00
|
|
|
case SEEK_CUR:
|
2002-03-18 12:57:06 +08:00
|
|
|
if (offset < 0) {
|
2002-03-21 03:13:03 +08:00
|
|
|
if (ms->fpos < (size_t)(-offset)) {
|
2002-03-17 04:11:06 +08:00
|
|
|
ms->fpos = 0;
|
|
|
|
/*return EINVAL;*/
|
|
|
|
} else {
|
|
|
|
ms->fpos = ms->fpos + offset;
|
|
|
|
}
|
|
|
|
} else {
|
2002-03-21 03:13:03 +08:00
|
|
|
if (ms->fpos < (size_t)(offset)) {
|
2002-03-17 04:11:06 +08:00
|
|
|
ms->fpos = ms->fsize;
|
|
|
|
/*return EINVAL;*/
|
|
|
|
} else {
|
|
|
|
ms->fpos = ms->fpos + offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
case SEEK_SET:
|
2002-03-21 03:13:03 +08:00
|
|
|
if (ms->fsize < (size_t)(offset)) {
|
2002-03-17 04:11:06 +08:00
|
|
|
ms->fpos = ms->fsize;
|
|
|
|
/*return EINVAL;*/
|
|
|
|
} else {
|
|
|
|
ms->fpos = offset;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
case SEEK_END:
|
2002-03-18 12:57:06 +08:00
|
|
|
if (offset > 0) {
|
2002-03-17 04:11:06 +08:00
|
|
|
ms->fpos = ms->fsize;
|
|
|
|
/*return EINVAL;*/
|
2002-03-21 03:13:03 +08:00
|
|
|
} else if (ms->fpos < (size_t)(-offset)) {
|
2002-03-17 04:11:06 +08:00
|
|
|
ms->fpos = 0;
|
|
|
|
/*return EINVAL;*/
|
|
|
|
} else {
|
|
|
|
ms->fpos = ms->fsize + offset;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return 0;
|
2002-03-18 07:50:31 +08:00
|
|
|
/*return EINVAL;*/
|
|
|
|
}
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
|
|
|
|
/* {{{ */
|
|
|
|
static char *php_stream_memory_gets(php_stream *stream, char *buf, size_t maxlen TSRMLS_DC)
|
|
|
|
{
|
2002-03-18 07:50:31 +08:00
|
|
|
size_t n = 1;
|
|
|
|
char *c = buf;
|
|
|
|
|
2002-03-18 12:57:06 +08:00
|
|
|
php_stream_memory_data *ms;
|
2002-03-18 07:50:31 +08:00
|
|
|
|
|
|
|
assert(stream != NULL);
|
|
|
|
ms = stream->abstract;
|
|
|
|
assert(ms != NULL);
|
|
|
|
assert(buf!= NULL);
|
|
|
|
assert(ms->data!= NULL);
|
|
|
|
|
|
|
|
while(n < maxlen && ms->fpos<ms->fsize) {
|
|
|
|
n++;
|
2002-03-18 12:57:06 +08:00
|
|
|
if ((*c = ms->data[ms->fpos++]) == '\n') {
|
2002-03-18 07:50:31 +08:00
|
|
|
c++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c++;
|
2002-03-17 04:11:06 +08:00
|
|
|
}
|
2002-03-18 07:50:31 +08:00
|
|
|
*c = 0;
|
|
|
|
return buf;
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
|
|
|
|
/* {{{ */
|
|
|
|
static int php_stream_memory_cast(php_stream *stream, int castas, void **ret TSRMLS_DC)
|
2002-03-22 04:37:04 +08:00
|
|
|
{
|
2002-03-18 07:50:31 +08:00
|
|
|
return FAILURE;
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-03-17 04:11:06 +08:00
|
|
|
|
|
|
|
php_stream_ops php_stream_memory_ops = {
|
2002-03-18 07:50:31 +08:00
|
|
|
php_stream_memory_write, php_stream_memory_read,
|
|
|
|
php_stream_memory_close, php_stream_memory_flush,
|
2002-03-28 08:49:00 +08:00
|
|
|
"MEMORY",
|
2002-03-18 07:50:31 +08:00
|
|
|
php_stream_memory_seek,
|
|
|
|
php_stream_memory_gets,
|
|
|
|
php_stream_memory_cast,
|
2002-03-28 08:49:00 +08:00
|
|
|
NULL
|
2002-03-17 04:11:06 +08:00
|
|
|
};
|
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
|
|
|
|
/* {{{ */
|
|
|
|
PHPAPI php_stream *_php_stream_memory_create(int mode STREAMS_DC TSRMLS_DC)
|
|
|
|
{
|
2002-03-18 12:57:06 +08:00
|
|
|
php_stream_memory_data *self;
|
2002-03-17 04:11:06 +08:00
|
|
|
|
|
|
|
self = emalloc(sizeof(*self));
|
2002-03-18 12:57:06 +08:00
|
|
|
assert(self != NULL);
|
2002-03-17 04:11:06 +08:00
|
|
|
self->data = NULL;
|
|
|
|
self->fpos = 0;
|
|
|
|
self->fsize = 0;
|
2002-03-18 07:50:31 +08:00
|
|
|
self->smax = -1;
|
|
|
|
self->mode = mode;
|
2002-03-18 12:57:06 +08:00
|
|
|
return php_stream_alloc(&php_stream_memory_ops, self, 0, "rwb");
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
|
|
|
|
/* {{{ */
|
|
|
|
PHPAPI php_stream *_php_stream_memory_open(int mode, char *buf, size_t length STREAMS_DC TSRMLS_DC)
|
|
|
|
{ php_stream *stream;
|
2002-03-18 12:57:06 +08:00
|
|
|
php_stream_memory_data *ms;
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-04-03 00:46:33 +08:00
|
|
|
if ((stream = php_stream_memory_create_rel(mode)) != NULL) {
|
2002-03-20 03:16:20 +08:00
|
|
|
ms = stream->abstract;
|
2002-04-03 00:46:33 +08:00
|
|
|
|
|
|
|
if (mode == TEMP_STREAM_READONLY) {
|
|
|
|
/* use the buffer directly */
|
|
|
|
ms->data = buf;
|
|
|
|
ms->fsize = length;
|
|
|
|
} else {
|
|
|
|
if (length) {
|
|
|
|
assert(buf != NULL);
|
|
|
|
php_stream_write(stream, buf, length);
|
|
|
|
}
|
|
|
|
}
|
2002-03-18 07:50:31 +08:00
|
|
|
}
|
|
|
|
return stream;
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
/* {{{ */
|
|
|
|
PHPAPI char *_php_stream_memory_get_buffer(php_stream *stream, size_t *length STREAMS_DC TSRMLS_DC)
|
2002-03-22 04:37:04 +08:00
|
|
|
{
|
2002-03-18 12:57:06 +08:00
|
|
|
php_stream_memory_data *ms;
|
2002-03-18 07:50:31 +08:00
|
|
|
|
|
|
|
assert(stream != NULL);
|
|
|
|
ms = stream->abstract;
|
|
|
|
assert(ms != NULL);
|
|
|
|
assert(length != 0);
|
|
|
|
|
|
|
|
*length = ms->fsize;
|
|
|
|
return ms->data;
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
2002-03-18 12:57:06 +08:00
|
|
|
/* }}} */
|
|
|
|
|
2002-03-20 07:29:37 +08:00
|
|
|
/* }}} */
|
2002-03-20 03:15:40 +08:00
|
|
|
|
2002-03-18 07:50:31 +08:00
|
|
|
/* {{{ ------- TEMP stream implementation -------*/
|
|
|
|
|
2002-03-18 12:57:06 +08:00
|
|
|
typedef struct {
|
|
|
|
php_stream *innerstream;
|
|
|
|
size_t smax;
|
|
|
|
int mode;
|
|
|
|
} php_stream_temp_data;
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
|
|
|
|
/* {{{ */
|
|
|
|
static size_t php_stream_temp_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
|
2002-03-22 04:37:04 +08:00
|
|
|
{
|
2002-03-18 12:57:06 +08:00
|
|
|
php_stream_temp_data *ts;
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-18 12:57:06 +08:00
|
|
|
assert(stream != NULL);
|
|
|
|
ts = stream->abstract;
|
|
|
|
assert(ts != NULL);
|
|
|
|
|
|
|
|
if (php_stream_is(ts->innerstream, PHP_STREAM_IS_MEMORY)) {
|
|
|
|
size_t memsize;
|
2002-03-18 19:18:07 +08:00
|
|
|
char *membuf = php_stream_memory_get_buffer(ts->innerstream, &memsize);
|
2002-03-18 12:57:06 +08:00
|
|
|
|
|
|
|
if (memsize + count >= ts->smax) {
|
|
|
|
php_stream *file = php_stream_fopen_tmpfile();
|
|
|
|
php_stream_write(file, membuf, memsize);
|
2002-03-21 20:55:37 +08:00
|
|
|
php_stream_close(ts->innerstream);
|
2002-03-18 12:57:06 +08:00
|
|
|
ts->innerstream = file;
|
2002-03-18 07:50:31 +08:00
|
|
|
}
|
|
|
|
}
|
2002-03-18 12:57:06 +08:00
|
|
|
return php_stream_write(ts->innerstream, buf, count);
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
/* {{{ */
|
|
|
|
static size_t php_stream_temp_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
|
2002-03-22 04:37:04 +08:00
|
|
|
{
|
2002-03-18 12:57:06 +08:00
|
|
|
php_stream_temp_data *ts;
|
2002-03-18 07:50:31 +08:00
|
|
|
|
|
|
|
assert(stream != NULL);
|
2002-03-18 12:57:06 +08:00
|
|
|
ts = stream->abstract;
|
|
|
|
assert(ts != NULL);
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-18 12:57:06 +08:00
|
|
|
return php_stream_read(ts->innerstream, buf, count);
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2002-03-18 07:50:31 +08:00
|
|
|
|
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
/* {{{ */
|
|
|
|
static int php_stream_temp_close(php_stream *stream, int close_handle TSRMLS_DC)
|
|
|
|
{
|
2002-03-18 12:57:06 +08:00
|
|
|
php_stream_temp_data *ts;
|
2002-03-20 07:29:37 +08:00
|
|
|
int ret;
|
2002-03-18 07:50:31 +08:00
|
|
|
|
|
|
|
assert(stream != NULL);
|
2002-03-18 12:57:06 +08:00
|
|
|
ts = stream->abstract;
|
|
|
|
assert(ts != NULL);
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-20 07:29:37 +08:00
|
|
|
ret = php_stream_free(ts->innerstream, PHP_STREAM_FREE_CLOSE | (close_handle ? 0 : PHP_STREAM_FREE_PRESERVE_HANDLE));
|
|
|
|
|
|
|
|
efree(ts);
|
2002-03-22 04:37:04 +08:00
|
|
|
|
2002-03-20 07:29:37 +08:00
|
|
|
return ret;
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2002-03-18 07:50:31 +08:00
|
|
|
|
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
/* {{{ */
|
|
|
|
static int php_stream_temp_flush(php_stream *stream TSRMLS_DC)
|
2002-03-22 04:37:04 +08:00
|
|
|
{
|
2002-03-18 12:57:06 +08:00
|
|
|
php_stream_temp_data *ts;
|
2002-03-18 07:50:31 +08:00
|
|
|
|
|
|
|
assert(stream != NULL);
|
2002-03-18 12:57:06 +08:00
|
|
|
ts = stream->abstract;
|
|
|
|
assert(ts != NULL);
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-18 12:57:06 +08:00
|
|
|
return php_stream_flush(ts->innerstream);
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2002-03-18 07:50:31 +08:00
|
|
|
|
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
/* {{{ */
|
|
|
|
static int php_stream_temp_seek(php_stream *stream, off_t offset, int whence TSRMLS_DC)
|
2002-03-22 04:37:04 +08:00
|
|
|
{
|
2002-03-18 12:57:06 +08:00
|
|
|
php_stream_temp_data *ts;
|
2002-03-18 07:50:31 +08:00
|
|
|
|
|
|
|
assert(stream != NULL);
|
2002-03-18 12:57:06 +08:00
|
|
|
ts = stream->abstract;
|
|
|
|
assert(ts != NULL);
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-18 12:57:06 +08:00
|
|
|
return php_stream_seek(ts->innerstream, offset, whence);
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
|
|
|
|
/* {{{ */
|
|
|
|
char *php_stream_temp_gets(php_stream *stream, char *buf, size_t maxlen TSRMLS_DC)
|
2002-03-22 04:37:04 +08:00
|
|
|
{
|
2002-03-18 12:57:06 +08:00
|
|
|
php_stream_temp_data *ts;
|
2002-03-18 07:50:31 +08:00
|
|
|
|
|
|
|
assert(stream != NULL);
|
2002-03-18 12:57:06 +08:00
|
|
|
ts = stream->abstract;
|
|
|
|
assert(ts != NULL);
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-18 12:57:06 +08:00
|
|
|
return php_stream_gets(ts->innerstream, buf, maxlen);
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
|
|
|
|
/* {{{ */
|
|
|
|
static int php_stream_temp_cast(php_stream *stream, int castas, void **ret TSRMLS_DC)
|
2002-03-22 04:37:04 +08:00
|
|
|
{
|
2002-03-18 12:57:06 +08:00
|
|
|
php_stream_temp_data *ts;
|
2002-03-20 07:29:37 +08:00
|
|
|
php_stream *file;
|
|
|
|
size_t memsize;
|
|
|
|
char *membuf;
|
|
|
|
off_t pos;
|
2002-03-18 07:50:31 +08:00
|
|
|
|
|
|
|
assert(stream != NULL);
|
2002-03-18 12:57:06 +08:00
|
|
|
ts = stream->abstract;
|
|
|
|
assert(ts != NULL);
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-20 07:29:37 +08:00
|
|
|
if (php_stream_is(ts->innerstream, PHP_STREAM_IS_STDIO)) {
|
|
|
|
return php_stream_cast(ts->innerstream, castas, ret, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we are still using a memory based backing. If they are if we can be
|
|
|
|
* a FILE*, say yes because we can perform the conversion.
|
|
|
|
* If they actually want to perform the conversion, we need to switch
|
|
|
|
* the memory stream to a tmpfile stream */
|
|
|
|
|
|
|
|
if (ret == NULL && castas == PHP_STREAM_AS_STDIO) {
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* say "no" to other stream forms */
|
|
|
|
if (ret == NULL) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
2002-03-22 04:37:04 +08:00
|
|
|
|
2002-03-20 07:29:37 +08:00
|
|
|
/* perform the conversion and then pass the request on to the innerstream */
|
|
|
|
membuf = php_stream_memory_get_buffer(ts->innerstream, &memsize);
|
|
|
|
file = php_stream_fopen_tmpfile();
|
|
|
|
php_stream_write(file, membuf, memsize);
|
|
|
|
pos = php_stream_tell(ts->innerstream);
|
2002-03-22 04:37:04 +08:00
|
|
|
|
2002-03-20 07:29:37 +08:00
|
|
|
php_stream_close(ts->innerstream);
|
|
|
|
ts->innerstream = file;
|
|
|
|
php_stream_seek(ts->innerstream, pos, SEEK_SET);
|
2002-03-22 04:37:04 +08:00
|
|
|
|
2002-03-20 07:29:37 +08:00
|
|
|
return php_stream_cast(ts->innerstream, castas, ret, 1);
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2002-03-18 07:50:31 +08:00
|
|
|
|
|
|
|
php_stream_ops php_stream_temp_ops = {
|
|
|
|
php_stream_temp_write, php_stream_temp_read,
|
|
|
|
php_stream_temp_close, php_stream_temp_flush,
|
2002-03-28 08:49:00 +08:00
|
|
|
"TEMP",
|
2002-03-18 07:50:31 +08:00
|
|
|
php_stream_temp_seek,
|
|
|
|
php_stream_temp_gets,
|
|
|
|
php_stream_temp_cast,
|
2002-03-28 08:49:00 +08:00
|
|
|
NULL
|
2002-03-18 07:50:31 +08:00
|
|
|
};
|
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
|
|
|
|
/* {{{ */
|
|
|
|
PHPAPI php_stream *_php_stream_temp_create(int mode, size_t max_memory_usage STREAMS_DC TSRMLS_DC)
|
2002-03-22 04:37:04 +08:00
|
|
|
{
|
2002-03-18 07:50:31 +08:00
|
|
|
php_stream_temp_data *self;
|
|
|
|
php_stream *stream;
|
|
|
|
|
2002-03-20 07:29:37 +08:00
|
|
|
self = ecalloc(1, sizeof(*self));
|
2002-03-18 12:57:06 +08:00
|
|
|
assert(self != NULL);
|
2002-03-18 07:50:31 +08:00
|
|
|
self->smax = max_memory_usage;
|
|
|
|
self->mode = mode;
|
2002-03-18 12:57:06 +08:00
|
|
|
stream = php_stream_alloc(&php_stream_temp_ops, self, 0, "rwb");
|
|
|
|
self->innerstream = php_stream_memory_create(mode);
|
2002-03-20 07:29:37 +08:00
|
|
|
// php_stream_temp_write(stream, NULL, 0 TSRMLS_CC);
|
2002-03-18 07:50:31 +08:00
|
|
|
return stream;
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-20 03:15:40 +08:00
|
|
|
|
|
|
|
/* {{{ */
|
|
|
|
PHPAPI php_stream *_php_stream_temp_open(int mode, size_t max_memory_usage, char *buf, size_t length STREAMS_DC TSRMLS_DC)
|
2002-03-22 04:37:04 +08:00
|
|
|
{
|
2002-03-18 07:50:31 +08:00
|
|
|
php_stream *stream;
|
|
|
|
php_stream_temp_data *ms;
|
|
|
|
|
2002-03-18 19:18:07 +08:00
|
|
|
if ((stream = php_stream_temp_create_rel(mode & ~TEMP_STREAM_READONLY, max_memory_usage)) != NULL) {
|
2002-03-18 12:57:06 +08:00
|
|
|
if (length) {
|
2002-03-18 07:50:31 +08:00
|
|
|
assert(buf != NULL);
|
2002-03-19 02:54:32 +08:00
|
|
|
php_stream_temp_write(stream, buf, length TSRMLS_CC);
|
2002-03-18 07:50:31 +08:00
|
|
|
}
|
|
|
|
ms = stream->abstract;
|
|
|
|
assert(ms != NULL);
|
|
|
|
ms->mode = mode;
|
|
|
|
}
|
|
|
|
return stream;
|
2002-03-20 03:15:40 +08:00
|
|
|
}
|
|
|
|
/* }}} */
|
2002-03-18 07:50:31 +08:00
|
|
|
|
2002-03-17 04:11:06 +08:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
* vim600: noet sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: noet sw=4 ts=4
|
|
|
|
*/
|