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 - tar.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 540 751 71.9 %
Date: 2016-09-27 Functions: 12 12 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | TAR archive support for Phar                                         |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 2005-2016 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.01 of the PHP license,      |
       8             :   | that is bundled with this package in the file LICENSE, and is        |
       9             :   | available through the world-wide-web at the following url:           |
      10             :   | http://www.php.net/license/3_01.txt.                                 |
      11             :   | If you did not receive a copy of the PHP license and are unable to   |
      12             :   | obtain it through the world-wide-web, please send a note to          |
      13             :   | license@php.net so we can mail you a copy immediately.               |
      14             :   +----------------------------------------------------------------------+
      15             :   | Authors: Dmitry Stogov <dmitry@zend.com>                             |
      16             :   |          Gregory Beaver <cellog@php.net>                             |
      17             :   +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : #include "phar_internal.h"
      21             : 
      22        1688 : static uint32_t phar_tar_number(char *buf, int len) /* {{{ */
      23             : {
      24        1688 :         uint32_t num = 0;
      25        1688 :         int i = 0;
      26             : 
      27        4786 :         while (i < len && buf[i] == ' ') {
      28        1410 :                 ++i;
      29             :         }
      30             : 
      31       15251 :         while (i < len && buf[i] >= '0' && buf[i] <= '7') {
      32       11875 :                 num = num * 8 + (buf[i] - '0');
      33       11875 :                 ++i;
      34             :         }
      35             : 
      36        1688 :         return num;
      37             : }
      38             : /* }}} */
      39             : 
      40             : /* adapted from format_octal() in libarchive
      41             :  *
      42             :  * Copyright (c) 2003-2009 Tim Kientzle
      43             :  * All rights reserved.
      44             :  *
      45             :  * Redistribution and use in source and binary forms, with or without
      46             :  * modification, are permitted provided that the following conditions
      47             :  * are met:
      48             :  * 1. Redistributions of source code must retain the above copyright
      49             :  *    notice, this list of conditions and the following disclaimer.
      50             :  * 2. Redistributions in binary form must reproduce the above copyright
      51             :  *    notice, this list of conditions and the following disclaimer in the
      52             :  *    documentation and/or other materials provided with the distribution.
      53             :  *
      54             :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
      55             :  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
      56             :  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
      57             :  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
      58             :  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      59             :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      60             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      61             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      62             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
      63             :  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      64             :  */
      65        3520 : static int phar_tar_octal(char *buf, uint32_t val, int len) /* {{{ */
      66             : {
      67        3520 :         char *p = buf;
      68        3520 :         int s = len;
      69             : 
      70        3520 :         p += len;               /* Start at the end and work backwards. */
      71       38720 :         while (s-- > 0) {
      72       31680 :                 *--p = (char)('0' + (val & 7));
      73       31680 :                 val >>= 3;
      74             :         }
      75             : 
      76        3520 :         if (val == 0)
      77        3520 :                 return SUCCESS;
      78             : 
      79             :         /* If it overflowed, fill field with max value. */
      80           0 :         while (len-- > 0)
      81           0 :                 *p++ = '7';
      82             : 
      83           0 :         return FAILURE;
      84             : }
      85             : /* }}} */
      86             : 
      87        1420 : static uint32_t phar_tar_checksum(char *buf, int len) /* {{{ */
      88             : {
      89        1420 :         uint32_t sum = 0;
      90        1420 :         char *end = buf + len;
      91             : 
      92      713560 :         while (buf != end) {
      93      710720 :                 sum += (unsigned char)*buf;
      94      710720 :                 ++buf;
      95             :         }
      96        1420 :         return sum;
      97             : }
      98             : /* }}} */
      99             : 
     100         168 : int phar_is_tar(char *buf, char *fname) /* {{{ */
     101             : {
     102         168 :         tar_header *header = (tar_header *) buf;
     103         168 :         uint32_t checksum = phar_tar_number(header->checksum, sizeof(header->checksum));
     104             :         uint32_t ret;
     105             :         char save[sizeof(header->checksum)], *bname;
     106             : 
     107             :         /* assume that the first filename in a tar won't begin with <?php */
     108         168 :         if (!strncmp(buf, "<?php", sizeof("<?php")-1)) {
     109          82 :                 return 0;
     110             :         }
     111             : 
     112          86 :         memcpy(save, header->checksum, sizeof(header->checksum));
     113          86 :         memset(header->checksum, ' ', sizeof(header->checksum));
     114          86 :         ret = (checksum == phar_tar_checksum(buf, 512));
     115          86 :         memcpy(header->checksum, save, sizeof(header->checksum));
     116          86 :         if ((bname = strrchr(fname, PHP_DIR_SEPARATOR))) {
     117          86 :                 fname = bname;
     118             :         }
     119          86 :         if (!ret && (bname = strstr(fname, ".tar")) && (bname[4] == '\0' || bname[4] == '.')) {
     120             :                 /* probably a corrupted tar - so we will pretend it is one */
     121           4 :                 return 1;
     122             :         }
     123          82 :         return ret;
     124             : }
     125             : /* }}} */
     126             : 
     127          84 : int phar_open_or_create_tar(char *fname, int fname_len, char *alias, int alias_len, int is_data, int options, phar_archive_data** pphar, char **error) /* {{{ */
     128             : {
     129             :         phar_archive_data *phar;
     130          84 :         int ret = phar_create_or_parse_filename(fname, fname_len, alias, alias_len, is_data, options, &phar, error);
     131             : 
     132          84 :         if (FAILURE == ret) {
     133          15 :                 return FAILURE;
     134             :         }
     135             : 
     136          69 :         if (pphar) {
     137          69 :                 *pphar = phar;
     138             :         }
     139             : 
     140          69 :         phar->is_data = is_data;
     141             : 
     142          69 :         if (phar->is_tar) {
     143          34 :                 return ret;
     144             :         }
     145             : 
     146          35 :         if (phar->is_brandnew) {
     147          34 :                 phar->is_tar = 1;
     148          34 :                 phar->is_zip = 0;
     149          34 :                 phar->internal_file_start = 0;
     150          34 :                 return SUCCESS;
     151             :         }
     152             : 
     153             :         /* we've reached here - the phar exists and is a regular phar */
     154           1 :         if (error) {
     155           1 :                 spprintf(error, 4096, "phar tar error: \"%s\" already exists as a regular phar and must be deleted from disk prior to creating as a tar-based phar", fname);
     156             :         }
     157           1 :         return FAILURE;
     158             : }
     159             : /* }}} */
     160             : 
     161           7 : static int phar_tar_process_metadata(phar_entry_info *entry, php_stream *fp) /* {{{ */
     162             : {
     163             :         char *metadata;
     164           7 :         size_t save = php_stream_tell(fp), read;
     165             :         phar_entry_info *mentry;
     166             : 
     167           7 :         metadata = (char *) safe_emalloc(1, entry->uncompressed_filesize, 1);
     168             : 
     169           7 :         read = php_stream_read(fp, metadata, entry->uncompressed_filesize);
     170           7 :         if (read != entry->uncompressed_filesize) {
     171           0 :                 efree(metadata);
     172           0 :                 php_stream_seek(fp, save, SEEK_SET);
     173           0 :                 return FAILURE;
     174             :         }
     175             : 
     176           7 :         if (phar_parse_metadata(&metadata, &entry->metadata, entry->uncompressed_filesize) == FAILURE) {
     177             :                 /* if not valid serialized data, it is a regular string */
     178           0 :                 efree(metadata);
     179           0 :                 php_stream_seek(fp, save, SEEK_SET);
     180           0 :                 return FAILURE;
     181             :         }
     182             : 
     183          11 :         if (entry->filename_len == sizeof(".phar/.metadata.bin")-1 && !memcmp(entry->filename, ".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1)) {
     184           4 :                 entry->phar->metadata = entry->metadata;
     185           4 :                 ZVAL_UNDEF(&entry->metadata);
     186           6 :         } else if (entry->filename_len >= sizeof(".phar/.metadata/") + sizeof("/.metadata.bin") - 1 && NULL != (mentry = zend_hash_str_find_ptr(&(entry->phar->manifest), entry->filename + sizeof(".phar/.metadata/") - 1, entry->filename_len - (sizeof("/.metadata.bin") - 1 + sizeof(".phar/.metadata/") - 1)))) {
     187             :                 /* transfer this metadata to the entry it refers */
     188           3 :                 mentry->metadata = entry->metadata;
     189           3 :                 ZVAL_UNDEF(&entry->metadata);
     190             :         }
     191             : 
     192           7 :         efree(metadata);
     193           7 :         php_stream_seek(fp, save, SEEK_SET);
     194           7 :         return SUCCESS;
     195             : }
     196             : /* }}} */
     197             : 
     198             : #if !HAVE_STRNLEN
     199             : static size_t strnlen(const char *s, size_t maxlen) {
     200             :         char *r = (char *)memchr(s, '\0', maxlen);
     201             :         return r ? r-s : maxlen;
     202             : }
     203             : #endif
     204             : 
     205          83 : int phar_parse_tarfile(php_stream* fp, char *fname, int fname_len, char *alias, int alias_len, phar_archive_data** pphar, int is_data, uint32_t compression, char **error) /* {{{ */
     206             : {
     207          83 :         char buf[512], *actual_alias = NULL, *p;
     208          83 :         phar_entry_info entry = {0};
     209          83 :         size_t pos = 0, read, totalsize;
     210             :         tar_header *hdr;
     211             :         uint32_t sum1, sum2, size, old;
     212             :         phar_archive_data *myphar, *actual;
     213          83 :         int last_was_longlink = 0;
     214             :         int linkname_len;
     215             : 
     216          83 :         if (error) {
     217          58 :                 *error = NULL;
     218             :         }
     219             : 
     220          83 :         php_stream_seek(fp, 0, SEEK_END);
     221          83 :         totalsize = php_stream_tell(fp);
     222          83 :         php_stream_seek(fp, 0, SEEK_SET);
     223          83 :         read = php_stream_read(fp, buf, sizeof(buf));
     224             : 
     225          83 :         if (read != sizeof(buf)) {
     226           0 :                 if (error) {
     227           0 :                         spprintf(error, 4096, "phar error: \"%s\" is not a tar file or is truncated", fname);
     228             :                 }
     229           0 :                 php_stream_close(fp);
     230           0 :                 return FAILURE;
     231             :         }
     232             : 
     233          83 :         hdr = (tar_header*)buf;
     234          83 :         old = (memcmp(hdr->magic, "ustar", sizeof("ustar")-1) != 0);
     235             : 
     236          83 :         myphar = (phar_archive_data *) pecalloc(1, sizeof(phar_archive_data), PHAR_G(persist));
     237          83 :         myphar->is_persistent = PHAR_G(persist);
     238             :         /* estimate number of entries, can't be certain with tar files */
     239          83 :         zend_hash_init(&myphar->manifest, 2 + (totalsize >> 12),
     240             :                 zend_get_hash_value, destroy_phar_manifest_entry, (zend_bool)myphar->is_persistent);
     241          83 :         zend_hash_init(&myphar->mounted_dirs, 5,
     242             :                 zend_get_hash_value, NULL, (zend_bool)myphar->is_persistent);
     243          83 :         zend_hash_init(&myphar->virtual_dirs, 4 + (totalsize >> 11),
     244             :                 zend_get_hash_value, NULL, (zend_bool)myphar->is_persistent);
     245          83 :         myphar->is_tar = 1;
     246             :         /* remember whether this entire phar was compressed with gz/bzip2 */
     247          83 :         myphar->flags = compression;
     248             : 
     249          83 :         entry.is_tar = 1;
     250          83 :         entry.is_crc_checked = 1;
     251          83 :         entry.phar = myphar;
     252          83 :         pos += sizeof(buf);
     253             : 
     254             :         do {
     255             :                 phar_entry_info *newentry;
     256             : 
     257         417 :                 pos = php_stream_tell(fp);
     258         417 :                 hdr = (tar_header*) buf;
     259         417 :                 sum1 = phar_tar_number(hdr->checksum, sizeof(hdr->checksum));
     260         417 :                 if (sum1 == 0 && phar_tar_checksum(buf, sizeof(buf)) == 0) {
     261          31 :                         break;
     262             :                 }
     263         386 :                 memset(hdr->checksum, ' ', sizeof(hdr->checksum));
     264         386 :                 sum2 = phar_tar_checksum(buf, old?sizeof(old_tar_header):sizeof(tar_header));
     265             : 
     266         386 :                 size = entry.uncompressed_filesize = entry.compressed_filesize =
     267         386 :                         phar_tar_number(hdr->size, sizeof(hdr->size));
     268             : 
     269             :                 /* skip global/file headers (pax) */
     270         386 :                 if (!old && (hdr->typeflag == TAR_GLOBAL_HDR || hdr->typeflag == TAR_FILE_HDR)) {
     271           2 :                         size = (size+511)&~511;
     272           2 :                         goto next;
     273             :                 }
     274             : 
     275         384 :                 if (((!old && hdr->prefix[0] == 0) || old) && strnlen(hdr->name, 100) == sizeof(".phar/signature.bin")-1 && !strncmp(hdr->name, ".phar/signature.bin", sizeof(".phar/signature.bin")-1)) {
     276             :                         zend_off_t curloc;
     277             : 
     278          38 :                         if (size > 511) {
     279           0 :                                 if (error) {
     280           0 :                                         spprintf(error, 4096, "phar error: tar-based phar \"%s\" has signature that is larger than 511 bytes, cannot process", fname);
     281             :                                 }
     282             : bail:
     283           1 :                                 php_stream_close(fp);
     284           1 :                                 phar_destroy_phar_data(myphar);
     285           1 :                                 return FAILURE;
     286             :                         }
     287          38 :                         curloc = php_stream_tell(fp);
     288          38 :                         read = php_stream_read(fp, buf, size);
     289          38 :                         if (read != size || read <= 8) {
     290           1 :                                 if (error) {
     291           1 :                                         spprintf(error, 4096, "phar error: tar-based phar \"%s\" signature cannot be read", fname);
     292             :                                 }
     293           1 :                                 goto bail;
     294             :                         }
     295             : #ifdef WORDS_BIGENDIAN
     296             : # define PHAR_GET_32(buffer) \
     297             :         (((((unsigned char*)(buffer))[3]) << 24) \
     298             :                 | ((((unsigned char*)(buffer))[2]) << 16) \
     299             :                 | ((((unsigned char*)(buffer))[1]) <<  8) \
     300             :                 | (((unsigned char*)(buffer))[0]))
     301             : #else
     302             : # define PHAR_GET_32(buffer) (uint32_t) *(buffer)
     303             : #endif
     304          37 :                         myphar->sig_flags = PHAR_GET_32(buf);
     305          37 :                         if (FAILURE == phar_verify_signature(fp, php_stream_tell(fp) - size - 512, myphar->sig_flags, buf + 8, size - 8, fname, &myphar->signature, &myphar->sig_len, error)) {
     306           0 :                                 if (error) {
     307           0 :                                         char *save = *error;
     308           0 :                                         spprintf(error, 4096, "phar error: tar-based phar \"%s\" signature cannot be verified: %s", fname, save);
     309           0 :                                         efree(save);
     310             :                                 }
     311           0 :                                 goto bail;
     312             :                         }
     313          37 :                         php_stream_seek(fp, curloc + 512, SEEK_SET);
     314             :                         /* signature checked out, let's ensure this is the last file in the phar */
     315          37 :                         if (((hdr->typeflag == '\0') || (hdr->typeflag == TAR_FILE)) && size > 0) {
     316             :                                 /* this is not good enough - seek succeeds even on truncated tars */
     317          36 :                                 php_stream_seek(fp, 512, SEEK_CUR);
     318          36 :                                 if ((uint)php_stream_tell(fp) > totalsize) {
     319           0 :                                         if (error) {
     320           0 :                                                 spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
     321             :                                         }
     322           0 :                                         php_stream_close(fp);
     323           0 :                                         phar_destroy_phar_data(myphar);
     324           0 :                                         return FAILURE;
     325             :                                 }
     326             :                         }
     327             : 
     328          37 :                         read = php_stream_read(fp, buf, sizeof(buf));
     329             : 
     330          37 :                         if (read != sizeof(buf)) {
     331           0 :                                 if (error) {
     332           0 :                                         spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
     333             :                                 }
     334           0 :                                 php_stream_close(fp);
     335           0 :                                 phar_destroy_phar_data(myphar);
     336           0 :                                 return FAILURE;
     337             :                         }
     338             : 
     339          37 :                         hdr = (tar_header*) buf;
     340          37 :                         sum1 = phar_tar_number(hdr->checksum, sizeof(hdr->checksum));
     341             : 
     342          37 :                         if (sum1 == 0 && phar_tar_checksum(buf, sizeof(buf)) == 0) {
     343          37 :                                 break;
     344             :                         }
     345             : 
     346           0 :                         if (error) {
     347           0 :                                 spprintf(error, 4096, "phar error: \"%s\" has entries after signature, invalid phar", fname);
     348             :                         }
     349             : 
     350           0 :                         goto bail;
     351             :                 }
     352             : 
     353         346 :                 if (!last_was_longlink && hdr->typeflag == 'L') {
     354           4 :                         last_was_longlink = 1;
     355             :                         /* support the ././@LongLink system for storing long filenames */
     356           4 :                         entry.filename_len = entry.uncompressed_filesize;
     357             : 
     358             :                         /* Check for overflow - bug 61065 */
     359           4 :                         if (entry.filename_len == UINT_MAX || entry.filename_len == 0) {
     360           1 :                                 if (error) {
     361           1 :                                         spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (invalid entry size)", fname);
     362             :                                 }
     363           1 :                                 php_stream_close(fp);
     364           1 :                                 phar_destroy_phar_data(myphar);
     365           1 :                                 return FAILURE;
     366             :                         }
     367           3 :                         entry.filename = pemalloc(entry.filename_len+1, myphar->is_persistent);
     368             : 
     369           3 :                         read = php_stream_read(fp, entry.filename, entry.filename_len);
     370           3 :                         if (read != entry.filename_len) {
     371           0 :                                 efree(entry.filename);
     372           0 :                                 if (error) {
     373           0 :                                         spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
     374             :                                 }
     375           0 :                                 php_stream_close(fp);
     376           0 :                                 phar_destroy_phar_data(myphar);
     377           0 :                                 return FAILURE;
     378             :                         }
     379           3 :                         entry.filename[entry.filename_len] = '\0';
     380             : 
     381             :                         /* skip blank stuff */
     382           3 :                         size = ((size+511)&~511) - size;
     383             : 
     384             :                         /* this is not good enough - seek succeeds even on truncated tars */
     385           3 :                         php_stream_seek(fp, size, SEEK_CUR);
     386           3 :                         if ((uint)php_stream_tell(fp) > totalsize) {
     387           0 :                                 efree(entry.filename);
     388           0 :                                 if (error) {
     389           0 :                                         spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
     390             :                                 }
     391           0 :                                 php_stream_close(fp);
     392           0 :                                 phar_destroy_phar_data(myphar);
     393           0 :                                 return FAILURE;
     394             :                         }
     395             : 
     396           3 :                         read = php_stream_read(fp, buf, sizeof(buf));
     397             : 
     398           3 :                         if (read != sizeof(buf)) {
     399           0 :                                 efree(entry.filename);
     400           0 :                                 if (error) {
     401           0 :                                         spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
     402             :                                 }
     403           0 :                                 php_stream_close(fp);
     404           0 :                                 phar_destroy_phar_data(myphar);
     405           0 :                                 return FAILURE;
     406             :                         }
     407           3 :                         continue;
     408         347 :                 } else if (!last_was_longlink && !old && hdr->prefix[0] != 0) {
     409             :                         char name[256];
     410             :                         int i, j;
     411             : 
     412         571 :                         for (i = 0; i < 155; i++) {
     413         568 :                                 name[i] = hdr->prefix[i];
     414         568 :                                 if (name[i] == '\0') {
     415           2 :                                         break;
     416             :                                 }
     417             :                         }
     418           5 :                         name[i++] = '/';
     419         406 :                         for (j = 0; j < 100; j++) {
     420         402 :                                 name[i+j] = hdr->name[j];
     421         402 :                                 if (name[i+j] == '\0') {
     422           1 :                                         break;
     423             :                                 }
     424             :                         }
     425             : 
     426           5 :                         entry.filename_len = i+j;
     427             : 
     428           5 :                         if (name[entry.filename_len - 1] == '/') {
     429             :                                 /* some tar programs store directories with trailing slash */
     430           0 :                                 entry.filename_len--;
     431             :                         }
     432           5 :                         entry.filename = pestrndup(name, entry.filename_len, myphar->is_persistent);
     433         337 :                 } else if (!last_was_longlink) {
     434             :                         int i;
     435             : 
     436             :                         /* calculate strlen, which can be no longer than 100 */
     437        6059 :                         for (i = 0; i < 100; i++) {
     438        6059 :                                 if (hdr->name[i] == '\0') {
     439         334 :                                         break;
     440             :                                 }
     441             :                         }
     442         334 :                         entry.filename_len = i;
     443         334 :                         entry.filename = pestrndup(hdr->name, i, myphar->is_persistent);
     444             : 
     445         334 :                         if (i > 0 && entry.filename[entry.filename_len - 1] == '/') {
     446             :                                 /* some tar programs store directories with trailing slash */
     447          23 :                                 entry.filename[entry.filename_len - 1] = '\0';
     448          23 :                                 entry.filename_len--;
     449             :                         }
     450             :                 }
     451         342 :                 last_was_longlink = 0;
     452             : 
     453         342 :                 phar_add_virtual_dirs(myphar, entry.filename, entry.filename_len);
     454             : 
     455         342 :                 if (sum1 != sum2) {
     456           2 :                         if (error) {
     457           2 :                                 spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (checksum mismatch of file \"%s\")", fname, entry.filename);
     458             :                         }
     459           2 :                         pefree(entry.filename, myphar->is_persistent);
     460           2 :                         php_stream_close(fp);
     461           2 :                         phar_destroy_phar_data(myphar);
     462           2 :                         return FAILURE;
     463             :                 }
     464             : 
     465         340 :                 entry.tar_type = ((old & (hdr->typeflag == '\0')) ? TAR_FILE : hdr->typeflag);
     466         340 :                 entry.offset = entry.offset_abs = pos; /* header_offset unused in tar */
     467         340 :                 entry.fp_type = PHAR_FP;
     468         340 :                 entry.flags = phar_tar_number(hdr->mode, sizeof(hdr->mode)) & PHAR_ENT_PERM_MASK;
     469         340 :                 entry.timestamp = phar_tar_number(hdr->mtime, sizeof(hdr->mtime));
     470         340 :                 entry.is_persistent = myphar->is_persistent;
     471             : 
     472         340 :                 if (old && entry.tar_type == TAR_FILE && S_ISDIR(entry.flags)) {
     473           0 :                         entry.tar_type = TAR_DIR;
     474             :                 }
     475             : 
     476         340 :                 if (entry.tar_type == TAR_DIR) {
     477          40 :                         entry.is_dir = 1;
     478             :                 } else {
     479         300 :                         entry.is_dir = 0;
     480             :                 }
     481             : 
     482         340 :                 entry.link = NULL;
     483             :                 /* link field is null-terminated unless it has 100 non-null chars.
     484             :                  * Thus we can not use strlen. */
     485         340 :                 linkname_len = strnlen(hdr->linkname, 100);
     486         340 :                 if (entry.tar_type == TAR_LINK) {
     487           3 :                         if (!zend_hash_str_exists(&myphar->manifest, hdr->linkname, linkname_len)) {
     488           1 :                                 if (error) {
     489           1 :                                         spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file - hard link to non-existent file \"%.*s\"", fname, linkname_len, hdr->linkname);
     490             :                                 }
     491           1 :                                 pefree(entry.filename, entry.is_persistent);
     492           1 :                                 php_stream_close(fp);
     493           1 :                                 phar_destroy_phar_data(myphar);
     494           1 :                                 return FAILURE;
     495             :                         }
     496           2 :                         entry.link = estrndup(hdr->linkname, linkname_len);
     497         337 :                 } else if (entry.tar_type == TAR_SYMLINK) {
     498           9 :                         entry.link = estrndup(hdr->linkname, linkname_len);
     499             :                 }
     500         339 :                 phar_set_inode(&entry);
     501             : 
     502         678 :                 newentry = zend_hash_str_update_mem(&myphar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info));
     503             :                 ZEND_ASSERT(newentry != NULL);
     504             : 
     505         339 :                 if (entry.is_persistent) {
     506           0 :                         ++entry.manifest_pos;
     507             :                 }
     508             : 
     509         339 :                 if (entry.filename_len >= sizeof(".phar/.metadata")-1 && !memcmp(entry.filename, ".phar/.metadata", sizeof(".phar/.metadata")-1)) {
     510           7 :                         if (FAILURE == phar_tar_process_metadata(newentry, fp)) {
     511           0 :                                 if (error) {
     512           0 :                                         spprintf(error, 4096, "phar error: tar-based phar \"%s\" has invalid metadata in magic file \"%s\"", fname, entry.filename);
     513             :                                 }
     514           0 :                                 php_stream_close(fp);
     515           0 :                                 phar_destroy_phar_data(myphar);
     516           0 :                                 return FAILURE;
     517             :                         }
     518             :                 }
     519             : 
     520         339 :                 if (!actual_alias && entry.filename_len == sizeof(".phar/alias.txt")-1 && !strncmp(entry.filename, ".phar/alias.txt", sizeof(".phar/alias.txt")-1)) {
     521             :                         /* found explicit alias */
     522          10 :                         if (size > 511) {
     523           1 :                                 if (error) {
     524           1 :                                         spprintf(error, 4096, "phar error: tar-based phar \"%s\" has alias that is larger than 511 bytes, cannot process", fname);
     525             :                                 }
     526           1 :                                 php_stream_close(fp);
     527           1 :                                 phar_destroy_phar_data(myphar);
     528           1 :                                 return FAILURE;
     529             :                         }
     530             : 
     531           9 :                         read = php_stream_read(fp, buf, size);
     532             : 
     533           9 :                         if (read == size) {
     534           9 :                                 buf[size] = '\0';
     535           9 :                                 if (!phar_validate_alias(buf, size)) {
     536           4 :                                         if (size > 50) {
     537           1 :                                                 buf[50] = '.';
     538           1 :                                                 buf[51] = '.';
     539           1 :                                                 buf[52] = '.';
     540           1 :                                                 buf[53] = '\0';
     541             :                                         }
     542             : 
     543           4 :                                         if (error) {
     544           4 :                                                 spprintf(error, 4096, "phar error: invalid alias \"%s\" in tar-based phar \"%s\"", buf, fname);
     545             :                                         }
     546             : 
     547           4 :                                         php_stream_close(fp);
     548           4 :                                         phar_destroy_phar_data(myphar);
     549           4 :                                         return FAILURE;
     550             :                                 }
     551             : 
     552           5 :                                 actual_alias = pestrndup(buf, size, myphar->is_persistent);
     553           5 :                                 myphar->alias = actual_alias;
     554           5 :                                 myphar->alias_len = size;
     555           5 :                                 php_stream_seek(fp, pos, SEEK_SET);
     556             :                         } else {
     557           0 :                                 if (error) {
     558           0 :                                         spprintf(error, 4096, "phar error: Unable to read alias from tar-based phar \"%s\"", fname);
     559             :                                 }
     560             : 
     561           0 :                                 php_stream_close(fp);
     562           0 :                                 phar_destroy_phar_data(myphar);
     563           0 :                                 return FAILURE;
     564             :                         }
     565             :                 }
     566             : 
     567         334 :                 size = (size+511)&~511;
     568             : 
     569         334 :                 if (((hdr->typeflag == '\0') || (hdr->typeflag == TAR_FILE)) && size > 0) {
     570             : next:
     571             :                         /* this is not good enough - seek succeeds even on truncated tars */
     572         279 :                         php_stream_seek(fp, size, SEEK_CUR);
     573         279 :                         if ((uint)php_stream_tell(fp) > totalsize) {
     574           1 :                                 if (error) {
     575           1 :                                         spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
     576             :                                 }
     577           1 :                                 php_stream_close(fp);
     578           1 :                                 phar_destroy_phar_data(myphar);
     579           1 :                                 return FAILURE;
     580             :                         }
     581             :                 }
     582             : 
     583         335 :                 read = php_stream_read(fp, buf, sizeof(buf));
     584             : 
     585         335 :                 if (read != sizeof(buf)) {
     586           4 :                         if (error) {
     587           4 :                                 spprintf(error, 4096, "phar error: \"%s\" is a corrupted tar file (truncated)", fname);
     588             :                         }
     589           4 :                         php_stream_close(fp);
     590           4 :                         phar_destroy_phar_data(myphar);
     591           4 :                         return FAILURE;
     592             :                 }
     593         334 :         } while (!php_stream_eof(fp));
     594             : 
     595          68 :         if (zend_hash_str_exists(&(myphar->manifest), ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
     596          46 :                 myphar->is_data = 0;
     597             :         } else {
     598          22 :                 myphar->is_data = 1;
     599             :         }
     600             : 
     601             :         /* ensure signature set */
     602          68 :         if (!myphar->is_data && PHAR_G(require_hash) && !myphar->signature) {
     603           1 :                 php_stream_close(fp);
     604           1 :                 phar_destroy_phar_data(myphar);
     605           1 :                 if (error) {
     606           1 :                         spprintf(error, 0, "tar-based phar \"%s\" does not have a signature", fname);
     607             :                 }
     608           1 :                 return FAILURE;
     609             :         }
     610             : 
     611          67 :         myphar->fname = pestrndup(fname, fname_len, myphar->is_persistent);
     612             : #ifdef PHP_WIN32
     613             :         phar_unixify_path_separators(myphar->fname, fname_len);
     614             : #endif
     615          67 :         myphar->fname_len = fname_len;
     616          67 :         myphar->fp = fp;
     617          67 :         p = strrchr(myphar->fname, '/');
     618             : 
     619          67 :         if (p) {
     620          67 :                 myphar->ext = memchr(p, '.', (myphar->fname + fname_len) - p);
     621          67 :                 if (myphar->ext == p) {
     622           0 :                         myphar->ext = memchr(p + 1, '.', (myphar->fname + fname_len) - p - 1);
     623             :                 }
     624          67 :                 if (myphar->ext) {
     625          67 :                         myphar->ext_len = (myphar->fname + fname_len) - myphar->ext;
     626             :                 }
     627             :         }
     628             : 
     629          67 :         phar_request_initialize();
     630             : 
     631         134 :         if (NULL == (actual = zend_hash_str_add_ptr(&(PHAR_G(phar_fname_map)), myphar->fname, fname_len, myphar))) {
     632           0 :                 if (error) {
     633           0 :                         spprintf(error, 4096, "phar error: Unable to add tar-based phar \"%s\" to phar registry", fname);
     634             :                 }
     635           0 :                 php_stream_close(fp);
     636           0 :                 phar_destroy_phar_data(myphar);
     637           0 :                 return FAILURE;
     638             :         }
     639             : 
     640          67 :         myphar = actual;
     641             : 
     642          67 :         if (actual_alias) {
     643             :                 phar_archive_data *fd_ptr;
     644             : 
     645           5 :                 myphar->is_temporary_alias = 0;
     646             : 
     647          10 :                 if (NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), actual_alias, myphar->alias_len))) {
     648           1 :                         if (SUCCESS != phar_free_alias(fd_ptr, actual_alias, myphar->alias_len)) {
     649           1 :                                 if (error) {
     650           1 :                                         spprintf(error, 4096, "phar error: Unable to add tar-based phar \"%s\", alias is already in use", fname);
     651             :                                 }
     652           1 :                                 zend_hash_str_del(&(PHAR_G(phar_fname_map)), myphar->fname, fname_len);
     653           1 :                                 return FAILURE;
     654             :                         }
     655             :                 }
     656             : 
     657           4 :                 zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), actual_alias, myphar->alias_len, myphar);
     658             :         } else {
     659             :                 phar_archive_data *fd_ptr;
     660             : 
     661          62 :                 if (alias_len) {
     662           0 :                         if (NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len))) {
     663           0 :                                 if (SUCCESS != phar_free_alias(fd_ptr, alias, alias_len)) {
     664           0 :                                         if (error) {
     665           0 :                                                 spprintf(error, 4096, "phar error: Unable to add tar-based phar \"%s\", alias is already in use", fname);
     666             :                                         }
     667           0 :                                         zend_hash_str_del(&(PHAR_G(phar_fname_map)), myphar->fname, fname_len);
     668           0 :                                         return FAILURE;
     669             :                                 }
     670             :                         }
     671           0 :                         zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len, myphar);
     672           0 :                         myphar->alias = pestrndup(alias, alias_len, myphar->is_persistent);
     673           0 :                         myphar->alias_len = alias_len;
     674             :                 } else {
     675          62 :                         myphar->alias = pestrndup(myphar->fname, fname_len, myphar->is_persistent);
     676          62 :                         myphar->alias_len = fname_len;
     677             :                 }
     678             : 
     679          62 :                 myphar->is_temporary_alias = 1;
     680             :         }
     681             : 
     682          66 :         if (pphar) {
     683          61 :                 *pphar = myphar;
     684             :         }
     685             : 
     686          66 :         return SUCCESS;
     687             : }
     688             : /* }}} */
     689             : 
     690             : struct _phar_pass_tar_info {
     691             :         php_stream *old;
     692             :         php_stream *new;
     693             :         int free_fp;
     694             :         int free_ufp;
     695             :         char **error;
     696             : };
     697             : 
     698         891 : static int phar_tar_writeheaders_int(phar_entry_info *entry, void *argument) /* {{{ */
     699             : {
     700             :         tar_header header;
     701             :         size_t pos;
     702         891 :         struct _phar_pass_tar_info *fp = (struct _phar_pass_tar_info *)argument;
     703             :         char padding[512];
     704             : 
     705         891 :         if (entry->is_mounted) {
     706           4 :                 return ZEND_HASH_APPLY_KEEP;
     707             :         }
     708             : 
     709         887 :         if (entry->is_deleted) {
     710           4 :                 if (entry->fp_refcount <= 0) {
     711           4 :                         return ZEND_HASH_APPLY_REMOVE;
     712             :                 } else {
     713             :                         /* we can't delete this in-memory until it is closed */
     714           0 :                         return ZEND_HASH_APPLY_KEEP;
     715             :                 }
     716             :         }
     717             : 
     718         883 :         phar_add_virtual_dirs(entry->phar, entry->filename, entry->filename_len);
     719         883 :         memset((char *) &header, 0, sizeof(header));
     720             : 
     721         883 :         if (entry->filename_len > 100) {
     722             :                 char *boundary;
     723           9 :                 if (entry->filename_len > 256) {
     724           1 :                         if (fp->error) {
     725           1 :                                 spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, filename \"%s\" is too long for tar file format", entry->phar->fname, entry->filename);
     726             :                         }
     727           1 :                         return ZEND_HASH_APPLY_STOP;
     728             :                 }
     729           8 :                 boundary = entry->filename + entry->filename_len - 101;
     730         424 :                 while (*boundary && *boundary != '/') {
     731         408 :                         ++boundary;
     732             :                 }
     733           8 :                 if (!*boundary || ((boundary - entry->filename) > 155)) {
     734           2 :                         if (fp->error) {
     735           2 :                                 spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, filename \"%s\" is too long for tar file format", entry->phar->fname, entry->filename);
     736             :                         }
     737           2 :                         return ZEND_HASH_APPLY_STOP;
     738             :                 }
     739           6 :                 memcpy(header.prefix, entry->filename, boundary - entry->filename);
     740           6 :                 memcpy(header.name, boundary + 1, entry->filename_len - (boundary + 1 - entry->filename));
     741             :         } else {
     742         874 :                 memcpy(header.name, entry->filename, entry->filename_len);
     743             :         }
     744             : 
     745         880 :         phar_tar_octal(header.mode, entry->flags & PHAR_ENT_PERM_MASK, sizeof(header.mode)-1);
     746             : 
     747         880 :         if (FAILURE == phar_tar_octal(header.size, entry->uncompressed_filesize, sizeof(header.size)-1)) {
     748           0 :                 if (fp->error) {
     749           0 :                         spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, filename \"%s\" is too large for tar file format", entry->phar->fname, entry->filename);
     750             :                 }
     751           0 :                 return ZEND_HASH_APPLY_STOP;
     752             :         }
     753             : 
     754         880 :         if (FAILURE == phar_tar_octal(header.mtime, entry->timestamp, sizeof(header.mtime)-1)) {
     755           0 :                 if (fp->error) {
     756           0 :                         spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, file modification time of file \"%s\" is too large for tar file format", entry->phar->fname, entry->filename);
     757             :                 }
     758           0 :                 return ZEND_HASH_APPLY_STOP;
     759             :         }
     760             : 
     761             :         /* calc checksum */
     762         880 :         header.typeflag = entry->tar_type;
     763             : 
     764         880 :         if (entry->link) {
     765          10 :                 strncpy(header.linkname, entry->link, strlen(entry->link));
     766             :         }
     767             : 
     768         880 :         strncpy(header.magic, "ustar", sizeof("ustar")-1);
     769         880 :         strncpy(header.version, "00", sizeof("00")-1);
     770         880 :         strncpy(header.checksum, "        ", sizeof("        ")-1);
     771         880 :         entry->crc32 = phar_tar_checksum((char *)&header, sizeof(header));
     772             : 
     773         880 :         if (FAILURE == phar_tar_octal(header.checksum, entry->crc32, sizeof(header.checksum)-1)) {
     774           0 :                 if (fp->error) {
     775           0 :                         spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, checksum of file \"%s\" is too large for tar file format", entry->phar->fname, entry->filename);
     776             :                 }
     777           0 :                 return ZEND_HASH_APPLY_STOP;
     778             :         }
     779             : 
     780             :         /* write header */
     781         880 :         entry->header_offset = php_stream_tell(fp->new);
     782             : 
     783         880 :         if (sizeof(header) != php_stream_write(fp->new, (char *) &header, sizeof(header))) {
     784           0 :                 if (fp->error) {
     785           0 :                         spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, header for  file \"%s\" could not be written", entry->phar->fname, entry->filename);
     786             :                 }
     787           0 :                 return ZEND_HASH_APPLY_STOP;
     788             :         }
     789             : 
     790         880 :         pos = php_stream_tell(fp->new); /* save start of file within tar */
     791             : 
     792             :         /* write contents */
     793         880 :         if (entry->uncompressed_filesize) {
     794         782 :                 if (FAILURE == phar_open_entry_fp(entry, fp->error, 0)) {
     795           0 :                         return ZEND_HASH_APPLY_STOP;
     796             :                 }
     797             : 
     798         782 :                 if (-1 == phar_seek_efp(entry, 0, SEEK_SET, 0, 0)) {
     799           0 :                         if (fp->error) {
     800           0 :                                 spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, contents of file \"%s\" could not be written, seek failed", entry->phar->fname, entry->filename);
     801             :                         }
     802           0 :                         return ZEND_HASH_APPLY_STOP;
     803             :                 }
     804             : 
     805         782 :                 if (SUCCESS != php_stream_copy_to_stream_ex(phar_get_efp(entry, 0), fp->new, entry->uncompressed_filesize, NULL)) {
     806           0 :                         if (fp->error) {
     807           0 :                                 spprintf(fp->error, 4096, "tar-based phar \"%s\" cannot be created, contents of file \"%s\" could not be written", entry->phar->fname, entry->filename);
     808             :                         }
     809           0 :                         return ZEND_HASH_APPLY_STOP;
     810             :                 }
     811             : 
     812         782 :                 memset(padding, 0, 512);
     813         782 :                 php_stream_write(fp->new, padding, ((entry->uncompressed_filesize +511)&~511) - entry->uncompressed_filesize);
     814             :         }
     815             : 
     816         880 :         if (!entry->is_modified && entry->fp_refcount) {
     817             :                 /* open file pointers refer to this fp, do not free the stream */
     818           0 :                 switch (entry->fp_type) {
     819             :                         case PHAR_FP:
     820           0 :                                 fp->free_fp = 0;
     821           0 :                                 break;
     822             :                         case PHAR_UFP:
     823           0 :                                 fp->free_ufp = 0;
     824             :                         default:
     825             :                                 break;
     826             :                 }
     827             :         }
     828             : 
     829         880 :         entry->is_modified = 0;
     830             : 
     831         880 :         if (entry->fp_type == PHAR_MOD && entry->fp != entry->phar->fp && entry->fp != entry->phar->ufp) {
     832         374 :                 if (!entry->fp_refcount) {
     833         368 :                         php_stream_close(entry->fp);
     834             :                 }
     835         374 :                 entry->fp = NULL;
     836             :         }
     837             : 
     838         880 :         entry->fp_type = PHAR_FP;
     839             : 
     840             :         /* note new location within tar */
     841         880 :         entry->offset = entry->offset_abs = pos;
     842         880 :         return ZEND_HASH_APPLY_KEEP;
     843             : }
     844             : /* }}} */
     845             : 
     846         716 : static int phar_tar_writeheaders(zval *zv, void *argument) /* {{{ */
     847             : {
     848         716 :         return phar_tar_writeheaders_int(Z_PTR_P(zv), argument);
     849             : }
     850             : /* }}} */
     851             : 
     852          23 : int phar_tar_setmetadata(zval *metadata, phar_entry_info *entry, char **error) /* {{{ */
     853             : {
     854             :         php_serialize_data_t metadata_hash;
     855             : 
     856          23 :         if (entry->metadata_str.s) {
     857          16 :                 smart_str_free(&entry->metadata_str);
     858             :         }
     859             : 
     860          23 :         entry->metadata_str.s = NULL;
     861          23 :         PHP_VAR_SERIALIZE_INIT(metadata_hash);
     862          23 :         php_var_serialize(&entry->metadata_str, metadata, &metadata_hash);
     863          23 :         PHP_VAR_SERIALIZE_DESTROY(metadata_hash);
     864          23 :         entry->uncompressed_filesize = entry->compressed_filesize = entry->metadata_str.s ? ZSTR_LEN(entry->metadata_str.s) : 0;
     865             : 
     866          23 :         if (entry->fp && entry->fp_type == PHAR_MOD) {
     867           9 :                 php_stream_close(entry->fp);
     868             :         }
     869             : 
     870          23 :         entry->fp_type = PHAR_MOD;
     871          23 :         entry->is_modified = 1;
     872          23 :         entry->fp = php_stream_fopen_tmpfile();
     873          23 :         entry->offset = entry->offset_abs = 0;
     874          23 :         if (entry->fp == NULL) {
     875           0 :                 spprintf(error, 0, "phar error: unable to create temporary file");
     876           0 :                 return -1;
     877             :         }
     878          23 :         if (ZSTR_LEN(entry->metadata_str.s) != php_stream_write(entry->fp, ZSTR_VAL(entry->metadata_str.s), ZSTR_LEN(entry->metadata_str.s))) {
     879           0 :                 spprintf(error, 0, "phar tar error: unable to write metadata to magic metadata file \"%s\"", entry->filename);
     880           0 :                 zend_hash_str_del(&(entry->phar->manifest), entry->filename, entry->filename_len);
     881           0 :                 return ZEND_HASH_APPLY_STOP;
     882             :         }
     883             : 
     884          23 :         return ZEND_HASH_APPLY_KEEP;
     885             : }
     886             : /* }}} */
     887             : 
     888         716 : static int phar_tar_setupmetadata(zval *zv, void *argument) /* {{{ */
     889             : {
     890             :         int lookfor_len;
     891         716 :         struct _phar_pass_tar_info *i = (struct _phar_pass_tar_info *)argument;
     892         716 :         char *lookfor, **error = i->error;
     893         716 :         phar_entry_info *entry = (phar_entry_info *)Z_PTR_P(zv), *metadata, newentry = {0};
     894             : 
     895         716 :         if (entry->filename_len >= sizeof(".phar/.metadata") && !memcmp(entry->filename, ".phar/.metadata", sizeof(".phar/.metadata")-1)) {
     896          19 :                 if (entry->filename_len == sizeof(".phar/.metadata.bin")-1 && !memcmp(entry->filename, ".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1)) {
     897          10 :                         return phar_tar_setmetadata(&entry->phar->metadata, entry, error);
     898             :                 }
     899             :                 /* search for the file this metadata entry references */
     900           9 :                 if (entry->filename_len >= sizeof(".phar/.metadata/") + sizeof("/.metadata.bin") - 1 && !zend_hash_str_exists(&(entry->phar->manifest), entry->filename + sizeof(".phar/.metadata/") - 1, entry->filename_len - (sizeof("/.metadata.bin") - 1 + sizeof(".phar/.metadata/") - 1))) {
     901             :                         /* this is orphaned metadata, erase it */
     902           0 :                         return ZEND_HASH_APPLY_REMOVE;
     903             :                 }
     904             :                 /* we can keep this entry, the file that refers to it exists */
     905           9 :                 return ZEND_HASH_APPLY_KEEP;
     906             :         }
     907             : 
     908         697 :         if (!entry->is_modified) {
     909         395 :                 return ZEND_HASH_APPLY_KEEP;
     910             :         }
     911             : 
     912             :         /* now we are dealing with regular files, so look for metadata */
     913         302 :         lookfor_len = spprintf(&lookfor, 0, ".phar/.metadata/%s/.metadata.bin", entry->filename);
     914             : 
     915         604 :         if (Z_TYPE(entry->metadata) == IS_UNDEF) {
     916         298 :                 zend_hash_str_del(&(entry->phar->manifest), lookfor, lookfor_len);
     917         298 :                 efree(lookfor);
     918         298 :                 return ZEND_HASH_APPLY_KEEP;
     919             :         }
     920             : 
     921           8 :         if (NULL != (metadata = zend_hash_str_find_ptr(&(entry->phar->manifest), lookfor, lookfor_len))) {
     922             :                 int ret;
     923           2 :                 ret = phar_tar_setmetadata(&entry->metadata, metadata, error);
     924           2 :                 efree(lookfor);
     925           2 :                 return ret;
     926             :         }
     927             : 
     928           2 :         newentry.filename = lookfor;
     929           2 :         newentry.filename_len = lookfor_len;
     930           2 :         newentry.phar = entry->phar;
     931           2 :         newentry.tar_type = TAR_FILE;
     932           2 :         newentry.is_tar = 1;
     933             : 
     934           4 :         if (NULL == (metadata = zend_hash_str_add_mem(&(entry->phar->manifest), lookfor, lookfor_len, (void *)&newentry, sizeof(phar_entry_info)))) {
     935           0 :                 efree(lookfor);
     936           0 :                 spprintf(error, 0, "phar tar error: unable to add magic metadata file to manifest for file \"%s\"", entry->filename);
     937           0 :                 return ZEND_HASH_APPLY_STOP;
     938             :         }
     939             : 
     940           2 :         return phar_tar_setmetadata(&entry->metadata, metadata, error);
     941             : }
     942             : /* }}} */
     943             : 
     944         195 : int phar_tar_flush(phar_archive_data *phar, char *user_stub, zend_long len, int defaultstub, char **error) /* {{{ */
     945             : {
     946         195 :         phar_entry_info entry = {0};
     947             :         static const char newstub[] = "<?php // tar-based phar archive stub file\n__HALT_COMPILER();";
     948             :         php_stream *oldfile, *newfile, *stubfile;
     949             :         int closeoldfile, free_user_stub, signature_length;
     950             :         struct _phar_pass_tar_info pass;
     951             :         char *buf, *signature, *tmp, sigbuf[8];
     952         195 :         char halt_stub[] = "__HALT_COMPILER();";
     953             : 
     954         195 :         entry.flags = PHAR_ENT_PERM_DEF_FILE;
     955         195 :         entry.timestamp = time(NULL);
     956         195 :         entry.is_modified = 1;
     957         195 :         entry.is_crc_checked = 1;
     958         195 :         entry.is_tar = 1;
     959         195 :         entry.tar_type = '0';
     960         195 :         entry.phar = phar;
     961         195 :         entry.fp_type = PHAR_MOD;
     962         195 :         entry.fp = NULL;
     963         195 :         entry.filename = NULL;
     964             : 
     965         195 :         if (phar->is_persistent) {
     966           0 :                 if (error) {
     967           0 :                         spprintf(error, 0, "internal error: attempt to flush cached tar-based phar \"%s\"", phar->fname);
     968             :                 }
     969           0 :                 return EOF;
     970             :         }
     971             : 
     972         195 :         if (phar->is_data) {
     973          20 :                 goto nostub;
     974             :         }
     975             : 
     976             :         /* set alias */
     977         215 :         if (!phar->is_temporary_alias && phar->alias_len) {
     978          40 :                 entry.filename = estrndup(".phar/alias.txt", sizeof(".phar/alias.txt")-1);
     979          40 :                 entry.filename_len = sizeof(".phar/alias.txt")-1;
     980          40 :                 entry.fp = php_stream_fopen_tmpfile();
     981          40 :                 if (entry.fp == NULL) {
     982           0 :                         efree(entry.filename);
     983           0 :                         spprintf(error, 0, "phar error: unable to create temporary file");
     984           0 :                         return -1;
     985             :                 }
     986          40 :                 if (phar->alias_len != (int)php_stream_write(entry.fp, phar->alias, phar->alias_len)) {
     987           0 :                         if (error) {
     988           0 :                                 spprintf(error, 0, "unable to set alias in tar-based phar \"%s\"", phar->fname);
     989             :                         }
     990           0 :                         php_stream_close(entry.fp);
     991           0 :                         efree(entry.filename);
     992           0 :                         return EOF;
     993             :                 }
     994             : 
     995          40 :                 entry.uncompressed_filesize = phar->alias_len;
     996             : 
     997          80 :                 if (NULL == zend_hash_str_update_mem(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info))) {
     998           0 :                         if (error) {
     999           0 :                                 spprintf(error, 0, "unable to set alias in tar-based phar \"%s\"", phar->fname);
    1000             :                         }
    1001           0 :                         php_stream_close(entry.fp);
    1002           0 :                         efree(entry.filename);
    1003           0 :                         return EOF;
    1004             :                 }
    1005             :                 /* At this point the entry is saved into the manifest. The manifest destroy
    1006             :                         routine will care about any resources to be freed. */
    1007             :         } else {
    1008         135 :                 zend_hash_str_del(&phar->manifest, ".phar/alias.txt", sizeof(".phar/alias.txt")-1);
    1009             :         }
    1010             : 
    1011             :         /* set stub */
    1012         201 :         if (user_stub && !defaultstub) {
    1013             :                 char *pos;
    1014          27 :                 if (len < 0) {
    1015             :                         /* resource passed in */
    1016           2 :                         if (!(php_stream_from_zval_no_verify(stubfile, (zval *)user_stub))) {
    1017           0 :                                 if (error) {
    1018           0 :                                         spprintf(error, 0, "unable to access resource to copy stub to new tar-based phar \"%s\"", phar->fname);
    1019             :                                 }
    1020           0 :                                 return EOF;
    1021             :                         }
    1022           2 :                         if (len == -1) {
    1023           1 :                                 len = PHP_STREAM_COPY_ALL;
    1024             :                         } else {
    1025           1 :                                 len = -len;
    1026             :                         }
    1027           2 :                         user_stub = 0;
    1028             : 
    1029             :                         // TODO: refactor to avoid reallocation ???
    1030             : //???           len = php_stream_copy_to_mem(stubfile, &user_stub, len, 0)
    1031             :                         {
    1032           2 :                                 zend_string *str = php_stream_copy_to_mem(stubfile, len, 0);
    1033           2 :                                 if (str) {
    1034           2 :                                         len = ZSTR_LEN(str);
    1035           2 :                                         user_stub = estrndup(ZSTR_VAL(str), ZSTR_LEN(str));
    1036             :                                         zend_string_release(str);
    1037             :                                 } else {
    1038           0 :                                         user_stub = NULL;
    1039           0 :                                         len = 0;
    1040             :                                 }
    1041             :                         }
    1042             : 
    1043           2 :                         if (!len || !user_stub) {
    1044           0 :                                 if (error) {
    1045           0 :                                         spprintf(error, 0, "unable to read resource to copy stub to new tar-based phar \"%s\"", phar->fname);
    1046             :                                 }
    1047           0 :                                 return EOF;
    1048             :                         }
    1049           2 :                         free_user_stub = 1;
    1050             :                 } else {
    1051          25 :                         free_user_stub = 0;
    1052             :                 }
    1053             : 
    1054          27 :                 tmp = estrndup(user_stub, len);
    1055          27 :                 if ((pos = php_stristr(tmp, halt_stub, len, sizeof(halt_stub) - 1)) == NULL) {
    1056           1 :                         efree(tmp);
    1057           1 :                         if (error) {
    1058           1 :                                 spprintf(error, 0, "illegal stub for tar-based phar \"%s\"", phar->fname);
    1059             :                         }
    1060           1 :                         if (free_user_stub) {
    1061           0 :                                 efree(user_stub);
    1062             :                         }
    1063           1 :                         return EOF;
    1064             :                 }
    1065          26 :                 pos = user_stub + (pos - tmp);
    1066          26 :                 efree(tmp);
    1067             : 
    1068          26 :                 len = pos - user_stub + 18;
    1069          26 :                 entry.fp = php_stream_fopen_tmpfile();
    1070          26 :                 if (entry.fp == NULL) {
    1071           0 :                         spprintf(error, 0, "phar error: unable to create temporary file");
    1072           0 :                         return EOF;
    1073             :                 }
    1074          26 :                 entry.uncompressed_filesize = len + 5;
    1075             : 
    1076          52 :                 if ((size_t)len != php_stream_write(entry.fp, user_stub, len)
    1077          52 :                 ||            5 != php_stream_write(entry.fp, " ?>\r\n", 5)) {
    1078           0 :                         if (error) {
    1079           0 :                                 spprintf(error, 0, "unable to create stub from string in new tar-based phar \"%s\"", phar->fname);
    1080             :                         }
    1081           0 :                         if (free_user_stub) {
    1082           0 :                                 efree(user_stub);
    1083             :                         }
    1084           0 :                         php_stream_close(entry.fp);
    1085           0 :                         return EOF;
    1086             :                 }
    1087             : 
    1088          26 :                 entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1);
    1089          26 :                 entry.filename_len = sizeof(".phar/stub.php")-1;
    1090          26 :                 zend_hash_str_update_mem(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info));
    1091             : 
    1092          26 :                 if (free_user_stub) {
    1093           2 :                         efree(user_stub);
    1094             :                 }
    1095             :         } else {
    1096             :                 /* Either this is a brand new phar (add the stub), or the default stub is required (overwrite the stub) */
    1097         148 :                 entry.fp = php_stream_fopen_tmpfile();
    1098         148 :                 if (entry.fp == NULL) {
    1099           0 :                         spprintf(error, 0, "phar error: unable to create temporary file");
    1100           0 :                         return EOF;
    1101             :                 }
    1102         148 :                 if (sizeof(newstub)-1 != php_stream_write(entry.fp, newstub, sizeof(newstub)-1)) {
    1103           0 :                         php_stream_close(entry.fp);
    1104           0 :                         if (error) {
    1105           0 :                                 spprintf(error, 0, "unable to %s stub in%star-based phar \"%s\", failed", user_stub ? "overwrite" : "create", user_stub ? " " : " new ", phar->fname);
    1106             :                         }
    1107           0 :                         return EOF;
    1108             :                 }
    1109             : 
    1110         148 :                 entry.uncompressed_filesize = entry.compressed_filesize = sizeof(newstub) - 1;
    1111         148 :                 entry.filename = estrndup(".phar/stub.php", sizeof(".phar/stub.php")-1);
    1112         148 :                 entry.filename_len = sizeof(".phar/stub.php")-1;
    1113             : 
    1114         148 :                 if (!defaultstub) {
    1115         131 :                         if (!zend_hash_str_exists(&phar->manifest, ".phar/stub.php", sizeof(".phar/stub.php")-1)) {
    1116          42 :                                 if (NULL == zend_hash_str_add_mem(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info))) {
    1117           0 :                                         php_stream_close(entry.fp);
    1118           0 :                                         efree(entry.filename);
    1119           0 :                                         if (error) {
    1120           0 :                                                 spprintf(error, 0, "unable to create stub in tar-based phar \"%s\"", phar->fname);
    1121             :                                         }
    1122           0 :                                         return EOF;
    1123             :                                 }
    1124             :                         } else {
    1125         110 :                                 php_stream_close(entry.fp);
    1126         110 :                                 efree(entry.filename);
    1127             :                         }
    1128             :                 } else {
    1129          34 :                         if (NULL == zend_hash_str_update_mem(&phar->manifest, entry.filename, entry.filename_len, (void*)&entry, sizeof(phar_entry_info))) {
    1130           0 :                                 php_stream_close(entry.fp);
    1131           0 :                                 efree(entry.filename);
    1132           0 :                                 if (error) {
    1133           0 :                                         spprintf(error, 0, "unable to overwrite stub in tar-based phar \"%s\"", phar->fname);
    1134             :                                 }
    1135           0 :                                 return EOF;
    1136             :                         }
    1137             :                 }
    1138             :         }
    1139             : nostub:
    1140         351 :         if (phar->fp && !phar->is_brandnew) {
    1141         157 :                 oldfile = phar->fp;
    1142         157 :                 closeoldfile = 0;
    1143         157 :                 php_stream_rewind(oldfile);
    1144             :         } else {
    1145          37 :                 oldfile = php_stream_open_wrapper(phar->fname, "rb", 0, NULL);
    1146          37 :                 closeoldfile = oldfile != NULL;
    1147             :         }
    1148             : 
    1149         194 :         newfile = php_stream_fopen_tmpfile();
    1150         194 :         if (!newfile) {
    1151           0 :                 if (error) {
    1152           0 :                         spprintf(error, 0, "unable to create temporary file");
    1153             :                 }
    1154           0 :                 if (closeoldfile) {
    1155           0 :                         php_stream_close(oldfile);
    1156             :                 }
    1157           0 :                 return EOF;
    1158             :         }
    1159             : 
    1160         194 :         pass.old = oldfile;
    1161         194 :         pass.new = newfile;
    1162         194 :         pass.error = error;
    1163         194 :         pass.free_fp = 1;
    1164         194 :         pass.free_ufp = 1;
    1165             : 
    1166         388 :         if (Z_TYPE(phar->metadata) != IS_UNDEF) {
    1167             :                 phar_entry_info *mentry;
    1168          18 :                 if (NULL != (mentry = zend_hash_str_find_ptr(&(phar->manifest), ".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1))) {
    1169           7 :                         if (ZEND_HASH_APPLY_KEEP != phar_tar_setmetadata(&phar->metadata, mentry, error)) {
    1170           0 :                                 if (closeoldfile) {
    1171           0 :                                         php_stream_close(oldfile);
    1172             :                                 }
    1173           0 :                                 return EOF;
    1174             :                         }
    1175             :                 } else {
    1176           2 :                         phar_entry_info newentry = {0};
    1177             : 
    1178           2 :                         newentry.filename = estrndup(".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1);
    1179           2 :                         newentry.filename_len = sizeof(".phar/.metadata.bin")-1;
    1180           2 :                         newentry.phar = phar;
    1181           2 :                         newentry.tar_type = TAR_FILE;
    1182           2 :                         newentry.is_tar = 1;
    1183             : 
    1184           4 :                         if (NULL == (mentry = zend_hash_str_add_mem(&(phar->manifest), ".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1, (void *)&newentry, sizeof(phar_entry_info)))) {
    1185           0 :                                 spprintf(error, 0, "phar tar error: unable to add magic metadata file to manifest for phar archive \"%s\"", phar->fname);
    1186           0 :                                 if (closeoldfile) {
    1187           0 :                                         php_stream_close(oldfile);
    1188             :                                 }
    1189           0 :                                 return EOF;
    1190             :                         }
    1191             : 
    1192           2 :                         if (ZEND_HASH_APPLY_KEEP != phar_tar_setmetadata(&phar->metadata, mentry, error)) {
    1193           0 :                                 zend_hash_str_del(&(phar->manifest), ".phar/.metadata.bin", sizeof(".phar/.metadata.bin")-1);
    1194           0 :                                 if (closeoldfile) {
    1195           0 :                                         php_stream_close(oldfile);
    1196             :                                 }
    1197           0 :                                 return EOF;
    1198             :                         }
    1199             :                 }
    1200             :         }
    1201             : 
    1202         194 :         zend_hash_apply_with_argument(&phar->manifest, phar_tar_setupmetadata, (void *) &pass);
    1203             : 
    1204         194 :         if (error && *error) {
    1205           0 :                 if (closeoldfile) {
    1206           0 :                         php_stream_close(oldfile);
    1207             :                 }
    1208             : 
    1209             :                 /* on error in the hash iterator above, error is set */
    1210           0 :                 php_stream_close(newfile);
    1211           0 :                 return EOF;
    1212             :         }
    1213             : 
    1214         194 :         zend_hash_apply_with_argument(&phar->manifest, phar_tar_writeheaders, (void *) &pass);
    1215             : 
    1216             :         /* add signature for executable tars or tars explicitly set with setSignatureAlgorithm */
    1217         194 :         if (!phar->is_data || phar->sig_flags) {
    1218         175 :                 if (FAILURE == phar_create_signature(phar, newfile, &signature, &signature_length, error)) {
    1219           0 :                         if (error) {
    1220           0 :                                 char *save = *error;
    1221           0 :                                 spprintf(error, 0, "phar error: unable to write signature to tar-based phar: %s", save);
    1222           0 :                                 efree(save);
    1223             :                         }
    1224             : 
    1225           0 :                         if (closeoldfile) {
    1226           0 :                                 php_stream_close(oldfile);
    1227             :                         }
    1228             : 
    1229           0 :                         php_stream_close(newfile);
    1230           0 :                         return EOF;
    1231             :                 }
    1232             : 
    1233         175 :                 entry.filename = ".phar/signature.bin";
    1234         175 :                 entry.filename_len = sizeof(".phar/signature.bin")-1;
    1235         175 :                 entry.fp = php_stream_fopen_tmpfile();
    1236         175 :                 if (entry.fp == NULL) {
    1237           0 :                         spprintf(error, 0, "phar error: unable to create temporary file");
    1238           0 :                         return EOF;
    1239             :                 }
    1240             : #ifdef WORDS_BIGENDIAN
    1241             : # define PHAR_SET_32(var, buffer) \
    1242             :         *(uint32_t *)(var) = (((((unsigned char*)&(buffer))[3]) << 24) \
    1243             :                 | ((((unsigned char*)&(buffer))[2]) << 16) \
    1244             :                 | ((((unsigned char*)&(buffer))[1]) << 8) \
    1245             :                 | (((unsigned char*)&(buffer))[0]))
    1246             : #else
    1247             : # define PHAR_SET_32(var, buffer) *(uint32_t *)(var) = (uint32_t) (buffer)
    1248             : #endif
    1249         175 :                 PHAR_SET_32(sigbuf, phar->sig_flags);
    1250         175 :                 PHAR_SET_32(sigbuf + 4, signature_length);
    1251             : 
    1252         175 :                 if (8 != (int)php_stream_write(entry.fp, sigbuf, 8) || signature_length != (int)php_stream_write(entry.fp, signature, signature_length)) {
    1253           0 :                         efree(signature);
    1254           0 :                         if (error) {
    1255           0 :                                 spprintf(error, 0, "phar error: unable to write signature to tar-based phar %s", phar->fname);
    1256             :                         }
    1257             : 
    1258           0 :                         if (closeoldfile) {
    1259           0 :                                 php_stream_close(oldfile);
    1260             :                         }
    1261           0 :                         php_stream_close(newfile);
    1262           0 :                         return EOF;
    1263             :                 }
    1264             : 
    1265         175 :                 efree(signature);
    1266         175 :                 entry.uncompressed_filesize = entry.compressed_filesize = signature_length + 8;
    1267             :                 /* throw out return value and write the signature */
    1268         175 :                 entry.filename_len = phar_tar_writeheaders_int(&entry, (void *)&pass);
    1269             : 
    1270         175 :                 if (error && *error) {
    1271           0 :                         if (closeoldfile) {
    1272           0 :                                 php_stream_close(oldfile);
    1273             :                         }
    1274             :                         /* error is set by writeheaders */
    1275           0 :                         php_stream_close(newfile);
    1276           0 :                         return EOF;
    1277             :                 }
    1278             :         } /* signature */
    1279             : 
    1280             :         /* add final zero blocks */
    1281         194 :         buf = (char *) ecalloc(1024, 1);
    1282         194 :         php_stream_write(newfile, buf, 1024);
    1283         194 :         efree(buf);
    1284             : 
    1285         194 :         if (closeoldfile) {
    1286           1 :                 php_stream_close(oldfile);
    1287             :         }
    1288             : 
    1289             :         /* on error in the hash iterator above, error is set */
    1290         194 :         if (error && *error) {
    1291           3 :                 php_stream_close(newfile);
    1292           3 :                 return EOF;
    1293             :         }
    1294             : 
    1295         191 :         if (phar->fp && pass.free_fp) {
    1296         157 :                 php_stream_close(phar->fp);
    1297             :         }
    1298             : 
    1299         191 :         if (phar->ufp) {
    1300           3 :                 if (pass.free_ufp) {
    1301           3 :                         php_stream_close(phar->ufp);
    1302             :                 }
    1303           3 :                 phar->ufp = NULL;
    1304             :         }
    1305             : 
    1306         191 :         phar->is_brandnew = 0;
    1307         191 :         php_stream_rewind(newfile);
    1308             : 
    1309         191 :         if (phar->donotflush) {
    1310             :                 /* deferred flush */
    1311           7 :                 phar->fp = newfile;
    1312             :         } else {
    1313         184 :                 phar->fp = php_stream_open_wrapper(phar->fname, "w+b", IGNORE_URL|STREAM_MUST_SEEK|REPORT_ERRORS, NULL);
    1314         184 :                 if (!phar->fp) {
    1315           0 :                         phar->fp = newfile;
    1316           0 :                         if (error) {
    1317           0 :                                 spprintf(error, 0, "unable to open new phar \"%s\" for writing", phar->fname);
    1318             :                         }
    1319           0 :                         return EOF;
    1320             :                 }
    1321             : 
    1322         184 :                 if (phar->flags & PHAR_FILE_COMPRESSED_GZ) {
    1323             :                         php_stream_filter *filter;
    1324             :                         /* to properly compress, we have to tell zlib to add a zlib header */
    1325             :                         zval filterparams;
    1326             : 
    1327           8 :                         array_init(&filterparams);
    1328             : /* this is defined in zlib's zconf.h */
    1329             : #ifndef MAX_WBITS
    1330             : #define MAX_WBITS 15
    1331             : #endif
    1332           8 :                         add_assoc_long(&filterparams, "window", MAX_WBITS + 16);
    1333           8 :                         filter = php_stream_filter_create("zlib.deflate", &filterparams, php_stream_is_persistent(phar->fp));
    1334             :                         zval_dtor(&filterparams);
    1335             : 
    1336           8 :                         if (!filter) {
    1337             :                                 /* copy contents uncompressed rather than lose them */
    1338           0 :                                 php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
    1339           0 :                                 php_stream_close(newfile);
    1340           0 :                                 if (error) {
    1341           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);
    1342             :                                 }
    1343           0 :                                 return EOF;
    1344             :                         }
    1345             : 
    1346           8 :                         php_stream_filter_append(&phar->fp->writefilters, filter);
    1347           8 :                         php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
    1348           8 :                         php_stream_filter_flush(filter, 1);
    1349           8 :                         php_stream_filter_remove(filter, 1);
    1350           8 :                         php_stream_close(phar->fp);
    1351             :                         /* use the temp stream as our base */
    1352           8 :                         phar->fp = newfile;
    1353         176 :                 } else if (phar->flags & PHAR_FILE_COMPRESSED_BZ2) {
    1354             :                         php_stream_filter *filter;
    1355             : 
    1356           4 :                         filter = php_stream_filter_create("bzip2.compress", NULL, php_stream_is_persistent(phar->fp));
    1357           4 :                         php_stream_filter_append(&phar->fp->writefilters, filter);
    1358           4 :                         php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
    1359           4 :                         php_stream_filter_flush(filter, 1);
    1360           4 :                         php_stream_filter_remove(filter, 1);
    1361           4 :                         php_stream_close(phar->fp);
    1362             :                         /* use the temp stream as our base */
    1363           4 :                         phar->fp = newfile;
    1364             :                 } else {
    1365         172 :                         php_stream_copy_to_stream_ex(newfile, phar->fp, PHP_STREAM_COPY_ALL, NULL);
    1366             :                         /* we could also reopen the file in "rb" mode but there is no need for that */
    1367         172 :                         php_stream_close(newfile);
    1368             :                 }
    1369             :         }
    1370         191 :         return EOF;
    1371             : }
    1372             : /* }}} */
    1373             : 
    1374             : /*
    1375             :  * Local variables:
    1376             :  * tab-width: 4
    1377             :  * c-basic-offset: 4
    1378             :  * End:
    1379             :  * vim600: noet sw=4 ts=4 fdm=marker
    1380             :  * vim<600: noet sw=4 ts=4
    1381             :  */

Generated by: LCOV version 1.10

Generated at Tue, 27 Sep 2016 10:26:05 +0000 (2 days ago)

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