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

Generated by: LCOV version 1.10

Generated at Tue, 14 Apr 2015 11:48:48 +0000 (7 days ago)

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