[ruby-talk:444678] Re: Zlib bug or intended behaviour?

On 2013-10-9 6:22 pm, Andreas Schmidt wrote:
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?
You'll probably need to check zlib more than in Ruby... but my _guess_ is that when you add the bytes and call inflate, it expects the data to be well-formed incl the CRC. However, when you stream it in, it is probably deflating the stream as it gets it but when you don't supply the CRC, it is able to return what was inflated, but is unable to confirm if the data is actually correct since the CRC has not been verified. In fact, if you see this function at https://www.zlib.net/manual.html: ZEXTERN int ZEXPORT inflate(z_streamp strm, int flush);
inflate decompresses as much data as possible, and stops when the input buffer becomes empty or the output buffer becomes full. It may introduce some output latency (reading input without producing any output) except when forced to flush.
It adds:
If a preset dictionary is needed after this call (see inflateSetDictionary below), inflate sets strm->adler to the Adler-32 checksum of the dictionary chosen by the compressor and returns Z_NEED_DICT; otherwise it sets strm->adler to the Adler-32 checksum of all output produced so far (that is, total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described below. At the end of the stream, inflate() checks that its computed Adler-32 checksum is equal to that saved by the compressor and returns Z_STREAM_END only if the checksum is correct.
So, if it does not know that the stream has ended, it does not confirm that checksum but returns data up to that point (till the end, in your case). Hope this helps. Best regards, Mohit.
participants (1)
-
Mohit Sindhwani