annotate src/zlib-1.2.8/contrib/dotzlib/DotZLib/ChecksumImpl.cs @ 56:af97cad61ff0

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