dodan override za lintian
[ossec-hids.git] / src / external / zlib-1.2.8 / contrib / dotzlib / DotZLib / ChecksumImpl.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.Runtime.InteropServices;\r
10 using System.Text;\r
11 \r
12 \r
13 namespace DotZLib\r
14 {\r
15     #region ChecksumGeneratorBase\r
16     /// <summary>\r
17     /// Implements the common functionality needed for all <see cref="ChecksumGenerator"/>s\r
18     /// </summary>\r
19     /// <example></example>\r
20     public abstract class ChecksumGeneratorBase : ChecksumGenerator\r
21     {\r
22         /// <summary>\r
23         /// The value of the current checksum\r
24         /// </summary>\r
25         protected uint _current;\r
26 \r
27         /// <summary>\r
28         /// Initializes a new instance of the checksum generator base - the current checksum is\r
29         /// set to zero\r
30         /// </summary>\r
31         public ChecksumGeneratorBase()\r
32         {\r
33             _current = 0;\r
34         }\r
35 \r
36         /// <summary>\r
37         /// Initializes a new instance of the checksum generator basewith a specified value\r
38         /// </summary>\r
39         /// <param name="initialValue">The value to set the current checksum to</param>\r
40         public ChecksumGeneratorBase(uint initialValue)\r
41         {\r
42             _current = initialValue;\r
43         }\r
44 \r
45         /// <summary>\r
46         /// Resets the current checksum to zero\r
47         /// </summary>\r
48         public void Reset() { _current = 0; }\r
49 \r
50         /// <summary>\r
51         /// Gets the current checksum value\r
52         /// </summary>\r
53         public uint Value { get { return _current; } }\r
54 \r
55         /// <summary>\r
56         /// Updates the current checksum with part of an array of bytes\r
57         /// </summary>\r
58         /// <param name="data">The data to update the checksum with</param>\r
59         /// <param name="offset">Where in <c>data</c> to start updating</param>\r
60         /// <param name="count">The number of bytes from <c>data</c> to use</param>\r
61         /// <exception cref="ArgumentException">The sum of offset and count is larger than the length of <c>data</c></exception>\r
62         /// <exception cref="NullReferenceException"><c>data</c> is a null reference</exception>\r
63         /// <exception cref="ArgumentOutOfRangeException">Offset or count is negative.</exception>\r
64         /// <remarks>All the other <c>Update</c> methods are implmeneted in terms of this one.\r
65         /// This is therefore the only method a derived class has to implement</remarks>\r
66         public abstract void Update(byte[] data, int offset, int count);\r
67 \r
68         /// <summary>\r
69         /// Updates the current checksum with an array of bytes.\r
70         /// </summary>\r
71         /// <param name="data">The data to update the checksum with</param>\r
72         public void Update(byte[] data)\r
73         {\r
74             Update(data, 0, data.Length);\r
75         }\r
76 \r
77         /// <summary>\r
78         /// Updates the current checksum with the data from a string\r
79         /// </summary>\r
80         /// <param name="data">The string to update the checksum with</param>\r
81         /// <remarks>The characters in the string are converted by the UTF-8 encoding</remarks>\r
82         public void Update(string data)\r
83         {\r
84                         Update(Encoding.UTF8.GetBytes(data));\r
85         }\r
86 \r
87         /// <summary>\r
88         /// Updates the current checksum with the data from a string, using a specific encoding\r
89         /// </summary>\r
90         /// <param name="data">The string to update the checksum with</param>\r
91         /// <param name="encoding">The encoding to use</param>\r
92         public void Update(string data, Encoding encoding)\r
93         {\r
94             Update(encoding.GetBytes(data));\r
95         }\r
96 \r
97     }\r
98     #endregion\r
99 \r
100     #region CRC32\r
101     /// <summary>\r
102     /// Implements a CRC32 checksum generator\r
103     /// </summary>\r
104     public sealed class CRC32Checksum : ChecksumGeneratorBase\r
105     {\r
106         #region DLL imports\r
107 \r
108         [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]\r
109         private static extern uint crc32(uint crc, int data, uint length);\r
110 \r
111         #endregion\r
112 \r
113         /// <summary>\r
114         /// Initializes a new instance of the CRC32 checksum generator\r
115         /// </summary>\r
116         public CRC32Checksum() : base() {}\r
117 \r
118         /// <summary>\r
119         /// Initializes a new instance of the CRC32 checksum generator with a specified value\r
120         /// </summary>\r
121         /// <param name="initialValue">The value to set the current checksum to</param>\r
122         public CRC32Checksum(uint initialValue) : base(initialValue) {}\r
123 \r
124         /// <summary>\r
125         /// Updates the current checksum with part of an array of bytes\r
126         /// </summary>\r
127         /// <param name="data">The data to update the checksum with</param>\r
128         /// <param name="offset">Where in <c>data</c> to start updating</param>\r
129         /// <param name="count">The number of bytes from <c>data</c> to use</param>\r
130         /// <exception cref="ArgumentException">The sum of offset and count is larger than the length of <c>data</c></exception>\r
131         /// <exception cref="NullReferenceException"><c>data</c> is a null reference</exception>\r
132         /// <exception cref="ArgumentOutOfRangeException">Offset or count is negative.</exception>\r
133         public override void Update(byte[] data, int offset, int count)\r
134         {\r
135             if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();\r
136             if ((offset+count) > data.Length) throw new ArgumentException();\r
137             GCHandle hData = GCHandle.Alloc(data, GCHandleType.Pinned);\r
138             try\r
139             {\r
140                 _current = crc32(_current, hData.AddrOfPinnedObject().ToInt32()+offset, (uint)count);\r
141             }\r
142             finally\r
143             {\r
144                 hData.Free();\r
145             }\r
146         }\r
147 \r
148     }\r
149     #endregion\r
150 \r
151     #region Adler\r
152     /// <summary>\r
153     /// Implements a checksum generator that computes the Adler checksum on data\r
154     /// </summary>\r
155     public sealed class AdlerChecksum : ChecksumGeneratorBase\r
156     {\r
157         #region DLL imports\r
158 \r
159         [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]\r
160         private static extern uint adler32(uint adler, int data, uint length);\r
161 \r
162         #endregion\r
163 \r
164         /// <summary>\r
165         /// Initializes a new instance of the Adler checksum generator\r
166         /// </summary>\r
167         public AdlerChecksum() : base() {}\r
168 \r
169         /// <summary>\r
170         /// Initializes a new instance of the Adler checksum generator with a specified value\r
171         /// </summary>\r
172         /// <param name="initialValue">The value to set the current checksum to</param>\r
173         public AdlerChecksum(uint initialValue) : base(initialValue) {}\r
174 \r
175         /// <summary>\r
176         /// Updates the current checksum with part of an array of bytes\r
177         /// </summary>\r
178         /// <param name="data">The data to update the checksum with</param>\r
179         /// <param name="offset">Where in <c>data</c> to start updating</param>\r
180         /// <param name="count">The number of bytes from <c>data</c> to use</param>\r
181         /// <exception cref="ArgumentException">The sum of offset and count is larger than the length of <c>data</c></exception>\r
182         /// <exception cref="NullReferenceException"><c>data</c> is a null reference</exception>\r
183         /// <exception cref="ArgumentOutOfRangeException">Offset or count is negative.</exception>\r
184         public override void Update(byte[] data, int offset, int count)\r
185         {\r
186             if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();\r
187             if ((offset+count) > data.Length) throw new ArgumentException();\r
188             GCHandle hData = GCHandle.Alloc(data, GCHandleType.Pinned);\r
189             try\r
190             {\r
191                 _current = adler32(_current, hData.AddrOfPinnedObject().ToInt32()+offset, (uint)count);\r
192             }\r
193             finally\r
194             {\r
195                 hData.Free();\r
196             }\r
197         }\r
198 \r
199     }\r
200     #endregion\r
201 \r
202 }