annotate src/zlib-1.2.8/contrib/dotzlib/DotZLib/Deflater.cs @ 56:af97cad61ff0

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