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