]> granicus.if.org Git - php/commitdiff
Refactor zlib (all tests pass)
authorXinchen Hui <laruence@gmail.com>
Mon, 5 May 2014 07:51:55 +0000 (15:51 +0800)
committerXinchen Hui <laruence@gmail.com>
Mon, 5 May 2014 07:52:09 +0000 (15:52 +0800)
ext/standard/image.c
ext/zlib/tests/gzclose_basic.phpt
ext/zlib/tests/gzeof_variation1.phpt
ext/zlib/zlib.c
ext/zlib/zlib_filter.c

index 8b51688bd6ba086e9bb3bbe8517d72e484432733..4dcfe83e389bb7aa3d13cff1c5a04be33584451c 100644 (file)
@@ -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<<factor++);
-                       buf = (char *) erealloc(buf,szlength);
-                       status = uncompress(buf, &szlength, bufz, slength);
+                       szlength = bufz->len * (1<<factor++);
+                       buf = (char *) erealloc(buf, szlength);
+                       status = uncompress(buf, &szlength, bufz->val, bufz->len);
                } while ((status==Z_BUF_ERROR)&&(factor<maxfactor));
                
                if (bufz) {
-                       pefree(bufz, 0);
+                       STR_RELEASE(bufz);
                }       
                
                if (status == Z_OK) {
index d61855bf9fcba207d9eb586204b7993c55197f81..acad3b2f077f905a5e3bf3fee3b0c144a8f1858b 100644 (file)
@@ -32,8 +32,8 @@ gzread($h, 20);
 --EXPECTF--
 bool(true)
 
-Warning: gzread(): %d is not a valid stream resource in %s on line %d
+Warning: gzread(): supplied resource is not a valid stream resource in %s on line %d
 bool(true)
 
-Warning: gzread(): %d is not a valid stream resource in %s on line %d
+Warning: gzread(): supplied resource is not a valid stream resource in %s on line %d
 ===DONE===
index 77a1eccb66bb32ae442aa0424411a739fc2be773..ba0d8e59323cf7d2fb69b76331f2a8ba74f8c8ea 100644 (file)
@@ -26,6 +26,6 @@ unlink($filename);
 bool(false)
 bool(false)
 
-Warning: gzeof(): %d is not a valid stream resource in %s on line %d
+Warning: gzeof(): supplied resource is not a valid stream resource in %s on line %d
 bool(false)
-===DONE===
\ No newline at end of file
+===DONE===
index 3753f98a67b136094bd879bb6adfa9cd7828a2c0..c38d6eca2cf2e9dddf357442e297f8ae4b93c351 100644 (file)
@@ -67,15 +67,18 @@ static int php_zlib_output_conflict_check(const char *handler_name, size_t handl
 /* {{{ php_zlib_output_encoding() */
 static int php_zlib_output_encoding(TSRMLS_D)
 {
-       zval **enc;
+       zval *enc;
 
        if (!ZLIBG(compression_coding)) {
-               zend_is_auto_global(ZEND_STRL("_SERVER") TSRMLS_CC);
-               if (PG(http_globals)[TRACK_VARS_SERVER] && SUCCESS == zend_hash_find(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_ACCEPT_ENCODING", sizeof("HTTP_ACCEPT_ENCODING"), (void *) &enc)) {
-                       convert_to_string(*enc);
-                       if (strstr(Z_STRVAL_PP(enc), "gzip")) {
+               zend_string *name = STR_INIT("_SERVER", sizeof("_SERVER") - 1, 0);
+               zend_is_auto_global(name TSRMLS_CC);
+               STR_RELEASE(name);
+               if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY &&
+                       (enc = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_ACCEPT_ENCODING", sizeof("HTTP_ACCEPT_ENCODING") - 1))) {
+                       convert_to_string(enc);
+                       if (strstr(Z_STRVAL_P(enc), "gzip")) {
                                ZLIBG(compression_coding) = PHP_ZLIB_ENCODING_GZIP;
-                       } else if (strstr(Z_STRVAL_PP(enc), "deflate")) {
+                       } else if (strstr(Z_STRVAL_P(enc), "deflate")) {
                                ZLIBG(compression_coding) = PHP_ZLIB_ENCODING_DEFLATE;
                        }
                }
@@ -276,7 +279,7 @@ static php_output_handler *php_zlib_output_handler_init(const char *handler_name
 /* {{{ php_zlib_output_compression_start() */
 static void php_zlib_output_compression_start(TSRMLS_D)
 {
-       zval *zoh;
+       zval zoh;
        php_output_handler *h;
 
        switch (ZLIBG(output_compression)) {
@@ -290,9 +293,8 @@ static void php_zlib_output_compression_start(TSRMLS_D)
                                        (h = php_zlib_output_handler_init(ZEND_STRL(PHP_ZLIB_OUTPUT_HANDLER_NAME), ZLIBG(output_compression), PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC)) &&
                                        (SUCCESS == php_output_handler_start(h TSRMLS_CC))) {
                                if (ZLIBG(output_handler) && *ZLIBG(output_handler)) {
-                                       MAKE_STD_ZVAL(zoh);
-                                       ZVAL_STRING(zoh, ZLIBG(output_handler), 1);
-                                       php_output_start_user(zoh, ZLIBG(output_compression), PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC);
+                                       ZVAL_STRING(&zoh, ZLIBG(output_handler));
+                                       php_output_start_user(&zoh, ZLIBG(output_compression), PHP_OUTPUT_HANDLER_STDFLAGS TSRMLS_CC);
                                        zval_ptr_dtor(&zoh);
                                }
                        }
@@ -302,42 +304,39 @@ static void php_zlib_output_compression_start(TSRMLS_D)
 /* }}} */
 
 /* {{{ php_zlib_encode() */
-static int php_zlib_encode(const char *in_buf, size_t in_len, char **out_buf, size_t *out_len, int encoding, int level TSRMLS_DC)
+static zend_string *php_zlib_encode(const char *in_buf, size_t in_len, int encoding, int level TSRMLS_DC)
 {
        int status;
        z_stream Z;
+       zend_string *out;
 
        memset(&Z, 0, sizeof(z_stream));
        Z.zalloc = php_zlib_alloc;
        Z.zfree = php_zlib_free;
 
        if (Z_OK == (status = deflateInit2(&Z, level, Z_DEFLATED, encoding, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) {
-               *out_len = PHP_ZLIB_BUFFER_SIZE_GUESS(in_len);
-               *out_buf = emalloc(*out_len);
+               out = STR_ALLOC(PHP_ZLIB_BUFFER_SIZE_GUESS(in_len), 0);
 
                Z.next_in = (Bytef *) in_buf;
-               Z.next_out = (Bytef *) *out_buf;
+               Z.next_out = (Bytef *) out->val;
                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])
index 9a59c3a85505207c6bbe5a31eaa141204b1db459..17f141b06c2f0322e5e09a72f96f17187b79e82c 100644 (file)
@@ -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);