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