]> granicus.if.org Git - php/commitdiff
- MFH: fix bug #34821 zlib encoders fail on widely varying binary data on windows
authorMichael Wallner <mike@php.net>
Tue, 25 Oct 2005 15:16:20 +0000 (15:16 +0000)
committerMichael Wallner <mike@php.net>
Tue, 25 Oct 2005 15:16:20 +0000 (15:16 +0000)
NEWS
ext/zlib/tests/bug_34821.phpt [new file with mode: 0644]
ext/zlib/zlib.c

diff --git a/NEWS b/NEWS
index f092130a2f386a0eba0d92364842c4f7accf234d..829a8e68305b728f4af42cdd6a1a94e9f48f652c 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -11,6 +11,8 @@ PHP                                                                        NEWS
 - Fixed bug #34893 (PHP5.1 overloading, Cannot access private property).
   (Dmitry)
 - Fixed bug #34899 (Fixed sqlite extension compile failure). (Ilia)
+- Fixed bug #34821 (zlib encoders fail on widely varying binary data on windows)
+  (Mike, Ilia)
 - Fixed bug #34782 (token_get_all() gives wrong result). (Dmitry)
 - Fixed bug #34767 (Zend Engine 1 Compatibility not copying objects correctly).
   (Dmitry)
diff --git a/ext/zlib/tests/bug_34821.phpt b/ext/zlib/tests/bug_34821.phpt
new file mode 100644 (file)
index 0000000..ff845c6
--- /dev/null
@@ -0,0 +1,41 @@
+--TEST--
+bug 34821
+--SKIPIF--
+<?php if (!extension_loaded("zlib")) print "skip"; ?>
+--FILE--
+<?php
+
+// test 50 bytes to 500k
+$b = array(
+       50, 
+       500, 
+       5000, 
+       500000,
+//     1000000, // works, but test would take too long
+);
+
+srand(time());
+
+foreach ($b as $size) {
+       $s = '';
+       for ($i = 0; $i <= $size; ++$i) {
+               $s .= chr(rand(0,255));
+       }
+       var_dump($s == gzinflate(gzdeflate($s)));
+       var_dump($s == gzuncompress(gzcompress($s)));
+       var_dump($s == gzinflate(substr(gzencode($s), 10, -8)));
+}
+?>
+--EXPECT--
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
index 20f2258c5c63adff41b5e5d4cb380ae4fc1a7153..a6b19d5ce6fa1f0917702f137aae43da2ab62b7f 100644 (file)
 #endif
 #endif
 
+/* Win32 needs some more memory */
+#ifdef PHP_WIN32
+#define PHP_ZLIB_MODIFIER 100
+#else
+#define PHP_ZLIB_MODIFIER 1000
+#endif
+
 #define OS_CODE                        0x03 /* FIXME */
 #define GZIP_HEADER_LENGTH             10
 #define GZIP_FOOTER_LENGTH             8
@@ -386,7 +393,7 @@ PHP_FUNCTION(gzcompress)
                RETURN_FALSE;
        }
 
-       l2 = data_len + (data_len / 1000) + 15 + 1; /* room for \0 */
+       l2 = data_len + (data_len / PHP_ZLIB_MODIFIER) + 15 + 1; /* room for \0 */
        s2 = (char *) emalloc(l2);
        if (!s2) {
                RETURN_FALSE;
@@ -482,7 +489,7 @@ PHP_FUNCTION(gzdeflate)
        stream.next_in = (Bytef *) data;
        stream.avail_in = data_len;
 
-       stream.avail_out = stream.avail_in + (stream.avail_in / 1000) + 15 + 1; /* room for \0 */
+       stream.avail_out = stream.avail_in + (stream.avail_in / PHP_ZLIB_MODIFIER) + 15 + 1; /* room for \0 */
 
        s2 = (char *) emalloc(stream.avail_out);
        if (!s2) {
@@ -618,7 +625,7 @@ static int php_do_deflate(uint str_length, Bytef **p_buffer, uint *p_buffer_len,
        int start_offset = ((do_start && ZLIBG(compression_coding) == CODING_GZIP) ? 10 : 0);
        int end_offset = (do_end ? 8 : 0);
 
-       outlen = (uint) (sizeof(char) * (str_length * 1.001f + 12) + 1); /* leave some room for a trailing \0 */
+       outlen = (uint) (sizeof(char) * (str_length / PHP_ZLIB_MODIFIER + 12) + 1); /* leave some room for a trailing \0 */
        if ((outlen + start_offset + end_offset) > *p_buffer_len) {
                buffer = (Bytef *) emalloc(outlen + start_offset + end_offset);
        } else {
@@ -758,7 +765,7 @@ PHP_FUNCTION(gzencode)
        stream.next_in = (Bytef *) data;
        stream.avail_in = data_len;
 
-       stream.avail_out = stream.avail_in + (stream.avail_in / 1000) + 15 + 1; /* room for \0 */
+       stream.avail_out = stream.avail_in + (stream.avail_in / PHP_ZLIB_MODIFIER) + 15 + 1; /* room for \0 */
        s2 = (char *) emalloc(stream.avail_out + GZIP_HEADER_LENGTH + (coding == CODING_GZIP ? GZIP_FOOTER_LENGTH : 0));
 
        /* add gzip file header */