From 72f46a34b8aed7ec9ce1a31b111f3bf35075ffa6 Mon Sep 17 00:00:00 2001 From: Xinchen Hui Date: Mon, 5 May 2014 15:51:55 +0800 Subject: [PATCH] Refactor zlib (all tests pass) --- ext/standard/image.c | 19 ++++---- ext/zlib/tests/gzclose_basic.phpt | 4 +- ext/zlib/tests/gzeof_variation1.phpt | 4 +- ext/zlib/zlib.c | 66 ++++++++++++++-------------- ext/zlib/zlib_filter.c | 41 ++++++++--------- 5 files changed, 65 insertions(+), 69 deletions(-) diff --git a/ext/standard/image.c b/ext/standard/image.c index 8b51688bd6..4dcfe83e38 100644 --- a/ext/standard/image.c +++ b/ext/standard/image.c @@ -202,11 +202,12 @@ static struct gfxinfo *php_handle_swc(php_stream * stream TSRMLS_DC) long bits; unsigned char a[64]; unsigned long len=64, szlength; - int factor=1,maxfactor=16; - int slength, status=0; - char *b, *buf=NULL, *bufz=NULL; + int factor = 1,maxfactor = 16; + int status = 0; + char *b, *buf = NULL; + zend_string *bufz; - b = ecalloc (1, len + 1); + b = ecalloc(1, len + 1); if (php_stream_seek(stream, 5, SEEK_CUR)) return NULL; @@ -219,7 +220,7 @@ static struct gfxinfo *php_handle_swc(php_stream * stream TSRMLS_DC) if (php_stream_seek(stream, 8, SEEK_SET)) return NULL; - slength = php_stream_copy_to_mem(stream, &bufz, PHP_STREAM_COPY_ALL, 0); + bufz = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0); /* * zlib::uncompress() wants to know the output data length @@ -230,13 +231,13 @@ static struct gfxinfo *php_handle_swc(php_stream * stream TSRMLS_DC) */ do { - szlength=slength*(1<len * (1<val, bufz->len); } while ((status==Z_BUF_ERROR)&&(factorval; Z.avail_in = in_len; - Z.avail_out = *out_len; + Z.avail_out = out->len; status = deflate(&Z, Z_FINISH); deflateEnd(&Z); if (Z_STREAM_END == status) { /* size buffer down to actual length */ - *out_buf = erealloc(*out_buf, Z.total_out + 1); - (*out_buf)[*out_len = Z.total_out] = '\0'; - return SUCCESS; + out = STR_REALLOC(out, Z.total_out, 0); + out->val[out->len] = '\0'; + return out; } else { - efree(*out_buf); + STR_FREE(out); } } - *out_buf = NULL; - *out_len = 0; - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", zError(status)); - return FAILURE; + return NULL; } /* }}} */ @@ -501,7 +500,7 @@ static PHP_FUNCTION(ob_gzhandler) } if (ctx.out.data) { - RETVAL_STRINGL(ctx.out.data, ctx.out.used, 1); + RETVAL_STRINGL(ctx.out.data, ctx.out.used); if (ctx.out.free) { efree(ctx.out.data); } @@ -520,9 +519,9 @@ static PHP_FUNCTION(zlib_get_coding_type) } switch (ZLIBG(compression_coding)) { case PHP_ZLIB_ENCODING_GZIP: - RETURN_STRINGL("gzip", sizeof("gzip") - 1, 1); + RETURN_STRINGL("gzip", sizeof("gzip") - 1); case PHP_ZLIB_ENCODING_DEFLATE: - RETURN_STRINGL("deflate", sizeof("deflate") - 1, 1); + RETURN_STRINGL("deflate", sizeof("deflate") - 1); default: RETURN_FALSE; } @@ -631,17 +630,15 @@ static PHP_FUNCTION(readgzfile) #define PHP_ZLIB_ENCODE_FUNC(name, default_encoding) \ static PHP_FUNCTION(name) \ { \ - char *in_buf, *out_buf; \ - int in_len; \ - size_t out_len; \ + zend_string *in, *out; \ long level = -1; \ long encoding = default_encoding; \ if (default_encoding) { \ - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &in_buf, &in_len, &level, &encoding)) { \ + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S|ll", &in, &level, &encoding)) { \ return; \ } \ } else { \ - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|l", &in_buf, &in_len, &encoding, &level)) { \ + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Sl|l", &in, &encoding, &level)) { \ return; \ } \ } \ @@ -658,10 +655,10 @@ static PHP_FUNCTION(name) \ php_error_docref(NULL TSRMLS_CC, E_WARNING, "encoding mode must be either ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP or ZLIB_ENCODING_DEFLATE"); \ RETURN_FALSE; \ } \ - if (SUCCESS != php_zlib_encode(in_buf, in_len, &out_buf, &out_len, encoding, level TSRMLS_CC)) { \ + if ((out = php_zlib_encode(in->val, in->len, encoding, level TSRMLS_CC)) == NULL) { \ RETURN_FALSE; \ } \ - RETURN_STRINGL(out_buf, out_len, 0); \ + RETURN_STR(out); \ } #define PHP_ZLIB_DECODE_FUNC(name, encoding) \ @@ -681,7 +678,8 @@ static PHP_FUNCTION(name) \ if (SUCCESS != php_zlib_decode(in_buf, in_len, &out_buf, &out_len, encoding, max_len TSRMLS_CC)) { \ RETURN_FALSE; \ } \ - RETURN_STRINGL(out_buf, out_len, 0); \ + RETVAL_STRINGL(out_buf, out_len); \ + efree(out_buf); \ } /* {{{ proto binary zlib_encode(binary data, int encoding[, int level = -1]) diff --git a/ext/zlib/zlib_filter.c b/ext/zlib/zlib_filter.c index 9a59c3a855..17f141b06c 100644 --- a/ext/zlib/zlib_filter.c +++ b/ext/zlib/zlib_filter.c @@ -66,12 +66,12 @@ static php_stream_filter_status_t php_zlib_inflate_filter( int status; php_stream_filter_status_t exit_status = PSFS_FEED_ME; - if (!thisfilter || !thisfilter->abstract) { + if (!thisfilter || !Z_PTR(thisfilter->abstract)) { /* Should never happen */ return PSFS_ERR_FATAL; } - data = (php_zlib_filter_data *)(thisfilter->abstract); + data = (php_zlib_filter_data *)(Z_PTR(thisfilter->abstract)); while (buckets_in->head) { size_t bin = 0, desired; @@ -156,8 +156,8 @@ static php_stream_filter_status_t php_zlib_inflate_filter( static void php_zlib_inflate_dtor(php_stream_filter *thisfilter TSRMLS_DC) { - if (thisfilter && thisfilter->abstract) { - php_zlib_filter_data *data = thisfilter->abstract; + if (thisfilter && Z_PTR(thisfilter->abstract)) { + php_zlib_filter_data *data = Z_PTR(thisfilter->abstract); if (!data->finished) { inflateEnd(&(data->strm)); } @@ -191,12 +191,12 @@ static php_stream_filter_status_t php_zlib_deflate_filter( int status; php_stream_filter_status_t exit_status = PSFS_FEED_ME; - if (!thisfilter || !thisfilter->abstract) { + if (!thisfilter || !Z_PTR(thisfilter->abstract)) { /* Should never happen */ return PSFS_ERR_FATAL; } - data = (php_zlib_filter_data *)(thisfilter->abstract); + data = (php_zlib_filter_data *)(Z_PTR(thisfilter->abstract)); while (buckets_in->head) { size_t bin = 0, desired; @@ -265,8 +265,8 @@ static php_stream_filter_status_t php_zlib_deflate_filter( static void php_zlib_deflate_dtor(php_stream_filter *thisfilter TSRMLS_DC) { - if (thisfilter && thisfilter->abstract) { - php_zlib_filter_data *data = thisfilter->abstract; + if (thisfilter && Z_PTR(thisfilter->abstract)) { + php_zlib_filter_data *data = Z_PTR(thisfilter->abstract); deflateEnd(&(data->strm)); pefree(data->inbuf, data->persistent); pefree(data->outbuf, data->persistent); @@ -324,15 +324,14 @@ static php_stream_filter *php_zlib_filter_create(const char *filtername, zval *f int windowBits = -MAX_WBITS; if (filterparams) { - zval **tmpzval; + zval *tmpzval; if ((Z_TYPE_P(filterparams) == IS_ARRAY || Z_TYPE_P(filterparams) == IS_OBJECT) && - zend_hash_find(HASH_OF(filterparams), "window", sizeof("window"), (void **) &tmpzval) == SUCCESS) { + (tmpzval = zend_hash_str_find(HASH_OF(filterparams), "window", sizeof("window") - 1))) { zval tmp; /* log-2 base of history window (9 - 15) */ - tmp = **tmpzval; - zval_copy_ctor(&tmp); + ZVAL_DUP(&tmp, tmpzval); convert_to_long(&tmp); if (Z_LVAL(tmp) < -MAX_WBITS || Z_LVAL(tmp) > MAX_WBITS + 32) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid parameter give for window size. (%ld)", Z_LVAL(tmp)); @@ -354,7 +353,7 @@ static php_stream_filter *php_zlib_filter_create(const char *filtername, zval *f if (filterparams) { - zval **tmpzval, tmp; + zval *tmpzval, tmp; /* filterparams can either be a scalar value to indicate compression level (shortcut method) Or can be a hash containing one or more of 'window', 'memory', and/or 'level' members. */ @@ -362,9 +361,8 @@ static php_stream_filter *php_zlib_filter_create(const char *filtername, zval *f switch (Z_TYPE_P(filterparams)) { case IS_ARRAY: case IS_OBJECT: - if (zend_hash_find(HASH_OF(filterparams), "memory", sizeof("memory"), (void**) &tmpzval) == SUCCESS) { - tmp = **tmpzval; - zval_copy_ctor(&tmp); + if ((tmpzval = zend_hash_str_find(HASH_OF(filterparams), "memory", sizeof("memory") -1))) { + ZVAL_DUP(&tmp, tmpzval); convert_to_long(&tmp); /* Memory Level (1 - 9) */ @@ -375,9 +373,8 @@ static php_stream_filter *php_zlib_filter_create(const char *filtername, zval *f } } - if (zend_hash_find(HASH_OF(filterparams), "window", sizeof("window"), (void**) &tmpzval) == SUCCESS) { - tmp = **tmpzval; - zval_copy_ctor(&tmp); + if ((tmpzval = zend_hash_str_find(HASH_OF(filterparams), "window", sizeof("window") - 1))) { + ZVAL_DUP(&tmp, tmpzval); convert_to_long(&tmp); /* log-2 base of history window (9 - 15) */ @@ -388,8 +385,8 @@ static php_stream_filter *php_zlib_filter_create(const char *filtername, zval *f } } - if (zend_hash_find(HASH_OF(filterparams), "level", sizeof("level"), (void**) &tmpzval) == SUCCESS) { - tmp = **tmpzval; + if ((tmpzval = zend_hash_str_find(HASH_OF(filterparams), "level", sizeof("level") - 1))) { + ZVAL_COPY_VALUE(&tmp, tmpzval); /* Psuedo pass through to catch level validating code */ goto factory_setlevel; @@ -398,7 +395,7 @@ static php_stream_filter *php_zlib_filter_create(const char *filtername, zval *f case IS_STRING: case IS_DOUBLE: case IS_LONG: - tmp = *filterparams; + ZVAL_COPY_VALUE(&tmp, filterparams); factory_setlevel: zval_copy_ctor(&tmp); convert_to_long(&tmp); -- 2.40.0