annotate src/zlib-1.2.7/contrib/dotzlib/DotZLib/Deflater.cs @ 23:619f715526df sv_v2.1

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