• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPOffice / PhpSpreadsheet / 17999085524

25 Sep 2025 06:34AM UTC coverage: 95.867% (+0.3%) from 95.602%
17999085524

Pull #4662

github

web-flow
Merge 0dac8acdb into e3cac6f1f
Pull Request #4662: WIP Do Not Install

45116 of 47061 relevant lines covered (95.87%)

373.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.24
/src/PhpSpreadsheet/Shared/OLE/ChainedBlockStream.php
1
<?php
2

3
namespace PhpOffice\PhpSpreadsheet\Shared\OLE;
4

5
use PhpOffice\PhpSpreadsheet\Exception;
6
use PhpOffice\PhpSpreadsheet\Shared\OLE;
7

8
class ChainedBlockStream
9
{
10
    /** @var mixed */
11
    public $context;
12

13
    /**
14
     * The OLE container of the file that is being read.
15
     */
16
    public ?OLE $ole = null;
17

18
    /**
19
     * Parameters specified by fopen().
20
     *
21
     * @var mixed[]
22
     */
23
    public array $params = [];
24

25
    /**
26
     * The binary data of the file.
27
     */
28
    public string $data;
29

30
    /**
31
     * The file pointer.
32
     *
33
     * @var int byte offset
34
     */
35
    public int $pos = 0;
36

37
    /**
38
     * Implements support for fopen().
39
     * For creating streams using this wrapper, use OLE_PPS_File::getStream().
40
     *
41
     * @param string $path resource name including scheme, e.g.
42
     *                                    ole-chainedblockstream://oleInstanceId=1
43
     * @param string $mode only "r" is supported
44
     * @param int $options mask of STREAM_REPORT_ERRORS and STREAM_USE_PATH
45
     * @param ?string $openedPath absolute path of the opened stream (out parameter)
46
     *
47
     * @return bool true on success
48
     */
49
    public function stream_open(string $path, string $mode, int $options, ?string &$openedPath): bool // @codingStandardsIgnoreLine
5✔
50
    {
51
        if ($mode[0] !== 'r') {
5✔
52
            if ($options & STREAM_REPORT_ERRORS) {
2✔
53
                trigger_error('Only reading is supported', E_USER_WARNING);
1✔
54
            }
55

56
            return false;
2✔
57
        }
58

59
        // 25 is length of "ole-chainedblockstream://"
60
        parse_str(substr($path, 25), $this->params);
3✔
61
        if (!isset($this->params['oleInstanceId'], $this->params['blockId'], $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']])) { //* @phpstan-ignore-line
3✔
62
            if ($options & STREAM_REPORT_ERRORS) {
2✔
63
                trigger_error('OLE stream not found', E_USER_WARNING);
1✔
64
            }
65

66
            return false;
2✔
67
        }
68
        $this->ole = $GLOBALS['_OLE_INSTANCES'][$this->params['oleInstanceId']]; //* @phpstan-ignore-line
1✔
69
        if (!($this->ole instanceof OLE)) { //* @phpstan-ignore-line
1✔
70
            throw new Exception('class is not OLE');
×
71
        }
72

73
        $blockId = $this->params['blockId'];
1✔
74
        $this->data = '';
1✔
75
        if (isset($this->params['size']) && $this->params['size'] < $this->ole->bigBlockThreshold && $blockId != $this->ole->root->startBlock) {
1✔
76
            // Block id refers to small blocks
77
            $rootPos = $this->ole->getBlockOffset((int) $this->ole->root->startBlock);
×
78
            while ($blockId != -2) {
×
79
                /** @var int $blockId */
80
                $pos = $rootPos + $blockId * $this->ole->bigBlockSize;
×
81
                $blockId = $this->ole->sbat[$blockId];
×
82
                fseek($this->ole->_file_handle, $pos);
×
83
                $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
×
84
            }
85
        } else {
86
            // Block id refers to big blocks
87
            while ($blockId != -2) {
1✔
88
                /** @var int $blockId */
89
                $pos = $this->ole->getBlockOffset($blockId);
1✔
90
                fseek($this->ole->_file_handle, $pos);
1✔
91
                $this->data .= fread($this->ole->_file_handle, $this->ole->bigBlockSize);
1✔
92
                $blockId = $this->ole->bbat[$blockId];
1✔
93
            }
94
        }
95
        if (isset($this->params['size'])) {
1✔
96
            $this->data = substr($this->data, 0, $this->params['size']); //* @phpstan-ignore-line
×
97
        }
98

99
        if ($options & STREAM_USE_PATH) {
1✔
100
            $openedPath = $path;
×
101
        }
102

103
        return true;
1✔
104
    }
105

106
    /**
107
     * Implements support for fclose().
108
     */
109
    public function stream_close(): void // @codingStandardsIgnoreLine
1✔
110
    {
111
        $this->ole = null;
1✔
112
        unset($GLOBALS['_OLE_INSTANCES']);
1✔
113
    }
114

115
    /**
116
     * Implements support for fread(), fgets() etc.
117
     *
118
     * @param int $count maximum number of bytes to read
119
     *
120
     * @return false|string
121
     */
122
    public function stream_read(int $count): bool|string // @codingStandardsIgnoreLine
1✔
123
    {
124
        if ($this->stream_eof()) {
1✔
125
            return false;
×
126
        }
127
        $s = substr($this->data, (int) $this->pos, $count);
1✔
128
        $this->pos += $count;
1✔
129

130
        return $s;
1✔
131
    }
132

133
    /**
134
     * Implements support for feof().
135
     *
136
     * @return bool TRUE if the file pointer is at EOF; otherwise FALSE
137
     */
138
    public function stream_eof(): bool // @codingStandardsIgnoreLine
1✔
139
    {
140
        return $this->pos >= strlen($this->data);
1✔
141
    }
142

143
    /**
144
     * Returns the position of the file pointer, i.e. its offset into the file
145
     * stream. Implements support for ftell().
146
     */
147
    public function stream_tell(): int // @codingStandardsIgnoreLine
1✔
148
    {
149
        return $this->pos;
1✔
150
    }
151

152
    /**
153
     * Implements support for fseek().
154
     *
155
     * @param int $offset byte offset
156
     * @param int $whence SEEK_SET, SEEK_CUR or SEEK_END
157
     */
158
    public function stream_seek(int $offset, int $whence): bool // @codingStandardsIgnoreLine
1✔
159
    {
160
        if ($whence == SEEK_SET && $offset >= 0) {
1✔
161
            $this->pos = $offset;
1✔
162
        } elseif ($whence == SEEK_CUR && -$offset <= $this->pos) {
×
163
            $this->pos += $offset;
×
164
        } elseif ($whence == SEEK_END && -$offset <= count($this->data)) { // @phpstan-ignore-line
×
165
            $this->pos = strlen($this->data) + $offset;
×
166
        } else {
167
            return false;
×
168
        }
169

170
        return true;
1✔
171
    }
172

173
    /**
174
     * Implements support for fstat(). Currently the only supported field is
175
     * "size".
176
     *
177
     * @return array{size: int}
178
     */
179
    public function stream_stat(): array // @codingStandardsIgnoreLine
×
180
    {
181
        return [
×
182
            'size' => strlen($this->data),
×
183
        ];
×
184
    }
185

186
    // Methods used by stream_wrapper_register() that are not implemented:
187
    // bool stream_flush ( void )
188
    // int stream_write ( string data )
189
    // bool rename ( string path_from, string path_to )
190
    // bool mkdir ( string path, int mode, int options )
191
    // bool rmdir ( string path, int options )
192
    // bool dir_opendir ( string path, int options )
193
    // array url_stat ( string path, int flags )
194
    // string dir_readdir ( void )
195
    // bool dir_rewinddir ( void )
196
    // bool dir_closedir ( void )
197
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc