2 // © Copyright Henrik Ravn 2004
\r
4 // Use, modification and distribution are subject to the Boost Software License, Version 1.0.
\r
5 // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
\r
10 using System.Runtime.InteropServices;
\r
17 #region Internal types
\r
20 /// Defines constants for the various flush types used with zlib
\r
22 internal enum FlushTypes
\r
24 None, Partial, Sync, Full, Finish, Block
\r
27 #region ZStream structure
\r
28 // internal mapping of the zlib zstream structure for marshalling
\r
29 [StructLayoutAttribute(LayoutKind.Sequential, Pack=4, Size=0, CharSet=CharSet.Ansi)]
\r
30 internal struct ZStream
\r
32 public IntPtr next_in;
\r
33 public uint avail_in;
\r
34 public uint total_in;
\r
36 public IntPtr next_out;
\r
37 public uint avail_out;
\r
38 public uint total_out;
\r
40 [MarshalAs(UnmanagedType.LPStr)]
\r
57 #region Public enums
\r
59 /// Defines constants for the available compression levels in zlib
\r
61 public enum CompressLevel : int
\r
64 /// The default compression level with a reasonable compromise between compression and speed
\r
68 /// No compression at all. The data are passed straight through.
\r
72 /// The maximum compression rate available.
\r
76 /// The fastest available compression level.
\r
82 #region Exception classes
\r
84 /// The exception that is thrown when an error occurs on the zlib dll
\r
86 public class ZLibException : ApplicationException
\r
89 /// Initializes a new instance of the <see cref="ZLibException"/> class with a specified
\r
90 /// error message and error code
\r
92 /// <param name="errorCode">The zlib error code that caused the exception</param>
\r
93 /// <param name="msg">A message that (hopefully) describes the error</param>
\r
94 public ZLibException(int errorCode, string msg) : base(String.Format("ZLib error {0} {1}", errorCode, msg))
\r
99 /// Initializes a new instance of the <see cref="ZLibException"/> class with a specified
\r
102 /// <param name="errorCode">The zlib error code that caused the exception</param>
\r
103 public ZLibException(int errorCode) : base(String.Format("ZLib error {0}", errorCode))
\r
112 /// Declares methods and properties that enables a running checksum to be calculated
\r
114 public interface ChecksumGenerator
\r
117 /// Gets the current value of the checksum
\r
119 uint Value { get; }
\r
122 /// Clears the current checksum to 0
\r
127 /// Updates the current checksum with an array of bytes
\r
129 /// <param name="data">The data to update the checksum with</param>
\r
130 void Update(byte[] data);
\r
133 /// Updates the current checksum with part of an array of bytes
\r
135 /// <param name="data">The data to update the checksum with</param>
\r
136 /// <param name="offset">Where in <c>data</c> to start updating</param>
\r
137 /// <param name="count">The number of bytes from <c>data</c> to use</param>
\r
138 /// <exception cref="ArgumentException">The sum of offset and count is larger than the length of <c>data</c></exception>
\r
139 /// <exception cref="ArgumentNullException"><c>data</c> is a null reference</exception>
\r
140 /// <exception cref="ArgumentOutOfRangeException">Offset or count is negative.</exception>
\r
141 void Update(byte[] data, int offset, int count);
\r
144 /// Updates the current checksum with the data from a string
\r
146 /// <param name="data">The string to update the checksum with</param>
\r
147 /// <remarks>The characters in the string are converted by the UTF-8 encoding</remarks>
\r
148 void Update(string data);
\r
151 /// Updates the current checksum with the data from a string, using a specific encoding
\r
153 /// <param name="data">The string to update the checksum with</param>
\r
154 /// <param name="encoding">The encoding to use</param>
\r
155 void Update(string data, Encoding encoding);
\r
160 /// Represents the method that will be called from a codec when new data
\r
163 /// <paramref name="data">The byte array containing the processed data</paramref>
\r
164 /// <paramref name="startIndex">The index of the first processed byte in <c>data</c></paramref>
\r
165 /// <paramref name="count">The number of processed bytes available</paramref>
\r
166 /// <remarks>On return from this method, the data may be overwritten, so grab it while you can.
\r
167 /// You cannot assume that startIndex will be zero.
\r
169 public delegate void DataAvailableHandler(byte[] data, int startIndex, int count);
\r
172 /// Declares methods and events for implementing compressors/decompressors
\r
174 public interface Codec
\r
177 /// Occurs when more processed data are available.
\r
179 event DataAvailableHandler DataAvailable;
\r
182 /// Adds more data to the codec to be processed.
\r
184 /// <param name="data">Byte array containing the data to be added to the codec</param>
\r
185 /// <remarks>Adding data may, or may not, raise the <c>DataAvailable</c> event</remarks>
\r
186 void Add(byte[] data);
\r
189 /// Adds more data to the codec to be processed.
\r
191 /// <param name="data">Byte array containing the data to be added to the codec</param>
\r
192 /// <param name="offset">The index of the first byte to add from <c>data</c></param>
\r
193 /// <param name="count">The number of bytes to add</param>
\r
194 /// <remarks>Adding data may, or may not, raise the <c>DataAvailable</c> event</remarks>
\r
195 void Add(byte[] data, int offset, int count);
\r
198 /// Finishes up any pending data that needs to be processed and handled.
\r
203 /// Gets the checksum of the data that has been added so far
\r
205 uint Checksum { get; }
\r
214 /// Encapsulates general information about the ZLib library
\r
218 #region DLL imports
\r
219 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
\r
220 private static extern uint zlibCompileFlags();
\r
222 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
\r
223 private static extern string zlibVersion();
\r
226 #region Private stuff
\r
227 private uint _flags;
\r
229 // helper function that unpacks a bitsize mask
\r
230 private static int bitSize(uint bits)
\r
243 /// Constructs an instance of the <c>Info</c> class.
\r
247 _flags = zlibCompileFlags();
\r
251 /// True if the library is compiled with debug info
\r
253 public bool HasDebugInfo { get { return 0 != (_flags & 0x100); } }
\r
256 /// True if the library is compiled with assembly optimizations
\r
258 public bool UsesAssemblyCode { get { return 0 != (_flags & 0x200); } }
\r
261 /// Gets the size of the unsigned int that was compiled into Zlib
\r
263 public int SizeOfUInt { get { return bitSize(_flags & 3); } }
\r
266 /// Gets the size of the unsigned long that was compiled into Zlib
\r
268 public int SizeOfULong { get { return bitSize((_flags >> 2) & 3); } }
\r
271 /// Gets the size of the pointers that were compiled into Zlib
\r
273 public int SizeOfPointer { get { return bitSize((_flags >> 4) & 3); } }
\r
276 /// Gets the size of the z_off_t type that was compiled into Zlib
\r
278 public int SizeOfOffset { get { return bitSize((_flags >> 6) & 3); } }
\r
281 /// Gets the version of ZLib as a string, e.g. "1.2.1"
\r
283 public static string Version { get { return zlibVersion(); } }
\r