mirror of
https://github.com/aaru-dps/Aaru.Checksums.git
synced 2025-12-16 19:24:29 +00:00
General code refactor.
This commit is contained in:
@@ -37,6 +37,7 @@ using Aaru.Helpers;
|
||||
|
||||
namespace Aaru.Checksums
|
||||
{
|
||||
/// <inheritdoc />
|
||||
/// <summary>Implements the Adler-32 algorithm</summary>
|
||||
public sealed class Adler32Context : IChecksum
|
||||
{
|
||||
|
||||
@@ -45,21 +45,32 @@ namespace Aaru.Checksums
|
||||
static byte[] _eccBTable;
|
||||
static uint[] _edcTable;
|
||||
|
||||
/// <summary>
|
||||
/// Checks the EDC and ECC of a CD sector
|
||||
/// </summary>
|
||||
/// <summary>Checks the EDC and ECC of a CD sector</summary>
|
||||
/// <param name="buffer">CD sector</param>
|
||||
/// <returns><c>true</c> if all checks were correct, <c>false</c> if any of them weren't, and <c>null</c> if none of them are present.</returns>
|
||||
/// <returns>
|
||||
/// <c>true</c> if all checks were correct, <c>false</c> if any of them weren't, and <c>null</c> if none of them
|
||||
/// are present.
|
||||
/// </returns>
|
||||
public static bool? CheckCdSector(byte[] buffer) => CheckCdSector(buffer, out _, out _, out _);
|
||||
|
||||
/// <summary>
|
||||
/// Checks the EDC and ECC of a CD sector
|
||||
/// </summary>
|
||||
/// <summary>Checks the EDC and ECC of a CD sector</summary>
|
||||
/// <param name="buffer">CD sector</param>
|
||||
/// <param name="correctEccP"><c>true</c> if ECC P is correct, <c>false</c> if it isn't, and <c>null</c> if there is no ECC P in sector.</param>
|
||||
/// <param name="correctEccQ"><c>true</c> if ECC Q is correct, <c>false</c> if it isn't, and <c>null</c> if there is no ECC Q in sector.</param>
|
||||
/// <param name="correctEdc"><c>true</c> if EDC is correct, <c>false</c> if it isn't, and <c>null</c> if there is no EDC in sector.</param>
|
||||
/// <returns><c>true</c> if all checks were correct, <c>false</c> if any of them weren't, and <c>null</c> if none of them are present.</returns>
|
||||
/// <param name="correctEccP">
|
||||
/// <c>true</c> if ECC P is correct, <c>false</c> if it isn't, and <c>null</c> if there is no ECC
|
||||
/// P in sector.
|
||||
/// </param>
|
||||
/// <param name="correctEccQ">
|
||||
/// <c>true</c> if ECC Q is correct, <c>false</c> if it isn't, and <c>null</c> if there is no ECC
|
||||
/// Q in sector.
|
||||
/// </param>
|
||||
/// <param name="correctEdc">
|
||||
/// <c>true</c> if EDC is correct, <c>false</c> if it isn't, and <c>null</c> if there is no EDC in
|
||||
/// sector.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if all checks were correct, <c>false</c> if any of them weren't, and <c>null</c> if none of them
|
||||
/// are present.
|
||||
/// </returns>
|
||||
public static bool? CheckCdSector(byte[] buffer, out bool? correctEccP, out bool? correctEccQ,
|
||||
out bool? correctEdc)
|
||||
{
|
||||
|
||||
@@ -33,9 +33,7 @@
|
||||
namespace Aaru.Checksums
|
||||
{
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Implements the CRC16 algorithm with CCITT polynomial and seed
|
||||
/// </summary>
|
||||
/// <summary>Implements the CRC16 algorithm with CCITT polynomial and seed</summary>
|
||||
public sealed class CRC16CCITTContext : Crc16Context
|
||||
{
|
||||
/// <summary>CCITT CRC16 polynomial</summary>
|
||||
@@ -66,9 +64,7 @@ namespace Aaru.Checksums
|
||||
0x9ff8, 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of the CRC16 with CCITT polynomial and seed.
|
||||
/// </summary>
|
||||
/// <summary>Initializes an instance of the CRC16 with CCITT polynomial and seed.</summary>
|
||||
/// <inheritdoc />
|
||||
public CRC16CCITTContext() : base(CRC16_CCITT_POLY, CRC16_CCITT_SEED, _ccittCrc16Table, true) {}
|
||||
|
||||
@@ -99,9 +95,7 @@ namespace Aaru.Checksums
|
||||
/// <param name="hash">Byte array of the hash value.</param>
|
||||
public static string Data(byte[] data, out byte[] hash) => Data(data, (uint)data.Length, out hash);
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the CCITT CRC16 of the specified buffer with the specified parameters
|
||||
/// </summary>
|
||||
/// <summary>Calculates the CCITT CRC16 of the specified buffer with the specified parameters</summary>
|
||||
/// <param name="buffer">Buffer</param>
|
||||
public static ushort Calculate(byte[] buffer) =>
|
||||
Calculate(buffer, CRC16_CCITT_POLY, CRC16_CCITT_SEED, _ccittCrc16Table, true);
|
||||
|
||||
@@ -113,7 +113,7 @@ namespace Aaru.Checksums
|
||||
if((entry & 1) == 1)
|
||||
entry = (entry >> 1) ^ polynomial;
|
||||
else
|
||||
entry = entry >> 1;
|
||||
entry >>= 1;
|
||||
|
||||
table[i] = (ushort)entry;
|
||||
}
|
||||
@@ -215,9 +215,7 @@ namespace Aaru.Checksums
|
||||
return crc16Output.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the CRC16 of the specified buffer with the specified parameters
|
||||
/// </summary>
|
||||
/// <summary>Calculates the CRC16 of the specified buffer with the specified parameters</summary>
|
||||
/// <param name="buffer">Buffer</param>
|
||||
/// <param name="polynomial">Polynomial</param>
|
||||
/// <param name="seed">Seed</param>
|
||||
|
||||
@@ -33,9 +33,7 @@
|
||||
namespace Aaru.Checksums
|
||||
{
|
||||
/// <inheritdoc />
|
||||
/// <summary>
|
||||
/// Implements the CRC16 algorithm with IBM polynomial and seed
|
||||
/// </summary>
|
||||
/// <summary>Implements the CRC16 algorithm with IBM polynomial and seed</summary>
|
||||
public sealed class CRC16IBMContext : Crc16Context
|
||||
{
|
||||
const ushort CRC16_IBM_POLY = 0xA001;
|
||||
@@ -65,9 +63,7 @@ namespace Aaru.Checksums
|
||||
0x8641, 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of the CRC16 with IBM polynomial and seed.
|
||||
/// </summary>
|
||||
/// <summary>Initializes an instance of the CRC16 with IBM polynomial and seed.</summary>
|
||||
/// <inheritdoc />
|
||||
public CRC16IBMContext() : base(CRC16_IBM_POLY, CRC16_IBM_SEED, _ibmCrc16Table, false) {}
|
||||
|
||||
@@ -98,9 +94,7 @@ namespace Aaru.Checksums
|
||||
/// <param name="hash">Byte array of the hash value.</param>
|
||||
public static string Data(byte[] data, out byte[] hash) => Data(data, (uint)data.Length, out hash);
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the IBM CRC16 of the specified buffer with the specified parameters
|
||||
/// </summary>
|
||||
/// <summary>Calculates the IBM CRC16 of the specified buffer with the specified parameters</summary>
|
||||
/// <param name="buffer">Buffer</param>
|
||||
public static ushort Calculate(byte[] buffer) =>
|
||||
Calculate(buffer, CRC16_IBM_POLY, CRC16_IBM_SEED, _ibmCrc16Table, false);
|
||||
|
||||
@@ -37,6 +37,7 @@ using Aaru.Helpers;
|
||||
|
||||
namespace Aaru.Checksums
|
||||
{
|
||||
/// <inheritdoc />
|
||||
/// <summary>Implements a CRC32 algorithm</summary>
|
||||
public sealed class Crc32Context : IChecksum
|
||||
{
|
||||
@@ -63,7 +64,7 @@ namespace Aaru.Checksums
|
||||
if((entry & 1) == 1)
|
||||
entry = (entry >> 1) ^ CRC32_ISO_POLY;
|
||||
else
|
||||
entry = entry >> 1;
|
||||
entry >>= 1;
|
||||
|
||||
_table[i] = entry;
|
||||
}
|
||||
@@ -85,7 +86,7 @@ namespace Aaru.Checksums
|
||||
if((entry & 1) == 1)
|
||||
entry = (entry >> 1) ^ polynomial;
|
||||
else
|
||||
entry = entry >> 1;
|
||||
entry >>= 1;
|
||||
|
||||
_table[i] = entry;
|
||||
}
|
||||
@@ -146,7 +147,7 @@ namespace Aaru.Checksums
|
||||
{
|
||||
var fileStream = new FileStream(filename, FileMode.Open);
|
||||
|
||||
uint localhashInt = seed;
|
||||
uint localHashInt = seed;
|
||||
|
||||
uint[] localTable = new uint[256];
|
||||
|
||||
@@ -158,16 +159,16 @@ namespace Aaru.Checksums
|
||||
if((entry & 1) == 1)
|
||||
entry = (entry >> 1) ^ polynomial;
|
||||
else
|
||||
entry = entry >> 1;
|
||||
entry >>= 1;
|
||||
|
||||
localTable[i] = entry;
|
||||
}
|
||||
|
||||
for(int i = 0; i < fileStream.Length; i++)
|
||||
localhashInt = (localhashInt >> 8) ^ localTable[fileStream.ReadByte() ^ (localhashInt & 0xff)];
|
||||
localHashInt = (localHashInt >> 8) ^ localTable[fileStream.ReadByte() ^ (localHashInt & 0xff)];
|
||||
|
||||
localhashInt ^= seed;
|
||||
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
||||
localHashInt ^= seed;
|
||||
hash = BigEndianBitConverter.GetBytes(localHashInt);
|
||||
|
||||
var crc32Output = new StringBuilder();
|
||||
|
||||
@@ -194,7 +195,7 @@ namespace Aaru.Checksums
|
||||
/// <param name="seed">CRC seed</param>
|
||||
public static string Data(byte[] data, uint len, out byte[] hash, uint polynomial, uint seed)
|
||||
{
|
||||
uint localhashInt = seed;
|
||||
uint localHashInt = seed;
|
||||
|
||||
uint[] localTable = new uint[256];
|
||||
|
||||
@@ -206,16 +207,16 @@ namespace Aaru.Checksums
|
||||
if((entry & 1) == 1)
|
||||
entry = (entry >> 1) ^ polynomial;
|
||||
else
|
||||
entry = entry >> 1;
|
||||
entry >>= 1;
|
||||
|
||||
localTable[i] = entry;
|
||||
}
|
||||
|
||||
for(int i = 0; i < len; i++)
|
||||
localhashInt = (localhashInt >> 8) ^ localTable[data[i] ^ (localhashInt & 0xff)];
|
||||
localHashInt = (localHashInt >> 8) ^ localTable[data[i] ^ (localHashInt & 0xff)];
|
||||
|
||||
localhashInt ^= seed;
|
||||
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
||||
localHashInt ^= seed;
|
||||
hash = BigEndianBitConverter.GetBytes(localHashInt);
|
||||
|
||||
var crc32Output = new StringBuilder();
|
||||
|
||||
|
||||
@@ -41,13 +41,9 @@ namespace Aaru.Checksums
|
||||
/// <summary>Implements a CRC64 algorithm</summary>
|
||||
public sealed class Crc64Context : IChecksum
|
||||
{
|
||||
/// <summary>
|
||||
/// ECMA CRC64 polynomial
|
||||
/// </summary>
|
||||
/// <summary>ECMA CRC64 polynomial</summary>
|
||||
public const ulong CRC64_ECMA_POLY = 0xC96C5795D7870F42;
|
||||
/// <summary>
|
||||
/// ECMA CRC64 seed
|
||||
/// </summary>
|
||||
/// <summary>ECMA CRC64 seed</summary>
|
||||
public const ulong CRC64_ECMA_SEED = 0xFFFFFFFFFFFFFFFF;
|
||||
|
||||
readonly ulong _finalSeed;
|
||||
@@ -69,7 +65,7 @@ namespace Aaru.Checksums
|
||||
if((entry & 1) == 1)
|
||||
entry = (entry >> 1) ^ CRC64_ECMA_POLY;
|
||||
else
|
||||
entry = entry >> 1;
|
||||
entry >>= 1;
|
||||
|
||||
_table[i] = entry;
|
||||
}
|
||||
@@ -92,7 +88,7 @@ namespace Aaru.Checksums
|
||||
if((entry & 1) == 1)
|
||||
entry = (entry >> 1) ^ polynomial;
|
||||
else
|
||||
entry = entry >> 1;
|
||||
entry >>= 1;
|
||||
|
||||
_table[i] = entry;
|
||||
}
|
||||
@@ -155,7 +151,7 @@ namespace Aaru.Checksums
|
||||
{
|
||||
var fileStream = new FileStream(filename, FileMode.Open);
|
||||
|
||||
ulong localhashInt = seed;
|
||||
ulong localHashInt = seed;
|
||||
|
||||
ulong[] localTable = new ulong[256];
|
||||
|
||||
@@ -167,16 +163,16 @@ namespace Aaru.Checksums
|
||||
if((entry & 1) == 1)
|
||||
entry = (entry >> 1) ^ polynomial;
|
||||
else
|
||||
entry = entry >> 1;
|
||||
entry >>= 1;
|
||||
|
||||
localTable[i] = entry;
|
||||
}
|
||||
|
||||
for(int i = 0; i < fileStream.Length; i++)
|
||||
localhashInt = (localhashInt >> 8) ^ localTable[(ulong)fileStream.ReadByte() ^ (localhashInt & 0xffL)];
|
||||
localHashInt = (localHashInt >> 8) ^ localTable[(ulong)fileStream.ReadByte() ^ (localHashInt & 0xffL)];
|
||||
|
||||
localhashInt ^= seed;
|
||||
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
||||
localHashInt ^= seed;
|
||||
hash = BigEndianBitConverter.GetBytes(localHashInt);
|
||||
|
||||
var crc64Output = new StringBuilder();
|
||||
|
||||
@@ -203,7 +199,7 @@ namespace Aaru.Checksums
|
||||
/// <param name="seed">CRC seed</param>
|
||||
public static string Data(byte[] data, uint len, out byte[] hash, ulong polynomial, ulong seed)
|
||||
{
|
||||
ulong localhashInt = seed;
|
||||
ulong localHashInt = seed;
|
||||
|
||||
ulong[] localTable = new ulong[256];
|
||||
|
||||
@@ -215,16 +211,16 @@ namespace Aaru.Checksums
|
||||
if((entry & 1) == 1)
|
||||
entry = (entry >> 1) ^ polynomial;
|
||||
else
|
||||
entry = entry >> 1;
|
||||
entry >>= 1;
|
||||
|
||||
localTable[i] = entry;
|
||||
}
|
||||
|
||||
for(int i = 0; i < len; i++)
|
||||
localhashInt = (localhashInt >> 8) ^ localTable[data[i] ^ (localhashInt & 0xff)];
|
||||
localHashInt = (localHashInt >> 8) ^ localTable[data[i] ^ (localHashInt & 0xff)];
|
||||
|
||||
localhashInt ^= seed;
|
||||
hash = BigEndianBitConverter.GetBytes(localhashInt);
|
||||
localHashInt ^= seed;
|
||||
hash = BigEndianBitConverter.GetBytes(localHashInt);
|
||||
|
||||
var crc64Output = new StringBuilder();
|
||||
|
||||
|
||||
@@ -164,6 +164,7 @@ namespace Aaru.Checksums
|
||||
public static string Data(byte[] data, out byte[] hash) => Data(data, (uint)data.Length, out hash);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>Implements the Fletcher-16 algorithm</summary>
|
||||
public sealed class Fletcher16Context : IChecksum
|
||||
{
|
||||
|
||||
@@ -48,8 +48,7 @@ namespace Aaru.Checksums
|
||||
public sealed class Register : IPluginRegister
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public List<Type> GetAllChecksumPlugins() => Assembly.
|
||||
GetExecutingAssembly().GetTypes().
|
||||
public List<Type> GetAllChecksumPlugins() => Assembly.GetExecutingAssembly().GetTypes().
|
||||
Where(t => t.GetInterfaces().Contains(typeof(IChecksum))).
|
||||
Where(t => t.IsClass).ToList();
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@ using Aaru.CommonTypes.Interfaces;
|
||||
|
||||
namespace Aaru.Checksums
|
||||
{
|
||||
/// <inheritdoc />
|
||||
/// <summary>Wraps up .NET SHA1 implementation to a Init(), Update(), Final() context.</summary>
|
||||
public sealed class Sha1Context : IChecksum
|
||||
{
|
||||
|
||||
@@ -38,6 +38,7 @@ using Aaru.CommonTypes.Interfaces;
|
||||
|
||||
namespace Aaru.Checksums
|
||||
{
|
||||
/// <inheritdoc />
|
||||
/// <summary>Wraps up .NET SHA256 implementation to a Init(), Update(), Final() context.</summary>
|
||||
public sealed class Sha256Context : IChecksum
|
||||
{
|
||||
|
||||
@@ -38,6 +38,7 @@ using Aaru.CommonTypes.Interfaces;
|
||||
|
||||
namespace Aaru.Checksums
|
||||
{
|
||||
/// <inheritdoc />
|
||||
/// <summary>Wraps up .NET SHA384 implementation to a Init(), Update(), Final() context.</summary>
|
||||
public sealed class Sha384Context : IChecksum
|
||||
{
|
||||
|
||||
@@ -38,6 +38,7 @@ using Aaru.CommonTypes.Interfaces;
|
||||
|
||||
namespace Aaru.Checksums
|
||||
{
|
||||
/// <inheritdoc />
|
||||
/// <summary>Wraps up .NET SHA512 implementation to a Init(), Update(), Final() context.</summary>
|
||||
public sealed class Sha512Context : IChecksum
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user