2000-11-21 08:40:13 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 23:32:16 +08:00
|
|
|
| PHP Version 4 |
|
2000-11-21 08:40:13 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 23:32:16 +08:00
|
|
|
| Copyright (c) 1997-2002 The PHP Group |
|
2000-11-21 08:40:13 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| 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. |
|
|
|
|
+----------------------------------------------------------------------+
|
2002-02-28 16:29:35 +08:00
|
|
|
| Author: Hartmut Holzgraefe <hartmut@six.de> |
|
2000-11-21 08:40:13 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
/* $Id$ */
|
|
|
|
#define IS_EXT_MODULE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
|
|
#include "php.h"
|
|
|
|
#include "php_zlib.h"
|
2001-02-24 06:07:16 +08:00
|
|
|
#include "fopen_wrappers.h"
|
2000-11-21 08:40:13 +08:00
|
|
|
|
2002-03-16 05:03:08 +08:00
|
|
|
struct php_gz_stream_data_t {
|
2000-11-21 08:40:13 +08:00
|
|
|
gzFile gz_file;
|
2002-03-16 09:37:24 +08:00
|
|
|
php_stream *stream;
|
2000-11-21 08:40:13 +08:00
|
|
|
};
|
|
|
|
|
2002-03-19 02:54:32 +08:00
|
|
|
static size_t php_gziop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
|
2000-11-21 08:40:13 +08:00
|
|
|
{
|
2002-03-16 09:37:24 +08:00
|
|
|
struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *)stream->abstract;
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2002-03-16 22:39:51 +08:00
|
|
|
if (buf == NULL && count == 0) {
|
2002-03-16 05:03:08 +08:00
|
|
|
if (gzeof(self->gz_file))
|
|
|
|
return EOF;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gzread(self->gz_file, buf, count);
|
2000-11-21 08:40:13 +08:00
|
|
|
}
|
|
|
|
|
2002-03-19 02:54:32 +08:00
|
|
|
static char *php_gziop_gets(php_stream *stream, char *buf, size_t size TSRMLS_DC)
|
2002-03-16 05:03:08 +08:00
|
|
|
{
|
2002-03-16 09:37:24 +08:00
|
|
|
struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *)stream->abstract;
|
2002-03-16 05:03:08 +08:00
|
|
|
return gzgets(self->gz_file, buf, size);
|
2000-11-21 08:40:13 +08:00
|
|
|
}
|
|
|
|
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2002-03-19 02:54:32 +08:00
|
|
|
static size_t php_gziop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
|
2002-03-16 05:03:08 +08:00
|
|
|
{
|
2002-03-16 09:37:24 +08:00
|
|
|
struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *)stream->abstract;
|
2002-03-16 05:03:08 +08:00
|
|
|
return gzwrite(self->gz_file, (char*)buf, count);
|
2000-11-21 08:40:13 +08:00
|
|
|
}
|
|
|
|
|
2002-03-19 02:54:32 +08:00
|
|
|
static int php_gziop_seek(php_stream *stream, off_t offset, int whence TSRMLS_DC)
|
2002-03-16 05:03:08 +08:00
|
|
|
{
|
2002-03-16 09:37:24 +08:00
|
|
|
struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *)stream->abstract;
|
2002-03-16 05:03:08 +08:00
|
|
|
return gzseek(self->gz_file, offset, whence);
|
2000-11-21 08:40:13 +08:00
|
|
|
}
|
|
|
|
|
2002-03-19 02:54:32 +08:00
|
|
|
static int php_gziop_close(php_stream *stream, int close_handle TSRMLS_DC)
|
2002-03-16 05:03:08 +08:00
|
|
|
{
|
2002-03-16 09:37:24 +08:00
|
|
|
struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *)stream->abstract;
|
2002-03-18 19:49:40 +08:00
|
|
|
int ret = EOF;
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2002-03-16 22:39:51 +08:00
|
|
|
if (close_handle)
|
|
|
|
ret = gzclose(self->gz_file);
|
2002-03-19 02:54:32 +08:00
|
|
|
php_stream_free(self->stream, PHP_STREAM_FREE_CLOSE | (close_handle == 0 ? PHP_STREAM_FREE_PRESERVE_HANDLE : 0));
|
2002-03-16 05:03:08 +08:00
|
|
|
efree(self);
|
2000-11-21 08:40:13 +08:00
|
|
|
|
2002-03-16 05:03:08 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2000-11-21 08:40:13 +08:00
|
|
|
|
2002-03-16 05:03:08 +08:00
|
|
|
php_stream_ops php_stream_gzio_ops = {
|
|
|
|
php_gziop_write, php_gziop_read,
|
|
|
|
php_gziop_close, NULL,
|
|
|
|
php_gziop_seek, php_gziop_gets,
|
|
|
|
NULL, "ZLIB"
|
2000-11-21 08:40:13 +08:00
|
|
|
};
|
|
|
|
|
2002-03-20 21:21:55 +08:00
|
|
|
php_stream *php_stream_gzopen(char *path, char *mode, int options, char **opened_path, void *wrappercontext STREAMS_DC TSRMLS_DC)
|
2000-11-21 08:40:13 +08:00
|
|
|
{
|
2002-03-16 09:37:24 +08:00
|
|
|
struct php_gz_stream_data_t *self;
|
|
|
|
php_stream *stream = NULL;
|
2002-03-16 05:03:08 +08:00
|
|
|
|
|
|
|
self = emalloc(sizeof(*self));
|
2002-03-17 02:42:42 +08:00
|
|
|
|
|
|
|
if (strncmp("zlib:", path, 5) == 0)
|
|
|
|
path += 5;
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2002-03-18 06:50:59 +08:00
|
|
|
self->stream = php_stream_open_wrapper(path, mode, options, opened_path);
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2002-03-16 22:39:51 +08:00
|
|
|
if (self->stream) {
|
2002-03-16 05:03:08 +08:00
|
|
|
int fd;
|
2002-03-16 22:39:51 +08:00
|
|
|
if (SUCCESS == php_stream_cast(self->stream, PHP_STREAM_AS_FD, (void**)&fd, REPORT_ERRORS)) {
|
2002-03-16 05:03:08 +08:00
|
|
|
self->gz_file = gzdopen(fd, mode);
|
|
|
|
if (self->gz_file) {
|
|
|
|
stream = php_stream_alloc(&php_stream_gzio_ops, self, 0, mode);
|
|
|
|
if (stream)
|
|
|
|
return stream;
|
|
|
|
gzclose(self->gz_file);
|
|
|
|
}
|
2000-11-21 08:40:13 +08:00
|
|
|
}
|
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
if (stream)
|
|
|
|
php_stream_close(stream);
|
|
|
|
if (self && self->stream)
|
|
|
|
php_stream_close(self->stream);
|
|
|
|
if (self)
|
|
|
|
efree(self);
|
|
|
|
|
2000-11-21 08:40:13 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
|
|
|
|
php_stream_wrapper php_stream_gzip_wrapper = {
|
|
|
|
php_stream_gzopen,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-11-21 08:40:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
*/
|