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