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: 1408 1798 78.3 %
Date: 2016-08-24 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-2016 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: 81596423ffe39dd3d0a0432acb536431a8ece4a7 $ */
      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       47105 : ZEND_INI_MH(phar_ini_modify_handler) /* {{{ */
      50             : {
      51             :         zend_bool old, ini;
      52             : 
      53       47105 :         if (ZSTR_LEN(entry->name) == sizeof("phar.readonly")-1) {
      54       23586 :                 old = PHAR_G(readonly_orig);
      55             :         } else {
      56       23519 :                 old = PHAR_G(require_hash_orig);
      57             :         }
      58             : 
      59       47107 :         if (ZSTR_LEN(new_value) == 2 && !strcasecmp("on", ZSTR_VAL(new_value))) {
      60           2 :                 ini = (zend_bool) 1;
      61             :         }
      62       47105 :         else if (ZSTR_LEN(new_value) == 3 && !strcasecmp("yes", ZSTR_VAL(new_value))) {
      63           2 :                 ini = (zend_bool) 1;
      64             :         }
      65       47103 :         else if (ZSTR_LEN(new_value) == 4 && !strcasecmp("true", ZSTR_VAL(new_value))) {
      66           2 :                 ini = (zend_bool) 1;
      67             :         }
      68             :         else {
      69       47099 :                 ini = (zend_bool) atoi(ZSTR_VAL(new_value));
      70             :         }
      71             : 
      72             :         /* do not allow unsetting in runtime */
      73       47105 :         if (stage == ZEND_INI_STAGE_STARTUP) {
      74       47008 :                 if (ZSTR_LEN(entry->name) == sizeof("phar.readonly")-1) {
      75       23504 :                         PHAR_G(readonly_orig) = ini;
      76             :                 } else {
      77       23504 :                         PHAR_G(require_hash_orig) = ini;
      78             :                 }
      79          97 :         } else if (old && !ini) {
      80           2 :                 return FAILURE;
      81             :         }
      82             : 
      83       47103 :         if (ZSTR_LEN(entry->name) == sizeof("phar.readonly")-1) {
      84       23585 :                 PHAR_G(readonly) = ini;
      85       23585 :                 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       23518 :                 PHAR_G(require_hash) = ini;
      90             :         }
      91             : 
      92       47103 :         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       23504 : 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       23504 :         uint i = 0;
     107             : 
     108       23504 :         if (!PHAR_G(cache_list) || !(PHAR_G(cache_list)[0])) {
     109       23382 :                 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       23504 : ZEND_INI_MH(phar_ini_cache_list) /* {{{ */
     185             : {
     186       23504 :         PHAR_G(cache_list) = ZSTR_VAL(new_value);
     187             : 
     188       23504 :         if (stage == ZEND_INI_STAGE_STARTUP) {
     189       23504 :                 phar_split_cache_list();
     190             :         }
     191             : 
     192       23504 :         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         639 : void phar_destroy_phar_data(phar_archive_data *phar) /* {{{ */
     207             : {
     208         639 :         if (phar->alias && phar->alias != phar->fname) {
     209         508 :                 pefree(phar->alias, phar->is_persistent);
     210         508 :                 phar->alias = NULL;
     211             :         }
     212             : 
     213         639 :         if (phar->fname) {
     214         567 :                 pefree(phar->fname, phar->is_persistent);
     215         567 :                 phar->fname = NULL;
     216             :         }
     217             : 
     218         639 :         if (phar->signature) {
     219         395 :                 pefree(phar->signature, phar->is_persistent);
     220         395 :                 phar->signature = NULL;
     221             :         }
     222             : 
     223         639 :         if (phar->manifest.u.flags) {
     224         582 :                 zend_hash_destroy(&phar->manifest);
     225         582 :                 phar->manifest.u.flags = 0;
     226             :         }
     227             : 
     228         639 :         if (phar->mounted_dirs.u.flags) {
     229         582 :                 zend_hash_destroy(&phar->mounted_dirs);
     230         582 :                 phar->mounted_dirs.u.flags = 0;
     231             :         }
     232             : 
     233         639 :         if (phar->virtual_dirs.u.flags) {
     234         582 :                 zend_hash_destroy(&phar->virtual_dirs);
     235         582 :                 phar->virtual_dirs.u.flags = 0;
     236             :         }
     237             : 
     238        1278 :         if (Z_TYPE(phar->metadata) != IS_UNDEF) {
     239          22 :                 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          19 :                         zval_ptr_dtor(&phar->metadata);
     248             :                 }
     249          22 :                 phar->metadata_len = 0;
     250          22 :                 ZVAL_UNDEF(&phar->metadata);
     251             :         }
     252             : 
     253         639 :         if (phar->fp) {
     254         114 :                 php_stream_close(phar->fp);
     255         114 :                 phar->fp = 0;
     256             :         }
     257             : 
     258         639 :         if (phar->ufp) {
     259          20 :                 php_stream_close(phar->ufp);
     260          20 :                 phar->ufp = 0;
     261             :         }
     262             : 
     263         639 :         pefree(phar, phar->is_persistent);
     264         639 : }
     265             : /* }}}*/
     266             : 
     267             : /**
     268             :  * Delete refcount and destruct if needed. On destruct return 1 else 0.
     269             :  */
     270        8620 : int phar_archive_delref(phar_archive_data *phar) /* {{{ */
     271             : {
     272        8620 :         if (phar->is_persistent) {
     273           0 :                 return 0;
     274             :         }
     275             : 
     276        8620 :         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        8606 :         } else if (!phar->refcount) {
     283             :                 /* invalidate phar cache */
     284         660 :                 PHAR_G(last_phar) = NULL;
     285         660 :                 PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
     286             : 
     287         660 :                 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         574 :                         php_stream_close(phar->fp);
     293         574 :                         phar->fp = NULL;
     294             :                 }
     295             : 
     296         660 :                 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        8573 :         return 0;
     306             : }
     307             : /* }}}*/
     308             : 
     309             : /**
     310             :  * Destroy phar's in shutdown, here we don't care about aliases
     311             :  */
     312         509 : static void destroy_phar_data_only(zval *zv) /* {{{ */
     313             : {
     314         509 :         phar_archive_data *phar_data = (phar_archive_data *) Z_PTR_P(zv);
     315             : 
     316         509 :         if (EG(exception) || --phar_data->refcount < 0) {
     317         503 :                 phar_destroy_phar_data(phar_data);
     318             :         }
     319         509 : }
     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        5114 : static int phar_tmpclose_apply(zval *zv) /* {{{ */
     335             : {
     336        5114 :         phar_entry_info *entry = (phar_entry_info *) Z_PTR_P(zv);
     337             : 
     338        5114 :         if (entry->fp_type != PHAR_TMP) {
     339        5097 :                 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         561 : static void destroy_phar_data(zval *zv) /* {{{ */
     355             : {
     356         561 :         phar_archive_data *phar_data = (phar_archive_data *)Z_PTR_P(zv);
     357             : 
     358         561 :         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         509 :                 zend_hash_apply(&(phar_data->manifest), phar_tmpclose_apply);
     362         509 :                 destroy_phar_data_only(zv);
     363         509 :                 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        5352 : void destroy_phar_manifest_entry_int(phar_entry_info *entry) /* {{{ */
     378             : {
     379             : 
     380        5352 :         if (entry->cfp) {
     381           0 :                 php_stream_close(entry->cfp);
     382           0 :                 entry->cfp = 0;
     383             :         }
     384             : 
     385        5352 :         if (entry->fp) {
     386          14 :                 php_stream_close(entry->fp);
     387          14 :                 entry->fp = 0;
     388             :         }
     389             : 
     390       10704 :         if (Z_TYPE(entry->metadata) != IS_UNDEF) {
     391          57 :                 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          54 :                         zval_ptr_dtor(&entry->metadata);
     400             :                 }
     401          57 :                 entry->metadata_len = 0;
     402          57 :                 ZVAL_UNDEF(&entry->metadata);
     403             :         }
     404             : 
     405        5352 :         if (entry->metadata_str.s) {
     406          30 :                 smart_str_free(&entry->metadata_str);
     407          30 :                 entry->metadata_str.s = NULL;
     408             :         }
     409             : 
     410        5352 :         pefree(entry->filename, entry->is_persistent);
     411             : 
     412        5352 :         if (entry->link) {
     413          13 :                 pefree(entry->link, entry->is_persistent);
     414          13 :                 entry->link = 0;
     415             :         }
     416             : 
     417        5352 :         if (entry->tmp) {
     418          17 :                 pefree(entry->tmp, entry->is_persistent);
     419          17 :                 entry->tmp = 0;
     420             :         }
     421        5352 : }
     422             : /* }}} */
     423             : 
     424        5350 : void destroy_phar_manifest_entry(zval *zv) /* {{{ */
     425             : {
     426        5350 :         phar_entry_info *entry = Z_PTR_P(zv);
     427        5350 :         destroy_phar_manifest_entry_int(entry);
     428        5350 :         pefree(entry, entry->is_persistent);
     429        5350 : }
     430             : /* }}} */
     431             : 
     432        8165 : int phar_entry_delref(phar_entry_data *idata) /* {{{ */
     433             : {
     434        8165 :         int ret = 0;
     435             : 
     436        8165 :         if (idata->internal_file && !idata->internal_file->is_persistent) {
     437        8165 :                 if (--idata->internal_file->fp_refcount < 0) {
     438           0 :                         idata->internal_file->fp_refcount = 0;
     439             :                 }
     440             : 
     441        8165 :                 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        8165 :                 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        8165 :         phar_archive_delref(idata->phar);
     452        8165 :         efree(idata);
     453        8165 :         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 = *(uint16_t*)(buffer); \
     520             :         buffer += 2
     521             : #endif
     522             : #define PHAR_ZIP_16(var) ((uint16_t)((((uint16_t)var[0]) & 0xff) | \
     523             :         (((uint16_t)var[1]) & 0xff) << 8))
     524             : #define PHAR_ZIP_32(var) ((uint32_t)((((uint32_t)var[0]) & 0xff) | \
     525             :         (((uint32_t)var[1]) & 0xff) << 8 | \
     526             :         (((uint32_t)var[2]) & 0xff) << 16 | \
     527             :         (((uint32_t)var[3]) & 0xff) << 24))
     528             : 
     529             : /**
     530             :  * Open an already loaded phar
     531             :  */
     532        2896 : 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        2896 :         if (error) {
     540        2460 :                 *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        2982 :         if (SUCCESS == phar_get_archive(&phar, fname, fname_len, alias, alias_len, error)
     554        2939 :                 && ((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        2117 :                 if (!is_data) {
     566             :                         /* prevent any ".phar" without a stub getting through */
     567        1726 :                         if (!phar->halt_offset && !phar->is_brandnew && (phar->is_tar || phar->is_zip)) {
     568         760 :                                 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        2115 :                 if (pphar) {
     578         678 :                         *pphar = phar;
     579             :                 }
     580             : 
     581        2115 :                 return SUCCESS;
     582             :         } else {
     583             : #ifdef PHP_WIN32
     584             :                 efree(unixfname);
     585             : #endif
     586         779 :                 if (pphar) {
     587         652 :                         *pphar = NULL;
     588             :                 }
     589             : 
     590         779 :                 if (phar && error && !(options & REPORT_ERRORS)) {
     591           0 :                         efree(error);
     592             :                 }
     593             : 
     594         779 :                 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        1132 : int phar_parse_metadata(char **buffer, zval *metadata, uint32_t zip_metadata_len) /* {{{ */
     608             : {
     609             :         php_unserialize_data_t var_hash;
     610             : 
     611        1132 :         if (zip_metadata_len) {
     612             :                 const unsigned char *p;
     613         114 :                 unsigned char *p_buff = (unsigned char *)estrndup(*buffer, zip_metadata_len);
     614         114 :                 p = p_buff;
     615         114 :                 ZVAL_NULL(metadata);
     616         114 :                 PHP_VAR_UNSERIALIZE_INIT(var_hash);
     617             : 
     618         114 :                 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          51 :                 efree(p_buff);
     626          51 :                 PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
     627             : 
     628          51 :                 if (PHAR_G(persist)) {
     629             :                         /* lazy init metadata */
     630           6 :                         zval_ptr_dtor(metadata);
     631           6 :                         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        1063 :         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, uint32_t compression, char **error) /* {{{ */
     653             : {
     654             :         char b32[4], *buffer, *endbuffer, *savebuf;
     655         287 :         phar_archive_data *mydata = NULL;
     656             :         phar_entry_info entry;
     657             :         uint32_t manifest_len, manifest_count, manifest_flags, manifest_index, tmp_len, sig_flags;
     658             :         uint16_t manifest_ver;
     659             :         uint32_t 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             :                                 uint32_t 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         272 :         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 > (size_t)(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 > (size_t)(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 > (size_t)(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         624 : 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         624 :         phar_archive_data **test, *unused = NULL;
    1248             : 
    1249         624 :         test = &unused;
    1250             : 
    1251         624 :         if (error) {
    1252         624 :                 *error = NULL;
    1253             :         }
    1254             : 
    1255             :         /* first try to open an existing file */
    1256         624 :         if (phar_detect_phar_fname_ext(fname, fname_len, &ext_str, &ext_len, !is_data, 0, 1) == SUCCESS) {
    1257         430 :                 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         617 :         if (phar_open_parsed_phar(fname, fname_len, alias, alias_len, is_data, options, test, &my_error) == SUCCESS) {
    1273         251 :                 if (pphar) {
    1274         251 :                         *pphar = *test;
    1275             :                 }
    1276             : 
    1277         251 :                 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         251 :                 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         251 :                 if (!PHAR_G(readonly) || (*test)->is_data) {
    1293         243 :                         (*test)->is_writeable = 1;
    1294             :                 }
    1295         251 :                 return SUCCESS;
    1296         366 :         } 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         359 :         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         102 :                 return phar_open_or_create_zip(fname, fname_len, alias, alias_len, is_data, options, pphar, error);
    1308             :         }
    1309             : 
    1310         257 :         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          83 :                 return phar_open_or_create_tar(fname, fname_len, alias, alias_len, is_data, options, pphar, error);
    1313             :         }
    1314             : 
    1315         174 :         return phar_create_or_parse_filename(fname, fname_len, alias, alias_len, is_data, options, pphar, error);
    1316             : }
    1317             : /* }}} */
    1318             : 
    1319         359 : 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         359 :         zend_string *actual = NULL;
    1324             :         char *p;
    1325             : 
    1326         359 :         if (!pphar) {
    1327           0 :                 pphar = &mydata;
    1328             :         }
    1329         359 :         if (php_check_open_basedir(fname)) {
    1330           0 :                 return FAILURE;
    1331             :         }
    1332             : 
    1333             :         /* first open readonly so it won't be created if not present */
    1334         359 :         fp = php_stream_open_wrapper(fname, "rb", IGNORE_URL|STREAM_MUST_SEEK|0, &actual);
    1335             : 
    1336         359 :         if (actual) {
    1337         180 :                 fname = ZSTR_VAL(actual);
    1338         180 :                 fname_len = ZSTR_LEN(actual);
    1339             :         }
    1340             : 
    1341         359 :         if (fp) {
    1342         180 :                 if (phar_open_from_fp(fp, fname, fname_len, alias, alias_len, options, pphar, is_data, error) == SUCCESS) {
    1343         128 :                         if ((*pphar)->is_data || !PHAR_G(readonly)) {
    1344         110 :                                 (*pphar)->is_writeable = 1;
    1345             :                         }
    1346         128 :                         if (actual) {
    1347         128 :                                 zend_string_release(actual);
    1348             :                         }
    1349         128 :                         return SUCCESS;
    1350             :                 } else {
    1351             :                         /* file exists, but is either corrupt or not a phar archive */
    1352          52 :                         if (actual) {
    1353          52 :                                 zend_string_release(actual);
    1354             :                         }
    1355          52 :                         return FAILURE;
    1356             :                 }
    1357             :         }
    1358             : 
    1359         179 :         if (actual) {
    1360           0 :                 zend_string_release(actual);
    1361             :         }
    1362             : 
    1363         179 :         if (PHAR_G(readonly) && !is_data) {
    1364           0 :                 if (options & REPORT_ERRORS) {
    1365           0 :                         if (error) {
    1366           0 :                                 spprintf(error, 0, "creating archive \"%s\" disabled by the php.ini setting phar.readonly", fname);
    1367             :                         }
    1368             :                 }
    1369           0 :                 return FAILURE;
    1370             :         }
    1371             : 
    1372             :         /* set up our manifest */
    1373         179 :         mydata = ecalloc(1, sizeof(phar_archive_data));
    1374         179 :         mydata->fname = expand_filepath(fname, NULL);
    1375         179 :         fname_len = strlen(mydata->fname);
    1376             : #ifdef PHP_WIN32
    1377             :         phar_unixify_path_separators(mydata->fname, fname_len);
    1378             : #endif
    1379         179 :         p = strrchr(mydata->fname, '/');
    1380             : 
    1381         179 :         if (p) {
    1382         179 :                 mydata->ext = memchr(p, '.', (mydata->fname + fname_len) - p);
    1383         179 :                 if (mydata->ext == p) {
    1384           0 :                         mydata->ext = memchr(p + 1, '.', (mydata->fname + fname_len) - p - 1);
    1385             :                 }
    1386         179 :                 if (mydata->ext) {
    1387         179 :                         mydata->ext_len = (mydata->fname + fname_len) - mydata->ext;
    1388             :                 }
    1389             :         }
    1390             : 
    1391         179 :         if (pphar) {
    1392         179 :                 *pphar = mydata;
    1393             :         }
    1394             : 
    1395         179 :         zend_hash_init(&mydata->manifest, sizeof(phar_entry_info),
    1396             :                 zend_get_hash_value, destroy_phar_manifest_entry, 0);
    1397         179 :         zend_hash_init(&mydata->mounted_dirs, sizeof(char *),
    1398             :                 zend_get_hash_value, NULL, 0);
    1399         179 :         zend_hash_init(&mydata->virtual_dirs, sizeof(char *),
    1400             :                 zend_get_hash_value, NULL, (zend_bool)mydata->is_persistent);
    1401         179 :         mydata->fname_len = fname_len;
    1402         179 :         snprintf(mydata->version, sizeof(mydata->version), "%s", PHP_PHAR_API_VERSION);
    1403         179 :         mydata->is_temporary_alias = alias ? 0 : 1;
    1404         179 :         mydata->internal_file_start = -1;
    1405         179 :         mydata->fp = NULL;
    1406         179 :         mydata->is_writeable = 1;
    1407         179 :         mydata->is_brandnew = 1;
    1408         179 :         phar_request_initialize();
    1409         179 :         zend_hash_str_add_ptr(&(PHAR_G(phar_fname_map)), mydata->fname, fname_len, mydata);
    1410             : 
    1411         179 :         if (is_data) {
    1412          11 :                 alias = NULL;
    1413          11 :                 alias_len = 0;
    1414          11 :                 mydata->is_data = 1;
    1415             :                 /* assume tar format, PharData can specify other */
    1416          11 :                 mydata->is_tar = 1;
    1417             :         } else {
    1418             :                 phar_archive_data *fd_ptr;
    1419             : 
    1420         177 :                 if (alias && NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len))) {
    1421           0 :                         if (SUCCESS != phar_free_alias(fd_ptr, alias, alias_len)) {
    1422           0 :                                 if (error) {
    1423           0 :                                         spprintf(error, 4096, "phar error: phar \"%s\" cannot set alias \"%s\", already in use by another phar archive", mydata->fname, alias);
    1424             :                                 }
    1425             : 
    1426           0 :                                 zend_hash_str_del(&(PHAR_G(phar_fname_map)), mydata->fname, fname_len);
    1427             : 
    1428           0 :                                 if (pphar) {
    1429           0 :                                         *pphar = NULL;
    1430             :                                 }
    1431             : 
    1432           0 :                                 return FAILURE;
    1433             :                         }
    1434             :                 }
    1435             : 
    1436         168 :                 mydata->alias = alias ? estrndup(alias, alias_len) : estrndup(mydata->fname, fname_len);
    1437         168 :                 mydata->alias_len = alias ? alias_len : fname_len;
    1438             :         }
    1439             : 
    1440         179 :         if (alias_len && alias) {
    1441          18 :                 if (NULL == zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len, mydata)) {
    1442           0 :                         if (options & REPORT_ERRORS) {
    1443           0 :                                 if (error) {
    1444           0 :                                         spprintf(error, 0, "archive \"%s\" cannot be associated with alias \"%s\", already in use", fname, alias);
    1445             :                                 }
    1446             :                         }
    1447             : 
    1448           0 :                         zend_hash_str_del(&(PHAR_G(phar_fname_map)), mydata->fname, fname_len);
    1449             : 
    1450           0 :                         if (pphar) {
    1451           0 :                                 *pphar = NULL;
    1452             :                         }
    1453             : 
    1454           0 :                         return FAILURE;
    1455             :                 }
    1456             :         }
    1457             : 
    1458         179 :         return SUCCESS;
    1459             : }
    1460             : /* }}}*/
    1461             : 
    1462             : /**
    1463             :  * Return an already opened filename.
    1464             :  *
    1465             :  * Or scan a phar file for the required __HALT_COMPILER(); ?> token and verify
    1466             :  * that the manifest is proper, then pass it to phar_parse_pharfile().  SUCCESS
    1467             :  * or FAILURE is returned and pphar is set to a pointer to the phar's manifest
    1468             :  */
    1469        2144 : int phar_open_from_filename(char *fname, int fname_len, char *alias, int alias_len, int options, phar_archive_data** pphar, char **error) /* {{{ */
    1470             : {
    1471             :         php_stream *fp;
    1472             :         zend_string *actual;
    1473        2144 :         int ret, is_data = 0;
    1474             : 
    1475        2144 :         if (error) {
    1476        1843 :                 *error = NULL;
    1477             :         }
    1478             : 
    1479        2144 :         if (!strstr(fname, ".phar")) {
    1480         424 :                 is_data = 1;
    1481             :         }
    1482             : 
    1483        2144 :         if (phar_open_parsed_phar(fname, fname_len, alias, alias_len, is_data, options, pphar, error) == SUCCESS) {
    1484        1802 :                 return SUCCESS;
    1485         342 :         } else if (error && *error) {
    1486           3 :                 return FAILURE;
    1487             :         }
    1488         339 :         if (php_check_open_basedir(fname)) {
    1489           0 :                 return FAILURE;
    1490             :         }
    1491             : 
    1492         339 :         fp = php_stream_open_wrapper(fname, "rb", IGNORE_URL|STREAM_MUST_SEEK, &actual);
    1493             : 
    1494         339 :         if (!fp) {
    1495          63 :                 if (options & REPORT_ERRORS) {
    1496           1 :                         if (error) {
    1497           1 :                                 spprintf(error, 0, "unable to open phar for reading \"%s\"", fname);
    1498             :                         }
    1499             :                 }
    1500          63 :                 if (actual) {
    1501           0 :                         zend_string_release(actual);
    1502             :                 }
    1503          63 :                 return FAILURE;
    1504             :         }
    1505             : 
    1506         276 :         if (actual) {
    1507         276 :                 fname = ZSTR_VAL(actual);
    1508         276 :                 fname_len = ZSTR_LEN(actual);
    1509             :         }
    1510             : 
    1511         276 :         ret =  phar_open_from_fp(fp, fname, fname_len, alias, alias_len, options, pphar, is_data, error);
    1512             : 
    1513         276 :         if (actual) {
    1514         276 :                 zend_string_release(actual);
    1515             :         }
    1516             : 
    1517         276 :         return ret;
    1518             : }
    1519             : /* }}}*/
    1520             : 
    1521         528 : static inline char *phar_strnstr(const char *buf, int buf_len, const char *search, int search_len) /* {{{ */
    1522             : {
    1523             :         const char *c;
    1524         528 :         ptrdiff_t so_far = 0;
    1525             : 
    1526         528 :         if (buf_len < search_len) {
    1527           0 :                 return NULL;
    1528             :         }
    1529             : 
    1530         528 :         c = buf - 1;
    1531             : 
    1532             :         do {
    1533        4232 :                 if (!(c = memchr(c + 1, search[0], buf_len - search_len - so_far))) {
    1534         241 :                         return (char *) NULL;
    1535             :                 }
    1536             : 
    1537        3991 :                 so_far = c - buf;
    1538             : 
    1539        3991 :                 if (so_far >= (buf_len - search_len)) {
    1540           0 :                         return (char *) NULL;
    1541             :                 }
    1542             : 
    1543        3991 :                 if (!memcmp(c, search, search_len)) {
    1544         287 :                         return (char *) c;
    1545             :                 }
    1546        3704 :         } while (1);
    1547             : }
    1548             : /* }}} */
    1549             : 
    1550             : /**
    1551             :  * Scan an open fp for the required __HALT_COMPILER(); ?> token and verify
    1552             :  * that the manifest is proper, then pass it to phar_parse_pharfile().  SUCCESS
    1553             :  * or FAILURE is returned and pphar is set to a pointer to the phar's manifest
    1554             :  */
    1555         528 : 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) /* {{{ */
    1556             : {
    1557         528 :         const char token[] = "__HALT_COMPILER();";
    1558         528 :         const char zip_magic[] = "PK\x03\x04";
    1559         528 :         const char gz_magic[] = "\x1f\x8b\x08";
    1560         528 :         const char bz_magic[] = "BZh";
    1561         528 :         char *pos, test = '\0';
    1562         528 :         const int window_size = 1024;
    1563             :         char buffer[1024 + sizeof(token)]; /* a 1024 byte window + the size of the halt_compiler token (moving window) */
    1564         528 :         const zend_long readsize = sizeof(buffer) - sizeof(token);
    1565         528 :         const zend_long tokenlen = sizeof(token) - 1;
    1566             :         zend_long halt_offset;
    1567             :         size_t got;
    1568         528 :         uint32_t compression = PHAR_FILE_COMPRESSED_NONE;
    1569             : 
    1570         528 :         if (error) {
    1571         306 :                 *error = NULL;
    1572             :         }
    1573             : 
    1574         528 :         if (-1 == php_stream_rewind(fp)) {
    1575           0 :                 MAPPHAR_ALLOC_FAIL("cannot rewind phar \"%s\"")
    1576             :         }
    1577             : 
    1578         528 :         buffer[sizeof(buffer)-1] = '\0';
    1579         528 :         memset(buffer, 32, sizeof(token));
    1580         528 :         halt_offset = 0;
    1581             : 
    1582             :         /* Maybe it's better to compile the file instead of just searching,  */
    1583             :         /* but we only want the offset. So we want a .re scanner to find it. */
    1584        1319 :         while(!php_stream_eof(fp)) {
    1585         719 :                 if ((got = php_stream_read(fp, buffer+tokenlen, readsize)) < (size_t) tokenlen) {
    1586           2 :                         MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (truncated entry)")
    1587             :                 }
    1588             : 
    1589         717 :                 if (!test) {
    1590         548 :                         test = '\1';
    1591         548 :                         pos = buffer+tokenlen;
    1592         548 :                         if (!memcmp(pos, gz_magic, 3)) {
    1593          14 :                                 char err = 0;
    1594             :                                 php_stream_filter *filter;
    1595             :                                 php_stream *temp;
    1596             :                                 /* to properly decompress, we have to tell zlib to look for a zlib or gzip header */
    1597             :                                 zval filterparams;
    1598             : 
    1599          14 :                                 if (!PHAR_G(has_zlib)) {
    1600           0 :                                         MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\" to temporary file, enable zlib extension in php.ini")
    1601             :                                 }
    1602          14 :                                 array_init(&filterparams);
    1603             : /* this is defined in zlib's zconf.h */
    1604             : #ifndef MAX_WBITS
    1605             : #define MAX_WBITS 15
    1606             : #endif
    1607          14 :                                 add_assoc_long_ex(&filterparams, "window", sizeof("window") - 1, MAX_WBITS + 32);
    1608             : 
    1609             :                                 /* entire file is gzip-compressed, uncompress to temporary file */
    1610          14 :                                 if (!(temp = php_stream_fopen_tmpfile())) {
    1611           0 :                                         MAPPHAR_ALLOC_FAIL("unable to create temporary file for decompression of gzipped phar archive \"%s\"")
    1612             :                                 }
    1613             : 
    1614          14 :                                 php_stream_rewind(fp);
    1615          14 :                                 filter = php_stream_filter_create("zlib.inflate", &filterparams, php_stream_is_persistent(fp));
    1616             : 
    1617          14 :                                 if (!filter) {
    1618           0 :                                         err = 1;
    1619           0 :                                         add_assoc_long_ex(&filterparams, "window", sizeof("window") - 1, MAX_WBITS);
    1620           0 :                                         filter = php_stream_filter_create("zlib.inflate", &filterparams, php_stream_is_persistent(fp));
    1621             :                                         zval_dtor(&filterparams);
    1622             : 
    1623           0 :                                         if (!filter) {
    1624           0 :                                                 php_stream_close(temp);
    1625           0 :                                                 MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\", ext/zlib is buggy in PHP versions older than 5.2.6")
    1626             :                                         }
    1627             :                                 } else {
    1628             :                                         zval_dtor(&filterparams);
    1629             :                                 }
    1630             : 
    1631          14 :                                 php_stream_filter_append(&temp->writefilters, filter);
    1632             : 
    1633          14 :                                 if (SUCCESS != php_stream_copy_to_stream_ex(fp, temp, PHP_STREAM_COPY_ALL, NULL)) {
    1634           0 :                                         if (err) {
    1635           0 :                                                 php_stream_close(temp);
    1636           0 :                                                 MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\", ext/zlib is buggy in PHP versions older than 5.2.6")
    1637             :                                         }
    1638           0 :                                         php_stream_close(temp);
    1639           0 :                                         MAPPHAR_ALLOC_FAIL("unable to decompress gzipped phar archive \"%s\" to temporary file")
    1640             :                                 }
    1641             : 
    1642          14 :                                 php_stream_filter_flush(filter, 1);
    1643          14 :                                 php_stream_filter_remove(filter, 1);
    1644          14 :                                 php_stream_close(fp);
    1645          14 :                                 fp = temp;
    1646          14 :                                 php_stream_rewind(fp);
    1647          14 :                                 compression = PHAR_FILE_COMPRESSED_GZ;
    1648             : 
    1649             :                                 /* now, start over */
    1650          14 :                                 test = '\0';
    1651          14 :                                 continue;
    1652         534 :                         } else if (!memcmp(pos, bz_magic, 3)) {
    1653             :                                 php_stream_filter *filter;
    1654             :                                 php_stream *temp;
    1655             : 
    1656           8 :                                 if (!PHAR_G(has_bz2)) {
    1657           0 :                                         MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\" to temporary file, enable bz2 extension in php.ini")
    1658             :                                 }
    1659             : 
    1660             :                                 /* entire file is bzip-compressed, uncompress to temporary file */
    1661           8 :                                 if (!(temp = php_stream_fopen_tmpfile())) {
    1662           0 :                                         MAPPHAR_ALLOC_FAIL("unable to create temporary file for decompression of bzipped phar archive \"%s\"")
    1663             :                                 }
    1664             : 
    1665           8 :                                 php_stream_rewind(fp);
    1666           8 :                                 filter = php_stream_filter_create("bzip2.decompress", NULL, php_stream_is_persistent(fp));
    1667             : 
    1668           8 :                                 if (!filter) {
    1669           0 :                                         php_stream_close(temp);
    1670           0 :                                         MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\", filter creation failed")
    1671             :                                 }
    1672             : 
    1673           8 :                                 php_stream_filter_append(&temp->writefilters, filter);
    1674             : 
    1675           8 :                                 if (SUCCESS != php_stream_copy_to_stream_ex(fp, temp, PHP_STREAM_COPY_ALL, NULL)) {
    1676           0 :                                         php_stream_close(temp);
    1677           0 :                                         MAPPHAR_ALLOC_FAIL("unable to decompress bzipped phar archive \"%s\" to temporary file")
    1678             :                                 }
    1679             : 
    1680           8 :                                 php_stream_filter_flush(filter, 1);
    1681           8 :                                 php_stream_filter_remove(filter, 1);
    1682           8 :                                 php_stream_close(fp);
    1683           8 :                                 fp = temp;
    1684           8 :                                 php_stream_rewind(fp);
    1685           8 :                                 compression = PHAR_FILE_COMPRESSED_BZ2;
    1686             : 
    1687             :                                 /* now, start over */
    1688           8 :                                 test = '\0';
    1689           8 :                                 continue;
    1690             :                         }
    1691             : 
    1692         526 :                         if (!memcmp(pos, zip_magic, 4)) {
    1693          85 :                                 php_stream_seek(fp, 0, SEEK_END);
    1694          85 :                                 return phar_parse_zipfile(fp, fname, fname_len, alias, alias_len, pphar, error);
    1695             :                         }
    1696             : 
    1697         441 :                         if (got > 512) {
    1698         167 :                                 if (phar_is_tar(pos, fname)) {
    1699          82 :                                         php_stream_rewind(fp);
    1700          82 :                                         return phar_parse_tarfile(fp, fname, fname_len, alias, alias_len, pphar, is_data, compression, error);
    1701             :                                 }
    1702             :                         }
    1703             :                 }
    1704             : 
    1705         528 :                 if (got > 0 && (pos = phar_strnstr(buffer, got + sizeof(token), token, sizeof(token)-1)) != NULL) {
    1706         287 :                         halt_offset += (pos - buffer); /* no -tokenlen+tokenlen here */
    1707         287 :                         return phar_parse_pharfile(fp, fname, fname_len, alias, alias_len, halt_offset, pphar, compression, error);
    1708             :                 }
    1709             : 
    1710         241 :                 halt_offset += got;
    1711         241 :                 memmove(buffer, buffer + window_size, tokenlen); /* move the memory buffer by the size of the window */
    1712             :         }
    1713             : 
    1714          72 :         MAPPHAR_ALLOC_FAIL("internal corruption of phar \"%s\" (__HALT_COMPILER(); not found)")
    1715             : }
    1716             : /* }}} */
    1717             : 
    1718             : /*
    1719             :  * given the location of the file extension and the start of the file path,
    1720             :  * determine the end of the portion of the path (i.e. /path/to/file.ext/blah
    1721             :  * grabs "/path/to/file.ext" as does the straight /path/to/file.ext),
    1722             :  * stat it to determine if it exists.
    1723             :  * if so, check to see if it is a directory and fail if so
    1724             :  * if not, check to see if its dirname() exists (i.e. "/path/to") and is a directory
    1725             :  * succeed if we are creating the file, otherwise fail.
    1726             :  */
    1727        1075 : static int phar_analyze_path(const char *fname, const char *ext, int ext_len, int for_create) /* {{{ */
    1728             : {
    1729             :         php_stream_statbuf ssb;
    1730             :         char *realpath;
    1731        1075 :         char *filename = estrndup(fname, (ext - fname) + ext_len);
    1732             : 
    1733        1075 :         if ((realpath = expand_filepath(filename, NULL))) {
    1734             : #ifdef PHP_WIN32
    1735             :                 phar_unixify_path_separators(realpath, strlen(realpath));
    1736             : #endif
    1737        1074 :                 if (zend_hash_str_exists(&(PHAR_G(phar_fname_map)), realpath, strlen(realpath))) {
    1738           8 :                         efree(realpath);
    1739           8 :                         efree(filename);
    1740           8 :                         return SUCCESS;
    1741             :                 }
    1742             : 
    1743        1066 :                 if (PHAR_G(manifest_cached) && zend_hash_str_exists(&cached_phars, realpath, strlen(realpath))) {
    1744           0 :                         efree(realpath);
    1745           0 :                         efree(filename);
    1746           0 :                         return SUCCESS;
    1747             :                 }
    1748        1066 :                 efree(realpath);
    1749             :         }
    1750             : 
    1751        1067 :         if (SUCCESS == php_stream_stat_path((char *) filename, &ssb)) {
    1752             : 
    1753         415 :                 efree(filename);
    1754             : 
    1755         415 :                 if (ssb.sb.st_mode & S_IFDIR) {
    1756           0 :                         return FAILURE;
    1757             :                 }
    1758             : 
    1759         415 :                 if (for_create == 1) {
    1760           0 :                         return FAILURE;
    1761             :                 }
    1762             : 
    1763         415 :                 return SUCCESS;
    1764             :         } else {
    1765             :                 char *slash;
    1766             : 
    1767         652 :                 if (!for_create) {
    1768         212 :                         efree(filename);
    1769         212 :                         return FAILURE;
    1770             :                 }
    1771             : 
    1772         440 :                 slash = (char *) strrchr(filename, '/');
    1773             : 
    1774         440 :                 if (slash) {
    1775         406 :                         *slash = '\0';
    1776             :                 }
    1777             : 
    1778         440 :                 if (SUCCESS != php_stream_stat_path((char *) filename, &ssb)) {
    1779          34 :                         if (!slash) {
    1780          34 :                                 if (!(realpath = expand_filepath(filename, NULL))) {
    1781           0 :                                         efree(filename);
    1782           0 :                                         return FAILURE;
    1783             :                                 }
    1784             : #ifdef PHP_WIN32
    1785             :                                 phar_unixify_path_separators(realpath, strlen(realpath));
    1786             : #endif
    1787          34 :                                 slash = strstr(realpath, filename);
    1788          34 :                                 if (slash) {
    1789          34 :                                         slash += ((ext - fname) + ext_len);
    1790          34 :                                         *slash = '\0';
    1791             :                                 }
    1792          34 :                                 slash = strrchr(realpath, '/');
    1793             : 
    1794          34 :                                 if (slash) {
    1795          34 :                                         *slash = '\0';
    1796             :                                 } else {
    1797           0 :                                         efree(realpath);
    1798           0 :                                         efree(filename);
    1799           0 :                                         return FAILURE;
    1800             :                                 }
    1801             : 
    1802          34 :                                 if (SUCCESS != php_stream_stat_path(realpath, &ssb)) {
    1803           0 :                                         efree(realpath);
    1804           0 :                                         efree(filename);
    1805           0 :                                         return FAILURE;
    1806             :                                 }
    1807             : 
    1808          34 :                                 efree(realpath);
    1809             : 
    1810          34 :                                 if (ssb.sb.st_mode & S_IFDIR) {
    1811          34 :                                         efree(filename);
    1812          34 :                                         return SUCCESS;
    1813             :                                 }
    1814             :                         }
    1815             : 
    1816           0 :                         efree(filename);
    1817           0 :                         return FAILURE;
    1818             :                 }
    1819             : 
    1820         406 :                 efree(filename);
    1821             : 
    1822         406 :                 if (ssb.sb.st_mode & S_IFDIR) {
    1823         405 :                         return SUCCESS;
    1824             :                 }
    1825             : 
    1826           1 :                 return FAILURE;
    1827             :         }
    1828             : }
    1829             : /* }}} */
    1830             : 
    1831             : /* check for ".phar" in extension */
    1832        1098 : static int phar_check_str(const char *fname, const char *ext_str, int ext_len, int executable, int for_create) /* {{{ */
    1833             : {
    1834             :         char test[51];
    1835             :         const char *pos;
    1836             : 
    1837        1098 :         if (ext_len >= 50) {
    1838           0 :                 return FAILURE;
    1839             :         }
    1840             : 
    1841        1098 :         if (executable == 1) {
    1842             :                 /* copy "." as well */
    1843         805 :                 memcpy(test, ext_str - 1, ext_len + 1);
    1844         805 :                 test[ext_len + 1] = '\0';
    1845             :                 /* executable phars must contain ".phar" as a valid extension (phar://.pharmy/oops is invalid) */
    1846             :                 /* (phar://hi/there/.phar/oops is also invalid) */
    1847         805 :                 pos = strstr(test, ".phar");
    1848             : 
    1849        2703 :                 if (pos && (*(pos - 1) != '/')
    1850        1898 :                                 && (pos += 5) && (*pos == '\0' || *pos == '/' || *pos == '.')) {
    1851         790 :                         return phar_analyze_path(fname, ext_str, ext_len, for_create);
    1852             :                 } else {
    1853          15 :                         return FAILURE;
    1854             :                 }
    1855             :         }
    1856             : 
    1857             :         /* data phars need only contain a single non-"." to be valid */
    1858         293 :         if (!executable) {
    1859         178 :                 pos = strstr(ext_str, ".phar");
    1860         711 :                 if (!(pos && (*(pos - 1) != '/')
    1861         533 :                                         && (pos += 5) && (*pos == '\0' || *pos == '/' || *pos == '.')) && *(ext_str + 1) != '.' && *(ext_str + 1) != '/' && *(ext_str + 1) != '\0') {
    1862         170 :                         return phar_analyze_path(fname, ext_str, ext_len, for_create);
    1863             :                 }
    1864             :         } else {
    1865         115 :                 if (*(ext_str + 1) != '.' && *(ext_str + 1) != '/' && *(ext_str + 1) != '\0') {
    1866         115 :                         return phar_analyze_path(fname, ext_str, ext_len, for_create);
    1867             :                 }
    1868             :         }
    1869             : 
    1870           8 :         return FAILURE;
    1871             : }
    1872             : /* }}} */
    1873             : 
    1874             : /*
    1875             :  * if executable is 1, only returns SUCCESS if the extension is one of the tar/zip .phar extensions
    1876             :  * if executable is 0, it returns SUCCESS only if the filename does *not* contain ".phar" anywhere, and treats
    1877             :  * the first extension as the filename extension
    1878             :  *
    1879             :  * if an extension is found, it sets ext_str to the location of the file extension in filename,
    1880             :  * and ext_len to the length of the extension.
    1881             :  * for urls like "phar://alias/oops" it instead sets ext_len to -1 and returns FAILURE, which tells
    1882             :  * the calling function to use "alias" as the phar alias
    1883             :  *
    1884             :  * the last parameter should be set to tell the thing to assume that filename is the full path, and only to check the
    1885             :  * extension rules, not to iterate.
    1886             :  */
    1887        3495 : 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) /* {{{ */
    1888             : {
    1889             :         const char *pos, *slash;
    1890             : 
    1891        3495 :         *ext_str = NULL;
    1892        3495 :         *ext_len = 0;
    1893             : 
    1894        3495 :         if (!filename_len || filename_len == 1) {
    1895          16 :                 return FAILURE;
    1896             :         }
    1897             : 
    1898        3479 :         phar_request_initialize();
    1899             :         /* first check for alias in first segment */
    1900        3479 :         pos = memchr(filename, '/', filename_len);
    1901             : 
    1902        3479 :         if (pos && pos != filename) {
    1903             :                 /* check for url like http:// or phar:// */
    1904          83 :                 if (*(pos - 1) == ':' && (pos - filename) < filename_len - 1 && *(pos + 1) == '/') {
    1905           6 :                         *ext_len = -2;
    1906           6 :                         *ext_str = NULL;
    1907           6 :                         return FAILURE;
    1908             :                 }
    1909          77 :                 if (zend_hash_str_exists(&(PHAR_G(phar_alias_map)), (char *) filename, pos - filename)) {
    1910          64 :                         *ext_str = pos;
    1911          64 :                         *ext_len = -1;
    1912          64 :                         return FAILURE;
    1913             :                 }
    1914             : 
    1915          13 :                 if (PHAR_G(manifest_cached) && zend_hash_str_exists(&cached_alias, (char *) filename, pos - filename)) {
    1916           0 :                         *ext_str = pos;
    1917           0 :                         *ext_len = -1;
    1918           0 :                         return FAILURE;
    1919             :                 }
    1920             :         }
    1921             : 
    1922        3409 :         if (zend_hash_num_elements(&(PHAR_G(phar_fname_map))) || PHAR_G(manifest_cached)) {
    1923             :                 phar_archive_data *pphar;
    1924             : 
    1925        2587 :                 if (is_complete) {
    1926         832 :                         if (NULL != (pphar = zend_hash_str_find_ptr(&(PHAR_G(phar_fname_map)), (char *) filename, filename_len))) {
    1927         246 :                                 *ext_str = filename + (filename_len - pphar->ext_len);
    1928             : woohoo:
    1929        2313 :                                 *ext_len = pphar->ext_len;
    1930             : 
    1931        2313 :                                 if (executable == 2) {
    1932        1887 :                                         return SUCCESS;
    1933             :                                 }
    1934             : 
    1935         426 :                                 if (executable == 1 && !pphar->is_data) {
    1936         351 :                                         return SUCCESS;
    1937             :                                 }
    1938             : 
    1939          75 :                                 if (!executable && pphar->is_data) {
    1940          72 :                                         return SUCCESS;
    1941             :                                 }
    1942             : 
    1943           3 :                                 return FAILURE;
    1944             :                         }
    1945             : 
    1946         174 :                         if (PHAR_G(manifest_cached) && NULL != (pphar = zend_hash_str_find_ptr(&cached_phars, (char *) filename, filename_len))) {
    1947           4 :                                 *ext_str = filename + (filename_len - pphar->ext_len);
    1948           4 :                                 goto woohoo;
    1949             :                         }
    1950             :                 } else {
    1951             :                         zend_string *str_key;
    1952             :                         zend_ulong unused;
    1953             : 
    1954        5093 :                         for (zend_hash_internal_pointer_reset(&(PHAR_G(phar_fname_map)));
    1955        2922 :                                 HASH_KEY_NON_EXISTENT != zend_hash_get_current_key(&(PHAR_G(phar_fname_map)), &str_key, &unused);
    1956             :                                 zend_hash_move_forward(&(PHAR_G(phar_fname_map)))
    1957         751 :                         ) {
    1958        2804 :                                 if (ZSTR_LEN(str_key) > (uint) filename_len) {
    1959         178 :                                         continue;
    1960             :                                 }
    1961             : 
    1962        8853 :                                 if (!memcmp(filename, ZSTR_VAL(str_key), ZSTR_LEN(str_key)) && ((uint)filename_len == ZSTR_LEN(str_key)
    1963        6227 :                                         || filename[ZSTR_LEN(str_key)] == '/' || filename[ZSTR_LEN(str_key)] == '\0')) {
    1964        2053 :                                         if (NULL == (pphar = zend_hash_get_current_data_ptr(&(PHAR_G(phar_fname_map))))) {
    1965           0 :                                                 break;
    1966             :                                         }
    1967        2053 :                                         *ext_str = filename + (ZSTR_LEN(str_key) - pphar->ext_len);
    1968        2053 :                                         goto woohoo;
    1969             :                                 }
    1970             :                         }
    1971             : 
    1972         118 :                         if (PHAR_G(manifest_cached)) {
    1973          20 :                                 for (zend_hash_internal_pointer_reset(&cached_phars);
    1974          10 :                                         HASH_KEY_NON_EXISTENT != zend_hash_get_current_key(&cached_phars, &str_key, &unused);
    1975             :                                         zend_hash_move_forward(&cached_phars)
    1976           0 :                                 ) {
    1977          10 :                                         if (ZSTR_LEN(str_key) > (uint) filename_len) {
    1978           0 :                                                 continue;
    1979             :                                         }
    1980             : 
    1981          38 :                                         if (!memcmp(filename, ZSTR_VAL(str_key), ZSTR_LEN(str_key)) && ((uint)filename_len == ZSTR_LEN(str_key)
    1982          28 :                                                 || filename[ZSTR_LEN(str_key)] == '/' || filename[ZSTR_LEN(str_key)] == '\0')) {
    1983          10 :                                                 if (NULL == (pphar = zend_hash_get_current_data_ptr(&cached_phars))) {
    1984           0 :                                                         break;
    1985             :                                                 }
    1986          10 :                                                 *ext_str = filename + (ZSTR_LEN(str_key) - pphar->ext_len);
    1987          10 :                                                 goto woohoo;
    1988             :                                         }
    1989             :                                 }
    1990             :                         }
    1991             :                 }
    1992             :         }
    1993             : 
    1994        1096 :         pos = memchr(filename + 1, '.', filename_len);
    1995             : next_extension:
    1996        1116 :         if (!pos) {
    1997          18 :                 return FAILURE;
    1998             :         }
    1999             : 
    2000        2197 :         while (pos != filename && (*(pos - 1) == '/' || *(pos - 1) == '\0')) {
    2001           1 :                 pos = memchr(pos + 1, '.', filename_len - (pos - filename) + 1);
    2002           1 :                 if (!pos) {
    2003           0 :                         return FAILURE;
    2004             :                 }
    2005             :         }
    2006             : 
    2007        1098 :         slash = memchr(pos, '/', filename_len - (pos - filename));
    2008             : 
    2009        1098 :         if (!slash) {
    2010             :                 /* this is a url like "phar://blah.phar" with no directory */
    2011         995 :                 *ext_str = pos;
    2012         995 :                 *ext_len = strlen(pos);
    2013             : 
    2014             :                 /* file extension must contain "phar" */
    2015         995 :                 switch (phar_check_str(filename, *ext_str, *ext_len, executable, for_create)) {
    2016             :                         case SUCCESS:
    2017         779 :                                 return SUCCESS;
    2018             :                         case FAILURE:
    2019             :                                 /* we are at the end of the string, so we fail */
    2020         216 :                                 return FAILURE;
    2021             :                 }
    2022             :         }
    2023             : 
    2024             :         /* we've found an extension that ends at a directory separator */
    2025         103 :         *ext_str = pos;
    2026         103 :         *ext_len = slash - pos;
    2027             : 
    2028         103 :         switch (phar_check_str(filename, *ext_str, *ext_len, executable, for_create)) {
    2029             :                 case SUCCESS:
    2030          83 :                         return SUCCESS;
    2031             :                 case FAILURE:
    2032             :                         /* look for more extensions */
    2033          20 :                         pos = strchr(pos + 1, '.');
    2034          20 :                         if (pos) {
    2035          15 :                                 *ext_str = NULL;
    2036          15 :                                 *ext_len = 0;
    2037             :                         }
    2038          20 :                         goto next_extension;
    2039             :         }
    2040             : 
    2041           0 :         return FAILURE;
    2042             : }
    2043             : /* }}} */
    2044             : 
    2045        1424 : static int php_check_dots(const char *element, int n) /* {{{ */
    2046             : {
    2047        1442 :         for(n--; n >= 0; --n) {
    2048        1433 :                 if (element[n] != '.') {
    2049        1415 :                         return 1;
    2050             :                 }
    2051             :         }
    2052           9 :         return 0;
    2053             : }
    2054             : /* }}} */
    2055             : 
    2056             : #define IS_DIRECTORY_UP(element, len) \
    2057             :         (len >= 2 && !php_check_dots(element, len))
    2058             : 
    2059             : #define IS_DIRECTORY_CURRENT(element, len) \
    2060             :         (len == 1 && element[0] == '.')
    2061             : 
    2062             : #define IS_BACKSLASH(c) ((c) == '/')
    2063             : 
    2064             : /**
    2065             :  * Remove .. and . references within a phar filename
    2066             :  */
    2067        2133 : char *phar_fix_filepath(char *path, int *new_len, int use_cwd) /* {{{ */
    2068             : {
    2069             :         char *newpath;
    2070             :         int newpath_len;
    2071             :         char *ptr;
    2072             :         char *tok;
    2073        2133 :         int ptr_length, path_length = *new_len;
    2074             : 
    2075        2144 :         if (PHAR_G(cwd_len) && use_cwd && path_length > 2 && path[0] == '.' && path[1] == '/') {
    2076          11 :                 newpath_len = PHAR_G(cwd_len);
    2077          11 :                 newpath = emalloc(strlen(path) + newpath_len + 1);
    2078          11 :                 memcpy(newpath, PHAR_G(cwd), newpath_len);
    2079             :         } else {
    2080        2122 :                 newpath = emalloc(strlen(path) + 2);
    2081        2122 :                 newpath[0] = '/';
    2082        2122 :                 newpath_len = 1;
    2083             :         }
    2084             : 
    2085        2133 :         ptr = path;
    2086             : 
    2087        2133 :         if (*ptr == '/') {
    2088        2080 :                 ++ptr;
    2089             :         }
    2090             : 
    2091        2133 :         tok = ptr;
    2092             : 
    2093             :         do {
    2094        2135 :                 ptr = memchr(ptr, '/', path_length - (ptr - path));
    2095        2135 :         } while (ptr && ptr - tok == 0 && *ptr == '/' && ++ptr && ++tok);
    2096             : 
    2097        2133 :         if (!ptr && (path_length - (tok - path))) {
    2098        1050 :                 switch (path_length - (tok - path)) {
    2099             :                         case 1:
    2100         478 :                                 if (*tok == '.') {
    2101           2 :                                         efree(path);
    2102           2 :                                         *new_len = 1;
    2103           2 :                                         efree(newpath);
    2104           2 :                                         return estrndup("/", 1);
    2105             :                                 }
    2106         476 :                                 break;
    2107             :                         case 2:
    2108          16 :                                 if (tok[0] == '.' && tok[1] == '.') {
    2109           0 :                                         efree(path);
    2110           0 :                                         *new_len = 1;
    2111           0 :                                         efree(newpath);
    2112           0 :                                         return estrndup("/", 1);
    2113             :                                 }
    2114             :                 }
    2115        1048 :                 efree(newpath);
    2116        1048 :                 return path;
    2117             :         }
    2118             : 
    2119        2578 :         while (ptr) {
    2120        1052 :                 ptr_length = ptr - tok;
    2121             : last_time:
    2122        1701 :                 if (IS_DIRECTORY_UP(tok, ptr_length)) {
    2123             : #define PREVIOUS newpath[newpath_len - 1]
    2124             : 
    2125          23 :                         while (newpath_len > 1 && !IS_BACKSLASH(PREVIOUS)) {
    2126           5 :                                 newpath_len--;
    2127             :                         }
    2128             : 
    2129           9 :                         if (newpath[0] != '/') {
    2130           0 :                                 newpath[newpath_len] = '\0';
    2131           9 :                         } else if (newpath_len > 1) {
    2132           0 :                                 --newpath_len;
    2133             :                         }
    2134        1683 :                 } else if (!IS_DIRECTORY_CURRENT(tok, ptr_length)) {
    2135        1672 :                         if (newpath_len > 1) {
    2136        1014 :                                 newpath[newpath_len++] = '/';
    2137        1014 :                                 memcpy(newpath + newpath_len, tok, ptr_length+1);
    2138             :                         } else {
    2139         658 :                                 memcpy(newpath + newpath_len, tok, ptr_length+1);
    2140             :                         }
    2141             : 
    2142        1672 :                         newpath_len += ptr_length;
    2143             :                 }
    2144             : 
    2145        1692 :                 if (ptr == path + path_length) {
    2146         640 :                         break;
    2147             :                 }
    2148             : 
    2149        1052 :                 tok = ++ptr;
    2150             : 
    2151             :                 do {
    2152        1053 :                         ptr = memchr(ptr, '/', path_length - (ptr - path));
    2153        1053 :                 } while (ptr && ptr - tok == 0 && *ptr == '/' && ++ptr && ++tok);
    2154             : 
    2155        1052 :                 if (!ptr && (path_length - (tok - path))) {
    2156         640 :                         ptr_length = path_length - (tok - path);
    2157         640 :                         ptr = path + path_length;
    2158         640 :                         goto last_time;
    2159             :                 }
    2160             :         }
    2161             : 
    2162        1083 :         efree(path);
    2163        1083 :         *new_len = newpath_len;
    2164        1083 :         newpath[newpath_len] = '\0';
    2165        1083 :         return erealloc(newpath, newpath_len + 1);
    2166             : }
    2167             : /* }}} */
    2168             : 
    2169             : /**
    2170             :  * Process a phar stream name, ensuring we can handle any of:
    2171             :  *
    2172             :  * - whatever.phar
    2173             :  * - whatever.phar.gz
    2174             :  * - whatever.phar.bz2
    2175             :  * - whatever.phar.php
    2176             :  *
    2177             :  * Optionally the name might start with 'phar://'
    2178             :  *
    2179             :  * This is used by phar_parse_url()
    2180             :  */
    2181        2591 : 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) /* {{{ */
    2182             : {
    2183             :         const char *ext_str;
    2184             : #ifdef PHP_WIN32
    2185             :         char *save;
    2186             : #endif
    2187             :         int ext_len;
    2188             : 
    2189        2591 :         if (CHECK_NULL_PATH(filename, filename_len)) {
    2190           0 :                 return FAILURE;
    2191             :         }
    2192             : 
    2193        2591 :         if (CHECK_NULL_PATH(filename, filename_len)) {
    2194           0 :                 return FAILURE;
    2195             :         }
    2196             : 
    2197        2591 :         if (!strncasecmp(filename, "phar://", 7)) {
    2198        2122 :                 filename += 7;
    2199        2122 :                 filename_len -= 7;
    2200             :         }
    2201             : 
    2202        2591 :         ext_len = 0;
    2203             : #ifdef PHP_WIN32
    2204             :         save = filename;
    2205             :         filename = estrndup(filename, filename_len);
    2206             :         phar_unixify_path_separators(filename, filename_len);
    2207             : #endif
    2208        2591 :         if (phar_detect_phar_fname_ext(filename, filename_len, &ext_str, &ext_len, executable, for_create, 0) == FAILURE) {
    2209          98 :                 if (ext_len != -1) {
    2210          34 :                         if (!ext_str) {
    2211             :                                 /* no / detected, restore arch for error message */
    2212             : #ifdef PHP_WIN32
    2213             :                                 *arch = save;
    2214             : #else
    2215          20 :                                 *arch = (char*)filename;
    2216             : #endif
    2217             :                         }
    2218             : 
    2219             : #ifdef PHP_WIN32
    2220             :                         efree(filename);
    2221             : #endif
    2222          34 :                         return FAILURE;
    2223             :                 }
    2224             : 
    2225          64 :                 ext_len = 0;
    2226             :                 /* no extension detected - instead we are dealing with an alias */
    2227             :         }
    2228             : 
    2229        2557 :         *arch_len = ext_str - filename + ext_len;
    2230        2557 :         *arch = estrndup(filename, *arch_len);
    2231             : 
    2232        2557 :         if (ext_str[ext_len]) {
    2233        2080 :                 *entry_len = filename_len - *arch_len;
    2234        2080 :                 *entry = estrndup(ext_str+ext_len, *entry_len);
    2235             : #ifdef PHP_WIN32
    2236             :                 phar_unixify_path_separators(*entry, *entry_len);
    2237             : #endif
    2238        2080 :                 *entry = phar_fix_filepath(*entry, entry_len, 0);
    2239             :         } else {
    2240         477 :                 *entry_len = 1;
    2241         477 :                 *entry = estrndup("/", 1);
    2242             :         }
    2243             : 
    2244             : #ifdef PHP_WIN32
    2245             :         efree(filename);
    2246             : #endif
    2247             : 
    2248        2557 :         return SUCCESS;
    2249             : }
    2250             : /* }}} */
    2251             : 
    2252             : /**
    2253             :  * Invoked when a user calls Phar::mapPhar() from within an executing .phar
    2254             :  * to set up its manifest directly
    2255             :  */
    2256         135 : int phar_open_executed_filename(char *alias, int alias_len, char **error) /* {{{ */
    2257             : {
    2258             :         char *fname;
    2259             :         php_stream *fp;
    2260             :         int fname_len;
    2261         135 :         zend_string *actual = NULL;
    2262             :         int ret;
    2263             : 
    2264         135 :         if (error) {
    2265         135 :                 *error = NULL;
    2266             :         }
    2267             : 
    2268         135 :         fname = (char*)zend_get_executed_filename();
    2269         135 :         fname_len = strlen(fname);
    2270             : 
    2271         135 :         if (phar_open_parsed_phar(fname, fname_len, alias, alias_len, 0, REPORT_ERRORS, NULL, 0) == SUCCESS) {
    2272          62 :                 return SUCCESS;
    2273             :         }
    2274             : 
    2275          73 :         if (!strcmp(fname, "[no active file]")) {
    2276           0 :                 if (error) {
    2277           0 :                         spprintf(error, 0, "cannot initialize a phar outside of PHP execution");
    2278             :                 }
    2279           0 :                 return FAILURE;
    2280             :         }
    2281             : 
    2282          73 :         if (0 == zend_get_constant_str("__COMPILER_HALT_OFFSET__", sizeof("__COMPILER_HALT_OFFSET__")-1)) {
    2283           1 :                 if (error) {
    2284           1 :                         spprintf(error, 0, "__HALT_COMPILER(); must be declared in a phar");
    2285             :                 }
    2286           1 :                 return FAILURE;
    2287             :         }
    2288             : 
    2289          72 :         if (php_check_open_basedir(fname)) {
    2290           0 :                 return FAILURE;
    2291             :         }
    2292             : 
    2293          72 :         fp = php_stream_open_wrapper(fname, "rb", IGNORE_URL|STREAM_MUST_SEEK|REPORT_ERRORS, &actual);
    2294             : 
    2295          72 :         if (!fp) {
    2296           0 :                 if (error) {
    2297           0 :                         spprintf(error, 0, "unable to open phar for reading \"%s\"", fname);
    2298             :                 }
    2299           0 :                 if (actual) {
    2300           0 :                         zend_string_release(actual);
    2301             :                 }
    2302           0 :                 return FAILURE;
    2303             :         }
    2304             : 
    2305          72 :         if (actual) {
    2306          72 :                 fname = ZSTR_VAL(actual);
    2307          72 :                 fname_len = ZSTR_LEN(actual);
    2308             :         }
    2309             : 
    2310          72 :         ret = phar_open_from_fp(fp, fname, fname_len, alias, alias_len, REPORT_ERRORS, NULL, 0, error);
    2311             : 
    2312          72 :         if (actual) {
    2313          72 :                 zend_string_release(actual);
    2314             :         }
    2315             : 
    2316          72 :         return ret;
    2317             : }
    2318             : /* }}} */
    2319             : 
    2320             : /**
    2321             :  * Validate the CRC32 of a file opened from within the phar
    2322             :  */
    2323        1249 : int phar_postprocess_file(phar_entry_data *idata, uint32_t crc32, char **error, int process_zip) /* {{{ */
    2324             : {
    2325        1249 :         uint32_t crc = ~0;
    2326        1249 :         int len = idata->internal_file->uncompressed_filesize;
    2327        1249 :         php_stream *fp = idata->fp;
    2328        1249 :         phar_entry_info *entry = idata->internal_file;
    2329             : 
    2330        1249 :         if (error) {
    2331        1249 :                 *error = NULL;
    2332             :         }
    2333             : 
    2334        1249 :         if (entry->is_zip && process_zip > 0) {
    2335             :                 /* verify local file header */
    2336             :                 phar_zip_file_header local;
    2337             :                 phar_zip_data_desc desc;
    2338             : 
    2339         175 :                 if (SUCCESS != phar_open_archive_fp(idata->phar)) {
    2340           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);
    2341           0 :                         return FAILURE;
    2342             :                 }
    2343         175 :                 php_stream_seek(phar_get_entrypfp(idata->internal_file), entry->header_offset, SEEK_SET);
    2344             : 
    2345         175 :                 if (sizeof(local) != php_stream_read(phar_get_entrypfp(idata->internal_file), (char *) &local, sizeof(local))) {
    2346             : 
    2347           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);
    2348           0 :                         return FAILURE;
    2349             :                 }
    2350             : 
    2351             :                 /* check for data descriptor */
    2352         175 :                 if (((PHAR_ZIP_16(local.flags)) & 0x8) == 0x8) {
    2353           1 :                         php_stream_seek(phar_get_entrypfp(idata->internal_file),
    2354             :                                         entry->header_offset + sizeof(local) +
    2355             :                                         PHAR_ZIP_16(local.filename_len) +
    2356             :                                         PHAR_ZIP_16(local.extra_len) +
    2357             :                                         entry->compressed_filesize, SEEK_SET);
    2358           1 :                         if (sizeof(desc) != php_stream_read(phar_get_entrypfp(idata->internal_file),
    2359             :                                                             (char *) &desc, sizeof(desc))) {
    2360           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);
    2361           0 :                                 return FAILURE;
    2362             :                         }
    2363           2 :                         if (desc.signature[0] == 'P' && desc.signature[1] == 'K') {
    2364           1 :                                 memcpy(&(local.crc32), &(desc.crc32), 12);
    2365             :                         } else {
    2366             :                                 /* old data descriptors have no signature */
    2367           0 :                                 memcpy(&(local.crc32), &desc, 12);
    2368             :                         }
    2369             :                 }
    2370             :                 /* verify local header */
    2371         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)) {
    2372           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);
    2373           0 :                         return FAILURE;
    2374             :                 }
    2375             : 
    2376             :                 /* construct actual offset to file start - local extra_len can be different from central extra_len */
    2377         175 :                 entry->offset = entry->offset_abs =
    2378         175 :                         sizeof(local) + entry->header_offset + PHAR_ZIP_16(local.filename_len) + PHAR_ZIP_16(local.extra_len);
    2379             : 
    2380         175 :                 if (idata->zero && idata->zero != entry->offset_abs) {
    2381           2 :                         idata->zero = entry->offset_abs;
    2382             :                 }
    2383             :         }
    2384             : 
    2385        1249 :         if (process_zip == 1) {
    2386        1059 :                 return SUCCESS;
    2387             :         }
    2388             : 
    2389         190 :         php_stream_seek(fp, idata->zero, SEEK_SET);
    2390             : 
    2391       17992 :         while (len--) {
    2392       17612 :                 CRC32(crc, php_stream_getc(fp));
    2393             :         }
    2394             : 
    2395         190 :         php_stream_seek(fp, idata->zero, SEEK_SET);
    2396             : 
    2397         190 :         if (~crc == crc32) {
    2398         188 :                 entry->is_crc_checked = 1;
    2399         188 :                 return SUCCESS;
    2400             :         } else {
    2401           2 :                 spprintf(error, 0, "phar error: internal corruption of phar \"%s\" (crc32 mismatch on file \"%s\")", idata->phar->fname, entry->filename);
    2402           2 :                 return FAILURE;
    2403             :         }
    2404             : }
    2405             : /* }}} */
    2406             : 
    2407      127682 : static inline void phar_set_32(char *buffer, int var) /* {{{ */
    2408             : {
    2409             : #ifdef WORDS_BIGENDIAN
    2410             :         *((buffer) + 3) = (unsigned char) (((var) >> 24) & 0xFF);
    2411             :         *((buffer) + 2) = (unsigned char) (((var) >> 16) & 0xFF);
    2412             :         *((buffer) + 1) = (unsigned char) (((var) >> 8) & 0xFF);
    2413             :         *((buffer) + 0) = (unsigned char) ((var) & 0xFF);
    2414             : #else
    2415      127682 :          memcpy(buffer, &var, sizeof(var));
    2416             : #endif
    2417      127682 : } /* }}} */
    2418             : 
    2419       14390 : static int phar_flush_clean_deleted_apply(zval *zv) /* {{{ */
    2420             : {
    2421       14390 :         phar_entry_info *entry = (phar_entry_info *)Z_PTR_P(zv);
    2422             : 
    2423       14390 :         if (entry->fp_refcount <= 0 && entry->is_deleted) {
    2424          10 :                 return ZEND_HASH_APPLY_REMOVE;
    2425             :         } else {
    2426       14380 :                 return ZEND_HASH_APPLY_KEEP;
    2427             :         }
    2428             : }
    2429             : /* }}} */
    2430             : 
    2431             : #include "stub.h"
    2432             : 
    2433          97 : zend_string *phar_create_default_stub(const char *index_php, const char *web_index, char **error) /* {{{ */
    2434             : {
    2435             :         int index_len, web_len;
    2436             : 
    2437          97 :         if (error) {
    2438          16 :                 *error = NULL;
    2439             :         }
    2440             : 
    2441          97 :         if (!index_php) {
    2442          87 :                 index_php = "index.php";
    2443             :         }
    2444             : 
    2445          97 :         if (!web_index) {
    2446          93 :                 web_index = "index.php";
    2447             :         }
    2448             : 
    2449          97 :         index_len = strlen(index_php);
    2450          97 :         web_len = strlen(web_index);
    2451             : 
    2452          97 :         if (index_len > 400) {
    2453             :                 /* ridiculous size not allowed for index.php startup filename */
    2454           2 :                 if (error) {
    2455           2 :                         spprintf(error, 0, "Illegal filename passed in for stub creation, was %d characters long, and only 400 or less is allowed", index_len);
    2456           2 :                         return NULL;
    2457             :                 }
    2458             :         }
    2459             : 
    2460          95 :         if (web_len > 400) {
    2461             :                 /* ridiculous size not allowed for index.php startup filename */
    2462           1 :                 if (error) {
    2463           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);
    2464           1 :                         return NULL;
    2465             :                 }
    2466             :         }
    2467             : 
    2468          94 :         return phar_get_stub(index_php, web_index, index_len+1, web_len+1);
    2469             : }
    2470             : /* }}} */
    2471             : 
    2472             : /**
    2473             :  * Save phar contents to disk
    2474             :  *
    2475             :  * user_stub contains either a string, or a resource pointer, if len is a negative length.
    2476             :  * user_stub and len should be both 0 if the default or existing stub should be used
    2477             :  */
    2478        4922 : int phar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int convert, char **error) /* {{{ */
    2479             : {
    2480        4922 :         char halt_stub[] = "__HALT_COMPILER();";
    2481             :         zend_string *newstub;
    2482             :         char *tmp;
    2483             :         phar_entry_info *entry, *newentry;
    2484        4922 :         int halt_offset, restore_alias_len, global_flags = 0, closeoldfile;
    2485        4922 :         char *pos, has_dirs = 0;
    2486             :         char manifest[18], entry_buffer[24];
    2487             :         zend_off_t manifest_ftell;
    2488             :         zend_long offset;
    2489             :         size_t wrote;
    2490             :         uint32_t manifest_len, mytime, loc, new_manifest_count;
    2491             :         uint32_t newcrc32;
    2492             :         php_stream *file, *oldfile, *newfile, *stubfile;
    2493             :         php_stream_filter *filter;
    2494             :         php_serialize_data_t metadata_hash;
    2495        4922 :         smart_str main_metadata_str = {0};
    2496        4922 :         int free_user_stub, free_fp = 1, free_ufp = 1;
    2497        4922 :         int manifest_hack = 0;
    2498             : 
    2499        4922 :         if (phar->is_persistent) {
    2500           0 :                 if (error) {
    2501           0 :                         spprintf(error, 0, "internal error: attempt to flush cached zip-based phar \"%s\"", phar->fname);
    2502             :                 }
    2503           0 :                 return EOF;
    2504             :         }
    2505             : 
    2506        4922 :         if (error) {
    2507        4922 :                 *error = NULL;
    2508             :         }
    2509             : 
    2510        4922 :         if (!zend_hash_num_elements(&phar->manifest) && !user_stub) {
    2511          14 :                 return EOF;
    2512             :         }
    2513             : 
    2514        4908 :         zend_hash_clean(&phar->virtual_dirs);
    2515             : 
    2516        4908 :         if (phar->is_zip) {
    2517         203 :                 return phar_zip_flush(phar, user_stub, len, convert, error);
    2518             :         }
    2519             : 
    2520        4705 :         if (phar->is_tar) {
    2521         195 :                 return phar_tar_flush(phar, user_stub, len, convert, error);
    2522             :         }
    2523             : 
    2524        4510 :         if (PHAR_G(readonly)) {
    2525           0 :                 return EOF;
    2526             :         }
    2527             : 
    2528        8932 :         if (phar->fp && !phar->is_brandnew) {
    2529        4422 :                 oldfile = phar->fp;
    2530        4422 :                 closeoldfile = 0;
    2531        4422 :                 php_stream_rewind(oldfile);
    2532             :         } else {
    2533          88 :                 oldfile = php_stream_open_wrapper(phar->fname, "rb", 0, NULL);
    2534          88 :                 closeoldfile = oldfile != NULL;
    2535             :         }
    2536        4510 :         newfile = php_stream_fopen_tmpfile();
    2537        4510 :         if (!newfile) {
    2538           0 :                 if (error) {
    2539           0 :                         spprintf(error, 0, "unable to create temporary file");
    2540             :                 }
    2541           0 :                 if (closeoldfile) {
    2542           0 :                         php_stream_close(oldfile);
    2543             :                 }
    2544           0 :                 return EOF;
    2545             :         }
    2546             : 
    2547        4510 :         if (user_stub) {
    2548             :                 zend_string *suser_stub;
    2549          29 :                 if (len < 0) {
    2550             :                         /* resource passed in */
    2551           4 :                         if (!(php_stream_from_zval_no_verify(stubfile, (zval *)user_stub))) {
    2552           0 :                                 if (closeoldfile) {
    2553           0 :                                         php_stream_close(oldfile);
    2554             :                                 }
    2555           0 :                                 php_stream_close(newfile);
    2556           0 :                                 if (error) {
    2557           0 :                                         spprintf(error, 0, "unable to access resource to copy stub to new phar \"%s\"", phar->fname);
    2558             :                                 }
    2559           0 :                                 return EOF;
    2560             :                         }
    2561           4 :                         if (len == -1) {
    2562           3 :                                 len = PHP_STREAM_COPY_ALL;
    2563             :                         } else {
    2564           1 :                                 len = -len;
    2565             :                         }
    2566           4 :                         user_stub = 0;
    2567             : 
    2568           4 :                         if (!(suser_stub = php_stream_copy_to_mem(stubfile, len, 0))) {
    2569           0 :                                 if (closeoldfile) {
    2570           0 :                                         php_stream_close(oldfile);
    2571             :                                 }
    2572           0 :                                 php_stream_close(newfile);
    2573           0 :                                 if (error) {
    2574           0 :                                         spprintf(error, 0, "unable to read resource to copy stub to new phar \"%s\"", phar->fname);
    2575             :                                 }
    2576           0 :                                 return EOF;
    2577             :                         }
    2578           4 :                         free_user_stub = 1;
    2579           4 :                         user_stub = ZSTR_VAL(suser_stub);
    2580           4 :                         len = ZSTR_LEN(suser_stub);
    2581             :                 } else {
    2582          25 :                         free_user_stub = 0;
    2583             :                 }
    2584          29 :                 tmp = estrndup(user_stub, len);
    2585          29 :                 if ((pos = php_stristr(tmp, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) {
    2586           1 :                         efree(tmp);
    2587           1 :                         if (closeoldfile) {
    2588           0 :                                 php_stream_close(oldfile);
    2589             :                         }
    2590           1 :                         php_stream_close(newfile);
    2591           1 :                         if (error) {
    2592           1 :                                 spprintf(error, 0, "illegal stub for phar \"%s\"", phar->fname);
    2593             :                         }
    2594           1 :                         if (free_user_stub) {
    2595             :                                 zend_string_free(suser_stub);
    2596             :                         }
    2597           1 :                         return EOF;
    2598             :                 }
    2599          28 :                 pos = user_stub + (pos - tmp);
    2600          28 :                 efree(tmp);
    2601          28 :                 len = pos - user_stub + 18;
    2602          56 :                 if ((size_t)len != php_stream_write(newfile, user_stub, len)
    2603          56 :                 ||                        5 != php_stream_write(newfile, " ?>\r\n", 5)) {
    2604           0 :                         if (closeoldfile) {
    2605           0 :                                 php_stream_close(oldfile);
    2606             :                         }
    2607           0 :                         php_stream_close(newfile);
    2608           0 :                         if (error) {
    2609           0 :                                 spprintf(error, 0, "unable to create stub from string in new phar \"%s\"", phar->fname);
    2610             :                         }
    2611           0 :                         if (free_user_stub) {
    2612             :                                 zend_string_free(suser_stub);
    2613             :                         }
    2614           0 :                         return EOF;
    2615             :                 }
    2616          28 :                 phar->halt_offset = len + 5;
    2617          28 :                 if (free_user_stub) {
    2618             :                         zend_string_free(suser_stub);
    2619             :                 }
    2620             :         } else {
    2621             :                 size_t written;
    2622             : 
    2623        8881 :                 if (!user_stub && phar->halt_offset && oldfile && !phar->is_brandnew) {
    2624        4400 :                         php_stream_copy_to_stream_ex(oldfile, newfile, phar->halt_offset, &written);
    2625        4400 :                         newstub = NULL;
    2626             :                 } else {
    2627             :                         /* this is either a brand new phar or a default stub overwrite */
    2628          81 :                         newstub = phar_create_default_stub(NULL, NULL, NULL);
    2629          81 :                         phar->halt_offset = ZSTR_LEN(newstub);
    2630          81 :                         written = php_stream_write(newfile, ZSTR_VAL(newstub), phar->halt_offset);
    2631             :                 }
    2632        4481 :                 if (phar->halt_offset != written) {
    2633           0 :                         if (closeoldfile) {
    2634           0 :                                 php_stream_close(oldfile);
    2635             :                         }
    2636           0 :                         php_stream_close(newfile);
    2637           0 :                         if (error) {
    2638           0 :                                 if (newstub) {
    2639           0 :                                         spprintf(error, 0, "unable to create stub in new phar \"%s\"", phar->fname);
    2640             :                                 } else {
    2641           0 :                                         spprintf(error, 0, "unable to copy stub of old phar to new phar \"%s\"", phar->fname);
    2642             :                                 }
    2643             :                         }
    2644           0 :                         if (newstub) {
    2645             :                                 zend_string_free(newstub);
    2646             :                         }
    2647           0 :                         return EOF;
    2648             :                 }
    2649        4481 :                 if (newstub) {
    2650             :                         zend_string_free(newstub);
    2651             :                 }
    2652             :         }
    2653        4509 :         manifest_ftell = php_stream_tell(newfile);
    2654        4509 :         halt_offset = manifest_ftell;
    2655             : 
    2656             :         /* Check whether we can get rid of some of the deleted entries which are
    2657             :          * unused. However some might still be in use so even after this clean-up
    2658             :          * we need to skip entries marked is_deleted. */
    2659        4509 :         zend_hash_apply(&phar->manifest, phar_flush_clean_deleted_apply);
    2660             : 
    2661             :         /* compress as necessary, calculate crcs, serialize meta-data, manifest size, and file sizes */
    2662        4509 :         main_metadata_str.s = NULL;
    2663        9018 :         if (Z_TYPE(phar->metadata) != IS_UNDEF) {
    2664          13 :                 PHP_VAR_SERIALIZE_INIT(metadata_hash);
    2665          13 :                 php_var_serialize(&main_metadata_str, &phar->metadata, &metadata_hash);
    2666          13 :                 PHP_VAR_SERIALIZE_DESTROY(metadata_hash);
    2667             :         }
    2668        4509 :         new_manifest_count = 0;
    2669        4509 :         offset = 0;
    2670       23398 :         for (zend_hash_internal_pointer_reset(&phar->manifest);
    2671       18889 :                 zend_hash_has_more_elements(&phar->manifest) == SUCCESS;
    2672       14380 :                 zend_hash_move_forward(&phar->manifest)) {
    2673       28760 :                 if ((entry = zend_hash_get_current_data_ptr(&phar->manifest)) == NULL) {
    2674           0 :                         continue;
    2675             :                 }
    2676       14380 :                 if (entry->cfp) {
    2677             :                         /* did we forget to get rid of cfp last time? */
    2678           0 :                         php_stream_close(entry->cfp);
    2679           0 :                         entry->cfp = 0;
    2680             :                 }
    2681       14380 :                 if (entry->is_deleted || entry->is_mounted) {
    2682             :                         /* remove this from the new phar */
    2683           5 :                         continue;
    2684             :                 }
    2685       14375 :                 if (!entry->is_modified && entry->fp_refcount) {
    2686             :                         /* open file pointers refer to this fp, do not free the stream */
    2687           2 :                         switch (entry->fp_type) {
    2688             :                                 case PHAR_FP:
    2689           2 :                                         free_fp = 0;
    2690           2 :                                         break;
    2691             :                                 case PHAR_UFP:
    2692           0 :                                         free_ufp = 0;
    2693             :                                 default:
    2694             :                                         break;
    2695             :                         }
    2696             :                 }
    2697             :                 /* after excluding deleted files, calculate manifest size in bytes and number of entries */
    2698       14375 :                 ++new_manifest_count;
    2699       14375 :                 phar_add_virtual_dirs(phar, entry->filename, entry->filename_len);
    2700             : 
    2701       14375 :                 if (entry->is_dir) {
    2702             :                         /* we use this to calculate API version, 1.1.1 is used for phars with directories */
    2703          27 :                         has_dirs = 1;
    2704             :                 }
    2705       28750 :                 if (Z_TYPE(entry->metadata) != IS_UNDEF) {
    2706          78 :                         if (entry->metadata_str.s) {
    2707          53 :                                 smart_str_free(&entry->metadata_str);
    2708             :                         }
    2709          78 :                         entry->metadata_str.s = NULL;
    2710          78 :                         PHP_VAR_SERIALIZE_INIT(metadata_hash);
    2711          78 :                         php_var_serialize(&entry->metadata_str, &entry->metadata, &metadata_hash);
    2712          78 :                         PHP_VAR_SERIALIZE_DESTROY(metadata_hash);
    2713             :                 } else {
    2714       14297 :                         if (entry->metadata_str.s) {
    2715           2 :                                 smart_str_free(&entry->metadata_str);
    2716             :                         }
    2717       14297 :                         entry->metadata_str.s = NULL;
    2718             :                 }
    2719             : 
    2720             :                 /* 32 bits for filename length, length of filename, manifest + metadata, and add 1 for trailing / if a directory */
    2721       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);
    2722             : 
    2723             :                 /* compress and rehash as necessary */
    2724       14375 :                 if ((oldfile && !entry->is_modified) || entry->is_dir) {
    2725        6848 :                         if (entry->fp_type == PHAR_UFP) {
    2726             :                                 /* reset so we can copy the compressed data over */
    2727           1 :                                 entry->fp_type = PHAR_FP;
    2728             :                         }
    2729        6848 :                         continue;
    2730             :                 }
    2731        7527 :                 if (!phar_get_efp(entry, 0)) {
    2732             :                         /* re-open internal file pointer just-in-time */
    2733           1 :                         newentry = phar_open_jit(phar, entry, error);
    2734           1 :                         if (!newentry) {
    2735             :                                 /* major problem re-opening, so we ignore this file and the error */
    2736           1 :                                 efree(*error);
    2737           1 :                                 *error = NULL;
    2738           1 :                                 continue;
    2739             :                         }
    2740           0 :                         entry = newentry;
    2741             :                 }
    2742        7526 :                 file = phar_get_efp(entry, 0);
    2743        7526 :                 if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 1)) {
    2744           0 :                         if (closeoldfile) {
    2745           0 :                                 php_stream_close(oldfile);
    2746             :                         }
    2747           0 :                         php_stream_close(newfile);
    2748           0 :                         if (error) {
    2749           0 :                                 spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
    2750             :                         }
    2751           0 :                         return EOF;
    2752             :                 }
    2753        7526 :                 newcrc32 = ~0;
    2754        7526 :                 mytime = entry->uncompressed_filesize;
    2755       98547 :                 for (loc = 0;loc < mytime; ++loc) {
    2756       91021 :                         CRC32(newcrc32, php_stream_getc(file));
    2757             :                 }
    2758        7526 :                 entry->crc32 = ~newcrc32;
    2759        7526 :                 entry->is_crc_checked = 1;
    2760        7526 :                 if (!(entry->flags & PHAR_ENT_COMPRESSION_MASK)) {
    2761             :                         /* not compressed */
    2762        7495 :                         entry->compressed_filesize = entry->uncompressed_filesize;
    2763        7495 :                         continue;
    2764             :                 }
    2765          31 :                 filter = php_stream_filter_create(phar_compress_filter(entry, 0), NULL, 0);
    2766          31 :                 if (!filter) {
    2767           0 :                         if (closeoldfile) {
    2768           0 :                                 php_stream_close(oldfile);
    2769             :                         }
    2770           0 :                         php_stream_close(newfile);
    2771           0 :                         if (entry->flags & PHAR_ENT_COMPRESSED_GZ) {
    2772           0 :                                 if (error) {
    2773           0 :                                         spprintf(error, 0, "unable to gzip compress file \"%s\" to new phar \"%s\"", entry->filename, phar->fname);
    2774             :                                 }
    2775             :                         } else {
    2776           0 :                                 if (error) {
    2777           0 :                                         spprintf(error, 0, "unable to bzip2 compress file \"%s\" to new phar \"%s\"", entry->filename, phar->fname);
    2778             :                                 }
    2779             :                         }
    2780           0 :                         return EOF;
    2781             :                 }
    2782             : 
    2783             :                 /* create new file that holds the compressed version */
    2784             :                 /* work around inability to specify freedom in write and strictness
    2785             :                 in read count */
    2786          31 :                 entry->cfp = php_stream_fopen_tmpfile();
    2787          31 :                 if (!entry->cfp) {
    2788           0 :                         if (error) {
    2789           0 :                                 spprintf(error, 0, "unable to create temporary file");
    2790             :                         }
    2791           0 :                         if (closeoldfile) {
    2792           0 :                                 php_stream_close(oldfile);
    2793             :                         }
    2794           0 :                         php_stream_close(newfile);
    2795           0 :                         return EOF;
    2796             :                 }
    2797          31 :                 php_stream_flush(file);
    2798          31 :                 if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 0)) {
    2799           0 :                         if (closeoldfile) {
    2800           0 :                                 php_stream_close(oldfile);
    2801             :                         }
    2802           0 :                         php_stream_close(newfile);
    2803           0 :                         if (error) {
    2804           0 :                                 spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
    2805             :                         }
    2806           0 :                         return EOF;
    2807             :                 }
    2808          31 :                 php_stream_filter_append((&entry->cfp->writefilters), filter);
    2809          31 :                 if (SUCCESS != php_stream_copy_to_stream_ex(file, entry->cfp, entry->uncompressed_filesize, NULL)) {
    2810           0 :                         if (closeoldfile) {
    2811           0 :                                 php_stream_close(oldfile);
    2812             :                         }
    2813           0 :                         php_stream_close(newfile);
    2814           0 :                         if (error) {
    2815           0 :                                 spprintf(error, 0, "unable to copy compressed file contents of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
    2816             :                         }
    2817           0 :                         return EOF;
    2818             :                 }
    2819          31 :                 php_stream_filter_flush(filter, 1);
    2820          31 :                 php_stream_flush(entry->cfp);
    2821          31 :                 php_stream_filter_remove(filter, 1);
    2822          31 :                 php_stream_seek(entry->cfp, 0, SEEK_END);
    2823          31 :                 entry->compressed_filesize = (uint32_t) php_stream_tell(entry->cfp);
    2824             :                 /* generate crc on compressed file */
    2825          31 :                 php_stream_rewind(entry->cfp);
    2826          31 :                 entry->old_flags = entry->flags;
    2827          31 :                 entry->is_modified = 1;
    2828          31 :                 global_flags |= (entry->flags & PHAR_ENT_COMPRESSION_MASK);
    2829             :         }
    2830        4509 :         global_flags |= PHAR_HDR_SIGNATURE;
    2831             : 
    2832             :         /* write out manifest pre-header */
    2833             :         /*  4: manifest length
    2834             :          *  4: manifest entry count
    2835             :          *  2: phar version
    2836             :          *  4: phar global flags
    2837             :          *  4: alias length
    2838             :          *  ?: the alias itself
    2839             :          *  4: phar metadata length
    2840             :          *  ?: phar metadata
    2841             :          */
    2842        4509 :         restore_alias_len = phar->alias_len;
    2843        4509 :         if (phar->is_temporary_alias) {
    2844         305 :                 phar->alias_len = 0;
    2845             :         }
    2846             : 
    2847        4509 :         manifest_len = offset + phar->alias_len + sizeof(manifest) + (main_metadata_str.s ? ZSTR_LEN(main_metadata_str.s) : 0);
    2848        4509 :         phar_set_32(manifest, manifest_len);
    2849             :         /* Hack - see bug #65028, add padding byte to the end of the manifest */
    2850        4509 :         if(manifest[0] == '\r' || manifest[0] == '\n') {
    2851           3 :                 manifest_len++;
    2852           3 :                 phar_set_32(manifest, manifest_len);
    2853           3 :                 manifest_hack = 1;
    2854             :         }
    2855        4509 :         phar_set_32(manifest+4, new_manifest_count);
    2856        4509 :         if (has_dirs) {
    2857          26 :                 *(manifest + 8) = (unsigned char) (((PHAR_API_VERSION) >> 8) & 0xFF);
    2858          26 :                 *(manifest + 9) = (unsigned char) (((PHAR_API_VERSION) & 0xF0));
    2859             :         } else {
    2860        4483 :                 *(manifest + 8) = (unsigned char) (((PHAR_API_VERSION_NODIR) >> 8) & 0xFF);
    2861        4483 :                 *(manifest + 9) = (unsigned char) (((PHAR_API_VERSION_NODIR) & 0xF0));
    2862             :         }
    2863        4509 :         phar_set_32(manifest+10, global_flags);
    2864        4509 :         phar_set_32(manifest+14, phar->alias_len);
    2865             : 
    2866             :         /* write the manifest header */
    2867        9018 :         if (sizeof(manifest) != php_stream_write(newfile, manifest, sizeof(manifest))
    2868        9018 :         || (size_t)phar->alias_len != php_stream_write(newfile, phar->alias, phar->alias_len)) {
    2869             : 
    2870           0 :                 if (closeoldfile) {
    2871           0 :                         php_stream_close(oldfile);
    2872             :                 }
    2873             : 
    2874           0 :                 php_stream_close(newfile);
    2875           0 :                 phar->alias_len = restore_alias_len;
    2876             : 
    2877           0 :                 if (error) {
    2878           0 :                         spprintf(error, 0, "unable to write manifest header of new phar \"%s\"", phar->fname);
    2879             :                 }
    2880             : 
    2881           0 :                 return EOF;
    2882             :         }
    2883             : 
    2884        4509 :         phar->alias_len = restore_alias_len;
    2885             : 
    2886        4509 :         phar_set_32(manifest, main_metadata_str.s ? ZSTR_LEN(main_metadata_str.s) : 0);
    2887        4522 :         if (4 != php_stream_write(newfile, manifest, 4) || ((main_metadata_str.s ? ZSTR_LEN(main_metadata_str.s) : 0)
    2888          13 :         && ZSTR_LEN(main_metadata_str.s) != php_stream_write(newfile, ZSTR_VAL(main_metadata_str.s), ZSTR_LEN(main_metadata_str.s)))) {
    2889             :                 smart_str_free(&main_metadata_str);
    2890             : 
    2891           0 :                 if (closeoldfile) {
    2892           0 :                         php_stream_close(oldfile);
    2893             :                 }
    2894             : 
    2895           0 :                 php_stream_close(newfile);
    2896           0 :                 phar->alias_len = restore_alias_len;
    2897             : 
    2898           0 :                 if (error) {
    2899           0 :                         spprintf(error, 0, "unable to write manifest meta-data of new phar \"%s\"", phar->fname);
    2900             :                 }
    2901             : 
    2902           0 :                 return EOF;
    2903             :         }
    2904             :         smart_str_free(&main_metadata_str);
    2905             : 
    2906             :         /* re-calculate the manifest location to simplify later code */
    2907        4509 :         manifest_ftell = php_stream_tell(newfile);
    2908             : 
    2909             :         /* now write the manifest */
    2910       23398 :         for (zend_hash_internal_pointer_reset(&phar->manifest);
    2911       18889 :                 zend_hash_has_more_elements(&phar->manifest) == SUCCESS;
    2912       14380 :                 zend_hash_move_forward(&phar->manifest)) {
    2913             : 
    2914       28760 :                 if ((entry = zend_hash_get_current_data_ptr(&phar->manifest)) == NULL) {
    2915           0 :                         continue;
    2916             :                 }
    2917             : 
    2918       14380 :                 if (entry->is_deleted || entry->is_mounted) {
    2919             :                         /* remove this from the new phar if deleted, ignore if mounted */
    2920           5 :                         continue;
    2921             :                 }
    2922             : 
    2923       14375 :                 if (entry->is_dir) {
    2924             :                         /* add 1 for trailing slash */
    2925          27 :                         phar_set_32(entry_buffer, entry->filename_len + 1);
    2926             :                 } else {
    2927       14348 :                         phar_set_32(entry_buffer, entry->filename_len);
    2928             :                 }
    2929             : 
    2930       43152 :                 if (4 != php_stream_write(newfile, entry_buffer, 4)
    2931       43125 :                 || entry->filename_len != php_stream_write(newfile, entry->filename, entry->filename_len)
    2932       57527 :                 || (entry->is_dir && 1 != php_stream_write(newfile, "/", 1))) {
    2933           0 :                         if (closeoldfile) {
    2934           0 :                                 php_stream_close(oldfile);
    2935             :                         }
    2936           0 :                         php_stream_close(newfile);
    2937           0 :                         if (error) {
    2938           0 :                                 if (entry->is_dir) {
    2939           0 :                                         spprintf(error, 0, "unable to write filename of directory \"%s\" to manifest of new phar \"%s\"", entry->filename, phar->fname);
    2940             :                                 } else {
    2941           0 :                                         spprintf(error, 0, "unable to write filename of file \"%s\" to manifest of new phar \"%s\"", entry->filename, phar->fname);
    2942             :                                 }
    2943             :                         }
    2944           0 :                         return EOF;
    2945             :                 }
    2946             : 
    2947             :                 /* set the manifest meta-data:
    2948             :                         4: uncompressed filesize
    2949             :                         4: creation timestamp
    2950             :                         4: compressed filesize
    2951             :                         4: crc32
    2952             :                         4: flags
    2953             :                         4: metadata-len
    2954             :                         +: metadata
    2955             :                 */
    2956       14375 :                 mytime = time(NULL);
    2957       14375 :                 phar_set_32(entry_buffer, entry->uncompressed_filesize);
    2958       14375 :                 phar_set_32(entry_buffer+4, mytime);
    2959       14375 :                 phar_set_32(entry_buffer+8, entry->compressed_filesize);
    2960       14375 :                 phar_set_32(entry_buffer+12, entry->crc32);
    2961       14375 :                 phar_set_32(entry_buffer+16, entry->flags);
    2962       14375 :                 phar_set_32(entry_buffer+20, entry->metadata_str.s ? ZSTR_LEN(entry->metadata_str.s) : 0);
    2963             : 
    2964       28828 :                 if (sizeof(entry_buffer) != php_stream_write(newfile, entry_buffer, sizeof(entry_buffer))
    2965       28750 :                 || (entry->metadata_str.s &&
    2966          78 :                     ZSTR_LEN(entry->metadata_str.s) != php_stream_write(newfile, ZSTR_VAL(entry->metadata_str.s), ZSTR_LEN(entry->metadata_str.s)))) {
    2967           0 :                         if (closeoldfile) {
    2968           0 :                                 php_stream_close(oldfile);
    2969             :                         }
    2970             : 
    2971           0 :                         php_stream_close(newfile);
    2972             : 
    2973           0 :                         if (error) {
    2974           0 :                                 spprintf(error, 0, "unable to write temporary manifest of file \"%s\" to manifest of new phar \"%s\"", entry->filename, phar->fname);
    2975             :                         }
    2976             : 
    2977           0 :                         return EOF;
    2978             :                 }
    2979             :         }
    2980             :         /* Hack - see bug #65028, add padding byte to the end of the manifest */
    2981        4509 :         if(manifest_hack) {
    2982           3 :                 if(1 != php_stream_write(newfile, manifest, 1)) {
    2983           0 :                         if (closeoldfile) {
    2984           0 :                                 php_stream_close(oldfile);
    2985             :                         }
    2986             : 
    2987           0 :                         php_stream_close(newfile);
    2988             : 
    2989           0 :                         if (error) {
    2990           0 :                                 spprintf(error, 0, "unable to write manifest padding byte");
    2991             :                         }
    2992             : 
    2993           0 :                         return EOF;
    2994             :                 }
    2995             :         }
    2996             : 
    2997             :         /* now copy the actual file data to the new phar */
    2998        4509 :         offset = php_stream_tell(newfile);
    2999       23396 :         for (zend_hash_internal_pointer_reset(&phar->manifest);
    3000       18887 :                 zend_hash_has_more_elements(&phar->manifest) == SUCCESS;
    3001       14378 :                 zend_hash_move_forward(&phar->manifest)) {
    3002             : 
    3003       28758 :                 if ((entry = zend_hash_get_current_data_ptr(&phar->manifest)) == NULL) {
    3004           0 :                         continue;
    3005             :                 }
    3006             : 
    3007       14379 :                 if (entry->is_deleted || entry->is_dir || entry->is_mounted) {
    3008          32 :                         continue;
    3009             :                 }
    3010             : 
    3011       14347 :                 if (entry->cfp) {
    3012          31 :                         file = entry->cfp;
    3013          31 :                         php_stream_rewind(file);
    3014             :                 } else {
    3015       14316 :                         file = phar_get_efp(entry, 0);
    3016       14316 :                         if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 0)) {
    3017           1 :                                 if (closeoldfile) {
    3018           0 :                                         php_stream_close(oldfile);
    3019             :                                 }
    3020           1 :                                 php_stream_close(newfile);
    3021           1 :                                 if (error) {
    3022           1 :                                         spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
    3023             :                                 }
    3024           1 :                                 return EOF;
    3025             :                         }
    3026             :                 }
    3027             : 
    3028       14346 :                 if (!file) {
    3029           0 :                         if (closeoldfile) {
    3030           0 :                                 php_stream_close(oldfile);
    3031             :                         }
    3032           0 :                         php_stream_close(newfile);
    3033           0 :                         if (error) {
    3034           0 :                                 spprintf(error, 0, "unable to seek to start of file \"%s\" while creating new phar \"%s\"", entry->filename, phar->fname);
    3035             :                         }
    3036           0 :                         return EOF;
    3037             :                 }
    3038             : 
    3039             :                 /* this will have changed for all files that have either changed compression or been modified */
    3040       14346 :                 entry->offset = entry->offset_abs = offset;
    3041       14346 :                 offset += entry->compressed_filesize;
    3042       14346 :                 if (php_stream_copy_to_stream_ex(file, newfile, entry->compressed_filesize, &wrote) == FAILURE) {
    3043           0 :                         if (closeoldfile) {
    3044           0 :                                 php_stream_close(oldfile);
    3045             :                         }
    3046             : 
    3047           0 :                         php_stream_close(newfile);
    3048             : 
    3049           0 :                         if (error) {
    3050           0 :                                 spprintf(error, 0, "unable to write contents of file \"%s\" to new phar \"%s\"", entry->filename, phar->fname);
    3051             :                         }
    3052             : 
    3053           0 :                         return EOF;
    3054             :                 }
    3055             : 
    3056       14346 :                 entry->is_modified = 0;
    3057             : 
    3058       14346 :                 if (entry->cfp) {
    3059          31 :                         php_stream_close(entry->cfp);
    3060          31 :                         entry->cfp = NULL;
    3061             :                 }
    3062             : 
    3063       14346 :                 if (entry->fp_type == PHAR_MOD) {
    3064             :                         /* 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 */
    3065        4343 :                         if (entry->fp_refcount == 0 && entry->fp != phar->fp && entry->fp != phar->ufp) {
    3066        4307 :                                 php_stream_close(entry->fp);
    3067             :                         }
    3068             : 
    3069        4343 :                         entry->fp = NULL;
    3070        4343 :                         entry->fp_type = PHAR_FP;
    3071       10003 :                 } else if (entry->fp_type == PHAR_UFP) {
    3072        3092 :                         entry->fp_type = PHAR_FP;
    3073             :                 }
    3074             :         }
    3075             : 
    3076             :         /* append signature */
    3077        4508 :         if (global_flags & PHAR_HDR_SIGNATURE) {
    3078             :                 char sig_buf[4];
    3079             : 
    3080        4508 :                 php_stream_rewind(newfile);
    3081             : 
    3082        4508 :                 if (phar->signature) {
    3083        4390 :                         efree(phar->signature);
    3084        4390 :                         phar->signature = NULL;
    3085             :                 }
    3086             : 
    3087        4508 :                 switch(phar->sig_flags) {
    3088             : #ifndef PHAR_HASH_OK
    3089             :                         case PHAR_SIG_SHA512:
    3090             :                         case PHAR_SIG_SHA256:
    3091             :                                 if (closeoldfile) {
    3092             :                                         php_stream_close(oldfile);
    3093             :                                 }
    3094             :                                 php_stream_close(newfile);
    3095             :                                 if (error) {
    3096             :                                         spprintf(error, 0, "unable to write contents of file \"%s\" to new phar \"%s\" with requested hash type", entry->filename, phar->fname);
    3097             :                                 }
    3098             :                                 return EOF;
    3099             : #endif
    3100             :                         default: {
    3101        4508 :                                 char *digest = NULL;
    3102             :                                 int digest_len;
    3103             : 
    3104        4508 :                                 if (FAILURE == phar_create_signature(phar, newfile, &digest, &digest_len, error)) {
    3105           0 :                                         if (error) {
    3106           0 :                                                 char *save = *error;
    3107           0 :                                                 spprintf(error, 0, "phar error: unable to write signature: %s", save);
    3108           0 :                                                 efree(save);
    3109             :                                         }
    3110           0 :                                         if (digest) {
    3111           0 :                                                 efree(digest);
    3112             :                                         }
    3113           0 :                                         if (closeoldfile) {
    3114           0 :                                                 php_stream_close(oldfile);
    3115             :                                         }
    3116           0 :                                         php_stream_close(newfile);
    3117           0 :                                         return EOF;
    3118             :                                 }
    3119             : 
    3120        4508 :                                 php_stream_write(newfile, digest, digest_len);
    3121        4508 :                                 efree(digest);
    3122        4508 :                                 if (phar->sig_flags == PHAR_SIG_OPENSSL) {
    3123           1 :                                         phar_set_32(sig_buf, digest_len);
    3124           1 :                                         php_stream_write(newfile, sig_buf, 4);
    3125             :                                 }
    3126             :                                 break;
    3127             :                         }
    3128             :                 }
    3129        4508 :                 phar_set_32(sig_buf, phar->sig_flags);
    3130        4508 :                 php_stream_write(newfile, sig_buf, 4);
    3131        4508 :                 php_stream_write(newfile, "GBMB", 4);
    3132             :         }
    3133             : 
    3134             :         /* finally, close the temp file, rename the original phar,
    3135             :            move the temp to the old phar, unlink the old phar, and reload it into memory
    3136             :         */
    3137        4508 :         if (phar->fp && free_fp) {
    3138        4432 :                 php_stream_close(phar->fp);
    3139             :         }
    3140             : 
    3141        4508 :         if (phar->ufp) {
    3142          17 :                 if (free_ufp) {
    3143          17 :                         php_stream_close(phar->ufp);
    3144             :                 }
    3145          17 :                 phar->ufp = NULL;
    3146             :         }
    3147             : 
    3148        4508 :         if (closeoldfile) {
    3149          15 :                 php_stream_close(oldfile);
    3150             :         }
    3151             : 
    3152        4508 :         phar->internal_file_start = halt_offset + manifest_len + 4;
    3153        4508 :         phar->halt_offset = halt_offset;
    3154        4508 :         phar->is_brandnew = 0;
    3155             : 
    3156        4508 :         php_stream_rewind(newfile);
    3157             : 
    3158        4508 :         if (phar->donotflush) {
    3159             :                 /* deferred flush */
    3160          12 :                 phar->fp = newfile;
    3161             :         } else {
    3162        4496 :                 phar->fp = php_stream_open_wrapper(phar->fname, "w+b", IGNORE_URL|STREAM_MUST_SEEK|REPORT_ERRORS, NULL);
    3163        4496 :                 if (!phar->fp) {
    3164           0 :                         phar->fp = newfile;
    3165           0 :                         if (error) {
    3166           0 :                                 spprintf(error, 4096, "unable to open new phar \"%s\" for writing", phar->fname);
    3167             :                         }
    3168           0 :                         return EOF;
    3169             :                 }
    3170             : 
    3171        4496 :                 if (phar->flags & PHAR_FILE_COMPRESSED_GZ) {
    3172             :                         /* to properly compress, we have to tell zlib to add a zlib header */
    3173             :                         zval filterparams;
    3174             : 
    3175           5 :                         array_init(&filterparams);
    3176           5 :                         add_assoc_long(&filterparams, "window", MAX_WBITS+16);
    3177           5 :                         filter = php_stream_filter_create("zlib.deflate", &filterparams, php_stream_is_persistent(phar->fp));
    3178             :                         zval_dtor(&filterparams);
    3179             : 
    3180           5 :                         if (!filter) {
    3181           0 :                                 if (error) {
    3182           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);
    3183             :                                 }
    3184           0 :                                 return EOF;
    3185             :                         }
    3186             : 
    3187           5 :                         php_stream_filter_append(&phar->fp->writefilters, filter);
    3188           5 :                         php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
    3189           5 :                         php_stream_filter_flush(filter, 1);
    3190           5 :                         php_stream_filter_remove(filter, 1);
    3191           5 :                         php_stream_close(phar->fp);
    3192             :                         /* use the temp stream as our base */
    3193           5 :                         phar->fp = newfile;
    3194        4491 :                 } else if (phar->flags & PHAR_FILE_COMPRESSED_BZ2) {
    3195           3 :                         filter = php_stream_filter_create("bzip2.compress", NULL, php_stream_is_persistent(phar->fp));
    3196           3 :                         php_stream_filter_append(&phar->fp->writefilters, filter);
    3197           3 :                         php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
    3198           3 :                         php_stream_filter_flush(filter, 1);
    3199           3 :                         php_stream_filter_remove(filter, 1);
    3200           3 :                         php_stream_close(phar->fp);
    3201             :                         /* use the temp stream as our base */
    3202           3 :                         phar->fp = newfile;
    3203             :                 } else {
    3204        4488 :                         php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
    3205             :                         /* we could also reopen the file in "rb" mode but there is no need for that */
    3206        4488 :                         php_stream_close(newfile);
    3207             :                 }
    3208             :         }
    3209             : 
    3210        4508 :         if (-1 == php_stream_seek(phar->fp, phar->halt_offset, SEEK_SET)) {
    3211           0 :                 if (error) {
    3212           0 :                         spprintf(error, 0, "unable to seek to __HALT_COMPILER(); in new phar \"%s\"", phar->fname);
    3213             :                 }
    3214           0 :                 return EOF;
    3215             :         }
    3216             : 
    3217        4508 :         return EOF;
    3218             : }
    3219             : /* }}} */
    3220             : 
    3221             : #ifdef COMPILE_DL_PHAR
    3222             : #ifdef ZTS
    3223             : ZEND_TSRMLS_CACHE_DEFINE()
    3224             : #endif
    3225             : ZEND_GET_MODULE(phar)
    3226             : #endif
    3227             : 
    3228             : /* {{{ phar_functions[]
    3229             :  *
    3230             :  * Every user visible function must have an entry in phar_functions[].
    3231             :  */
    3232             : zend_function_entry phar_functions[] = {
    3233             :         PHP_FE_END
    3234             : };
    3235             : /* }}}*/
    3236             : 
    3237           4 : static size_t phar_zend_stream_reader(void *handle, char *buf, size_t len) /* {{{ */
    3238             : {
    3239           4 :         return php_stream_read(phar_get_pharfp((phar_archive_data*)handle), buf, len);
    3240             : }
    3241             : /* }}} */
    3242             : 
    3243           2 : static size_t phar_zend_stream_fsizer(void *handle) /* {{{ */
    3244             : {
    3245           2 :         return ((phar_archive_data*)handle)->halt_offset + 32;
    3246             : } /* }}} */
    3247             : 
    3248             : zend_op_array *(*phar_orig_compile_file)(zend_file_handle *file_handle, int type);
    3249             : #define phar_orig_zend_open zend_stream_open_function
    3250             : 
    3251       17227 : static zend_string *phar_resolve_path(const char *filename, int filename_len)
    3252             : {
    3253       17227 :         return phar_find_in_include_path((char *) filename, filename_len, NULL);
    3254             : }
    3255             : 
    3256       33339 : static zend_op_array *phar_compile_file(zend_file_handle *file_handle, int type) /* {{{ */
    3257             : {
    3258             :         zend_op_array *res;
    3259       33339 :         char *name = NULL;
    3260             :         int failed;
    3261             :         phar_archive_data *phar;
    3262             : 
    3263       33339 :         if (!file_handle || !file_handle->filename) {
    3264           0 :                 return phar_orig_compile_file(file_handle, type);
    3265             :         }
    3266       33339 :         if (strstr(file_handle->filename, ".phar") && !strstr(file_handle->filename, "://")) {
    3267         179 :                 if (SUCCESS == phar_open_from_filename((char*)file_handle->filename, strlen(file_handle->filename), NULL, 0, 0, &phar, NULL)) {
    3268         158 :                         if (phar->is_zip || phar->is_tar) {
    3269          53 :                                 zend_file_handle f = *file_handle;
    3270             : 
    3271             :                                 /* zip or tar-based phar */
    3272          53 :                                 spprintf(&name, 4096, "phar://%s/%s", file_handle->filename, ".phar/stub.php");
    3273          53 :                                 if (SUCCESS == phar_orig_zend_open((const char *)name, file_handle)) {
    3274          52 :                                         efree(name);
    3275          52 :                                         name = NULL;
    3276          52 :                                         file_handle->filename = f.filename;
    3277          52 :                                         if (file_handle->opened_path) {
    3278          52 :                                                 efree(file_handle->opened_path);
    3279             :                                         }
    3280          52 :                                         file_handle->opened_path = f.opened_path;
    3281          52 :                                         file_handle->free_filename = f.free_filename;
    3282             :                                 } else {
    3283           1 :                                         *file_handle = f;
    3284             :                                 }
    3285          52 :                         } else if (phar->flags & PHAR_FILE_COMPRESSION_MASK) {
    3286             :                                 /* compressed phar */
    3287           2 :                                 file_handle->type = ZEND_HANDLE_STREAM;
    3288             :                                 /* we do our own reading directly from the phar, don't change the next line */
    3289           2 :                                 file_handle->handle.stream.handle  = phar;
    3290           2 :                                 file_handle->handle.stream.reader  = phar_zend_stream_reader;
    3291           2 :                                 file_handle->handle.stream.closer  = NULL;
    3292           2 :                                 file_handle->handle.stream.fsizer  = phar_zend_stream_fsizer;
    3293           2 :                                 file_handle->handle.stream.isatty  = 0;
    3294           4 :                                 phar->is_persistent ?
    3295           0 :                                         php_stream_rewind(PHAR_G(cached_fp)[phar->phar_pos].fp) :
    3296           2 :                                         php_stream_rewind(phar->fp);
    3297           2 :                                 memset(&file_handle->handle.stream.mmap, 0, sizeof(file_handle->handle.stream.mmap));
    3298             :                         }
    3299             :                 }
    3300             :         }
    3301             : 
    3302       33339 :         zend_try {
    3303       33339 :                 failed = 0;
    3304       33339 :                 CG(zend_lineno) = 0;
    3305       33339 :                 res = phar_orig_compile_file(file_handle, type);
    3306         293 :         } zend_catch {
    3307         293 :                 failed = 1;
    3308         293 :                 res = NULL;
    3309       33339 :         } zend_end_try();
    3310             : 
    3311       33339 :         if (name) {
    3312           1 :                 efree(name);
    3313             :         }
    3314             : 
    3315       33339 :         if (failed) {
    3316         293 :                 zend_bailout();
    3317             :         }
    3318             : 
    3319       33046 :         return res;
    3320             : }
    3321             : /* }}} */
    3322             : 
    3323             : typedef zend_op_array* (zend_compile_t)(zend_file_handle*, int);
    3324             : typedef zend_compile_t* (compile_hook)(zend_compile_t *ptr);
    3325             : 
    3326      941760 : static void mime_type_dtor(zval *zv)
    3327             : {
    3328      941760 :         free(Z_PTR_P(zv));
    3329      941760 : }
    3330             : 
    3331       23504 : PHP_GINIT_FUNCTION(phar) /* {{{ */
    3332             : {
    3333             : #if defined(COMPILE_DL_PHAR) && defined(ZTS)
    3334             :         ZEND_TSRMLS_CACHE_UPDATE();
    3335             : #endif
    3336             :         phar_mime_type mime;
    3337             : 
    3338       23504 :         memset(phar_globals, 0, sizeof(zend_phar_globals));
    3339       23504 :         phar_globals->readonly = 1;
    3340             : 
    3341       23504 :         zend_hash_init(&phar_globals->mime_types, 0, NULL, mime_type_dtor, 1);
    3342             : 
    3343             : #define PHAR_SET_MIME(mimetype, ret, fileext) \
    3344             :                 mime.mime = mimetype; \
    3345             :                 mime.len = sizeof((mimetype))+1; \
    3346             :                 mime.type = ret; \
    3347             :                 zend_hash_str_add_mem(&phar_globals->mime_types, fileext, sizeof(fileext)-1, (void *)&mime, sizeof(phar_mime_type)); \
    3348             : 
    3349       23504 :         PHAR_SET_MIME("text/html", PHAR_MIME_PHPS, "phps")
    3350       23504 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "c")
    3351       23504 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "cc")
    3352       23504 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "cpp")
    3353       23504 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "c++")
    3354       23504 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "dtd")
    3355       23504 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "h")
    3356       23504 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "log")
    3357       23504 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "rng")
    3358       23504 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "txt")
    3359       23504 :         PHAR_SET_MIME("text/plain", PHAR_MIME_OTHER, "xsd")
    3360       23504 :         PHAR_SET_MIME("", PHAR_MIME_PHP, "php")
    3361       23504 :         PHAR_SET_MIME("", PHAR_MIME_PHP, "inc")
    3362       23504 :         PHAR_SET_MIME("video/avi", PHAR_MIME_OTHER, "avi")
    3363       23504 :         PHAR_SET_MIME("image/bmp", PHAR_MIME_OTHER, "bmp")
    3364       23504 :         PHAR_SET_MIME("text/css", PHAR_MIME_OTHER, "css")
    3365       23504 :         PHAR_SET_MIME("image/gif", PHAR_MIME_OTHER, "gif")
    3366       23504 :         PHAR_SET_MIME("text/html", PHAR_MIME_OTHER, "htm")
    3367       23504 :         PHAR_SET_MIME("text/html", PHAR_MIME_OTHER, "html")
    3368       23504 :         PHAR_SET_MIME("text/html", PHAR_MIME_OTHER, "htmls")
    3369       23504 :         PHAR_SET_MIME("image/x-ico", PHAR_MIME_OTHER, "ico")
    3370       23504 :         PHAR_SET_MIME("image/jpeg", PHAR_MIME_OTHER, "jpe")
    3371       23504 :         PHAR_SET_MIME("image/jpeg", PHAR_MIME_OTHER, "jpg")
    3372       23504 :         PHAR_SET_MIME("image/jpeg", PHAR_MIME_OTHER, "jpeg")
    3373       23504 :         PHAR_SET_MIME("application/x-javascript", PHAR_MIME_OTHER, "js")
    3374       23504 :         PHAR_SET_MIME("audio/midi", PHAR_MIME_OTHER, "midi")
    3375       23504 :         PHAR_SET_MIME("audio/midi", PHAR_MIME_OTHER, "mid")
    3376       23504 :         PHAR_SET_MIME("audio/mod", PHAR_MIME_OTHER, "mod")
    3377       23504 :         PHAR_SET_MIME("movie/quicktime", PHAR_MIME_OTHER, "mov")
    3378       23504 :         PHAR_SET_MIME("audio/mp3", PHAR_MIME_OTHER, "mp3")
    3379       23504 :         PHAR_SET_MIME("video/mpeg", PHAR_MIME_OTHER, "mpg")
    3380       23504 :         PHAR_SET_MIME("video/mpeg", PHAR_MIME_OTHER, "mpeg")
    3381       23504 :         PHAR_SET_MIME("application/pdf", PHAR_MIME_OTHER, "pdf")
    3382       23504 :         PHAR_SET_MIME("image/png", PHAR_MIME_OTHER, "png")
    3383       23504 :         PHAR_SET_MIME("application/shockwave-flash", PHAR_MIME_OTHER, "swf")
    3384       23504 :         PHAR_SET_MIME("image/tiff", PHAR_MIME_OTHER, "tif")
    3385       23504 :         PHAR_SET_MIME("image/tiff", PHAR_MIME_OTHER, "tiff")
    3386       23504 :         PHAR_SET_MIME("audio/wav", PHAR_MIME_OTHER, "wav")
    3387       23504 :         PHAR_SET_MIME("image/xbm", PHAR_MIME_OTHER, "xbm")
    3388       23504 :         PHAR_SET_MIME("text/xml", PHAR_MIME_OTHER, "xml")
    3389             : 
    3390       23504 :         phar_restore_orig_functions();
    3391       23504 : }
    3392             : /* }}} */
    3393             : 
    3394       23544 : PHP_GSHUTDOWN_FUNCTION(phar) /* {{{ */
    3395             : {
    3396       23544 :         zend_hash_destroy(&phar_globals->mime_types);
    3397       23544 : }
    3398             : /* }}} */
    3399             : 
    3400       23504 : PHP_MINIT_FUNCTION(phar) /* {{{ */
    3401             : {
    3402       23504 :         REGISTER_INI_ENTRIES();
    3403             : 
    3404       23504 :         phar_orig_compile_file = zend_compile_file;
    3405       23504 :         zend_compile_file = phar_compile_file;
    3406             : 
    3407       23504 :         phar_save_resolve_path = zend_resolve_path;
    3408       23504 :         zend_resolve_path = phar_resolve_path;
    3409             : 
    3410       23504 :         phar_object_init();
    3411             : 
    3412       23504 :         phar_intercept_functions_init();
    3413       23504 :         phar_save_orig_functions();
    3414             : 
    3415       23504 :         return php_register_url_stream_wrapper("phar", &php_stream_phar_wrapper);
    3416             : }
    3417             : /* }}} */
    3418             : 
    3419       23544 : PHP_MSHUTDOWN_FUNCTION(phar) /* {{{ */
    3420             : {
    3421       23544 :         php_unregister_url_stream_wrapper("phar");
    3422             : 
    3423       23544 :         phar_intercept_functions_shutdown();
    3424             : 
    3425       23544 :         if (zend_compile_file == phar_compile_file) {
    3426       23481 :                 zend_compile_file = phar_orig_compile_file;
    3427             :         }
    3428             : 
    3429       23544 :         if (PHAR_G(manifest_cached)) {
    3430           3 :                 zend_hash_destroy(&(cached_phars));
    3431           3 :                 zend_hash_destroy(&(cached_alias));
    3432             :         }
    3433             : 
    3434       23544 :         return SUCCESS;
    3435             : }
    3436             : /* }}} */
    3437             : 
    3438       25747 : void phar_request_initialize(void) /* {{{ */
    3439             : {
    3440       25747 :         if (!PHAR_G(request_init))
    3441             :         {
    3442         579 :                 PHAR_G(last_phar) = NULL;
    3443         579 :                 PHAR_G(last_phar_name) = PHAR_G(last_alias) = NULL;
    3444         579 :                 PHAR_G(has_bz2) = zend_hash_str_exists(&module_registry, "bz2", sizeof("bz2")-1);
    3445         579 :                 PHAR_G(has_zlib) = zend_hash_str_exists(&module_registry, "zlib", sizeof("zlib")-1);
    3446         579 :                 PHAR_G(request_init) = 1;
    3447         579 :                 PHAR_G(request_ends) = 0;
    3448         579 :                 PHAR_G(request_done) = 0;
    3449         579 :                 zend_hash_init(&(PHAR_G(phar_fname_map)), 5, zend_get_hash_value, destroy_phar_data,  0);
    3450         579 :                 zend_hash_init(&(PHAR_G(phar_persist_map)), 5, zend_get_hash_value, NULL,  0);
    3451         579 :                 zend_hash_init(&(PHAR_G(phar_alias_map)), 5, zend_get_hash_value, NULL, 0);
    3452             : 
    3453         579 :                 if (PHAR_G(manifest_cached)) {
    3454             :                         phar_archive_data *pphar;
    3455           3 :                         phar_entry_fp *stuff = (phar_entry_fp *) ecalloc(zend_hash_num_elements(&cached_phars), sizeof(phar_entry_fp));
    3456             : 
    3457           9 :                         for (zend_hash_internal_pointer_reset(&cached_phars);
    3458             :                         (pphar = zend_hash_get_current_data_ptr(&cached_phars)) != NULL;
    3459           3 :                         zend_hash_move_forward(&cached_phars)) {
    3460           3 :                                 stuff[pphar->phar_pos].manifest = (phar_entry_fp_info *) ecalloc( zend_hash_num_elements(&(pphar->manifest)), sizeof(phar_entry_fp_info));
    3461             :                         }
    3462             : 
    3463           3 :                         PHAR_G(cached_fp) = stuff;
    3464             :                 }
    3465             : 
    3466         579 :                 PHAR_G(phar_SERVER_mung_list) = 0;
    3467         579 :                 PHAR_G(cwd) = NULL;
    3468         579 :                 PHAR_G(cwd_len) = 0;
    3469         579 :                 PHAR_G(cwd_init) = 0;
    3470             :         }
    3471       25747 : }
    3472             : /* }}} */
    3473             : 
    3474       23500 : PHP_RSHUTDOWN_FUNCTION(phar) /* {{{ */
    3475             : {
    3476             :         uint32_t i;
    3477             : 
    3478       23500 :         PHAR_G(request_ends) = 1;
    3479             : 
    3480       23500 :         if (PHAR_G(request_init))
    3481             :         {
    3482         579 :                 phar_release_functions();
    3483         579 :                 zend_hash_destroy(&(PHAR_G(phar_alias_map)));
    3484         579 :                 PHAR_G(phar_alias_map.u.flags) = 0;
    3485         579 :                 zend_hash_destroy(&(PHAR_G(phar_fname_map)));
    3486         579 :                 PHAR_G(phar_fname_map.u.flags) = 0;
    3487         579 :                 zend_hash_destroy(&(PHAR_G(phar_persist_map)));
    3488         579 :                 PHAR_G(phar_persist_map.u.flags) = 0;
    3489         579 :                 PHAR_G(phar_SERVER_mung_list) = 0;
    3490             : 
    3491         579 :                 if (PHAR_G(cached_fp)) {
    3492           6 :                         for (i = 0; i < zend_hash_num_elements(&cached_phars); ++i) {
    3493           3 :                                 if (PHAR_G(cached_fp)[i].fp) {
    3494           1 :                                         php_stream_close(PHAR_G(cached_fp)[i].fp);
    3495             :                                 }
    3496           3 :                                 if (PHAR_G(cached_fp)[i].ufp) {
    3497           0 :                                         php_stream_close(PHAR_G(cached_fp)[i].ufp);
    3498             :                                 }
    3499           3 :                                 efree(PHAR_G(cached_fp)[i].manifest);
    3500             :                         }
    3501           3 :                         efree(PHAR_G(cached_fp));
    3502           3 :                         PHAR_G(cached_fp) = 0;
    3503             :                 }
    3504             : 
    3505         579 :                 PHAR_G(request_init) = 0;
    3506             : 
    3507         579 :                 if (PHAR_G(cwd)) {
    3508          26 :                         efree(PHAR_G(cwd));
    3509             :                 }
    3510             : 
    3511         579 :                 PHAR_G(cwd) = NULL;
    3512         579 :                 PHAR_G(cwd_len) = 0;
    3513         579 :                 PHAR_G(cwd_init) = 0;
    3514             :         }
    3515             : 
    3516       23500 :         PHAR_G(request_done) = 1;
    3517       23500 :         return SUCCESS;
    3518             : }
    3519             : /* }}} */
    3520             : 
    3521         150 : PHP_MINFO_FUNCTION(phar) /* {{{ */
    3522             : {
    3523         150 :         phar_request_initialize();
    3524         150 :         php_info_print_table_start();
    3525         150 :         php_info_print_table_header(2, "Phar: PHP Archive support", "enabled");
    3526         150 :         php_info_print_table_row(2, "Phar EXT version", PHP_PHAR_VERSION);
    3527         150 :         php_info_print_table_row(2, "Phar API version", PHP_PHAR_API_VERSION);
    3528         150 :         php_info_print_table_row(2, "SVN revision", "$Id: 81596423ffe39dd3d0a0432acb536431a8ece4a7 $");
    3529         150 :         php_info_print_table_row(2, "Phar-based phar archives", "enabled");
    3530         150 :         php_info_print_table_row(2, "Tar-based phar archives", "enabled");
    3531         150 :         php_info_print_table_row(2, "ZIP-based phar archives", "enabled");
    3532             : 
    3533         150 :         if (PHAR_G(has_zlib)) {
    3534         150 :                 php_info_print_table_row(2, "gzip compression", "enabled");
    3535             :         } else {
    3536           0 :                 php_info_print_table_row(2, "gzip compression", "disabled (install ext/zlib)");
    3537             :         }
    3538             : 
    3539         150 :         if (PHAR_G(has_bz2)) {
    3540         150 :                 php_info_print_table_row(2, "bzip2 compression", "enabled");
    3541             :         } else {
    3542           0 :                 php_info_print_table_row(2, "bzip2 compression", "disabled (install pecl/bz2)");
    3543             :         }
    3544             : #ifdef PHAR_HAVE_OPENSSL
    3545         150 :         php_info_print_table_row(2, "Native OpenSSL support", "enabled");
    3546             : #else
    3547             :         if (zend_hash_str_exists(&module_registry, "openssl", sizeof("openssl")-1)) {
    3548             :                 php_info_print_table_row(2, "OpenSSL support", "enabled");
    3549             :         } else {
    3550             :                 php_info_print_table_row(2, "OpenSSL support", "disabled (install ext/openssl)");
    3551             :         }
    3552             : #endif
    3553         150 :         php_info_print_table_end();
    3554             : 
    3555         150 :         php_info_print_box_start(0);
    3556         150 :         PUTS("Phar based on pear/PHP_Archive, original concept by Davey Shafik.");
    3557         150 :         PUTS(!sapi_module.phpinfo_as_text?"<br />":"\n");
    3558         150 :         PUTS("Phar fully realized by Gregory Beaver and Marcus Boerger.");
    3559         150 :         PUTS(!sapi_module.phpinfo_as_text?"<br />":"\n");
    3560         150 :         PUTS("Portions of tar implementation Copyright (c) 2003-2009 Tim Kientzle.");
    3561         150 :         php_info_print_box_end();
    3562             : 
    3563         150 :         DISPLAY_INI_ENTRIES();
    3564         150 : }
    3565             : /* }}} */
    3566             : 
    3567             : /* {{{ phar_module_entry
    3568             :  */
    3569             : static const zend_module_dep phar_deps[] = {
    3570             :         ZEND_MOD_OPTIONAL("apc")
    3571             :         ZEND_MOD_OPTIONAL("bz2")
    3572             :         ZEND_MOD_OPTIONAL("openssl")
    3573             :         ZEND_MOD_OPTIONAL("zlib")
    3574             :         ZEND_MOD_OPTIONAL("standard")
    3575             : #if defined(HAVE_HASH) && !defined(COMPILE_DL_HASH)
    3576             :         ZEND_MOD_REQUIRED("hash")
    3577             : #endif
    3578             :         ZEND_MOD_REQUIRED("spl")
    3579             :         ZEND_MOD_END
    3580             : };
    3581             : 
    3582             : zend_module_entry phar_module_entry = {
    3583             :         STANDARD_MODULE_HEADER_EX, NULL,
    3584             :         phar_deps,
    3585             :         "Phar",
    3586             :         phar_functions,
    3587             :         PHP_MINIT(phar),
    3588             :         PHP_MSHUTDOWN(phar),
    3589             :         NULL,
    3590             :         PHP_RSHUTDOWN(phar),
    3591             :         PHP_MINFO(phar),
    3592             :         PHP_PHAR_VERSION,
    3593             :         PHP_MODULE_GLOBALS(phar),   /* globals descriptor */
    3594             :         PHP_GINIT(phar),            /* globals ctor */
    3595             :         PHP_GSHUTDOWN(phar),        /* globals dtor */
    3596             :         NULL,                       /* post deactivate */
    3597             :         STANDARD_MODULE_PROPERTIES_EX
    3598             : };
    3599             : /* }}} */
    3600             : 
    3601             : /*
    3602             :  * Local variables:
    3603             :  * tab-width: 4
    3604             :  * c-basic-offset: 4
    3605             :  * End:
    3606             :  * vim600: noet sw=4 ts=4 fdm=marker
    3607             :  * vim<600: noet sw=4 ts=4
    3608             :  */

Generated by: LCOV version 1.10

Generated at Wed, 24 Aug 2016 12:20:28 +0000 (28 hours ago)

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