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