mirror of
https://github.com/aaru-dps/AaruBenchmark.git
synced 2025-12-16 19:24:36 +00:00
Add native Fletcher-16.
This commit is contained in:
@@ -32,7 +32,9 @@
|
|||||||
|
|
||||||
// Disabled because the speed is abnormally slow
|
// Disabled because the speed is abnormally slow
|
||||||
|
|
||||||
|
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;
|
||||||
@@ -315,20 +317,29 @@ namespace Aaru6.Checksums
|
|||||||
{
|
{
|
||||||
const byte FLETCHER_MODULE = 0xFF;
|
const byte FLETCHER_MODULE = 0xFF;
|
||||||
const byte NMAX = 22;
|
const byte NMAX = 22;
|
||||||
|
|
||||||
|
readonly IntPtr _nativeContext;
|
||||||
byte _sum1, _sum2;
|
byte _sum1, _sum2;
|
||||||
|
readonly bool _useNative;
|
||||||
|
|
||||||
/// <summary>Initializes the Fletcher-16 sums</summary>
|
/// <summary>Initializes the Fletcher-16 sums</summary>
|
||||||
public Fletcher16Context()
|
public Fletcher16Context()
|
||||||
{
|
{
|
||||||
_sum1 = 0xFF;
|
_sum1 = 0xFF;
|
||||||
_sum2 = 0xFF;
|
_sum2 = 0xFF;
|
||||||
|
|
||||||
|
if(!Native.IsSupported)
|
||||||
|
return;
|
||||||
|
|
||||||
|
_nativeContext = fletcher16_init();
|
||||||
|
_useNative = _nativeContext != IntPtr.Zero;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
/// <summary>Updates the hash with data.</summary>
|
/// <summary>Updates the hash with data.</summary>
|
||||||
/// <param name="data">Data buffer.</param>
|
/// <param name="data">Data buffer.</param>
|
||||||
/// <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) => Step(ref _sum1, ref _sum2, data, len);
|
public void Update(byte[] data, uint len) => Step(ref _sum1, ref _sum2, data, len, _useNative, _nativeContext);
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
/// <summary>Updates the hash with data.</summary>
|
/// <summary>Updates the hash with data.</summary>
|
||||||
@@ -341,6 +352,12 @@ namespace Aaru6.Checksums
|
|||||||
{
|
{
|
||||||
ushort finalSum = (ushort)((_sum2 << 8) | _sum1);
|
ushort finalSum = (ushort)((_sum2 << 8) | _sum1);
|
||||||
|
|
||||||
|
if(!_useNative)
|
||||||
|
return BigEndianBitConverter.GetBytes(finalSum);
|
||||||
|
|
||||||
|
fletcher16_final(_nativeContext, ref finalSum);
|
||||||
|
fletcher16_free(_nativeContext);
|
||||||
|
|
||||||
return BigEndianBitConverter.GetBytes(finalSum);
|
return BigEndianBitConverter.GetBytes(finalSum);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -349,6 +366,13 @@ namespace Aaru6.Checksums
|
|||||||
public string End()
|
public string End()
|
||||||
{
|
{
|
||||||
ushort finalSum = (ushort)((_sum2 << 8) | _sum1);
|
ushort finalSum = (ushort)((_sum2 << 8) | _sum1);
|
||||||
|
|
||||||
|
if(_useNative)
|
||||||
|
{
|
||||||
|
fletcher16_final(_nativeContext, ref finalSum);
|
||||||
|
fletcher16_free(_nativeContext);
|
||||||
|
}
|
||||||
|
|
||||||
var fletcherOutput = new StringBuilder();
|
var fletcherOutput = new StringBuilder();
|
||||||
|
|
||||||
for(int i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++)
|
for(int i = 0; i < BigEndianBitConverter.GetBytes(finalSum).Length; i++)
|
||||||
@@ -357,8 +381,28 @@ namespace Aaru6.Checksums
|
|||||||
return fletcherOutput.ToString();
|
return fletcherOutput.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Step(ref byte previousSum1, ref byte previousSum2, byte[] data, uint len)
|
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
||||||
|
static extern IntPtr fletcher16_init();
|
||||||
|
|
||||||
|
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
||||||
|
static extern int fletcher16_update(IntPtr ctx, byte[] data, uint len);
|
||||||
|
|
||||||
|
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
||||||
|
static extern int fletcher16_final(IntPtr ctx, ref ushort checksum);
|
||||||
|
|
||||||
|
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
||||||
|
static extern void fletcher16_free(IntPtr ctx);
|
||||||
|
|
||||||
|
static void Step(ref byte previousSum1, ref byte previousSum2, byte[] data, uint len, bool useNative,
|
||||||
|
IntPtr nativeContext)
|
||||||
{
|
{
|
||||||
|
if(useNative)
|
||||||
|
{
|
||||||
|
fletcher16_update(nativeContext, data, len);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
uint sum1 = previousSum1;
|
uint sum1 = previousSum1;
|
||||||
uint sum2 = previousSum2;
|
uint sum2 = previousSum2;
|
||||||
uint n;
|
uint n;
|
||||||
@@ -502,6 +546,17 @@ namespace Aaru6.Checksums
|
|||||||
/// <param name="hash">Byte array of the hash value.</param>
|
/// <param name="hash">Byte array of the hash value.</param>
|
||||||
public static string File(string filename, out byte[] hash)
|
public static string File(string filename, out byte[] hash)
|
||||||
{
|
{
|
||||||
|
bool useNative = Native.IsSupported;
|
||||||
|
IntPtr nativeContext = IntPtr.Zero;
|
||||||
|
|
||||||
|
if(useNative)
|
||||||
|
{
|
||||||
|
nativeContext = fletcher16_init();
|
||||||
|
|
||||||
|
if(nativeContext == IntPtr.Zero)
|
||||||
|
useNative = false;
|
||||||
|
}
|
||||||
|
|
||||||
var fileStream = new FileStream(filename, FileMode.Open);
|
var fileStream = new FileStream(filename, FileMode.Open);
|
||||||
|
|
||||||
byte localSum1 = 0xFF;
|
byte localSum1 = 0xFF;
|
||||||
@@ -512,13 +567,19 @@ namespace Aaru6.Checksums
|
|||||||
|
|
||||||
while(read > 0)
|
while(read > 0)
|
||||||
{
|
{
|
||||||
Step(ref localSum1, ref localSum2, buffer, (uint)read);
|
Step(ref localSum1, ref localSum2, buffer, (uint)read, useNative, nativeContext);
|
||||||
|
|
||||||
read = fileStream.Read(buffer, 0, 65536);
|
read = fileStream.Read(buffer, 0, 65536);
|
||||||
}
|
}
|
||||||
|
|
||||||
ushort finalSum = (ushort)((localSum2 << 8) | localSum1);
|
ushort finalSum = (ushort)((localSum2 << 8) | localSum1);
|
||||||
|
|
||||||
|
if(useNative)
|
||||||
|
{
|
||||||
|
fletcher16_final(nativeContext, ref finalSum);
|
||||||
|
fletcher16_free(nativeContext);
|
||||||
|
}
|
||||||
|
|
||||||
hash = BigEndianBitConverter.GetBytes(finalSum);
|
hash = BigEndianBitConverter.GetBytes(finalSum);
|
||||||
|
|
||||||
var fletcherOutput = new StringBuilder();
|
var fletcherOutput = new StringBuilder();
|
||||||
@@ -537,13 +598,30 @@ namespace Aaru6.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, uint len, out byte[] hash)
|
public static string Data(byte[] data, uint len, out byte[] hash)
|
||||||
{
|
{
|
||||||
|
bool useNative = Native.IsSupported;
|
||||||
|
IntPtr nativeContext = IntPtr.Zero;
|
||||||
|
|
||||||
|
if(useNative)
|
||||||
|
{
|
||||||
|
nativeContext = fletcher16_init();
|
||||||
|
|
||||||
|
if(nativeContext == IntPtr.Zero)
|
||||||
|
useNative = false;
|
||||||
|
}
|
||||||
|
|
||||||
byte localSum1 = 0xFF;
|
byte localSum1 = 0xFF;
|
||||||
byte localSum2 = 0xFF;
|
byte localSum2 = 0xFF;
|
||||||
|
|
||||||
Step(ref localSum1, ref localSum2, data, len);
|
Step(ref localSum1, ref localSum2, data, len, useNative, nativeContext);
|
||||||
|
|
||||||
ushort finalSum = (ushort)((localSum2 << 8) | localSum1);
|
ushort finalSum = (ushort)((localSum2 << 8) | localSum1);
|
||||||
|
|
||||||
|
if(useNative)
|
||||||
|
{
|
||||||
|
fletcher16_final(nativeContext, ref finalSum);
|
||||||
|
fletcher16_free(nativeContext);
|
||||||
|
}
|
||||||
|
|
||||||
hash = BigEndianBitConverter.GetBytes(finalSum);
|
hash = BigEndianBitConverter.GetBytes(finalSum);
|
||||||
|
|
||||||
var adlerOutput = new StringBuilder();
|
var adlerOutput = new StringBuilder();
|
||||||
|
|||||||
@@ -148,12 +148,12 @@ 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)
|
if(result?.Length != _expectedRandomCrc16Ccitt.Length)
|
||||||
throw new Exception("Invalid hash length");
|
throw new Exception("Invalid hash length");
|
||||||
|
|
||||||
if(result.Where((t, i) => t != _expectedRandomCrc16Ccitt[i]).Any())
|
if(result.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()
|
||||||
|
|||||||
@@ -77,6 +77,8 @@ namespace AaruBenchmark.Checksums
|
|||||||
|
|
||||||
public static void Fletcher16()
|
public static void Fletcher16()
|
||||||
{
|
{
|
||||||
|
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);
|
||||||
|
|||||||
@@ -76,18 +76,6 @@ namespace AaruBenchmark.Checksums
|
|||||||
0xb5, 0xf2, 0x8c, 0xbb, 0x4a, 0xa5, 0x1b, 0x40, 0x7c, 0xb6
|
0xb5, 0xf2, 0x8c, 0xbb, 0x4a, 0xa5, 0x1b, 0x40, 0x7c, 0xb6
|
||||||
};
|
};
|
||||||
|
|
||||||
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
|
||||||
static extern IntPtr fletcher16_init();
|
|
||||||
|
|
||||||
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
|
||||||
static extern int fletcher16_update(IntPtr ctx, byte[] data, uint len);
|
|
||||||
|
|
||||||
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
|
||||||
static extern int fletcher16_final(IntPtr ctx, ref ushort crc);
|
|
||||||
|
|
||||||
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
|
||||||
static extern void fletcher16_free(IntPtr ctx);
|
|
||||||
|
|
||||||
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
[DllImport("libAaru.Checksums.Native", SetLastError = true)]
|
||||||
static extern IntPtr fletcher32_init();
|
static extern IntPtr fletcher32_init();
|
||||||
|
|
||||||
@@ -114,38 +102,23 @@ namespace AaruBenchmark.Checksums
|
|||||||
|
|
||||||
public static void Fletcher16()
|
public static void Fletcher16()
|
||||||
{
|
{
|
||||||
|
Native.ForceManaged = false;
|
||||||
|
|
||||||
byte[] data = new byte[1048576];
|
byte[] data = new byte[1048576];
|
||||||
ushort fletcher16 = 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 Fletcher16Context();
|
||||||
|
ctx.Update(data);
|
||||||
|
byte[] result = ctx.Final();
|
||||||
|
|
||||||
IntPtr ctx = fletcher16_init();
|
if(result?.Length != _expectedRandomFletcher16.Length)
|
||||||
|
throw new Exception("Invalid hash length");
|
||||||
|
|
||||||
if(ctx == IntPtr.Zero)
|
if(result.Where((t, i) => t != _expectedRandomFletcher16[i]).Any())
|
||||||
throw new Exception("Could not initialize digest");
|
|
||||||
|
|
||||||
int ret = fletcher16_update(ctx, data, (uint)data.Length);
|
|
||||||
|
|
||||||
if(ret != 0)
|
|
||||||
throw new Exception("Could not digest block");
|
|
||||||
|
|
||||||
ret = fletcher16_final(ctx, ref fletcher16);
|
|
||||||
|
|
||||||
if(ret != 0)
|
|
||||||
throw new Exception("Could not finalize hash");
|
|
||||||
|
|
||||||
fletcher16_free(ctx);
|
|
||||||
|
|
||||||
fletcher16 = (ushort)((fletcher16 << 8) | (fletcher16 >> 8));
|
|
||||||
|
|
||||||
hash = BitConverter.GetBytes(fletcher16);
|
|
||||||
|
|
||||||
if(hash.Where((t, i) => t != _expectedRandomFletcher16[i]).Any())
|
|
||||||
throw new Exception("Invalid hash value");
|
throw new Exception("Invalid hash value");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user