Beispiel #1
0
        private unsafe void RawWrite(byte *buffer, byte *pOutput, int count)
        {
#if DEBUG
            fixed(byte *bufferPointer = &_buffer[0])
            {
                Contracts.Assert(pOutput == bufferPointer);
            }
#endif
            Constants.RetCode ret;
            _zstrm.AvailIn = (uint)count;
            _zstrm.NextIn  = buffer;
            _zstrm.NextOut = pOutput + BufferUsed;
            do
            {
                RefreshOutput(pOutput);
                fixed(ZStream *pZstream = &_zstrm)
                {
                    ret = Zlib.deflate(pZstream, Constants.Flush.NoFlush);
                }

                if (ret != Constants.RetCode.OK)
                {
                    throw Contracts.Except("Zlib.deflate failed with {0}", ret);
                }
            } while (_zstrm.AvailIn > 0);
            _zstrm.NextIn = null;
        }
 protected override void Dispose(bool disposing)
 {
     if (_disposed)
     {
         return;
     }
     _disposed = true;
     Constants.RetCode ret;
     unsafe
     {
         fixed(ZStream *pZstream = &_zstrm)
         {
             ret = Zlib.inflateEnd(pZstream);
         }
     }
     base.Dispose(disposing);
     if (disposing)
     {
         GC.SuppressFinalize(this);
         if (ret != Constants.RetCode.OK)
         {
             throw Contracts.Except("Zlib inflateEnd failed with {0}", ret);
         }
     }
 }
Beispiel #3
0
        protected override void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }
            _disposed = true;

            Constants.RetCode disposeRet = Constants.RetCode.StreamEnd;
            if (disposing)
            {
                unsafe
                {
                    fixed(byte *pOutput = _buffer)
                    fixed(ZStream * pZstream = &_zstrm)
                    {
                        pZstream->AvailIn = 0;
                        pZstream->NextIn  = null;
                        pZstream->NextOut = pOutput + BufferUsed;
                        do
                        {
                            RefreshOutput(pOutput);
                            disposeRet = Zlib.deflate(pZstream, Constants.Flush.Finish);
                        } while (disposeRet == Constants.RetCode.OK);
                        if (disposeRet == Constants.RetCode.StreamEnd)
                        {
                            Flush();
                            _compressed.Flush();
                        }
                    }
                }
            }
            Constants.RetCode ret;
            unsafe
            {
                fixed(ZStream *pZstream = &_zstrm)
                {
                    ret = Zlib.deflateEnd(pZstream);
                }
            }
            base.Dispose(disposing);
            if (disposing)
            {
                GC.SuppressFinalize(this);
                if (disposeRet != Constants.RetCode.StreamEnd)
                {
                    throw Contracts.Except("Zlib deflate failed with {0}", disposeRet);
                }
                if (ret != Constants.RetCode.OK)
                {
                    throw Contracts.Except("Zlib deflateEnd failed with {0}", ret);
                }
            }
        }
 public ZInflateStream(Stream compressed, bool useZlibFormat = false)
 {
     Constants.RetCode ret;
     _compressed = compressed;
     _buffer     = new byte[1 << 15];
     unsafe
     {
         fixed(ZStream *pZstream = &_zstrm)
         {
             ret = Zlib.InflateInit2(pZstream, useZlibFormat ? Constants.MaxBufferSize : -Constants.MaxBufferSize);
         }
     }
     if (ret != Constants.RetCode.OK)
     {
         throw Contracts.Except("Could not initialize zstream. Error code: {0}", ret);
     }
     _bufferUsed = 0;
 }
Beispiel #5
0
 public ZDeflateStream(Stream compressed, Constants.Level level = Constants.Level.BestCompression,
                       Constants.Strategy strategy = Constants.Strategy.DefaultStrategy, int memLevel = 9,
                       bool useZlibFormat          = false, int windowBits = Constants.MaxBufferSize)
 {
     Constants.RetCode ret;
     _compressed = compressed;
     _buffer     = new byte[1 << 15];
     unsafe
     {
         fixed(ZStream *pZstream = &_zstrm)
         {
             ret = Zlib.DeflateInit2(pZstream, (int)level, 8, useZlibFormat ? windowBits : -windowBits, memLevel, strategy);
         }
     }
     if (ret != Constants.RetCode.OK)
     {
         throw Contracts.Except("Could not initialize zstream. Error code: {0}", ret);
     }
     _zstrm.AvailOut = (uint)_buffer.Length;
 }
        private unsafe int InternalRead(byte *pInput, byte *pOutput, int count)
        {
            Constants.RetCode ret;

            _zstrm.NextIn   = pInput + _bufferUsed - _zstrm.AvailIn;
            _zstrm.NextOut  = pOutput;
            _zstrm.AvailOut = (uint)count;
            do
            {
                if (_compressed != null && (_bufferUsed == 0 || _zstrm.AvailIn == 0))
                {
                    _bufferUsed    = _compressed.Read(_buffer, 0, _buffer.Length);
                    _zstrm.AvailIn = (uint)_bufferUsed;
                    if (_bufferUsed == 0)
                    {
                        break;
                    }
                    _zstrm.NextIn = pInput;
                }
                else
                {
                    _zstrm.NextIn = pInput + _bufferUsed - _zstrm.AvailIn;
                }

                if (_zstrm.AvailIn == 0)
                    return(0);

                fixed(ZStream *pZstream = &_zstrm)
                {
                    ret = Zlib.inflate(pZstream, Constants.Flush.NoFlush);
                    if (!(ret == Constants.RetCode.StreamEnd || ret == Constants.RetCode.OK))
                    {
                        throw Contracts.Except($"{nameof(Zlib.inflate)} failed with {ret}");
                    }
                }
            } while (ret != Constants.RetCode.StreamEnd && _zstrm.AvailOut != 0);

            return(count - (int)_zstrm.AvailOut);
        }