DotZlib - error handling code Inflate 1 Z_STREAM_END
I'm using a slightly modified version of DotZlib, which is part of the contrib directory with the zlib source code, to bloat the real-time data stream.
Instead of the usual inflateInit, I need to use InflateInit2 - but that's the only difference from the provided library.
Emptiness after multiple reads I get error code 1 from zlib and cannot recover when bytes are added.
Source code from zlib contrib directory:
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 better code? The implementation is well developed, but incomplete from my point of view.
a source to share
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);
}
it 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 () creates data but does not provide enough data to make avail_out 0, you will not call OnDataAvailable even if the data is available.
- you can call inflate (), although avail_in is 0, which I could easily imagine, there might be a flow error.
(NB: I suspect that you know me professionally. This answer is provided in a personal capacity and is not related to my job for my employer.)
a source to share