mirror of
https://github.com/aaru-dps/AaruBenchmark.git
synced 2025-12-16 19:24:36 +00:00
Add native CRC16.
This commit is contained in:
@@ -32,6 +32,7 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Aaru.CommonTypes.Interfaces;
|
using Aaru.CommonTypes.Interfaces;
|
||||||
using Aaru.Helpers;
|
using Aaru.Helpers;
|
||||||
@@ -46,6 +47,10 @@ namespace Aaru6.Checksums
|
|||||||
readonly bool _inverse;
|
readonly bool _inverse;
|
||||||
readonly ushort[][] _table;
|
readonly ushort[][] _table;
|
||||||
ushort _hashInt;
|
ushort _hashInt;
|
||||||
|
readonly IntPtr _nativeContext;
|
||||||
|
readonly bool _useCcitt;
|
||||||
|
readonly bool _useIbm;
|
||||||
|
readonly bool _useNative;
|
||||||
|
|
||||||
/// <summary>Initializes the CRC16 table with a custom polynomial and seed</summary>
|
/// <summary>Initializes the CRC16 table with a custom polynomial and seed</summary>
|
||||||
public Crc16Context(ushort polynomial, ushort seed, ushort[][] table, bool inverse)
|
public Crc16Context(ushort polynomial, ushort seed, ushort[][] table, bool inverse)
|
||||||
@@ -54,7 +59,29 @@ namespace Aaru6.Checksums
|
|||||||
_finalSeed = seed;
|
_finalSeed = seed;
|
||||||
_inverse = inverse;
|
_inverse = inverse;
|
||||||
|
|
||||||
_table = table ?? GenerateTable(polynomial, inverse);
|
_useNative = Native.IsSupported;
|
||||||
|
|
||||||
|
_useCcitt = polynomial == CRC16CCITTContext.CRC16_CCITT_POLY &&
|
||||||
|
seed == CRC16CCITTContext.CRC16_CCITT_SEED && inverse;
|
||||||
|
|
||||||
|
_useIbm = polynomial == CRC16IBMContext.CRC16_IBM_POLY && seed == CRC16IBMContext.CRC16_IBM_SEED &&
|
||||||
|
!inverse;
|
||||||
|
|
||||||
|
if(_useCcitt && _useNative)
|
||||||
|
{
|
||||||
|
_nativeContext = crc16_ccitt_init();
|
||||||
|
_useNative = _nativeContext != IntPtr.Zero;
|
||||||
|
}
|
||||||
|
else if(_useIbm && _useNative)
|
||||||
|
{
|
||||||
|
_nativeContext = crc16_init();
|
||||||
|
_useNative = _nativeContext != IntPtr.Zero;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
_useNative = false;
|
||||||
|
|
||||||
|
if(!_useNative)
|
||||||
|
_table = table ?? GenerateTable(polynomial, inverse);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@@ -63,10 +90,26 @@ namespace Aaru6.Checksums
|
|||||||
/// <param name="len">Length of buffer to hash.</param>
|
/// <param name="len">Length of buffer to hash.</param>
|
||||||
public void Update(byte[] data, uint len)
|
public void Update(byte[] data, uint len)
|
||||||
{
|
{
|
||||||
if(_inverse)
|
switch(_useNative)
|
||||||
StepInverse(ref _hashInt, _table, data, len);
|
{
|
||||||
else
|
case true when _useCcitt:
|
||||||
Step(ref _hashInt, _table, data, len);
|
crc16_ccitt_update(_nativeContext, data, len);
|
||||||
|
|
||||||
|
break;
|
||||||
|
case true when _useIbm:
|
||||||
|
crc16_update(_nativeContext, data, len);
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
if(_inverse)
|
||||||
|
StepInverse(ref _hashInt, _table, data, len);
|
||||||
|
else
|
||||||
|
Step(ref _hashInt, _table, data, len);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@@ -76,19 +119,65 @@ namespace Aaru6.Checksums
|
|||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
/// <summary>Returns a byte array of the hash value.</summary>
|
/// <summary>Returns a byte array of the hash value.</summary>
|
||||||
public byte[] Final() => !_inverse ? BigEndianBitConverter.GetBytes((ushort)(_hashInt ^ _finalSeed))
|
public byte[] Final()
|
||||||
: BigEndianBitConverter.GetBytes((ushort)~(_hashInt ^ _finalSeed));
|
{
|
||||||
|
ushort crc = 0;
|
||||||
|
|
||||||
|
switch(_useNative)
|
||||||
|
{
|
||||||
|
case true when _useCcitt:
|
||||||
|
crc16_ccitt_final(_nativeContext, ref crc);
|
||||||
|
crc16_ccitt_free(_nativeContext);
|
||||||
|
|
||||||
|
break;
|
||||||
|
case true when _useIbm:
|
||||||
|
crc16_final(_nativeContext, ref crc);
|
||||||
|
crc16_free(_nativeContext);
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
if(_inverse)
|
||||||
|
crc = (ushort)~(_hashInt ^ _finalSeed);
|
||||||
|
else
|
||||||
|
crc = (ushort)(_hashInt ^ _finalSeed);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return BigEndianBitConverter.GetBytes(crc);
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
/// <summary>Returns a hexadecimal representation of the hash value.</summary>
|
/// <summary>Returns a hexadecimal representation of the hash value.</summary>
|
||||||
public string End()
|
public string End()
|
||||||
{
|
{
|
||||||
var crc16Output = new StringBuilder();
|
var crc16Output = new StringBuilder();
|
||||||
|
ushort final = 0;
|
||||||
|
|
||||||
ushort final = (ushort)(_hashInt ^ _finalSeed);
|
switch(_useNative)
|
||||||
|
{
|
||||||
|
case true when _useCcitt:
|
||||||
|
crc16_ccitt_final(_nativeContext, ref final);
|
||||||
|
crc16_ccitt_free(_nativeContext);
|
||||||
|
|
||||||
if(_inverse)
|
break;
|
||||||
final = (ushort)~final;
|
case true when _useIbm:
|
||||||
|
crc16_final(_nativeContext, ref final);
|
||||||
|
crc16_free(_nativeContext);
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
if(_inverse)
|
||||||
|
final = (ushort)~(_hashInt ^ _finalSeed);
|
||||||
|
else
|
||||||
|
final = (ushort)(_hashInt ^ _finalSeed);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
byte[] finalBytes = BigEndianBitConverter.GetBytes(final);
|
byte[] finalBytes = BigEndianBitConverter.GetBytes(final);
|
||||||
|
|
||||||
@@ -98,6 +187,30 @@ namespace Aaru6.Checksums
|
|||||||
return crc16Output.ToString();
|
return crc16Output.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
||||||
|
static extern IntPtr crc16_init();
|
||||||
|
|
||||||
|
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
||||||
|
static extern int crc16_update(IntPtr ctx, byte[] data, uint len);
|
||||||
|
|
||||||
|
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
||||||
|
static extern int crc16_final(IntPtr ctx, ref ushort crc);
|
||||||
|
|
||||||
|
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
||||||
|
static extern void crc16_free(IntPtr ctx);
|
||||||
|
|
||||||
|
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
||||||
|
static extern IntPtr crc16_ccitt_init();
|
||||||
|
|
||||||
|
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
||||||
|
static extern int crc16_ccitt_update(IntPtr ctx, byte[] data, uint len);
|
||||||
|
|
||||||
|
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
||||||
|
static extern int crc16_ccitt_final(IntPtr ctx, ref ushort crc);
|
||||||
|
|
||||||
|
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
||||||
|
static extern void crc16_ccitt_free(IntPtr ctx);
|
||||||
|
|
||||||
static void Step(ref ushort previousCrc, ushort[][] table, byte[] data, uint len)
|
static void Step(ref ushort previousCrc, ushort[][] table, byte[] data, uint len)
|
||||||
{
|
{
|
||||||
// Unroll according to Intel slicing by uint8_t
|
// Unroll according to Intel slicing by uint8_t
|
||||||
@@ -234,10 +347,34 @@ namespace Aaru6.Checksums
|
|||||||
public static string File(string filename, out byte[] hash, ushort polynomial, ushort seed, ushort[][] table,
|
public static string File(string filename, out byte[] hash, ushort polynomial, ushort seed, ushort[][] table,
|
||||||
bool inverse)
|
bool inverse)
|
||||||
{
|
{
|
||||||
|
bool useNative = Native.IsSupported;
|
||||||
|
|
||||||
|
bool useCcitt = polynomial == CRC16CCITTContext.CRC16_CCITT_POLY &&
|
||||||
|
seed == CRC16CCITTContext.CRC16_CCITT_SEED && inverse;
|
||||||
|
|
||||||
|
bool useIbm = polynomial == CRC16IBMContext.CRC16_IBM_POLY && seed == CRC16IBMContext.CRC16_IBM_SEED &&
|
||||||
|
!inverse;
|
||||||
|
|
||||||
|
IntPtr nativeContext = IntPtr.Zero;
|
||||||
|
|
||||||
var fileStream = new FileStream(filename, FileMode.Open);
|
var fileStream = new FileStream(filename, FileMode.Open);
|
||||||
|
|
||||||
ushort localHashInt = seed;
|
ushort localHashInt = seed;
|
||||||
|
|
||||||
|
switch(useNative)
|
||||||
|
{
|
||||||
|
case true when useCcitt:
|
||||||
|
nativeContext = crc16_ccitt_init();
|
||||||
|
useNative = nativeContext != IntPtr.Zero;
|
||||||
|
|
||||||
|
break;
|
||||||
|
case true when useIbm:
|
||||||
|
nativeContext = crc16_init();
|
||||||
|
useNative = nativeContext != IntPtr.Zero;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
ushort[][] localTable = table ?? GenerateTable(polynomial, inverse);
|
ushort[][] localTable = table ?? GenerateTable(polynomial, inverse);
|
||||||
|
|
||||||
byte[] buffer = new byte[65536];
|
byte[] buffer = new byte[65536];
|
||||||
@@ -245,18 +382,52 @@ namespace Aaru6.Checksums
|
|||||||
|
|
||||||
while(read > 0)
|
while(read > 0)
|
||||||
{
|
{
|
||||||
if(inverse)
|
switch(useNative)
|
||||||
StepInverse(ref localHashInt, localTable, buffer, (uint)read);
|
{
|
||||||
else
|
case true when useCcitt:
|
||||||
Step(ref localHashInt, localTable, buffer, (uint)read);
|
crc16_ccitt_update(nativeContext, buffer, (uint)read);
|
||||||
|
|
||||||
|
break;
|
||||||
|
case true when useIbm:
|
||||||
|
crc16_update(nativeContext, buffer, (uint)read);
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
if(inverse)
|
||||||
|
StepInverse(ref localHashInt, localTable, buffer, (uint)read);
|
||||||
|
else
|
||||||
|
Step(ref localHashInt, localTable, buffer, (uint)read);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
read = fileStream.Read(buffer, 0, 65536);
|
read = fileStream.Read(buffer, 0, 65536);
|
||||||
}
|
}
|
||||||
|
|
||||||
localHashInt ^= seed;
|
localHashInt ^= seed;
|
||||||
|
|
||||||
if(inverse)
|
switch(useNative)
|
||||||
localHashInt = (ushort)~localHashInt;
|
{
|
||||||
|
case true when useCcitt:
|
||||||
|
crc16_ccitt_final(nativeContext, ref localHashInt);
|
||||||
|
crc16_ccitt_free(nativeContext);
|
||||||
|
|
||||||
|
break;
|
||||||
|
case true when useIbm:
|
||||||
|
crc16_final(nativeContext, ref localHashInt);
|
||||||
|
crc16_free(nativeContext);
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
if(inverse)
|
||||||
|
localHashInt = (ushort)~localHashInt;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
hash = BigEndianBitConverter.GetBytes(localHashInt);
|
hash = BigEndianBitConverter.GetBytes(localHashInt);
|
||||||
|
|
||||||
@@ -281,19 +452,77 @@ namespace Aaru6.Checksums
|
|||||||
public static string Data(byte[] data, uint len, out byte[] hash, ushort polynomial, ushort seed,
|
public static string Data(byte[] data, uint len, out byte[] hash, ushort polynomial, ushort seed,
|
||||||
ushort[][] table, bool inverse)
|
ushort[][] table, bool inverse)
|
||||||
{
|
{
|
||||||
|
bool useNative = Native.IsSupported;
|
||||||
|
|
||||||
|
bool useCcitt = polynomial == CRC16CCITTContext.CRC16_CCITT_POLY &&
|
||||||
|
seed == CRC16CCITTContext.CRC16_CCITT_SEED && inverse;
|
||||||
|
|
||||||
|
bool useIbm = polynomial == CRC16IBMContext.CRC16_IBM_POLY && seed == CRC16IBMContext.CRC16_IBM_SEED &&
|
||||||
|
!inverse;
|
||||||
|
|
||||||
|
IntPtr nativeContext = IntPtr.Zero;
|
||||||
|
|
||||||
ushort localHashInt = seed;
|
ushort localHashInt = seed;
|
||||||
|
|
||||||
|
switch(useNative)
|
||||||
|
{
|
||||||
|
case true when useCcitt:
|
||||||
|
nativeContext = crc16_ccitt_init();
|
||||||
|
useNative = nativeContext != IntPtr.Zero;
|
||||||
|
|
||||||
|
break;
|
||||||
|
case true when useIbm:
|
||||||
|
nativeContext = crc16_init();
|
||||||
|
useNative = nativeContext != IntPtr.Zero;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
ushort[][] localTable = table ?? GenerateTable(polynomial, inverse);
|
ushort[][] localTable = table ?? GenerateTable(polynomial, inverse);
|
||||||
|
|
||||||
if(inverse)
|
switch(useNative)
|
||||||
StepInverse(ref localHashInt, localTable, data, len);
|
{
|
||||||
else
|
case true when useCcitt:
|
||||||
Step(ref localHashInt, localTable, data, len);
|
crc16_ccitt_update(nativeContext, data, len);
|
||||||
|
|
||||||
|
break;
|
||||||
|
case true when useIbm:
|
||||||
|
crc16_update(nativeContext, data, len);
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
if(inverse)
|
||||||
|
StepInverse(ref localHashInt, localTable, data, len);
|
||||||
|
else
|
||||||
|
Step(ref localHashInt, localTable, data, len);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
localHashInt ^= seed;
|
localHashInt ^= seed;
|
||||||
|
|
||||||
if(inverse)
|
switch(useNative)
|
||||||
localHashInt = (ushort)~localHashInt;
|
{
|
||||||
|
case true when useCcitt:
|
||||||
|
crc16_ccitt_final(nativeContext, ref localHashInt);
|
||||||
|
crc16_ccitt_free(nativeContext);
|
||||||
|
|
||||||
|
break;
|
||||||
|
case true when useIbm:
|
||||||
|
crc16_final(nativeContext, ref localHashInt);
|
||||||
|
crc16_free(nativeContext);
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
if(inverse)
|
||||||
|
localHashInt = (ushort)~localHashInt;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
hash = BigEndianBitConverter.GetBytes(localHashInt);
|
hash = BigEndianBitConverter.GetBytes(localHashInt);
|
||||||
|
|
||||||
@@ -314,19 +543,77 @@ namespace Aaru6.Checksums
|
|||||||
/// <returns>CRC16</returns>
|
/// <returns>CRC16</returns>
|
||||||
public static ushort Calculate(byte[] buffer, ushort polynomial, ushort seed, ushort[][] table, bool inverse)
|
public static ushort Calculate(byte[] buffer, ushort polynomial, ushort seed, ushort[][] table, bool inverse)
|
||||||
{
|
{
|
||||||
|
bool useNative = Native.IsSupported;
|
||||||
|
|
||||||
|
bool useCcitt = polynomial == CRC16CCITTContext.CRC16_CCITT_POLY &&
|
||||||
|
seed == CRC16CCITTContext.CRC16_CCITT_SEED && inverse;
|
||||||
|
|
||||||
|
bool useIbm = polynomial == CRC16IBMContext.CRC16_IBM_POLY && seed == CRC16IBMContext.CRC16_IBM_SEED &&
|
||||||
|
!inverse;
|
||||||
|
|
||||||
|
IntPtr nativeContext = IntPtr.Zero;
|
||||||
|
|
||||||
ushort localHashInt = seed;
|
ushort localHashInt = seed;
|
||||||
|
|
||||||
|
switch(useNative)
|
||||||
|
{
|
||||||
|
case true when useCcitt:
|
||||||
|
nativeContext = crc16_ccitt_init();
|
||||||
|
useNative = nativeContext != IntPtr.Zero;
|
||||||
|
|
||||||
|
break;
|
||||||
|
case true when useIbm:
|
||||||
|
nativeContext = crc16_init();
|
||||||
|
useNative = nativeContext != IntPtr.Zero;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
ushort[][] localTable = table ?? GenerateTable(polynomial, inverse);
|
ushort[][] localTable = table ?? GenerateTable(polynomial, inverse);
|
||||||
|
|
||||||
if(inverse)
|
switch(useNative)
|
||||||
StepInverse(ref localHashInt, localTable, buffer, (uint)buffer.Length);
|
{
|
||||||
else
|
case true when useCcitt:
|
||||||
Step(ref localHashInt, localTable, buffer, (uint)buffer.Length);
|
crc16_ccitt_update(nativeContext, buffer, (uint)buffer.Length);
|
||||||
|
|
||||||
|
break;
|
||||||
|
case true when useIbm:
|
||||||
|
crc16_update(nativeContext, buffer, (uint)buffer.Length);
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
if(inverse)
|
||||||
|
StepInverse(ref localHashInt, localTable, buffer, (uint)buffer.Length);
|
||||||
|
else
|
||||||
|
Step(ref localHashInt, localTable, buffer, (uint)buffer.Length);
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
localHashInt ^= seed;
|
localHashInt ^= seed;
|
||||||
|
|
||||||
if(inverse)
|
switch(useNative)
|
||||||
localHashInt = (ushort)~localHashInt;
|
{
|
||||||
|
case true when useCcitt:
|
||||||
|
crc16_ccitt_final(nativeContext, ref localHashInt);
|
||||||
|
crc16_ccitt_free(nativeContext);
|
||||||
|
|
||||||
|
break;
|
||||||
|
case true when useIbm:
|
||||||
|
crc16_final(nativeContext, ref localHashInt);
|
||||||
|
crc16_free(nativeContext);
|
||||||
|
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
if(inverse)
|
||||||
|
localHashInt = (ushort)~localHashInt;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return localHashInt;
|
return localHashInt;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,8 +36,8 @@ namespace Aaru6.Checksums
|
|||||||
/// <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
|
public sealed class CRC16IBMContext : Crc16Context
|
||||||
{
|
{
|
||||||
const ushort CRC16_IBM_POLY = 0xA001;
|
internal const ushort CRC16_IBM_POLY = 0xA001;
|
||||||
const ushort CRC16_IBM_SEED = 0x0000;
|
internal const ushort CRC16_IBM_SEED = 0x0000;
|
||||||
|
|
||||||
static readonly ushort[][] _ibmCrc16Table =
|
static readonly ushort[][] _ibmCrc16Table =
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -139,6 +139,8 @@ namespace AaruBenchmark.Checksums
|
|||||||
|
|
||||||
public static void Crc16Ccitt()
|
public static void Crc16Ccitt()
|
||||||
{
|
{
|
||||||
|
Native.ForceManaged = true;
|
||||||
|
|
||||||
byte[] data = new byte[1048576];
|
byte[] data = new byte[1048576];
|
||||||
|
|
||||||
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
|
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
|
||||||
@@ -149,16 +151,18 @@ namespace AaruBenchmark.Checksums
|
|||||||
IChecksum ctx = new CRC16CCITTContext();
|
IChecksum ctx = new CRC16CCITTContext();
|
||||||
ctx.Update(data);
|
ctx.Update(data);
|
||||||
byte[] result = ctx.Final();
|
byte[] result = ctx.Final();
|
||||||
/*
|
|
||||||
if(result?.Length != _expectedRandomCrc16Ccitt.Length)
|
|
||||||
throw new Exception("Invalid hash length");
|
|
||||||
|
|
||||||
if(result.Where((t, i) => t != _expectedRandomCrc16Ccitt[i]).Any())
|
if(result?.Length != _expectedRandomCrc16Ccitt.Length)
|
||||||
throw new Exception("Invalid hash value");*/
|
throw new Exception("Invalid hash length");
|
||||||
|
|
||||||
|
if(result.Where((t, i) => t != _expectedRandomCrc16Ccitt[i]).Any())
|
||||||
|
throw new Exception("Invalid hash value");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Crc16()
|
public static void Crc16()
|
||||||
{
|
{
|
||||||
|
Native.ForceManaged = true;
|
||||||
|
|
||||||
byte[] data = new byte[1048576];
|
byte[] data = new byte[1048576];
|
||||||
|
|
||||||
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
|
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
|
||||||
|
|||||||
@@ -100,30 +100,6 @@ namespace AaruBenchmark.Checksums
|
|||||||
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
||||||
static extern void fletcher32_free(IntPtr ctx);
|
static extern void fletcher32_free(IntPtr ctx);
|
||||||
|
|
||||||
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
|
||||||
static extern IntPtr crc16_ccitt_init();
|
|
||||||
|
|
||||||
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
|
||||||
static extern int crc16_ccitt_update(IntPtr ctx, byte[] data, uint len);
|
|
||||||
|
|
||||||
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
|
||||||
static extern int crc16_ccitt_final(IntPtr ctx, ref ushort crc);
|
|
||||||
|
|
||||||
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
|
||||||
static extern void crc16_ccitt_free(IntPtr ctx);
|
|
||||||
|
|
||||||
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
|
||||||
static extern IntPtr crc16_init();
|
|
||||||
|
|
||||||
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
|
||||||
static extern int crc16_update(IntPtr ctx, byte[] data, uint len);
|
|
||||||
|
|
||||||
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
|
||||||
static extern int crc16_final(IntPtr ctx, ref ushort crc);
|
|
||||||
|
|
||||||
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
|
||||||
static extern void crc16_free(IntPtr ctx);
|
|
||||||
|
|
||||||
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
||||||
static extern IntPtr crc32_init();
|
static extern IntPtr crc32_init();
|
||||||
|
|
||||||
@@ -259,75 +235,45 @@ namespace AaruBenchmark.Checksums
|
|||||||
|
|
||||||
public static void Crc16Ccitt()
|
public static void Crc16Ccitt()
|
||||||
{
|
{
|
||||||
|
Native.ForceManaged = true;
|
||||||
|
|
||||||
byte[] data = new byte[1048576];
|
byte[] data = new byte[1048576];
|
||||||
ushort crc = 0;
|
|
||||||
byte[] hash;
|
|
||||||
|
|
||||||
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
|
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
|
||||||
|
|
||||||
fs.Read(data, 0, 1048576);
|
fs.Read(data, 0, 1048576);
|
||||||
fs.Close();
|
fs.Close();
|
||||||
fs.Dispose();
|
fs.Dispose();
|
||||||
|
IChecksum ctx = new CRC16CCITTContext();
|
||||||
|
ctx.Update(data);
|
||||||
|
byte[] result = ctx.Final();
|
||||||
|
|
||||||
IntPtr ctx = crc16_ccitt_init();
|
if(result?.Length != _expectedRandomCrc16Ccitt.Length)
|
||||||
|
throw new Exception("Invalid hash length");
|
||||||
|
|
||||||
if(ctx == IntPtr.Zero)
|
if(result.Where((t, i) => t != _expectedRandomCrc16Ccitt[i]).Any())
|
||||||
throw new Exception("Could not initialize digest");
|
|
||||||
|
|
||||||
int ret = crc16_ccitt_update(ctx, data, (uint)data.Length);
|
|
||||||
|
|
||||||
if(ret != 0)
|
|
||||||
throw new Exception("Could not digest block");
|
|
||||||
|
|
||||||
ret = crc16_ccitt_final(ctx, ref crc);
|
|
||||||
|
|
||||||
if(ret != 0)
|
|
||||||
throw new Exception("Could not finalize hash");
|
|
||||||
|
|
||||||
crc16_ccitt_free(ctx);
|
|
||||||
|
|
||||||
crc = (ushort)((crc << 8) | (crc >> 8));
|
|
||||||
|
|
||||||
hash = BitConverter.GetBytes(crc);
|
|
||||||
|
|
||||||
if(hash.Where((t, i) => t != _expectedRandomCrc16Ccitt[i]).Any())
|
|
||||||
throw new Exception("Invalid hash value");
|
throw new Exception("Invalid hash value");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Crc16()
|
public static void Crc16()
|
||||||
{
|
{
|
||||||
|
Native.ForceManaged = false;
|
||||||
|
|
||||||
byte[] data = new byte[1048576];
|
byte[] data = new byte[1048576];
|
||||||
ushort crc = 0;
|
|
||||||
byte[] hash;
|
|
||||||
|
|
||||||
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
|
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
|
||||||
|
|
||||||
fs.Read(data, 0, 1048576);
|
fs.Read(data, 0, 1048576);
|
||||||
fs.Close();
|
fs.Close();
|
||||||
fs.Dispose();
|
fs.Dispose();
|
||||||
|
IChecksum ctx = new CRC16IBMContext();
|
||||||
|
ctx.Update(data);
|
||||||
|
byte[] result = ctx.Final();
|
||||||
|
|
||||||
IntPtr ctx = crc16_init();
|
if(result?.Length != _expectedRandomCrc16.Length)
|
||||||
|
throw new Exception("Invalid hash length");
|
||||||
|
|
||||||
if(ctx == IntPtr.Zero)
|
if(result.Where((t, i) => t != _expectedRandomCrc16[i]).Any())
|
||||||
throw new Exception("Could not initialize digest");
|
|
||||||
|
|
||||||
int ret = crc16_update(ctx, data, (uint)data.Length);
|
|
||||||
|
|
||||||
if(ret != 0)
|
|
||||||
throw new Exception("Could not digest block");
|
|
||||||
|
|
||||||
ret = crc16_final(ctx, ref crc);
|
|
||||||
|
|
||||||
if(ret != 0)
|
|
||||||
throw new Exception("Could not finalize hash");
|
|
||||||
|
|
||||||
crc16_free(ctx);
|
|
||||||
|
|
||||||
crc = (ushort)((crc << 8) | (crc >> 8));
|
|
||||||
|
|
||||||
hash = BitConverter.GetBytes(crc);
|
|
||||||
|
|
||||||
if(hash.Where((t, i) => t != _expectedRandomCrc16[i]).Any())
|
|
||||||
throw new Exception("Invalid hash value");
|
throw new Exception("Invalid hash value");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user