Move to block based namespaces so it can compile with .NET Core 3.1.

This commit is contained in:
2023-09-21 14:03:21 +01:00
parent 2bd046ef06
commit 362aea152c
9 changed files with 1840 additions and 1813 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -5,341 +5,344 @@ using System.Linq;
using Aaru.Checksums; using Aaru.Checksums;
using Aaru.CommonTypes.Interfaces; using Aaru.CommonTypes.Interfaces;
namespace AaruBenchmark.Checksums; // ReSharper disable ArrangeNamespaceBody
public class Aaru namespace AaruBenchmark.Checksums
{ {
static readonly byte[] _expectedRandomAdler32 = public class Aaru
{ {
0x37, 0x28, 0xd1, 0x86 static readonly byte[] _expectedRandomAdler32 =
}; {
0x37, 0x28, 0xd1, 0x86
static readonly byte[] _expectedRandomFletcher16 = };
{
0x33, 0x57 static readonly byte[] _expectedRandomFletcher16 =
}; {
0x33, 0x57
static readonly byte[] _expectedRandomFletcher32 = };
{
0x21, 0x12, 0x61, 0xF5 static readonly byte[] _expectedRandomFletcher32 =
}; {
0x21, 0x12, 0x61, 0xF5
static readonly byte[] _expectedRandomCrc16Ccitt = };
{
0xC9, 0xBF static readonly byte[] _expectedRandomCrc16Ccitt =
}; {
0xC9, 0xBF
static readonly byte[] _expectedRandomCrc16 = };
{
0x2d, 0x6d static readonly byte[] _expectedRandomCrc16 =
}; {
0x2d, 0x6d
static readonly byte[] _expectedRandomCrc32 = };
{
0x2b, 0x6e, 0x68, 0x54 static readonly byte[] _expectedRandomCrc32 =
}; {
0x2b, 0x6e, 0x68, 0x54
static readonly byte[] _expectedRandomCrc64 = };
{
0xbf, 0x09, 0x99, 0x2c, 0xc5, 0xed, 0xe3, 0x8e static readonly byte[] _expectedRandomCrc64 =
}; {
0xbf, 0x09, 0x99, 0x2c, 0xc5, 0xed, 0xe3, 0x8e
static readonly byte[] _expectedRandomMd5 = };
{
0xd7, 0x8f, 0x0e, 0xec, 0x41, 0x7b, 0xe3, 0x86, 0x21, 0x9b, 0x21, 0xb7, 0x00, 0x04, 0x4b, 0x95 static readonly byte[] _expectedRandomMd5 =
}; {
0xd7, 0x8f, 0x0e, 0xec, 0x41, 0x7b, 0xe3, 0x86, 0x21, 0x9b, 0x21, 0xb7, 0x00, 0x04, 0x4b, 0x95
static readonly byte[] _expectedRandomSha1 = };
{
0x72, 0x0d, 0x3b, 0x71, 0x7d, 0xe0, 0xc7, 0x4c, 0x77, 0xdd, 0x9c, 0xaa, 0x9e, 0xba, 0x50, 0x60, 0xdc, 0xbd, static readonly byte[] _expectedRandomSha1 =
0x28, 0x8d {
}; 0x72, 0x0d, 0x3b, 0x71, 0x7d, 0xe0, 0xc7, 0x4c, 0x77, 0xdd, 0x9c, 0xaa, 0x9e, 0xba, 0x50, 0x60, 0xdc, 0xbd,
0x28, 0x8d
static readonly byte[] _expectedRandomSha256 = };
{
0x4d, 0x1a, 0x6b, 0x8a, 0x54, 0x67, 0x00, 0xc4, 0x8e, 0xda, 0x70, 0xd3, 0x39, 0x1c, 0x8f, 0x15, 0x8a, 0x8d, static readonly byte[] _expectedRandomSha256 =
0x12, 0xb2, 0x38, 0x92, 0x89, 0x29, 0x50, 0x47, 0x8c, 0x41, 0x8e, 0x25, 0xcc, 0x39 {
}; 0x4d, 0x1a, 0x6b, 0x8a, 0x54, 0x67, 0x00, 0xc4, 0x8e, 0xda, 0x70, 0xd3, 0x39, 0x1c, 0x8f, 0x15, 0x8a, 0x8d,
0x12, 0xb2, 0x38, 0x92, 0x89, 0x29, 0x50, 0x47, 0x8c, 0x41, 0x8e, 0x25, 0xcc, 0x39
static readonly byte[] _expectedRandomSha384 = };
{
0xdb, 0x53, 0x0e, 0x17, 0x9b, 0x81, 0xfe, 0x5f, 0x6d, 0x20, 0x41, 0x04, 0x6e, 0x77, 0xd9, 0x85, 0xf2, 0x85, static readonly byte[] _expectedRandomSha384 =
0x8a, 0x66, 0xca, 0xd3, 0x8d, 0x1a, 0xd5, 0xac, 0x67, 0xa9, 0x74, 0xe1, 0xef, 0x3f, 0x4d, 0xdf, 0x94, 0x15, {
0x2e, 0xac, 0x2e, 0xfe, 0x16, 0x95, 0x81, 0x54, 0xdc, 0x59, 0xd4, 0xc3 0xdb, 0x53, 0x0e, 0x17, 0x9b, 0x81, 0xfe, 0x5f, 0x6d, 0x20, 0x41, 0x04, 0x6e, 0x77, 0xd9, 0x85, 0xf2, 0x85,
}; 0x8a, 0x66, 0xca, 0xd3, 0x8d, 0x1a, 0xd5, 0xac, 0x67, 0xa9, 0x74, 0xe1, 0xef, 0x3f, 0x4d, 0xdf, 0x94, 0x15,
0x2e, 0xac, 0x2e, 0xfe, 0x16, 0x95, 0x81, 0x54, 0xdc, 0x59, 0xd4, 0xc3
static readonly byte[] _expectedRandomSha512 = };
{
0x6a, 0x0a, 0x18, 0xc2, 0xad, 0xf8, 0x83, 0xac, 0x58, 0xe6, 0x21, 0x96, 0xdb, 0x8d, 0x3d, 0x0e, 0xb9, 0x87, static readonly byte[] _expectedRandomSha512 =
0xd1, 0x49, 0x24, 0x97, 0xdb, 0x15, 0xb9, 0xfc, 0xcc, 0xb0, 0x36, 0xdf, 0x64, 0xae, 0xdb, 0x3e, 0x82, 0xa0, {
0x4d, 0xdc, 0xd1, 0x37, 0x48, 0x92, 0x95, 0x51, 0xf9, 0xdd, 0xab, 0x82, 0xf4, 0x8a, 0x85, 0x3f, 0x9a, 0x01, 0x6a, 0x0a, 0x18, 0xc2, 0xad, 0xf8, 0x83, 0xac, 0x58, 0xe6, 0x21, 0x96, 0xdb, 0x8d, 0x3d, 0x0e, 0xb9, 0x87,
0xb5, 0xf2, 0x8c, 0xbb, 0x4a, 0xa5, 0x1b, 0x40, 0x7c, 0xb6 0xd1, 0x49, 0x24, 0x97, 0xdb, 0x15, 0xb9, 0xfc, 0xcc, 0xb0, 0x36, 0xdf, 0x64, 0xae, 0xdb, 0x3e, 0x82, 0xa0,
}; 0x4d, 0xdc, 0xd1, 0x37, 0x48, 0x92, 0x95, 0x51, 0xf9, 0xdd, 0xab, 0x82, 0xf4, 0x8a, 0x85, 0x3f, 0x9a, 0x01,
0xb5, 0xf2, 0x8c, 0xbb, 0x4a, 0xa5, 0x1b, 0x40, 0x7c, 0xb6
public static void Fletcher16() };
{
byte[] data = new byte[1048576]; public static void Fletcher16()
{
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read); byte[] data = new byte[1048576];
fs.Read(data, 0, 1048576); var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
fs.Close();
fs.Dispose(); fs.Read(data, 0, 1048576);
IChecksum ctx = new Fletcher16Context(); fs.Close();
ctx.Update(data); fs.Dispose();
byte[] result = ctx.Final(); IChecksum ctx = new Fletcher16Context();
ctx.Update(data);
if(result?.Length != _expectedRandomFletcher16.Length) byte[] result = ctx.Final();
throw new Exception("Invalid hash length");
if(result?.Length != _expectedRandomFletcher16.Length)
if(result.Where((t, i) => t != _expectedRandomFletcher16[i]).Any()) throw new Exception("Invalid hash length");
throw new Exception("Invalid hash value");
} if(result.Where((t, i) => t != _expectedRandomFletcher16[i]).Any())
throw new Exception("Invalid hash value");
public static void Fletcher32() }
{
byte[] data = new byte[1048576]; public static void Fletcher32()
{
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read); byte[] data = new byte[1048576];
fs.Read(data, 0, 1048576); var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
fs.Close();
fs.Dispose(); fs.Read(data, 0, 1048576);
IChecksum ctx = new Fletcher32Context(); fs.Close();
ctx.Update(data); fs.Dispose();
byte[] result = ctx.Final(); IChecksum ctx = new Fletcher32Context();
ctx.Update(data);
if(result?.Length != _expectedRandomFletcher32.Length) byte[] result = ctx.Final();
throw new Exception("Invalid hash length");
if(result?.Length != _expectedRandomFletcher32.Length)
if(result.Where((t, i) => t != _expectedRandomFletcher32[i]).Any()) throw new Exception("Invalid hash length");
throw new Exception("Invalid hash value");
} if(result.Where((t, i) => t != _expectedRandomFletcher32[i]).Any())
throw new Exception("Invalid hash value");
public static void Adler32() }
{
byte[] data = new byte[1048576]; public static void Adler32()
{
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read); byte[] data = new byte[1048576];
fs.Read(data, 0, 1048576); var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
fs.Close();
fs.Dispose(); fs.Read(data, 0, 1048576);
IChecksum ctx = new Adler32Context(); fs.Close();
ctx.Update(data); fs.Dispose();
byte[] result = ctx.Final(); IChecksum ctx = new Adler32Context();
ctx.Update(data);
if(result?.Length != _expectedRandomAdler32.Length) byte[] result = ctx.Final();
throw new Exception("Invalid hash length");
if(result?.Length != _expectedRandomAdler32.Length)
if(result.Where((t, i) => t != _expectedRandomAdler32[i]).Any()) throw new Exception("Invalid hash length");
throw new Exception("Invalid hash value");
} if(result.Where((t, i) => t != _expectedRandomAdler32[i]).Any())
throw new Exception("Invalid hash value");
public static void Crc16Ccitt() }
{
byte[] data = new byte[1048576]; public static void Crc16Ccitt()
{
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read); byte[] data = new byte[1048576];
fs.Read(data, 0, 1048576); var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
fs.Close();
fs.Dispose(); fs.Read(data, 0, 1048576);
IChecksum ctx = new CRC16CCITTContext(); fs.Close();
ctx.Update(data); fs.Dispose();
byte[] result = ctx.Final(); IChecksum ctx = new CRC16CCITTContext();
ctx.Update(data);
if(result?.Length != _expectedRandomCrc16Ccitt.Length) byte[] result = ctx.Final();
throw new Exception("Invalid hash length");
if(result?.Length != _expectedRandomCrc16Ccitt.Length)
if(result.Where((t, i) => t != _expectedRandomCrc16Ccitt[i]).Any()) throw new Exception("Invalid hash length");
throw new Exception("Invalid hash value");
} if(result.Where((t, i) => t != _expectedRandomCrc16Ccitt[i]).Any())
throw new Exception("Invalid hash value");
public static void Crc16() }
{
byte[] data = new byte[1048576]; public static void Crc16()
{
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read); byte[] data = new byte[1048576];
fs.Read(data, 0, 1048576); var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
fs.Close();
fs.Dispose(); fs.Read(data, 0, 1048576);
IChecksum ctx = new CRC16IBMContext(); fs.Close();
ctx.Update(data); fs.Dispose();
byte[] result = ctx.Final(); IChecksum ctx = new CRC16IBMContext();
ctx.Update(data);
if(result?.Length != _expectedRandomCrc16.Length) byte[] result = ctx.Final();
throw new Exception("Invalid hash length");
if(result?.Length != _expectedRandomCrc16.Length)
if(result.Where((t, i) => t != _expectedRandomCrc16[i]).Any()) throw new Exception("Invalid hash length");
throw new Exception("Invalid hash value");
} if(result.Where((t, i) => t != _expectedRandomCrc16[i]).Any())
throw new Exception("Invalid hash value");
public static void Crc32() }
{
byte[] data = new byte[1048576]; public static void Crc32()
{
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read); byte[] data = new byte[1048576];
fs.Read(data, 0, 1048576); var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
fs.Close();
fs.Dispose(); fs.Read(data, 0, 1048576);
IChecksum ctx = new Crc32Context(); fs.Close();
ctx.Update(data); fs.Dispose();
byte[] result = ctx.Final(); IChecksum ctx = new Crc32Context();
ctx.Update(data);
if(result?.Length != _expectedRandomCrc32.Length) byte[] result = ctx.Final();
throw new Exception("Invalid hash length");
if(result?.Length != _expectedRandomCrc32.Length)
if(result.Where((t, i) => t != _expectedRandomCrc32[i]).Any()) throw new Exception("Invalid hash length");
throw new Exception("Invalid hash value");
} if(result.Where((t, i) => t != _expectedRandomCrc32[i]).Any())
throw new Exception("Invalid hash value");
public static void Crc64() }
{
byte[] data = new byte[1048576]; public static void Crc64()
{
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read); byte[] data = new byte[1048576];
fs.Read(data, 0, 1048576); var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
fs.Close();
fs.Dispose(); fs.Read(data, 0, 1048576);
IChecksum ctx = new Crc64Context(); fs.Close();
ctx.Update(data); fs.Dispose();
byte[] result = ctx.Final(); IChecksum ctx = new Crc64Context();
ctx.Update(data);
if(result?.Length != _expectedRandomCrc64.Length) byte[] result = ctx.Final();
throw new Exception("Invalid hash length");
if(result?.Length != _expectedRandomCrc64.Length)
if(result.Where((t, i) => t != _expectedRandomCrc64[i]).Any()) throw new Exception("Invalid hash length");
throw new Exception("Invalid hash value");
} if(result.Where((t, i) => t != _expectedRandomCrc64[i]).Any())
throw new Exception("Invalid hash value");
public static void Md5() }
{
byte[] data = new byte[1048576]; public static void Md5()
{
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read); byte[] data = new byte[1048576];
fs.Read(data, 0, 1048576); var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
fs.Close();
fs.Dispose(); fs.Read(data, 0, 1048576);
IChecksum ctx = new Md5Context(); fs.Close();
ctx.Update(data); fs.Dispose();
byte[] result = ctx.Final(); IChecksum ctx = new Md5Context();
ctx.Update(data);
if(result?.Length != _expectedRandomMd5.Length) byte[] result = ctx.Final();
throw new Exception("Invalid hash length");
if(result?.Length != _expectedRandomMd5.Length)
if(result.Where((t, i) => t != _expectedRandomMd5[i]).Any()) throw new Exception("Invalid hash length");
throw new Exception("Invalid hash value");
} if(result.Where((t, i) => t != _expectedRandomMd5[i]).Any())
throw new Exception("Invalid hash value");
public static void SpamSum() }
{
byte[] data = new byte[1048576]; public static void SpamSum()
{
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read); byte[] data = new byte[1048576];
fs.Read(data, 0, 1048576); var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
fs.Close();
fs.Dispose(); fs.Read(data, 0, 1048576);
IChecksum ctx = new SpamSumContext(); fs.Close();
ctx.Update(data); fs.Dispose();
string result = ctx.End(); IChecksum ctx = new SpamSumContext();
} ctx.Update(data);
string result = ctx.End();
public static void CliSpamSum() }
{
var proc = new Process(); public static void CliSpamSum()
proc.StartInfo.UseShellExecute = false; {
proc.StartInfo.FileName = "/usr/bin/ssdeep"; var proc = new Process();
proc.StartInfo.CreateNoWindow = true; proc.StartInfo.UseShellExecute = false;
proc.StartInfo.ArgumentList.Add(Path.Combine(Program.Folder, "random")); proc.StartInfo.FileName = "/usr/bin/ssdeep";
proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.CreateNoWindow = true;
proc.Start(); proc.StartInfo.ArgumentList.Add(Path.Combine(Program.Folder, "random"));
proc.StandardOutput.ReadToEnd(); proc.StartInfo.RedirectStandardOutput = true;
proc.WaitForExit(); proc.Start();
} proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
public static void Sha1() }
{
byte[] data = new byte[1048576]; public static void Sha1()
{
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read); byte[] data = new byte[1048576];
fs.Read(data, 0, 1048576); var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
fs.Close();
fs.Dispose(); fs.Read(data, 0, 1048576);
IChecksum ctx = new Sha1Context(); fs.Close();
ctx.Update(data); fs.Dispose();
byte[] result = ctx.Final(); IChecksum ctx = new Sha1Context();
ctx.Update(data);
if(result?.Length != _expectedRandomSha1.Length) byte[] result = ctx.Final();
throw new Exception("Invalid hash length");
if(result?.Length != _expectedRandomSha1.Length)
if(result.Where((t, i) => t != _expectedRandomSha1[i]).Any()) throw new Exception("Invalid hash length");
throw new Exception("Invalid hash value");
} if(result.Where((t, i) => t != _expectedRandomSha1[i]).Any())
throw new Exception("Invalid hash value");
public static void Sha256() }
{
byte[] data = new byte[1048576]; public static void Sha256()
{
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read); byte[] data = new byte[1048576];
fs.Read(data, 0, 1048576); var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
fs.Close();
fs.Dispose(); fs.Read(data, 0, 1048576);
IChecksum ctx = new Sha256Context(); fs.Close();
ctx.Update(data); fs.Dispose();
byte[] result = ctx.Final(); IChecksum ctx = new Sha256Context();
ctx.Update(data);
if(result?.Length != _expectedRandomSha256.Length) byte[] result = ctx.Final();
throw new Exception("Invalid hash length");
if(result?.Length != _expectedRandomSha256.Length)
if(result.Where((t, i) => t != _expectedRandomSha256[i]).Any()) throw new Exception("Invalid hash length");
throw new Exception("Invalid hash value");
} if(result.Where((t, i) => t != _expectedRandomSha256[i]).Any())
throw new Exception("Invalid hash value");
public static void Sha384() }
{
byte[] data = new byte[1048576]; public static void Sha384()
{
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read); byte[] data = new byte[1048576];
fs.Read(data, 0, 1048576); var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
fs.Close();
fs.Dispose(); fs.Read(data, 0, 1048576);
IChecksum ctx = new Sha384Context(); fs.Close();
ctx.Update(data); fs.Dispose();
byte[] result = ctx.Final(); IChecksum ctx = new Sha384Context();
ctx.Update(data);
if(result?.Length != _expectedRandomSha384.Length) byte[] result = ctx.Final();
throw new Exception("Invalid hash length");
if(result?.Length != _expectedRandomSha384.Length)
if(result.Where((t, i) => t != _expectedRandomSha384[i]).Any()) throw new Exception("Invalid hash length");
throw new Exception("Invalid hash value");
} if(result.Where((t, i) => t != _expectedRandomSha384[i]).Any())
throw new Exception("Invalid hash value");
public static void Sha512() }
{
byte[] data = new byte[1048576]; public static void Sha512()
{
var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read); byte[] data = new byte[1048576];
fs.Read(data, 0, 1048576); var fs = new FileStream(Path.Combine(Program.Folder, "random"), FileMode.Open, FileAccess.Read);
fs.Close();
fs.Dispose(); fs.Read(data, 0, 1048576);
IChecksum ctx = new Sha512Context(); fs.Close();
ctx.Update(data); fs.Dispose();
byte[] result = ctx.Final(); IChecksum ctx = new Sha512Context();
ctx.Update(data);
if(result?.Length != _expectedRandomSha512.Length) byte[] result = ctx.Final();
throw new Exception("Invalid hash length");
if(result?.Length != _expectedRandomSha512.Length)
if(result.Where((t, i) => t != _expectedRandomSha512[i]).Any()) throw new Exception("Invalid hash length");
throw new Exception("Invalid hash value");
if(result.Where((t, i) => t != _expectedRandomSha512[i]).Any())
throw new Exception("Invalid hash value");
}
} }
} }

