xref: /PHP-8.0/ext/zip/zip_stream.c (revision a6ffd8d1)
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: Piere-Alain Joye <pierre@php.net>                            |
14   +----------------------------------------------------------------------+
15 */
16 
17 #ifdef HAVE_CONFIG_H
18 #   include "config.h"
19 #endif
20 #include "php.h"
21 #ifdef HAVE_ZIP
22 
23 #include "php_streams.h"
24 #include "ext/standard/file.h"
25 #include "ext/standard/php_string.h"
26 #include "fopen_wrappers.h"
27 #include "php_zip.h"
28 
29 #include "ext/standard/url.h"
30 
31 /* needed for ssize_t definition */
32 #include <sys/types.h>
33 
34 struct php_zip_stream_data_t {
35 	struct zip *za;
36 	struct zip_file *zf;
37 	size_t cursor;
38 	php_stream *stream;
39 };
40 
41 #define STREAM_DATA_FROM_STREAM() \
42 	struct php_zip_stream_data_t *self = (struct php_zip_stream_data_t *) stream->abstract;
43 
44 
45 /* {{{ php_zip_ops_read */
php_zip_ops_read(php_stream * stream,char * buf,size_t count)46 static ssize_t php_zip_ops_read(php_stream *stream, char *buf, size_t count)
47 {
48 	ssize_t n = 0;
49 	STREAM_DATA_FROM_STREAM();
50 
51 	if (self->zf) {
52 		n = zip_fread(self->zf, buf, count);
53 		if (n < 0) {
54 #if LIBZIP_VERSION_MAJOR < 1
55 			int ze, se;
56 			zip_file_error_get(self->zf, &ze, &se);
57 			stream->eof = 1;
58 			php_error_docref(NULL, E_WARNING, "Zip stream error: %s", zip_file_strerror(self->zf));
59 #else
60 			zip_error_t *err;
61 			err = zip_file_get_error(self->zf);
62 			stream->eof = 1;
63 			php_error_docref(NULL, E_WARNING, "Zip stream error: %s", zip_error_strerror(err));
64 			zip_error_fini(err);
65 #endif
66 			return -1;
67 		}
68 		/* cast count to signed value to avoid possibly negative n
69 		 * being cast to unsigned value */
70 		if (n == 0 || n < (ssize_t)count) {
71 			stream->eof = 1;
72 		} else {
73 			self->cursor += n;
74 		}
75 	}
76 	return n;
77 }
78 /* }}} */
79 
80 /* {{{ php_zip_ops_write */
php_zip_ops_write(php_stream * stream,const char * buf,size_t count)81 static ssize_t php_zip_ops_write(php_stream *stream, const char *buf, size_t count)
82 {
83 	if (!stream) {
84 		return -1;
85 	}
86 
87 	return count;
88 }
89 /* }}} */
90 
91 /* {{{ php_zip_ops_close */
php_zip_ops_close(php_stream * stream,int close_handle)92 static int php_zip_ops_close(php_stream *stream, int close_handle)
93 {
94 	STREAM_DATA_FROM_STREAM();
95 	if (close_handle) {
96 		if (self->zf) {
97 			zip_fclose(self->zf);
98 			self->zf = NULL;
99 		}
100 
101 		if (self->za) {
102 			zip_close(self->za);
103 			self->za = NULL;
104 		}
105 	}
106 	efree(self);
107 	stream->abstract = NULL;
108 	return EOF;
109 }
110 /* }}} */
111 
112 /* {{{ php_zip_ops_flush */
php_zip_ops_flush(php_stream * stream)113 static int php_zip_ops_flush(php_stream *stream)
114 {
115 	if (!stream) {
116 		return 0;
117 	}
118 
119 	return 0;
120 }
121 /* }}} */
122 
php_zip_ops_stat(php_stream * stream,php_stream_statbuf * ssb)123 static int php_zip_ops_stat(php_stream *stream, php_stream_statbuf *ssb) /* {{{ */
124 {
125 	struct zip_stat sb;
126 	const char *path = stream->orig_path;
127 	size_t path_len = strlen(stream->orig_path);
128 	char file_dirname[MAXPATHLEN];
129 	struct zip *za;
130 	char *fragment;
131 	size_t fragment_len;
132 	int err;
133 	zend_string *file_basename;
134 
135 	fragment = strchr(path, '#');
136 	if (!fragment) {
137 		return -1;
138 	}
139 
140 
141 	if (strncasecmp("zip://", path, 6) == 0) {
142 		path += 6;
143 	}
144 
145 	fragment_len = strlen(fragment);
146 
147 	if (fragment_len < 1) {
148 		return -1;
149 	}
150 	path_len = strlen(path);
151 	if (path_len >= MAXPATHLEN) {
152 		return -1;
153 	}
154 
155 	memcpy(file_dirname, path, path_len - fragment_len);
156 	file_dirname[path_len - fragment_len] = '\0';
157 
158 	file_basename = php_basename((char *)path, path_len - fragment_len, NULL, 0);
159 	fragment++;
160 
161 	if (ZIP_OPENBASEDIR_CHECKPATH(file_dirname)) {
162 		zend_string_release_ex(file_basename, 0);
163 		return -1;
164 	}
165 
166 	za = zip_open(file_dirname, ZIP_CREATE, &err);
167 	if (za) {
168 		memset(ssb, 0, sizeof(php_stream_statbuf));
169 		if (zip_stat(za, fragment, ZIP_FL_NOCASE, &sb) != 0) {
170 			zip_close(za);
171 			zend_string_release_ex(file_basename, 0);
172 			return -1;
173 		}
174 		zip_close(za);
175 
176 		if (path[path_len-1] != '/') {
177 			ssb->sb.st_size = sb.size;
178 			ssb->sb.st_mode |= S_IFREG; /* regular file */
179 		} else {
180 			ssb->sb.st_size = 0;
181 			ssb->sb.st_mode |= S_IFDIR; /* regular directory */
182 		}
183 
184 		ssb->sb.st_mtime = sb.mtime;
185 		ssb->sb.st_atime = sb.mtime;
186 		ssb->sb.st_ctime = sb.mtime;
187 		ssb->sb.st_nlink = 1;
188 		ssb->sb.st_rdev = -1;
189 #ifndef PHP_WIN32
190 		ssb->sb.st_blksize = -1;
191 		ssb->sb.st_blocks = -1;
192 #endif
193 		ssb->sb.st_ino = -1;
194 	}
195 	zend_string_release_ex(file_basename, 0);
196 	return 0;
197 }
198 /* }}} */
199 
200 const php_stream_ops php_stream_zipio_ops = {
201 	php_zip_ops_write, php_zip_ops_read,
202 	php_zip_ops_close, php_zip_ops_flush,
203 	"zip",
204 	NULL, /* seek */
205 	NULL, /* cast */
206 	php_zip_ops_stat, /* stat */
207 	NULL  /* set_option */
208 };
209 
210 /* {{{ php_stream_zip_open */
php_stream_zip_open(struct zip * arch,const char * path,const char * mode STREAMS_DC)211 php_stream *php_stream_zip_open(struct zip *arch, const char *path, const char *mode STREAMS_DC)
212 {
213 	struct zip_file *zf = NULL;
214 
215 	php_stream *stream = NULL;
216 	struct php_zip_stream_data_t *self;
217 
218 	if (strncmp(mode,"r", strlen("r")) != 0) {
219 		return NULL;
220 	}
221 
222 	if (arch) {
223 		zf = zip_fopen(arch, path, 0);
224 		if (zf) {
225 			self = emalloc(sizeof(*self));
226 
227 			self->za = NULL; /* to keep it open on stream close */
228 			self->zf = zf;
229 			self->stream = NULL;
230 			self->cursor = 0;
231 			stream = php_stream_alloc(&php_stream_zipio_ops, self, NULL, mode);
232 			stream->orig_path = estrdup(path);
233 		}
234 	}
235 
236 	return stream;
237 }
238 /* }}} */
239 
240 /* {{{ php_stream_zip_opener */
php_stream_zip_opener(php_stream_wrapper * wrapper,const char * path,const char * mode,int options,zend_string ** opened_path,php_stream_context * context STREAMS_DC)241 php_stream *php_stream_zip_opener(php_stream_wrapper *wrapper,
242 											const char *path,
243 											const char *mode,
244 											int options,
245 											zend_string **opened_path,
246 											php_stream_context *context STREAMS_DC)
247 {
248 	size_t path_len;
249 
250 	zend_string *file_basename;
251 	char file_dirname[MAXPATHLEN];
252 
253 	struct zip *za;
254 	struct zip_file *zf = NULL;
255 	char *fragment;
256 	size_t fragment_len;
257 	int err;
258 
259 	php_stream *stream = NULL;
260 	struct php_zip_stream_data_t *self;
261 
262 	fragment = strchr(path, '#');
263 	if (!fragment) {
264 		return NULL;
265 	}
266 
267 	if (strncasecmp("zip://", path, 6) == 0) {
268 		path += 6;
269 	}
270 
271 	fragment_len = strlen(fragment);
272 
273 	if (fragment_len < 1) {
274 		return NULL;
275 	}
276 	path_len = strlen(path);
277 	if (path_len >= MAXPATHLEN || mode[0] != 'r') {
278 		return NULL;
279 	}
280 
281 	memcpy(file_dirname, path, path_len - fragment_len);
282 	file_dirname[path_len - fragment_len] = '\0';
283 
284 	file_basename = php_basename(path, path_len - fragment_len, NULL, 0);
285 	fragment++;
286 
287 	if (ZIP_OPENBASEDIR_CHECKPATH(file_dirname)) {
288 		zend_string_release_ex(file_basename, 0);
289 		return NULL;
290 	}
291 
292 	za = zip_open(file_dirname, ZIP_CREATE, &err);
293 	if (za) {
294 		zval *tmpzval;
295 
296 		if (context && NULL != (tmpzval = php_stream_context_get_option(context, "zip", "password"))) {
297 			if (Z_TYPE_P(tmpzval) != IS_STRING || zip_set_default_password(za, Z_STRVAL_P(tmpzval))) {
298 				php_error_docref(NULL, E_WARNING, "Can't set zip password");
299 			}
300 		}
301 
302 		zf = zip_fopen(za, fragment, 0);
303 		if (zf) {
304 			self = emalloc(sizeof(*self));
305 
306 			self->za = za;
307 			self->zf = zf;
308 			self->stream = NULL;
309 			self->cursor = 0;
310 			stream = php_stream_alloc(&php_stream_zipio_ops, self, NULL, mode);
311 
312 			if (opened_path) {
313 				*opened_path = zend_string_init(path, strlen(path), 0);
314 			}
315 		} else {
316 			zip_close(za);
317 		}
318 	}
319 
320 	zend_string_release_ex(file_basename, 0);
321 
322 	if (!stream) {
323 		return NULL;
324 	} else {
325 		return stream;
326 	}
327 }
328 /* }}} */
329 
330 static const php_stream_wrapper_ops zip_stream_wops = {
331 	php_stream_zip_opener,
332 	NULL,	/* close */
333 	NULL,	/* fstat */
334 	NULL,	/* stat */
335 	NULL,	/* opendir */
336 	"zip wrapper",
337 	NULL,	/* unlink */
338 	NULL,	/* rename */
339 	NULL,	/* mkdir */
340 	NULL,	/* rmdir */
341 	NULL	/* metadata */
342 };
343 
344 const php_stream_wrapper php_stream_zip_wrapper = {
345 	&zip_stream_wops,
346 	NULL,
347 	0 /* is_url */
348 };
349 #endif /* HAVE_ZIP */
350