123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- <?php
- require_once __DIR__.'/../PPS.php';
- class OLE_PPS_Root extends OLE_PPS
- {
-
- var $_tmp_dir;
-
- function OLE_PPS_Root($time_1st, $time_2nd, $raChild)
- {
- $this->_tmp_dir = '';
- $this->OLE_PPS(
- null,
- OLE::Asc2Ucs('Root Entry'),
- OLE_PPS_TYPE_ROOT,
- null,
- null,
- null,
- $time_1st,
- $time_2nd,
- null,
- $raChild);
- }
-
- function setTempDir($dir)
- {
- if (is_dir($dir)) {
- $this->_tmp_dir = $dir;
- return true;
- }
- return false;
- }
-
- function save($filename)
- {
-
- $this->_BIG_BLOCK_SIZE = pow(2,
- ((isset($this->_BIG_BLOCK_SIZE))? $this->_adjust2($this->_BIG_BLOCK_SIZE) : 9));
- $this->_SMALL_BLOCK_SIZE= pow(2,
- ((isset($this->_SMALL_BLOCK_SIZE))? $this->_adjust2($this->_SMALL_BLOCK_SIZE): 6));
-
- if (($filename == '-') || ($filename == '')) {
- $this->_tmp_filename = tempnam($this->_tmp_dir, "OLE_PPS_Root");
- $this->_FILEH_ = @fopen($this->_tmp_filename,"w+b");
- if ($this->_FILEH_ == false) {
- return $this->raiseError("Can't create temporary file.");
- }
- } else {
- $this->_FILEH_ = @fopen($filename, "wb");
- if ($this->_FILEH_ == false) {
- return $this->raiseError("Can't open $filename. It may be in use or protected.");
- }
- }
-
- $aList = array();
- $this->_savePpsSetPnt($aList);
-
- list($iSBDcnt, $iBBcnt, $iPPScnt) = $this->_calcSize($aList);
-
- $this->_saveHeader($iSBDcnt, $iBBcnt, $iPPScnt);
-
- $this->_data = $this->_makeSmallData($aList);
-
- $this->_saveBigData($iSBDcnt, $aList);
-
- $this->_savePps($aList);
-
- $this->_saveBbd($iSBDcnt, $iBBcnt, $iPPScnt);
-
- if (($filename == '-') || ($filename == '')) {
- fseek($this->_FILEH_, 0);
- fpassthru($this->_FILEH_);
- @fclose($this->_FILEH_);
-
- @unlink($this->_tmp_filename);
- } else {
- @fclose($this->_FILEH_);
- }
- return true;
- }
-
- function _calcSize(&$raList)
- {
-
- list($iSBDcnt, $iBBcnt, $iPPScnt) = array(0,0,0);
- $iSmallLen = 0;
- $iSBcnt = 0;
- for ($i = 0; $i < count($raList); $i++) {
- if ($raList[$i]->Type == OLE_PPS_TYPE_FILE) {
- $raList[$i]->Size = $raList[$i]->_DataLen();
- if ($raList[$i]->Size < OLE_DATA_SIZE_SMALL) {
- $iSBcnt += floor($raList[$i]->Size / $this->_SMALL_BLOCK_SIZE)
- + (($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE)? 1: 0);
- } else {
- $iBBcnt += (floor($raList[$i]->Size / $this->_BIG_BLOCK_SIZE) +
- (($raList[$i]->Size % $this->_BIG_BLOCK_SIZE)? 1: 0));
- }
- }
- }
- $iSmallLen = $iSBcnt * $this->_SMALL_BLOCK_SIZE;
- $iSlCnt = floor($this->_BIG_BLOCK_SIZE / OLE_LONG_INT_SIZE);
- $iSBDcnt = floor($iSBcnt / $iSlCnt) + (($iSBcnt % $iSlCnt)? 1:0);
- $iBBcnt += (floor($iSmallLen / $this->_BIG_BLOCK_SIZE) +
- (( $iSmallLen % $this->_BIG_BLOCK_SIZE)? 1: 0));
- $iCnt = count($raList);
- $iBdCnt = $this->_BIG_BLOCK_SIZE / OLE_PPS_SIZE;
- $iPPScnt = (floor($iCnt/$iBdCnt) + (($iCnt % $iBdCnt)? 1: 0));
- return array($iSBDcnt, $iBBcnt, $iPPScnt);
- }
-
- function _adjust2($i2)
- {
- $iWk = log($i2)/log(2);
- return ($iWk > floor($iWk))? floor($iWk)+1:$iWk;
- }
-
- function _saveHeader($iSBDcnt, $iBBcnt, $iPPScnt)
- {
- $FILE = $this->_FILEH_;
-
- $iBlCnt = $this->_BIG_BLOCK_SIZE / OLE_LONG_INT_SIZE;
- $i1stBdL = ($this->_BIG_BLOCK_SIZE - 0x4C) / OLE_LONG_INT_SIZE;
- $iBdExL = 0;
- $iAll = $iBBcnt + $iPPScnt + $iSBDcnt;
- $iAllW = $iAll;
- $iBdCntW = floor($iAllW / $iBlCnt) + (($iAllW % $iBlCnt)? 1: 0);
- $iBdCnt = floor(($iAll + $iBdCntW) / $iBlCnt) + ((($iAllW+$iBdCntW) % $iBlCnt)? 1: 0);
-
- if ($iBdCnt > $i1stBdL) {
- while (1) {
- $iBdExL++;
- $iAllW++;
- $iBdCntW = floor($iAllW / $iBlCnt) + (($iAllW % $iBlCnt)? 1: 0);
- $iBdCnt = floor(($iAllW + $iBdCntW) / $iBlCnt) + ((($iAllW+$iBdCntW) % $iBlCnt)? 1: 0);
- if ($iBdCnt <= ($iBdExL*$iBlCnt+ $i1stBdL)) {
- break;
- }
- }
- }
-
- fwrite($FILE,
- "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1"
- . "\x00\x00\x00\x00"
- . "\x00\x00\x00\x00"
- . "\x00\x00\x00\x00"
- . "\x00\x00\x00\x00"
- . pack("v", 0x3b)
- . pack("v", 0x03)
- . pack("v", -2)
- . pack("v", 9)
- . pack("v", 6)
- . pack("v", 0)
- . "\x00\x00\x00\x00"
- . "\x00\x00\x00\x00"
- . pack("V", $iBdCnt)
- . pack("V", $iBBcnt+$iSBDcnt)
- . pack("V", 0)
- . pack("V", 0x1000)
- . pack("V", 0)
- . pack("V", 1)
- );
-
- if ($iBdCnt < $i1stBdL) {
- fwrite($FILE,
- pack("V", -2).
- pack("V", 0)
- );
- } else {
- fwrite($FILE, pack("V", $iAll+$iBdCnt) . pack("V", $iBdExL));
- }
-
- for ($i = 0; $i < $i1stBdL && $i < $iBdCnt; $i++) {
- fwrite($FILE, pack("V", $iAll+$i));
- }
- if ($i < $i1stBdL) {
- for ($j = 0; $j < ($i1stBdL-$i); $j++) {
- fwrite($FILE, (pack("V", -1)));
- }
- }
- }
-
- function _saveBigData($iStBlk, &$raList)
- {
- $FILE = $this->_FILEH_;
-
- for ($i = 0; $i < count($raList); $i++) {
- if ($raList[$i]->Type != OLE_PPS_TYPE_DIR) {
- $raList[$i]->Size = $raList[$i]->_DataLen();
- if (($raList[$i]->Size >= OLE_DATA_SIZE_SMALL) ||
- (($raList[$i]->Type == OLE_PPS_TYPE_ROOT) && isset($raList[$i]->_data)))
- {
-
- if (isset($raList[$i]->_PPS_FILE)) {
- $iLen = 0;
- fseek($raList[$i]->_PPS_FILE, 0);
- while($sBuff = fread($raList[$i]->_PPS_FILE, 4096)) {
- $iLen += strlen($sBuff);
- fwrite($FILE, $sBuff);
- }
- } else {
- fwrite($FILE, $raList[$i]->_data);
- }
- if ($raList[$i]->Size % $this->_BIG_BLOCK_SIZE) {
- for ($j = 0; $j < ($this->_BIG_BLOCK_SIZE - ($raList[$i]->Size % $this->_BIG_BLOCK_SIZE)); $j++) {
- fwrite($FILE, "\x00");
- }
- }
-
- $raList[$i]->_StartBlock = $iStBlk;
- $iStBlk +=
- (floor($raList[$i]->Size / $this->_BIG_BLOCK_SIZE) +
- (($raList[$i]->Size % $this->_BIG_BLOCK_SIZE)? 1: 0));
- }
-
- if (isset($raList[$i]->_PPS_FILE)) {
- @fclose($raList[$i]->_PPS_FILE);
- $raList[$i]->_PPS_FILE = null;
- @unlink($raList[$i]->_tmp_filename);
- }
- }
- }
- }
-
- function _makeSmallData(&$raList)
- {
- $sRes = '';
- $FILE = $this->_FILEH_;
- $iSmBlk = 0;
- for ($i = 0; $i < count($raList); $i++) {
-
- if ($raList[$i]->Type == OLE_PPS_TYPE_FILE) {
- if ($raList[$i]->Size <= 0) {
- continue;
- }
- if ($raList[$i]->Size < OLE_DATA_SIZE_SMALL) {
- $iSmbCnt = floor($raList[$i]->Size / $this->_SMALL_BLOCK_SIZE)
- + (($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE)? 1: 0);
-
- for ($j = 0; $j < ($iSmbCnt-1); $j++) {
- fwrite($FILE, pack("V", $j+$iSmBlk+1));
- }
- fwrite($FILE, pack("V", -2));
-
- if ($raList[$i]->_PPS_FILE) {
- fseek($raList[$i]->_PPS_FILE, 0);
- while ($sBuff = fread($raList[$i]->_PPS_FILE, 4096)) {
- $sRes .= $sBuff;
- }
- } else {
- $sRes .= $raList[$i]->_data;
- }
- if ($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE) {
- for ($j = 0; $j < ($this->_SMALL_BLOCK_SIZE - ($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE)); $j++) {
- $sRes .= "\x00";
- }
- }
-
- $raList[$i]->_StartBlock = $iSmBlk;
- $iSmBlk += $iSmbCnt;
- }
- }
- }
- $iSbCnt = floor($this->_BIG_BLOCK_SIZE / OLE_LONG_INT_SIZE);
- if ($iSmBlk % $iSbCnt) {
- for ($i = 0; $i < ($iSbCnt - ($iSmBlk % $iSbCnt)); $i++) {
- fwrite($FILE, pack("V", -1));
- }
- }
- return $sRes;
- }
-
- function _savePps(&$raList)
- {
-
- for ($i = 0; $i < count($raList); $i++) {
- fwrite($this->_FILEH_, $raList[$i]->_getPpsWk());
- }
-
- $iCnt = count($raList);
- $iBCnt = $this->_BIG_BLOCK_SIZE / OLE_PPS_SIZE;
- if ($iCnt % $iBCnt) {
- for ($i = 0; $i < (($iBCnt - ($iCnt % $iBCnt)) * OLE_PPS_SIZE); $i++) {
- fwrite($this->_FILEH_, "\x00");
- }
- }
- }
-
- function _saveBbd($iSbdSize, $iBsize, $iPpsCnt)
- {
- $FILE = $this->_FILEH_;
-
- $iBbCnt = $this->_BIG_BLOCK_SIZE / OLE_LONG_INT_SIZE;
- $i1stBdL = ($this->_BIG_BLOCK_SIZE - 0x4C) / OLE_LONG_INT_SIZE;
- $iBdExL = 0;
- $iAll = $iBsize + $iPpsCnt + $iSbdSize;
- $iAllW = $iAll;
- $iBdCntW = floor($iAllW / $iBbCnt) + (($iAllW % $iBbCnt)? 1: 0);
- $iBdCnt = floor(($iAll + $iBdCntW) / $iBbCnt) + ((($iAllW+$iBdCntW) % $iBbCnt)? 1: 0);
-
- if ($iBdCnt >$i1stBdL) {
- while (1) {
- $iBdExL++;
- $iAllW++;
- $iBdCntW = floor($iAllW / $iBbCnt) + (($iAllW % $iBbCnt)? 1: 0);
- $iBdCnt = floor(($iAllW + $iBdCntW) / $iBbCnt) + ((($iAllW+$iBdCntW) % $iBbCnt)? 1: 0);
- if ($iBdCnt <= ($iBdExL*$iBbCnt+ $i1stBdL)) {
- break;
- }
- }
- }
-
-
- if ($iSbdSize > 0) {
- for ($i = 0; $i < ($iSbdSize - 1); $i++) {
- fwrite($FILE, pack("V", $i+1));
- }
- fwrite($FILE, pack("V", -2));
- }
-
- for ($i = 0; $i < ($iBsize - 1); $i++) {
- fwrite($FILE, pack("V", $i+$iSbdSize+1));
- }
- fwrite($FILE, pack("V", -2));
-
- for ($i = 0; $i < ($iPpsCnt - 1); $i++) {
- fwrite($FILE, pack("V", $i+$iSbdSize+$iBsize+1));
- }
- fwrite($FILE, pack("V", -2));
-
- for ($i = 0; $i < $iBdCnt; $i++) {
- fwrite($FILE, pack("V", 0xFFFFFFFD));
- }
-
- for ($i = 0; $i < $iBdExL; $i++) {
- fwrite($FILE, pack("V", 0xFFFFFFFC));
- }
-
- if (($iAllW + $iBdCnt) % $iBbCnt) {
- for ($i = 0; $i < ($iBbCnt - (($iAllW + $iBdCnt) % $iBbCnt)); $i++) {
- fwrite($FILE, pack("V", -1));
- }
- }
-
- if ($iBdCnt > $i1stBdL) {
- $iN=0;
- $iNb=0;
- for ($i = $i1stBdL;$i < $iBdCnt; $i++, $iN++) {
- if ($iN >= ($iBbCnt - 1)) {
- $iN = 0;
- $iNb++;
- fwrite($FILE, pack("V", $iAll+$iBdCnt+$iNb));
- }
- fwrite($FILE, pack("V", $iBsize+$iSbdSize+$iPpsCnt+$i));
- }
- if (($iBdCnt-$i1stBdL) % ($iBbCnt-1)) {
- for ($i = 0; $i < (($iBbCnt - 1) - (($iBdCnt - $i1stBdL) % ($iBbCnt - 1))); $i++) {
- fwrite($FILE, pack("V", -1));
- }
- }
- fwrite($FILE, pack("V", -2));
- }
- }
- }
- ?>
|