--- /dev/null
+//\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.IO;\r
+using System.Runtime.InteropServices;\r
+using System.Text;\r
+\r
+\r
+namespace DotZLib\r
+{\r
+\r
+ #region Internal types\r
+\r
+ /// <summary>\r
+ /// Defines constants for the various flush types used with zlib\r
+ /// </summary>\r
+ internal enum FlushTypes \r
+ {\r
+ None, Partial, Sync, Full, Finish, Block\r
+ }\r
+\r
+ #region ZStream structure\r
+ // internal mapping of the zlib zstream structure for marshalling\r
+ [StructLayoutAttribute(LayoutKind.Sequential, Pack=4, Size=0, CharSet=CharSet.Ansi)]\r
+ internal struct ZStream\r
+ {\r
+ public IntPtr next_in;\r
+ public uint avail_in;\r
+ public uint total_in;\r
+\r
+ public IntPtr next_out;\r
+ public uint avail_out;\r
+ public uint total_out;\r
+\r
+ [MarshalAs(UnmanagedType.LPStr)]\r
+ string msg; \r
+ uint state;\r
+\r
+ uint zalloc;\r
+ uint zfree;\r
+ uint opaque;\r
+\r
+ int data_type;\r
+ public uint adler;\r
+ uint reserved;\r
+ }\r
+\r
+ #endregion\r
+ \r
+ #endregion\r
+\r
+ #region Public enums\r
+ /// <summary>\r
+ /// Defines constants for the available compression levels in zlib\r
+ /// </summary>\r
+ public enum CompressLevel : int\r
+ {\r
+ /// <summary>\r
+ /// The default compression level with a reasonable compromise between compression and speed\r
+ /// </summary>\r
+ Default = -1, \r
+ /// <summary>\r
+ /// No compression at all. The data are passed straight through.\r
+ /// </summary>\r
+ None = 0,\r
+ /// <summary>\r
+ /// The maximum compression rate available.\r
+ /// </summary>\r
+ Best = 9, \r
+ /// <summary>\r
+ /// The fastest available compression level.\r
+ /// </summary>\r
+ Fastest = 1\r
+ }\r
+ #endregion\r
+\r
+ #region Exception classes\r
+ /// <summary>\r
+ /// The exception that is thrown when an error occurs on the zlib dll\r
+ /// </summary>\r
+ public class ZLibException : ApplicationException\r
+ {\r
+ /// <summary>\r
+ /// Initializes a new instance of the <see cref="ZLibException"/> class with a specified \r
+ /// error message and error code\r
+ /// </summary>\r
+ /// <param name="errorCode">The zlib error code that caused the exception</param>\r
+ /// <param name="msg">A message that (hopefully) describes the error</param>\r
+ public ZLibException(int errorCode, string msg) : base(String.Format("ZLib error {0} {1}", errorCode, msg))\r
+ {\r
+ }\r
+\r
+ /// <summary>\r
+ /// Initializes a new instance of the <see cref="ZLibException"/> class with a specified \r
+ /// error code\r
+ /// </summary>\r
+ /// <param name="errorCode">The zlib error code that caused the exception</param>\r
+ public ZLibException(int errorCode) : base(String.Format("ZLib error {0}", errorCode))\r
+ {\r
+ }\r
+ }\r
+ #endregion\r
+\r
+ #region Interfaces\r
+\r
+ /// <summary>\r
+ /// Declares methods and properties that enables a running checksum to be calculated \r
+ /// </summary>\r
+ public interface ChecksumGenerator\r
+ {\r
+ /// <summary>\r
+ /// Gets the current value of the checksum\r
+ /// </summary>\r
+ uint Value { get; }\r
+\r
+ /// <summary>\r
+ /// Clears the current checksum to 0\r
+ /// </summary>\r
+ void Reset();\r
+\r
+ /// <summary>\r
+ /// Updates the current checksum with an array of bytes\r
+ /// </summary>\r
+ /// <param name="data">The data to update the checksum with</param>\r
+ void Update(byte[] data);\r
+\r
+ /// <summary>\r
+ /// Updates the current checksum with part of an array of bytes\r
+ /// </summary>\r
+ /// <param name="data">The data to update the checksum with</param>\r
+ /// <param name="offset">Where in <c>data</c> to start updating</param>\r
+ /// <param name="count">The number of bytes from <c>data</c> to use</param>\r
+ /// <exception cref="ArgumentException">The sum of offset and count is larger than the length of <c>data</c></exception>\r
+ /// <exception cref="ArgumentNullException"><c>data</c> is a null reference</exception>\r
+ /// <exception cref="ArgumentOutOfRangeException">Offset or count is negative.</exception>\r
+ void Update(byte[] data, int offset, int count);\r
+\r
+ /// <summary>\r
+ /// Updates the current checksum with the data from a string\r
+ /// </summary>\r
+ /// <param name="data">The string to update the checksum with</param>\r
+ /// <remarks>The characters in the string are converted by the UTF-8 encoding</remarks>\r
+ void Update(string data);\r
+\r
+ /// <summary>\r
+ /// Updates the current checksum with the data from a string, using a specific encoding\r
+ /// </summary>\r
+ /// <param name="data">The string to update the checksum with</param>\r
+ /// <param name="encoding">The encoding to use</param>\r
+ void Update(string data, Encoding encoding);\r
+ }\r
+\r
+\r
+ /// <summary>\r
+ /// Represents the method that will be called from a codec when new data\r
+ /// are available.\r
+ /// </summary>\r
+ /// <paramref name="data">The byte array containing the processed data</paramref>\r
+ /// <paramref name="startIndex">The index of the first processed byte in <c>data</c></paramref>\r
+ /// <paramref name="count">The number of processed bytes available</paramref>\r
+ /// <remarks>On return from this method, the data may be overwritten, so grab it while you can. \r
+ /// You cannot assume that startIndex will be zero.\r
+ /// </remarks>\r
+ public delegate void DataAvailableHandler(byte[] data, int startIndex, int count);\r
+\r
+ /// <summary>\r
+ /// Declares methods and events for implementing compressors/decompressors\r
+ /// </summary>\r
+ public interface Codec\r
+ {\r
+ /// <summary>\r
+ /// Occurs when more processed data are available.\r
+ /// </summary>\r
+ event DataAvailableHandler DataAvailable;\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
+ /// <remarks>Adding data may, or may not, raise the <c>DataAvailable</c> event</remarks>\r
+ void Add(byte[] data);\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
+ void Add(byte[] data, int offset, int count);\r
+\r
+ /// <summary>\r
+ /// Finishes up any pending data that needs to be processed and handled.\r
+ /// </summary>\r
+ void Finish();\r
+\r
+ /// <summary>\r
+ /// Gets the checksum of the data that has been added so far\r
+ /// </summary>\r
+ uint Checksum { get; }\r
+\r
+\r
+ }\r
+\r
+ #endregion\r
+\r
+ #region Classes\r
+ /// <summary>\r
+ /// Encapsulates general information about the ZLib library\r
+ /// </summary>\r
+ public class Info\r
+ {\r
+ #region DLL imports\r
+ [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]\r
+ private static extern uint zlibCompileFlags();\r
+\r
+ [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]\r
+ private static extern string zlibVersion();\r
+ #endregion\r
+\r
+ #region Private stuff\r
+ private uint _flags;\r
+\r
+ // helper function that unpacks a bitsize mask\r
+ private static int bitSize(uint bits)\r
+ {\r
+ switch (bits)\r
+ {\r
+ case 0: return 16;\r
+ case 1: return 32;\r
+ case 2: return 64;\r
+ }\r
+ return -1;\r
+ }\r
+ #endregion\r
+\r
+ /// <summary>\r
+ /// Constructs an instance of the <c>Info</c> class.\r
+ /// </summary>\r
+ public Info()\r
+ {\r
+ _flags = zlibCompileFlags();\r
+ }\r
+\r
+ /// <summary>\r
+ /// True if the library is compiled with debug info\r
+ /// </summary>\r
+ public bool HasDebugInfo { get { return 0 != (_flags & 0x100); } }\r
+\r
+ /// <summary>\r
+ /// True if the library is compiled with assembly optimizations\r
+ /// </summary>\r
+ public bool UsesAssemblyCode { get { return 0 != (_flags & 0x200); } }\r
+\r
+ /// <summary>\r
+ /// Gets the size of the unsigned int that was compiled into Zlib\r
+ /// </summary>\r
+ public int SizeOfUInt { get { return bitSize(_flags & 3); } }\r
+\r
+ /// <summary>\r
+ /// Gets the size of the unsigned long that was compiled into Zlib\r
+ /// </summary>\r
+ public int SizeOfULong { get { return bitSize((_flags >> 2) & 3); } }\r
+\r
+ /// <summary>\r
+ /// Gets the size of the pointers that were compiled into Zlib\r
+ /// </summary>\r
+ public int SizeOfPointer { get { return bitSize((_flags >> 4) & 3); } }\r
+\r
+ /// <summary>\r
+ /// Gets the size of the z_off_t type that was compiled into Zlib\r
+ /// </summary>\r
+ public int SizeOfOffset { get { return bitSize((_flags >> 6) & 3); } }\r
+\r
+ /// <summary>\r
+ /// Gets the version of ZLib as a string, e.g. "1.2.1"\r
+ /// </summary>\r
+ public static string Version { get { return zlibVersion(); } }\r
+ }\r
+\r
+ #endregion\r
+\r
+}\r