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

Generated by: LCOV version 1.10

Generated at Sun, 29 Mar 2015 03:45:56 +0000 (3 hours ago)

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