View File

@@ -3,244 +3,247 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace AaruBenchmark.Checksums; // ReSharper disable ArrangeNamespaceBody
public class OpenSsl namespace AaruBenchmark.Checksums
{ {
static readonly byte[] _expectedRandomMd5 = public class OpenSsl
{ {
0xd7, 0x8f, 0x0e, 0xec, 0x41, 0x7b, 0xe3, 0x86, 0x21, 0x9b, 0x21, 0xb7, 0x00, 0x04, 0x4b, 0x95 static readonly byte[] _expectedRandomMd5 =
}; {
0xd7, 0x8f, 0x0e, 0xec, 0x41, 0x7b, 0xe3, 0x86, 0x21, 0x9b, 0x21, 0xb7, 0x00, 0x04, 0x4b, 0x95
};
static readonly byte[] _expectedRandomSha1 = static readonly byte[] _expectedRandomSha1 =
{ {
0x72, 0x0d, 0x3b, 0x71, 0x7d, 0xe0, 0xc7, 0x4c, 0x77, 0xdd, 0x9c, 0xaa, 0x9e, 0xba, 0x50, 0x60, 0xdc, 0xbd, 0x72, 0x0d, 0x3b, 0x71, 0x7d, 0xe0, 0xc7, 0x4c, 0x77, 0xdd, 0x9c, 0xaa, 0x9e, 0xba, 0x50, 0x60, 0xdc, 0xbd,
0x28, 0x8d 0x28, 0x8d
}; };
static readonly byte[] _expectedRandomSha256 = static readonly byte[] _expectedRandomSha256 =
{ {
0x4d, 0x1a, 0x6b, 0x8a, 0x54, 0x67, 0x00, 0xc4, 0x8e, 0xda, 0x70, 0xd3, 0x39, 0x1c, 0x8f, 0x15, 0x8a, 0x8d, 0x4d, 0x1a, 0x6b, 0x8a, 0x54, 0x67, 0x00, 0xc4, 0x8e, 0xda, 0x70, 0xd3, 0x39, 0x1c, 0x8f, 0x15, 0x8a, 0x8d,
0x12, 0xb2, 0x38, 0x92, 0x89, 0x29, 0x50, 0x47, 0x8c, 0x41, 0x8e, 0x25, 0xcc, 0x39 0x12, 0xb2, 0x38, 0x92, 0x89, 0x29, 0x50, 0x47, 0x8c, 0x41, 0x8e, 0x25, 0xcc, 0x39
}; };
static readonly byte[] _expectedRandomSha384 = static readonly byte[] _expectedRandomSha384 =
{ {
0xdb, 0x53, 0x0e, 0x17, 0x9b, 0x81, 0xfe, 0x5f, 0x6d, 0x20, 0x41, 0x04, 0x6e, 0x77, 0xd9, 0x85, 0xf2, 0x85, 0xdb, 0x53, 0x0e, 0x17, 0x9b, 0x81, 0xfe, 0x5f, 0x6d, 0x20, 0x41, 0x04, 0x6e, 0x77, 0xd9, 0x85, 0xf2, 0x85,
0x8a, 0x66, 0xca, 0xd3, 0x8d, 0x1a, 0xd5, 0xac, 0x67, 0xa9, 0x74, 0xe1, 0xef, 0x3f, 0x4d, 0xdf, 0x94, 0x15, 0x8a, 0x66, 0xca, 0xd3, 0x8d, 0x1a, 0xd5, 0xac, 0x67, 0xa9, 0x74, 0xe1, 0xef, 0x3f, 0x4d, 0xdf, 0x94, 0x15,
0x2e, 0xac, 0x2e, 0xfe, 0x16, 0x95, 0x81, 0x54, 0xdc, 0x59, 0xd4, 0xc3 0x2e, 0xac, 0x2e, 0xfe, 0x16, 0x95, 0x81, 0x54, 0xdc, 0x59, 0xd4, 0xc3
}; };
static readonly byte[] _expectedRandomSha512 = static readonly byte[] _expectedRandomSha512 =
{ {
0x6a, 0x0a, 0x18, 0xc2, 0xad, 0xf8, 0x83, 0xac, 0x58, 0xe6, 0x21, 0x96, 0xdb, 0x8d, 0x3d, 0x0e, 0xb9, 0x87, 0x6a, 0x0a, 0x18, 0xc2, 0xad, 0xf8, 0x83, 0xac, 0x58, 0xe6, 0x21, 0x96, 0xdb, 0x8d, 0x3d, 0x0e, 0xb9, 0x87,
0xd1, 0x49, 0x24, 0x97, 0xdb, 0x15, 0xb9, 0xfc, 0xcc, 0xb0, 0x36, 0xdf, 0x64, 0xae, 0xdb, 0x3e, 0x82, 0xa0, 0xd1, 0x49, 0x24, 0x97, 0xdb, 0x15, 0xb9, 0xfc, 0xcc, 0xb0, 0x36, 0xdf, 0x64, 0xae, 0xdb, 0x3e, 0x82, 0xa0,
0x4d, 0xdc, 0xd1, 0x37, 0x48, 0x92, 0x95, 0x51, 0xf9, 0xdd, 0xab, 0x82, 0xf4, 0x8a, 0x85, 0x3f, 0x9a, 0x01, 0x4d, 0xdc, 0xd1, 0x37, 0x48, 0x92, 0x95, 0x51, 0xf9, 0xdd, 0xab, 0x82, 0xf4, 0x8a, 0x85, 0x3f, 0x9a, 0x01,
0xb5, 0xf2, 0x8c, 0xbb, 0x4a, 0xa5, 0x1b, 0x40, 0x7c, 0xb6 0xb5, 0xf2, 0x8c, 0xbb, 0x4a, 0xa5, 0x1b, 0x40, 0x7c, 0xb6
}; };
[DllImport("libssl", SetLastError = true)] [DllImport("libssl", SetLastError = true)]
static extern int EVP_DigestInit(IntPtr ctx, IntPtr type); static extern int EVP_DigestInit(IntPtr ctx, IntPtr type);
[DllImport("libssl", SetLastError = true)] [DllImport("libssl", SetLastError = true)]
static extern IntPtr EVP_MD_CTX_new(); static extern IntPtr EVP_MD_CTX_new();
[DllImport("libssl", SetLastError = true)] [DllImport("libssl", SetLastError = true)]
static extern IntPtr EVP_md5(); static extern IntPtr EVP_md5();
[DllImport("libssl", SetLastError = true)] [DllImport("libssl", SetLastError = true)]
static extern IntPtr EVP_sha1(); static extern IntPtr EVP_sha1();
[DllImport("libssl", SetLastError = true)] [DllImport("libssl", SetLastError = true)]
static extern IntPtr EVP_sha256(); static extern IntPtr EVP_sha256();
[DllImport("libssl", SetLastError = true)] [DllImport("libssl", SetLastError = true)]
static extern IntPtr EVP_sha384(); static extern IntPtr EVP_sha384();
[DllImport("libssl", SetLastError = true)] [DllImport("libssl", SetLastError = true)]
static extern IntPtr EVP_sha512(); static extern IntPtr EVP_sha512();
[DllImport("libssl", SetLastError = true)] [DllImport("libssl", SetLastError = true)]
static extern int EVP_DigestUpdate(IntPtr ctx, byte[] d, ulong cnt); static extern int EVP_DigestUpdate(IntPtr ctx, byte[] d, ulong cnt);
[DllImport("libssl", SetLastError = true)] [DllImport("libssl", SetLastError = true)]
static extern int EVP_DigestFinal(IntPtr ctx, byte[] md, ref uint s); static extern int EVP_DigestFinal(IntPtr ctx, byte[] md, ref uint s);
[DllImport("libssl", SetLastError = true)] [DllImport("libssl", SetLastError = true)]
static extern void EVP_MD_CTX_free(IntPtr ctx); static extern void EVP_MD_CTX_free(IntPtr ctx);
public static void Md5() public static void Md5()
{ {
byte[] data = new byte[1048576]; byte[] data = new byte[1048576];
uint s = 0; uint s = 0;
byte[] hash = new byte[16]; byte[] hash = new byte[16];
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();
IntPtr md = EVP_md5(); IntPtr md = EVP_md5();
IntPtr ctx = EVP_MD_CTX_new(); IntPtr ctx = EVP_MD_CTX_new();
int ret = EVP_DigestInit(ctx, md); int ret = EVP_DigestInit(ctx, md);
if(ret != 1) if(ret != 1)
throw new Exception("Could not initialize digest"); throw new Exception("Could not initialize digest");
ret = EVP_DigestUpdate(ctx, data, (ulong)data.Length); ret = EVP_DigestUpdate(ctx, data, (ulong)data.Length);
if(ret != 1) if(ret != 1)
throw new Exception("Could not digest block"); throw new Exception("Could not digest block");
ret = EVP_DigestFinal(ctx, hash, ref s); ret = EVP_DigestFinal(ctx, hash, ref s);
if(ret != 1) if(ret != 1)
throw new Exception("Could not finalize hash"); throw new Exception("Could not finalize hash");
EVP_MD_CTX_free(ctx); EVP_MD_CTX_free(ctx);
if(hash.Where((t, i) => t != _expectedRandomMd5[i]).Any()) if(hash.Where((t, i) => t != _expectedRandomMd5[i]).Any())
throw new Exception("Invalid hash value"); throw new Exception("Invalid hash value");
} }
public static void Sha1() public static void Sha1()
{ {
byte[] data = new byte[1048576]; byte[] data = new byte[1048576];
uint s = 0; uint s = 0;
byte[] hash = new byte[20]; byte[] hash = new byte[20];
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();
IntPtr md = EVP_sha1(); IntPtr md = EVP_sha1();
IntPtr ctx = EVP_MD_CTX_new(); IntPtr ctx = EVP_MD_CTX_new();
int ret = EVP_DigestInit(ctx, md); int ret = EVP_DigestInit(ctx, md);
if(ret != 1) if(ret != 1)
throw new Exception("Could not initialize digest"); throw new Exception("Could not initialize digest");
ret = EVP_DigestUpdate(ctx, data, (ulong)data.Length); ret = EVP_DigestUpdate(ctx, data, (ulong)data.Length);
if(ret != 1) if(ret != 1)
throw new Exception("Could not digest block"); throw new Exception("Could not digest block");
ret = EVP_DigestFinal(ctx, hash, ref s); ret = EVP_DigestFinal(ctx, hash, ref s);
if(ret != 1) if(ret != 1)
throw new Exception("Could not finalize hash"); throw new Exception("Could not finalize hash");
EVP_MD_CTX_free(ctx); EVP_MD_CTX_free(ctx);
if(hash.Where((t, i) => t != _expectedRandomSha1[i]).Any()) if(hash.Where((t, i) => t != _expectedRandomSha1[i]).Any())
throw new Exception("Invalid hash value"); throw new Exception("Invalid hash value");
} }
public static void Sha256() public static void Sha256()
{ {
byte[] data = new byte[1048576]; byte[] data = new byte[1048576];
uint s = 0; uint s = 0;
byte[] hash = new byte[32]; byte[] hash = new byte[32];
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();
IntPtr md = EVP_sha256(); IntPtr md = EVP_sha256();
IntPtr ctx = EVP_MD_CTX_new(); IntPtr ctx = EVP_MD_CTX_new();
int ret = EVP_DigestInit(ctx, md); int ret = EVP_DigestInit(ctx, md);
if(ret != 1) if(ret != 1)
throw new Exception("Could not initialize digest"); throw new Exception("Could not initialize digest");
ret = EVP_DigestUpdate(ctx, data, (ulong)data.Length); ret = EVP_DigestUpdate(ctx, data, (ulong)data.Length);
if(ret != 1) if(ret != 1)
throw new Exception("Could not digest block"); throw new Exception("Could not digest block");
ret = EVP_DigestFinal(ctx, hash, ref s); ret = EVP_DigestFinal(ctx, hash, ref s);
if(ret != 1) if(ret != 1)
throw new Exception("Could not finalize hash"); throw new Exception("Could not finalize hash");
EVP_MD_CTX_free(ctx); EVP_MD_CTX_free(ctx);
if(hash.Where((t, i) => t != _expectedRandomSha256[i]).Any()) if(hash.Where((t, i) => t != _expectedRandomSha256[i]).Any())
throw new Exception("Invalid hash value"); throw new Exception("Invalid hash value");
} }
public static void Sha384() public static void Sha384()
{ {
byte[] data = new byte[1048576]; byte[] data = new byte[1048576];
uint s = 0; uint s = 0;
byte[] hash = new byte[48]; byte[] hash = new byte[48];
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();
IntPtr md = EVP_sha384(); IntPtr md = EVP_sha384();
IntPtr ctx = EVP_MD_CTX_new(); IntPtr ctx = EVP_MD_CTX_new();
int ret = EVP_DigestInit(ctx, md); int ret = EVP_DigestInit(ctx, md);
if(ret != 1) if(ret != 1)
throw new Exception("Could not initialize digest"); throw new Exception("Could not initialize digest");
ret = EVP_DigestUpdate(ctx, data, (ulong)data.Length); ret = EVP_DigestUpdate(ctx, data, (ulong)data.Length);
if(ret != 1) if(ret != 1)
throw new Exception("Could not digest block"); throw new Exception("Could not digest block");
ret = EVP_DigestFinal(ctx, hash, ref s); ret = EVP_DigestFinal(ctx, hash, ref s);
if(ret != 1) if(ret != 1)
throw new Exception("Could not finalize hash"); throw new Exception("Could not finalize hash");
EVP_MD_CTX_free(ctx); EVP_MD_CTX_free(ctx);
if(hash.Where((t, i) => t != _expectedRandomSha384[i]).Any()) if(hash.Where((t, i) => t != _expectedRandomSha384[i]).Any())
throw new Exception("Invalid hash value"); throw new Exception("Invalid hash value");
} }
public static void Sha512() public static void Sha512()
{ {
byte[] data = new byte[1048576]; byte[] data = new byte[1048576];
uint s = 0; uint s = 0;
byte[] hash = new byte[64]; byte[] hash = new byte[64];
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();
IntPtr md = EVP_sha512(); IntPtr md = EVP_sha512();
IntPtr ctx = EVP_MD_CTX_new(); IntPtr ctx = EVP_MD_CTX_new();
int ret = EVP_DigestInit(ctx, md); int ret = EVP_DigestInit(ctx, md);
if(ret != 1) if(ret != 1)
throw new Exception("Could not initialize digest"); throw new Exception("Could not initialize digest");
ret = EVP_DigestUpdate(ctx, data, (ulong)data.Length); ret = EVP_DigestUpdate(ctx, data, (ulong)data.Length);
if(ret != 1) if(ret != 1)
throw new Exception("Could not digest block"); throw new Exception("Could not digest block");
ret = EVP_DigestFinal(ctx, hash, ref s); ret = EVP_DigestFinal(ctx, hash, ref s);
if(ret != 1) if(ret != 1)
throw new Exception("Could not finalize hash"); throw new Exception("Could not finalize hash");
EVP_MD_CTX_free(ctx); EVP_MD_CTX_free(ctx);
if(hash.Where((t, i) => t != _expectedRandomSha512[i]).Any()) if(hash.Where((t, i) => t != _expectedRandomSha512[i]).Any())
throw new Exception("Invalid hash value"); throw new Exception("Invalid hash value");
}
} }
} }

View File

@@ -3,263 +3,266 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace AaruBenchmark.Checksums; // ReSharper disable ArrangeNamespaceBody
public class RHash namespace AaruBenchmark.Checksums
{ {
const uint RHASH_CRC32 = 0x01; public class RHash
const uint RHASH_MD5 = 0x04;
const uint RHASH_SHA1 = 0x08;
const uint RHASH_SHA256 = 0x20000;
const uint RHASH_SHA384 = 0x40000;
const uint RHASH_SHA512 = 0x80000;
const uint RHASH_CRC32C = 0x4000000;
static readonly byte[] _expectedRandomCrc32 =
{ {
0x2b, 0x6e, 0x68, 0x54 const uint RHASH_CRC32 = 0x01;
}; const uint RHASH_MD5 = 0x04;
static readonly byte[] _expectedRandomMd5 = const uint RHASH_SHA1 = 0x08;
{ const uint RHASH_SHA256 = 0x20000;
0xd7, 0x8f, 0x0e, 0xec, 0x41, 0x7b, 0xe3, 0x86, 0x21, 0x9b, 0x21, 0xb7, 0x00, 0x04, 0x4b, 0x95 const uint RHASH_SHA384 = 0x40000;
}; const uint RHASH_SHA512 = 0x80000;
const uint RHASH_CRC32C = 0x4000000;
static readonly byte[] _expectedRandomCrc32 =
{
0x2b, 0x6e, 0x68, 0x54
};
static readonly byte[] _expectedRandomMd5 =
{
0xd7, 0x8f, 0x0e, 0xec, 0x41, 0x7b, 0xe3, 0x86, 0x21, 0x9b, 0x21, 0xb7, 0x00, 0x04, 0x4b, 0x95
};
static readonly byte[] _expectedRandomSha1 = static readonly byte[] _expectedRandomSha1 =
{ {
0x72, 0x0d, 0x3b, 0x71, 0x7d, 0xe0, 0xc7, 0x4c, 0x77, 0xdd, 0x9c, 0xaa, 0x9e, 0xba, 0x50, 0x60, 0xdc, 0xbd, 0x72, 0x0d, 0x3b, 0x71, 0x7d, 0xe0, 0xc7, 0x4c, 0x77, 0xdd, 0x9c, 0xaa, 0x9e, 0xba, 0x50, 0x60, 0xdc, 0xbd,
0x28, 0x8d 0x28, 0x8d
}; };
static readonly byte[] _expectedRandomSha256 = static readonly byte[] _expectedRandomSha256 =
{ {
0x4d, 0x1a, 0x6b, 0x8a, 0x54, 0x67, 0x00, 0xc4, 0x8e, 0xda, 0x70, 0xd3, 0x39, 0x1c, 0x8f, 0x15, 0x8a, 0x8d, 0x4d, 0x1a, 0x6b, 0x8a, 0x54, 0x67, 0x00, 0xc4, 0x8e, 0xda, 0x70, 0xd3, 0x39, 0x1c, 0x8f, 0x15, 0x8a, 0x8d,
0x12, 0xb2, 0x38, 0x92, 0x89, 0x29, 0x50, 0x47, 0x8c, 0x41, 0x8e, 0x25, 0xcc, 0x39 0x12, 0xb2, 0x38, 0x92, 0x89, 0x29, 0x50, 0x47, 0x8c, 0x41, 0x8e, 0x25, 0xcc, 0x39
}; };
static readonly byte[] _expectedRandomSha384 = static readonly byte[] _expectedRandomSha384 =
{ {
0xdb, 0x53, 0x0e, 0x17, 0x9b, 0x81, 0xfe, 0x5f, 0x6d, 0x20, 0x41, 0x04, 0x6e, 0x77, 0xd9, 0x85, 0xf2, 0x85, 0xdb, 0x53, 0x0e, 0x17, 0x9b, 0x81, 0xfe, 0x5f, 0x6d, 0x20, 0x41, 0x04, 0x6e, 0x77, 0xd9, 0x85, 0xf2, 0x85,
0x8a, 0x66, 0xca, 0xd3, 0x8d, 0x1a, 0xd5, 0xac, 0x67, 0xa9, 0x74, 0xe1, 0xef, 0x3f, 0x4d, 0xdf, 0x94, 0x15, 0x8a, 0x66, 0xca, 0xd3, 0x8d, 0x1a, 0xd5, 0xac, 0x67, 0xa9, 0x74, 0xe1, 0xef, 0x3f, 0x4d, 0xdf, 0x94, 0x15,
0x2e, 0xac, 0x2e, 0xfe, 0x16, 0x95, 0x81, 0x54, 0xdc, 0x59, 0xd4, 0xc3 0x2e, 0xac, 0x2e, 0xfe, 0x16, 0x95, 0x81, 0x54, 0xdc, 0x59, 0xd4, 0xc3
}; };
static readonly byte[] _expectedRandomSha512 = static readonly byte[] _expectedRandomSha512 =
{ {
0x6a, 0x0a, 0x18, 0xc2, 0xad, 0xf8, 0x83, 0xac, 0x58, 0xe6, 0x21, 0x96, 0xdb, 0x8d, 0x3d, 0x0e, 0xb9, 0x87, 0x6a, 0x0a, 0x18, 0xc2, 0xad, 0xf8, 0x83, 0xac, 0x58, 0xe6, 0x21, 0x96, 0xdb, 0x8d, 0x3d, 0x0e, 0xb9, 0x87,
0xd1, 0x49, 0x24, 0x97, 0xdb, 0x15, 0xb9, 0xfc, 0xcc, 0xb0, 0x36, 0xdf, 0x64, 0xae, 0xdb, 0x3e, 0x82, 0xa0, 0xd1, 0x49, 0x24, 0x97, 0xdb, 0x15, 0xb9, 0xfc, 0xcc, 0xb0, 0x36, 0xdf, 0x64, 0xae, 0xdb, 0x3e, 0x82, 0xa0,
0x4d, 0xdc, 0xd1, 0x37, 0x48, 0x92, 0x95, 0x51, 0xf9, 0xdd, 0xab, 0x82, 0xf4, 0x8a, 0x85, 0x3f, 0x9a, 0x01, 0x4d, 0xdc, 0xd1, 0x37, 0x48, 0x92, 0x95, 0x51, 0xf9, 0xdd, 0xab, 0x82, 0xf4, 0x8a, 0x85, 0x3f, 0x9a, 0x01,
0xb5, 0xf2, 0x8c, 0xbb, 0x4a, 0xa5, 0x1b, 0x40, 0x7c, 0xb6 0xb5, 0xf2, 0x8c, 0xbb, 0x4a, 0xa5, 0x1b, 0x40, 0x7c, 0xb6
}; };
[DllImport("librhash", SetLastError = true)] [DllImport("librhash", SetLastError = true)]
static extern void rhash_library_init(); static extern void rhash_library_init();
[DllImport("librhash", SetLastError = true)] [DllImport("librhash", SetLastError = true)]
static extern IntPtr rhash_init(uint hash_id); static extern IntPtr rhash_init(uint hash_id);
[DllImport("librhash", SetLastError = true)] [DllImport("librhash", SetLastError = true)]
static extern int rhash_update(IntPtr ctx, byte[] message, ulong length); static extern int rhash_update(IntPtr ctx, byte[] message, ulong length);
[DllImport("librhash", SetLastError = true)] [DllImport("librhash", SetLastError = true)]
static extern int rhash_final(IntPtr ctx, byte[] first_result); static extern int rhash_final(IntPtr ctx, byte[] first_result);
[DllImport("librhash", SetLastError = true)] [DllImport("librhash", SetLastError = true)]
static extern void rhash_free(IntPtr ctx); static extern void rhash_free(IntPtr ctx);
public static void Crc32() public static void Crc32()
{ {
byte[] data = new byte[1048576]; byte[] data = new byte[1048576];
byte[] hash = new byte[4]; byte[] hash = new byte[4];
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();
rhash_library_init(); rhash_library_init();
IntPtr ctx = rhash_init(RHASH_CRC32); IntPtr ctx = rhash_init(RHASH_CRC32);
if(ctx == IntPtr.Zero) if(ctx == IntPtr.Zero)
throw new Exception("Could not initialize digest"); throw new Exception("Could not initialize digest");
int ret = rhash_update(ctx, data, (ulong)data.Length); int ret = rhash_update(ctx, data, (ulong)data.Length);
if(ret != 0) if(ret != 0)
throw new Exception("Could not digest block"); throw new Exception("Could not digest block");
ret = rhash_final(ctx, hash); ret = rhash_final(ctx, hash);
if(ret != 0) if(ret != 0)
throw new Exception("Could not finalize hash"); throw new Exception("Could not finalize hash");
rhash_free(ctx); rhash_free(ctx);
if(hash.Where((t, i) => t != _expectedRandomCrc32[i]).Any()) if(hash.Where((t, i) => t != _expectedRandomCrc32[i]).Any())
throw new Exception("Invalid hash value"); throw new Exception("Invalid hash value");
} }
public static void Md5() public static void Md5()
{ {
byte[] data = new byte[1048576]; byte[] data = new byte[1048576];
byte[] hash = new byte[16]; byte[] hash = new byte[16];
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();
rhash_library_init(); rhash_library_init();
IntPtr ctx = rhash_init(RHASH_MD5); IntPtr ctx = rhash_init(RHASH_MD5);
if(ctx == IntPtr.Zero) if(ctx == IntPtr.Zero)
throw new Exception("Could not initialize digest"); throw new Exception("Could not initialize digest");
int ret = rhash_update(ctx, data, (ulong)data.Length); int ret = rhash_update(ctx, data, (ulong)data.Length);
if(ret != 0) if(ret != 0)
throw new Exception("Could not digest block"); throw new Exception("Could not digest block");
ret = rhash_final(ctx, hash); ret = rhash_final(ctx, hash);
if(ret != 0) if(ret != 0)
throw new Exception("Could not finalize hash"); throw new Exception("Could not finalize hash");
rhash_free(ctx); rhash_free(ctx);
if(hash.Where((t, i) => t != _expectedRandomMd5[i]).Any()) if(hash.Where((t, i) => t != _expectedRandomMd5[i]).Any())
throw new Exception("Invalid hash value"); throw new Exception("Invalid hash value");
} }
public static void Sha1() public static void Sha1()
{ {
byte[] data = new byte[1048576]; byte[] data = new byte[1048576];
byte[] hash = new byte[20]; byte[] hash = new byte[20];
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();
rhash_library_init(); rhash_library_init();
IntPtr ctx = rhash_init(RHASH_SHA1); IntPtr ctx = rhash_init(RHASH_SHA1);
if(ctx == IntPtr.Zero) if(ctx == IntPtr.Zero)
throw new Exception("Could not initialize digest"); throw new Exception("Could not initialize digest");
int ret = rhash_update(ctx, data, (ulong)data.Length); int ret = rhash_update(ctx, data, (ulong)data.Length);
if(ret != 0) if(ret != 0)
throw new Exception("Could not digest block"); throw new Exception("Could not digest block");
ret = rhash_final(ctx, hash); ret = rhash_final(ctx, hash);
if(ret != 0) if(ret != 0)
throw new Exception("Could not finalize hash"); throw new Exception("Could not finalize hash");
rhash_free(ctx); rhash_free(ctx);
if(hash.Where((t, i) => t != _expectedRandomSha1[i]).Any()) if(hash.Where((t, i) => t != _expectedRandomSha1[i]).Any())
throw new Exception("Invalid hash value"); throw new Exception("Invalid hash value");
} }
public static void Sha256() public static void Sha256()
{ {
byte[] data = new byte[1048576]; byte[] data = new byte[1048576];
byte[] hash = new byte[32]; byte[] hash = new byte[32];
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();
rhash_library_init(); rhash_library_init();
IntPtr ctx = rhash_init(RHASH_SHA256); IntPtr ctx = rhash_init(RHASH_SHA256);
if(ctx == IntPtr.Zero) if(ctx == IntPtr.Zero)
throw new Exception("Could not initialize digest"); throw new Exception("Could not initialize digest");
int ret = rhash_update(ctx, data, (ulong)data.Length); int ret = rhash_update(ctx, data, (ulong)data.Length);
if(ret != 0) if(ret != 0)
throw new Exception("Could not digest block"); throw new Exception("Could not digest block");
ret = rhash_final(ctx, hash); ret = rhash_final(ctx, hash);
if(ret != 0) if(ret != 0)
throw new Exception("Could not finalize hash"); throw new Exception("Could not finalize hash");
rhash_free(ctx); rhash_free(ctx);
if(hash.Where((t, i) => t != _expectedRandomSha256[i]).Any()) if(hash.Where((t, i) => t != _expectedRandomSha256[i]).Any())
throw new Exception("Invalid hash value"); throw new Exception("Invalid hash value");
} }
public static void Sha384() public static void Sha384()
{ {
byte[] data = new byte[1048576]; byte[] data = new byte[1048576];
byte[] hash = new byte[48]; byte[] hash = new byte[48];
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();
rhash_library_init(); rhash_library_init();
IntPtr ctx = rhash_init(RHASH_SHA384); IntPtr ctx = rhash_init(RHASH_SHA384);
if(ctx == IntPtr.Zero) if(ctx == IntPtr.Zero)
throw new Exception("Could not initialize digest"); throw new Exception("Could not initialize digest");
int ret = rhash_update(ctx, data, (ulong)data.Length); int ret = rhash_update(ctx, data, (ulong)data.Length);
if(ret != 0) if(ret != 0)
throw new Exception("Could not digest block"); throw new Exception("Could not digest block");
ret = rhash_final(ctx, hash); ret = rhash_final(ctx, hash);
if(ret != 0) if(ret != 0)
throw new Exception("Could not finalize hash"); throw new Exception("Could not finalize hash");
rhash_free(ctx); rhash_free(ctx);
if(hash.Where((t, i) => t != _expectedRandomSha384[i]).Any()) if(hash.Where((t, i) => t != _expectedRandomSha384[i]).Any())
throw new Exception("Invalid hash value"); throw new Exception("Invalid hash value");
} }
public static void Sha512() public static void Sha512()
{ {
byte[] data = new byte[1048576]; byte[] data = new byte[1048576];
byte[] hash = new byte[64]; byte[] hash = new byte[64];
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();
rhash_library_init(); rhash_library_init();
IntPtr ctx = rhash_init(RHASH_SHA512); IntPtr ctx = rhash_init(RHASH_SHA512);
if(ctx == IntPtr.Zero) if(ctx == IntPtr.Zero)
throw new Exception("Could not initialize digest"); throw new Exception("Could not initialize digest");
int ret = rhash_update(ctx, data, (ulong)data.Length); int ret = rhash_update(ctx, data, (ulong)data.Length);
if(ret != 0) if(ret != 0)
throw new Exception("Could not digest block"); throw new Exception("Could not digest block");
ret = rhash_final(ctx, hash); ret = rhash_final(ctx, hash);
if(ret != 0) if(ret != 0)
throw new Exception("Could not finalize hash"); throw new Exception("Could not finalize hash");
rhash_free(ctx); rhash_free(ctx);
if(hash.Where((t, i) => t != _expectedRandomSha512[i]).Any()) if(hash.Where((t, i) => t != _expectedRandomSha512[i]).Any())
throw new Exception("Invalid hash value"); throw new Exception("Invalid hash value");
}
} }
} }

