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