annotate src/zlib-1.2.8/contrib/dotzlib/DotZLib/Inflater.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 decompressor, using the inflate algorithm in the ZLib dll
Chris@43 17 /// </summary>
Chris@43 18 public class Inflater : 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 inflateInit_(ref ZStream sz, string vs, int size);
Chris@43 23
Chris@43 24 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
Chris@43 25 private static extern int inflate(ref ZStream sz, int flush);
Chris@43 26
Chris@43 27 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
Chris@43 28 private static extern int inflateReset(ref ZStream sz);
Chris@43 29
Chris@43 30 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
Chris@43 31 private static extern int inflateEnd(ref ZStream sz);
Chris@43 32 #endregion
Chris@43 33
Chris@43 34 /// <summary>
Chris@43 35 /// Constructs an new instance of the <c>Inflater</c>
Chris@43 36 /// </summary>
Chris@43 37 public Inflater() : base()
Chris@43 38 {
Chris@43 39 int retval = inflateInit_(ref _ztream, Info.Version, Marshal.SizeOf(_ztream));
Chris@43 40 if (retval != 0)
Chris@43 41 throw new ZLibException(retval, "Could not initialize inflater");
Chris@43 42
Chris@43 43 resetOutput();
Chris@43 44 }
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 err = inflate(ref _ztream, (int)FlushTypes.None);
Chris@43 68 if (err == 0)
Chris@43 69 while (_ztream.avail_out == 0)
Chris@43 70 {
Chris@43 71 OnDataAvailable();
Chris@43 72 err = inflate(ref _ztream, (int)FlushTypes.None);
Chris@43 73 }
Chris@43 74
Chris@43 75 inputIndex += (int)_ztream.total_in;
Chris@43 76 }
Chris@43 77 setChecksum( _ztream.adler );
Chris@43 78 }
Chris@43 79
Chris@43 80
Chris@43 81 /// <summary>
Chris@43 82 /// Finishes up any pending data that needs to be processed and handled.
Chris@43 83 /// </summary>
Chris@43 84 public override void Finish()
Chris@43 85 {
Chris@43 86 int err;
Chris@43 87 do
Chris@43 88 {
Chris@43 89 err = inflate(ref _ztream, (int)FlushTypes.Finish);
Chris@43 90 OnDataAvailable();
Chris@43 91 }
Chris@43 92 while (err == 0);
Chris@43 93 setChecksum( _ztream.adler );
Chris@43 94 inflateReset(ref _ztream);
Chris@43 95 resetOutput();
Chris@43 96 }
Chris@43 97
Chris@43 98 /// <summary>
Chris@43 99 /// Closes the internal zlib inflate stream
Chris@43 100 /// </summary>
Chris@43 101 protected override void CleanUp() { inflateEnd(ref _ztream); }
Chris@43 102
Chris@43 103
Chris@43 104 }
Chris@43 105 }