Revert r28215: incorrectly moved files
[lhc/web/wiklou.git] / includes / MacBinary.php
index b4f508a..da357e5 100644 (file)
@@ -6,32 +6,31 @@
  * Copyright (C) 2005 Brion Vibber <brion@pobox.com>
  * 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
+ * @addtogroup 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.
@@ -46,7 +45,7 @@ class MacBinary {
                $this->resourceLength = 0;
                $this->handle = fopen( $filename, 'rb' );
        }
-       
+
        /**
         * Does this appear to be a valid MacBinary archive?
         * @return bool
@@ -54,7 +53,7 @@ class MacBinary {
        function isValid() {
                return $this->valid;
        }
-       
+
        /**
         * Get length of data fork
         * @return int
@@ -62,7 +61,7 @@ class MacBinary {
        function dataForkLength() {
                return $this->dataLength;
        }
-       
+
        /**
         * Copy the data fork to an external file or resource.
         * @param resource $destination
@@ -72,21 +71,21 @@ class MacBinary {
                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 +97,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 +138,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,
@@ -225,7 +224,7 @@ class MacBinary {
                }
                return $crc;
        }
-       
+
        /**
         * @param resource $destination
         * @param int $bytesToCopy
@@ -240,7 +239,7 @@ class MacBinary {
                        fwrite( $destination, $buffer );
                }
        }
-       
+
        /**
         * Hex dump of the header for debugging
         * @access private
@@ -248,22 +247,25 @@ 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
+