Skip to content

Commit

Permalink
Refactor file to avoid deflate state
Browse files Browse the repository at this point in the history
  • Loading branch information
donatj committed Dec 4, 2022
1 parent fed05c2 commit f803194
Showing 1 changed file with 14 additions and 33 deletions.
47 changes: 14 additions & 33 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ class File

private readonly string $fileName;

// This is beeing defined & cleaned up while processing the data
/** @psalm-suppress PropertyNotSetInConstructor */
private DeflateContext $deflate;

private int $totalSize = 0;

public function __construct(
Expand Down Expand Up @@ -236,7 +232,7 @@ private function readStream(bool $send): void
$this->uncompressedSize = 0;
$hash = hash_init('crc32b');

$this->compressionInit();
$deflate = $this->compressionInit();

while (!$this->stream->eof() && ($this->maxSize === null || $this->uncompressedSize < $this->maxSize)) {
$readLength = min(($this->maxSize ?? PHP_INT_MAX) - $this->uncompressedSize, self::CHUNKED_READ_BLOCK_SIZE);
Expand All @@ -247,7 +243,16 @@ private function readStream(bool $send): void

$this->uncompressedSize += strlen($data);

$data = $this->compressData(stream: $this->stream, data: $data);
if($deflate) {
/** @psalm-suppress InvalidArgument */
$data = deflate_add(
$deflate,
$data,
$this->stream->eof()
? ZLIB_FINISH
: ZLIB_NO_FLUSH
);
}

$this->compressedSize += strlen($data);

Expand All @@ -260,12 +265,12 @@ private function readStream(bool $send): void
$this->crc = hexdec(hash_final($hash));
}

private function compressionInit(): void
private function compressionInit(): ?DeflateContext
{
switch($this->compressionMethod) {
case CompressionMethod::STORE:
// Noting to do
return;
return null;
case CompressionMethod::DEFLATE:
$deflateContext = deflate_init(
ZLIB_ENCODING_RAW,
Expand All @@ -280,38 +285,14 @@ private function compressionInit(): void

// False positive, resource is no longer returned from this function
/** @psalm-suppress InvalidPropertyAssignmentValue */
$this->deflate = $deflateContext;
return;
return $deflateContext;
default:
// @codeCoverageIgnoreStart
throw new RuntimeException('Unsupported Compression Method ' . print_r($this->compressionMethod, true));
// @codeCoverageIgnoreEnd
}
}

private function compressData(StreamInterface $stream, string $data): string
{
switch($this->compressionMethod) {
case CompressionMethod::STORE:
// Noting to do
return $data;
case CompressionMethod::DEFLATE:
// False positive, resource is no longer used in this function
/** @psalm-suppress InvalidArgument */
return deflate_add(
$this->deflate,
$data,
$stream->eof()
? ZLIB_FINISH
: ZLIB_NO_FLUSH
);
default:
// @codeCoverageIgnoreStart
throw new RuntimeException('Unsupported Compression Method '. print_r($this->compressionMethod, true));
// @codeCoverageIgnoreEnd
}
}

private function getCdrFile(): string
{
$footer = $this->buildZip64ExtraBlock();
Expand Down

0 comments on commit f803194

Please sign in to comment.