View File

@@ -1,134 +1,137 @@
using System.IO;
#if NET8_0_OR_GREATER #if NET8_0_OR_GREATER
using Aaru6.Checksums; using Aaru6.Checksums;
#else #else
using Aaru.Checksums; using Aaru.Checksums;
#endif #endif
using System.IO;
using Aaru.Compression; using Aaru.Compression;
using CUETools.Codecs; using CUETools.Codecs;
using CUETools.Codecs.Flake; using CUETools.Codecs.Flake;
namespace AaruBenchmark.Compression; // ReSharper disable ArrangeNamespaceBody
public class Aaru namespace AaruBenchmark.Compression
{ {
public static void AppleRle() public class Aaru
{ {
const int bufferSize = 20960; public static void AppleRle()
byte[] input = new byte[1102];
var fs = new FileStream(Path.Combine(Program.Folder, "apple_rle.bin"), FileMode.Open, FileAccess.Read);
fs.Read(input, 0, input.Length);
fs.Close();
fs.Dispose();
byte[] output = new byte[bufferSize];
var rle = new AppleRle(new MemoryStream(input));
for(int i = 0; i < bufferSize; i++)
output[i] = (byte)rle.ProduceByte();
string crc = Crc32Context.Data(output, out _);
if(crc != "3525ef06")
throw new InvalidDataException("Incorrect decompressed checksum");
}
public static void TeleDiskLzh()
{
const int bufsz = 512;
byte[] input = new byte[9040];
var fs = new FileStream(Path.Combine(Program.Folder, "teledisk_lzh.bin"), FileMode.Open, FileAccess.Read);
fs.Read(input, 0, input.Length);
fs.Close();
fs.Dispose();
int rd;
int total_rd = 0;
var lzh = new TeleDiskLzh(new MemoryStream(input));
var outMs = new MemoryStream();
do
{ {
if((rd = lzh.Decode(out byte[] obuf, bufsz)) > 0) const int bufferSize = 20960;
outMs.Write(obuf, 0, rd);
total_rd += rd; byte[] input = new byte[1102];
} while(rd == bufsz);
byte[] output = outMs.ToArray(); var fs = new FileStream(Path.Combine(Program.Folder, "apple_rle.bin"), FileMode.Open, FileAccess.Read);
if(total_rd != 39820) fs.Read(input, 0, input.Length);
throw new InvalidDataException("Incorrect decompressed data"); fs.Close();
fs.Dispose();
if(output.Length != 39820) byte[] output = new byte[bufferSize];
throw new InvalidDataException("Incorrect decompressed data");
string crc = Crc32Context.Data(output, out _); var rle = new AppleRle(new MemoryStream(input));
if(crc != "22bd5d44") for(int i = 0; i < bufferSize; i++)
throw new InvalidDataException("Incorrect decompressed checksum"); output[i] = (byte)rle.ProduceByte();
}
public static void Flac() string crc = Crc32Context.Data(output, out _);
{
var flacMs = new FileStream(Path.Combine(Program.Folder, "flac.flac"), FileMode.Open, FileAccess.Read);
var flakeReader = new AudioDecoder(new DecoderSettings(), "", flacMs);
byte[] block = new byte[9633792];
int samples = block.Length / 2352 * 588;
var audioBuffer = new AudioBuffer(AudioPCMConfig.RedBook, block, samples);
flakeReader.Read(audioBuffer, samples);
flakeReader.Close();
flacMs.Close();
string crc = Crc32Context.Data(block, out _); if(crc != "3525ef06")
throw new InvalidDataException("Incorrect decompressed checksum");
}
if(crc != "dfbc99bb") public static void TeleDiskLzh()
throw new InvalidDataException("Incorrect decompressed checksum");
}
public static void CompressFlac()
{
var dataStream = new FileStream(Path.Combine(Program.Folder, "audio.bin"), FileMode.Open, FileAccess.Read);
byte[] decompressed = new byte[9633792];
dataStream.Read(decompressed, 0, decompressed.Length);
dataStream.Close();
byte[] backendBuffer = new byte[9633792];
var flakeWriterSettings = new EncoderSettings
{ {
PCM = AudioPCMConfig.RedBook, const int bufsz = 512;
DoMD5 = false,
BlockSize = 4608,
MinFixedOrder = 0,
MaxFixedOrder = 4,
MinLPCOrder = 1,
MaxLPCOrder = 32,
MaxPartitionOrder = 8,
StereoMethod = StereoMethod.Evaluate,
PredictionType = PredictionType.Search,
WindowMethod = WindowMethod.EvaluateN,
EstimationDepth = 5,
MinPrecisionSearch = 1,
MaxPrecisionSearch = 1,
TukeyParts = 0,
TukeyOverlap = 1.0,
TukeyP = 1.0,
AllowNonSubset = true
};
var flakeWriter = new AudioEncoder(flakeWriterSettings, "", new MemoryStream(backendBuffer)) byte[] input = new byte[9040];
var fs = new FileStream(Path.Combine(Program.Folder, "teledisk_lzh.bin"), FileMode.Open, FileAccess.Read);
fs.Read(input, 0, input.Length);
fs.Close();
fs.Dispose();
int rd;
int total_rd = 0;
var lzh = new TeleDiskLzh(new MemoryStream(input));
var outMs = new MemoryStream();
do
{
if((rd = lzh.Decode(out byte[] obuf, bufsz)) > 0)
outMs.Write(obuf, 0, rd);
total_rd += rd;
} while(rd == bufsz);
byte[] output = outMs.ToArray();
if(total_rd != 39820)
throw new InvalidDataException("Incorrect decompressed data");
if(output.Length != 39820)
throw new InvalidDataException("Incorrect decompressed data");
string crc = Crc32Context.Data(output, out _);
if(crc != "22bd5d44")
throw new InvalidDataException("Incorrect decompressed checksum");
}
public static void Flac()
{ {
DoSeekTable = false var flacMs = new FileStream(Path.Combine(Program.Folder, "flac.flac"), FileMode.Open, FileAccess.Read);
}; var flakeReader = new AudioDecoder(new DecoderSettings(), "", flacMs);
byte[] block = new byte[9633792];
int samples = block.Length / 2352 * 588;
var audioBuffer = new AudioBuffer(AudioPCMConfig.RedBook, block, samples);
flakeReader.Read(audioBuffer, samples);
flakeReader.Close();
flacMs.Close();
var audioBuffer = new AudioBuffer(AudioPCMConfig.RedBook, decompressed, 2408448); string crc = Crc32Context.Data(block, out _);
flakeWriter.Write(audioBuffer);
if(crc != "dfbc99bb")
throw new InvalidDataException("Incorrect decompressed checksum");
}
public static void CompressFlac()
{
var dataStream = new FileStream(Path.Combine(Program.Folder, "audio.bin"), FileMode.Open, FileAccess.Read);
byte[] decompressed = new byte[9633792];
dataStream.Read(decompressed, 0, decompressed.Length);
dataStream.Close();
byte[] backendBuffer = new byte[9633792];
var flakeWriterSettings = new EncoderSettings
{
PCM = AudioPCMConfig.RedBook,
DoMD5 = false,
BlockSize = 4608,
MinFixedOrder = 0,
MaxFixedOrder = 4,
MinLPCOrder = 1,
MaxLPCOrder = 32,
MaxPartitionOrder = 8,
StereoMethod = StereoMethod.Evaluate,
PredictionType = PredictionType.Search,
WindowMethod = WindowMethod.EvaluateN,
EstimationDepth = 5,
MinPrecisionSearch = 1,
MaxPrecisionSearch = 1,
TukeyParts = 0,
TukeyOverlap = 1.0,
TukeyP = 1.0,
AllowNonSubset = true
};
var flakeWriter = new AudioEncoder(flakeWriterSettings, "", new MemoryStream(backendBuffer))
{
DoSeekTable = false
};
var audioBuffer = new AudioBuffer(AudioPCMConfig.RedBook, decompressed, 2408448);
flakeWriter.Write(audioBuffer);
}
} }
} }

