Created
September 28, 2017 09:04
-
-
Save gitlost/e9895e7687b153c860cddbb1bfec5933 to your computer and use it in GitHub Desktop.
php_zlib_inflate_filter() returning 0 bytes_consumed in certain circumstances.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function non_repeating_str( $len = 8192 ) { | |
| $ret = ''; | |
| mt_srand( 1 ); | |
| $iterations = (int) ( $len / 256 ) + 1; | |
| for ( $i = 0; $i < $iterations; $i++ ) { | |
| $haves = array(); | |
| $cnt = 0; | |
| while ( $cnt < 256 ) { | |
| $j = mt_rand( 0, 255 ); | |
| if ( ! isset( $haves[ $j ] ) ) { | |
| $haves[ $j ] = $j; | |
| $cnt++; | |
| $ret .= chr( $j ); | |
| } | |
| } | |
| } | |
| return substr( $ret, 0, $len ); | |
| } | |
| $base_len = 32768 - 23 /*overhead*/; | |
| $stream = fopen( 'php://memory', 'rb+' ); | |
| // Fails for 1 <= $i <= 8. | |
| for ( $i = -16; $i <= 16; $i++ ) { | |
| $in_data = non_repeating_str( $base_len + $i ); | |
| $deflate_filter = stream_filter_append( $stream, 'zlib.deflate', STREAM_FILTER_WRITE, array( 'window' => 16 + 15 ) ); | |
| rewind( $stream ); | |
| fwrite( $stream, $in_data ); | |
| stream_filter_remove( $deflate_filter ); | |
| rewind( $stream ); | |
| $out_data = stream_get_contents( $stream ); | |
| $out_data_len = strlen( $out_data ); | |
| $inflate_filter = stream_filter_prepend( $stream, 'zlib.inflate', STREAM_FILTER_WRITE, array( 'window' => 16 + 15 ) ); | |
| rewind( $stream ); | |
| $fwrite_len = fwrite( $stream, $out_data ); | |
| stream_filter_remove( $inflate_filter ); | |
| if ( $out_data_len !== $fwrite_len ) { | |
| echo "bug i=$i out_data_len=$out_data_len fwrite_len=$fwrite_len\n"; | |
| } | |
| } | |
| fclose( $stream ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment