+++ /dev/null
-//\r
-// © Copyright Henrik Ravn 2004\r
-//\r
-// Use, modification and distribution are subject to the Boost Software License, Version 1.0.\r
-// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)\r
-//\r
-\r
-using System;\r
-using System.Runtime.InteropServices;\r
-using System.Text;\r
-\r
-\r
-namespace DotZLib\r
-{\r
- #region ChecksumGeneratorBase\r
- /// <summary>\r
- /// Implements the common functionality needed for all <see cref="ChecksumGenerator"/>s\r
- /// </summary>\r
- /// <example></example>\r
- public abstract class ChecksumGeneratorBase : ChecksumGenerator\r
- {\r
- /// <summary>\r
- /// The value of the current checksum\r
- /// </summary>\r
- protected uint _current;\r
-\r
- /// <summary>\r
- /// Initializes a new instance of the checksum generator base - the current checksum is\r
- /// set to zero\r
- /// </summary>\r
- public ChecksumGeneratorBase()\r
- {\r
- _current = 0;\r
- }\r
-\r
- /// <summary>\r
- /// Initializes a new instance of the checksum generator basewith a specified value\r
- /// </summary>\r
- /// <param name="initialValue">The value to set the current checksum to</param>\r
- public ChecksumGeneratorBase(uint initialValue)\r
- {\r
- _current = initialValue;\r
- }\r
-\r
- /// <summary>\r
- /// Resets the current checksum to zero\r
- /// </summary>\r
- public void Reset() { _current = 0; }\r
-\r
- /// <summary>\r
- /// Gets the current checksum value\r
- /// </summary>\r
- public uint Value { get { return _current; } }\r
-\r
- /// <summary>\r
- /// Updates the current checksum with part of an array of bytes\r
- /// </summary>\r
- /// <param name="data">The data to update the checksum with</param>\r
- /// <param name="offset">Where in <c>data</c> to start updating</param>\r
- /// <param name="count">The number of bytes from <c>data</c> to use</param>\r
- /// <exception cref="ArgumentException">The sum of offset and count is larger than the length of <c>data</c></exception>\r
- /// <exception cref="NullReferenceException"><c>data</c> is a null reference</exception>\r
- /// <exception cref="ArgumentOutOfRangeException">Offset or count is negative.</exception>\r
- /// <remarks>All the other <c>Update</c> methods are implmeneted in terms of this one.\r
- /// This is therefore the only method a derived class has to implement</remarks>\r
- public abstract void Update(byte[] data, int offset, int count);\r
-\r
- /// <summary>\r
- /// Updates the current checksum with an array of bytes.\r
- /// </summary>\r
- /// <param name="data">The data to update the checksum with</param>\r
- public void Update(byte[] data)\r
- {\r
- Update(data, 0, data.Length);\r
- }\r
-\r
- /// <summary>\r
- /// Updates the current checksum with the data from a string\r
- /// </summary>\r
- /// <param name="data">The string to update the checksum with</param>\r
- /// <remarks>The characters in the string are converted by the UTF-8 encoding</remarks>\r
- public void Update(string data)\r
- {\r
- Update(Encoding.UTF8.GetBytes(data));\r
- }\r
-\r
- /// <summary>\r
- /// Updates the current checksum with the data from a string, using a specific encoding\r
- /// </summary>\r
- /// <param name="data">The string to update the checksum with</param>\r
- /// <param name="encoding">The encoding to use</param>\r
- public void Update(string data, Encoding encoding)\r
- {\r
- Update(encoding.GetBytes(data));\r
- }\r
-\r
- }\r
- #endregion\r
-\r
- #region CRC32\r
- /// <summary>\r
- /// Implements a CRC32 checksum generator\r
- /// </summary>\r
- public sealed class CRC32Checksum : ChecksumGeneratorBase\r
- {\r
- #region DLL imports\r
-\r
- [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]\r
- private static extern uint crc32(uint crc, int data, uint length);\r
-\r
- #endregion\r
-\r
- /// <summary>\r
- /// Initializes a new instance of the CRC32 checksum generator\r
- /// </summary>\r
- public CRC32Checksum() : base() {}\r
-\r
- /// <summary>\r
- /// Initializes a new instance of the CRC32 checksum generator with a specified value\r
- /// </summary>\r
- /// <param name="initialValue">The value to set the current checksum to</param>\r
- public CRC32Checksum(uint initialValue) : base(initialValue) {}\r
-\r
- /// <summary>\r
- /// Updates the current checksum with part of an array of bytes\r
- /// </summary>\r
- /// <param name="data">The data to update the checksum with</param>\r
- /// <param name="offset">Where in <c>data</c> to start updating</param>\r
- /// <param name="count">The number of bytes from <c>data</c> to use</param>\r
- /// <exception cref="ArgumentException">The sum of offset and count is larger than the length of <c>data</c></exception>\r
- /// <exception cref="NullReferenceException"><c>data</c> is a null reference</exception>\r
- /// <exception cref="ArgumentOutOfRangeException">Offset or count is negative.</exception>\r
- public override void Update(byte[] data, int offset, int count)\r
- {\r
- if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();\r
- if ((offset+count) > data.Length) throw new ArgumentException();\r
- GCHandle hData = GCHandle.Alloc(data, GCHandleType.Pinned);\r
- try\r
- {\r
- _current = crc32(_current, hData.AddrOfPinnedObject().ToInt32()+offset, (uint)count);\r
- }\r
- finally\r
- {\r
- hData.Free();\r
- }\r
- }\r
-\r
- }\r
- #endregion\r
-\r
- #region Adler\r
- /// <summary>\r
- /// Implements a checksum generator that computes the Adler checksum on data\r
- /// </summary>\r
- public sealed class AdlerChecksum : ChecksumGeneratorBase\r
- {\r
- #region DLL imports\r
-\r
- [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]\r
- private static extern uint adler32(uint adler, int data, uint length);\r
-\r
- #endregion\r
-\r
- /// <summary>\r
- /// Initializes a new instance of the Adler checksum generator\r
- /// </summary>\r
- public AdlerChecksum() : base() {}\r
-\r
- /// <summary>\r
- /// Initializes a new instance of the Adler checksum generator with a specified value\r
- /// </summary>\r
- /// <param name="initialValue">The value to set the current checksum to</param>\r
- public AdlerChecksum(uint initialValue) : base(initialValue) {}\r
-\r
- /// <summary>\r
- /// Updates the current checksum with part of an array of bytes\r
- /// </summary>\r
- /// <param name="data">The data to update the checksum with</param>\r
- /// <param name="offset">Where in <c>data</c> to start updating</param>\r
- /// <param name="count">The number of bytes from <c>data</c> to use</param>\r
- /// <exception cref="ArgumentException">The sum of offset and count is larger than the length of <c>data</c></exception>\r
- /// <exception cref="NullReferenceException"><c>data</c> is a null reference</exception>\r
- /// <exception cref="ArgumentOutOfRangeException">Offset or count is negative.</exception>\r
- public override void Update(byte[] data, int offset, int count)\r
- {\r
- if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();\r
- if ((offset+count) > data.Length) throw new ArgumentException();\r
- GCHandle hData = GCHandle.Alloc(data, GCHandleType.Pinned);\r
- try\r
- {\r
- _current = adler32(_current, hData.AddrOfPinnedObject().ToInt32()+offset, (uint)count);\r
- }\r
- finally\r
- {\r
- hData.Free();\r
- }\r
- }\r
-\r
- }\r
- #endregion\r
-\r
-}
\ No newline at end of file