View File

@@ -2,159 +2,162 @@ using System.IO;
using Ionic.BZip2; using Ionic.BZip2;
using Ionic.Zlib; using Ionic.Zlib;
namespace AaruBenchmark.Compression; // ReSharper disable ArrangeNamespaceBody
public static class DotNetZip namespace AaruBenchmark.Compression
{ {
public static void Gzip() public static class DotNetZip
{ {
var _dataStream = new FileStream(Path.Combine(Program.Folder, "gzip.gz"), FileMode.Open, FileAccess.Read); public static void Gzip()
Stream str = new GZipStream(_dataStream, CompressionMode.Decompress, true);
byte[] compressed = new byte[1048576];
int pos = 0;
int left = 1048576;
bool oneZero = false;
while(left > 0)
{ {
int done = str.Read(compressed, pos, left); var _dataStream = new FileStream(Path.Combine(Program.Folder, "gzip.gz"), FileMode.Open, FileAccess.Read);
Stream str = new GZipStream(_dataStream, CompressionMode.Decompress, true);
byte[] compressed = new byte[1048576];
int pos = 0;
int left = 1048576;
bool oneZero = false;
if(done == 0) while(left > 0)
{ {
if(oneZero) int done = str.Read(compressed, pos, left);
throw new IOException("Could not read the file!");
oneZero = true; if(done == 0)
{
if(oneZero)
throw new IOException("Could not read the file!");
oneZero = true;
}
left -= done;
pos += done;
} }
left -= done; str.Close();
pos += done; str.Dispose();
} }
str.Close(); public static void CompressGzip()
str.Dispose();
}
public static void CompressGzip()
{
var dataStream = new FileStream(Path.Combine(Program.Folder, "data.bin"), FileMode.Open, FileAccess.Read);
byte[] decompressed = new byte[8388608];
dataStream.Read(decompressed, 0, decompressed.Length);
dataStream.Close();
var cmpMs = new MemoryStream();
Stream cmpStream = new GZipStream(cmpMs, CompressionMode.Compress, true);
cmpStream.Write(decompressed, 0, decompressed.Length);
cmpStream.Close();
cmpMs.Position = 0;
/* This is just to test integrity, disabled for benchmarking
Stream str = new GZipStream(cmpMs, CompressionMode.Decompress, true);
byte[] compressed = new byte[decompressed.Length];
int pos = 0;
int left = compressed.Length;
bool oneZero = false;
while(left > 0)
{ {
int done = str.Read(compressed, pos, left); var dataStream = new FileStream(Path.Combine(Program.Folder, "data.bin"), FileMode.Open, FileAccess.Read);
byte[] decompressed = new byte[8388608];
dataStream.Read(decompressed, 0, decompressed.Length);
dataStream.Close();
if(done == 0) var cmpMs = new MemoryStream();
Stream cmpStream = new GZipStream(cmpMs, CompressionMode.Compress, true);
cmpStream.Write(decompressed, 0, decompressed.Length);
cmpStream.Close();
cmpMs.Position = 0;
/* This is just to test integrity, disabled for benchmarking
Stream str = new GZipStream(cmpMs, CompressionMode.Decompress, true);
byte[] compressed = new byte[decompressed.Length];
int pos = 0;
int left = compressed.Length;
bool oneZero = false;
while(left > 0)
{ {
if(oneZero) int done = str.Read(compressed, pos, left);
throw new IOException("Could not read the file!");
oneZero = true; if(done == 0)
{
if(oneZero)
throw new IOException("Could not read the file!");
oneZero = true;
}
left -= done;
pos += done;
} }
left -= done; str.Close();
pos += done; str.Dispose();
string newCrc = Crc32Context.Data(compressed, (uint)compressed.Length, out _);
if(newCrc != "954bf76e")
throw new InvalidDataException("Incorrect decompressed checksum");
*/
} }
str.Close(); public static void Bzip2()
str.Dispose();
string newCrc = Crc32Context.Data(compressed, (uint)compressed.Length, out _);
if(newCrc != "954bf76e")
throw new InvalidDataException("Incorrect decompressed checksum");
*/
}
public static void Bzip2()
{
var _dataStream = new FileStream(Path.Combine(Program.Folder, "bzip2.bz2"), FileMode.Open, FileAccess.Read);
Stream str = new BZip2InputStream(_dataStream, true);
byte[] compressed = new byte[1048576];
int pos = 0;
int left = 1048576;
bool oneZero = false;
while(left > 0)
{ {
int done = str.Read(compressed, pos, left); var _dataStream = new FileStream(Path.Combine(Program.Folder, "bzip2.bz2"), FileMode.Open, FileAccess.Read);
Stream str = new BZip2InputStream(_dataStream, true);
byte[] compressed = new byte[1048576];
int pos = 0;
int left = 1048576;
bool oneZero = false;
if(done == 0) while(left > 0)
{ {
if(oneZero) int done = str.Read(compressed, pos, left);
throw new IOException("Could not read the file!");
oneZero = true; if(done == 0)
{
if(oneZero)
throw new IOException("Could not read the file!");
oneZero = true;
}
left -= done;
pos += done;
} }
left -= done; str.Close();
pos += done; str.Dispose();
} }
str.Close(); public static void CompressBzip2()
str.Dispose();
}
public static void CompressBzip2()
{
var dataStream = new FileStream(Path.Combine(Program.Folder, "data.bin"), FileMode.Open, FileAccess.Read);
byte[] decompressed = new byte[8388608];
dataStream.Read(decompressed, 0, decompressed.Length);
dataStream.Close();
var cmpMs = new MemoryStream();
Stream cmpStream = new BZip2OutputStream(cmpMs, 9, true);
cmpStream.Write(decompressed, 0, decompressed.Length);
cmpStream.Close();
cmpMs.Position = 0;
/* This is just to test integrity, disabled for benchmarking
Stream str = new BZip2InputStream(cmpMs, true);
byte[] compressed = new byte[decompressed.Length];
int pos = 0;
int left = compressed.Length;
bool oneZero = false;
while(left > 0)
{ {
int done = str.Read(compressed, pos, left); var dataStream = new FileStream(Path.Combine(Program.Folder, "data.bin"), FileMode.Open, FileAccess.Read);
byte[] decompressed = new byte[8388608];
dataStream.Read(decompressed, 0, decompressed.Length);
dataStream.Close();
if(done == 0) var cmpMs = new MemoryStream();
Stream cmpStream = new BZip2OutputStream(cmpMs, 9, true);
cmpStream.Write(decompressed, 0, decompressed.Length);
cmpStream.Close();
cmpMs.Position = 0;
/* This is just to test integrity, disabled for benchmarking
Stream str = new BZip2InputStream(cmpMs, true);
byte[] compressed = new byte[decompressed.Length];
int pos = 0;
int left = compressed.Length;
bool oneZero = false;
while(left > 0)
{ {
if(oneZero) int done = str.Read(compressed, pos, left);
throw new IOException("Could not read the file!");
oneZero = true; if(done == 0)
{
if(oneZero)
throw new IOException("Could not read the file!");
oneZero = true;
}
left -= done;
pos += done;
} }
left -= done; str.Close();
pos += done; str.Dispose();
string newCrc = Crc32Context.Data(compressed, (uint)compressed.Length, out _);
if(newCrc != "954bf76e")
throw new InvalidDataException("Incorrect decompressed checksum");
*/
} }
str.Close();
str.Dispose();
string newCrc = Crc32Context.Data(compressed, (uint)compressed.Length, out _);
if(newCrc != "954bf76e")
throw new InvalidDataException("Incorrect decompressed checksum");
*/
} }
} }

