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

Generated by: LCOV version 1.10

Generated at Tue, 22 Jul 2014 01:33:16 +0000 (2 days ago)

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