php-src/ext/zlib/zlib_fopen_wrapper.c

184 lines
5.1 KiB
C
Raw Normal View History

/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
2006-01-01 12:51:34 +00:00
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.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: Wez Furlong <wez@thebrainroom.com>, based on work by: |
2002-11-25 12:30:28 +00:00
| Hartmut Holzgraefe <hholzgra@php.net> |
+----------------------------------------------------------------------+
*/
2003-03-17 14:45:07 +00:00
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include "php.h"
#include "php_zlib.h"
2001-02-23 22:07:16 +00:00
#include "fopen_wrappers.h"
#include "main/php_network.h"
2002-03-15 21:03:08 +00:00
struct php_gz_stream_data_t {
gzFile gz_file;
php_stream *stream;
};
static ssize_t php_gziop_read(php_stream *stream, char *buf, size_t count)
{
2003-03-17 14:45:07 +00:00
struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
int read;
2015-01-03 09:22:58 +00:00
/* XXX this needs to be looped for the case count > UINT_MAX */
read = gzread(self->gz_file, buf, count);
2015-01-03 09:22:58 +00:00
2003-03-17 14:45:07 +00:00
if (gzeof(self->gz_file)) {
stream->eof = 1;
2003-03-17 14:45:07 +00:00
}
2015-01-03 09:22:58 +00:00
return read;
}
static ssize_t php_gziop_write(php_stream *stream, const char *buf, size_t count)
2002-03-15 21:03:08 +00:00
{
2003-03-17 14:45:07 +00:00
struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
/* XXX this needs to be looped for the case count > UINT_MAX */
return gzwrite(self->gz_file, (char *) buf, count);
}
2014-12-13 22:06:14 +00:00
static int php_gziop_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
2002-03-15 21:03:08 +00:00
{
2003-03-17 14:45:07 +00:00
struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
assert(self != NULL);
if (whence == SEEK_END) {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "SEEK_END is not supported");
return -1;
}
*newoffs = gzseek(self->gz_file, offset, whence);
return (*newoffs < 0) ? -1 : 0;
}
2014-12-13 22:06:14 +00:00
static int php_gziop_close(php_stream *stream, int close_handle)
2002-03-15 21:03:08 +00:00
{
2003-03-17 14:45:07 +00:00
struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
2002-03-18 11:49:40 +00:00
int ret = EOF;
2015-01-03 09:22:58 +00:00
if (close_handle) {
if (self->gz_file) {
ret = gzclose(self->gz_file);
self->gz_file = NULL;
}
if (self->stream) {
php_stream_close(self->stream);
self->stream = NULL;
}
}
2002-03-15 21:03:08 +00:00
efree(self);
2002-03-15 21:03:08 +00:00
return ret;
}
2003-03-17 14:45:07 +00:00
2014-12-13 22:06:14 +00:00
static int php_gziop_flush(php_stream *stream)
{
2003-03-17 14:45:07 +00:00
struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
return gzflush(self->gz_file, Z_SYNC_FLUSH);
}
const php_stream_ops php_stream_gzio_ops = {
2002-03-15 21:03:08 +00:00
php_gziop_write, php_gziop_read,
php_gziop_close, php_gziop_flush,
"ZLIB",
2015-01-03 09:22:58 +00:00
php_gziop_seek,
NULL, /* cast */
NULL, /* stat */
NULL /* set_option */
};
2015-01-03 09:22:58 +00:00
php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, const char *path, const char *mode, int options,
zend_string **opened_path, php_stream_context *context STREAMS_DC)
{
struct php_gz_stream_data_t *self;
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, E_WARNING, "Cannot open a zlib stream for reading and writing at the same time!");
}
return NULL;
}
2015-01-03 09:22:58 +00:00
2003-03-17 14:45:07 +00:00
if (strncasecmp("compress.zlib://", path, 16) == 0) {
path += 16;
2003-03-17 14:45:07 +00:00
} else if (strncasecmp("zlib:", path, 5) == 0) {
path += 5;
2003-03-17 14:45:07 +00:00
}
2015-01-03 09:22:58 +00:00
innerstream = php_stream_open_wrapper_ex(path, mode, STREAM_MUST_SEEK | options | STREAM_WILL_CAST, opened_path, context);
2015-01-03 09:22:58 +00:00
if (innerstream) {
php_socket_t fd;
2003-03-17 14:45:07 +00:00
if (SUCCESS == php_stream_cast(innerstream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
self = emalloc(sizeof(*self));
self->stream = innerstream;
self->gz_file = gzdopen(dup(fd), mode);
if (self->gz_file) {
zval *zlevel = context ? php_stream_context_get_option(context, "zlib", "level") : NULL;
if (zlevel && (Z_OK != gzsetparams(self->gz_file, zval_get_long(zlevel), Z_DEFAULT_STRATEGY))) {
php_error(E_WARNING, "failed setting compression level");
}
stream = php_stream_alloc_rel(&php_stream_gzio_ops, self, 0, mode);
if (stream) {
stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
2002-03-15 21:03:08 +00:00
return stream;
}
2002-03-15 21:03:08 +00:00
gzclose(self->gz_file);
}
efree(self);
2003-03-17 14:45:07 +00:00
if (options & REPORT_ERRORS) {
2014-12-13 22:06:14 +00:00
php_error_docref(NULL, E_WARNING, "gzopen failed");
2003-03-17 14:45:07 +00:00
}
}
2003-03-17 14:45:07 +00:00
php_stream_close(innerstream);
2003-03-17 14:45:07 +00:00
}
return NULL;
}
2002-03-15 21:03:08 +00:00
static const php_stream_wrapper_ops gzip_stream_wops = {
2002-03-15 21:03:08 +00:00
php_stream_gzopen,
NULL, /* close */
NULL, /* stat */
NULL, /* stat_url */
NULL, /* opendir */
"ZLIB",
NULL, /* unlink */
NULL, /* rename */
NULL, /* mkdir */
2016-06-21 21:40:50 +00:00
NULL, /* rmdir */
NULL
};
const php_stream_wrapper php_stream_gzip_wrapper = {
&gzip_stream_wops,
NULL,
0, /* is_url */
2002-03-15 21:03:08 +00:00
};