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