Passed
Push — master ( 5dee5a...729c4d )
by
unknown
20:09 queued 09:33
created

OLE::readInt1()   A

Complexity

Conditions 3
Paths 1

Size

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