1
votes

Using zlib version 1.2.7 I've encoutered a strange issue: call of inflate() fails with error -3 Z_DATA_ERROR and message "incorrect header check". The data was compressed by using deflate() before this. Here is the simple example of this error. What's wrong with it?

    std::string testWord = "Hello world";
const int buffSize = 1000;
byte buffer[buffSize];
byte outBuffer[buffSize];
z_stream readStream, writeStream;

readStream.zalloc = Z_NULL;
readStream.zfree = Z_NULL;
readStream.opaque = Z_NULL;
readStream.avail_in = 0;
readStream.next_in = Z_NULL;

writeStream.zalloc = Z_NULL;
writeStream.zfree = Z_NULL;
writeStream.opaque = Z_NULL;
writeStream.avail_in = 0;
writeStream.next_in = Z_NULL;

int rez = inflateInit(&readStream);
if (rez != Z_OK)
    std::cout << "InflateInit returned error " << rez;

rez = deflateInit(&writeStream, Z_DEFAULT_COMPRESSION);
if (rez != Z_OK)
    std::cout << "deflateInit returned error " << rez;

writeStream.next_in = (byte*)testWord.c_str();
writeStream.avail_in = testWord.size();
writeStream.next_out = (Bytef*) buffer;
writeStream.avail_out = buffSize;

rez = deflate(&writeStream, Z_FINISH);
if (rez != Z_STREAM_END)
    std::cout << "deflate returned error " << rez;

rez = deflateEnd(&writeStream);
if (rez != Z_OK)
    std::cout << "deflateEnd returned error " << rez;

readStream.next_in = buffer;
readStream.avail_in = buffSize - writeStream.avail_out;
readStream.next_out = outBuffer;
readStream.avail_out = buffSize;

rez = inflate(&readStream, Z_FINISH);
if (rez != Z_STREAM_END)
{
    std::cout << "inflate returned error " << rez << " msg: " << readStream.msg;
    return;
}
1

1 Answers

1
votes

This looks like a bug:

readStream.avail_out = 0;

It should probably read:

readStream.avail_out = buffSize;