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