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

Generated by: LCOV version 1.10

Generated at Thu, 30 Oct 2014 07:41:37 +0000 (2 days ago)

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