|
-
May 7th, 2011, 06:20 AM
#1
Strange zlib problem - works in one part, fails in chunks
Hello There,
I looked for a specific zlib forum but could not find anything useful as well as looking through the web a searching codeguru even; I just hope that the answer is out there.
I have a simple HTTP client code what receives data from webservers. The nasty stuff comes with gzip encoding, when there is "Content-Encoding: gzip" in the header field. It was quite a bit of struggle for me to work it out and now it is more or less okay. More or less, because if the data coming is small enough to fit into the out buffer then it is all fine; however it fails on the second chunk if a smaller buffer is set or data is bigger than OBufferSize.
Any idea or a better implementation suggestion (with code example) is much appreciated.
Code:
// Size is how much data we have in char* pIn
const int OBufferSize = 1024;
z_stream cmpr_stream;
cmpr_stream.total_in = 0;
cmpr_stream.total_out = 0;
cmpr_stream.zalloc = Z_NULL;
if (inflateInit2(&cmpr_stream, -8) != Z_OK) {
qDebug() << "cmpr_stream error!";
}
QByteArray uncompressed;
int RetVal;
while (cmpr_stream.total_in < (unsigned int)Size) {
cmpr_stream.next_out = buffer;
cmpr_stream.avail_out = OBufferSize;
cmpr_stream.next_in = (unsigned char *)(pIn + cmpr_stream.total_in);
cmpr_stream.avail_in = Size - cmpr_stream.total_in;
int RetVal = inflate(&cmpr_stream, Z_SYNC_FLUSH);
// Check decompression result
if (RetVal == Z_OK || RetVal == Z_STREAM_END) {
uncompressed.append(QByteArray::fromRawData((char *)buffer, cmpr_stream.total_out));
} else {
qCritical() << "zlib uncompression failed, code: " << RetVal << " for mem to mem";
inflateEnd(&cmpr_stream);
break;
}
if (RetVal == Z_STREAM_END) {
inflateEnd(&cmpr_stream);
break;
}
}
delete buffer;
Last edited by luftwaffe; May 7th, 2011 at 06:22 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|