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