xref: /PHP-7.1/ext/zlib/zlib_fopen_wrapper.c (revision ccd4716e)
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 /* $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)35 static size_t php_gziop_read(php_stream *stream, char *buf, size_t count)
36 {
37 	struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
38 	int read;
39 
40 	/* XXX this needs to be looped for the case count > UINT_MAX */
41 	read = gzread(self->gz_file, buf, count);
42 
43 	if (gzeof(self->gz_file)) {
44 		stream->eof = 1;
45 	}
46 
47 	return (size_t)((read < 0) ? 0 : read);
48 }
49 
php_gziop_write(php_stream * stream,const char * buf,size_t count)50 static size_t php_gziop_write(php_stream *stream, const char *buf, size_t count)
51 {
52 	struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
53 	int wrote;
54 
55 	/* XXX this needs to be looped for the case count > UINT_MAX */
56 	wrote = gzwrite(self->gz_file, (char *) buf, count);
57 
58 	return (size_t)((wrote < 0) ? 0 : wrote);
59 }
60 
php_gziop_seek(php_stream * stream,zend_off_t offset,int whence,zend_off_t * newoffs)61 static int php_gziop_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
62 {
63 	struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
64 
65 	assert(self != NULL);
66 
67 	if (whence == SEEK_END) {
68 		php_error_docref(NULL, E_WARNING, "SEEK_END is not supported");
69 		return -1;
70 	}
71 	*newoffs = gzseek(self->gz_file, offset, whence);
72 
73 	return (*newoffs < 0) ? -1 : 0;
74 }
75 
php_gziop_close(php_stream * stream,int close_handle)76 static int php_gziop_close(php_stream *stream, int close_handle)
77 {
78 	struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
79 	int ret = EOF;
80 
81 	if (close_handle) {
82 		if (self->gz_file) {
83 			ret = gzclose(self->gz_file);
84 			self->gz_file = NULL;
85 		}
86 		if (self->stream) {
87 			php_stream_close(self->stream);
88 			self->stream = NULL;
89 		}
90 	}
91 	efree(self);
92 
93 	return ret;
94 }
95 
php_gziop_flush(php_stream * stream)96 static int php_gziop_flush(php_stream *stream)
97 {
98 	struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
99 
100 	return gzflush(self->gz_file, Z_SYNC_FLUSH);
101 }
102 
103 php_stream_ops php_stream_gzio_ops = {
104 	php_gziop_write, php_gziop_read,
105 	php_gziop_close, php_gziop_flush,
106 	"ZLIB",
107 	php_gziop_seek,
108 	NULL, /* cast */
109 	NULL, /* stat */
110 	NULL  /* set_option */
111 };
112 
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)113 php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, const char *path, const char *mode, int options,
114 							  zend_string **opened_path, php_stream_context *context STREAMS_DC)
115 {
116 	struct php_gz_stream_data_t *self;
117 	php_stream *stream = NULL, *innerstream = NULL;
118 
119 	/* sanity check the stream: it can be either read-only or write-only */
120 	if (strchr(mode, '+')) {
121 		if (options & REPORT_ERRORS) {
122 			php_error_docref(NULL, E_WARNING, "cannot open a zlib stream for reading and writing at the same time!");
123 		}
124 		return NULL;
125 	}
126 
127 	if (strncasecmp("compress.zlib://", path, 16) == 0) {
128 		path += 16;
129 	} else if (strncasecmp("zlib:", path, 5) == 0) {
130 		path += 5;
131 	}
132 
133 	innerstream = php_stream_open_wrapper_ex(path, mode, STREAM_MUST_SEEK | options | STREAM_WILL_CAST, opened_path, context);
134 
135 	if (innerstream) {
136 		php_socket_t fd;
137 
138 		if (SUCCESS == php_stream_cast(innerstream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
139 			self = emalloc(sizeof(*self));
140 			self->stream = innerstream;
141 			self->gz_file = gzdopen(dup(fd), mode);
142 
143 			if (self->gz_file) {
144 				stream = php_stream_alloc_rel(&php_stream_gzio_ops, self, 0, mode);
145 				if (stream) {
146 					stream->flags |= PHP_STREAM_FLAG_NO_BUFFER;
147 					return stream;
148 				}
149 
150 				gzclose(self->gz_file);
151 			}
152 
153 			efree(self);
154 			if (options & REPORT_ERRORS) {
155 				php_error_docref(NULL, E_WARNING, "gzopen failed");
156 			}
157 		}
158 
159 		php_stream_close(innerstream);
160 	}
161 
162 	return NULL;
163 }
164 
165 static php_stream_wrapper_ops gzip_stream_wops = {
166 	php_stream_gzopen,
167 	NULL, /* close */
168 	NULL, /* stat */
169 	NULL, /* stat_url */
170 	NULL, /* opendir */
171 	"ZLIB",
172 	NULL, /* unlink */
173 	NULL, /* rename */
174 	NULL, /* mkdir */
175 	NULL, /* rmdir */
176 	NULL
177 };
178 
179 php_stream_wrapper php_stream_gzip_wrapper =	{
180 	&gzip_stream_wops,
181 	NULL,
182 	0, /* is_url */
183 };
184 
185 /*
186  * Local variables:
187  * tab-width: 4
188  * c-basic-offset: 4
189  * End:
190  * vim600: noet sw=4 ts=4 fdm=marker
191  * vim<600: noet sw=4 ts=4
192  */
193