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: 1402 1791 78.3 %
Date: 2015-07-05 Functions: 46 46 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Sun, 05 Jul 2015 15:44:53 +0000 (15 hours ago)

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