1--TEST--
2Test incremental deflate_add() functionality
3--EXTENSIONS--
4zlib
5--FILE--
6<?php
7
8function deflateStream($mode, $flushSize, $flushType) {
9    $buffer = "";
10    $deflated = null;
11    $resource = deflate_init($mode);
12
13    while (true) {
14        $dataToCompress = yield $deflated;
15        if (isset($dataToCompress)) {
16            $buffer .= $dataToCompress;
17            if (strlen($buffer) >= $flushSize) {
18                $deflated = deflate_add($resource, $buffer, $flushType);
19                $buffer = "";
20            } else {
21                $deflated = null;
22            }
23        } else {
24            $deflated = deflate_add($resource, $buffer, ZLIB_FINISH);
25        }
26    }
27}
28
29$modes = [
30    'ZLIB_ENCODING_RAW'     => ZLIB_ENCODING_RAW,
31    'ZLIB_ENCODING_GZIP'    => ZLIB_ENCODING_GZIP,
32    'ZLIB_ENCODING_DEFLATE' => ZLIB_ENCODING_DEFLATE,
33];
34$flushSizes = [1, 4, 32768];
35$flushTypes = [
36    'ZLIB_SYNC_FLUSH' => ZLIB_SYNC_FLUSH,
37    'ZLIB_PARTIAL_FLUSH' => ZLIB_PARTIAL_FLUSH,
38    'ZLIB_FULL_FLUSH' => ZLIB_FULL_FLUSH,
39    'ZLIB_NO_FLUSH' => ZLIB_NO_FLUSH,
40];
41
42/* Z_BLOCK is only available for deflate when built against zlib >= 1.2.4 */
43if (ZLIB_VERNUM >= 0x1240) {
44    $flushTypes['ZLIB_BLOCK'] = ZLIB_BLOCK;
45}
46
47foreach ($modes as $modeKey => $mode) {
48    foreach ($flushSizes as $flushSize) {
49        foreach ($flushTypes as $flushTypeKey => $flushType) {
50            $uncompressed = $compressed = "";
51            $stream = deflateStream($mode, $flushSize, $flushType);
52            foreach (range("a", "z") as $c) {
53                $uncompressed .= $c;
54                $compressed .= $stream->send($c);
55            }
56            $compressed .= $stream->send(null);
57            if ($uncompressed !== zlib_decode($compressed)) {
58                echo "Error: {$modeKey} | {$flushSize} | {$flushTypeKey}\n";
59            }
60        }
61    }
62}
63?>
64===DONE===
65--EXPECT--
66===DONE===
67