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

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:59:02 +0000 (7 days ago)

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