View File

@@ -1,83 +1,86 @@
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
namespace AaruBenchmark.Compression; // ReSharper disable ArrangeNamespaceBody
public static class NetRuntime namespace AaruBenchmark.Compression
{ {
public static void Gzip() public static class NetRuntime
{ {
var _dataStream = new FileStream(Path.Combine(Program.Folder, "gzip.gz"), FileMode.Open, FileAccess.Read); public static void Gzip()
Stream str = new GZipStream(_dataStream, CompressionMode.Decompress, true);
byte[] compressed = new byte[1048576];
int pos = 0;
int left = 1048576;
bool oneZero = false;
while(left > 0)
{ {
int done = str.Read(compressed, pos, left); var _dataStream = new FileStream(Path.Combine(Program.Folder, "gzip.gz"), FileMode.Open, FileAccess.Read);
Stream str = new GZipStream(_dataStream, CompressionMode.Decompress, true);
byte[] compressed = new byte[1048576];
int pos = 0;
int left = 1048576;
bool oneZero = false;
if(done == 0) while(left > 0)
{ {
if(oneZero) int done = str.Read(compressed, pos, left);
throw new IOException("Could not read the file!");
oneZero = true; if(done == 0)
{
if(oneZero)
throw new IOException("Could not read the file!");
oneZero = true;
}
left -= done;
pos += done;
} }
left -= done; str.Close();
pos += done; str.Dispose();
} }
str.Close(); public static void CompressGzip()
str.Dispose();
}
public static void CompressGzip()
{
var dataStream = new FileStream(Path.Combine(Program.Folder, "data.bin"), FileMode.Open, FileAccess.Read);
byte[] decompressed = new byte[8388608];
dataStream.Read(decompressed, 0, decompressed.Length);
dataStream.Close();
var cmpMs = new MemoryStream();
Stream cmpStream = new GZipStream(cmpMs, CompressionMode.Compress, true);
cmpStream.Write(decompressed, 0, decompressed.Length);
cmpStream.Close();
cmpMs.Position = 0;
/* This is just to test integrity, disabled for benchmarking
Stream str = new GZipStream(cmpMs, CompressionMode.Decompress, true);
byte[] compressed = new byte[decompressed.Length];
int pos = 0;
int left = compressed.Length;
bool oneZero = false;
while(left > 0)
{ {
int done = str.Read(compressed, pos, left); var dataStream = new FileStream(Path.Combine(Program.Folder, "data.bin"), FileMode.Open, FileAccess.Read);
byte[] decompressed = new byte[8388608];
dataStream.Read(decompressed, 0, decompressed.Length);
dataStream.Close();
if(done == 0) var cmpMs = new MemoryStream();
Stream cmpStream = new GZipStream(cmpMs, CompressionMode.Compress, true);
cmpStream.Write(decompressed, 0, decompressed.Length);
cmpStream.Close();
cmpMs.Position = 0;
/* This is just to test integrity, disabled for benchmarking
Stream str = new GZipStream(cmpMs, CompressionMode.Decompress, true);
byte[] compressed = new byte[decompressed.Length];
int pos = 0;
int left = compressed.Length;
bool oneZero = false;
while(left > 0)
{ {
if(oneZero) int done = str.Read(compressed, pos, left);
throw new IOException("Could not read the file!");
oneZero = true; if(done == 0)
{
if(oneZero)
throw new IOException("Could not read the file!");
oneZero = true;
}
left -= done;
pos += done;
} }
left -= done; str.Close();
pos += done; str.Dispose();
string newCrc = Crc32Context.Data(compressed, (uint)compressed.Length, out _);
if(newCrc != "954bf76e")
throw new InvalidDataException("Incorrect decompressed checksum");
*/
} }
str.Close();
str.Dispose();
string newCrc = Crc32Context.Data(compressed, (uint)compressed.Length, out _);
if(newCrc != "954bf76e")
throw new InvalidDataException("Incorrect decompressed checksum");
*/
} }
} }

