annotate src/zlib-1.2.8/contrib/dotzlib/DotZLib/ChecksumImpl.cs @ 168:ceec0dd9ec9c

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