Zlib bug or intended behaviour?

Hi,

I recently had to inflate a malformed compressed (RFC1950) string. The
zlib magicbytes as well as the adler32-crc were missing.
I tried different ways to inflate the string. First I prepended the zlib
magic byte “\x78\x01” without success. 2nd I used the Zlib::Inflate
object and pipedin the magicbytes which worked to my surprise.
Can anybody tell me the difference of the internal behaviour of zlib, or
is it a bug?

Here’s the code which shows the different ways (tested on Ruby 1.9.3):

require ‘zlib’
zipped = Zlib.deflate(“Hello World!”)

cut off header and adler32

bad = zipped[2…-5]

begin
puts “* unzipping bad string the regular way …”
puts Zlib.inflate bad
rescue => bang
puts " >> Error"
puts bang
end

puts

begin
puts “* inflating bad string with prepended zlib header …”
prepended = “\x78\x01”
prepended << bad
puts Zlib.inflate prepended
rescue => bang
puts bang
end

puts

begin
puts “* inflating bad string the other way …”
header ="\x78\x01"
zs = Zlib::Inflate.new
zs << header
puts zs.inflate bad
rescue => bang
puts bang
exit
end
puts

puts “Why did this work?”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs