Generate 16+MAX_WBITS decompressable data

Marc Christiansen usenet at solar-empire.de
Tue Feb 12 15:39:47 EST 2013


Terry Reedy <tjreedy at udel.edu> wrote:
> On 2/12/2013 7:47 AM, Fayaz Yusuf Khan wrote:
>> dcomp = zlib.decompressobj(16+zlib.MAX_WBITS)
> 
> Since zlib.MAX_WBITS is the largest value that should be passed (15), 
> adding 16 makes no sense. Since it is also the default, there is also no 
> point in providing it explicitly. "Its absolute value should be between 
> 8 and 15 for the most recent versions of the zlib library".

The above code uses a feature of the zlib library which isn't really
widely known.
>From http://www.zlib.net/manual.html#Advanced (inflateInit2):
  windowBits can also be greater than 15 for optional gzip decoding. Add
  32 to windowBits to enable zlib and gzip decoding with automatic
  header detection, or add 16 to decode only the gzip format (the zlib
  format will return a Z_DATA_ERROR).


>> How do I generate the chunk here? From what I've been trying I'm getting
>> this exception:
>>>>> import zlib
>>>>> zlib.compress('hello')
>> 'x\x9c\xcbH\xcd\xc9\xc9\x07\x00\x06,\x02\x15'
>>>>> zlib.decompress(_, 16+zlib.MAX_WBITS)
>> Traceback (most recent call last):
>>    File "<stdin>", line 1, in <module>
>> zlib.error: Error -3 while decompressing data: incorrect header check

Try using a compressobj with 24 <= wbits < 32. It should work, but I
didn't try.

Marc



More information about the Python-list mailing list