cannam@89
|
1 //
|
cannam@89
|
2 // © Copyright Henrik Ravn 2004
|
cannam@89
|
3 //
|
cannam@89
|
4 // Use, modification and distribution are subject to the Boost Software License, Version 1.0.
|
cannam@89
|
5 // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
cannam@89
|
6 //
|
cannam@89
|
7
|
cannam@89
|
8 using System;
|
cannam@89
|
9 using System.IO;
|
cannam@89
|
10 using System.Runtime.InteropServices;
|
cannam@89
|
11 using System.Text;
|
cannam@89
|
12
|
cannam@89
|
13
|
cannam@89
|
14 namespace DotZLib
|
cannam@89
|
15 {
|
cannam@89
|
16
|
cannam@89
|
17 #region Internal types
|
cannam@89
|
18
|
cannam@89
|
19 /// <summary>
|
cannam@89
|
20 /// Defines constants for the various flush types used with zlib
|
cannam@89
|
21 /// </summary>
|
cannam@89
|
22 internal enum FlushTypes
|
cannam@89
|
23 {
|
cannam@89
|
24 None, Partial, Sync, Full, Finish, Block
|
cannam@89
|
25 }
|
cannam@89
|
26
|
cannam@89
|
27 #region ZStream structure
|
cannam@89
|
28 // internal mapping of the zlib zstream structure for marshalling
|
cannam@89
|
29 [StructLayoutAttribute(LayoutKind.Sequential, Pack=4, Size=0, CharSet=CharSet.Ansi)]
|
cannam@89
|
30 internal struct ZStream
|
cannam@89
|
31 {
|
cannam@89
|
32 public IntPtr next_in;
|
cannam@89
|
33 public uint avail_in;
|
cannam@89
|
34 public uint total_in;
|
cannam@89
|
35
|
cannam@89
|
36 public IntPtr next_out;
|
cannam@89
|
37 public uint avail_out;
|
cannam@89
|
38 public uint total_out;
|
cannam@89
|
39
|
cannam@89
|
40 [MarshalAs(UnmanagedType.LPStr)]
|
cannam@89
|
41 string msg;
|
cannam@89
|
42 uint state;
|
cannam@89
|
43
|
cannam@89
|
44 uint zalloc;
|
cannam@89
|
45 uint zfree;
|
cannam@89
|
46 uint opaque;
|
cannam@89
|
47
|
cannam@89
|
48 int data_type;
|
cannam@89
|
49 public uint adler;
|
cannam@89
|
50 uint reserved;
|
cannam@89
|
51 }
|
cannam@89
|
52
|
cannam@89
|
53 #endregion
|
cannam@89
|
54
|
cannam@89
|
55 #endregion
|
cannam@89
|
56
|
cannam@89
|
57 #region Public enums
|
cannam@89
|
58 /// <summary>
|
cannam@89
|
59 /// Defines constants for the available compression levels in zlib
|
cannam@89
|
60 /// </summary>
|
cannam@89
|
61 public enum CompressLevel : int
|
cannam@89
|
62 {
|
cannam@89
|
63 /// <summary>
|
cannam@89
|
64 /// The default compression level with a reasonable compromise between compression and speed
|
cannam@89
|
65 /// </summary>
|
cannam@89
|
66 Default = -1,
|
cannam@89
|
67 /// <summary>
|
cannam@89
|
68 /// No compression at all. The data are passed straight through.
|
cannam@89
|
69 /// </summary>
|
cannam@89
|
70 None = 0,
|
cannam@89
|
71 /// <summary>
|
cannam@89
|
72 /// The maximum compression rate available.
|
cannam@89
|
73 /// </summary>
|
cannam@89
|
74 Best = 9,
|
cannam@89
|
75 /// <summary>
|
cannam@89
|
76 /// The fastest available compression level.
|
cannam@89
|
77 /// </summary>
|
cannam@89
|
78 Fastest = 1
|
cannam@89
|
79 }
|
cannam@89
|
80 #endregion
|
cannam@89
|
81
|
cannam@89
|
82 #region Exception classes
|
cannam@89
|
83 /// <summary>
|
cannam@89
|
84 /// The exception that is thrown when an error occurs on the zlib dll
|
cannam@89
|
85 /// </summary>
|
cannam@89
|
86 public class ZLibException : ApplicationException
|
cannam@89
|
87 {
|
cannam@89
|
88 /// <summary>
|
cannam@89
|
89 /// Initializes a new instance of the <see cref="ZLibException"/> class with a specified
|
cannam@89
|
90 /// error message and error code
|
cannam@89
|
91 /// </summary>
|
cannam@89
|
92 /// <param name="errorCode">The zlib error code that caused the exception</param>
|
cannam@89
|
93 /// <param name="msg">A message that (hopefully) describes the error</param>
|
cannam@89
|
94 public ZLibException(int errorCode, string msg) : base(String.Format("ZLib error {0} {1}", errorCode, msg))
|
cannam@89
|
95 {
|
cannam@89
|
96 }
|
cannam@89
|
97
|
cannam@89
|
98 /// <summary>
|
cannam@89
|
99 /// Initializes a new instance of the <see cref="ZLibException"/> class with a specified
|
cannam@89
|
100 /// error code
|
cannam@89
|
101 /// </summary>
|
cannam@89
|
102 /// <param name="errorCode">The zlib error code that caused the exception</param>
|
cannam@89
|
103 public ZLibException(int errorCode) : base(String.Format("ZLib error {0}", errorCode))
|
cannam@89
|
104 {
|
cannam@89
|
105 }
|
cannam@89
|
106 }
|
cannam@89
|
107 #endregion
|
cannam@89
|
108
|
cannam@89
|
109 #region Interfaces
|
cannam@89
|
110
|
cannam@89
|
111 /// <summary>
|
cannam@89
|
112 /// Declares methods and properties that enables a running checksum to be calculated
|
cannam@89
|
113 /// </summary>
|
cannam@89
|
114 public interface ChecksumGenerator
|
cannam@89
|
115 {
|
cannam@89
|
116 /// <summary>
|
cannam@89
|
117 /// Gets the current value of the checksum
|
cannam@89
|
118 /// </summary>
|
cannam@89
|
119 uint Value { get; }
|
cannam@89
|
120
|
cannam@89
|
121 /// <summary>
|
cannam@89
|
122 /// Clears the current checksum to 0
|
cannam@89
|
123 /// </summary>
|
cannam@89
|
124 void Reset();
|
cannam@89
|
125
|
cannam@89
|
126 /// <summary>
|
cannam@89
|
127 /// Updates the current checksum with an array of bytes
|
cannam@89
|
128 /// </summary>
|
cannam@89
|
129 /// <param name="data">The data to update the checksum with</param>
|
cannam@89
|
130 void Update(byte[] data);
|
cannam@89
|
131
|
cannam@89
|
132 /// <summary>
|
cannam@89
|
133 /// Updates the current checksum with part of an array of bytes
|
cannam@89
|
134 /// </summary>
|
cannam@89
|
135 /// <param name="data">The data to update the checksum with</param>
|
cannam@89
|
136 /// <param name="offset">Where in <c>data</c> to start updating</param>
|
cannam@89
|
137 /// <param name="count">The number of bytes from <c>data</c> to use</param>
|
cannam@89
|
138 /// <exception cref="ArgumentException">The sum of offset and count is larger than the length of <c>data</c></exception>
|
cannam@89
|
139 /// <exception cref="ArgumentNullException"><c>data</c> is a null reference</exception>
|
cannam@89
|
140 /// <exception cref="ArgumentOutOfRangeException">Offset or count is negative.</exception>
|
cannam@89
|
141 void Update(byte[] data, int offset, int count);
|
cannam@89
|
142
|
cannam@89
|
143 /// <summary>
|
cannam@89
|
144 /// Updates the current checksum with the data from a string
|
cannam@89
|
145 /// </summary>
|
cannam@89
|
146 /// <param name="data">The string to update the checksum with</param>
|
cannam@89
|
147 /// <remarks>The characters in the string are converted by the UTF-8 encoding</remarks>
|
cannam@89
|
148 void Update(string data);
|
cannam@89
|
149
|
cannam@89
|
150 /// <summary>
|
cannam@89
|
151 /// Updates the current checksum with the data from a string, using a specific encoding
|
cannam@89
|
152 /// </summary>
|
cannam@89
|
153 /// <param name="data">The string to update the checksum with</param>
|
cannam@89
|
154 /// <param name="encoding">The encoding to use</param>
|
cannam@89
|
155 void Update(string data, Encoding encoding);
|
cannam@89
|
156 }
|
cannam@89
|
157
|
cannam@89
|
158
|
cannam@89
|
159 /// <summary>
|
cannam@89
|
160 /// Represents the method that will be called from a codec when new data
|
cannam@89
|
161 /// are available.
|
cannam@89
|
162 /// </summary>
|
cannam@89
|
163 /// <paramref name="data">The byte array containing the processed data</paramref>
|
cannam@89
|
164 /// <paramref name="startIndex">The index of the first processed byte in <c>data</c></paramref>
|
cannam@89
|
165 /// <paramref name="count">The number of processed bytes available</paramref>
|
cannam@89
|
166 /// <remarks>On return from this method, the data may be overwritten, so grab it while you can.
|
cannam@89
|
167 /// You cannot assume that startIndex will be zero.
|
cannam@89
|
168 /// </remarks>
|
cannam@89
|
169 public delegate void DataAvailableHandler(byte[] data, int startIndex, int count);
|
cannam@89
|
170
|
cannam@89
|
171 /// <summary>
|
cannam@89
|
172 /// Declares methods and events for implementing compressors/decompressors
|
cannam@89
|
173 /// </summary>
|
cannam@89
|
174 public interface Codec
|
cannam@89
|
175 {
|
cannam@89
|
176 /// <summary>
|
cannam@89
|
177 /// Occurs when more processed data are available.
|
cannam@89
|
178 /// </summary>
|
cannam@89
|
179 event DataAvailableHandler DataAvailable;
|
cannam@89
|
180
|
cannam@89
|
181 /// <summary>
|
cannam@89
|
182 /// Adds more data to the codec to be processed.
|
cannam@89
|
183 /// </summary>
|
cannam@89
|
184 /// <param name="data">Byte array containing the data to be added to the codec</param>
|
cannam@89
|
185 /// <remarks>Adding data may, or may not, raise the <c>DataAvailable</c> event</remarks>
|
cannam@89
|
186 void Add(byte[] data);
|
cannam@89
|
187
|
cannam@89
|
188 /// <summary>
|
cannam@89
|
189 /// Adds more data to the codec to be processed.
|
cannam@89
|
190 /// </summary>
|
cannam@89
|
191 /// <param name="data">Byte array containing the data to be added to the codec</param>
|
cannam@89
|
192 /// <param name="offset">The index of the first byte to add from <c>data</c></param>
|
cannam@89
|
193 /// <param name="count">The number of bytes to add</param>
|
cannam@89
|
194 /// <remarks>Adding data may, or may not, raise the <c>DataAvailable</c> event</remarks>
|
cannam@89
|
195 void Add(byte[] data, int offset, int count);
|
cannam@89
|
196
|
cannam@89
|
197 /// <summary>
|
cannam@89
|
198 /// Finishes up any pending data that needs to be processed and handled.
|
cannam@89
|
199 /// </summary>
|
cannam@89
|
200 void Finish();
|
cannam@89
|
201
|
cannam@89
|
202 /// <summary>
|
cannam@89
|
203 /// Gets the checksum of the data that has been added so far
|
cannam@89
|
204 /// </summary>
|
cannam@89
|
205 uint Checksum { get; }
|
cannam@89
|
206
|
cannam@89
|
207
|
cannam@89
|
208 }
|
cannam@89
|
209
|
cannam@89
|
210 #endregion
|
cannam@89
|
211
|
cannam@89
|
212 #region Classes
|
cannam@89
|
213 /// <summary>
|
cannam@89
|
214 /// Encapsulates general information about the ZLib library
|
cannam@89
|
215 /// </summary>
|
cannam@89
|
216 public class Info
|
cannam@89
|
217 {
|
cannam@89
|
218 #region DLL imports
|
cannam@89
|
219 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
|
cannam@89
|
220 private static extern uint zlibCompileFlags();
|
cannam@89
|
221
|
cannam@89
|
222 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
|
cannam@89
|
223 private static extern string zlibVersion();
|
cannam@89
|
224 #endregion
|
cannam@89
|
225
|
cannam@89
|
226 #region Private stuff
|
cannam@89
|
227 private uint _flags;
|
cannam@89
|
228
|
cannam@89
|
229 // helper function that unpacks a bitsize mask
|
cannam@89
|
230 private static int bitSize(uint bits)
|
cannam@89
|
231 {
|
cannam@89
|
232 switch (bits)
|
cannam@89
|
233 {
|
cannam@89
|
234 case 0: return 16;
|
cannam@89
|
235 case 1: return 32;
|
cannam@89
|
236 case 2: return 64;
|
cannam@89
|
237 }
|
cannam@89
|
238 return -1;
|
cannam@89
|
239 }
|
cannam@89
|
240 #endregion
|
cannam@89
|
241
|
cannam@89
|
242 /// <summary>
|
cannam@89
|
243 /// Constructs an instance of the <c>Info</c> class.
|
cannam@89
|
244 /// </summary>
|
cannam@89
|
245 public Info()
|
cannam@89
|
246 {
|
cannam@89
|
247 _flags = zlibCompileFlags();
|
cannam@89
|
248 }
|
cannam@89
|
249
|
cannam@89
|
250 /// <summary>
|
cannam@89
|
251 /// True if the library is compiled with debug info
|
cannam@89
|
252 /// </summary>
|
cannam@89
|
253 public bool HasDebugInfo { get { return 0 != (_flags & 0x100); } }
|
cannam@89
|
254
|
cannam@89
|
255 /// <summary>
|
cannam@89
|
256 /// True if the library is compiled with assembly optimizations
|
cannam@89
|
257 /// </summary>
|
cannam@89
|
258 public bool UsesAssemblyCode { get { return 0 != (_flags & 0x200); } }
|
cannam@89
|
259
|
cannam@89
|
260 /// <summary>
|
cannam@89
|
261 /// Gets the size of the unsigned int that was compiled into Zlib
|
cannam@89
|
262 /// </summary>
|
cannam@89
|
263 public int SizeOfUInt { get { return bitSize(_flags & 3); } }
|
cannam@89
|
264
|
cannam@89
|
265 /// <summary>
|
cannam@89
|
266 /// Gets the size of the unsigned long that was compiled into Zlib
|
cannam@89
|
267 /// </summary>
|
cannam@89
|
268 public int SizeOfULong { get { return bitSize((_flags >> 2) & 3); } }
|
cannam@89
|
269
|
cannam@89
|
270 /// <summary>
|
cannam@89
|
271 /// Gets the size of the pointers that were compiled into Zlib
|
cannam@89
|
272 /// </summary>
|
cannam@89
|
273 public int SizeOfPointer { get { return bitSize((_flags >> 4) & 3); } }
|
cannam@89
|
274
|
cannam@89
|
275 /// <summary>
|
cannam@89
|
276 /// Gets the size of the z_off_t type that was compiled into Zlib
|
cannam@89
|
277 /// </summary>
|
cannam@89
|
278 public int SizeOfOffset { get { return bitSize((_flags >> 6) & 3); } }
|
cannam@89
|
279
|
cannam@89
|
280 /// <summary>
|
cannam@89
|
281 /// Gets the version of ZLib as a string, e.g. "1.2.1"
|
cannam@89
|
282 /// </summary>
|
cannam@89
|
283 public static string Version { get { return zlibVersion(); } }
|
cannam@89
|
284 }
|
cannam@89
|
285
|
cannam@89
|
286 #endregion
|
cannam@89
|
287
|
cannam@89
|
288 }
|