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