2 // © Copyright Henrik Ravn 2004
\r
4 // Use, modification and distribution are subject to the Boost Software License, Version 1.0.
\r
5 // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
\r
9 using System.Diagnostics;
\r
10 using System.Runtime.InteropServices;
\r
16 /// Implements a data decompressor, using the inflate algorithm in the ZLib dll
\r
18 public class Inflater : CodecBase
\r
21 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Ansi)]
\r
22 private static extern int inflateInit_(ref ZStream sz, string vs, int size);
\r
24 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
\r
25 private static extern int inflate(ref ZStream sz, int flush);
\r
27 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
\r
28 private static extern int inflateReset(ref ZStream sz);
\r
30 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
\r
31 private static extern int inflateEnd(ref ZStream sz);
\r
35 /// Constructs an new instance of the <c>Inflater</c>
\r
37 public Inflater() : base()
\r
39 int retval = inflateInit_(ref _ztream, Info.Version, Marshal.SizeOf(_ztream));
\r
41 throw new ZLibException(retval, "Could not initialize inflater");
\r
48 /// Adds more data to the codec to be processed.
\r
50 /// <param name="data">Byte array containing the data to be added to the codec</param>
\r
51 /// <param name="offset">The index of the first byte to add from <c>data</c></param>
\r
52 /// <param name="count">The number of bytes to add</param>
\r
53 /// <remarks>Adding data may, or may not, raise the <c>DataAvailable</c> event</remarks>
\r
54 public override void Add(byte[] data, int offset, int count)
\r
56 if (data == null) throw new ArgumentNullException();
\r
57 if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
\r
58 if ((offset+count) > data.Length) throw new ArgumentException();
\r
61 int inputIndex = offset;
\r
64 while (err >= 0 && inputIndex < total)
\r
66 copyInput(data, inputIndex, Math.Min(total - inputIndex, kBufferSize));
\r
67 err = inflate(ref _ztream, (int)FlushTypes.None);
\r
69 while (_ztream.avail_out == 0)
\r
72 err = inflate(ref _ztream, (int)FlushTypes.None);
\r
75 inputIndex += (int)_ztream.total_in;
\r
77 setChecksum( _ztream.adler );
\r
82 /// Finishes up any pending data that needs to be processed and handled.
\r
84 public override void Finish()
\r
89 err = inflate(ref _ztream, (int)FlushTypes.Finish);
\r
93 setChecksum( _ztream.adler );
\r
94 inflateReset(ref _ztream);
\r
99 /// Closes the internal zlib inflate stream
\r
101 protected override void CleanUp() { inflateEnd(ref _ztream); }
\r