- initialize default compression level
authorMichael Wallner <mike@php.net>
Mon, 10 Oct 2005 19:43:05 +0000 (19:43 +0000)
committerMichael Wallner <mike@php.net>
Mon, 10 Oct 2005 19:43:05 +0000 (19:43 +0000)
- add the functions to the function entries ...
- add test
- fix buffer errors when encoding much widely differing (binary) data

http.c
http_encoding_api.c
http_functions.c
tests/encodings.phpt [new file with mode: 0644]

diff --git a/http.c b/http.c
index 26bcf0f1e35580c0bff50a590f380ba644472801..060d55b96524c9535713bc3ce4f515ae903b7c0a 100644 (file)
--- 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
 };
index a1893df44395e0b2de4472ce6a3e05f4ed030406..f0328cdabfd4832978b26d0afd83967ba8c561f0 100644 (file)
@@ -95,8 +95,12 @@ PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t enco
 #ifdef HTTP_HAVE_ZLIB
 #include <zlib.h>
 
+/* 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;
 }
index d205ea26567f10a5106f68b054fe21bb0df26d57..88aefb47ae33ec54521a645c3f08d27c467a65ab 100644 (file)
@@ -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 (file)
index 0000000..e66eabd
--- /dev/null
@@ -0,0 +1,58 @@
+--TEST--
+encodings
+--SKIPIF--
+<?php
+include 'skip.inc';
+skipif(!function_exists('http_gzencode'), 'need zlib');
+?>
+--FILE--
+<?php
+echo "-TEST\n";
+
+error_reporting(E_ALL);
+
+$s = '';
+
+srand(time());
+for ($i = 0; $i < 1000000; $i++) {
+       $s .= chr(rand(0,255));
+}
+
+/* cannot test ext/zlib against this generated data, 
+   because it will fail with such widely differing binary data */
+
+var_dump($s == http_gzdecode(http_gzencode($s)));
+var_dump($s == http_inflate(http_deflate($s)));
+var_dump($s == http_uncompress(http_compress($s)));
+
+
+$s = "A simple test string, which won't blow up ext/zlib.\n";
+
+var_dump($s == http_gzdecode(gzencode($s)));
+var_dump($s == http_inflate(gzdeflate($s)));
+var_dump($s == http_uncompress(gzcompress($s)));
+
+/* no gzdecode in ext/zlib
+var_dump($s == gzdecode(http_gzencode($s))); */
+var_dump($s == gzinflate(http_deflate($s)));
+var_dump($s == gzuncompress(http_compress($s)));
+
+if (extension_loaded('zlib')) {
+       (gzencode($s) == http_gzencode($s)) or print "GZIP Failed\n";
+       (gzdeflate($s) == http_deflate($s)) or print "DEFLATE Failed\n";
+       (gzcompress($s) == http_compress($s)) or print "COMPRESS Failed\n";
+}
+
+echo "Done\n";
+?>
+--EXPECTF--
+%sTEST
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+Done