Chris@43: // Chris@43: // © Copyright Henrik Ravn 2004 Chris@43: // Chris@43: // Use, modification and distribution are subject to the Boost Software License, Version 1.0. Chris@43: // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@43: // Chris@43: Chris@43: using System; Chris@43: using System.Diagnostics; Chris@43: using System.Runtime.InteropServices; Chris@43: Chris@43: namespace DotZLib Chris@43: { Chris@43: Chris@43: /// Chris@43: /// Implements a data compressor, using the deflate algorithm in the ZLib dll Chris@43: /// Chris@43: public sealed class Deflater : CodecBase Chris@43: { Chris@43: #region Dll imports Chris@43: [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Ansi)] Chris@43: private static extern int deflateInit_(ref ZStream sz, int level, string vs, int size); Chris@43: Chris@43: [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)] Chris@43: private static extern int deflate(ref ZStream sz, int flush); Chris@43: Chris@43: [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)] Chris@43: private static extern int deflateReset(ref ZStream sz); Chris@43: Chris@43: [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)] Chris@43: private static extern int deflateEnd(ref ZStream sz); Chris@43: #endregion Chris@43: Chris@43: /// Chris@43: /// Constructs an new instance of the Deflater Chris@43: /// Chris@43: /// The compression level to use for this Deflater Chris@43: public Deflater(CompressLevel level) : base() Chris@43: { Chris@43: int retval = deflateInit_(ref _ztream, (int)level, Info.Version, Marshal.SizeOf(_ztream)); Chris@43: if (retval != 0) Chris@43: throw new ZLibException(retval, "Could not initialize deflater"); Chris@43: Chris@43: resetOutput(); Chris@43: } Chris@43: Chris@43: /// Chris@43: /// Adds more data to the codec to be processed. Chris@43: /// Chris@43: /// Byte array containing the data to be added to the codec Chris@43: /// The index of the first byte to add from data Chris@43: /// The number of bytes to add Chris@43: /// Adding data may, or may not, raise the DataAvailable event Chris@43: public override void Add(byte[] data, int offset, int count) Chris@43: { Chris@43: if (data == null) throw new ArgumentNullException(); Chris@43: if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException(); Chris@43: if ((offset+count) > data.Length) throw new ArgumentException(); Chris@43: Chris@43: int total = count; Chris@43: int inputIndex = offset; Chris@43: int err = 0; Chris@43: Chris@43: while (err >= 0 && inputIndex < total) Chris@43: { Chris@43: copyInput(data, inputIndex, Math.Min(total - inputIndex, kBufferSize)); Chris@43: while (err >= 0 && _ztream.avail_in > 0) Chris@43: { Chris@43: err = deflate(ref _ztream, (int)FlushTypes.None); Chris@43: if (err == 0) Chris@43: while (_ztream.avail_out == 0) Chris@43: { Chris@43: OnDataAvailable(); Chris@43: err = deflate(ref _ztream, (int)FlushTypes.None); Chris@43: } Chris@43: inputIndex += (int)_ztream.total_in; Chris@43: } Chris@43: } Chris@43: setChecksum( _ztream.adler ); Chris@43: } Chris@43: Chris@43: Chris@43: /// Chris@43: /// Finishes up any pending data that needs to be processed and handled. Chris@43: /// Chris@43: public override void Finish() Chris@43: { Chris@43: int err; Chris@43: do Chris@43: { Chris@43: err = deflate(ref _ztream, (int)FlushTypes.Finish); Chris@43: OnDataAvailable(); Chris@43: } Chris@43: while (err == 0); Chris@43: setChecksum( _ztream.adler ); Chris@43: deflateReset(ref _ztream); Chris@43: resetOutput(); Chris@43: } Chris@43: Chris@43: /// Chris@43: /// Closes the internal zlib deflate stream Chris@43: /// Chris@43: protected override void CleanUp() { deflateEnd(ref _ztream); } Chris@43: Chris@43: } Chris@43: }