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
|
|
|
+----------------------------------------------------------------------+
|
2003-01-01 00:08:15 +08:00
|
|
|
| Copyright (c) 1997-2003 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-03-21 09:11:52 +08:00
|
|
|
| Author: Wez Furlong <wez@thebrainroom.com>, based on work by: |
|
2002-11-25 20:30:28 +08:00
|
|
|
| Hartmut Holzgraefe <hholzgra@php.net> |
|
2000-11-21 08:40:13 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
2003-03-17 22:45:07 +08:00
|
|
|
|
2000-11-21 08:40:13 +08:00
|
|
|
/* $Id$ */
|
2003-03-17 22:45:07 +08:00
|
|
|
|
2000-11-21 08:40:13 +08:00
|
|
|
#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-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
|
|
|
{
|
2003-03-17 22:45:07 +08:00
|
|
|
struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
|
2002-12-09 18:54:20 +08:00
|
|
|
int read;
|
2002-10-05 18:35:13 +08:00
|
|
|
|
2002-12-09 18:54:20 +08:00
|
|
|
read = gzread(self->gz_file, buf, count);
|
2002-10-16 00:01:00 +08:00
|
|
|
|
2003-03-17 22:45:07 +08:00
|
|
|
if (gzeof(self->gz_file)) {
|
2002-10-05 18:35:13 +08:00
|
|
|
stream->eof = 1;
|
2003-03-17 22:45:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (read < 0) ? 0 : read;
|
2000-11-21 08:40:13 +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
|
|
|
{
|
2003-03-17 22:45:07 +08:00
|
|
|
struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
|
2002-12-09 18:54:20 +08:00
|
|
|
int wrote;
|
2003-03-17 22:45:07 +08:00
|
|
|
|
|
|
|
wrote = gzwrite(self->gz_file, (char *) buf, count);
|
|
|
|
|
|
|
|
return (wrote < 0) ? 0 : wrote;
|
2000-11-21 08:40:13 +08:00
|
|
|
}
|
|
|
|
|
2002-09-23 09:47:04 +08:00
|
|
|
static int php_gziop_seek(php_stream *stream, off_t offset, int whence, off_t *newoffs TSRMLS_DC)
|
2002-03-16 05:03:08 +08:00
|
|
|
{
|
2003-03-17 22:45:07 +08:00
|
|
|
struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
|
2002-09-23 09:47:04 +08:00
|
|
|
int ret;
|
|
|
|
|
2002-03-21 09:11:52 +08:00
|
|
|
assert(self != NULL);
|
|
|
|
|
2002-09-23 09:47:04 +08:00
|
|
|
ret = gzseek(self->gz_file, offset, whence);
|
|
|
|
*newoffs = gztell(self->gz_file);
|
|
|
|
|
2003-03-17 22:45:07 +08:00
|
|
|
return (ret < 0) ? -1 : 0;
|
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
|
|
|
{
|
2003-03-17 22:45:07 +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-10-16 00:01:00 +08:00
|
|
|
if (close_handle) {
|
2002-12-09 18:54:20 +08:00
|
|
|
if (self->gz_file) {
|
|
|
|
ret = gzclose(self->gz_file);
|
|
|
|
self->gz_file = NULL;
|
|
|
|
}
|
2002-10-16 00:01:00 +08:00
|
|
|
}
|
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;
|
|
|
|
}
|
2003-03-17 22:45:07 +08:00
|
|
|
|
2002-03-21 10:27:41 +08:00
|
|
|
static int php_gziop_flush(php_stream *stream TSRMLS_DC)
|
|
|
|
{
|
2003-03-17 22:45:07 +08:00
|
|
|
struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
|
|
|
|
|
2002-03-21 10:27:41 +08:00
|
|
|
return gzflush(self->gz_file, Z_SYNC_FLUSH);
|
|
|
|
}
|
|
|
|
|
2002-03-16 05:03:08 +08:00
|
|
|
php_stream_ops php_stream_gzio_ops = {
|
|
|
|
php_gziop_write, php_gziop_read,
|
2002-03-21 10:27:41 +08:00
|
|
|
php_gziop_close, php_gziop_flush,
|
2002-03-28 08:49:00 +08:00
|
|
|
"ZLIB",
|
2002-09-23 09:47:04 +08:00
|
|
|
php_gziop_seek,
|
|
|
|
NULL, /* cast */
|
|
|
|
NULL, /* stat */
|
|
|
|
NULL /* set_option */
|
2000-11-21 08:40:13 +08:00
|
|
|
};
|
|
|
|
|
2003-03-17 22:45:07 +08:00
|
|
|
php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, char *path, char *mode, int options,
|
|
|
|
char **opened_path, php_stream_context *context 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;
|
2002-12-09 18:54:20 +08:00
|
|
|
php_stream *stream = NULL, *innerstream = NULL;
|
|
|
|
|
|
|
|
/* sanity check the stream: it can be either read-only or write-only */
|
|
|
|
if (strchr(mode, '+')) {
|
|
|
|
if (options & REPORT_ERRORS) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot open a zlib stream for reading and writing at the same time!");
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
|
|
|
|
self = emalloc(sizeof(*self));
|
2002-03-17 02:42:42 +08:00
|
|
|
|
2003-03-17 22:45:07 +08:00
|
|
|
if (strncasecmp("compress.zlib://", path, 16) == 0) {
|
2002-04-19 18:06:41 +08:00
|
|
|
path += 16;
|
2003-03-17 22:45:07 +08:00
|
|
|
} else if (strncasecmp("zlib:", path, 5) == 0) {
|
2002-04-07 01:29:39 +08:00
|
|
|
path += 5;
|
2003-03-17 22:45:07 +08:00
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2003-03-17 22:45:07 +08:00
|
|
|
innerstream = php_stream_open_wrapper(path, mode, STREAM_MUST_SEEK | options | STREAM_WILL_CAST, opened_path);
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2002-12-09 18:54:20 +08:00
|
|
|
if (innerstream) {
|
2002-03-16 05:03:08 +08:00
|
|
|
int fd;
|
2003-03-17 22:45:07 +08:00
|
|
|
|
|
|
|
if (SUCCESS == php_stream_cast(innerstream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_RELEASE, (void **) &fd, REPORT_ERRORS)) {
|
2002-03-16 05:03:08 +08:00
|
|
|
self->gz_file = gzdopen(fd, mode);
|
|
|
|
if (self->gz_file) {
|
2002-03-21 09:11:52 +08:00
|
|
|
stream = php_stream_alloc_rel(&php_stream_gzio_ops, self, 0, mode);
|
2002-10-15 10:27:15 +08:00
|
|
|
if (stream) {
|
|
|
|
stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
|
2002-03-16 05:03:08 +08:00
|
|
|
return stream;
|
2002-10-15 10:27:15 +08:00
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
gzclose(self->gz_file);
|
|
|
|
}
|
2003-03-17 22:45:07 +08:00
|
|
|
if (options & REPORT_ERRORS) {
|
2002-08-23 01:40:25 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "gzopen failed");
|
2003-03-17 22:45:07 +08:00
|
|
|
}
|
2002-12-09 18:54:20 +08:00
|
|
|
} else if (innerstream) {
|
|
|
|
php_stream_close(innerstream);
|
2000-11-21 08:40:13 +08:00
|
|
|
}
|
|
|
|
}
|
2003-03-17 22:45:07 +08:00
|
|
|
|
|
|
|
if (stream) {
|
2002-03-16 05:03:08 +08:00
|
|
|
php_stream_close(stream);
|
2003-03-17 22:45:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (self) {
|
2002-03-16 05:03:08 +08:00
|
|
|
efree(self);
|
2003-03-17 22:45:07 +08:00
|
|
|
}
|
|
|
|
|
2000-11-21 08:40:13 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-03-16 05:03:08 +08:00
|
|
|
|
2002-03-25 02:05:49 +08:00
|
|
|
static php_stream_wrapper_ops gzip_stream_wops = {
|
2002-03-16 05:03:08 +08:00
|
|
|
php_stream_gzopen,
|
2002-09-26 18:14:41 +08:00
|
|
|
NULL, /* close */
|
|
|
|
NULL, /* stat */
|
|
|
|
NULL, /* stat_url */
|
|
|
|
NULL, /* opendir */
|
2003-05-14 14:10:04 +08:00
|
|
|
"ZLIB",
|
|
|
|
NULL /* unlink */
|
2002-03-25 02:05:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
php_stream_wrapper php_stream_gzip_wrapper = {
|
|
|
|
&gzip_stream_wops,
|
2002-04-17 06:14:27 +08:00
|
|
|
NULL,
|
|
|
|
0, /* is_url */
|
2002-03-16 05:03:08 +08:00
|
|
|
};
|
|
|
|
|
2000-11-21 08:40:13 +08:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
2003-03-17 22:45:07 +08:00
|
|
|
* vim600: noet sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: noet sw=4 ts=4
|
2000-11-21 08:40:13 +08:00
|
|
|
*/
|