Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
100 / 100 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
OLERead | |
100.00% |
98 / 98 |
|
100.00% |
5 / 5 |
31 | |
100.00% |
1 / 1 |
read | |
100.00% |
35 / 35 |
|
100.00% |
1 / 1 |
10 | |||
getStream | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
7 | |||
readData | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
readPropertySets | |
100.00% |
30 / 30 |
|
100.00% |
1 / 1 |
10 | |||
getInt4d | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * This file is part of PHPWord - A pure PHP library for reading and writing |
4 | * word processing documents. |
5 | * |
6 | * PHPWord is free software distributed under the terms of the GNU Lesser |
7 | * General Public License version 3 as published by the Free Software Foundation. |
8 | * |
9 | * For the full copyright and license information, please read the LICENSE |
10 | * file that was distributed with this source code. For the full list of |
11 | * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
12 | * |
13 | * @see https://github.com/PHPOffice/PHPWord |
14 | * @copyright 2010-2018 PHPWord contributors |
15 | * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
16 | */ |
17 | |
18 | namespace PhpOffice\PhpWord\Shared; |
19 | |
20 | use PhpOffice\PhpWord\Exception\Exception; |
21 | |
22 | defined('IDENTIFIER_OLE') || |
23 | define('IDENTIFIER_OLE', pack('CCCCCCCC', 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1)); |
24 | |
25 | class OLERead |
26 | { |
27 | private $data = ''; |
28 | |
29 | // OLE identifier |
30 | const IDENTIFIER_OLE = IDENTIFIER_OLE; |
31 | |
32 | // Size of a sector = 512 bytes |
33 | const BIG_BLOCK_SIZE = 0x200; |
34 | |
35 | // Size of a short sector = 64 bytes |
36 | const SMALL_BLOCK_SIZE = 0x40; |
37 | |
38 | // Size of a directory entry always = 128 bytes |
39 | const PROPERTY_STORAGE_BLOCK_SIZE = 0x80; |
40 | |
41 | // Minimum size of a standard stream = 4096 bytes, streams smaller than this are stored as short streams |
42 | const SMALL_BLOCK_THRESHOLD = 0x1000; |
43 | |
44 | // header offsets |
45 | const NUM_BIG_BLOCK_DEPOT_BLOCKS_POS = 0x2c; |
46 | const ROOT_START_BLOCK_POS = 0x30; |
47 | const SMALL_BLOCK_DEPOT_BLOCK_POS = 0x3c; |
48 | const EXTENSION_BLOCK_POS = 0x44; |
49 | const NUM_EXTENSION_BLOCK_POS = 0x48; |
50 | const BIG_BLOCK_DEPOT_BLOCKS_POS = 0x4c; |
51 | |
52 | // property storage offsets (directory offsets) |
53 | const SIZE_OF_NAME_POS = 0x40; |
54 | const TYPE_POS = 0x42; |
55 | const START_BLOCK_POS = 0x74; |
56 | const SIZE_POS = 0x78; |
57 | |
58 | public $wrkdocument = null; |
59 | public $wrk1Table = null; |
60 | public $wrkData = null; |
61 | public $wrkObjectPool = null; |
62 | public $summaryInformation = null; |
63 | public $docSummaryInfos = null; |
64 | public $numBigBlockDepotBlocks = null; |
65 | public $rootStartBlock = null; |
66 | public $sbdStartBlock = null; |
67 | public $extensionBlock = null; |
68 | public $numExtensionBlocks = null; |
69 | public $bigBlockChain = null; |
70 | public $smallBlockChain = null; |
71 | public $entry = null; |
72 | public $rootentry = null; |
73 | public $wrkObjectPoolelseif = null; |
74 | public $props = array(); |
75 | |
76 | /** |
77 | * Read the file |
78 | * |
79 | * @param $sFileName string Filename |
80 | * |
81 | * @throws Exception |
82 | */ |
83 | public function read($sFileName) |
84 | { |
85 | // Check if file exists and is readable |
86 | if (!is_readable($sFileName)) { |
87 | throw new Exception('Could not open ' . $sFileName . ' for reading! File does not exist, or it is not readable.'); |
88 | } |
89 | |
90 | // Get the file identifier |
91 | // Don't bother reading the whole file until we know it's a valid OLE file |
92 | $this->data = file_get_contents($sFileName, false, null, 0, 8); |
93 | |
94 | // Check OLE identifier |
95 | if ($this->data != self::IDENTIFIER_OLE) { |
96 | throw new Exception('The filename ' . $sFileName . ' is not recognised as an OLE file'); |
97 | } |
98 | |
99 | // Get the file data |
100 | $this->data = file_get_contents($sFileName); |
101 | |
102 | // Total number of sectors used for the SAT |
103 | $this->numBigBlockDepotBlocks = self::getInt4d($this->data, self::NUM_BIG_BLOCK_DEPOT_BLOCKS_POS); |
104 | |
105 | // SecID of the first sector of the directory stream |
106 | $this->rootStartBlock = self::getInt4d($this->data, self::ROOT_START_BLOCK_POS); |
107 | |
108 | // SecID of the first sector of the SSAT (or -2 if not extant) |
109 | $this->sbdStartBlock = self::getInt4d($this->data, self::SMALL_BLOCK_DEPOT_BLOCK_POS); |
110 | |
111 | // SecID of the first sector of the MSAT (or -2 if no additional sectors are used) |
112 | $this->extensionBlock = self::getInt4d($this->data, self::EXTENSION_BLOCK_POS); |
113 | |
114 | // Total number of sectors used by MSAT |
115 | $this->numExtensionBlocks = self::getInt4d($this->data, self::NUM_EXTENSION_BLOCK_POS); |
116 | |
117 | $bigBlockDepotBlocks = array(); |
118 | $pos = self::BIG_BLOCK_DEPOT_BLOCKS_POS; |
119 | |
120 | $bbdBlocks = $this->numBigBlockDepotBlocks; |
121 | |
122 | // @codeCoverageIgnoreStart |
123 | if ($this->numExtensionBlocks != 0) { |
124 | $bbdBlocks = (self::BIG_BLOCK_SIZE - self::BIG_BLOCK_DEPOT_BLOCKS_POS) / 4; |
125 | } |
126 | // @codeCoverageIgnoreEnd |
127 | |
128 | for ($i = 0; $i < $bbdBlocks; ++$i) { |
129 | $bigBlockDepotBlocks[$i] = self::getInt4d($this->data, $pos); |
130 | $pos += 4; |
131 | } |
132 | |
133 | // @codeCoverageIgnoreStart |
134 | for ($j = 0; $j < $this->numExtensionBlocks; ++$j) { |
135 | $pos = ($this->extensionBlock + 1) * self::BIG_BLOCK_SIZE; |
136 | $blocksToRead = min($this->numBigBlockDepotBlocks - $bbdBlocks, self::BIG_BLOCK_SIZE / 4 - 1); |
137 | |
138 | for ($i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; ++$i) { |
139 | $bigBlockDepotBlocks[$i] = self::getInt4d($this->data, $pos); |
140 | $pos += 4; |
141 | } |
142 | |
143 | $bbdBlocks += $blocksToRead; |
144 | if ($bbdBlocks < $this->numBigBlockDepotBlocks) { |
145 | $this->extensionBlock = self::getInt4d($this->data, $pos); |
146 | } |
147 | } |
148 | // @codeCoverageIgnoreEnd |
149 | |
150 | $pos = 0; |
151 | $this->bigBlockChain = ''; |
152 | $bbs = self::BIG_BLOCK_SIZE / 4; |
153 | for ($i = 0; $i < $this->numBigBlockDepotBlocks; ++$i) { |
154 | $pos = ($bigBlockDepotBlocks[$i] + 1) * self::BIG_BLOCK_SIZE; |
155 | |
156 | $this->bigBlockChain .= substr($this->data, $pos, 4 * $bbs); |
157 | $pos += 4 * $bbs; |
158 | } |
159 | |
160 | $pos = 0; |
161 | $sbdBlock = $this->sbdStartBlock; |
162 | $this->smallBlockChain = ''; |
163 | while ($sbdBlock != -2) { |
164 | $pos = ($sbdBlock + 1) * self::BIG_BLOCK_SIZE; |
165 | |
166 | $this->smallBlockChain .= substr($this->data, $pos, 4 * $bbs); |
167 | $pos += 4 * $bbs; |
168 | |
169 | $sbdBlock = self::getInt4d($this->bigBlockChain, $sbdBlock * 4); |
170 | } |
171 | |
172 | // read the directory stream |
173 | $block = $this->rootStartBlock; |
174 | $this->entry = $this->readData($block); |
175 | |
176 | $this->readPropertySets(); |
177 | } |
178 | |
179 | /** |
180 | * Extract binary stream data |
181 | * |
182 | * @param mixed $stream |
183 | * @return string |
184 | */ |
185 | public function getStream($stream) |
186 | { |
187 | if ($stream === null) { |
188 | return null; |
189 | } |
190 | |
191 | $streamData = ''; |
192 | |
193 | if ($this->props[$stream]['size'] < self::SMALL_BLOCK_THRESHOLD) { |
194 | $rootdata = $this->readData($this->props[$this->rootentry]['startBlock']); |
195 | |
196 | $block = $this->props[$stream]['startBlock']; |
197 | |
198 | while ($block != -2) { |
199 | $pos = $block * self::SMALL_BLOCK_SIZE; |
200 | $streamData .= substr($rootdata, $pos, self::SMALL_BLOCK_SIZE); |
201 | |
202 | $block = self::getInt4d($this->smallBlockChain, $block * 4); |
203 | } |
204 | |
205 | return $streamData; |
206 | } |
207 | |
208 | $numBlocks = $this->props[$stream]['size'] / self::BIG_BLOCK_SIZE; |
209 | if ($this->props[$stream]['size'] % self::BIG_BLOCK_SIZE != 0) { |
210 | ++$numBlocks; |
211 | } |
212 | |
213 | if ($numBlocks == 0) { |
214 | return ''; // @codeCoverageIgnore |
215 | } |
216 | |
217 | $block = $this->props[$stream]['startBlock']; |
218 | |
219 | while ($block != -2) { |
220 | $pos = ($block + 1) * self::BIG_BLOCK_SIZE; |
221 | $streamData .= substr($this->data, $pos, self::BIG_BLOCK_SIZE); |
222 | $block = self::getInt4d($this->bigBlockChain, $block * 4); |
223 | } |
224 | |
225 | return $streamData; |
226 | } |
227 | |
228 | /** |
229 | * Read a standard stream (by joining sectors using information from SAT) |
230 | * |
231 | * @param int $blSectorId Sector ID where the stream starts |
232 | * @return string Data for standard stream |
233 | */ |
234 | private function readData($blSectorId) |
235 | { |
236 | $block = $blSectorId; |
237 | $data = ''; |
238 | |
239 | while ($block != -2) { |
240 | $pos = ($block + 1) * self::BIG_BLOCK_SIZE; |
241 | $data .= substr($this->data, $pos, self::BIG_BLOCK_SIZE); |
242 | $block = self::getInt4d($this->bigBlockChain, $block * 4); |
243 | } |
244 | |
245 | return $data; |
246 | } |
247 | |
248 | /** |
249 | * Read entries in the directory stream. |
250 | */ |
251 | private function readPropertySets() |
252 | { |
253 | $offset = 0; |
254 | |
255 | // loop through entires, each entry is 128 bytes |
256 | $entryLen = strlen($this->entry); |
257 | while ($offset < $entryLen) { |
258 | // entry data (128 bytes) |
259 | $data = substr($this->entry, $offset, self::PROPERTY_STORAGE_BLOCK_SIZE); |
260 | |
261 | // size in bytes of name |
262 | $nameSize = ord($data[self::SIZE_OF_NAME_POS]) | (ord($data[self::SIZE_OF_NAME_POS + 1]) << 8); |
263 | |
264 | // type of entry |
265 | $type = ord($data[self::TYPE_POS]); |
266 | |
267 | // sectorID of first sector or short sector, if this entry refers to a stream (the case with workbook) |
268 | // sectorID of first sector of the short-stream container stream, if this entry is root entry |
269 | $startBlock = self::getInt4d($data, self::START_BLOCK_POS); |
270 | |
271 | $size = self::getInt4d($data, self::SIZE_POS); |
272 | |
273 | $name = str_replace("\x00", '', substr($data, 0, $nameSize)); |
274 | |
275 | $this->props[] = array( |
276 | 'name' => $name, |
277 | 'type' => $type, |
278 | 'startBlock' => $startBlock, |
279 | 'size' => $size, ); |
280 | |
281 | // tmp helper to simplify checks |
282 | $upName = strtoupper($name); |
283 | |
284 | // Workbook directory entry (BIFF5 uses Book, BIFF8 uses Workbook) |
285 | // print_r($upName.PHP_EOL); |
286 | if (($upName === 'WORDDOCUMENT')) { |
287 | $this->wrkdocument = count($this->props) - 1; |
288 | } elseif ($upName === '1TABLE') { |
289 | $this->wrk1Table = count($this->props) - 1; |
290 | } elseif ($upName === 'DATA') { |
291 | $this->wrkData = count($this->props) - 1; |
292 | } elseif ($upName === 'OBJECTPOOL') { |
293 | $this->wrkObjectPoolelseif = count($this->props) - 1; |
294 | } elseif ($upName === 'ROOT ENTRY' || $upName === 'R') { |
295 | $this->rootentry = count($this->props) - 1; |
296 | } |
297 | |
298 | // Summary information |
299 | if ($name == chr(5) . 'SummaryInformation') { |
300 | $this->summaryInformation = count($this->props) - 1; |
301 | } |
302 | |
303 | // Additional Document Summary information |
304 | if ($name == chr(5) . 'DocumentSummaryInformation') { |
305 | $this->docSummaryInfos = count($this->props) - 1; |
306 | } |
307 | |
308 | $offset += self::PROPERTY_STORAGE_BLOCK_SIZE; |
309 | } |
310 | } |
311 | |
312 | /** |
313 | * Read 4 bytes of data at specified position |
314 | * |
315 | * @param string $data |
316 | * @param int $pos |
317 | * @return int |
318 | */ |
319 | private static function getInt4d($data, $pos) |
320 | { |
321 | // FIX: represent numbers correctly on 64-bit system |
322 | // http://sourceforge.net/tracker/index.php?func=detail&aid=1487372&group_id=99160&atid=623334 |
323 | // Hacked by Andreas Rehm 2006 to ensure correct result of the <<24 block on 32 and 64bit systems |
324 | $or24 = ord($data[$pos + 3]); |
325 | if ($or24 >= 128) { |
326 | // negative number |
327 | $ord24 = -abs((256 - $or24) << 24); |
328 | } else { |
329 | $ord24 = ($or24 & 127) << 24; |
330 | } |
331 | |
332 | return ord($data[$pos]) | (ord($data[$pos + 1]) << 8) | (ord($data[$pos + 2]) << 16) | $ord24; |
333 | } |
334 | } |