1--TEST--
2Test incremental inflate_add() functionality
3--EXTENSIONS--
4zlib
5--FILE--
6<?php
7function inflateStream($mode, $flushSize) {
8    $buffer = "";
9    $inflated = null;
10    $resource = inflate_init($mode);
11
12    while (true) {
13        $dataToInflate = yield $inflated;
14        if (isset($dataToInflate)) {
15            $buffer .= $dataToInflate;
16            if (strlen($buffer) >= $flushSize) {
17                $inflated = inflate_add($resource, $buffer);
18                $buffer = "";
19            } else {
20                $inflated = null;
21            }
22        } else {
23            $inflated = inflate_add($resource, $buffer, ZLIB_FINISH);
24        }
25    }
26}
27
28$modes = [
29    'ZLIB_ENCODING_RAW'     => ZLIB_ENCODING_RAW,
30    'ZLIB_ENCODING_GZIP'    => ZLIB_ENCODING_GZIP,
31    'ZLIB_ENCODING_DEFLATE' => ZLIB_ENCODING_DEFLATE,
32];
33$flushSizes = [1, 4, 32768];
34$flushTypes = [
35    'ZLIB_SYNC_FLUSH' => ZLIB_SYNC_FLUSH,
36    'ZLIB_PARTIAL_FLUSH' => ZLIB_PARTIAL_FLUSH,
37    'ZLIB_FULL_FLUSH' => ZLIB_FULL_FLUSH,
38    'ZLIB_NO_FLUSH' => ZLIB_NO_FLUSH,
39    'ZLIB_BLOCK' => ZLIB_BLOCK,
40];
41
42$uncompressed = "";
43for ($i=0;$i<(32768*2);$i++) {
44    $uncompressed .= chr(rand(48,125));
45}
46
47foreach ($modes as $modeKey => $mode) {
48    $compressed = zlib_encode($uncompressed, $mode);
49    $compressedLen = strlen($compressed);
50    foreach ($flushSizes as $flushSize) {
51        foreach ($flushTypes as $flushTypeKey => $flushType) {
52            $inflated = "";
53            $stream = inflateStream($mode, $flushSize, $flushType);
54            for ($i=0;$i<$compressedLen;$i++) {
55                $inflated .= $stream->send($compressed[$i]);
56            }
57            $inflated .= $stream->send(null);
58            if ($inflated !== $uncompressed) {
59                echo "Error: {$modeKey} | {$flushSize} | {$flushTypeKey}\n";
60            }
61        }
62
63    }
64}
65
66?>
67===DONE===
68--EXPECT--
69===DONE===
70