Skip to content

Fix #48725: Support for flushing in zlib stream #6019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions ext/zlib/tests/bug48725.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
Bug #48725 (Support for flushing in zlib stream)
--SKIPIF--
<?php
if (!extension_loaded('zlib')) die('skip zip extension not available');
?>
--FILE--
<?php
$text = str_repeat('0123456789abcdef', 1000);

$temp = fopen('php://temp', 'r+');
stream_filter_append($temp, 'zlib.deflate', STREAM_FILTER_WRITE);
fwrite($temp, $text);

rewind($temp);

var_dump(bin2hex(stream_get_contents($temp)));
var_dump(ftell($temp));

fclose($temp);
?>
--EXPECT--
string(138) "ecc7c901c0100000b09594bac641d97f840e22f9253c31bdb9d4d6c75cdf3ec1ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddaffc0f0000ffff"
int(69)
14 changes: 10 additions & 4 deletions ext/zlib/zlib_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ typedef struct _php_zlib_filter_data {
unsigned char *outbuf;
size_t outbuf_len;
int persistent;
zend_bool finished;
zend_bool finished; /* for zlib.deflate: signals that no flush is pending */
} php_zlib_filter_data;

/* }}} */
Expand Down Expand Up @@ -196,14 +196,18 @@ static php_stream_filter_status_t php_zlib_deflate_filter(
bucket = php_stream_bucket_make_writeable(bucket);

while (bin < (unsigned int) bucket->buflen) {
int flush_mode;

desired = bucket->buflen - bin;
if (desired > data->inbuf_len) {
desired = data->inbuf_len;
}
memcpy(data->strm.next_in, bucket->buf + bin, desired);
data->strm.avail_in = desired;

status = deflate(&(data->strm), flags & PSFS_FLAG_FLUSH_CLOSE ? Z_FULL_FLUSH : (flags & PSFS_FLAG_FLUSH_INC ? Z_SYNC_FLUSH : Z_NO_FLUSH));
flush_mode = flags & PSFS_FLAG_FLUSH_CLOSE ? Z_FULL_FLUSH : (flags & PSFS_FLAG_FLUSH_INC ? Z_SYNC_FLUSH : Z_NO_FLUSH);
data->finished = flush_mode != Z_NO_FLUSH;
status = deflate(&(data->strm), flush_mode);
if (status != Z_OK) {
/* Something bad happened */
php_stream_bucket_delref(bucket);
Expand All @@ -230,11 +234,12 @@ static php_stream_filter_status_t php_zlib_deflate_filter(
php_stream_bucket_delref(bucket);
}

if (flags & PSFS_FLAG_FLUSH_CLOSE) {
if (flags & PSFS_FLAG_FLUSH_CLOSE || ((flags & PSFS_FLAG_FLUSH_INC) && !data->finished)) {
/* Spit it out! */
status = Z_OK;
while (status == Z_OK) {
status = deflate(&(data->strm), Z_FINISH);
status = deflate(&(data->strm), (flags & PSFS_FLAG_FLUSH_CLOSE ? Z_FINISH : Z_SYNC_FLUSH));
data->finished = 1;
if (data->strm.avail_out < data->outbuf_len) {
size_t bucketlen = data->outbuf_len - data->strm.avail_out;

Expand Down Expand Up @@ -395,6 +400,7 @@ static php_stream_filter *php_zlib_filter_create(const char *filtername, zval *f
}
}
status = deflateInit2(&(data->strm), level, Z_DEFLATED, windowBits, memLevel, 0);
data->finished = 1;
fops = &php_zlib_deflate_ops;
} else {
status = Z_DATA_ERROR;
Expand Down