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

Generated by: LCOV version 1.10

Generated at Sun, 06 Apr 2014 17:31:07 +0000 (9 days ago)

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