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