示例#1
0
 public function __construct(ReadableStream $stream, int $encoding = null, int $level = 1, array $options = [])
 {
     if (!isset($options['window'])) {
         $options['window'] = 15;
     }
     $this->context = \deflate_init($encoding ?? \ZLIB_ENCODING_GZIP, \array_merge($options, ['level' => $level]));
     $this->bufferSize = \max(8192, \pow(2, $options['window']));
     parent::__construct($stream);
 }
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);
        }
    }
}
示例#3
0
 /**
  * @param int $type Compression type. Use GZIP or DEFLATE constants defined in this class.
  * @param int $level Compression level.
  * @param int $hwm
  *
  * @throws \Icicle\Exception\UnsupportedError If the zlib extension is not loaded.
  * @throws \Icicle\Exception\InvalidArgumentError If the $type is not a valid compression type.
  */
 public function __construct(int $type, int $level = self::DEFAULT_LEVEL, int $hwm = 0)
 {
     // @codeCoverageIgnoreStart
     if (!extension_loaded('zlib')) {
         throw new UnsupportedError('zlib extension required to decode compressed streams.');
     }
     // @codeCoverageIgnoreEnd
     if (-1 > $level || 9 < $level) {
         throw new InvalidArgumentError('Level must be between -1 (default) and 9.');
     }
     switch ($type) {
         case self::GZIP:
         case self::DEFLATE:
             $this->resource = deflate_init($type, ['level' => $level]);
             break;
         default:
             throw new InvalidArgumentError('Invalid compression type.');
     }
     if (null === $this->resource) {
         throw new FailureException('Could not initialize deflate handle.');
     }
     parent::__construct($hwm);
 }
<?php

var_dump(deflate_init(42));
var_dump(deflate_init(ZLIB_ENCODING_DEFLATE, ['level' => 42]));
var_dump(deflate_init(ZLIB_ENCODING_DEFLATE, ['level' => -2]));
var_dump(deflate_init(ZLIB_ENCODING_DEFLATE, ['memory' => 0]));
var_dump(deflate_init(ZLIB_ENCODING_DEFLATE, ['memory' => 10]));
 * 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));
示例#7
0
 /**
  * Get or create the zlib compression context to be used.
  * 
  * @return resource
  */
 public function getCompressionContext()
 {
     if ($this->compression) {
         return $this->compression;
     }
     $context = \deflate_init(\ZLIB_ENCODING_RAW, ['level' => 1, 'window' => $this->compressionWindow]);
     if ($this->compressionTakeover) {
         return $this->compression = $context;
     }
     return $context;
 }
<?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_GZIP);
var_dump(deflate_add($resource, "aaaaaaaaaaaaaaaaaaaaaa", ZLIB_BLOCK));
?>
===DONE===