*
* 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;
$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.
*/
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'];
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,
* 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 ) {
}
return $crc;
}
-
+
/**
- * @param resource $destination
- * @param int $bytesToCopy
- * @return bool
+ * @param $destination Resource
+ * @param $bytesToCopy Integer
+ * @return Boolean
* @access private
*/
function copyBytesTo( $destination, $bytesToCopy ) {
fwrite( $destination, $buffer );
}
}
-
+
/**
* Hex dump of the header for debugging
* @access private
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