forked from clue/reactphp-zlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecompressor.php
More file actions
115 lines (96 loc) · 3.26 KB
/
Decompressor.php
File metadata and controls
115 lines (96 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Clue\React\Zlib;
use Clue\StreamFilter as Filter;
/**
* The `Decompressor` class can be used to decompress a stream of data.
*
* It implements the [`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface)
* and accepts compressed data on its writable side and emits decompressed data
* on its readable side.
*
* ```php
* $encoding = ZLIB_ENCODING_GZIP; // or ZLIB_ENCODING_RAW or ZLIB_ENCODING_DEFLATE
* $decompressor = new Clue\React\Zlib\Decompressor($encoding);
*
* $decompressor->on('data', function ($data) {
* echo $data; // decompressed data chunk
* });
*
* $decompressor->write($compressed); // write compressed binary data chunk
* ```
*
* This is particularly useful in a piping context:
*
* ```php
* $input->pipe($decompressor)->pipe($filterBadWords)->pipe($output);
* ```
*
* For more details, see ReactPHP's
* [`DuplexStreamInterface`](https://github.com/reactphp/stream#duplexstreaminterface).
*/
final class Decompressor extends TransformStream
{
/** @var ?resource */
private $context;
/**
* @param int $encoding ZLIB_ENCODING_GZIP, ZLIB_ENCODING_RAW or ZLIB_ENCODING_DEFLATE
*/
public function __construct($encoding)
{
$errstr = '';
set_error_handler(function ($_, $error) use (&$errstr) {
// Match errstr from PHP's warning message.
// inflate_init(): encoding mode must be ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP or ZLIB_ENCODING_DEFLATE
$errstr = strstr($error, ':'); // @codeCoverageIgnore
});
try {
$context = inflate_init($encoding);
} catch (\ValueError $e) { // @codeCoverageIgnoreStart
// Throws ValueError on PHP 8.0+
restore_error_handler();
throw $e;
} // @codeCoverageIgnoreEnd
restore_error_handler();
if ($context === false) {
throw new \InvalidArgumentException('Unable to initialize decompressor' . $errstr); // @codeCoverageIgnore
}
$this->context = $context;
}
protected function transformData($chunk)
{
$errstr = '';
set_error_handler(function ($_, $error) use (&$errstr) {
// Match errstr from PHP's warning message.
// inflate_add(): data error
$errstr = strstr($error, ':');
});
$ret = inflate_add($this->context, $chunk);
restore_error_handler();
if ($ret === false) {
throw new \RuntimeException('Unable to decompress' . $errstr);
}
if ($ret !== '') {
$this->emit('data', [$ret]);
}
}
protected function transformEnd($chunk)
{
$errstr = '';
set_error_handler(function ($_, $error) use (&$errstr) {
// Match errstr from PHP's warning message.
// inflate_add(): data error
$errstr = strstr($error, ':');
});
$ret = inflate_add($this->context, $chunk, ZLIB_FINISH);
$this->context = null;
restore_error_handler();
if ($ret === false) {
throw new \RuntimeException('Unable to decompress' . $errstr);
}
if ($ret !== '') {
$this->emit('data', [$ret]);
}
$this->emit('end');
$this->close();
}
}