Merge branch 'master' of ssh://git.samba.org/data/git/samba into selftest
[kai/samba.git] / source4 / lib / zlib / contrib / dotzlib / DotZLib / DotZLib.cs
1 //\r
2 // © Copyright Henrik Ravn 2004\r
3 //\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
6 //\r
7 \r
8 using System;\r
9 using System.IO;\r
10 using System.Runtime.InteropServices;\r
11 using System.Text;\r
12 \r
13 \r
14 namespace DotZLib\r
15 {\r
16 \r
17     #region Internal types\r
18 \r
19     /// <summary>\r
20     /// Defines constants for the various flush types used with zlib\r
21     /// </summary>\r
22     internal enum FlushTypes \r
23     {\r
24         None,  Partial,  Sync,  Full,  Finish,  Block\r
25     }\r
26 \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
31     {\r
32         public IntPtr next_in;\r
33         public uint avail_in;\r
34         public uint total_in;\r
35 \r
36         public IntPtr next_out;\r
37         public uint avail_out;\r
38         public uint total_out;\r
39 \r
40         [MarshalAs(UnmanagedType.LPStr)]\r
41         string msg; \r
42         uint state;\r
43 \r
44         uint zalloc;\r
45         uint zfree;\r
46         uint opaque;\r
47 \r
48         int data_type;\r
49         public uint adler;\r
50         uint reserved;\r
51     }\r
52 \r
53     #endregion\r
54     \r
55     #endregion\r
56 \r
57     #region Public enums\r
58     /// <summary>\r
59     /// Defines constants for the available compression levels in zlib\r
60     /// </summary>\r
61     public enum CompressLevel : int\r
62     {\r
63         /// <summary>\r
64         /// The default compression level with a reasonable compromise between compression and speed\r
65         /// </summary>\r
66         Default = -1,   \r
67         /// <summary>\r
68         /// No compression at all. The data are passed straight through.\r
69         /// </summary>\r
70         None = 0,\r
71         /// <summary>\r
72         /// The maximum compression rate available.\r
73         /// </summary>\r
74         Best = 9,   \r
75         /// <summary>\r
76         /// The fastest available compression level.\r
77         /// </summary>\r
78         Fastest = 1\r
79     }\r
80     #endregion\r
81 \r
82     #region Exception classes\r
83     /// <summary>\r
84     /// The exception that is thrown when an error occurs on the zlib dll\r
85     /// </summary>\r
86     public class ZLibException : ApplicationException\r
87     {\r
88         /// <summary>\r
89         /// Initializes a new instance of the <see cref="ZLibException"/> class with a specified \r
90         /// error message and error code\r
91         /// </summary>\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
95         {\r
96         }\r
97 \r
98         /// <summary>\r
99         /// Initializes a new instance of the <see cref="ZLibException"/> class with a specified \r
100         /// error code\r
101         /// </summary>\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
104         {\r
105         }\r
106     }\r
107     #endregion\r
108 \r
109     #region Interfaces\r
110 \r
111     /// <summary>\r
112     /// Declares methods and properties that enables a running checksum to be calculated \r
113     /// </summary>\r
114     public interface ChecksumGenerator\r
115     {\r
116         /// <summary>\r
117         /// Gets the current value of the checksum\r
118         /// </summary>\r
119         uint Value { get; }\r
120 \r
121         /// <summary>\r
122         /// Clears the current checksum to 0\r
123         /// </summary>\r
124         void Reset();\r
125 \r
126         /// <summary>\r
127         /// Updates the current checksum with an array of bytes\r
128         /// </summary>\r
129         /// <param name="data">The data to update the checksum with</param>\r
130         void Update(byte[] data);\r
131 \r
132         /// <summary>\r
133         /// Updates the current checksum with part of an array of bytes\r
134         /// </summary>\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
142 \r
143         /// <summary>\r
144         /// Updates the current checksum with the data from a string\r
145         /// </summary>\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
149 \r
150         /// <summary>\r
151         /// Updates the current checksum with the data from a string, using a specific encoding\r
152         /// </summary>\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
156     }\r
157 \r
158 \r
159     /// <summary>\r
160     /// Represents the method that will be called from a codec when new data\r
161     /// are available.\r
162     /// </summary>\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
168     /// </remarks>\r
169     public delegate void DataAvailableHandler(byte[] data, int startIndex, int count);\r
170 \r
171     /// <summary>\r
172     /// Declares methods and events for implementing compressors/decompressors\r
173     /// </summary>\r
174     public interface Codec\r
175     {\r
176         /// <summary>\r
177         /// Occurs when more processed data are available.\r
178         /// </summary>\r
179         event DataAvailableHandler DataAvailable;\r
180 \r
181         /// <summary>\r
182         /// Adds more data to the codec to be processed.\r
183         /// </summary>\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
187 \r
188         /// <summary>\r
189         /// Adds more data to the codec to be processed.\r
190         /// </summary>\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
196 \r
197         /// <summary>\r
198         /// Finishes up any pending data that needs to be processed and handled.\r
199         /// </summary>\r
200         void Finish();\r
201 \r
202         /// <summary>\r
203         /// Gets the checksum of the data that has been added so far\r
204         /// </summary>\r
205         uint Checksum { get; }\r
206 \r
207 \r
208     }\r
209 \r
210     #endregion\r
211 \r
212     #region Classes\r
213     /// <summary>\r
214     /// Encapsulates general information about the ZLib library\r
215     /// </summary>\r
216     public class Info\r
217     {\r
218         #region DLL imports\r
219         [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]\r
220         private static extern uint zlibCompileFlags();\r
221 \r
222         [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]\r
223         private static extern string zlibVersion();\r
224         #endregion\r
225 \r
226         #region Private stuff\r
227         private uint _flags;\r
228 \r
229         // helper function that unpacks a bitsize mask\r
230         private static int bitSize(uint bits)\r
231         {\r
232             switch (bits)\r
233             {\r
234                 case 0: return 16;\r
235                 case 1: return 32;\r
236                 case 2: return 64;\r
237             }\r
238             return -1;\r
239         }\r
240         #endregion\r
241 \r
242         /// <summary>\r
243         /// Constructs an instance of the <c>Info</c> class.\r
244         /// </summary>\r
245         public Info()\r
246         {\r
247             _flags = zlibCompileFlags();\r
248         }\r
249 \r
250         /// <summary>\r
251         /// True if the library is compiled with debug info\r
252         /// </summary>\r
253         public bool HasDebugInfo { get { return 0 != (_flags & 0x100); } }\r
254 \r
255         /// <summary>\r
256         /// True if the library is compiled with assembly optimizations\r
257         /// </summary>\r
258         public bool UsesAssemblyCode { get { return 0 != (_flags & 0x200); } }\r
259 \r
260         /// <summary>\r
261         /// Gets the size of the unsigned int that was compiled into Zlib\r
262         /// </summary>\r
263         public int SizeOfUInt { get { return bitSize(_flags & 3); } }\r
264 \r
265         /// <summary>\r
266         /// Gets the size of the unsigned long that was compiled into Zlib\r
267         /// </summary>\r
268         public int SizeOfULong { get { return bitSize((_flags >> 2) & 3); } }\r
269 \r
270         /// <summary>\r
271         /// Gets the size of the pointers that were compiled into Zlib\r
272         /// </summary>\r
273         public int SizeOfPointer { get { return bitSize((_flags >> 4) & 3); } }\r
274 \r
275         /// <summary>\r
276         /// Gets the size of the z_off_t type that was compiled into Zlib\r
277         /// </summary>\r
278         public int SizeOfOffset { get { return bitSize((_flags >> 6) & 3); } }\r
279 \r
280         /// <summary>\r
281         /// Gets the version of ZLib as a string, e.g. "1.2.1"\r
282         /// </summary>\r
283         public static string Version { get { return zlibVersion(); } }\r
284     }\r
285 \r
286     #endregion\r
287 \r
288 }\r