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

Generated by: LCOV version 1.10

Generated at Tue, 26 Jul 2016 17:07:42 +0000 (22 hours ago)

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