$this->open( $filename );
$this->loadHeader();
}
-
+
/**
* The file must be seekable, such as local filesystem.
* Remote URLs probably won't work.
$this->resourceLength = 0;
$this->handle = fopen( $filename, 'rb' );
}
-
+
/**
* Does this appear to be a valid MacBinary archive?
* @return bool
function isValid() {
return $this->valid;
}
-
+
/**
* Get length of data fork
* @return int
function dataForkLength() {
return $this->dataLength;
}
-
+
/**
* Copy the data fork to an external file or resource.
* @param resource $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 );
-
+
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,
}
return $crc;
}
-
+
/**
* @param resource $destination
* @param int $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 ) {