xref: /php-src/ext/zlib/zlib_fopen_wrapper.c (revision 067df263)
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    | https://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 #ifndef _GNU_SOURCE
19 # define _GNU_SOURCE
20 #endif
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 ssize_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 read;
46 }
47 
php_gziop_write(php_stream * stream,const char * buf,size_t count)48 static ssize_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 
52 	/* XXX this needs to be looped for the case count > UINT_MAX */
53 	return gzwrite(self->gz_file, (char *) buf, count);
54 }
55 
php_gziop_seek(php_stream * stream,zend_off_t offset,int whence,zend_off_t * newoffs)56 static int php_gziop_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
57 {
58 	struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
59 
60 	assert(self != NULL);
61 
62 	if (whence == SEEK_END) {
63 		php_error_docref(NULL, E_WARNING, "SEEK_END is not supported");
64 		return -1;
65 	}
66 	*newoffs = gzseek(self->gz_file, offset, whence);
67 
68 	return (*newoffs < 0) ? -1 : 0;
69 }
70 
php_gziop_close(php_stream * stream,int close_handle)71 static int php_gziop_close(php_stream *stream, int close_handle)
72 {
73 	struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
74 	int ret = EOF;
75 
76 	if (close_handle) {
77 		if (self->gz_file) {
78 			ret = gzclose(self->gz_file);
79 			self->gz_file = NULL;
80 		}
81 		if (self->stream) {
82 			php_stream_close(self->stream);
83 			self->stream = NULL;
84 		}
85 	}
86 	efree(self);
87 
88 	return ret;
89 }
90 
php_gziop_flush(php_stream * stream)91 static int php_gziop_flush(php_stream *stream)
92 {
93 	struct php_gz_stream_data_t *self = (struct php_gz_stream_data_t *) stream->abstract;
94 
95 	return gzflush(self->gz_file, Z_SYNC_FLUSH);
96 }
97 
98 const php_stream_ops php_stream_gzio_ops = {
99 	php_gziop_write, php_gziop_read,
100 	php_gziop_close, php_gziop_flush,
101 	"ZLIB",
102 	php_gziop_seek,
103 	NULL, /* cast */
104 	NULL, /* stat */
105 	NULL  /* set_option */
106 };
107 
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)108 php_stream *php_stream_gzopen(php_stream_wrapper *wrapper, const char *path, const char *mode, int options,
109 							  zend_string **opened_path, php_stream_context *context STREAMS_DC)
110 {
111 	struct php_gz_stream_data_t *self;
112 	php_stream *stream = NULL, *innerstream = NULL;
113 
114 	/* sanity check the stream: it can be either read-only or write-only */
115 	if (strchr(mode, '+')) {
116 		if (options & REPORT_ERRORS) {
117 			php_error_docref(NULL, E_WARNING, "Cannot open a zlib stream for reading and writing at the same time!");
118 		}
119 		return NULL;
120 	}
121 
122 	if (strncasecmp("compress.zlib://", path, 16) == 0) {
123 		path += 16;
124 	} else if (strncasecmp("zlib:", path, 5) == 0) {
125 		path += 5;
126 	}
127 
128 	innerstream = php_stream_open_wrapper_ex(path, mode, STREAM_MUST_SEEK | options | STREAM_WILL_CAST, opened_path, context);
129 
130 	if (innerstream) {
131 		php_socket_t fd;
132 
133 		if (SUCCESS == php_stream_cast(innerstream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) {
134 			self = emalloc(sizeof(*self));
135 			self->stream = innerstream;
136 			self->gz_file = gzdopen(dup(fd), mode);
137 
138 			if (self->gz_file) {
139 				zval *zlevel = context ? php_stream_context_get_option(context, "zlib", "level") : NULL;
140 				if (zlevel && (Z_OK != gzsetparams(self->gz_file, zval_get_long(zlevel), Z_DEFAULT_STRATEGY))) {
141 					php_error(E_WARNING, "failed setting compression level");
142 				}
143 
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 const 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 const php_stream_wrapper php_stream_gzip_wrapper =	{
180 	&gzip_stream_wops,
181 	NULL,
182 	0, /* is_url */
183 };
184