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: 1937 2513 77.1 %
Date: 2014-04-18 Functions: 90 91 98.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Fri, 18 Apr 2014 07:01:33 +0000 (16 hours ago)

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