DotZlib - Inflate error handling code 1 Z_STREAM_END
Question
I am using a slightly modified version of the DotZlib which is part of the contrib directory with the zlib source code to inflate a realtime data stream.
Instead of the regular inflateInit I need to use InflateInit2 - but this the only difference to the provided library.
Netherlesse after a couple of reads I receive error code 1 from the zlib and do not manage to recover when adding bytes.
The original code from the zlib contrib directory is:
public override void Add(byte[] data, int offset, int count)
{
if (data == null) throw new ArgumentNullException();
if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException()
;
if ((offset+count) > data.Length) throw new ArgumentException();
int total = count;
int inputIndex = offset;
int err = 0;
while (err >= 0 && inputIndex < total)
{
copyInput(data, inputIndex, Math.Min(total - inputIndex, kBufferSize));
err = inflate(ref _ztream, (int)FlushTypes.None);
if (err == 0)
while (_ztream.avail_out == 0)
{
OnDataAvailable();
err = inflate(ref _ztream, (int)FlushTypes.None);
}
inputIndex += (int)_ztream.total_in;
}
setChecksum( _ztream.adler );
}
BTW Does anyone know how to contribute improved code? The implementation is nicely designed, but incomplete from my point of view.
Solution
I think that
err = inflate(ref _ztream, (int)FlushTypes.None);
if (err == 0)
while (_ztream.avail_out == 0)
{
OnDataAvailable();
err = inflate(ref _ztream, (int)FlushTypes.None);
}
should be
while (_ztream.avail_in > 0)
{
err = inflate(ref _ztream, (int)FlushTypes.None);
if (err!=0)
break;
OnDataAvailable();
}
There are two problems I see with the first version of the code:
- if inflate() produces data but does not produce enough data to make avail_out be 0, you won't call OnDataAvailable, even though data is available.
- you might call inflate() even though avail_in is 0, which I could easily imagine might produce a stream-end error.
(NB: I suspect that you know me in a professional capacity. This answer is given in a private capacity and is not related to my work for my employer.)