1*212397c6Schristos // 2*212397c6Schristos // � Copyright Henrik Ravn 2004 3*212397c6Schristos // 4*212397c6Schristos // Use, modification and distribution are subject to the Boost Software License, Version 1.0. 5*212397c6Schristos // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6*212397c6Schristos // 7*212397c6Schristos 8*212397c6Schristos using System; 9*212397c6Schristos using System.Diagnostics; 10*212397c6Schristos using System.Runtime.InteropServices; 11*212397c6Schristos 12*212397c6Schristos namespace DotZLib 13*212397c6Schristos { 14*212397c6Schristos 15*212397c6Schristos /// <summary> 16*212397c6Schristos /// Implements a data compressor, using the deflate algorithm in the ZLib dll 17*212397c6Schristos /// </summary> 18*212397c6Schristos public sealed class Deflater : CodecBase 19*212397c6Schristos { 20*212397c6Schristos #region Dll imports 21*212397c6Schristos [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Ansi)] deflateInit_(ref ZStream sz, int level, string vs, int size)22*212397c6Schristos private static extern int deflateInit_(ref ZStream sz, int level, string vs, int size); 23*212397c6Schristos 24*212397c6Schristos [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)] deflate(ref ZStream sz, int flush)25*212397c6Schristos private static extern int deflate(ref ZStream sz, int flush); 26*212397c6Schristos 27*212397c6Schristos [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)] deflateReset(ref ZStream sz)28*212397c6Schristos private static extern int deflateReset(ref ZStream sz); 29*212397c6Schristos 30*212397c6Schristos [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)] deflateEnd(ref ZStream sz)31*212397c6Schristos private static extern int deflateEnd(ref ZStream sz); 32*212397c6Schristos #endregion 33*212397c6Schristos 34*212397c6Schristos /// <summary> 35*212397c6Schristos /// Constructs an new instance of the <c>Deflater</c> 36*212397c6Schristos /// </summary> 37*212397c6Schristos /// <param name="level">The compression level to use for this <c>Deflater</c></param> Deflater(CompressLevel level)38*212397c6Schristos public Deflater(CompressLevel level) : base() 39*212397c6Schristos { 40*212397c6Schristos int retval = deflateInit_(ref _ztream, (int)level, Info.Version, Marshal.SizeOf(_ztream)); 41*212397c6Schristos if (retval != 0) 42*212397c6Schristos throw new ZLibException(retval, "Could not initialize deflater"); 43*212397c6Schristos 44*212397c6Schristos resetOutput(); 45*212397c6Schristos } 46*212397c6Schristos 47*212397c6Schristos /// <summary> 48*212397c6Schristos /// Adds more data to the codec to be processed. 49*212397c6Schristos /// </summary> 50*212397c6Schristos /// <param name="data">Byte array containing the data to be added to the codec</param> 51*212397c6Schristos /// <param name="offset">The index of the first byte to add from <c>data</c></param> 52*212397c6Schristos /// <param name="count">The number of bytes to add</param> 53*212397c6Schristos /// <remarks>Adding data may, or may not, raise the <c>DataAvailable</c> event</remarks> Add(byte[] data, int offset, int count)54*212397c6Schristos public override void Add(byte[] data, int offset, int count) 55*212397c6Schristos { 56*212397c6Schristos if (data == null) throw new ArgumentNullException(); 57*212397c6Schristos if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException(); 58*212397c6Schristos if ((offset+count) > data.Length) throw new ArgumentException(); 59*212397c6Schristos 60*212397c6Schristos int total = count; 61*212397c6Schristos int inputIndex = offset; 62*212397c6Schristos int err = 0; 63*212397c6Schristos 64*212397c6Schristos while (err >= 0 && inputIndex < total) 65*212397c6Schristos { 66*212397c6Schristos copyInput(data, inputIndex, Math.Min(total - inputIndex, kBufferSize)); 67*212397c6Schristos while (err >= 0 && _ztream.avail_in > 0) 68*212397c6Schristos { 69*212397c6Schristos err = deflate(ref _ztream, (int)FlushTypes.None); 70*212397c6Schristos if (err == 0) 71*212397c6Schristos while (_ztream.avail_out == 0) 72*212397c6Schristos { 73*212397c6Schristos OnDataAvailable(); 74*212397c6Schristos err = deflate(ref _ztream, (int)FlushTypes.None); 75*212397c6Schristos } 76*212397c6Schristos inputIndex += (int)_ztream.total_in; 77*212397c6Schristos } 78*212397c6Schristos } 79*212397c6Schristos setChecksum( _ztream.adler ); 80*212397c6Schristos } 81*212397c6Schristos 82*212397c6Schristos 83*212397c6Schristos /// <summary> 84*212397c6Schristos /// Finishes up any pending data that needs to be processed and handled. 85*212397c6Schristos /// </summary> Finish()86*212397c6Schristos public override void Finish() 87*212397c6Schristos { 88*212397c6Schristos int err; 89*212397c6Schristos do 90*212397c6Schristos { 91*212397c6Schristos err = deflate(ref _ztream, (int)FlushTypes.Finish); 92*212397c6Schristos OnDataAvailable(); 93*212397c6Schristos } 94*212397c6Schristos while (err == 0); 95*212397c6Schristos setChecksum( _ztream.adler ); 96*212397c6Schristos deflateReset(ref _ztream); 97*212397c6Schristos resetOutput(); 98*212397c6Schristos } 99*212397c6Schristos 100*212397c6Schristos /// <summary> 101*212397c6Schristos /// Closes the internal zlib deflate stream 102*212397c6Schristos /// </summary> CleanUp()103*212397c6Schristos protected override void CleanUp() { deflateEnd(ref _ztream); } 104*212397c6Schristos 105*212397c6Schristos } 106*212397c6Schristos } 107