General code refactor.

This commit is contained in:
2021-08-17 21:23:22 +01:00
parent da3cbf28f4
commit 7fdadfa84f
15 changed files with 641 additions and 642 deletions

View File

@@ -37,6 +37,7 @@ using Aaru.Helpers;
namespace Aaru.Checksums namespace Aaru.Checksums
{ {
/// <inheritdoc />
/// <summary>Implements the Adler-32 algorithm</summary> /// <summary>Implements the Adler-32 algorithm</summary>
public sealed class Adler32Context : IChecksum public sealed class Adler32Context : IChecksum
{ {

View File

@@ -45,21 +45,32 @@ namespace Aaru.Checksums
static byte[] _eccBTable; static byte[] _eccBTable;
static uint[] _edcTable; static uint[] _edcTable;
/// <summary> /// <summary>Checks the EDC and ECC of a CD sector</summary>
/// Checks the EDC and ECC of a CD sector
/// </summary>
/// <param name="buffer">CD sector</param> /// <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 _); public static bool? CheckCdSector(byte[] buffer) => CheckCdSector(buffer, out _, out _, out _);
/// <summary> /// <summary>Checks the EDC and ECC of a CD sector</summary>
/// Checks the EDC and ECC of a CD sector
/// </summary>
/// <param name="buffer">CD sector</param> /// <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="correctEccP">
/// <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> /// <c>true</c> if ECC P is correct, <c>false</c> if it isn't, and <c>null</c> if there is no ECC
/// <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> /// P in sector.
/// <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>
/// <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, public static bool? CheckCdSector(byte[] buffer, out bool? correctEccP, out bool? correctEccQ,
out bool? correctEdc) out bool? correctEdc)
{ {

View File

@@ -33,9 +33,7 @@
namespace Aaru.Checksums namespace Aaru.Checksums
{ {
/// <inheritdoc /> /// <inheritdoc />
/// <summary> /// <summary>Implements the CRC16 algorithm with CCITT polynomial and seed</summary>
/// Implements the CRC16 algorithm with CCITT polynomial and seed
/// </summary>
public sealed class CRC16CCITTContext : Crc16Context public sealed class CRC16CCITTContext : Crc16Context
{ {
/// <summary>CCITT CRC16 polynomial</summary> /// <summary>CCITT CRC16 polynomial</summary>
@@ -66,9 +64,7 @@ namespace Aaru.Checksums
0x9ff8, 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 0x9ff8, 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
}; };
/// <summary> /// <summary>Initializes an instance of the CRC16 with CCITT polynomial and seed.</summary>
/// Initializes an instance of the CRC16 with CCITT polynomial and seed.
/// </summary>
/// <inheritdoc /> /// <inheritdoc />
public CRC16CCITTContext() : base(CRC16_CCITT_POLY, CRC16_CCITT_SEED, _ccittCrc16Table, true) {} 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> /// <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); public static string Data(byte[] data, out byte[] hash) => Data(data, (uint)data.Length, out hash);
/// <summary> /// <summary>Calculates the CCITT CRC16 of the specified buffer with the specified parameters</summary>
/// Calculates the CCITT CRC16 of the specified buffer with the specified parameters
/// </summary>
/// <param name="buffer">Buffer</param> /// <param name="buffer">Buffer</param>
public static ushort Calculate(byte[] buffer) => public static ushort Calculate(byte[] buffer) =>
Calculate(buffer, CRC16_CCITT_POLY, CRC16_CCITT_SEED, _ccittCrc16Table, true); Calculate(buffer, CRC16_CCITT_POLY, CRC16_CCITT_SEED, _ccittCrc16Table, true);

View File

@@ -113,7 +113,7 @@ namespace Aaru.Checksums
if((entry & 1) == 1) if((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial; entry = (entry >> 1) ^ polynomial;
else else
entry = entry >> 1; entry >>= 1;
table[i] = (ushort)entry; table[i] = (ushort)entry;
} }
@@ -215,9 +215,7 @@ namespace Aaru.Checksums
return crc16Output.ToString(); return crc16Output.ToString();
} }
/// <summary> /// <summary>Calculates the CRC16 of the specified buffer with the specified parameters</summary>
/// Calculates the CRC16 of the specified buffer with the specified parameters
/// </summary>
/// <param name="buffer">Buffer</param> /// <param name="buffer">Buffer</param>
/// <param name="polynomial">Polynomial</param> /// <param name="polynomial">Polynomial</param>
/// <param name="seed">Seed</param> /// <param name="seed">Seed</param>

View File

@@ -33,9 +33,7 @@
namespace Aaru.Checksums namespace Aaru.Checksums
{ {
/// <inheritdoc /> /// <inheritdoc />
/// <summary> /// <summary>Implements the CRC16 algorithm with IBM polynomial and seed</summary>
/// Implements the CRC16 algorithm with IBM polynomial and seed
/// </summary>
public sealed class CRC16IBMContext : Crc16Context public sealed class CRC16IBMContext : Crc16Context
{ {
const ushort CRC16_IBM_POLY = 0xA001; const ushort CRC16_IBM_POLY = 0xA001;
@@ -65,9 +63,7 @@ namespace Aaru.Checksums
0x8641, 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040 0x8641, 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
}; };
/// <summary> /// <summary>Initializes an instance of the CRC16 with IBM polynomial and seed.</summary>
/// Initializes an instance of the CRC16 with IBM polynomial and seed.
/// </summary>
/// <inheritdoc /> /// <inheritdoc />
public CRC16IBMContext() : base(CRC16_IBM_POLY, CRC16_IBM_SEED, _ibmCrc16Table, false) {} 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> /// <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); public static string Data(byte[] data, out byte[] hash) => Data(data, (uint)data.Length, out hash);
/// <summary> /// <summary>Calculates the IBM CRC16 of the specified buffer with the specified parameters</summary>
/// Calculates the IBM CRC16 of the specified buffer with the specified parameters
/// </summary>
/// <param name="buffer">Buffer</param> /// <param name="buffer">Buffer</param>
public static ushort Calculate(byte[] buffer) => public static ushort Calculate(byte[] buffer) =>
Calculate(buffer, CRC16_IBM_POLY, CRC16_IBM_SEED, _ibmCrc16Table, false); Calculate(buffer, CRC16_IBM_POLY, CRC16_IBM_SEED, _ibmCrc16Table, false);

View File

@@ -37,6 +37,7 @@ using Aaru.Helpers;
namespace Aaru.Checksums namespace Aaru.Checksums
{ {
/// <inheritdoc />
/// <summary>Implements a CRC32 algorithm</summary> /// <summary>Implements a CRC32 algorithm</summary>
public sealed class Crc32Context : IChecksum public sealed class Crc32Context : IChecksum
{ {
@@ -63,7 +64,7 @@ namespace Aaru.Checksums
if((entry & 1) == 1) if((entry & 1) == 1)
entry = (entry >> 1) ^ CRC32_ISO_POLY; entry = (entry >> 1) ^ CRC32_ISO_POLY;
else else
entry = entry >> 1; entry >>= 1;
_table[i] = entry; _table[i] = entry;
} }
@@ -85,7 +86,7 @@ namespace Aaru.Checksums
if((entry & 1) == 1) if((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial; entry = (entry >> 1) ^ polynomial;
else else
entry = entry >> 1; entry >>= 1;
_table[i] = entry; _table[i] = entry;
} }
@@ -146,7 +147,7 @@ namespace Aaru.Checksums
{ {
var fileStream = new FileStream(filename, FileMode.Open); var fileStream = new FileStream(filename, FileMode.Open);
uint localhashInt = seed; uint localHashInt = seed;
uint[] localTable = new uint[256]; uint[] localTable = new uint[256];
@@ -158,16 +159,16 @@ namespace Aaru.Checksums
if((entry & 1) == 1) if((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial; entry = (entry >> 1) ^ polynomial;
else else
entry = entry >> 1; entry >>= 1;
localTable[i] = entry; localTable[i] = entry;
} }
for(int i = 0; i < fileStream.Length; i++) 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; localHashInt ^= seed;
hash = BigEndianBitConverter.GetBytes(localhashInt); hash = BigEndianBitConverter.GetBytes(localHashInt);
var crc32Output = new StringBuilder(); var crc32Output = new StringBuilder();
@@ -194,7 +195,7 @@ namespace Aaru.Checksums
/// <param name="seed">CRC seed</param> /// <param name="seed">CRC seed</param>
public static string Data(byte[] data, uint len, out byte[] hash, uint polynomial, uint seed) 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]; uint[] localTable = new uint[256];
@@ -206,16 +207,16 @@ namespace Aaru.Checksums
if((entry & 1) == 1) if((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial; entry = (entry >> 1) ^ polynomial;
else else
entry = entry >> 1; entry >>= 1;
localTable[i] = entry; localTable[i] = entry;
} }
for(int i = 0; i < len; i++) 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; localHashInt ^= seed;
hash = BigEndianBitConverter.GetBytes(localhashInt); hash = BigEndianBitConverter.GetBytes(localHashInt);
var crc32Output = new StringBuilder(); var crc32Output = new StringBuilder();

View File

@@ -41,13 +41,9 @@ namespace Aaru.Checksums
/// <summary>Implements a CRC64 algorithm</summary> /// <summary>Implements a CRC64 algorithm</summary>
public sealed class Crc64Context : IChecksum public sealed class Crc64Context : IChecksum
{ {
/// <summary> /// <summary>ECMA CRC64 polynomial</summary>
/// ECMA CRC64 polynomial
/// </summary>
public const ulong CRC64_ECMA_POLY = 0xC96C5795D7870F42; public const ulong CRC64_ECMA_POLY = 0xC96C5795D7870F42;
/// <summary> /// <summary>ECMA CRC64 seed</summary>
/// ECMA CRC64 seed
/// </summary>
public const ulong CRC64_ECMA_SEED = 0xFFFFFFFFFFFFFFFF; public const ulong CRC64_ECMA_SEED = 0xFFFFFFFFFFFFFFFF;
readonly ulong _finalSeed; readonly ulong _finalSeed;
@@ -69,7 +65,7 @@ namespace Aaru.Checksums
if((entry & 1) == 1) if((entry & 1) == 1)
entry = (entry >> 1) ^ CRC64_ECMA_POLY; entry = (entry >> 1) ^ CRC64_ECMA_POLY;
else else
entry = entry >> 1; entry >>= 1;
_table[i] = entry; _table[i] = entry;
} }
@@ -92,7 +88,7 @@ namespace Aaru.Checksums
if((entry & 1) == 1) if((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial; entry = (entry >> 1) ^ polynomial;
else else
entry = entry >> 1; entry >>= 1;
_table[i] = entry; _table[i] = entry;
} }
@@ -155,7 +151,7 @@ namespace Aaru.Checksums
{ {
var fileStream = new FileStream(filename, FileMode.Open); var fileStream = new FileStream(filename, FileMode.Open);
ulong localhashInt = seed; ulong localHashInt = seed;
ulong[] localTable = new ulong[256]; ulong[] localTable = new ulong[256];
@@ -167,16 +163,16 @@ namespace Aaru.Checksums
if((entry & 1) == 1) if((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial; entry = (entry >> 1) ^ polynomial;
else else
entry = entry >> 1; entry >>= 1;
localTable[i] = entry; localTable[i] = entry;
} }
for(int i = 0; i < fileStream.Length; i++) 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; localHashInt ^= seed;
hash = BigEndianBitConverter.GetBytes(localhashInt); hash = BigEndianBitConverter.GetBytes(localHashInt);
var crc64Output = new StringBuilder(); var crc64Output = new StringBuilder();
@@ -203,7 +199,7 @@ namespace Aaru.Checksums
/// <param name="seed">CRC seed</param> /// <param name="seed">CRC seed</param>
public static string Data(byte[] data, uint len, out byte[] hash, ulong polynomial, ulong seed) 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]; ulong[] localTable = new ulong[256];
@@ -215,16 +211,16 @@ namespace Aaru.Checksums
if((entry & 1) == 1) if((entry & 1) == 1)
entry = (entry >> 1) ^ polynomial; entry = (entry >> 1) ^ polynomial;
else else
entry = entry >> 1; entry >>= 1;
localTable[i] = entry; localTable[i] = entry;
} }
for(int i = 0; i < len; i++) 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; localHashInt ^= seed;
hash = BigEndianBitConverter.GetBytes(localhashInt); hash = BigEndianBitConverter.GetBytes(localHashInt);
var crc64Output = new StringBuilder(); var crc64Output = new StringBuilder();

View File

@@ -164,6 +164,7 @@ namespace Aaru.Checksums
public static string Data(byte[] data, out byte[] hash) => Data(data, (uint)data.Length, out hash); public static string Data(byte[] data, out byte[] hash) => Data(data, (uint)data.Length, out hash);
} }
/// <inheritdoc />
/// <summary>Implements the Fletcher-16 algorithm</summary> /// <summary>Implements the Fletcher-16 algorithm</summary>
public sealed class Fletcher16Context : IChecksum public sealed class Fletcher16Context : IChecksum
{ {

View File

@@ -48,8 +48,7 @@ namespace Aaru.Checksums
public sealed class Register : IPluginRegister public sealed class Register : IPluginRegister
{ {
/// <inheritdoc /> /// <inheritdoc />
public List<Type> GetAllChecksumPlugins() => Assembly. public List<Type> GetAllChecksumPlugins() => Assembly.GetExecutingAssembly().GetTypes().
GetExecutingAssembly().GetTypes().
Where(t => t.GetInterfaces().Contains(typeof(IChecksum))). Where(t => t.GetInterfaces().Contains(typeof(IChecksum))).
Where(t => t.IsClass).ToList(); Where(t => t.IsClass).ToList();

View File

@@ -38,6 +38,7 @@ using Aaru.CommonTypes.Interfaces;
namespace Aaru.Checksums namespace Aaru.Checksums
{ {
/// <inheritdoc />
/// <summary>Wraps up .NET SHA1 implementation to a Init(), Update(), Final() context.</summary> /// <summary>Wraps up .NET SHA1 implementation to a Init(), Update(), Final() context.</summary>
public sealed class Sha1Context : IChecksum public sealed class Sha1Context : IChecksum
{ {

View File

@@ -38,6 +38,7 @@ using Aaru.CommonTypes.Interfaces;
namespace Aaru.Checksums namespace Aaru.Checksums
{ {
/// <inheritdoc />
/// <summary>Wraps up .NET SHA256 implementation to a Init(), Update(), Final() context.</summary> /// <summary>Wraps up .NET SHA256 implementation to a Init(), Update(), Final() context.</summary>
public sealed class Sha256Context : IChecksum public sealed class Sha256Context : IChecksum
{ {

View File

@@ -38,6 +38,7 @@ using Aaru.CommonTypes.Interfaces;
namespace Aaru.Checksums namespace Aaru.Checksums
{ {
/// <inheritdoc />
/// <summary>Wraps up .NET SHA384 implementation to a Init(), Update(), Final() context.</summary> /// <summary>Wraps up .NET SHA384 implementation to a Init(), Update(), Final() context.</summary>
public sealed class Sha384Context : IChecksum public sealed class Sha384Context : IChecksum
{ {

View File

@@ -38,6 +38,7 @@ using Aaru.CommonTypes.Interfaces;
namespace Aaru.Checksums namespace Aaru.Checksums
{ {
/// <inheritdoc />
/// <summary>Wraps up .NET SHA512 implementation to a Init(), Update(), Final() context.</summary> /// <summary>Wraps up .NET SHA512 implementation to a Init(), Update(), Final() context.</summary>
public sealed class Sha512Context : IChecksum public sealed class Sha512Context : IChecksum
{ {