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 2447 77.7 %
Date: 2015-04-14 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 :         if ((oldname = zend_memrchr(phar->fname, '/', phar->fname_len))) {
    2070          51 :                 ++oldname;
    2071             :         } else {
    2072           0 :                 oldname = phar->fname;
    2073             :         }
    2074          51 :         oldname_len = strlen(oldname);
    2075             : 
    2076          51 :         basename = estrndup(oldname, oldname_len);
    2077          51 :         spprintf(&newname, 0, "%s.%s", strtok(basename, "."), ext);
    2078          51 :         efree(basename);
    2079             : 
    2080             : 
    2081             : 
    2082          51 :         basepath = estrndup(oldpath, (strlen(oldpath) - oldname_len));
    2083          51 :         phar->fname_len = spprintf(&newpath, 0, "%s%s", basepath, newname);
    2084          51 :         phar->fname = newpath;
    2085          51 :         phar->ext = newpath + phar->fname_len - strlen(ext) - 1;
    2086          51 :         efree(basepath);
    2087          51 :         efree(newname);
    2088             : 
    2089          51 :         if (PHAR_G(manifest_cached) && NULL != (pphar = zend_hash_str_find_ptr(&cached_phars, newpath, phar->fname_len))) {
    2090           0 :                 efree(oldpath);
    2091           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);
    2092           0 :                 return NULL;
    2093             :         }
    2094             : 
    2095         102 :         if (NULL != (pphar = zend_hash_str_find_ptr(&(PHAR_GLOBALS->phar_fname_map), newpath, phar->fname_len))) {
    2096           1 :                 if (pphar->fname_len == phar->fname_len && !memcmp(pphar->fname, phar->fname, phar->fname_len)) {
    2097           1 :                         if (!zend_hash_num_elements(&phar->manifest)) {
    2098           0 :                                 pphar->is_tar = phar->is_tar;
    2099           0 :                                 pphar->is_zip = phar->is_zip;
    2100           0 :                                 pphar->is_data = phar->is_data;
    2101           0 :                                 pphar->flags = phar->flags;
    2102           0 :                                 pphar->fp = phar->fp;
    2103           0 :                                 phar->fp = NULL;
    2104           0 :                                 phar_destroy_phar_data(phar);
    2105           0 :                                 phar = pphar;
    2106           0 :                                 phar->refcount++;
    2107           0 :                                 newpath = oldpath;
    2108           0 :                                 goto its_ok;
    2109             :                         }
    2110             :                 }
    2111             : 
    2112           1 :                 efree(oldpath);
    2113           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);
    2114           1 :                 return NULL;
    2115             :         }
    2116             : its_ok:
    2117          50 :         if (SUCCESS == php_stream_stat_path(newpath, &ssb)) {
    2118           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "phar \"%s\" exists and must be unlinked prior to conversion", newpath);
    2119           1 :                 efree(oldpath);
    2120           1 :                 return NULL;
    2121             :         }
    2122          49 :         if (!phar->is_data) {
    2123          35 :                 if (SUCCESS != phar_detect_phar_fname_ext(newpath, phar->fname_len, (const char **) &(phar->ext), &(phar->ext_len), 1, 1, 1)) {
    2124           1 :                         efree(oldpath);
    2125           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "phar \"%s\" has invalid extension %s", phar->fname, ext);
    2126           1 :                         return NULL;
    2127             :                 }
    2128             : 
    2129          34 :                 if (phar->alias) {
    2130          22 :                         if (phar->is_temporary_alias) {
    2131          15 :                                 phar->alias = NULL;
    2132          15 :                                 phar->alias_len = 0;
    2133             :                         } else {
    2134           7 :                                 phar->alias = estrndup(newpath, strlen(newpath));
    2135           7 :                                 phar->alias_len = strlen(newpath);
    2136           7 :                                 phar->is_temporary_alias = 1;
    2137           7 :                                 zend_hash_str_update_ptr(&(PHAR_GLOBALS->phar_alias_map), newpath, phar->fname_len, phar);
    2138             :                         }
    2139             :                 }
    2140             : 
    2141             :         } else {
    2142             : 
    2143          14 :                 if (SUCCESS != phar_detect_phar_fname_ext(newpath, phar->fname_len, (const char **) &(phar->ext), &(phar->ext_len), 0, 1, 1)) {
    2144           3 :                         efree(oldpath);
    2145           3 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "data phar \"%s\" has invalid extension %s", phar->fname, ext);
    2146           3 :                         return NULL;
    2147             :                 }
    2148             : 
    2149          11 :                 phar->alias = NULL;
    2150          11 :                 phar->alias_len = 0;
    2151             :         }
    2152             : 
    2153          90 :         if ((!pphar || phar == pphar) && NULL == zend_hash_str_update_ptr(&(PHAR_GLOBALS->phar_fname_map), newpath, phar->fname_len, phar)) {
    2154           0 :                 efree(oldpath);
    2155           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to add newly converted phar \"%s\" to the list of phars", phar->fname);
    2156           0 :                 return NULL;
    2157             :         }
    2158             : 
    2159          45 :         phar_flush(phar, 0, 0, 1, &error);
    2160             : 
    2161          45 :         if (error) {
    2162           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "%s", error);
    2163           0 :                 efree(error);
    2164           0 :                 efree(oldpath);
    2165           0 :                 return NULL;
    2166             :         }
    2167             : 
    2168          45 :         efree(oldpath);
    2169             : 
    2170          45 :         if (phar->is_data) {
    2171          11 :                 ce = phar_ce_data;
    2172             :         } else {
    2173          34 :                 ce = phar_ce_archive;
    2174             :         }
    2175             : 
    2176          45 :         ZVAL_NULL(&ret);
    2177          45 :         if (SUCCESS != object_init_ex(&ret, ce)) {
    2178             :                 zval_dtor(&ret);
    2179           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Unable to instantiate phar object when converting archive \"%s\"", phar->fname);
    2180           0 :                 return NULL;
    2181             :         }
    2182             : 
    2183          90 :         ZVAL_STRINGL(&arg1, phar->fname, phar->fname_len);
    2184             : 
    2185          45 :         zend_call_method_with_1_params(&ret, ce, &ce->constructor, "__construct", NULL, &arg1);
    2186          45 :         zval_ptr_dtor(&arg1);
    2187          45 :         return Z_OBJ(ret);
    2188             : }
    2189             : /* }}} */
    2190             : 
    2191          51 : static zend_object *phar_convert_to_other(phar_archive_data *source, int convert, char *ext, php_uint32 flags) /* {{{ */
    2192             : {
    2193             :         phar_archive_data *phar;
    2194             :         phar_entry_info *entry, newentry;
    2195             :         zend_object *ret;
    2196             : 
    2197             :         /* invalidate phar cache */
    2198          51 :         PHAR_G(last_phar) = NULL;
    2199          51 :         PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
    2200             : 
    2201          51 :         phar = (phar_archive_data *) ecalloc(1, sizeof(phar_archive_data));
    2202             :         /* set whole-archive compression and type from parameter */
    2203          51 :         phar->flags = flags;
    2204          51 :         phar->is_data = source->is_data;
    2205             : 
    2206          51 :         switch (convert) {
    2207             :                 case PHAR_FORMAT_TAR:
    2208          26 :                         phar->is_tar = 1;
    2209          26 :                         break;
    2210             :                 case PHAR_FORMAT_ZIP:
    2211          13 :                         phar->is_zip = 1;
    2212          13 :                         break;
    2213             :                 default:
    2214          12 :                         phar->is_data = 0;
    2215             :                         break;
    2216             :         }
    2217             : 
    2218          51 :         zend_hash_init(&(phar->manifest), sizeof(phar_entry_info),
    2219             :                 zend_get_hash_value, destroy_phar_manifest_entry, 0);
    2220          51 :         zend_hash_init(&phar->mounted_dirs, sizeof(char *),
    2221             :                 zend_get_hash_value, NULL, 0);
    2222          51 :         zend_hash_init(&phar->virtual_dirs, sizeof(char *),
    2223             :                 zend_get_hash_value, NULL, 0);
    2224             : 
    2225          51 :         phar->fp = php_stream_fopen_tmpfile();
    2226          51 :         if (phar->fp == NULL) {
    2227           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "unable to create temporary file");
    2228           0 :                 return NULL;
    2229             :         }
    2230          51 :         phar->fname = source->fname;
    2231          51 :         phar->fname_len = source->fname_len;
    2232          51 :         phar->is_temporary_alias = source->is_temporary_alias;
    2233          51 :         phar->alias = source->alias;
    2234             : 
    2235         102 :         if (Z_TYPE(source->metadata) != IS_UNDEF) {
    2236           2 :                 ZVAL_DUP(&phar->metadata, &source->metadata);
    2237           2 :                 phar->metadata_len = 0;
    2238             :         }
    2239             : 
    2240             :         /* first copy each file's uncompressed contents to a temporary file and set per-file flags */
    2241         393 :         ZEND_HASH_FOREACH_PTR(&source->manifest, entry) {
    2242             : 
    2243         171 :                 newentry = *entry;
    2244             : 
    2245         171 :                 if (newentry.link) {
    2246           2 :                         newentry.link = estrdup(newentry.link);
    2247           2 :                         goto no_copy;
    2248             :                 }
    2249             : 
    2250         169 :                 if (newentry.tmp) {
    2251           2 :                         newentry.tmp = estrdup(newentry.tmp);
    2252           2 :                         goto no_copy;
    2253             :                 }
    2254             : 
    2255         167 :                 newentry.metadata_str.s = NULL;
    2256             : 
    2257         167 :                 if (FAILURE == phar_copy_file_contents(&newentry, phar->fp)) {
    2258           0 :                         zend_hash_destroy(&(phar->manifest));
    2259           0 :                         php_stream_close(phar->fp);
    2260           0 :                         efree(phar);
    2261             :                         /* exception already thrown */
    2262           0 :                         return NULL;
    2263             :                 }
    2264             : no_copy:
    2265         171 :                 newentry.filename = estrndup(newentry.filename, newentry.filename_len);
    2266             : 
    2267         171 :                 if (Z_TYPE(newentry.metadata) != IS_UNDEF) {
    2268             :                         zval_copy_ctor(&newentry.metadata);
    2269           8 :                         newentry.metadata_str.s = NULL;
    2270             :                 }
    2271             : 
    2272         171 :                 newentry.is_zip = phar->is_zip;
    2273         171 :                 newentry.is_tar = phar->is_tar;
    2274             : 
    2275         171 :                 if (newentry.is_tar) {
    2276          96 :                         newentry.tar_type = (entry->is_dir ? TAR_DIR : TAR_FILE);
    2277             :                 }
    2278             : 
    2279         171 :                 newentry.is_modified = 1;
    2280         171 :                 newentry.phar = phar;
    2281         171 :                 newentry.old_flags = newentry.flags & ~PHAR_ENT_COMPRESSION_MASK; /* remove compression from old_flags */
    2282         171 :                 phar_set_inode(&newentry);
    2283         171 :                 zend_hash_str_add_mem(&(phar->manifest), newentry.filename, newentry.filename_len, (void*)&newentry, sizeof(phar_entry_info));
    2284         171 :                 phar_add_virtual_dirs(phar, newentry.filename, newentry.filename_len);
    2285             :         } ZEND_HASH_FOREACH_END();
    2286             : 
    2287          51 :         if ((ret = phar_rename_archive(phar, ext, 0))) {
    2288          45 :                 return ret;
    2289             :         } else {
    2290           6 :                 zend_hash_destroy(&(phar->manifest));
    2291           6 :                 zend_hash_destroy(&(phar->mounted_dirs));
    2292           6 :                 zend_hash_destroy(&(phar->virtual_dirs));
    2293           6 :                 php_stream_close(phar->fp);
    2294           6 :                 efree(phar->fname);
    2295           6 :                 efree(phar);
    2296           6 :                 return NULL;
    2297             :         }
    2298             : }
    2299             : /* }}} */
    2300             : 
    2301             : /* {{{ proto object Phar::convertToExecutable([int format[, int compression [, string file_ext]]])
    2302             :  * Convert a phar.tar or phar.zip archive to the phar file format. The
    2303             :  * optional parameter allows the user to determine the new
    2304             :  * filename extension (default is phar).
    2305             :  */
    2306          37 : PHP_METHOD(Phar, convertToExecutable)
    2307             : {
    2308          37 :         char *ext = NULL;
    2309             :         int is_data;
    2310          37 :         size_t ext_len = 0;
    2311             :         php_uint32 flags;
    2312             :         zend_object *ret;
    2313             :         /* a number that is not 0, 1 or 2 (Which is also Greg's birthday, so there) */
    2314          37 :         zend_long format = 9021976, method = 9021976;
    2315          37 :         PHAR_ARCHIVE_OBJECT();
    2316             : 
    2317          37 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|lls", &format, &method, &ext, &ext_len) == FAILURE) {
    2318           1 :                 return;
    2319             :         }
    2320             : 
    2321          36 :         if (PHAR_G(readonly)) {
    2322           2 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2323             :                         "Cannot write out executable phar archive, phar is read-only");
    2324           2 :                 return;
    2325             :         }
    2326             : 
    2327          34 :         switch (format) {
    2328             :                 case 9021976:
    2329             :                 case PHAR_FORMAT_SAME: /* null is converted to 0 */
    2330             :                         /* by default, use the existing format */
    2331           2 :                         if (phar_obj->archive->is_tar) {
    2332           2 :                                 format = PHAR_FORMAT_TAR;
    2333           0 :                         } else if (phar_obj->archive->is_zip) {
    2334           0 :                                 format = PHAR_FORMAT_ZIP;
    2335             :                         } else {
    2336           0 :                                 format = PHAR_FORMAT_PHAR;
    2337             :                         }
    2338           2 :                         break;
    2339             :                 case PHAR_FORMAT_PHAR:
    2340             :                 case PHAR_FORMAT_TAR:
    2341             :                 case PHAR_FORMAT_ZIP:
    2342          31 :                         break;
    2343             :                 default:
    2344           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2345             :                                 "Unknown file format specified, please pass one of Phar::PHAR, Phar::TAR or Phar::ZIP");
    2346           1 :                         return;
    2347             :         }
    2348             : 
    2349          33 :         switch (method) {
    2350             :                 case 9021976:
    2351          14 :                         flags = phar_obj->archive->flags & PHAR_FILE_COMPRESSION_MASK;
    2352          14 :                         break;
    2353             :                 case 0:
    2354           9 :                         flags = PHAR_FILE_COMPRESSED_NONE;
    2355           9 :                         break;
    2356             :                 case PHAR_ENT_COMPRESSED_GZ:
    2357           6 :                         if (format == PHAR_FORMAT_ZIP) {
    2358           1 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2359             :                                         "Cannot compress entire archive with gzip, zip archives do not support whole-archive compression");
    2360           1 :                                 return;
    2361             :                         }
    2362             : 
    2363           5 :                         if (!PHAR_G(has_zlib)) {
    2364           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2365             :                                         "Cannot compress entire archive with gzip, enable ext/zlib in php.ini");
    2366           0 :                                 return;
    2367             :                         }
    2368             : 
    2369           5 :                         flags = PHAR_FILE_COMPRESSED_GZ;
    2370           5 :                         break;
    2371             :                 case PHAR_ENT_COMPRESSED_BZ2:
    2372           3 :                         if (format == PHAR_FORMAT_ZIP) {
    2373           1 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2374             :                                         "Cannot compress entire archive with bz2, zip archives do not support whole-archive compression");
    2375           1 :                                 return;
    2376             :                         }
    2377             : 
    2378           2 :                         if (!PHAR_G(has_bz2)) {
    2379           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2380             :                                         "Cannot compress entire archive with bz2, enable ext/bz2 in php.ini");
    2381           0 :                                 return;
    2382             :                         }
    2383             : 
    2384           2 :                         flags = PHAR_FILE_COMPRESSED_BZ2;
    2385           2 :                         break;
    2386             :                 default:
    2387           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2388             :                                 "Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
    2389           1 :                         return;
    2390             :         }
    2391             : 
    2392          30 :         is_data = phar_obj->archive->is_data;
    2393          30 :         phar_obj->archive->is_data = 0;
    2394          30 :         ret = phar_convert_to_other(phar_obj->archive, format, ext, flags);
    2395          30 :         phar_obj->archive->is_data = is_data;
    2396             : 
    2397          30 :         if (ret) {
    2398          28 :                 ZVAL_OBJ(return_value, ret);
    2399             :         } else {
    2400           2 :                 RETURN_NULL();
    2401             :         }
    2402             : }
    2403             : /* }}} */
    2404             : 
    2405             : /* {{{ proto object Phar::convertToData([int format[, int compression [, string file_ext]]])
    2406             :  * Convert an archive to a non-executable .tar or .zip.
    2407             :  * The optional parameter allows the user to determine the new
    2408             :  * filename extension (default is .zip or .tar).
    2409             :  */
    2410          22 : PHP_METHOD(Phar, convertToData)
    2411             : {
    2412          22 :         char *ext = NULL;
    2413             :         int is_data;
    2414          22 :         size_t ext_len = 0;
    2415             :         php_uint32 flags;
    2416             :         zend_object *ret;
    2417             :         /* a number that is not 0, 1 or 2 (Which is also Greg's birthday so there) */
    2418          22 :         zend_long format = 9021976, method = 9021976;
    2419          22 :         PHAR_ARCHIVE_OBJECT();
    2420             : 
    2421          22 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|lls", &format, &method, &ext, &ext_len) == FAILURE) {
    2422           1 :                 return;
    2423             :         }
    2424             : 
    2425          21 :         switch (format) {
    2426             :                 case 9021976:
    2427             :                 case PHAR_FORMAT_SAME: /* null is converted to 0 */
    2428             :                         /* by default, use the existing format */
    2429           3 :                         if (phar_obj->archive->is_tar) {
    2430           1 :                                 format = PHAR_FORMAT_TAR;
    2431           2 :                         } else if (phar_obj->archive->is_zip) {
    2432           1 :                                 format = PHAR_FORMAT_ZIP;
    2433             :                         } else {
    2434           1 :                                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2435             :                                         "Cannot write out data phar archive, use Phar::TAR or Phar::ZIP");
    2436           1 :                                 return;
    2437             :                         }
    2438           2 :                         break;
    2439             :                 case PHAR_FORMAT_PHAR:
    2440           1 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2441             :                                 "Cannot write out data phar archive, use Phar::TAR or Phar::ZIP");
    2442           1 :                         return;
    2443             :                 case PHAR_FORMAT_TAR:
    2444             :                 case PHAR_FORMAT_ZIP:
    2445          16 :                         break;
    2446             :                 default:
    2447           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2448             :                                 "Unknown file format specified, please pass one of Phar::TAR or Phar::ZIP");
    2449           1 :                         return;
    2450             :         }
    2451             : 
    2452          18 :         switch (method) {
    2453             :                 case 9021976:
    2454           7 :                         flags = phar_obj->archive->flags & PHAR_FILE_COMPRESSION_MASK;
    2455           7 :                         break;
    2456             :                 case 0:
    2457           3 :                         flags = PHAR_FILE_COMPRESSED_NONE;
    2458           3 :                         break;
    2459             :                 case PHAR_ENT_COMPRESSED_GZ:
    2460           5 :                         if (format == PHAR_FORMAT_ZIP) {
    2461           1 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2462             :                                         "Cannot compress entire archive with gzip, zip archives do not support whole-archive compression");
    2463           1 :                                 return;
    2464             :                         }
    2465             : 
    2466           4 :                         if (!PHAR_G(has_zlib)) {
    2467           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2468             :                                         "Cannot compress entire archive with gzip, enable ext/zlib in php.ini");
    2469           0 :                                 return;
    2470             :                         }
    2471             : 
    2472           4 :                         flags = PHAR_FILE_COMPRESSED_GZ;
    2473           4 :                         break;
    2474             :                 case PHAR_ENT_COMPRESSED_BZ2:
    2475           2 :                         if (format == PHAR_FORMAT_ZIP) {
    2476           1 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2477             :                                         "Cannot compress entire archive with bz2, zip archives do not support whole-archive compression");
    2478           1 :                                 return;
    2479             :                         }
    2480             : 
    2481           1 :                         if (!PHAR_G(has_bz2)) {
    2482           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2483             :                                         "Cannot compress entire archive with bz2, enable ext/bz2 in php.ini");
    2484           0 :                                 return;
    2485             :                         }
    2486             : 
    2487           1 :                         flags = PHAR_FILE_COMPRESSED_BZ2;
    2488           1 :                         break;
    2489             :                 default:
    2490           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    2491             :                                 "Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
    2492           1 :                         return;
    2493             :         }
    2494             : 
    2495          15 :         is_data = phar_obj->archive->is_data;
    2496          15 :         phar_obj->archive->is_data = 1;
    2497          15 :         ret = phar_convert_to_other(phar_obj->archive, format, ext, flags);
    2498          15 :         phar_obj->archive->is_data = is_data;
    2499             : 
    2500          15 :         if (ret) {
    2501          11 :                 ZVAL_OBJ(return_value, ret);
    2502             :         } else {
    2503           4 :                 RETURN_NULL();
    2504             :         }
    2505             : }
    2506             : /* }}} */
    2507             : 
    2508             : /* {{{ proto int|false Phar::isCompressed()
    2509             :  * Returns Phar::GZ or PHAR::BZ2 if the entire archive is compressed
    2510             :  * (.tar.gz/tar.bz2 and so on), or FALSE otherwise.
    2511             :  */
    2512          19 : PHP_METHOD(Phar, isCompressed)
    2513             : {
    2514          19 :         PHAR_ARCHIVE_OBJECT();
    2515             : 
    2516          19 :         if (zend_parse_parameters_none() == FAILURE) {
    2517           0 :                 return;
    2518             :         }
    2519             : 
    2520          19 :         if (phar_obj->archive->flags & PHAR_FILE_COMPRESSED_GZ) {
    2521          10 :                 RETURN_LONG(PHAR_ENT_COMPRESSED_GZ);
    2522             :         }
    2523             : 
    2524           9 :         if (phar_obj->archive->flags & PHAR_FILE_COMPRESSED_BZ2) {
    2525           7 :                 RETURN_LONG(PHAR_ENT_COMPRESSED_BZ2);
    2526             :         }
    2527             : 
    2528           2 :         RETURN_FALSE;
    2529             : }
    2530             : /* }}} */
    2531             : 
    2532             : /* {{{ proto bool Phar::isWritable()
    2533             :  * Returns true if phar.readonly=0 or phar is a PharData AND the actual file is writable.
    2534             :  */
    2535           8 : PHP_METHOD(Phar, isWritable)
    2536             : {
    2537             :         php_stream_statbuf ssb;
    2538           8 :         PHAR_ARCHIVE_OBJECT();
    2539             : 
    2540           8 :         if (zend_parse_parameters_none() == FAILURE) {
    2541           0 :                 return;
    2542             :         }
    2543             : 
    2544           8 :         if (!phar_obj->archive->is_writeable) {
    2545           3 :                 RETURN_FALSE;
    2546             :         }
    2547             : 
    2548           5 :         if (SUCCESS != php_stream_stat_path(phar_obj->archive->fname, &ssb)) {
    2549           1 :                 if (phar_obj->archive->is_brandnew) {
    2550             :                         /* assume it works if the file doesn't exist yet */
    2551           1 :                         RETURN_TRUE;
    2552             :                 }
    2553           0 :                 RETURN_FALSE;
    2554             :         }
    2555             : 
    2556           4 :         RETURN_BOOL((ssb.sb.st_mode & (S_IWOTH | S_IWGRP | S_IWUSR)) != 0);
    2557             : }
    2558             : /* }}} */
    2559             : 
    2560             : /* {{{ proto bool Phar::delete(string entry)
    2561             :  * Deletes a named file within the archive.
    2562             :  */
    2563           7 : PHP_METHOD(Phar, delete)
    2564             : {
    2565             :         char *fname;
    2566             :         size_t fname_len;
    2567             :         char *error;
    2568             :         phar_entry_info *entry;
    2569           7 :         PHAR_ARCHIVE_OBJECT();
    2570             : 
    2571           7 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    2572           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2573             :                         "Cannot write out phar archive, phar is read-only");
    2574           1 :                 return;
    2575             :         }
    2576             : 
    2577           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &fname, &fname_len) == FAILURE) {
    2578           1 :                 RETURN_FALSE;
    2579             :         }
    2580             : 
    2581           5 :         if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    2582           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    2583           0 :                 return;
    2584             :         }
    2585           5 :         if (zend_hash_str_exists(&phar_obj->archive->manifest, fname, (uint) fname_len)) {
    2586           8 :                 if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, (uint) fname_len))) {
    2587           4 :                         if (entry->is_deleted) {
    2588             :                                 /* entry is deleted, but has not been flushed to disk yet */
    2589           0 :                                 RETURN_TRUE;
    2590             :                         } else {
    2591           4 :                                 entry->is_deleted = 1;
    2592           4 :                                 entry->is_modified = 1;
    2593           4 :                                 phar_obj->archive->is_modified = 1;
    2594             :                         }
    2595             :                 }
    2596             :         } else {
    2597           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist and cannot be deleted", fname);
    2598           1 :                 RETURN_FALSE;
    2599             :         }
    2600             : 
    2601           4 :         phar_flush(phar_obj->archive, NULL, 0, 0, &error);
    2602           4 :         if (error) {
    2603           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    2604           0 :                 efree(error);
    2605             :         }
    2606             : 
    2607           4 :         RETURN_TRUE;
    2608             : }
    2609             : /* }}} */
    2610             : 
    2611             : /* {{{ proto int Phar::getAlias()
    2612             :  * Returns the alias for the Phar or NULL.
    2613             :  */
    2614          42 : PHP_METHOD(Phar, getAlias)
    2615             : {
    2616          42 :         PHAR_ARCHIVE_OBJECT();
    2617             : 
    2618          42 :         if (zend_parse_parameters_none() == FAILURE) {
    2619           0 :                 return;
    2620             :         }
    2621             : 
    2622          42 :         if (phar_obj->archive->alias && phar_obj->archive->alias != phar_obj->archive->fname) {
    2623          66 :                 RETURN_STRINGL(phar_obj->archive->alias, phar_obj->archive->alias_len);
    2624             :         }
    2625             : }
    2626             : /* }}} */
    2627             : 
    2628             : /* {{{ proto int Phar::getPath()
    2629             :  * Returns the real path to the phar archive on disk
    2630             :  */
    2631          15 : PHP_METHOD(Phar, getPath)
    2632             : {
    2633          15 :         PHAR_ARCHIVE_OBJECT();
    2634             : 
    2635          15 :         if (zend_parse_parameters_none() == FAILURE) {
    2636           0 :                 return;
    2637             :         }
    2638             : 
    2639          30 :         RETURN_STRINGL(phar_obj->archive->fname, phar_obj->archive->fname_len);
    2640             : }
    2641             : /* }}} */
    2642             : 
    2643             : /* {{{ proto bool Phar::setAlias(string alias)
    2644             :  * Sets the alias for a Phar archive. The default value is the full path
    2645             :  * to the archive.
    2646             :  */
    2647          53 : PHP_METHOD(Phar, setAlias)
    2648             : {
    2649             :         char *alias, *error, *oldalias;
    2650             :         phar_archive_data *fd_ptr;
    2651             :         size_t alias_len, oldalias_len;
    2652          53 :         int old_temp, readd = 0;
    2653             : 
    2654          53 :         PHAR_ARCHIVE_OBJECT();
    2655             : 
    2656          53 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    2657           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2658             :                         "Cannot write out phar archive, phar is read-only");
    2659           1 :                 RETURN_FALSE;
    2660             :         }
    2661             : 
    2662             :         /* invalidate phar cache */
    2663          52 :         PHAR_G(last_phar) = NULL;
    2664          52 :         PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
    2665             : 
    2666          52 :         if (phar_obj->archive->is_data) {
    2667           3 :                 if (phar_obj->archive->is_tar) {
    2668           2 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2669             :                                 "A Phar alias cannot be set in a plain tar archive");
    2670             :                 } else {
    2671           1 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2672             :                                 "A Phar alias cannot be set in a plain zip archive");
    2673             :                 }
    2674           3 :                 RETURN_FALSE;
    2675             :         }
    2676             : 
    2677          49 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &alias, &alias_len) == SUCCESS) {
    2678          48 :                 if (alias_len == phar_obj->archive->alias_len && memcmp(phar_obj->archive->alias, alias, alias_len) == 0) {
    2679           0 :                         RETURN_TRUE;
    2680             :                 }
    2681          96 :                 if (alias_len && NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_GLOBALS->phar_alias_map), alias, alias_len))) {
    2682           4 :                         spprintf(&error, 0, "alias \"%s\" is already used for archive \"%s\" and cannot be used for other archives", alias, fd_ptr->fname);
    2683           4 :                         if (SUCCESS == phar_free_alias(fd_ptr, alias, alias_len)) {
    2684           1 :                                 efree(error);
    2685           1 :                                 goto valid_alias;
    2686             :                         }
    2687           3 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    2688           3 :                         efree(error);
    2689           3 :                         RETURN_FALSE;
    2690             :                 }
    2691          44 :                 if (!phar_validate_alias(alias, alias_len)) {
    2692           4 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2693           4 :                                 "Invalid alias \"%s\" specified for phar \"%s\"", alias, phar_obj->archive->fname);
    2694           4 :                         RETURN_FALSE;
    2695             :                 }
    2696             : valid_alias:
    2697          41 :                 if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    2698           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    2699           0 :                         return;
    2700             :                 }
    2701          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))) {
    2702          23 :                         zend_hash_str_del(&(PHAR_GLOBALS->phar_alias_map), phar_obj->archive->alias, phar_obj->archive->alias_len);
    2703          23 :                         readd = 1;
    2704             :                 }
    2705             : 
    2706          41 :                 oldalias = phar_obj->archive->alias;
    2707          41 :                 oldalias_len = phar_obj->archive->alias_len;
    2708          41 :                 old_temp = phar_obj->archive->is_temporary_alias;
    2709             : 
    2710          41 :                 if (alias_len) {
    2711          41 :                         phar_obj->archive->alias = estrndup(alias, alias_len);
    2712             :                 } else {
    2713           0 :                         phar_obj->archive->alias = NULL;
    2714             :                 }
    2715             : 
    2716          41 :                 phar_obj->archive->alias_len = alias_len;
    2717          41 :                 phar_obj->archive->is_temporary_alias = 0;
    2718          41 :                 phar_flush(phar_obj->archive, NULL, 0, 0, &error);
    2719             : 
    2720          41 :                 if (error) {
    2721           0 :                         phar_obj->archive->alias = oldalias;
    2722           0 :                         phar_obj->archive->alias_len = oldalias_len;
    2723           0 :                         phar_obj->archive->is_temporary_alias = old_temp;
    2724           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    2725           0 :                         if (readd) {
    2726           0 :                                 zend_hash_str_add_ptr(&(PHAR_GLOBALS->phar_alias_map), oldalias, oldalias_len, phar_obj->archive);
    2727             :                         }
    2728           0 :                         efree(error);
    2729           0 :                         RETURN_FALSE;
    2730             :                 }
    2731             : 
    2732          41 :                 zend_hash_str_add_ptr(&(PHAR_GLOBALS->phar_alias_map), alias, alias_len, phar_obj->archive);
    2733             : 
    2734          41 :                 if (oldalias) {
    2735          41 :                         efree(oldalias);
    2736             :                 }
    2737             : 
    2738          41 :                 RETURN_TRUE;
    2739             :         }
    2740             : 
    2741           1 :         RETURN_FALSE;
    2742             : }
    2743             : /* }}} */
    2744             : 
    2745             : /* {{{ proto string Phar::getVersion()
    2746             :  * Return version info of Phar archive
    2747             :  */
    2748           2 : PHP_METHOD(Phar, getVersion)
    2749             : {
    2750           2 :         PHAR_ARCHIVE_OBJECT();
    2751             : 
    2752           1 :         if (zend_parse_parameters_none() == FAILURE) {
    2753           0 :                 return;
    2754             :         }
    2755             : 
    2756           2 :         RETURN_STRING(phar_obj->archive->version);
    2757             : }
    2758             : /* }}} */
    2759             : 
    2760             : /* {{{ proto void Phar::startBuffering()
    2761             :  * Do not flush a writeable phar (save its contents) until explicitly requested
    2762             :  */
    2763           9 : PHP_METHOD(Phar, startBuffering)
    2764             : {
    2765           9 :         PHAR_ARCHIVE_OBJECT();
    2766             : 
    2767           9 :         if (zend_parse_parameters_none() == FAILURE) {
    2768           0 :                 return;
    2769             :         }
    2770             : 
    2771           9 :         phar_obj->archive->donotflush = 1;
    2772             : }
    2773             : /* }}} */
    2774             : 
    2775             : /* {{{ proto bool Phar::isBuffering()
    2776             :  * Returns whether write operations are flushing to disk immediately.
    2777             :  */
    2778           9 : PHP_METHOD(Phar, isBuffering)
    2779             : {
    2780           9 :         PHAR_ARCHIVE_OBJECT();
    2781             : 
    2782           9 :         if (zend_parse_parameters_none() == FAILURE) {
    2783           0 :                 return;
    2784             :         }
    2785             : 
    2786           9 :         RETURN_BOOL(phar_obj->archive->donotflush);
    2787             : }
    2788             : /* }}} */
    2789             : 
    2790             : /* {{{ proto bool Phar::stopBuffering()
    2791             :  * Saves the contents of a modified archive to disk.
    2792             :  */
    2793          69 : PHP_METHOD(Phar, stopBuffering)
    2794             : {
    2795             :         char *error;
    2796             : 
    2797          69 :         PHAR_ARCHIVE_OBJECT();
    2798             : 
    2799          69 :         if (zend_parse_parameters_none() == FAILURE) {
    2800           0 :                 return;
    2801             :         }
    2802             : 
    2803          69 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    2804           0 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2805             :                         "Cannot write out phar archive, phar is read-only");
    2806           0 :                 return;
    2807             :         }
    2808             : 
    2809          69 :         phar_obj->archive->donotflush = 0;
    2810          69 :         phar_flush(phar_obj->archive, 0, 0, 0, &error);
    2811             : 
    2812          69 :         if (error) {
    2813           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    2814           0 :                 efree(error);
    2815             :         }
    2816             : }
    2817             : /* }}} */
    2818             : 
    2819             : /* {{{ proto bool Phar::setStub(string|stream stub [, int len])
    2820             :  * Change the stub in a phar, phar.tar or phar.zip archive to something other
    2821             :  * than the default. The stub *must* end with a call to __HALT_COMPILER().
    2822             :  */
    2823          84 : PHP_METHOD(Phar, setStub)
    2824             : {
    2825             :         zval *zstub;
    2826             :         char *stub, *error;
    2827             :         size_t stub_len;
    2828          84 :         zend_long len = -1;
    2829             :         php_stream *stream;
    2830          84 :         PHAR_ARCHIVE_OBJECT();
    2831             : 
    2832          84 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    2833           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2834             :                         "Cannot change stub, phar is read-only");
    2835           1 :                 return;
    2836             :         }
    2837             : 
    2838          83 :         if (phar_obj->archive->is_data) {
    2839           3 :                 if (phar_obj->archive->is_tar) {
    2840           2 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2841             :                                 "A Phar stub cannot be set in a plain tar archive");
    2842             :                 } else {
    2843           1 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2844             :                                 "A Phar stub cannot be set in a plain zip archive");
    2845             :                 }
    2846           3 :                 return;
    2847             :         }
    2848             : 
    2849          80 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "r|l", &zstub, &len) == SUCCESS) {
    2850          10 :                 if ((php_stream_from_zval_no_verify(stream, zstub)) != NULL) {
    2851          10 :                         if (len > 0) {
    2852           4 :                                 len = -len;
    2853             :                         } else {
    2854           6 :                                 len = -1;
    2855             :                         }
    2856          10 :                         if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    2857           0 :                                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    2858           0 :                                 return;
    2859             :                         }
    2860          10 :                         phar_flush(phar_obj->archive, (char *) zstub, len, 0, &error);
    2861          10 :                         if (error) {
    2862           0 :                                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    2863           0 :                                 efree(error);
    2864             :                         }
    2865          10 :                         RETURN_TRUE;
    2866             :                 } else {
    2867           0 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2868             :                                 "Cannot change stub, unable to read from input stream");
    2869             :                 }
    2870          70 :         } else if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &stub, &stub_len) == SUCCESS) {
    2871          69 :                 if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    2872           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    2873           0 :                         return;
    2874             :                 }
    2875          69 :                 phar_flush(phar_obj->archive, stub, stub_len, 0, &error);
    2876             : 
    2877          69 :                 if (error) {
    2878           3 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    2879           3 :                         efree(error);
    2880             :                 }
    2881             : 
    2882          69 :                 RETURN_TRUE;
    2883             :         }
    2884             : 
    2885           1 :         RETURN_FALSE;
    2886             : }
    2887             : /* }}} */
    2888             : 
    2889             : /* {{{ proto bool Phar::setDefaultStub([string index[, string webindex]])
    2890             :  * In a pure phar archive, sets a stub that can be used to run the archive
    2891             :  * regardless of whether the phar extension is available. The first parameter
    2892             :  * is the CLI startup filename, which defaults to "index.php". The second
    2893             :  * parameter is the web startup filename and also defaults to "index.php"
    2894             :  * (falling back to CLI behaviour).
    2895             :  * Both parameters are optional.
    2896             :  * In a phar.zip or phar.tar archive, the default stub is used only to
    2897             :  * identify the archive to the extension as a Phar object. This allows the
    2898             :  * extension to treat phar.zip and phar.tar types as honorary phars. Since
    2899             :  * files cannot be loaded via this kind of stub, no parameters are accepted
    2900             :  * when the Phar object is zip- or tar-based.
    2901             :  */
    2902          19 : PHP_METHOD(Phar, setDefaultStub)
    2903             : {
    2904          19 :         char *index = NULL, *webindex = NULL, *error = NULL, *stub = NULL;
    2905          19 :         size_t index_len = 0, webindex_len = 0;
    2906          19 :         int created_stub = 0;
    2907          19 :         size_t stub_len = 0;
    2908          19 :         PHAR_ARCHIVE_OBJECT();
    2909             : 
    2910          19 :         if (phar_obj->archive->is_data) {
    2911           3 :                 if (phar_obj->archive->is_tar) {
    2912           2 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2913             :                                 "A Phar stub cannot be set in a plain tar archive");
    2914             :                 } else {
    2915           1 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2916             :                                 "A Phar stub cannot be set in a plain zip archive");
    2917             :                 }
    2918           3 :                 return;
    2919             :         }
    2920             : 
    2921          16 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s", &index, &index_len, &webindex, &webindex_len) == FAILURE) {
    2922           1 :                 RETURN_FALSE;
    2923             :         }
    2924             : 
    2925          15 :         if (ZEND_NUM_ARGS() > 0 && (phar_obj->archive->is_tar || phar_obj->archive->is_zip)) {
    2926           4 :                 php_error_docref(NULL, E_WARNING, "method accepts no arguments for a tar- or zip-based phar stub, %d given", ZEND_NUM_ARGS());
    2927           4 :                 RETURN_FALSE;
    2928             :         }
    2929             : 
    2930          11 :         if (PHAR_G(readonly)) {
    2931           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2932             :                         "Cannot change stub: phar.readonly=1");
    2933           1 :                 RETURN_FALSE;
    2934             :         }
    2935             : 
    2936          10 :         if (!phar_obj->archive->is_tar && !phar_obj->archive->is_zip) {
    2937           8 :                 stub = phar_create_default_stub(index, webindex, &stub_len, &error);
    2938             : 
    2939           8 :                 if (error) {
    2940           1 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "%s", error);
    2941           1 :                         efree(error);
    2942           1 :                         if (stub) {
    2943           0 :                                 efree(stub);
    2944             :                         }
    2945           1 :                         RETURN_FALSE;
    2946             :                 }
    2947             : 
    2948           7 :                 created_stub = 1;
    2949             :         }
    2950             : 
    2951           9 :         if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    2952           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    2953           0 :                 return;
    2954             :         }
    2955           9 :         phar_flush(phar_obj->archive, stub, stub_len, 1, &error);
    2956             : 
    2957           9 :         if (created_stub) {
    2958           7 :                 efree(stub);
    2959             :         }
    2960             : 
    2961           9 :         if (error) {
    2962           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    2963           0 :                 efree(error);
    2964           0 :                 RETURN_FALSE;
    2965             :         }
    2966             : 
    2967           9 :         RETURN_TRUE;
    2968             : }
    2969             : /* }}} */
    2970             : 
    2971             : /* {{{ proto array Phar::setSignatureAlgorithm(int sigtype[, string privatekey])
    2972             :  * Sets the signature algorithm for a phar and applies it. The signature
    2973             :  * algorithm must be one of Phar::MD5, Phar::SHA1, Phar::SHA256,
    2974             :  * Phar::SHA512, or Phar::OPENSSL. Note that zip- based phar archives
    2975             :  * cannot support signatures.
    2976             :  */
    2977          19 : PHP_METHOD(Phar, setSignatureAlgorithm)
    2978             : {
    2979             :         zend_long algo;
    2980          19 :         char *error, *key = NULL;
    2981          19 :         size_t key_len = 0;
    2982             : 
    2983          19 :         PHAR_ARCHIVE_OBJECT();
    2984             : 
    2985          19 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    2986           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    2987             :                         "Cannot set signature algorithm, phar is read-only");
    2988           1 :                 return;
    2989             :         }
    2990             : 
    2991          18 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "l|s", &algo, &key, &key_len) != SUCCESS) {
    2992           0 :                 return;
    2993             :         }
    2994             : 
    2995          18 :         switch (algo) {
    2996             :                 case PHAR_SIG_SHA256:
    2997             :                 case PHAR_SIG_SHA512:
    2998             : #ifndef PHAR_HASH_OK
    2999             :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3000             :                                 "SHA-256 and SHA-512 signatures are only supported if the hash extension is enabled and built non-shared");
    3001             :                         return;
    3002             : #endif
    3003             :                 case PHAR_SIG_MD5:
    3004             :                 case PHAR_SIG_SHA1:
    3005             :                 case PHAR_SIG_OPENSSL:
    3006          18 :                         if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    3007           0 :                                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    3008           0 :                                 return;
    3009             :                         }
    3010          18 :                         phar_obj->archive->sig_flags = algo;
    3011          18 :                         phar_obj->archive->is_modified = 1;
    3012          18 :                         PHAR_G(openssl_privatekey) = key;
    3013          18 :                         PHAR_G(openssl_privatekey_len) = key_len;
    3014             : 
    3015          18 :                         phar_flush(phar_obj->archive, 0, 0, 0, &error);
    3016          18 :                         if (error) {
    3017           0 :                                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    3018           0 :                                 efree(error);
    3019             :                         }
    3020          18 :                         break;
    3021             :                 default:
    3022           0 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3023             :                                 "Unknown signature algorithm specified");
    3024             :         }
    3025             : }
    3026             : /* }}} */
    3027             : 
    3028             : /* {{{ proto array|false Phar::getSignature()
    3029             :  * Returns a hash signature, or FALSE if the archive is unsigned.
    3030             :  */
    3031          31 : PHP_METHOD(Phar, getSignature)
    3032             : {
    3033          31 :         PHAR_ARCHIVE_OBJECT();
    3034             : 
    3035          31 :         if (zend_parse_parameters_none() == FAILURE) {
    3036           0 :                 return;
    3037             :         }
    3038             : 
    3039          31 :         if (phar_obj->archive->signature) {
    3040             :                 zend_string *unknown;
    3041             : 
    3042          29 :                 array_init(return_value);
    3043          29 :                 add_assoc_stringl(return_value, "hash", phar_obj->archive->signature, phar_obj->archive->sig_len);
    3044          29 :                 switch(phar_obj->archive->sig_flags) {
    3045             :                         case PHAR_SIG_MD5:
    3046           6 :                                 add_assoc_stringl(return_value, "hash_type", "MD5", 3);
    3047           6 :                                 break;
    3048             :                         case PHAR_SIG_SHA1:
    3049          11 :                                 add_assoc_stringl(return_value, "hash_type", "SHA-1", 5);
    3050          11 :                                 break;
    3051             :                         case PHAR_SIG_SHA256:
    3052           4 :                                 add_assoc_stringl(return_value, "hash_type", "SHA-256", 7);
    3053           4 :                                 break;
    3054             :                         case PHAR_SIG_SHA512:
    3055           4 :                                 add_assoc_stringl(return_value, "hash_type", "SHA-512", 7);
    3056           4 :                                 break;
    3057             :                         case PHAR_SIG_OPENSSL:
    3058           4 :                                 add_assoc_stringl(return_value, "hash_type", "OpenSSL", 7);
    3059           4 :                                 break;
    3060             :                         default:
    3061           0 :                                 unknown = strpprintf(0, "Unknown (%u)", phar_obj->archive->sig_flags);
    3062           0 :                                 add_assoc_str(return_value, "hash_type", unknown);
    3063             :                                 break;
    3064             :                 }
    3065             :         } else {
    3066           2 :                 RETURN_FALSE;
    3067             :         }
    3068             : }
    3069             : /* }}} */
    3070             : 
    3071             : /* {{{ proto bool Phar::getModified()
    3072             :  * Return whether phar was modified
    3073             :  */
    3074           2 : PHP_METHOD(Phar, getModified)
    3075             : {
    3076           2 :         PHAR_ARCHIVE_OBJECT();
    3077             : 
    3078           2 :         if (zend_parse_parameters_none() == FAILURE) {
    3079           0 :                 return;
    3080             :         }
    3081             : 
    3082           2 :         RETURN_BOOL(phar_obj->archive->is_modified);
    3083             : }
    3084             : /* }}} */
    3085             : 
    3086          40 : static int phar_set_compression(zval *zv, void *argument) /* {{{ */
    3087             : {
    3088          40 :         phar_entry_info *entry = (phar_entry_info *)Z_PTR_P(zv);
    3089          40 :         php_uint32 compress = *(php_uint32 *)argument;
    3090             : 
    3091          40 :         if (entry->is_deleted) {
    3092           0 :                 return ZEND_HASH_APPLY_KEEP;
    3093             :         }
    3094             : 
    3095          40 :         entry->old_flags = entry->flags;
    3096          40 :         entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
    3097          40 :         entry->flags |= compress;
    3098          40 :         entry->is_modified = 1;
    3099          40 :         return ZEND_HASH_APPLY_KEEP;
    3100             : }
    3101             : /* }}} */
    3102             : 
    3103          40 : static int phar_test_compression(zval *zv, void *argument) /* {{{ */
    3104             : {
    3105          40 :         phar_entry_info *entry = (phar_entry_info *)Z_PTR_P(zv);
    3106             : 
    3107          40 :         if (entry->is_deleted) {
    3108           0 :                 return ZEND_HASH_APPLY_KEEP;
    3109             :         }
    3110             : 
    3111          40 :         if (!PHAR_G(has_bz2)) {
    3112           0 :                 if (entry->flags & PHAR_ENT_COMPRESSED_BZ2) {
    3113           0 :                         *(int *) argument = 0;
    3114             :                 }
    3115             :         }
    3116             : 
    3117          40 :         if (!PHAR_G(has_zlib)) {
    3118           0 :                 if (entry->flags & PHAR_ENT_COMPRESSED_GZ) {
    3119           0 :                         *(int *) argument = 0;
    3120             :                 }
    3121             :         }
    3122             : 
    3123          40 :         return ZEND_HASH_APPLY_KEEP;
    3124             : }
    3125             : /* }}} */
    3126             : 
    3127          14 : static void pharobj_set_compression(HashTable *manifest, php_uint32 compress) /* {{{ */
    3128             : {
    3129          14 :         zend_hash_apply_with_argument(manifest, phar_set_compression, &compress);
    3130          14 : }
    3131             : /* }}} */
    3132             : 
    3133          14 : static int pharobj_cancompress(HashTable *manifest) /* {{{ */
    3134             : {
    3135             :         int test;
    3136             : 
    3137          14 :         test = 1;
    3138          14 :         zend_hash_apply_with_argument(manifest, phar_test_compression, &test);
    3139          14 :         return test;
    3140             : }
    3141             : /* }}} */
    3142             : 
    3143             : /* {{{ proto object Phar::compress(int method[, string extension])
    3144             :  * Compress a .tar, or .phar.tar with whole-file compression
    3145             :  * The parameter can be one of Phar::GZ or Phar::BZ2 to specify
    3146             :  * the kind of compression desired
    3147             :  */
    3148           6 : PHP_METHOD(Phar, compress)
    3149             : {
    3150             :         zend_long method;
    3151           6 :         char *ext = NULL;
    3152           6 :         size_t ext_len = 0;
    3153             :         php_uint32 flags;
    3154             :         zend_object *ret;
    3155           6 :         PHAR_ARCHIVE_OBJECT();
    3156             : 
    3157           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|s", &method, &ext, &ext_len) == FAILURE) {
    3158           1 :                 return;
    3159             :         }
    3160             : 
    3161           5 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    3162           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3163             :                         "Cannot compress phar archive, phar is read-only");
    3164           1 :                 return;
    3165             :         }
    3166             : 
    3167           4 :         if (phar_obj->archive->is_zip) {
    3168           0 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3169             :                         "Cannot compress zip-based archives with whole-archive compression");
    3170           0 :                 return;
    3171             :         }
    3172             : 
    3173           4 :         switch (method) {
    3174             :                 case 0:
    3175           1 :                         flags = PHAR_FILE_COMPRESSED_NONE;
    3176           1 :                         break;
    3177             :                 case PHAR_ENT_COMPRESSED_GZ:
    3178           2 :                         if (!PHAR_G(has_zlib)) {
    3179           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3180             :                                         "Cannot compress entire archive with gzip, enable ext/zlib in php.ini");
    3181           0 :                                 return;
    3182             :                         }
    3183           2 :                         flags = PHAR_FILE_COMPRESSED_GZ;
    3184           2 :                         break;
    3185             : 
    3186             :                 case PHAR_ENT_COMPRESSED_BZ2:
    3187           1 :                         if (!PHAR_G(has_bz2)) {
    3188           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3189             :                                         "Cannot compress entire archive with bz2, enable ext/bz2 in php.ini");
    3190           0 :                                 return;
    3191             :                         }
    3192           1 :                         flags = PHAR_FILE_COMPRESSED_BZ2;
    3193           1 :                         break;
    3194             :                 default:
    3195           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3196             :                                 "Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
    3197           0 :                         return;
    3198             :         }
    3199             : 
    3200           4 :         if (phar_obj->archive->is_tar) {
    3201           2 :                 ret = phar_convert_to_other(phar_obj->archive, PHAR_FORMAT_TAR, ext, flags);
    3202             :         } else {
    3203           2 :                 ret = phar_convert_to_other(phar_obj->archive, PHAR_FORMAT_PHAR, ext, flags);
    3204             :         }
    3205             : 
    3206           4 :         if (ret) {
    3207           4 :                 ZVAL_OBJ(return_value, ret);
    3208             :         } else {
    3209           0 :                 RETURN_NULL();
    3210             :         }
    3211             : }
    3212             : /* }}} */
    3213             : 
    3214             : /* {{{ proto object Phar::decompress([string extension])
    3215             :  * Decompress a .tar, or .phar.tar with whole-file compression
    3216             :  */
    3217           5 : PHP_METHOD(Phar, decompress)
    3218             : {
    3219           5 :         char *ext = NULL;
    3220           5 :         size_t ext_len = 0;
    3221             :         zend_object *ret;
    3222           5 :         PHAR_ARCHIVE_OBJECT();
    3223             : 
    3224           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &ext, &ext_len) == FAILURE) {
    3225           1 :                 return;
    3226             :         }
    3227             : 
    3228           4 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    3229           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3230             :                         "Cannot decompress phar archive, phar is read-only");
    3231           1 :                 return;
    3232             :         }
    3233             : 
    3234           3 :         if (phar_obj->archive->is_zip) {
    3235           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3236             :                         "Cannot decompress zip-based archives with whole-archive compression");
    3237           1 :                 return;
    3238             :         }
    3239             : 
    3240           2 :         if (phar_obj->archive->is_tar) {
    3241           0 :                 ret = phar_convert_to_other(phar_obj->archive, PHAR_FORMAT_TAR, ext, PHAR_FILE_COMPRESSED_NONE);
    3242             :         } else {
    3243           2 :                 ret = phar_convert_to_other(phar_obj->archive, PHAR_FORMAT_PHAR, ext, PHAR_FILE_COMPRESSED_NONE);
    3244             :         }
    3245             : 
    3246           2 :         if (ret) {
    3247           2 :                 ZVAL_OBJ(return_value, ret);
    3248             :         } else {
    3249           0 :                 RETURN_NULL();
    3250             :         }
    3251             : }
    3252             : /* }}} */
    3253             : 
    3254             : /* {{{ proto object Phar::compressFiles(int method)
    3255             :  * Compress all files within a phar or zip archive using the specified compression
    3256             :  * The parameter can be one of Phar::GZ or Phar::BZ2 to specify
    3257             :  * the kind of compression desired
    3258             :  */
    3259          13 : PHP_METHOD(Phar, compressFiles)
    3260             : {
    3261             :         char *error;
    3262             :         php_uint32 flags;
    3263             :         zend_long method;
    3264          13 :         PHAR_ARCHIVE_OBJECT();
    3265             : 
    3266          13 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &method) == FAILURE) {
    3267           1 :                 return;
    3268             :         }
    3269             : 
    3270          12 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    3271           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3272             :                         "Phar is readonly, cannot change compression");
    3273           0 :                 return;
    3274             :         }
    3275             : 
    3276          12 :         switch (method) {
    3277             :                 case PHAR_ENT_COMPRESSED_GZ:
    3278           8 :                         if (!PHAR_G(has_zlib)) {
    3279           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3280             :                                         "Cannot compress files within archive with gzip, enable ext/zlib in php.ini");
    3281           0 :                                 return;
    3282             :                         }
    3283           8 :                         flags = PHAR_ENT_COMPRESSED_GZ;
    3284           8 :                         break;
    3285             : 
    3286             :                 case PHAR_ENT_COMPRESSED_BZ2:
    3287           3 :                         if (!PHAR_G(has_bz2)) {
    3288           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3289             :                                         "Cannot compress files within archive with bz2, enable ext/bz2 in php.ini");
    3290           0 :                                 return;
    3291             :                         }
    3292           3 :                         flags = PHAR_ENT_COMPRESSED_BZ2;
    3293           3 :                         break;
    3294             :                 default:
    3295           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3296             :                                 "Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
    3297           1 :                         return;
    3298             :         }
    3299             : 
    3300          11 :         if (phar_obj->archive->is_tar) {
    3301           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3302             :                         "Cannot compress with Gzip compression, tar archives cannot compress individual files, use compress() to compress the whole archive");
    3303           0 :                 return;
    3304             :         }
    3305             : 
    3306          11 :         if (!pharobj_cancompress(&phar_obj->archive->manifest)) {
    3307           0 :                 if (flags == PHAR_FILE_COMPRESSED_GZ) {
    3308           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3309             :                                 "Cannot compress all files as Gzip, some are compressed as bzip2 and cannot be decompressed");
    3310             :                 } else {
    3311           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3312             :                                 "Cannot compress all files as Bzip2, some are compressed as gzip and cannot be decompressed");
    3313             :                 }
    3314           0 :                 return;
    3315             :         }
    3316             : 
    3317          11 :         if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    3318           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    3319           0 :                 return;
    3320             :         }
    3321          11 :         pharobj_set_compression(&phar_obj->archive->manifest, flags);
    3322          11 :         phar_obj->archive->is_modified = 1;
    3323          11 :         phar_flush(phar_obj->archive, 0, 0, 0, &error);
    3324             : 
    3325          11 :         if (error) {
    3326           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "%s", error);
    3327           0 :                 efree(error);
    3328             :         }
    3329             : }
    3330             : /* }}} */
    3331             : 
    3332             : /* {{{ proto bool Phar::decompressFiles()
    3333             :  * decompress every file
    3334             :  */
    3335           4 : PHP_METHOD(Phar, decompressFiles)
    3336             : {
    3337             :         char *error;
    3338           4 :         PHAR_ARCHIVE_OBJECT();
    3339             : 
    3340           4 :         if (zend_parse_parameters_none() == FAILURE) {
    3341           0 :                 return;
    3342             :         }
    3343             : 
    3344           4 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    3345           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3346             :                         "Phar is readonly, cannot change compression");
    3347           1 :                 return;
    3348             :         }
    3349             : 
    3350           3 :         if (!pharobj_cancompress(&phar_obj->archive->manifest)) {
    3351           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    3352             :                         "Cannot decompress all files, some are compressed as bzip2 or gzip and cannot be decompressed");
    3353           0 :                 return;
    3354             :         }
    3355             : 
    3356           3 :         if (phar_obj->archive->is_tar) {
    3357           0 :                 RETURN_TRUE;
    3358             :         } else {
    3359           3 :                 if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    3360           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    3361           0 :                         return;
    3362             :                 }
    3363           3 :                 pharobj_set_compression(&phar_obj->archive->manifest, PHAR_ENT_COMPRESSED_NONE);
    3364             :         }
    3365             : 
    3366           3 :         phar_obj->archive->is_modified = 1;
    3367           3 :         phar_flush(phar_obj->archive, 0, 0, 0, &error);
    3368             : 
    3369           3 :         if (error) {
    3370           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "%s", error);
    3371           0 :                 efree(error);
    3372             :         }
    3373             : 
    3374           3 :         RETURN_TRUE;
    3375             : }
    3376             : /* }}} */
    3377             : 
    3378             : /* {{{ proto bool Phar::copy(string oldfile, string newfile)
    3379             :  * copy a file internal to the phar archive to another new file within the phar
    3380             :  */
    3381          17 : PHP_METHOD(Phar, copy)
    3382             : {
    3383             :         char *oldfile, *newfile, *error;
    3384             :         const char *pcr_error;
    3385             :         size_t oldfile_len, newfile_len;
    3386          17 :         phar_entry_info *oldentry, newentry = {0}, *temp;
    3387          17 :         int tmp_len = 0;
    3388             : 
    3389          17 :         PHAR_ARCHIVE_OBJECT();
    3390             : 
    3391          17 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &oldfile, &oldfile_len, &newfile, &newfile_len) == FAILURE) {
    3392           1 :                 return;
    3393             :         }
    3394             : 
    3395          16 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    3396           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3397             :                         "Cannot copy \"%s\" to \"%s\", phar is read-only", oldfile, newfile);
    3398           1 :                 RETURN_FALSE;
    3399             :         }
    3400             : 
    3401          15 :         if (oldfile_len >= sizeof(".phar")-1 && !memcmp(oldfile, ".phar", sizeof(".phar")-1)) {
    3402             :                 /* can't copy a meta file */
    3403           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3404           1 :                         "file \"%s\" cannot be copied to file \"%s\", cannot copy Phar meta-file in %s", oldfile, newfile, phar_obj->archive->fname);
    3405           1 :                 RETURN_FALSE;
    3406             :         }
    3407             : 
    3408          14 :         if (newfile_len >= sizeof(".phar")-1 && !memcmp(newfile, ".phar", sizeof(".phar")-1)) {
    3409             :                 /* can't copy a meta file */
    3410           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3411           1 :                         "file \"%s\" cannot be copied to file \"%s\", cannot copy to Phar meta-file in %s", oldfile, newfile, phar_obj->archive->fname);
    3412           1 :                 RETURN_FALSE;
    3413             :         }
    3414             : 
    3415          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) {
    3416           1 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3417           1 :                         "file \"%s\" cannot be copied to file \"%s\", file does not exist in %s", oldfile, newfile, phar_obj->archive->fname);
    3418           1 :                 RETURN_FALSE;
    3419             :         }
    3420             : 
    3421          12 :         if (zend_hash_str_exists(&phar_obj->archive->manifest, newfile, (uint) newfile_len)) {
    3422           2 :                 if (NULL != (temp = zend_hash_str_find_ptr(&phar_obj->archive->manifest, newfile, (uint) newfile_len)) || !temp->is_deleted) {
    3423           1 :                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3424           1 :                                 "file \"%s\" cannot be copied to file \"%s\", file must not already exist in phar %s", oldfile, newfile, phar_obj->archive->fname);
    3425           1 :                         RETURN_FALSE;
    3426             :                 }
    3427             :         }
    3428             : 
    3429          11 :         tmp_len = (int)newfile_len;
    3430          11 :         if (phar_path_check(&newfile, &tmp_len, &pcr_error) > pcr_is_ok) {
    3431           3 :                 zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
    3432           3 :                                 "file \"%s\" contains invalid characters %s, cannot be copied from \"%s\" in phar %s", newfile, pcr_error, oldfile, phar_obj->archive->fname);
    3433           3 :                 RETURN_FALSE;
    3434             :         }
    3435           8 :         newfile_len = tmp_len;
    3436             : 
    3437           8 :         if (phar_obj->archive->is_persistent) {
    3438           0 :                 if (FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    3439           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    3440           0 :                         return;
    3441             :                 }
    3442             :                 /* re-populate with copied-on-write entry */
    3443           0 :                 oldentry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, oldfile, (uint) oldfile_len);
    3444             :         }
    3445             : 
    3446           8 :         memcpy((void *) &newentry, oldentry, sizeof(phar_entry_info));
    3447             : 
    3448           8 :         if (Z_TYPE(newentry.metadata) != IS_UNDEF) {
    3449             :                 zval_copy_ctor(&newentry.metadata);
    3450           1 :                 newentry.metadata_str.s = NULL;
    3451             :         }
    3452             : 
    3453           8 :         newentry.filename = estrndup(newfile, newfile_len);
    3454           8 :         newentry.filename_len = newfile_len;
    3455           8 :         newentry.fp_refcount = 0;
    3456             : 
    3457           8 :         if (oldentry->fp_type != PHAR_FP) {
    3458           1 :                 if (FAILURE == phar_copy_entry_fp(oldentry, &newentry, &error)) {
    3459           0 :                         efree(newentry.filename);
    3460           0 :                         php_stream_close(newentry.fp);
    3461           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    3462           0 :                         efree(error);
    3463           0 :                         return;
    3464             :                 }
    3465             :         }
    3466             : 
    3467           8 :         zend_hash_str_add_mem(&oldentry->phar->manifest, newfile, newfile_len, &newentry, sizeof(phar_entry_info));
    3468           8 :         phar_obj->archive->is_modified = 1;
    3469           8 :         phar_flush(phar_obj->archive, 0, 0, 0, &error);
    3470             : 
    3471           8 :         if (error) {
    3472           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    3473           0 :                 efree(error);
    3474             :         }
    3475             : 
    3476           8 :         RETURN_TRUE;
    3477             : }
    3478             : /* }}} */
    3479             : 
    3480             : /* {{{ proto int Phar::offsetExists(string entry)
    3481             :  * determines whether a file exists in the phar
    3482             :  */
    3483          34 : PHP_METHOD(Phar, offsetExists)
    3484             : {
    3485             :         char *fname;
    3486             :         size_t fname_len;
    3487             :         phar_entry_info *entry;
    3488             : 
    3489          34 :         PHAR_ARCHIVE_OBJECT();
    3490             : 
    3491          34 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &fname, &fname_len) == FAILURE) {
    3492           1 :                 return;
    3493             :         }
    3494             : 
    3495          33 :         if (zend_hash_str_exists(&phar_obj->archive->manifest, fname, (uint) fname_len)) {
    3496          38 :                 if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, (uint) fname_len))) {
    3497          19 :                         if (entry->is_deleted) {
    3498             :                                 /* entry is deleted, but has not been flushed to disk yet */
    3499           0 :                                 RETURN_FALSE;
    3500             :                         }
    3501             :                 }
    3502             : 
    3503          19 :                 if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) {
    3504             :                         /* none of these are real files, so they don't exist */
    3505           2 :                         RETURN_FALSE;
    3506             :                 }
    3507          17 :                 RETURN_TRUE;
    3508             :         } else {
    3509          14 :                 if (zend_hash_str_exists(&phar_obj->archive->virtual_dirs, fname, (uint) fname_len)) {
    3510           3 :                         RETURN_TRUE;
    3511             :                 }
    3512          11 :                 RETURN_FALSE;
    3513             :         }
    3514             : }
    3515             : /* }}} */
    3516             : 
    3517             : /* {{{ proto int Phar::offsetGet(string entry)
    3518             :  * get a PharFileInfo object for a specific file
    3519             :  */
    3520         332 : PHP_METHOD(Phar, offsetGet)
    3521             : {
    3522             :         char *fname, *error;
    3523             :         size_t fname_len;
    3524             :         zval zfname;
    3525             :         phar_entry_info *entry;
    3526             :         zend_string *sfname;
    3527         332 :         PHAR_ARCHIVE_OBJECT();
    3528             : 
    3529         332 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &fname, &fname_len) == FAILURE) {
    3530           1 :                 return;
    3531             :         }
    3532             : 
    3533             :         /* security is 0 here so that we can get a better error message than "entry doesn't exist" */
    3534         331 :         if (!(entry = phar_get_entry_info_dir(phar_obj->archive, fname, fname_len, 1, &error, 0))) {
    3535           3 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist%s%s", fname, error?", ":"", error?error:"");
    3536             :         } else {
    3537         328 :                 if (fname_len == sizeof(".phar/stub.php")-1 && !memcmp(fname, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
    3538           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);
    3539           0 :                         return;
    3540             :                 }
    3541             : 
    3542         328 :                 if (fname_len == sizeof(".phar/alias.txt")-1 && !memcmp(fname, ".phar/alias.txt", sizeof(".phar/alias.txt")-1)) {
    3543           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);
    3544           0 :                         return;
    3545             :                 }
    3546             : 
    3547         328 :                 if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) {
    3548           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot directly get any files or directories in magic \".phar\" directory", phar_obj->archive->fname);
    3549           1 :                         return;
    3550             :                 }
    3551             : 
    3552         327 :                 if (entry->is_temp_dir) {
    3553           6 :                         efree(entry->filename);
    3554           6 :                         efree(entry);
    3555             :                 }
    3556             : 
    3557         327 :                 sfname = strpprintf(0, "phar://%s/%s", phar_obj->archive->fname, fname);
    3558         327 :                 ZVAL_NEW_STR(&zfname, sfname);
    3559         327 :                 spl_instantiate_arg_ex1(phar_obj->spl.info_class, return_value, &zfname);
    3560         327 :                 zval_ptr_dtor(&zfname);
    3561             :         }
    3562             : }
    3563             : /* }}} */
    3564             : 
    3565             : /* {{{ add a file within the phar archive from a string or resource
    3566             :  */
    3567        4457 : static void phar_add_file(phar_archive_data **pphar, char *filename, int filename_len, char *cont_str, size_t cont_len, zval *zresource)
    3568             : {
    3569             :         char *error;
    3570             :         size_t contents_len;
    3571             :         phar_entry_data *data;
    3572             :         php_stream *contents_file;
    3573             : 
    3574        4457 :         if (filename_len >= sizeof(".phar")-1 && !memcmp(filename, ".phar", sizeof(".phar")-1)) {
    3575           2 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create any files in magic \".phar\" directory", (*pphar)->fname);
    3576           2 :                 return;
    3577             :         }
    3578             : 
    3579        4455 :         if (!(data = phar_get_or_create_entry_data((*pphar)->fname, (*pphar)->fname_len, filename, filename_len, "w+b", 0, &error, 1))) {
    3580           4 :                 if (error) {
    3581           4 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist and cannot be created: %s", filename, error);
    3582           4 :                         efree(error);
    3583             :                 } else {
    3584           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist and cannot be created", filename);
    3585             :                 }
    3586           4 :                 return;
    3587             :         } else {
    3588        4451 :                 if (error) {
    3589           0 :                         efree(error);
    3590             :                 }
    3591             : 
    3592        4451 :                 if (!data->internal_file->is_dir) {
    3593        4451 :                         if (cont_str) {
    3594         354 :                                 contents_len = php_stream_write(data->fp, cont_str, cont_len);
    3595         354 :                                 if (contents_len != cont_len) {
    3596           0 :                                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s could not be written to", filename);
    3597           0 :                                         return;
    3598             :                                 }
    3599             :                         } else {
    3600        4097 :                                 if (!(php_stream_from_zval_no_verify(contents_file, zresource))) {
    3601           0 :                                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s could not be written to", filename);
    3602           0 :                                         return;
    3603             :                                 }
    3604        4097 :                                 php_stream_copy_to_stream_ex(contents_file, data->fp, PHP_STREAM_COPY_ALL, &contents_len);
    3605             :                         }
    3606             : 
    3607        4451 :                         data->internal_file->compressed_filesize = data->internal_file->uncompressed_filesize = contents_len;
    3608             :                 }
    3609             : 
    3610             :                 /* check for copy-on-write */
    3611        4451 :                 if (pphar[0] != data->phar) {
    3612           0 :                         *pphar = data->phar;
    3613             :                 }
    3614        4451 :                 phar_entry_delref(data);
    3615        4451 :                 phar_flush(*pphar, 0, 0, 0, &error);
    3616             : 
    3617        4451 :                 if (error) {
    3618           4 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    3619           4 :                         efree(error);
    3620             :                 }
    3621             :         }
    3622             : }
    3623             : /* }}} */
    3624             : 
    3625             : /* {{{ create a directory within the phar archive
    3626             :  */
    3627          18 : static void phar_mkdir(phar_archive_data **pphar, char *dirname, int dirname_len)
    3628             : {
    3629             :         char *error;
    3630             :         phar_entry_data *data;
    3631             : 
    3632          18 :         if (!(data = phar_get_or_create_entry_data((*pphar)->fname, (*pphar)->fname_len, dirname, dirname_len, "w+b", 2, &error, 1))) {
    3633           0 :                 if (error) {
    3634           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Directory %s does not exist and cannot be created: %s", dirname, error);
    3635           0 :                         efree(error);
    3636             :                 } else {
    3637           0 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Directory %s does not exist and cannot be created", dirname);
    3638             :                 }
    3639             : 
    3640           0 :                 return;
    3641             :         } else {
    3642          18 :                 if (error) {
    3643           0 :                         efree(error);
    3644             :                 }
    3645             : 
    3646             :                 /* check for copy on write */
    3647          18 :                 if (data->phar != *pphar) {
    3648           0 :                         *pphar = data->phar;
    3649             :                 }
    3650          18 :                 phar_entry_delref(data);
    3651          18 :                 phar_flush(*pphar, 0, 0, 0, &error);
    3652             : 
    3653          18 :                 if (error) {
    3654           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    3655           0 :                         efree(error);
    3656             :                 }
    3657             :         }
    3658             : }
    3659             : /* }}} */
    3660             : 
    3661             : /* {{{ proto int Phar::offsetSet(string entry, string value)
    3662             :  * set the contents of an internal file to those of an external file
    3663             :  */
    3664         250 : PHP_METHOD(Phar, offsetSet)
    3665             : {
    3666         250 :         char *fname, *cont_str = NULL;
    3667             :         size_t fname_len, cont_len;
    3668             :         zval *zresource;
    3669         250 :         PHAR_ARCHIVE_OBJECT();
    3670             : 
    3671         250 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    3672           3 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
    3673           3 :                 return;
    3674             :         }
    3675             : 
    3676         741 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "sr", &fname, &fname_len, &zresource) == FAILURE
    3677         494 :         && zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &fname, &fname_len, &cont_str, &cont_len) == FAILURE) {
    3678           1 :                 return;
    3679             :         }
    3680             : 
    3681         246 :         if (fname_len == sizeof(".phar/stub.php")-1 && !memcmp(fname, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
    3682           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);
    3683           3 :                 return;
    3684             :         }
    3685             : 
    3686         243 :         if (fname_len == sizeof(".phar/alias.txt")-1 && !memcmp(fname, ".phar/alias.txt", sizeof(".phar/alias.txt")-1)) {
    3687           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);
    3688           4 :                 return;
    3689             :         }
    3690             : 
    3691         239 :         if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) {
    3692           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot set any files or directories in magic \".phar\" directory", phar_obj->archive->fname);
    3693           1 :                 return;
    3694             :         }
    3695             : 
    3696         238 :         phar_add_file(&(phar_obj->archive), fname, fname_len, cont_str, cont_len, zresource);
    3697             : }
    3698             : /* }}} */
    3699             : 
    3700             : /* {{{ proto int Phar::offsetUnset(string entry)
    3701             :  * remove a file from a phar
    3702             :  */
    3703           7 : PHP_METHOD(Phar, offsetUnset)
    3704             : {
    3705             :         char *fname, *error;
    3706             :         size_t fname_len;
    3707             :         phar_entry_info *entry;
    3708           7 :         PHAR_ARCHIVE_OBJECT();
    3709             : 
    3710           7 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    3711           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
    3712           1 :                 return;
    3713             :         }
    3714             : 
    3715           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &fname, &fname_len) == FAILURE) {
    3716           1 :                 return;
    3717             :         }
    3718             : 
    3719           5 :         if (zend_hash_str_exists(&phar_obj->archive->manifest, fname, (uint) fname_len)) {
    3720           6 :                 if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, (uint) fname_len))) {
    3721           3 :                         if (entry->is_deleted) {
    3722             :                                 /* entry is deleted, but has not been flushed to disk yet */
    3723           0 :                                 return;
    3724             :                         }
    3725             : 
    3726           3 :                         if (phar_obj->archive->is_persistent) {
    3727           0 :                                 if (FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    3728           0 :                                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    3729           0 :                                         return;
    3730             :                                 }
    3731             :                                 /* re-populate entry after copy on write */
    3732           0 :                                 entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, (uint) fname_len);
    3733             :                         }
    3734           3 :                         entry->is_modified = 0;
    3735           3 :                         entry->is_deleted = 1;
    3736             :                         /* we need to "flush" the stream to save the newly deleted file on disk */
    3737           3 :                         phar_flush(phar_obj->archive, 0, 0, 0, &error);
    3738             : 
    3739           3 :                         if (error) {
    3740           0 :                                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    3741           0 :                                 efree(error);
    3742             :                         }
    3743             : 
    3744           3 :                         RETURN_TRUE;
    3745             :                 }
    3746             :         } else {
    3747           2 :                 RETURN_FALSE;
    3748             :         }
    3749             : }
    3750             : /* }}} */
    3751             : 
    3752             : /* {{{ proto string Phar::addEmptyDir(string dirname)
    3753             :  * Adds an empty directory to the phar archive
    3754             :  */
    3755          20 : PHP_METHOD(Phar, addEmptyDir)
    3756             : {
    3757             :         char *dirname;
    3758             :         size_t dirname_len;
    3759             : 
    3760          20 :         PHAR_ARCHIVE_OBJECT();
    3761             : 
    3762          20 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &dirname, &dirname_len) == FAILURE) {
    3763           1 :                 return;
    3764             :         }
    3765             : 
    3766          19 :         if (dirname_len >= sizeof(".phar")-1 && !memcmp(dirname, ".phar", sizeof(".phar")-1)) {
    3767           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create a directory in magic \".phar\" directory");
    3768           1 :                 return;
    3769             :         }
    3770             : 
    3771          18 :         phar_mkdir(&phar_obj->archive, dirname, dirname_len);
    3772             : }
    3773             : /* }}} */
    3774             : 
    3775             : /* {{{ proto string Phar::addFile(string filename[, string localname])
    3776             :  * Adds a file to the archive using the filename, or the second parameter as the name within the archive
    3777             :  */
    3778        4102 : PHP_METHOD(Phar, addFile)
    3779             : {
    3780        4102 :         char *fname, *localname = NULL;
    3781        4102 :         size_t fname_len, localname_len = 0;
    3782             :         php_stream *resource;
    3783             :         zval zresource;
    3784             : 
    3785        4102 :         PHAR_ARCHIVE_OBJECT();
    3786             : 
    3787        4102 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s", &fname, &fname_len, &localname, &localname_len) == FAILURE) {
    3788           1 :                 return;
    3789             :         }
    3790             : 
    3791             : #if PHP_API_VERSION < 20100412
    3792             :         if (PG(safe_mode) && (!php_checkuid(fname, NULL, CHECKUID_ALLOW_ONLY_FILE))) {
    3793             :                 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);
    3794             :                 return;
    3795             :         }
    3796             : #endif
    3797             : 
    3798        4101 :         if (!strstr(fname, "://") && php_check_open_basedir(fname)) {
    3799           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);
    3800           0 :                 return;
    3801             :         }
    3802             : 
    3803        4101 :         if (!(resource = php_stream_open_wrapper(fname, "rb", 0, NULL))) {
    3804           1 :                 zend_throw_exception_ex(spl_ce_RuntimeException, 0, "phar error: unable to open file \"%s\" to add to phar archive", fname);
    3805           1 :                 return;
    3806             :         }
    3807             : 
    3808        4100 :         if (localname) {
    3809        4099 :                 fname = localname;
    3810        4099 :                 fname_len = localname_len;
    3811             :         }
    3812             : 
    3813        4100 :         php_stream_to_zval(resource, &zresource);
    3814        4100 :         phar_add_file(&(phar_obj->archive), fname, fname_len, NULL, 0, &zresource);
    3815        4100 :         zval_ptr_dtor(&zresource);
    3816             : }
    3817             : /* }}} */
    3818             : 
    3819             : /* {{{ proto string Phar::addFromString(string localname, string contents)
    3820             :  * Adds a file to the archive using its contents as a string
    3821             :  */
    3822         120 : PHP_METHOD(Phar, addFromString)
    3823             : {
    3824             :         char *localname, *cont_str;
    3825             :         size_t localname_len, cont_len;
    3826             : 
    3827         120 :         PHAR_ARCHIVE_OBJECT();
    3828             : 
    3829         120 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &localname, &localname_len, &cont_str, &cont_len) == FAILURE) {
    3830           1 :                 return;
    3831             :         }
    3832             : 
    3833         119 :         phar_add_file(&(phar_obj->archive), localname, localname_len, cont_str, cont_len, NULL);
    3834             : }
    3835             : /* }}} */
    3836             : 
    3837             : /* {{{ proto string Phar::getStub()
    3838             :  * Returns the stub at the head of a phar archive as a string.
    3839             :  */
    3840         137 : PHP_METHOD(Phar, getStub)
    3841             : {
    3842             :         size_t len;
    3843             :         zend_string *buf;
    3844             :         php_stream *fp;
    3845         137 :         php_stream_filter *filter = NULL;
    3846             :         phar_entry_info *stub;
    3847             : 
    3848         137 :         PHAR_ARCHIVE_OBJECT();
    3849             : 
    3850         137 :         if (zend_parse_parameters_none() == FAILURE) {
    3851           0 :                 return;
    3852             :         }
    3853             : 
    3854         137 :         if (phar_obj->archive->is_tar || phar_obj->archive->is_zip) {
    3855             : 
    3856         140 :                 if (NULL != (stub = zend_hash_str_find_ptr(&(phar_obj->archive->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1))) {
    3857         128 :                         if (phar_obj->archive->fp && !phar_obj->archive->is_brandnew && !(stub->flags & PHAR_ENT_COMPRESSION_MASK)) {
    3858          64 :                                 fp = phar_obj->archive->fp;
    3859             :                         } else {
    3860           0 :                                 if (!(fp = php_stream_open_wrapper(phar_obj->archive->fname, "rb", 0, NULL))) {
    3861           0 :                                         zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "phar error: unable to open phar \"%s\"", phar_obj->archive->fname);
    3862           0 :                                         return;
    3863             :                                 }
    3864           0 :                                 if (stub->flags & PHAR_ENT_COMPRESSION_MASK) {
    3865             :                                         char *filter_name;
    3866             : 
    3867           0 :                                         if ((filter_name = phar_decompress_filter(stub, 0)) != NULL) {
    3868           0 :                                                 filter = php_stream_filter_create(filter_name, NULL, php_stream_is_persistent(fp));
    3869             :                                         } else {
    3870           0 :                                                 filter = NULL;
    3871             :                                         }
    3872           0 :                                         if (!filter) {
    3873           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));
    3874           0 :                                                 return;
    3875             :                                         }
    3876           0 :                                         php_stream_filter_append(&fp->readfilters, filter);
    3877             :                                 }
    3878             :                         }
    3879             : 
    3880          64 :                         if (!fp)  {
    3881           0 :                                 zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    3882             :                                         "Unable to read stub");
    3883           0 :                                 return;
    3884             :                         }
    3885             : 
    3886          64 :                         php_stream_seek(fp, stub->offset_abs, SEEK_SET);
    3887          64 :                         len = stub->uncompressed_filesize;
    3888          64 :                         goto carry_on;
    3889             :                 } else {
    3890           6 :                         RETURN_EMPTY_STRING();
    3891             :                 }
    3892             :         }
    3893          67 :         len = phar_obj->archive->halt_offset;
    3894             : 
    3895         134 :         if (phar_obj->archive->fp && !phar_obj->archive->is_brandnew) {
    3896          67 :                 fp = phar_obj->archive->fp;
    3897             :         } else {
    3898           0 :                 fp = php_stream_open_wrapper(phar_obj->archive->fname, "rb", 0, NULL);
    3899             :         }
    3900             : 
    3901          67 :         if (!fp)  {
    3902           0 :                 zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    3903             :                         "Unable to read stub");
    3904           0 :                 return;
    3905             :         }
    3906             : 
    3907          67 :         php_stream_rewind(fp);
    3908             : carry_on:
    3909         131 :         buf = zend_string_alloc(len, 0);
    3910             : 
    3911         131 :         if (len != php_stream_read(fp, buf->val, len)) {
    3912           0 :                 if (fp != phar_obj->archive->fp) {
    3913           0 :                         php_stream_close(fp);
    3914             :                 }
    3915           0 :                 zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    3916             :                         "Unable to read stub");
    3917             :                 zend_string_release(buf);
    3918           0 :                 return;
    3919             :         }
    3920             : 
    3921         131 :         if (filter) {
    3922           0 :                 php_stream_filter_flush(filter, 1);
    3923           0 :                 php_stream_filter_remove(filter, 1);
    3924             :         }
    3925             : 
    3926         131 :         if (fp != phar_obj->archive->fp) {
    3927           0 :                 php_stream_close(fp);
    3928             :         }
    3929             : 
    3930         131 :         buf->val[len] = '\0';
    3931         131 :         buf->len = len;
    3932         131 :         RETVAL_STR(buf);
    3933             : }
    3934             : /* }}}*/
    3935             : 
    3936             : /* {{{ proto int Phar::hasMetaData()
    3937             :  * Returns TRUE if the phar has global metadata, FALSE otherwise.
    3938             :  */
    3939           1 : PHP_METHOD(Phar, hasMetadata)
    3940             : {
    3941           1 :         PHAR_ARCHIVE_OBJECT();
    3942             : 
    3943           2 :         RETURN_BOOL(Z_TYPE(phar_obj->archive->metadata) != IS_UNDEF);
    3944             : }
    3945             : /* }}} */
    3946             : 
    3947             : /* {{{ proto int Phar::getMetaData()
    3948             :  * Returns the global metadata of the phar
    3949             :  */
    3950          17 : PHP_METHOD(Phar, getMetadata)
    3951             : {
    3952          17 :         PHAR_ARCHIVE_OBJECT();
    3953             : 
    3954          17 :         if (zend_parse_parameters_none() == FAILURE) {
    3955           0 :                 return;
    3956             :         }
    3957             : 
    3958          34 :         if (Z_TYPE(phar_obj->archive->metadata) != IS_UNDEF) {
    3959          12 :                 if (phar_obj->archive->is_persistent) {
    3960             :                         zval ret;
    3961           1 :                         char *buf = estrndup((char *) Z_PTR(phar_obj->archive->metadata), phar_obj->archive->metadata_len);
    3962             :                         /* assume success, we would have failed before */
    3963           1 :                         phar_parse_metadata(&buf, &ret, phar_obj->archive->metadata_len);
    3964           1 :                         efree(buf);
    3965           2 :                         RETURN_ZVAL(&ret, 0, 1);
    3966             :                 }
    3967          33 :                 RETURN_ZVAL(&phar_obj->archive->metadata, 1, 0);
    3968             :         }
    3969             : }
    3970             : /* }}} */
    3971             : 
    3972             : /* {{{ proto int Phar::setMetaData(mixed $metadata)
    3973             :  * Sets the global metadata of the phar
    3974             :  */
    3975          10 : PHP_METHOD(Phar, setMetadata)
    3976             : {
    3977             :         char *error;
    3978             :         zval *metadata;
    3979             : 
    3980          10 :         PHAR_ARCHIVE_OBJECT();
    3981             : 
    3982          10 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    3983           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
    3984           1 :                 return;
    3985             :         }
    3986             : 
    3987           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &metadata) == FAILURE) {
    3988           1 :                 return;
    3989             :         }
    3990             : 
    3991           8 :         if (phar_obj->archive->is_persistent && FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
    3992           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
    3993           0 :                 return;
    3994             :         }
    3995          16 :         if (Z_TYPE(phar_obj->archive->metadata) != IS_UNDEF) {
    3996           1 :                 zval_ptr_dtor(&phar_obj->archive->metadata);
    3997           1 :                 ZVAL_UNDEF(&phar_obj->archive->metadata);
    3998             :         }
    3999             : 
    4000          16 :         ZVAL_ZVAL(&phar_obj->archive->metadata, metadata, 1, 0);
    4001           8 :         phar_obj->archive->is_modified = 1;
    4002           8 :         phar_flush(phar_obj->archive, 0, 0, 0, &error);
    4003             : 
    4004           8 :         if (error) {
    4005           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    4006           0 :                 efree(error);
    4007             :         }
    4008             : }
    4009             : /* }}} */
    4010             : 
    4011             : /* {{{ proto int Phar::delMetadata()
    4012             :  * Deletes the global metadata of the phar
    4013             :  */
    4014           3 : PHP_METHOD(Phar, delMetadata)
    4015             : {
    4016             :         char *error;
    4017             : 
    4018           3 :         PHAR_ARCHIVE_OBJECT();
    4019             : 
    4020           3 :         if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
    4021           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
    4022           1 :                 return;
    4023             :         }
    4024             : 
    4025           4 :         if (Z_TYPE(phar_obj->archive->metadata) != IS_UNDEF) {
    4026           1 :                 zval_ptr_dtor(&phar_obj->archive->metadata);
    4027           1 :                 ZVAL_UNDEF(&phar_obj->archive->metadata);
    4028           1 :                 phar_obj->archive->is_modified = 1;
    4029           1 :                 phar_flush(phar_obj->archive, 0, 0, 0, &error);
    4030             : 
    4031           1 :                 if (error) {
    4032           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    4033           0 :                         efree(error);
    4034           0 :                         RETURN_FALSE;
    4035             :                 } else {
    4036           1 :                         RETURN_TRUE;
    4037             :                 }
    4038             : 
    4039             :         } else {
    4040           1 :                 RETURN_TRUE;
    4041             :         }
    4042             : }
    4043             : /* }}} */
    4044             : #if PHP_API_VERSION < 20100412
    4045             : #define PHAR_OPENBASEDIR_CHECKPATH(filename) \
    4046             :         (PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) || php_check_open_basedir(filename)
    4047             : #else
    4048             : #define PHAR_OPENBASEDIR_CHECKPATH(filename) \
    4049             :         php_check_open_basedir(filename)
    4050             : #endif
    4051             : 
    4052          21 : static int phar_extract_file(zend_bool overwrite, phar_entry_info *entry, char *dest, int dest_len, char **error) /* {{{ */
    4053             : {
    4054             :         php_stream_statbuf ssb;
    4055             :         int len;
    4056             :         php_stream *fp;
    4057             :         char *fullpath;
    4058             :         const char *slash;
    4059             :         mode_t mode;
    4060             : 
    4061          21 :         if (entry->is_mounted) {
    4062             :                 /* silently ignore mounted entries */
    4063           4 :                 return SUCCESS;
    4064             :         }
    4065             : 
    4066          17 :         if (entry->filename_len >= sizeof(".phar")-1 && !memcmp(entry->filename, ".phar", sizeof(".phar")-1)) {
    4067           0 :                 return SUCCESS;
    4068             :         }
    4069             : 
    4070          17 :         len = spprintf(&fullpath, 0, "%s/%s", dest, entry->filename);
    4071             : 
    4072          17 :         if (len >= MAXPATHLEN) {
    4073             :                 char *tmp;
    4074             :                 /* truncate for error message */
    4075           1 :                 fullpath[50] = '\0';
    4076           1 :                 if (entry->filename_len > 50) {
    4077           1 :                         tmp = estrndup(entry->filename, 50);
    4078           1 :                         spprintf(error, 4096, "Cannot extract \"%s...\" to \"%s...\", extracted filename is too long for filesystem", tmp, fullpath);
    4079           1 :                         efree(tmp);
    4080             :                 } else {
    4081           0 :                         spprintf(error, 4096, "Cannot extract \"%s\" to \"%s...\", extracted filename is too long for filesystem", entry->filename, fullpath);
    4082             :                 }
    4083           1 :                 efree(fullpath);
    4084           1 :                 return FAILURE;
    4085             :         }
    4086             : 
    4087          16 :         if (!len) {
    4088           0 :                 spprintf(error, 4096, "Cannot extract \"%s\", internal error", entry->filename);
    4089           0 :                 efree(fullpath);
    4090           0 :                 return FAILURE;
    4091             :         }
    4092             : 
    4093          16 :         if (PHAR_OPENBASEDIR_CHECKPATH(fullpath)) {
    4094           0 :                 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", openbasedir/safe mode restrictions in effect", entry->filename, fullpath);
    4095           0 :                 efree(fullpath);
    4096           0 :                 return FAILURE;
    4097             :         }
    4098             : 
    4099             :         /* let see if the path already exists */
    4100          16 :         if (!overwrite && SUCCESS == php_stream_stat_path(fullpath, &ssb)) {
    4101           1 :                 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", path already exists", entry->filename, fullpath);
    4102           1 :                 efree(fullpath);
    4103           1 :                 return FAILURE;
    4104             :         }
    4105             : 
    4106             :         /* perform dirname */
    4107          30 :         slash = zend_memrchr(entry->filename, '/', entry->filename_len);
    4108             : 
    4109          15 :         if (slash) {
    4110           6 :                 fullpath[dest_len + (slash - entry->filename) + 1] = '\0';
    4111             :         } else {
    4112           9 :                 fullpath[dest_len] = '\0';
    4113             :         }
    4114             : 
    4115          15 :         if (FAILURE == php_stream_stat_path(fullpath, &ssb)) {
    4116           6 :                 if (entry->is_dir) {
    4117           3 :                         if (!php_stream_mkdir(fullpath, entry->flags & PHAR_ENT_PERM_MASK,  PHP_STREAM_MKDIR_RECURSIVE, NULL)) {
    4118           0 :                                 spprintf(error, 4096, "Cannot extract \"%s\", could not create directory \"%s\"", entry->filename, fullpath);
    4119           0 :                                 efree(fullpath);
    4120           0 :                                 return FAILURE;
    4121             :                         }
    4122             :                 } else {
    4123           3 :                         if (!php_stream_mkdir(fullpath, 0777,  PHP_STREAM_MKDIR_RECURSIVE, NULL)) {
    4124           0 :                                 spprintf(error, 4096, "Cannot extract \"%s\", could not create directory \"%s\"", entry->filename, fullpath);
    4125           0 :                                 efree(fullpath);
    4126           0 :                                 return FAILURE;
    4127             :                         }
    4128             :                 }
    4129             :         }
    4130             : 
    4131          15 :         if (slash) {
    4132           6 :                 fullpath[dest_len + (slash - entry->filename) + 1] = '/';
    4133             :         } else {
    4134           9 :                 fullpath[dest_len] = '/';
    4135             :         }
    4136             : 
    4137             :         /* it is a standalone directory, job done */
    4138          15 :         if (entry->is_dir) {
    4139           3 :                 efree(fullpath);
    4140           3 :                 return SUCCESS;
    4141             :         }
    4142             : 
    4143             : #if PHP_API_VERSION < 20100412
    4144             :         fp = php_stream_open_wrapper(fullpath, "w+b", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
    4145             : #else
    4146          12 :         fp = php_stream_open_wrapper(fullpath, "w+b", REPORT_ERRORS, NULL);
    4147             : #endif
    4148             : 
    4149          12 :         if (!fp) {
    4150           0 :                 spprintf(error, 4096, "Cannot extract \"%s\", could not open for writing \"%s\"", entry->filename, fullpath);
    4151           0 :                 efree(fullpath);
    4152           0 :                 return FAILURE;
    4153             :         }
    4154             : 
    4155          12 :         if (!phar_get_efp(entry, 0)) {
    4156           0 :                 if (FAILURE == phar_open_entry_fp(entry, error, 1)) {
    4157           0 :                         if (error) {
    4158           0 :                                 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to open internal file pointer: %s", entry->filename, fullpath, *error);
    4159             :                         } else {
    4160           0 :                                 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to open internal file pointer", entry->filename, fullpath);
    4161             :                         }
    4162           0 :                         efree(fullpath);
    4163           0 :                         php_stream_close(fp);
    4164           0 :                         return FAILURE;
    4165             :                 }
    4166             :         }
    4167             : 
    4168          12 :         if (FAILURE == phar_seek_efp(entry, 0, SEEK_SET, 0, 0)) {
    4169           0 :                 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", unable to seek internal file pointer", entry->filename, fullpath);
    4170           0 :                 efree(fullpath);
    4171           0 :                 php_stream_close(fp);
    4172           0 :                 return FAILURE;
    4173             :         }
    4174             : 
    4175          12 :         if (SUCCESS != php_stream_copy_to_stream_ex(phar_get_efp(entry, 0), fp, entry->uncompressed_filesize, NULL)) {
    4176           0 :                 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", copying contents failed", entry->filename, fullpath);
    4177           0 :                 efree(fullpath);
    4178           0 :                 php_stream_close(fp);
    4179           0 :                 return FAILURE;
    4180             :         }
    4181             : 
    4182          12 :         php_stream_close(fp);
    4183          12 :         mode = (mode_t) entry->flags & PHAR_ENT_PERM_MASK;
    4184             : 
    4185          12 :         if (FAILURE == VCWD_CHMOD(fullpath, mode)) {
    4186           0 :                 spprintf(error, 4096, "Cannot extract \"%s\" to \"%s\", setting file permissions failed", entry->filename, fullpath);
    4187           0 :                 efree(fullpath);
    4188           0 :                 return FAILURE;
    4189             :         }
    4190             : 
    4191          12 :         efree(fullpath);
    4192          12 :         return SUCCESS;
    4193             : }
    4194             : /* }}} */
    4195             : 
    4196             : /* {{{ proto bool Phar::extractTo(string pathto[[, mixed files], bool overwrite])
    4197             :  * Extract one or more file from a phar archive, optionally overwriting existing files
    4198             :  */
    4199          18 : PHP_METHOD(Phar, extractTo)
    4200             : {
    4201          18 :         char *error = NULL;
    4202             :         php_stream *fp;
    4203             :         php_stream_statbuf ssb;
    4204             :         phar_entry_info *entry;
    4205             :         char *pathto, *filename, *actual;
    4206             :         size_t pathto_len, filename_len;
    4207             :         int ret, i;
    4208             :         int nelems;
    4209          18 :         zval *zval_files = NULL;
    4210          18 :         zend_bool overwrite = 0;
    4211             : 
    4212          18 :         PHAR_ARCHIVE_OBJECT();
    4213             : 
    4214          18 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|z!b", &pathto, &pathto_len, &zval_files, &overwrite) == FAILURE) {
    4215           1 :                 return;
    4216             :         }
    4217             : 
    4218          17 :         fp = php_stream_open_wrapper(phar_obj->archive->fname, "rb", IGNORE_URL|STREAM_MUST_SEEK, &actual);
    4219             : 
    4220          17 :         if (!fp) {
    4221           1 :                 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
    4222           1 :                         "Invalid argument, %s cannot be found", phar_obj->archive->fname);
    4223           1 :                 return;
    4224             :         }
    4225             : 
    4226          16 :         efree(actual);
    4227          16 :         php_stream_close(fp);
    4228             : 
    4229          16 :         if (pathto_len < 1) {
    4230           1 :                 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
    4231             :                         "Invalid argument, extraction path must be non-zero length");
    4232           1 :                 return;
    4233             :         }
    4234             : 
    4235          15 :         if (pathto_len >= MAXPATHLEN) {
    4236           1 :                 char *tmp = estrndup(pathto, 50);
    4237             :                 /* truncate for error message */
    4238           1 :                 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0, "Cannot extract to \"%s...\", destination directory is too long for filesystem", tmp);
    4239           1 :                 efree(tmp);
    4240           1 :                 return;
    4241             :         }
    4242             : 
    4243          14 :         if (php_stream_stat_path(pathto, &ssb) < 0) {
    4244           7 :                 ret = php_stream_mkdir(pathto, 0777,  PHP_STREAM_MKDIR_RECURSIVE, NULL);
    4245           7 :                 if (!ret) {
    4246           0 :                         zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    4247             :                                 "Unable to create path \"%s\" for extraction", pathto);
    4248           0 :                         return;
    4249             :                 }
    4250           7 :         } else if (!(ssb.sb.st_mode & S_IFDIR)) {
    4251           1 :                 zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    4252             :                         "Unable to use path \"%s\" for extraction, it is a file, must be a directory", pathto);
    4253           1 :                 return;
    4254             :         }
    4255             : 
    4256          13 :         if (zval_files) {
    4257          20 :                 switch (Z_TYPE_P(zval_files)) {
    4258             :                         case IS_NULL:
    4259           0 :                                 goto all_files;
    4260             :                         case IS_STRING:
    4261           7 :                                 filename = Z_STRVAL_P(zval_files);
    4262           7 :                                 filename_len = Z_STRLEN_P(zval_files);
    4263           7 :                                 break;
    4264             :                         case IS_ARRAY:
    4265           2 :                                 nelems = zend_hash_num_elements(Z_ARRVAL_P(zval_files));
    4266           2 :                                 if (nelems == 0 ) {
    4267           0 :                                         RETURN_FALSE;
    4268             :                                 }
    4269           4 :                                 for (i = 0; i < nelems; i++) {
    4270             :                                         zval *zval_file;
    4271           3 :                                         if ((zval_file = zend_hash_index_find(Z_ARRVAL_P(zval_files), i)) != NULL) {
    4272           3 :                                                 switch (Z_TYPE_P(zval_file)) {
    4273             :                                                         case IS_STRING:
    4274           2 :                                                                 break;
    4275             :                                                         default:
    4276           1 :                                                                 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
    4277             :                                                                         "Invalid argument, array of filenames to extract contains non-string value");
    4278           1 :                                                                 return;
    4279             :                                                 }
    4280           4 :                                                 if (NULL == (entry = zend_hash_find_ptr(&phar_obj->archive->manifest, Z_STR_P(zval_file)))) {
    4281           0 :                                                         zend_throw_exception_ex(phar_ce_PharException, 0,
    4282           0 :                                                                 "Phar Error: attempted to extract non-existent file \"%s\" from phar \"%s\"", Z_STRVAL_P(zval_file), phar_obj->archive->fname);
    4283             :                                                 }
    4284           2 :                                                 if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, &error)) {
    4285           0 :                                                         zend_throw_exception_ex(phar_ce_PharException, 0,
    4286           0 :                                                                 "Extraction from phar \"%s\" failed: %s", phar_obj->archive->fname, error);
    4287           0 :                                                         efree(error);
    4288           0 :                                                         return;
    4289             :                                                 }
    4290             :                                         }
    4291             :                                 }
    4292           1 :                                 RETURN_TRUE;
    4293             :                         default:
    4294           1 :                                 zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
    4295             :                                         "Invalid argument, expected a filename (string) or array of filenames");
    4296           1 :                                 return;
    4297             :                 }
    4298             : 
    4299          14 :                 if (NULL == (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, filename, filename_len))) {
    4300           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0,
    4301           0 :                                 "Phar Error: attempted to extract non-existent file \"%s\" from phar \"%s\"", filename, phar_obj->archive->fname);
    4302           0 :                         return;
    4303             :                 }
    4304             : 
    4305           7 :                 if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, &error)) {
    4306           4 :                         zend_throw_exception_ex(phar_ce_PharException, 0,
    4307           2 :                                 "Extraction from phar \"%s\" failed: %s", phar_obj->archive->fname, error);
    4308           2 :                         efree(error);
    4309           2 :                         return;
    4310             :                 }
    4311             :         } else {
    4312             :                 phar_archive_data *phar;
    4313             : all_files:
    4314           3 :                 phar = phar_obj->archive;
    4315             :                 /* Extract all files */
    4316           3 :                 if (!zend_hash_num_elements(&(phar->manifest))) {
    4317           0 :                         RETURN_TRUE;
    4318             :                 }
    4319             : 
    4320          27 :                 ZEND_HASH_FOREACH_PTR(&phar->manifest, entry) {
    4321          12 :                         if (FAILURE == phar_extract_file(overwrite, entry, pathto, pathto_len, &error)) {
    4322           0 :                                 zend_throw_exception_ex(phar_ce_PharException, 0,
    4323             :                                         "Extraction from phar \"%s\" failed: %s", phar->fname, error);
    4324           0 :                                 efree(error);
    4325           0 :                                 return;
    4326             :                         }
    4327             :                 } ZEND_HASH_FOREACH_END();
    4328             :         }
    4329           8 :         RETURN_TRUE;
    4330             : }
    4331             : /* }}} */
    4332             : 
    4333             : 
    4334             : /* {{{ proto void PharFileInfo::__construct(string entry)
    4335             :  * Construct a Phar entry object
    4336             :  */
    4337         400 : PHP_METHOD(PharFileInfo, __construct)
    4338             : {
    4339             :         char *fname, *arch, *entry, *error;
    4340             :         size_t fname_len;
    4341             :         int arch_len, entry_len;
    4342             :         phar_entry_object *entry_obj;
    4343             :         phar_entry_info *entry_info;
    4344             :         phar_archive_data *phar_data;
    4345         400 :         zval *zobj = getThis(), arg1;
    4346             : 
    4347         400 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &fname, &fname_len) == FAILURE) {
    4348           1 :                 return;
    4349             :         }
    4350             : 
    4351         399 :         entry_obj = (phar_entry_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset);
    4352             : 
    4353         399 :         if (entry_obj->entry) {
    4354           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot call constructor twice");
    4355           1 :                 return;
    4356             :         }
    4357             : 
    4358         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) {
    4359           1 :                 zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    4360             :                         "'%s' is not a valid phar archive URL (must have at least phar://filename.phar)", fname);
    4361           1 :                 return;
    4362             :         }
    4363             : 
    4364         397 :         if (phar_open_from_filename(arch, arch_len, NULL, 0, REPORT_ERRORS, &phar_data, &error) == FAILURE) {
    4365           1 :                 efree(arch);
    4366           1 :                 efree(entry);
    4367           1 :                 if (error) {
    4368           1 :                         zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    4369             :                                 "Cannot open phar file '%s': %s", fname, error);
    4370           1 :                         efree(error);
    4371             :                 } else {
    4372           0 :                         zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    4373             :                                 "Cannot open phar file '%s'", fname);
    4374             :                 }
    4375           1 :                 return;
    4376             :         }
    4377             : 
    4378         396 :         if ((entry_info = phar_get_entry_info_dir(phar_data, entry, entry_len, 1, &error, 1)) == NULL) {
    4379           2 :                 zend_throw_exception_ex(spl_ce_RuntimeException, 0,
    4380           2 :                         "Cannot access phar file entry '%s' in archive '%s'%s%s", entry, arch, error ? ", " : "", error ? error : "");
    4381           1 :                 efree(arch);
    4382           1 :                 efree(entry);
    4383           1 :                 return;
    4384             :         }
    4385             : 
    4386         395 :         efree(arch);
    4387         395 :         efree(entry);
    4388             : 
    4389         395 :         entry_obj->entry = entry_info;
    4390             : 
    4391         790 :         ZVAL_STRINGL(&arg1, fname, fname_len);
    4392             : 
    4393         395 :         zend_call_method_with_1_params(zobj, Z_OBJCE_P(zobj),
    4394             :                 &spl_ce_SplFileInfo->constructor, "__construct", NULL, &arg1);
    4395             : 
    4396         395 :         zval_ptr_dtor(&arg1);
    4397             : }
    4398             : /* }}} */
    4399             : 
    4400             : #define PHAR_ENTRY_OBJECT() \
    4401             :         zval *zobj = getThis(); \
    4402             :         phar_entry_object *entry_obj = (phar_entry_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset); \
    4403             :         if (!entry_obj->entry) { \
    4404             :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4405             :                         "Cannot call method on an uninitialized PharFileInfo object"); \
    4406             :                 return; \
    4407             :         }
    4408             : 
    4409             : /* {{{ proto void PharFileInfo::__destruct()
    4410             :  * clean up directory-based entry objects
    4411             :  */
    4412         396 : PHP_METHOD(PharFileInfo, __destruct)
    4413             : {
    4414         396 :         zval *zobj = getThis();
    4415         396 :         phar_entry_object *entry_obj = (phar_entry_object*)((char*)Z_OBJ_P(zobj) - Z_OBJ_P(zobj)->handlers->offset);
    4416             : 
    4417         396 :         if (entry_obj->entry && entry_obj->entry->is_temp_dir) {
    4418          10 :                 if (entry_obj->entry->filename) {
    4419          10 :                         efree(entry_obj->entry->filename);
    4420          10 :                         entry_obj->entry->filename = NULL;
    4421             :                 }
    4422             : 
    4423          10 :                 efree(entry_obj->entry);
    4424          10 :                 entry_obj->entry = NULL;
    4425             :         }
    4426         396 : }
    4427             : /* }}} */
    4428             : 
    4429             : /* {{{ proto int PharFileInfo::getCompressedSize()
    4430             :  * Returns the compressed size
    4431             :  */
    4432           5 : PHP_METHOD(PharFileInfo, getCompressedSize)
    4433             : {
    4434           5 :         PHAR_ENTRY_OBJECT();
    4435             : 
    4436           5 :         if (zend_parse_parameters_none() == FAILURE) {
    4437           0 :                 return;
    4438             :         }
    4439             : 
    4440           5 :         RETURN_LONG(entry_obj->entry->compressed_filesize);
    4441             : }
    4442             : /* }}} */
    4443             : 
    4444             : /* {{{ proto bool PharFileInfo::isCompressed([int compression_type])
    4445             :  * Returns whether the entry is compressed, and whether it is compressed with Phar::GZ or Phar::BZ2 if specified
    4446             :  */
    4447         102 : PHP_METHOD(PharFileInfo, isCompressed)
    4448             : {
    4449             :         /* a number that is not Phar::GZ or Phar::BZ2 */
    4450         102 :         zend_long method = 9021976;
    4451         102 :         PHAR_ENTRY_OBJECT();
    4452             : 
    4453         102 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &method) == FAILURE) {
    4454           1 :                 return;
    4455             :         }
    4456             : 
    4457         101 :         switch (method) {
    4458             :                 case 9021976:
    4459          68 :                         RETURN_BOOL(entry_obj->entry->flags & PHAR_ENT_COMPRESSION_MASK);
    4460             :                 case PHAR_ENT_COMPRESSED_GZ:
    4461          16 :                         RETURN_BOOL(entry_obj->entry->flags & PHAR_ENT_COMPRESSED_GZ);
    4462             :                 case PHAR_ENT_COMPRESSED_BZ2:
    4463          16 :                         RETURN_BOOL(entry_obj->entry->flags & PHAR_ENT_COMPRESSED_BZ2);
    4464             :                 default:
    4465           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4466             :                                 "Unknown compression type specified"); \
    4467             :         }
    4468             : }
    4469             : /* }}} */
    4470             : 
    4471             : /* {{{ proto int PharFileInfo::getCRC32()
    4472             :  * Returns CRC32 code or throws an exception if not CRC checked
    4473             :  */
    4474           3 : PHP_METHOD(PharFileInfo, getCRC32)
    4475             : {
    4476           3 :         PHAR_ENTRY_OBJECT();
    4477             : 
    4478           3 :         if (zend_parse_parameters_none() == FAILURE) {
    4479           0 :                 return;
    4480             :         }
    4481             : 
    4482           3 :         if (entry_obj->entry->is_dir) {
    4483           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4484             :                         "Phar entry is a directory, does not have a CRC"); \
    4485           1 :                 return;
    4486             :         }
    4487             : 
    4488           2 :         if (entry_obj->entry->is_crc_checked) {
    4489           1 :                 RETURN_LONG(entry_obj->entry->crc32);
    4490             :         } else {
    4491           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4492             :                         "Phar entry was not CRC checked"); \
    4493             :         }
    4494             : }
    4495             : /* }}} */
    4496             : 
    4497             : /* {{{ proto int PharFileInfo::isCRCChecked()
    4498             :  * Returns whether file entry is CRC checked
    4499             :  */
    4500          10 : PHP_METHOD(PharFileInfo, isCRCChecked)
    4501             : {
    4502          10 :         PHAR_ENTRY_OBJECT();
    4503             : 
    4504          10 :         if (zend_parse_parameters_none() == FAILURE) {
    4505           0 :                 return;
    4506             :         }
    4507             : 
    4508          10 :         RETURN_BOOL(entry_obj->entry->is_crc_checked);
    4509             : }
    4510             : /* }}} */
    4511             : 
    4512             : /* {{{ proto int PharFileInfo::getPharFlags()
    4513             :  * Returns the Phar file entry flags
    4514             :  */
    4515           5 : PHP_METHOD(PharFileInfo, getPharFlags)
    4516             : {
    4517           5 :         PHAR_ENTRY_OBJECT();
    4518             : 
    4519           5 :         if (zend_parse_parameters_none() == FAILURE) {
    4520           0 :                 return;
    4521             :         }
    4522             : 
    4523           5 :         RETURN_LONG(entry_obj->entry->flags & ~(PHAR_ENT_PERM_MASK|PHAR_ENT_COMPRESSION_MASK));
    4524             : }
    4525             : /* }}} */
    4526             : 
    4527             : /* {{{ proto int PharFileInfo::chmod()
    4528             :  * set the file permissions for the Phar.  This only allows setting execution bit, read/write
    4529             :  */
    4530          23 : PHP_METHOD(PharFileInfo, chmod)
    4531             : {
    4532             :         char *error;
    4533             :         zend_long perms;
    4534          23 :         PHAR_ENTRY_OBJECT();
    4535             : 
    4536          23 :         if (entry_obj->entry->is_temp_dir) {
    4537           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4538           1 :                         "Phar entry \"%s\" is a temporary directory (not an actual entry in the archive), cannot chmod", entry_obj->entry->filename); \
    4539           1 :                 return;
    4540             :         }
    4541             : 
    4542          22 :         if (PHAR_G(readonly) && !entry_obj->entry->phar->is_data) {
    4543           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);
    4544           3 :                 return;
    4545             :         }
    4546             : 
    4547          19 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &perms) == FAILURE) {
    4548           1 :                 return;
    4549             :         }
    4550             : 
    4551          18 :         if (entry_obj->entry->is_persistent) {
    4552           0 :                 phar_archive_data *phar = entry_obj->entry->phar;
    4553             : 
    4554           0 :                 if (FAILURE == phar_copy_on_write(&phar)) {
    4555           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
    4556           0 :                         return;
    4557             :                 }
    4558             :                 /* re-populate after copy-on-write */
    4559           0 :                 entry_obj->entry = zend_hash_str_find_ptr(&phar->manifest, entry_obj->entry->filename, entry_obj->entry->filename_len);
    4560             :         }
    4561             :         /* clear permissions */
    4562          18 :         entry_obj->entry->flags &= ~PHAR_ENT_PERM_MASK;
    4563          18 :         perms &= 0777;
    4564          18 :         entry_obj->entry->flags |= perms;
    4565          18 :         entry_obj->entry->old_flags = entry_obj->entry->flags;
    4566          18 :         entry_obj->entry->phar->is_modified = 1;
    4567          18 :         entry_obj->entry->is_modified = 1;
    4568             : 
    4569             :         /* hackish cache in php_stat needs to be cleared */
    4570             :         /* if this code fails to work, check main/streams/streams.c, _php_stream_stat_path */
    4571          18 :         if (BG(CurrentLStatFile)) {
    4572           1 :                 efree(BG(CurrentLStatFile));
    4573             :         }
    4574             : 
    4575          18 :         if (BG(CurrentStatFile)) {
    4576          18 :                 efree(BG(CurrentStatFile));
    4577             :         }
    4578             : 
    4579          18 :         BG(CurrentLStatFile) = NULL;
    4580          18 :         BG(CurrentStatFile) = NULL;
    4581          18 :         phar_flush(entry_obj->entry->phar, 0, 0, 0, &error);
    4582             : 
    4583          18 :         if (error) {
    4584           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    4585           0 :                 efree(error);
    4586             :         }
    4587             : }
    4588             : /* }}} */
    4589             : 
    4590             : /* {{{ proto int PharFileInfo::hasMetaData()
    4591             :  * Returns the metadata of the entry
    4592             :  */
    4593           4 : PHP_METHOD(PharFileInfo, hasMetadata)
    4594             : {
    4595           4 :         PHAR_ENTRY_OBJECT();
    4596             : 
    4597           4 :         if (zend_parse_parameters_none() == FAILURE) {
    4598           0 :                 return;
    4599             :         }
    4600             : 
    4601           8 :         RETURN_BOOL(Z_TYPE(entry_obj->entry->metadata) != IS_UNDEF);
    4602             : }
    4603             : /* }}} */
    4604             : 
    4605             : /* {{{ proto int PharFileInfo::getMetaData()
    4606             :  * Returns the metadata of the entry
    4607             :  */
    4608          49 : PHP_METHOD(PharFileInfo, getMetadata)
    4609             : {
    4610          49 :         PHAR_ENTRY_OBJECT();
    4611             : 
    4612          49 :         if (zend_parse_parameters_none() == FAILURE) {
    4613           0 :                 return;
    4614             :         }
    4615             : 
    4616          98 :         if (Z_TYPE(entry_obj->entry->metadata) != IS_UNDEF) {
    4617          39 :                 if (entry_obj->entry->is_persistent) {
    4618             :                         zval ret;
    4619           0 :                         char *buf = estrndup((char *) Z_PTR(entry_obj->entry->metadata), entry_obj->entry->metadata_len);
    4620             :                         /* assume success, we would have failed before */
    4621           0 :                         phar_parse_metadata(&buf, &ret, entry_obj->entry->metadata_len);
    4622           0 :                         efree(buf);
    4623           0 :                         RETURN_ZVAL(&ret, 0, 1);
    4624             :                 }
    4625         117 :                 RETURN_ZVAL(&entry_obj->entry->metadata, 1, 0);
    4626             :         }
    4627             : }
    4628             : /* }}} */
    4629             : 
    4630             : /* {{{ proto int PharFileInfo::setMetaData(mixed $metadata)
    4631             :  * Sets the metadata of the entry
    4632             :  */
    4633          24 : PHP_METHOD(PharFileInfo, setMetadata)
    4634             : {
    4635             :         char *error;
    4636             :         zval *metadata;
    4637             : 
    4638          24 :         PHAR_ENTRY_OBJECT();
    4639             : 
    4640          24 :         if (PHAR_G(readonly) && !entry_obj->entry->phar->is_data) {
    4641           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
    4642           1 :                 return;
    4643             :         }
    4644             : 
    4645          23 :         if (entry_obj->entry->is_temp_dir) {
    4646           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4647             :                         "Phar entry is a temporary directory (not an actual entry in the archive), cannot set metadata"); \
    4648           1 :                 return;
    4649             :         }
    4650             : 
    4651          22 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &metadata) == FAILURE) {
    4652           1 :                 return;
    4653             :         }
    4654             : 
    4655          21 :         if (entry_obj->entry->is_persistent) {
    4656           0 :                 phar_archive_data *phar = entry_obj->entry->phar;
    4657             : 
    4658           0 :                 if (FAILURE == phar_copy_on_write(&phar)) {
    4659           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
    4660           0 :                         return;
    4661             :                 }
    4662             :                 /* re-populate after copy-on-write */
    4663           0 :                 entry_obj->entry = zend_hash_str_find_ptr(&phar->manifest, entry_obj->entry->filename, entry_obj->entry->filename_len);
    4664             :         }
    4665          42 :         if (Z_TYPE(entry_obj->entry->metadata) != IS_UNDEF) {
    4666          13 :                 zval_ptr_dtor(&entry_obj->entry->metadata);
    4667          13 :                 ZVAL_UNDEF(&entry_obj->entry->metadata);
    4668             :         }
    4669             : 
    4670          42 :         ZVAL_ZVAL(&entry_obj->entry->metadata, metadata, 1, 0);
    4671             : 
    4672          21 :         entry_obj->entry->is_modified = 1;
    4673          21 :         entry_obj->entry->phar->is_modified = 1;
    4674          21 :         phar_flush(entry_obj->entry->phar, 0, 0, 0, &error);
    4675             : 
    4676          21 :         if (error) {
    4677           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    4678           0 :                 efree(error);
    4679             :         }
    4680             : }
    4681             : /* }}} */
    4682             : 
    4683             : /* {{{ proto bool PharFileInfo::delMetaData()
    4684             :  * Deletes the metadata of the entry
    4685             :  */
    4686           7 : PHP_METHOD(PharFileInfo, delMetadata)
    4687             : {
    4688             :         char *error;
    4689             : 
    4690           7 :         PHAR_ENTRY_OBJECT();
    4691             : 
    4692           7 :         if (zend_parse_parameters_none() == FAILURE) {
    4693           0 :                 return;
    4694             :         }
    4695             : 
    4696           7 :         if (PHAR_G(readonly) && !entry_obj->entry->phar->is_data) {
    4697           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Write operations disabled by the php.ini setting phar.readonly");
    4698           1 :                 return;
    4699             :         }
    4700             : 
    4701           6 :         if (entry_obj->entry->is_temp_dir) {
    4702           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4703             :                         "Phar entry is a temporary directory (not an actual entry in the archive), cannot delete metadata"); \
    4704           1 :                 return;
    4705             :         }
    4706             : 
    4707          10 :         if (Z_TYPE(entry_obj->entry->metadata) != IS_UNDEF) {
    4708           3 :                 if (entry_obj->entry->is_persistent) {
    4709           0 :                         phar_archive_data *phar = entry_obj->entry->phar;
    4710             : 
    4711           0 :                         if (FAILURE == phar_copy_on_write(&phar)) {
    4712           0 :                                 zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
    4713           0 :                                 return;
    4714             :                         }
    4715             :                         /* re-populate after copy-on-write */
    4716           0 :                         entry_obj->entry = zend_hash_str_find_ptr(&phar->manifest, entry_obj->entry->filename, entry_obj->entry->filename_len);
    4717             :                 }
    4718           3 :                 zval_ptr_dtor(&entry_obj->entry->metadata);
    4719           3 :                 ZVAL_UNDEF(&entry_obj->entry->metadata);
    4720           3 :                 entry_obj->entry->is_modified = 1;
    4721           3 :                 entry_obj->entry->phar->is_modified = 1;
    4722             : 
    4723           3 :                 phar_flush(entry_obj->entry->phar, 0, 0, 0, &error);
    4724             : 
    4725           3 :                 if (error) {
    4726           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    4727           0 :                         efree(error);
    4728           0 :                         RETURN_FALSE;
    4729             :                 } else {
    4730           3 :                         RETURN_TRUE;
    4731             :                 }
    4732             : 
    4733             :         } else {
    4734           2 :                 RETURN_TRUE;
    4735             :         }
    4736             : }
    4737             : /* }}} */
    4738             : 
    4739             : /* {{{ proto string PharFileInfo::getContent()
    4740             :  * return the complete file contents of the entry (like file_get_contents)
    4741             :  */
    4742          37 : PHP_METHOD(PharFileInfo, getContent)
    4743             : {
    4744             :         char *error;
    4745             :         php_stream *fp;
    4746             :         phar_entry_info *link;
    4747             :         zend_string *str;
    4748             : 
    4749          37 :         PHAR_ENTRY_OBJECT();
    4750             : 
    4751          37 :         if (zend_parse_parameters_none() == FAILURE) {
    4752           0 :                 return;
    4753             :         }
    4754             : 
    4755          37 :         if (entry_obj->entry->is_dir) {
    4756           4 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4757           4 :                         "Phar error: Cannot retrieve contents, \"%s\" in phar \"%s\" is a directory", entry_obj->entry->filename, entry_obj->entry->phar->fname);
    4758           2 :                 return;
    4759             :         }
    4760             : 
    4761          35 :         link = phar_get_link_source(entry_obj->entry);
    4762             : 
    4763          35 :         if (!link) {
    4764           0 :                 link = entry_obj->entry;
    4765             :         }
    4766             : 
    4767          35 :         if (SUCCESS != phar_open_entry_fp(link, &error, 0)) {
    4768           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4769           0 :                         "Phar error: Cannot retrieve contents, \"%s\" in phar \"%s\": %s", entry_obj->entry->filename, entry_obj->entry->phar->fname, error);
    4770           0 :                 efree(error);
    4771           0 :                 return;
    4772             :         }
    4773             : 
    4774          35 :         if (!(fp = phar_get_efp(link, 0))) {
    4775           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4776           0 :                         "Phar error: Cannot retrieve contents of \"%s\" in phar \"%s\"", entry_obj->entry->filename, entry_obj->entry->phar->fname);
    4777           0 :                 return;
    4778             :         }
    4779             : 
    4780          35 :         phar_seek_efp(link, 0, SEEK_SET, 0, 0);
    4781          35 :         str = php_stream_copy_to_mem(fp, link->uncompressed_filesize, 0);
    4782          35 :         if (str) {
    4783          35 :                 RETURN_STR(str);
    4784             :         } else {
    4785           0 :                 RETURN_EMPTY_STRING();
    4786             :         }
    4787             : }
    4788             : /* }}} */
    4789             : 
    4790             : /* {{{ proto int PharFileInfo::compress(int compression_type)
    4791             :  * Instructs the Phar class to compress the current file using zlib or bzip2 compression
    4792             :  */
    4793          16 : PHP_METHOD(PharFileInfo, compress)
    4794             : {
    4795             :         zend_long method;
    4796             :         char *error;
    4797          16 :         PHAR_ENTRY_OBJECT();
    4798             : 
    4799          16 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &method) == FAILURE) {
    4800           0 :                 return;
    4801             :         }
    4802             : 
    4803          16 :         if (entry_obj->entry->is_tar) {
    4804           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4805             :                         "Cannot compress with Gzip compression, not possible with tar-based phar archives");
    4806           1 :                 return;
    4807             :         }
    4808             : 
    4809          15 :         if (entry_obj->entry->is_dir) {
    4810           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4811             :                         "Phar entry is a directory, cannot set compression"); \
    4812           1 :                 return;
    4813             :         }
    4814             : 
    4815          14 :         if (PHAR_G(readonly) && !entry_obj->entry->phar->is_data) {
    4816           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4817             :                         "Phar is readonly, cannot change compression");
    4818           1 :                 return;
    4819             :         }
    4820             : 
    4821          13 :         if (entry_obj->entry->is_deleted) {
    4822           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4823             :                         "Cannot compress deleted file");
    4824           0 :                 return;
    4825             :         }
    4826             : 
    4827          13 :         if (entry_obj->entry->is_persistent) {
    4828           0 :                 phar_archive_data *phar = entry_obj->entry->phar;
    4829             : 
    4830           0 :                 if (FAILURE == phar_copy_on_write(&phar)) {
    4831           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
    4832           0 :                         return;
    4833             :                 }
    4834             :                 /* re-populate after copy-on-write */
    4835           0 :                 entry_obj->entry = zend_hash_str_find_ptr(&phar->manifest, entry_obj->entry->filename, entry_obj->entry->filename_len);
    4836             :         }
    4837          13 :         switch (method) {
    4838             :                 case PHAR_ENT_COMPRESSED_GZ:
    4839           8 :                         if (entry_obj->entry->flags & PHAR_ENT_COMPRESSED_GZ) {
    4840           2 :                                 RETURN_TRUE;
    4841             :                         }
    4842             : 
    4843           6 :                         if ((entry_obj->entry->flags & PHAR_ENT_COMPRESSED_BZ2) != 0) {
    4844           0 :                                 if (!PHAR_G(has_bz2)) {
    4845           0 :                                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4846             :                                                 "Cannot compress with gzip compression, file is already compressed with bzip2 compression and bz2 extension is not enabled, cannot decompress");
    4847           0 :                                         return;
    4848             :                                 }
    4849             : 
    4850             :                                 /* decompress this file indirectly */
    4851           0 :                                 if (SUCCESS != phar_open_entry_fp(entry_obj->entry, &error, 1)) {
    4852           0 :                                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4853           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);
    4854           0 :                                         efree(error);
    4855           0 :                                         return;
    4856             :                                 }
    4857             :                         }
    4858             : 
    4859           6 :                         if (!PHAR_G(has_zlib)) {
    4860           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4861             :                                         "Cannot compress with gzip compression, zlib extension is not enabled");
    4862           0 :                                 return;
    4863             :                         }
    4864             : 
    4865           6 :                         entry_obj->entry->old_flags = entry_obj->entry->flags;
    4866           6 :                         entry_obj->entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
    4867           6 :                         entry_obj->entry->flags |= PHAR_ENT_COMPRESSED_GZ;
    4868           6 :                         break;
    4869             :                 case PHAR_ENT_COMPRESSED_BZ2:
    4870           4 :                         if (entry_obj->entry->flags & PHAR_ENT_COMPRESSED_BZ2) {
    4871           1 :                                 RETURN_TRUE;
    4872             :                         }
    4873             : 
    4874           3 :                         if ((entry_obj->entry->flags & PHAR_ENT_COMPRESSED_GZ) != 0) {
    4875           1 :                                 if (!PHAR_G(has_zlib)) {
    4876           0 :                                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4877             :                                                 "Cannot compress with bzip2 compression, file is already compressed with gzip compression and zlib extension is not enabled, cannot decompress");
    4878           0 :                                         return;
    4879             :                                 }
    4880             : 
    4881             :                                 /* decompress this file indirectly */
    4882           1 :                                 if (SUCCESS != phar_open_entry_fp(entry_obj->entry, &error, 1)) {
    4883           0 :                                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4884           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);
    4885           0 :                                         efree(error);
    4886           0 :                                         return;
    4887             :                                 }
    4888             :                         }
    4889             : 
    4890           3 :                         if (!PHAR_G(has_bz2)) {
    4891           0 :                                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4892             :                                         "Cannot compress with bzip2 compression, bz2 extension is not enabled");
    4893           0 :                                 return;
    4894             :                         }
    4895           3 :                         entry_obj->entry->old_flags = entry_obj->entry->flags;
    4896           3 :                         entry_obj->entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
    4897           3 :                         entry_obj->entry->flags |= PHAR_ENT_COMPRESSED_BZ2;
    4898           3 :                         break;
    4899             :                 default:
    4900           1 :                         zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4901             :                                 "Unknown compression type specified"); \
    4902             :         }
    4903             : 
    4904          10 :         entry_obj->entry->phar->is_modified = 1;
    4905          10 :         entry_obj->entry->is_modified = 1;
    4906          10 :         phar_flush(entry_obj->entry->phar, 0, 0, 0, &error);
    4907             : 
    4908          10 :         if (error) {
    4909           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    4910           0 :                 efree(error);
    4911             :         }
    4912             : 
    4913          10 :         RETURN_TRUE;
    4914             : }
    4915             : /* }}} */
    4916             : 
    4917             : /* {{{ proto int PharFileInfo::decompress()
    4918             :  * Instructs the Phar class to decompress the current file
    4919             :  */
    4920           7 : PHP_METHOD(PharFileInfo, decompress)
    4921             : {
    4922             :         char *error;
    4923           7 :         PHAR_ENTRY_OBJECT();
    4924             : 
    4925           7 :         if (zend_parse_parameters_none() == FAILURE) {
    4926           0 :                 return;
    4927             :         }
    4928             : 
    4929           7 :         if (entry_obj->entry->is_dir) {
    4930           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, \
    4931             :                         "Phar entry is a directory, cannot set compression"); \
    4932           1 :                 return;
    4933             :         }
    4934             : 
    4935           6 :         if ((entry_obj->entry->flags & PHAR_ENT_COMPRESSION_MASK) == 0) {
    4936           3 :                 RETURN_TRUE;
    4937             :         }
    4938             : 
    4939           3 :         if (PHAR_G(readonly) && !entry_obj->entry->phar->is_data) {
    4940           1 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4941             :                         "Phar is readonly, cannot decompress");
    4942           1 :                 return;
    4943             :         }
    4944             : 
    4945           2 :         if (entry_obj->entry->is_deleted) {
    4946           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4947             :                         "Cannot compress deleted file");
    4948           0 :                 return;
    4949             :         }
    4950             : 
    4951           2 :         if ((entry_obj->entry->flags & PHAR_ENT_COMPRESSED_GZ) != 0 && !PHAR_G(has_zlib)) {
    4952           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4953             :                         "Cannot decompress Gzip-compressed file, zlib extension is not enabled");
    4954           0 :                 return;
    4955             :         }
    4956             : 
    4957           2 :         if ((entry_obj->entry->flags & PHAR_ENT_COMPRESSED_BZ2) != 0 && !PHAR_G(has_bz2)) {
    4958           0 :                 zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
    4959             :                         "Cannot decompress Bzip2-compressed file, bz2 extension is not enabled");
    4960           0 :                 return;
    4961             :         }
    4962             : 
    4963           2 :         if (entry_obj->entry->is_persistent) {
    4964           0 :                 phar_archive_data *phar = entry_obj->entry->phar;
    4965             : 
    4966           0 :                 if (FAILURE == phar_copy_on_write(&phar)) {
    4967           0 :                         zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar->fname);
    4968           0 :                         return;
    4969             :                 }
    4970             :                 /* re-populate after copy-on-write */
    4971           0 :                 entry_obj->entry = zend_hash_str_find_ptr(&phar->manifest, entry_obj->entry->filename, entry_obj->entry->filename_len);
    4972             :         }
    4973           2 :         if (!entry_obj->entry->fp) {
    4974           2 :                 if (FAILURE == phar_open_archive_fp(entry_obj->entry->phar)) {
    4975           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);
    4976           0 :                         return;
    4977             :                 }
    4978           2 :                 entry_obj->entry->fp_type = PHAR_FP;
    4979             :         }
    4980             : 
    4981           2 :         entry_obj->entry->old_flags = entry_obj->entry->flags;
    4982           2 :         entry_obj->entry->flags &= ~PHAR_ENT_COMPRESSION_MASK;
    4983           2 :         entry_obj->entry->phar->is_modified = 1;
    4984           2 :         entry_obj->entry->is_modified = 1;
    4985           2 :         phar_flush(entry_obj->entry->phar, 0, 0, 0, &error);
    4986             : 
    4987           2 :         if (error) {
    4988           0 :                 zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
    4989           0 :                 efree(error);
    4990             :         }
    4991           2 :         RETURN_TRUE;
    4992             : }
    4993             : /* }}} */
    4994             : 
    4995             : #endif /* HAVE_SPL */
    4996             : 
    4997             : /* {{{ phar methods */
    4998             : PHAR_ARG_INFO
    4999             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar___construct, 0, 0, 1)
    5000             :         ZEND_ARG_INFO(0, filename)
    5001             :         ZEND_ARG_INFO(0, flags)
    5002             :         ZEND_ARG_INFO(0, alias)
    5003             :         ZEND_ARG_INFO(0, fileformat)
    5004             : ZEND_END_ARG_INFO()
    5005             : 
    5006             : PHAR_ARG_INFO
    5007             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_createDS, 0, 0, 0)
    5008             :         ZEND_ARG_INFO(0, index)
    5009             :         ZEND_ARG_INFO(0, webindex)
    5010             : ZEND_END_ARG_INFO()
    5011             : 
    5012             : PHAR_ARG_INFO
    5013             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_cancompress, 0, 0, 0)
    5014             :         ZEND_ARG_INFO(0, method)
    5015             : ZEND_END_ARG_INFO()
    5016             : 
    5017             : PHAR_ARG_INFO
    5018             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_isvalidpharfilename, 0, 0, 1)
    5019             :         ZEND_ARG_INFO(0, filename)
    5020             :         ZEND_ARG_INFO(0, executable)
    5021             : ZEND_END_ARG_INFO()
    5022             : 
    5023             : PHAR_ARG_INFO
    5024             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_loadPhar, 0, 0, 1)
    5025             :         ZEND_ARG_INFO(0, filename)
    5026             :         ZEND_ARG_INFO(0, alias)
    5027             : ZEND_END_ARG_INFO()
    5028             : 
    5029             : PHAR_ARG_INFO
    5030             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_mapPhar, 0, 0, 0)
    5031             :         ZEND_ARG_INFO(0, alias)
    5032             :         ZEND_ARG_INFO(0, offset)
    5033             : ZEND_END_ARG_INFO()
    5034             : 
    5035             : PHAR_ARG_INFO
    5036             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_mount, 0, 0, 2)
    5037             :         ZEND_ARG_INFO(0, inphar)
    5038             :         ZEND_ARG_INFO(0, externalfile)
    5039             : ZEND_END_ARG_INFO()
    5040             : 
    5041             : PHAR_ARG_INFO
    5042             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_mungServer, 0, 0, 1)
    5043             :         ZEND_ARG_INFO(0, munglist)
    5044             : ZEND_END_ARG_INFO()
    5045             : 
    5046             : PHAR_ARG_INFO
    5047             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_webPhar, 0, 0, 0)
    5048             :         ZEND_ARG_INFO(0, alias)
    5049             :         ZEND_ARG_INFO(0, index)
    5050             :         ZEND_ARG_INFO(0, f404)
    5051             :         ZEND_ARG_INFO(0, mimetypes)
    5052             :         ZEND_ARG_INFO(0, rewrites)
    5053             : ZEND_END_ARG_INFO()
    5054             : 
    5055             : PHAR_ARG_INFO
    5056             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_running, 0, 0, 1)
    5057             :         ZEND_ARG_INFO(0, retphar)
    5058             : ZEND_END_ARG_INFO()
    5059             : 
    5060             : PHAR_ARG_INFO
    5061             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_ua, 0, 0, 1)
    5062             :         ZEND_ARG_INFO(0, archive)
    5063             : ZEND_END_ARG_INFO()
    5064             : 
    5065             : #if HAVE_SPL
    5066             : PHAR_ARG_INFO
    5067             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_build, 0, 0, 1)
    5068             :         ZEND_ARG_INFO(0, iterator)
    5069             :         ZEND_ARG_INFO(0, base_directory)
    5070             : ZEND_END_ARG_INFO()
    5071             : 
    5072             : PHAR_ARG_INFO
    5073             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_conv, 0, 0, 0)
    5074             :         ZEND_ARG_INFO(0, format)
    5075             :         ZEND_ARG_INFO(0, compression_type)
    5076             :         ZEND_ARG_INFO(0, file_ext)
    5077             : ZEND_END_ARG_INFO()
    5078             : 
    5079             : PHAR_ARG_INFO
    5080             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_comps, 0, 0, 1)
    5081             :         ZEND_ARG_INFO(0, compression_type)
    5082             :         ZEND_ARG_INFO(0, file_ext)
    5083             : ZEND_END_ARG_INFO()
    5084             : 
    5085             : PHAR_ARG_INFO
    5086             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_decomp, 0, 0, 0)
    5087             :         ZEND_ARG_INFO(0, file_ext)
    5088             : ZEND_END_ARG_INFO()
    5089             : 
    5090             : PHAR_ARG_INFO
    5091             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_comp, 0, 0, 1)
    5092             :         ZEND_ARG_INFO(0, compression_type)
    5093             : ZEND_END_ARG_INFO()
    5094             : 
    5095             : PHAR_ARG_INFO
    5096             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_compo, 0, 0, 0)
    5097             :         ZEND_ARG_INFO(0, compression_type)
    5098             : ZEND_END_ARG_INFO()
    5099             : 
    5100             : PHAR_ARG_INFO
    5101             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_copy, 0, 0, 2)
    5102             :         ZEND_ARG_INFO(0, newfile)
    5103             :         ZEND_ARG_INFO(0, oldfile)
    5104             : ZEND_END_ARG_INFO()
    5105             : 
    5106             : PHAR_ARG_INFO
    5107             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_delete, 0, 0, 1)
    5108             :         ZEND_ARG_INFO(0, entry)
    5109             : ZEND_END_ARG_INFO()
    5110             : 
    5111             : PHAR_ARG_INFO
    5112             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_fromdir, 0, 0, 1)
    5113             :         ZEND_ARG_INFO(0, base_dir)
    5114             :         ZEND_ARG_INFO(0, regex)
    5115             : ZEND_END_ARG_INFO()
    5116             : 
    5117             : PHAR_ARG_INFO
    5118             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_offsetExists, 0, 0, 1)
    5119             :         ZEND_ARG_INFO(0, entry)
    5120             : ZEND_END_ARG_INFO()
    5121             : 
    5122             : PHAR_ARG_INFO
    5123             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_offsetSet, 0, 0, 2)
    5124             :         ZEND_ARG_INFO(0, entry)
    5125             :         ZEND_ARG_INFO(0, value)
    5126             : ZEND_END_ARG_INFO()
    5127             : 
    5128             : PHAR_ARG_INFO
    5129             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setAlias, 0, 0, 1)
    5130             :         ZEND_ARG_INFO(0, alias)
    5131             : ZEND_END_ARG_INFO()
    5132             : 
    5133             : PHAR_ARG_INFO
    5134             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setMetadata, 0, 0, 1)
    5135             :         ZEND_ARG_INFO(0, metadata)
    5136             : ZEND_END_ARG_INFO()
    5137             : 
    5138             : PHAR_ARG_INFO
    5139             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setSigAlgo, 0, 0, 1)
    5140             :         ZEND_ARG_INFO(0, algorithm)
    5141             :         ZEND_ARG_INFO(0, privatekey)
    5142             : ZEND_END_ARG_INFO()
    5143             : 
    5144             : PHAR_ARG_INFO
    5145             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_setStub, 0, 0, 1)
    5146             :         ZEND_ARG_INFO(0, newstub)
    5147             :         ZEND_ARG_INFO(0, maxlen)
    5148             : ZEND_END_ARG_INFO()
    5149             : 
    5150             : PHAR_ARG_INFO
    5151             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_emptydir, 0, 0, 0)
    5152             :         ZEND_ARG_INFO(0, dirname)
    5153             : ZEND_END_ARG_INFO()
    5154             : 
    5155             : PHAR_ARG_INFO
    5156             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_extract, 0, 0, 1)
    5157             :         ZEND_ARG_INFO(0, pathto)
    5158             :         ZEND_ARG_INFO(0, files)
    5159             :         ZEND_ARG_INFO(0, overwrite)
    5160             : ZEND_END_ARG_INFO()
    5161             : 
    5162             : PHAR_ARG_INFO
    5163             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_addfile, 0, 0, 1)
    5164             :         ZEND_ARG_INFO(0, filename)
    5165             :         ZEND_ARG_INFO(0, localname)
    5166             : ZEND_END_ARG_INFO()
    5167             : 
    5168             : PHAR_ARG_INFO
    5169             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_fromstring, 0, 0, 1)
    5170             :         ZEND_ARG_INFO(0, localname)
    5171             :         ZEND_ARG_INFO(0, contents)
    5172             : ZEND_END_ARG_INFO()
    5173             : 
    5174             : PHAR_ARG_INFO
    5175             : ZEND_BEGIN_ARG_INFO_EX(arginfo_phar_isff, 0, 0, 1)
    5176             :         ZEND_ARG_INFO(0, fileformat)
    5177             : ZEND_END_ARG_INFO()
    5178             : 
    5179             : PHAR_ARG_INFO
    5180             : ZEND_BEGIN_ARG_INFO(arginfo_phar__void, 0)
    5181             : ZEND_END_ARG_INFO()
    5182             : 
    5183             : 
    5184             : #endif /* HAVE_SPL */
    5185             : 
    5186             : zend_function_entry php_archive_methods[] = {
    5187             : #if !HAVE_SPL
    5188             :         PHP_ME(Phar, __construct,           arginfo_phar___construct,  ZEND_ACC_PRIVATE)
    5189             : #else
    5190             :         PHP_ME(Phar, __construct,           arginfo_phar___construct,  ZEND_ACC_PUBLIC)
    5191             :         PHP_ME(Phar, __destruct,            arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5192             :         PHP_ME(Phar, addEmptyDir,           arginfo_phar_emptydir,     ZEND_ACC_PUBLIC)
    5193             :         PHP_ME(Phar, addFile,               arginfo_phar_addfile,      ZEND_ACC_PUBLIC)
    5194             :         PHP_ME(Phar, addFromString,         arginfo_phar_fromstring,   ZEND_ACC_PUBLIC)
    5195             :         PHP_ME(Phar, buildFromDirectory,    arginfo_phar_fromdir,      ZEND_ACC_PUBLIC)
    5196             :         PHP_ME(Phar, buildFromIterator,     arginfo_phar_build,        ZEND_ACC_PUBLIC)
    5197             :         PHP_ME(Phar, compressFiles,         arginfo_phar_comp,         ZEND_ACC_PUBLIC)
    5198             :         PHP_ME(Phar, decompressFiles,       arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5199             :         PHP_ME(Phar, compress,              arginfo_phar_comps,        ZEND_ACC_PUBLIC)
    5200             :         PHP_ME(Phar, decompress,            arginfo_phar_decomp,       ZEND_ACC_PUBLIC)
    5201             :         PHP_ME(Phar, convertToExecutable,   arginfo_phar_conv,         ZEND_ACC_PUBLIC)
    5202             :         PHP_ME(Phar, convertToData,         arginfo_phar_conv,         ZEND_ACC_PUBLIC)
    5203             :         PHP_ME(Phar, copy,                  arginfo_phar_copy,         ZEND_ACC_PUBLIC)
    5204             :         PHP_ME(Phar, count,                 arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5205             :         PHP_ME(Phar, delete,                arginfo_phar_delete,       ZEND_ACC_PUBLIC)
    5206             :         PHP_ME(Phar, delMetadata,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5207             :         PHP_ME(Phar, extractTo,             arginfo_phar_extract,      ZEND_ACC_PUBLIC)
    5208             :         PHP_ME(Phar, getAlias,              arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5209             :         PHP_ME(Phar, getPath,               arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5210             :         PHP_ME(Phar, getMetadata,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5211             :         PHP_ME(Phar, getModified,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5212             :         PHP_ME(Phar, getSignature,          arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5213             :         PHP_ME(Phar, getStub,               arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5214             :         PHP_ME(Phar, getVersion,            arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5215             :         PHP_ME(Phar, hasMetadata,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5216             :         PHP_ME(Phar, isBuffering,           arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5217             :         PHP_ME(Phar, isCompressed,          arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5218             :         PHP_ME(Phar, isFileFormat,          arginfo_phar_isff,         ZEND_ACC_PUBLIC)
    5219             :         PHP_ME(Phar, isWritable,            arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5220             :         PHP_ME(Phar, offsetExists,          arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
    5221             :         PHP_ME(Phar, offsetGet,             arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
    5222             :         PHP_ME(Phar, offsetSet,             arginfo_phar_offsetSet,    ZEND_ACC_PUBLIC)
    5223             :         PHP_ME(Phar, offsetUnset,           arginfo_phar_offsetExists, ZEND_ACC_PUBLIC)
    5224             :         PHP_ME(Phar, setAlias,              arginfo_phar_setAlias,     ZEND_ACC_PUBLIC)
    5225             :         PHP_ME(Phar, setDefaultStub,        arginfo_phar_createDS,     ZEND_ACC_PUBLIC)
    5226             :         PHP_ME(Phar, setMetadata,           arginfo_phar_setMetadata,  ZEND_ACC_PUBLIC)
    5227             :         PHP_ME(Phar, setSignatureAlgorithm, arginfo_phar_setSigAlgo,   ZEND_ACC_PUBLIC)
    5228             :         PHP_ME(Phar, setStub,               arginfo_phar_setStub,      ZEND_ACC_PUBLIC)
    5229             :         PHP_ME(Phar, startBuffering,        arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5230             :         PHP_ME(Phar, stopBuffering,         arginfo_phar__void,        ZEND_ACC_PUBLIC)
    5231             : #endif
    5232             :         /* static member functions */
    5233             :         PHP_ME(Phar, apiVersion,            arginfo_phar__void,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5234             :         PHP_ME(Phar, canCompress,           arginfo_phar_cancompress,  ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5235             :         PHP_ME(Phar, canWrite,              arginfo_phar__void,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5236             :         PHP_ME(Phar, createDefaultStub,     arginfo_phar_createDS,     ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5237             :         PHP_ME(Phar, getSupportedCompression,arginfo_phar__void,       ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5238             :         PHP_ME(Phar, getSupportedSignatures,arginfo_phar__void,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5239             :         PHP_ME(Phar, interceptFileFuncs,    arginfo_phar__void,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5240             :         PHP_ME(Phar, isValidPharFilename,   arginfo_phar_isvalidpharfilename, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5241             :         PHP_ME(Phar, loadPhar,              arginfo_phar_loadPhar,     ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5242             :         PHP_ME(Phar, mapPhar,               arginfo_phar_mapPhar,      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5243             :         PHP_ME(Phar, running,               arginfo_phar_running,      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5244             :         PHP_ME(Phar, mount,                 arginfo_phar_mount,        ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5245             :         PHP_ME(Phar, mungServer,            arginfo_phar_mungServer,   ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5246             :         PHP_ME(Phar, unlinkArchive,         arginfo_phar_ua,           ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5247             :         PHP_ME(Phar, webPhar,               arginfo_phar_webPhar,      ZEND_ACC_PUBLIC|ZEND_ACC_STATIC|ZEND_ACC_FINAL)
    5248             :         PHP_FE_END
    5249             : };
    5250             : 
    5251             : #if HAVE_SPL
    5252             : PHAR_ARG_INFO
    5253             : ZEND_BEGIN_ARG_INFO_EX(arginfo_entry___construct, 0, 0, 1)
    5254             :         ZEND_ARG_INFO(0, filename)
    5255             : ZEND_END_ARG_INFO()
    5256             : 
    5257             : PHAR_ARG_INFO
    5258             : ZEND_BEGIN_ARG_INFO_EX(arginfo_entry_chmod, 0, 0, 1)
    5259             :         ZEND_ARG_INFO(0, perms)
    5260             : ZEND_END_ARG_INFO()
    5261             : 
    5262             : zend_function_entry php_entry_methods[] = {
    5263             :         PHP_ME(PharFileInfo, __construct,        arginfo_entry___construct,  ZEND_ACC_PUBLIC)
    5264             :         PHP_ME(PharFileInfo, __destruct,         arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5265             :         PHP_ME(PharFileInfo, chmod,              arginfo_entry_chmod,        ZEND_ACC_PUBLIC)
    5266             :         PHP_ME(PharFileInfo, compress,           arginfo_phar_comp,          ZEND_ACC_PUBLIC)
    5267             :         PHP_ME(PharFileInfo, decompress,         arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5268             :         PHP_ME(PharFileInfo, delMetadata,        arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5269             :         PHP_ME(PharFileInfo, getCompressedSize,  arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5270             :         PHP_ME(PharFileInfo, getCRC32,           arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5271             :         PHP_ME(PharFileInfo, getContent,         arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5272             :         PHP_ME(PharFileInfo, getMetadata,        arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5273             :         PHP_ME(PharFileInfo, getPharFlags,       arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5274             :         PHP_ME(PharFileInfo, hasMetadata,        arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5275             :         PHP_ME(PharFileInfo, isCompressed,       arginfo_phar_compo,         ZEND_ACC_PUBLIC)
    5276             :         PHP_ME(PharFileInfo, isCRCChecked,       arginfo_phar__void,         ZEND_ACC_PUBLIC)
    5277             :         PHP_ME(PharFileInfo, setMetadata,        arginfo_phar_setMetadata,   ZEND_ACC_PUBLIC)
    5278             :         PHP_FE_END
    5279             : };
    5280             : #endif /* HAVE_SPL */
    5281             : 
    5282             : zend_function_entry phar_exception_methods[] = {
    5283             :         PHP_FE_END
    5284             : };
    5285             : /* }}} */
    5286             : 
    5287             : #define REGISTER_PHAR_CLASS_CONST_LONG(class_name, const_name, value) \
    5288             :         zend_declare_class_constant_long(class_name, const_name, sizeof(const_name)-1, (zend_long)value);
    5289             : 
    5290             : #define phar_exception_get_default() zend_exception_get_default()
    5291             : 
    5292       20871 : void phar_object_init(void) /* {{{ */
    5293             : {
    5294             :         zend_class_entry ce;
    5295             : 
    5296       20871 :         INIT_CLASS_ENTRY(ce, "PharException", phar_exception_methods);
    5297       20871 :         phar_ce_PharException = zend_register_internal_class_ex(&ce, phar_exception_get_default());
    5298             : 
    5299             : #if HAVE_SPL
    5300       20871 :         INIT_CLASS_ENTRY(ce, "Phar", php_archive_methods);
    5301       20871 :         phar_ce_archive = zend_register_internal_class_ex(&ce, spl_ce_RecursiveDirectoryIterator);
    5302             : 
    5303       20871 :         zend_class_implements(phar_ce_archive, 2, spl_ce_Countable, zend_ce_arrayaccess);
    5304             : 
    5305       20871 :         INIT_CLASS_ENTRY(ce, "PharData", php_archive_methods);
    5306       20871 :         phar_ce_data = zend_register_internal_class_ex(&ce, spl_ce_RecursiveDirectoryIterator);
    5307             : 
    5308       20871 :         zend_class_implements(phar_ce_data, 2, spl_ce_Countable, zend_ce_arrayaccess);
    5309             : 
    5310       20871 :         INIT_CLASS_ENTRY(ce, "PharFileInfo", php_entry_methods);
    5311       20871 :         phar_ce_entry = zend_register_internal_class_ex(&ce, spl_ce_SplFileInfo);
    5312             : #else
    5313             :         INIT_CLASS_ENTRY(ce, "Phar", php_archive_methods);
    5314             :         phar_ce_archive = zend_register_internal_class(&ce);
    5315             :         phar_ce_archive->ce_flags |= ZEND_ACC_FINAL;
    5316             : 
    5317             :         INIT_CLASS_ENTRY(ce, "PharData", php_archive_methods);
    5318             :         phar_ce_data = zend_register_internal_class(&ce);
    5319             :         phar_ce_data->ce_flags |= ZEND_ACC_FINAL;
    5320             : #endif
    5321             : 
    5322       20871 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "BZ2", PHAR_ENT_COMPRESSED_BZ2)
    5323       20871 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "GZ", PHAR_ENT_COMPRESSED_GZ)
    5324       20871 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "NONE", PHAR_ENT_COMPRESSED_NONE)
    5325       20871 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "PHAR", PHAR_FORMAT_PHAR)
    5326       20871 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "TAR", PHAR_FORMAT_TAR)
    5327       20871 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "ZIP", PHAR_FORMAT_ZIP)
    5328       20871 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "COMPRESSED", PHAR_ENT_COMPRESSION_MASK)
    5329       20871 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "PHP", PHAR_MIME_PHP)
    5330       20871 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "PHPS", PHAR_MIME_PHPS)
    5331       20871 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "MD5", PHAR_SIG_MD5)
    5332       20871 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "OPENSSL", PHAR_SIG_OPENSSL)
    5333       20871 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA1", PHAR_SIG_SHA1)
    5334       20871 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA256", PHAR_SIG_SHA256)
    5335       20871 :         REGISTER_PHAR_CLASS_CONST_LONG(phar_ce_archive, "SHA512", PHAR_SIG_SHA512)
    5336       20871 : }
    5337             : /* }}} */
    5338             : 
    5339             : /*
    5340             :  * Local variables:
    5341             :  * tab-width: 4
    5342             :  * c-basic-offset: 4
    5343             :  * End:
    5344             :  * vim600: noet sw=4 ts=4 fdm=marker
    5345             :  * vim<600: noet sw=4 ts=4
    5346             :  */

Generated by: LCOV version 1.10

Generated at Tue, 14 Apr 2015 11:48:49 +0000 (11 days ago)

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