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.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 1404 1792 78.3 %
Date: 2015-08-29 Functions: 46 46 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | phar php single-file executable PHP extension                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 2005-2015 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: 1ed4c8034d7a1b19ec1cc7fe638d845b897300b1 $ */
      21             : 
      22             : #define PHAR_MAIN 1
      23             : #include "phar_internal.h"
      24             : #include "SAPI.h"
      25             : #include "func_interceptors.h"
      26             : 
      27             : static void destroy_phar_data(zval *zv);
      28             : 
      29             : ZEND_DECLARE_MODULE_GLOBALS(phar)
      30             : zend_string *(*phar_save_resolve_path)(const char *filename, int filename_len);
      31             : 
      32             : /**
      33             :  * set's phar->is_writeable based on the current INI value
      34             :  */
      35          95 : static int phar_set_writeable_bit(zval *zv, void *argument) /* {{{ */
      36             : {
      37          95 :         zend_bool keep = *(zend_bool *)argument;
      38          95 :         phar_archive_data *phar = (phar_archive_data *)Z_PTR_P(zv);
      39             : 
      40          95 :         if (!phar->is_data) {
      41          68 :                 phar->is_writeable = !keep;
      42             :         }
      43             : 
      44          95 :         return ZEND_HASH_APPLY_KEEP;
      45             : }
      46             : /* }}} */
      47             : 
      48             : /* if the original value is 0 (disabled), then allow setting/unsetting at will. Otherwise only allow 1 (enabled), and error on disabling */
      49       42679 : ZEND_INI_MH(phar_ini_modify_handler) /* {{{ */
      50             : {
      51             :         zend_bool old, ini;
      52             : 
      53       42679 :         if (ZSTR_LEN(entry->name) == sizeof("phar.readonly")-1) {
      54       21373 :                 old = PHAR_G(readonly_orig);
      55             :         } else {
      56       21306 :                 old = PHAR_G(require_hash_orig);
      57             :         }
      58             : 
      59       42681 :         if (ZSTR_LEN(new_value) == 2 && !strcasecmp("on", ZSTR_VAL(new_value))) {
      60           2 :                 ini = (zend_bool) 1;
      61             :         }
      62       42679 :         else if (ZSTR_LEN(new_value) == 3 && !strcasecmp("yes", ZSTR_VAL(new_value))) {
      63           2 :                 ini = (zend_bool) 1;
      64             :         }
      65       42677 :         else if (ZSTR_LEN(new_value) == 4 && !strcasecmp("true", ZSTR_VAL(new_value))) {
      66           2 :                 ini = (zend_bool) 1;
      67             :         }
      68             :         else {
      69       42673 :                 ini = (zend_bool) atoi(ZSTR_VAL(new_value));
      70             :         }
      71             : 
      72             :         /* do not allow unsetting in runtime */
      73       42679 :         if (stage == ZEND_INI_STAGE_STARTUP) {
      74       42582 :                 if (ZSTR_LEN(entry->name) == sizeof("phar.readonly")-1) {
      75       21291 :                         PHAR_G(readonly_orig) = ini;
      76             :                 } else {
      77       21291 :                         PHAR_G(require_hash_orig) = ini;
      78             :                 }
      79          97 :         } else if (old && !ini) {
      80           2 :                 return FAILURE;
      81             :         }
      82             : 
      83       42677 :         if (ZSTR_LEN(entry->name) == sizeof("phar.readonly")-1) {
      84       21372 :                 PHAR_G(readonly) = ini;
      85       21372 :                 if (PHAR_G(request_init) && PHAR_G(phar_fname_map.u.flags)) {
      86          44 :                         zend_hash_apply_with_argument(&(PHAR_G(phar_fname_map)), phar_set_writeable_bit, (void *)&ini);
      87             :                 }
      88             :         } else {
      89       21305 :                 PHAR_G(require_hash) = ini;
      90             :         }
      91             : 
      92       42677 :         return SUCCESS;
      93             : }
      94             : /* }}}*/
      95             : 
      96             : /* this global stores the global cached pre-parsed manifests */
      97             : HashTable cached_phars;
      98             : HashTable cached_alias;
      99             : 
     100       21291 : static void phar_split_cache_list(void) /* {{{ */
     101             : {
     102             :         char *tmp;
     103             :         char *key, *lasts, *end;
     104             :         char ds[2];
     105             :         phar_archive_data *phar;
     106       21291 :         uint i = 0;
     107             : 
     108       21291 :         if (!PHAR_G(cache_list) || !(PHAR_G(cache_list)[0])) {
     109       21169 :                 return;
     110             :         }
     111             : 
     112         122 :         ds[0] = DEFAULT_DIR_SEPARATOR;
     113         122 :         ds[1] = '\0';
     114         122 :         tmp = estrdup(PHAR_G(cache_list));
     115             : 
     116             :         /* fake request startup */
     117         122 :         PHAR_G(request_init) = 1;
     118         122 :         zend_hash_init(&EG(regular_list), 0, NULL, NULL, 0);
     119         122 :         EG(regular_list).nNextFreeElement=1;    /* we don't want resource id 0 */
     120             : 
     121         122 :         PHAR_G(has_bz2) = zend_hash_str_exists(&module_registry, "bz2", sizeof("bz2")-1);
     122         122 :         PHAR_G(has_zlib) = zend_hash_str_exists(&module_registry, "zlib", sizeof("zlib")-1);
     123             :         /* these two are dummies and will be destroyed later */
     124         122 :         zend_hash_init(&cached_phars, sizeof(phar_archive_data*), zend_get_hash_value, destroy_phar_data,  1);
     125         122 :         zend_hash_init(&cached_alias, sizeof(phar_archive_data*), zend_get_hash_value, NULL, 1);
     126             :         /* these two are real and will be copied over cached_phars/cached_alias later */
     127         122 :         zend_hash_init(&(PHAR_G(phar_fname_map)), sizeof(phar_archive_data*), zend_get_hash_value, destroy_phar_data,  1);
     128         122 :         zend_hash_init(&(PHAR_G(phar_alias_map)), sizeof(phar_archive_data*), zend_get_hash_value, NULL, 1);
     129         122 :         PHAR_G(manifest_cached) = 1;
     130         122 :         PHAR_G(persist) = 1;
     131             : 
     132         247 :         for (key = php_strtok_r(tmp, ds, &lasts);
     133             :                         key;
     134           3 :                         key = php_strtok_r(NULL, ds, &lasts)) {
     135         122 :                 end = strchr(key, DEFAULT_DIR_SEPARATOR);
     136             : 
     137         122 :                 if (end) {
     138           0 :                         if (SUCCESS == phar_open_from_filename(key, end - key, NULL, 0, 0, &phar, NULL)) {
     139             : finish_up:
     140           3 :                                 phar->phar_pos = i++;
     141           3 :                                 php_stream_close(phar->fp);
     142           3 :                                 phar->fp = NULL;
     143             :                         } else {
     144             : finish_error:
     145         119 :                                 PHAR_G(persist) = 0;
     146         119 :                                 PHAR_G(manifest_cached) = 0;
     147         119 :                                 efree(tmp);
     148         119 :                                 zend_hash_destroy(&(PHAR_G(phar_fname_map)));
     149         119 :                                 PHAR_G(phar_fname_map.u.flags) = 0;
     150         119 :                                 zend_hash_destroy(&(PHAR_G(phar_alias_map)));
     151         119 :                                 PHAR_G(phar_alias_map.u.flags) = 0;
     152         119 :                                 zend_hash_destroy(&cached_phars);
     153         119 :                                 zend_hash_destroy(&cached_alias);
     154         119 :                                 zend_hash_graceful_reverse_destroy(&EG(regular_list));
     155         119 :                                 memset(&EG(regular_list), 0, sizeof(HashTable));
     156             :                                 /* free cached manifests */
     157         119 :                                 PHAR_G(request_init) = 0;
     158         119 :                                 return;
     159             :                         }
     160             :                 } else {
     161         122 :                         if (SUCCESS == phar_open_from_filename(key, strlen(key), NULL, 0, 0, &phar, NULL)) {
     162           3 :                                 goto finish_up;
     163             :                         } else {
     164         119 :                                 goto finish_error;
     165             :                         }
     166             :                 }
     167             :         }
     168             : 
     169           3 :         PHAR_G(persist) = 0;
     170           3 :         PHAR_G(request_init) = 0;
     171             :         /* destroy dummy values from before */
     172           3 :         zend_hash_destroy(&cached_phars);
     173           3 :         zend_hash_destroy(&cached_alias);
     174           3 :         cached_phars = PHAR_G(phar_fname_map);
     175           3 :         cached_alias = PHAR_G(phar_alias_map);
     176           3 :         PHAR_G(phar_fname_map.u.flags) = 0;
     177           3 :         PHAR_G(phar_alias_map.u.flags) = 0;
     178           3 :         zend_hash_graceful_reverse_destroy(&EG(regular_list));
     179           3 :         memset(&EG(regular_list), 0, sizeof(HashTable));
     180           3 :         efree(tmp);
     181             : }
     182             : /* }}} */
     183             : 
     184       21291 : ZEND_INI_MH(phar_ini_cache_list) /* {{{ */
     185             : {
     186       21291 :         PHAR_G(cache_list) = ZSTR_VAL(new_value);
     187             : 
     188       21291 :         if (stage == ZEND_INI_STAGE_STARTUP) {
     189       21291 :                 phar_split_cache_list();
     190             :         }
     191             : 
     192       21291 :         return SUCCESS;
     193             : }
     194             : /* }}} */
     195             : 
     196             : PHP_INI_BEGIN()
     197             :         STD_PHP_INI_BOOLEAN("phar.readonly", "1", PHP_INI_ALL, phar_ini_modify_handler, readonly, zend_phar_globals, phar_globals)
     198             :         STD_PHP_INI_BOOLEAN("phar.require_hash", "1", PHP_INI_ALL, phar_ini_modify_handler, require_hash, zend_phar_globals, phar_globals)
     199             :         STD_PHP_INI_ENTRY("phar.cache_list", "", PHP_INI_SYSTEM, phar_ini_cache_list, cache_list, zend_phar_globals, phar_globals)
     200             : PHP_INI_END()
     201             : 
     202             : /**
     203             :  * When all uses of a phar have been concluded, this frees the manifest
     204             :  * and the phar slot
     205             :  */
     206         628 : void phar_destroy_phar_data(phar_archive_data *phar) /* {{{ */
     207             : {
     208         628 :         if (phar->alias && phar->alias != phar->fname) {
     209         499 :                 pefree(phar->alias, phar->is_persistent);
     210         499 :                 phar->alias = NULL;
     211             :         }
     212             : 
     213         628 :         if (phar->fname) {
     214         557 :                 pefree(phar->fname, phar->is_persistent);
     215         557 :                 phar->fname = NULL;
     216             :         }
     217             : 
     218         628 :         if (phar->signature) {
     219         394 :                 pefree(phar->signature, phar->is_persistent);
     220         394 :                 phar->signature = NULL;
     221             :         }
     222             : 
     223         628 :         if (phar->manifest.u.flags) {
     224         571 :                 zend_hash_destroy(&phar->manifest);
     225         571 :                 phar->manifest.u.flags = 0;
     226             :         }
     227             : 
     228         628 :         if (phar->mounted_dirs.u.flags) {
     229         571 :                 zend_hash_destroy(&phar->mounted_dirs);
     230         571 :                 phar->mounted_dirs.u.flags = 0;
     231             :         }
     232             : 
     233         628 :         if (phar->virtual_dirs.u.flags) {
     234         571 :                 zend_hash_destroy(&phar->virtual_dirs);
     235         571 :                 phar->virtual_dirs.u.flags = 0;
     236             :         }
     237             : 
     238        1256 :         if (Z_TYPE(phar->metadata) != IS_UNDEF) {
     239          21 :                 if (phar->is_persistent) {
     240           3 :                         if (phar->metadata_len) {
     241             :                                 /* for zip comments that are strings */
     242           3 :                                 free(Z_PTR(phar->metadata));
     243             :                         } else {
     244           0 :                                 zval_internal_ptr_dtor(&phar->metadata);
     245             :                         }
     246             :                 } else {
     247          18 :                         zval_ptr_dtor(&phar->metadata);
     248             :                 }
     249          21 :                 phar->metadata_len = 0;
     250          21 :                 ZVAL_UNDEF(&phar->metadata);
     251             :         }
     252             : 
     253         628 :         if (phar->fp) {
     254         113 :                 php_stream_close(phar->fp);
     255         113 :                 phar->fp = 0;
     256             :         }
     257             : 
     258         628 :         if (phar->ufp) {
     259          20 :                 php_stream_close(phar->ufp);
     260          20 :                 phar->ufp = 0;
     261             :         }
     262             : 
     263         628 :         pefree(phar, phar->is_persistent);
     264         628 : }
     265             : /* }}}*/
     266             : 
     267             : /**
     268             :  * Delete refcount and destruct if needed. On destruct return 1 else 0.
     269             :  */
     270        8608 : int phar_archive_delref(phar_archive_data *phar) /* {{{ */
     271             : {
     272        8608 :         if (phar->is_persistent) {
     273           0 :                 return 0;
     274             :         }
     275             : 
     276        8608 :         if (--phar->refcount < 0) {
     277          23 :                 if (PHAR_G(request_done)
     278           9 :                 || zend_hash_str_del(&(PHAR_G(phar_fname_map)), phar->fname, phar->fname_len) != SUCCESS) {
     279           5 :                         phar_destroy_phar_data(phar);
     280             :                 }
     281          14 :                 return 1;
     282        8594 :         } else if (!phar->refcount) {
     283             :                 /* invalidate phar cache */
     284         650 :                 PHAR_G(last_phar) = NULL;
     285         650 :                 PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
     286             : 
     287         650 :                 if (phar->fp && !(phar->flags & PHAR_FILE_COMPRESSION_MASK)) {
     288             :                         /* close open file handle - allows removal or rename of
     289             :                         the file on windows, which has greedy locking
     290             :                         only close if the archive was not already compressed.  If it
     291             :                         was compressed, then the fp does not refer to the original file */
     292         565 :                         php_stream_close(phar->fp);
     293         565 :                         phar->fp = NULL;
     294             :                 }
     295             : 
     296         650 :                 if (!zend_hash_num_elements(&phar->manifest)) {
     297             :                         /* this is a new phar that has perhaps had an alias/metadata set, but has never
     298             :                         been flushed */
     299          33 :                         if (zend_hash_str_del(&(PHAR_G(phar_fname_map)), phar->fname, phar->fname_len) != SUCCESS) {
     300           0 :                                 phar_destroy_phar_data(phar);
     301             :                         }
     302          33 :                         return 1;
     303             :                 }
     304             :         }
     305        8561 :         return 0;
     306             : }
     307             : /* }}}*/
     308             : 
     309             : /**
     310             :  * Destroy phar's in shutdown, here we don't care about aliases
     311             :  */
     312         499 : static void destroy_phar_data_only(zval *zv) /* {{{ */
     313             : {
     314         499 :         phar_archive_data *phar_data = (phar_archive_data *) Z_PTR_P(zv);
     315             : 
     316         499 :         if (EG(exception) || --phar_data->refcount < 0) {
     317         493 :                 phar_destroy_phar_data(phar_data);
     318             :         }
     319         499 : }
     320             : /* }}}*/
     321             : 
     322             : /**
     323             :  * Delete aliases to phar's that got kicked out of the global table
     324             :  */
     325          19 : static int phar_unalias_apply(zval *zv, void *argument) /* {{{ */
     326             : {
     327          19 :         return Z_PTR_P(zv) == argument ? ZEND_HASH_APPLY_REMOVE : ZEND_HASH_APPLY_KEEP;
     328             : }
     329             : /* }}} */
     330             : 
     331             : /**
     332             :  * Delete aliases to phar's that got kicked out of the global table
     333             :  */
     334        5066 : static int phar_tmpclose_apply(zval *zv) /* {{{ */
     335             : {
     336        5066 :         phar_entry_info *entry = (phar_entry_info *) Z_PTR_P(zv);
     337             : 
     338        5066 :         if (entry->fp_type != PHAR_TMP) {
     339        5049 :                 return ZEND_HASH_APPLY_KEEP;
     340             :         }
     341             : 
     342          17 :         if (entry->fp && !entry->fp_refcount) {
     343           7 :                 php_stream_close(entry->fp);
     344           7 :                 entry->fp = NULL;
     345             :         }
     346             : 
     347          17 :         return ZEND_HASH_APPLY_KEEP;
     348             : }
     349             : /* }}} */
     350             : 
     351             : /**
     352             :  * Filename map destructor
     353             :  */
     354         551 : static void destroy_phar_data(zval *zv) /* {{{ */
     355             : {
     356         551 :         phar_archive_data *phar_data = (phar_archive_data *)Z_PTR_P(zv);
     357             : 
     358         551 :         if (PHAR_G(request_ends)) {
     359             :                 /* first, iterate over the manifest and close all PHAR_TMP entry fp handles,
     360             :                 this prevents unnecessary unfreed stream resources */
     361         499 :                 zend_hash_apply(&(phar_data->manifest), phar_tmpclose_apply);
     362         499 :                 destroy_phar_data_only(zv);
     363         499 :                 return;
     364             :         }
     365             : 
     366          52 :         zend_hash_apply_with_argument(&(PHAR_G(phar_alias_map)), phar_unalias_apply, phar_data);
     367             : 
     368          52 :         if (--phar_data->refcount < 0) {
     369          52 :                 phar_destroy_phar_data(phar_data);
     370             :         }
     371             : }
     372             : /* }}}*/
     373             : 
     374             : /**
     375             :  * destructor for the manifest hash, frees each file's entry
     376             :  */
     377        5302 : void destroy_phar_manifest_entry_int(phar_entry_info *entry) /* {{{ */
     378             : {
     379             : 
     380        5302 :         if (entry->cfp) {
     381           0 :                 php_stream_close(entry->cfp);
     382           0 :                 entry->cfp = 0;
     383             :         }
     384             : 
     385        5302 :         if (entry->fp) {
     386          14 :                 php_stream_close(entry->fp);
     387          14 :                 entry->fp = 0;
     388             :         }
     389             : 
     390       10604 :         if (Z_TYPE(entry->metadata) != IS_UNDEF) {
     391          55 :                 if (entry->is_persistent) {
     392           3 :                         if (entry->metadata_len) {
     393             :                                 /* for zip comments that are strings */
     394           3 :                                 free(Z_PTR(entry->metadata));
     395             :                         } else {
     396           0 :                                 zval_internal_ptr_dtor(&entry->metadata);
     397             :                         }
     398             :                 } else {
     399          52 :                         zval_ptr_dtor(&entry->metadata);
     400             :                 }
     401          55 :                 entry->metadata_len = 0;
     402          55 :                 ZVAL_UNDEF(&entry->metadata);
     403             :         }
     404             : 
     405        5302 :         if (entry->metadata_str.s) {
     406          29 :                 smart_str_free(&entry->metadata_str);
     407          29 :                 entry->metadata_str.s = NULL;
     408             :         }
     409             : 
     410        5302 :         pefree(entry->filename, entry->is_persistent);
     411             : 
     412        5302 :         if (entry->link) {
     413          10 :                 pefree(entry->link, entry->is_persistent);
     414          10 :                 entry->link = 0;
     415             :         }
     416             : 
     417        5302 :         if (entry->tmp) {
     418          17 :                 pefree(entry->tmp, entry->is_persistent);
     419          17 :                 entry->tmp = 0;
     420             :         }
     421        5302 : }
     422             : /* }}} */
     423             : 
     424        5300 : void destroy_phar_manifest_entry(zval *zv) /* {{{ */
     425             : {
     426        5300 :         phar_entry_info *entry = Z_PTR_P(zv);
     427        5300 :         destroy_phar_manifest_entry_int(entry);
     428        5300 :         pefree(entry, entry->is_persistent);
     429        5300 : }
     430             : /* }}} */
     431             : 
     432        8163 : int phar_entry_delref(phar_entry_data *idata) /* {{{ */
     433             : {
     434        8163 :         int ret = 0;
     435             : 
     436        8163 :         if (idata->internal_file && !idata->internal_file->is_persistent) {
     437        8163 :                 if (--idata->internal_file->fp_refcount < 0) {
     438           0 :                         idata->internal_file->fp_refcount = 0;
     439             :                 }
     440             : 
     441        8163 :                 if (idata->fp && idata->fp != idata->phar->fp && idata->fp != idata->phar->ufp && idata->fp != idata->internal_file->fp) {
     442          50 :                         php_stream_close(idata->fp);
     443             :                 }
     444             :                 /* if phar_get_or_create_entry_data returns a sub-directory, we have to free it */
     445        8163 :                 if (idata->internal_file->is_temp_dir) {
     446           2 :                         destroy_phar_manifest_entry_int(idata->internal_file);
     447           2 :                         efree(idata->internal_file);
     448             :                 }
     449             :         }
     450             : 
     451        8163 :         phar_archive_delref(idata->phar);
     452        8163 :         efree(idata);
     453        8163 :         return ret;
     454             : }
     455             : /* }}} */
     456             : 
     457             : /**
     458             :  * Removes an entry, either by actually removing it or by marking it.
     459             :  */
     460           9 : void phar_entry_remove(phar_entry_data *idata, char **error) /* {{{ */
     461             : {
     462             :         phar_archive_data *phar;
     463             : 
     464           9 :         phar = idata->phar;
     465             : 
     466           9 :         if (idata->internal_file->fp_refcount < 2) {
     467           9 :                 if (idata->fp && idata->fp != idata->phar->fp && idata->fp != idata->phar->ufp && idata->fp != idata->internal_file->fp) {
     468           0 :                         php_stream_close(idata->fp);
     469             :                 }
     470           9 :                 zend_hash_str_del(&idata->phar->manifest, idata->internal_file->filename, idata->internal_file->filename_len);
     471           9 :                 idata->phar->refcount--;
     472           9 :                 efree(idata);
     473             :         } else {
     474           0 :                 idata->internal_file->is_deleted = 1;
     475           0 :                 phar_entry_delref(idata);
     476             :         }
     477             : 
     478           9 :         if (!phar->donotflush) {
     479           9 :                 phar_flush(phar, 0, 0, 0, error);
     480             :         }
     481           9 : }
     482             : /* }}} */
     483             : 
     484             : #define MAPPHAR_ALLOC_FAIL(msg) \
     485             :         if (fp) {\
     486             :                 php_stream_close(fp);\
     487             :         }\
     488             :         if (error) {\
     489             :                 spprintf(error, 0, msg, fname);\
     490             :         }\
     491             :         return FAILURE;
     492             : 
     493             : #define MAPPHAR_FAIL(msg) \
     494             :         efree(savebuf);\
     495             :         if (mydata) {\
     496             :                 phar_destroy_phar_data(mydata);\
     497             :         }\
     498             :         if (signature) {\
     499             :                 pefree(signature, PHAR_G(persist));\
     500             :         }\
     501             :         MAPPHAR_ALLOC_FAIL(msg)
     502             : 
     503             : #ifdef WORDS_BIGENDIAN
     504             : # define PHAR_GET_32(buffer, var) \
     505             :         var = ((((unsigned char*)(buffer))[3]) << 24) \
     506             :                 | ((((unsigned char*)(buffer))[2]) << 16) \
     507             :                 | ((((unsigned char*)(buffer))[1]) <<  8) \
     508             :                 | (((unsigned char*)(buffer))[0]); \
     509             :         (buffer) += 4
     510             : # define PHAR_GET_16(buffer, var) \
     511             :         var = ((((unsigned char*)(buffer))[1]) <<  8) \
     512             :                 | (((unsigned char*)(buffer))[0]); \
     513             :         (buffer) += 2
     514             : #else
     515             : # define PHAR_GET_32(buffer, var) \
     516             :         memcpy(&var, buffer, sizeof(var)); \
     517             :         buffer += 4
     518             : # define PHAR_GET_16(buffer, var) \
     519             :         var = *(php_uint16*)(buffer); \
     520             :         buffer += 2
     521             : #endif
     522             : #define PHAR_ZIP_16(var) ((php_uint16)((((php_uint16)var[0]) & 0xff) | \
     523             :         (((php_uint16)var[1]) & 0xff) << 8))
     524             : #define PHAR_ZIP_32(var) ((php_uint32)((((php_uint32)var[0]) & 0xff) | \
     525             :         (((php_uint32)var[1]) & 0xff) << 8 | \
     526             :         (((php_uint32)var[2]) & 0xff) << 16 | \
     527             :         (((php_uint32)var[3]) & 0xff) << 24))
     528             : 
     529             : /**
     530             :  * Open an already loaded phar
     531             :  */
     532        2861 : int phar_open_parsed_phar(char *fname, int fname_len, char *alias, int alias_len, int is_data, int options, phar_archive_data** pphar, char **error) /* {{{ */
     533             : {
     534             :         phar_archive_data *phar;
     535             : #ifdef PHP_WIN32
     536             :         char *unixfname;
     537             : #endif
     538             : 
     539        2861 :         if (error) {
     540        2425 :                 *error = NULL;
     541             :         }
     542             : #ifdef PHP_WIN32
     543             :         unixfname = estrndup(fname, fname_len);
     544             :         phar_unixify_path_separators(unixfname, fname_len);
     545             : 
     546             :         if (SUCCESS == phar_get_archive(&phar, unixfname, fname_len, alias, alias_len, error)
     547             :                 && ((alias && fname_len == phar->fname_len
     548             :                 && !strncmp(unixfname, phar->fname, fname_len)) || !alias)
     549             :         ) {
     550             :                 phar_entry_info *stub;
     551             :                 efree(unixfname);
     552             : #else
     553        2947 :         if (SUCCESS == phar_get_archive(&phar, fname, fname_len, alias, alias_len, error)
     554        2904 :                 && ((alias && fname_len == phar->fname_len
     555          86 :                 && !strncmp(fname, phar->fname, fname_len)) || !alias)
     556             :         ) {
     557             :                 phar_entry_info *stub;
     558             : #endif
     559             :                 /* logic above is as follows:
     560             :                    If an explicit alias was requested, ensure the filename passed in
     561             :                    matches the phar's filename.
     562             :                    If no alias was passed in, then it can match either and be valid
     563             :                  */
     564             : 
     565        2093 :                 if (!is_data) {
     566             :                         /* prevent any ".phar" without a stub getting through */
     567        1713 :                         if (!phar->halt_offset && !phar->is_brandnew && (phar->is_tar || phar->is_zip)) {
     568         734 :                                 if (PHAR_G(readonly) && NULL == (stub = zend_hash_str_find_ptr(&(phar->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1))) {
     569           2 :                                         if (error) {
     570           2 :                                                 spprintf(error, 0, "'%s' is not a phar archive. Use PharData::__construct() for a standard zip or tar archive", fname);
     571             :                                         }
     572           2 :                                         return FAILURE;
     573             :                                 }
     574             :                         }
     575             :                 }
     576             : 
     577        2091 :                 if (pphar) {
     578         673 :                         *pphar = phar;
     579             :                 }
     580             : 
     581        2091 :                 return SUCCESS;
     582             :         } else {
     583             : #ifdef PHP_WIN32
     584             :                 efree(unixfname);
     585             : #endif
     586         768 :                 if (pphar) {
     587         641 :                         *pphar = NULL;
     588             :                 }
     589             : 
     590         768 :                 if (phar && error && !(options & REPORT_ERRORS)) {
     591           0 :                         efree(error);
     592             :                 }
     593             : 
     594         768 :                 return FAILURE;
     595             :         }
     596             : }
     597             : /* }}}*/
     598             : 
     599             : /**
     600             :  * Parse out metadata from the manifest for a single file
     601             :  *
     602             :  * Meta-data is in this format:
     603             :  * [len32][data...]
     604             :  *
     605             :  * data is the serialized zval
     606             :  */
     607        1128 : int phar_parse_metadata(char **buffer, zval *metadata, php_uint32 zip_metadata_len) /* {{{ */
     608             : {
     609             :         php_unserialize_data_t var_hash;
     610             : 
     611        1128 :         if (zip_metadata_len) {
     612             :                 const unsigned char *p;
     613         110 :                 unsigned char *p_buff = (unsigned char *)estrndup(*buffer, zip_metadata_len);
     614         110 :                 p = p_buff;
     615         110 :                 ZVAL_NULL(metadata);
     616         110 :                 PHP_VAR_UNSERIALIZE_INIT(var_hash);
     617             : 
     618         110 :                 if (!php_var_unserialize(metadata, &p, p + zip_metadata_len, &var_hash)) {
     619          63 :                         efree(p_buff);
     620          63 :                         PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
     621          63 :                         zval_ptr_dtor(metadata);
     622          63 :                         ZVAL_UNDEF(metadata);
     623          63 :                         return FAILURE;
     624             :                 }
     625          47 :                 efree(p_buff);
     626          47 :                 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
     627             : 
     628          47 :                 if (PHAR_G(persist)) {
     629             :                         /* lazy init metadata */
     630           6 :                         zval_ptr_dtor(metadata);
     631          12 :                         Z_PTR_P(metadata) = pemalloc(zip_metadata_len, 1);
     632           6 :                         memcpy(Z_PTR_P(metadata), *buffer, zip_metadata_len);
     633           6 :                         return SUCCESS;
     634             :                 }
     635             :         } else {
     636        1018 :                 ZVAL_UNDEF(metadata);
     637             :         }
     638             : 
     639        1059 :         return SUCCESS;
     640             : }
     641             : /* }}}*/
     642             : 
     643             : /**
     644             :  * Does not check for a previously opened phar in the cache.
     645             :  *
     646             :  * Parse a new one and add it to the cache, returning either SUCCESS or
     647             :  * FAILURE, and setting pphar to the pointer to the manifest entry
     648             :  *
     649             :  * This is used by phar_open_from_filename to process the manifest, but can be called
     650             :  * directly.
     651             :  */
     652         287 : static int phar_parse_pharfile(php_stream *fp, char *fname, int fname_len, char *alias, int alias_len, zend_long halt_offset, phar_archive_data** pphar, php_uint32 compression, char **error) /* {{{ */
     653             : {
     654             :         char b32[4], *buffer, *endbuffer, *savebuf;
     655         287 :         phar_archive_data *mydata = NULL;
     656             :         phar_entry_info entry;
     657             :         php_uint32 manifest_len, manifest_count, manifest_flags, manifest_index, tmp_len, sig_flags;
     658             :         php_uint16 manifest_ver;
     659             :         php_uint32 len;
     660             :         zend_long offset;
     661         287 :         int sig_len, register_alias = 0, temp_alias = 0;
     662         287 :         char *signature = NULL;
     663             : 
     664         287 :         if (pphar) {
     665         175 :                 *pphar = NULL;
     666             :         }
     667             : 
     668         287 :         if (error) {
     669         183 :                 *error = NULL;
     670             :         }
     671             : 
     672             :         /* check for ?>\n and increment accordingly */
     673         287 :         if (-1 == php_stream_seek(fp, halt_offset, SEEK_SET)) {
     674           0 :                 MAPPHAR_ALLOC_FAIL("cannot seek to __HALT_COMPILER(); location in phar \"%s\"")
     675             :         }
     676             : 
     677         287 :         buffer = b32;
     678             : 
     679         287 :         if (3 != php_stream_read(fp, buffer, 3)) {
     680           3 :                 MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at stub end)")
     681             :         }
     682             : 
     683         284 :         if ((*buffer == ' ' || *buffer == '\n') && *(buffer + 1) == '?' && *(buffer + 2) == '>') {
     684             :                 int nextchar;
     685         280 :                 halt_offset += 3;
     686         280 :                 if (EOF == (nextchar = php_stream_getc(fp))) {
     687           0 :                         MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at stub end)")
     688             :                 }
     689             : 
     690         280 :                 if ((char) nextchar == '\r') {
     691             :                         /* if we have an \r we require an \n as well */
     692         157 :                         if (EOF == (nextchar = php_stream_getc(fp)) || (char)nextchar != '\n') {
     693           0 :                                 MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at stub end)")
     694             :                         }
     695         157 :                         ++halt_offset;
     696             :                 }
     697             : 
     698         280 :                 if ((char) nextchar == '\n') {
     699         160 :                         ++halt_offset;
     700             :                 }
     701             :         }
     702             : 
     703             :         /* make sure we are at the right location to read the manifest */
     704         284 :         if (-1 == php_stream_seek(fp, halt_offset, SEEK_SET)) {
     705           0 :                 MAPPHAR_ALLOC_FAIL("cannot seek to __HALT_COMPILER(); location in phar \"%s\"")
     706             :         }
     707             : 
     708             :         /* read in manifest */
     709         284 :         buffer = b32;
     710             : 
     711         284 :         if (4 != php_stream_read(fp, buffer, 4)) {
     712           4 :                 MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated manifest at manifest length)")
     713             :         }
     714             : 
     715         280 :         PHAR_GET_32(buffer, manifest_len);
     716             : 
     717         280 :         if (manifest_len > 1048576 * 100) {
     718             :                 /* prevent serious memory issues by limiting manifest to at most 100 MB in length */
     719           1 :                 MAPPHAR_ALLOC_FAIL("manifest cannot be larger than 100 MB in phar \"%s\"")
     720             :         }
     721             : 
     722         279 :         buffer = (char *)emalloc(manifest_len);
     723         279 :         savebuf = buffer;
     724         279 :         endbuffer = buffer + manifest_len;
     725             : 
     726         279 :         if (manifest_len < 10 || manifest_len != php_stream_read(fp, buffer, manifest_len)) {
     727           2 :                 MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest header)")
     728             :         }
     729             : 
     730             :         /* extract the number of entries */
     731         277 :         PHAR_GET_32(buffer, manifest_count);
     732             : 
     733         277 :         if (manifest_count == 0) {
     734           0 :                 MAPPHAR_FAIL("in phar \"%s\", manifest claims to have zero entries.  Phars must have at least 1 entry");
     735             :         }
     736             : 
     737             :         /* extract API version, lowest nibble currently unused */
     738         554 :         manifest_ver = (((unsigned char)buffer[0]) << 8)
     739         277 :                                  + ((unsigned char)buffer[1]);
     740         277 :         buffer += 2;
     741             : 
     742         277 :         if ((manifest_ver & PHAR_API_VER_MASK) < PHAR_API_MIN_READ) {
     743           0 :                 efree(savebuf);
     744           0 :                 php_stream_close(fp);
     745           0 :                 if (error) {
     746           0 :                         spprintf(error, 0, "phar \"%s\" is API version %1.u.%1.u.%1.u, and cannot be processed", fname, manifest_ver >> 12, (manifest_ver >> 8) & 0xF, (manifest_ver >> 4) & 0x0F);
     747             :                 }
     748           0 :                 return FAILURE;
     749             :         }
     750             : 
     751         277 :         PHAR_GET_32(buffer, manifest_flags);
     752             : 
     753         277 :         manifest_flags &= ~PHAR_HDR_COMPRESSION_MASK;
     754         277 :         manifest_flags &= ~PHAR_FILE_COMPRESSION_MASK;
     755             :         /* remember whether this entire phar was compressed with gz/bzip2 */
     756         277 :         manifest_flags |= compression;
     757             : 
     758             :         /* The lowest nibble contains the phar wide flags. The compression flags can */
     759             :         /* be ignored on reading because it is being generated anyways. */
     760         277 :         if (manifest_flags & PHAR_HDR_SIGNATURE) {
     761         181 :                 char sig_buf[8], *sig_ptr = sig_buf;
     762             :                 zend_off_t read_len;
     763             :                 size_t end_of_phar;
     764             : 
     765         724 :                 if (-1 == php_stream_seek(fp, -8, SEEK_END)
     766         362 :                 || (read_len = php_stream_tell(fp)) < 20
     767             :                 || 8 != php_stream_read(fp, sig_buf, 8)
     768         362 :                 || memcmp(sig_buf+4, "GBMB", 4)) {
     769           0 :                         efree(savebuf);
     770           0 :                         php_stream_close(fp);
     771           0 :                         if (error) {
     772           0 :                                 spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
     773             :                         }
     774           0 :                         return FAILURE;
     775             :                 }
     776             : 
     777         181 :                 PHAR_GET_32(sig_ptr, sig_flags);
     778             : 
     779         181 :                 switch(sig_flags) {
     780             :                         case PHAR_SIG_OPENSSL: {
     781             :                                 php_uint32 signature_len;
     782             :                                 char *sig;
     783             :                                 zend_off_t whence;
     784             : 
     785             :                                 /* we store the signature followed by the signature length */
     786           2 :                                 if (-1 == php_stream_seek(fp, -12, SEEK_CUR)
     787           2 :                                 || 4 != php_stream_read(fp, sig_buf, 4)) {
     788           0 :                                         efree(savebuf);
     789           0 :                                         php_stream_close(fp);
     790           0 :                                         if (error) {
     791           0 :                                                 spprintf(error, 0, "phar \"%s\" openssl signature length could not be read", fname);
     792             :                                         }
     793           0 :                                         return FAILURE;
     794             :                                 }
     795             : 
     796           1 :                                 sig_ptr = sig_buf;
     797           1 :                                 PHAR_GET_32(sig_ptr, signature_len);
     798           1 :                                 sig = (char *) emalloc(signature_len);
     799           1 :                                 whence = signature_len + 4;
     800           1 :                                 whence = -whence;
     801             : 
     802           3 :                                 if (-1 == php_stream_seek(fp, whence, SEEK_CUR)
     803           2 :                                 || !(end_of_phar = php_stream_tell(fp))
     804           1 :                                 || signature_len != php_stream_read(fp, sig, signature_len)) {
     805           0 :                                         efree(savebuf);
     806           0 :                                         efree(sig);
     807           0 :                                         php_stream_close(fp);
     808           0 :                                         if (error) {
     809           0 :                                                 spprintf(error, 0, "phar \"%s\" openssl signature could not be read", fname);
     810             :                                         }
     811           0 :                                         return FAILURE;
     812             :                                 }
     813             : 
     814           1 :                                 if (FAILURE == phar_verify_signature(fp, end_of_phar, PHAR_SIG_OPENSSL, sig, signature_len, fname, &signature, &sig_len, error)) {
     815           0 :                                         efree(savebuf);
     816           0 :                                         efree(sig);
     817           0 :                                         php_stream_close(fp);
     818           0 :                                         if (error) {
     819           0 :                                                 char *save = *error;
     820           0 :                                                 spprintf(error, 0, "phar \"%s\" openssl signature could not be verified: %s", fname, *error);
     821           0 :                                                 efree(save);
     822             :                                         }
     823           0 :                                         return FAILURE;
     824             :                                 }
     825           1 :                                 efree(sig);
     826             :                         }
     827           1 :                         break;
     828             : #if PHAR_HASH_OK
     829             :                         case PHAR_SIG_SHA512: {
     830             :                                 unsigned char digest[64];
     831             : 
     832           1 :                                 php_stream_seek(fp, -(8 + 64), SEEK_END);
     833           1 :                                 read_len = php_stream_tell(fp);
     834             : 
     835           1 :                                 if (php_stream_read(fp, (char*)digest, sizeof(digest)) != sizeof(digest)) {
     836           0 :                                         efree(savebuf);
     837           0 :                                         php_stream_close(fp);
     838           0 :                                         if (error) {
     839           0 :                                                 spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
     840             :                                         }
     841           0 :                                         return FAILURE;
     842             :                                 }
     843             : 
     844           1 :                                 if (FAILURE == phar_verify_signature(fp, read_len, PHAR_SIG_SHA512, (char *)digest, 64, fname, &signature, &sig_len, error)) {
     845           0 :                                         efree(savebuf);
     846           0 :                                         php_stream_close(fp);
     847           0 :                                         if (error) {
     848           0 :                                                 char *save = *error;
     849           0 :                                                 spprintf(error, 0, "phar \"%s\" SHA512 signature could not be verified: %s", fname, *error);
     850           0 :                                                 efree(save);
     851             :                                         }
     852           0 :                                         return FAILURE;
     853             :                                 }
     854           1 :                                 break;
     855             :                         }
     856             :                         case PHAR_SIG_SHA256: {
     857             :                                 unsigned char digest[32];
     858             : 
     859           1 :                                 php_stream_seek(fp, -(8 + 32), SEEK_END);
     860           1 :                                 read_len = php_stream_tell(fp);
     861             : 
     862           1 :                                 if (php_stream_read(fp, (char*)digest, sizeof(digest)) != sizeof(digest)) {
     863           0 :                                         efree(savebuf);
     864           0 :                                         php_stream_close(fp);
     865           0 :                                         if (error) {
     866           0 :                                                 spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
     867             :                                         }
     868           0 :                                         return FAILURE;
     869             :                                 }
     870             : 
     871           1 :                                 if (FAILURE == phar_verify_signature(fp, read_len, PHAR_SIG_SHA256, (char *)digest, 32, fname, &signature, &sig_len, error)) {
     872           0 :                                         efree(savebuf);
     873           0 :                                         php_stream_close(fp);
     874           0 :                                         if (error) {
     875           0 :                                                 char *save = *error;
     876           0 :                                                 spprintf(error, 0, "phar \"%s\" SHA256 signature could not be verified: %s", fname, *error);
     877           0 :                                                 efree(save);
     878             :                                         }
     879           0 :                                         return FAILURE;
     880             :                                 }
     881           1 :                                 break;
     882             :                         }
     883             : #else
     884             :                         case PHAR_SIG_SHA512:
     885             :                         case PHAR_SIG_SHA256:
     886             :                                 efree(savebuf);
     887             :                                 php_stream_close(fp);
     888             : 
     889             :                                 if (error) {
     890             :                                         spprintf(error, 0, "phar \"%s\" has a unsupported signature", fname);
     891             :                                 }
     892             :                                 return FAILURE;
     893             : #endif
     894             :                         case PHAR_SIG_SHA1: {
     895             :                                 unsigned char digest[20];
     896             : 
     897         177 :                                 php_stream_seek(fp, -(8 + 20), SEEK_END);
     898         177 :                                 read_len = php_stream_tell(fp);
     899             : 
     900         177 :                                 if (php_stream_read(fp, (char*)digest, sizeof(digest)) != sizeof(digest)) {
     901           0 :                                         efree(savebuf);
     902           0 :                                         php_stream_close(fp);
     903           0 :                                         if (error) {
     904           0 :                                                 spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
     905             :                                         }
     906           0 :                                         return FAILURE;
     907             :                                 }
     908             : 
     909         177 :                                 if (FAILURE == phar_verify_signature(fp, read_len, PHAR_SIG_SHA1, (char *)digest, 20, fname, &signature, &sig_len, error)) {
     910           0 :                                         efree(savebuf);
     911           0 :                                         php_stream_close(fp);
     912           0 :                                         if (error) {
     913           0 :                                                 char *save = *error;
     914           0 :                                                 spprintf(error, 0, "phar \"%s\" SHA1 signature could not be verified: %s", fname, *error);
     915           0 :                                                 efree(save);
     916             :                                         }
     917           0 :                                         return FAILURE;
     918             :                                 }
     919         177 :                                 break;
     920             :                         }
     921             :                         case PHAR_SIG_MD5: {
     922             :                                 unsigned char digest[16];
     923             : 
     924           1 :                                 php_stream_seek(fp, -(8 + 16), SEEK_END);
     925           1 :                                 read_len = php_stream_tell(fp);
     926             : 
     927           1 :                                 if (php_stream_read(fp, (char*)digest, sizeof(digest)) != sizeof(digest)) {
     928           0 :                                         efree(savebuf);
     929           0 :                                         php_stream_close(fp);
     930           0 :                                         if (error) {
     931           0 :                                                 spprintf(error, 0, "phar \"%s\" has a broken signature", fname);
     932             :                                         }
     933           0 :                                         return FAILURE;
     934             :                                 }
     935             : 
     936           1 :                                 if (FAILURE == phar_verify_signature(fp, read_len, PHAR_SIG_MD5, (char *)digest, 16, fname, &signature, &sig_len, error)) {
     937           0 :                                         efree(savebuf);
     938           0 :                                         php_stream_close(fp);
     939           0 :                                         if (error) {
     940           0 :                                                 char *save = *error;
     941           0 :                                                 spprintf(error, 0, "phar \"%s\" MD5 signature could not be verified: %s", fname, *error);
     942           0 :                                                 efree(save);
     943             :                                         }
     944           0 :                                         return FAILURE;
     945             :                                 }
     946           1 :                                 break;
     947             :                         }
     948             :                         default:
     949           0 :                                 efree(savebuf);
     950           0 :                                 php_stream_close(fp);
     951             : 
     952           0 :                                 if (error) {
     953           0 :                                         spprintf(error, 0, "phar \"%s\" has a broken or unsupported signature", fname);
     954             :                                 }
     955           0 :                                 return FAILURE;
     956             :                 }
     957          96 :         } else if (PHAR_G(require_hash)) {
     958           1 :                 efree(savebuf);
     959           1 :                 php_stream_close(fp);
     960             : 
     961           1 :                 if (error) {
     962           1 :                         spprintf(error, 0, "phar \"%s\" does not have a signature", fname);
     963             :                 }
     964           1 :                 return FAILURE;
     965             :         } else {
     966          95 :                 sig_flags = 0;
     967          95 :                 sig_len = 0;
     968             :         }
     969             : 
     970             :         /* extract alias */
     971         276 :         PHAR_GET_32(buffer, tmp_len);
     972             : 
     973         276 :         if (buffer + tmp_len > endbuffer) {
     974           0 :                 MAPPHAR_FAIL("internal corruption of phar \"%s\" (buffer overrun)");
     975             :         }
     976             : 
     977         276 :         if (manifest_len < 10 + tmp_len) {
     978           0 :                 MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest header)")
     979             :         }
     980             : 
     981             :         /* tmp_len = 0 says alias length is 0, which means the alias is not stored in the phar */
     982         276 :         if (tmp_len) {
     983             :                 /* if the alias is stored we enforce it (implicit overrides explicit) */
     984         102 :                 if (alias && alias_len && (alias_len != (int)tmp_len || strncmp(alias, buffer, tmp_len)))
     985             :                 {
     986           0 :                         buffer[tmp_len] = '\0';
     987           0 :                         php_stream_close(fp);
     988             : 
     989           0 :                         if (signature) {
     990           0 :                                 efree(signature);
     991             :                         }
     992             : 
     993           0 :                         if (error) {
     994           0 :                                 spprintf(error, 0, "cannot load phar \"%s\" with implicit alias \"%s\" under different alias \"%s\"", fname, buffer, alias);
     995             :                         }
     996             : 
     997           0 :                         efree(savebuf);
     998           0 :                         return FAILURE;
     999             :                 }
    1000             : 
    1001         102 :                 alias_len = tmp_len;
    1002         102 :                 alias = buffer;
    1003         102 :                 buffer += tmp_len;
    1004         102 :                 register_alias = 1;
    1005         299 :         } else if (!alias_len || !alias) {
    1006             :                 /* if we neither have an explicit nor an implicit alias, we use the filename */
    1007         125 :                 alias = NULL;
    1008         125 :                 alias_len = 0;
    1009         125 :                 register_alias = 0;
    1010          49 :         } else if (alias_len) {
    1011          49 :                 register_alias = 1;
    1012          49 :                 temp_alias = 1;
    1013             :         }
    1014             : 
    1015             :         /* we have 5 32-bit items plus 1 byte at least */
    1016         276 :         if (manifest_count > ((manifest_len - 10 - tmp_len) / (5 * 4 + 1))) {
    1017             :                 /* prevent serious memory issues */
    1018           4 :                 MAPPHAR_FAIL("internal corruption of phar \"%s\" (too many manifest entries for size of manifest)")
    1019             :         }
    1020             : 
    1021         332 :         mydata = pecalloc(1, sizeof(phar_archive_data), PHAR_G(persist));
    1022         272 :         mydata->is_persistent = PHAR_G(persist);
    1023             : 
    1024             :         /* check whether we have meta data, zero check works regardless of byte order */
    1025         272 :         PHAR_GET_32(buffer, len);
    1026         272 :         if (mydata->is_persistent) {
    1027          60 :                 mydata->metadata_len = len;
    1028          60 :                 if(!len) {
    1029             :                         /* FIXME: not sure why this is needed but removing it breaks tests */
    1030          57 :                         PHAR_GET_32(buffer, len);
    1031             :                 }
    1032             :         }
    1033         272 :         if(len > endbuffer - buffer) {
    1034           0 :                 MAPPHAR_FAIL("internal corruption of phar \"%s\" (trying to read past buffer end)");
    1035             :         }
    1036         272 :         if (phar_parse_metadata(&buffer, &mydata->metadata, len) == FAILURE) {
    1037          57 :                 MAPPHAR_FAIL("unable to read phar metadata in .phar file \"%s\"");
    1038             :         }
    1039         215 :         buffer += len;
    1040             : 
    1041             :         /* set up our manifest */
    1042         215 :         zend_hash_init(&mydata->manifest, manifest_count,
    1043             :                 zend_get_hash_value, destroy_phar_manifest_entry, (zend_bool)mydata->is_persistent);
    1044         215 :         zend_hash_init(&mydata->mounted_dirs, 5,
    1045             :                 zend_get_hash_value, NULL, (zend_bool)mydata->is_persistent);
    1046         215 :         zend_hash_init(&mydata->virtual_dirs, manifest_count * 2,
    1047             :                 zend_get_hash_value, NULL, (zend_bool)mydata->is_persistent);
    1048         215 :         mydata->fname = pestrndup(fname, fname_len, mydata->is_persistent);
    1049             : #ifdef PHP_WIN32
    1050             :         phar_unixify_path_separators(mydata->fname, fname_len);
    1051             : #endif
    1052         215 :         mydata->fname_len = fname_len;
    1053         215 :         offset = halt_offset + manifest_len + 4;
    1054         215 :         memset(&entry, 0, sizeof(phar_entry_info));
    1055         215 :         entry.phar = mydata;
    1056         215 :         entry.fp_type = PHAR_FP;
    1057         215 :         entry.is_persistent = mydata->is_persistent;
    1058             : 
    1059        1042 :         for (manifest_index = 0; manifest_index < manifest_count; ++manifest_index) {
    1060         830 :                 if (buffer + 4 > endbuffer) {
    1061           0 :                         MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest entry)")
    1062             :                 }
    1063             : 
    1064         830 :                 PHAR_GET_32(buffer, entry.filename_len);
    1065             : 
    1066         830 :                 if (entry.filename_len == 0) {
    1067           0 :                         MAPPHAR_FAIL("zero-length filename encountered in phar \"%s\"");
    1068             :                 }
    1069             : 
    1070         830 :                 if (entry.is_persistent) {
    1071           4 :                         entry.manifest_pos = manifest_index;
    1072             :                 }
    1073             : 
    1074         830 :                 if (entry.filename_len + 20 > endbuffer - buffer) {
    1075           0 :                         MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest entry)");
    1076             :                 }
    1077             : 
    1078         837 :                 if ((manifest_ver & PHAR_API_VER_MASK) >= PHAR_API_MIN_DIR && buffer[entry.filename_len - 1] == '/') {
    1079           7 :                         entry.is_dir = 1;
    1080             :                 } else {
    1081         823 :                         entry.is_dir = 0;
    1082             :                 }
    1083             : 
    1084         830 :                 phar_add_virtual_dirs(mydata, buffer, entry.filename_len);
    1085         830 :                 entry.filename = pestrndup(buffer, entry.filename_len, entry.is_persistent);
    1086         830 :                 buffer += entry.filename_len;
    1087         830 :                 PHAR_GET_32(buffer, entry.uncompressed_filesize);
    1088         830 :                 PHAR_GET_32(buffer, entry.timestamp);
    1089             : 
    1090         830 :                 if (offset == halt_offset + (int)manifest_len + 4) {
    1091         328 :                         mydata->min_timestamp = entry.timestamp;
    1092         328 :                         mydata->max_timestamp = entry.timestamp;
    1093             :                 } else {
    1094         502 :                         if (mydata->min_timestamp > entry.timestamp) {
    1095           0 :                                 mydata->min_timestamp = entry.timestamp;
    1096         502 :                         } else if (mydata->max_timestamp < entry.timestamp) {
    1097           0 :                                 mydata->max_timestamp = entry.timestamp;
    1098             :                         }
    1099             :                 }
    1100             : 
    1101         830 :                 PHAR_GET_32(buffer, entry.compressed_filesize);
    1102         830 :                 PHAR_GET_32(buffer, entry.crc32);
    1103         830 :                 PHAR_GET_32(buffer, entry.flags);
    1104             : 
    1105         830 :                 if (entry.is_dir) {
    1106           7 :                         entry.filename_len--;
    1107           7 :                         entry.flags |= PHAR_ENT_PERM_DEF_DIR;
    1108             :                 }
    1109             : 
    1110         830 :                 PHAR_GET_32(buffer, len);
    1111         830 :                 if (entry.is_persistent) {
    1112           4 :                         entry.metadata_len = len;
    1113             :                 } else {
    1114         826 :                         entry.metadata_len = 0;
    1115             :                 }
    1116         830 :                 if (len > endbuffer - buffer) {
    1117           1 :                         pefree(entry.filename, entry.is_persistent);
    1118           1 :                         MAPPHAR_FAIL("internal corruption of phar \"%s\" (truncated manifest entry)");
    1119             :                 }
    1120         829 :                 if (phar_parse_metadata(&buffer, &entry.metadata, len) == FAILURE) {
    1121           0 :                         pefree(entry.filename, entry.is_persistent);
    1122           0 :                         MAPPHAR_FAIL("unable to read file metadata in .phar file \"%s\"");
    1123             :                 }
    1124         829 :                 buffer += len;
    1125             : 
    1126         829 :                 entry.offset = entry.offset_abs = offset;
    1127         829 :                 offset += entry.compressed_filesize;
    1128             : 
    1129         829 :                 switch (entry.flags & PHAR_ENT_COMPRESSION_MASK) {
    1130             :                         case PHAR_ENT_COMPRESSED_GZ:
    1131           9 :                                 if (!PHAR_G(has_zlib)) {
    1132           0 :                                         if (Z_TYPE(entry.metadata) != IS_UNDEF) {
    1133           0 :                                                 if (entry.is_persistent) {
    1134           0 :                                                         free(Z_PTR(entry.metadata));
    1135             :                                                 } else {
    1136           0 :                                                         zval_ptr_dtor(&entry.metadata);
    1137             :                                                 }
    1138             :                                         }
    1139           0 :                                         pefree(entry.filename, entry.is_persistent);
    1140           0 :                                         MAPPHAR_FAIL("zlib extension is required for gz compressed .phar file \"%s\"");
    1141             :                                 }
    1142           9 :                                 break;
    1143             :                         case PHAR_ENT_COMPRESSED_BZ2:
    1144           1 :                                 if (!PHAR_G(has_bz2)) {
    1145           0 :                                         if (Z_TYPE(entry.metadata) != IS_UNDEF) {
    1146           0 :                                                 if (entry.is_persistent) {
    1147           0 :                                                         free(Z_PTR(entry.metadata));
    1148             :                                                 } else {
    1149           0 :                                                         zval_ptr_dtor(&entry.metadata);
    1150             :                                                 }
    1151             :                                         }
    1152           0 :                                         pefree(entry.filename, entry.is_persistent);
    1153           0 :                                         MAPPHAR_FAIL("bz2 extension is required for bzip2 compressed .phar file \"%s\"");
    1154             :                                 }
    1155           1 :                                 break;
    1156             :                         default:
    1157         819 :                                 if (entry.uncompressed_filesize != entry.compressed_filesize) {
    1158           2 :                                         if (Z_TYPE(entry.metadata) != IS_UNDEF) {
    1159           0 :                                                 if (entry.is_persistent) {
    1160           0 :                                                         free(Z_PTR(entry.metadata));
    1161             :                                                 } else {
    1162           0 :                                                         zval_ptr_dtor(&entry.metadata);
    1163             :                                                 }
    1164             :                                         }
    1165           2 :                                         pefree(entry.filename, entry.is_persistent);
    1166           2 :                                         MAPPHAR_FAIL("internal corruption of phar \"%s\" (compressed and uncompressed size does not match for uncompressed entry)");
    1167             :                                 }
    1168             :                                 break;
    1169             :                 }
    1170             : 
    1171         827 :                 manifest_flags |= (entry.flags & PHAR_ENT_COMPRESSION_MASK);
    1172             :                 /* if signature matched, no need to check CRC32 for each file */
    1173         827 :                 entry.is_crc_checked = (manifest_flags & PHAR_HDR_SIGNATURE ? 1 : 0);
    1174         827 :                 phar_set_inode(&entry);
    1175         827 :                 zend_hash_str_add_mem(&mydata->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info));
    1176             :         }
    1177             : 
    1178         212 :         snprintf(mydata->version, sizeof(mydata->version), "%u.%u.%u", manifest_ver >> 12, (manifest_ver >> 8) & 0xF, (manifest_ver >> 4) & 0xF);
    1179         212 :         mydata->internal_file_start = halt_offset + manifest_len + 4;
    1180         212 :         mydata->halt_offset = halt_offset;
    1181         212 :         mydata->flags = manifest_flags;
    1182         212 :         endbuffer = strrchr(mydata->fname, '/');
    1183             : 
    1184         212 :         if (endbuffer) {
    1185         212 :                 mydata->ext = memchr(endbuffer, '.', (mydata->fname + fname_len) - endbuffer);
    1186         212 :                 if (mydata->ext == endbuffer) {
    1187           0 :                         mydata->ext = memchr(endbuffer + 1, '.', (mydata->fname + fname_len) - endbuffer - 1);
    1188             :                 }
    1189         212 :                 if (mydata->ext) {
    1190         212 :                         mydata->ext_len = (mydata->fname + mydata->fname_len) - mydata->ext;
    1191             :                 }
    1192             :         }
    1193             : 
    1194         424 :         mydata->alias = alias ?
    1195         145 :                 pestrndup(alias, alias_len, mydata->is_persistent) :
    1196          67 :                 pestrndup(mydata->fname, fname_len, mydata->is_persistent);
    1197         212 :         mydata->alias_len = alias ? alias_len : fname_len;
    1198         212 :         mydata->sig_flags = sig_flags;
    1199         212 :         mydata->fp = fp;
    1200         212 :         mydata->sig_len = sig_len;
    1201         212 :         mydata->signature = signature;
    1202         212 :         phar_request_initialize();
    1203             : 
    1204         212 :         if (register_alias) {
    1205             :                 phar_archive_data *fd_ptr;
    1206             : 
    1207         145 :                 mydata->is_temporary_alias = temp_alias;
    1208             : 
    1209         145 :                 if (!phar_validate_alias(mydata->alias, mydata->alias_len)) {
    1210           0 :                         signature = NULL;
    1211           0 :                         fp = NULL;
    1212           0 :                         MAPPHAR_FAIL("Cannot open archive \"%s\", invalid alias");
    1213             :                 }
    1214             : 
    1215         290 :                 if (NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len))) {
    1216           5 :                         if (SUCCESS != phar_free_alias(fd_ptr, alias, alias_len)) {
    1217           3 :                                 signature = NULL;
    1218           3 :                                 fp = NULL;
    1219           3 :                                 MAPPHAR_FAIL("Cannot open archive \"%s\", alias is already in use by existing archive");
    1220             :                         }
    1221             :                 }
    1222             : 
    1223         142 :                 zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len, mydata);
    1224             :         } else {
    1225          67 :                 mydata->is_temporary_alias = 1;
    1226             :         }
    1227             : 
    1228         209 :         zend_hash_str_add_ptr(&(PHAR_G(phar_fname_map)), mydata->fname, fname_len, mydata);
    1229         209 :         efree(savebuf);
    1230             : 
    1231         209 :         if (pphar) {
    1232         109 :                 *pphar = mydata;
    1233             :         }
    1234             : 
    1235         209 :         return SUCCESS;
    1236             : }
    1237             : /* }}} */
    1238             : 
    1239             : /**
    1240             :  * Create or open a phar for writing
    1241             :  */
    1242         612 : int phar_open_or_create_filename(char *fname, int fname_len, char *alias, int alias_len, int is_data, int options, phar_archive_data** pphar, char **error) /* {{{ */
    1243             : {
    1244             :         const char *ext_str, *z;
    1245             :         char *my_error;
    1246             :         int ext_len;
    1247         612 :         phar_archive_data **test, *unused = NULL;
    1248             : 
    1249         612 :         test = &unused;
    1250             : 
    1251         612 :         if (error) {
    1252         612 :                 *error = NULL;
    1253             :         }
    1254             : 
    1255             :         /* first try to open an existing file */
    1256         612 :         if (phar_detect_phar_fname_ext(fname, fname_len, &ext_str, &ext_len, !is_data, 0, 1) == SUCCESS) {
    1257         418 :                 goto check_file;
    1258             :         }
    1259             : 
    1260             :         /* next try to create a new file */
    1261         194 :         if (FAILURE == phar_detect_phar_fname_ext(fname, fname_len, &ext_str, &ext_len, !is_data, 1, 1)) {
    1262           7 :                 if (error) {
    1263           7 :                         if (ext_len == -2) {
    1264           2 :                                 spprintf(error, 0, "Cannot create a phar archive from a URL like \"%s\". Phar objects can only be created from local files", fname);
    1265             :                         } else {
    1266           5 :                                 spprintf(error, 0, "Cannot create phar '%s', file extension (or combination) not recognised or the directory does not exist", fname);
    1267             :                         }
    1268             :                 }
    1269           7 :                 return FAILURE;
    1270             :         }
    1271             : check_file:
    1272         605 :         if (phar_open_parsed_phar(fname, fname_len, alias, alias_len, is_data, options, test, &my_error) == SUCCESS) {
    1273         250 :                 if (pphar) {
    1274         250 :                         *pphar = *test;
    1275             :                 }
    1276             : 
    1277         250 :                 if ((*test)->is_data && !(*test)->is_tar && !(*test)->is_zip) {
    1278           0 :                         if (error) {
    1279           0 :                                 spprintf(error, 0, "Cannot open '%s' as a PharData object. Use Phar::__construct() for executable archives", fname);
    1280             :                         }
    1281           0 :                         return FAILURE;
    1282             :                 }
    1283             : 
    1284         250 :                 if (PHAR_G(readonly) && !(*test)->is_data && ((*test)->is_tar || (*test)->is_zip)) {
    1285             :                         phar_entry_info *stub;
    1286           0 :                         if (NULL == (stub = zend_hash_str_find_ptr(&((*test)->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1))) {
    1287           0 :                                 spprintf(error, 0, "'%s' is not a phar archive. Use PharData::__construct() for a standard zip or tar archive", fname);
    1288           0 :                                 return FAILURE;
    1289             :                         }
    1290             :                 }
    1291             : 
    1292         250 :                 if (!PHAR_G(readonly) || (*test)->is_data) {
    1293         242 :                         (*test)->is_writeable = 1;
    1294             :                 }
    1295         250 :                 return SUCCESS;
    1296         355 :         } else if (my_error) {
    1297           7 :                 if (error) {
    1298           7 :                         *error = my_error;
    1299             :                 } else {
    1300           0 :                         efree(my_error);
    1301             :                 }
    1302           7 :                 return FAILURE;
    1303             :         }
    1304             : 
    1305         348 :         if (ext_len > 3 && (z = memchr(ext_str, 'z', ext_len)) && ((ext_str + ext_len) - z >= 2) && !memcmp(z + 1, "ip", 2)) {
    1306             :                 /* assume zip-based phar */
    1307          98 :                 return phar_open_or_create_zip(fname, fname_len, alias, alias_len, is_data, options, pphar, error);
    1308             :         }
    1309             : 
    1310         250 :         if (ext_len > 3 && (z = memchr(ext_str, 't', ext_len)) && ((ext_str + ext_len) - z >= 2) && !memcmp(z + 1, "ar", 2)) {
    1311             :                 /* assume tar-based phar */
    1312          79 :                 return phar_open_or_create_tar(fname, fname_len, alias, alias_len, is_data, options, pphar, error);
    1313             :         }
    1314             : 
    1315         171 :         return phar_create_or_parse_filename(fname, fname_len, alias, alias_len, is_data, options, pphar, error);
    1316             : }
    1317             : /* }}} */
    1318             : 
    1319         348 : int phar_create_or_parse_filename(char *fname, int fname_len, char *alias, int alias_len, int is_data, int options, phar_archive_data** pphar, char **error) /* {{{ */
    1320             : {
    1321             :         phar_archive_data *mydata;
    1322             :         php_stream *fp;
    1323         348 :         zend_string *actual = NULL;
    1324             :         char *p;
    1325             : 
    1326         348 :         if (!pphar) {
    1327           0 :                 pphar = &mydata;
    1328             :         }
    1329             : #if PHP_API_VERSION < 20100412
    1330             :         if (PG(safe_mode) && (!php_checkuid(fname, NULL, CHECKUID_ALLOW_ONLY_FILE))) {
    1331             :                 return FAILURE;
    1332             :         }
    1333             : #endif
    1334         348 :         if (php_check_open_basedir(fname)) {
    1335           0 :                 return FAILURE;
    1336             :         }
    1337             : 
    1338             :         /* first open readonly so it won't be created if not present */
    1339         348 :         fp = php_stream_open_wrapper(fname, "rb", IGNORE_URL|STREAM_MUST_SEEK|0, &actual);
    1340             : 
    1341         348 :         if (actual) {
    1342         169 :                 fname = ZSTR_VAL(actual);
    1343         169 :                 fname_len = ZSTR_LEN(actual);
    1344             :         }
    1345             : 
    1346         348 :         if (fp) {
    1347         169 :                 if (phar_open_from_fp(fp, fname, fname_len, alias, alias_len, options, pphar, is_data, error) == SUCCESS) {
    1348         119 :                         if ((*pphar)->is_data || !PHAR_G(readonly)) {
    1349         102 :                                 (*pphar)->is_writeable = 1;
    1350             :                         }
    1351         119 :                         if (actual) {
    1352         119 :                                 zend_string_release(actual);
    1353             :                         }
    1354         119 :                         return SUCCESS;
    1355             :                 } else {
    1356             :                         /* file exists, but is either corrupt or not a phar archive */
    1357          50 :                         if (actual) {
    1358          50 :                                 zend_string_release(actual);
    1359             :                         }
    1360          50 :                         return FAILURE;
    1361             :                 }
    1362             :         }
    1363             : 
    1364         179 :         if (actual) {
    1365           0 :                 zend_string_release(actual);
    1366             :         }
    1367             : 
    1368         179 :         if (PHAR_G(readonly) && !is_data) {
    1369           0 :                 if (options & REPORT_ERRORS) {
    1370           0 :                         if (error) {
    1371           0 :                                 spprintf(error, 0, "creating archive \"%s\" disabled by the php.ini setting phar.readonly", fname);
    1372             :                         }
    1373             :                 }
    1374           0 :                 return FAILURE;
    1375             :         }
    1376             : 
    1377             :         /* set up our manifest */
    1378         179 :         mydata = ecalloc(1, sizeof(phar_archive_data));
    1379         179 :         mydata->fname = expand_filepath(fname, NULL);
    1380         179 :         fname_len = strlen(mydata->fname);
    1381             : #ifdef PHP_WIN32
    1382             :         phar_unixify_path_separators(mydata->fname, fname_len);
    1383             : #endif
    1384         179 :         p = strrchr(mydata->fname, '/');
    1385             : 
    1386         179 :         if (p) {
    1387         179 :                 mydata->ext = memchr(p, '.', (mydata->fname + fname_len) - p);
    1388         179 :                 if (mydata->ext == p) {
    1389           0 :                         mydata->ext = memchr(p + 1, '.', (mydata->fname + fname_len) - p - 1);
    1390             :                 }
    1391         179 :                 if (mydata->ext) {
    1392         179 :                         mydata->ext_len = (mydata->fname + fname_len) - mydata->ext;
    1393             :                 }
    1394             :         }
    1395             : 
    1396         179 :         if (pphar) {
    1397         179 :                 *pphar = mydata;
    1398             :         }
    1399             : 
    1400         179 :         zend_hash_init(&mydata->manifest, sizeof(phar_entry_info),
    1401             :                 zend_get_hash_value, destroy_phar_manifest_entry, 0);
    1402         179 :         zend_hash_init(&mydata->mounted_dirs, sizeof(char *),
    1403             :                 zend_get_hash_value, NULL, 0);
    1404         179 :         zend_hash_init(&mydata->virtual_dirs, sizeof(char *),
    1405             :                 zend_get_hash_value, NULL, (zend_bool)mydata->is_persistent);
    1406         179 :         mydata->fname_len = fname_len;
    1407         179 :         snprintf(mydata->version, sizeof(mydata->version), "%s", PHP_PHAR_API_VERSION);
    1408         179 :         mydata->is_temporary_alias = alias ? 0 : 1;
    1409         179 :         mydata->internal_file_start = -1;
    1410         179 :         mydata->fp = NULL;
    1411         179 :         mydata->is_writeable = 1;
    1412         179 :         mydata->is_brandnew = 1;
    1413         179 :         phar_request_initialize();
    1414         179 :         zend_hash_str_add_ptr(&(PHAR_G(phar_fname_map)), mydata->fname, fname_len, mydata);
    1415             : 
    1416         179 :         if (is_data) {
    1417          11 :                 alias = NULL;
    1418          11 :                 alias_len = 0;
    1419          11 :                 mydata->is_data = 1;
    1420             :                 /* assume tar format, PharData can specify other */
    1421          11 :                 mydata->is_tar = 1;
    1422             :         } else {
    1423             :                 phar_archive_data *fd_ptr;
    1424             : 
    1425         177 :                 if (alias && NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len))) {
    1426           0 :                         if (SUCCESS != phar_free_alias(fd_ptr, alias, alias_len)) {
    1427           0 :                                 if (error) {
    1428           0 :                                         spprintf(error, 4096, "phar error: phar \"%s\" cannot set alias \"%s\", already in use by another phar archive", mydata->fname, alias);
    1429             :                                 }
    1430             : 
    1431           0 :                                 zend_hash_str_del(&(PHAR_G(phar_fname_map)), mydata->fname, fname_len);
    1432             : 
    1433           0 :                                 if (pphar) {
    1434           0 :                                         *pphar = NULL;
    1435             :                                 }
    1436             : 
    1437           0 :                                 return FAILURE;
    1438             :                         }
    1439             :                 }
    1440             : 
    1441         168 :                 mydata->alias = alias ? estrndup(alias, alias_len) : estrndup(mydata->fname, fname_len);
    1442         168 :                 mydata->alias_len = alias ? alias_len : fname_len;
    1443             :         }
    1444             : 
    1445         179 :         if (alias_len && alias) {
    1446          18 :                 if (NULL == zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len, mydata)) {
    1447           0 :                         if (options & REPORT_ERRORS) {
    1448           0 :                                 if (error) {
    1449           0 :                                         spprintf(error, 0, "archive \"%s\" cannot be associated with alias \"%s\", already in use", fname, alias);
    1450             :                                 }
    1451             :                         }
    1452             : 
    1453           0 :                         zend_hash_str_del(&(PHAR_G(phar_fname_map)), mydata->fname, fname_len);
    1454             : 
    1455           0 :                         if (pphar) {
    1456           0 :                                 *pphar = NULL;
    1457             :                         }
    1458             : 
    1459           0 :                         return FAILURE;
    1460             :                 }
    1461             :         }
    1462             : 
    1463         179 :         return SUCCESS;
    1464             : }
    1465             : /* }}}*/
    1466             : 
    1467             : /**
    1468             :  * Return an already opened filename.
    1469             :  *
    1470             :  * Or scan a phar file for the required __HALT_COMPILER(); ?> token and verify
    1471             :  * that the manifest is proper, then pass it to phar_parse_pharfile().  SUCCESS
    1472             :  * or FAILURE is returned and pphar is set to a pointer to the phar's manifest
    1473             :  */
    1474        2121 : int phar_open_from_filename(char *fname, int fname_len, char *alias, int alias_len, int options, phar_archive_data** pphar, char **error) /* {{{ */
    1475             : {
    1476             :         php_stream *fp;
    1477             :         zend_string *actual;
    1478        2121 :         int ret, is_data = 0;
    1479             : 
    1480        2121 :         if (error) {
    1481        1820 :                 *error = NULL;
    1482             :         }
    1483             : 
    1484        2121 :         if (!strstr(fname, ".phar")) {
    1485         414 :                 is_data = 1;
    1486             :         }
    1487             : 
    1488        2121 :         if (phar_open_parsed_phar(fname, fname_len, alias, alias_len, is_data, options, pphar, error) == SUCCESS) {
    1489        1779 :                 return SUCCESS;
    1490         342 :         } else if (error && *error) {
    1491           3 :                 return FAILURE;
    1492             :         }
    1493             : #if PHP_API_VERSION < 20100412
    1494             :         if (PG(safe_mode) && (!php_checkuid(fname, NULL, CHECKUID_ALLOW_ONLY_FILE))) {
    1495             :                 return FAILURE;
    1496             :         }
    1497             : #endif
    1498         339 :         if (php_check_open_basedir(fname)) {
    1499           0 :                 return FAILURE;
    1500             :         }
    1501             : 
    1502         339 :         fp = php_stream_open_wrapper(fname, "rb", IGNORE_URL|STREAM_MUST_SEEK, &actual);
    1503             : 
    1504         339 :         if (!fp) {
    1505          63 :                 if (options & REPORT_ERRORS) {
    1506           1 :                         if (error) {
    1507           1 :                                 spprintf(error, 0, "unable to open phar for reading \"%s\"", fname);
    1508             :                         }
    1509             :                 }
    1510          63 :                 if (actual) {
    1511           0 :                         zend_string_release(actual);
    1512             :                 }
    1513          63 :                 return FAILURE;
    1514             :         }
    1515             : 
    1516         276 :         if (actual) {
    1517         276 :                 fname = ZSTR_VAL(actual);
    1518         276 :                 fname_len = ZSTR_LEN(actual);
    1519             :         }
    1520             : 
    1521         276 :         ret =  phar_open_from_fp(fp, fname, fname_len, alias, alias_len, options, pphar, is_data, error);
    1522             : 
    1523         276 :         if (actual) {
    1524         276 :                 zend_string_release(actual);
    1525             :         }
    1526             : 
    1527         276 :         return ret;
    1528             : }
    1529             : /* }}}*/
    1530             : 
    1531         528 : static inline char *phar_strnstr(const char *buf, int buf_len, const char *search, int search_len) /* {{{ */
    1532             : {
    1533             :         const char *c;
    1534         528 :         ptrdiff_t so_far = 0;
    1535             : 
    1536         528 :         if (buf_len < search_len) {
    1537           0 :                 return NULL;
    1538             :         }
    1539             : 
    1540         528 :         c = buf - 1;
    1541             : 
    1542             :         do {
    1543        4232 :                 if (!(c = memchr(c + 1, search[0], buf_len - search_len - so_far))) {
    1544         241 :                         return (char *) NULL;
    1545             :                 }
    1546             : 
    1547        3991 :                 so_far = c - buf;
    1548             : 
    1549        3991 :                 if (so_far >= (buf_len - search_len)) {
    1550           0 :                         return (char *) NULL;
    1551             :                 }
    1552             : 
    1553        3991 :                 if (!memcmp(c, search, search_len)) {
    1554         287 :                         return (char *) c;
    1555             :                 }
    1556        3704 :         } while (1);
    1557             : }
    1558             : /* }}} */
    1559             : 
    1560             : /**
    1561             :  * Scan an open fp for the required __HALT_COMPILER(); ?> token and verify
    1562             :  * that the manifest is proper, then pass it to phar_parse_pharfile().  SUCCESS
    1563             :  * or FAILURE is returned and pphar is set to a pointer to the phar's manifest
    1564             :  */
    1565         517 : static int phar_open_from_fp(php_stream* fp, char *fname, int fname_len, char *alias, int alias_len, int options, phar_archive_data** pphar, int is_data, char **error) /* {{{ */
    1566             : {
    1567         517 :         const char token[] = "__HALT_COMPILER();";
    1568         517 :         const char zip_magic[] = "PK\x03\x04";
    1569         517 :         const char gz_magic[] = "\x1f\x8b\x08";
    1570         517 :         const char bz_magic[] = "BZh";
    1571         517 :         char *pos, test = '\0';
    1572         517 :         const int window_size = 1024;
    1573             :         char buffer[1024 + sizeof(token)]; /* a 1024 byte window + the size of the halt_compiler token (moving window) */
    1574         517 :         const zend_long readsize = sizeof(buffer) - sizeof(token);
    1575         517 :         const zend_long tokenlen = sizeof(token) - 1;
    1576             :         zend_long halt_offset;
    1577             :         size_t got;
    1578         517 :         php_uint32 compression = PHAR_FILE_COMPRESSED_NONE;
    1579             : 
    1580         517 :         if (error) {
    1581         295 :                 *error = NULL;
    1582             :         }
    1583             : 
    1584         517 :         if (-1 == php_stream_rewind(fp)) {
    1585           0 :                 MAPPHAR_ALLOC_FAIL("cannot rewind phar \"%s\"")
    1586             :         }
    1587             : 
    1588         517 :         buffer[sizeof(buffer)-1] = '\0';
    1589         517 :         memset(buffer, 32, sizeof(token));
    1590         517 :         halt_offset = 0;
    1591             : 
    1592             :         /* Maybe it's better to compile the file instead of just searching,  */
    1593             :         /* but we only want the offset. So we want a .re scanner to find it. */
    1594        1296 :         while(!php_stream_eof(fp)) {
    1595         707 :                 if ((got = php_stream_read(fp, buffer+tokenlen, readsize)) < (size_t) tokenlen) {
    1596           2 :                         MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated entry)")
    1597             :                 }
    1598             : 
    1599         705 :                 if (!test) {
    1600         536 :                         test = '\1';
    1601         536 :                         pos = buffer+tokenlen;
    1602         536 :                         if (!memcmp(pos, gz_magic, 3)) {
    1603          13 :                                 char err = 0;
    1604             :                                 php_stream_filter *filter;
    1605             :                                 php_stream *temp;
    1606             :                                 /* to properly decompress, we have to tell zlib to look for a zlib or gzip header */
    1607             :                                 zval filterparams;
    1608             : 
    1609          13 :                                 if (!PHAR_G(has_zlib)) {
    1610           0 :                                         MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\" to temporary file, enable zlib extension in php.ini")
    1611             :                                 }
    1612          13 :                                 array_init(&filterparams);
    1613             : /* this is defined in zlib's zconf.h */
    1614             : #ifndef MAX_WBITS
    1615             : #define MAX_WBITS 15
    1616             : #endif
    1617          13 :                                 add_assoc_long_ex(&filterparams, "window", sizeof("window") - 1, MAX_WBITS + 32);
    1618             : 
    1619             :                                 /* entire file is gzip-compressed, uncompress to temporary file */
    1620          13 :                                 if (!(temp = php_stream_fopen_tmpfile())) {
    1621           0 :                                         MAPPHAR_ALLOC_FAIL("unable to create temporary file for decompression of gzipped phar archive \"%s\"")
    1622             :                                 }
    1623             : 
    1624          13 :                                 php_stream_rewind(fp);
    1625          13 :                                 filter = php_stream_filter_create("zlib.inflate", &filterparams, php_stream_is_persistent(fp));
    1626             : 
    1627          13 :                                 if (!filter) {
    1628           0 :                                         err = 1;
    1629           0 :                                         add_assoc_long_ex(&filterparams, "window", sizeof("window") - 1, MAX_WBITS);
    1630           0 :                                         filter = php_stream_filter_create("zlib.inflate", &filterparams, php_stream_is_persistent(fp));
    1631             :                                         zval_dtor(&filterparams);
    1632             : 
    1633           0 :                                         if (!filter) {
    1634           0 :                                                 php_stream_close(temp);
    1635           0 :                                                 MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\", ext/zlib is buggy in PHP versions older than 5.2.6")
    1636             :                                         }
    1637             :                                 } else {
    1638             :                                         zval_dtor(&filterparams);
    1639             :                                 }
    1640             : 
    1641          13 :                                 php_stream_filter_append(&temp->writefilters, filter);
    1642             : 
    1643          13 :                                 if (SUCCESS != php_stream_copy_to_stream_ex(fp, temp, PHP_STREAM_COPY_ALL, NULL)) {
    1644           0 :                                         if (err) {
    1645           0 :                                                 php_stream_close(temp);
    1646           0 :                                                 MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\", ext/zlib is buggy in PHP versions older than 5.2.6")
    1647             :                                         }
    1648           0 :                                         php_stream_close(temp);
    1649           0 :                                         MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\" to temporary file")
    1650             :                                 }
    1651             : 
    1652          13 :                                 php_stream_filter_flush(filter, 1);
    1653          13 :                                 php_stream_filter_remove(filter, 1);
    1654          13 :                                 php_stream_close(fp);
    1655          13 :                                 fp = temp;
    1656          13 :                                 php_stream_rewind(fp);
    1657          13 :                                 compression = PHAR_FILE_COMPRESSED_GZ;
    1658             : 
    1659             :                                 /* now, start over */
    1660          13 :                                 test = '\0';
    1661          13 :                                 continue;
    1662         523 :                         } else if (!memcmp(pos, bz_magic, 3)) {
    1663             :                                 php_stream_filter *filter;
    1664             :                                 php_stream *temp;
    1665             : 
    1666           8 :                                 if (!PHAR_G(has_bz2)) {
    1667           0 :                                         MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\" to temporary file, enable bz2 extension in php.ini")
    1668             :                                 }
    1669             : 
    1670             :                                 /* entire file is bzip-compressed, uncompress to temporary file */
    1671           8 :                                 if (!(temp = php_stream_fopen_tmpfile())) {
    1672           0 :                                         MAPPHAR_ALLOC_FAIL("unable to create temporary file for decompression of bzipped phar archive \"%s\"")
    1673             :                                 }
    1674             : 
    1675           8 :                                 php_stream_rewind(fp);
    1676           8 :                                 filter = php_stream_filter_create("bzip2.decompress", NULL, php_stream_is_persistent(fp));
    1677             : 
    1678           8 :                                 if (!filter) {
    1679           0 :                                         php_stream_close(temp);
    1680           0 :                                         MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\", filter creation failed")
    1681             :                                 }
    1682             : 
    1683           8 :                                 php_stream_filter_append(&temp->writefilters, filter);
    1684             : 
    1685           8 :                                 if (SUCCESS != php_stream_copy_to_stream_ex(fp, temp, PHP_STREAM_COPY_ALL, NULL)) {
    1686           0 :                                         php_stream_close(temp);
    1687           0 :                                         MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\" to temporary file")
    1688             :                                 }
    1689             : 
    1690           8 :                                 php_stream_filter_flush(filter, 1);
    1691           8 :                                 php_stream_filter_remove(filter, 1);
    1692           8 :                                 php_stream_close(fp);
    1693           8 :                                 fp = temp;
    1694           8 :                                 php_stream_rewind(fp);
    1695           8 :                                 compression = PHAR_FILE_COMPRESSED_BZ2;
    1696             : 
    1697             :                                 /* now, start over */
    1698           8 :                                 test = '\0';
    1699           8 :                                 continue;
    1700             :                         }
    1701             : 
    1702         515 :                         if (!memcmp(pos, zip_magic, 4)) {
    1703          81 :                                 php_stream_seek(fp, 0, SEEK_END);
    1704          81 :                                 return phar_parse_zipfile(fp, fname, fname_len, alias, alias_len, pphar, error);
    1705             :                         }
    1706             : 
    1707         434 :                         if (got > 512) {
    1708         160 :                                 if (phar_is_tar(pos, fname)) {
    1709          75 :                                         php_stream_rewind(fp);
    1710          75 :                                         return phar_parse_tarfile(fp, fname, fname_len, alias, alias_len, pphar, is_data, compression, error);
    1711             :                                 }
    1712             :                         }
    1713             :                 }
    1714             : 
    1715         528 :                 if (got > 0 && (pos = phar_strnstr(buffer, got + sizeof(token), token, sizeof(token)-1)) != NULL) {
    1716         287 :                         halt_offset += (pos - buffer); /* no -tokenlen+tokenlen here */
    1717         287 :                         return phar_parse_pharfile(fp, fname, fname_len, alias, alias_len, halt_offset, pphar, compression, error);
    1718             :                 }
    1719             : 
    1720         241 :                 halt_offset += got;
    1721         241 :                 memmove(buffer, buffer + window_size, tokenlen); /* move the memory buffer by the size of the window */
    1722             :         }
    1723             : 
    1724          72 :         MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (__HALT_COMPILER(); not found)")
    1725             : }
    1726             : /* }}} */
    1727             : 
    1728             : /*
    1729             :  * given the location of the file extension and the start of the file path,
    1730             :  * determine the end of the portion of the path (i.e. /path/to/file.ext/blah
    1731             :  * grabs "/path/to/file.ext" as does the straight /path/to/file.ext),
    1732             :  * stat it to determine if it exists.
    1733             :  * if so, check to see if it is a directory and fail if so
    1734             :  * if not, check to see if its dirname() exists (i.e. "/path/to") and is a directory
    1735             :  * succeed if we are creating the file, otherwise fail.
    1736             :  */
    1737        1052 : static int phar_analyze_path(const char *fname, const char *ext, int ext_len, int for_create) /* {{{ */
    1738             : {
    1739             :         php_stream_statbuf ssb;
    1740             :         char *realpath;
    1741        1052 :         char *filename = estrndup(fname, (ext - fname) + ext_len);
    1742             : 
    1743        1052 :         if ((realpath = expand_filepath(filename, NULL))) {
    1744             : #ifdef PHP_WIN32
    1745             :                 phar_unixify_path_separators(realpath, strlen(realpath));
    1746             : #endif
    1747        1051 :                 if (zend_hash_str_exists(&(PHAR_G(phar_fname_map)), realpath, strlen(realpath))) {
    1748           8 :                         efree(realpath);
    1749           8 :                         efree(filename);
    1750           8 :                         return SUCCESS;
    1751             :                 }
    1752             : 
    1753        1043 :                 if (PHAR_G(manifest_cached) && zend_hash_str_exists(&cached_phars, realpath, strlen(realpath))) {
    1754           0 :                         efree(realpath);
    1755           0 :                         efree(filename);
    1756           0 :                         return SUCCESS;
    1757             :                 }
    1758        1043 :                 efree(realpath);
    1759             :         }
    1760             : 
    1761        1044 :         if (SUCCESS == php_stream_stat_path((char *) filename, &ssb)) {
    1762             : 
    1763         393 :                 efree(filename);
    1764             : 
    1765         393 :                 if (ssb.sb.st_mode & S_IFDIR) {
    1766           0 :                         return FAILURE;
    1767             :                 }
    1768             : 
    1769         393 :                 if (for_create == 1) {
    1770           0 :                         return FAILURE;
    1771             :                 }
    1772             : 
    1773         393 :                 return SUCCESS;
    1774             :         } else {
    1775             :                 char *slash;
    1776             : 
    1777         651 :                 if (!for_create) {
    1778         212 :                         efree(filename);
    1779         212 :                         return FAILURE;
    1780             :                 }
    1781             : 
    1782         439 :                 slash = (char *) strrchr(filename, '/');
    1783             : 
    1784         439 :                 if (slash) {
    1785         405 :                         *slash = '\0';
    1786             :                 }
    1787             : 
    1788         439 :                 if (SUCCESS != php_stream_stat_path((char *) filename, &ssb)) {
    1789          34 :                         if (!slash) {
    1790          34 :                                 if (!(realpath = expand_filepath(filename, NULL))) {
    1791           0 :                                         efree(filename);
    1792           0 :                                         return FAILURE;
    1793             :                                 }
    1794             : #ifdef PHP_WIN32
    1795             :                                 phar_unixify_path_separators(realpath, strlen(realpath));
    1796             : #endif
    1797          34 :                                 slash = strstr(realpath, filename) + ((ext - fname) + ext_len);
    1798          34 :                                 *slash = '\0';
    1799          34 :                                 slash = strrchr(realpath, '/');
    1800             : 
    1801          34 :                                 if (slash) {
    1802          34 :                                         *slash = '\0';
    1803             :                                 } else {
    1804           0 :                                         efree(realpath);
    1805           0 :                                         efree(filename);
    1806           0 :                                         return FAILURE;
    1807             :                                 }
    1808             : 
    1809          34 :                                 if (SUCCESS != php_stream_stat_path(realpath, &ssb)) {
    1810           0 :                                         efree(realpath);
    1811           0 :                                         efree(filename);
    1812           0 :                                         return FAILURE;
    1813             :                                 }
    1814             : 
    1815          34 :                                 efree(realpath);
    1816             : 
    1817          34 :                                 if (ssb.sb.st_mode & S_IFDIR) {
    1818          34 :                                         efree(filename);
    1819          34 :                                         return SUCCESS;
    1820             :                                 }
    1821             :                         }
    1822             : 
    1823           0 :                         efree(filename);
    1824           0 :                         return FAILURE;
    1825             :                 }
    1826             : 
    1827         405 :                 efree(filename);
    1828             : 
    1829         405 :                 if (ssb.sb.st_mode & S_IFDIR) {
    1830         404 :                         return SUCCESS;
    1831             :                 }
    1832             : 
    1833           1 :                 return FAILURE;
    1834             :         }
    1835             : }
    1836             : /* }}} */
    1837             : 
    1838             : /* check for ".phar" in extension */
    1839        1075 : static int phar_check_str(const char *fname, const char *ext_str, int ext_len, int executable, int for_create) /* {{{ */
    1840             : {
    1841             :         char test[51];
    1842             :         const char *pos;
    1843             : 
    1844        1075 :         if (ext_len >= 50) {
    1845           0 :                 return FAILURE;
    1846             :         }
    1847             : 
    1848        1075 :         if (executable == 1) {
    1849             :                 /* copy "." as well */
    1850         803 :                 memcpy(test, ext_str - 1, ext_len + 1);
    1851         803 :                 test[ext_len + 1] = '\0';
    1852             :                 /* executable phars must contain ".phar" as a valid extension (phar://.pharmy/oops is invalid) */
    1853             :                 /* (phar://hi/there/.phar/oops is also invalid) */
    1854         803 :                 pos = strstr(test, ".phar");
    1855             : 
    1856        2699 :                 if (pos && (*(pos - 1) != '/')
    1857        1896 :                                 && (pos += 5) && (*pos == '\0' || *pos == '/' || *pos == '.')) {
    1858         788 :                         return phar_analyze_path(fname, ext_str, ext_len, for_create);
    1859             :                 } else {
    1860          15 :                         return FAILURE;
    1861             :                 }
    1862             :         }
    1863             : 
    1864             :         /* data phars need only contain a single non-"." to be valid */
    1865         272 :         if (!executable) {
    1866         157 :                 pos = strstr(ext_str, ".phar");
    1867         627 :                 if (!(pos && (*(pos - 1) != '/')
    1868         470 :                                         && (pos += 5) && (*pos == '\0' || *pos == '/' || *pos == '.')) && *(ext_str + 1) != '.' && *(ext_str + 1) != '/' && *(ext_str + 1) != '\0') {
    1869         149 :                         return phar_analyze_path(fname, ext_str, ext_len, for_create);
    1870             :                 }
    1871             :         } else {
    1872         115 :                 if (*(ext_str + 1) != '.' && *(ext_str + 1) != '/' && *(ext_str + 1) != '\0') {
    1873         115 :                         return phar_analyze_path(fname, ext_str, ext_len, for_create);
    1874             :                 }
    1875             :         }
    1876             : 
    1877           8 :         return FAILURE;
    1878             : }
    1879             : /* }}} */
    1880             : 
    1881             : /*
    1882             :  * if executable is 1, only returns SUCCESS if the extension is one of the tar/zip .phar extensions
    1883             :  * if executable is 0, it returns SUCCESS only if the filename does *not* contain ".phar" anywhere, and treats
    1884             :  * the first extension as the filename extension
    1885             :  *
    1886             :  * if an extension is found, it sets ext_str to the location of the file extension in filename,
    1887             :  * and ext_len to the length of the extension.
    1888             :  * for urls like "phar://alias/oops" it instead sets ext_len to -1 and returns FAILURE, which tells
    1889             :  * the calling function to use "alias" as the phar alias
    1890             :  *
    1891             :  * the last parameter should be set to tell the thing to assume that filename is the full path, and only to check the
    1892             :  * extension rules, not to iterate.
    1893             :  */
    1894        3447 : int phar_detect_phar_fname_ext(const char *filename, int filename_len, const char **ext_str, int *ext_len, int executable, int for_create, int is_complete) /* {{{ */
    1895             : {
    1896             :         const char *pos, *slash;
    1897             : 
    1898        3447 :         *ext_str = NULL;
    1899        3447 :         *ext_len = 0;
    1900             : 
    1901        3447 :         if (!filename_len || filename_len == 1) {
    1902          16 :                 return FAILURE;
    1903             :         }
    1904             : 
    1905        3431 :         phar_request_initialize();
    1906             :         /* first check for alias in first segment */
    1907        3431 :         pos = memchr(filename, '/', filename_len);
    1908             : 
    1909        3431 :         if (pos && pos != filename) {
    1910             :                 /* check for url like http:// or phar:// */
    1911          83 :                 if (*(pos - 1) == ':' && (pos - filename) < filename_len - 1 && *(pos + 1) == '/') {
    1912           6 :                         *ext_len = -2;
    1913           6 :                         *ext_str = NULL;
    1914           6 :                         return FAILURE;
    1915             :                 }
    1916          77 :                 if (zend_hash_str_exists(&(PHAR_G(phar_alias_map)), (char *) filename, pos - filename)) {
    1917          64 :                         *ext_str = pos;
    1918          64 :                         *ext_len = -1;
    1919          64 :                         return FAILURE;
    1920             :                 }
    1921             : 
    1922          13 :                 if (PHAR_G(manifest_cached) && zend_hash_str_exists(&cached_alias, (char *) filename, pos - filename)) {
    1923           0 :                         *ext_str = pos;
    1924           0 :                         *ext_len = -1;
    1925           0 :                         return FAILURE;
    1926             :                 }
    1927             :         }
    1928             : 
    1929        3361 :         if (zend_hash_num_elements(&(PHAR_G(phar_fname_map))) || PHAR_G(manifest_cached)) {
    1930             :                 phar_archive_data *pphar;
    1931             : 
    1932        2559 :                 if (is_complete) {
    1933         826 :                         if (NULL != (pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), (char *) filename, filename_len))) {
    1934         245 :                                 *ext_str = filename + (filename_len - pphar->ext_len);
    1935             : woohoo:
    1936        2288 :                                 *ext_len = pphar->ext_len;
    1937             : 
    1938        2288 :                                 if (executable == 2) {
    1939        1864 :                                         return SUCCESS;
    1940             :                                 }
    1941             : 
    1942         424 :                                 if (executable == 1 && !pphar->is_data) {
    1943         351 :                                         return SUCCESS;
    1944             :                                 }
    1945             : 
    1946          73 :                                 if (!executable && pphar->is_data) {
    1947          70 :                                         return SUCCESS;
    1948             :                                 }
    1949             : 
    1950           3 :                                 return FAILURE;
    1951             :                         }
    1952             : 
    1953         172 :                         if (PHAR_G(manifest_cached) && NULL != (pphar = zend_hash_str_find_ptr(&cached_phars, (char *) filename, filename_len))) {
    1954           4 :                                 *ext_str = filename + (filename_len - pphar->ext_len);
    1955           4 :                                 goto woohoo;
    1956             :                         }
    1957             :                 } else {
    1958             :                         zend_string *str_key;
    1959             :                         zend_ulong unused;
    1960             : 
    1961        5039 :                         for (zend_hash_internal_pointer_reset(&(PHAR_G(phar_fname_map)));
    1962        2893 :                                 HASH_KEY_NON_EXISTENT != zend_hash_get_current_key(&(PHAR_G(phar_fname_map)), &str_key, &unused);
    1963             :                                 zend_hash_move_forward(&(PHAR_G(phar_fname_map)))
    1964         747 :                         ) {
    1965        2776 :                                 if (ZSTR_LEN(str_key) > (uint) filename_len) {
    1966         177 :                                         continue;
    1967             :                                 }
    1968             : 
    1969        8755 :                                 if (!memcmp(filename, ZSTR_VAL(str_key), ZSTR_LEN(str_key)) && ((uint)filename_len == ZSTR_LEN(str_key)
    1970        6156 :                                         || filename[ZSTR_LEN(str_key)] == '/' || filename[ZSTR_LEN(str_key)] == '\0')) {
    1971        2029 :                                         if (NULL == (pphar = zend_hash_get_current_data_ptr(&(PHAR_G(phar_fname_map))))) {
    1972           0 :                                                 break;
    1973             :                                         }
    1974        2029 :                                         *ext_str = filename + (ZSTR_LEN(str_key) - pphar->ext_len);
    1975        2029 :                                         goto woohoo;
    1976             :                                 }
    1977             :                         }
    1978             : 
    1979         117 :                         if (PHAR_G(manifest_cached)) {
    1980          20 :                                 for (zend_hash_internal_pointer_reset(&cached_phars);
    1981          10 :                                         HASH_KEY_NON_EXISTENT != zend_hash_get_current_key(&cached_phars, &str_key, &unused);
    1982             :                                         zend_hash_move_forward(&cached_phars)
    1983           0 :                                 ) {
    1984          10 :                                         if (ZSTR_LEN(str_key) > (uint) filename_len) {
    1985           0 :                                                 continue;
    1986             :                                         }
    1987             : 
    1988          38 :                                         if (!memcmp(filename, ZSTR_VAL(str_key), ZSTR_LEN(str_key)) && ((uint)filename_len == ZSTR_LEN(str_key)
    1989          28 :                                                 || filename[ZSTR_LEN(str_key)] == '/' || filename[ZSTR_LEN(str_key)] == '\0')) {
    1990          10 :                                                 if (NULL == (pphar = zend_hash_get_current_data_ptr(&cached_phars))) {
    1991           0 :                                                         break;
    1992             :                                                 }
    1993          10 :                                                 *ext_str = filename + (ZSTR_LEN(str_key) - pphar->ext_len);
    1994          10 :                                                 goto woohoo;
    1995             :                                         }
    1996             :                                 }
    1997             :                         }
    1998             :                 }
    1999             :         }
    2000             : 
    2001        1073 :         pos = memchr(filename + 1, '.', filename_len);
    2002             : next_extension:
    2003        1093 :         if (!pos) {
    2004          18 :                 return FAILURE;
    2005             :         }
    2006             : 
    2007        2151 :         while (pos != filename && (*(pos - 1) == '/' || *(pos - 1) == '\0')) {
    2008           1 :                 pos = memchr(pos + 1, '.', filename_len - (pos - filename) + 1);
    2009           1 :                 if (!pos) {
    2010           0 :                         return FAILURE;
    2011             :                 }
    2012             :         }
    2013             : 
    2014        1075 :         slash = memchr(pos, '/', filename_len - (pos - filename));
    2015             : 
    2016        1075 :         if (!slash) {
    2017             :                 /* this is a url like "phar://blah.phar" with no directory */
    2018         972 :                 *ext_str = pos;
    2019         972 :                 *ext_len = strlen(pos);
    2020             : 
    2021             :                 /* file extension must contain "phar" */
    2022         972 :                 switch (phar_check_str(filename, *ext_str, *ext_len, executable, for_create)) {
    2023             :                         case SUCCESS:
    2024         756 :                                 return SUCCESS;
    2025             :                         case FAILURE:
    2026             :                                 /* we are at the end of the string, so we fail */
    2027         216 :                                 return FAILURE;
    2028             :                 }
    2029             :         }
    2030             : 
    2031             :         /* we've found an extension that ends at a directory separator */
    2032         103 :         *ext_str = pos;
    2033         103 :         *ext_len = slash - pos;
    2034             : 
    2035         103 :         switch (phar_check_str(filename, *ext_str, *ext_len, executable, for_create)) {
    2036             :                 case SUCCESS:
    2037          83 :                         return SUCCESS;
    2038             :                 case FAILURE:
    2039             :                         /* look for more extensions */
    2040          20 :                         pos = strchr(pos + 1, '.');
    2041          20 :                         if (pos) {
    2042          15 :                                 *ext_str = NULL;
    2043          15 :                                 *ext_len = 0;
    2044             :                         }
    2045          20 :                         goto next_extension;
    2046             :         }
    2047             : 
    2048           0 :         return FAILURE;
    2049             : }
    2050             : /* }}} */
    2051             : 
    2052        1424 : static int php_check_dots(const char *element, int n) /* {{{ */
    2053             : {
    2054        1442 :         for(n--; n >= 0; --n) {
    2055        1433 :                 if (element[n] != '.') {
    2056        1415 :                         return 1;
    2057             :                 }
    2058             :         }
    2059           9 :         return 0;
    2060             : }
    2061             : /* }}} */
    2062             : 
    2063             : #define IS_DIRECTORY_UP(element, len) \
    2064             :         (len >= 2 && !php_check_dots(element, len))
    2065             : 
    2066             : #define IS_DIRECTORY_CURRENT(element, len) \
    2067             :         (len == 1 && element[0] == '.')
    2068             : 
    2069             : #define IS_BACKSLASH(c) ((c) == '/')
    2070             : 
    2071             : /**
    2072             :  * Remove .. and . references within a phar filename
    2073             :  */
    2074        2110 : char *phar_fix_filepath(char *path, int *new_len, int use_cwd) /* {{{ */
    2075             : {
    2076             :         char *newpath;
    2077             :         int newpath_len;
    2078             :         char *ptr;
    2079             :         char *tok;
    2080        2110 :         int ptr_length, path_length = *new_len;
    2081             : 
    2082        2121 :         if (PHAR_G(cwd_len) && use_cwd && path_length > 2 && path[0] == '.' && path[1] == '/') {
    2083          11 :                 newpath_len = PHAR_G(cwd_len);
    2084          11 :                 newpath = emalloc(strlen(path) + newpath_len + 1);
    2085          11 :                 memcpy(newpath, PHAR_G(cwd), newpath_len);
    2086             :         } else {
    2087        2099 :                 newpath = emalloc(strlen(path) + 2);
    2088        2099 :                 newpath[0] = '/';
    2089        2099 :                 newpath_len = 1;
    2090             :         }
    2091             : 
    2092        2110 :         ptr = path;
    2093             : 
    2094        2110 :         if (*ptr == '/') {
    2095        2057 :                 ++ptr;
    2096             :         }
    2097             : 
    2098        2110 :         tok = ptr;
    2099             : 
    2100             :         do {
    2101        2112 :                 ptr = memchr(ptr, '/', path_length - (ptr - path));
    2102        2112 :         } while (ptr && ptr - tok == 0 && *ptr == '/' && ++ptr && ++tok);
    2103             : 
    2104        2110 :         if (!ptr && (path_length - (tok - path))) {
    2105        1037 :                 switch (path_length - (tok - path)) {
    2106             :                         case 1:
    2107         478 :                                 if (*tok == '.') {
    2108           2 :                                         efree(path);
    2109           2 :                                         *new_len = 1;
    2110           2 :                                         efree(newpath);
    2111           2 :                                         return estrndup("/", 1);
    2112             :                                 }
    2113         476 :                                 break;
    2114             :                         case 2:
    2115          16 :                                 if (tok[0] == '.' && tok[1] == '.') {
    2116           0 :                                         efree(path);
    2117           0 :                                         *new_len = 1;
    2118           0 :                                         efree(newpath);
    2119           0 :                                         return estrndup("/", 1);
    2120             :                                 }
    2121             :                 }
    2122        1035 :                 efree(newpath);
    2123        1035 :                 return path;
    2124             :         }
    2125             : 
    2126        2558 :         while (ptr) {
    2127        1052 :                 ptr_length = ptr - tok;
    2128             : last_time:
    2129        1701 :                 if (IS_DIRECTORY_UP(tok, ptr_length)) {
    2130             : #define PREVIOUS newpath[newpath_len - 1]
    2131             : 
    2132          23 :                         while (newpath_len > 1 && !IS_BACKSLASH(PREVIOUS)) {
    2133           5 :                                 newpath_len--;
    2134             :                         }
    2135             : 
    2136           9 :                         if (newpath[0] != '/') {
    2137           0 :                                 newpath[newpath_len] = '\0';
    2138           9 :                         } else if (newpath_len > 1) {
    2139           0 :                                 --newpath_len;
    2140             :                         }
    2141        1683 :                 } else if (!IS_DIRECTORY_CURRENT(tok, ptr_length)) {
    2142        1672 :                         if (newpath_len > 1) {
    2143        1014 :                                 newpath[newpath_len++] = '/';
    2144        1014 :                                 memcpy(newpath + newpath_len, tok, ptr_length+1);
    2145             :                         } else {
    2146         658 :                                 memcpy(newpath + newpath_len, tok, ptr_length+1);
    2147             :                         }
    2148             : 
    2149        1672 :                         newpath_len += ptr_length;
    2150             :                 }
    2151             : 
    2152        1692 :                 if (ptr == path + path_length) {
    2153         640 :                         break;
    2154             :                 }
    2155             : 
    2156        1052 :                 tok = ++ptr;
    2157             : 
    2158             :                 do {
    2159        1053 :                         ptr = memchr(ptr, '/', path_length - (ptr - path));
    2160        1053 :                 } while (ptr && ptr - tok == 0 && *ptr == '/' && ++ptr && ++tok);
    2161             : 
    2162        1052 :                 if (!ptr && (path_length - (tok - path))) {
    2163         640 :                         ptr_length = path_length - (tok - path);
    2164         640 :                         ptr = path + path_length;
    2165         640 :                         goto last_time;
    2166             :                 }
    2167             :         }
    2168             : 
    2169        1073 :         efree(path);
    2170        1073 :         *new_len = newpath_len;
    2171        1073 :         newpath[newpath_len] = '\0';
    2172        1073 :         return erealloc(newpath, newpath_len + 1);
    2173             : }
    2174             : /* }}} */
    2175             : 
    2176             : /**
    2177             :  * Process a phar stream name, ensuring we can handle any of:
    2178             :  *
    2179             :  * - whatever.phar
    2180             :  * - whatever.phar.gz
    2181             :  * - whatever.phar.bz2
    2182             :  * - whatever.phar.php
    2183             :  *
    2184             :  * Optionally the name might start with 'phar://'
    2185             :  *
    2186             :  * This is used by phar_parse_url()
    2187             :  */
    2188        2556 : int phar_split_fname(const char *filename, int filename_len, char **arch, int *arch_len, char **entry, int *entry_len, int executable, int for_create) /* {{{ */
    2189             : {
    2190             :         const char *ext_str;
    2191             : #ifdef PHP_WIN32
    2192             :         char *save;
    2193             : #endif
    2194             :         int ext_len;
    2195             : 
    2196        2556 :         if (!strncasecmp(filename, "phar://", 7)) {
    2197        2099 :                 filename += 7;
    2198        2099 :                 filename_len -= 7;
    2199             :         }
    2200             : 
    2201        2556 :         ext_len = 0;
    2202             : #ifdef PHP_WIN32
    2203             :         save = filename;
    2204             :         filename = estrndup(filename, filename_len);
    2205             :         phar_unixify_path_separators(filename, filename_len);
    2206             : #endif
    2207        2556 :         if (phar_detect_phar_fname_ext(filename, filename_len, &ext_str, &ext_len, executable, for_create, 0) == FAILURE) {
    2208          98 :                 if (ext_len != -1) {
    2209          34 :                         if (!ext_str) {
    2210             :                                 /* no / detected, restore arch for error message */
    2211             : #ifdef PHP_WIN32
    2212             :                                 *arch = save;
    2213             : #else
    2214          20 :                                 *arch = (char*)filename;
    2215             : #endif
    2216             :                         }
    2217             : 
    2218             : #ifdef PHP_WIN32
    2219             :                         efree(filename);
    2220             : #endif
    2221          34 :                         return FAILURE;
    2222             :                 }
    2223             : 
    2224          64 :                 ext_len = 0;
    2225             :                 /* no extension detected - instead we are dealing with an alias */
    2226             :         }
    2227             : 
    2228        2522 :         *arch_len = ext_str - filename + ext_len;
    2229        2522 :         *arch = estrndup(filename, *arch_len);
    2230             : 
    2231        2522 :         if (ext_str[ext_len]) {
    2232        2057 :                 *entry_len = filename_len - *arch_len;
    2233        2057 :                 *entry = estrndup(ext_str+ext_len, *entry_len);
    2234             : #ifdef PHP_WIN32
    2235             :                 phar_unixify_path_separators(*entry, *entry_len);
    2236             : #endif
    2237        2057 :                 *entry = phar_fix_filepath(*entry, entry_len, 0);
    2238             :         } else {
    2239         465 :                 *entry_len = 1;
    2240         465 :                 *entry = estrndup("/", 1);
    2241             :         }
    2242             : 
    2243             : #ifdef PHP_WIN32
    2244             :         efree(filename);
    2245             : #endif
    2246             : 
    2247        2522 :         return SUCCESS;
    2248             : }
    2249             : /* }}} */
    2250             : 
    2251             : /**
    2252             :  * Invoked when a user calls Phar::mapPhar() from within an executing .phar
    2253             :  * to set up its manifest directly
    2254             :  */
    2255         135 : int phar_open_executed_filename(char *alias, int alias_len, char **error) /* {{{ */
    2256             : {
    2257             :         char *fname;
    2258             :         php_stream *fp;
    2259             :         int fname_len;
    2260         135 :         zend_string *actual = NULL;
    2261             :         int ret;
    2262             : 
    2263         135 :         if (error) {
    2264         135 :                 *error = NULL;
    2265             :         }
    2266             : 
    2267         135 :         fname = (char*)zend_get_executed_filename();
    2268         135 :         fname_len = strlen(fname);
    2269             : 
    2270         135 :         if (phar_open_parsed_phar(fname, fname_len, alias, alias_len, 0, REPORT_ERRORS, NULL, 0) == SUCCESS) {
    2271          62 :                 return SUCCESS;
    2272             :         }
    2273             : 
    2274          73 :         if (!strcmp(fname, "[no active file]")) {
    2275           0 :                 if (error) {
    2276           0 :                         spprintf(error, 0, "cannot initialize a phar outside of PHP execution");
    2277             :                 }
    2278           0 :                 return FAILURE;
    2279             :         }
    2280             : 
    2281          73 :         if (0 == zend_get_constant_str("__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1)) {
    2282           1 :                 if (error) {
    2283           1 :                         spprintf(error, 0, "__HALT_COMPILER(); must be declared in a phar");
    2284             :                 }
    2285           1 :                 return FAILURE;
    2286             :         }
    2287             : 
    2288             : 
    2289             : #if PHP_API_VERSION < 20100412
    2290             :         if (PG(safe_mode) && (!php_checkuid(fname, NULL, CHECKUID_ALLOW_ONLY_FILE))) {
    2291             :                 return FAILURE;
    2292             :         }
    2293             : #endif
    2294             : 
    2295          72 :         if (php_check_open_basedir(fname)) {
    2296           0 :                 return FAILURE;
    2297             :         }
    2298             : 
    2299          72 :         fp = php_stream_open_wrapper(fname, "rb", IGNORE_URL|STREAM_MUST_SEEK|REPORT_ERRORS, &actual);
    2300             : 
    2301          72 :         if (!fp) {
    2302           0 :                 if (error) {
    2303           0 :                         spprintf(error, 0, "unable to open phar for reading \"%s\"", fname);
    2304             :                 }
    2305           0 :                 if (actual) {
    2306           0 :                         zend_string_release(actual);
    2307             :                 }
    2308           0 :                 return FAILURE;
    2309             :         }
    2310             : 
    2311          72 :         if (actual) {
    2312          72 :                 fname = ZSTR_VAL(actual);
    2313          72 :                 fname_len = ZSTR_LEN(actual);
    2314             :         }
    2315             : 
    2316          72 :         ret = phar_open_from_fp(fp, fname, fname_len, alias, alias_len, REPORT_ERRORS, NULL, 0, error);
    2317             : 
    2318          72 :         if (actual) {
    2319          72 :                 zend_string_release(actual);
    2320             :         }
    2321             : 
    2322          72 :         return ret;
    2323             : }
    2324             : /* }}} */
    2325             : 
    2326             : /**
    2327             :  * Validate the CRC32 of a file opened from within the phar
    2328             :  */
    2329        1244 : int phar_postprocess_file(phar_entry_data *idata, php_uint32 crc32, char **error, int process_zip) /* {{{ */
    2330             : {
    2331        1244 :         php_uint32 crc = ~0;
    2332        1244 :         int len = idata->internal_file->uncompressed_filesize;
    2333        1244 :         php_stream *fp = idata->fp;
    2334        1244 :         phar_entry_info *entry = idata->internal_file;
    2335             : 
    2336        1244 :         if (error) {
    2337        1244 :                 *error = NULL;
    2338             :         }
    2339             : 
    2340        1244 :         if (entry->is_zip && process_zip > 0) {
    2341             :                 /* verify local file header */
    2342             :                 phar_zip_file_header local;
    2343             :                 phar_zip_data_desc desc;
    2344             : 
    2345         175 :                 if (SUCCESS != phar_open_archive_fp(idata->phar)) {
    2346           0 :                         spprintf(error, 0, "phar error: unable to open zip-based phar archive \"%s\" to verify local file header for file \"%s\"", idata->phar->fname, entry->filename);
    2347           0 :                         return FAILURE;
    2348             :                 }
    2349         175 :                 php_stream_seek(phar_get_entrypfp(idata->internal_file), entry->header_offset, SEEK_SET);
    2350             : 
    2351         175 :                 if (sizeof(local) != php_stream_read(phar_get_entrypfp(idata->internal_file), (char *) &local, sizeof(local))) {
    2352             : 
    2353           0 :                         spprintf(error, 0, "phar error: internal corruption of zip-based phar \"%s\" (cannot read local file header for file \"%s\")", idata->phar->fname, entry->filename);
    2354           0 :                         return FAILURE;
    2355             :                 }
    2356             : 
    2357             :                 /* check for data descriptor */
    2358         175 :                 if (((PHAR_ZIP_16(local.flags)) & 0x8) == 0x8) {
    2359           1 :                         php_stream_seek(phar_get_entrypfp(idata->internal_file),
    2360             :                                         entry->header_offset + sizeof(local) +
    2361             :                                         PHAR_ZIP_16(local.filename_len) +
    2362             :                                         PHAR_ZIP_16(local.extra_len) +
    2363             :                                         entry->compressed_filesize, SEEK_SET);
    2364           1 :                         if (sizeof(desc) != php_stream_read(phar_get_entrypfp(idata->internal_file),
    2365             :                                                             (char *) &desc, sizeof(desc))) {
    2366           0 :                                 spprintf(error, 0, "phar error: internal corruption of zip-based phar \"%s\" (cannot read local data descriptor for file \"%s\")", idata->phar->fname, entry->filename);
    2367           0 :                                 return FAILURE;
    2368             :                         }
    2369           2 :                         if (desc.signature[0] == 'P' && desc.signature[1] == 'K') {
    2370           1 :                                 memcpy(&(local.crc32), &(desc.crc32), 12);
    2371             :                         } else {
    2372             :                                 /* old data descriptors have no signature */
    2373           0 :                                 memcpy(&(local.crc32), &desc, 12);
    2374             :                         }
    2375             :                 }
    2376             :                 /* verify local header */
    2377         175 :                 if (entry->filename_len != PHAR_ZIP_16(local.filename_len) || entry->crc32 != PHAR_ZIP_32(local.crc32) || entry->uncompressed_filesize != PHAR_ZIP_32(local.uncompsize) || entry->compressed_filesize != PHAR_ZIP_32(local.compsize)) {
    2378           0 :                         spprintf(error, 0, "phar error: internal corruption of zip-based phar \"%s\" (local header of file \"%s\" does not match central directory)", idata->phar->fname, entry->filename);
    2379           0 :                         return FAILURE;
    2380             :                 }
    2381             : 
    2382             :                 /* construct actual offset to file start - local extra_len can be different from central extra_len */
    2383         175 :                 entry->offset = entry->offset_abs =
    2384         175 :                         sizeof(local) + entry->header_offset + PHAR_ZIP_16(local.filename_len) + PHAR_ZIP_16(local.extra_len);
    2385             : 
    2386         175 :                 if (idata->zero && idata->zero != entry->offset_abs) {
    2387           2 :                         idata->zero = entry->offset_abs;
    2388             :                 }
    2389             :         }
    2390             : 
    2391        1244 :         if (process_zip == 1) {
    2392        1054 :                 return SUCCESS;
    2393             :         }
    2394             : 
    2395         190 :         php_stream_seek(fp, idata->zero, SEEK_SET);
    2396             : 
    2397       17992 :         while (len--) {
    2398       17612 :                 CRC32(crc, php_stream_getc(fp));
    2399             :         }
    2400             : 
    2401         190 :         php_stream_seek(fp, idata->zero, SEEK_SET);
    2402             : 
    2403         190 :         if (~crc == crc32) {
    2404         188 :                 entry->is_crc_checked = 1;
    2405         188 :                 return SUCCESS;
    2406             :         } else {
    2407           2 :                 spprintf(error, 0, "phar error: internal corruption of phar \"%s\" (crc32 mismatch on file \"%s\")", idata->phar->fname, entry->filename);
    2408           2 :                 return FAILURE;
    2409             :         }
    2410             : }
    2411             : /* }}} */
    2412             : 
    2413      127682 : static inline void phar_set_32(char *buffer, int var) /* {{{ */
    2414             : {
    2415             : #ifdef WORDS_BIGENDIAN
    2416             :         *((buffer) + 3) = (unsigned char) (((var) >> 24) & 0xFF);
    2417             :         *((buffer) + 2) = (unsigned char) (((var) >> 16) & 0xFF);
    2418             :         *((buffer) + 1) = (unsigned char) (((var) >> 8) & 0xFF);
    2419             :         *((buffer) + 0) = (unsigned char) ((var) & 0xFF);
    2420             : #else
    2421      127682 :          memcpy(buffer, &var, sizeof(var));
    2422             : #endif
    2423      127682 : } /* }}} */
    2424             : 
    2425       14390 : static int phar_flush_clean_deleted_apply(zval *zv) /* {{{ */
    2426             : {
    2427       14390 :         phar_entry_info *entry = (phar_entry_info *)Z_PTR_P(zv);
    2428             : 
    2429       14390 :         if (entry->fp_refcount <= 0 && entry->is_deleted) {
    2430          10 :                 return ZEND_HASH_APPLY_REMOVE;
    2431             :         } else {
    2432       14380 :                 return ZEND_HASH_APPLY_KEEP;
    2433             :         }
    2434             : }
    2435             : /* }}} */
    2436             : 
    2437             : #include "stub.h"
    2438             : 
    2439          97 : zend_string *phar_create_default_stub(const char *index_php, const char *web_index, char **error) /* {{{ */
    2440             : {
    2441             :         int index_len, web_len;
    2442             : 
    2443          97 :         if (error) {
    2444          16 :                 *error = NULL;
    2445             :         }
    2446             : 
    2447          97 :         if (!index_php) {
    2448          87 :                 index_php = "index.php";
    2449             :         }
    2450             : 
    2451          97 :         if (!web_index) {
    2452          93 :                 web_index = "index.php";
    2453             :         }
    2454             : 
    2455          97 :         index_len = strlen(index_php);
    2456          97 :         web_len = strlen(web_index);
    2457             : 
    2458          97 :         if (index_len > 400) {
    2459             :                 /* ridiculous size not allowed for index.php startup filename */
    2460           2 :                 if (error) {
    2461           2 :                         spprintf(error, 0, "Illegal filename passed in for stub creation, was %d characters long, and only 400 or less is allowed", index_len);
    2462           2 :                         return NULL;
    2463             :                 }
    2464             :         }
    2465             : 
    2466          95 :         if (web_len > 400) {
    2467             :                 /* ridiculous size not allowed for index.php startup filename */
    2468           1 :                 if (error) {
    2469           1 :                         spprintf(error, 0, "Illegal web filename passed in for stub creation, was %d characters long, and only 400 or less is allowed", web_len);
    2470           1 :                         return NULL;
    2471             :                 }
    2472             :         }
    2473             : 
    2474          94 :         return phar_get_stub(index_php, web_index, index_len+1, web_len+1);
    2475             : }
    2476             : /* }}} */
    2477             : 
    2478             : /**
    2479             :  * Save phar contents to disk
    2480             :  *
    2481             :  * user_stub contains either a string, or a resource pointer, if len is a negative length.
    2482             :  * user_stub and len should be both 0 if the default or existing stub should be used
    2483             :  */
    2484        4920 : int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int convert, char **error) /* {{{ */
    2485             : {
    2486        4920 :         char halt_stub[] = "__HALT_COMPILER();";
    2487             :         zend_string *newstub;
    2488             :         char *tmp;
    2489             :         phar_entry_info *entry, *newentry;
    2490        4920 :         int halt_offset, restore_alias_len, global_flags = 0, closeoldfile;
    2491        4920 :         char *pos, has_dirs = 0;
    2492             :         char manifest[18], entry_buffer[24];
    2493             :         zend_off_t manifest_ftell;
    2494             :         zend_long offset;
    2495             :         size_t wrote;
    2496             :         php_uint32 manifest_len, mytime, loc, new_manifest_count;
    2497             :         php_uint32 newcrc32;
    2498             :         php_stream *file, *oldfile, *newfile, *stubfile;
    2499             :         php_stream_filter *filter;
    2500             :         php_serialize_data_t metadata_hash;
    2501        4920 :         smart_str main_metadata_str = {0};
    2502        4920 :         int free_user_stub, free_fp = 1, free_ufp = 1;
    2503        4920 :         int manifest_hack = 0;
    2504             : 
    2505        4920 :         if (phar->is_persistent) {
    2506           0 :                 if (error) {
    2507           0 :                         spprintf(error, 0, "internal error: attempt to flush cached zip-based phar \"%s\"", phar->fname);
    2508             :                 }
    2509           0 :                 return EOF;
    2510             :         }
    2511             : 
    2512        4920 :         if (error) {
    2513        4920 :                 *error = NULL;
    2514             :         }
    2515             : 
    2516        4920 :         if (!zend_hash_num_elements(&phar->manifest) && !user_stub) {
    2517          14 :                 return EOF;
    2518             :         }
    2519             : 
    2520        4906 :         zend_hash_clean(&phar->virtual_dirs);
    2521             : 
    2522        4906 :         if (phar->is_zip) {
    2523         203 :                 return phar_zip_flush(phar, user_stub, len, convert, error);
    2524             :         }
    2525             : 
    2526        4703 :         if (phar->is_tar) {
    2527         193 :                 return phar_tar_flush(phar, user_stub, len, convert, error);
    2528             :         }
    2529             : 
    2530        4510 :         if (PHAR_G(readonly)) {
    2531           0 :                 return EOF;
    2532             :         }
    2533             : 
    2534        8932 :         if (phar->fp && !phar->is_brandnew) {
    2535        4422 :                 oldfile = phar->fp;
    2536        4422 :                 closeoldfile = 0;
    2537        4422 :                 php_stream_rewind(oldfile);
    2538             :         } else {
    2539          88 :                 oldfile = php_stream_open_wrapper(phar->fname, "rb", 0, NULL);
    2540          88 :                 closeoldfile = oldfile != NULL;
    2541             :         }
    2542        4510 :         newfile = php_stream_fopen_tmpfile();
    2543        4510 :         if (!newfile) {
    2544           0 :                 if (error) {
    2545           0 :                         spprintf(error, 0, "unable to create temporary file");
    2546             :                 }
    2547           0 :                 if (closeoldfile) {
    2548           0 :                         php_stream_close(oldfile);
    2549             :                 }
    2550           0 :                 return EOF;
    2551             :         }
    2552             : 
    2553        4510 :         if (user_stub) {
    2554             :                 zend_string *suser_stub;
    2555          29 :                 if (len < 0) {
    2556             :                         /* resource passed in */
    2557           4 :                         if (!(php_stream_from_zval_no_verify(stubfile, (zval *)user_stub))) {
    2558           0 :                                 if (closeoldfile) {
    2559           0 :                                         php_stream_close(oldfile);
    2560             :                                 }
    2561           0 :                                 php_stream_close(newfile);
    2562           0 :                                 if (error) {
    2563           0 :                                         spprintf(error, 0, "unable to access resource to copy stub to new phar \"%s\"", phar->fname);
    2564             :                                 }
    2565           0 :                                 return EOF;
    2566             :                         }
    2567           4 :                         if (len == -1) {
    2568           3 :                                 len = PHP_STREAM_COPY_ALL;
    2569             :                         } else {
    2570           1 :                                 len = -len;
    2571             :                         }
    2572           4 :                         user_stub = 0;
    2573             : 
    2574           4 :                         if (!(suser_stub = php_stream_copy_to_mem(stubfile, len, 0))) {
    2575           0 :                                 if (closeoldfile) {
    2576           0 :                                         php_stream_close(oldfile);
    2577             :                                 }
    2578           0 :                                 php_stream_close(newfile);
    2579           0 :                                 if (error) {
    2580           0 :                                         spprintf(error, 0, "unable to read resource to copy stub to new phar \"%s\"", phar->fname);
    2581             :                                 }
    2582           0 :                                 return EOF;
    2583             :                         }
    2584           4 :                         free_user_stub = 1;
    2585           4 :                         user_stub = ZSTR_VAL(suser_stub);
    2586           4 :                         len = ZSTR_LEN(suser_stub);
    2587             :                 } else {
    2588          25 :                         free_user_stub = 0;
    2589             :                 }
    2590          29 :                 tmp = estrndup(user_stub, len);
    2591          29 :                 if ((pos = php_stristr(tmp, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) {
    2592           1 :                         efree(tmp);
    2593           1 :                         if (closeoldfile) {
    2594           0 :                                 php_stream_close(oldfile);
    2595             :                         }
    2596           1 :                         php_stream_close(newfile);
    2597           1 :                         if (error) {
    2598           1 :                                 spprintf(error, 0, "illegal stub for phar \"%s\"", phar->fname);
    2599             :                         }
    2600           1 :                         if (free_user_stub) {
    2601             :                                 zend_string_free(suser_stub);
    2602             :                         }
    2603           1 :                         return EOF;
    2604             :                 }
    2605          28 :                 pos = user_stub + (pos - tmp);
    2606          28 :                 efree(tmp);
    2607          28 :                 len = pos - user_stub + 18;
    2608          56 :                 if ((size_t)len != php_stream_write(newfile, user_stub, len)
    2609          56 :                 ||                        5 != php_stream_write(newfile, " ?>\r\n", 5)) {
    2610           0 :                         if (closeoldfile) {
    2611           0 :                                 php_stream_close(oldfile);
    2612             :                         }
    2613           0 :                         php_stream_close(newfile);
    2614           0 :                         if (error) {
    2615           0 :                                 spprintf(error, 0, "unable to create stub from string in new phar \"%s\"", phar->fname);
    2616             :                         }
    2617           0 :                         if (free_user_stub) {
    2618             :                                 zend_string_free(suser_stub);
    2619             :                         }
    2620           0 :                         return EOF;
    2621             :                 }
    2622          28 :                 phar->halt_offset = len + 5;
    2623          28 :                 if (free_user_stub) {
    2624             :                         zend_string_free(suser_stub);
    2625             :                 }
    2626             :         } else {
    2627             :                 size_t written;
    2628             : 
    2629        8881 :                 if (!user_stub && phar->halt_offset && oldfile && !phar->is_brandnew) {
    2630        4400 :                         php_stream_copy_to_stream_ex(oldfile, newfile, phar->halt_offset, &written);
    2631        4400 :                         newstub = NULL;
    2632             :                 } else {
    2633             :                         /* this is either a brand new phar or a default stub overwrite */
    2634          81 :                         newstub = phar_create_default_stub(NULL, NULL, NULL);
    2635          81 :                         phar->halt_offset = ZSTR_LEN(newstub);
    2636          81 :                         written = php_stream_write(newfile, ZSTR_VAL(newstub), phar->halt_offset);
    2637             :                 }
    2638        4481 :                 if (phar->halt_offset != written) {
    2639           0 :                         if (closeoldfile) {
    2640           0 :                                 php_stream_close(oldfile);
    2641             :                         }
    2642           0 :                         php_stream_close(newfile);
    2643           0 :                         if (error) {
    2644           0 :                                 if (newstub) {
    2645           0 :                                         spprintf(error, 0, "unable to create stub in new phar \"%s\"", phar->fname);
    2646             :                                 } else {
    2647           0 :                                         spprintf(error, 0, "unable to copy stub of old phar to new phar \"%s\"", phar->fname);
    2648             :                                 }
    2649             :                         }
    2650           0 :                         if (newstub) {
    2651             :                                 zend_string_free(newstub);
    2652             :                         }
    2653           0 :                         return EOF;
    2654             :                 }
    2655        4481 :                 if (newstub) {
    2656             :                         zend_string_free(newstub);
    2657             :                 }
    2658             :         }
    2659        4509 :         manifest_ftell = php_stream_tell(newfile);
    2660        4509 :         halt_offset = manifest_ftell;
    2661             : 
    2662             :         /* Check whether we can get rid of some of the deleted entries which are
    2663             :          * unused. However some might still be in use so even after this clean-up
    2664             :          * we need to skip entries marked is_deleted. */
    2665        4509 :         zend_hash_apply(&phar->manifest, phar_flush_clean_deleted_apply);
    2666             : 
    2667             :         /* compress as necessary, calculate crcs, serialize meta-data, manifest size, and file sizes */
    2668        4509 :         main_metadata_str.s = NULL;
    2669        9018 :         if (Z_TYPE(phar->metadata) != IS_UNDEF) {
    2670          13 :                 PHP_VAR_SERIALIZE_INIT(metadata_hash);
    2671          13 :                 php_var_serialize(&main_metadata_str, &phar->metadata, &metadata_hash);
    2672          13 :                 PHP_VAR_SERIALIZE_DESTROY(metadata_hash);
    2673             :         }
    2674        4509 :         new_manifest_count = 0;
    2675        4509 :         offset = 0;
    2676       23398 :         for (zend_hash_internal_pointer_reset(&phar->manifest);
    2677       18889 :                 zend_hash_has_more_elements(&phar->manifest) == SUCCESS;
    2678       14380 :                 zend_hash_move_forward(&phar->manifest)) {
    2679       28760 :                 if ((entry = zend_hash_get_current_data_ptr(&phar->manifest)) == NULL) {
    2680           0 :                         continue;
    2681             :                 }
    2682       14380 :                 if (entry->cfp) {
    2683             :                         /* did we forget to get rid of cfp last time? */
    2684           0 :                         php_stream_close(entry->cfp);
    2685           0 :                         entry->cfp = 0;
    2686             :                 }
    2687       14380 :                 if (entry->is_deleted || entry->is_mounted) {
    2688             :                         /* remove this from the new phar */
    2689           5 :                         continue;
    2690             :                 }
    2691       14375 :                 if (!entry->is_modified && entry->fp_refcount) {
    2692             :                         /* open file pointers refer to this fp, do not free the stream */
    2693           2 :                         switch (entry->fp_type) {
    2694             :                                 case PHAR_FP:
    2695           2 :                                         free_fp = 0;
    2696           2 :                                         break;
    2697             :                                 case PHAR_UFP:
    2698           0 :                                         free_ufp = 0;
    2699             :                                 default:
    2700             :                                         break;
    2701             :                         }
    2702             :                 }
    2703             :                 /* after excluding deleted files, calculate manifest size in bytes and number of entries */
    2704       14375 :                 ++new_manifest_count;
    2705       14375 :                 phar_add_virtual_dirs(phar, entry->filename, entry->filename_len);
    2706             : 
    2707       14375 :                 if (entry->is_dir) {
    2708             :                         /* we use this to calculate API version, 1.1.1 is used for phars with directories */
    2709          27 :                         has_dirs = 1;
    2710             :                 }
    2711       28750 :                 if (Z_TYPE(entry->metadata) != IS_UNDEF) {
    2712          78 :                         if (entry->metadata_str.s) {
    2713          53 :                                 smart_str_free(&entry->metadata_str);
    2714             :                         }
    2715          78 :                         entry->metadata_str.s = NULL;
    2716          78 :                         PHP_VAR_SERIALIZE_INIT(metadata_hash);
    2717          78 :                         php_var_serialize(&entry->metadata_str, &entry->metadata, &metadata_hash);
    2718          78 :                         PHP_VAR_SERIALIZE_DESTROY(metadata_hash);
    2719             :                 } else {
    2720       14297 :                         if (entry->metadata_str.s) {
    2721           2 :                                 smart_str_free(&entry->metadata_str);
    2722             :                         }
    2723       14297 :                         entry->metadata_str.s = NULL;
    2724             :                 }
    2725             : 
    2726             :                 /* 32 bits for filename length, length of filename, manifest + metadata, and add 1 for trailing / if a directory */
    2727       14375 :                 offset += 4 + entry->filename_len + sizeof(entry_buffer) + (entry->metadata_str.s ? ZSTR_LEN(entry->metadata_str.s) : 0) + (entry->is_dir ? 1 : 0);
    2728             : 
    2729             :                 /* compress and rehash as necessary */
    2730       14375 :                 if ((oldfile && !entry->is_modified) || entry->is_dir) {
    2731        6848 :                         if (entry->fp_type == PHAR_UFP) {
    2732             :                                 /* reset so we can copy the compressed data over */
    2733           1 :                                 entry->fp_type = PHAR_FP;
    2734             :                         }
    2735        6848 :                         continue;
    2736             :                 }
    2737        7527 :                 if (!phar_get_efp(entry, 0)) {
    2738             :                         /* re-open internal file pointer just-in-time */
    2739           1 :                         newentry = phar_open_jit(phar, entry, error);
    2740           1 :                         if (!newentry) {
    2741             :                                 /* major problem re-opening, so we ignore this file and the error */
    2742           1 :                                 efree(*error);
    2743           1 :                                 *error = NULL;
    2744           1 :                                 continue;
    2745             :                         }
    2746           0 :                         entry = newentry;
    2747             :                 }
    2748        7526 :                 file = phar_get_efp(entry, 0);
    2749        7526 :                 if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 1)) {
    2750           0 :                         if (closeoldfile) {
    2751           0 :                                 php_stream_close(oldfile);
    2752             :                         }
    2753           0 :                         php_stream_close(newfile);
    2754           0 :                         if (error) {
    2755           0 :                                 spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
    2756             :                         }
    2757           0 :                         return EOF;
    2758             :                 }
    2759        7526 :                 newcrc32 = ~0;
    2760        7526 :                 mytime = entry->uncompressed_filesize;
    2761       98547 :                 for (loc = 0;loc < mytime; ++loc) {
    2762       91021 :                         CRC32(newcrc32, php_stream_getc(file));
    2763             :                 }
    2764        7526 :                 entry->crc32 = ~newcrc32;
    2765        7526 :                 entry->is_crc_checked = 1;
    2766        7526 :                 if (!(entry->flags & PHAR_ENT_COMPRESSION_MASK)) {
    2767             :                         /* not compressed */
    2768        7495 :                         entry->compressed_filesize = entry->uncompressed_filesize;
    2769        7495 :                         continue;
    2770             :                 }
    2771          31 :                 filter = php_stream_filter_create(phar_compress_filter(entry, 0), NULL, 0);
    2772          31 :                 if (!filter) {
    2773           0 :                         if (closeoldfile) {
    2774           0 :                                 php_stream_close(oldfile);
    2775             :                         }
    2776           0 :                         php_stream_close(newfile);
    2777           0 :                         if (entry->flags & PHAR_ENT_COMPRESSED_GZ) {
    2778           0 :                                 if (error) {
    2779           0 :                                         spprintf(error, 0, "unable to gzip compress file \"%s\" to new phar \"%s\"", entry->filename, phar->fname);
    2780             :                                 }
    2781             :                         } else {
    2782           0 :                                 if (error) {
    2783           0 :                                         spprintf(error, 0, "unable to bzip2 compress file \"%s\" to new phar \"%s\"", entry->filename, phar->fname);
    2784             :                                 }
    2785             :                         }
    2786           0 :                         return EOF;
    2787             :                 }
    2788             : 
    2789             :                 /* create new file that holds the compressed version */
    2790             :                 /* work around inability to specify freedom in write and strictness
    2791             :                 in read count */
    2792          31 :                 entry->cfp = php_stream_fopen_tmpfile();
    2793          31 :                 if (!entry->cfp) {
    2794           0 :                         if (error) {
    2795           0 :                                 spprintf(error, 0, "unable to create temporary file");
    2796             :                         }
    2797           0 :                         if (closeoldfile) {
    2798           0 :                                 php_stream_close(oldfile);
    2799             :                         }
    2800           0 :                         php_stream_close(newfile);
    2801           0 :                         return EOF;
    2802             :                 }
    2803          31 :                 php_stream_flush(file);
    2804          31 :                 if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 0)) {
    2805           0 :                         if (closeoldfile) {
    2806           0 :                                 php_stream_close(oldfile);
    2807             :                         }
    2808           0 :                         php_stream_close(newfile);
    2809           0 :                         if (error) {
    2810           0 :                                 spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
    2811             :                         }
    2812           0 :                         return EOF;
    2813             :                 }
    2814          31 :                 php_stream_filter_append((&entry->cfp->writefilters), filter);
    2815          31 :                 if (SUCCESS != php_stream_copy_to_stream_ex(file, entry->cfp, entry->uncompressed_filesize, NULL)) {
    2816           0 :                         if (closeoldfile) {
    2817           0 :                                 php_stream_close(oldfile);
    2818             :                         }
    2819           0 :                         php_stream_close(newfile);
    2820           0 :                         if (error) {
    2821           0 :                                 spprintf(error, 0, "unable to copy compressed file contents of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
    2822             :                         }
    2823           0 :                         return EOF;
    2824             :                 }
    2825          31 :                 php_stream_filter_flush(filter, 1);
    2826          31 :                 php_stream_flush(entry->cfp);
    2827          31 :                 php_stream_filter_remove(filter, 1);
    2828          31 :                 php_stream_seek(entry->cfp, 0, SEEK_END);
    2829          31 :                 entry->compressed_filesize = (php_uint32) php_stream_tell(entry->cfp);
    2830             :                 /* generate crc on compressed file */
    2831          31 :                 php_stream_rewind(entry->cfp);
    2832          31 :                 entry->old_flags = entry->flags;
    2833          31 :                 entry->is_modified = 1;
    2834          31 :                 global_flags |= (entry->flags & PHAR_ENT_COMPRESSION_MASK);
    2835             :         }
    2836        4509 :         global_flags |= PHAR_HDR_SIGNATURE;
    2837             : 
    2838             :         /* write out manifest pre-header */
    2839             :         /*  4: manifest length
    2840             :          *  4: manifest entry count
    2841             :          *  2: phar version
    2842             :          *  4: phar global flags
    2843             :          *  4: alias length
    2844             :          *  ?: the alias itself
    2845             :          *  4: phar metadata length
    2846             :          *  ?: phar metadata
    2847             :          */
    2848        4509 :         restore_alias_len = phar->alias_len;
    2849        4509 :         if (phar->is_temporary_alias) {
    2850         305 :                 phar->alias_len = 0;
    2851             :         }
    2852             : 
    2853        4509 :         manifest_len = offset + phar->alias_len + sizeof(manifest) + (main_metadata_str.s ? ZSTR_LEN(main_metadata_str.s) : 0);
    2854        4509 :         phar_set_32(manifest, manifest_len);
    2855             :         /* Hack - see bug #65028, add padding byte to the end of the manifest */
    2856        4509 :         if(manifest[0] == '\r' || manifest[0] == '\n') {
    2857           3 :                 manifest_len++;
    2858           3 :                 phar_set_32(manifest, manifest_len);
    2859           3 :                 manifest_hack = 1;
    2860             :         }
    2861        4509 :         phar_set_32(manifest+4, new_manifest_count);
    2862        4509 :         if (has_dirs) {
    2863          26 :                 *(manifest + 8) = (unsigned char) (((PHAR_API_VERSION) >> 8) & 0xFF);
    2864          26 :                 *(manifest + 9) = (unsigned char) (((PHAR_API_VERSION) & 0xF0));
    2865             :         } else {
    2866        4483 :                 *(manifest + 8) = (unsigned char) (((PHAR_API_VERSION_NODIR) >> 8) & 0xFF);
    2867        4483 :                 *(manifest + 9) = (unsigned char) (((PHAR_API_VERSION_NODIR) & 0xF0));
    2868             :         }
    2869        4509 :         phar_set_32(manifest+10, global_flags);
    2870        4509 :         phar_set_32(manifest+14, phar->alias_len);
    2871             : 
    2872             :         /* write the manifest header */
    2873        9018 :         if (sizeof(manifest) != php_stream_write(newfile, manifest, sizeof(manifest))
    2874        9018 :         || (size_t)phar->alias_len != php_stream_write(newfile, phar->alias, phar->alias_len)) {
    2875             : 
    2876           0 :                 if (closeoldfile) {
    2877           0 :                         php_stream_close(oldfile);
    2878             :                 }
    2879             : 
    2880           0 :                 php_stream_close(newfile);
    2881           0 :                 phar->alias_len = restore_alias_len;
    2882             : 
    2883           0 :                 if (error) {
    2884           0 :                         spprintf(error, 0, "unable to write manifest header of new phar \"%s\"", phar->fname);
    2885             :                 }
    2886             : 
    2887           0 :                 return EOF;
    2888             :         }
    2889             : 
    2890        4509 :         phar->alias_len = restore_alias_len;
    2891             : 
    2892        4509 :         phar_set_32(manifest, main_metadata_str.s ? ZSTR_LEN(main_metadata_str.s) : 0);
    2893        4522 :         if (4 != php_stream_write(newfile, manifest, 4) || ((main_metadata_str.s ? ZSTR_LEN(main_metadata_str.s) : 0)
    2894          13 :         && ZSTR_LEN(main_metadata_str.s) != php_stream_write(newfile, ZSTR_VAL(main_metadata_str.s), ZSTR_LEN(main_metadata_str.s)))) {
    2895             :                 smart_str_free(&main_metadata_str);
    2896             : 
    2897           0 :                 if (closeoldfile) {
    2898           0 :                         php_stream_close(oldfile);
    2899             :                 }
    2900             : 
    2901           0 :                 php_stream_close(newfile);
    2902           0 :                 phar->alias_len = restore_alias_len;
    2903             : 
    2904           0 :                 if (error) {
    2905           0 :                         spprintf(error, 0, "unable to write manifest meta-data of new phar \"%s\"", phar->fname);
    2906             :                 }
    2907             : 
    2908           0 :                 return EOF;
    2909             :         }
    2910             :         smart_str_free(&main_metadata_str);
    2911             : 
    2912             :         /* re-calculate the manifest location to simplify later code */
    2913        4509 :         manifest_ftell = php_stream_tell(newfile);
    2914             : 
    2915             :         /* now write the manifest */
    2916       23398 :         for (zend_hash_internal_pointer_reset(&phar->manifest);
    2917       18889 :                 zend_hash_has_more_elements(&phar->manifest) == SUCCESS;
    2918       14380 :                 zend_hash_move_forward(&phar->manifest)) {
    2919             : 
    2920       28760 :                 if ((entry = zend_hash_get_current_data_ptr(&phar->manifest)) == NULL) {
    2921           0 :                         continue;
    2922             :                 }
    2923             : 
    2924       14380 :                 if (entry->is_deleted || entry->is_mounted) {
    2925             :                         /* remove this from the new phar if deleted, ignore if mounted */
    2926           5 :                         continue;
    2927             :                 }
    2928             : 
    2929       14375 :                 if (entry->is_dir) {
    2930             :                         /* add 1 for trailing slash */
    2931          27 :                         phar_set_32(entry_buffer, entry->filename_len + 1);
    2932             :                 } else {
    2933       14348 :                         phar_set_32(entry_buffer, entry->filename_len);
    2934             :                 }
    2935             : 
    2936       43152 :                 if (4 != php_stream_write(newfile, entry_buffer, 4)
    2937       43125 :                 || entry->filename_len != php_stream_write(newfile, entry->filename, entry->filename_len)
    2938       57527 :                 || (entry->is_dir && 1 != php_stream_write(newfile, "/", 1))) {
    2939           0 :                         if (closeoldfile) {
    2940           0 :                                 php_stream_close(oldfile);
    2941             :                         }
    2942           0 :                         php_stream_close(newfile);
    2943           0 :                         if (error) {
    2944           0 :                                 if (entry->is_dir) {
    2945           0 :                                         spprintf(error, 0, "unable to write filename of directory \"%s\" to manifest of new phar \"%s\"", entry->filename, phar->fname);
    2946             :                                 } else {
    2947           0 :                                         spprintf(error, 0, "unable to write filename of file \"%s\" to manifest of new phar \"%s\"", entry->filename, phar->fname);
    2948             :                                 }
    2949             :                         }
    2950           0 :                         return EOF;
    2951             :                 }
    2952             : 
    2953             :                 /* set the manifest meta-data:
    2954             :                         4: uncompressed filesize
    2955             :                         4: creation timestamp
    2956             :                         4: compressed filesize
    2957             :                         4: crc32
    2958             :                         4: flags
    2959             :                         4: metadata-len
    2960             :                         +: metadata
    2961             :                 */
    2962       14375 :                 mytime = time(NULL);
    2963       14375 :                 phar_set_32(entry_buffer, entry->uncompressed_filesize);
    2964       14375 :                 phar_set_32(entry_buffer+4, mytime);
    2965       14375 :                 phar_set_32(entry_buffer+8, entry->compressed_filesize);
    2966       14375 :                 phar_set_32(entry_buffer+12, entry->crc32);
    2967       14375 :                 phar_set_32(entry_buffer+16, entry->flags);
    2968       14375 :                 phar_set_32(entry_buffer+20, entry->metadata_str.s ? ZSTR_LEN(entry->metadata_str.s) : 0);
    2969             : 
    2970       28828 :                 if (sizeof(entry_buffer) != php_stream_write(newfile, entry_buffer, sizeof(entry_buffer))
    2971       28750 :                 || (entry->metadata_str.s &&
    2972          78 :                     ZSTR_LEN(entry->metadata_str.s) != php_stream_write(newfile, ZSTR_VAL(entry->metadata_str.s), ZSTR_LEN(entry->metadata_str.s)))) {
    2973           0 :                         if (closeoldfile) {
    2974           0 :                                 php_stream_close(oldfile);
    2975             :                         }
    2976             : 
    2977           0 :                         php_stream_close(newfile);
    2978             : 
    2979           0 :                         if (error) {
    2980           0 :                                 spprintf(error, 0, "unable to write temporary manifest of file \"%s\" to manifest of new phar \"%s\"", entry->filename, phar->fname);
    2981             :                         }
    2982             : 
    2983           0 :                         return EOF;
    2984             :                 }
    2985             :         }
    2986             :         /* Hack - see bug #65028, add padding byte to the end of the manifest */
    2987        4509 :         if(manifest_hack) {
    2988           3 :                 if(1 != php_stream_write(newfile, manifest, 1)) {
    2989           0 :                         if (closeoldfile) {
    2990           0 :                                 php_stream_close(oldfile);
    2991             :                         }
    2992             : 
    2993           0 :                         php_stream_close(newfile);
    2994             : 
    2995           0 :                         if (error) {
    2996           0 :                                 spprintf(error, 0, "unable to write manifest padding byte");
    2997             :                         }
    2998             : 
    2999           0 :                         return EOF;
    3000             :                 }
    3001             :         }
    3002             : 
    3003             :         /* now copy the actual file data to the new phar */
    3004        4509 :         offset = php_stream_tell(newfile);
    3005       23396 :         for (zend_hash_internal_pointer_reset(&phar->manifest);
    3006       18887 :                 zend_hash_has_more_elements(&phar->manifest) == SUCCESS;
    3007       14378 :                 zend_hash_move_forward(&phar->manifest)) {
    3008             : 
    3009       28758 :                 if ((entry = zend_hash_get_current_data_ptr(&phar->manifest)) == NULL) {
    3010           0 :                         continue;
    3011             :                 }
    3012             : 
    3013       14379 :                 if (entry->is_deleted || entry->is_dir || entry->is_mounted) {
    3014          32 :                         continue;
    3015             :                 }
    3016             : 
    3017       14347 :                 if (entry->cfp) {
    3018          31 :                         file = entry->cfp;
    3019          31 :                         php_stream_rewind(file);
    3020             :                 } else {
    3021       14316 :                         file = phar_get_efp(entry, 0);
    3022       14316 :                         if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 0)) {
    3023           1 :                                 if (closeoldfile) {
    3024           0 :                                         php_stream_close(oldfile);
    3025             :                                 }
    3026           1 :                                 php_stream_close(newfile);
    3027           1 :                                 if (error) {
    3028           1 :                                         spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
    3029             :                                 }
    3030           1 :                                 return EOF;
    3031             :                         }
    3032             :                 }
    3033             : 
    3034       14346 :                 if (!file) {
    3035           0 :                         if (closeoldfile) {
    3036           0 :                                 php_stream_close(oldfile);
    3037             :                         }
    3038           0 :                         php_stream_close(newfile);
    3039           0 :                         if (error) {
    3040           0 :                                 spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
    3041             :                         }
    3042           0 :                         return EOF;
    3043             :                 }
    3044             : 
    3045             :                 /* this will have changed for all files that have either changed compression or been modified */
    3046       14346 :                 entry->offset = entry->offset_abs = offset;
    3047       14346 :                 offset += entry->compressed_filesize;
    3048       14346 :                 if (php_stream_copy_to_stream_ex(file, newfile, entry->compressed_filesize, &wrote) == FAILURE) {
    3049           0 :                         if (closeoldfile) {
    3050           0 :                                 php_stream_close(oldfile);
    3051             :                         }
    3052             : 
    3053           0 :                         php_stream_close(newfile);
    3054             : 
    3055           0 :                         if (error) {
    3056           0 :                                 spprintf(error, 0, "unable to write contents of file \"%s\" to new phar \"%s\"", entry->filename, phar->fname);
    3057             :                         }
    3058             : 
    3059           0 :                         return EOF;
    3060             :                 }
    3061             : 
    3062       14346 :                 entry->is_modified = 0;
    3063             : 
    3064       14346 :                 if (entry->cfp) {
    3065          31 :                         php_stream_close(entry->cfp);
    3066          31 :                         entry->cfp = NULL;
    3067             :                 }
    3068             : 
    3069       14346 :                 if (entry->fp_type == PHAR_MOD) {
    3070             :                         /* this fp is in use by a phar_entry_data returned by phar_get_entry_data, it will be closed when the phar_entry_data is phar_entry_delref'ed */
    3071        4343 :                         if (entry->fp_refcount == 0 && entry->fp != phar->fp && entry->fp != phar->ufp) {
    3072        4307 :                                 php_stream_close(entry->fp);
    3073             :                         }
    3074             : 
    3075        4343 :                         entry->fp = NULL;
    3076        4343 :                         entry->fp_type = PHAR_FP;
    3077       10003 :                 } else if (entry->fp_type == PHAR_UFP) {
    3078        3092 :                         entry->fp_type = PHAR_FP;
    3079             :                 }
    3080             :         }
    3081             : 
    3082             :         /* append signature */
    3083        4508 :         if (global_flags & PHAR_HDR_SIGNATURE) {
    3084             :                 char sig_buf[4];
    3085             : 
    3086        4508 :                 php_stream_rewind(newfile);
    3087             : 
    3088        4508 :                 if (phar->signature) {
    3089        4390 :                         efree(phar->signature);
    3090        4390 :                         phar->signature = NULL;
    3091             :                 }
    3092             : 
    3093        4508 :                 switch(phar->sig_flags) {
    3094             : #ifndef PHAR_HASH_OK
    3095             :                         case PHAR_SIG_SHA512:
    3096             :                         case PHAR_SIG_SHA256:
    3097             :                                 if (closeoldfile) {
    3098             :                                         php_stream_close(oldfile);
    3099             :                                 }
    3100             :                                 php_stream_close(newfile);
    3101             :                                 if (error) {
    3102             :                                         spprintf(error, 0, "unable to write contents of file \"%s\" to new phar \"%s\" with requested hash type", entry->filename, phar->fname);
    3103             :                                 }
    3104             :                                 return EOF;
    3105             : #endif
    3106             :                         default: {
    3107        4508 :                                 char *digest = NULL;
    3108             :                                 int digest_len;
    3109             : 
    3110        4508 :                                 if (FAILURE == phar_create_signature(phar, newfile, &digest, &digest_len, error)) {
    3111           0 :                                         if (error) {
    3112           0 :                                                 char *save = *error;
    3113           0 :                                                 spprintf(error, 0, "phar error: unable to write signature: %s", save);
    3114           0 :                                                 efree(save);
    3115             :                                         }
    3116           0 :                                         if (digest) {
    3117           0 :                                                 efree(digest);
    3118             :                                         }
    3119           0 :                                         if (closeoldfile) {
    3120           0 :                                                 php_stream_close(oldfile);
    3121             :                                         }
    3122           0 :                                         php_stream_close(newfile);
    3123           0 :                                         return EOF;
    3124             :                                 }
    3125             : 
    3126        4508 :                                 php_stream_write(newfile, digest, digest_len);
    3127        4508 :                                 efree(digest);
    3128        4508 :                                 if (phar->sig_flags == PHAR_SIG_OPENSSL) {
    3129           1 :                                         phar_set_32(sig_buf, digest_len);
    3130           1 :                                         php_stream_write(newfile, sig_buf, 4);
    3131             :                                 }
    3132             :                                 break;
    3133             :                         }
    3134             :                 }
    3135        4508 :                 phar_set_32(sig_buf, phar->sig_flags);
    3136        4508 :                 php_stream_write(newfile, sig_buf, 4);
    3137        4508 :                 php_stream_write(newfile, "GBMB", 4);
    3138             :         }
    3139             : 
    3140             :         /* finally, close the temp file, rename the original phar,
    3141             :            move the temp to the old phar, unlink the old phar, and reload it into memory
    3142             :         */
    3143        4508 :         if (phar->fp && free_fp) {
    3144        4432 :                 php_stream_close(phar->fp);
    3145             :         }
    3146             : 
    3147        4508 :         if (phar->ufp) {
    3148          17 :                 if (free_ufp) {
    3149          17 :                         php_stream_close(phar->ufp);
    3150             :                 }
    3151          17 :                 phar->ufp = NULL;
    3152             :         }
    3153             : 
    3154        4508 :         if (closeoldfile) {
    3155          15 :                 php_stream_close(oldfile);
    3156             :         }
    3157             : 
    3158        4508 :         phar->internal_file_start = halt_offset + manifest_len + 4;
    3159        4508 :         phar->halt_offset = halt_offset;
    3160        4508 :         phar->is_brandnew = 0;
    3161             : 
    3162        4508 :         php_stream_rewind(newfile);
    3163             : 
    3164        4508 :         if (phar->donotflush) {
    3165             :                 /* deferred flush */
    3166          12 :                 phar->fp = newfile;
    3167             :         } else {
    3168        4496 :                 phar->fp = php_stream_open_wrapper(phar->fname, "w+b", IGNORE_URL|STREAM_MUST_SEEK|REPORT_ERRORS, NULL);
    3169        4496 :                 if (!phar->fp) {
    3170           0 :                         phar->fp = newfile;
    3171           0 :                         if (error) {
    3172           0 :                                 spprintf(error, 4096, "unable to open new phar \"%s\" for writing", phar->fname);
    3173             :                         }
    3174           0 :                         return EOF;
    3175             :                 }
    3176             : 
    3177        4496 :                 if (phar->flags & PHAR_FILE_COMPRESSED_GZ) {
    3178             :                         /* to properly compress, we have to tell zlib to add a zlib header */
    3179             :                         zval filterparams;
    3180             : 
    3181           5 :                         array_init(&filterparams);
    3182           5 :                         add_assoc_long(&filterparams, "window", MAX_WBITS+16);
    3183           5 :                         filter = php_stream_filter_create("zlib.deflate", &filterparams, php_stream_is_persistent(phar->fp));
    3184             :                         zval_dtor(&filterparams);
    3185             : 
    3186           5 :                         if (!filter) {
    3187           0 :                                 if (error) {
    3188           0 :                                         spprintf(error, 4096, "unable to compress all contents of phar \"%s\" using zlib, PHP versions older than 5.2.6 have a buggy zlib", phar->fname);
    3189             :                                 }
    3190           0 :                                 return EOF;
    3191             :                         }
    3192             : 
    3193           5 :                         php_stream_filter_append(&phar->fp->writefilters, filter);
    3194           5 :                         php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
    3195           5 :                         php_stream_filter_flush(filter, 1);
    3196           5 :                         php_stream_filter_remove(filter, 1);
    3197           5 :                         php_stream_close(phar->fp);
    3198             :                         /* use the temp stream as our base */
    3199           5 :                         phar->fp = newfile;
    3200        4491 :                 } else if (phar->flags & PHAR_FILE_COMPRESSED_BZ2) {
    3201           3 :                         filter = php_stream_filter_create("bzip2.compress", NULL, php_stream_is_persistent(phar->fp));
    3202           3 :                         php_stream_filter_append(&phar->fp->writefilters, filter);
    3203           3 :                         php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
    3204           3 :                         php_stream_filter_flush(filter, 1);
    3205           3 :                         php_stream_filter_remove(filter, 1);
    3206           3 :                         php_stream_close(phar->fp);
    3207             :                         /* use the temp stream as our base */
    3208           3 :                         phar->fp = newfile;
    3209             :                 } else {
    3210        4488 :                         php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
    3211             :                         /* we could also reopen the file in "rb" mode but there is no need for that */
    3212        4488 :                         php_stream_close(newfile);
    3213             :                 }
    3214             :         }
    3215             : 
    3216        4508 :         if (-1 == php_stream_seek(phar->fp, phar->halt_offset, SEEK_SET)) {
    3217           0 :                 if (error) {
    3218           0 :                         spprintf(error, 0, "unable to seek to __HALT_COMPILER(); in new phar \"%s\"", phar->fname);
    3219             :                 }
    3220           0 :                 return EOF;
    3221             :         }
    3222             : 
    3223        4508 :         return EOF;
    3224             : }
    3225             : /* }}} */
    3226             : 
    3227             : #ifdef COMPILE_DL_PHAR
    3228             : #ifdef ZTS
    3229             : ZEND_TSRMLS_CACHE_DEFINE();
    3230             : #endif
    3231             : ZEND_GET_MODULE(phar)
    3232             : #endif
    3233             : 
    3234             : /* {{{ phar_functions[]
    3235             :  *
    3236             :  * Every user visible function must have an entry in phar_functions[].
    3237             :  */
    3238             : zend_function_entry phar_functions[] = {
    3239             :         PHP_FE_END
    3240             : };
    3241             : /* }}}*/
    3242             : 
    3243           4 : static size_t phar_zend_stream_reader(void *handle, char *buf, size_t len) /* {{{ */
    3244             : {
    3245           4 :         return php_stream_read(phar_get_pharfp((phar_archive_data*)handle), buf, len);
    3246             : }
    3247             : /* }}} */
    3248             : 
    3249           2 : static size_t phar_zend_stream_fsizer(void *handle) /* {{{ */
    3250             : {
    3251           2 :         return ((phar_archive_data*)handle)->halt_offset + 32;
    3252             : } /* }}} */
    3253             : 
    3254             : zend_op_array *(*phar_orig_compile_file)(zend_file_handle *file_handle, int type);
    3255             : #define phar_orig_zend_open zend_stream_open_function
    3256             : 
    3257       14971 : static zend_string *phar_resolve_path(const char *filename, int filename_len)
    3258             : {
    3259       14971 :         return phar_find_in_include_path((char *) filename, filename_len, NULL);
    3260             : }
    3261             : 
    3262       29211 : static zend_op_array *phar_compile_file(zend_file_handle *file_handle, int type) /* {{{ */
    3263             : {
    3264             :         zend_op_array *res;
    3265       29211 :         char *name = NULL;
    3266             :         int failed;
    3267             :         phar_archive_data *phar;
    3268             : 
    3269       29211 :         if (!file_handle || !file_handle->filename) {
    3270           0 :                 return phar_orig_compile_file(file_handle, type);
    3271             :         }
    3272       29211 :         if (strstr(file_handle->filename, ".phar") && !strstr(file_handle->filename, "://")) {
    3273         179 :                 if (SUCCESS == phar_open_from_filename((char*)file_handle->filename, strlen(file_handle->filename), NULL, 0, 0, &phar, NULL)) {
    3274         158 :                         if (phar->is_zip || phar->is_tar) {
    3275          53 :                                 zend_file_handle f = *file_handle;
    3276             : 
    3277             :                                 /* zip or tar-based phar */
    3278          53 :                                 spprintf(&name, 4096, "phar://%s/%s", file_handle->filename, ".phar/stub.php");
    3279          53 :                                 if (SUCCESS == phar_orig_zend_open((const char *)name, file_handle)) {
    3280          52 :                                         efree(name);
    3281          52 :                                         name = NULL;
    3282          52 :                                         file_handle->filename = f.filename;
    3283          52 :                                         if (file_handle->opened_path) {
    3284          52 :                                                 efree(file_handle->opened_path);
    3285             :                                         }
    3286          52 :                                         file_handle->opened_path = f.opened_path;
    3287          52 :                                         file_handle->free_filename = f.free_filename;
    3288             :                                 } else {
    3289           1 :                                         *file_handle = f;
    3290             :                                 }
    3291          52 :                         } else if (phar->flags & PHAR_FILE_COMPRESSION_MASK) {
    3292             :                                 /* compressed phar */
    3293           2 :                                 file_handle->type = ZEND_HANDLE_STREAM;
    3294             :                                 /* we do our own reading directly from the phar, don't change the next line */
    3295           2 :                                 file_handle->handle.stream.handle  = phar;
    3296           2 :                                 file_handle->handle.stream.reader  = phar_zend_stream_reader;
    3297           2 :                                 file_handle->handle.stream.closer  = NULL;
    3298           2 :                                 file_handle->handle.stream.fsizer  = phar_zend_stream_fsizer;
    3299           2 :                                 file_handle->handle.stream.isatty  = 0;
    3300           4 :                                 phar->is_persistent ?
    3301           0 :                                         php_stream_rewind(PHAR_G(cached_fp)[phar->phar_pos].fp) :
    3302           2 :                                         php_stream_rewind(phar->fp);
    3303           2 :                                 memset(&file_handle->handle.stream.mmap, 0, sizeof(file_handle->handle.stream.mmap));
    3304             :                         }
    3305             :                 }
    3306             :         }
    3307             : 
    3308       29211 :         zend_try {
    3309       29211 :                 failed = 0;
    3310       29211 :                 CG(zend_lineno) = 0;
    3311       29211 :                 res = phar_orig_compile_file(file_handle, type);
    3312         240 :         } zend_catch {
    3313         240 :                 failed = 1;
    3314         240 :                 res = NULL;
    3315       29211 :         } zend_end_try();
    3316             : 
    3317       29211 :         if (name) {
    3318           1 :                 efree(name);
    3319             :         }
    3320             : 
    3321       29211 :         if (failed) {
    3322         240 :                 zend_bailout();
    3323             :         }
    3324             : 
    3325       28971 :         return res;
    3326             : }
    3327             : /* }}} */
    3328             : 
    3329             : typedef zend_op_array* (zend_compile_t)(zend_file_handle*, int);
    3330             : typedef zend_compile_t* (compile_hook)(zend_compile_t *ptr);
    3331             : 
    3332      853080 : static void mime_type_dtor(zval *zv)
    3333             : {
    3334      853080 :         free(Z_PTR_P(zv));
    3335      853080 : }
    3336             : 
    3337       21291 : PHP_GINIT_FUNCTION(phar) /* {{{ */
    3338             : {
    3339             : #if defined(COMPILE_DL_PHAR) && defined(ZTS)
    3340             :         ZEND_TSRMLS_CACHE_UPDATE();
    3341             : #endif
    3342             :         phar_mime_type mime;
    3343             : 
    3344       21291 :         memset(phar_globals, 0, sizeof(zend_phar_globals));
    3345       21291 :         phar_globals->readonly = 1;
    3346             : 
    3347       21291 :         zend_hash_init(&phar_globals->mime_types, 0, NULL, mime_type_dtor, 1);
    3348             : 
    3349             : #define PHAR_SET_MIME(mimetype, ret, fileext) \
    3350             :                 mime.mime = mimetype; \
    3351             :                 mime.len = sizeof((mimetype))+1; \
    3352             :                 mime.type = ret; \
    3353             :                 zend_hash_str_add_mem(&phar_globals->mime_types, fileext, sizeof(fileext)-1, (void *)&mime, sizeof(phar_mime_type)); \
    3354             : 
    3355       21291 :         PHAR_SET_MIME("text/html", PHAR_MIME_PHPS, "phps")
    3356       21291 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "c")
    3357       21291 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "cc")
    3358       21291 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "cpp")
    3359       21291 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "c++")
    3360       21291 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "dtd")
    3361       21291 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "h")
    3362       21291 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "log")
    3363       21291 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "rng")
    3364       21291 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "txt")
    3365       21291 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "xsd")
    3366       21291 :         PHAR_SET_MIME("", PHAR_MIME_PHP, "php")
    3367       21291 :         PHAR_SET_MIME("", PHAR_MIME_PHP, "inc")
    3368       21291 :         PHAR_SET_MIME("video/avi", PHAR_MIME_OTHER, "avi")
    3369       21291 :         PHAR_SET_MIME("image/bmp", PHAR_MIME_OTHER, "bmp")
    3370       21291 :         PHAR_SET_MIME("text/css", PHAR_MIME_OTHER, "css")
    3371       21291 :         PHAR_SET_MIME("image/gif", PHAR_MIME_OTHER, "gif")
    3372       21291 :         PHAR_SET_MIME("text/html", PHAR_MIME_OTHER, "htm")
    3373       21291 :         PHAR_SET_MIME("text/html", PHAR_MIME_OTHER, "html")
    3374       21291 :         PHAR_SET_MIME("text/html", PHAR_MIME_OTHER, "htmls")
    3375       21291 :         PHAR_SET_MIME("image/x-ico", PHAR_MIME_OTHER, "ico")
    3376       21291 :         PHAR_SET_MIME("image/jpeg", PHAR_MIME_OTHER, "jpe")
    3377       21291 :         PHAR_SET_MIME("image/jpeg", PHAR_MIME_OTHER, "jpg")
    3378       21291 :         PHAR_SET_MIME("image/jpeg", PHAR_MIME_OTHER, "jpeg")
    3379       21291 :         PHAR_SET_MIME("application/x-javascript", PHAR_MIME_OTHER, "js")
    3380       21291 :         PHAR_SET_MIME("audio/midi", PHAR_MIME_OTHER, "midi")
    3381       21291 :         PHAR_SET_MIME("audio/midi", PHAR_MIME_OTHER, "mid")
    3382       21291 :         PHAR_SET_MIME("audio/mod", PHAR_MIME_OTHER, "mod")
    3383       21291 :         PHAR_SET_MIME("movie/quicktime", PHAR_MIME_OTHER, "mov")
    3384       21291 :         PHAR_SET_MIME("audio/mp3", PHAR_MIME_OTHER, "mp3")
    3385       21291 :         PHAR_SET_MIME("video/mpeg", PHAR_MIME_OTHER, "mpg")
    3386       21291 :         PHAR_SET_MIME("video/mpeg", PHAR_MIME_OTHER, "mpeg")
    3387       21291 :         PHAR_SET_MIME("application/pdf", PHAR_MIME_OTHER, "pdf")
    3388       21291 :         PHAR_SET_MIME("image/png", PHAR_MIME_OTHER, "png")
    3389       21291 :         PHAR_SET_MIME("application/shockwave-flash", PHAR_MIME_OTHER, "swf")
    3390       21291 :         PHAR_SET_MIME("image/tiff", PHAR_MIME_OTHER, "tif")
    3391       21291 :         PHAR_SET_MIME("image/tiff", PHAR_MIME_OTHER, "tiff")
    3392       21291 :         PHAR_SET_MIME("audio/wav", PHAR_MIME_OTHER, "wav")
    3393       21291 :         PHAR_SET_MIME("image/xbm", PHAR_MIME_OTHER, "xbm")
    3394       21291 :         PHAR_SET_MIME("text/xml", PHAR_MIME_OTHER, "xml")
    3395             : 
    3396       21291 :         phar_restore_orig_functions();
    3397       21291 : }
    3398             : /* }}} */
    3399             : 
    3400       21327 : PHP_GSHUTDOWN_FUNCTION(phar) /* {{{ */
    3401             : {
    3402       21327 :         zend_hash_destroy(&phar_globals->mime_types);
    3403       21327 : }
    3404             : /* }}} */
    3405             : 
    3406       21291 : PHP_MINIT_FUNCTION(phar) /* {{{ */
    3407             : {
    3408       21291 :         REGISTER_INI_ENTRIES();
    3409             : 
    3410       21291 :         phar_orig_compile_file = zend_compile_file;
    3411       21291 :         zend_compile_file = phar_compile_file;
    3412             : 
    3413       21291 :         phar_save_resolve_path = zend_resolve_path;
    3414       21291 :         zend_resolve_path = phar_resolve_path;
    3415             : 
    3416       21291 :         phar_object_init();
    3417             : 
    3418       21291 :         phar_intercept_functions_init();
    3419       21291 :         phar_save_orig_functions();
    3420             : 
    3421       21291 :         return php_register_url_stream_wrapper("phar", &php_stream_phar_wrapper);
    3422             : }
    3423             : /* }}} */
    3424             : 
    3425       21327 : PHP_MSHUTDOWN_FUNCTION(phar) /* {{{ */
    3426             : {
    3427       21327 :         php_unregister_url_stream_wrapper("phar");
    3428             : 
    3429       21327 :         phar_intercept_functions_shutdown();
    3430             : 
    3431       21327 :         if (zend_compile_file == phar_compile_file) {
    3432       21327 :                 zend_compile_file = phar_orig_compile_file;
    3433             :         }
    3434             : 
    3435       21327 :         if (PHAR_G(manifest_cached)) {
    3436           3 :                 zend_hash_destroy(&(cached_phars));
    3437           3 :                 zend_hash_destroy(&(cached_alias));
    3438             :         }
    3439             : 
    3440       21327 :         return SUCCESS;
    3441             : }
    3442             : /* }}} */
    3443             : 
    3444       25614 : void phar_request_initialize(void) /* {{{ */
    3445             : {
    3446       25614 :         if (!PHAR_G(request_init))
    3447             :         {
    3448         561 :                 PHAR_G(last_phar) = NULL;
    3449         561 :                 PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
    3450         561 :                 PHAR_G(has_bz2) = zend_hash_str_exists(&module_registry, "bz2", sizeof("bz2")-1);
    3451         561 :                 PHAR_G(has_zlib) = zend_hash_str_exists(&module_registry, "zlib", sizeof("zlib")-1);
    3452         561 :                 PHAR_G(request_init) = 1;
    3453         561 :                 PHAR_G(request_ends) = 0;
    3454         561 :                 PHAR_G(request_done) = 0;
    3455         561 :                 zend_hash_init(&(PHAR_G(phar_fname_map)), 5, zend_get_hash_value, destroy_phar_data,  0);
    3456         561 :                 zend_hash_init(&(PHAR_G(phar_persist_map)), 5, zend_get_hash_value, NULL,  0);
    3457         561 :                 zend_hash_init(&(PHAR_G(phar_alias_map)), 5, zend_get_hash_value, NULL, 0);
    3458             : 
    3459         561 :                 if (PHAR_G(manifest_cached)) {
    3460             :                         phar_archive_data *pphar;
    3461           3 :                         phar_entry_fp *stuff = (phar_entry_fp *) ecalloc(zend_hash_num_elements(&cached_phars), sizeof(phar_entry_fp));
    3462             : 
    3463           9 :                         for (zend_hash_internal_pointer_reset(&cached_phars);
    3464             :                         (pphar = zend_hash_get_current_data_ptr(&cached_phars)) != NULL;
    3465           3 :                         zend_hash_move_forward(&cached_phars)) {
    3466           3 :                                 stuff[pphar->phar_pos].manifest = (phar_entry_fp_info *) ecalloc( zend_hash_num_elements(&(pphar->manifest)), sizeof(phar_entry_fp_info));
    3467             :                         }
    3468             : 
    3469           3 :                         PHAR_G(cached_fp) = stuff;
    3470             :                 }
    3471             : 
    3472         561 :                 PHAR_G(phar_SERVER_mung_list) = 0;
    3473         561 :                 PHAR_G(cwd) = NULL;
    3474         561 :                 PHAR_G(cwd_len) = 0;
    3475         561 :                 PHAR_G(cwd_init) = 0;
    3476             :         }
    3477       25614 : }
    3478             : /* }}} */
    3479             : 
    3480       21286 : PHP_RSHUTDOWN_FUNCTION(phar) /* {{{ */
    3481             : {
    3482             :         int i;
    3483             : 
    3484       21286 :         PHAR_G(request_ends) = 1;
    3485             : 
    3486       21286 :         if (PHAR_G(request_init))
    3487             :         {
    3488         561 :                 phar_release_functions();
    3489         561 :                 zend_hash_destroy(&(PHAR_G(phar_alias_map)));
    3490         561 :                 PHAR_G(phar_alias_map.u.flags) = 0;
    3491         561 :                 zend_hash_destroy(&(PHAR_G(phar_fname_map)));
    3492         561 :                 PHAR_G(phar_fname_map.u.flags) = 0;
    3493         561 :                 zend_hash_destroy(&(PHAR_G(phar_persist_map)));
    3494         561 :                 PHAR_G(phar_persist_map.u.flags) = 0;
    3495         561 :                 PHAR_G(phar_SERVER_mung_list) = 0;
    3496             : 
    3497         561 :                 if (PHAR_G(cached_fp)) {
    3498           6 :                         for (i = 0; i < zend_hash_num_elements(&cached_phars); ++i) {
    3499           3 :                                 if (PHAR_G(cached_fp)[i].fp) {
    3500           1 :                                         php_stream_close(PHAR_G(cached_fp)[i].fp);
    3501             :                                 }
    3502           3 :                                 if (PHAR_G(cached_fp)[i].ufp) {
    3503           0 :                                         php_stream_close(PHAR_G(cached_fp)[i].ufp);
    3504             :                                 }
    3505           3 :                                 efree(PHAR_G(cached_fp)[i].manifest);
    3506             :                         }
    3507           3 :                         efree(PHAR_G(cached_fp));
    3508           3 :                         PHAR_G(cached_fp) = 0;
    3509             :                 }
    3510             : 
    3511         561 :                 PHAR_G(request_init) = 0;
    3512             : 
    3513         561 :                 if (PHAR_G(cwd)) {
    3514          26 :                         efree(PHAR_G(cwd));
    3515             :                 }
    3516             : 
    3517         561 :                 PHAR_G(cwd) = NULL;
    3518         561 :                 PHAR_G(cwd_len) = 0;
    3519         561 :                 PHAR_G(cwd_init) = 0;
    3520             :         }
    3521             : 
    3522       21286 :         PHAR_G(request_done) = 1;
    3523       21286 :         return SUCCESS;
    3524             : }
    3525             : /* }}} */
    3526             : 
    3527         142 : PHP_MINFO_FUNCTION(phar) /* {{{ */
    3528             : {
    3529         142 :         phar_request_initialize();
    3530         142 :         php_info_print_table_start();
    3531         142 :         php_info_print_table_header(2, "Phar: PHP Archive support", "enabled");
    3532         142 :         php_info_print_table_row(2, "Phar EXT version", PHP_PHAR_VERSION);
    3533         142 :         php_info_print_table_row(2, "Phar API version", PHP_PHAR_API_VERSION);
    3534         142 :         php_info_print_table_row(2, "SVN revision", "$Id: 1ed4c8034d7a1b19ec1cc7fe638d845b897300b1 $");
    3535         142 :         php_info_print_table_row(2, "Phar-based phar archives", "enabled");
    3536         142 :         php_info_print_table_row(2, "Tar-based phar archives", "enabled");
    3537         142 :         php_info_print_table_row(2, "ZIP-based phar archives", "enabled");
    3538             : 
    3539         142 :         if (PHAR_G(has_zlib)) {
    3540         142 :                 php_info_print_table_row(2, "gzip compression", "enabled");
    3541             :         } else {
    3542           0 :                 php_info_print_table_row(2, "gzip compression", "disabled (install ext/zlib)");
    3543             :         }
    3544             : 
    3545         142 :         if (PHAR_G(has_bz2)) {
    3546         142 :                 php_info_print_table_row(2, "bzip2 compression", "enabled");
    3547             :         } else {
    3548           0 :                 php_info_print_table_row(2, "bzip2 compression", "disabled (install pecl/bz2)");
    3549             :         }
    3550             : #ifdef PHAR_HAVE_OPENSSL
    3551         142 :         php_info_print_table_row(2, "Native OpenSSL support", "enabled");
    3552             : #else
    3553             :         if (zend_hash_str_exists(&module_registry, "openssl", sizeof("openssl")-1)) {
    3554             :                 php_info_print_table_row(2, "OpenSSL support", "enabled");
    3555             :         } else {
    3556             :                 php_info_print_table_row(2, "OpenSSL support", "disabled (install ext/openssl)");
    3557             :         }
    3558             : #endif
    3559         142 :         php_info_print_table_end();
    3560             : 
    3561         142 :         php_info_print_box_start(0);
    3562         142 :         PUTS("Phar based on pear/PHP_Archive, original concept by Davey Shafik.");
    3563         142 :         PUTS(!sapi_module.phpinfo_as_text?"<br />":"\n");
    3564         142 :         PUTS("Phar fully realized by Gregory Beaver and Marcus Boerger.");
    3565         142 :         PUTS(!sapi_module.phpinfo_as_text?"<br />":"\n");
    3566         142 :         PUTS("Portions of tar implementation Copyright (c) 2003-2009 Tim Kientzle.");
    3567         142 :         php_info_print_box_end();
    3568             : 
    3569         142 :         DISPLAY_INI_ENTRIES();
    3570         142 : }
    3571             : /* }}} */
    3572             : 
    3573             : /* {{{ phar_module_entry
    3574             :  */
    3575             : static const zend_module_dep phar_deps[] = {
    3576             :         ZEND_MOD_OPTIONAL("apc")
    3577             :         ZEND_MOD_OPTIONAL("bz2")
    3578             :         ZEND_MOD_OPTIONAL("openssl")
    3579             :         ZEND_MOD_OPTIONAL("zlib")
    3580             :         ZEND_MOD_OPTIONAL("standard")
    3581             : #if defined(HAVE_HASH) && !defined(COMPILE_DL_HASH)
    3582             :         ZEND_MOD_REQUIRED("hash")
    3583             : #endif
    3584             : #if HAVE_SPL
    3585             :         ZEND_MOD_REQUIRED("spl")
    3586             : #endif
    3587             :         ZEND_MOD_END
    3588             : };
    3589             : 
    3590             : zend_module_entry phar_module_entry = {
    3591             :         STANDARD_MODULE_HEADER_EX, NULL,
    3592             :         phar_deps,
    3593             :         "Phar",
    3594             :         phar_functions,
    3595             :         PHP_MINIT(phar),
    3596             :         PHP_MSHUTDOWN(phar),
    3597             :         NULL,
    3598             :         PHP_RSHUTDOWN(phar),
    3599             :         PHP_MINFO(phar),
    3600             :         PHP_PHAR_VERSION,
    3601             :         PHP_MODULE_GLOBALS(phar),   /* globals descriptor */
    3602             :         PHP_GINIT(phar),            /* globals ctor */
    3603             :         PHP_GSHUTDOWN(phar),        /* globals dtor */
    3604             :         NULL,                       /* post deactivate */
    3605             :         STANDARD_MODULE_PROPERTIES_EX
    3606             : };
    3607             : /* }}} */
    3608             : 
    3609             : /*
    3610             :  * Local variables:
    3611             :  * tab-width: 4
    3612             :  * c-basic-offset: 4
    3613             :  * End:
    3614             :  * vim600: noet sw=4 ts=4 fdm=marker
    3615             :  * vim<600: noet sw=4 ts=4
    3616             :  */

Generated by: LCOV version 1.10

Generated at Sat, 29 Aug 2015 10:22:21 +0000 (3 days ago)

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