xref: /PHP-7.3/ext/zlib/zlib_fopen_wrapper.c (revision 8d3f8ca1)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 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 #define _GNU_SOURCE
21 
22 #include "php.h"
23 #include "php_zlib.h"
24 #include "fopen_wrappers.h"
25 
26 #include "main/php_network.h"
27 
28 struct php_gz_stream_data_t	{
29 	gzFile gz_file;
30 	php_stream *stream;
31 };
32 
php_gziop_read(php_stream * stream,char * buf,size_t count)33 static size_t php_gziop_read(php_stream *stream, char *buf, size_t count)
34 {
35 	struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
36 	int read;
37 
38 	/* XXX this needs to be looped for the case count > UINT_MAX */
39 	read = gzread(self->gz_file, buf, count);
40 
41 	if (gzeof(self->gz_file)) {
42 		stream->eof = 1;
43 	}
44 
45 	return (size_t)((read < 0) ? 0 : read);
46 }
47 
php_gziop_write(php_stream * stream,const char * buf,size_t count)48 static size_t php_gziop_write(php_stream *stream, const char *buf, size_t count)
49 {
50 	struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
51 	int wrote;
52 
53 	/* XXX this needs to be looped for the case count > UINT_MAX */
54 	wrote = gzwrite(self->gz_file, (char *) buf, count);
55 
56 	return (size_t)((wrote < 0) ? 0 : wrote);
57 }
58 
php_gziop_seek(php_stream * stream,zend_off_t offset,int whence,zend_off_t * newoffs)59 static int php_gziop_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
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, 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)74 static int php_gziop_close(php_stream *stream, int close_handle)
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)94 static int php_gziop_flush(php_stream *stream)
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 const 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,zend_string ** opened_path,php_stream_context * context STREAMS_DC)111 php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, const char *path, const char *mode, int options,
112 							  zend_string **opened_path, php_stream_context *context STREAMS_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, 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 				zval *zlevel = context ? php_stream_context_get_option(context, "zlib", "level") : NULL;
143 				if (zlevel && (Z_OK != gzsetparams(self->gz_file, zval_get_long(zlevel), Z_DEFAULT_STRATEGY))) {
144 					php_error(E_WARNING, "failed setting compression level");
145 				}
146 
147 				stream = php_stream_alloc_rel(&php_stream_gzio_ops, self, 0, mode);
148 				if (stream) {
149 					stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
150 					return stream;
151 				}
152 
153 				gzclose(self->gz_file);
154 			}
155 
156 			efree(self);
157 			if (options & REPORT_ERRORS) {
158 				php_error_docref(NULL, E_WARNING, "gzopen failed");
159 			}
160 		}
161 
162 		php_stream_close(innerstream);
163 	}
164 
165 	return NULL;
166 }
167 
168 static const php_stream_wrapper_ops gzip_stream_wops = {
169 	php_stream_gzopen,
170 	NULL, /* close */
171 	NULL, /* stat */
172 	NULL, /* stat_url */
173 	NULL, /* opendir */
174 	"ZLIB",
175 	NULL, /* unlink */
176 	NULL, /* rename */
177 	NULL, /* mkdir */
178 	NULL, /* rmdir */
179 	NULL
180 };
181 
182 const php_stream_wrapper php_stream_gzip_wrapper =	{
183 	&gzip_stream_wops,
184 	NULL,
185 	0, /* is_url */
186 };
187 
188 /*
189  * Local variables:
190  * tab-width: 4
191  * c-basic-offset: 4
192  * End:
193  * vim600: noet sw=4 ts=4 fdm=marker
194  * vim<600: noet sw=4 ts=4
195  */
196