annotate src/zlib-1.2.7/contrib/dotzlib/DotZLib/ChecksumImpl.cs @ 4:e13257ea84a4

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