2 // © Copyright Henrik Ravn 2004
4 // Use, modification and distribution are subject to the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 using System
.Runtime
.InteropServices
;
15 #region ChecksumGeneratorBase
17 /// Implements the common functionality needed for all <see cref="ChecksumGenerator"/>s
19 /// <example></example>
20 public abstract class ChecksumGeneratorBase
: ChecksumGenerator
23 /// The value of the current checksum
25 protected uint _current
;
28 /// Initializes a new instance of the checksum generator base - the current checksum is
31 public ChecksumGeneratorBase()
37 /// Initializes a new instance of the checksum generator basewith a specified value
39 /// <param name="initialValue">The value to set the current checksum to</param>
40 public ChecksumGeneratorBase(uint initialValue
)
42 _current
= initialValue
;
46 /// Resets the current checksum to zero
48 public void Reset() { _current = 0; }
51 /// Gets the current checksum value
53 public uint Value { get { return _current; }
}
56 /// Updates the current checksum with part of an array of bytes
58 /// <param name="data">The data to update the checksum with</param>
59 /// <param name="offset">Where in <c>data</c> to start updating</param>
60 /// <param name="count">The number of bytes from <c>data</c> to use</param>
61 /// <exception cref="ArgumentException">The sum of offset and count is larger than the length of <c>data</c></exception>
62 /// <exception cref="NullReferenceException"><c>data</c> is a null reference</exception>
63 /// <exception cref="ArgumentOutOfRangeException">Offset or count is negative.</exception>
64 /// <remarks>All the other <c>Update</c> methods are implmeneted in terms of this one.
65 /// This is therefore the only method a derived class has to implement</remarks>
66 public abstract void Update(byte[] data
, int offset
, int count
);
69 /// Updates the current checksum with an array of bytes.
71 /// <param name="data">The data to update the checksum with</param>
72 public void Update(byte[] data
)
74 Update(data
, 0, data
.Length
);
78 /// Updates the current checksum with the data from a string
80 /// <param name="data">The string to update the checksum with</param>
81 /// <remarks>The characters in the string are converted by the UTF-8 encoding</remarks>
82 public void Update(string data
)
84 Update(Encoding
.UTF8
.GetBytes(data
));
88 /// Updates the current checksum with the data from a string, using a specific encoding
90 /// <param name="data">The string to update the checksum with</param>
91 /// <param name="encoding">The encoding to use</param>
92 public void Update(string data
, Encoding encoding
)
94 Update(encoding
.GetBytes(data
));
102 /// Implements a CRC32 checksum generator
104 public sealed class CRC32Checksum
: ChecksumGeneratorBase
108 [DllImport("ZLIB1.dll", CallingConvention
=CallingConvention
.Cdecl
)]
109 private static extern uint crc32(uint crc
, int data
, uint length
);
114 /// Initializes a new instance of the CRC32 checksum generator
116 public CRC32Checksum() : base() {}
119 /// Initializes a new instance of the CRC32 checksum generator with a specified value
121 /// <param name="initialValue">The value to set the current checksum to</param>
122 public CRC32Checksum(uint initialValue
) : base(initialValue
) {}
125 /// Updates the current checksum with part of an array of bytes
127 /// <param name="data">The data to update the checksum with</param>
128 /// <param name="offset">Where in <c>data</c> to start updating</param>
129 /// <param name="count">The number of bytes from <c>data</c> to use</param>
130 /// <exception cref="ArgumentException">The sum of offset and count is larger than the length of <c>data</c></exception>
131 /// <exception cref="NullReferenceException"><c>data</c> is a null reference</exception>
132 /// <exception cref="ArgumentOutOfRangeException">Offset or count is negative.</exception>
133 public override void Update(byte[] data
, int offset
, int count
)
135 if (offset
< 0 || count
< 0) throw new ArgumentOutOfRangeException();
136 if ((offset
+count
) > data
.Length
) throw new ArgumentException();
137 GCHandle hData
= GCHandle
.Alloc(data
, GCHandleType
.Pinned
);
140 _current
= crc32(_current
, hData
.AddrOfPinnedObject().ToInt32()+offset
, (uint)count
);
153 /// Implements a checksum generator that computes the Adler checksum on data
155 public sealed class AdlerChecksum
: ChecksumGeneratorBase
159 [DllImport("ZLIB1.dll", CallingConvention
=CallingConvention
.Cdecl
)]
160 private static extern uint adler32(uint adler
, int data
, uint length
);
165 /// Initializes a new instance of the Adler checksum generator
167 public AdlerChecksum() : base() {}
170 /// Initializes a new instance of the Adler checksum generator with a specified value
172 /// <param name="initialValue">The value to set the current checksum to</param>
173 public AdlerChecksum(uint initialValue
) : base(initialValue
) {}
176 /// Updates the current checksum with part of an array of bytes
178 /// <param name="data">The data to update the checksum with</param>
179 /// <param name="offset">Where in <c>data</c> to start updating</param>
180 /// <param name="count">The number of bytes from <c>data</c> to use</param>
181 /// <exception cref="ArgumentException">The sum of offset and count is larger than the length of <c>data</c></exception>
182 /// <exception cref="NullReferenceException"><c>data</c> is a null reference</exception>
183 /// <exception cref="ArgumentOutOfRangeException">Offset or count is negative.</exception>
184 public override void Update(byte[] data
, int offset
, int count
)
186 if (offset
< 0 || count
< 0) throw new ArgumentOutOfRangeException();
187 if ((offset
+count
) > data
.Length
) throw new ArgumentException();
188 GCHandle hData
= GCHandle
.Alloc(data
, GCHandleType
.Pinned
);
191 _current
= adler32(_current
, hData
.AddrOfPinnedObject().ToInt32()+offset
, (uint)count
);