1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | http://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Wez Furlong <wez@thebrainroom.com>, based on work by: |
14 | Hartmut Holzgraefe <hholzgra@php.net> |
15 +----------------------------------------------------------------------+
16 */
17
18 #define _GNU_SOURCE
19
20 #include "php.h"
21 #include "php_zlib.h"
22 #include "fopen_wrappers.h"
23
24 #include "main/php_network.h"
25
26 struct php_gz_stream_data_t {
27 gzFile gz_file;
28 php_stream *stream;
29 };
30
php_gziop_read(php_stream * stream,char * buf,size_t count)31 static ssize_t php_gziop_read(php_stream *stream, char *buf, size_t count)
32 {
33 struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
34 int read;
35
36 /* XXX this needs to be looped for the case count > UINT_MAX */
37 read = gzread(self->gz_file, buf, count);
38
39 if (gzeof(self->gz_file)) {
40 stream->eof = 1;
41 }
42
43 return read;
44 }
45
php_gziop_write(php_stream * stream,const char * buf,size_t count)46 static ssize_t php_gziop_write(php_stream *stream, const char *buf, size_t count)
47 {
48 struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
49
50 /* XXX this needs to be looped for the case count > UINT_MAX */
51 return gzwrite(self->gz_file, (char *) buf, count);
52 }
53
php_gziop_seek(php_stream * stream,zend_off_t offset,int whence,zend_off_t * newoffs)54 static int php_gziop_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
55 {
56 struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
57
58 assert(self != NULL);
59
60 if (whence == SEEK_END) {
61 php_error_docref(NULL, E_WARNING, "SEEK_END is not supported");
62 return -1;
63 }
64 *newoffs = gzseek(self->gz_file, offset, whence);
65
66 return (*newoffs < 0) ? -1 : 0;
67 }
68
php_gziop_close(php_stream * stream,int close_handle)69 static int php_gziop_close(php_stream *stream, int close_handle)
70 {
71 struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
72 int ret = EOF;
73
74 if (close_handle) {
75 if (self->gz_file) {
76 ret = gzclose(self->gz_file);
77 self->gz_file = NULL;
78 }
79 if (self->stream) {
80 php_stream_close(self->stream);
81 self->stream = NULL;
82 }
83 }
84 efree(self);
85
86 return ret;
87 }
88
php_gziop_flush(php_stream * stream)89 static int php_gziop_flush(php_stream *stream)
90 {
91 struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
92
93 return gzflush(self->gz_file, Z_SYNC_FLUSH);
94 }
95
96 const php_stream_ops php_stream_gzio_ops = {
97 php_gziop_write, php_gziop_read,
98 php_gziop_close, php_gziop_flush,
99 "ZLIB",
100 php_gziop_seek,
101 NULL, /* cast */
102 NULL, /* stat */
103 NULL /* set_option */
104 };
105
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)106 php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, const char *path, const char *mode, int options,
107 zend_string **opened_path, php_stream_context *context STREAMS_DC)
108 {
109 struct php_gz_stream_data_t *self;
110 php_stream *stream = NULL, *innerstream = NULL;
111
112 /* sanity check the stream: it can be either read-only or write-only */
113 if (strchr(mode, '+')) {
114 if (options & REPORT_ERRORS) {
115 php_error_docref(NULL, E_WARNING, "Cannot open a zlib stream for reading and writing at the same time!");
116 }
117 return NULL;
118 }
119
120 if (strncasecmp("compress.zlib://", path, 16) == 0) {
121 path += 16;
122 } else if (strncasecmp("zlib:", path, 5) == 0) {
123 path += 5;
124 }
125
126 innerstream = php_stream_open_wrapper_ex(path, mode, STREAM_MUST_SEEK | options | STREAM_WILL_CAST, opened_path, context);
127
128 if (innerstream) {
129 php_socket_t fd;
130
131 if (SUCCESS == php_stream_cast(innerstream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
132 self = emalloc(sizeof(*self));
133 self->stream = innerstream;
134 self->gz_file = gzdopen(dup(fd), mode);
135
136 if (self->gz_file) {
137 zval *zlevel = context ? php_stream_context_get_option(context, "zlib", "level") : NULL;
138 if (zlevel && (Z_OK != gzsetparams(self->gz_file, zval_get_long(zlevel), Z_DEFAULT_STRATEGY))) {
139 php_error(E_WARNING, "failed setting compression level");
140 }
141
142 stream = php_stream_alloc_rel(&php_stream_gzio_ops, self, 0, mode);
143 if (stream) {
144 stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
145 return stream;
146 }
147
148 gzclose(self->gz_file);
149 }
150
151 efree(self);
152 if (options & REPORT_ERRORS) {
153 php_error_docref(NULL, E_WARNING, "gzopen failed");
154 }
155 }
156
157 php_stream_close(innerstream);
158 }
159
160 return NULL;
161 }
162
163 static const php_stream_wrapper_ops gzip_stream_wops = {
164 php_stream_gzopen,
165 NULL, /* close */
166 NULL, /* stat */
167 NULL, /* stat_url */
168 NULL, /* opendir */
169 "ZLIB",
170 NULL, /* unlink */
171 NULL, /* rename */
172 NULL, /* mkdir */
173 NULL, /* rmdir */
174 NULL
175 };
176
177 const php_stream_wrapper php_stream_gzip_wrapper = {
178 &gzip_stream_wops,
179 NULL,
180 0, /* is_url */
181 };
182