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