Passed
Push — master ( fcb5ef...e65bc8 )
by
unknown
13:57 queued 18s
created

OLE::readInt1()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 6
rs 10
ccs 3
cts 3
cp 1
cc 3
nc 1
nop 1
crap 3
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared;
4
5
// vim: set expandtab tabstop=4 shiftwidth=4:
6
// +----------------------------------------------------------------------+
7
// | PHP Version 4                                                        |
8
// +----------------------------------------------------------------------+
9
// | Copyright (c) 1997-2002 The PHP Group                                |
10
// +----------------------------------------------------------------------+
11
// | This source file is subject to version 2.02 of the PHP license,      |
12
// | that is bundled with this package in the file LICENSE, and is        |
13
// | available at through the world-wide-web at                           |
14
// | http://www.php.net/license/2_02.txt.                                 |
15
// | If you did not receive a copy of the PHP license and are unable to   |
16
// | obtain it through the world-wide-web, please send a note to          |
17
// | [email protected] so we can mail you a copy immediately.               |
18
// +----------------------------------------------------------------------+
19
// | Author: Xavier Noguer <[email protected]>                              |
20
// | Based on OLE::Storage_Lite by Kawai, Takanori                        |
21
// +----------------------------------------------------------------------+
22
//
23
24
use PhpOffice\PhpSpreadsheet\Exception;
25
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
26
use PhpOffice\PhpSpreadsheet\Shared\OLE\ChainedBlockStream;
27
use PhpOffice\PhpSpreadsheet\Shared\OLE\PPS\Root;
28
29
/*
30
 * Array for storing OLE instances that are accessed from
31
 * OLE_ChainedBlockStream::stream_open().
32
 *
33
 * @var array
34
 */
35 72
$GLOBALS['_OLE_INSTANCES'] = [];
36
37
/**
38
 * OLE package base class.
39
 *
40
 * @author   Xavier Noguer <[email protected]>
41
 * @author   Christian Schmidt <[email protected]>
42
 */
