xref: /PHP-7.2/ext/phar/phar_object.c (revision e5c95234)
1 /*
2   +----------------------------------------------------------------------+
3   | phar php single-file executable PHP extension                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 2005-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   | Authors: Gregory Beaver <cellog@php.net>                             |
16   |          Marcus Boerger <helly@php.net>                              |
17   +----------------------------------------------------------------------+
18 */
19 
20 /* $Id$ */
21 
22 #include "phar_internal.h"
23 #include "func_interceptors.h"
24 
25 static zend_class_entry *phar_ce_archive;
26 static zend_class_entry *phar_ce_data;
27 static zend_class_entry *phar_ce_PharException;
28 static zend_class_entry *phar_ce_entry;
29 
phar_file_type(HashTable * mimes,char * file,char ** mime_type)30 static int phar_file_type(HashTable *mimes, char *file, char **mime_type) /* {{{ */
31 {
32 	char *ext;
33 	phar_mime_type *mime;
34 	ext = strrchr(file, '.');
35 	if (!ext) {
36 		*mime_type = "text/plain";
37 		/* no file extension = assume text/plain */
38 		return PHAR_MIME_OTHER;
39 	}
40 	++ext;
41 	if (NULL == (mime = zend_hash_str_find_ptr(mimes, ext, strlen(ext)))) {
42 		*mime_type = "application/octet-stream";
43 		return PHAR_MIME_OTHER;
44 	}
45 	*mime_type = mime->mime;
46 	return mime->type;
47 }
48 /* }}} */
49 
phar_mung_server_vars(char * fname,char * entry,int entry_len,char * basename,int request_uri_len)50 static void phar_mung_server_vars(char *fname, char *entry, int entry_len, char *basename, int request_uri_len) /* {{{ */
51 {
52 	HashTable *_SERVER;
53 	zval *stuff;
54 	char *path_info;
55 	size_t basename_len = strlen(basename);
56 	size_t code;
57 	zval temp;
58 
59 	/* "tweak" $_SERVER variables requested in earlier call to Phar::mungServer() */
60 	if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_UNDEF) {
61 		return;
62 	}
63 
64 	_SERVER = Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]);
65 
66 	/* PATH_INFO and PATH_TRANSLATED should always be munged */
67 	if (NULL != (stuff = zend_hash_str_find(_SERVER, "PATH_INFO", sizeof("PATH_INFO")-1))) {
68 		path_info = Z_STRVAL_P(stuff);
69 		code = Z_STRLEN_P(stuff);
70 		if (code > entry_len && !memcmp(path_info, entry, entry_len)) {
71 			ZVAL_STR(&temp, Z_STR_P(stuff));
72 			ZVAL_STRINGL(stuff, path_info + entry_len, request_uri_len);
73 			zend_hash_str_update(_SERVER, "PHAR_PATH_INFO", sizeof("PHAR_PATH_INFO")-1, &temp);
74 		}
75 	}
76 
77 	if (NULL != (stuff = zend_hash_str_find(_SERVER, "PATH_TRANSLATED", sizeof("PATH_TRANSLATED")-1))) {
78 		zend_string *str = strpprintf(4096, "phar://%s%s", fname, entry);
79 
80 		ZVAL_STR(&temp, Z_STR_P(stuff));
81 		ZVAL_NEW_STR(stuff, str);
82 
83 		zend_hash_str_update(_SERVER, "PHAR_PATH_TRANSLATED", sizeof("PHAR_PATH_TRANSLATED")-1, &temp);
84 	}
85 
86 	if (!PHAR_G(phar_SERVER_mung_list)) {
87 		return;
88 	}
89 
90 	if (PHAR_G(phar_SERVER_mung_list) & PHAR_MUNG_REQUEST_URI) {
91 		if (NULL != (stuff = zend_hash_str_find(_SERVER, "REQUEST_URI", sizeof("REQUEST_URI")-1))) {
92 			path_info = Z_STRVAL_P(stuff);
93 			code = Z_STRLEN_P(stuff);
94 			if (code > basename_len && !memcmp(path_info, basename, basename_len)) {
95 				ZVAL_STR(&temp, Z_STR_P(stuff));
96 				ZVAL_STRINGL(stuff, path_info + basename_len, code - basename_len);
97 				zend_hash_str_update(_SERVER, "PHAR_REQUEST_URI", sizeof("PHAR_REQUEST_URI")-1, &temp);
98 			}
99 		}
100 	}
101 
102 	if (PHAR_G(phar_SERVER_mung_list) & PHAR_MUNG_PHP_SELF) {
103 		if (NULL != (stuff = zend_hash_str_find(_SERVER, "PHP_SELF", sizeof("PHP_SELF")-1))) {
104 			path_info = Z_STRVAL_P(stuff);
105 			code = Z_STRLEN_P(stuff);
106 
107 			if (code > basename_len && !memcmp(path_info, basename, basename_len)) {
108 				ZVAL_STR(&temp, Z_STR_P(stuff));
109 				ZVAL_STRINGL(stuff, path_info + basename_len, code - basename_len);
110 				zend_hash_str_update(_SERVER, "PHAR_PHP_SELF", sizeof("PHAR_PHP_SELF")-1, &temp);
111 			}
112 		}
113 	}
114 
115 	if (PHAR_G(phar_SERVER_mung_list) & PHAR_MUNG_SCRIPT_NAME) {
116 		if (NULL != (stuff = zend_hash_str_find(_SERVER, "SCRIPT_NAME", sizeof("SCRIPT_NAME")-1))) {
117 			ZVAL_STR(&temp, Z_STR_P(stuff));
118 			ZVAL_STRINGL(stuff, entry, entry_len);
119 			zend_hash_str_update(_SERVER, "PHAR_SCRIPT_NAME", sizeof("PHAR_SCRIPT_NAME")-1, &temp);
120 		}
121 	}
122 
123 	if (PHAR_G(phar_SERVER_mung_list) & PHAR_MUNG_SCRIPT_FILENAME) {
124 		if (NULL != (stuff = zend_hash_str_find(_SERVER, "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME")-1))) {
125 			zend_string *str = strpprintf(4096, "phar://%s%s", fname, entry);
126 
127 			ZVAL_STR(&temp, Z_STR_P(stuff));
128 			ZVAL_NEW_STR(stuff, str);
129 
130 			zend_hash_str_update(_SERVER, "PHAR_SCRIPT_FILENAME", sizeof("PHAR_SCRIPT_FILENAME")-1, &temp);
131 		}
132 	}
133 }
134 /* }}} */
135 
phar_file_action(phar_archive_data * phar,phar_entry_info * info,char * mime_type,int code,char * entry,int entry_len,char * arch,char * basename,char * ru,int ru_len)136 static int phar_file_action(phar_archive_data *phar, phar_entry_info *info, char *mime_type, int code, char *entry, int entry_len, char *arch, char *basename, char *ru, int ru_len) /* {{{ */
137 {
138 	char *name = NULL, buf[8192];
139 	const char *cwd;
140 	zend_syntax_highlighter_ini syntax_highlighter_ini;
141 	sapi_header_line ctr = {0};
142 	size_t got;
143 	zval dummy;
144 	size_t name_len;
145 	zend_file_handle file_handle;
146 	zend_op_array *new_op_array;
147 	zval result;
148 	php_stream *fp;
149 	zend_off_t position;
150 
151 	switch (code) {
152 		case PHAR_MIME_PHPS:
153 			efree(basename);
154 			/* highlight source */
155 			if (entry[0] == '/') {
156 				spprintf(&name, 4096, "phar://%s%s", arch, entry);
157 			} else {
158 				spprintf(&name, 4096, "phar://%s/%s", arch, entry);
159 			}
160 			php_get_highlight_struct(&syntax_highlighter_ini);
161 
162 			highlight_file(name, &syntax_highlighter_ini);
163 
164 			efree(name);
165 #ifdef PHP_WIN32
166 			efree(arch);
167 #endif
168 			zend_bailout();
169 		case PHAR_MIME_OTHER:
170 			/* send headers, output file contents */
171 			efree(basename);
172 			ctr.line_len = spprintf(&(ctr.line), 0, "Content-type: %s", mime_type);
173 			sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
174 			efree(ctr.line);
175 			ctr.line_len = spprintf(&(ctr.line), 0, "Content-length: %u", info->uncompressed_filesize);
176 			sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
177 			efree(ctr.line);
178 
179 			if (FAILURE == sapi_send_headers()) {
180 				zend_bailout();
181 			}
182 
183 			/* prepare to output  */
184 			fp = phar_get_efp(info, 1);
185 
186 			if (!fp) {
187 				char *error;
188 				if (!phar_open_jit(phar, info, &error)) {
189 					if (error) {
190 						zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
191 						efree(error);
192 					}
193 					return -1;
194 				}
195 				fp = phar_get_efp(info, 1);
196 			}
197 			position = 0;
198 			phar_seek_efp(info, 0, SEEK_SET, 0, 1);
199 
200 			do {
201 				got = php_stream_read(fp, buf, MIN(8192, info->uncompressed_filesize - position));
202 				if (got > 0) {
203 					PHPWRITE(buf, got);
204 					position += got;
205 					if (position == (zend_off_t) info->uncompressed_filesize) {
206 						break;
207 					}
208 				}
209 			} while (1);
210 
211 			zend_bailout();
212 		case PHAR_MIME_PHP:
213 			if (basename) {
214 				phar_mung_server_vars(arch, entry, entry_len, basename, ru_len);
215 				efree(basename);
216 			}
217 
218 			if (entry[0] == '/') {
219 				name_len = spprintf(&name, 4096, "phar://%s%s", arch, entry);
220 			} else {
221 				name_len = spprintf(&name, 4096, "phar://%s/%s", arch, entry);
222 			}
223 
224 			file_handle.type = ZEND_HANDLE_FILENAME;
225 			file_handle.handle.fd = 0;
226 			file_handle.filename = name;
227 			file_handle.opened_path = NULL;
228 			file_handle.free_filename = 0;
229 
230 			PHAR_G(cwd) = NULL;
231 			PHAR_G(cwd_len) = 0;
232 
233 			ZVAL_NULL(&dummy);
234 			if (zend_hash_str_add(&EG(included_files), name, name_len, &dummy) != NULL) {
235 				if ((cwd = zend_memrchr(entry, '/', entry_len))) {
236 					PHAR_G(cwd_init) = 1;
237 					if (entry == cwd) {
238 						/* root directory */
239 						PHAR_G(cwd_len) = 0;
240 						PHAR_G(cwd) = NULL;
241 					} else if (entry[0] == '/') {
242 						PHAR_G(cwd_len) = (int)(cwd - (entry + 1));
243 						PHAR_G(cwd) = estrndup(entry + 1, PHAR_G(cwd_len));
244 					} else {
245 						PHAR_G(cwd_len) = (int)(cwd - entry);
246 						PHAR_G(cwd) = estrndup(entry, PHAR_G(cwd_len));
247 					}
248 				}
249 
250 				new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE);
251 
252 				if (!new_op_array) {
253 					zend_hash_str_del(&EG(included_files), name, name_len);
254 				}
255 
256 				zend_destroy_file_handle(&file_handle);
257 
258 			} else {
259 				efree(name);
260 				new_op_array = NULL;
261 			}
262 #ifdef PHP_WIN32
263 			efree(arch);
264 #endif
265 			if (new_op_array) {
266 				ZVAL_UNDEF(&result);
267 
268 				zend_try {
269 					zend_execute(new_op_array, &result);
270 					if (PHAR_G(cwd)) {
271 						efree(PHAR_G(cwd));
272 						PHAR_G(cwd) = NULL;
273 						PHAR_G(cwd_len) = 0;
274 					}
275 
276 					PHAR_G(cwd_init) = 0;
277 					efree(name);
278 					destroy_op_array(new_op_array);
279 					efree(new_op_array);
280 					zval_ptr_dtor(&result);
281 				} zend_catch {
282 					if (PHAR_G(cwd)) {
283 						efree(PHAR_G(cwd));
284 						PHAR_G(cwd) = NULL;
285 						PHAR_G(cwd_len) = 0;
286 					}
287 
288 					PHAR_G(cwd_init) = 0;
289 					efree(name);
290 				} zend_end_try();
291 
292 				zend_bailout();
293 			}
294 
295 			return PHAR_MIME_PHP;
296 	}
297 	return -1;
298 }
299 /* }}} */
300 
phar_do_403(char * entry,int entry_len)301 static void phar_do_403(char *entry, int entry_len) /* {{{ */
302 {
303 	sapi_header_line ctr = {0};
304 
305 	ctr.response_code = 403;
306 	ctr.line_len = sizeof("HTTP/1.0 403 Access Denied")-1;
307 	ctr.line = "HTTP/1.0 403 Access Denied";
308 	sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
309 	sapi_send_headers();
310 	PHPWRITE("<html>\n <head>\n  <title>Access Denied</title>\n </head>\n <body>\n  <h1>403 - File ", sizeof("<html>\n <head>\n  <title>Access Denied</title>\n </head>\n <body>\n  <h1>403 - File ") - 1);
311 	PHPWRITE("Access Denied</h1>\n </body>\n</html>", sizeof("Access Denied</h1>\n </body>\n</html>") - 1);
312 }
313 /* }}} */
314 
phar_do_404(phar_archive_data * phar,char * fname,int fname_len,char * f404,int f404_len,char * entry,size_t entry_len)315 static void phar_do_404(phar_archive_data *phar, char *fname, int fname_len, char *f404, int f404_len, char *entry, size_t entry_len) /* {{{ */
316 {
317 	sapi_header_line ctr = {0};
318 	phar_entry_info	*info;
319 
320 	if (phar && f404_len) {
321 		info = phar_get_entry_info(phar, f404, f404_len, NULL, 1);
322 
323 		if (info) {
324 			phar_file_action(phar, info, "text/html", PHAR_MIME_PHP, f404, f404_len, fname, NULL, NULL, 0);
325 			return;
326 		}
327 	}
328 
329 	ctr.response_code = 404;
330 	ctr.line_len = sizeof("HTTP/1.0 404 Not Found")-1;
331 	ctr.line = "HTTP/1.0 404 Not Found";
332 	sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
333 	sapi_send_headers();
334 	PHPWRITE("<html>\n <head>\n  <title>File Not Found</title>\n </head>\n <body>\n  <h1>404 - File ", sizeof("<html>\n <head>\n  <title>File Not Found</title>\n </head>\n <body>\n  <h1>404 - File ") - 1);
335 	PHPWRITE("Not Found</h1>\n </body>\n</html>",  sizeof("Not Found</h1>\n </body>\n</html>") - 1);
336 }
337 /* }}} */
338 
339 /* post-process REQUEST_URI and retrieve the actual request URI.  This is for
340    cases like http://localhost/blah.phar/path/to/file.php/extra/stuff
341    which calls "blah.phar" file "path/to/file.php" with PATH_INFO "/extra/stuff" */
phar_postprocess_ru_web(char * fname,int fname_len,char ** entry,int * entry_len,char ** ru,int * ru_len)342 static void phar_postprocess_ru_web(char *fname, int fname_len, char **entry, int *entry_len, char **ru, int *ru_len) /* {{{ */
343 {
344 	char *e = *entry + 1, *u = NULL, *u1 = NULL, *saveu = NULL;
345 	int e_len = *entry_len - 1, u_len = 0;
346 	phar_archive_data *pphar;
347 
348 	/* we already know we can retrieve the phar if we reach here */
349 	pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), fname, fname_len);
350 
351 	if (!pphar && PHAR_G(manifest_cached)) {
352 		pphar = zend_hash_str_find_ptr(&cached_phars, fname, fname_len);
353 	}
354 
355 	do {
356 		if (zend_hash_str_exists(&(pphar->manifest), e, e_len)) {
357 			if (u) {
358 				u[0] = '/';
359 				*ru = estrndup(u, u_len+1);
360 				++u_len;
361 				u[0] = '\0';
362 			} else {
363 				*ru = NULL;
364 			}
365 			*ru_len = u_len;
366 			*entry_len = e_len + 1;
367 			return;
368 		}
369 
370 		if (u) {
371 			u1 = strrchr(e, '/');
372 			u[0] = '/';
373 			saveu = u;
374 			e_len += u_len + 1;
375 			u = u1;
376 			if (!u) {
377 				return;
378 			}
379 		} else {
380 			u = strrchr(e, '/');
381 			if (!u) {
382 				if (saveu) {
383 					saveu[0] = '/';
384 				}
385 				return;
386 			}
387 		}
388 
389 		u[0] = '\0';
390 		u_len = (int)strlen(u + 1);
391 		e_len -= u_len + 1;
392 
393 		if (e_len < 0) {
394 			if (saveu) {
395 				saveu[0] = '/';
396 			}
397 			return;
398 		}
399 	} while (1);
400 }
401 /* }}} */
402 
403 /* {{{ proto void Phar::running([bool retphar = true])
404  * return the name of the currently running phar archive.  If the optional parameter
405  * is set to true, return the phar:// URL to the currently running phar
406  */
PHP_METHOD(Phar,running)407 PHP_METHOD(Phar, running)
408 {
409 	char *fname, *arch, *entry;
410 	int fname_len, arch_len, entry_len;
411 	zend_bool retphar = 1;
412 
413 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &retphar) == FAILURE) {
414 		return;
415 	}
416 
417 	fname = (char*)zend_get_executed_filename();
418 	fname_len = (int)strlen(fname);
419 
420 	if (fname_len > 7 && !memcmp(fname, "phar://", 7) && SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
421 		efree(entry);
422 		if (retphar) {
423 			RETVAL_STRINGL(fname, arch_len + 7);
424 			efree(arch);
425 			return;
426 		} else {
427 			// TODO: avoid reallocation ???
428 			RETVAL_STRINGL(arch, arch_len);
429 			efree(arch);
430 			return;
431 		}
432 	}
433 
434 	RETURN_EMPTY_STRING();
435 }
436 /* }}} */
437 
438 /* {{{ proto void Phar::mount(string pharpath, string externalfile)
439  * mount an external file or path to a location within the phar.  This maps
440  * an external file or directory to a location within the phar archive, allowing
441  * reference to an external location as if it were within the phar archive.  This
442  * is useful for writable temp files like databases
443  */
PHP_METHOD(Phar,mount)444 PHP_METHOD(Phar, mount)
445 {
446 	char *fname, *arch = NULL, *entry = NULL, *path, *actual;
447 	int fname_len, arch_len, entry_len;
448 	size_t path_len, actual_len;
449 	phar_archive_data *pphar;
450 
451 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "pp", &path, &path_len, &actual, &actual_len) == FAILURE) {
452 		return;
453 	}
454 
455 	if (ZEND_SIZE_T_INT_OVFL(path_len) || ZEND_SIZE_T_INT_OVFL(actual_len)) {
456 		RETURN_FALSE;
457 	}
458 
459 	fname = (char*)zend_get_executed_filename();
460 	fname_len = (int)strlen(fname);
461 
462 #ifdef PHP_WIN32
463 	phar_unixify_path_separators(fname, fname_len);
464 #endif
465 
466 	if (fname_len > 7 && !memcmp(fname, "phar://", 7) && SUCCESS == phar_split_fname(fname, fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
467 		efree(entry);
468 		entry = NULL;
469 
470 		if (path_len > 7 && !memcmp(path, "phar://", 7)) {
471 			zend_throw_exception_ex(phar_ce_PharException, 0, "Can only mount internal paths within a phar archive, use a relative path instead of \"%s\"", path);
472 			efree(arch);
473 			return;
474 		}
475 carry_on2:
476 		if (NULL == (pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), arch, arch_len))) {
477 			if (PHAR_G(manifest_cached) && NULL != (pphar = zend_hash_str_find_ptr(&cached_phars, arch, arch_len))) {
478 				if (SUCCESS == phar_copy_on_write(&pphar)) {
479 					goto carry_on;
480 				}
481 			}
482 
483 			zend_throw_exception_ex(phar_ce_PharException, 0, "%s is not a phar archive, cannot mount", arch);
484 
485 			if (arch) {
486 				efree(arch);
487 			}
488 			return;
489 		}
490 carry_on:
491 		if (SUCCESS != phar_mount_entry(pphar, actual, (int)actual_len, path, (int)path_len)) {
492 			zend_throw_exception_ex(phar_ce_PharException, 0, "Mounting of %s to %s within phar %s failed", path, actual, arch);
493 			if (path && path == entry) {
494 				efree(entry);
495 			}
496 
497 			if (arch) {
498 				efree(arch);
499 			}
500 
501 			return;
502 		}
503 
504 		if (entry && path && path == entry) {
505 			efree(entry);
506 		}
507 
508 		if (arch) {
509 			efree(arch);
510 		}
511 
512 		return;
513 	} else if (PHAR_G(phar_fname_map.u.flags) && NULL != (pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), fname, fname_len))) {
514 		goto carry_on;
515 	} else if (PHAR_G(manifest_cached) && NULL != (pphar = zend_hash_str_find_ptr(&cached_phars, fname, fname_len))) {
516 		if (SUCCESS == phar_copy_on_write(&pphar)) {
517 			goto carry_on;
518 		}
519 
520 		goto carry_on;
521 	} else if (SUCCESS == phar_split_fname(path, (int)path_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
522 		path = entry;
523 		path_len = entry_len;
524 		goto carry_on2;
525 	}
526 
527 	zend_throw_exception_ex(phar_ce_PharException, 0, "Mounting of %s to %s failed", path, actual);
528 }
529 /* }}} */
530 
531 /* {{{ proto void Phar::webPhar([string alias, [string index, [string f404, [array mimetypes, [callback rewrites]]]]])
532  * mapPhar for web-based phars. Reads the currently executed file (a phar)
533  * and registers its manifest. When executed in the CLI or CGI command-line sapi,
534  * this works exactly like mapPhar().  When executed by a web-based sapi, this
535  * reads $_SERVER['REQUEST_URI'] (the actual original value) and parses out the
536  * intended internal file.
537  */
PHP_METHOD(Phar,webPhar)538 PHP_METHOD(Phar, webPhar)
539 {
540 	zval *mimeoverride = NULL, *rewrite = NULL;
541 	char *alias = NULL, *error, *index_php = NULL, *f404 = NULL, *ru = NULL;
542 	size_t alias_len = 0, f404_len = 0, free_pathinfo = 0;
543 	int  ru_len = 0;
544 	char *fname, *path_info, *mime_type = NULL, *entry, *pt;
545 	const char *basename;
546 	size_t fname_len, index_php_len = 0;
547 	int entry_len, code, not_cgi;
548 	phar_archive_data *phar = NULL;
549 	phar_entry_info *info = NULL;
550 	size_t sapi_mod_name_len = strlen(sapi_module.name);
551 
552 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!saz", &alias, &alias_len, &index_php, &index_php_len, &f404, &f404_len, &mimeoverride, &rewrite) == FAILURE) {
553 		return;
554 	}
555 
556 	phar_request_initialize();
557 	fname = (char*)zend_get_executed_filename();
558 	fname_len = strlen(fname);
559 
560 	if (ZEND_SIZE_T_INT_OVFL(alias_len)
561 			|| ZEND_SIZE_T_INT_OVFL(f404_len) || ZEND_SIZE_T_INT_OVFL(index_php_len)) {
562 		RETURN_FALSE;
563 	}
564 
565 	if (phar_open_executed_filename(alias, (int)alias_len, &error) != SUCCESS) {
566 		if (error) {
567 			zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
568 			efree(error);
569 		}
570 		return;
571 	}
572 
573 	/* retrieve requested file within phar */
574 	if (!(SG(request_info).request_method
575           && SG(request_info).request_uri
576           && (!strcmp(SG(request_info).request_method, "GET")
577            || !strcmp(SG(request_info).request_method, "POST")
578            || !strcmp(SG(request_info).request_method, "DELETE")
579            || !strcmp(SG(request_info).request_method, "HEAD")
580            || !strcmp(SG(request_info).request_method, "OPTIONS")
581            || !strcmp(SG(request_info).request_method, "PATCH")
582            || !strcmp(SG(request_info).request_method, "PUT")
583           )
584          )
585       ) {
586 		return;
587 	}
588 
589 #ifdef PHP_WIN32
590 	fname = estrndup(fname, fname_len);
591 	phar_unixify_path_separators(fname, fname_len);
592 #endif
593 	basename = zend_memrchr(fname, '/', fname_len);
594 
595 	if (!basename) {
596 		basename = fname;
597 	} else {
598 		++basename;
599 	}
600 
601 	if ((sapi_mod_name_len == sizeof("cgi-fcgi") - 1 && !strncmp(sapi_module.name, "cgi-fcgi", sizeof("cgi-fcgi") - 1))
602 		|| (sapi_mod_name_len == sizeof("fpm-fcgi") - 1 && !strncmp(sapi_module.name, "fpm-fcgi", sizeof("fpm-fcgi") - 1))
603 		|| (sapi_mod_name_len == sizeof("cgi") - 1 && !strncmp(sapi_module.name, "cgi", sizeof("cgi") - 1))) {
604 
605 		if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) != IS_UNDEF) {
606 			HashTable *_server = Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]);
607 			zval *z_script_name, *z_path_info;
608 
609 			if (NULL == (z_script_name = zend_hash_str_find(_server, "SCRIPT_NAME", sizeof("SCRIPT_NAME")-1)) ||
610 				IS_STRING != Z_TYPE_P(z_script_name) ||
611 				!strstr(Z_STRVAL_P(z_script_name), basename)) {
612 				return;
613 			}
614 
615 			if (NULL != (z_path_info = zend_hash_str_find(_server, "PATH_INFO", sizeof("PATH_INFO")-1)) &&
616 				IS_STRING == Z_TYPE_P(z_path_info)) {
617 				entry_len = (int)Z_STRLEN_P(z_path_info);
618 				entry = estrndup(Z_STRVAL_P(z_path_info), entry_len);
619 				path_info = emalloc(Z_STRLEN_P(z_script_name) + entry_len + 1);
620 				memcpy(path_info, Z_STRVAL_P(z_script_name), Z_STRLEN_P(z_script_name));
621 				memcpy(path_info + Z_STRLEN_P(z_script_name), entry, entry_len + 1);
622 				free_pathinfo = 1;
623 			} else {
624 				entry_len = 0;
625 				entry = estrndup("", 0);
626 				path_info = Z_STRVAL_P(z_script_name);
627 			}
628 
629 			pt = estrndup(Z_STRVAL_P(z_script_name), Z_STRLEN_P(z_script_name));
630 
631 		} else {
632 			char *testit;
633 
634 			testit = sapi_getenv("SCRIPT_NAME", sizeof("SCRIPT_NAME")-1);
635 			if (!(pt = strstr(testit, basename))) {
636 				efree(testit);
637 				return;
638 			}
639 
640 			path_info = sapi_getenv("PATH_INFO", sizeof("PATH_INFO")-1);
641 
642 			if (path_info) {
643 				entry = path_info;
644 				entry_len = (int)strlen(entry);
645 				spprintf(&path_info, 0, "%s%s", testit, path_info);
646 				free_pathinfo = 1;
647 			} else {
648 				path_info = testit;
649 				free_pathinfo = 1;
650 				entry = estrndup("", 0);
651 				entry_len = 0;
652 			}
653 
654 			pt = estrndup(testit, (pt - testit) + (fname_len - (basename - fname)));
655 		}
656 		not_cgi = 0;
657 	} else {
658 		path_info = SG(request_info).request_uri;
659 
660 		if (!(pt = strstr(path_info, basename))) {
661 			/* this can happen with rewrite rules - and we have no idea what to do then, so return */
662 			return;
663 		}
664 
665 		entry_len = (int)strlen(path_info);
666 		entry_len -= (pt - path_info) + (fname_len - (basename - fname));
667 		entry = estrndup(pt + (fname_len - (basename - fname)), entry_len);
668 
669 		pt = estrndup(path_info, (pt - path_info) + (fname_len - (basename - fname)));
670 		not_cgi = 1;
671 	}
672 
673 	if (rewrite) {
674 		zend_fcall_info fci;
675 		zend_fcall_info_cache fcc;
676 		zval params, retval;
677 
678 		ZVAL_STRINGL(&params, entry, entry_len);
679 
680 		if (FAILURE == zend_fcall_info_init(rewrite, 0, &fci, &fcc, NULL, NULL)) {
681 			zend_throw_exception_ex(phar_ce_PharException, 0, "phar error: invalid rewrite callback");
682 
683 			if (free_pathinfo) {
684 				efree(path_info);
685 			}
686 			efree(pt);
687 
688 			return;
689 		}
690 
691 		fci.param_count = 1;
692 		fci.params = &params;
693 		Z_ADDREF(params);
694 		fci.retval = &retval;
695 
696 		if (FAILURE == zend_call_function(&fci, &fcc)) {
697 			if (!EG(exception)) {
698 				zend_throw_exception_ex(phar_ce_PharException, 0, "phar error: failed to call rewrite callback");
699 			}
700 
701 			if (free_pathinfo) {
702 				efree(path_info);
703 			}
704 			efree(pt);
705 
706 			return;
707 		}
708 
709 		if (Z_TYPE_P(fci.retval) == IS_UNDEF || Z_TYPE(retval) == IS_UNDEF) {
710 			if (free_pathinfo) {
711 				efree(path_info);
712 			}
713 			zend_throw_exception_ex(phar_ce_PharException, 0, "phar error: rewrite callback must return a string or false");
714 			efree(pt);
715 			return;
716 		}
717 
718 		switch (Z_TYPE(retval)) {
719 			case IS_STRING:
720 				efree(entry);
721 				if (ZEND_SIZE_T_INT_OVFL(Z_STRLEN_P(fci.retval))) {
722 					zend_throw_exception_ex(phar_ce_PharException, 0, "phar error: rewrite callback returned oversized value");
723 					return;
724 				}
725 				entry = estrndup(Z_STRVAL_P(fci.retval), Z_STRLEN_P(fci.retval));
726 				entry_len = (int)Z_STRLEN_P(fci.retval);
727 				break;
728 			case IS_TRUE:
729 			case IS_FALSE:
730 				phar_do_403(entry, entry_len);
731 
732 				if (free_pathinfo) {
733 					efree(path_info);
734 				}
735 				efree(pt);
736 
737 				zend_bailout();
738 				return;
739 			default:
740 				if (free_pathinfo) {
741 					efree(path_info);
742 				}
743 				efree(pt);
744 
745 				zend_throw_exception_ex(phar_ce_PharException, 0, "phar error: rewrite callback must return a string or false");
746 				return;
747 		}
748 	}
749 
750 	if (entry_len) {
751 		phar_postprocess_ru_web(fname, (int)fname_len, &entry, &entry_len, &ru, &ru_len);
752 	}
753 
754 	if (!entry_len || (entry_len == 1 && entry[0] == '/')) {
755 		efree(entry);
756 		/* direct request */
757 		if (index_php_len) {
758 			entry = index_php;
759 			entry_len = (int)index_php_len;
760 			if (entry[0] != '/') {
761 				spprintf(&entry, 0, "/%s", index_php);
762 				++entry_len;
763 			}
764 		} else {
765 			/* assume "index.php" is starting point */
766 			entry = estrndup("/index.php", sizeof("/index.php"));
767 			entry_len = sizeof("/index.php")-1;
768 		}
769 
770 		if (FAILURE == phar_get_archive(&phar, fname, (int)fname_len, NULL, 0, NULL) ||
771 			(info = phar_get_entry_info(phar, entry, entry_len, NULL, 0)) == NULL) {
772 			phar_do_404(phar, fname, (int)fname_len, f404, (int)f404_len, entry, entry_len);
773 
774 			if (free_pathinfo) {
775 				efree(path_info);
776 			}
777 
778 			zend_bailout();
779 		} else {
780 			char *tmp = NULL, sa = '\0';
781 			sapi_header_line ctr = {0};
782 			ctr.response_code = 301;
783 			ctr.line_len = sizeof("HTTP/1.1 301 Moved Permanently")-1;
784 			ctr.line = "HTTP/1.1 301 Moved Permanently";
785 			sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
786 
787 			if (not_cgi) {
788 				tmp = strstr(path_info, basename) + fname_len;
789 				sa = *tmp;
790 				*tmp = '\0';
791 			}
792 
793 			ctr.response_code = 0;
794 
795 			if (path_info[strlen(path_info)-1] == '/') {
796 				ctr.line_len = spprintf(&(ctr.line), 4096, "Location: %s%s", path_info, entry + 1);
797 			} else {
798 				ctr.line_len = spprintf(&(ctr.line), 4096, "Location: %s%s", path_info, entry);
799 			}
800 
801 			if (not_cgi) {
802 				*tmp = sa;
803 			}
804 
805 			if (free_pathinfo) {
806 				efree(path_info);
807 			}
808 
809 			sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
810 			sapi_send_headers();
811 			efree(ctr.line);
812 			zend_bailout();
813 		}
814 	}
815 
816 	if (FAILURE == phar_get_archive(&phar, fname, (int)fname_len, NULL, 0, NULL) ||
817 		(info = phar_get_entry_info(phar, entry, entry_len, NULL, 0)) == NULL) {
818 		phar_do_404(phar, fname, (int)fname_len, f404, (int)f404_len, entry, entry_len);
819 #ifdef PHP_WIN32
820 		efree(fname);
821 #endif
822 		zend_bailout();
823 	}
824 
825 	if (mimeoverride && zend_hash_num_elements(Z_ARRVAL_P(mimeoverride))) {
826 		const char *ext = zend_memrchr(entry, '.', entry_len);
827 		zval *val;
828 
829 		if (ext) {
830 			++ext;
831 
832 			if (NULL != (val = zend_hash_str_find(Z_ARRVAL_P(mimeoverride), ext, strlen(ext)))) {
833 				switch (Z_TYPE_P(val)) {
834 					case IS_LONG:
835 						if (Z_LVAL_P(val) == PHAR_MIME_PHP || Z_LVAL_P(val) == PHAR_MIME_PHPS) {
836 							mime_type = "";
837 							code = (int)Z_LVAL_P(val);
838 						} else {
839 							zend_throw_exception_ex(phar_ce_PharException, 0, "Unknown mime type specifier used, only Phar::PHP, Phar::PHPS and a mime type string are allowed");
840 							if (free_pathinfo) {
841 								efree(path_info);
842 							}
843 							efree(pt);
844 							efree(entry);
845 #ifdef PHP_WIN32
846 							efree(fname);
847 #endif
848 							RETURN_FALSE;
849 						}
850 						break;
851 					case IS_STRING:
852 						mime_type = Z_STRVAL_P(val);
853 						code = PHAR_MIME_OTHER;
854 						break;
855 					default:
856 						zend_throw_exception_ex(phar_ce_PharException, 0, "Unknown mime type specifier used (not a string or int), only Phar::PHP, Phar::PHPS and a mime type string are allowed");
857 						if (free_pathinfo) {
858 							efree(path_info);
859 						}
860 						efree(pt);
861 						efree(entry);
862 #ifdef PHP_WIN32
863 						efree(fname);
864 #endif
865 						RETURN_FALSE;
866 				}
867 			}
868 		}
869 	}
870 
871 	if (!mime_type) {
872 		code = phar_file_type(&PHAR_G(mime_types), entry, &mime_type);
873 	}
874 	phar_file_action(phar, info, mime_type, code, entry, entry_len, fname, pt, ru, ru_len);
875 }
876 /* }}} */
877 
878 /* {{{ proto void Phar::mungServer(array munglist)
879  * Defines a list of up to 4 $_SERVER variables that should be modified for execution
880  * to mask the presence of the phar archive.  This should be used in conjunction with
881  * Phar::webPhar(), and has no effect otherwise
882  * SCRIPT_NAME, PHP_SELF, REQUEST_URI and SCRIPT_FILENAME
883  */
PHP_METHOD(Phar,mungServer)884 PHP_METHOD(Phar, mungServer)
885 {
886 	zval *mungvalues, *data;
887 
888 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "a", &mungvalues) == FAILURE) {
889 		return;
890 	}
891 
892 	if (!zend_hash_num_elements(Z_ARRVAL_P(mungvalues))) {
893 		zend_throw_exception_ex(phar_ce_PharException, 0, "No values passed to Phar::mungServer(), expecting an array of any of these strings: PHP_SELF, REQUEST_URI, SCRIPT_FILENAME, SCRIPT_NAME");
894 		return;
895 	}
896 
897 	if (zend_hash_num_elements(Z_ARRVAL_P(mungvalues)) > 4) {
898 		zend_throw_exception_ex(phar_ce_PharException, 0, "Too many values passed to Phar::mungServer(), expecting an array of any of these strings: PHP_SELF, REQUEST_URI, SCRIPT_FILENAME, SCRIPT_NAME");
899 		return;
900 	}
901 
902 	phar_request_initialize();
903 
904 	ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(mungvalues), data) {
905 
906 		if (Z_TYPE_P(data) != IS_STRING) {
907 			zend_throw_exception_ex(phar_ce_PharException, 0, "Non-string value passed to Phar::mungServer(), expecting an array of any of these strings: PHP_SELF, REQUEST_URI, SCRIPT_FILENAME, SCRIPT_NAME");
908 			return;
909 		}
910 
911 		if (Z_STRLEN_P(data) == sizeof("PHP_SELF")-1 && !strncmp(Z_STRVAL_P(data), "PHP_SELF", sizeof("PHP_SELF")-1)) {
912 			PHAR_G(phar_SERVER_mung_list) |= PHAR_MUNG_PHP_SELF;
913 		}
914 
915 		if (Z_STRLEN_P(data) == sizeof("REQUEST_URI")-1) {
916 			if (!strncmp(Z_STRVAL_P(data), "REQUEST_URI", sizeof("REQUEST_URI")-1)) {
917 				PHAR_G(phar_SERVER_mung_list) |= PHAR_MUNG_REQUEST_URI;
918 			}
919 			if (!strncmp(Z_STRVAL_P(data), "SCRIPT_NAME", sizeof("SCRIPT_NAME")-1)) {
920 				PHAR_G(phar_SERVER_mung_list) |= PHAR_MUNG_SCRIPT_NAME;
921 			}
922 		}
923 
924 		if (Z_STRLEN_P(data) == sizeof("SCRIPT_FILENAME")-1 && !strncmp(Z_STRVAL_P(data), "SCRIPT_FILENAME", sizeof("SCRIPT_FILENAME")-1)) {
925 			PHAR_G(phar_SERVER_mung_list) |= PHAR_MUNG_SCRIPT_FILENAME;
926 		}
927 	} ZEND_HASH_FOREACH_END();
928 }
929 /* }}} */
930 
931 /* {{{ proto void Phar::interceptFileFuncs()
932  * instructs phar to intercept fopen, file_get_contents, opendir, and all of the stat-related functions
933  * and return stat on files within the phar for relative paths
934  *
935  * Once called, this cannot be reversed, and continue until the end of the request.
936  *
937  * This allows legacy scripts to be pharred unmodified
938  */
PHP_METHOD(Phar,interceptFileFuncs)939 PHP_METHOD(Phar, interceptFileFuncs)
940 {
941 	if (zend_parse_parameters_none() == FAILURE) {
942 		return;
943 	}
944 	phar_intercept_functions();
945 }
946 /* }}} */
947 
948 /* {{{ proto array Phar::createDefaultStub([string indexfile[, string webindexfile]])
949  * Return a stub that can be used to run a phar-based archive without the phar extension
950  * indexfile is the CLI startup filename, which defaults to "index.php", webindexfile
951  * is the web startup filename, and also defaults to "index.php"
952  */
PHP_METHOD(Phar,createDefaultStub)953 PHP_METHOD(Phar, createDefaultStub)
954 {
955 	char *index = NULL, *webindex = NULL, *error;
956 	zend_string *stub;
957 	size_t index_len = 0, webindex_len = 0;
958 
959 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|pp", &index, &index_len, &webindex, &webindex_len) == FAILURE) {
960 		return;
961 	}
962 
963 	stub = phar_create_default_stub(index, webindex, &error);
964 
965 	if (error) {
966 		zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
967 		efree(error);
968 		return;
969 	}
970 	RETURN_NEW_STR(stub);
971 }
972 /* }}} */
973 
974 /* {{{ proto mixed Phar::mapPhar([string alias, [int dataoffset]])
975  * Reads the currently executed file (a phar) and registers its manifest */
PHP_METHOD(Phar,mapPhar)976 PHP_METHOD(Phar, mapPhar)
977 {
978 	char *alias = NULL, *error;
979 	size_t alias_len = 0;
980 	zend_long dataoffset = 0;
981 
982 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!l", &alias, &alias_len, &dataoffset) == FAILURE) {
983 		return;
984 	}
985 
986 	if (ZEND_SIZE_T_INT_OVFL(alias_len)) {
987 		RETURN_FALSE;
988 	}
989 	phar_request_initialize();
990 
991 	RETVAL_BOOL(phar_open_executed_filename(alias, (int)alias_len, &error) == SUCCESS);
992 
993 	if (error) {
994 		zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
995 		efree(error);
996 	}
997 } /* }}} */
998 
999 /* {{{ proto mixed Phar::loadPhar(string filename [, string alias])
1000  * Loads any phar archive with an alias */
PHP_METHOD(Phar,loadPhar)1001 PHP_METHOD(Phar, loadPhar)
1002 {
1003 	char *fname, *alias = NULL, *error;
1004 	size_t fname_len, alias_len = 0;
1005 
1006 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|s!", &fname, &fname_len, &alias, &alias_len) == FAILURE) {
1007 		return;
1008 	}
1009 
1010 	if (ZEND_SIZE_T_INT_OVFL(alias_len) || ZEND_SIZE_T_INT_OVFL(fname_len)) {
1011 		RETURN_FALSE;
1012 	}
1013 	phar_request_initialize();
1014 
1015 	RETVAL_BOOL(phar_open_from_filename(fname, (int)fname_len, alias, (int)alias_len, REPORT_ERRORS, NULL, &error) == SUCCESS);
1016 
1017 	if (error) {
1018 		zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
1019 		efree(error);
1020 	}
1021 } /* }}} */
1022 
1023 /* {{{ proto string Phar::apiVersion()
1024  * Returns the api version */
PHP_METHOD(Phar,apiVersion)1025 PHP_METHOD(Phar, apiVersion)
1026 {
1027 	if (zend_parse_parameters_none() == FAILURE) {
1028 		return;
1029 	}
1030 	RETURN_STRINGL(PHP_PHAR_API_VERSION, sizeof(PHP_PHAR_API_VERSION)-1);
1031 }
1032 /* }}}*/
1033 
1034 /* {{{ proto bool Phar::canCompress([int method])
1035  * Returns whether phar extension supports compression using zlib/bzip2 */
PHP_METHOD(Phar,canCompress)1036 PHP_METHOD(Phar, canCompress)
1037 {
1038 	zend_long method = 0;
1039 
1040 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &method) == FAILURE) {
1041 		return;
1042 	}
1043 
1044 	phar_request_initialize();
1045 	switch (method) {
1046 	case PHAR_ENT_COMPRESSED_GZ:
1047 		if (PHAR_G(has_zlib)) {
1048 			RETURN_TRUE;
1049 		} else {
1050 			RETURN_FALSE;
1051 		}
1052 	case PHAR_ENT_COMPRESSED_BZ2:
1053 		if (PHAR_G(has_bz2)) {
1054 			RETURN_TRUE;
1055 		} else {
1056 			RETURN_FALSE;
1057 		}
1058 	default:
1059 		if (PHAR_G(has_zlib) || PHAR_G(has_bz2)) {
1060 			RETURN_TRUE;
1061 		} else {
1062 			RETURN_FALSE;
1063 		}
1064 	}
1065 }
1066 /* }}} */
1067 
1068 /* {{{ proto bool Phar::canWrite()
1069  * Returns whether phar extension supports writing and creating phars */
PHP_METHOD(Phar,canWrite)1070 PHP_METHOD(Phar, canWrite)
1071 {
1072 	if (zend_parse_parameters_none() == FAILURE) {
1073 		return;
1074 	}
1075 	RETURN_BOOL(!PHAR_G(readonly));
1076 }
1077 /* }}} */
1078 
1079 /* {{{ proto bool Phar::isValidPharFilename(string filename[, bool executable = true])
1080  * Returns whether the given filename is a valid phar filename */
PHP_METHOD(Phar,isValidPharFilename)1081 PHP_METHOD(Phar, isValidPharFilename)
1082 {
1083 	char *fname;
1084 	const char *ext_str;
1085 	size_t fname_len;
1086 	int ext_len, is_executable;
1087 	zend_bool executable = 1;
1088 
1089 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|b", &fname, &fname_len, &executable) == FAILURE) {
1090 		return;
1091 	}
1092 
1093 	if (ZEND_SIZE_T_INT_OVFL(fname_len)) {
1094 		RETURN_FALSE;
1095 	}
1096 
1097 	is_executable = executable;
1098 	RETVAL_BOOL(phar_detect_phar_fname_ext(fname, (int)fname_len, &ext_str, &ext_len, is_executable, 2, 1) == SUCCESS);
1099 }
1100 /* }}} */
1101 
1102 /**
1103  * from spl_directory
1104  */
phar_spl_foreign_dtor(spl_filesystem_object * object)1105 static void phar_spl_foreign_dtor(spl_filesystem_object *object) /* {{{ */
1106 {
1107 	phar_archive_data *phar = (phar_archive_data *) object->oth;
1108 
1109 	if (!phar->is_persistent) {
1110 		phar_archive_delref(phar);
1111 	}
1112 
1113 	object->oth = NULL;
1114 }
1115 /* }}} */
1116 
1117 /**
1118  * from spl_directory
1119  */
phar_spl_foreign_clone(spl_filesystem_object * src,spl_filesystem_object * dst)1120 static void phar_spl_foreign_clone(spl_filesystem_object *src, spl_filesystem_object *dst) /* {{{ */
1121 {
1122 	phar_archive_data *phar_data = (phar_archive_data *) dst->oth;
1123 
1124 	if (!phar_data->is_persistent) {
1125 		++(phar_data->refcount);
1126 	}
1127 }
1128 /* }}} */
1129 
1130 static spl_other_handler phar_spl_foreign_handler = {
1131 	phar_spl_foreign_dtor,
1132 	phar_spl_foreign_clone
1133 };
1134 
1135 /* {{{ proto void Phar::__construct(string fname [, int flags [, string alias]])
1136  * Construct a Phar archive object
1137  *
1138  * proto void PharData::__construct(string fname [[, int flags [, string alias]], int file format = Phar::TAR])
1139  * Construct a PharData archive object
1140  *
1141  * This function is used as the constructor for both the Phar and PharData
1142  * classes, hence the two prototypes above.
1143  */
PHP_METHOD(Phar,__construct)1144 PHP_METHOD(Phar, __construct)
1145 {
1146 	char *fname, *alias = NULL, *error, *arch = NULL, *entry = NULL, *save_fname;
1147 	size_t fname_len, alias_len = 0;
1148 	int arch_len, entry_len, is_data;
1149 	zend_long flags = SPL_FILE_DIR_SKIPDOTS|SPL_FILE_DIR_UNIXPATHS;
1150 	zend_long format = 0;
1151 	phar_archive_object *phar_obj;
1152 	phar_archive_data   *phar_data;
1153 	zval *zobj = getThis(), arg1, arg2;
1154 
1155 	phar_obj = (phar_archive_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset);
1156 
1157 	is_data = instanceof_function(Z_OBJCE_P(zobj), phar_ce_data);
1158 
1159 	if (is_data) {
1160 		if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p|ls!l", &fname, &fname_len, &flags, &alias, &alias_len, &format) == FAILURE) {
1161 			return;
1162 		}
1163 	} else {
1164 		if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p|ls!", &fname, &fname_len, &flags, &alias, &alias_len) == FAILURE) {
1165 			return;
1166 		}
1167 	}
1168 
1169 	if (ZEND_SIZE_T_INT_OVFL(alias_len) || ZEND_SIZE_T_INT_OVFL(fname_len)) {
1170 		RETURN_FALSE;
1171 	}
1172 	if (phar_obj->archive) {
1173 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot call constructor twice");
1174 		return;
1175 	}
1176 
1177 	save_fname = fname;
1178 	if (SUCCESS == phar_split_fname(fname, (int)fname_len, &arch, &arch_len, &entry, &entry_len, !is_data, 2)) {
1179 		/* use arch (the basename for the archive) for fname instead of fname */
1180 		/* this allows support for RecursiveDirectoryIterator of subdirectories */
1181 #ifdef PHP_WIN32
1182 		phar_unixify_path_separators(arch, arch_len);
1183 #endif
1184 		fname = arch;
1185 		fname_len = arch_len;
1186 #ifdef PHP_WIN32
1187 	} else {
1188 		arch = estrndup(fname, fname_len);
1189 		arch_len = fname_len;
1190 		fname = arch;
1191 		phar_unixify_path_separators(arch, arch_len);
1192 #endif
1193 	}
1194 
1195 	if (phar_open_or_create_filename(fname, (int)fname_len, alias, (int)alias_len, is_data, REPORT_ERRORS, &phar_data, &error) == FAILURE) {
1196 
1197 		if (fname == arch && fname != save_fname) {
1198 			efree(arch);
1199 			fname = save_fname;
1200 		}
1201 
1202 		if (entry) {
1203 			efree(entry);
1204 		}
1205 
1206 		if (error) {
1207 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
1208 				"%s", error);
1209 			efree(error);
1210 		} else {
1211 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
1212 				"Phar creation or opening failed");
1213 		}
1214 
1215 		return;
1216 	}
1217 
1218 	if (is_data && phar_data->is_tar && phar_data->is_brandnew && format == PHAR_FORMAT_ZIP) {
1219 		phar_data->is_zip = 1;
1220 		phar_data->is_tar = 0;
1221 	}
1222 
1223 	if (fname == arch) {
1224 		efree(arch);
1225 		fname = save_fname;
1226 	}
1227 
1228 	if ((is_data && !phar_data->is_data) || (!is_data && phar_data->is_data)) {
1229 		if (is_data) {
1230 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
1231 				"PharData class can only be used for non-executable tar and zip archives");
1232 		} else {
1233 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
1234 				"Phar class can only be used for executable tar and zip archives");
1235 		}
1236 		efree(entry);
1237 		return;
1238 	}
1239 
1240 	is_data = phar_data->is_data;
1241 
1242 	if (!phar_data->is_persistent) {
1243 		++(phar_data->refcount);
1244 	}
1245 
1246 	phar_obj->archive = phar_data;
1247 	phar_obj->spl.oth_handler = &phar_spl_foreign_handler;
1248 
1249 	if (entry) {
1250 		fname_len = spprintf(&fname, 0, "phar://%s%s", phar_data->fname, entry);
1251 		efree(entry);
1252 	} else {
1253 		fname_len = spprintf(&fname, 0, "phar://%s", phar_data->fname);
1254 	}
1255 
1256 	ZVAL_STRINGL(&arg1, fname, fname_len);
1257 	ZVAL_LONG(&arg2, flags);
1258 
1259 	zend_call_method_with_2_params(zobj, Z_OBJCE_P(zobj),
1260 		&spl_ce_RecursiveDirectoryIterator->constructor, "__construct", NULL, &arg1, &arg2);
1261 
1262 	zval_ptr_dtor(&arg1);
1263 
1264 	if (!phar_data->is_persistent) {
1265 		phar_obj->archive->is_data = is_data;
1266 	} else if (!EG(exception)) {
1267 		/* register this guy so we can modify if necessary */
1268 		zend_hash_str_add_ptr(&PHAR_G(phar_persist_map), (const char *) phar_obj->archive, sizeof(phar_obj->archive), phar_obj);
1269 	}
1270 
1271 	phar_obj->spl.info_class = phar_ce_entry;
1272 	efree(fname);
1273 }
1274 /* }}} */
1275 
1276 /* {{{ proto array Phar::getSupportedSignatures()
1277  * Return array of supported signature types
1278  */
PHP_METHOD(Phar,getSupportedSignatures)1279 PHP_METHOD(Phar, getSupportedSignatures)
1280 {
1281 	if (zend_parse_parameters_none() == FAILURE) {
1282 		return;
1283 	}
1284 
1285 	array_init(return_value);
1286 
1287 	add_next_index_stringl(return_value, "MD5", 3);
1288 	add_next_index_stringl(return_value, "SHA-1", 5);
1289 #ifdef PHAR_HASH_OK
1290 	add_next_index_stringl(return_value, "SHA-256", 7);
1291 	add_next_index_stringl(return_value, "SHA-512", 7);
1292 #endif
1293 #if PHAR_HAVE_OPENSSL
1294 	add_next_index_stringl(return_value, "OpenSSL", 7);
1295 #else
1296 	if (zend_hash_str_exists(&module_registry, "openssl", sizeof("openssl")-1)) {
1297 		add_next_index_stringl(return_value, "OpenSSL", 7);
1298 	}
1299 #endif
1300 }
1301 /* }}} */
1302 
1303 /* {{{ proto array Phar::getSupportedCompression()
1304  * Return array of supported comparession algorithms
1305  */
PHP_METHOD(Phar,getSupportedCompression)1306 PHP_METHOD(Phar, getSupportedCompression)
1307 {
1308 	if (zend_parse_parameters_none() == FAILURE) {
1309 		return;
1310 	}
1311 
1312 	array_init(return_value);
1313 	phar_request_initialize();
1314 
1315 	if (PHAR_G(has_zlib)) {
1316 		add_next_index_stringl(return_value, "GZ", 2);
1317 	}
1318 
1319 	if (PHAR_G(has_bz2)) {
1320 		add_next_index_stringl(return_value, "BZIP2", 5);
1321 	}
1322 }
1323 /* }}} */
1324 
1325 /* {{{ proto array Phar::unlinkArchive(string archive)
1326  * Completely remove a phar archive from memory and disk
1327  */
PHP_METHOD(Phar,unlinkArchive)1328 PHP_METHOD(Phar, unlinkArchive)
1329 {
1330 	char *fname, *error, *zname, *arch, *entry;
1331 	size_t fname_len;
1332 	int zname_len, arch_len, entry_len;
1333 	phar_archive_data *phar;
1334 
1335 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
1336 		RETURN_FALSE;
1337 	}
1338 
1339 	if (ZEND_SIZE_T_INT_OVFL(fname_len)) {
1340 		RETURN_FALSE;
1341 	}
1342 	if (!fname_len) {
1343 		zend_throw_exception_ex(phar_ce_PharException, 0, "Unknown phar archive \"\"");
1344 		return;
1345 	}
1346 
1347 	if (FAILURE == phar_open_from_filename(fname, (int)fname_len, NULL, 0, REPORT_ERRORS, &phar, &error)) {
1348 		if (error) {
1349 			zend_throw_exception_ex(phar_ce_PharException, 0, "Unknown phar archive \"%s\": %s", fname, error);
1350 			efree(error);
1351 		} else {
1352 			zend_throw_exception_ex(phar_ce_PharException, 0, "Unknown phar archive \"%s\"", fname);
1353 		}
1354 		return;
1355 	}
1356 
1357 	zname = (char*)zend_get_executed_filename();
1358 	zname_len = (int)strlen(zname);
1359 
1360 	if (zname_len > 7 && !memcmp(zname, "phar://", 7) && SUCCESS == phar_split_fname(zname, zname_len, &arch, &arch_len, &entry, &entry_len, 2, 0)) {
1361 		if ((size_t)arch_len == fname_len && !memcmp(arch, fname, arch_len)) {
1362 			zend_throw_exception_ex(phar_ce_PharException, 0, "phar archive \"%s\" cannot be unlinked from within itself", fname);
1363 			efree(arch);
1364 			efree(entry);
1365 			return;
1366 		}
1367 		efree(arch);
1368 		efree(entry);
1369 	}
1370 
1371 	if (phar->is_persistent) {
1372 		zend_throw_exception_ex(phar_ce_PharException, 0, "phar archive \"%s\" is in phar.cache_list, cannot unlinkArchive()", fname);
1373 		return;
1374 	}
1375 
1376 	if (phar->refcount) {
1377 		zend_throw_exception_ex(phar_ce_PharException, 0, "phar archive \"%s\" has open file handles or objects.  fclose() all file handles, and unset() all objects prior to calling unlinkArchive()", fname);
1378 		return;
1379 	}
1380 
1381 	fname = estrndup(phar->fname, phar->fname_len);
1382 
1383 	/* invalidate phar cache */
1384 	PHAR_G(last_phar) = NULL;
1385 	PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
1386 
1387 	phar_archive_delref(phar);
1388 	unlink(fname);
1389 	efree(fname);
1390 	RETURN_TRUE;
1391 }
1392 /* }}} */
1393 
1394 #define PHAR_ARCHIVE_OBJECT() \
1395 	zval *zobj = getThis(); \
1396 	phar_archive_object *phar_obj = (phar_archive_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset); \
1397 	if (!phar_obj->archive) { \
1398 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
1399 			"Cannot call method on an uninitialized Phar object"); \
1400 		return; \
1401 	}
1402 
1403 /* {{{ proto void Phar::__destruct()
1404  * if persistent, remove from the cache
1405  */
PHP_METHOD(Phar,__destruct)1406 PHP_METHOD(Phar, __destruct)
1407 {
1408 	zval *zobj = getThis();
1409 	phar_archive_object *phar_obj = (phar_archive_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset);
1410 
1411 	if (phar_obj->archive && phar_obj->archive->is_persistent) {
1412 		zend_hash_str_del(&PHAR_G(phar_persist_map), (const char *) phar_obj->archive, sizeof(phar_obj->archive));
1413 	}
1414 }
1415 /* }}} */
1416 
1417 struct _phar_t {
1418 	phar_archive_object *p;
1419 	zend_class_entry *c;
1420 	char *b;
1421 	zval *ret;
1422 	php_stream *fp;
1423 	uint32_t l;
1424 	int count;
1425 };
1426 
phar_build(zend_object_iterator * iter,void * puser)1427 static int phar_build(zend_object_iterator *iter, void *puser) /* {{{ */
1428 {
1429 	zval *value;
1430 	zend_bool close_fp = 1;
1431 	struct _phar_t *p_obj = (struct _phar_t*) puser;
1432 	uint32_t str_key_len, base_len = p_obj->l;
1433 	phar_entry_data *data;
1434 	php_stream *fp;
1435 	size_t fname_len;
1436 	size_t contents_len;
1437 	char *fname, *error = NULL, *base = p_obj->b, *save = NULL, *temp = NULL;
1438 	zend_string *opened;
1439 	char *str_key;
1440 	zend_class_entry *ce = p_obj->c;
1441 	phar_archive_object *phar_obj = p_obj->p;
1442 	php_stream_statbuf ssb;
1443 
1444 	value = iter->funcs->get_current_data(iter);
1445 
1446 	if (EG(exception)) {
1447 		return ZEND_HASH_APPLY_STOP;
1448 	}
1449 
1450 	if (!value) {
1451 		/* failure in get_current_data */
1452 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned no value", ZSTR_VAL(ce->name));
1453 		return ZEND_HASH_APPLY_STOP;
1454 	}
1455 
1456 	switch (Z_TYPE_P(value)) {
1457 		case IS_STRING:
1458 			break;
1459 		case IS_RESOURCE:
1460 			php_stream_from_zval_no_verify(fp, value);
1461 
1462 			if (!fp) {
1463 				zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Iterator %s returned an invalid stream handle", ZSTR_VAL(ce->name));
1464 				return ZEND_HASH_APPLY_STOP;
1465 			}
1466 
1467 			if (iter->funcs->get_current_key) {
1468 				zval key;
1469 				iter->funcs->get_current_key(iter, &key);
1470 
1471 				if (EG(exception)) {
1472 					return ZEND_HASH_APPLY_STOP;
1473 				}
1474 
1475 				if (Z_TYPE(key) != IS_STRING) {
1476 					zval_dtor(&key);
1477 					zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned an invalid key (must return a string)", ZSTR_VAL(ce->name));
1478 					return ZEND_HASH_APPLY_STOP;
1479 				}
1480 
1481 				if (ZEND_SIZE_T_INT_OVFL(Z_STRLEN(key))) {
1482 					zval_dtor(&key);
1483 					zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned an invalid key (too long)", ZSTR_VAL(ce->name));
1484 					return ZEND_HASH_APPLY_STOP;
1485 				}
1486 
1487 				str_key_len = (int)Z_STRLEN(key);
1488 				str_key = estrndup(Z_STRVAL(key), str_key_len);
1489 
1490 				save = str_key;
1491 				zval_dtor(&key);
1492 			} else {
1493 				zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned an invalid key (must return a string)", ZSTR_VAL(ce->name));
1494 				return ZEND_HASH_APPLY_STOP;
1495 			}
1496 
1497 			close_fp = 0;
1498 			opened = zend_string_init("[stream]", sizeof("[stream]") - 1, 0);
1499 			goto after_open_fp;
1500 		case IS_OBJECT:
1501 			if (instanceof_function(Z_OBJCE_P(value), spl_ce_SplFileInfo)) {
1502 				char *test = NULL;
1503 				zval dummy;
1504 				spl_filesystem_object *intern = (spl_filesystem_object*)((char*)Z_OBJ_P(value) - Z_OBJ_P(value)->handlers->offset);
1505 
1506 				if (!base_len) {
1507 					zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Iterator %s returns an SplFileInfo object, so base directory must be specified", ZSTR_VAL(ce->name));
1508 					return ZEND_HASH_APPLY_STOP;
1509 				}
1510 
1511 				switch (intern->type) {
1512 					case SPL_FS_DIR:
1513 						test = spl_filesystem_object_get_path(intern, NULL);
1514 						fname_len = spprintf(&fname, 0, "%s%c%s", test, DEFAULT_SLASH, intern->u.dir.entry.d_name);
1515 						php_stat(fname, fname_len, FS_IS_DIR, &dummy);
1516 
1517 						if (Z_TYPE(dummy) == IS_TRUE) {
1518 							/* ignore directories */
1519 							efree(fname);
1520 							return ZEND_HASH_APPLY_KEEP;
1521 						}
1522 
1523 						test = expand_filepath(fname, NULL);
1524 						efree(fname);
1525 
1526 						if (test) {
1527 							fname = test;
1528 							fname_len = strlen(fname);
1529 						} else {
1530 							zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Could not resolve file path");
1531 							return ZEND_HASH_APPLY_STOP;
1532 						}
1533 
1534 						save = fname;
1535 						goto phar_spl_fileinfo;
1536 					case SPL_FS_INFO:
1537 					case SPL_FS_FILE:
1538 						fname = expand_filepath(intern->file_name, NULL);
1539 						if (!fname) {
1540 							zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Could not resolve file path");
1541 							return ZEND_HASH_APPLY_STOP;
1542 						}
1543 
1544 						fname_len = strlen(fname);
1545 						save = fname;
1546 						goto phar_spl_fileinfo;
1547 				}
1548 			}
1549 			/* fall-through */
1550 		default:
1551 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned an invalid value (must return a string)", ZSTR_VAL(ce->name));
1552 			return ZEND_HASH_APPLY_STOP;
1553 	}
1554 
1555 	fname = Z_STRVAL_P(value);
1556 	fname_len = Z_STRLEN_P(value);
1557 
1558 phar_spl_fileinfo:
1559 	if (base_len) {
1560 		temp = expand_filepath(base, NULL);
1561 		if (!temp) {
1562 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Could not resolve file path");
1563 			if (save) {
1564 				efree(save);
1565 			}
1566 			return ZEND_HASH_APPLY_STOP;
1567 		}
1568 
1569 		base = temp;
1570 		base_len = (int)strlen(base);
1571 
1572 		if (strstr(fname, base)) {
1573 			str_key_len = fname_len - base_len;
1574 
1575 			if (str_key_len <= 0) {
1576 				if (save) {
1577 					efree(save);
1578 					efree(temp);
1579 				}
1580 				return ZEND_HASH_APPLY_KEEP;
1581 			}
1582 
1583 			str_key = fname + base_len;
1584 
1585 			if (*str_key == '/' || *str_key == '\\') {
1586 				str_key++;
1587 				str_key_len--;
1588 			}
1589 
1590 		} else {
1591 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned a path \"%s\" that is not in the base directory \"%s\"", ZSTR_VAL(ce->name), fname, base);
1592 
1593 			if (save) {
1594 				efree(save);
1595 				efree(temp);
1596 			}
1597 
1598 			return ZEND_HASH_APPLY_STOP;
1599 		}
1600 	} else {
1601 		if (iter->funcs->get_current_key) {
1602 			zval key;
1603 			iter->funcs->get_current_key(iter, &key);
1604 
1605 			if (EG(exception)) {
1606 				return ZEND_HASH_APPLY_STOP;
1607 			}
1608 
1609 			if (Z_TYPE(key) != IS_STRING) {
1610 				zval_dtor(&key);
1611 				zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned an invalid key (must return a string)", ZSTR_VAL(ce->name));
1612 				return ZEND_HASH_APPLY_STOP;
1613 			}
1614 
1615 			if (ZEND_SIZE_T_INT_OVFL(Z_STRLEN(key))) {
1616 				zval_dtor(&key);
1617 				zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned an invalid key (too long)", ZSTR_VAL(ce->name));
1618 				return ZEND_HASH_APPLY_STOP;
1619 			}
1620 
1621 			str_key_len = (int)Z_STRLEN(key);
1622 			str_key = estrndup(Z_STRVAL(key), str_key_len);
1623 
1624 			save = str_key;
1625 			zval_dtor(&key);
1626 		} else {
1627 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned an invalid key (must return a string)", ZSTR_VAL(ce->name));
1628 			return ZEND_HASH_APPLY_STOP;
1629 		}
1630 	}
1631 
1632 	if (php_check_open_basedir(fname)) {
1633 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned a path \"%s\" that open_basedir prevents opening", ZSTR_VAL(ce->name), fname);
1634 
1635 		if (save) {
1636 			efree(save);
1637 		}
1638 
1639 		if (temp) {
1640 			efree(temp);
1641 		}
1642 
1643 		return ZEND_HASH_APPLY_STOP;
1644 	}
1645 
1646 	/* try to open source file, then create internal phar file and copy contents */
1647 	fp = php_stream_open_wrapper(fname, "rb", STREAM_MUST_SEEK|0, &opened);
1648 
1649 	if (!fp) {
1650 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Iterator %s returned a file that could not be opened \"%s\"", ZSTR_VAL(ce->name), fname);
1651 
1652 		if (save) {
1653 			efree(save);
1654 		}
1655 
1656 		if (temp) {
1657 			efree(temp);
1658 		}
1659 
1660 		return ZEND_HASH_APPLY_STOP;
1661 	}
1662 after_open_fp:
1663 	if (str_key_len >= sizeof(".phar")-1 && !memcmp(str_key, ".phar", sizeof(".phar")-1)) {
1664 		/* silently skip any files that would be added to the magic .phar directory */
1665 		if (save) {
1666 			efree(save);
1667 		}
1668 
1669 		if (temp) {
1670 			efree(temp);
1671 		}
1672 
1673 		if (opened) {
1674 			zend_string_release(opened);
1675 		}
1676 
1677 		if (close_fp) {
1678 			php_stream_close(fp);
1679 		}
1680 
1681 		return ZEND_HASH_APPLY_KEEP;
1682 	}
1683 
1684 	if (!(data = phar_get_or_create_entry_data(phar_obj->archive->fname, phar_obj->archive->fname_len, str_key, str_key_len, "w+b", 0, &error, 1))) {
1685 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s cannot be created: %s", str_key, error);
1686 		efree(error);
1687 
1688 		if (save) {
1689 			efree(save);
1690 		}
1691 
1692 		if (opened) {
1693 			zend_string_release(opened);
1694 		}
1695 
1696 		if (temp) {
1697 			efree(temp);
1698 		}
1699 
1700 		if (close_fp) {
1701 			php_stream_close(fp);
1702 		}
1703 
1704 		return ZEND_HASH_APPLY_STOP;
1705 
1706 	} else {
1707 		if (error) {
1708 			efree(error);
1709 		}
1710 		/* convert to PHAR_UFP */
1711 		if (data->internal_file->fp_type == PHAR_MOD) {
1712 			php_stream_close(data->internal_file->fp);
1713 		}
1714 
1715 		data->internal_file->fp = NULL;
1716 		data->internal_file->fp_type = PHAR_UFP;
1717 		data->internal_file->offset_abs = data->internal_file->offset = php_stream_tell(p_obj->fp);
1718 		data->fp = NULL;
1719 		php_stream_copy_to_stream_ex(fp, p_obj->fp, PHP_STREAM_COPY_ALL, &contents_len);
1720 		data->internal_file->uncompressed_filesize = data->internal_file->compressed_filesize =
1721 			php_stream_tell(p_obj->fp) - data->internal_file->offset;
1722 		if (php_stream_stat(fp, &ssb) != -1) {
1723 			data->internal_file->flags = ssb.sb.st_mode & PHAR_ENT_PERM_MASK ;
1724 		} else {
1725 #ifndef _WIN32
1726 			mode_t mask;
1727 			mask = umask(0);
1728 			umask(mask);
1729 			data->internal_file->flags &= ~mask;
1730 #endif
1731 		}
1732 	}
1733 
1734 	if (close_fp) {
1735 		php_stream_close(fp);
1736 	}
1737 
1738 	add_assoc_str(p_obj->ret, str_key, opened);
1739 
1740 	if (save) {
1741 		efree(save);
1742 	}
1743 
1744 	if (temp) {
1745 		efree(temp);
1746 	}
1747 
1748 	data->internal_file->compressed_filesize = data->internal_file->uncompressed_filesize = contents_len;
1749 	phar_entry_delref(data);
1750 
1751 	return ZEND_HASH_APPLY_KEEP;
1752 }
1753 /* }}} */
1754 
1755 /* {{{ proto array Phar::buildFromDirectory(string base_dir[, string regex])
1756  * Construct a phar archive from an existing directory, recursively.
1757  * Optional second parameter is a regular expression for filtering directory contents.
1758  *
1759  * Return value is an array mapping phar index to actual files added.
1760  */
PHP_METHOD(Phar,buildFromDirectory)1761 PHP_METHOD(Phar, buildFromDirectory)
1762 {
1763 	char *dir, *error, *regex = NULL;
1764 	size_t dir_len, regex_len = 0;
1765 	zend_bool apply_reg = 0;
1766 	zval arg, arg2, iter, iteriter, regexiter;
1767 	struct _phar_t pass;
1768 
1769 	PHAR_ARCHIVE_OBJECT();
1770 
1771 	if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
1772 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
1773 			"Cannot write to archive - write operations restricted by INI setting");
1774 		return;
1775 	}
1776 
1777 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|s", &dir, &dir_len, &regex, &regex_len) == FAILURE) {
1778 		RETURN_FALSE;
1779 	}
1780 
1781 	if (ZEND_SIZE_T_UINT_OVFL(dir_len)) {
1782 		RETURN_FALSE;
1783 	}
1784 
1785 	if (SUCCESS != object_init_ex(&iter, spl_ce_RecursiveDirectoryIterator)) {
1786 		zval_ptr_dtor(&iter);
1787 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to instantiate directory iterator for %s", phar_obj->archive->fname);
1788 		RETURN_FALSE;
1789 	}
1790 
1791 	ZVAL_STRINGL(&arg, dir, dir_len);
1792 	ZVAL_LONG(&arg2, SPL_FILE_DIR_SKIPDOTS|SPL_FILE_DIR_UNIXPATHS);
1793 
1794 	zend_call_method_with_2_params(&iter, spl_ce_RecursiveDirectoryIterator,
1795 			&spl_ce_RecursiveDirectoryIterator->constructor, "__construct", NULL, &arg, &arg2);
1796 
1797 	zval_ptr_dtor(&arg);
1798 	if (EG(exception)) {
1799 		zval_ptr_dtor(&iter);
1800 		RETURN_FALSE;
1801 	}
1802 
1803 	if (SUCCESS != object_init_ex(&iteriter, spl_ce_RecursiveIteratorIterator)) {
1804 		zval_ptr_dtor(&iter);
1805 		zval_ptr_dtor(&iteriter);
1806 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to instantiate directory iterator for %s", phar_obj->archive->fname);
1807 		RETURN_FALSE;
1808 	}
1809 
1810 	zend_call_method_with_1_params(&iteriter, spl_ce_RecursiveIteratorIterator,
1811 			&spl_ce_RecursiveIteratorIterator->constructor, "__construct", NULL, &iter);
1812 
1813 	if (EG(exception)) {
1814 		zval_ptr_dtor(&iter);
1815 		zval_ptr_dtor(&iteriter);
1816 		RETURN_FALSE;
1817 	}
1818 
1819 	zval_ptr_dtor(&iter);
1820 
1821 	if (regex_len > 0) {
1822 		apply_reg = 1;
1823 
1824 		if (SUCCESS != object_init_ex(&regexiter, spl_ce_RegexIterator)) {
1825 			zval_ptr_dtor(&iteriter);
1826 			zval_dtor(&regexiter);
1827 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to instantiate regex iterator for %s", phar_obj->archive->fname);
1828 			RETURN_FALSE;
1829 		}
1830 
1831 		ZVAL_STRINGL(&arg2, regex, regex_len);
1832 
1833 		zend_call_method_with_2_params(&regexiter, spl_ce_RegexIterator,
1834 			&spl_ce_RegexIterator->constructor, "__construct", NULL, &iteriter, &arg2);
1835 		zval_ptr_dtor(&arg2);
1836 	}
1837 
1838 	array_init(return_value);
1839 
1840 	pass.c = apply_reg ? Z_OBJCE(regexiter) : Z_OBJCE(iteriter);
1841 	pass.p = phar_obj;
1842 	pass.b = dir;
1843 	pass.l = (uint32_t)dir_len;
1844 	pass.count = 0;
1845 	pass.ret = return_value;
1846 	pass.fp = php_stream_fopen_tmpfile();
1847 	if (pass.fp == NULL) {
1848 		zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" unable to create temporary file", phar_obj->archive->fname);
1849 		return;
1850 	}
1851 
1852 	if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
1853 		zval_ptr_dtor(&iteriter);
1854 		if (apply_reg) {
1855 			zval_ptr_dtor(&regexiter);
1856 		}
1857 		php_stream_close(pass.fp);
1858 		zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
1859 		return;
1860 	}
1861 
1862 	if (SUCCESS == spl_iterator_apply((apply_reg ? &regexiter : &iteriter), (spl_iterator_apply_func_t) phar_build, (void *) &pass)) {
1863 		zval_ptr_dtor(&iteriter);
1864 
1865 		if (apply_reg) {
1866 			zval_ptr_dtor(&regexiter);
1867 		}
1868 
1869 		phar_obj->archive->ufp = pass.fp;
1870 		phar_flush(phar_obj->archive, 0, 0, 0, &error);
1871 
1872 		if (error) {
1873 			zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
1874 			efree(error);
1875 		}
1876 
1877 	} else {
1878 		zval_ptr_dtor(&iteriter);
1879 		if (apply_reg) {
1880 			zval_ptr_dtor(&regexiter);
1881 		}
1882 		php_stream_close(pass.fp);
1883 	}
1884 }
1885 /* }}} */
1886 
1887 /* {{{ proto array Phar::buildFromIterator(Iterator iter[, string base_directory])
1888  * Construct a phar archive from an iterator.  The iterator must return a series of strings
1889  * that are full paths to files that should be added to the phar.  The iterator key should
1890  * be the path that the file will have within the phar archive.
1891  *
1892  * If base directory is specified, then the key will be ignored, and instead the portion of
1893  * the current value minus the base directory will be used
1894  *
1895  * Returned is an array mapping phar index to actual file added
1896  */
PHP_METHOD(Phar,buildFromIterator)1897 PHP_METHOD(Phar, buildFromIterator)
1898 {
1899 	zval *obj;
1900 	char *error;
1901 	size_t base_len = 0;
1902 	char *base = NULL;
1903 	struct _phar_t pass;
1904 
1905 	PHAR_ARCHIVE_OBJECT();
1906 
1907 	if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
1908 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
1909 			"Cannot write out phar archive, phar is read-only");
1910 		return;
1911 	}
1912 
1913 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|s", &obj, zend_ce_traversable, &base, &base_len) == FAILURE) {
1914 		RETURN_FALSE;
1915 	}
1916 
1917 	if (ZEND_SIZE_T_UINT_OVFL(base_len)) {
1918 		RETURN_FALSE;
1919 	}
1920 
1921 	if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
1922 		zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
1923 		return;
1924 	}
1925 
1926 	array_init(return_value);
1927 
1928 	pass.c = Z_OBJCE_P(obj);
1929 	pass.p = phar_obj;
1930 	pass.b = base;
1931 	pass.l = (uint32_t)base_len;
1932 	pass.ret = return_value;
1933 	pass.count = 0;
1934 	pass.fp = php_stream_fopen_tmpfile();
1935 	if (pass.fp == NULL) {
1936 		zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\": unable to create temporary file", phar_obj->archive->fname);
1937 		return;
1938 	}
1939 
1940 	if (SUCCESS == spl_iterator_apply(obj, (spl_iterator_apply_func_t) phar_build, (void *) &pass)) {
1941 		phar_obj->archive->ufp = pass.fp;
1942 		phar_flush(phar_obj->archive, 0, 0, 0, &error);
1943 		if (error) {
1944 			zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
1945 			efree(error);
1946 		}
1947 	} else {
1948 		php_stream_close(pass.fp);
1949 	}
1950 }
1951 /* }}} */
1952 
1953 /* {{{ proto int Phar::count()
1954  * Returns the number of entries in the Phar archive
1955  */
PHP_METHOD(Phar,count)1956 PHP_METHOD(Phar, count)
1957 {
1958 	/* mode can be ignored, maximum depth is 1 */
1959 	zend_long mode;
1960 	PHAR_ARCHIVE_OBJECT();
1961 
1962 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &mode) == FAILURE) {
1963 		RETURN_FALSE;
1964 	}
1965 
1966 	RETURN_LONG(zend_hash_num_elements(&phar_obj->archive->manifest));
1967 }
1968 /* }}} */
1969 
1970 /* {{{ proto bool Phar::isFileFormat(int format)
1971  * Returns true if the phar archive is based on the tar/zip/phar file format depending
1972  * on whether Phar::TAR, Phar::ZIP or Phar::PHAR was passed in
1973  */
PHP_METHOD(Phar,isFileFormat)1974 PHP_METHOD(Phar, isFileFormat)
1975 {
1976 	zend_long type;
1977 	PHAR_ARCHIVE_OBJECT();
1978 
1979 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &type) == FAILURE) {
1980 		RETURN_FALSE;
1981 	}
1982 
1983 	switch (type) {
1984 		case PHAR_FORMAT_TAR:
1985 			RETURN_BOOL(phar_obj->archive->is_tar);
1986 		case PHAR_FORMAT_ZIP:
1987 			RETURN_BOOL(phar_obj->archive->is_zip);
1988 		case PHAR_FORMAT_PHAR:
1989 			RETURN_BOOL(!phar_obj->archive->is_tar && !phar_obj->archive->is_zip);
1990 		default:
1991 			zend_throw_exception_ex(phar_ce_PharException, 0, "Unknown file format specified");
1992 	}
1993 }
1994 /* }}} */
1995 
phar_copy_file_contents(phar_entry_info * entry,php_stream * fp)1996 static int phar_copy_file_contents(phar_entry_info *entry, php_stream *fp) /* {{{ */
1997 {
1998 	char *error;
1999 	zend_off_t offset;
2000 	phar_entry_info *link;
2001 
2002 	if (FAILURE == phar_open_entry_fp(entry, &error, 1)) {
2003 		if (error) {
2004 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
2005 				"Cannot convert phar archive \"%s\", unable to open entry \"%s\" contents: %s", entry->phar->fname, entry->filename, error);
2006 			efree(error);
2007 		} else {
2008 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
2009 				"Cannot convert phar archive \"%s\", unable to open entry \"%s\" contents", entry->phar->fname, entry->filename);
2010 		}
2011 		return FAILURE;
2012 	}
2013 
2014 	/* copy old contents in entirety */
2015 	phar_seek_efp(entry, 0, SEEK_SET, 0, 1);
2016 	offset = php_stream_tell(fp);
2017 	link = phar_get_link_source(entry);
2018 
2019 	if (!link) {
2020 		link = entry;
2021 	}
2022 
2023 	if (SUCCESS != php_stream_copy_to_stream_ex(phar_get_efp(link, 0), fp, link->uncompressed_filesize, NULL)) {
2024 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
2025 			"Cannot convert phar archive \"%s\", unable to copy entry \"%s\" contents", entry->phar->fname, entry->filename);
2026 		return FAILURE;
2027 	}
2028 
2029 	if (entry->fp_type == PHAR_MOD) {
2030 		/* save for potential restore on error */
2031 		entry->cfp = entry->fp;
2032 		entry->fp = NULL;
2033 	}
2034 
2035 	/* set new location of file contents */
2036 	entry->fp_type = PHAR_FP;
2037 	entry->offset = offset;
2038 	return SUCCESS;
2039 }
2040 /* }}} */
2041 
phar_rename_archive(phar_archive_data ** sphar,char * ext)2042 static zend_object *phar_rename_archive(phar_archive_data **sphar, char *ext) /* {{{ */
2043 {
2044 	const char *oldname = NULL;
2045 	phar_archive_data *phar = *sphar;
2046 	char *oldpath = NULL;
2047 	char *basename = NULL, *basepath = NULL;
2048 	char *newname = NULL, *newpath = NULL;
2049 	zval ret, arg1;
2050 	zend_class_entry *ce;
2051 	char *error = NULL;
2052 	const char *pcr_error;
2053 	int ext_len = ext ? strlen(ext) : 0;
2054 	size_t new_len, oldname_len, phar_ext_len;
2055 	phar_archive_data *pphar = NULL;
2056 	php_stream_statbuf ssb;
2057 
2058 	int phar_ext_list_len, i = 0;
2059 	char *ext_pos = NULL;
2060 	/* Array of PHAR extensions, Must be in order, starting with longest
2061 	 * ending with the shortest. */
2062 	char *phar_ext_list[] = {
2063 		".phar.tar.bz2",
2064 		".phar.tar.gz",
2065 		".phar.php",
2066 		".phar.bz2",
2067 		".phar.zip",
2068 		".phar.tar",
2069 		".phar.gz",
2070 		".tar.bz2",
2071 		".tar.gz",
2072 		".phar",
2073 		".tar",
2074 		".zip"
2075 	};
2076 
2077 	if (!ext) {
2078 		if (phar->is_zip) {
2079 
2080 			if (phar->is_data) {
2081 				ext = "zip";
2082 			} else {
2083 				ext = "phar.zip";
2084 			}
2085 
2086 		} else if (phar->is_tar) {
2087 
2088 			switch (phar->flags) {
2089 				case PHAR_FILE_COMPRESSED_GZ:
2090 					if (phar->is_data) {
2091 						ext = "tar.gz";
2092 					} else {
2093 						ext = "phar.tar.gz";
2094 					}
2095 					break;
2096 				case PHAR_FILE_COMPRESSED_BZ2:
2097 					if (phar->is_data) {
2098 						ext = "tar.bz2";
2099 					} else {
2100 						ext = "phar.tar.bz2";
2101 					}
2102 					break;
2103 				default:
2104 					if (phar->is_data) {
2105 						ext = "tar";
2106 					} else {
2107 						ext = "phar.tar";
2108 					}
2109 			}
2110 		} else {
2111 
2112 			switch (phar->flags) {
2113 				case PHAR_FILE_COMPRESSED_GZ:
2114 					ext = "phar.gz";
2115 					break;
2116 				case PHAR_FILE_COMPRESSED_BZ2:
2117 					ext = "phar.bz2";
2118 					break;
2119 				default:
2120 					ext = "phar";
2121 			}
2122 		}
2123 	} else if (phar_path_check(&ext, &ext_len, &pcr_error) > pcr_is_ok) {
2124 
2125 		if (phar->is_data) {
2126 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "data phar converted from \"%s\" has invalid extension %s", phar->fname, ext);
2127 		} else {
2128 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "phar converted from \"%s\" has invalid extension %s", phar->fname, ext);
2129 		}
2130 		return NULL;
2131 	}
2132 
2133 
2134 	oldpath = estrndup(phar->fname, phar->fname_len);
2135 	if ((oldname = zend_memrchr(phar->fname, '/', phar->fname_len))) {
2136 		++oldname;
2137 	} else {
2138 		oldname = phar->fname;
2139 	}
2140 
2141 	oldname_len = strlen(oldname);
2142 	/* Copy the old name to create base for the new name */
2143 	basename = estrndup(oldname, oldname_len);
2144 
2145 	phar_ext_list_len = sizeof(phar_ext_list)/sizeof(phar_ext_list[0]);
2146 	/* Remove possible PHAR extensions */
2147 	/* phar_ext_list must be in order of longest extension to shortest */
2148 	for (i=0; i < phar_ext_list_len; i++) {
2149 		phar_ext_len = strlen(phar_ext_list[i]);
2150 		if (phar_ext_len && oldname_len > phar_ext_len) {
2151 			/* Check if the basename strings ends with the extension */
2152 			if (memcmp(phar_ext_list[i], basename + (oldname_len - phar_ext_len), phar_ext_len) == 0) {
2153 				ext_pos = basename + (oldname_len - phar_ext_len);
2154 				ext_pos[0] = '\0';
2155 				break;
2156 			}
2157 		}
2158 		ext_pos = NULL;
2159 	}
2160 
2161 	/* If no default PHAR extension found remove the last extension */
2162 	if (!ext_pos) {
2163 		ext_pos = strrchr(basename, '.');
2164 		if (ext_pos) {
2165 			ext_pos[0] = '\0';
2166 		}
2167 	}
2168 	ext_pos = NULL;
2169 
2170 	if (ext[0] == '.') {
2171 		++ext;
2172 	}
2173 	/* Append extension to the basename */
2174 	spprintf(&newname, 0, "%s.%s", basename, ext);
2175 	efree(basename);
2176 
2177 	basepath = estrndup(oldpath, (strlen(oldpath) - oldname_len));
2178 	new_len = spprintf(&newpath, 0, "%s%s", basepath, newname);
2179 	if (ZEND_SIZE_T_INT_OVFL(new_len)) {
2180 		efree(oldpath);
2181 		efree(basepath);
2182 		efree(newpath);
2183 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "New name is too long");
2184 		return NULL;
2185 	}
2186 	phar->fname_len = (int)new_len;
2187 	phar->fname = newpath;
2188 	phar->ext = newpath + phar->fname_len - strlen(ext) - 1;
2189 	efree(basepath);
2190 	efree(newname);
2191 
2192 	if (PHAR_G(manifest_cached) && NULL != (pphar = zend_hash_str_find_ptr(&cached_phars, newpath, phar->fname_len))) {
2193 		efree(oldpath);
2194 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to add newly converted phar \"%s\" to the list of phars, new phar name is in phar.cache_list", phar->fname);
2195 		return NULL;
2196 	}
2197 
2198 	if (NULL != (pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), newpath, phar->fname_len))) {
2199 		if (pphar->fname_len == phar->fname_len && !memcmp(pphar->fname, phar->fname, phar->fname_len)) {
2200 			if (!zend_hash_num_elements(&phar->manifest)) {
2201 				pphar->is_tar = phar->is_tar;
2202 				pphar->is_zip = phar->is_zip;
2203 				pphar->is_data = phar->is_data;
2204 				pphar->flags = phar->flags;
2205 				pphar->fp = phar->fp;
2206 				phar->fp = NULL;
2207 				phar_destroy_phar_data(phar);
2208 				*sphar = NULL;
2209 				phar = pphar;
2210 				phar->refcount++;
2211 				newpath = oldpath;
2212 				goto its_ok;
2213 			}
2214 		}
2215 
2216 		efree(oldpath);
2217 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to add newly converted phar \"%s\" to the list of phars, a phar with that name already exists", phar->fname);
2218 		return NULL;
2219 	}
2220 its_ok:
2221 	if (SUCCESS == php_stream_stat_path(newpath, &ssb)) {
2222 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "phar \"%s\" exists and must be unlinked prior to conversion", newpath);
2223 		efree(oldpath);
2224 		return NULL;
2225 	}
2226 	if (!phar->is_data) {
2227 		if (SUCCESS != phar_detect_phar_fname_ext(newpath, phar->fname_len, (const char **) &(phar->ext), &(phar->ext_len), 1, 1, 1)) {
2228 			efree(oldpath);
2229 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "phar \"%s\" has invalid extension %s", phar->fname, ext);
2230 			return NULL;
2231 		}
2232 
2233 		if (phar->alias) {
2234 			if (phar->is_temporary_alias) {
2235 				phar->alias = NULL;
2236 				phar->alias_len = 0;
2237 			} else {
2238 				phar->alias = estrndup(newpath, strlen(newpath));
2239 				phar->alias_len = (int)strlen(newpath);
2240 				phar->is_temporary_alias = 1;
2241 				zend_hash_str_update_ptr(&(PHAR_G(phar_alias_map)), newpath, phar->fname_len, phar);
2242 			}
2243 		}
2244 
2245 	} else {
2246 
2247 		if (SUCCESS != phar_detect_phar_fname_ext(newpath, phar->fname_len, (const char **) &(phar->ext), &(phar->ext_len), 0, 1, 1)) {
2248 			efree(oldpath);
2249 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "data phar \"%s\" has invalid extension %s", phar->fname, ext);
2250 			return NULL;
2251 		}
2252 
2253 		phar->alias = NULL;
2254 		phar->alias_len = 0;
2255 	}
2256 
2257 	if ((!pphar || phar == pphar) && NULL == zend_hash_str_update_ptr(&(PHAR_G(phar_fname_map)), newpath, phar->fname_len, phar)) {
2258 		efree(oldpath);
2259 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to add newly converted phar \"%s\" to the list of phars", phar->fname);
2260 		return NULL;
2261 	}
2262 
2263 	phar_flush(phar, 0, 0, 1, &error);
2264 
2265 	if (error) {
2266 		zend_hash_str_del(&(PHAR_G(phar_fname_map)), newpath, phar->fname_len);
2267 		*sphar = NULL;
2268 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "%s", error);
2269 		efree(error);
2270 		efree(oldpath);
2271 		return NULL;
2272 	}
2273 
2274 	efree(oldpath);
2275 
2276 	if (phar->is_data) {
2277 		ce = phar_ce_data;
2278 	} else {
2279 		ce = phar_ce_archive;
2280 	}
2281 
2282 	ZVAL_NULL(&ret);
2283 	if (SUCCESS != object_init_ex(&ret, ce)) {
2284 		zval_dtor(&ret);
2285 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to instantiate phar object when converting archive \"%s\"", phar->fname);
2286 		return NULL;
2287 	}
2288 
2289 	ZVAL_STRINGL(&arg1, phar->fname, phar->fname_len);
2290 
2291 	zend_call_method_with_1_params(&ret, ce, &ce->constructor, "__construct", NULL, &arg1);
2292 	zval_ptr_dtor(&arg1);
2293 	return Z_OBJ(ret);
2294 }
2295 /* }}} */
2296 
phar_convert_to_other(phar_archive_data * source,int convert,char * ext,uint32_t flags)2297 static zend_object *phar_convert_to_other(phar_archive_data *source, int convert, char *ext, uint32_t flags) /* {{{ */
2298 {
2299 	phar_archive_data *phar;
2300 	phar_entry_info *entry, newentry;
2301 	zend_object *ret;
2302 
2303 	/* invalidate phar cache */
2304 	PHAR_G(last_phar) = NULL;
2305 	PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
2306 
2307 	phar = (phar_archive_data *) ecalloc(1, sizeof(phar_archive_data));
2308 	/* set whole-archive compression and type from parameter */
2309 	phar->flags = flags;
2310 	phar->is_data = source->is_data;
2311 
2312 	switch (convert) {
2313 		case PHAR_FORMAT_TAR:
2314 			phar->is_tar = 1;
2315 			break;
2316 		case PHAR_FORMAT_ZIP:
2317 			phar->is_zip = 1;
2318 			break;
2319 		default:
2320 			phar->is_data = 0;
2321 			break;
2322 	}
2323 
2324 	zend_hash_init(&(phar->manifest), sizeof(phar_entry_info),
2325 		zend_get_hash_value, destroy_phar_manifest_entry, 0);
2326 	zend_hash_init(&phar->mounted_dirs, sizeof(char *),
2327 		zend_get_hash_value, NULL, 0);
2328 	zend_hash_init(&phar->virtual_dirs, sizeof(char *),
2329 		zend_get_hash_value, NULL, 0);
2330 
2331 	phar->fp = php_stream_fopen_tmpfile();
2332 	if (phar->fp == NULL) {
2333 		zend_throw_exception_ex(phar_ce_PharException, 0, "unable to create temporary file");
2334 		return NULL;
2335 	}
2336 	phar->fname = source->fname;
2337 	phar->fname_len = source->fname_len;
2338 	phar->is_temporary_alias = source->is_temporary_alias;
2339 	phar->alias = source->alias;
2340 
2341 	if (Z_TYPE(source->metadata) != IS_UNDEF) {
2342 		ZVAL_DUP(&phar->metadata, &source->metadata);
2343 		phar->metadata_len = 0;
2344 	}
2345 
2346 	/* first copy each file's uncompressed contents to a temporary file and set per-file flags */
2347 	ZEND_HASH_FOREACH_PTR(&source->manifest, entry) {
2348 
2349 		newentry = *entry;
2350 
2351 		if (newentry.link) {
2352 			newentry.link = estrdup(newentry.link);
2353 			goto no_copy;
2354 		}
2355 
2356 		if (newentry.tmp) {
2357 			newentry.tmp = estrdup(newentry.tmp);
2358 			goto no_copy;
2359 		}
2360 
2361 		newentry.metadata_str.s = NULL;
2362 
2363 		if (FAILURE == phar_copy_file_contents(&newentry, phar->fp)) {
2364 			zend_hash_destroy(&(phar->manifest));
2365 			php_stream_close(phar->fp);
2366 			efree(phar);
2367 			/* exception already thrown */
2368 			return NULL;
2369 		}
2370 no_copy:
2371 		newentry.filename = estrndup(newentry.filename, newentry.filename_len);
2372 
2373 		if (Z_TYPE(newentry.metadata) != IS_UNDEF) {
2374 			zval_copy_ctor(&newentry.metadata);
2375 			newentry.metadata_str.s = NULL;
2376 		}
2377 
2378 		newentry.is_zip = phar->is_zip;
2379 		newentry.is_tar = phar->is_tar;
2380 
2381 		if (newentry.is_tar) {
2382 			newentry.tar_type = (entry->is_dir ? TAR_DIR : TAR_FILE);
2383 		}
2384 
2385 		newentry.is_modified = 1;
2386 		newentry.phar = phar;
2387 		newentry.old_flags = newentry.flags & ~PHAR_ENT_COMPRESSION_MASK; /* remove compression from old_flags */
2388 		phar_set_inode(&newentry);
2389 		zend_hash_str_add_mem(&(phar->manifest), newentry.filename, newentry.filename_len, (void*)&newentry, sizeof(phar_entry_info));
2390 		phar_add_virtual_dirs(phar, newentry.filename, newentry.filename_len);
2391 	} ZEND_HASH_FOREACH_END();
2392 
2393 	if ((ret = phar_rename_archive(&phar, ext))) {
2394 		return ret;
2395 	} else {
2396 		if(phar != NULL) {
2397 			zend_hash_destroy(&(phar->manifest));
2398 			zend_hash_destroy(&(phar->mounted_dirs));
2399 			zend_hash_destroy(&(phar->virtual_dirs));
2400 			if (phar->fp) {
2401 				php_stream_close(phar->fp);
2402 			}
2403 			efree(phar->fname);
2404 			efree(phar);
2405 		}
2406 		return NULL;
2407 	}
2408 }
2409 /* }}} */
2410 
2411 /* {{{ proto object Phar::convertToExecutable([int format[, int compression [, string file_ext]]])
2412  * Convert a phar.tar or phar.zip archive to the phar file format. The
2413  * optional parameter allows the user to determine the new
2414  * filename extension (default is phar).
2415  */
PHP_METHOD(Phar,convertToExecutable)2416 PHP_METHOD(Phar, convertToExecutable)
2417 {
2418 	char *ext = NULL;
2419 	int is_data;
2420 	size_t ext_len = 0;
2421 	uint32_t flags;
2422 	zend_object *ret;
2423 	/* a number that is not 0, 1 or 2 (Which is also Greg's birthday, so there) */
2424 	zend_long format = 9021976, method = 9021976;
2425 	PHAR_ARCHIVE_OBJECT();
2426 
2427 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|lls", &format, &method, &ext, &ext_len) == FAILURE) {
2428 		return;
2429 	}
2430 
2431 	if (PHAR_G(readonly)) {
2432 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
2433 			"Cannot write out executable phar archive, phar is read-only");
2434 		return;
2435 	}
2436 
2437 	switch (format) {
2438 		case 9021976:
2439 		case PHAR_FORMAT_SAME: /* null is converted to 0 */
2440 			/* by default, use the existing format */
2441 			if (phar_obj->archive->is_tar) {
2442 				format = PHAR_FORMAT_TAR;
2443 			} else if (phar_obj->archive->is_zip) {
2444 				format = PHAR_FORMAT_ZIP;
2445 			} else {
2446 				format = PHAR_FORMAT_PHAR;
2447 			}
2448 			break;
2449 		case PHAR_FORMAT_PHAR:
2450 		case PHAR_FORMAT_TAR:
2451 		case PHAR_FORMAT_ZIP:
2452 			break;
2453 		default:
2454 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
2455 				"Unknown file format specified, please pass one of Phar::PHAR, Phar::TAR or Phar::ZIP");
2456 			return;
2457 	}
2458 
2459 	switch (method) {
2460 		case 9021976:
2461 			flags = phar_obj->archive->flags & PHAR_FILE_COMPRESSION_MASK;
2462 			break;
2463 		case 0:
2464 			flags = PHAR_FILE_COMPRESSED_NONE;
2465 			break;
2466 		case PHAR_ENT_COMPRESSED_GZ:
2467 			if (format == PHAR_FORMAT_ZIP) {
2468 				zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
2469 					"Cannot compress entire archive with gzip, zip archives do not support whole-archive compression");
2470 				return;
2471 			}
2472 
2473 			if (!PHAR_G(has_zlib)) {
2474 				zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
2475 					"Cannot compress entire archive with gzip, enable ext/zlib in php.ini");
2476 				return;
2477 			}
2478 
2479 			flags = PHAR_FILE_COMPRESSED_GZ;
2480 			break;
2481 		case PHAR_ENT_COMPRESSED_BZ2:
2482 			if (format == PHAR_FORMAT_ZIP) {
2483 				zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
2484 					"Cannot compress entire archive with bz2, zip archives do not support whole-archive compression");
2485 				return;
2486 			}
2487 
2488 			if (!PHAR_G(has_bz2)) {
2489 				zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
2490 					"Cannot compress entire archive with bz2, enable ext/bz2 in php.ini");
2491 				return;
2492 			}
2493 
2494 			flags = PHAR_FILE_COMPRESSED_BZ2;
2495 			break;
2496 		default:
2497 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
2498 				"Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
2499 			return;
2500 	}
2501 
2502 	is_data = phar_obj->archive->is_data;
2503 	phar_obj->archive->is_data = 0;
2504 	ret = phar_convert_to_other(phar_obj->archive, (int)format, ext, flags);
2505 	phar_obj->archive->is_data = is_data;
2506 
2507 	if (ret) {
2508 		ZVAL_OBJ(return_value, ret);
2509 	} else {
2510 		RETURN_NULL();
2511 	}
2512 }
2513 /* }}} */
2514 
2515 /* {{{ proto object Phar::convertToData([int format[, int compression [, string file_ext]]])
2516  * Convert an archive to a non-executable .tar or .zip.
2517  * The optional parameter allows the user to determine the new
2518  * filename extension (default is .zip or .tar).
2519  */
PHP_METHOD(Phar,convertToData)2520 PHP_METHOD(Phar, convertToData)
2521 {
2522 	char *ext = NULL;
2523 	int is_data;
2524 	size_t ext_len = 0;
2525 	uint32_t flags;
2526 	zend_object *ret;
2527 	/* a number that is not 0, 1 or 2 (Which is also Greg's birthday so there) */
2528 	zend_long format = 9021976, method = 9021976;
2529 	PHAR_ARCHIVE_OBJECT();
2530 
2531 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|lls", &format, &method, &ext, &ext_len) == FAILURE) {
2532 		return;
2533 	}
2534 
2535 	switch (format) {
2536 		case 9021976:
2537 		case PHAR_FORMAT_SAME: /* null is converted to 0 */
2538 			/* by default, use the existing format */
2539 			if (phar_obj->archive->is_tar) {
2540 				format = PHAR_FORMAT_TAR;
2541 			} else if (phar_obj->archive->is_zip) {
2542 				format = PHAR_FORMAT_ZIP;
2543 			} else {
2544 				zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
2545 					"Cannot write out data phar archive, use Phar::TAR or Phar::ZIP");
2546 				return;
2547 			}
2548 			break;
2549 		case PHAR_FORMAT_PHAR:
2550 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
2551 				"Cannot write out data phar archive, use Phar::TAR or Phar::ZIP");
2552 			return;
2553 		case PHAR_FORMAT_TAR:
2554 		case PHAR_FORMAT_ZIP:
2555 			break;
2556 		default:
2557 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
2558 				"Unknown file format specified, please pass one of Phar::TAR or Phar::ZIP");
2559 			return;
2560 	}
2561 
2562 	switch (method) {
2563 		case 9021976:
2564 			flags = phar_obj->archive->flags & PHAR_FILE_COMPRESSION_MASK;
2565 			break;
2566 		case 0:
2567 			flags = PHAR_FILE_COMPRESSED_NONE;
2568 			break;
2569 		case PHAR_ENT_COMPRESSED_GZ:
2570 			if (format == PHAR_FORMAT_ZIP) {
2571 				zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
2572 					"Cannot compress entire archive with gzip, zip archives do not support whole-archive compression");
2573 				return;
2574 			}
2575 
2576 			if (!PHAR_G(has_zlib)) {
2577 				zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
2578 					"Cannot compress entire archive with gzip, enable ext/zlib in php.ini");
2579 				return;
2580 			}
2581 
2582 			flags = PHAR_FILE_COMPRESSED_GZ;
2583 			break;
2584 		case PHAR_ENT_COMPRESSED_BZ2:
2585 			if (format == PHAR_FORMAT_ZIP) {
2586 				zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
2587 					"Cannot compress entire archive with bz2, zip archives do not support whole-archive compression");
2588 				return;
2589 			}
2590 
2591 			if (!PHAR_G(has_bz2)) {
2592 				zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
2593 					"Cannot compress entire archive with bz2, enable ext/bz2 in php.ini");
2594 				return;
2595 			}
2596 
2597 			flags = PHAR_FILE_COMPRESSED_BZ2;
2598 			break;
2599 		default:
2600 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
2601 				"Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
2602 			return;
2603 	}
2604 
2605 	is_data = phar_obj->archive->is_data;
2606 	phar_obj->archive->is_data = 1;
2607 	ret = phar_convert_to_other(phar_obj->archive, (int)format, ext, flags);
2608 	phar_obj->archive->is_data = is_data;
2609 
2610 	if (ret) {
2611 		ZVAL_OBJ(return_value, ret);
2612 	} else {
2613 		RETURN_NULL();
2614 	}
2615 }
2616 /* }}} */
2617 
2618 /* {{{ proto int|false Phar::isCompressed()
2619  * Returns Phar::GZ or PHAR::BZ2 if the entire archive is compressed
2620  * (.tar.gz/tar.bz2 and so on), or FALSE otherwise.
2621  */
PHP_METHOD(Phar,isCompressed)2622 PHP_METHOD(Phar, isCompressed)
2623 {
2624 	PHAR_ARCHIVE_OBJECT();
2625 
2626 	if (zend_parse_parameters_none() == FAILURE) {
2627 		return;
2628 	}
2629 
2630 	if (phar_obj->archive->flags & PHAR_FILE_COMPRESSED_GZ) {
2631 		RETURN_LONG(PHAR_ENT_COMPRESSED_GZ);
2632 	}
2633 
2634 	if (phar_obj->archive->flags & PHAR_FILE_COMPRESSED_BZ2) {
2635 		RETURN_LONG(PHAR_ENT_COMPRESSED_BZ2);
2636 	}
2637 
2638 	RETURN_FALSE;
2639 }
2640 /* }}} */
2641 
2642 /* {{{ proto bool Phar::isWritable()
2643  * Returns true if phar.readonly=0 or phar is a PharData AND the actual file is writable.
2644  */
PHP_METHOD(Phar,isWritable)2645 PHP_METHOD(Phar, isWritable)
2646 {
2647 	php_stream_statbuf ssb;
2648 	PHAR_ARCHIVE_OBJECT();
2649 
2650 	if (zend_parse_parameters_none() == FAILURE) {
2651 		return;
2652 	}
2653 
2654 	if (!phar_obj->archive->is_writeable) {
2655 		RETURN_FALSE;
2656 	}
2657 
2658 	if (SUCCESS != php_stream_stat_path(phar_obj->archive->fname, &ssb)) {
2659 		if (phar_obj->archive->is_brandnew) {
2660 			/* assume it works if the file doesn't exist yet */
2661 			RETURN_TRUE;
2662 		}
2663 		RETURN_FALSE;
2664 	}
2665 
2666 	RETURN_BOOL((ssb.sb.st_mode & (S_IWOTH | S_IWGRP | S_IWUSR)) != 0);
2667 }
2668 /* }}} */
2669 
2670 /* {{{ proto bool Phar::delete(string entry)
2671  * Deletes a named file within the archive.
2672  */
PHP_METHOD(Phar,delete)2673 PHP_METHOD(Phar, delete)
2674 {
2675 	char *fname;
2676 	size_t fname_len;
2677 	char *error;
2678 	phar_entry_info *entry;
2679 	PHAR_ARCHIVE_OBJECT();
2680 
2681 	if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
2682 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
2683 			"Cannot write out phar archive, phar is read-only");
2684 		return;
2685 	}
2686 
2687 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
2688 		RETURN_FALSE;
2689 	}
2690 
2691 	if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
2692 		zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
2693 		return;
2694 	}
2695 	if (zend_hash_str_exists(&phar_obj->archive->manifest, fname, (uint32_t) fname_len)) {
2696 		if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, (uint32_t) fname_len))) {
2697 			if (entry->is_deleted) {
2698 				/* entry is deleted, but has not been flushed to disk yet */
2699 				RETURN_TRUE;
2700 			} else {
2701 				entry->is_deleted = 1;
2702 				entry->is_modified = 1;
2703 				phar_obj->archive->is_modified = 1;
2704 			}
2705 		}
2706 	} else {
2707 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist and cannot be deleted", fname);
2708 		RETURN_FALSE;
2709 	}
2710 
2711 	phar_flush(phar_obj->archive, NULL, 0, 0, &error);
2712 	if (error) {
2713 		zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
2714 		efree(error);
2715 	}
2716 
2717 	RETURN_TRUE;
2718 }
2719 /* }}} */
2720 
2721 /* {{{ proto int Phar::getAlias()
2722  * Returns the alias for the Phar or NULL.
2723  */
PHP_METHOD(Phar,getAlias)2724 PHP_METHOD(Phar, getAlias)
2725 {
2726 	PHAR_ARCHIVE_OBJECT();
2727 
2728 	if (zend_parse_parameters_none() == FAILURE) {
2729 		return;
2730 	}
2731 
2732 	if (phar_obj->archive->alias && phar_obj->archive->alias != phar_obj->archive->fname) {
2733 		RETURN_STRINGL(phar_obj->archive->alias, phar_obj->archive->alias_len);
2734 	}
2735 }
2736 /* }}} */
2737 
2738 /* {{{ proto int Phar::getPath()
2739  * Returns the real path to the phar archive on disk
2740  */
PHP_METHOD(Phar,getPath)2741 PHP_METHOD(Phar, getPath)
2742 {
2743 	PHAR_ARCHIVE_OBJECT();
2744 
2745 	if (zend_parse_parameters_none() == FAILURE) {
2746 		return;
2747 	}
2748 
2749 	RETURN_STRINGL(phar_obj->archive->fname, phar_obj->archive->fname_len);
2750 }
2751 /* }}} */
2752 
2753 /* {{{ proto bool Phar::setAlias(string alias)
2754  * Sets the alias for a Phar archive. The default value is the full path
2755  * to the archive.
2756  */
PHP_METHOD(Phar,setAlias)2757 PHP_METHOD(Phar, setAlias)
2758 {
2759 	char *alias, *error, *oldalias;
2760 	phar_archive_data *fd_ptr;
2761 	size_t alias_len, oldalias_len;
2762 	int old_temp, readd = 0;
2763 
2764 	PHAR_ARCHIVE_OBJECT();
2765 
2766 	if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
2767 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
2768 			"Cannot write out phar archive, phar is read-only");
2769 		RETURN_FALSE;
2770 	}
2771 
2772 	/* invalidate phar cache */
2773 	PHAR_G(last_phar) = NULL;
2774 	PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
2775 
2776 	if (phar_obj->archive->is_data) {
2777 		if (phar_obj->archive->is_tar) {
2778 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
2779 				"A Phar alias cannot be set in a plain tar archive");
2780 		} else {
2781 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
2782 				"A Phar alias cannot be set in a plain zip archive");
2783 		}
2784 		RETURN_FALSE;
2785 	}
2786 
2787 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &alias, &alias_len) == SUCCESS) {
2788 		if (ZEND_SIZE_T_INT_OVFL(alias_len)) {
2789 			RETURN_FALSE;
2790 		}
2791 		if (alias_len == (size_t)phar_obj->archive->alias_len && memcmp(phar_obj->archive->alias, alias, alias_len) == 0) {
2792 			RETURN_TRUE;
2793 		}
2794 		if (alias_len && NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len))) {
2795 			spprintf(&error, 0, "alias \"%s\" is already used for archive \"%s\" and cannot be used for other archives", alias, fd_ptr->fname);
2796 			if (SUCCESS == phar_free_alias(fd_ptr, alias, (int)alias_len)) {
2797 				efree(error);
2798 				goto valid_alias;
2799 			}
2800 			zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
2801 			efree(error);
2802 			RETURN_FALSE;
2803 		}
2804 		if (!phar_validate_alias(alias, (int)alias_len)) {
2805 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
2806 				"Invalid alias \"%s\" specified for phar \"%s\"", alias, phar_obj->archive->fname);
2807 			RETURN_FALSE;
2808 		}
2809 valid_alias:
2810 		if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
2811 			zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
2812 			return;
2813 		}
2814 		if (phar_obj->archive->alias_len && NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), phar_obj->archive->alias, phar_obj->archive->alias_len))) {
2815 			zend_hash_str_del(&(PHAR_G(phar_alias_map)), phar_obj->archive->alias, phar_obj->archive->alias_len);
2816 			readd = 1;
2817 		}
2818 
2819 		oldalias = phar_obj->archive->alias;
2820 		oldalias_len = phar_obj->archive->alias_len;
2821 		old_temp = phar_obj->archive->is_temporary_alias;
2822 
2823 		if (alias_len) {
2824 			phar_obj->archive->alias = estrndup(alias, alias_len);
2825 		} else {
2826 			phar_obj->archive->alias = NULL;
2827 		}
2828 
2829 		phar_obj->archive->alias_len = (int)alias_len;
2830 		phar_obj->archive->is_temporary_alias = 0;
2831 		phar_flush(phar_obj->archive, NULL, 0, 0, &error);
2832 
2833 		if (error) {
2834 			phar_obj->archive->alias = oldalias;
2835 			phar_obj->archive->alias_len = (int)oldalias_len;
2836 			phar_obj->archive->is_temporary_alias = old_temp;
2837 			zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
2838 			if (readd) {
2839 				zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), oldalias, oldalias_len, phar_obj->archive);
2840 			}
2841 			efree(error);
2842 			RETURN_FALSE;
2843 		}
2844 
2845 		zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len, phar_obj->archive);
2846 
2847 		if (oldalias) {
2848 			efree(oldalias);
2849 		}
2850 
2851 		RETURN_TRUE;
2852 	}
2853 
2854 	RETURN_FALSE;
2855 }
2856 /* }}} */
2857 
2858 /* {{{ proto string Phar::getVersion()
2859  * Return version info of Phar archive
2860  */
PHP_METHOD(Phar,getVersion)2861 PHP_METHOD(Phar, getVersion)
2862 {
2863 	PHAR_ARCHIVE_OBJECT();
2864 
2865 	if (zend_parse_parameters_none() == FAILURE) {
2866 		return;
2867 	}
2868 
2869 	RETURN_STRING(phar_obj->archive->version);
2870 }
2871 /* }}} */
2872 
2873 /* {{{ proto void Phar::startBuffering()
2874  * Do not flush a writeable phar (save its contents) until explicitly requested
2875  */
PHP_METHOD(Phar,startBuffering)2876 PHP_METHOD(Phar, startBuffering)
2877 {
2878 	PHAR_ARCHIVE_OBJECT();
2879 
2880 	if (zend_parse_parameters_none() == FAILURE) {
2881 		return;
2882 	}
2883 
2884 	phar_obj->archive->donotflush = 1;
2885 }
2886 /* }}} */
2887 
2888 /* {{{ proto bool Phar::isBuffering()
2889  * Returns whether write operations are flushing to disk immediately.
2890  */
PHP_METHOD(Phar,isBuffering)2891 PHP_METHOD(Phar, isBuffering)
2892 {
2893 	PHAR_ARCHIVE_OBJECT();
2894 
2895 	if (zend_parse_parameters_none() == FAILURE) {
2896 		return;
2897 	}
2898 
2899 	RETURN_BOOL(phar_obj->archive->donotflush);
2900 }
2901 /* }}} */
2902 
2903 /* {{{ proto bool Phar::stopBuffering()
2904  * Saves the contents of a modified archive to disk.
2905  */
PHP_METHOD(Phar,stopBuffering)2906 PHP_METHOD(Phar, stopBuffering)
2907 {
2908 	char *error;
2909 
2910 	PHAR_ARCHIVE_OBJECT();
2911 
2912 	if (zend_parse_parameters_none() == FAILURE) {
2913 		return;
2914 	}
2915 
2916 	if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
2917 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
2918 			"Cannot write out phar archive, phar is read-only");
2919 		return;
2920 	}
2921 
2922 	phar_obj->archive->donotflush = 0;
2923 	phar_flush(phar_obj->archive, 0, 0, 0, &error);
2924 
2925 	if (error) {
2926 		zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
2927 		efree(error);
2928 	}
2929 }
2930 /* }}} */
2931 
2932 /* {{{ proto bool Phar::setStub(string|stream stub [, int len])
2933  * Change the stub in a phar, phar.tar or phar.zip archive to something other
2934  * than the default. The stub *must* end with a call to __HALT_COMPILER().
2935  */
PHP_METHOD(Phar,setStub)2936 PHP_METHOD(Phar, setStub)
2937 {
2938 	zval *zstub;
2939 	char *stub, *error;
2940 	size_t stub_len;
2941 	zend_long len = -1;
2942 	php_stream *stream;
2943 	PHAR_ARCHIVE_OBJECT();
2944 
2945 	if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
2946 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
2947 			"Cannot change stub, phar is read-only");
2948 		return;
2949 	}
2950 
2951 	if (phar_obj->archive->is_data) {
2952 		if (phar_obj->archive->is_tar) {
2953 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
2954 				"A Phar stub cannot be set in a plain tar archive");
2955 		} else {
2956 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
2957 				"A Phar stub cannot be set in a plain zip archive");
2958 		}
2959 		return;
2960 	}
2961 
2962 	if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "r|l", &zstub, &len) == SUCCESS) {
2963 		if ((php_stream_from_zval_no_verify(stream, zstub)) != NULL) {
2964 			if (len > 0) {
2965 				len = -len;
2966 			} else {
2967 				len = -1;
2968 			}
2969 			if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
2970 				zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
2971 				return;
2972 			}
2973 			phar_flush(phar_obj->archive, (char *) zstub, len, 0, &error);
2974 			if (error) {
2975 				zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
2976 				efree(error);
2977 			}
2978 			RETURN_TRUE;
2979 		} else {
2980 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
2981 				"Cannot change stub, unable to read from input stream");
2982 		}
2983 	} else if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &stub, &stub_len) == SUCCESS) {
2984 		if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
2985 			zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
2986 			return;
2987 		}
2988 		phar_flush(phar_obj->archive, stub, stub_len, 0, &error);
2989 
2990 		if (error) {
2991 			zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
2992 			efree(error);
2993 		}
2994 
2995 		RETURN_TRUE;
2996 	}
2997 
2998 	RETURN_FALSE;
2999 }
3000 /* }}} */
3001 
3002 /* {{{ proto bool Phar::setDefaultStub([string index[, string webindex]])
3003  * In a pure phar archive, sets a stub that can be used to run the archive
3004  * regardless of whether the phar extension is available. The first parameter
3005  * is the CLI startup filename, which defaults to "index.php". The second
3006  * parameter is the web startup filename and also defaults to "index.php"
3007  * (falling back to CLI behaviour).
3008  * Both parameters are optional.
3009  * In a phar.zip or phar.tar archive, the default stub is used only to
3010  * identify the archive to the extension as a Phar object. This allows the
3011  * extension to treat phar.zip and phar.tar types as honorary phars. Since
3012  * files cannot be loaded via this kind of stub, no parameters are accepted
3013  * when the Phar object is zip- or tar-based.
3014  */
PHP_METHOD(Phar,setDefaultStub)3015 PHP_METHOD(Phar, setDefaultStub)
3016 {
3017 	char *index = NULL, *webindex = NULL, *error = NULL;
3018 	zend_string *stub = NULL;
3019 	size_t index_len = 0, webindex_len = 0;
3020 	int created_stub = 0;
3021 	PHAR_ARCHIVE_OBJECT();
3022 
3023 	if (phar_obj->archive->is_data) {
3024 		if (phar_obj->archive->is_tar) {
3025 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3026 				"A Phar stub cannot be set in a plain tar archive");
3027 		} else {
3028 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3029 				"A Phar stub cannot be set in a plain zip archive");
3030 		}
3031 		return;
3032 	}
3033 
3034 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s", &index, &index_len, &webindex, &webindex_len) == FAILURE) {
3035 		RETURN_FALSE;
3036 	}
3037 
3038 	if (ZEND_NUM_ARGS() > 0 && (phar_obj->archive->is_tar || phar_obj->archive->is_zip)) {
3039 		php_error_docref(NULL, E_WARNING, "method accepts no arguments for a tar- or zip-based phar stub, %d given", ZEND_NUM_ARGS());
3040 		RETURN_FALSE;
3041 	}
3042 
3043 	if (PHAR_G(readonly)) {
3044 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3045 			"Cannot change stub: phar.readonly=1");
3046 		RETURN_FALSE;
3047 	}
3048 
3049 	if (!phar_obj->archive->is_tar && !phar_obj->archive->is_zip) {
3050 		stub = phar_create_default_stub(index, webindex, &error);
3051 
3052 		if (error) {
3053 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "%s", error);
3054 			efree(error);
3055 			if (stub) {
3056 				zend_string_free(stub);
3057 			}
3058 			RETURN_FALSE;
3059 		}
3060 
3061 		created_stub = 1;
3062 	}
3063 
3064 	if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
3065 		zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
3066 		return;
3067 	}
3068 	phar_flush(phar_obj->archive, stub ? ZSTR_VAL(stub) : 0, stub ? ZSTR_LEN(stub) : 0, 1, &error);
3069 
3070 	if (created_stub) {
3071 		zend_string_free(stub);
3072 	}
3073 
3074 	if (error) {
3075 		zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
3076 		efree(error);
3077 		RETURN_FALSE;
3078 	}
3079 
3080 	RETURN_TRUE;
3081 }
3082 /* }}} */
3083 
3084 /* {{{ proto array Phar::setSignatureAlgorithm(int sigtype[, string privatekey])
3085  * Sets the signature algorithm for a phar and applies it. The signature
3086  * algorithm must be one of Phar::MD5, Phar::SHA1, Phar::SHA256,
3087  * Phar::SHA512, or Phar::OPENSSL. Note that zip- based phar archives
3088  * cannot support signatures.
3089  */
PHP_METHOD(Phar,setSignatureAlgorithm)3090 PHP_METHOD(Phar, setSignatureAlgorithm)
3091 {
3092 	zend_long algo;
3093 	char *error, *key = NULL;
3094 	size_t key_len = 0;
3095 
3096 	PHAR_ARCHIVE_OBJECT();
3097 
3098 	if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
3099 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3100 			"Cannot set signature algorithm, phar is read-only");
3101 		return;
3102 	}
3103 
3104 	if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "l|s", &algo, &key, &key_len) != SUCCESS) {
3105 		return;
3106 	}
3107 	if (ZEND_SIZE_T_INT_OVFL(key_len)) {
3108 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3109 					"Cannot set signature algorithm, key too long");
3110 		return;
3111 	}
3112 
3113 	switch (algo) {
3114 		case PHAR_SIG_SHA256:
3115 		case PHAR_SIG_SHA512:
3116 #ifndef PHAR_HASH_OK
3117 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3118 				"SHA-256 and SHA-512 signatures are only supported if the hash extension is enabled and built non-shared");
3119 			return;
3120 #endif
3121 		case PHAR_SIG_MD5:
3122 		case PHAR_SIG_SHA1:
3123 		case PHAR_SIG_OPENSSL:
3124 			if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
3125 				zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
3126 				return;
3127 			}
3128 			phar_obj->archive->sig_flags = (php_uint32)algo;
3129 			phar_obj->archive->is_modified = 1;
3130 			PHAR_G(openssl_privatekey) = key;
3131 			PHAR_G(openssl_privatekey_len) = (int)key_len;
3132 
3133 			phar_flush(phar_obj->archive, 0, 0, 0, &error);
3134 			if (error) {
3135 				zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
3136 				efree(error);
3137 			}
3138 			break;
3139 		default:
3140 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3141 				"Unknown signature algorithm specified");
3142 	}
3143 }
3144 /* }}} */
3145 
3146 /* {{{ proto array|false Phar::getSignature()
3147  * Returns a hash signature, or FALSE if the archive is unsigned.
3148  */
PHP_METHOD(Phar,getSignature)3149 PHP_METHOD(Phar, getSignature)
3150 {
3151 	PHAR_ARCHIVE_OBJECT();
3152 
3153 	if (zend_parse_parameters_none() == FAILURE) {
3154 		return;
3155 	}
3156 
3157 	if (phar_obj->archive->signature) {
3158 		zend_string *unknown;
3159 
3160 		array_init(return_value);
3161 		add_assoc_stringl(return_value, "hash", phar_obj->archive->signature, phar_obj->archive->sig_len);
3162 		switch(phar_obj->archive->sig_flags) {
3163 			case PHAR_SIG_MD5:
3164 				add_assoc_stringl(return_value, "hash_type", "MD5", 3);
3165 				break;
3166 			case PHAR_SIG_SHA1:
3167 				add_assoc_stringl(return_value, "hash_type", "SHA-1", 5);
3168 				break;
3169 			case PHAR_SIG_SHA256:
3170 				add_assoc_stringl(return_value, "hash_type", "SHA-256", 7);
3171 				break;
3172 			case PHAR_SIG_SHA512:
3173 				add_assoc_stringl(return_value, "hash_type", "SHA-512", 7);
3174 				break;
3175 			case PHAR_SIG_OPENSSL:
3176 				add_assoc_stringl(return_value, "hash_type", "OpenSSL", 7);
3177 				break;
3178 			default:
3179 				unknown = strpprintf(0, "Unknown (%u)", phar_obj->archive->sig_flags);
3180 				add_assoc_str(return_value, "hash_type", unknown);
3181 				break;
3182 		}
3183 	} else {
3184 		RETURN_FALSE;
3185 	}
3186 }
3187 /* }}} */
3188 
3189 /* {{{ proto bool Phar::getModified()
3190  * Return whether phar was modified
3191  */
PHP_METHOD(Phar,getModified)3192 PHP_METHOD(Phar, getModified)
3193 {
3194 	PHAR_ARCHIVE_OBJECT();
3195 
3196 	if (zend_parse_parameters_none() == FAILURE) {
3197 		return;
3198 	}
3199 
3200 	RETURN_BOOL(phar_obj->archive->is_modified);
3201 }
3202 /* }}} */
3203 
phar_set_compression(zval * zv,void * argument)3204 static int phar_set_compression(zval *zv, void *argument) /* {{{ */
3205 {
3206 	phar_entry_info *entry = (phar_entry_info *)Z_PTR_P(zv);
3207 	uint32_t compress = *(uint32_t *)argument;
3208 
3209 	if (entry->is_deleted) {
3210 		return ZEND_HASH_APPLY_KEEP;
3211 	}
3212 
3213 	entry->old_flags = entry->flags;
3214 	entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
3215 	entry->flags |= compress;
3216 	entry->is_modified = 1;
3217 	return ZEND_HASH_APPLY_KEEP;
3218 }
3219 /* }}} */
3220 
phar_test_compression(zval * zv,void * argument)3221 static int phar_test_compression(zval *zv, void *argument) /* {{{ */
3222 {
3223 	phar_entry_info *entry = (phar_entry_info *)Z_PTR_P(zv);
3224 
3225 	if (entry->is_deleted) {
3226 		return ZEND_HASH_APPLY_KEEP;
3227 	}
3228 
3229 	if (!PHAR_G(has_bz2)) {
3230 		if (entry->flags & PHAR_ENT_COMPRESSED_BZ2) {
3231 			*(int *) argument = 0;
3232 		}
3233 	}
3234 
3235 	if (!PHAR_G(has_zlib)) {
3236 		if (entry->flags & PHAR_ENT_COMPRESSED_GZ) {
3237 			*(int *) argument = 0;
3238 		}
3239 	}
3240 
3241 	return ZEND_HASH_APPLY_KEEP;
3242 }
3243 /* }}} */
3244 
pharobj_set_compression(HashTable * manifest,uint32_t compress)3245 static void pharobj_set_compression(HashTable *manifest, uint32_t compress) /* {{{ */
3246 {
3247 	zend_hash_apply_with_argument(manifest, phar_set_compression, &compress);
3248 }
3249 /* }}} */
3250 
pharobj_cancompress(HashTable * manifest)3251 static int pharobj_cancompress(HashTable *manifest) /* {{{ */
3252 {
3253 	int test;
3254 
3255 	test = 1;
3256 	zend_hash_apply_with_argument(manifest, phar_test_compression, &test);
3257 	return test;
3258 }
3259 /* }}} */
3260 
3261 /* {{{ proto object Phar::compress(int method[, string extension])
3262  * Compress a .tar, or .phar.tar with whole-file compression
3263  * The parameter can be one of Phar::GZ or Phar::BZ2 to specify
3264  * the kind of compression desired
3265  */
PHP_METHOD(Phar,compress)3266 PHP_METHOD(Phar, compress)
3267 {
3268 	zend_long method;
3269 	char *ext = NULL;
3270 	size_t ext_len = 0;
3271 	uint32_t flags;
3272 	zend_object *ret;
3273 	PHAR_ARCHIVE_OBJECT();
3274 
3275 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|s", &method, &ext, &ext_len) == FAILURE) {
3276 		return;
3277 	}
3278 
3279 	if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
3280 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3281 			"Cannot compress phar archive, phar is read-only");
3282 		return;
3283 	}
3284 
3285 	if (phar_obj->archive->is_zip) {
3286 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3287 			"Cannot compress zip-based archives with whole-archive compression");
3288 		return;
3289 	}
3290 
3291 	switch (method) {
3292 		case 0:
3293 			flags = PHAR_FILE_COMPRESSED_NONE;
3294 			break;
3295 		case PHAR_ENT_COMPRESSED_GZ:
3296 			if (!PHAR_G(has_zlib)) {
3297 				zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
3298 					"Cannot compress entire archive with gzip, enable ext/zlib in php.ini");
3299 				return;
3300 			}
3301 			flags = PHAR_FILE_COMPRESSED_GZ;
3302 			break;
3303 
3304 		case PHAR_ENT_COMPRESSED_BZ2:
3305 			if (!PHAR_G(has_bz2)) {
3306 				zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
3307 					"Cannot compress entire archive with bz2, enable ext/bz2 in php.ini");
3308 				return;
3309 			}
3310 			flags = PHAR_FILE_COMPRESSED_BZ2;
3311 			break;
3312 		default:
3313 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
3314 				"Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
3315 			return;
3316 	}
3317 
3318 	if (phar_obj->archive->is_tar) {
3319 		ret = phar_convert_to_other(phar_obj->archive, PHAR_FORMAT_TAR, ext, flags);
3320 	} else {
3321 		ret = phar_convert_to_other(phar_obj->archive, PHAR_FORMAT_PHAR, ext, flags);
3322 	}
3323 
3324 	if (ret) {
3325 		ZVAL_OBJ(return_value, ret);
3326 	} else {
3327 		RETURN_NULL();
3328 	}
3329 }
3330 /* }}} */
3331 
3332 /* {{{ proto object Phar::decompress([string extension])
3333  * Decompress a .tar, or .phar.tar with whole-file compression
3334  */
PHP_METHOD(Phar,decompress)3335 PHP_METHOD(Phar, decompress)
3336 {
3337 	char *ext = NULL;
3338 	size_t ext_len = 0;
3339 	zend_object *ret;
3340 	PHAR_ARCHIVE_OBJECT();
3341 
3342 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &ext, &ext_len) == FAILURE) {
3343 		return;
3344 	}
3345 
3346 	if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
3347 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3348 			"Cannot decompress phar archive, phar is read-only");
3349 		return;
3350 	}
3351 
3352 	if (phar_obj->archive->is_zip) {
3353 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3354 			"Cannot decompress zip-based archives with whole-archive compression");
3355 		return;
3356 	}
3357 
3358 	if (phar_obj->archive->is_tar) {
3359 		ret = phar_convert_to_other(phar_obj->archive, PHAR_FORMAT_TAR, ext, PHAR_FILE_COMPRESSED_NONE);
3360 	} else {
3361 		ret = phar_convert_to_other(phar_obj->archive, PHAR_FORMAT_PHAR, ext, PHAR_FILE_COMPRESSED_NONE);
3362 	}
3363 
3364 	if (ret) {
3365 		ZVAL_OBJ(return_value, ret);
3366 	} else {
3367 		RETURN_NULL();
3368 	}
3369 }
3370 /* }}} */
3371 
3372 /* {{{ proto object Phar::compressFiles(int method)
3373  * Compress all files within a phar or zip archive using the specified compression
3374  * The parameter can be one of Phar::GZ or Phar::BZ2 to specify
3375  * the kind of compression desired
3376  */
PHP_METHOD(Phar,compressFiles)3377 PHP_METHOD(Phar, compressFiles)
3378 {
3379 	char *error;
3380 	uint32_t flags;
3381 	zend_long method;
3382 	PHAR_ARCHIVE_OBJECT();
3383 
3384 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &method) == FAILURE) {
3385 		return;
3386 	}
3387 
3388 	if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
3389 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
3390 			"Phar is readonly, cannot change compression");
3391 		return;
3392 	}
3393 
3394 	switch (method) {
3395 		case PHAR_ENT_COMPRESSED_GZ:
3396 			if (!PHAR_G(has_zlib)) {
3397 				zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
3398 					"Cannot compress files within archive with gzip, enable ext/zlib in php.ini");
3399 				return;
3400 			}
3401 			flags = PHAR_ENT_COMPRESSED_GZ;
3402 			break;
3403 
3404 		case PHAR_ENT_COMPRESSED_BZ2:
3405 			if (!PHAR_G(has_bz2)) {
3406 				zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
3407 					"Cannot compress files within archive with bz2, enable ext/bz2 in php.ini");
3408 				return;
3409 			}
3410 			flags = PHAR_ENT_COMPRESSED_BZ2;
3411 			break;
3412 		default:
3413 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
3414 				"Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
3415 			return;
3416 	}
3417 
3418 	if (phar_obj->archive->is_tar) {
3419 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
3420 			"Cannot compress with Gzip compression, tar archives cannot compress individual files, use compress() to compress the whole archive");
3421 		return;
3422 	}
3423 
3424 	if (!pharobj_cancompress(&phar_obj->archive->manifest)) {
3425 		if (flags == PHAR_FILE_COMPRESSED_GZ) {
3426 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
3427 				"Cannot compress all files as Gzip, some are compressed as bzip2 and cannot be decompressed");
3428 		} else {
3429 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
3430 				"Cannot compress all files as Bzip2, some are compressed as gzip and cannot be decompressed");
3431 		}
3432 		return;
3433 	}
3434 
3435 	if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
3436 		zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
3437 		return;
3438 	}
3439 	pharobj_set_compression(&phar_obj->archive->manifest, flags);
3440 	phar_obj->archive->is_modified = 1;
3441 	phar_flush(phar_obj->archive, 0, 0, 0, &error);
3442 
3443 	if (error) {
3444 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "%s", error);
3445 		efree(error);
3446 	}
3447 }
3448 /* }}} */
3449 
3450 /* {{{ proto bool Phar::decompressFiles()
3451  * decompress every file
3452  */
PHP_METHOD(Phar,decompressFiles)3453 PHP_METHOD(Phar, decompressFiles)
3454 {
3455 	char *error;
3456 	PHAR_ARCHIVE_OBJECT();
3457 
3458 	if (zend_parse_parameters_none() == FAILURE) {
3459 		return;
3460 	}
3461 
3462 	if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
3463 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
3464 			"Phar is readonly, cannot change compression");
3465 		return;
3466 	}
3467 
3468 	if (!pharobj_cancompress(&phar_obj->archive->manifest)) {
3469 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
3470 			"Cannot decompress all files, some are compressed as bzip2 or gzip and cannot be decompressed");
3471 		return;
3472 	}
3473 
3474 	if (phar_obj->archive->is_tar) {
3475 		RETURN_TRUE;
3476 	} else {
3477 		if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
3478 			zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
3479 			return;
3480 		}
3481 		pharobj_set_compression(&phar_obj->archive->manifest, PHAR_ENT_COMPRESSED_NONE);
3482 	}
3483 
3484 	phar_obj->archive->is_modified = 1;
3485 	phar_flush(phar_obj->archive, 0, 0, 0, &error);
3486 
3487 	if (error) {
3488 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "%s", error);
3489 		efree(error);
3490 	}
3491 
3492 	RETURN_TRUE;
3493 }
3494 /* }}} */
3495 
3496 /* {{{ proto bool Phar::copy(string oldfile, string newfile)
3497  * copy a file internal to the phar archive to another new file within the phar
3498  */
PHP_METHOD(Phar,copy)3499 PHP_METHOD(Phar, copy)
3500 {
3501 	char *oldfile, *newfile, *error;
3502 	const char *pcr_error;
3503 	size_t oldfile_len, newfile_len;
3504 	phar_entry_info *oldentry, newentry = {0}, *temp;
3505 	int tmp_len = 0;
3506 
3507 	PHAR_ARCHIVE_OBJECT();
3508 
3509 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "pp", &oldfile, &oldfile_len, &newfile, &newfile_len) == FAILURE) {
3510 		return;
3511 	}
3512 	if (ZEND_SIZE_T_INT_OVFL(newfile_len)) {
3513 		RETURN_FALSE;
3514 	}
3515 	if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
3516 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3517 			"Cannot copy \"%s\" to \"%s\", phar is read-only", oldfile, newfile);
3518 		RETURN_FALSE;
3519 	}
3520 
3521 	if (oldfile_len >= sizeof(".phar")-1 && !memcmp(oldfile, ".phar", sizeof(".phar")-1)) {
3522 		/* can't copy a meta file */
3523 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3524 			"file \"%s\" cannot be copied to file \"%s\", cannot copy Phar meta-file in %s", oldfile, newfile, phar_obj->archive->fname);
3525 		RETURN_FALSE;
3526 	}
3527 
3528 	if (newfile_len >= sizeof(".phar")-1 && !memcmp(newfile, ".phar", sizeof(".phar")-1)) {
3529 		/* can't copy a meta file */
3530 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3531 			"file \"%s\" cannot be copied to file \"%s\", cannot copy to Phar meta-file in %s", oldfile, newfile, phar_obj->archive->fname);
3532 		RETURN_FALSE;
3533 	}
3534 
3535 	if (!zend_hash_str_exists(&phar_obj->archive->manifest, oldfile, (uint32_t) oldfile_len) || NULL == (oldentry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, oldfile, (uint32_t) oldfile_len)) || oldentry->is_deleted) {
3536 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3537 			"file \"%s\" cannot be copied to file \"%s\", file does not exist in %s", oldfile, newfile, phar_obj->archive->fname);
3538 		RETURN_FALSE;
3539 	}
3540 
3541 	if (zend_hash_str_exists(&phar_obj->archive->manifest, newfile, (uint32_t) newfile_len)) {
3542 		if (NULL != (temp = zend_hash_str_find_ptr(&phar_obj->archive->manifest, newfile, (uint32_t) newfile_len)) || !temp->is_deleted) {
3543 			zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3544 				"file \"%s\" cannot be copied to file \"%s\", file must not already exist in phar %s", oldfile, newfile, phar_obj->archive->fname);
3545 			RETURN_FALSE;
3546 		}
3547 	}
3548 
3549 	tmp_len = (int)newfile_len;
3550 	if (phar_path_check(&newfile, &tmp_len, &pcr_error) > pcr_is_ok) {
3551 		zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3552 				"file \"%s\" contains invalid characters %s, cannot be copied from \"%s\" in phar %s", newfile, pcr_error, oldfile, phar_obj->archive->fname);
3553 		RETURN_FALSE;
3554 	}
3555 	newfile_len = tmp_len;
3556 
3557 	if (phar_obj->archive->is_persistent) {
3558 		if (FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
3559 			zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
3560 			return;
3561 		}
3562 		/* re-populate with copied-on-write entry */
3563 		oldentry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, oldfile, (uint32_t) oldfile_len);
3564 	}
3565 
3566 	memcpy((void *) &newentry, oldentry, sizeof(phar_entry_info));
3567 
3568 	if (Z_TYPE(newentry.metadata) != IS_UNDEF) {
3569 		zval_copy_ctor(&newentry.metadata);
3570 		newentry.metadata_str.s = NULL;
3571 	}
3572 
3573 	newentry.filename = estrndup(newfile, newfile_len);
3574 	newentry.filename_len = (int)newfile_len;
3575 	newentry.fp_refcount = 0;
3576 
3577 	if (oldentry->fp_type != PHAR_FP) {
3578 		if (FAILURE == phar_copy_entry_fp(oldentry, &newentry, &error)) {
3579 			efree(newentry.filename);
3580 			php_stream_close(newentry.fp);
3581 			zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
3582 			efree(error);
3583 			return;
3584 		}
3585 	}
3586 
3587 	zend_hash_str_add_mem(&oldentry->phar->manifest, newfile, newfile_len, &newentry, sizeof(phar_entry_info));
3588 	phar_obj->archive->is_modified = 1;
3589 	phar_flush(phar_obj->archive, 0, 0, 0, &error);
3590 
3591 	if (error) {
3592 		zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
3593 		efree(error);
3594 	}
3595 
3596 	RETURN_TRUE;
3597 }
3598 /* }}} */
3599 
3600 /* {{{ proto int Phar::offsetExists(string entry)
3601  * determines whether a file exists in the phar
3602  */
PHP_METHOD(Phar,offsetExists)3603 PHP_METHOD(Phar, offsetExists)
3604 {
3605 	char *fname;
3606 	size_t fname_len;
3607 	phar_entry_info *entry;
3608 
3609 	PHAR_ARCHIVE_OBJECT();
3610 
3611 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
3612 		return;
3613 	}
3614 	if (ZEND_SIZE_T_INT_OVFL(fname_len)) {
3615 		RETURN_FALSE;
3616 	}
3617 
3618 	if (zend_hash_str_exists(&phar_obj->archive->manifest, fname, (uint32_t) fname_len)) {
3619 		if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, (uint32_t) fname_len))) {
3620 			if (entry->is_deleted) {
3621 				/* entry is deleted, but has not been flushed to disk yet */
3622 				RETURN_FALSE;
3623 			}
3624 		}
3625 
3626 		if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) {
3627 			/* none of these are real files, so they don't exist */
3628 			RETURN_FALSE;
3629 		}
3630 		RETURN_TRUE;
3631 	} else {
3632 		if (zend_hash_str_exists(&phar_obj->archive->virtual_dirs, fname, (uint32_t) fname_len)) {
3633 			RETURN_TRUE;
3634 		}
3635 		RETURN_FALSE;
3636 	}
3637 }
3638 /* }}} */
3639 
3640 /* {{{ proto int Phar::offsetGet(string entry)
3641  * get a PharFileInfo object for a specific file
3642  */
PHP_METHOD(Phar,offsetGet)3643 PHP_METHOD(Phar, offsetGet)
3644 {
3645 	char *fname, *error;
3646 	size_t fname_len;
3647 	zval zfname;
3648 	phar_entry_info *entry;
3649 	zend_string *sfname;
3650 	PHAR_ARCHIVE_OBJECT();
3651 
3652 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
3653 		return;
3654 	}
3655 
3656 	if (ZEND_SIZE_T_INT_OVFL(fname_len)) {
3657 		RETURN_FALSE;
3658 	}
3659 
3660 	/* security is 0 here so that we can get a better error message than "entry doesn't exist" */
3661 	if (!(entry = phar_get_entry_info_dir(phar_obj->archive, fname, (int)fname_len, 1, &error, 0))) {
3662 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist%s%s", fname, error?", ":"", error?error:"");
3663 	} else {
3664 		if (fname_len == sizeof(".phar/stub.php")-1 && !memcmp(fname, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
3665 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot get stub \".phar/stub.php\" directly in phar \"%s\", use getStub", phar_obj->archive->fname);
3666 			return;
3667 		}
3668 
3669 		if (fname_len == sizeof(".phar/alias.txt")-1 && !memcmp(fname, ".phar/alias.txt", sizeof(".phar/alias.txt")-1)) {
3670 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot get alias \".phar/alias.txt\" directly in phar \"%s\", use getAlias", phar_obj->archive->fname);
3671 			return;
3672 		}
3673 
3674 		if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) {
3675 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot directly get any files or directories in magic \".phar\" directory");
3676 			return;
3677 		}
3678 
3679 		if (entry->is_temp_dir) {
3680 			efree(entry->filename);
3681 			efree(entry);
3682 		}
3683 
3684 		sfname = strpprintf(0, "phar://%s/%s", phar_obj->archive->fname, fname);
3685 		ZVAL_NEW_STR(&zfname, sfname);
3686 		spl_instantiate_arg_ex1(phar_obj->spl.info_class, return_value, &zfname);
3687 		zval_ptr_dtor(&zfname);
3688 	}
3689 }
3690 /* }}} */
3691 
3692 /* {{{ add a file within the phar archive from a string or resource
3693  */
phar_add_file(phar_archive_data ** pphar,char * filename,int filename_len,char * cont_str,size_t cont_len,zval * zresource)3694 static void phar_add_file(phar_archive_data **pphar, char *filename, int filename_len, char *cont_str, size_t cont_len, zval *zresource)
3695 {
3696 	int start_pos=0;
3697 	char *error;
3698 	size_t contents_len;
3699 	phar_entry_data *data;
3700 	php_stream *contents_file = NULL;
3701 	php_stream_statbuf ssb;
3702 
3703 	if (filename_len >= (int)sizeof(".phar")-1) {
3704 		start_pos = ('/' == filename[0] ? 1 : 0); /* account for any leading slash: multiple-leads handled elsewhere */
3705 		if (!memcmp(&filename[start_pos], ".phar", sizeof(".phar")-1) && (filename[start_pos+5] == '/' || filename[start_pos+5] == '\\' || filename[start_pos+5] == '\0')) {
3706 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create any files in magic \".phar\" directory");
3707 			return;
3708 		}
3709 	}
3710 
3711 	if (!(data = phar_get_or_create_entry_data((*pphar)->fname, (*pphar)->fname_len, filename, filename_len, "w+b", 0, &error, 1))) {
3712 		if (error) {
3713 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist and cannot be created: %s", filename, error);
3714 			efree(error);
3715 		} else {
3716 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist and cannot be created", filename);
3717 		}
3718 		return;
3719 	} else {
3720 		if (error) {
3721 			efree(error);
3722 		}
3723 
3724 		if (!data->internal_file->is_dir) {
3725 			if (cont_str) {
3726 				contents_len = php_stream_write(data->fp, cont_str, cont_len);
3727 				if (contents_len != cont_len) {
3728 					zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s could not be written to", filename);
3729 					return;
3730 				}
3731 			} else {
3732 				if (!(php_stream_from_zval_no_verify(contents_file, zresource))) {
3733 					zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s could not be written to", filename);
3734 					return;
3735 				}
3736 				php_stream_copy_to_stream_ex(contents_file, data->fp, PHP_STREAM_COPY_ALL, &contents_len);
3737 			}
3738 			data->internal_file->compressed_filesize = data->internal_file->uncompressed_filesize = contents_len;
3739 		}
3740 
3741 		if (contents_file != NULL && php_stream_stat(contents_file, &ssb) != -1) {
3742 			data->internal_file->flags = ssb.sb.st_mode & PHAR_ENT_PERM_MASK ;
3743 		} else {
3744 #ifndef _WIN32
3745 			mode_t mask;
3746 			mask = umask(0);
3747 			umask(mask);
3748 			data->internal_file->flags &= ~mask;
3749 #endif
3750 		}
3751 
3752 		/* check for copy-on-write */
3753 		if (pphar[0] != data->phar) {
3754 			*pphar = data->phar;
3755 		}
3756 		phar_entry_delref(data);
3757 		phar_flush(*pphar, 0, 0, 0, &error);
3758 
3759 		if (error) {
3760 			zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
3761 			efree(error);
3762 		}
3763 	}
3764 }
3765 /* }}} */
3766 
3767 /* {{{ create a directory within the phar archive
3768  */
phar_mkdir(phar_archive_data ** pphar,char * dirname,int dirname_len)3769 static void phar_mkdir(phar_archive_data **pphar, char *dirname, int dirname_len)
3770 {
3771 	char *error;
3772 	phar_entry_data *data;
3773 
3774 	if (!(data = phar_get_or_create_entry_data((*pphar)->fname, (*pphar)->fname_len, dirname, dirname_len, "w+b", 2, &error, 1))) {
3775 		if (error) {
3776 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Directory %s does not exist and cannot be created: %s", dirname, error);
3777 			efree(error);
3778 		} else {
3779 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Directory %s does not exist and cannot be created", dirname);
3780 		}
3781 
3782 		return;
3783 	} else {
3784 		if (error) {
3785 			efree(error);
3786 		}
3787 
3788 		/* check for copy on write */
3789 		if (data->phar != *pphar) {
3790 			*pphar = data->phar;
3791 		}
3792 		phar_entry_delref(data);
3793 		phar_flush(*pphar, 0, 0, 0, &error);
3794 
3795 		if (error) {
3796 			zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
3797 			efree(error);
3798 		}
3799 	}
3800 }
3801 /* }}} */
3802 
3803 /* {{{ proto int Phar::offsetSet(string entry, string value)
3804  * set the contents of an internal file to those of an external file
3805  */
PHP_METHOD(Phar,offsetSet)3806 PHP_METHOD(Phar, offsetSet)
3807 {
3808 	char *fname, *cont_str = NULL;
3809 	size_t fname_len, cont_len;
3810 	zval *zresource;
3811 	PHAR_ARCHIVE_OBJECT();
3812 
3813 	if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
3814 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
3815 		return;
3816 	}
3817 
3818 	if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "pr", &fname, &fname_len, &zresource) == FAILURE
3819 	&& zend_parse_parameters(ZEND_NUM_ARGS(), "ps", &fname, &fname_len, &cont_str, &cont_len) == FAILURE) {
3820 		return;
3821 	}
3822 	if (ZEND_SIZE_T_INT_OVFL(fname_len)) {
3823 		RETURN_FALSE;
3824 	}
3825 	if (fname_len == sizeof(".phar/stub.php")-1 && !memcmp(fname, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
3826 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot set stub \".phar/stub.php\" directly in phar \"%s\", use setStub", phar_obj->archive->fname);
3827 		return;
3828 	}
3829 
3830 	if (fname_len == sizeof(".phar/alias.txt")-1 && !memcmp(fname, ".phar/alias.txt", sizeof(".phar/alias.txt")-1)) {
3831 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot set alias \".phar/alias.txt\" directly in phar \"%s\", use setAlias", phar_obj->archive->fname);
3832 		return;
3833 	}
3834 
3835 	if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) {
3836 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot set any files or directories in magic \".phar\" directory");
3837 		return;
3838 	}
3839 
3840 	phar_add_file(&(phar_obj->archive), fname, (int)fname_len, cont_str, cont_len, zresource);
3841 }
3842 /* }}} */
3843 
3844 /* {{{ proto int Phar::offsetUnset(string entry)
3845  * remove a file from a phar
3846  */
PHP_METHOD(Phar,offsetUnset)3847 PHP_METHOD(Phar, offsetUnset)
3848 {
3849 	char *fname, *error;
3850 	size_t fname_len;
3851 	phar_entry_info *entry;
3852 	PHAR_ARCHIVE_OBJECT();
3853 
3854 	if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
3855 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
3856 		return;
3857 	}
3858 
3859 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
3860 		return;
3861 	}
3862 	if (ZEND_SIZE_T_INT_OVFL(fname_len)) {
3863 		RETURN_FALSE;
3864 	}
3865 
3866 	if (zend_hash_str_exists(&phar_obj->archive->manifest, fname, (uint32_t) fname_len)) {
3867 		if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, (uint32_t) fname_len))) {
3868 			if (entry->is_deleted) {
3869 				/* entry is deleted, but has not been flushed to disk yet */
3870 				return;
3871 			}
3872 
3873 			if (phar_obj->archive->is_persistent) {
3874 				if (FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
3875 					zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
3876 					return;
3877 				}
3878 				/* re-populate entry after copy on write */
3879 				entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, (uint32_t) fname_len);
3880 			}
3881 			entry->is_modified = 0;
3882 			entry->is_deleted = 1;
3883 			/* we need to "flush" the stream to save the newly deleted file on disk */
3884 			phar_flush(phar_obj->archive, 0, 0, 0, &error);
3885 
3886 			if (error) {
3887 				zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
3888 				efree(error);
3889 			}
3890 
3891 			RETURN_TRUE;
3892 		}
3893 	} else {
3894 		RETURN_FALSE;
3895 	}
3896 }
3897 /* }}} */
3898 
3899 /* {{{ proto string Phar::addEmptyDir(string dirname)
3900  * Adds an empty directory to the phar archive
3901  */
PHP_METHOD(Phar,addEmptyDir)3902 PHP_METHOD(Phar, addEmptyDir)
3903 {
3904 	char *dirname;
3905 	size_t dirname_len;
3906 
3907 	PHAR_ARCHIVE_OBJECT();
3908 
3909 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &dirname, &dirname_len) == FAILURE) {
3910 		return;
3911 	}
3912 	if (ZEND_SIZE_T_INT_OVFL(dirname_len)) {
3913 		RETURN_FALSE;
3914 	}
3915 
3916 	if (dirname_len >= sizeof(".phar")-1 && !memcmp(dirname, ".phar", sizeof(".phar")-1)) {
3917 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create a directory in magic \".phar\" directory");
3918 		return;
3919 	}
3920 
3921 	phar_mkdir(&phar_obj->archive, dirname, (int)dirname_len);
3922 }
3923 /* }}} */
3924 
3925 /* {{{ proto string Phar::addFile(string filename[, string localname])
3926  * Adds a file to the archive using the filename, or the second parameter as the name within the archive
3927  */
PHP_METHOD(Phar,addFile)3928 PHP_METHOD(Phar, addFile)
3929 {
3930 	char *fname, *localname = NULL;
3931 	size_t fname_len, localname_len = 0;
3932 	php_stream *resource;
3933 	zval zresource;
3934 
3935 	PHAR_ARCHIVE_OBJECT();
3936 
3937 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|s", &fname, &fname_len, &localname, &localname_len) == FAILURE) {
3938 		return;
3939 	}
3940 	if (ZEND_SIZE_T_INT_OVFL(fname_len)) {
3941 		RETURN_FALSE;
3942 	}
3943 
3944 	if (!strstr(fname, "://") && php_check_open_basedir(fname)) {
3945 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "phar error: unable to open file \"%s\" to add to phar archive, open_basedir restrictions prevent this", fname);
3946 		return;
3947 	}
3948 
3949 	if (!(resource = php_stream_open_wrapper(fname, "rb", 0, NULL))) {
3950 		zend_throw_exception_ex(spl_ce_RuntimeException, 0, "phar error: unable to open file \"%s\" to add to phar archive", fname);
3951 		return;
3952 	}
3953 
3954 	if (localname) {
3955 		fname = localname;
3956 		fname_len = localname_len;
3957 	}
3958 
3959 	php_stream_to_zval(resource, &zresource);
3960 	phar_add_file(&(phar_obj->archive), fname, (int)fname_len, NULL, 0, &zresource);
3961 	zval_ptr_dtor(&zresource);
3962 }
3963 /* }}} */
3964 
3965 /* {{{ proto string Phar::addFromString(string localname, string contents)
3966  * Adds a file to the archive using its contents as a string
3967  */
PHP_METHOD(Phar,addFromString)3968 PHP_METHOD(Phar, addFromString)
3969 {
3970 	char *localname, *cont_str;
3971 	size_t localname_len, cont_len;
3972 
3973 	PHAR_ARCHIVE_OBJECT();
3974 
3975 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ps", &localname, &localname_len, &cont_str, &cont_len) == FAILURE) {
3976 		return;
3977 	}
3978 	if (ZEND_SIZE_T_INT_OVFL(localname_len)) {
3979 		RETURN_FALSE;
3980 	}
3981 
3982 	phar_add_file(&(phar_obj->archive), localname, (int)localname_len, cont_str, cont_len, NULL);
3983 }
3984 /* }}} */
3985 
3986 /* {{{ proto string Phar::getStub()
3987  * Returns the stub at the head of a phar archive as a string.
3988  */
PHP_METHOD(Phar,getStub)3989 PHP_METHOD(Phar, getStub)
3990 {
3991 	size_t len;
3992 	zend_string *buf;
3993 	php_stream *fp;
3994 	php_stream_filter *filter = NULL;
3995 	phar_entry_info *stub;
3996 
3997 	PHAR_ARCHIVE_OBJECT();
3998 
3999 	if (zend_parse_parameters_none() == FAILURE) {
4000 		return;
4001 	}
4002 
4003 	if (phar_obj->archive->is_tar || phar_obj->archive->is_zip) {
4004 
4005 		if (NULL != (stub = zend_hash_str_find_ptr(&(phar_obj->archive->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1))) {
4006 			if (phar_obj->archive->fp && !phar_obj->archive->is_brandnew && !(stub->flags & PHAR_ENT_COMPRESSION_MASK)) {
4007 				fp = phar_obj->archive->fp;
4008 			} else {
4009 				if (!(fp = php_stream_open_wrapper(phar_obj->archive->fname, "rb", 0, NULL))) {
4010 					zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "phar error: unable to open phar \"%s\"", phar_obj->archive->fname);
4011 					return;
4012 				}
4013 				if (stub->flags & PHAR_ENT_COMPRESSION_MASK) {
4014 					char *filter_name;
4015 
4016 					if ((filter_name = phar_decompress_filter(stub, 0)) != NULL) {
4017 						filter = php_stream_filter_create(filter_name, NULL, php_stream_is_persistent(fp));
4018 					} else {
4019 						filter = NULL;
4020 					}
4021 					if (!filter) {
4022 						zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "phar error: unable to read stub of phar \"%s\" (cannot create %s filter)", phar_obj->archive->fname, phar_decompress_filter(stub, 1));
4023 						return;
4024 					}
4025 					php_stream_filter_append(&fp->readfilters, filter);
4026 				}
4027 			}
4028 
4029 			if (!fp)  {
4030 				zend_throw_exception_ex(spl_ce_RuntimeException, 0,
4031 					"Unable to read stub");
4032 				return;
4033 			}
4034 
4035 			php_stream_seek(fp, stub->offset_abs, SEEK_SET);
4036 			len = stub->uncompressed_filesize;
4037 			goto carry_on;
4038 		} else {
4039 			RETURN_EMPTY_STRING();
4040 		}
4041 	}
4042 	len = phar_obj->archive->halt_offset;
4043 
4044 	if (phar_obj->archive->fp && !phar_obj->archive->is_brandnew) {
4045 		fp = phar_obj->archive->fp;
4046 	} else {
4047 		fp = php_stream_open_wrapper(phar_obj->archive->fname, "rb", 0, NULL);
4048 	}
4049 
4050 	if (!fp)  {
4051 		zend_throw_exception_ex(spl_ce_RuntimeException, 0,
4052 			"Unable to read stub");
4053 		return;
4054 	}
4055 
4056 	php_stream_rewind(fp);
4057 carry_on:
4058 	buf = zend_string_alloc(len, 0);
4059 
4060 	if (len != php_stream_read(fp, ZSTR_VAL(buf), len)) {
4061 		if (fp != phar_obj->archive->fp) {
4062 			php_stream_close(fp);
4063 		}
4064 		zend_throw_exception_ex(spl_ce_RuntimeException, 0,
4065 			"Unable to read stub");
4066 		zend_string_release(buf);
4067 		return;
4068 	}
4069 
4070 	if (filter) {
4071 		php_stream_filter_flush(filter, 1);
4072 		php_stream_filter_remove(filter, 1);
4073 	}
4074 
4075 	if (fp != phar_obj->archive->fp) {
4076 		php_stream_close(fp);
4077 	}
4078 
4079 	ZSTR_VAL(buf)[len] = '\0';
4080 	ZSTR_LEN(buf) = len;
4081 	RETVAL_STR(buf);
4082 }
4083 /* }}}*/
4084 
4085 /* {{{ proto int Phar::hasMetaData()
4086  * Returns TRUE if the phar has global metadata, FALSE otherwise.
4087  */
PHP_METHOD(Phar,hasMetadata)4088 PHP_METHOD(Phar, hasMetadata)
4089 {
4090 	PHAR_ARCHIVE_OBJECT();
4091 
4092 	RETURN_BOOL(Z_TYPE(phar_obj->archive->metadata) != IS_UNDEF);
4093 }
4094 /* }}} */
4095 
4096 /* {{{ proto int Phar::getMetaData()
4097  * Returns the global metadata of the phar
4098  */
PHP_METHOD(Phar,getMetadata)4099 PHP_METHOD(Phar, getMetadata)
4100 {
4101 	PHAR_ARCHIVE_OBJECT();
4102 
4103 	if (zend_parse_parameters_none() == FAILURE) {
4104 		return;
4105 	}
4106 
4107 	if (Z_TYPE(phar_obj->archive->metadata) != IS_UNDEF) {
4108 		if (phar_obj->archive->is_persistent) {
4109 			char *buf = estrndup((char *) Z_PTR(phar_obj->archive->metadata), phar_obj->archive->metadata_len);
4110 			/* assume success, we would have failed before */
4111 			phar_parse_metadata(&buf, return_value, phar_obj->archive->metadata_len);
4112 			efree(buf);
4113 		} else {
4114 			ZVAL_COPY(return_value, &phar_obj->archive->metadata);
4115 		}
4116 	}
4117 }
4118 /* }}} */
4119 
4120 /* {{{ proto int Phar::setMetaData(mixed $metadata)
4121  * Sets the global metadata of the phar
4122  */
PHP_METHOD(Phar,setMetadata)4123 PHP_METHOD(Phar, setMetadata)
4124 {
4125 	char *error;
4126 	zval *metadata;
4127 
4128 	PHAR_ARCHIVE_OBJECT();
4129 
4130 	if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
4131 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
4132 		return;
4133 	}
4134 
4135 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &metadata) == FAILURE) {
4136 		return;
4137 	}
4138 
4139 	if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
4140 		zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
4141 		return;
4142 	}
4143 	if (Z_TYPE(phar_obj->archive->metadata) != IS_UNDEF) {
4144 		zval_ptr_dtor(&phar_obj->archive->metadata);
4145 		ZVAL_UNDEF(&phar_obj->archive->metadata);
4146 	}
4147 
4148 	ZVAL_COPY(&phar_obj->archive->metadata, metadata);
4149 	phar_obj->archive->is_modified = 1;
4150 	phar_flush(phar_obj->archive, 0, 0, 0, &error);
4151 
4152 	if (error) {
4153 		zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
4154 		efree(error);
4155 	}
4156 }
4157 /* }}} */
4158 
4159 /* {{{ proto int Phar::delMetadata()
4160  * Deletes the global metadata of the phar
4161  */
PHP_METHOD(Phar,delMetadata)4162 PHP_METHOD(Phar, delMetadata)
4163 {
4164 	char *error;
4165 
4166 	PHAR_ARCHIVE_OBJECT();
4167 
4168 	if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
4169 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
4170 		return;
4171 	}
4172 
4173 	if (Z_TYPE(phar_obj->archive->metadata) != IS_UNDEF) {
4174 		zval_ptr_dtor(&phar_obj->archive->metadata);
4175 		ZVAL_UNDEF(&phar_obj->archive->metadata);
4176 		phar_obj->archive->is_modified = 1;
4177 		phar_flush(phar_obj->archive, 0, 0, 0, &error);
4178 
4179 		if (error) {
4180 			zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
4181 			efree(error);
4182 			RETURN_FALSE;
4183 		} else {
4184 			RETURN_TRUE;
4185 		}
4186 
4187 	} else {
4188 		RETURN_TRUE;
4189 	}
4190 }
4191 /* }}} */
4192 
phar_extract_file(zend_bool overwrite,phar_entry_info * entry,char * dest,int dest_len,char ** error)4193 static int phar_extract_file(zend_bool overwrite, phar_entry_info *entry, char *dest, int dest_len, char **error) /* {{{ */
4194 {
4195 	php_stream_statbuf ssb;
4196 	size_t len;
4197 	php_stream *fp;
4198 	char *fullpath;
4199 	const char *slash;
4200 	mode_t mode;
4201 	cwd_state new_state;
4202 	char *filename;
4203 	size_t filename_len;
4204 
4205 	if (entry->is_mounted) {
4206 		/* silently ignore mounted entries */
4207 		return SUCCESS;
4208 	}
4209 
4210 	if (entry->filename_len >= sizeof(".phar")-1 && !memcmp(entry->filename, ".phar", sizeof(".phar")-1)) {
4211 		return SUCCESS;
4212 	}
4213 	/* strip .. from path and restrict it to be under dest directory */
4214 	new_state.cwd = (char*)emalloc(2);
4215 	new_state.cwd[0] = DEFAULT_SLASH;
4216 	new_state.cwd[1] = '\0';
4217 	new_state.cwd_length = 1;
4218 	if (virtual_file_ex(&new_state, entry->filename, NULL, CWD_EXPAND) != 0 ||
4219 			new_state.cwd_length <= 1) {
4220 		if (EINVAL == errno && entry->filename_len > 50) {
4221 			char *tmp = estrndup(entry->filename, 50);
4222 			spprintf(error, 4096, "Cannot extract \"%s...\" to \"%s...\", extracted filename is too long for filesystem", tmp, dest);
4223 			efree(tmp);
4224 		} else {
4225 			spprintf(error, 4096, "Cannot extract \"%s\", internal error", entry->filename);
4226 		}
4227 		efree(new_state.cwd);
4228 		return FAILURE;
4229 	}
4230 	filename = new_state.cwd + 1;
4231 	filename_len = new_state.cwd_length - 1;
4232 #ifdef PHP_WIN32
4233 	/* unixify the path back, otherwise non zip formats might be broken */
4234 	{
4235 		int cnt = filename_len;
4236 
4237 		do {
4238 			if ('\\' == filename[cnt]) {
4239 				filename[cnt] = '/';
4240 			}
4241 		} while (cnt-- >= 0);
4242 	}
4243 #endif
4244 
4245 	len = spprintf(&fullpath, 0, "%s/%s", dest, filename);
4246 
4247 	if (len >= MAXPATHLEN) {
4248 		char *tmp;
4249 		/* truncate for error message */
4250 		fullpath[50] = '\0';
4251 		if (entry->filename_len > 50) {
4252 			tmp = estrndup(entry->filename, 50);
4253 			spprintf(error, 4096, "Cannot extract \"%s...\" to \"%s...\", extracted filename is too long for filesystem", tmp, fullpath);
4254 			efree(tmp);
4255 		} else {
4256 			spprintf(error, 4096, "Cannot extract \"%s\" to \"%s...\", extracted filename is too long for filesystem", entry->filename, fullpath);
4257 		}
4258 		efree(fullpath);
4259 		efree(new_state.cwd);
4260 		return FAILURE;
4261 	}
4262 
4263 	if (!len) {
4264 		spprintf(error, 4096, "Cannot extract \"%s\", internal error", entry->filename);
4265 		efree(fullpath);
4266 		efree(new_state.cwd);
4267 		return FAILURE;
4268 	}
4269 
4270 	if (php_check_open_basedir(fullpath)) {
4271 		spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", openbasedir/safe mode restrictions in effect", entry->filename, fullpath);
4272 		efree(fullpath);
4273 		efree(new_state.cwd);
4274 		return FAILURE;
4275 	}
4276 
4277 	/* let see if the path already exists */
4278 	if (!overwrite && SUCCESS == php_stream_stat_path(fullpath, &ssb)) {
4279 		spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", path already exists", entry->filename, fullpath);
4280 		efree(fullpath);
4281 		efree(new_state.cwd);
4282 		return FAILURE;
4283 	}
4284 
4285 	/* perform dirname */
4286 	slash = zend_memrchr(filename, '/', filename_len);
4287 
4288 	if (slash) {
4289 		fullpath[dest_len + (slash - filename) + 1] = '\0';
4290 	} else {
4291 		fullpath[dest_len] = '\0';
4292 	}
4293 
4294 	if (FAILURE == php_stream_stat_path(fullpath, &ssb)) {
4295 		if (entry->is_dir) {
4296 			if (!php_stream_mkdir(fullpath, entry->flags & PHAR_ENT_PERM_MASK,  PHP_STREAM_MKDIR_RECURSIVE, NULL)) {
4297 				spprintf(error, 4096, "Cannot extract \"%s\", could not create directory \"%s\"", entry->filename, fullpath);
4298 				efree(fullpath);
4299 				efree(new_state.cwd);
4300 				return FAILURE;
4301 			}
4302 		} else {
4303 			if (!php_stream_mkdir(fullpath, 0777,  PHP_STREAM_MKDIR_RECURSIVE, NULL)) {
4304 				spprintf(error, 4096, "Cannot extract \"%s\", could not create directory \"%s\"", entry->filename, fullpath);
4305 				efree(fullpath);
4306 				efree(new_state.cwd);
4307 				return FAILURE;
4308 			}
4309 		}
4310 	}
4311 
4312 	if (slash) {
4313 		fullpath[dest_len + (slash - filename) + 1] = '/';
4314 	} else {
4315 		fullpath[dest_len] = '/';
4316 	}
4317 
4318 	filename = NULL;
4319 	efree(new_state.cwd);
4320 	/* it is a standalone directory, job done */
4321 	if (entry->is_dir) {
4322 		efree(fullpath);
4323 		return SUCCESS;
4324 	}
4325 
4326 	fp = php_stream_open_wrapper(fullpath, "w+b", REPORT_ERRORS, NULL);
4327 
4328 	if (!fp) {
4329 		spprintf(error, 4096, "Cannot extract \"%s\", could not open for writing \"%s\"", entry->filename, fullpath);
4330 		efree(fullpath);
4331 		return FAILURE;
4332 	}
4333 
4334 	if (!phar_get_efp(entry, 0)) {
4335 		if (FAILURE == phar_open_entry_fp(entry, error, 1)) {
4336 			if (error) {
4337 				spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to open internal file pointer: %s", entry->filename, fullpath, *error);
4338 			} else {
4339 				spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to open internal file pointer", entry->filename, fullpath);
4340 			}
4341 			efree(fullpath);
4342 			php_stream_close(fp);
4343 			return FAILURE;
4344 		}
4345 	}
4346 
4347 	if (FAILURE == phar_seek_efp(entry, 0, SEEK_SET, 0, 0)) {
4348 		spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to seek internal file pointer", entry->filename, fullpath);
4349 		efree(fullpath);
4350 		php_stream_close(fp);
4351 		return FAILURE;
4352 	}
4353 
4354 	if (SUCCESS != php_stream_copy_to_stream_ex(phar_get_efp(entry, 0), fp, entry->uncompressed_filesize, NULL)) {
4355 		spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", copying contents failed", entry->filename, fullpath);
4356 		efree(fullpath);
4357 		php_stream_close(fp);
4358 		return FAILURE;
4359 	}
4360 
4361 	php_stream_close(fp);
4362 	mode = (mode_t) entry->flags & PHAR_ENT_PERM_MASK;
4363 
4364 	if (FAILURE == VCWD_CHMOD(fullpath, mode)) {
4365 		spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", setting file permissions failed", entry->filename, fullpath);
4366 		efree(fullpath);
4367 		return FAILURE;
4368 	}
4369 
4370 	efree(fullpath);
4371 	return SUCCESS;
4372 }
4373 /* }}} */
4374 
extract_helper(phar_archive_data * archive,zend_string * search,char * pathto,size_t pathto_len,zend_bool overwrite,char ** error)4375 static int extract_helper(phar_archive_data *archive, zend_string *search, char *pathto, size_t pathto_len, zend_bool overwrite, char **error) { /* {{{ */
4376 	int extracted = 0;
4377 	phar_entry_info *entry;
4378 
4379 	if (!search) {
4380 		/* nothing to match -- extract all files */
4381 		ZEND_HASH_FOREACH_PTR(&archive->manifest, entry) {
4382 			if (FAILURE == phar_extract_file(overwrite, entry, pathto, (int)pathto_len, error)) return -1;
4383 			extracted++;
4384 		} ZEND_HASH_FOREACH_END();
4385 	} else if ('/' == ZSTR_VAL(search)[ZSTR_LEN(search) - 1]) {
4386 		/* ends in "/" -- extract all entries having that prefix */
4387 		ZEND_HASH_FOREACH_PTR(&archive->manifest, entry) {
4388 			if (0 != strncmp(ZSTR_VAL(search), entry->filename, ZSTR_LEN(search))) continue;
4389 			if (FAILURE == phar_extract_file(overwrite, entry, pathto, (int)pathto_len, error)) return -1;
4390 			extracted++;
4391 		} ZEND_HASH_FOREACH_END();
4392 	} else {
4393 		/* otherwise, looking for an exact match */
4394 		entry = zend_hash_find_ptr(&archive->manifest, search);
4395 		if (NULL == entry) return 0;
4396 		if (FAILURE == phar_extract_file(overwrite, entry, pathto, (int)pathto_len, error)) return -1;
4397 		return 1;
4398 	}
4399 
4400 	return extracted;
4401 }
4402 /* }}} */
4403 
4404 /* {{{ proto bool Phar::extractTo(string pathto[[, mixed files], bool overwrite])
4405  * Extract one or more file from a phar archive, optionally overwriting existing files
4406  */
PHP_METHOD(Phar,extractTo)4407 PHP_METHOD(Phar, extractTo)
4408 {
4409 	php_stream *fp;
4410 	php_stream_statbuf ssb;
4411 	char *pathto;
4412 	zend_string *filename;
4413 	size_t pathto_len;
4414 	int ret, i;
4415 	int nelems;
4416 	zval *zval_file;
4417 	zval *zval_files = NULL;
4418 	zend_bool overwrite = 0;
4419 	char *error = NULL;
4420 
4421 	PHAR_ARCHIVE_OBJECT();
4422 
4423 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|z!b", &pathto, &pathto_len, &zval_files, &overwrite) == FAILURE) {
4424 		return;
4425 	}
4426 
4427 	fp = php_stream_open_wrapper(phar_obj->archive->fname, "rb", IGNORE_URL|STREAM_MUST_SEEK, NULL);
4428 
4429 	if (!fp) {
4430 		zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
4431 			"Invalid argument, %s cannot be found", phar_obj->archive->fname);
4432 		return;
4433 	}
4434 
4435 	php_stream_close(fp);
4436 
4437 	if (pathto_len < 1) {
4438 		zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
4439 			"Invalid argument, extraction path must be non-zero length");
4440 		return;
4441 	}
4442 
4443 	if (pathto_len >= MAXPATHLEN) {
4444 		char *tmp = estrndup(pathto, 50);
4445 		/* truncate for error message */
4446 		zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Cannot extract to \"%s...\", destination directory is too long for filesystem", tmp);
4447 		efree(tmp);
4448 		return;
4449 	}
4450 
4451 	if (php_stream_stat_path(pathto, &ssb) < 0) {
4452 		ret = php_stream_mkdir(pathto, 0777,  PHP_STREAM_MKDIR_RECURSIVE, NULL);
4453 		if (!ret) {
4454 			zend_throw_exception_ex(spl_ce_RuntimeException, 0,
4455 				"Unable to create path \"%s\" for extraction", pathto);
4456 			return;
4457 		}
4458 	} else if (!(ssb.sb.st_mode & S_IFDIR)) {
4459 		zend_throw_exception_ex(spl_ce_RuntimeException, 0,
4460 			"Unable to use path \"%s\" for extraction, it is a file, must be a directory", pathto);
4461 		return;
4462 	}
4463 
4464 	if (zval_files) {
4465 		switch (Z_TYPE_P(zval_files)) {
4466 			case IS_NULL:
4467 				filename = NULL;
4468 				break;
4469 			case IS_STRING:
4470 				filename = Z_STR_P(zval_files);
4471 				break;
4472 			case IS_ARRAY:
4473 				nelems = zend_hash_num_elements(Z_ARRVAL_P(zval_files));
4474 				if (nelems == 0 ) {
4475 					RETURN_FALSE;
4476 				}
4477 				for (i = 0; i < nelems; i++) {
4478 					if ((zval_file = zend_hash_index_find(Z_ARRVAL_P(zval_files), i)) != NULL) {
4479 						if (IS_STRING != Z_TYPE_P(zval_file)) {
4480 							zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
4481 								"Invalid argument, array of filenames to extract contains non-string value");
4482 							return;
4483 						}
4484 						switch (extract_helper(phar_obj->archive, Z_STR_P(zval_file), pathto, pathto_len, overwrite, &error)) {
4485 							case -1:
4486 								zend_throw_exception_ex(phar_ce_PharException, 0, "Extraction from phar \"%s\" failed: %s",
4487 									phar_obj->archive->fname, error);
4488 								efree(error);
4489 								return;
4490 							case 0:
4491 								zend_throw_exception_ex(phar_ce_PharException, 0,
4492 									"Phar Error: attempted to extract non-existent file or directory \"%s\" from phar \"%s\"",
4493 									ZSTR_VAL(Z_STR_P(zval_file)), phar_obj->archive->fname);
4494 								return;
4495 						}
4496 					}
4497 				}
4498 				RETURN_TRUE;
4499 			default:
4500 				zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
4501 					"Invalid argument, expected a filename (string) or array of filenames");
4502 				return;
4503 		}
4504 	} else {
4505 		filename = NULL;
4506 	}
4507 
4508 	ret = extract_helper(phar_obj->archive, filename, pathto, pathto_len, overwrite, &error);
4509 	if (-1 == ret) {
4510 		zend_throw_exception_ex(phar_ce_PharException, 0, "Extraction from phar \"%s\" failed: %s",
4511 			phar_obj->archive->fname, error);
4512 		efree(error);
4513 	} else if (0 == ret && NULL != filename) {
4514 		zend_throw_exception_ex(phar_ce_PharException, 0,
4515 			"Phar Error: attempted to extract non-existent file or directory \"%s\" from phar \"%s\"",
4516 			ZSTR_VAL(filename), phar_obj->archive->fname);
4517 	} else {
4518 		RETURN_TRUE;
4519 	}
4520 }
4521 /* }}} */
4522 
4523 
4524 /* {{{ proto void PharFileInfo::__construct(string entry)
4525  * Construct a Phar entry object
4526  */
PHP_METHOD(PharFileInfo,__construct)4527 PHP_METHOD(PharFileInfo, __construct)
4528 {
4529 	char *fname, *arch, *entry, *error;
4530 	size_t fname_len;
4531 	int arch_len, entry_len;
4532 	phar_entry_object *entry_obj;
4533 	phar_entry_info *entry_info;
4534 	phar_archive_data *phar_data;
4535 	zval *zobj = getThis(), arg1;
4536 
4537 	if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
4538 		return;
4539 	}
4540 
4541 	entry_obj = (phar_entry_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset);
4542 
4543 	if (entry_obj->entry) {
4544 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot call constructor twice");
4545 		return;
4546 	}
4547 
4548 	if (fname_len < 7 || memcmp(fname, "phar://", 7) || phar_split_fname(fname, (int)fname_len, &arch, &arch_len, &entry, &entry_len, 2, 0) == FAILURE) {
4549 		zend_throw_exception_ex(spl_ce_RuntimeException, 0,
4550 			"'%s' is not a valid phar archive URL (must have at least phar://filename.phar)", fname);
4551 		return;
4552 	}
4553 
4554 	if (phar_open_from_filename(arch, arch_len, NULL, 0, REPORT_ERRORS, &phar_data, &error) == FAILURE) {
4555 		efree(arch);
4556 		efree(entry);
4557 		if (error) {
4558 			zend_throw_exception_ex(spl_ce_RuntimeException, 0,
4559 				"Cannot open phar file '%s': %s", fname, error);
4560 			efree(error);
4561 		} else {
4562 			zend_throw_exception_ex(spl_ce_RuntimeException, 0,
4563 				"Cannot open phar file '%s'", fname);
4564 		}
4565 		return;
4566 	}
4567 
4568 	if ((entry_info = phar_get_entry_info_dir(phar_data, entry, entry_len, 1, &error, 1)) == NULL) {
4569 		zend_throw_exception_ex(spl_ce_RuntimeException, 0,
4570 			"Cannot access phar file entry '%s' in archive '%s'%s%s", entry, arch, error ? ", " : "", error ? error : "");
4571 		efree(arch);
4572 		efree(entry);
4573 		return;
4574 	}
4575 
4576 	efree(arch);
4577 	efree(entry);
4578 
4579 	entry_obj->entry = entry_info;
4580 
4581 	ZVAL_STRINGL(&arg1, fname, fname_len);
4582 
4583 	zend_call_method_with_1_params(zobj, Z_OBJCE_P(zobj),
4584 		&spl_ce_SplFileInfo->constructor, "__construct", NULL, &arg1);
4585 
4586 	zval_ptr_dtor(&arg1);
4587 }
4588 /* }}} */
4589 
4590 #define PHAR_ENTRY_OBJECT() \
4591 	zval *zobj = getThis(); \
4592 	phar_entry_object *entry_obj = (phar_entry_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset); \
4593 	if (!entry_obj->entry) { \
4594 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
4595 			"Cannot call method on an uninitialized PharFileInfo object"); \
4596 		return; \
4597 	}
4598 
4599 /* {{{ proto void PharFileInfo::__destruct()
4600  * clean up directory-based entry objects
4601  */
PHP_METHOD(PharFileInfo,__destruct)4602 PHP_METHOD(PharFileInfo, __destruct)
4603 {
4604 	zval *zobj = getThis();
4605 	phar_entry_object *entry_obj = (phar_entry_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset);
4606 
4607 	if (entry_obj->entry && entry_obj->entry->is_temp_dir) {
4608 		if (entry_obj->entry->filename) {
4609 			efree(entry_obj->entry->filename);
4610 			entry_obj->entry->filename = NULL;
4611 		}
4612 
4613 		efree(entry_obj->entry);
4614 		entry_obj->entry = NULL;
4615 	}
4616 }
4617 /* }}} */
4618 
4619 /* {{{ proto int PharFileInfo::getCompressedSize()
4620  * Returns the compressed size
4621  */
PHP_METHOD(PharFileInfo,getCompressedSize)4622 PHP_METHOD(PharFileInfo, getCompressedSize)
4623 {
4624 	PHAR_ENTRY_OBJECT();
4625 
4626 	if (zend_parse_parameters_none() == FAILURE) {
4627 		return;
4628 	}
4629 
4630 	RETURN_LONG(entry_obj->entry->compressed_filesize);
4631 }
4632 /* }}} */
4633 
4634 /* {{{ proto bool PharFileInfo::isCompressed([int compression_type])
4635  * Returns whether the entry is compressed, and whether it is compressed with Phar::GZ or Phar::BZ2 if specified
4636  */
PHP_METHOD(PharFileInfo,isCompressed)4637 PHP_METHOD(PharFileInfo, isCompressed)
4638 {
4639 	/* a number that is not Phar::GZ or Phar::BZ2 */
4640 	zend_long method = 9021976;
4641 	PHAR_ENTRY_OBJECT();
4642 
4643 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &method) == FAILURE) {
4644 		return;
4645 	}
4646 
4647 	switch (method) {
4648 		case 9021976:
4649 			RETURN_BOOL(entry_obj->entry->flags & PHAR_ENT_COMPRESSION_MASK);
4650 		case PHAR_ENT_COMPRESSED_GZ:
4651 			RETURN_BOOL(entry_obj->entry->flags & PHAR_ENT_COMPRESSED_GZ);
4652 		case PHAR_ENT_COMPRESSED_BZ2:
4653 			RETURN_BOOL(entry_obj->entry->flags & PHAR_ENT_COMPRESSED_BZ2);
4654 		default:
4655 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
4656 				"Unknown compression type specified"); \
4657 	}
4658 }
4659 /* }}} */
4660 
4661 /* {{{ proto int PharFileInfo::getCRC32()
4662  * Returns CRC32 code or throws an exception if not CRC checked
4663  */
PHP_METHOD(PharFileInfo,getCRC32)4664 PHP_METHOD(PharFileInfo, getCRC32)
4665 {
4666 	PHAR_ENTRY_OBJECT();
4667 
4668 	if (zend_parse_parameters_none() == FAILURE) {
4669 		return;
4670 	}
4671 
4672 	if (entry_obj->entry->is_dir) {
4673 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
4674 			"Phar entry is a directory, does not have a CRC"); \
4675 		return;
4676 	}
4677 
4678 	if (entry_obj->entry->is_crc_checked) {
4679 		RETURN_LONG(entry_obj->entry->crc32);
4680 	} else {
4681 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
4682 			"Phar entry was not CRC checked"); \
4683 	}
4684 }
4685 /* }}} */
4686 
4687 /* {{{ proto int PharFileInfo::isCRCChecked()
4688  * Returns whether file entry is CRC checked
4689  */
PHP_METHOD(PharFileInfo,isCRCChecked)4690 PHP_METHOD(PharFileInfo, isCRCChecked)
4691 {
4692 	PHAR_ENTRY_OBJECT();
4693 
4694 	if (zend_parse_parameters_none() == FAILURE) {
4695 		return;
4696 	}
4697 
4698 	RETURN_BOOL(entry_obj->entry->is_crc_checked);
4699 }
4700 /* }}} */
4701 
4702 /* {{{ proto int PharFileInfo::getPharFlags()
4703  * Returns the Phar file entry flags
4704  */
PHP_METHOD(PharFileInfo,getPharFlags)4705 PHP_METHOD(PharFileInfo, getPharFlags)
4706 {
4707 	PHAR_ENTRY_OBJECT();
4708 
4709 	if (zend_parse_parameters_none() == FAILURE) {
4710 		return;
4711 	}
4712 
4713 	RETURN_LONG(entry_obj->entry->flags & ~(PHAR_ENT_PERM_MASK|PHAR_ENT_COMPRESSION_MASK));
4714 }
4715 /* }}} */
4716 
4717 /* {{{ proto int PharFileInfo::chmod()
4718  * set the file permissions for the Phar.  This only allows setting execution bit, read/write
4719  */
PHP_METHOD(PharFileInfo,chmod)4720 PHP_METHOD(PharFileInfo, chmod)
4721 {
4722 	char *error;
4723 	zend_long perms;
4724 	PHAR_ENTRY_OBJECT();
4725 
4726 	if (entry_obj->entry->is_temp_dir) {
4727 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
4728 			"Phar entry \"%s\" is a temporary directory (not an actual entry in the archive), cannot chmod", entry_obj->entry->filename); \
4729 		return;
4730 	}
4731 
4732 	if (PHAR_G(readonly) && !entry_obj->entry->phar->is_data) {
4733 		zend_throw_exception_ex(phar_ce_PharException, 0, "Cannot modify permissions for file \"%s\" in phar \"%s\", write operations are prohibited", entry_obj->entry->filename, entry_obj->entry->phar->fname);
4734 		return;
4735 	}
4736 
4737 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &perms) == FAILURE) {
4738 		return;
4739 	}
4740 
4741 	if (entry_obj->entry->is_persistent) {
4742 		phar_archive_data *phar = entry_obj->entry->phar;
4743 
4744 		if (FAILURE == phar_copy_on_write(&phar)) {
4745 			zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
4746 			return;
4747 		}
4748 		/* re-populate after copy-on-write */
4749 		entry_obj->entry = zend_hash_str_find_ptr(&phar->manifest, entry_obj->entry->filename, entry_obj->entry->filename_len);
4750 	}
4751 	/* clear permissions */
4752 	entry_obj->entry->flags &= ~PHAR_ENT_PERM_MASK;
4753 	perms &= 0777;
4754 	entry_obj->entry->flags |= perms;
4755 	entry_obj->entry->old_flags = entry_obj->entry->flags;
4756 	entry_obj->entry->phar->is_modified = 1;
4757 	entry_obj->entry->is_modified = 1;
4758 
4759 	/* hackish cache in php_stat needs to be cleared */
4760 	/* if this code fails to work, check main/streams/streams.c, _php_stream_stat_path */
4761 	if (BG(CurrentLStatFile)) {
4762 		efree(BG(CurrentLStatFile));
4763 	}
4764 
4765 	if (BG(CurrentStatFile)) {
4766 		efree(BG(CurrentStatFile));
4767 	}
4768 
4769 	BG(CurrentLStatFile) = NULL;
4770 	BG(CurrentStatFile) = NULL;
4771 	phar_flush(entry_obj->entry->phar, 0, 0, 0, &error);
4772 
4773 	if (error) {
4774 		zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
4775 		efree(error);
4776 	}
4777 }
4778 /* }}} */
4779 
4780 /* {{{ proto int PharFileInfo::hasMetaData()
4781  * Returns the metadata of the entry
4782  */
PHP_METHOD(PharFileInfo,hasMetadata)4783 PHP_METHOD(PharFileInfo, hasMetadata)
4784 {
4785 	PHAR_ENTRY_OBJECT();
4786 
4787 	if (zend_parse_parameters_none() == FAILURE) {
4788 		return;
4789 	}
4790 
4791 	RETURN_BOOL(Z_TYPE(entry_obj->entry->metadata) != IS_UNDEF);
4792 }
4793 /* }}} */
4794 
4795 /* {{{ proto int PharFileInfo::getMetaData()
4796  * Returns the metadata of the entry
4797  */
PHP_METHOD(PharFileInfo,getMetadata)4798 PHP_METHOD(PharFileInfo, getMetadata)
4799 {
4800 	PHAR_ENTRY_OBJECT();
4801 
4802 	if (zend_parse_parameters_none() == FAILURE) {
4803 		return;
4804 	}
4805 
4806 	if (Z_TYPE(entry_obj->entry->metadata) != IS_UNDEF) {
4807 		if (entry_obj->entry->is_persistent) {
4808 			char *buf = estrndup((char *) Z_PTR(entry_obj->entry->metadata), entry_obj->entry->metadata_len);
4809 			/* assume success, we would have failed before */
4810 			phar_parse_metadata(&buf, return_value, entry_obj->entry->metadata_len);
4811 			efree(buf);
4812 		} else {
4813 			ZVAL_COPY(return_value, &entry_obj->entry->metadata);
4814 		}
4815 	}
4816 }
4817 /* }}} */
4818 
4819 /* {{{ proto int PharFileInfo::setMetaData(mixed $metadata)
4820  * Sets the metadata of the entry
4821  */
PHP_METHOD(PharFileInfo,setMetadata)4822 PHP_METHOD(PharFileInfo, setMetadata)
4823 {
4824 	char *error;
4825 	zval *metadata;
4826 
4827 	PHAR_ENTRY_OBJECT();
4828 
4829 	if (PHAR_G(readonly) && !entry_obj->entry->phar->is_data) {
4830 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
4831 		return;
4832 	}
4833 
4834 	if (entry_obj->entry->is_temp_dir) {
4835 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
4836 			"Phar entry is a temporary directory (not an actual entry in the archive), cannot set metadata"); \
4837 		return;
4838 	}
4839 
4840 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &metadata) == FAILURE) {
4841 		return;
4842 	}
4843 
4844 	if (entry_obj->entry->is_persistent) {
4845 		phar_archive_data *phar = entry_obj->entry->phar;
4846 
4847 		if (FAILURE == phar_copy_on_write(&phar)) {
4848 			zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
4849 			return;
4850 		}
4851 		/* re-populate after copy-on-write */
4852 		entry_obj->entry = zend_hash_str_find_ptr(&phar->manifest, entry_obj->entry->filename, entry_obj->entry->filename_len);
4853 	}
4854 	if (Z_TYPE(entry_obj->entry->metadata) != IS_UNDEF) {
4855 		zval_ptr_dtor(&entry_obj->entry->metadata);
4856 		ZVAL_UNDEF(&entry_obj->entry->metadata);
4857 	}
4858 
4859 	ZVAL_COPY(&entry_obj->entry->metadata, metadata);
4860 
4861 	entry_obj->entry->is_modified = 1;
4862 	entry_obj->entry->phar->is_modified = 1;
4863 	phar_flush(entry_obj->entry->phar, 0, 0, 0, &error);
4864 
4865 	if (error) {
4866 		zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
4867 		efree(error);
4868 	}
4869 }
4870 /* }}} */
4871 
4872 /* {{{ proto bool PharFileInfo::delMetaData()
4873  * Deletes the metadata of the entry
4874  */
PHP_METHOD(PharFileInfo,delMetadata)4875 PHP_METHOD(PharFileInfo, delMetadata)
4876 {
4877 	char *error;
4878 
4879 	PHAR_ENTRY_OBJECT();
4880 
4881 	if (zend_parse_parameters_none() == FAILURE) {
4882 		return;
4883 	}
4884 
4885 	if (PHAR_G(readonly) && !entry_obj->entry->phar->is_data) {
4886 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
4887 		return;
4888 	}
4889 
4890 	if (entry_obj->entry->is_temp_dir) {
4891 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
4892 			"Phar entry is a temporary directory (not an actual entry in the archive), cannot delete metadata"); \
4893 		return;
4894 	}
4895 
4896 	if (Z_TYPE(entry_obj->entry->metadata) != IS_UNDEF) {
4897 		if (entry_obj->entry->is_persistent) {
4898 			phar_archive_data *phar = entry_obj->entry->phar;
4899 
4900 			if (FAILURE == phar_copy_on_write(&phar)) {
4901 				zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
4902 				return;
4903 			}
4904 			/* re-populate after copy-on-write */
4905 			entry_obj->entry = zend_hash_str_find_ptr(&phar->manifest, entry_obj->entry->filename, entry_obj->entry->filename_len);
4906 		}
4907 		zval_ptr_dtor(&entry_obj->entry->metadata);
4908 		ZVAL_UNDEF(&entry_obj->entry->metadata);
4909 		entry_obj->entry->is_modified = 1;
4910 		entry_obj->entry->phar->is_modified = 1;
4911 
4912 		phar_flush(entry_obj->entry->phar, 0, 0, 0, &error);
4913 
4914 		if (error) {
4915 			zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
4916 			efree(error);
4917 			RETURN_FALSE;
4918 		} else {
4919 			RETURN_TRUE;
4920 		}
4921 
4922 	} else {
4923 		RETURN_TRUE;
4924 	}
4925 }
4926 /* }}} */
4927 
4928 /* {{{ proto string PharFileInfo::getContent()
4929  * return the complete file contents of the entry (like file_get_contents)
4930  */
PHP_METHOD(PharFileInfo,getContent)4931 PHP_METHOD(PharFileInfo, getContent)
4932 {
4933 	char *error;
4934 	php_stream *fp;
4935 	phar_entry_info *link;
4936 	zend_string *str;
4937 
4938 	PHAR_ENTRY_OBJECT();
4939 
4940 	if (zend_parse_parameters_none() == FAILURE) {
4941 		return;
4942 	}
4943 
4944 	if (entry_obj->entry->is_dir) {
4945 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
4946 			"Phar error: Cannot retrieve contents, \"%s\" in phar \"%s\" is a directory", entry_obj->entry->filename, entry_obj->entry->phar->fname);
4947 		return;
4948 	}
4949 
4950 	link = phar_get_link_source(entry_obj->entry);
4951 
4952 	if (!link) {
4953 		link = entry_obj->entry;
4954 	}
4955 
4956 	if (SUCCESS != phar_open_entry_fp(link, &error, 0)) {
4957 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
4958 			"Phar error: Cannot retrieve contents, \"%s\" in phar \"%s\": %s", entry_obj->entry->filename, entry_obj->entry->phar->fname, error);
4959 		efree(error);
4960 		return;
4961 	}
4962 
4963 	if (!(fp = phar_get_efp(link, 0))) {
4964 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
4965 			"Phar error: Cannot retrieve contents of \"%s\" in phar \"%s\"", entry_obj->entry->filename, entry_obj->entry->phar->fname);
4966 		return;
4967 	}
4968 
4969 	phar_seek_efp(link, 0, SEEK_SET, 0, 0);
4970 	str = php_stream_copy_to_mem(fp, link->uncompressed_filesize, 0);
4971 	if (str) {
4972 		RETURN_STR(str);
4973 	} else {
4974 		RETURN_EMPTY_STRING();
4975 	}
4976 }
4977 /* }}} */
4978 
4979 /* {{{ proto int PharFileInfo::compress(int compression_type)
4980  * Instructs the Phar class to compress the current file using zlib or bzip2 compression
4981  */
PHP_METHOD(PharFileInfo,compress)4982 PHP_METHOD(PharFileInfo, compress)
4983 {
4984 	zend_long method;
4985 	char *error;
4986 	PHAR_ENTRY_OBJECT();
4987 
4988 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &method) == FAILURE) {
4989 		return;
4990 	}
4991 
4992 	if (entry_obj->entry->is_tar) {
4993 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
4994 			"Cannot compress with Gzip compression, not possible with tar-based phar archives");
4995 		return;
4996 	}
4997 
4998 	if (entry_obj->entry->is_dir) {
4999 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
5000 			"Phar entry is a directory, cannot set compression"); \
5001 		return;
5002 	}
5003 
5004 	if (PHAR_G(readonly) && !entry_obj->entry->phar->is_data) {
5005 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
5006 			"Phar is readonly, cannot change compression");
5007 		return;
5008 	}
5009 
5010 	if (entry_obj->entry->is_deleted) {
5011 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
5012 			"Cannot compress deleted file");
5013 		return;
5014 	}
5015 
5016 	if (entry_obj->entry->is_persistent) {
5017 		phar_archive_data *phar = entry_obj->entry->phar;
5018 
5019 		if (FAILURE == phar_copy_on_write(&phar)) {
5020 			zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
5021 			return;
5022 		}
5023 		/* re-populate after copy-on-write */
5024 		entry_obj->entry = zend_hash_str_find_ptr(&phar->manifest, entry_obj->entry->filename, entry_obj->entry->filename_len);
5025 	}
5026 	switch (method) {
5027 		case PHAR_ENT_COMPRESSED_GZ:
5028 			if (entry_obj->entry->flags & PHAR_ENT_COMPRESSED_GZ) {
5029 				RETURN_TRUE;
5030 			}
5031 
5032 			if ((entry_obj->entry->flags & PHAR_ENT_COMPRESSED_BZ2) != 0) {
5033 				if (!PHAR_G(has_bz2)) {
5034 					zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
5035 						"Cannot compress with gzip compression, file is already compressed with bzip2 compression and bz2 extension is not enabled, cannot decompress");
5036 					return;
5037 				}
5038 
5039 				/* decompress this file indirectly */
5040 				if (SUCCESS != phar_open_entry_fp(entry_obj->entry, &error, 1)) {
5041 					zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
5042 						"Phar error: Cannot decompress bzip2-compressed file \"%s\" in phar \"%s\" in order to compress with gzip: %s", entry_obj->entry->filename, entry_obj->entry->phar->fname, error);
5043 					efree(error);
5044 					return;
5045 				}
5046 			}
5047 
5048 			if (!PHAR_G(has_zlib)) {
5049 				zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
5050 					"Cannot compress with gzip compression, zlib extension is not enabled");
5051 				return;
5052 			}
5053 
5054 			entry_obj->entry->old_flags = entry_obj->entry->flags;
5055 			entry_obj->entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
5056 			entry_obj->entry->flags |= PHAR_ENT_COMPRESSED_GZ;
5057 			break;
5058 		case PHAR_ENT_COMPRESSED_BZ2:
5059 			if (entry_obj->entry->flags & PHAR_ENT_COMPRESSED_BZ2) {
5060 				RETURN_TRUE;
5061 			}
5062 
5063 			if ((entry_obj->entry->flags & PHAR_ENT_COMPRESSED_GZ) != 0) {
5064 				if (!PHAR_G(has_zlib)) {
5065 					zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
5066 						"Cannot compress with bzip2 compression, file is already compressed with gzip compression and zlib extension is not enabled, cannot decompress");
5067 					return;
5068 				}
5069 
5070 				/* decompress this file indirectly */
5071 				if (SUCCESS != phar_open_entry_fp(entry_obj->entry, &error, 1)) {
5072 					zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
5073 						"Phar error: Cannot decompress gzip-compressed file \"%s\" in phar \"%s\" in order to compress with bzip2: %s", entry_obj->entry->filename, entry_obj->entry->phar->fname, error);
5074 					efree(error);
5075 					return;
5076 				}
5077 			}
5078 
5079 			if (!PHAR_G(has_bz2)) {
5080 				zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
5081 					"Cannot compress with bzip2 compression, bz2 extension is not enabled");
5082 				return;
5083 			}
5084 			entry_obj->entry->old_flags = entry_obj->entry->flags;
5085 			entry_obj->entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
5086 			entry_obj->entry->flags |= PHAR_ENT_COMPRESSED_BZ2;
5087 			break;
5088 		default:
5089 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
5090 				"Unknown compression type specified"); \
5091 	}
5092 
5093 	entry_obj->entry->phar->is_modified = 1;
5094 	entry_obj->entry->is_modified = 1;
5095 	phar_flush(entry_obj->entry->phar, 0, 0, 0, &error);
5096 
5097 	if (error) {
5098 		zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
5099 		efree(error);
5100 	}
5101 
5102 	RETURN_TRUE;
5103 }
5104 /* }}} */
5105 
5106 /* {{{ proto int PharFileInfo::decompress()
5107  * Instructs the Phar class to decompress the current file
5108  */
PHP_METHOD(PharFileInfo,decompress)5109 PHP_METHOD(PharFileInfo, decompress)
5110 {
5111 	char *error;
5112 	PHAR_ENTRY_OBJECT();
5113 
5114 	if (zend_parse_parameters_none() == FAILURE) {
5115 		return;
5116 	}
5117 
5118 	if (entry_obj->entry->is_dir) {
5119 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
5120 			"Phar entry is a directory, cannot set compression"); \
5121 		return;
5122 	}
5123 
5124 	if ((entry_obj->entry->flags & PHAR_ENT_COMPRESSION_MASK) == 0) {
5125 		RETURN_TRUE;
5126 	}
5127 
5128 	if (PHAR_G(readonly) && !entry_obj->entry->phar->is_data) {
5129 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
5130 			"Phar is readonly, cannot decompress");
5131 		return;
5132 	}
5133 
5134 	if (entry_obj->entry->is_deleted) {
5135 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
5136 			"Cannot compress deleted file");
5137 		return;
5138 	}
5139 
5140 	if ((entry_obj->entry->flags & PHAR_ENT_COMPRESSED_GZ) != 0 && !PHAR_G(has_zlib)) {
5141 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
5142 			"Cannot decompress Gzip-compressed file, zlib extension is not enabled");
5143 		return;
5144 	}
5145 
5146 	if ((entry_obj->entry->flags & PHAR_ENT_COMPRESSED_BZ2) != 0 && !PHAR_G(has_bz2)) {
5147 		zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
5148 			"Cannot decompress Bzip2-compressed file, bz2 extension is not enabled");
5149 		return;
5150 	}
5151 
5152 	if (entry_obj->entry->is_persistent) {
5153 		phar_archive_data *phar = entry_obj->entry->phar;
5154 
5155 		if (FAILURE == phar_copy_on_write(&phar)) {
5156 			zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
5157 			return;
5158 		}
5159 		/* re-populate after copy-on-write */
5160 		entry_obj->entry = zend_hash_str_find_ptr(&phar->manifest, entry_obj->entry->filename, entry_obj->entry->filename_len);
5161 	}
5162 	if (!entry_obj->entry->fp) {
5163 		if (FAILURE == phar_open_archive_fp(entry_obj->entry->phar)) {
5164 			zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot decompress entry \"%s\", phar error: Cannot open phar archive \"%s\" for reading", entry_obj->entry->filename, entry_obj->entry->phar->fname);
5165 			return;
5166 		}
5167 		entry_obj->entry->fp_type = PHAR_FP;
5168 	}
5169 
5170 	entry_obj->entry->old_flags = entry_obj->entry->flags;
5171 	entry_obj->entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
5172 	entry_obj->entry->phar->is_modified = 1;
5173 	entry_obj->entry->is_modified = 1;
5174 	phar_flush(entry_obj->entry->phar, 0, 0, 0, &error);
5175 
5176 	if (error) {
5177 		zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
5178 		efree(error);
5179 	}
5180 	RETURN_TRUE;
5181 }
5182 /* }}} */
5183 
5184 /* {{{ phar methods */
5185 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar___construct, 0, 0, 1)
5186 	ZEND_ARG_INFO(0, filename)
5187 	ZEND_ARG_INFO(0, flags)
5188 	ZEND_ARG_INFO(0, alias)
5189 ZEND_END_ARG_INFO()
5190 
5191 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_createDS, 0, 0, 0)
5192 	ZEND_ARG_INFO(0, index)
5193 	ZEND_ARG_INFO(0, webindex)
5194 ZEND_END_ARG_INFO()
5195 
5196 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_cancompress, 0, 0, 0)
5197 	ZEND_ARG_INFO(0, method)
5198 ZEND_END_ARG_INFO()
5199 
5200 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_isvalidpharfilename, 0, 0, 1)
5201 	ZEND_ARG_INFO(0, filename)
5202 	ZEND_ARG_INFO(0, executable)
5203 ZEND_END_ARG_INFO()
5204 
5205 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_loadPhar, 0, 0, 1)
5206 	ZEND_ARG_INFO(0, filename)
5207 	ZEND_ARG_INFO(0, alias)
5208 ZEND_END_ARG_INFO()
5209 
5210 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_mapPhar, 0, 0, 0)
5211 	ZEND_ARG_INFO(0, alias)
5212 	ZEND_ARG_INFO(0, offset)
5213 ZEND_END_ARG_INFO()
5214 
5215 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_mount, 0, 0, 2)
5216 	ZEND_ARG_INFO(0, inphar)
5217 	ZEND_ARG_INFO(0, externalfile)
5218 ZEND_END_ARG_INFO()
5219 
5220 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_mungServer, 0, 0, 1)
5221 	ZEND_ARG_INFO(0, munglist)
5222 ZEND_END_ARG_INFO()
5223 
5224 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_webPhar, 0, 0, 0)
5225 	ZEND_ARG_INFO(0, alias)
5226 	ZEND_ARG_INFO(0, index)
5227 	ZEND_ARG_INFO(0, f404)
5228 	ZEND_ARG_INFO(0, mimetypes)
5229 	ZEND_ARG_INFO(0, rewrites)
5230 ZEND_END_ARG_INFO()
5231 
5232 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_running, 0, 0, 0)
5233 	ZEND_ARG_INFO(0, retphar)
5234 ZEND_END_ARG_INFO()
5235 
5236 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_ua, 0, 0, 1)
5237 	ZEND_ARG_INFO(0, archive)
5238 ZEND_END_ARG_INFO()
5239 
5240 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_build, 0, 0, 1)
5241 	ZEND_ARG_INFO(0, iterator)
5242 	ZEND_ARG_INFO(0, base_directory)
5243 ZEND_END_ARG_INFO()
5244 
5245 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_conv, 0, 0, 0)
5246 	ZEND_ARG_INFO(0, format)
5247 	ZEND_ARG_INFO(0, compression_type)
5248 	ZEND_ARG_INFO(0, file_ext)
5249 ZEND_END_ARG_INFO()
5250 
5251 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_comps, 0, 0, 1)
5252 	ZEND_ARG_INFO(0, compression_type)
5253 	ZEND_ARG_INFO(0, file_ext)
5254 ZEND_END_ARG_INFO()
5255 
5256 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_decomp, 0, 0, 0)
5257 	ZEND_ARG_INFO(0, file_ext)
5258 ZEND_END_ARG_INFO()
5259 
5260 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_comp, 0, 0, 1)
5261 	ZEND_ARG_INFO(0, compression_type)
5262 ZEND_END_ARG_INFO()
5263 
5264 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_compo, 0, 0, 0)
5265 	ZEND_ARG_INFO(0, compression_type)
5266 ZEND_END_ARG_INFO()
5267 
5268 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_copy, 0, 0, 2)
5269 	ZEND_ARG_INFO(0, newfile)
5270 	ZEND_ARG_INFO(0, oldfile)
5271 ZEND_END_ARG_INFO()
5272 
5273 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_delete, 0, 0, 1)
5274 	ZEND_ARG_INFO(0, entry)
5275 ZEND_END_ARG_INFO()
5276 
5277 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_fromdir, 0, 0, 1)
5278 	ZEND_ARG_INFO(0, base_dir)
5279 	ZEND_ARG_INFO(0, regex)
5280 ZEND_END_ARG_INFO()
5281 
5282 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_offsetExists, 0, 0, 1)
5283 	ZEND_ARG_INFO(0, entry)
5284 ZEND_END_ARG_INFO()
5285 
5286 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_offsetSet, 0, 0, 2)
5287 	ZEND_ARG_INFO(0, entry)
5288 	ZEND_ARG_INFO(0, value)
5289 ZEND_END_ARG_INFO()
5290 
5291 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setAlias, 0, 0, 1)
5292 	ZEND_ARG_INFO(0, alias)
5293 ZEND_END_ARG_INFO()
5294 
5295 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setMetadata, 0, 0, 1)
5296 	ZEND_ARG_INFO(0, metadata)
5297 ZEND_END_ARG_INFO()
5298 
5299 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setSigAlgo, 0, 0, 1)
5300 	ZEND_ARG_INFO(0, algorithm)
5301 	ZEND_ARG_INFO(0, privatekey)
5302 ZEND_END_ARG_INFO()
5303 
5304 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setStub, 0, 0, 1)
5305 	ZEND_ARG_INFO(0, newstub)
5306 	ZEND_ARG_INFO(0, maxlen)
5307 ZEND_END_ARG_INFO()
5308 
5309 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_emptydir, 0, 0, 0)
5310 	ZEND_ARG_INFO(0, dirname)
5311 ZEND_END_ARG_INFO()
5312 
5313 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_extract, 0, 0, 1)
5314 	ZEND_ARG_INFO(0, pathto)
5315 	ZEND_ARG_INFO(0, files)
5316 	ZEND_ARG_INFO(0, overwrite)
5317 ZEND_END_ARG_INFO()
5318 
5319 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_addfile, 0, 0, 1)
5320 	ZEND_ARG_INFO(0, filename)
5321 	ZEND_ARG_INFO(0, localname)
5322 ZEND_END_ARG_INFO()
5323 
5324 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_fromstring, 0, 0, 1)
5325 	ZEND_ARG_INFO(0, localname)
5326 	ZEND_ARG_INFO(0, contents)
5327 ZEND_END_ARG_INFO()
5328 
5329 ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_isff, 0, 0, 1)
5330 	ZEND_ARG_INFO(0, fileformat)
5331 ZEND_END_ARG_INFO()
5332 
5333 ZEND_BEGIN_ARG_INFO(arginfo_phar__void, 0)
5334 ZEND_END_ARG_INFO()
5335 
5336 
5337 zend_function_entry php_archive_methods[] = {
5338 	PHP_ME(Phar, __construct,           arginfo_phar___construct,  ZEND_ACC_PUBLIC)
5339 	PHP_ME(Phar, __destruct,            arginfo_phar__void,        ZEND_ACC_PUBLIC)
5340 	PHP_ME(Phar, addEmptyDir,           arginfo_phar_emptydir,     ZEND_ACC_PUBLIC)
5341 	PHP_ME(Phar, addFile,               arginfo_phar_addfile,      ZEND_ACC_PUBLIC)
5342 	PHP_ME(Phar, addFromString,         arginfo_phar_fromstring,   ZEND_ACC_PUBLIC)
5343 	PHP_ME(Phar, buildFromDirectory,    arginfo_phar_fromdir,      ZEND_ACC_PUBLIC)
5344 	PHP_ME(Phar, buildFromIterator,     arginfo_phar_build,        ZEND_ACC_PUBLIC)
5345 	PHP_ME(Phar, compressFiles,         arginfo_phar_comp,         ZEND_ACC_PUBLIC)
5346 	PHP_ME(Phar, decompressFiles,       arginfo_phar__void,        ZEND_ACC_PUBLIC)
5347 	PHP_ME(Phar, compress,              arginfo_phar_comps,        ZEND_ACC_PUBLIC)
5348 	PHP_ME(Phar, decompress,            arginfo_phar_decomp,       ZEND_ACC_PUBLIC)
5349 	PHP_ME(Phar, convertToExecutable,   arginfo_phar_conv,         ZEND_ACC_PUBLIC)
5350 	PHP_ME(Phar, convertToData,         arginfo_phar_conv,         ZEND_ACC_PUBLIC)
5351 	PHP_ME(Phar, copy,                  arginfo_phar_copy,         ZEND_ACC_PUBLIC)
5352 	PHP_ME(Phar, count,                 arginfo_phar__void,        ZEND_ACC_PUBLIC)
5353 	PHP_ME(Phar, delete,                arginfo_phar_delete,       ZEND_ACC_PUBLIC)
5354 	PHP_ME(Phar, delMetadata,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
5355 	PHP_ME(Phar, extractTo,             arginfo_phar_extract,      ZEND_ACC_PUBLIC)
5356 	PHP_ME(Phar, getAlias,              arginfo_phar__void,        ZEND_ACC_PUBLIC)
5357 	PHP_ME(Phar, getPath,               arginfo_phar__void,        ZEND_ACC_PUBLIC)
5358 	PHP_ME(Phar, getMetadata,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
5359 	PHP_ME(Phar, getModified,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
5360 	PHP_ME(Phar, getSignature,          arginfo_phar__void,        ZEND_ACC_PUBLIC)
5361 	PHP_ME(Phar, getStub,               arginfo_phar__void,        ZEND_ACC_PUBLIC)
5362 	PHP_ME(Phar, getVersion,            arginfo_phar__void,        ZEND_ACC_PUBLIC)
5363 	PHP_ME(Phar, hasMetadata,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
5364 	PHP_ME(Phar, isBuffering,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
5365 	PHP_ME(Phar, isCompressed,          arginfo_phar__void,        ZEND_ACC_PUBLIC)
5366 	PHP_ME(Phar, isFileFormat,          arginfo_phar_isff,         ZEND_ACC_PUBLIC)
5367 	PHP_ME(Phar, isWritable,            arginfo_phar__void,        ZEND_ACC_PUBLIC)
5368 	PHP_ME(Phar, offsetExists,          arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
5369 	PHP_ME(Phar, offsetGet,             arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
5370 	PHP_ME(Phar, offsetSet,             arginfo_phar_offsetSet,    ZEND_ACC_PUBLIC)
5371 	PHP_ME(Phar, offsetUnset,           arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
5372 	PHP_ME(Phar, setAlias,              arginfo_phar_setAlias,     ZEND_ACC_PUBLIC)
5373 	PHP_ME(Phar, setDefaultStub,        arginfo_phar_createDS,     ZEND_ACC_PUBLIC)
5374 	PHP_ME(Phar, setMetadata,           arginfo_phar_setMetadata,  ZEND_ACC_PUBLIC)
5375 	PHP_ME(Phar, setSignatureAlgorithm, arginfo_phar_setSigAlgo,   ZEND_ACC_PUBLIC)
5376 	PHP_ME(Phar, setStub,               arginfo_phar_setStub,      ZEND_ACC_PUBLIC)
5377 	PHP_ME(Phar, startBuffering,        arginfo_phar__void,        ZEND_ACC_PUBLIC)
5378 	PHP_ME(Phar, stopBuffering,         arginfo_phar__void,        ZEND_ACC_PUBLIC)
5379 	/* static member functions */
5380 	PHP_ME(Phar, apiVersion,            arginfo_phar__void,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5381 	PHP_ME(Phar, canCompress,           arginfo_phar_cancompress,  ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5382 	PHP_ME(Phar, canWrite,              arginfo_phar__void,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5383 	PHP_ME(Phar, createDefaultStub,     arginfo_phar_createDS,     ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5384 	PHP_ME(Phar, getSupportedCompression,arginfo_phar__void,       ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5385 	PHP_ME(Phar, getSupportedSignatures,arginfo_phar__void,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5386 	PHP_ME(Phar, interceptFileFuncs,    arginfo_phar__void,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5387 	PHP_ME(Phar, isValidPharFilename,   arginfo_phar_isvalidpharfilename, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5388 	PHP_ME(Phar, loadPhar,              arginfo_phar_loadPhar,     ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5389 	PHP_ME(Phar, mapPhar,               arginfo_phar_mapPhar,      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5390 	PHP_ME(Phar, running,               arginfo_phar_running,      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5391 	PHP_ME(Phar, mount,                 arginfo_phar_mount,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5392 	PHP_ME(Phar, mungServer,            arginfo_phar_mungServer,   ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5393 	PHP_ME(Phar, unlinkArchive,         arginfo_phar_ua,           ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5394 	PHP_ME(Phar, webPhar,               arginfo_phar_webPhar,      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5395 	PHP_FE_END
5396 };
5397 
5398 
5399 ZEND_BEGIN_ARG_INFO_EX(arginfo_data___construct, 0, 0, 1)
5400     ZEND_ARG_INFO(0, filename)
5401     ZEND_ARG_INFO(0, flags)
5402     ZEND_ARG_INFO(0, alias)
5403     ZEND_ARG_INFO(0, fileformat)
5404 ZEND_END_ARG_INFO()
5405 
5406 zend_function_entry php_data_methods[] = {
5407     PHP_ME(Phar, __construct,           arginfo_data___construct,  ZEND_ACC_PUBLIC)
5408     PHP_ME(Phar, __destruct,            arginfo_phar__void,        ZEND_ACC_PUBLIC)
5409     PHP_ME(Phar, addEmptyDir,           arginfo_phar_emptydir,     ZEND_ACC_PUBLIC)
5410     PHP_ME(Phar, addFile,               arginfo_phar_addfile,      ZEND_ACC_PUBLIC)
5411     PHP_ME(Phar, addFromString,         arginfo_phar_fromstring,   ZEND_ACC_PUBLIC)
5412     PHP_ME(Phar, buildFromDirectory,    arginfo_phar_fromdir,      ZEND_ACC_PUBLIC)
5413     PHP_ME(Phar, buildFromIterator,     arginfo_phar_build,        ZEND_ACC_PUBLIC)
5414     PHP_ME(Phar, compressFiles,         arginfo_phar_comp,         ZEND_ACC_PUBLIC)
5415     PHP_ME(Phar, decompressFiles,       arginfo_phar__void,        ZEND_ACC_PUBLIC)
5416     PHP_ME(Phar, compress,              arginfo_phar_comps,        ZEND_ACC_PUBLIC)
5417     PHP_ME(Phar, decompress,            arginfo_phar_decomp,       ZEND_ACC_PUBLIC)
5418     PHP_ME(Phar, convertToExecutable,   arginfo_phar_conv,         ZEND_ACC_PUBLIC)
5419     PHP_ME(Phar, convertToData,         arginfo_phar_conv,         ZEND_ACC_PUBLIC)
5420     PHP_ME(Phar, copy,                  arginfo_phar_copy,         ZEND_ACC_PUBLIC)
5421     PHP_ME(Phar, count,                 arginfo_phar__void,        ZEND_ACC_PUBLIC)
5422     PHP_ME(Phar, delete,                arginfo_phar_delete,       ZEND_ACC_PUBLIC)
5423     PHP_ME(Phar, delMetadata,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
5424     PHP_ME(Phar, extractTo,             arginfo_phar_extract,      ZEND_ACC_PUBLIC)
5425     PHP_ME(Phar, getAlias,              arginfo_phar__void,        ZEND_ACC_PUBLIC)
5426     PHP_ME(Phar, getPath,               arginfo_phar__void,        ZEND_ACC_PUBLIC)
5427     PHP_ME(Phar, getMetadata,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
5428     PHP_ME(Phar, getModified,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
5429     PHP_ME(Phar, getSignature,          arginfo_phar__void,        ZEND_ACC_PUBLIC)
5430     PHP_ME(Phar, getStub,               arginfo_phar__void,        ZEND_ACC_PUBLIC)
5431     PHP_ME(Phar, getVersion,            arginfo_phar__void,        ZEND_ACC_PUBLIC)
5432     PHP_ME(Phar, hasMetadata,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
5433     PHP_ME(Phar, isBuffering,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
5434     PHP_ME(Phar, isCompressed,          arginfo_phar__void,        ZEND_ACC_PUBLIC)
5435     PHP_ME(Phar, isFileFormat,          arginfo_phar_isff,         ZEND_ACC_PUBLIC)
5436     PHP_ME(Phar, isWritable,            arginfo_phar__void,        ZEND_ACC_PUBLIC)
5437     PHP_ME(Phar, offsetExists,          arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
5438     PHP_ME(Phar, offsetGet,             arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
5439     PHP_ME(Phar, offsetSet,             arginfo_phar_offsetSet,    ZEND_ACC_PUBLIC)
5440     PHP_ME(Phar, offsetUnset,           arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
5441     PHP_ME(Phar, setAlias,              arginfo_phar_setAlias,     ZEND_ACC_PUBLIC)
5442     PHP_ME(Phar, setDefaultStub,        arginfo_phar_createDS,     ZEND_ACC_PUBLIC)
5443     PHP_ME(Phar, setMetadata,           arginfo_phar_setMetadata,  ZEND_ACC_PUBLIC)
5444     PHP_ME(Phar, setSignatureAlgorithm, arginfo_phar_setSigAlgo,   ZEND_ACC_PUBLIC)
5445     PHP_ME(Phar, setStub,               arginfo_phar_setStub,      ZEND_ACC_PUBLIC)
5446     PHP_ME(Phar, startBuffering,        arginfo_phar__void,        ZEND_ACC_PUBLIC)
5447     PHP_ME(Phar, stopBuffering,         arginfo_phar__void,        ZEND_ACC_PUBLIC)
5448     /* static member functions */
5449     PHP_ME(Phar, apiVersion,            arginfo_phar__void,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5450     PHP_ME(Phar, canCompress,           arginfo_phar_cancompress,  ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5451     PHP_ME(Phar, canWrite,              arginfo_phar__void,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5452     PHP_ME(Phar, createDefaultStub,     arginfo_phar_createDS,     ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5453     PHP_ME(Phar, getSupportedCompression,arginfo_phar__void,       ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5454     PHP_ME(Phar, getSupportedSignatures,arginfo_phar__void,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5455     PHP_ME(Phar, interceptFileFuncs,    arginfo_phar__void,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5456     PHP_ME(Phar, isValidPharFilename,   arginfo_phar_isvalidpharfilename, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5457     PHP_ME(Phar, loadPhar,              arginfo_phar_loadPhar,     ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5458     PHP_ME(Phar, mapPhar,               arginfo_phar_mapPhar,      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5459     PHP_ME(Phar, running,               arginfo_phar_running,      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5460     PHP_ME(Phar, mount,                 arginfo_phar_mount,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5461     PHP_ME(Phar, mungServer,            arginfo_phar_mungServer,   ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5462     PHP_ME(Phar, unlinkArchive,         arginfo_phar_ua,           ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5463     PHP_ME(Phar, webPhar,               arginfo_phar_webPhar,      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
5464     PHP_FE_END
5465 };
5466 
5467 ZEND_BEGIN_ARG_INFO_EX(arginfo_entry___construct, 0, 0, 1)
5468 	ZEND_ARG_INFO(0, filename)
5469 ZEND_END_ARG_INFO()
5470 
5471 ZEND_BEGIN_ARG_INFO_EX(arginfo_entry_chmod, 0, 0, 1)
5472 	ZEND_ARG_INFO(0, perms)
5473 ZEND_END_ARG_INFO()
5474 
5475 zend_function_entry php_entry_methods[] = {
5476 	PHP_ME(PharFileInfo, __construct,        arginfo_entry___construct,  ZEND_ACC_PUBLIC)
5477 	PHP_ME(PharFileInfo, __destruct,         arginfo_phar__void,         ZEND_ACC_PUBLIC)
5478 	PHP_ME(PharFileInfo, chmod,              arginfo_entry_chmod,        ZEND_ACC_PUBLIC)
5479 	PHP_ME(PharFileInfo, compress,           arginfo_phar_comp,          ZEND_ACC_PUBLIC)
5480 	PHP_ME(PharFileInfo, decompress,         arginfo_phar__void,         ZEND_ACC_PUBLIC)
5481 	PHP_ME(PharFileInfo, delMetadata,        arginfo_phar__void,         ZEND_ACC_PUBLIC)
5482 	PHP_ME(PharFileInfo, getCompressedSize,  arginfo_phar__void,         ZEND_ACC_PUBLIC)
5483 	PHP_ME(PharFileInfo, getCRC32,           arginfo_phar__void,         ZEND_ACC_PUBLIC)
5484 	PHP_ME(PharFileInfo, getContent,         arginfo_phar__void,         ZEND_ACC_PUBLIC)
5485 	PHP_ME(PharFileInfo, getMetadata,        arginfo_phar__void,         ZEND_ACC_PUBLIC)
5486 	PHP_ME(PharFileInfo, getPharFlags,       arginfo_phar__void,         ZEND_ACC_PUBLIC)
5487 	PHP_ME(PharFileInfo, hasMetadata,        arginfo_phar__void,         ZEND_ACC_PUBLIC)
5488 	PHP_ME(PharFileInfo, isCompressed,       arginfo_phar_compo,         ZEND_ACC_PUBLIC)
5489 	PHP_ME(PharFileInfo, isCRCChecked,       arginfo_phar__void,         ZEND_ACC_PUBLIC)
5490 	PHP_ME(PharFileInfo, setMetadata,        arginfo_phar_setMetadata,   ZEND_ACC_PUBLIC)
5491 	PHP_FE_END
5492 };
5493 
5494 zend_function_entry phar_exception_methods[] = {
5495 	PHP_FE_END
5496 };
5497 /* }}} */
5498 
5499 #define REGISTER_PHAR_CLASS_CONST_LONG(class_name, const_name, value) \
5500 	zend_declare_class_constant_long(class_name, const_name, sizeof(const_name)-1, (zend_long)value);
5501 
phar_object_init(void)5502 void phar_object_init(void) /* {{{ */
5503 {
5504 	zend_class_entry ce;
5505 
5506 	INIT_CLASS_ENTRY(ce, "PharException", phar_exception_methods);
5507 	phar_ce_PharException = zend_register_internal_class_ex(&ce, zend_ce_exception);
5508 
5509 	INIT_CLASS_ENTRY(ce, "Phar", php_archive_methods);
5510 	phar_ce_archive = zend_register_internal_class_ex(&ce, spl_ce_RecursiveDirectoryIterator);
5511 
5512 	zend_class_implements(phar_ce_archive, 2, zend_ce_countable, zend_ce_arrayaccess);
5513 
5514 	INIT_CLASS_ENTRY(ce, "PharData", php_data_methods);
5515 	phar_ce_data = zend_register_internal_class_ex(&ce, spl_ce_RecursiveDirectoryIterator);
5516 
5517 	zend_class_implements(phar_ce_data, 2, zend_ce_countable, zend_ce_arrayaccess);
5518 
5519 	INIT_CLASS_ENTRY(ce, "PharFileInfo", php_entry_methods);
5520 	phar_ce_entry = zend_register_internal_class_ex(&ce, spl_ce_SplFileInfo);
5521 
5522 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "BZ2", PHAR_ENT_COMPRESSED_BZ2)
5523 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "GZ", PHAR_ENT_COMPRESSED_GZ)
5524 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "NONE", PHAR_ENT_COMPRESSED_NONE)
5525 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "PHAR", PHAR_FORMAT_PHAR)
5526 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "TAR", PHAR_FORMAT_TAR)
5527 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "ZIP", PHAR_FORMAT_ZIP)
5528 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "COMPRESSED", PHAR_ENT_COMPRESSION_MASK)
5529 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "PHP", PHAR_MIME_PHP)
5530 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "PHPS", PHAR_MIME_PHPS)
5531 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "MD5", PHAR_SIG_MD5)
5532 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "OPENSSL", PHAR_SIG_OPENSSL)
5533 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA1", PHAR_SIG_SHA1)
5534 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA256", PHAR_SIG_SHA256)
5535 	REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA512", PHAR_SIG_SHA512)
5536 }
5537 /* }}} */
5538 
5539 /*
5540  * Local variables:
5541  * tab-width: 4
5542  * c-basic-offset: 4
5543  * End:
5544  * vim600: noet sw=4 ts=4 fdm=marker
5545  * vim<600: noet sw=4 ts=4
5546  */
5547