PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/phar - phar_object.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 1925 2474 77.8 %
Date: 2015-09-02 Functions: 90 91 98.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Wed, 02 Sep 2015 17:19:16 +0000 (2 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.