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