View File

@@ -1,356 +1,359 @@
using System.IO; using System.IO;
#if NET8_0_OR_GREATER
using Aaru6.Checksums;
#else
using Aaru.Checksums;
#endif
using SharpCompress.Compressors; using SharpCompress.Compressors;
using SharpCompress.Compressors.ADC; using SharpCompress.Compressors.ADC;
using SharpCompress.Compressors.BZip2; using SharpCompress.Compressors.BZip2;
using SharpCompress.Compressors.Deflate; using SharpCompress.Compressors.Deflate;
using SharpCompress.Compressors.LZMA; using SharpCompress.Compressors.LZMA;
#if NET8_0_OR_GREATER
using Aaru6.Checksums;
#else
using Aaru.Checksums;
#endif
namespace AaruBenchmark.Compression; // ReSharper disable ArrangeNamespaceBody
public static class SharpCompress namespace AaruBenchmark.Compression
{ {
public static void Gzip() public static class SharpCompress
{ {
var _dataStream = new FileStream(Path.Combine(Program.Folder, "gzip.gz"), FileMode.Open, FileAccess.Read); public static void Gzip()
Stream str = new GZipStream(_dataStream, CompressionMode.Decompress);
byte[] compressed = new byte[1048576];
int pos = 0;
int left = 1048576;
bool oneZero = false;
while(left > 0)
{ {
int done = str.Read(compressed, pos, left); var _dataStream = new FileStream(Path.Combine(Program.Folder, "gzip.gz"), FileMode.Open, FileAccess.Read);
Stream str = new GZipStream(_dataStream, CompressionMode.Decompress);
byte[] compressed = new byte[1048576];
int pos = 0;
int left = 1048576;
bool oneZero = false;
if(done == 0) while(left > 0)
{ {
if(oneZero) int done = str.Read(compressed, pos, left);
throw new IOException("Could not read the file!");
oneZero = true; if(done == 0)
{
if(oneZero)
throw new IOException("Could not read the file!");
oneZero = true;
}
left -= done;
pos += done;
} }
left -= done; str.Close();
pos += done; str.Dispose();
} }
str.Close(); public static void CompressGzip()
str.Dispose();
}
public static void CompressGzip()
{
var dataStream = new FileStream(Path.Combine(Program.Folder, "data.bin"), FileMode.Open, FileAccess.Read);
byte[] decompressed = new byte[8388608];
dataStream.Read(decompressed, 0, decompressed.Length);
dataStream.Close();
byte[] backendBuffer = new byte[8388608];
Stream cmpStream = new GZipStream(new MemoryStream(backendBuffer), CompressionMode.Compress,
CompressionLevel.Level9);
cmpStream.Write(decompressed, 0, decompressed.Length);
cmpStream.Close();
/* This is just to test integrity, disabled for benchmarking
Stream str = new GZipStream(new MemoryStream(backendBuffer), CompressionMode.Decompress);
byte[] compressed = new byte[decompressed.Length];
int pos = 0;
int left = compressed.Length;
bool oneZero = false;
while(left > 0)
{ {
int done = str.Read(compressed, pos, left); var dataStream = new FileStream(Path.Combine(Program.Folder, "data.bin"), FileMode.Open, FileAccess.Read);
byte[] decompressed = new byte[8388608];
dataStream.Read(decompressed, 0, decompressed.Length);
dataStream.Close();
byte[] backendBuffer = new byte[8388608];
if(done == 0) Stream cmpStream = new GZipStream(new MemoryStream(backendBuffer), CompressionMode.Compress,
CompressionLevel.Level9);
cmpStream.Write(decompressed, 0, decompressed.Length);
cmpStream.Close();
/* This is just to test integrity, disabled for benchmarking
Stream str = new GZipStream(new MemoryStream(backendBuffer), CompressionMode.Decompress);
byte[] compressed = new byte[decompressed.Length];
int pos = 0;
int left = compressed.Length;
bool oneZero = false;
while(left > 0)
{ {
if(oneZero) int done = str.Read(compressed, pos, left);
throw new IOException("Could not read the file!");
oneZero = true; if(done == 0)
{
if(oneZero)
throw new IOException("Could not read the file!");
oneZero = true;
}
left -= done;
pos += done;
} }
left -= done; string newCrc = Crc32Context.Data(compressed, (uint)compressed.Length, out _);
pos += done;
if(newCrc != "954bf76e")
throw new InvalidDataException("Incorrect decompressed checksum");
*/
} }
string newCrc = Crc32Context.Data(compressed, (uint)compressed.Length, out _); public static void Bzip2()
if(newCrc != "954bf76e")
throw new InvalidDataException("Incorrect decompressed checksum");
*/
}
public static void Bzip2()
{
var _dataStream = new FileStream(Path.Combine(Program.Folder, "bzip2.bz2"), FileMode.Open, FileAccess.Read);
Stream str = new BZip2Stream(_dataStream, CompressionMode.Decompress, true);
byte[] compressed = new byte[1048576];
int pos = 0;
int left = 1048576;
bool oneZero = false;
while(left > 0)
{ {
int done = str.Read(compressed, pos, left); var _dataStream = new FileStream(Path.Combine(Program.Folder, "bzip2.bz2"), FileMode.Open, FileAccess.Read);
Stream str = new BZip2Stream(_dataStream, CompressionMode.Decompress, true);
byte[] compressed = new byte[1048576];
int pos = 0;
int left = 1048576;
bool oneZero = false;
if(done == 0) while(left > 0)
{ {
if(oneZero) int done = str.Read(compressed, pos, left);
throw new IOException("Could not read the file!");
oneZero = true; if(done == 0)
{
if(oneZero)
throw new IOException("Could not read the file!");
oneZero = true;
}
left -= done;
pos += done;
} }
left -= done; str.Close();
pos += done; str.Dispose();
} }
str.Close(); public static void CompressBzip2()
str.Dispose();
}
public static void CompressBzip2()
{
var dataStream = new FileStream(Path.Combine(Program.Folder, "data.bin"), FileMode.Open, FileAccess.Read);
byte[] decompressed = new byte[8388608];
dataStream.Read(decompressed, 0, decompressed.Length);
dataStream.Close();
byte[] backendBuffer = new byte[8388608];
Stream cmpStream = new BZip2Stream(new MemoryStream(backendBuffer), CompressionMode.Compress, true);
cmpStream.Write(decompressed, 0, decompressed.Length);
cmpStream.Close();
/* This is just to test integrity, disabled for benchmarking
Stream str = new BZip2Stream(new MemoryStream(backendBuffer), CompressionMode.Decompress, false);
byte[] compressed = new byte[decompressed.Length];
int pos = 0;
int left = compressed.Length;
bool oneZero = false;
while(left > 0)
{ {
int done = str.Read(compressed, pos, left); var dataStream = new FileStream(Path.Combine(Program.Folder, "data.bin"), FileMode.Open, FileAccess.Read);
byte[] decompressed = new byte[8388608];
dataStream.Read(decompressed, 0, decompressed.Length);
dataStream.Close();
byte[] backendBuffer = new byte[8388608];
if(done == 0) Stream cmpStream = new BZip2Stream(new MemoryStream(backendBuffer), CompressionMode.Compress, true);
cmpStream.Write(decompressed, 0, decompressed.Length);
cmpStream.Close();
/* This is just to test integrity, disabled for benchmarking
Stream str = new BZip2Stream(new MemoryStream(backendBuffer), CompressionMode.Decompress, false);
byte[] compressed = new byte[decompressed.Length];
int pos = 0;
int left = compressed.Length;
bool oneZero = false;
while(left > 0)
{ {
if(oneZero) int done = str.Read(compressed, pos, left);
throw new IOException("Could not read the file!");
oneZero = true; if(done == 0)
{
if(oneZero)
throw new IOException("Could not read the file!");
oneZero = true;
}
left -= done;
pos += done;
} }
left -= done; string newCrc = Crc32Context.Data(compressed, (uint)compressed.Length, out _);
pos += done;
if(newCrc != "954bf76e")
throw new InvalidDataException("Incorrect decompressed checksum");
*/
} }
string newCrc = Crc32Context.Data(compressed, (uint)compressed.Length, out _); public static void ADC()
if(newCrc != "954bf76e")
throw new InvalidDataException("Incorrect decompressed checksum");
*/
}
public static void ADC()
{
var _dataStream = new FileStream(Path.Combine(Program.Folder, "adc.bin"), FileMode.Open, FileAccess.Read);
Stream str = new ADCStream(_dataStream);
byte[] compressed = new byte[262144];
int pos = 0;
int left = 262144;
bool oneZero = false;
while(left > 0)
{ {
int done = str.Read(compressed, pos, left); var _dataStream = new FileStream(Path.Combine(Program.Folder, "adc.bin"), FileMode.Open, FileAccess.Read);
Stream str = new ADCStream(_dataStream);
byte[] compressed = new byte[262144];
int pos = 0;
int left = 262144;
bool oneZero = false;
if(done == 0) while(left > 0)
{ {
if(oneZero) int done = str.Read(compressed, pos, left);
throw new IOException("Could not read the file!");
oneZero = true; if(done == 0)
{
if(oneZero)
throw new IOException("Could not read the file!");
oneZero = true;
}
left -= done;
pos += done;
} }
left -= done; str.Close();
pos += done; str.Dispose();
string crc = Crc32Context.Data(compressed, 262144, out _);
if(crc != "5a5a7388")
throw new InvalidDataException("Incorrect decompressed checksum");
} }
str.Close(); public static void Lzip()
str.Dispose();
string crc = Crc32Context.Data(compressed, 262144, out _);
if(crc != "5a5a7388")
throw new InvalidDataException("Incorrect decompressed checksum");
}
public static void Lzip()
{
var _dataStream = new FileStream(Path.Combine(Program.Folder, "lzip.lz"), FileMode.Open, FileAccess.Read);
Stream str = new LZipStream(_dataStream, CompressionMode.Decompress);
byte[] compressed = new byte[1048576];
int pos = 0;
int left = 1048576;
bool oneZero = false;
while(left > 0)
{ {
int done = str.Read(compressed, pos, left); var _dataStream = new FileStream(Path.Combine(Program.Folder, "lzip.lz"), FileMode.Open, FileAccess.Read);
Stream str = new LZipStream(_dataStream, CompressionMode.Decompress);
byte[] compressed = new byte[1048576];
int pos = 0;
int left = 1048576;
bool oneZero = false;
if(done == 0) while(left > 0)
{ {
if(oneZero) int done = str.Read(compressed, pos, left);
throw new IOException("Could not read the file!");
oneZero = true; if(done == 0)
{
if(oneZero)
throw new IOException("Could not read the file!");
oneZero = true;
}
left -= done;
pos += done;
} }
left -= done; str.Close();
pos += done; str.Dispose();
string crc = Crc32Context.Data(compressed, 1048576, out _);
if(crc != "c64059c0")
throw new InvalidDataException("Incorrect decompressed checksum");
} }
str.Close(); public static void CompressLzip()
str.Dispose();
string crc = Crc32Context.Data(compressed, 1048576, out _);
if(crc != "c64059c0")
throw new InvalidDataException("Incorrect decompressed checksum");
}
public static void CompressLzip()
{
var dataStream = new FileStream(Path.Combine(Program.Folder, "data.bin"), FileMode.Open, FileAccess.Read);
byte[] decompressed = new byte[8388608];
dataStream.Read(decompressed, 0, decompressed.Length);
dataStream.Close();
byte[] backendBuffer = new byte[8388608];
Stream cmpStream = new LZipStream(new MemoryStream(backendBuffer), CompressionMode.Compress);
cmpStream.Write(decompressed, 0, decompressed.Length);
cmpStream.Close();
/* This is just to test integrity, disabled for benchmarking
Stream str = new LZipStream(new MemoryStream(backendBuffer), CompressionMode.Decompress);
byte[] compressed = new byte[decompressed.Length];
int pos = 0;
int left = compressed.Length;
bool oneZero = false;
while(left > 0)
{ {
int done = str.Read(compressed, pos, left); var dataStream = new FileStream(Path.Combine(Program.Folder, "data.bin"), FileMode.Open, FileAccess.Read);
byte[] decompressed = new byte[8388608];
dataStream.Read(decompressed, 0, decompressed.Length);
dataStream.Close();
byte[] backendBuffer = new byte[8388608];
if(done == 0) Stream cmpStream = new LZipStream(new MemoryStream(backendBuffer), CompressionMode.Compress);
cmpStream.Write(decompressed, 0, decompressed.Length);
cmpStream.Close();
/* This is just to test integrity, disabled for benchmarking
Stream str = new LZipStream(new MemoryStream(backendBuffer), CompressionMode.Decompress);
byte[] compressed = new byte[decompressed.Length];
int pos = 0;
int left = compressed.Length;
bool oneZero = false;
while(left > 0)
{ {
if(oneZero) int done = str.Read(compressed, pos, left);
throw new IOException("Could not read the file!");
oneZero = true; if(done == 0)
{
if(oneZero)
throw new IOException("Could not read the file!");
oneZero = true;
}
left -= done;
pos += done;
} }
left -= done; string newCrc = Crc32Context.Data(compressed, (uint)compressed.Length, out _);
pos += done;
if(newCrc != "954bf76e")
throw new InvalidDataException("Incorrect decompressed checksum");
*/
} }
string newCrc = Crc32Context.Data(compressed, (uint)compressed.Length, out _); public static void Lzma()
if(newCrc != "954bf76e")
throw new InvalidDataException("Incorrect decompressed checksum");
*/
}
public static void Lzma()
{
var _dataStream = new FileStream(Path.Combine(Program.Folder, "lzma.bin"), FileMode.Open, FileAccess.Read);
Stream str = new LzmaStream(new byte[]
{ {
0x5D, 0x00, 0x00, 0x00, 0x02 var _dataStream = new FileStream(Path.Combine(Program.Folder, "lzma.bin"), FileMode.Open, FileAccess.Read);
}, _dataStream);
byte[] compressed = new byte[8388608]; Stream str = new LzmaStream(new byte[]
int pos = 0;
int left = 8388608;
bool oneZero = false;
while(left > 0)
{
int done = str.Read(compressed, pos, left);
if(done == 0)
{ {
if(oneZero) 0x5D, 0x00, 0x00, 0x00, 0x02
throw new IOException("Could not read the file!"); }, _dataStream);
oneZero = true; byte[] compressed = new byte[8388608];
int pos = 0;
int left = 8388608;
bool oneZero = false;
while(left > 0)
{
int done = str.Read(compressed, pos, left);
if(done == 0)
{
if(oneZero)
throw new IOException("Could not read the file!");
oneZero = true;
}
left -= done;
pos += done;
} }
left -= done; str.Close();
pos += done; str.Dispose();
string crc = Crc32Context.Data(compressed, 8388608, out _);
if(crc != "954bf76e")
throw new InvalidDataException("Incorrect decompressed checksum");
} }
str.Close(); public static void CompressLzma()
str.Dispose();
string crc = Crc32Context.Data(compressed, 8388608, out _);
if(crc != "954bf76e")
throw new InvalidDataException("Incorrect decompressed checksum");
}
public static void CompressLzma()
{
var dataStream = new FileStream(Path.Combine(Program.Folder, "data.bin"), FileMode.Open, FileAccess.Read);
byte[] decompressed = new byte[8388608];
dataStream.Read(decompressed, 0, decompressed.Length);
dataStream.Close();
byte[] backendBuffer = new byte[8388608];
var cmpStream = new LzmaStream(new LzmaEncoderProperties(true, 1048576, 273), false,
new MemoryStream(backendBuffer));
byte[] propertiesArray = new byte[cmpStream.Properties.Length];
cmpStream.Properties.CopyTo(propertiesArray, 0);
cmpStream.Write(decompressed, 0, decompressed.Length);
cmpStream.Close();
/* This is just to test integrity, disabled for benchmarking
Stream str = new LzmaStream(propertiesArray, new MemoryStream(backendBuffer));
byte[] compressed = new byte[decompressed.Length];
int pos = 0;
int left = compressed.Length;
bool oneZero = false;
while(left > 0)
{ {
int done = str.Read(compressed, pos, left); var dataStream = new FileStream(Path.Combine(Program.Folder, "data.bin"), FileMode.Open, FileAccess.Read);
byte[] decompressed = new byte[8388608];
dataStream.Read(decompressed, 0, decompressed.Length);
dataStream.Close();
byte[] backendBuffer = new byte[8388608];
if(done == 0) var cmpStream = new LzmaStream(new LzmaEncoderProperties(true, 1048576, 273), false,
new MemoryStream(backendBuffer));
byte[] propertiesArray = new byte[cmpStream.Properties.Length];
cmpStream.Properties.CopyTo(propertiesArray, 0);
cmpStream.Write(decompressed, 0, decompressed.Length);
cmpStream.Close();
/* This is just to test integrity, disabled for benchmarking
Stream str = new LzmaStream(propertiesArray, new MemoryStream(backendBuffer));
byte[] compressed = new byte[decompressed.Length];
int pos = 0;
int left = compressed.Length;
bool oneZero = false;
while(left > 0)
{ {
if(oneZero) int done = str.Read(compressed, pos, left);
throw new IOException("Could not read the file!");
oneZero = true; if(done == 0)
{
if(oneZero)
throw new IOException("Could not read the file!");
oneZero = true;
}
left -= done;
pos += done;
} }
left -= done; string newCrc = Crc32Context.Data(compressed, (uint)compressed.Length, out _);
pos += done;
if(newCrc != "954bf76e")
throw new InvalidDataException("Incorrect decompressed checksum");
*/
} }
string newCrc = Crc32Context.Data(compressed, (uint)compressed.Length, out _);
if(newCrc != "954bf76e")
throw new InvalidDataException("Incorrect decompressed checksum");
*/
} }
} }

