/** * {@inheritdoc} * @throws \RuntimeException if zlib initialization error occurs * @throws \InvalidArgumentException if an error occurs while decoding (most likely invalid input) */ public function filter($value) { $context = inflate_init(ZLIB_ENCODING_DEFLATE); if (!$context) { // @codeCoverageIgnoreStart throw new \RuntimeException('Could not create inflate context'); // @codeCoverageIgnoreEnd } $output = @inflate_add($context, $value); if ($output === false) { throw new \InvalidArgumentException('Input does not appear to be a zlib stream'); } return $output; }
function inflateStream($mode, $flushSize) { $buffer = ""; $inflated = null; $resource = inflate_init($mode); while (true) { $dataToInflate = (yield $inflated); if (isset($dataToInflate)) { $buffer .= $dataToInflate; if (strlen($buffer) >= $flushSize) { $inflated = inflate_add($resource, $buffer); $buffer = ""; } else { $inflated = null; } } else { $inflated = inflate_add($resource, $buffer, ZLIB_FINISH); } } }
/** * @param int $type Compression type. Use GZIP or DEFLATE constants defined in this class. * @param int $hwm * * @throws \Icicle\Exception\UnsupportedError If the zlib extension is not loaded. */ public function __construct(int $type, int $hwm = 0) { // @codeCoverageIgnoreStart if (!extension_loaded('zlib')) { throw new UnsupportedError('zlib extension required to decode compressed streams.'); } // @codeCoverageIgnoreEnd parent::__construct($hwm); switch ($type) { case self::GZIP: case self::DEFLATE: $this->resource = inflate_init($type); break; default: throw new InvalidArgumentError('Invalid compression type.'); } if (null === $this->resource) { throw new FailureException('Could not initialize inflate handle.'); } }
public function __construct(ReadableStream $stream, int $encoding = null) { parent::__construct($stream); $this->context = \inflate_init($encoding ?? \ZLIB_ENCODING_GZIP); }
<?php $resource = inflate_init(ZLIB_ENCODING_GZIP); $uncompressed = implode(range("a", "z")); $compressed = gzencode($uncompressed); $inflated = ""; for ($i = 0; $i < strlen($compressed); $i++) { $inflated .= inflate_add($resource, $compressed[$i]); } $inflated .= inflate_add($resource, "", ZLIB_FINISH); assert($inflated === $uncompressed); // Now reuse the existing resource after finishing the previous operations ... $inflated = ""; for ($i = 0; $i < strlen($compressed); $i++) { $inflated .= inflate_add($resource, $compressed[$i]); } $inflated .= inflate_add($resource, "", ZLIB_FINISH); assert($inflated === $uncompressed); ?> ===DONE===
<?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));
/** * Get or create the zlib decompression context to be used. * * @return resource */ public function getDecompressionContext() { if ($this->decompression) { return $this->decompression; } $context = \inflate_init(\ZLIB_ENCODING_RAW); if ($this->decompressionTakeover) { return $this->decompression = $context; } return $context; }
<?php $badResource = fopen("php://memory", "r+"); var_dump(inflate_add($badResource, "test")); $resource = inflate_init(ZLIB_ENCODING_DEFLATE); $badFlushType = 6789; var_dump(inflate_add($resource, "test", $badFlushType));
<?php var_dump(inflate_init()); var_dump(inflate_init(42));
public function __invoke(Request $req, Response $res) { $headers = $req->getAllHeaders(); unset($headers["accept-encoding"]); $connection = $headers["connection"]; unset($headers["connection"]); foreach ($connection as $value) { foreach (explode(",", strtolower($value)) as $type) { $type = trim($type); if ($type == "upgrade") { $headers["connection"][0] = "upgrade"; } else { unset($headers[$type]); } } } if ($this->headers) { if (is_callable($this->headers)) { $headers = ($this->headers)($headers); } else { $headers = $this->headers + $headers; } } $promise = $this->client->request((new \Amp\Artax\Request())->setMethod($req->getMethod())->setUri($this->target . $req->getUri())->setAllHeaders($headers)->setBody((yield $req->getBody()))); // no async sending possible :-( [because of redirects] $promise->watch(function ($update) use($req, $res, &$hasBody, &$status, &$zlib) { list($type, $data) = $update; if ($type == Notify::RESPONSE_HEADERS) { $headers = array_change_key_case($data["headers"], CASE_LOWER); foreach ($data["headers"] as $header => $values) { foreach ($values as $value) { $res->addHeader($header, $value); } } $res->setStatus($status = $data["status"]); $res->setReason($data["reason"]); if (isset($headers["content-encoding"]) && strcasecmp(trim(current($headers["content-encoding"])), 'gzip') === 0) { $zlib = inflate_init(ZLIB_ENCODING_GZIP); } $hasBody = true; } if ($type == Notify::RESPONSE_BODY_DATA) { if ($zlib) { $data = inflate_add($zlib, $data); } $res->stream($data); } if ($type == Notify::RESPONSE) { if (!$hasBody) { foreach ($data->getAllHeaders() as $header => $values) { foreach ($values as $value) { $res->addHeader($header, $value); } } $res->setStatus($status = $data->getStatus()); $res->setReason($data->getReason()); } if ($status == 101) { $req->setLocalVar("aerys.reverse.socket", $update["export_socket"]()); } $res->end($zlib ? inflate_add("", ZLIB_FINISH) : null); } }); (yield $promise); }