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