X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FMacBinary.php;h=0c38a64110c9297c2b0a2e1344f32d0caa75e949;hb=44eefff24c088ded89ad02dab4516b94e8c3aca5;hp=b4f508a9e91f1a7f15001592ab1eaabb4771fa67;hpb=3a64157feaaa54f2a46699b5dbd3394ad10decad;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/MacBinary.php b/includes/MacBinary.php index b4f508a9e9..0c38a64110 100644 --- a/includes/MacBinary.php +++ b/includes/MacBinary.php @@ -6,37 +6,36 @@ * Copyright (C) 2005 Brion Vibber * Portions based on Convert::BinHex by Eryq et al * http://www.mediawiki.org/ - * + * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or + * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * http://www.gnu.org/copyleft/gpl.html * - * @package MediaWiki - * @subpackage SpecialPage + * @ingroup SpecialPage */ class MacBinary { - function MacBinary( $filename ) { + function __construct( $filename ) { $this->open( $filename ); $this->loadHeader(); } - + /** * The file must be seekable, such as local filesystem. * Remote URLs probably won't work. * - * @param string $filename + * @param $filename String */ function open( $filename ) { $this->valid = false; @@ -46,47 +45,50 @@ class MacBinary { $this->resourceLength = 0; $this->handle = fopen( $filename, 'rb' ); } - + /** * Does this appear to be a valid MacBinary archive? - * @return bool + * + * @return Boolean */ function isValid() { return $this->valid; } - + /** * Get length of data fork - * @return int + * + * @return Integer */ function dataForkLength() { return $this->dataLength; } - + /** * Copy the data fork to an external file or resource. - * @param resource $destination - * @return bool + * + * @param $destination Ressource + * @return Boolean */ function extractData( $destination ) { if( !$this->isValid() ) { return false; } - + // Data fork appears immediately after header fseek( $this->handle, 128 ); return $this->copyBytesTo( $destination, $this->dataLength ); } - + /** * */ function close() { fclose( $this->handle ); } - + // -------------------------------------------------------------- - + /** * Check if the given file appears to be MacBinary-encoded, * as Internet Explorer on Mac OS may provide for unknown types. @@ -98,21 +100,21 @@ class MacBinary { */ function loadHeader() { $fname = 'MacBinary::loadHeader'; - + fseek( $this->handle, 0 ); $head = fread( $this->handle, 128 ); - $this->hexdump( $head ); - + #$this->hexdump( $head ); + if( strlen( $head ) < 128 ) { wfDebug( "$fname: couldn't read full MacBinary header\n" ); return false; } - + if( $head{0} != "\x00" || $head{74} != "\x00" ) { wfDebug( "$fname: header bytes 0 and 74 not null\n" ); return false; } - + $signature = substr( $head, 102, 4 ); $a = unpack( "ncrc", substr( $head, 124, 2 ) ); $storedCRC = $a['crc']; @@ -139,34 +141,34 @@ class MacBinary { return false; } } - + $nameLength = ord( $head{1} ); if( $nameLength < 1 || $nameLength > 63 ) { wfDebug( "$fname: invalid filename size $nameLength\n" ); return false; } $this->filename = substr( $head, 2, $nameLength ); - + $forks = unpack( "Ndata/Nresource", substr( $head, 83, 8 ) ); $this->dataLength = $forks['data']; $this->resourceLength = $forks['resource']; $maxForkLength = 0x7fffff; - + if( $this->dataLength < 0 || $this->dataLength > $maxForkLength ) { wfDebug( "$fname: invalid data fork length $this->dataLength\n" ); return false; } - + if( $this->resourceLength < 0 || $this->resourceLength > $maxForkLength ) { wfDebug( "$fname: invalid resource fork size $this->resourceLength\n" ); return false; } - + wfDebug( "$fname: appears to be MacBinary $this->version, data length $this->dataLength\n" ); $this->valid = true; return true; } - + /** * Calculate a 16-bit CRC value as for MacBinary headers. * Adapted from perl5 Convert::BinHex by Eryq, @@ -174,9 +176,9 @@ class MacBinary { * with magic array thingy by Jim Van Verth. * http://search.cpan.org/~eryq/Convert-BinHex-1.119/lib/Convert/BinHex.pm * - * @param string $data - * @param int $seed - * @return int + * @param $data String + * @param $seed Integer + * @return Integer * @access private */ function calcCRC( $data, $seed = 0 ) { @@ -225,11 +227,11 @@ class MacBinary { } return $crc; } - + /** - * @param resource $destination - * @param int $bytesToCopy - * @return bool + * @param $destination Resource + * @param $bytesToCopy Integer + * @return Boolean * @access private */ function copyBytesTo( $destination, $bytesToCopy ) { @@ -240,7 +242,7 @@ class MacBinary { fwrite( $destination, $buffer ); } } - + /** * Hex dump of the header for debugging * @access private @@ -248,22 +250,23 @@ class MacBinary { function hexdump( $data ) { global $wgDebugLogFile; if( !$wgDebugLogFile ) return; - + $width = 16; $at = 0; for( $remaining = strlen( $data ); $remaining > 0; $remaining -= $width ) { $line = sprintf( "%04x:", $at ); $printable = ''; - for( $i = 0; $i < $width; $i++ ) { + for( $i = 0; $i < $width && $remaining - $i > 0; $i++ ) { $byte = ord( $data{$at++} ); $line .= sprintf( " %02x", $byte ); $printable .= ($byte >= 32 && $byte <= 126 ) ? chr( $byte ) : '.'; } + if( $i < $width ) { + $line .= str_repeat( ' ', $width - $i ); + } wfDebug( "MacBinary: $line $printable\n" ); } } } - -?> \ No newline at end of file