43
class OLE
44
{
45
    const OLE_PPS_TYPE_ROOT = 5;
46
    const OLE_PPS_TYPE_DIR = 1;
47
    const OLE_PPS_TYPE_FILE = 2;
48
    const OLE_DATA_SIZE_SMALL = 0x1000;
49
    const OLE_LONG_INT_SIZE = 4;
50
    const OLE_PPS_SIZE = 0x80;
51
52
    /**
53
     * The file handle for reading an OLE container.
54
     *
55
     * @var resource
56
     */
57
    public $_file_handle;
58
59
    /**
60
     * Array of PPS's found on the OLE container.
61
     *
62
     * @var array<OLE\PPS|OLE\PPS\File|Root>
63
     */
64
    public array $_list = [];
65
66
    /**
67
     * Root directory of OLE container.
68
     */
69
    public Root $root;
70
71
    /**
72
     * Big Block Allocation Table.
73
     *
74
     * @var mixed[] (blockId => nextBlockId)
75
     */
76
    public array $bbat;
77
78
    /**
79
     * Short Block Allocation Table.
80
     *
81
     * @var mixed[] (blockId => nextBlockId)
82
     */
83
    public array $sbat;
84
85
    /**
86
     * Size of big blocks. This is usually 512.
87
     *
88
     * @var int<1, max> number of octets per block
89
     */
90
    public int $bigBlockSize;
91
92
    /**
93
     * Size of small blocks. This is usually 64.
94
     *
95
     * @var int number of octets per block
96
     */
97
    public int $smallBlockSize;
98
99
    /**
100
     * Threshold for big blocks.
101
     */
102
    public int $bigBlockThreshold;
103
104
    /**
105
     * Reads an OLE container from the contents of the file given.
106
     *
107
     * @acces public
108
     *
109
     * @return bool true on success, PEAR_Error on failure
110
     */
111 3
    public function read(string $filename): bool
112
    {
113 3
        $fh = @fopen($filename, 'rb');
114 3
        if ($fh === false) {
115 1
            throw new ReaderException("Can't open file $filename");
116
        }
117 2
        $this->_file_handle = $fh;
118
119 2
        $signature = fread($fh, 8);
120 2
        if ("\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" != $signature) {
121 1
            throw new ReaderException("File doesn't seem to be an OLE container.");
122
        }
123 1
        fseek($fh, 28);
124 1
        if (fread($fh, 2) != "\xFE\xFF") {
125
            // This shouldn't be a problem in practice
126
            throw new ReaderException('Only Little-Endian encoding is supported.');
127
        }
128
        // Size of blocks and short blocks in bytes
129
        /** @var int<1, max> */
130 1
        $temp = 2 ** self::readInt2($fh);
131 1
        $this->bigBlockSize = $temp;
132 1
        $this->smallBlockSize = 2 ** self::readInt2($fh);
133
134
        // Skip UID, revision number and version number
135 1
        fseek($fh, 44);
136
        // Number of blocks in Big Block Allocation Table
137 1
        $bbatBlockCount = self::readInt4($fh);
138
139
        // Root chain 1st block
140 1
        $directoryFirstBlockId = self::readInt4($fh);
141
142
        // Skip unused bytes
143 1
        fseek($fh, 56);
144
        // Streams shorter than this are stored using small blocks
145 1
        $this->bigBlockThreshold = self::readInt4($fh);
146
        // Block id of first sector in Short Block Allocation Table
147 1
        $sbatFirstBlockId = self::readInt4($fh);
148
        // Number of blocks in Short Block Allocation Table
149 1
        $sbbatBlockCount = self::readInt4($fh);
150
        // Block id of first sector in Master Block Allocation Table
151 1
        $mbatFirstBlockId = self::readInt4($fh);
152
        // Number of blocks in Master Block Allocation Table
153 1
        $mbbatBlockCount = self::readInt4($fh);
154 1
        $this->bbat = [];
155
156
        // Remaining 4 * 109 bytes of current block is beginning of Master
157
        // Block Allocation Table
158 1
        $mbatBlocks = [];
159 1
        for ($i = 0; $i < 109; ++$i) {
160 1
            $mbatBlocks[] = self::readInt4($fh);
161
        }
162
163
        // Read rest of Master Block Allocation Table (if any is left)
164 1
        $pos = $this->getBlockOffset($mbatFirstBlockId);
165 1
        for ($i = 0; $i < $mbbatBlockCount; ++$i) {
166
            fseek($fh, $pos);
167
            for ($j = 0; $j < $this->bigBlockSize / 4 - 1; ++$j) {
168
                $mbatBlocks[] = self::readInt4($fh);
169
            }
170
            // Last block id in each block points to next block
171
            $pos = $this->getBlockOffset(self::readInt4($fh));
172
        }
173
174
        // Read Big Block Allocation Table according to chain specified by $mbatBlocks
175 1
        for ($i = 0; $i < $bbatBlockCount; ++$i) {
176 1
            $pos = $this->getBlockOffset($mbatBlocks[$i]);
177 1
            fseek($fh, $pos);
178 1
            for ($j = 0; $j < $this->bigBlockSize / 4; ++$j) {
179 1
                $this->bbat[] = self::readInt4($fh);
180
            }
181
        }
182
183
        // Read short block allocation table (SBAT)
184 1
        $this->sbat = [];
185 1
        $shortBlockCount = $sbbatBlockCount * $this->bigBlockSize / 4;
186 1
        $sbatFh = $this->getStream($sbatFirstBlockId);
187 1
        for ($blockId = 0; $blockId < $shortBlockCount; ++$blockId) {
188 1
            $this->sbat[$blockId] = self::readInt4($sbatFh);
189
        }
190 1
        fclose($sbatFh);
191
192 1
        $this->readPpsWks($directoryFirstBlockId);
193
194 1
        return true;
195
    }
196
197
    /**
198
     * @param int $blockId byte offset from beginning of file
199
     */
200 1
    public function getBlockOffset(int $blockId): int
201
    {
202 1
        return 512 + $blockId * $this->bigBlockSize;
203
    }
204
205
    /**
206
     * Returns a stream for use with fread() etc. External callers should
207
     * use \PhpOffice\PhpSpreadsheet\Shared\OLE\PPS\File::getStream().
208
     *
209
     * @param int|OLE\PPS $blockIdOrPps block id or PPS
210
     *
211
     * @return resource read-only stream
212
     */
213 1
    public function getStream($blockIdOrPps)
214
    {
215 1
        static $isRegistered = false;
216 1
        if (!$isRegistered) {
217 1
            stream_wrapper_register('ole-chainedblockstream', ChainedBlockStream::class);
218 1
            $isRegistered = true;
219
        }
220
221
        // Store current instance in global array, so that it can be accessed
222
        // in OLE_ChainedBlockStream::stream_open().
223
        // Object is removed from self::$instances in OLE_Stream::close().
224 1
        $GLOBALS['_OLE_INSTANCES'][] = $this; //* @phpstan-ignore-line
225 1
        $keys = array_keys($GLOBALS['_OLE_INSTANCES']); //* @phpstan-ignore-line
226 1
        $instanceId = end($keys);
227
228 1
        $path = 'ole-chainedblockstream://oleInstanceId=' . $instanceId;
229 1
        if ($blockIdOrPps instanceof OLE\PPS) {
230
            $path .= '&blockId=' . $blockIdOrPps->startBlock;
231
            $path .= '&size=' . $blockIdOrPps->Size;
232
        } else {
233 1
            $path .= '&blockId=' . $blockIdOrPps;
234
        }
235
236 1
        $resource = fopen($path, 'rb');
237 1
        if ($resource === false) {
238
            throw new Exception("Unable to open stream $path");
239
        }
240
241 1
        return $resource;
242
    }
243
244
    /**
245
     * Reads a signed char.
246
     *
247
     * @param resource $fileHandle file handle
248
     */
249 1
    private static function readInt1($fileHandle): int
250
    {
251 1
        [, $tmp] = unpack('c', fread($fileHandle, 1) ?: '') ?: [0, 0];
252
        /** @var int $tmp */
253
254 1
        return $tmp;
255
    }
256
257
    /**
258
     * Reads an unsigned short (2 octets).
259
     *
260
     * @param resource $fileHandle file handle
261
     */
262 1
    private static function readInt2($fileHandle): int
263
    {
264 1
        [, $tmp] = unpack('v', fread($fileHandle, 2) ?: '') ?: [0, 0];
265
        /** @var int $tmp */
266
267 1
        return $tmp;
268
    }
269
270
    private const SIGNED_4OCTET_LIMIT = 2147483648;
271
272
    private const SIGNED_4OCTET_SUBTRACT = 2 * self::SIGNED_4OCTET_LIMIT;
273
274
    /**
275
     * Reads long (4 octets), interpreted as if signed on 32-bit system.
276
     *
277
     * @param resource $fileHandle file handle
278
     */
279 1
    private static function readInt4($fileHandle): int
280
    {
281 1
        [, $tmp] = unpack('V', fread($fileHandle, 4) ?: '') ?: [0, 0];
282
        /** @var int $tmp */
283 1
        if ($tmp >= self::SIGNED_4OCTET_LIMIT) {
284 1
            $tmp -= self::SIGNED_4OCTET_SUBTRACT;
285
        }
286
287 1
        return $tmp;
288
    }
289
290
    /**
291
     * Gets information about all PPS's on the OLE container from the PPS WK's
292
     * creates an OLE_PPS object for each one.
293
     *
294
     * @param int $blockId the block id of the first block
295
     *
296
     * @return bool true on success, PEAR_Error on failure
297
     */
298 1
    public function readPpsWks(int $blockId): bool
299
    {
300 1
        $fh = $this->getStream($blockId);
301 1
        for ($pos = 0; true; $pos += 128) {
302 1
            fseek($fh, $pos, SEEK_SET);
303 1
            $nameUtf16 = (string) fread($fh, 64);
304 1
            $nameLength = self::readInt2($fh);
305 1
            $nameUtf16 = substr($nameUtf16, 0, $nameLength - 2);
306
            // Simple conversion from UTF-16LE to ISO-8859-1
307 1
            $name = str_replace("\x00", '', $nameUtf16);
308 1
            $type = self::readInt1($fh);
309
            switch ($type) {
310
                case self::OLE_PPS_TYPE_ROOT:
311 1
                    $pps = new Root(null, null, []);
312 1
                    $this->root = $pps;
313
314 1
                    break;
315
                case self::OLE_PPS_TYPE_DIR:
316
                    $pps = new OLE\PPS(null, null, null, null, null, null, null, null, null, []);
317
318
                    break;
319
                case self::OLE_PPS_TYPE_FILE:
320 1
                    $pps = new OLE\PPS\File($name);
321
322 1
                    break;
323
                default:
324
                    throw new Exception('Unsupported PPS type');
325
            }
326 1
            fseek($fh, 1, SEEK_CUR);
327 1
            $pps->Type = $type;
328 1
            $pps->Name = $name;
329 1
            $pps->PrevPps = self::readInt4($fh);
330 1
            $pps->NextPps = self::readInt4($fh);
331 1
            $pps->DirPps = self::readInt4($fh);
332 1
            fseek($fh, 20, SEEK_CUR);
333 1
            $pps->Time1st = self::OLE2LocalDate((string) fread($fh, 8));
334 1
            $pps->Time2nd = self::OLE2LocalDate((string) fread($fh, 8));
335 1
            $pps->startBlock = self::readInt4($fh);
336 1
            $pps->Size = self::readInt4($fh);
337 1
            $pps->No = count($this->_list);
338 1
            $this->_list[] = $pps;
339
340
            // check if the PPS tree (starting from root) is complete
341 1
            if (isset($this->root) && $this->ppsTreeComplete($this->root->No)) {
342 1
                break;
343
            }
344
        }
345 1
        fclose($fh);
346
347
        // Initialize $pps->children on directories
348 1
        foreach ($this->_list as $pps) {
349 1
            if ($pps->Type == self::OLE_PPS_TYPE_DIR || $pps->Type == self::OLE_PPS_TYPE_ROOT) {
350 1
                $nos = [$pps->DirPps];
351 1
                $pps->children = [];
352 1
                while (!empty($nos)) {
353 1
                    $no = array_pop($nos);
354 1
                    if ($no != -1) {
355 1
                        $childPps = $this->_list[$no];
356 1
                        $nos[] = $childPps->PrevPps;
357 1
                        $nos[] = $childPps->NextPps;
358 1
                        $pps->children[] = $childPps;
359
                    }
360
                }
361
            }
362
        }
363
364 1
        return true;
365
    }
366
367
    /**
368
     * It checks whether the PPS tree is complete (all PPS's read)
369
     * starting with the given PPS (not necessarily root).
370
     *
371
     * @param int $index The index of the PPS from which we are checking
372
     *
373
     * @return bool Whether the PPS tree for the given PPS is complete
374
     */
375 1
    private function ppsTreeComplete(int $index): bool
376
    {
377 1
        if (!isset($this->_list[$index])) {
378 1
            return false;
379
        }
380 1
        $pps = $this->_list[$index];
381
382 1
        return
383 1
            ($pps->PrevPps == -1
384 1
                || $this->ppsTreeComplete($pps->PrevPps))
385 1
            && ($pps->NextPps == -1
386 1
                || $this->ppsTreeComplete($pps->NextPps))
387 1
            && ($pps->DirPps == -1
388 1
                || $this->ppsTreeComplete($pps->DirPps));
389
    }
390
391
    /**
392
     * Checks whether a PPS is a File PPS or not.
393
     * If there is no PPS for the index given, it will return false.
394
     *
395
     * @param int $index The index for the PPS
396
     *
397
     * @return bool true if it's a File PPS, false otherwise
398
     */
399
    public function isFile(int $index): bool
400
    {
401
        if (isset($this->_list[$index])) {
402
            return $this->_list[$index]->Type == self::OLE_PPS_TYPE_FILE;
403
        }
404
405
        return false;
406
    }
407
408
    /**
409
     * Checks whether a PPS is a Root PPS or not.
410
     * If there is no PPS for the index given, it will return false.
411
     *
412
     * @param int $index the index for the PPS
413
     *
414
     * @return bool true if it's a Root PPS, false otherwise
415
     */
416
    public function isRoot(int $index): bool
417
    {
418
        if (isset($this->_list[$index])) {
419
            return $this->_list[$index]->Type == self::OLE_PPS_TYPE_ROOT;
420
        }
421
422
        return false;
423
    }
424
425
    /**
426
     * Gives the total number of PPS's found in the OLE container.
427
     *
428
     * @return int The total number of PPS's found in the OLE container
429
     */
430
    public function ppsTotal(): int
431
    {
432
        return count($this->_list);
433
    }
434
435
    /**
436
     * Gets data from a PPS
437
     * If there is no PPS for the index given, it will return an empty string.
438
     *
439
     * @param int $index The index for the PPS
440
     * @param int $position The position from which to start reading
441
     *                          (relative to the PPS)
442
     * @param int $length The amount of bytes to read (at most)
443
     *
444
     * @return string The binary string containing the data requested
445
     *
446
     * @see OLE_PPS_File::getStream()
447
     */
448
    public function getData(int $index, int $position, int $length): string
449
    {
450
        // if position is not valid return empty string
451
        if (!isset($this->_list[$index]) || ($position >= $this->_list[$index]->Size) || ($position < 0)) {
452
            return '';
453
        }
454
        $fh = $this->getStream($this->_list[$index]);
455
        $data = (string) stream_get_contents($fh, $length, $position);
456
        fclose($fh);
457
458
        return $data;
459
    }
460
461
    /**
462
     * Gets the data length from a PPS
463
     * If there is no PPS for the index given, it will return 0.
464
     *
465
     * @param int $index The index for the PPS
466
     *
467
     * @return int The amount of bytes in data the PPS has
468
     */
469
    public function getDataLength(int $index): int
470
    {
471
        if (isset($this->_list[$index])) {
472
            return $this->_list[$index]->Size;
473
        }
474
475
        return 0;
476
    }
477
478
    /**
479
     * Utility function to transform ASCII text to Unicode.
480
     *
481
     * @param string $ascii The ASCII string to transform
482
     *
483
     * @return string The string in Unicode
484
     */
485 117
    public static function ascToUcs(string $ascii): string
486
    {
487 117
        $rawname = '';
488 117
        $iMax = strlen($ascii);
489 117
        for ($i = 0; $i < $iMax; ++$i) {
490 117
            $rawname .= $ascii[$i]
491 117
                . "\x00";
492
        }
493
494 117
        return $rawname;
495
    }
496
497
    /**
498
     * Utility function
499
     * Returns a string for the OLE container with the date given.
500
     *
501
     * @param float|int $date A timestamp
502
     *
503
     * @return string The string for the OLE container
504
     */
505 115
    public static function localDateToOLE($date): string
506
    {
507 115
        if (!$date) {
508 115
            return "\x00\x00\x00\x00\x00\x00\x00\x00";
509
        }
510 115
        $dateTime = Date::dateTimeFromTimestamp("$date");
511
512
        // days from 1-1-1601 until the beggining of UNIX era
513 115
        $days = 134774;
514
        // calculate seconds
515 115
        $big_date = $days * 24 * 3600 + (float) $dateTime->format('U');
516
        // multiply just to make MS happy
517 115
        $big_date *= 10000000;
518
519
        // Make HEX string
520 115
        $res = '';
521
522 115
        $factor = 2 ** 56;
523 115
        while ($factor >= 1) {
524 115
            $hex = (int) floor($big_date / $factor);
525 115
            $res = pack('c', $hex) . $res;
526 115
            $big_date = fmod($big_date, $factor);
527 115
            $factor /= 256;
528
        }
529
530 115
        return $res;
531
    }
532
533
    /**
534
     * Returns a timestamp from an OLE container's date.
535
     *
536
     * @param string $oleTimestamp A binary string with the encoded date
537
     *
538
     * @return float|int The Unix timestamp corresponding to the string
539
     */
540 124
    public static function OLE2LocalDate(string $oleTimestamp)
541
    {
542 124
        if (strlen($oleTimestamp) != 8) {
543 1
            throw new ReaderException('Expecting 8 byte string');
544
        }
545
546
        // convert to units of 100 ns since 1601:
547
        /** @var int[] */
548 123
        $unpackedTimestamp = unpack('v4', $oleTimestamp) ?: [];
549 123
        $timestampHigh = (float) $unpackedTimestamp[4] * 65536 + (float) $unpackedTimestamp[3];
550 123
        $timestampLow = (float) $unpackedTimestamp[2] * 65536 + (float) $unpackedTimestamp[1];
551
552
        // translate to seconds since 1601:
553 123
        $timestampHigh /= 10000000;
554 123
        $timestampLow /= 10000000;
555
556
        // days from 1601 to 1970:
557 123
        $days = 134774;
558
559
        // translate to seconds since 1970:
560 123
        $unixTimestamp = floor(65536.0 * 65536.0 * $timestampHigh + $timestampLow - $days * 24 * 3600 + 0.5);
561
562 123
        return IntOrFloat::evaluate($unixTimestamp);
563
    }
564
}
565