From 6541b0935ad5edc8e45b4a99c5e3f67812489bca Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Mon, 10 Oct 2005 19:43:05 +0000 Subject: [PATCH] - initialize default compression level - add the functions to the function entries ... - add test - fix buffer errors when encoding much widely differing (binary) data --- http.c | 8 ++++++ http_encoding_api.c | 14 +++++++---- http_functions.c | 4 +-- tests/encodings.phpt | 58 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 tests/encodings.phpt diff --git a/http.c b/http.c index 26bcf0f..060d55b 100644 --- a/http.c +++ b/http.c @@ -114,6 +114,14 @@ zend_function_entry http_functions[] = { PHP_FE(http_build_query, NULL) #endif PHP_FE(ob_etaghandler, NULL) +#ifdef HTTP_HAVE_ZLIB + PHP_FE(http_gzencode, NULL) + PHP_FE(http_gzdecode, NULL) + PHP_FE(http_deflate, NULL) + PHP_FE(http_inflate, NULL) + PHP_FE(http_compress, NULL) + PHP_FE(http_uncompress, NULL) +#endif EMPTY_FUNCTION_ENTRY }; diff --git a/http_encoding_api.c b/http_encoding_api.c index a1893df..f0328cd 100644 --- a/http_encoding_api.c +++ b/http_encoding_api.c @@ -95,8 +95,12 @@ PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t enco #ifdef HTTP_HAVE_ZLIB #include +/* max count of uncompress trials, alloc_size <<= 2 for each try */ #define HTTP_GZMAXTRY 10 -#define HTTP_GZBUFLEN(l) (l + (l / 1000) + 16 + 1) +/* safe padding */ +#define HTTP_GZSAFPAD 10 +/* add 1% extra space in case we need to encode widely differing (binary) data */ +#define HTTP_GZBUFLEN(l) (l + (l / 100) + HTTP_GZSAFPAD) static const char http_gzencode_header[] = { (const char) 0x1f, @@ -114,9 +118,9 @@ inline void http_init_gzencode_buffer(z_stream *Z, const char *data, size_t data Z->next_in = (Bytef *) data; Z->avail_in = data_len; - Z->avail_out = HTTP_GZBUFLEN(data_len) - 1; + Z->avail_out = HTTP_GZBUFLEN(data_len) + HTTP_GZSAFPAD - 1; - *buf_ptr = emalloc(Z->avail_out + sizeof(http_gzencode_header)); + *buf_ptr = emalloc(HTTP_GZBUFLEN(data_len) + sizeof(http_gzencode_header)); memcpy(*buf_ptr, http_gzencode_header, sizeof(http_gzencode_header)); Z->next_out = *buf_ptr + sizeof(http_gzencode_header); @@ -128,11 +132,11 @@ inline void http_init_deflate_buffer(z_stream *Z, const char *data, size_t data_ Z->zfree = Z_NULL; Z->opaque = Z_NULL; - Z->data_type = Z_ASCII; + Z->data_type = Z_UNKNOWN; Z->next_in = (Bytef *) data; Z->avail_in = data_len; Z->avail_out = HTTP_GZBUFLEN(data_len) - 1; - Z->next_out = emalloc(Z->avail_out); + Z->next_out = emalloc(HTTP_GZBUFLEN(data_len)); *buf_ptr = Z->next_out; } diff --git a/http_functions.c b/http_functions.c index d205ea2..88aefb4 100644 --- a/http_functions.c +++ b/http_functions.c @@ -1544,7 +1544,7 @@ PHP_FUNCTION(http_compress) { char *data; int data_len; - long level; + long level = -1; RETVAL_NULL(); @@ -1582,7 +1582,7 @@ PHP_FUNCTION(http_uncompress) size_t decoded_len; if (SUCCESS == http_encoding_uncompress(data, data_len, &decoded, &decoded_len)) { - RETURN_STRINGL(decoded, decoded_len, 0); + RETURN_STRINGL(decoded, (int) decoded_len, 0); } } } diff --git a/tests/encodings.phpt b/tests/encodings.phpt new file mode 100644 index 0000000..e66eabd --- /dev/null +++ b/tests/encodings.phpt @@ -0,0 +1,58 @@ +--TEST-- +encodings +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +%sTEST +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +Done -- 2.30.2