annotate src/zlib-1.2.7/contrib/dotzlib/DotZLib/Inflater.cs @ 4:e13257ea84a4

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