cannam@128: //
cannam@128: // © Copyright Henrik Ravn 2004
cannam@128: //
cannam@128: // Use, modification and distribution are subject to the Boost Software License, Version 1.0.
cannam@128: // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
cannam@128: //
cannam@128:
cannam@128: using System;
cannam@128: using System.Diagnostics;
cannam@128: using System.Runtime.InteropServices;
cannam@128:
cannam@128: namespace DotZLib
cannam@128: {
cannam@128:
cannam@128: ///
cannam@128: /// Implements a data decompressor, using the inflate algorithm in the ZLib dll
cannam@128: ///
cannam@128: public class Inflater : CodecBase
cannam@128: {
cannam@128: #region Dll imports
cannam@128: [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Ansi)]
cannam@128: private static extern int inflateInit_(ref ZStream sz, string vs, int size);
cannam@128:
cannam@128: [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
cannam@128: private static extern int inflate(ref ZStream sz, int flush);
cannam@128:
cannam@128: [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
cannam@128: private static extern int inflateReset(ref ZStream sz);
cannam@128:
cannam@128: [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
cannam@128: private static extern int inflateEnd(ref ZStream sz);
cannam@128: #endregion
cannam@128:
cannam@128: ///
cannam@128: /// Constructs an new instance of the Inflater
cannam@128: ///
cannam@128: public Inflater() : base()
cannam@128: {
cannam@128: int retval = inflateInit_(ref _ztream, Info.Version, Marshal.SizeOf(_ztream));
cannam@128: if (retval != 0)
cannam@128: throw new ZLibException(retval, "Could not initialize inflater");
cannam@128:
cannam@128: resetOutput();
cannam@128: }
cannam@128:
cannam@128:
cannam@128: ///
cannam@128: /// Adds more data to the codec to be processed.
cannam@128: ///
cannam@128: /// Byte array containing the data to be added to the codec
cannam@128: /// The index of the first byte to add from data
cannam@128: /// The number of bytes to add
cannam@128: /// Adding data may, or may not, raise the DataAvailable event
cannam@128: public override void Add(byte[] data, int offset, int count)
cannam@128: {
cannam@128: if (data == null) throw new ArgumentNullException();
cannam@128: if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
cannam@128: if ((offset+count) > data.Length) throw new ArgumentException();
cannam@128:
cannam@128: int total = count;
cannam@128: int inputIndex = offset;
cannam@128: int err = 0;
cannam@128:
cannam@128: while (err >= 0 && inputIndex < total)
cannam@128: {
cannam@128: copyInput(data, inputIndex, Math.Min(total - inputIndex, kBufferSize));
cannam@128: err = inflate(ref _ztream, (int)FlushTypes.None);
cannam@128: if (err == 0)
cannam@128: while (_ztream.avail_out == 0)
cannam@128: {
cannam@128: OnDataAvailable();
cannam@128: err = inflate(ref _ztream, (int)FlushTypes.None);
cannam@128: }
cannam@128:
cannam@128: inputIndex += (int)_ztream.total_in;
cannam@128: }
cannam@128: setChecksum( _ztream.adler );
cannam@128: }
cannam@128:
cannam@128:
cannam@128: ///
cannam@128: /// Finishes up any pending data that needs to be processed and handled.
cannam@128: ///
cannam@128: public override void Finish()
cannam@128: {
cannam@128: int err;
cannam@128: do
cannam@128: {
cannam@128: err = inflate(ref _ztream, (int)FlushTypes.Finish);
cannam@128: OnDataAvailable();
cannam@128: }
cannam@128: while (err == 0);
cannam@128: setChecksum( _ztream.adler );
cannam@128: inflateReset(ref _ztream);
cannam@128: resetOutput();
cannam@128: }
cannam@128:
cannam@128: ///
cannam@128: /// Closes the internal zlib inflate stream
cannam@128: ///
cannam@128: protected override void CleanUp() { inflateEnd(ref _ztream); }
cannam@128:
cannam@128:
cannam@128: }
cannam@128: }