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
9 using System.Runtime.InteropServices;
\r
15 #region ChecksumGeneratorBase
\r
17 /// Implements the common functionality needed for all <see cref="ChecksumGenerator"/>s
\r
19 /// <example></example>
\r
20 public abstract class ChecksumGeneratorBase : ChecksumGenerator
\r
23 /// The value of the current checksum
\r
25 protected uint _current;
\r
28 /// Initializes a new instance of the checksum generator base - the current checksum is
\r
31 public ChecksumGeneratorBase()
\r
37 /// Initializes a new instance of the checksum generator basewith a specified value
\r
39 /// <param name="initialValue">The value to set the current checksum to</param>
\r
40 public ChecksumGeneratorBase(uint initialValue)
\r
42 _current = initialValue;
\r
46 /// Resets the current checksum to zero
\r
48 public void Reset() { _current = 0; }
\r
51 /// Gets the current checksum value
\r
53 public uint Value { get { return _current; } }
\r
56 /// Updates the current checksum with part of an array of bytes
\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
69 /// Updates the current checksum with an array of bytes.
\r
71 /// <param name="data">The data to update the checksum with</param>
\r
72 public void Update(byte[] data)
\r
74 Update(data, 0, data.Length);
\r
78 /// Updates the current checksum with the data from a string
\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
84 Update(Encoding.UTF8.GetBytes(data));
\r
88 /// Updates the current checksum with the data from a string, using a specific encoding
\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
94 Update(encoding.GetBytes(data));
\r
102 /// Implements a CRC32 checksum generator
\r
104 public sealed class CRC32Checksum : ChecksumGeneratorBase
\r
106 #region DLL imports
\r
108 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
\r
109 private static extern uint crc32(uint crc, int data, uint length);
\r
114 /// Initializes a new instance of the CRC32 checksum generator
\r
116 public CRC32Checksum() : base() {}
\r
119 /// Initializes a new instance of the CRC32 checksum generator with a specified value
\r
121 /// <param name="initialValue">The value to set the current checksum to</param>
\r
122 public CRC32Checksum(uint initialValue) : base(initialValue) {}
\r
125 /// Updates the current checksum with part of an array of bytes
\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
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
140 _current = crc32(_current, hData.AddrOfPinnedObject().ToInt32()+offset, (uint)count);
\r
153 /// Implements a checksum generator that computes the Adler checksum on data
\r
155 public sealed class AdlerChecksum : ChecksumGeneratorBase
\r
157 #region DLL imports
\r
159 [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
\r
160 private static extern uint adler32(uint adler, int data, uint length);
\r
165 /// Initializes a new instance of the Adler checksum generator
\r
167 public AdlerChecksum() : base() {}
\r
170 /// Initializes a new instance of the Adler checksum generator with a specified value
\r
172 /// <param name="initialValue">The value to set the current checksum to</param>
\r
173 public AdlerChecksum(uint initialValue) : base(initialValue) {}
\r
176 /// Updates the current checksum with part of an array of bytes
\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
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
191 _current = adler32(_current, hData.AddrOfPinnedObject().ToInt32()+offset, (uint)count);
\r