PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/civicrm/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Shared/OLERead.php

https://github.com/nysenate/Bluebird-CRM
PHP | 348 lines | 182 code | 76 blank | 90 comment | 35 complexity | c69a3e0025ed2a76c7f60cce7bf73020 MD5 | raw file
Possible License(s): JSON, BSD-3-Clause, MPL-2.0-no-copyleft-exception, AGPL-1.0, GPL-2.0, AGPL-3.0, Apache-2.0, MIT, GPL-3.0, CC-BY-4.0, LGPL-2.1, BSD-2-Clause, LGPL-3.0
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Shared;
  3. use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
  4. class OLERead
  5. {
  6. private $data = '';
  7. // Size of a sector = 512 bytes
  8. const BIG_BLOCK_SIZE = 0x200;
  9. // Size of a short sector = 64 bytes
  10. const SMALL_BLOCK_SIZE = 0x40;
  11. // Size of a directory entry always = 128 bytes
  12. const PROPERTY_STORAGE_BLOCK_SIZE = 0x80;
  13. // Minimum size of a standard stream = 4096 bytes, streams smaller than this are stored as short streams
  14. const SMALL_BLOCK_THRESHOLD = 0x1000;
  15. // header offsets
  16. const NUM_BIG_BLOCK_DEPOT_BLOCKS_POS = 0x2c;
  17. const ROOT_START_BLOCK_POS = 0x30;
  18. const SMALL_BLOCK_DEPOT_BLOCK_POS = 0x3c;
  19. const EXTENSION_BLOCK_POS = 0x44;
  20. const NUM_EXTENSION_BLOCK_POS = 0x48;
  21. const BIG_BLOCK_DEPOT_BLOCKS_POS = 0x4c;
  22. // property storage offsets (directory offsets)
  23. const SIZE_OF_NAME_POS = 0x40;
  24. const TYPE_POS = 0x42;
  25. const START_BLOCK_POS = 0x74;
  26. const SIZE_POS = 0x78;
  27. public $wrkbook;
  28. public $summaryInformation;
  29. public $documentSummaryInformation;
  30. /**
  31. * @var int
  32. */
  33. private $numBigBlockDepotBlocks;
  34. /**
  35. * @var int
  36. */
  37. private $rootStartBlock;
  38. /**
  39. * @var int
  40. */
  41. private $sbdStartBlock;
  42. /**
  43. * @var int
  44. */
  45. private $extensionBlock;
  46. /**
  47. * @var int
  48. */
  49. private $numExtensionBlocks;
  50. /**
  51. * @var string
  52. */
  53. private $bigBlockChain;
  54. /**
  55. * @var string
  56. */
  57. private $smallBlockChain;
  58. /**
  59. * @var string
  60. */
  61. private $entry;
  62. /**
  63. * @var int
  64. */
  65. private $rootentry;
  66. /**
  67. * @var array
  68. */
  69. private $props = [];
  70. /**
  71. * Read the file.
  72. */
  73. public function read(string $pFilename): void
  74. {
  75. File::assertFile($pFilename);
  76. // Get the file identifier
  77. // Don't bother reading the whole file until we know it's a valid OLE file
  78. $this->data = file_get_contents($pFilename, false, null, 0, 8);
  79. // Check OLE identifier
  80. $identifierOle = pack('CCCCCCCC', 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1);
  81. if ($this->data != $identifierOle) {
  82. throw new ReaderException('The filename ' . $pFilename . ' is not recognised as an OLE file');
  83. }
  84. // Get the file data
  85. $this->data = file_get_contents($pFilename);
  86. // Total number of sectors used for the SAT
  87. $this->numBigBlockDepotBlocks = self::getInt4d($this->data, self::NUM_BIG_BLOCK_DEPOT_BLOCKS_POS);
  88. // SecID of the first sector of the directory stream
  89. $this->rootStartBlock = self::getInt4d($this->data, self::ROOT_START_BLOCK_POS);
  90. // SecID of the first sector of the SSAT (or -2 if not extant)
  91. $this->sbdStartBlock = self::getInt4d($this->data, self::SMALL_BLOCK_DEPOT_BLOCK_POS);
  92. // SecID of the first sector of the MSAT (or -2 if no additional sectors are used)
  93. $this->extensionBlock = self::getInt4d($this->data, self::EXTENSION_BLOCK_POS);
  94. // Total number of sectors used by MSAT
  95. $this->numExtensionBlocks = self::getInt4d($this->data, self::NUM_EXTENSION_BLOCK_POS);
  96. $bigBlockDepotBlocks = [];
  97. $pos = self::BIG_BLOCK_DEPOT_BLOCKS_POS;
  98. $bbdBlocks = $this->numBigBlockDepotBlocks;
  99. if ($this->numExtensionBlocks != 0) {
  100. $bbdBlocks = (self::BIG_BLOCK_SIZE - self::BIG_BLOCK_DEPOT_BLOCKS_POS) / 4;
  101. }
  102. for ($i = 0; $i < $bbdBlocks; ++$i) {
  103. $bigBlockDepotBlocks[$i] = self::getInt4d($this->data, $pos);
  104. $pos += 4;
  105. }
  106. for ($j = 0; $j < $this->numExtensionBlocks; ++$j) {
  107. $pos = ($this->extensionBlock + 1) * self::BIG_BLOCK_SIZE;
  108. $blocksToRead = min($this->numBigBlockDepotBlocks - $bbdBlocks, self::BIG_BLOCK_SIZE / 4 - 1);
  109. for ($i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; ++$i) {
  110. $bigBlockDepotBlocks[$i] = self::getInt4d($this->data, $pos);
  111. $pos += 4;
  112. }
  113. $bbdBlocks += $blocksToRead;
  114. if ($bbdBlocks < $this->numBigBlockDepotBlocks) {
  115. $this->extensionBlock = self::getInt4d($this->data, $pos);
  116. }
  117. }
  118. $pos = 0;
  119. $this->bigBlockChain = '';
  120. $bbs = self::BIG_BLOCK_SIZE / 4;
  121. for ($i = 0; $i < $this->numBigBlockDepotBlocks; ++$i) {
  122. $pos = ($bigBlockDepotBlocks[$i] + 1) * self::BIG_BLOCK_SIZE;
  123. $this->bigBlockChain .= substr($this->data, $pos, 4 * $bbs);
  124. $pos += 4 * $bbs;
  125. }
  126. $pos = 0;
  127. $sbdBlock = $this->sbdStartBlock;
  128. $this->smallBlockChain = '';
  129. while ($sbdBlock != -2) {
  130. $pos = ($sbdBlock + 1) * self::BIG_BLOCK_SIZE;
  131. $this->smallBlockChain .= substr($this->data, $pos, 4 * $bbs);
  132. $pos += 4 * $bbs;
  133. $sbdBlock = self::getInt4d($this->bigBlockChain, $sbdBlock * 4);
  134. }
  135. // read the directory stream
  136. $block = $this->rootStartBlock;
  137. $this->entry = $this->readData($block);
  138. $this->readPropertySets();
  139. }
  140. /**
  141. * Extract binary stream data.
  142. *
  143. * @param int $stream
  144. *
  145. * @return null|string
  146. */
  147. public function getStream($stream)
  148. {
  149. if ($stream === null) {
  150. return null;
  151. }
  152. $streamData = '';
  153. if ($this->props[$stream]['size'] < self::SMALL_BLOCK_THRESHOLD) {
  154. $rootdata = $this->readData($this->props[$this->rootentry]['startBlock']);
  155. $block = $this->props[$stream]['startBlock'];
  156. while ($block != -2) {
  157. $pos = $block * self::SMALL_BLOCK_SIZE;
  158. $streamData .= substr($rootdata, $pos, self::SMALL_BLOCK_SIZE);
  159. $block = self::getInt4d($this->smallBlockChain, $block * 4);
  160. }
  161. return $streamData;
  162. }
  163. $numBlocks = $this->props[$stream]['size'] / self::BIG_BLOCK_SIZE;
  164. if ($this->props[$stream]['size'] % self::BIG_BLOCK_SIZE != 0) {
  165. ++$numBlocks;
  166. }
  167. if ($numBlocks == 0) {
  168. return '';
  169. }
  170. $block = $this->props[$stream]['startBlock'];
  171. while ($block != -2) {
  172. $pos = ($block + 1) * self::BIG_BLOCK_SIZE;
  173. $streamData .= substr($this->data, $pos, self::BIG_BLOCK_SIZE);
  174. $block = self::getInt4d($this->bigBlockChain, $block * 4);
  175. }
  176. return $streamData;
  177. }
  178. /**
  179. * Read a standard stream (by joining sectors using information from SAT).
  180. *
  181. * @param int $bl Sector ID where the stream starts
  182. *
  183. * @return string Data for standard stream
  184. */
  185. private function readData($bl)
  186. {
  187. $block = $bl;
  188. $data = '';
  189. while ($block != -2) {
  190. $pos = ($block + 1) * self::BIG_BLOCK_SIZE;
  191. $data .= substr($this->data, $pos, self::BIG_BLOCK_SIZE);
  192. $block = self::getInt4d($this->bigBlockChain, $block * 4);
  193. }
  194. return $data;
  195. }
  196. /**
  197. * Read entries in the directory stream.
  198. */
  199. private function readPropertySets(): void
  200. {
  201. $offset = 0;
  202. // loop through entires, each entry is 128 bytes
  203. $entryLen = strlen($this->entry);
  204. while ($offset < $entryLen) {
  205. // entry data (128 bytes)
  206. $d = substr($this->entry, $offset, self::PROPERTY_STORAGE_BLOCK_SIZE);
  207. // size in bytes of name
  208. $nameSize = ord($d[self::SIZE_OF_NAME_POS]) | (ord($d[self::SIZE_OF_NAME_POS + 1]) << 8);
  209. // type of entry
  210. $type = ord($d[self::TYPE_POS]);
  211. // sectorID of first sector or short sector, if this entry refers to a stream (the case with workbook)
  212. // sectorID of first sector of the short-stream container stream, if this entry is root entry
  213. $startBlock = self::getInt4d($d, self::START_BLOCK_POS);
  214. $size = self::getInt4d($d, self::SIZE_POS);
  215. $name = str_replace("\x00", '', substr($d, 0, $nameSize));
  216. $this->props[] = [
  217. 'name' => $name,
  218. 'type' => $type,
  219. 'startBlock' => $startBlock,
  220. 'size' => $size,
  221. ];
  222. // tmp helper to simplify checks
  223. $upName = strtoupper($name);
  224. // Workbook directory entry (BIFF5 uses Book, BIFF8 uses Workbook)
  225. if (($upName === 'WORKBOOK') || ($upName === 'BOOK')) {
  226. $this->wrkbook = count($this->props) - 1;
  227. } elseif ($upName === 'ROOT ENTRY' || $upName === 'R') {
  228. // Root entry
  229. $this->rootentry = count($this->props) - 1;
  230. }
  231. // Summary information
  232. if ($name == chr(5) . 'SummaryInformation') {
  233. $this->summaryInformation = count($this->props) - 1;
  234. }
  235. // Additional Document Summary information
  236. if ($name == chr(5) . 'DocumentSummaryInformation') {
  237. $this->documentSummaryInformation = count($this->props) - 1;
  238. }
  239. $offset += self::PROPERTY_STORAGE_BLOCK_SIZE;
  240. }
  241. }
  242. /**
  243. * Read 4 bytes of data at specified position.
  244. *
  245. * @param string $data
  246. * @param int $pos
  247. *
  248. * @return int
  249. */
  250. private static function getInt4d($data, $pos)
  251. {
  252. if ($pos < 0) {
  253. // Invalid position
  254. throw new ReaderException('Parameter pos=' . $pos . ' is invalid.');
  255. }
  256. $len = strlen($data);
  257. if ($len < $pos + 4) {
  258. $data .= str_repeat("\0", $pos + 4 - $len);
  259. }
  260. // FIX: represent numbers correctly on 64-bit system
  261. // http://sourceforge.net/tracker/index.php?func=detail&aid=1487372&group_id=99160&atid=623334
  262. // Changed by Andreas Rehm 2006 to ensure correct result of the <<24 block on 32 and 64bit systems
  263. $_or_24 = ord($data[$pos + 3]);
  264. if ($_or_24 >= 128) {
  265. // negative number
  266. $_ord_24 = -abs((256 - $_or_24) << 24);
  267. } else {
  268. $_ord_24 = ($_or_24 & 127) << 24;
  269. }
  270. return ord($data[$pos]) | (ord($data[$pos + 1]) << 8) | (ord($data[$pos + 2]) << 16) | $_ord_24;
  271. }
  272. }