Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

zlib inflate woes...

13 views
Skip to first unread message

barcaroller

unread,
Oct 22, 2009, 7:09:05 PM10/22/09
to
I have two buffers that I pass to zlib's inflate function. I know for a
fact that both buffers contain valid zlib content because tools like
wireshark are able to inflate them. Using my code below, the first one
deflates successfully but the second one doesn't. Does anyone know why?

First Buffer:

uchar in[] = { 0x78,0x9c,0x12,0x61,
0x60,0xe0,0xb9,0xf2,
0x9b,0x25,0xbd,0x75,
0xc2,0xcb,0xcf,0x79,
0x49,0xa7,0x32,0x01,
0x00,0x00,0x00,0xff,
0xff };

Deflates successfully (to 14 00 00 0c d4 fb 04 67 85 90 e9 f3 6e 62 ca 69)
even though the 3-bit zlib header is 011 (reserved/error).

Second Buffer:

uchar in[] = { 0x4a,0x49,0x2b,0x4e,
0x4f,0x49,0x03,0xa1,
0x62,0x08,0x05,0x44,
0x69,0xe9,0x5c,0x00,
0x00,0x00,0x00,0xff,
0xff };

Causes a zlib error ("incorrect header check") even though the 3-bit zlib
header is 010 (compressed with dynamic Huffman codes).


The code I used is:

z_stream istream_;

istream_.zalloc = Z_NULL;
istream_.zfree = Z_NULL;
istream_.opaque = Z_NULL;
istream_.next_in = Z_NULL;
istream_.next_out = Z_NULL;
istream_.avail_in = 0;
istream_.avail_out = 0;

int zReturn_ = inflateInit(&istream_);

if (zReturn_ != Z_OK)
{
// Error
}

uchar in[] = {
// see buffers above
};

uchar out[16000];

istream_.next_in = in;
istream_.avail_in = sizeof(in);
istream_.next_out = out;
istream_.avail_out = sizeof(out);

zReturn_ = inflate(&istream_, Z_SYNC_FLUSH);

if (zReturn_ != Z_OK)
{
// Error
}


I also tried inflate() with Z_FINISH. I then tried zlib's uncompress().
Nothing worked.


Mark Adler

unread,
Oct 23, 2009, 10:48:54 AM10/23/09
to
On 2009-10-22 16:09:05 -0700, "barcaroller" <barca...@music.net> said:
> Using my code below, the first one deflates successfully but the second
> one doesn't. Does anyone know why?

These are fragments of a zlib compressed stream. �The first one is from
the beginning of a zlib stream, the second is from the middle. �You can
use raw inflate for the second one -- look at inflateInit2().

It looks like the source of the data is using Z_FULL_FLUSH to break the
stream into pieces at byte boundaries and sending the pieces.

Mark

barcaroller

unread,
Oct 23, 2009, 5:54:44 PM10/23/09
to

"Mark Adler" <mad...@alumni.caltech.edu> wrote in message
news:2009102307485416807-madler@alumnicaltechedu...

> These are fragments of a zlib compressed stream. The first one is from the
> beginning of a zlib stream, the second is from the middle. You can use raw
> inflate for the second one -- look at inflateInit2().
>
> It looks like the source of the data is using Z_FULL_FLUSH to break the
> stream into pieces at byte boundaries and sending the pieces.


Thank you. You gave me a valuable hint. SSL/TLS breaks a stream into SSL
records and compresses each record individually. I was decompressing each
record individually, not realizing that the order was important (I was
deliberately skipping some of the records). zlib's inflate(Z_SYNC_FLUSH) now
works as expected. Thanks again for your time.

0 new messages