I'm trying to decompress a zip file using zlib (without using any extension or 3rd party). Initially, the src_len is 48756255, and the dest_len is 49209890. The first pass in the while loop is fine: err is Z_OK and the second pass through starts. On the second pass, no matter what I do I get a Z_BUF_ERROR from inflate. stream.total_out at this point is 49034460, so there is a bit remaining but stream.avail_in on the second pass is 0. In any case, I would expect inflate to give me Z_STREAM_END. I really don't know what's going on, can anyone help?
void compression::uncompress2(char* dest, unsigned dest_len, char* src, unsigned src_len) {
TempAllocator ta;
z_stream_s stream = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
stream.next_in = (Bytef*)src;
stream.avail_in = (uInt)src_len;
stream.next_out = (Bytef*)dest;
stream.avail_out = (uInt)dest_len;
stream.zalloc = zalloc;
stream.zfree = zfree;
stream.opaque = &ta;
// no header
int err = inflateInit2(&stream, -MAX_WBITS);
XENSURE(err == Z_OK);
bool done = false;
while (!done) {
stream.next_out = (Bytef*)(dest + stream.total_out);
stream.avail_out = dest_len - stream.total_out;
err = inflate(&stream, Z_SYNC_FLUSH);
if (err == Z_STREAM_END)
done = true;
else if (err != Z_OK) {
break;
}
}
err = inflateEnd(&stream);
XENSURE(err == Z_OK);
}