View File

@@ -4,42 +4,45 @@ using System.IO;
using BenchmarkDotNet.Configs; using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running; using BenchmarkDotNet.Running;
namespace AaruBenchmark; // ReSharper disable ArrangeNamespaceBody
static class Program namespace AaruBenchmark
{ {
internal static string Folder => Path.Combine(Environment.CurrentDirectory, "data"); static class Program
static void Main(string[] args)
{ {
var config = ManualConfig.Create(DefaultConfig.Instance); internal static string Folder => Path.Combine(Environment.CurrentDirectory, "data");
config.CultureInfo = CultureInfo.InvariantCulture;
BenchmarkRunner.Run<ADCBenchs>(config); static void Main(string[] args)
BenchmarkRunner.Run<AppleRleBenchs>(config); {
BenchmarkRunner.Run<TeleDiskLzhBenchs>(config); var config = ManualConfig.Create(DefaultConfig.Instance);
BenchmarkRunner.Run<GzipBenchs>(config); config.CultureInfo = CultureInfo.InvariantCulture;
BenchmarkRunner.Run<Bzip2Benchs>(config);
BenchmarkRunner.Run<LzipBenchs>(config); BenchmarkRunner.Run<ADCBenchs>(config);
BenchmarkRunner.Run<LzmaBenchs>(config); BenchmarkRunner.Run<AppleRleBenchs>(config);
BenchmarkRunner.Run<FlacBenchs>(config); BenchmarkRunner.Run<TeleDiskLzhBenchs>(config);
BenchmarkRunner.Run<CompressGzipBenchs>(config); BenchmarkRunner.Run<GzipBenchs>(config);
BenchmarkRunner.Run<CompressBzip2Benchs>(config); BenchmarkRunner.Run<Bzip2Benchs>(config);
BenchmarkRunner.Run<CompressLzipBenchs>(config); BenchmarkRunner.Run<LzipBenchs>(config);
BenchmarkRunner.Run<CompressLzmaBenchs>(config); BenchmarkRunner.Run<LzmaBenchs>(config);
BenchmarkRunner.Run<CompressFlacBenchs>(config); BenchmarkRunner.Run<FlacBenchs>(config);
BenchmarkRunner.Run<Adler32Benchs>(config); BenchmarkRunner.Run<CompressGzipBenchs>(config);
BenchmarkRunner.Run<Crc16CcittBenchs>(config); BenchmarkRunner.Run<CompressBzip2Benchs>(config);
BenchmarkRunner.Run<Crc16Benchs>(config); BenchmarkRunner.Run<CompressLzipBenchs>(config);
BenchmarkRunner.Run<Crc32Benchs>(config); BenchmarkRunner.Run<CompressLzmaBenchs>(config);
BenchmarkRunner.Run<Crc64Benchs>(config); BenchmarkRunner.Run<CompressFlacBenchs>(config);
BenchmarkRunner.Run<Fletcher16Benchs>(config); BenchmarkRunner.Run<Adler32Benchs>(config);
BenchmarkRunner.Run<Fletcher32Benchs>(config); BenchmarkRunner.Run<Crc16CcittBenchs>(config);
BenchmarkRunner.Run<Md5Benchs>(config); BenchmarkRunner.Run<Crc16Benchs>(config);
BenchmarkRunner.Run<Sha1Benchs>(config); BenchmarkRunner.Run<Crc32Benchs>(config);
BenchmarkRunner.Run<Sha256Benchs>(config); BenchmarkRunner.Run<Crc64Benchs>(config);
BenchmarkRunner.Run<Sha384Benchs>(config); BenchmarkRunner.Run<Fletcher16Benchs>(config);
BenchmarkRunner.Run<Sha512Benchs>(config); BenchmarkRunner.Run<Fletcher32Benchs>(config);
BenchmarkRunner.Run<SpamSumBenchs>(config); BenchmarkRunner.Run<Md5Benchs>(config);
BenchmarkRunner.Run<Sha1Benchs>(config);
BenchmarkRunner.Run<Sha256Benchs>(config);
BenchmarkRunner.Run<Sha384Benchs>(config);
BenchmarkRunner.Run<Sha512Benchs>(config);
BenchmarkRunner.Run<SpamSumBenchs>(config);
}
} }
} }