/** * {@inheritdoc} */ public function sendBinary(ReadableStream $stream, int $priority = 0) : Awaitable { return $this->writer->execute(function () use($stream) { $type = Frame::BINARY; $reserved = Frame::RESERVED1; $context = $this->deflate->getCompressionContext(); $len = 0; try { $chunk = (yield $stream->readBuffer(4092)); while (null !== ($next = (yield $stream->readBuffer(4092)))) { $chunk = \deflate_add($context, $chunk, \ZLIB_SYNC_FLUSH); $len += (yield $this->writeFrame(new Frame($type, $chunk, false, $reserved))); $chunk = $next; $type = Frame::CONTINUATION; $reserved = 0; } if ($chunk !== null) { $chunk = \substr(\deflate_add($context, $chunk, $this->deflate->getCompressionFlushMode()), 0, -4); $len += (yield $this->writeFrame(new Frame($type, $chunk, true, $reserved))); } return $len; } finally { $stream->close(); } }, $priority); }
protected function processChunk(string $chunk) : string { if ($chunk === '') { return \deflate_add($this->context, '', \ZLIB_FINISH); } return \deflate_add($this->context, $chunk, \ZLIB_SYNC_FLUSH); }
/** * {@inheritdoc} */ protected function send(string $data, float $timeout = 0, bool $end = false) : \Generator { if (false === ($data = deflate_add($this->resource, $data, $end ? \ZLIB_FINISH : \ZLIB_SYNC_FLUSH))) { throw new FailureException('Failed adding date to deflate stream.'); } return yield from parent::send($data, $timeout, $end); }
protected function processChunk(string $chunk) : string { $this->buffer .= $chunk; $compressed = ''; while (\strlen($this->buffer) >= $this->bufferSize) { $chunk = \substr($this->buffer, 0, $this->bufferSize); $this->buffer = \substr($this->buffer, \strlen($chunk)); $compressed .= \deflate_add($this->context, $chunk, \ZLIB_SYNC_FLUSH); } return $compressed; }
function deflateStream($mode, $flushSize, $flushType) { $buffer = ""; $deflated = null; $resource = deflate_init($mode); while (true) { $dataToCompress = (yield $deflated); if (isset($dataToCompress)) { $buffer .= $dataToCompress; if (strlen($buffer) >= $flushSize) { $deflated = deflate_add($resource, $buffer, $flushType); $buffer = ""; } else { $deflated = null; } } else { $deflated = deflate_add($resource, $buffer, ZLIB_FINISH); } } }
* USAGE: encode-inventory-file.php <input file> <output file> * * Agents may generate inventory data as a zlib stream. The file format is * rather impractical; this tool is mostly useful to generate input for testing * the decoder. */ error_reporting(E_ALL); if ($_SERVER['argc'] != 3) { print "USAGE: encode-inventory-file.php <input file> <output file>\n"; exit(1); } $input = file_get_contents($_SERVER['argv'][1]); if (!$input) { print "Could not read input file\n"; exit(1); } $context = deflate_init(ZLIB_ENCODING_DEFLATE); if (!$context) { print "Could not create deflate context\n"; exit(1); } // Compress input in blocks of 32 kB with ZLIB_SYNC_FLUSH for each block, just // like the agent does. $output = ''; foreach (str_split($input, 0x8000) as $chunk) { $output .= deflate_add($context, $chunk, ZLIB_SYNC_FLUSH); } if (file_put_contents($_SERVER['argv'][2], $output) !== strlen($output)) { print "Could not write output file\n"; exit(1); }
<?php $dict = range("a", "z"); $r = deflate_init(ZLIB_ENCODING_DEFLATE, ["dictionary" => $dict]); $a = deflate_add($r, "abdcde", ZLIB_FINISH); var_dump($a); $r = deflate_init(ZLIB_ENCODING_DEFLATE, ["dictionary" => implode("", $dict) . ""]); $dictStr_a = deflate_add($r, "abdcde", ZLIB_FINISH); var_dump($dictStr_a === $a); $r = inflate_init(ZLIB_ENCODING_DEFLATE, ["dictionary" => $dict]); var_dump(inflate_add($r, $a, ZLIB_FINISH)); $r = inflate_init(ZLIB_ENCODING_DEFLATE, ["dictionary" => ["8"] + range("a", "z")]); var_dump(inflate_add($r, $a, ZLIB_FINISH));
/** * {@inheritdoc} */ public function close() { $chunk = deflate_add($this->context, $this->buffer, ZLIB_FINISH); $this->buffer = ''; if ($this->stream !== NULL) { $this->stream->write($chunk); parent::close(); } }
<?php $badResource = fopen("php://memory", "r+"); var_dump(deflate_add($badResource, "test")); $resource = deflate_init(ZLIB_ENCODING_DEFLATE); $badFlushType = 6789; var_dump(deflate_add($resource, "test", $badFlushType));
<?php $resource = deflate_init(ZLIB_ENCODING_DEFLATE); foreach (range("a", "z") as $char) { deflate_add($resource, $char); } deflate_add($resource, "", ZLIB_FINISH); // Now reuse the existing resource after finishing the previous operations ... $uncompressed = $compressed = ""; foreach (range("a", "z") as $char) { $uncompressed .= $char; $compressed .= deflate_add($resource, $char, ZLIB_NO_FLUSH); } $compressed .= deflate_add($resource, "", ZLIB_FINISH); assert($uncompressed === zlib_decode($compressed)); ?> ===DONE===
<?php $resource = deflate_init(ZLIB_ENCODING_GZIP); var_dump(deflate_add($resource, "aaaaaaaaaaaaaaaaaaaaaa", ZLIB_BLOCK)); ?> ===DONE===