Doc #68556 [NEW]: zlib.deflate is not the reverse of zlib.inflate

From: Date: Sat, 06 Dec 2014 07:14:59 +0000
Subject: Doc #68556 [NEW]: zlib.deflate is not the reverse of zlib.inflate
Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-11725@lists.php.net to get a copy of this message
From: salsi at icosaedro dot it Operating system: PHP version: Irrelevant Package: Streams related Bug Type: Documentation Problem Bug description:zlib.deflate is not the reverse of zlib.inflate Description: ------------ Example #1 at http://php.net/manual/it/filters.compression.php is wrong, in fact it does not work, and the read file is not decoded at all. The reason is that the zlib.deflate filter actually generates a ZLIB Compress format (RFC 1950), NOT a DEFLATE format (RFC 1951), while zlib.inflate decompresses the DEFLATE, NOT the ZLIB Compress format; these two stream filters are not one the reverse of the other. So the situation is quite asymmetrical: DEFLATE compressed format (RFC 1951): gzdeflate() --> compress gzinflate() and zlib.inflate --> decompress ZLIB Compress format (RFC 1950: 2 bytes header + DEFLATE + ADLER32): gzcompress() and zlib.deflate --> compress gzuncompress() --> decompress That's why in some users' comments one may find the strange recipe that states "zlib.inflate works on DEFLATE compressed data, but you have to strip away the first two bytes": those two bytes are in fact the ZLIB Compress header, as the DEFLATE compressed data are actually ZLIB Compress; the final ADLER32 hash gets ignored by the DEFLATE decompressor anyway as garbage. I'm unsure if this is more a flawed API design than a doc issue. Test script: --------------- Example #1 with my fix: <?php $params = array('level' => 6, 'window' => 15, 'memory' => 9); $original_text = "This is a test.\nThis is only a test.\nThis is not an important string.\n"; echo "The original text is " . strlen($original_text) . " characters long.\n"; $fp = fopen('test.deflated', 'w'); stream_filter_append($fp, 'zlib.deflate', STREAM_FILTER_WRITE, $params); fwrite($fp, $original_text); fclose($fp); echo "The compressed file is " . filesize('test.deflated') . " bytes long.\n"; echo "The original text was:\n"; # WRONG: does not show the original message: ####/* Use readfile and zlib.inflate to decompress on the fly */ ####readfile('php://filter/zlib.inflate/resource=test.deflated'); # FIX: echo gzuncompress(file_get_contents("test.deflated")); /* Generates output: * * The original text is 70 characters long. * The compressed file is 56 bytes long. * The original text was: * This is a test. * This is only a test. * This is not an important string. * */ ?> -- Edit bug report at https://bugs.php.net/bug.php?id=68556&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68556&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68556&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68556&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=68556&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=68556&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=68556&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=68556&r=needscript Try newer version: https://bugs.php.net/fix.php?id=68556&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=68556&r=support Expected behavior: https://bugs.php.net/fix.php?id=68556&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=68556&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=68556&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=68556&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68556&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=68556&r=dst IIS Stability: https://bugs.php.net/fix.php?id=68556&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=68556&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=68556&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=68556&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=68556&r=mysqlcfg

« previous php.doc.bugs (#11725) next »