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