Add more constants, improve comments, and add tests

This commit is contained in:
Matthew Trescott 2017-06-25 11:07:14 +02:00 committed by Bob Weinand
parent 2fddc4a7f1
commit 0c4f11ecaa
4 changed files with 162 additions and 1 deletions

View File

@ -48,6 +48,7 @@ typedef struct _php_zlib_buffer {
typedef struct _php_zlib_context {
z_stream Z;
char *inflateDict;
size_t status;
size_t inflateDictlen;
php_zlib_buffer buffer;
} php_zlib_context;

View File

@ -0,0 +1,29 @@
--TEST--
inflate_get_read_len()
--SKIPIF--
<?php if (!extension_loaded("zlib")) print "skip"; ?>
--FILE--
<?php
$uncompressed = "Hello world.";
$random_junk = str_repeat("qebsouesl", 128);;
$compressed = zlib_encode($uncompressed, ZLIB_ENCODING_DEFLATE);
$compressed_len = strlen($compressed);
$compressed .= $random_junk;
$ctx = inflate_init(ZLIB_ENCODING_DEFLATE);
$buf = inflate_add($ctx, $compressed);
$detected_compressed_len = inflate_get_read_len($ctx);
echo 'Status: ' . inflate_get_status($ctx) . "\n";
echo 'Original compressed length: ' . $compressed_len . "\n";
echo 'Detected compressed length: ' . $detected_compressed_len . "\n";
echo ($compressed_len == $detected_compressed_len) ? 'The lengths are equal.' : 'The lengths are unequal.';
?>
--EXPECT--
Status: 1
Original compressed length: 20
Detected compressed length: 20
The lengths are equal.

View File

@ -0,0 +1,60 @@
--TEST--
inflate_get_status()
--SKIPIF--
<?php if (!extension_loaded("zlib")) print "skip"; ?>
--FILE--
<?php
$uncompressed = "Hello world.";
$random_junk = str_repeat("qebsouesl", 128);;
$compressed = zlib_encode($uncompressed, ZLIB_ENCODING_DEFLATE);
$compressed_len = strlen($compressed);
$compressed .= $random_junk;
$ctx = inflate_init(ZLIB_ENCODING_DEFLATE);
$status = inflate_get_status($ctx);
$buf = '';
for ($i = 0; $status == ZLIB_OK; ++$i)
{
$buf .= inflate_add($ctx, substr($compressed, $i, 1));
echo '$i = ' . $i . ', ';
$status = inflate_get_status($ctx);
echo 'Status: ' . $status;
echo "\n";
}
echo '$buf = ' . $buf;
echo "\n\n";
echo "Adding more data should reset the stream and result in a Z_OK (ZLIB_OK) status.\n";
inflate_add($ctx, substr($compressed, 0, 12));
echo 'Status: ' . inflate_get_status($ctx);
?>
--EXPECT--
$i = 0, Status: 0
$i = 1, Status: 0
$i = 2, Status: 0
$i = 3, Status: 0
$i = 4, Status: 0
$i = 5, Status: 0
$i = 6, Status: 0
$i = 7, Status: 0
$i = 8, Status: 0
$i = 9, Status: 0
$i = 10, Status: 0
$i = 11, Status: 0
$i = 12, Status: 0
$i = 13, Status: 0
$i = 14, Status: 0
$i = 15, Status: 0
$i = 16, Status: 0
$i = 17, Status: 0
$i = 18, Status: 0
$i = 19, Status: 1
$buf = Hello world.
Adding more data should reset the stream and result in a Z_OK (ZLIB_OK) status.
Status: 0

View File

@ -868,6 +868,7 @@ PHP_FUNCTION(inflate_init)
ctx->zfree = php_zlib_free;
((php_zlib_context *) ctx)->inflateDict = dict;
((php_zlib_context *) ctx)->inflateDictlen = dictlen;
((php_zlib_context *) ctx)->status = Z_OK;
if (encoding < 0) {
encoding += 15 - window;
@ -935,6 +936,13 @@ PHP_FUNCTION(inflate_add)
"flush mode must be ZLIB_NO_FLUSH, ZLIB_PARTIAL_FLUSH, ZLIB_SYNC_FLUSH, ZLIB_FULL_FLUSH, ZLIB_BLOCK or ZLIB_FINISH");
RETURN_FALSE;
}
/* Lazy-resetting the zlib stream so ctx->total_in remains available until the next inflate_add() call. */
if (((php_zlib_context *) ctx)->status == Z_STREAM_END)
{
((php_zlib_context *) ctx)->status = Z_OK;
inflateReset(ctx);
}
if (in_len <= 0 && flush_type != Z_FINISH) {
RETURN_EMPTY_STRING();
@ -950,6 +958,8 @@ PHP_FUNCTION(inflate_add)
status = inflate(ctx, flush_type);
buffer_used = ZSTR_LEN(out) - ctx->avail_out;
((php_zlib_context *) ctx)->status = status; /* Save status for exposing to userspace */
switch (status) {
case Z_OK:
if (ctx->avail_out == 0) {
@ -962,7 +972,6 @@ PHP_FUNCTION(inflate_add)
goto complete;
}
case Z_STREAM_END:
inflateReset(ctx);
goto complete;
case Z_BUF_ERROR:
if (flush_type == Z_FINISH && ctx->avail_out == 0) {
@ -1011,6 +1020,48 @@ PHP_FUNCTION(inflate_add)
}
/* }}} */
/* {{{ proto bool inflate_get_status(resource context)
Get decompression status, usually returns either ZLIB_OK or ZLIB_STREAM_END. */
PHP_FUNCTION(inflate_get_status)
{
zval *res;
z_stream *ctx;
if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "r", &res))
{
RETURN_NULL();
}
if (!(ctx = zend_fetch_resource_ex(res, NULL, le_inflate))) {
php_error_docref(NULL, E_WARNING, "Invalid zlib.inflate resource");
RETURN_FALSE;
}
RETURN_LONG(((php_zlib_context *) ctx)->status);
}
/* }}} */
/* {{{ proto bool inflate_get_read_len(resource context)
Get number of bytes read so far. */
PHP_FUNCTION(inflate_get_read_len)
{
zval *res;
z_stream *ctx;
if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "r", &res))
{
RETURN_NULL();
}
if (!(ctx = zend_fetch_resource_ex(res, NULL, le_inflate))) {
php_error_docref(NULL, E_WARNING, "Invalid zlib.inflate resource");
RETURN_FALSE;
}
RETURN_LONG(ctx->total_in);
}
/* }}} */
/* {{{ proto resource deflate_init(int encoding[, array options])
Initialize an incremental deflate context using the specified encoding */
PHP_FUNCTION(deflate_init)
@ -1322,6 +1373,14 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_inflate_add, 0, 0, 2)
ZEND_ARG_INFO(0, flush_behavior)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_inflate_get_status, 0, 0, 1)
ZEND_ARG_INFO(0, resource)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_inflate_get_read_len, 0, 0, 1)
ZEND_ARG_INFO(0, resource)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ php_zlib_functions[] */
@ -1354,6 +1413,8 @@ static const zend_function_entry php_zlib_functions[] = {
PHP_FE(deflate_add, arginfo_deflate_add)
PHP_FE(inflate_init, arginfo_inflate_init)
PHP_FE(inflate_add, arginfo_inflate_add)
PHP_FE(inflate_get_status, arginfo_inflate_get_status)
PHP_FE(inflate_get_read_len, arginfo_inflate_get_read_len)
PHP_FE(ob_gzhandler, arginfo_ob_gzhandler)
PHP_FE_END
};
@ -1469,6 +1530,16 @@ static PHP_MINIT_FUNCTION(zlib)
REGISTER_STRING_CONSTANT("ZLIB_VERSION", ZLIB_VERSION, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("ZLIB_VERNUM", ZLIB_VERNUM, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("ZLIB_OK", Z_OK, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("ZLIB_STREAM_END", Z_STREAM_END, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("ZLIB_NEED_DICT", Z_NEED_DICT, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("ZLIB_ERRNO", Z_ERRNO, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("ZLIB_STREAM_ERROR", Z_STREAM_ERROR, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("ZLIB_DATA_ERROR", Z_DATA_ERROR, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("ZLIB_MEM_ERROR", Z_MEM_ERROR, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("ZLIB_BUF_ERROR", Z_BUF_ERROR, CONST_CS|CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("ZLIB_VERSION_ERROR", Z_VERSION_ERROR, CONST_CS|CONST_PERSISTENT);
REGISTER_INI_ENTRIES();
return SUCCESS;
}