Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions src/Decompressor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ final class Decompressor extends TransformStream
/** @var ?resource */
private $context;

/** @var bool */
private $incomplete = false;

/**
* @param int $encoding ZLIB_ENCODING_GZIP, ZLIB_ENCODING_RAW or ZLIB_ENCODING_DEFLATE
*/
Expand Down Expand Up @@ -80,31 +83,24 @@ protected function transformData($chunk)
throw new \RuntimeException('Unable to decompress' . $errstr);
}

// detect incomplete data on PHP 7.2+ only, older versions lack inflate_get_status()
$this->incomplete = PHP_VERSION_ID >= 70200 && inflate_get_status($this->context) !== ZLIB_STREAM_END;

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, ':');
});
if ($chunk !== '') {
$this->transformData($chunk);
}

$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]);
if ($this->incomplete) {
throw new \RuntimeException('Unable to decompress: unexpected end of stream'); // @codeCoverageIgnore
}

$this->emit('end');
Expand Down
21 changes: 21 additions & 0 deletions tests/DeflateDecompressorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public function testInflateEmpty()
$this->decompressor->end(gzdeflate(''));
}

public function testInflateWithoutDataEmitsEnd()
{
$this->decompressor->on('error', $this->expectCallableNever());
$this->decompressor->on('end', $this->expectCallableOnce());

$this->decompressor->end();
}

public function testInflateHelloWorld()
{
$this->decompressor->on('data', function ($data) use (&$buffered) {
Expand Down Expand Up @@ -84,4 +92,17 @@ public function testDecompressInvalidOnEndEmitsErrorWithoutCallingCustomErrorHan
restore_error_handler();
$this->assertNull($error);
}

public function testDecompressTruncatedDataOnEndEmitsError()
{
if (PHP_VERSION_ID < 70200) {
$this->markTestSkipped('Requires PHP 7.2+ to detect incomplete data');
}

$this->decompressor->on('end', $this->expectCallableNever());
$this->decompressor->on('error', $this->expectCallableOnce());

$this->decompressor->write(substr(gzdeflate('hello world'), 0, -1));
$this->decompressor->end();
}
}
21 changes: 21 additions & 0 deletions tests/GzipDecompressorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public function testDecompressEmpty()
$this->decompressor->end(gzencode(''));
}

public function testDecompressWithoutDataEmitsEnd()
{
$this->decompressor->on('error', $this->expectCallableNever());
$this->decompressor->on('end', $this->expectCallableOnce());

$this->decompressor->end();
}

public function testDecompressHelloWorld()
{
$this->decompressor->on('data', function ($data) use (&$buffered) {
Expand Down Expand Up @@ -84,4 +92,17 @@ public function testDecompressInvalidOnEndEmitsErrorWithoutCallingCustomErrorHan
restore_error_handler();
$this->assertNull($error);
}

public function testDecompressTruncatedDataOnEndEmitsError()
{
if (PHP_VERSION_ID < 70200) {
$this->markTestSkipped('Requires PHP 7.2+ to detect incomplete data');
}

$this->decompressor->on('end', $this->expectCallableNever());
$this->decompressor->on('error', $this->expectCallableOnce());

$this->decompressor->write(substr(gzencode('hello world'), 0, -1));
$this->decompressor->end();
}
}
21 changes: 21 additions & 0 deletions tests/ZlibDecompressorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public function testDecompressEmpty()
$this->decompressor->end(gzcompress(''));
}

public function testDecompressWithoutDataEmitsEnd()
{
$this->decompressor->on('error', $this->expectCallableNever());
$this->decompressor->on('end', $this->expectCallableOnce());

$this->decompressor->end();
}

public function testDecompressHelloWorld()
{
$this->decompressor->on('data', function ($data) use (&$buffered) {
Expand Down Expand Up @@ -84,4 +92,17 @@ public function testDecompressInvalidOnEndEmitsErrorWithoutCallingCustomErrorHan
restore_error_handler();
$this->assertNull($error);
}

public function testDecompressTruncatedDataOnEndEmitsError()
{
if (PHP_VERSION_ID < 70200) {
$this->markTestSkipped('Requires PHP 7.2+ to detect incomplete data');
}

$this->decompressor->on('end', $this->expectCallableNever());
$this->decompressor->on('error', $this->expectCallableOnce());

$this->decompressor->write(substr(gzcompress('hello world'), 0, -1));
$this->decompressor->end();
}
}