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

LCOV - code coverage report
Current view: top level - ext/phar - phar_object.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 1902 2450 77.6 %
Date: 2015-07-05 Functions: 90 91 98.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 05 Jul 2015 15:44:53 +0000 (2 days ago)

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