Generalize Fletcher base class for all checksums

This commit is contained in:
Matt Nadareski
2024-11-11 01:57:08 -05:00
parent 72a98e3e03
commit 83f008919c
7 changed files with 26 additions and 70 deletions

View File

@@ -4,13 +4,8 @@ using static SabreTools.Hashing.Checksum.Constants;
namespace SabreTools.Hashing.Checksum
{
/// <see href="https://github.com/madler/zlib/blob/v1.2.11/adler32.c"/>
public class Adler32
public class Adler32 : ChecksumBase<uint>
{
/// <summary>
/// The current value of the hash
/// </summary>
private uint _hash;
public Adler32()
{
Reset();
@@ -19,18 +14,13 @@ namespace SabreTools.Hashing.Checksum
/// <summary>
/// Reset the internal hashing state
/// </summary>
public void Reset()
public override void Reset()
{
_hash = 1;
}
/// <summary>
/// Hash a block of data and append it to the existing hash
/// </summary>
/// <param name="data">Byte array representing the data</param>
/// <param name="offset">Offset in the byte array to include</param>
/// <param name="length">Length of the data to hash</param>
public void TransformBlock(byte[] data, int offset, int length)
/// <inheritdoc/>
public override void TransformBlock(byte[] data, int offset, int length)
{
// Split Adler-32 into component sums
uint sum2 = (_hash >> 16) & 0xffff;
@@ -140,10 +130,8 @@ namespace SabreTools.Hashing.Checksum
_hash |= sum2 << 16;
}
/// <summary>
/// Finalize the hash and return as a byte array
/// </summary>
public byte[] Finalize()
/// <inheritdoc/>
public override byte[] Finalize()
{
return BitConverter.GetBytes(_hash);
}

View File

@@ -5,7 +5,7 @@ namespace SabreTools.Hashing.Checksum
/// <summary>
/// Common base class for Fletcher checksums
/// </summary>
public abstract class Fletcher
public abstract class ChecksumBase
{
/// <summary>
/// Hash a block of data and append it to the existing hash
@@ -22,9 +22,9 @@ namespace SabreTools.Hashing.Checksum
}
/// <summary>
/// Common base class for Fletcher checksums
/// Common base class for checksums
/// </summary>
public abstract class Fletcher<T> : Fletcher where T : struct
public abstract class ChecksumBase<T> : ChecksumBase where T : struct
{
/// <summary>
/// The current value of the hash
@@ -34,7 +34,7 @@ namespace SabreTools.Hashing.Checksum
/// <summary>
/// Reset the internal hashing state
/// </summary>
public void Reset()
public virtual void Reset()
{
_hash = default;
}

View File

@@ -3,7 +3,7 @@ using static SabreTools.Hashing.HashOperations;
namespace SabreTools.Hashing.Checksum
{
public class Crc
public class Crc : ChecksumBase<ulong>
{
/// <summary>
/// Definition used to create the runner
@@ -15,11 +15,6 @@ namespace SabreTools.Hashing.Checksum
/// </summary>
private readonly CrcTable _table;
/// <summary>
/// The current value of the hash
/// </summary>
private ulong _hash;
public Crc(CrcDefinition def)
{
// Check for a valid bit width
@@ -31,27 +26,18 @@ namespace SabreTools.Hashing.Checksum
_hash = def.ReflectIn ? ReverseBits(def.Init, def.Width) : def.Init;
}
/// <summary>
/// Reset the internal hashing state
/// </summary>
public void Reset()
/// <inheritdoc/>
public override void Reset()
{
_hash = Def.Init;
}
/// <summary>
/// Hash a block of data and append it to the existing hash
/// </summary>
/// <param name="data">Byte array representing the data</param>
/// <param name="offset">Offset in the byte array to include</param>
/// <param name="length">Length of the data to hash</param>
public void TransformBlock(byte[] data, int offset, int length)
/// <inheritdoc/>
public override void TransformBlock(byte[] data, int offset, int length)
=> _table.TransformBlock(ref _hash, data, offset, length);
/// <summary>
/// Finalize the hash and return as a byte array
/// </summary>
public byte[] Finalize()
/// <inheritdoc/>
public override byte[] Finalize()
{
// Create a copy of the hash
ulong localHash = _hash;

View File

@@ -1,7 +1,7 @@
namespace SabreTools.Hashing.Checksum
{
/// <see href="https://en.wikipedia.org/wiki/Fletcher%27s_checksum#Optimizations"/>
public class Fletcher16 : Fletcher<ushort>
public class Fletcher16 : ChecksumBase<ushort>
{
public Fletcher16()
{

View File

@@ -4,7 +4,7 @@ namespace SabreTools.Hashing.Checksum
{
/// <see href="https://en.wikipedia.org/wiki/Fletcher%27s_checksum#Optimizations"/>
/// <remarks>Uses an Adler-32-like implementation instead of the above</remarks>
public class Fletcher32 : Fletcher<uint>
public class Fletcher32 : ChecksumBase<uint>
{
public Fletcher32()
{

View File

@@ -4,7 +4,7 @@ namespace SabreTools.Hashing.Checksum
{
/// <see href="https://en.wikipedia.org/wiki/Fletcher%27s_checksum#Optimizations"/>
/// <remarks>Uses an Adler-32-like implementation instead of the above</remarks>
public class Fletcher64 : Fletcher<ulong>
public class Fletcher64 : ChecksumBase<ulong>
{
public Fletcher64()
{

View File

@@ -34,20 +34,10 @@ namespace SabreTools.Hashing
{
switch (_hasher)
{
case Adler32 a32:
var a32Arr = a32.Finalize();
Array.Reverse(a32Arr);
return a32Arr;
case Crc cr:
var crArr = cr.Finalize();
Array.Reverse(crArr);
return crArr;
case Fletcher fc:
var fcArr = fc.Finalize();
Array.Reverse(fcArr);
return fcArr;
case ChecksumBase cb:
var cbArr = cb.Finalize();
Array.Reverse(cbArr);
return cbArr;
case HashAlgorithm ha:
return ha.Hash;
@@ -359,16 +349,8 @@ namespace SabreTools.Hashing
{
switch (_hasher)
{
case Adler32 a32:
a32.TransformBlock(buffer, offset, size);
break;
case Crc cr:
cr.TransformBlock(buffer, offset, size);
break;
case Fletcher fc:
fc.TransformBlock(buffer, offset, size);
case ChecksumBase cb:
cb.TransformBlock(buffer, offset, size);
break;
case HashAlgorithm ha: