cvs: pear /PHP_Archive/Archive Manager.php
| From: | Greg Beaver | Date: | Tue, 03 Jan 2006 04:57:16 +0000 |
| Subject: | cvs: pear /PHP_Archive/Archive Manager.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-37277@lists.php.net to get a copy of this message | ||
cellog Tue Jan 3 04:57:16 2006 UTC
Modified files:
/pear/PHP_Archive/Archive Manager.php
Log:
complete the initial validation routine
http://cvs.php.net/viewcvs.cgi/pear/PHP_Archive/Archive/Manager.php?r1=1.2&r2=1.3&diff_format=u Index: pear/PHP_Archive/Archive/Manager.php diff -u pear/PHP_Archive/Archive/Manager.php:1.2 pear/PHP_Archive/Archive/Manager.php:1.3 --- pear/PHP_Archive/Archive/Manager.php:1.2 Mon Dec 19 22:18:05 2005 +++ pear/PHP_Archive/Archive/Manager.php Tue Jan 3 04:57:16 2006 @@ -11,9 +11,9 @@ require_once 'System.php'; /** * - * @copyright Copyright © Gregory Beaver + * @copyright Copyright ? Gregory Beaver * @author Greg Beaver <cellog@php.net> - * @version $Id: Manager.php,v 1.2 2005/12/19 22:18:05 cellog Exp $ + * @version $Id: Manager.php,v 1.3 2006/01/03 04:57:16 cellog Exp $ * @package PHP_Archive * @category PHP */ @@ -47,7 +47,7 @@ while (!feof($fp) && $c = fgetc($fp)) { $version .= $c; } - if (version_compare($version, '0.7', '<')) { + if (version_compare($version, '0.7.1', '<')) { require_once 'PHP/Archive/Exception.php'; throw new PHP_Archive_Exception($phar . ' was created with obsolete PHP_Archive'); } @@ -140,13 +140,69 @@ throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); } $manifest = substr($manifest, 4); + if (strlen($manifest) < 4) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESUNDERFLOW, array( + 'archive' => $this->_archiveName)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } $ret = array(); for ($i = 0; $i < $info[1]; $i++) { + if (strlen($manifest) < 4) { + if (isset($savepath)) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESTRUNCATEDENTRY, array( + 'archive' => $this->_archiveName, 'last' => $savepath, + 'current' => '*unknown*', 'size' => $info[1], 'cur' => $i)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } else { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESTRUNCATEDENTRY, array( + 'archive' => $this->_archiveName, 'last' => '*none*', + 'current' => '*unknown*', 'size' => $info[1], 'cur' => $i)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } + } // length of the file name $len = unpack('V', substr($manifest, 0, 4)); + if (strlen($manifest) < $len[1] + 4) { + if (isset($savepath)) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESTRUNCATEDENTRY, array( + 'archive' => $this->_archiveName, 'last' => $savepath, + 'current' => '*unknown*', 'size' => $info[1], 'cur' => $i)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } else { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESTRUNCATEDENTRY, array( + 'archive' => $this->_archiveName, 'last' => '*none*', + 'current' => '*unknown*', 'size' => $info[1], 'cur' => $i)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } + } // file name + if (!isset($savepath)) { + $last = '*none*'; + } else { + $last = $savepath; + } $savepath = substr($manifest, 4, $len[1]); $manifest = substr($manifest, $len[1] + 4); + if (strlen($manifest) < 16) { + if (isset($savepath)) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESTRUNCATEDENTRY, array( + 'archive' => $this->_archiveName, 'last' => $last, + 'current' => $savepath, 'size' => $info[1], 'cur' => $i)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } else { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESTRUNCATEDENTRY, array( + 'archive' => $this->_archiveName, 'last' => $last, + 'current' => $savepath, 'size' => $info[1], 'cur' => $i)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } + } // retrieve manifest data: // 0 = uncompressed file size // 1 = timestamp of when file was added to phar @@ -157,6 +213,68 @@ } $this->_manifest = $ret; $this->_fileStart = ftell($fp); + foreach ($this->_manifest as $path => $info) { + $currentFilename = $path; + // actual file length in file includes 8-byte header + $internalFileLength = $info[3] - 8; + // seek to offset of file header within the .phar + if (!@fseek($this->fp, $this->_fileStart + $info[2])) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::FILELOCATIONINVALID, + array('file' => $path, 'loc' => $this->_fileStart + $info[2], + 'size' => filesize($this->_archiveName))); + continue; + } + $tdata = @fread($this->fp, 8); + if (strlen($tdata) < 8) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::FILETRUNCATED, + array('file' => $path, 'loc' => $this->_fileStart + $info[2])); + continue; + } + $temp = @unpack("Vcrc32/Visize", $tdata); + if (!$temp) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::FILECORRUPTEDCRCSIZE, + array('file' => $path, 'loc' => $this->_fileStart + $info[2])); + continue; + } + $data = ''; + $count = $this->internalFileLength; + while ($count) { + if ($count < 8192) { + $data .= @fread($this->fp, $count); + $count = 0; + } else { + $count -= 8192; + $data .= @fread($this->fp, 8192); + } + } + @fclose($this->fp); + if ($this->_compressed) { + $data = @gzinflate($data); + if ($data === false) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::FILECORRUPTEDGZ, + array('file' => $path, 'loc' => $this->_fileStart + $info[2])); + } + } + if ($temp['isize'] != $info[0]) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::FILECORRUPTEDSIZE, + array('file' => $path, 'expected' => $temp['isize'], + 'actual' => $info[0])); + } + if ($temp['crc32'] != crc32($data)) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::FILECORRUPTEDCRC, + array('file' => $path, 'expected' => $temp['crc32'], + 'actual' => crc32($data))); + } + } + if (count($errors)) { + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } fclose($fp); }
http://cvs.php.net/viewcvs.cgi/pear/PHP_Archive/Archive/Manager.php?r1=1.2&r2=1.3&diff_format=u Index: pear/PHP_Archive/Archive/Manager.php diff -u pear/PHP_Archive/Archive/Manager.php:1.2 pear/PHP_Archive/Archive/Manager.php:1.3 --- pear/PHP_Archive/Archive/Manager.php:1.2 Mon Dec 19 22:18:05 2005 +++ pear/PHP_Archive/Archive/Manager.php Tue Jan 3 04:57:16 2006 @@ -11,9 +11,9 @@ require_once 'System.php'; /** * - * @copyright Copyright © Gregory Beaver + * @copyright Copyright ? Gregory Beaver * @author Greg Beaver <cellog@php.net> - * @version $Id: Manager.php,v 1.2 2005/12/19 22:18:05 cellog Exp $ + * @version $Id: Manager.php,v 1.3 2006/01/03 04:57:16 cellog Exp $ * @package PHP_Archive * @category PHP */ @@ -47,7 +47,7 @@ while (!feof($fp) && $c = fgetc($fp)) { $version .= $c; } - if (version_compare($version, '0.7', '<')) { + if (version_compare($version, '0.7.1', '<')) { require_once 'PHP/Archive/Exception.php'; throw new PHP_Archive_Exception($phar . ' was created with obsolete PHP_Archive'); } @@ -140,13 +140,69 @@ throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); } $manifest = substr($manifest, 4); + if (strlen($manifest) < 4) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESUNDERFLOW, array( + 'archive' => $this->_archiveName)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } $ret = array(); for ($i = 0; $i < $info[1]; $i++) { + if (strlen($manifest) < 4) { + if (isset($savepath)) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESTRUNCATEDENTRY, array( + 'archive' => $this->_archiveName, 'last' => $savepath, + 'current' => '*unknown*', 'size' => $info[1], 'cur' => $i)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } else { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESTRUNCATEDENTRY, array( + 'archive' => $this->_archiveName, 'last' => '*none*', + 'current' => '*unknown*', 'size' => $info[1], 'cur' => $i)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } + } // length of the file name $len = unpack('V', substr($manifest, 0, 4)); + if (strlen($manifest) < $len[1] + 4) { + if (isset($savepath)) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESTRUNCATEDENTRY, array( + 'archive' => $this->_archiveName, 'last' => $savepath, + 'current' => '*unknown*', 'size' => $info[1], 'cur' => $i)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } else { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESTRUNCATEDENTRY, array( + 'archive' => $this->_archiveName, 'last' => '*none*', + 'current' => '*unknown*', 'size' => $info[1], 'cur' => $i)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } + } // file name + if (!isset($savepath)) { + $last = '*none*'; + } else { + $last = $savepath; + } $savepath = substr($manifest, 4, $len[1]); $manifest = substr($manifest, $len[1] + 4); + if (strlen($manifest) < 16) { + if (isset($savepath)) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESTRUNCATEDENTRY, array( + 'archive' => $this->_archiveName, 'last' => $last, + 'current' => $savepath, 'size' => $info[1], 'cur' => $i)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } else { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::MANIFESTENTRIESTRUNCATEDENTRY, array( + 'archive' => $this->_archiveName, 'last' => $last, + 'current' => $savepath, 'size' => $info[1], 'cur' => $i)); + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } + } // retrieve manifest data: // 0 = uncompressed file size // 1 = timestamp of when file was added to phar @@ -157,6 +213,68 @@ } $this->_manifest = $ret; $this->_fileStart = ftell($fp); + foreach ($this->_manifest as $path => $info) { + $currentFilename = $path; + // actual file length in file includes 8-byte header + $internalFileLength = $info[3] - 8; + // seek to offset of file header within the .phar + if (!@fseek($this->fp, $this->_fileStart + $info[2])) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::FILELOCATIONINVALID, + array('file' => $path, 'loc' => $this->_fileStart + $info[2], + 'size' => filesize($this->_archiveName))); + continue; + } + $tdata = @fread($this->fp, 8); + if (strlen($tdata) < 8) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::FILETRUNCATED, + array('file' => $path, 'loc' => $this->_fileStart + $info[2])); + continue; + } + $temp = @unpack("Vcrc32/Visize", $tdata); + if (!$temp) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::FILECORRUPTEDCRCSIZE, + array('file' => $path, 'loc' => $this->_fileStart + $info[2])); + continue; + } + $data = ''; + $count = $this->internalFileLength; + while ($count) { + if ($count < 8192) { + $data .= @fread($this->fp, $count); + $count = 0; + } else { + $count -= 8192; + $data .= @fread($this->fp, 8192); + } + } + @fclose($this->fp); + if ($this->_compressed) { + $data = @gzinflate($data); + if ($data === false) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::FILECORRUPTEDGZ, + array('file' => $path, 'loc' => $this->_fileStart + $info[2])); + } + } + if ($temp['isize'] != $info[0]) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::FILECORRUPTEDSIZE, + array('file' => $path, 'expected' => $temp['isize'], + 'actual' => $info[0])); + } + if ($temp['crc32'] != crc32($data)) { + $errors[] = new PHP_Archive_ExceptionExtended( + PHP_Archive_ExceptionExtended::FILECORRUPTEDCRC, + array('file' => $path, 'expected' => $temp['crc32'], + 'actual' => crc32($data))); + } + } + if (count($errors)) { + throw new PHP_Archive_Exception('invalid phar "' . $this->_archiveName . '"', $errors); + } fclose($fp); }