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

Generated by: LCOV version 1.10

Generated at Sun, 18 Jan 2015 09:00:45 +0000 (7 days ago)

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