Source for file OLE.php
Documentation is available at OLE.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Xavier Noguer <xnoguer@php.net> |
// | Based on OLE::Storage_Lite by Kawai, Takanori |
// +----------------------------------------------------------------------+
// $Id: OLE.php 260165 2008-05-23 16:33:58Z schmidt $
* Constants for OLE package
define('OLE_PPS_TYPE_ROOT', 5 );
define('OLE_PPS_TYPE_DIR', 1 );
define('OLE_PPS_TYPE_FILE', 2 );
define('OLE_DATA_SIZE_SMALL', 0x1000 );
define('OLE_LONG_INT_SIZE', 4 );
* Array for storing OLE instances that are accessed from
* OLE_ChainedBlockStream::stream_open().
$GLOBALS['_OLE_INSTANCES'] = array ();
* OLE package base class.
* @author Xavier Noguer <xnoguer@php.net>
* @author Christian Schmidt <schmidt@php.net>
* The file handle for reading an OLE container
* Array of PPS's found on the OLE container
* Root directory of OLE container
* Big Block Allocation Table
* @var array (blockId => nextBlockId)
* Short Block Allocation Table
* @var array (blockId => nextBlockId)
* Size of big blocks. This is usually 512.
* @var int number of octets per block.
* Size of small blocks. This is usually 64.
* @var int number of octets per block
* Creates a new OLE object
* Destructor (using PEAR)
* Just closes the file handle on the OLE file.
* Reads an OLE container from the contents of the file given.
* @return mixed true on success, PEAR_Error on failure
$fh = @fopen($file, "r");
return $this->raiseError (" Can't open file $file" );
$this->_file_handle = $fh;
$signature = fread($fh, 8 );
if ("\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" != $signature) {
return $this->raiseError ("File doesn't seem to be an OLE container.");
if (fread($fh, 2 ) != "\xFE\xFF") {
// This shouldn't be a problem in practice
return $this->raiseError ("Only Little-Endian encoding is supported.");
// Size of blocks and short blocks in bytes
// Skip UID, revision number and version number
// Number of blocks in Big Block Allocation Table
$bbatBlockCount = $this->_readInt4 ($fh);
$directoryFirstBlockId = $this->_readInt4 ($fh);
// Streams shorter than this are stored using small blocks
$this->bigBlockThreshold = $this->_readInt4 ($fh);
// Block id of first sector in Short Block Allocation Table
$sbatFirstBlockId = $this->_readInt4 ($fh);
// Number of blocks in Short Block Allocation Table
$sbbatBlockCount = $this->_readInt4 ($fh);
// Block id of first sector in Master Block Allocation Table
$mbatFirstBlockId = $this->_readInt4 ($fh);
// Number of blocks in Master Block Allocation Table
$mbbatBlockCount = $this->_readInt4 ($fh);
// Remaining 4 * 109 bytes of current block is beginning of Master
// Block Allocation Table
for ($i = 0; $i < 109; $i++ ) {
$mbatBlocks[] = $this->_readInt4 ($fh);
// Read rest of Master Block Allocation Table (if any is left)
$pos = $this->_getBlockOffset ($mbatFirstBlockId);
for ($i = 0; $i < $mbbatBlockCount; $i++ ) {
$mbatBlocks[] = $this->_readInt4 ($fh);
// Last block id in each block points to next block
$pos = $this->_getBlockOffset ($this->_readInt4 ($fh));
// Read Big Block Allocation Table according to chain specified by
for ($i = 0; $i < $bbatBlockCount; $i++ ) {
$pos = $this->_getBlockOffset ($mbatBlocks[$i]);
$this->bbat[] = $this->_readInt4 ($fh);
// Read short block allocation table (SBAT)
$shortBlockCount = $sbbatBlockCount * $this->bigBlockSize / 4;
$sbatFh = $this->getStream($sbatFirstBlockId);
// Avoid an infinite loop if ChainedBlockStream.php somehow is
for ($blockId = 0; $blockId < $shortBlockCount; $blockId++ ) {
$this->sbat[$blockId] = $this->_readInt4 ($sbatFh);
$this->_readPpsWks ($directoryFirstBlockId);
* @param int $blockId block id
* @return int byte offset from beginning of file
function _getBlockOffset ($blockId)
* Returns a stream for use with fread() etc. External callers should
* use OLE_PPS_File::getStream().
* @param int|PPS$blockIdOrPps block id or PPS
* @return resource read-only stream
include_once 'OLE/ChainedBlockStream.php';
static $isRegistered = false;
stream_wrapper_register ('ole-chainedblockstream',
'OLE_ChainedBlockStream');
// Store current instance in global array, so that it can be accessed
// in OLE_ChainedBlockStream::stream_open().
// Object is removed from self::$instances in OLE_Stream::close().
$GLOBALS['_OLE_INSTANCES'][] = $this;
$instanceId = end (array_keys ($GLOBALS['_OLE_INSTANCES']));
$path = 'ole-chainedblockstream://oleInstanceId=' . $instanceId;
if (is_a ($blockIdOrPps, 'OLE_PPS')) {
$path .= '&blockId=' . $blockIdOrPps->_StartBlock;
$path .= '&size=' . $blockIdOrPps->Size;
$path .= '&blockId=' . $blockIdOrPps;
return fopen($path, 'r');
* @param resource $fh file handle
* Reads an unsigned short (2 octets).
* @param resource $fh file handle
* Reads an unsigned long (4 octets).
* @param resource file handle
* Gets information about all PPS's on the OLE container from the PPS WK's
* creates an OLE_PPS object for each one.
* @param integer $blockId the block id of the first block
* @return mixed true on success, PEAR_Error on failure
function _readPpsWks ($blockId)
for ($pos = 0; ; $pos += 128 ) {
fseek($fh, $pos, SEEK_SET );
$nameUtf16 = fread($fh, 64 );
$nameLength = $this->_readInt2 ($fh);
$nameUtf16 = substr($nameUtf16, 0 , $nameLength - 2 );
// Simple conversion from UTF-16LE to ISO-8859-1
$type = $this->_readInt1 ($fh);
require_once 'OLE/PPS/Root.php';
$pps = new OLE_PPS(null , null , null , null , null ,
null , null , null , null , array ());
require_once 'OLE/PPS/File.php';
$pps->PrevPps = $this->_readInt4 ($fh);
$pps->NextPps = $this->_readInt4 ($fh);
$pps->DirPps = $this->_readInt4 ($fh);
fseek($fh, 20 , SEEK_CUR );
$pps->_StartBlock = $this->_readInt4 ($fh);
$pps->Size = $this->_readInt4 ($fh);
$pps->No = count($this->_list );
// check if the PPS tree (starting from root) is complete
if (isset ($this->root) &&
$this->_ppsTreeComplete ($this->root->No )) {
// Initialize $pps->children on directories
foreach ($this->_list as $pps) {
$nos = array ($pps->DirPps );
$pps->children = array ();
$childPps = $this->_list [$no];
$nos[] = $childPps->PrevPps;
$nos[] = $childPps->NextPps;
$pps->children [] = $childPps;
* It checks whether the PPS tree is complete (all PPS's read)
* starting with the given PPS (not necessarily root)
* @param integer $index The index of the PPS from which we are checking
* @return boolean Whether the PPS tree for the given PPS is complete
function _ppsTreeComplete ($index)
return isset ($this->_list [$index]) &&
($pps = $this->_list [$index]) &&
$this->_ppsTreeComplete ($pps->PrevPps )) &&
$this->_ppsTreeComplete ($pps->NextPps )) &&
$this->_ppsTreeComplete ($pps->DirPps ));
* Checks whether a PPS is a File PPS or not.
* If there is no PPS for the index given, it will return false.
* @param integer $index The index for the PPS
* @return bool true if it's a File PPS, false otherwise
if (isset ($this->_list [$index])) {
* Checks whether a PPS is a Root PPS or not.
* If there is no PPS for the index given, it will return false.
* @param integer $index The index for the PPS.
* @return bool true if it's a Root PPS, false otherwise
if (isset ($this->_list [$index])) {
* Gives the total number of PPS's found in the OLE container.
* @return integer The total number of PPS's found in the OLE container
return count($this->_list );
* If there is no PPS for the index given, it will return an empty string.
* @param integer $index The index for the PPS
* @param integer $position The position from which to start reading
* @param integer $length The amount of bytes to read (at most)
* @return string The binary string containing the data requested
* @see OLE_PPS_File::getStream()
function getData($index, $position, $length)
// if position is not valid return empty string
if (!isset ($this->_list [$index]) ||
$position >= $this->_list [$index]->Size ||
$fh = $this->getStream($this->_list [$index]);
* Gets the data length from a PPS
* If there is no PPS for the index given, it will return 0.
* @param integer $index The index for the PPS
* @return integer The amount of bytes in data the PPS has
if (isset ($this->_list [$index])) {
return $this->_list [$index]->Size;
* Utility function to transform ASCII text to Unicode
* @param string $ascii The ASCII string to transform
* @return string The string in Unicode
for ($i = 0; $i < strlen($ascii); $i++ ) {
$rawname .= $ascii{$i} . "\x00";
* Returns a string for the OLE container with the date given
* @param integer $date A timestamp
* @return string The string for the OLE container
return "\x00\x00\x00\x00\x00\x00\x00\x00";
// factor used for separating numbers into 4 bytes parts
// days from 1-1-1601 until the beggining of UNIX era
$big_date = $days * 24 * 3600 +
// multiply just to make MS happy
$high_part = floor($big_date / $factor);
$low_part = floor((($big_date / $factor) - $high_part) * $factor);
for ($i = 0; $i < 4; $i++ ) {
$hex = $low_part % 0x100;
for ($i = 0; $i < 4; $i++ ) {
$hex = $high_part % 0x100;
* Returns a timestamp from an OLE container's date
* @param integer $string A binary string with the encoded date
* @return string The timestamp corresponding to the string
return new PEAR_Error ("Expecting 8 byte string");
// factor used for separating numbers into 4 bytes parts
for ($i = 0; $i < 4; $i++ ) {
list (, $high_part) = unpack('C', $string{(7 - $i)});
for ($i = 4; $i < 8; $i++ ) {
list (, $low_part) = unpack('C', $string{(7 - $i)});
$big_date = ($high_part * $factor) + $low_part;
// days from 1-1-1601 until the beggining of UNIX era
// translate to seconds from beggining of UNIX era
$big_date -= $days * 24 * 3600;
Documentation generated on Mon, 11 Mar 2019 15:47:10 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|