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

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

Generated by: LCOV version 1.10

Generated at Fri, 19 Sep 2014 17:11:14 +0000 (3 days ago)

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